inc_complete/db/
handle.rs1use petgraph::visit::EdgeRef;
2
3use crate::{
4 Cell, Db, OutputType, Storage,
5 storage::{ComputationId, StorageFor},
6};
7
8pub struct DbHandle<'db, S> {
14 db: &'db mut Db<S>,
15 current_operation: Cell,
16}
17
18impl<'db, S> DbHandle<'db, S> {
19 pub(crate) fn new(db: &'db mut Db<S>, current_operation: Cell) -> Self {
20 let edges = db
22 .cells
23 .edges(current_operation.index())
24 .map(|edge| edge.id())
25 .collect::<Vec<_>>();
26
27 for edge in edges {
28 db.cells.remove_edge(edge);
29 }
30 Self {
31 db,
32 current_operation,
33 }
34 }
35
36 pub fn storage(&self) -> &S {
38 self.db.storage()
39 }
40
41 pub fn storage_mut(&mut self) -> &mut S {
46 self.db.storage_mut()
47 }
48}
49
50impl<'db, S: Storage> DbHandle<'db, S> {
51 pub fn get<C: OutputType + ComputationId>(&mut self, compute: C) -> &C::Output
52 where
53 S: StorageFor<C>,
54 {
55 let dependency = self.db.get_or_insert_cell(compute);
57 self.db
58 .cells
59 .update_edge(self.current_operation.index(), dependency.index(), ());
60
61 self.db.get_with_cell(dependency)
63 }
64}