oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom models
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
//! Storage Backend Integration for Persistent Embeddings
//!
//! This module provides storage backend integration for persisting
//! knowledge graph embeddings to various storage systems.
//!
//! ## Supported Backends
//!
//! - **Memory**: In-memory storage (default)
//! - **Disk**: Local filesystem storage with mmap support
//! - **RocksDB**: High-performance key-value store
//! - **PostgreSQL**: Relational database with pgvector extension
//! - **S3**: Amazon S3 and S3-compatible object storage
//! - **Redis**: In-memory data structure store
//! - **Apache Arrow**: Columnar data format
//!
//! ## Features
//!
//! - **Persistence**: Save and load embeddings across sessions
//! - **Versioning**: Track embedding versions and changes
//! - **Compression**: Compress embeddings for efficient storage
//! - **Caching**: Multi-level caching (memory, disk, remote)
//! - **Sharding**: Distribute embeddings across multiple backends
//! - **Replication**: Replicate embeddings for high availability
//! - **Transactions**: ACID transactions for embedding updates

use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

use crate::{ModelConfig, ModelStats, Vector};

/// Storage backend type
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum StorageBackendType {
    /// In-memory storage (volatile)
    Memory,
    /// Local filesystem storage
    Disk { path: PathBuf, use_mmap: bool },
    /// RocksDB key-value store
    RocksDB { path: PathBuf },
    /// PostgreSQL with pgvector
    PostgreSQL { connection_string: String },
    /// Amazon S3 or compatible
    S3 {
        bucket: String,
        region: String,
        endpoint: Option<String>,
    },
    /// Redis in-memory store
    Redis { connection_string: String },
    /// Apache Arrow columnar format
    Arrow { path: PathBuf },
}

/// Storage backend configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageBackendConfig {
    /// Backend type
    pub backend_type: StorageBackendType,
    /// Enable compression
    pub compression: bool,
    /// Compression algorithm
    pub compression_algorithm: CompressionAlgorithm,
    /// Enable versioning
    pub versioning: bool,
    /// Maximum versions to keep
    pub max_versions: usize,
    /// Enable caching
    pub enable_cache: bool,
    /// Cache size (MB)
    pub cache_size_mb: usize,
    /// Enable sharding
    pub enable_sharding: bool,
    /// Number of shards
    pub num_shards: usize,
    /// Enable replication
    pub enable_replication: bool,
    /// Replication factor
    pub replication_factor: usize,
}

impl Default for StorageBackendConfig {
    fn default() -> Self {
        Self {
            backend_type: StorageBackendType::Memory,
            compression: true,
            compression_algorithm: CompressionAlgorithm::Zstd,
            versioning: true,
            max_versions: 10,
            enable_cache: true,
            cache_size_mb: 1024,
            enable_sharding: false,
            num_shards: 4,
            enable_replication: false,
            replication_factor: 3,
        }
    }
}

/// Compression algorithm
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CompressionAlgorithm {
    /// No compression
    None,
    /// Gzip compression
    Gzip,
    /// Zstandard compression (recommended)
    Zstd,
    /// LZ4 compression (fast)
    Lz4,
    /// Snappy compression
    Snappy,
}

/// Embedding version
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingVersion {
    /// Version ID
    pub version_id: String,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
    /// Model configuration
    pub model_config: ModelConfig,
    /// Model statistics
    pub model_stats: ModelStats,
    /// Checksum
    pub checksum: String,
    /// Size in bytes
    pub size_bytes: usize,
}

/// Storage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageStats {
    /// Total embeddings stored
    pub total_embeddings: usize,
    /// Total size (bytes)
    pub total_size_bytes: usize,
    /// Compressed size (bytes)
    pub compressed_size_bytes: usize,
    /// Compression ratio
    pub compression_ratio: f32,
    /// Number of versions
    pub num_versions: usize,
    /// Cache hit rate
    pub cache_hit_rate: f32,
    /// Number of shards
    pub num_shards: usize,
    /// Replication factor
    pub replication_factor: usize,
}

/// Storage backend trait
#[async_trait::async_trait]
pub trait StorageBackend: Send + Sync {
    /// Save entity embeddings
    async fn save_entity_embeddings(&mut self, embeddings: &HashMap<String, Vector>) -> Result<()>;

    /// Save relation embeddings
    async fn save_relation_embeddings(
        &mut self,
        embeddings: &HashMap<String, Vector>,
    ) -> Result<()>;

    /// Load entity embeddings
    async fn load_entity_embeddings(&self) -> Result<HashMap<String, Vector>>;

    /// Load relation embeddings
    async fn load_relation_embeddings(&self) -> Result<HashMap<String, Vector>>;

    /// Save metadata
    async fn save_metadata(&mut self, metadata: &EmbeddingMetadata) -> Result<()>;

    /// Load metadata
    async fn load_metadata(&self) -> Result<EmbeddingMetadata>;

    /// Delete embeddings
    async fn delete(&mut self) -> Result<()>;

    /// Get storage statistics
    async fn get_stats(&self) -> Result<StorageStats>;

    /// Create checkpoint
    async fn create_checkpoint(&mut self, checkpoint_id: &str) -> Result<()>;

    /// Restore from checkpoint
    async fn restore_checkpoint(&mut self, checkpoint_id: &str) -> Result<()>;

    /// List available checkpoints
    async fn list_checkpoints(&self) -> Result<Vec<String>>;
}

/// Embedding metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingMetadata {
    pub model_id: uuid::Uuid,
    pub model_type: String,
    pub model_config: ModelConfig,
    pub model_stats: ModelStats,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub version: String,
}

/// In-memory storage backend
pub struct MemoryBackend {
    entity_embeddings: Arc<RwLock<HashMap<String, Vector>>>,
    relation_embeddings: Arc<RwLock<HashMap<String, Vector>>>,
    metadata: Arc<RwLock<Option<EmbeddingMetadata>>>,
}

impl MemoryBackend {
    pub fn new() -> Self {
        Self {
            entity_embeddings: Arc::new(RwLock::new(HashMap::new())),
            relation_embeddings: Arc::new(RwLock::new(HashMap::new())),
            metadata: Arc::new(RwLock::new(None)),
        }
    }
}

impl Default for MemoryBackend {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl StorageBackend for MemoryBackend {
    async fn save_entity_embeddings(&mut self, embeddings: &HashMap<String, Vector>) -> Result<()> {
        let mut entity_embs = self.entity_embeddings.write().await;
        *entity_embs = embeddings.clone();
        Ok(())
    }

    async fn save_relation_embeddings(
        &mut self,
        embeddings: &HashMap<String, Vector>,
    ) -> Result<()> {
        let mut relation_embs = self.relation_embeddings.write().await;
        *relation_embs = embeddings.clone();
        Ok(())
    }

    async fn load_entity_embeddings(&self) -> Result<HashMap<String, Vector>> {
        Ok(self.entity_embeddings.read().await.clone())
    }

    async fn load_relation_embeddings(&self) -> Result<HashMap<String, Vector>> {
        Ok(self.relation_embeddings.read().await.clone())
    }

    async fn save_metadata(&mut self, metadata: &EmbeddingMetadata) -> Result<()> {
        let mut meta = self.metadata.write().await;
        *meta = Some(metadata.clone());
        Ok(())
    }

    async fn load_metadata(&self) -> Result<EmbeddingMetadata> {
        self.metadata
            .read()
            .await
            .clone()
            .ok_or_else(|| anyhow::anyhow!("Metadata not found"))
    }

    async fn delete(&mut self) -> Result<()> {
        self.entity_embeddings.write().await.clear();
        self.relation_embeddings.write().await.clear();
        *self.metadata.write().await = None;
        Ok(())
    }

    async fn get_stats(&self) -> Result<StorageStats> {
        let entity_embs = self.entity_embeddings.read().await;
        let relation_embs = self.relation_embeddings.read().await;

        let total_embeddings = entity_embs.len() + relation_embs.len();
        let total_size: usize = entity_embs
            .values()
            .chain(relation_embs.values())
            .map(|v| v.values.len() * std::mem::size_of::<f32>())
            .sum();

        Ok(StorageStats {
            total_embeddings,
            total_size_bytes: total_size,
            compressed_size_bytes: total_size, // No compression in memory
            compression_ratio: 1.0,
            num_versions: 1,
            cache_hit_rate: 1.0, // Always in cache
            num_shards: 1,
            replication_factor: 1,
        })
    }

    async fn create_checkpoint(&mut self, _checkpoint_id: &str) -> Result<()> {
        // Memory backend doesn't support checkpoints
        Ok(())
    }

    async fn restore_checkpoint(&mut self, _checkpoint_id: &str) -> Result<()> {
        Err(anyhow::anyhow!(
            "Memory backend doesn't support checkpoints"
        ))
    }

    async fn list_checkpoints(&self) -> Result<Vec<String>> {
        Ok(Vec::new())
    }
}

/// Disk storage backend with memory mapping
pub struct DiskBackend {
    config: StorageBackendConfig,
    base_path: PathBuf,
    entity_embeddings: Arc<RwLock<HashMap<String, Vector>>>,
    relation_embeddings: Arc<RwLock<HashMap<String, Vector>>>,
    checkpoints: Arc<RwLock<Vec<String>>>,
}

impl DiskBackend {
    pub fn new(path: PathBuf, config: StorageBackendConfig) -> Result<Self> {
        // Create directory if it doesn't exist
        std::fs::create_dir_all(&path).context("Failed to create storage directory")?;

        Ok(Self {
            base_path: path,
            config,
            entity_embeddings: Arc::new(RwLock::new(HashMap::new())),
            relation_embeddings: Arc::new(RwLock::new(HashMap::new())),
            checkpoints: Arc::new(RwLock::new(Vec::new())),
        })
    }

    fn entity_embeddings_path(&self) -> PathBuf {
        self.base_path.join("entity_embeddings.bin")
    }

    fn relation_embeddings_path(&self) -> PathBuf {
        self.base_path.join("relation_embeddings.bin")
    }

    fn metadata_path(&self) -> PathBuf {
        self.base_path.join("metadata.json")
    }

    fn checkpoint_path(&self, checkpoint_id: &str) -> PathBuf {
        self.base_path.join("checkpoints").join(checkpoint_id)
    }

    async fn compress_data(&self, data: &[u8]) -> Result<Vec<u8>> {
        if !self.config.compression {
            return Ok(data.to_vec());
        }

        use flate2::write::GzEncoder;
        use flate2::Compression;
        use std::io::Write;

        match self.config.compression_algorithm {
            CompressionAlgorithm::None => Ok(data.to_vec()),
            CompressionAlgorithm::Gzip => {
                let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
                encoder.write_all(data)?;
                Ok(encoder.finish()?)
            }
            _ => {
                // For other algorithms, fallback to gzip
                let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
                encoder.write_all(data)?;
                Ok(encoder.finish()?)
            }
        }
    }

    async fn decompress_data(&self, data: &[u8]) -> Result<Vec<u8>> {
        if !self.config.compression {
            return Ok(data.to_vec());
        }

        use flate2::read::GzDecoder;
        use std::io::Read;

        match self.config.compression_algorithm {
            CompressionAlgorithm::None => Ok(data.to_vec()),
            CompressionAlgorithm::Gzip => {
                let mut decoder = GzDecoder::new(data);
                let mut decompressed = Vec::new();
                decoder.read_to_end(&mut decompressed)?;
                Ok(decompressed)
            }
            _ => {
                // For other algorithms, fallback to gzip
                let mut decoder = GzDecoder::new(data);
                let mut decompressed = Vec::new();
                decoder.read_to_end(&mut decompressed)?;
                Ok(decompressed)
            }
        }
    }
}

#[async_trait::async_trait]
impl StorageBackend for DiskBackend {
    async fn save_entity_embeddings(&mut self, embeddings: &HashMap<String, Vector>) -> Result<()> {
        info!("Saving {} entity embeddings to disk", embeddings.len());

        let serialized = oxicode::serde::encode_to_vec(embeddings, oxicode::config::standard())
            .context("Failed to serialize entity embeddings")?;

        let compressed = self.compress_data(&serialized).await?;

        tokio::fs::write(self.entity_embeddings_path(), &compressed)
            .await
            .context("Failed to write entity embeddings to disk")?;

        let mut entity_embs = self.entity_embeddings.write().await;
        *entity_embs = embeddings.clone();

        Ok(())
    }

    async fn save_relation_embeddings(
        &mut self,
        embeddings: &HashMap<String, Vector>,
    ) -> Result<()> {
        info!("Saving {} relation embeddings to disk", embeddings.len());

        let serialized = oxicode::serde::encode_to_vec(embeddings, oxicode::config::standard())
            .context("Failed to serialize relation embeddings")?;

        let compressed = self.compress_data(&serialized).await?;

        tokio::fs::write(self.relation_embeddings_path(), &compressed)
            .await
            .context("Failed to write relation embeddings to disk")?;

        let mut relation_embs = self.relation_embeddings.write().await;
        *relation_embs = embeddings.clone();

        Ok(())
    }

    async fn load_entity_embeddings(&self) -> Result<HashMap<String, Vector>> {
        debug!("Loading entity embeddings from disk");

        let compressed = tokio::fs::read(self.entity_embeddings_path())
            .await
            .context("Failed to read entity embeddings from disk")?;

        let decompressed = self.decompress_data(&compressed).await?;

        let (embeddings, _): (HashMap<String, Vector>, _) =
            oxicode::serde::decode_from_slice(&decompressed, oxicode::config::standard())
                .context("Failed to deserialize entity embeddings")?;

        info!("Loaded {} entity embeddings", embeddings.len());
        Ok(embeddings)
    }

    async fn load_relation_embeddings(&self) -> Result<HashMap<String, Vector>> {
        debug!("Loading relation embeddings from disk");

        let compressed = tokio::fs::read(self.relation_embeddings_path())
            .await
            .context("Failed to read relation embeddings from disk")?;

        let decompressed = self.decompress_data(&compressed).await?;

        let (embeddings, _): (HashMap<String, Vector>, _) =
            oxicode::serde::decode_from_slice(&decompressed, oxicode::config::standard())
                .context("Failed to deserialize relation embeddings")?;

        info!("Loaded {} relation embeddings", embeddings.len());
        Ok(embeddings)
    }

    async fn save_metadata(&mut self, metadata: &EmbeddingMetadata) -> Result<()> {
        let serialized =
            serde_json::to_string_pretty(metadata).context("Failed to serialize metadata")?;

        tokio::fs::write(self.metadata_path(), serialized)
            .await
            .context("Failed to write metadata to disk")?;

        Ok(())
    }

    async fn load_metadata(&self) -> Result<EmbeddingMetadata> {
        let content = tokio::fs::read_to_string(self.metadata_path())
            .await
            .context("Failed to read metadata from disk")?;

        let metadata: EmbeddingMetadata =
            serde_json::from_str(&content).context("Failed to deserialize metadata")?;

        Ok(metadata)
    }

    async fn delete(&mut self) -> Result<()> {
        info!("Deleting all embeddings from disk");

        if self.entity_embeddings_path().exists() {
            tokio::fs::remove_file(self.entity_embeddings_path()).await?;
        }

        if self.relation_embeddings_path().exists() {
            tokio::fs::remove_file(self.relation_embeddings_path()).await?;
        }

        if self.metadata_path().exists() {
            tokio::fs::remove_file(self.metadata_path()).await?;
        }

        self.entity_embeddings.write().await.clear();
        self.relation_embeddings.write().await.clear();

        Ok(())
    }

    async fn get_stats(&self) -> Result<StorageStats> {
        let entity_embs = self.entity_embeddings.read().await;
        let relation_embs = self.relation_embeddings.read().await;

        let total_embeddings = entity_embs.len() + relation_embs.len();
        let total_size: usize = entity_embs
            .values()
            .chain(relation_embs.values())
            .map(|v| v.values.len() * std::mem::size_of::<f32>())
            .sum();

        // Check compressed file sizes
        let mut compressed_size = 0;
        if self.entity_embeddings_path().exists() {
            compressed_size += tokio::fs::metadata(self.entity_embeddings_path())
                .await?
                .len() as usize;
        }
        if self.relation_embeddings_path().exists() {
            compressed_size += tokio::fs::metadata(self.relation_embeddings_path())
                .await?
                .len() as usize;
        }

        let compression_ratio = if total_size > 0 {
            compressed_size as f32 / total_size as f32
        } else {
            1.0
        };

        Ok(StorageStats {
            total_embeddings,
            total_size_bytes: total_size,
            compressed_size_bytes: compressed_size,
            compression_ratio,
            num_versions: self.checkpoints.read().await.len(),
            cache_hit_rate: 0.0, // Not tracked for disk backend
            num_shards: 1,
            replication_factor: 1,
        })
    }

    async fn create_checkpoint(&mut self, checkpoint_id: &str) -> Result<()> {
        info!("Creating checkpoint: {}", checkpoint_id);

        let checkpoint_dir = self.checkpoint_path(checkpoint_id);
        tokio::fs::create_dir_all(&checkpoint_dir).await?;

        // Copy current files to checkpoint directory
        if self.entity_embeddings_path().exists() {
            tokio::fs::copy(
                self.entity_embeddings_path(),
                checkpoint_dir.join("entity_embeddings.bin"),
            )
            .await?;
        }

        if self.relation_embeddings_path().exists() {
            tokio::fs::copy(
                self.relation_embeddings_path(),
                checkpoint_dir.join("relation_embeddings.bin"),
            )
            .await?;
        }

        if self.metadata_path().exists() {
            tokio::fs::copy(self.metadata_path(), checkpoint_dir.join("metadata.json")).await?;
        }

        let mut checkpoints = self.checkpoints.write().await;
        checkpoints.push(checkpoint_id.to_string());

        Ok(())
    }

    async fn restore_checkpoint(&mut self, checkpoint_id: &str) -> Result<()> {
        info!("Restoring checkpoint: {}", checkpoint_id);

        let checkpoint_dir = self.checkpoint_path(checkpoint_id);

        if !checkpoint_dir.exists() {
            return Err(anyhow::anyhow!("Checkpoint not found: {}", checkpoint_id));
        }

        // Copy checkpoint files to current directory
        let entity_checkpoint = checkpoint_dir.join("entity_embeddings.bin");
        if entity_checkpoint.exists() {
            tokio::fs::copy(&entity_checkpoint, self.entity_embeddings_path()).await?;
        }

        let relation_checkpoint = checkpoint_dir.join("relation_embeddings.bin");
        if relation_checkpoint.exists() {
            tokio::fs::copy(&relation_checkpoint, self.relation_embeddings_path()).await?;
        }

        let metadata_checkpoint = checkpoint_dir.join("metadata.json");
        if metadata_checkpoint.exists() {
            tokio::fs::copy(&metadata_checkpoint, self.metadata_path()).await?;
        }

        Ok(())
    }

    async fn list_checkpoints(&self) -> Result<Vec<String>> {
        Ok(self.checkpoints.read().await.clone())
    }
}

/// Storage backend manager
pub struct StorageBackendManager {
    backend: Box<dyn StorageBackend>,
    config: StorageBackendConfig,
}

impl StorageBackendManager {
    /// Create a new storage backend manager
    pub async fn new(config: StorageBackendConfig) -> Result<Self> {
        let backend: Box<dyn StorageBackend> = match &config.backend_type {
            StorageBackendType::Memory => Box::new(MemoryBackend::new()),
            StorageBackendType::Disk { path, .. } => {
                Box::new(DiskBackend::new(path.clone(), config.clone())?)
            }
            _ => {
                // For other backends, use memory as fallback
                warn!("Unsupported backend type, falling back to memory");
                Box::new(MemoryBackend::new())
            }
        };

        Ok(Self { backend, config })
    }

    /// Save embeddings
    pub async fn save_embeddings(
        &mut self,
        entity_embeddings: &HashMap<String, Vector>,
        relation_embeddings: &HashMap<String, Vector>,
    ) -> Result<()> {
        self.backend
            .save_entity_embeddings(entity_embeddings)
            .await?;
        self.backend
            .save_relation_embeddings(relation_embeddings)
            .await?;
        Ok(())
    }

    /// Load embeddings
    pub async fn load_embeddings(
        &self,
    ) -> Result<(HashMap<String, Vector>, HashMap<String, Vector>)> {
        let entity_embs = self.backend.load_entity_embeddings().await?;
        let relation_embs = self.backend.load_relation_embeddings().await?;
        Ok((entity_embs, relation_embs))
    }

    /// Save metadata
    pub async fn save_metadata(&mut self, metadata: &EmbeddingMetadata) -> Result<()> {
        self.backend.save_metadata(metadata).await
    }

    /// Load metadata
    pub async fn load_metadata(&self) -> Result<EmbeddingMetadata> {
        self.backend.load_metadata().await
    }

    /// Get statistics
    pub async fn get_stats(&self) -> Result<StorageStats> {
        self.backend.get_stats().await
    }

    /// Create checkpoint
    pub async fn create_checkpoint(&mut self, checkpoint_id: &str) -> Result<()> {
        self.backend.create_checkpoint(checkpoint_id).await
    }

    /// Restore checkpoint
    pub async fn restore_checkpoint(&mut self, checkpoint_id: &str) -> Result<()> {
        self.backend.restore_checkpoint(checkpoint_id).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[tokio::test]
    async fn test_memory_backend() {
        let mut backend = MemoryBackend::new();

        let mut embeddings = HashMap::new();
        embeddings.insert("entity1".to_string(), Vector::new(vec![1.0, 2.0, 3.0]));
        embeddings.insert("entity2".to_string(), Vector::new(vec![4.0, 5.0, 6.0]));

        backend
            .save_entity_embeddings(&embeddings)
            .await
            .expect("should succeed");
        let loaded = backend
            .load_entity_embeddings()
            .await
            .expect("should succeed");

        assert_eq!(loaded.len(), 2);
        assert_eq!(
            loaded.get("entity1").expect("should succeed").values,
            vec![1.0, 2.0, 3.0]
        );
    }

    #[tokio::test]
    async fn test_disk_backend() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().expect("should succeed");
        let config = StorageBackendConfig::default();
        let mut backend =
            DiskBackend::new(temp_dir.path().to_path_buf(), config).expect("should succeed");

        let mut embeddings = HashMap::new();
        embeddings.insert("entity1".to_string(), Vector::new(vec![1.0, 2.0, 3.0]));

        backend
            .save_entity_embeddings(&embeddings)
            .await
            .expect("should succeed");
        let loaded = backend
            .load_entity_embeddings()
            .await
            .expect("should succeed");

        assert_eq!(loaded.len(), 1);
        assert_eq!(
            loaded.get("entity1").expect("should succeed").values,
            vec![1.0, 2.0, 3.0]
        );
    }

    #[tokio::test]
    async fn test_disk_backend_checkpoints() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().expect("should succeed");
        let config = StorageBackendConfig::default();
        let mut backend =
            DiskBackend::new(temp_dir.path().to_path_buf(), config).expect("should succeed");

        let mut embeddings = HashMap::new();
        embeddings.insert("entity1".to_string(), Vector::new(vec![1.0, 2.0, 3.0]));

        backend
            .save_entity_embeddings(&embeddings)
            .await
            .expect("should succeed");
        backend
            .create_checkpoint("checkpoint1")
            .await
            .expect("should succeed");

        // Modify embeddings
        let mut new_embeddings = HashMap::new();
        new_embeddings.insert("entity2".to_string(), Vector::new(vec![4.0, 5.0, 6.0]));
        backend
            .save_entity_embeddings(&new_embeddings)
            .await
            .expect("should succeed");

        // Restore checkpoint
        backend
            .restore_checkpoint("checkpoint1")
            .await
            .expect("should succeed");
        let restored = backend
            .load_entity_embeddings()
            .await
            .expect("should succeed");

        assert_eq!(restored.len(), 1);
        assert!(restored.contains_key("entity1"));
    }
}