dakera-storage 0.10.1

Storage backends for the Dakera AI memory platform
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
//! L2 Disk Cache using RocksDB
//!
//! Provides persistent local caching between L1 RAM cache (moka) and L3 object storage (S3).
//! Optimized for NVMe SSDs with configurable cache size limits.

use std::path::Path;
use std::sync::Arc;

use rocksdb::{Options, DB};
use serde::{Deserialize, Serialize};
use tracing::{debug, warn};

use common::{DakeraError, Result, Vector, VectorId};

/// Configuration for the disk cache
#[derive(Debug, Clone)]
pub struct DiskCacheConfig {
    /// Path to the RocksDB database
    pub path: String,
    /// Maximum cache size in bytes (0 = unlimited)
    pub max_size_bytes: u64,
    /// Enable compression (LZ4)
    pub compression: bool,
    /// Write buffer size in bytes
    pub write_buffer_size: usize,
    /// Max number of write buffers
    pub max_write_buffer_number: i32,
}

impl Default for DiskCacheConfig {
    fn default() -> Self {
        Self {
            path: "./cache".to_string(),
            max_size_bytes: 10 * 1024 * 1024 * 1024, // 10GB default
            compression: true,
            write_buffer_size: 64 * 1024 * 1024, // 64MB
            max_write_buffer_number: 3,
        }
    }
}

/// Cache entry with metadata for eviction
#[derive(Debug, Serialize, Deserialize)]
struct CacheEntry {
    vector: Vector,
    access_count: u64,
    created_at: u64,
}

/// L2 Disk Cache backed by RocksDB
pub struct DiskCache {
    db: Arc<DB>,
    #[allow(dead_code)]
    config: DiskCacheConfig,
}

impl DiskCache {
    /// Create a new disk cache
    pub fn new(config: DiskCacheConfig) -> Result<Self> {
        let mut opts = Options::default();
        opts.create_if_missing(true);
        opts.set_write_buffer_size(config.write_buffer_size);
        opts.set_max_write_buffer_number(config.max_write_buffer_number);

        if config.compression {
            opts.set_compression_type(rocksdb::DBCompressionType::Lz4);
        }

        // Optimize for SSD
        opts.set_level_compaction_dynamic_level_bytes(true);
        opts.set_max_background_jobs(4);

        let db = DB::open(&opts, &config.path)
            .map_err(|e| DakeraError::Storage(format!("Failed to open RocksDB: {}", e)))?;

        debug!(path = %config.path, "Disk cache initialized");

        Ok(Self {
            db: Arc::new(db),
            config,
        })
    }

    /// Open existing cache or create new one
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let config = DiskCacheConfig {
            path: path.as_ref().to_string_lossy().to_string(),
            ..Default::default()
        };
        Self::new(config)
    }

    /// Generate cache key from namespace and vector ID
    fn make_key(namespace: &str, id: &VectorId) -> Vec<u8> {
        format!("{}:{}", namespace, id).into_bytes()
    }

    /// Generate namespace prefix for scanning
    fn namespace_prefix(namespace: &str) -> Vec<u8> {
        format!("{}:", namespace).into_bytes()
    }

    /// Insert a vector into the cache
    pub fn put(&self, namespace: &str, vector: &Vector) -> Result<()> {
        let key = Self::make_key(namespace, &vector.id);
        let entry = CacheEntry {
            vector: vector.clone(),
            access_count: 1,
            created_at: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        };

        let value = serde_json::to_vec(&entry)
            .map_err(|e| DakeraError::Storage(format!("Failed to serialize cache entry: {}", e)))?;

        self.db
            .put(&key, &value)
            .map_err(|e| DakeraError::Storage(format!("Failed to write to disk cache: {}", e)))?;

        debug!(namespace = %namespace, id = %vector.id, "Cached vector to disk");
        Ok(())
    }

    /// Insert multiple vectors into the cache
    pub fn put_batch(&self, namespace: &str, vectors: &[Vector]) -> Result<usize> {
        let mut batch = rocksdb::WriteBatch::default();
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        for vector in vectors {
            let key = Self::make_key(namespace, &vector.id);
            let entry = CacheEntry {
                vector: vector.clone(),
                access_count: 1,
                created_at: now,
            };

            let value = serde_json::to_vec(&entry).map_err(|e| {
                DakeraError::Storage(format!("Failed to serialize cache entry: {}", e))
            })?;

            batch.put(&key, &value);
        }

        let count = vectors.len();
        self.db.write(batch).map_err(|e| {
            DakeraError::Storage(format!("Failed to write batch to disk cache: {}", e))
        })?;

        debug!(namespace = %namespace, count = count, "Batch cached vectors to disk");
        Ok(count)
    }

    /// Get a vector from the cache
    pub fn get(&self, namespace: &str, id: &VectorId) -> Result<Option<Vector>> {
        let key = Self::make_key(namespace, id);

        match self.db.get(&key) {
            Ok(Some(value)) => {
                let entry: CacheEntry = serde_json::from_slice(&value).map_err(|e| {
                    DakeraError::Storage(format!("Failed to deserialize cache entry: {}", e))
                })?;
                Ok(Some(entry.vector))
            }
            Ok(None) => Ok(None),
            Err(e) => {
                warn!(error = %e, "Failed to read from disk cache");
                Ok(None)
            }
        }
    }

    /// Get multiple vectors from the cache
    pub fn get_batch(&self, namespace: &str, ids: &[VectorId]) -> Result<Vec<Vector>> {
        let keys: Vec<Vec<u8>> = ids.iter().map(|id| Self::make_key(namespace, id)).collect();

        let results = self.db.multi_get(&keys);
        let mut vectors = Vec::with_capacity(ids.len());

        for result in results {
            if let Ok(Some(value)) = result {
                if let Ok(entry) = serde_json::from_slice::<CacheEntry>(&value) {
                    vectors.push(entry.vector);
                }
            }
        }

        Ok(vectors)
    }

    /// Get all vectors in a namespace
    pub fn get_all(&self, namespace: &str) -> Result<Vec<Vector>> {
        let prefix = Self::namespace_prefix(namespace);
        let mut vectors = Vec::new();

        let iter = self.db.prefix_iterator(&prefix);
        for item in iter {
            match item {
                Ok((key, value)) => {
                    // Check if key still starts with prefix (RocksDB iterators can overshoot)
                    if !key.starts_with(&prefix) {
                        break;
                    }

                    if let Ok(entry) = serde_json::from_slice::<CacheEntry>(&value) {
                        vectors.push(entry.vector);
                    }
                }
                Err(e) => {
                    warn!(error = %e, "Error iterating disk cache");
                    break;
                }
            }
        }

        Ok(vectors)
    }

    /// Delete a vector from the cache
    pub fn delete(&self, namespace: &str, id: &VectorId) -> Result<bool> {
        let key = Self::make_key(namespace, id);

        // Check if exists first
        let existed = self
            .db
            .get(&key)
            .map_err(|e| DakeraError::Storage(format!("Failed to check disk cache: {}", e)))?
            .is_some();

        if existed {
            self.db.delete(&key).map_err(|e| {
                DakeraError::Storage(format!("Failed to delete from disk cache: {}", e))
            })?;
        }

        Ok(existed)
    }

    /// Delete multiple vectors from the cache
    pub fn delete_batch(&self, namespace: &str, ids: &[VectorId]) -> Result<usize> {
        let mut batch = rocksdb::WriteBatch::default();
        let mut count = 0;

        for id in ids {
            let key = Self::make_key(namespace, id);
            if self.db.get(&key).ok().flatten().is_some() {
                batch.delete(&key);
                count += 1;
            }
        }

        self.db.write(batch).map_err(|e| {
            DakeraError::Storage(format!("Failed to delete batch from disk cache: {}", e))
        })?;

        Ok(count)
    }

    /// Clear all entries in a namespace
    pub fn clear_namespace(&self, namespace: &str) -> Result<usize> {
        let prefix = Self::namespace_prefix(namespace);
        let mut batch = rocksdb::WriteBatch::default();
        let mut count = 0;

        let iter = self.db.prefix_iterator(&prefix);
        for item in iter {
            match item {
                Ok((key, _)) => {
                    if !key.starts_with(&prefix) {
                        break;
                    }
                    batch.delete(&key);
                    count += 1;
                }
                Err(_) => break,
            }
        }

        if count > 0 {
            self.db.write(batch).map_err(|e| {
                DakeraError::Storage(format!("Failed to clear namespace from disk cache: {}", e))
            })?;
        }

        debug!(namespace = %namespace, count = count, "Cleared namespace from disk cache");
        Ok(count)
    }

    /// Get approximate size of the cache in bytes
    pub fn approximate_size(&self) -> u64 {
        self.db
            .property_int_value("rocksdb.estimate-live-data-size")
            .ok()
            .flatten()
            .unwrap_or(0)
    }

    /// Get cache statistics
    pub fn stats(&self) -> DiskCacheStats {
        DiskCacheStats {
            approximate_size_bytes: self.approximate_size(),
            approximate_num_keys: self
                .db
                .property_int_value("rocksdb.estimate-num-keys")
                .ok()
                .flatten()
                .unwrap_or(0),
        }
    }

    /// Flush all pending writes to disk
    pub fn flush(&self) -> Result<()> {
        self.db
            .flush()
            .map_err(|e| DakeraError::Storage(format!("Failed to flush disk cache: {}", e)))
    }
}

/// Statistics for the disk cache
#[derive(Debug, Clone)]
pub struct DiskCacheStats {
    pub approximate_size_bytes: u64,
    pub approximate_num_keys: u64,
}

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

    fn create_test_cache() -> (DiskCache, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let config = DiskCacheConfig {
            path: temp_dir.path().to_string_lossy().to_string(),
            ..Default::default()
        };
        let cache = DiskCache::new(config).unwrap();
        (cache, temp_dir)
    }

    fn test_vector(id: &str) -> Vector {
        Vector {
            id: id.to_string(),
            values: vec![1.0, 2.0, 3.0],
            metadata: None,
            ttl_seconds: None,
            expires_at: None,
        }
    }

    #[test]
    fn test_put_and_get() {
        let (cache, _dir) = create_test_cache();
        let namespace = "test";
        let vector = test_vector("v1");

        cache.put(namespace, &vector).unwrap();
        let result = cache.get(namespace, &"v1".to_string()).unwrap();

        assert!(result.is_some());
        let retrieved = result.unwrap();
        assert_eq!(retrieved.id, "v1");
        assert_eq!(retrieved.values, vec![1.0, 2.0, 3.0]);
    }

    #[test]
    fn test_get_nonexistent() {
        let (cache, _dir) = create_test_cache();
        let result = cache.get("test", &"nonexistent".to_string()).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_batch_operations() {
        let (cache, _dir) = create_test_cache();
        let namespace = "test";
        let vectors = vec![test_vector("v1"), test_vector("v2"), test_vector("v3")];

        let count = cache.put_batch(namespace, &vectors).unwrap();
        assert_eq!(count, 3);

        let ids: Vec<String> = vec!["v1".to_string(), "v2".to_string(), "v3".to_string()];
        let retrieved = cache.get_batch(namespace, &ids).unwrap();
        assert_eq!(retrieved.len(), 3);
    }

    #[test]
    fn test_get_all() {
        let (cache, _dir) = create_test_cache();
        let namespace = "test";
        let vectors = vec![test_vector("v1"), test_vector("v2")];

        cache.put_batch(namespace, &vectors).unwrap();
        let all = cache.get_all(namespace).unwrap();
        assert_eq!(all.len(), 2);
    }

    #[test]
    fn test_delete() {
        let (cache, _dir) = create_test_cache();
        let namespace = "test";
        let vector = test_vector("v1");

        cache.put(namespace, &vector).unwrap();
        assert!(cache.get(namespace, &"v1".to_string()).unwrap().is_some());

        let deleted = cache.delete(namespace, &"v1".to_string()).unwrap();
        assert!(deleted);

        assert!(cache.get(namespace, &"v1".to_string()).unwrap().is_none());
    }

    #[test]
    fn test_delete_batch() {
        let (cache, _dir) = create_test_cache();
        let namespace = "test";
        let vectors = vec![test_vector("v1"), test_vector("v2"), test_vector("v3")];

        cache.put_batch(namespace, &vectors).unwrap();

        let ids = vec!["v1".to_string(), "v2".to_string()];
        let deleted = cache.delete_batch(namespace, &ids).unwrap();
        assert_eq!(deleted, 2);

        assert!(cache.get(namespace, &"v1".to_string()).unwrap().is_none());
        assert!(cache.get(namespace, &"v2".to_string()).unwrap().is_none());
        assert!(cache.get(namespace, &"v3".to_string()).unwrap().is_some());
    }

    #[test]
    fn test_clear_namespace() {
        let (cache, _dir) = create_test_cache();
        let vectors = vec![test_vector("v1"), test_vector("v2")];

        cache.put_batch("ns1", &vectors).unwrap();
        cache.put_batch("ns2", &vectors).unwrap();

        let cleared = cache.clear_namespace("ns1").unwrap();
        assert_eq!(cleared, 2);

        assert!(cache.get_all("ns1").unwrap().is_empty());
        assert_eq!(cache.get_all("ns2").unwrap().len(), 2);
    }

    #[test]
    fn test_namespace_isolation() {
        let (cache, _dir) = create_test_cache();
        let vector = test_vector("v1");

        cache.put("ns1", &vector).unwrap();
        cache.put("ns2", &vector).unwrap();

        assert!(cache.get("ns1", &"v1".to_string()).unwrap().is_some());
        assert!(cache.get("ns2", &"v1".to_string()).unwrap().is_some());

        cache.delete("ns1", &"v1".to_string()).unwrap();

        assert!(cache.get("ns1", &"v1".to_string()).unwrap().is_none());
        assert!(cache.get("ns2", &"v1".to_string()).unwrap().is_some());
    }

    #[test]
    fn test_stats() {
        let (cache, _dir) = create_test_cache();
        let vectors = vec![test_vector("v1"), test_vector("v2")];

        cache.put_batch("test", &vectors).unwrap();
        cache.flush().unwrap();

        let stats = cache.stats();
        // Stats are approximate, just check they're available
        let _ = stats.approximate_num_keys;
    }
}