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 db.cells
22 .get_mut(¤t_operation)
23 .unwrap()
24 .dependencies
25 .clear();
26
27 Self {
28 db,
29 current_operation,
30 }
31 }
32
33 pub fn storage(&self) -> &S {
38 self.db.storage()
39 }
40}
41
42impl<S: Storage> DbHandle<'_, S> {
43 #[cfg(not(feature = "async"))]
46 pub fn get<C: OutputType + ComputationId>(&self, compute: C) -> C::Output
47 where
48 S: StorageFor<C>,
49 {
50 let dependency = self.db.get_or_insert_cell(compute);
52 let mut cell = self.db.cells.get_mut(&self.current_operation).unwrap();
53 cell.dependencies.push(dependency);
54 drop(cell);
55
56 self.db.get_with_cell(dependency)
58 }
59
60 #[cfg(feature = "async")]
61 pub fn get<C: OutputType + ComputationId>(
62 &self,
63 compute: C,
64 ) -> impl Future<Output = C::Output> + Send
65 where
66 S: StorageFor<C> + Sync,
67 {
68 let dependency = self.db.get_or_insert_cell(compute);
70 let mut cell = self.db.cells.get(&self.current_operation).unwrap();
71 cell.dependencies.push(dependency);
72 drop(cell);
73
74 self.db.get_with_cell(dependency)
76 }
77}
78
79#[cfg(not(feature = "async"))]
80impl<'db, S, C> DbGet<C> for DbHandle<'db, S>
81where
82 C: OutputType + ComputationId,
83 S: Storage + StorageFor<C>,
84{
85 fn get(&self, key: C) -> C::Output {
86 self.get(key)
87 }
88}
89
90#[cfg(feature = "async")]
91impl<'db, S, C> DbGet<C> for DbHandle<'db, S>
92where
93 C: OutputType + ComputationId,
94 S: Storage + StorageFor<C> + Sync,
95{
96 fn get(&self, key: C) -> impl Future<Output = C::Output> + Send {
97 self.get(key)
98 }
99}