linkrs_storage/client/
traits_admin.rs1use crate::engine::background_freeze::FreezeStats;
4use linkrs_core::types::{CompactConfig, PasswordInfo, UserAlterInfo, UserInfo};
5use linkrs_core::{Edge, RoleType, StorageError, StorageResult};
6use std::sync::Arc;
7
8use super::StorageOperationContext;
9
10pub trait StorageAuthOps: Send + Sync + std::fmt::Debug {
12 fn change_password(&mut self, info: &PasswordInfo) -> Result<bool, StorageError>;
13 fn create_user(&mut self, info: &UserInfo) -> Result<bool, StorageError>;
14 fn alter_user(&mut self, info: &UserAlterInfo) -> Result<bool, StorageError>;
15 fn drop_user(&mut self, username: &str) -> Result<bool, StorageError>;
16 fn user_exists(&self, username: &str) -> bool;
17 fn list_users(&self) -> Vec<String>;
18 fn get_user(&self, username: &str) -> Option<UserInfo>;
19 fn update_last_login(&self, username: &str) -> Result<bool, StorageError>;
20 fn list_user_roles(&self, username: &str) -> Vec<(i64, RoleType)>;
21 fn list_all_user_roles(&self) -> Vec<(String, Vec<(i64, RoleType)>)>;
22 fn grant_role(
23 &mut self,
24 username: &str,
25 space_id: i64,
26 role: RoleType,
27 ) -> Result<bool, StorageError>;
28 fn revoke_role(&mut self, username: &str, space_id: i64) -> Result<bool, StorageError>;
29}
30
31pub trait StorageAdmin: Send + Sync + std::fmt::Debug {
33 fn load_from_disk(&mut self) -> Result<(), StorageError>;
34 fn save_to_disk(&self) -> Result<(), StorageError>;
35 fn get_storage_stats(&self) -> StorageStats;
36
37 fn find_dangling_edges(&self, space: &str) -> Result<Vec<Edge>, StorageError>;
38 fn repair_dangling_edges(&mut self, space: &str) -> Result<usize, StorageError>;
39
40 fn purge_expired(
54 &self,
55 space: &str,
56 target: Option<PurgeTarget<'_>>,
57 ) -> Result<Vec<TtlPurgeEntry>, StorageError>;
58
59 fn get_db_path(&self) -> &str;
60}
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub enum PurgeTarget<'a> {
67 Tag(&'a str),
68 Edge(&'a str),
69}
70
71#[derive(Debug, Clone)]
76pub struct TtlPurgeEntry {
77 pub schema_name: String,
78 pub is_edge: bool,
79 pub removed: usize,
80}
81
82pub trait StoragePersistenceOps: Send + Sync + std::fmt::Debug {
84 fn flush(&self) -> StorageResult<()>;
85
86 fn create_checkpoint(&self) -> StorageResult<Option<crate::CheckpointStats>>;
87
88 fn verify_snapshot(&self, snapshot_id: u64) -> StorageResult<bool>;
89
90 fn cleanup_snapshots(&self) -> StorageResult<usize>;
91
92 fn snapshot_stats(&self) -> crate::SnapshotStats;
93
94 fn persistence_diagnostics(&self) -> Option<crate::PersistenceDiagnostics>;
95
96 fn compact(&self, config: &CompactConfig) -> StorageResult<()>;
97
98 fn save_data(&self) -> StorageResult<()> {
99 self.flush()
100 }
101
102 fn save_data_to_dir(&self, dir: &std::path::Path) -> StorageResult<()>;
103
104 fn auto_flush_if_needed(&self) -> StorageResult<bool> {
105 if self.should_flush() {
106 self.flush()?;
107 Ok(true)
108 } else {
109 Ok(false)
110 }
111 }
112
113 fn auto_checkpoint_if_needed(&self) -> StorageResult<Option<crate::CheckpointStats>> {
114 if self.should_checkpoint() {
115 self.create_checkpoint()
116 } else {
117 Ok(None)
118 }
119 }
120
121 fn should_flush(&self) -> bool;
122
123 fn should_checkpoint(&self) -> bool;
124
125 fn set_outbox_materialized_lsn_provider(
126 &self,
127 _provider: Arc<
128 dyn Fn() -> StorageResult<Option<linkrs_core::types::CommitLsn>> + Send + Sync,
129 >,
130 ) {
131 }
132}
133
134pub trait StorageOperationContextOps: Send + Sync + std::fmt::Debug {
136 fn bind_auto_commit_context(&self) -> StorageResult<Self>
137 where
138 Self: Sized;
139
140 fn bind_read_operation_context(&self) -> StorageResult<Self>
153 where
154 Self: Sized,
155 {
156 Err(StorageError::not_supported(
157 "Read operation context binding is not supported by this storage implementation",
158 ))
159 }
160
161 fn bind_operation_context(&self, context: StorageOperationContext) -> Self
162 where
163 Self: Sized;
164
165 fn operation_context(&self) -> Option<Arc<StorageOperationContext>>;
166
167 fn finalize_operation(&self, _committed: bool) -> StorageResult<()> {
172 Ok(())
173 }
174}
175
176pub trait StorageSyncContextOps: Send + Sync + std::fmt::Debug {
178 fn get_sync_manager(&self) -> Option<Arc<linkrs_sync::SyncManager>>;
179}
180
181pub trait StorageGcOps: Send + Sync + std::fmt::Debug {
183 fn is_index_gc_running(&self) -> bool;
184
185 fn start_index_gc(&self) -> Option<crate::thread_pool::BackgroundTaskHandle>;
186
187 fn stop_index_gc(&self);
188}
189
190pub trait StorageTtlPurgeOps: Send + Sync + std::fmt::Debug {
197 fn start_ttl_purge(&self, interval: std::time::Duration);
200
201 fn stop_ttl_purge(&self);
202
203 fn is_ttl_purge_running(&self) -> bool;
204}
205
206pub trait StorageSnapshotOps: Send + Sync + std::fmt::Debug {
208 fn get_freeze_stats(&self) -> Option<FreezeStats>;
209 fn trigger_background_freeze(&self) -> StorageResult<()>;
210}
211
212#[derive(Debug, Clone)]
222pub struct StorageStats {
223 pub total_vertices: usize,
224 pub total_edges: usize,
225 pub total_spaces: usize,
226 pub total_tags: usize,
227 pub total_edge_types: usize,
228 pub total_size_bytes: u64,
230 pub data_size_bytes: u64,
232 pub index_size_bytes: u64,
234}