inc_complete/db/
handle.rs1use crate::{
2 Cell, Db, OutputType, Storage,
3 storage::{ComputationId, StorageFor},
4};
5
6use super::DbGet;
7
8pub struct DbHandle<'db, S> {
14 db: &'db Db<S>,
15 current_operation: Cell,
16}
17
18impl<'db, S> DbHandle<'db, S> {
19 pub(crate) fn new(db: &'db Db<S>, current_operation: Cell) -> Self {
20 let mut cell = db.cells
22 .get_mut(¤t_operation)
23 .unwrap();
24
25 cell.dependencies.clear();
26 cell.input_dependencies.clear();
27
28 Self {
29 db,
30 current_operation,
31 }
32 }
33
34 pub fn storage(&self) -> &S {
39 self.db.storage()
40 }
41}
42
43impl<S: Storage> DbHandle<'_, S> {
44 #[cfg(not(feature = "async"))]
47 pub fn get<C: OutputType + ComputationId>(&self, compute: C) -> C::Output
48 where
49 S: StorageFor<C>,
50 {
51 let dependency = self.db.get_or_insert_cell(compute);
53 let mut cell = self.db.cells.get_mut(&self.current_operation).unwrap();
54
55 cell.dependencies.push(dependency);
60 if C::IS_INPUT {
61 cell.input_dependencies.insert(dependency);
62 }
63
64 drop(cell);
65
66 let output = self.db.get_with_cell(dependency);
68
69 let dependency = self.db.cells.get(&dependency).unwrap();
70 let dependency_inputs = dependency.input_dependencies.clone();
71 drop(dependency);
72
73 let mut cell = self.db.cells.get_mut(&self.current_operation).unwrap();
74 for input in dependency_inputs {
75 cell.input_dependencies.insert(input);
76 }
77
78 output
79 }
80
81 #[cfg(feature = "async")]
82 pub fn get<C: OutputType + ComputationId>(
83 &self,
84 compute: C,
85 ) -> impl Future<Output = C::Output> + Send
86 where
87 S: StorageFor<C> + Sync,
88 {
89 let dependency = self.db.get_or_insert_cell(compute);
91 let mut cell = self.db.cells.get(&self.current_operation).unwrap();
92 cell.dependencies.push(dependency);
93 drop(cell);
94
95 self.db.get_with_cell(dependency)
97 }
98}
99
100#[cfg(not(feature = "async"))]
101impl<'db, S, C> DbGet<C> for DbHandle<'db, S>
102where
103 C: OutputType + ComputationId,
104 S: Storage + StorageFor<C>,
105{
106 fn get(&self, key: C) -> C::Output {
107 self.get(key)
108 }
109}
110
111#[cfg(feature = "async")]
112impl<'db, S, C> DbGet<C> for DbHandle<'db, S>
113where
114 C: OutputType + ComputationId,
115 S: Storage + StorageFor<C> + Sync,
116{
117 fn get(&self, key: C) -> impl Future<Output = C::Output> + Send {
118 self.get(key)
119 }
120}