oxirs-cluster 0.3.1

Raft-backed distributed dataset for high availability and horizontal scaling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
//! Persistent storage implementation
//!
//! Large implementation is split across sibling modules:
//! - `persistent_wal.rs`         — Write-Ahead Log operations
//! - `persistent_integrity.rs`   — Integrity verification and crash recovery
//! - `persistent_tests.rs`       — Unit tests (declared from mod.rs)

use super::config::StorageConfig;
use super::recovery::*;
use super::stats::StorageStats;
use super::types::*;

use crate::network::LogEntry;
use crate::raft::{OxirsNodeId, RdfApp, RdfCommand};
use crate::shard::ShardId;

use anyhow::{anyhow, Result};
use async_trait::async_trait;
use oxirs_core::model::Triple;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fs;
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock;

/// Storage backend trait for sharding support
#[async_trait]
pub trait StorageBackend: Send + Sync {
    /// Create a new shard
    async fn create_shard(&self, shard_id: ShardId) -> Result<()>;

    /// Delete a shard
    async fn delete_shard(&self, shard_id: ShardId) -> Result<()>;

    /// Insert a triple into a specific shard
    async fn insert_triple_to_shard(&self, shard_id: ShardId, triple: Triple) -> Result<()>;

    /// Delete a triple from a specific shard
    async fn delete_triple_from_shard(&self, shard_id: ShardId, triple: &Triple) -> Result<()>;

    /// Query triples from a specific shard
    async fn query_shard(
        &self,
        shard_id: ShardId,
        subject: Option<&str>,
        predicate: Option<&str>,
        object: Option<&str>,
    ) -> Result<Vec<Triple>>;

    /// Get shard size in bytes
    async fn get_shard_size(&self, shard_id: ShardId) -> Result<u64>;

    /// Get shard triple count
    async fn get_shard_triple_count(&self, shard_id: ShardId) -> Result<usize>;

    /// Export shard data for migration
    async fn export_shard(&self, shard_id: ShardId) -> Result<Vec<Triple>>;

    /// Import shard data during migration
    async fn import_shard(&self, shard_id: ShardId, triples: Vec<Triple>) -> Result<()>;

    /// Get all triples from a specific shard
    async fn get_shard_triples(&self, shard_id: ShardId) -> Result<Vec<Triple>>;

    /// Insert multiple triples into a specific shard
    async fn insert_triples_to_shard(&self, shard_id: ShardId, triples: Vec<Triple>) -> Result<()>;

    /// Mark a shard for deletion (logical deletion before physical cleanup)
    async fn mark_shard_for_deletion(&self, shard_id: ShardId) -> Result<()>;
}

/// StorageBackend implementation for PersistentStorage
pub struct PersistentStorage {
    /// Data directory
    pub(crate) data_dir: PathBuf,
    /// Node ID
    pub(crate) node_id: OxirsNodeId,
    /// In-memory Raft state (cached)
    pub(crate) raft_state: Arc<RwLock<RaftState>>,
    /// In-memory application state (cached)
    pub(crate) app_state: Arc<RwLock<RdfApp>>,
    /// Configuration
    pub(crate) config: StorageConfig,
    /// WAL sequence counter
    pub(crate) wal_sequence: Arc<RwLock<u64>>,
    /// WAL file writer
    pub(crate) wal_writer: Arc<RwLock<Option<BufWriter<File>>>>,
}

impl PersistentStorage {
    /// Create a new persistent storage instance
    pub async fn new(node_id: OxirsNodeId, config: StorageConfig) -> Result<Self> {
        let data_dir = PathBuf::from(&config.data_dir).join(format!("node-{node_id}"));

        if !data_dir.exists() {
            std::fs::create_dir_all(&data_dir)?;
        }

        let storage = Self {
            data_dir,
            node_id,
            raft_state: Arc::new(RwLock::new(RaftState::default())),
            app_state: Arc::new(RwLock::new(RdfApp::default())),
            config,
            wal_sequence: Arc::new(RwLock::new(0)),
            wal_writer: Arc::new(RwLock::new(None)),
        };

        if storage.config.enable_wal {
            storage.init_wal().await?;
        }

        if storage.config.enable_crash_recovery {
            storage.recover_from_crash().await?;
        }

        storage.load_state().await?;

        Ok(storage)
    }

    /// Initialize Write-Ahead Log
    async fn init_wal(&self) -> Result<()> {
        let ctx = super::persistent_wal::WalContext {
            data_dir: &self.data_dir,
            node_id: self.node_id,
            config: &self.config,
            wal_sequence: &self.wal_sequence,
            wal_writer: &self.wal_writer,
        };
        super::persistent_wal::init_wal(ctx).await
    }

    /// Write entry to WAL
    async fn write_wal_entry(&self, operation: WalOperation) -> Result<()> {
        super::persistent_wal::write_wal_entry(
            &self.config,
            &self.wal_sequence,
            &self.wal_writer,
            operation,
        )
        .await
    }

    /// Get current term
    pub async fn get_current_term(&self) -> u64 {
        self.raft_state.read().await.current_term
    }

    /// Set current term
    pub async fn set_current_term(&self, term: u64) -> Result<()> {
        {
            let mut state = self.raft_state.write().await;
            state.current_term = term;
            state.voted_for = None;
        }
        self.save_state().await
    }

    /// Get voted for
    pub async fn get_voted_for(&self) -> Option<OxirsNodeId> {
        self.raft_state.read().await.voted_for
    }

    /// Set voted for
    pub async fn set_voted_for(&self, candidate_id: Option<OxirsNodeId>) -> Result<()> {
        {
            let mut state = self.raft_state.write().await;
            state.voted_for = candidate_id;
        }
        self.save_state().await
    }

    /// Append log entries
    pub async fn append_entries(&self, entries: Vec<LogEntry>) -> Result<()> {
        {
            let mut state = self.raft_state.write().await;
            state.log.extend(entries);
        }
        self.save_state().await
    }

    /// Get log entries in range
    pub async fn get_log_entries(&self, start: u64, end: u64) -> Vec<LogEntry> {
        let state = self.raft_state.read().await;
        state
            .log
            .iter()
            .filter(|entry| entry.index >= start && entry.index < end)
            .cloned()
            .collect()
    }

    /// Get log entry at index
    pub async fn get_log_entry(&self, index: u64) -> Option<LogEntry> {
        let state = self.raft_state.read().await;
        state.log.iter().find(|entry| entry.index == index).cloned()
    }

    /// Get last log index
    pub async fn get_last_log_index(&self) -> u64 {
        let state = self.raft_state.read().await;
        state.log.last().map(|entry| entry.index).unwrap_or(0)
    }

    /// Get last log term
    pub async fn get_last_log_term(&self) -> u64 {
        let state = self.raft_state.read().await;
        state.log.last().map(|entry| entry.term).unwrap_or(0)
    }

    /// Truncate log from index
    pub async fn truncate_log(&self, from_index: u64) -> Result<()> {
        {
            let mut state = self.raft_state.write().await;
            state.log.retain(|entry| entry.index < from_index);
        }
        self.save_state().await
    }

    /// Get commit index
    pub async fn get_commit_index(&self) -> u64 {
        self.raft_state.read().await.commit_index
    }

    /// Set commit index
    pub async fn set_commit_index(&self, index: u64) -> Result<()> {
        {
            let mut state = self.raft_state.write().await;
            state.commit_index = index;
        }
        self.save_state().await
    }

    /// Get last applied index
    pub async fn get_last_applied(&self) -> u64 {
        self.raft_state.read().await.last_applied
    }

    /// Set last applied index
    pub async fn set_last_applied(&self, index: u64) -> Result<()> {
        {
            let mut state = self.raft_state.write().await;
            state.last_applied = index;
        }
        self.save_state().await
    }

    /// Apply command to state machine
    pub async fn apply_command(&self, command: &RdfCommand) -> Result<()> {
        let mut app_state = self.app_state.write().await;
        app_state.apply_command(command);
        self.save_app_state().await
    }

    /// Get application state
    pub async fn get_app_state(&self) -> RdfApp {
        self.app_state.read().await.clone()
    }

    /// Create snapshot
    pub async fn create_snapshot(&self) -> Result<SnapshotMetadata> {
        let raft_state = self.raft_state.read().await;
        let app_state = self.app_state.read().await;

        let last_log_entry = raft_state.log.last();
        let snapshot_path = self.data_dir.join("snapshot.json");
        let snapshot_data = serde_json::to_vec(&*app_state)?;
        fs::write(&snapshot_path, &snapshot_data)?;

        let mut hasher = Sha256::new();
        hasher.update(&snapshot_data);
        let checksum = hex::encode(hasher.finalize());

        let metadata = SnapshotMetadata {
            last_included_index: last_log_entry.map(|e| e.index).unwrap_or(0),
            last_included_term: last_log_entry.map(|e| e.term).unwrap_or(0),
            configuration: vec![self.node_id],
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("SystemTime should be after UNIX_EPOCH")
                .as_secs(),
            size: snapshot_data.len() as u64,
            checksum,
        };

        let metadata_path = self.data_dir.join("snapshot_metadata.json");
        let metadata_data = serde_json::to_vec(&metadata)?;
        fs::write(&metadata_path, &metadata_data)?;

        tracing::info!(
            "Created snapshot for node {} at index {} with {} bytes",
            self.node_id,
            metadata.last_included_index,
            metadata.size
        );

        Ok(metadata)
    }

    /// Load snapshot
    pub async fn load_snapshot(&self) -> Result<Option<(SnapshotMetadata, RdfApp)>> {
        let snapshot_path = self.data_dir.join("snapshot.json");
        let metadata_path = self.data_dir.join("snapshot_metadata.json");

        if !snapshot_path.exists() || !metadata_path.exists() {
            return Ok(None);
        }

        let metadata_data = fs::read(&metadata_path)?;
        let metadata: SnapshotMetadata = serde_json::from_slice(&metadata_data)?;

        let snapshot_data = fs::read(&snapshot_path)?;
        let app_state: RdfApp = serde_json::from_slice(&snapshot_data)?;

        Ok(Some((metadata, app_state)))
    }

    /// Compact log (remove entries before last snapshot)
    pub async fn compact_log(&self, until_index: u64) -> Result<()> {
        {
            let mut state = self.raft_state.write().await;
            state.log.retain(|entry| entry.index > until_index);
        }
        self.save_state().await
    }

    /// Check if compaction is needed
    pub async fn needs_compaction(&self) -> bool {
        let state = self.raft_state.read().await;
        state.log.len() > self.config.max_log_entries
    }

    /// Save Raft state to disk with WAL and atomic writes
    async fn save_state(&self) -> Result<()> {
        let state = self.raft_state.read().await;

        if self.config.enable_wal {
            self.write_wal_entry(WalOperation::WriteRaftState(state.clone()))
                .await?;
        }

        self.atomic_write_with_checksum("raft_state.dat", &*state)
            .await?;

        if self.config.enable_wal {
            let sequence = *self.wal_sequence.read().await;
            self.write_wal_entry(WalOperation::Commit(sequence)).await?;
        }

        Ok(())
    }

    /// Atomic write with corruption detection
    async fn atomic_write_with_checksum<T>(&self, filename: &str, data: &T) -> Result<()>
    where
        T: Serialize,
    {
        let path = self.data_dir.join(filename);
        let temp_path = self.data_dir.join(format!("{filename}.tmp"));

        let checksummed_data = if self.config.enable_corruption_detection {
            ChecksummedData::new(data)?
        } else {
            ChecksummedData {
                data,
                checksum: String::new(),
                timestamp: SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .expect("SystemTime should be after UNIX_EPOCH")
                    .as_secs(),
            }
        };

        let serialized =
            oxicode::serde::encode_to_vec(&checksummed_data, oxicode::config::standard())?;

        {
            let temp_file = OpenOptions::new()
                .write(true)
                .create(true)
                .truncate(true)
                .open(&temp_path)?;

            let mut writer = BufWriter::new(temp_file);
            writer.write_all(&serialized)?;
            writer.flush()?;

            if self.config.sync_writes {
                writer.get_mut().sync_all()?;
            }
        }

        std::fs::rename(&temp_path, &path)?;

        Ok(())
    }

    /// Load Raft state from disk with corruption detection
    async fn load_state(&self) -> Result<()> {
        let binary_path = self.data_dir.join("raft_state.dat");
        if binary_path.exists() {
            match self.load_with_checksum::<RaftState>(&binary_path).await {
                Ok(state) => {
                    *self.raft_state.write().await = state;
                    tracing::info!("Loaded Raft state (binary) for node {}", self.node_id);
                }
                Err(e) => {
                    tracing::error!("Failed to load binary Raft state: {}", e);
                    if self.config.enable_corruption_detection {
                        return Err(anyhow!("Corrupted Raft state file"));
                    }
                }
            }
        } else {
            let json_path = self.data_dir.join("raft_state.json");
            if json_path.exists() {
                let data = std::fs::read(&json_path)?;
                let state: RaftState = serde_json::from_slice(&data)?;
                *self.raft_state.write().await = state;
                tracing::info!("Loaded Raft state (legacy JSON) for node {}", self.node_id);
            }
        }

        let app_binary_path = self.data_dir.join("app_state.dat");
        if app_binary_path.exists() {
            match self.load_with_checksum::<RdfApp>(&app_binary_path).await {
                Ok(app_state) => {
                    *self.app_state.write().await = app_state;
                    tracing::info!(
                        "Loaded application state (binary) for node {}",
                        self.node_id
                    );
                }
                Err(e) => {
                    tracing::error!("Failed to load binary application state: {}", e);
                    if self.config.enable_corruption_detection {
                        return Err(anyhow!("Corrupted application state file"));
                    }
                }
            }
        } else {
            let app_json_path = self.data_dir.join("app_state.json");
            if app_json_path.exists() {
                let data = std::fs::read(&app_json_path)?;
                let app_state: RdfApp = serde_json::from_slice(&data)?;
                *self.app_state.write().await = app_state;
                tracing::info!(
                    "Loaded application state (legacy JSON) for node {}",
                    self.node_id
                );
            }
        }

        Ok(())
    }

    /// Load data with checksum verification
    async fn load_with_checksum<T>(&self, path: &Path) -> Result<T>
    where
        T: for<'de> Deserialize<'de> + Serialize,
    {
        let data = std::fs::read(path)?;
        let (checksummed_data, _): (ChecksummedData<T>, _) =
            oxicode::serde::decode_from_slice(&data, oxicode::config::standard())?;

        if self.config.enable_corruption_detection && !checksummed_data.verify()? {
            return Err(anyhow!("Checksum verification failed for {:?}", path));
        }

        Ok(checksummed_data.data)
    }

    /// Save application state to disk with WAL and atomic writes
    async fn save_app_state(&self) -> Result<()> {
        let app_state = self.app_state.read().await;

        if self.config.enable_wal {
            self.write_wal_entry(WalOperation::WriteAppState(app_state.clone()))
                .await?;
        }

        self.atomic_write_with_checksum("app_state.dat", &*app_state)
            .await?;

        if self.config.enable_wal {
            let sequence = *self.wal_sequence.read().await;
            self.write_wal_entry(WalOperation::Commit(sequence)).await?;
        }

        Ok(())
    }

    /// Get storage statistics
    pub async fn get_stats(&self) -> StorageStats {
        let raft_state = self.raft_state.read().await;
        let app_state = self.app_state.read().await;

        let data_dir_size = self.calculate_directory_size(&self.data_dir).unwrap_or(0);

        StorageStats {
            node_id: self.node_id,
            data_dir: self.data_dir.clone(),
            log_entries: raft_state.log.len(),
            current_term: raft_state.current_term,
            commit_index: raft_state.commit_index,
            last_applied: raft_state.last_applied,
            triple_count: app_state.len(),
            disk_usage_bytes: data_dir_size,
        }
    }

    /// Calculate directory size
    #[allow(clippy::only_used_in_recursion)]
    fn calculate_directory_size(&self, dir: &Path) -> Result<u64> {
        let mut size = 0;
        if dir.is_dir() {
            for entry in fs::read_dir(dir)? {
                let entry = entry?;
                let path = entry.path();
                if path.is_file() {
                    size += fs::metadata(&path)?.len();
                } else if path.is_dir() {
                    size += self.calculate_directory_size(&path)?;
                }
            }
        }
        Ok(size)
    }

    /// Backup current state
    pub async fn backup(&self) -> Result<PathBuf> {
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("SystemTime should be after UNIX_EPOCH")
            .as_secs();
        let backup_dir = self
            .data_dir
            .parent()
            .expect("data_dir should have a parent directory")
            .join(format!("backup-{}-{}", self.node_id, timestamp));

        fs::create_dir_all(&backup_dir)?;

        for entry in fs::read_dir(&self.data_dir)? {
            let entry = entry?;
            let source = entry.path();
            let dest = backup_dir.join(entry.file_name());
            fs::copy(&source, &dest)?;
        }

        tracing::info!("Created backup at {:?}", backup_dir);
        Ok(backup_dir)
    }

    /// Rotate WAL file when it gets too large
    pub async fn rotate_wal(&self) -> Result<()> {
        super::persistent_wal::rotate_wal(
            &self.config,
            &self.data_dir,
            self.node_id,
            &self.wal_sequence,
            &self.wal_writer,
        )
        .await
    }

    /// Compact WAL by removing committed entries
    pub async fn compact_wal(&self) -> Result<()> {
        super::persistent_wal::compact_wal(
            &self.config,
            &self.data_dir,
            self.node_id,
            &self.wal_sequence,
            &self.wal_writer,
        )
        .await
    }

    /// Verify data integrity across all files
    pub async fn verify_integrity(&self) -> Result<bool> {
        super::persistent_integrity::verify_integrity(
            &self.config,
            &self.data_dir,
            &self.raft_state,
            &self.app_state,
        )
        .await
    }

    /// Clean old backups
    pub async fn cleanup_old_backups(&self) -> Result<()> {
        let parent_dir = self
            .data_dir
            .parent()
            .expect("data_dir should have a parent directory");
        let backup_prefix = format!("backup-{}-", self.node_id);

        let mut backups = Vec::new();
        for entry in fs::read_dir(parent_dir)? {
            let entry = entry?;
            let name = entry.file_name();
            if let Some(name_str) = name.to_str() {
                if name_str.starts_with(&backup_prefix) {
                    backups.push((entry.path(), name_str.to_string()));
                }
            }
        }

        backups.sort_by(|a, b| b.1.cmp(&a.1));

        for (path, _) in backups.iter().skip(self.config.backup_retention) {
            fs::remove_dir_all(path)?;
            tracing::info!("Removed old backup: {:?}", path);
        }

        Ok(())
    }

    /// Perform crash recovery check and repair if needed
    pub async fn recover_from_crash(&self) -> Result<RecoveryReport> {
        super::persistent_integrity::recover_from_crash(
            &self.config,
            &self.data_dir,
            self.node_id,
            &self.raft_state,
        )
        .await
    }
}

#[async_trait]
impl StorageBackend for PersistentStorage {
    async fn create_shard(&self, shard_id: ShardId) -> Result<()> {
        let mut app_state = self.app_state.write().await;
        app_state.create_shard(shard_id);
        self.save_app_state().await
    }

    async fn delete_shard(&self, shard_id: ShardId) -> Result<()> {
        let mut app_state = self.app_state.write().await;
        app_state.delete_shard(shard_id);
        self.save_app_state().await
    }

    async fn insert_triple_to_shard(&self, shard_id: ShardId, triple: Triple) -> Result<()> {
        let mut app_state = self.app_state.write().await;
        app_state.insert_triple_to_shard(shard_id, triple);
        self.save_app_state().await
    }

    async fn delete_triple_from_shard(&self, shard_id: ShardId, triple: &Triple) -> Result<()> {
        let mut app_state = self.app_state.write().await;
        app_state.delete_triple_from_shard(shard_id, triple);
        self.save_app_state().await
    }

    async fn query_shard(
        &self,
        shard_id: ShardId,
        subject: Option<&str>,
        predicate: Option<&str>,
        object: Option<&str>,
    ) -> Result<Vec<Triple>> {
        let app_state = self.app_state.read().await;
        Ok(app_state.query_shard(shard_id, subject, predicate, object))
    }

    async fn get_shard_size(&self, shard_id: ShardId) -> Result<u64> {
        let app_state = self.app_state.read().await;
        Ok(app_state.get_shard_size(shard_id))
    }

    async fn get_shard_triple_count(&self, shard_id: ShardId) -> Result<usize> {
        let app_state = self.app_state.read().await;
        Ok(app_state.get_shard_triple_count(shard_id))
    }

    async fn export_shard(&self, shard_id: ShardId) -> Result<Vec<Triple>> {
        let app_state = self.app_state.read().await;
        Ok(app_state.export_shard(shard_id))
    }

    async fn import_shard(&self, shard_id: ShardId, triples: Vec<Triple>) -> Result<()> {
        let mut app_state = self.app_state.write().await;
        app_state.import_shard(shard_id, triples);
        self.save_app_state().await
    }

    async fn get_shard_triples(&self, shard_id: ShardId) -> Result<Vec<Triple>> {
        let app_state = self.app_state.read().await;
        Ok(app_state.get_shard_triples(shard_id))
    }

    async fn insert_triples_to_shard(&self, shard_id: ShardId, triples: Vec<Triple>) -> Result<()> {
        let mut app_state = self.app_state.write().await;
        app_state.insert_triples_to_shard(shard_id, triples);
        self.save_app_state().await
    }

    async fn mark_shard_for_deletion(&self, shard_id: ShardId) -> Result<()> {
        let mut app_state = self.app_state.write().await;
        app_state.mark_shard_for_deletion(shard_id);
        self.save_app_state().await
    }
}