crdb_core/
client_side_db.rs

1use crate::{
2    BinPtr, ClientStorageInfo, CrdbSyncFn, Db, EventId, Importance, LoginInfo, Object, ObjectId,
3    Query, QueryId, TypeId, Updatedness, Upload, UploadId,
4};
5use std::{
6    collections::{HashMap, HashSet},
7    sync::Arc,
8};
9
10pub struct SavedObjectMeta {
11    pub type_id: TypeId,
12    pub have_all_until: Option<Updatedness>,
13    pub importance: Importance,
14}
15
16#[derive(Clone)]
17pub struct SavedQuery {
18    pub query: Arc<Query>,
19    pub type_id: TypeId,
20    pub have_all_until: Option<Updatedness>,
21    pub importance: Importance,
22}
23
24impl SavedQuery {
25    pub fn now_have_all_until(&mut self, until: Updatedness) {
26        self.have_all_until = std::cmp::max(self.have_all_until, Some(until));
27    }
28}
29
30pub trait ClientSideDb: 'static + waaaa::Send + waaaa::Sync + Db {
31    fn storage_info(&self) -> impl waaaa::Future<Output = crate::Result<ClientStorageInfo>>;
32
33    fn save_login(&self, _info: LoginInfo) -> impl waaaa::Future<Output = crate::Result<()>>;
34
35    fn get_saved_login(&self) -> impl waaaa::Future<Output = crate::Result<Option<LoginInfo>>>;
36
37    fn get_json(
38        &self,
39        object_id: ObjectId,
40        importance: Importance,
41    ) -> impl waaaa::Future<Output = crate::Result<serde_json::Value>>;
42
43    fn remove_everything(&self) -> impl waaaa::Future<Output = crate::Result<()>>;
44
45    /// Either create an object if it did not exist yet, or recreate it
46    ///
47    /// Returns the new latest snapshot if it actually changed.
48    ///
49    /// `updatedness` is the up-to-date-ness of this recreation, or `None` if it is not known yet
50    /// (eg. it was initiated client-side and has not round-tripped to server yet)
51    fn recreate<T: Object>(
52        &self,
53        object_id: ObjectId,
54        new_created_at: EventId,
55        creation_value: Arc<T>,
56        updatedness: Option<Updatedness>,
57        additional_importance: Importance,
58    ) -> impl waaaa::Future<Output = crate::Result<Option<Arc<T>>>>;
59
60    fn client_query(
61        &self,
62        type_id: TypeId,
63        query: Arc<Query>,
64    ) -> impl waaaa::Future<Output = crate::Result<Vec<ObjectId>>>;
65
66    fn remove(&self, object_id: ObjectId) -> impl waaaa::Future<Output = crate::Result<()>>;
67
68    // TODO(test-high): introduce in db fuzzers
69    fn remove_event<T: Object>(
70        &self,
71        object_id: ObjectId,
72        event_id: EventId,
73    ) -> impl waaaa::Future<Output = crate::Result<()>>;
74
75    fn set_object_importance(
76        &self,
77        object_id: ObjectId,
78        new_importance: Importance,
79    ) -> impl waaaa::Future<Output = crate::Result<()>>;
80
81    fn set_importance_from_queries(
82        &self,
83        object_id: ObjectId,
84        new_importance_from_queries: Importance,
85    ) -> impl waaaa::Future<Output = crate::Result<()>>;
86
87    fn client_vacuum(
88        &self,
89        notify_removals: impl 'static + CrdbSyncFn<ObjectId>,
90        notify_query_removals: impl 'static + CrdbSyncFn<QueryId>,
91    ) -> impl waaaa::Future<Output = crate::Result<()>>;
92
93    fn list_uploads(&self) -> impl waaaa::Future<Output = crate::Result<Vec<UploadId>>>;
94
95    fn get_upload(
96        &self,
97        upload_id: UploadId,
98    ) -> impl waaaa::Future<Output = crate::Result<Option<Upload>>>;
99
100    fn enqueue_upload(
101        &self,
102        upload: Upload,
103        required_binaries: Vec<BinPtr>,
104    ) -> impl waaaa::Future<Output = crate::Result<UploadId>>;
105
106    fn upload_finished(
107        &self,
108        upload_id: UploadId,
109    ) -> impl waaaa::Future<Output = crate::Result<()>>;
110
111    fn get_saved_objects(
112        &self,
113    ) -> impl waaaa::Future<Output = crate::Result<HashMap<ObjectId, SavedObjectMeta>>>;
114
115    fn get_saved_queries(
116        &self,
117    ) -> impl waaaa::Future<Output = crate::Result<HashMap<QueryId, SavedQuery>>>;
118
119    fn record_query(
120        &self,
121        query_id: QueryId,
122        query: Arc<Query>,
123        type_id: TypeId,
124        importance: Importance,
125    ) -> impl waaaa::Future<Output = crate::Result<()>>;
126
127    fn set_query_importance(
128        &self,
129        query_id: QueryId,
130        importance: Importance,
131        objects_matching_query: Vec<ObjectId>,
132    ) -> impl waaaa::Future<Output = crate::Result<()>>;
133
134    fn forget_query(
135        &self,
136        query_id: QueryId,
137        objects_matching_query: Vec<ObjectId>,
138    ) -> impl waaaa::Future<Output = crate::Result<()>>;
139
140    fn update_queries(
141        &self,
142        queries: &HashSet<QueryId>,
143        now_have_all_until: Updatedness,
144    ) -> impl waaaa::Future<Output = crate::Result<()>>;
145}