rrag 0.1.0-alpha.2

High-performance Rust framework for Retrieval-Augmented Generation with pluggable components, async-first design, and comprehensive observability
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
//! # RRAG Storage System
//!
//! Pluggable storage backends with async I/O and efficient serialization.
//! Designed for Rust's ownership model and zero-copy operations where possible.

use crate::{Document, DocumentChunk, Embedding, RragError, RragResult};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::fs;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

/// Storage entry that can contain documents, chunks, or embeddings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StorageEntry {
    Document(Document),
    Chunk(DocumentChunk),
    Embedding(Embedding),
    Metadata(HashMap<String, serde_json::Value>),
}

/// Storage key for efficient lookups
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct StorageKey {
    /// Entry type
    pub entry_type: EntryType,

    /// Unique identifier
    pub id: String,

    /// Optional namespace/collection
    pub namespace: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EntryType {
    Document,
    Chunk,
    Embedding,
    Metadata,
}

impl StorageKey {
    pub fn document(id: impl Into<String>) -> Self {
        Self {
            entry_type: EntryType::Document,
            id: id.into(),
            namespace: None,
        }
    }

    pub fn chunk(document_id: impl Into<String>, chunk_index: usize) -> Self {
        Self {
            entry_type: EntryType::Chunk,
            id: format!("{}_{}", document_id.into(), chunk_index),
            namespace: None,
        }
    }

    pub fn embedding(id: impl Into<String>) -> Self {
        Self {
            entry_type: EntryType::Embedding,
            id: id.into(),
            namespace: None,
        }
    }

    pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
        self.namespace = Some(namespace.into());
        self
    }

    /// Generate a storage path for file-based systems
    pub fn to_path(&self) -> PathBuf {
        let type_str = match self.entry_type {
            EntryType::Document => "documents",
            EntryType::Chunk => "chunks",
            EntryType::Embedding => "embeddings",
            EntryType::Metadata => "metadata",
        };

        let mut path = PathBuf::from(type_str);

        if let Some(namespace) = &self.namespace {
            path.push(namespace);
        }

        path.push(format!("{}.json", self.id));
        path
    }
}

/// Storage query for bulk operations
#[derive(Debug, Clone)]
pub struct StorageQuery {
    /// Entry type filter
    pub entry_type: Option<EntryType>,

    /// Namespace filter
    pub namespace: Option<String>,

    /// Key prefix filter
    pub key_prefix: Option<String>,

    /// Metadata filters
    pub metadata_filters: HashMap<String, serde_json::Value>,

    /// Maximum results
    pub limit: Option<usize>,

    /// Offset for pagination
    pub offset: Option<usize>,
}

impl StorageQuery {
    pub fn new() -> Self {
        Self {
            entry_type: None,
            namespace: None,
            key_prefix: None,
            metadata_filters: HashMap::new(),
            limit: None,
            offset: None,
        }
    }

    pub fn documents() -> Self {
        Self::new().with_entry_type(EntryType::Document)
    }

    pub fn chunks() -> Self {
        Self::new().with_entry_type(EntryType::Chunk)
    }

    pub fn embeddings() -> Self {
        Self::new().with_entry_type(EntryType::Embedding)
    }

    pub fn with_entry_type(mut self, entry_type: EntryType) -> Self {
        self.entry_type = Some(entry_type);
        self
    }

    pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
        self.namespace = Some(namespace.into());
        self
    }

    pub fn with_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.key_prefix = Some(prefix.into());
        self
    }

    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    pub fn with_offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);
        self
    }
}

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

/// Core storage trait for different backends
#[async_trait]
pub trait Storage: Send + Sync {
    /// Storage backend name
    fn name(&self) -> &str;

    /// Store an entry
    async fn put(&self, key: &StorageKey, entry: &StorageEntry) -> RragResult<()>;

    /// Retrieve an entry
    async fn get(&self, key: &StorageKey) -> RragResult<Option<StorageEntry>>;

    /// Delete an entry
    async fn delete(&self, key: &StorageKey) -> RragResult<bool>;

    /// Check if an entry exists
    async fn exists(&self, key: &StorageKey) -> RragResult<bool>;

    /// List keys matching a query
    async fn list_keys(&self, query: &StorageQuery) -> RragResult<Vec<StorageKey>>;

    /// Bulk get operation
    async fn get_many(
        &self,
        keys: &[StorageKey],
    ) -> RragResult<Vec<(StorageKey, Option<StorageEntry>)>>;

    /// Bulk put operation
    async fn put_many(&self, entries: &[(StorageKey, StorageEntry)]) -> RragResult<()>;

    /// Bulk delete operation
    async fn delete_many(&self, keys: &[StorageKey]) -> RragResult<usize>;

    /// Clear all entries (optional)
    async fn clear(&self) -> RragResult<()> {
        Err(RragError::storage(
            "clear",
            std::io::Error::new(
                std::io::ErrorKind::Unsupported,
                "Clear operation not supported",
            ),
        ))
    }

    /// Get storage statistics
    async fn stats(&self) -> RragResult<StorageStats>;

    /// Health check
    async fn health_check(&self) -> RragResult<bool>;
}

/// Storage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageStats {
    /// Total number of entries
    pub total_entries: usize,

    /// Entries by type
    pub entries_by_type: HashMap<String, usize>,

    /// Storage size in bytes
    pub size_bytes: u64,

    /// Available space in bytes (if applicable)
    pub available_bytes: Option<u64>,

    /// Backend type
    pub backend_type: String,

    /// Last updated
    pub last_updated: chrono::DateTime<chrono::Utc>,
}

/// In-memory storage implementation
pub struct InMemoryStorage {
    /// Internal storage map
    data: Arc<tokio::sync::RwLock<HashMap<StorageKey, StorageEntry>>>,

    /// Configuration
    config: MemoryStorageConfig,
}

#[derive(Debug, Clone)]
pub struct MemoryStorageConfig {
    /// Maximum number of entries
    pub max_entries: Option<usize>,

    /// Maximum memory usage in bytes
    pub max_memory_bytes: Option<u64>,
}

impl Default for MemoryStorageConfig {
    fn default() -> Self {
        Self {
            max_entries: Some(100_000),
            max_memory_bytes: Some(1_000_000_000), // 1GB
        }
    }
}

impl InMemoryStorage {
    pub fn new() -> Self {
        Self {
            data: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
            config: MemoryStorageConfig::default(),
        }
    }

    pub fn with_config(config: MemoryStorageConfig) -> Self {
        Self {
            data: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
            config,
        }
    }

    /// Check if we're within memory limits
    async fn check_limits(&self) -> RragResult<()> {
        let data = self.data.read().await;

        if let Some(max_entries) = self.config.max_entries {
            if data.len() >= max_entries {
                return Err(RragError::storage(
                    "memory_limit",
                    std::io::Error::new(
                        std::io::ErrorKind::OutOfMemory,
                        format!("Exceeded maximum entries: {}", max_entries),
                    ),
                ));
            }
        }

        Ok(())
    }

    /// Filter entries based on query
    fn matches_query(&self, key: &StorageKey, query: &StorageQuery) -> bool {
        // Check entry type
        if let Some(entry_type) = &query.entry_type {
            if key.entry_type != *entry_type {
                return false;
            }
        }

        // Check namespace
        if let Some(namespace) = &query.namespace {
            match &key.namespace {
                Some(key_ns) if key_ns == namespace => {}
                None if namespace.is_empty() => {}
                _ => return false,
            }
        }

        // Check prefix
        if let Some(prefix) = &query.key_prefix {
            if !key.id.starts_with(prefix) {
                return false;
            }
        }

        true
    }
}

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

#[async_trait]
impl Storage for InMemoryStorage {
    fn name(&self) -> &str {
        "in_memory"
    }

    async fn put(&self, key: &StorageKey, entry: &StorageEntry) -> RragResult<()> {
        self.check_limits().await?;

        let mut data = self.data.write().await;
        data.insert(key.clone(), entry.clone());
        Ok(())
    }

    async fn get(&self, key: &StorageKey) -> RragResult<Option<StorageEntry>> {
        let data = self.data.read().await;
        Ok(data.get(key).cloned())
    }

    async fn delete(&self, key: &StorageKey) -> RragResult<bool> {
        let mut data = self.data.write().await;
        Ok(data.remove(key).is_some())
    }

    async fn exists(&self, key: &StorageKey) -> RragResult<bool> {
        let data = self.data.read().await;
        Ok(data.contains_key(key))
    }

    async fn list_keys(&self, query: &StorageQuery) -> RragResult<Vec<StorageKey>> {
        let data = self.data.read().await;
        let mut keys: Vec<StorageKey> = data
            .keys()
            .filter(|key| self.matches_query(key, query))
            .cloned()
            .collect();

        // Apply offset and limit
        if let Some(offset) = query.offset {
            if offset < keys.len() {
                keys = keys.into_iter().skip(offset).collect();
            } else {
                keys.clear();
            }
        }

        if let Some(limit) = query.limit {
            keys.truncate(limit);
        }

        Ok(keys)
    }

    async fn get_many(
        &self,
        keys: &[StorageKey],
    ) -> RragResult<Vec<(StorageKey, Option<StorageEntry>)>> {
        let data = self.data.read().await;
        let results = keys
            .iter()
            .map(|key| (key.clone(), data.get(key).cloned()))
            .collect();
        Ok(results)
    }

    async fn put_many(&self, entries: &[(StorageKey, StorageEntry)]) -> RragResult<()> {
        self.check_limits().await?;

        let mut data = self.data.write().await;
        for (key, entry) in entries {
            data.insert(key.clone(), entry.clone());
        }
        Ok(())
    }

    async fn delete_many(&self, keys: &[StorageKey]) -> RragResult<usize> {
        let mut data = self.data.write().await;
        let mut deleted = 0;
        for key in keys {
            if data.remove(key).is_some() {
                deleted += 1;
            }
        }
        Ok(deleted)
    }

    async fn clear(&self) -> RragResult<()> {
        let mut data = self.data.write().await;
        data.clear();
        Ok(())
    }

    async fn stats(&self) -> RragResult<StorageStats> {
        let data = self.data.read().await;

        let mut entries_by_type = HashMap::new();
        for key in data.keys() {
            let type_str = match key.entry_type {
                EntryType::Document => "documents",
                EntryType::Chunk => "chunks",
                EntryType::Embedding => "embeddings",
                EntryType::Metadata => "metadata",
            };
            *entries_by_type.entry(type_str.to_string()).or_insert(0) += 1;
        }

        // Estimate memory usage (rough calculation)
        let estimated_size = data.len() * 1024; // Rough estimate per entry

        Ok(StorageStats {
            total_entries: data.len(),
            entries_by_type,
            size_bytes: estimated_size as u64,
            available_bytes: self
                .config
                .max_memory_bytes
                .map(|max| max - estimated_size as u64),
            backend_type: "in_memory".to_string(),
            last_updated: chrono::Utc::now(),
        })
    }

    async fn health_check(&self) -> RragResult<bool> {
        // Simple health check - try to read the data
        let _data = self.data.read().await;
        Ok(true)
    }
}

/// File-based storage implementation
pub struct FileStorage {
    /// Base directory for storage
    base_dir: PathBuf,

    /// Configuration
    config: FileStorageConfig,
}

#[derive(Debug, Clone)]
pub struct FileStorageConfig {
    /// Whether to create directories automatically
    pub create_dirs: bool,

    /// File permissions (Unix only)
    pub file_permissions: Option<u32>,

    /// Whether to use compression
    pub compress: bool,

    /// Sync writes to disk immediately
    pub sync_writes: bool,
}

impl Default for FileStorageConfig {
    fn default() -> Self {
        Self {
            create_dirs: true,
            file_permissions: None,
            compress: false,
            sync_writes: false,
        }
    }
}

impl FileStorage {
    pub async fn new(base_dir: impl AsRef<Path>) -> RragResult<Self> {
        let base_dir = base_dir.as_ref().to_path_buf();

        if !base_dir.exists() {
            fs::create_dir_all(&base_dir)
                .await
                .map_err(|e| RragError::storage("create_directory", e))?;
        }

        Ok(Self {
            base_dir,
            config: FileStorageConfig::default(),
        })
    }

    pub async fn with_config(
        base_dir: impl AsRef<Path>,
        config: FileStorageConfig,
    ) -> RragResult<Self> {
        let base_dir = base_dir.as_ref().to_path_buf();

        if config.create_dirs && !base_dir.exists() {
            fs::create_dir_all(&base_dir)
                .await
                .map_err(|e| RragError::storage("create_directory", e))?;
        }

        Ok(Self { base_dir, config })
    }

    /// Get the full file path for a storage key
    fn get_file_path(&self, key: &StorageKey) -> PathBuf {
        self.base_dir.join(key.to_path())
    }

    /// Ensure parent directory exists
    async fn ensure_parent_dir(&self, file_path: &Path) -> RragResult<()> {
        if let Some(parent) = file_path.parent() {
            if !parent.exists() {
                fs::create_dir_all(parent)
                    .await
                    .map_err(|e| RragError::storage("create_parent_directory", e))?;
            }
        }
        Ok(())
    }
}

#[async_trait]
impl Storage for FileStorage {
    fn name(&self) -> &str {
        "file_system"
    }

    async fn put(&self, key: &StorageKey, entry: &StorageEntry) -> RragResult<()> {
        let file_path = self.get_file_path(key);
        self.ensure_parent_dir(&file_path).await?;

        let json_data =
            serde_json::to_vec_pretty(entry).map_err(|e| RragError::storage("serialize", e))?;

        let mut file = fs::File::create(&file_path)
            .await
            .map_err(|e| RragError::storage("create_file", e))?;

        file.write_all(&json_data)
            .await
            .map_err(|e| RragError::storage("write_file", e))?;

        if self.config.sync_writes {
            file.sync_all()
                .await
                .map_err(|e| RragError::storage("sync_file", e))?;
        }

        Ok(())
    }

    async fn get(&self, key: &StorageKey) -> RragResult<Option<StorageEntry>> {
        let file_path = self.get_file_path(key);

        if !file_path.exists() {
            return Ok(None);
        }

        let mut file = fs::File::open(&file_path)
            .await
            .map_err(|e| RragError::storage("open_file", e))?;

        let mut contents = Vec::new();
        file.read_to_end(&mut contents)
            .await
            .map_err(|e| RragError::storage("read_file", e))?;

        let entry =
            serde_json::from_slice(&contents).map_err(|e| RragError::storage("deserialize", e))?;

        Ok(Some(entry))
    }

    async fn delete(&self, key: &StorageKey) -> RragResult<bool> {
        let file_path = self.get_file_path(key);

        if !file_path.exists() {
            return Ok(false);
        }

        fs::remove_file(&file_path)
            .await
            .map_err(|e| RragError::storage("delete_file", e))?;

        Ok(true)
    }

    async fn exists(&self, key: &StorageKey) -> RragResult<bool> {
        let file_path = self.get_file_path(key);
        Ok(file_path.exists())
    }

    async fn list_keys(&self, _query: &StorageQuery) -> RragResult<Vec<StorageKey>> {
        // This is a simplified implementation
        // In production, you'd want more efficient directory traversal
        let keys = Vec::new();

        // For now, return empty - would need recursive directory walking
        // This is a limitation of the simple file storage implementation
        Ok(keys)
    }

    async fn get_many(
        &self,
        keys: &[StorageKey],
    ) -> RragResult<Vec<(StorageKey, Option<StorageEntry>)>> {
        let mut results = Vec::with_capacity(keys.len());

        for key in keys {
            let entry = self.get(key).await?;
            results.push((key.clone(), entry));
        }

        Ok(results)
    }

    async fn put_many(&self, entries: &[(StorageKey, StorageEntry)]) -> RragResult<()> {
        for (key, entry) in entries {
            self.put(key, entry).await?;
        }
        Ok(())
    }

    async fn delete_many(&self, keys: &[StorageKey]) -> RragResult<usize> {
        let mut deleted = 0;

        for key in keys {
            if self.delete(key).await? {
                deleted += 1;
            }
        }

        Ok(deleted)
    }

    async fn stats(&self) -> RragResult<StorageStats> {
        // Calculate directory size and file counts
        // This is a simplified implementation
        Ok(StorageStats {
            total_entries: 0, // Would need directory traversal
            entries_by_type: HashMap::new(),
            size_bytes: 0,
            available_bytes: None,
            backend_type: "file_system".to_string(),
            last_updated: chrono::Utc::now(),
        })
    }

    async fn health_check(&self) -> RragResult<bool> {
        // Check if base directory is accessible
        Ok(self.base_dir.exists() && self.base_dir.is_dir())
    }
}

/// High-level storage service with caching and batching
pub struct StorageService {
    /// Active storage backend
    storage: Arc<dyn Storage>,

    /// Service configuration
    #[allow(dead_code)]
    config: StorageServiceConfig,
}

#[derive(Debug, Clone)]
pub struct StorageServiceConfig {
    /// Enable write batching
    pub enable_batching: bool,

    /// Batch size for bulk operations
    pub batch_size: usize,

    /// Batch timeout in milliseconds
    pub batch_timeout_ms: u64,

    /// Enable read caching
    pub enable_caching: bool,

    /// Cache TTL in seconds
    pub cache_ttl_seconds: u64,
}

impl Default for StorageServiceConfig {
    fn default() -> Self {
        Self {
            enable_batching: true,
            batch_size: 100,
            batch_timeout_ms: 1000,
            enable_caching: false,
            cache_ttl_seconds: 300,
        }
    }
}

impl StorageService {
    pub fn new(storage: Arc<dyn Storage>) -> Self {
        Self {
            storage,
            config: StorageServiceConfig::default(),
        }
    }

    pub fn with_config(storage: Arc<dyn Storage>, config: StorageServiceConfig) -> Self {
        Self { storage, config }
    }

    /// Store a document
    pub async fn store_document(&self, document: &Document) -> RragResult<()> {
        let key = StorageKey::document(&document.id);
        let entry = StorageEntry::Document(document.clone());
        self.storage.put(&key, &entry).await
    }

    /// Store a chunk
    pub async fn store_chunk(&self, chunk: &DocumentChunk) -> RragResult<()> {
        let key = StorageKey::chunk(&chunk.document_id, chunk.chunk_index);
        let entry = StorageEntry::Chunk(chunk.clone());
        self.storage.put(&key, &entry).await
    }

    /// Store an embedding
    pub async fn store_embedding(&self, embedding: &Embedding) -> RragResult<()> {
        let key = StorageKey::embedding(&embedding.source_id);
        let entry = StorageEntry::Embedding(embedding.clone());
        self.storage.put(&key, &entry).await
    }

    /// Retrieve a document
    pub async fn get_document(&self, document_id: &str) -> RragResult<Option<Document>> {
        let key = StorageKey::document(document_id);
        match self.storage.get(&key).await? {
            Some(StorageEntry::Document(doc)) => Ok(Some(doc)),
            _ => Ok(None),
        }
    }

    /// Get storage statistics
    pub async fn get_stats(&self) -> RragResult<StorageStats> {
        self.storage.stats().await
    }

    /// Health check
    pub async fn health_check(&self) -> RragResult<bool> {
        self.storage.health_check().await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_in_memory_storage() {
        let storage = InMemoryStorage::new();

        let doc = Document::new("Test document");
        let key = StorageKey::document(&doc.id);
        let entry = StorageEntry::Document(doc.clone());

        // Test put and get
        storage.put(&key, &entry).await.unwrap();

        let retrieved = storage.get(&key).await.unwrap();
        assert!(retrieved.is_some());

        if let Some(StorageEntry::Document(retrieved_doc)) = retrieved {
            assert_eq!(retrieved_doc.id, doc.id);
            assert_eq!(retrieved_doc.content_str(), doc.content_str());
        }

        // Test exists
        assert!(storage.exists(&key).await.unwrap());

        // Test delete
        assert!(storage.delete(&key).await.unwrap());
        assert!(!storage.exists(&key).await.unwrap());
    }

    #[tokio::test]
    async fn test_file_storage() {
        let temp_dir = TempDir::new().unwrap();
        let storage = FileStorage::new(temp_dir.path()).await.unwrap();

        let doc = Document::new("Test document for file storage");
        let key = StorageKey::document(&doc.id);
        let entry = StorageEntry::Document(doc.clone());

        // Test put and get
        storage.put(&key, &entry).await.unwrap();

        let retrieved = storage.get(&key).await.unwrap();
        assert!(retrieved.is_some());

        if let Some(StorageEntry::Document(retrieved_doc)) = retrieved {
            assert_eq!(retrieved_doc.id, doc.id);
        }

        // Test file exists on disk
        let file_path = temp_dir.path().join(key.to_path());
        assert!(file_path.exists());
    }

    #[test]
    fn test_storage_key() {
        let doc_key = StorageKey::document("doc1");
        assert_eq!(doc_key.entry_type, EntryType::Document);
        assert_eq!(doc_key.id, "doc1");

        let chunk_key = StorageKey::chunk("doc1", 5);
        assert_eq!(chunk_key.entry_type, EntryType::Chunk);
        assert_eq!(chunk_key.id, "doc1_5");

        let ns_key = doc_key.with_namespace("test_namespace");
        assert_eq!(ns_key.namespace, Some("test_namespace".to_string()));
    }

    #[tokio::test]
    async fn test_storage_service() {
        let storage = Arc::new(InMemoryStorage::new());
        let service = StorageService::new(storage);

        let doc = Document::new("Test document for service");

        // Store document
        service.store_document(&doc).await.unwrap();

        // Retrieve document
        let retrieved = service.get_document(&doc.id).await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().id, doc.id);

        // Check stats
        let stats = service.get_stats().await.unwrap();
        assert_eq!(stats.total_entries, 1);
    }
}