Skip to main content

linkrs_storage/engine/sync_wrapper/
persistence.rs

1use super::SyncWrapper;
2use crate::macros::forward_methods;
3use crate::{StorageClient, StoragePersistenceOps};
4use linkrs_core::StorageError;
5
6impl<S: StorageClient + 'static> StoragePersistenceOps for SyncWrapper<S> {
7    forward_methods!(inner;
8        fn flush(&self) -> Result<(), StorageError>;
9        fn save_data(&self) -> linkrs_core::StorageResult<()>;
10        fn save_data_to_dir(&self, dir: &std::path::Path) -> linkrs_core::StorageResult<()>;
11        fn verify_snapshot(&self, snapshot_id: u64) -> linkrs_core::StorageResult<bool>;
12        fn cleanup_snapshots(&self) -> linkrs_core::StorageResult<usize>;
13        fn snapshot_stats(&self) -> crate::SnapshotStats;
14        fn persistence_diagnostics(&self) -> Option<crate::PersistenceDiagnostics>;
15        fn compact(&self, config: &linkrs_core::types::CompactConfig) -> linkrs_core::StorageResult<()>;
16        fn auto_flush_if_needed(&self) -> linkrs_core::StorageResult<bool>;
17        fn should_flush(&self) -> bool;
18        fn should_checkpoint(&self) -> bool;
19    );
20
21    fn create_checkpoint(&self) -> linkrs_core::StorageResult<Option<crate::CheckpointStats>> {
22        if self.enabled {
23            let manager = self.sync_manager.as_ref().ok_or_else(|| {
24                StorageError::invalid_operation(
25                    "Synchronization is enabled without an outbox manager",
26                )
27            })?;
28            manager
29                .create_checkpoint_outbox_snapshot()
30                .map_err(|error| {
31                    StorageError::db_error(format!(
32                        "Failed to create checkpoint outbox snapshot: {error}"
33                    ))
34                })?;
35        }
36        self.inner.create_checkpoint()
37    }
38
39    fn auto_checkpoint_if_needed(
40        &self,
41    ) -> linkrs_core::StorageResult<Option<crate::CheckpointStats>> {
42        if !self.should_checkpoint() {
43            return Ok(None);
44        }
45        if self.enabled {
46            let manager = self.sync_manager.as_ref().ok_or_else(|| {
47                StorageError::invalid_operation(
48                    "Synchronization is enabled without an outbox manager",
49                )
50            })?;
51            manager
52                .create_checkpoint_outbox_snapshot()
53                .map_err(|error| {
54                    StorageError::db_error(format!(
55                        "Failed to create checkpoint outbox snapshot: {error}"
56                    ))
57                })?;
58        }
59        self.inner.auto_checkpoint_if_needed()
60    }
61}