inc_complete/db/
handle.rs1use std::collections::BTreeSet;
2
3use crate::{
4 Cell, Computation, Db, Storage,
5 accumulate::{Accumulate, Accumulated},
6 storage::StorageFor,
7};
8
9use super::DbGet;
10
11pub struct DbHandle<'db, S> {
17 db: &'db Db<S>,
18 current_operation: Cell,
19}
20
21impl<'db, S> DbHandle<'db, S> {
22 pub(crate) fn new(db: &'db Db<S>, current_operation: Cell) -> Self {
23 let mut cell = db.cells.get_mut(¤t_operation).unwrap();
25
26 cell.dependencies.clear();
27 cell.input_dependencies.clear();
28
29 Self {
30 db,
31 current_operation,
32 }
33 }
34
35 pub fn storage(&self) -> &S {
40 self.db.storage()
41 }
42}
43
44impl<S: Storage> DbHandle<'_, S> {
45 pub fn get<C: Computation>(&self, compute: C) -> C::Output
48 where
49 S: StorageFor<C>,
50 {
51 let dependency = self.db.get_or_insert_cell(compute);
53 self.update_and_register_dependency::<C>(dependency);
54
55 self.db.get_with_cell(dependency)
57 }
58
59 fn update_and_register_dependency<C: Computation>(&self, dependency: Cell) {
61 self.update_and_register_dependency_inner(dependency, C::IS_INPUT);
62 }
63
64 fn update_and_register_dependency_inner(&self, dependency: Cell, is_input: bool) {
65 let mut cell = self.db.cells.get_mut(&self.current_operation).unwrap();
66
67 let newly_registered = !cell.dependencies.contains(&dependency);
70 if newly_registered {
71 cell.dependencies.push(dependency);
72 if is_input {
73 cell.input_dependencies.insert(dependency);
74 }
75 }
76 drop(cell);
77
78 self.db.update_cell(dependency);
80
81 if !newly_registered {
82 return;
83 }
84
85 let dependency = self.db.cells.get(&dependency).unwrap();
86 let dependency_inputs = dependency.input_dependencies.clone();
87 drop(dependency);
88
89 if !dependency_inputs.is_empty() {
92 let mut cell = self.db.cells.get_mut(&self.current_operation).unwrap();
93 for input in dependency_inputs {
94 cell.input_dependencies.insert(input);
95 }
96 }
97 }
98
99 pub fn accumulate<Item>(&self, item: Item)
105 where
106 S: Accumulate<Item>,
107 {
108 self.storage().accumulate(self.current_operation, item);
109 }
110
111 pub fn get_accumulated<Item, C>(&self, compute: C) -> BTreeSet<Item>
116 where
117 C: Computation,
118 Item: 'static + Ord,
119 S: StorageFor<Accumulated<Item>> + StorageFor<C> + Accumulate<Item>,
120 {
121 let dependency = self.db.get_or_insert_cell(compute);
122 self.get_accumulated_with_cell::<Item>(dependency)
123 }
124
125 pub(crate) fn get_accumulated_with_cell<Item>(&self, cell_id: Cell) -> BTreeSet<Item>
132 where
133 Item: 'static + Ord,
134 S: StorageFor<Accumulated<Item>> + Accumulate<Item>,
135 {
136 self.update_and_register_dependency_inner(cell_id, false);
137 let dependencies = self.db.with_cell(cell_id, |cell| cell.dependencies.clone());
138
139 let computation_id = Accumulated::<Item>::computation_id();
143 let mut result: BTreeSet<Item> = dependencies
144 .into_iter()
145 .filter(|&dep| self.db.with_cell(dep, |cell| cell.computation_id) != computation_id)
148 .flat_map(|dependency| self.get(Accumulated::<Item>::new(dependency)))
149 .collect();
150
151 result.extend(self.storage().get_accumulated::<Vec<Item>>(cell_id));
152 result
153 }
154}
155
156impl<'db, S, C> DbGet<C> for DbHandle<'db, S>
157where
158 C: Computation,
159 S: Storage + StorageFor<C>,
160{
161 fn get(&self, key: C) -> C::Output {
162 self.get(key)
163 }
164}