crdb_core/
server_side_db.rs

1use web_time::SystemTime;
2
3use crate::{
4    CanDoCallbacks, Db, EventId, Object, ObjectData, ObjectId, Query, Session, SessionRef,
5    SessionToken, TypeId, Update, Updatedness, User,
6};
7use std::{collections::HashSet, pin::Pin, sync::Arc};
8
9pub struct ReadPermsChanges {
10    pub object_id: ObjectId,
11    pub type_id: TypeId,
12    pub lost_read: HashSet<User>,
13    pub gained_read: HashSet<User>,
14}
15
16pub struct UsersWhoCanRead<L> {
17    pub users: HashSet<User>,
18    pub depends_on: Vec<ObjectId>,
19    pub locks: Vec<L>,
20}
21
22pub trait ServerSideDb: 'static + waaaa::Send + waaaa::Sync + Db {
23    type Connection: waaaa::Send;
24    type Transaction<'a>: waaaa::Send;
25    type Lock<'a>: waaaa::Send;
26
27    // TODO(blocked): replace with -> impl once https://github.com/rust-lang/rust/issues/100013 is fixed
28    // This will also remove the clippy lint
29    #[allow(clippy::type_complexity)]
30    fn get_users_who_can_read<'a, 'ret: 'a, T: Object, C: CanDoCallbacks>(
31        &'ret self,
32        object_id: ObjectId,
33        object: &'a T,
34        cb: &'a C,
35    ) -> Pin<Box<dyn 'a + waaaa::Future<Output = anyhow::Result<UsersWhoCanRead<Self::Lock<'ret>>>>>>;
36
37    fn get_transaction(&self) -> impl waaaa::Future<Output = crate::Result<Self::Transaction<'_>>>;
38
39    fn get_latest_snapshot(
40        &self,
41        transaction: &mut Self::Connection,
42        user: User,
43        object_id: ObjectId,
44    ) -> impl waaaa::Future<Output = crate::Result<Arc<serde_json::Value>>>;
45
46    // TODO(test-high): introduce in server-side fuzzer
47    fn get_all(
48        &self,
49        transaction: &mut Self::Connection,
50        user: User,
51        object_id: ObjectId,
52        only_updated_since: Option<Updatedness>,
53    ) -> impl waaaa::Future<Output = crate::Result<ObjectData>>;
54
55    fn server_query(
56        &self,
57        user: User,
58        type_id: TypeId,
59        only_updated_since: Option<Updatedness>,
60        query: Arc<Query>,
61    ) -> impl waaaa::Future<Output = crate::Result<Vec<ObjectId>>>;
62
63    /// Cleans up and optimizes up the database
64    ///
65    /// After running this, the database will reject any new change that would happen before
66    /// `no_new_changes_before` if it is set.
67    fn server_vacuum(
68        &self,
69        no_new_changes_before: Option<EventId>,
70        updatedness: Updatedness,
71        kill_sessions_older_than: Option<SystemTime>,
72        notify_recreation: impl FnMut(Update, HashSet<User>),
73    ) -> impl std::future::Future<Output = crate::Result<()>>;
74
75    /// This function assumes that the lock on `object_id` is already taken
76    ///
77    /// Returns `Some` iff the object actually changed
78    fn recreate_at<'a, T: Object, C: CanDoCallbacks>(
79        &'a self,
80        object_id: ObjectId,
81        event_id: EventId,
82        updatedness: Updatedness,
83        cb: &'a C,
84    ) -> impl 'a + waaaa::Future<Output = crate::Result<Option<(EventId, Arc<T>)>>>;
85
86    fn create_and_return_rdep_changes<T: Object>(
87        &self,
88        object_id: ObjectId,
89        created_at: EventId,
90        object: Arc<T>,
91        updatedness: Updatedness,
92    ) -> impl '_ + waaaa::Future<Output = crate::Result<Option<(Arc<T>, Vec<ReadPermsChanges>)>>>;
93
94    fn submit_and_return_rdep_changes<T: Object>(
95        &self,
96        object_id: ObjectId,
97        event_id: EventId,
98        event: Arc<T::Event>,
99        updatedness: Updatedness,
100    ) -> impl '_ + waaaa::Future<Output = crate::Result<Option<(Arc<T>, Vec<ReadPermsChanges>)>>>;
101
102    fn update_pending_rdeps(&self) -> impl '_ + waaaa::Future<Output = crate::Result<()>>;
103
104    fn login_session(
105        &self,
106        session: Session,
107    ) -> impl '_ + waaaa::Future<Output = crate::Result<(SessionToken, SessionRef)>>;
108
109    fn resume_session(
110        &self,
111        token: SessionToken,
112    ) -> impl '_ + waaaa::Future<Output = crate::Result<Session>>;
113
114    fn mark_session_active(
115        &self,
116        token: SessionToken,
117        at: SystemTime,
118    ) -> impl '_ + waaaa::Future<Output = crate::Result<()>>;
119
120    fn rename_session<'a>(
121        &'a self,
122        token: SessionToken,
123        new_name: &'a str,
124    ) -> impl 'a + waaaa::Future<Output = crate::Result<()>>;
125
126    fn list_sessions(
127        &self,
128        user: User,
129    ) -> impl '_ + waaaa::Future<Output = crate::Result<Vec<Session>>>;
130
131    fn disconnect_session(
132        &self,
133        user: User,
134        session: SessionRef,
135    ) -> impl '_ + waaaa::Future<Output = crate::Result<()>>;
136}