use crate::engine::background_freeze::FreezeStats;
use linkrs_core::types::{CompactConfig, PasswordInfo, UserAlterInfo, UserInfo};
use linkrs_core::{Edge, RoleType, StorageError, StorageResult};
use std::sync::Arc;
use super::StorageOperationContext;
pub trait StorageAuthOps: Send + Sync + std::fmt::Debug {
fn change_password(&mut self, info: &PasswordInfo) -> Result<bool, StorageError>;
fn create_user(&mut self, info: &UserInfo) -> Result<bool, StorageError>;
fn alter_user(&mut self, info: &UserAlterInfo) -> Result<bool, StorageError>;
fn drop_user(&mut self, username: &str) -> Result<bool, StorageError>;
fn user_exists(&self, username: &str) -> bool;
fn list_users(&self) -> Vec<String>;
fn get_user(&self, username: &str) -> Option<UserInfo>;
fn update_last_login(&self, username: &str) -> Result<bool, StorageError>;
fn list_user_roles(&self, username: &str) -> Vec<(i64, RoleType)>;
fn list_all_user_roles(&self) -> Vec<(String, Vec<(i64, RoleType)>)>;
fn grant_role(
&mut self,
username: &str,
space_id: i64,
role: RoleType,
) -> Result<bool, StorageError>;
fn revoke_role(&mut self, username: &str, space_id: i64) -> Result<bool, StorageError>;
}
pub trait StorageAdmin: Send + Sync + std::fmt::Debug {
fn load_from_disk(&mut self) -> Result<(), StorageError>;
fn save_to_disk(&self) -> Result<(), StorageError>;
fn get_storage_stats(&self) -> StorageStats;
fn find_dangling_edges(&self, space: &str) -> Result<Vec<Edge>, StorageError>;
fn repair_dangling_edges(&mut self, space: &str) -> Result<usize, StorageError>;
fn purge_expired(
&self,
space: &str,
target: Option<PurgeTarget<'_>>,
) -> Result<Vec<TtlPurgeEntry>, StorageError>;
fn get_db_path(&self) -> &str;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PurgeTarget<'a> {
Tag(&'a str),
Edge(&'a str),
}
#[derive(Debug, Clone)]
pub struct TtlPurgeEntry {
pub schema_name: String,
pub is_edge: bool,
pub removed: usize,
}
pub trait StoragePersistenceOps: Send + Sync + std::fmt::Debug {
fn flush(&self) -> StorageResult<()>;
fn create_checkpoint(&self) -> StorageResult<Option<crate::CheckpointStats>>;
fn verify_snapshot(&self, snapshot_id: u64) -> StorageResult<bool>;
fn cleanup_snapshots(&self) -> StorageResult<usize>;
fn snapshot_stats(&self) -> crate::SnapshotStats;
fn persistence_diagnostics(&self) -> Option<crate::PersistenceDiagnostics>;
fn compact(&self, config: &CompactConfig) -> StorageResult<()>;
fn save_data(&self) -> StorageResult<()> {
self.flush()
}
fn save_data_to_dir(&self, dir: &std::path::Path) -> StorageResult<()>;
fn auto_flush_if_needed(&self) -> StorageResult<bool> {
if self.should_flush() {
self.flush()?;
Ok(true)
} else {
Ok(false)
}
}
fn auto_checkpoint_if_needed(&self) -> StorageResult<Option<crate::CheckpointStats>> {
if self.should_checkpoint() {
self.create_checkpoint()
} else {
Ok(None)
}
}
fn should_flush(&self) -> bool;
fn should_checkpoint(&self) -> bool;
fn set_outbox_materialized_lsn_provider(
&self,
_provider: Arc<
dyn Fn() -> StorageResult<Option<linkrs_core::types::CommitLsn>> + Send + Sync,
>,
) {
}
}
pub trait StorageOperationContextOps: Send + Sync + std::fmt::Debug {
fn bind_auto_commit_context(&self) -> StorageResult<Self>
where
Self: Sized;
fn bind_read_operation_context(&self) -> StorageResult<Self>
where
Self: Sized,
{
Err(StorageError::not_supported(
"Read operation context binding is not supported by this storage implementation",
))
}
fn bind_operation_context(&self, context: StorageOperationContext) -> Self
where
Self: Sized;
fn operation_context(&self) -> Option<Arc<StorageOperationContext>>;
fn finalize_operation(&self, _committed: bool) -> StorageResult<()> {
Ok(())
}
}
pub trait StorageSyncContextOps: Send + Sync + std::fmt::Debug {
fn get_sync_manager(&self) -> Option<Arc<linkrs_sync::SyncManager>>;
}
pub trait StorageGcOps: Send + Sync + std::fmt::Debug {
fn is_index_gc_running(&self) -> bool;
fn start_index_gc(&self) -> Option<crate::thread_pool::BackgroundTaskHandle>;
fn stop_index_gc(&self);
}
pub trait StorageTtlPurgeOps: Send + Sync + std::fmt::Debug {
fn start_ttl_purge(&self, interval: std::time::Duration);
fn stop_ttl_purge(&self);
fn is_ttl_purge_running(&self) -> bool;
}
pub trait StorageSnapshotOps: Send + Sync + std::fmt::Debug {
fn get_freeze_stats(&self) -> Option<FreezeStats>;
fn trigger_background_freeze(&self) -> StorageResult<()>;
}
#[derive(Debug, Clone)]
pub struct StorageStats {
pub total_vertices: usize,
pub total_edges: usize,
pub total_spaces: usize,
pub total_tags: usize,
pub total_edge_types: usize,
pub total_size_bytes: u64,
pub data_size_bytes: u64,
pub index_size_bytes: u64,
}