motedb 0.1.2

AI-native embedded multimodal database for embodied intelligence (robots, AR glasses, industrial arms).
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
//! # Manifest: Atomic Multi-File Commit
//!
//! ## Problem
//! MoteDB writes multiple files during flush (data SST, indexes, etc).
//! Without atomicity, crashes can leave inconsistent state:
//! - Main data written, but indexes missing
//! - Some indexes written, others not
//! - OS buffer not flushed to disk
//!
//! ## Solution: Manifest File
//! Inspired by LevelDB/RocksDB, we use a manifest file as the single
//! atomic commit point:
//!
//! ```text
//! Flush Process:
//! 1. Write all data files (versioned names)
//!    - data_000001.sst
//!    - vector_000001.sst
//!    - spatial_000001.mmap
//!    - text_000001.sst
//! 2. fsync() all files
//! 3. Write MANIFEST-000001.tmp
//! 4. fsync() MANIFEST
//! 5. rename(MANIFEST.tmp → MANIFEST-CURRENT)  ← ATOMIC COMMIT POINT
//! 6. fsync() parent directory
//! ```text
//!
//! **Key Insight**: `rename()` is atomic on POSIX systems!
//! - Before rename: Old MANIFEST is valid, new files are orphans
//! - After rename: New MANIFEST is valid, all files are visible
//! - Crash anywhere: Either old state or new state (never inconsistent)

use crate::error::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::io::{BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::time::SystemTime;

/// Manifest: Records all active files in a database version
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Manifest {
    /// Monotonically increasing version number
    pub version: u64,
    
    /// LSM main data file
    pub lsm_file: Option<String>,
    
    /// Vector index files
    pub vector_indexes: HashMap<String, String>,
    
    /// Spatial index files
    pub spatial_indexes: HashMap<String, String>,
    
    /// Text index files
    pub text_indexes: HashMap<String, String>,
    
    /// Timestamp index files
    pub timestamp_indexes: HashMap<String, String>,
    
    /// Column value index files
    pub column_indexes: HashMap<String, String>,
    
    /// Checksum of all files (for integrity verification)
    pub checksum: u64,
    
    /// Creation timestamp
    pub timestamp: u64,
}

impl Manifest {
    /// Create a new manifest with given version
    pub fn new(version: u64) -> Self {
        Self {
            version,
            lsm_file: None,
            vector_indexes: HashMap::new(),
            spatial_indexes: HashMap::new(),
            text_indexes: HashMap::new(),
            timestamp_indexes: HashMap::new(),
            column_indexes: HashMap::new(),
            checksum: 0,
            timestamp: SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
        }
    }
    
    /// Calculate checksum of all files
    pub fn calculate_checksum(&mut self) -> Result<u64> {
        let mut hasher = 0u64;
        
        if let Some(ref file) = self.lsm_file {
            hasher = hasher.wrapping_add(Self::hash_string(file));
        }
        
        for (_, file) in &self.vector_indexes {
            hasher = hasher.wrapping_add(Self::hash_string(file));
        }
        
        for (_, file) in &self.spatial_indexes {
            hasher = hasher.wrapping_add(Self::hash_string(file));
        }
        
        for (_, file) in &self.text_indexes {
            hasher = hasher.wrapping_add(Self::hash_string(file));
        }
        
        for (_, file) in &self.timestamp_indexes {
            hasher = hasher.wrapping_add(Self::hash_string(file));
        }
        
        for (_, file) in &self.column_indexes {
            hasher = hasher.wrapping_add(Self::hash_string(file));
        }
        
        self.checksum = hasher;
        Ok(hasher)
    }
    
    /// Simple hash function for strings
    fn hash_string(s: &str) -> u64 {
        let mut hash = 0u64;
        for byte in s.bytes() {
            hash = hash.wrapping_mul(31).wrapping_add(byte as u64);
        }
        hash
    }
    
    /// Write manifest to temporary file (not yet committed)
    ///
    /// This is step 1 of atomic commit. The file is fully written and
    /// fsynced, but not yet visible (not renamed to MANIFEST-CURRENT).
    pub fn write_temp(&self, db_path: &Path) -> Result<PathBuf> {
        let temp_path = db_path.join(format!("MANIFEST-{:06}.tmp", self.version));
        
        let file = OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(&temp_path)?;
        
        let mut writer = BufWriter::new(file);
        
        // Serialize to JSON (human-readable for debugging)
        let json = serde_json::to_string_pretty(self)?;
        writer.write_all(json.as_bytes())?;
        
        // ✅ Flush buffer to OS
        writer.flush()?;
        
        // ✅ CRITICAL: fsync to ensure manifest is on disk
        writer.get_ref().sync_all()?;
        
        Ok(temp_path)
    }
    
    /// Atomic commit: rename temp manifest to MANIFEST-CURRENT
    ///
    /// This is the ATOMIC COMMIT POINT! After this rename:
    /// - The new manifest is visible
    /// - All referenced files become active
    /// - Old files can be garbage collected
    ///
    /// POSIX guarantees rename() is atomic, so we'll either see:
    /// - Old MANIFEST-CURRENT (crash before rename)
    /// - New MANIFEST-CURRENT (crash after rename)
    /// Never an inconsistent state!
    pub fn commit_atomic(temp_path: &Path, db_path: &Path) -> Result<()> {
        let current_path = db_path.join("MANIFEST-CURRENT");
        
        // ✅ Atomic rename (POSIX guarantee)
        std::fs::rename(temp_path, &current_path)?;
        
        // ✅ CRITICAL: fsync parent directory to ensure rename is persisted
        // Without this, the rename may only be in directory cache!
        #[cfg(unix)]
        {
            use std::os::unix::io::AsRawFd;
            let dir = File::open(db_path)?;
            unsafe {
                libc::fsync(dir.as_raw_fd());
            }
        }
        
        // On non-Unix, we can't fsync directory, but most modern filesystems
        // handle this reasonably well
        #[cfg(not(unix))]
        {
            // Best effort: open and sync the manifest file itself
            File::open(&current_path)?.sync_all()?;
        }
        
        Ok(())
    }
    
    /// Read the current manifest from MANIFEST-CURRENT
    pub fn read_current(db_path: &Path) -> Result<Self> {
        let current_path = db_path.join("MANIFEST-CURRENT");
        
        if !current_path.exists() {
            // No manifest yet, return empty manifest
            return Ok(Self::new(0));
        }
        
        let file = File::open(&current_path)?;
        let reader = BufReader::new(file);
        
        let manifest: Manifest = serde_json::from_reader(reader)?;
        
        Ok(manifest)
    }
    
    /// Full atomic write: temp write + commit
    pub fn write_atomic(&self, db_path: &Path) -> Result<()> {
        let temp_path = self.write_temp(db_path)?;
        Self::commit_atomic(&temp_path, db_path)?;
        Ok(())
    }
    
    /// Verify all files referenced in manifest exist
    pub fn verify_files(&self, db_path: &Path) -> Result<bool> {
        if let Some(ref file) = self.lsm_file {
            if !db_path.join(file).exists() {
                return Ok(false);
            }
        }
        
        for (_, file) in &self.vector_indexes {
            if !db_path.join(file).exists() {
                return Ok(false);
            }
        }
        
        for (_, file) in &self.spatial_indexes {
            if !db_path.join(file).exists() {
                return Ok(false);
            }
        }
        
        for (_, file) in &self.text_indexes {
            if !db_path.join(file).exists() {
                return Ok(false);
            }
        }
        
        for (_, file) in &self.timestamp_indexes {
            if !db_path.join(file).exists() {
                return Ok(false);
            }
        }
        
        for (_, file) in &self.column_indexes {
            if !db_path.join(file).exists() {
                return Ok(false);
            }
        }
        
        Ok(true)
    }
    
    /// List all orphan files (files not in manifest)
    pub fn find_orphans(&self, db_path: &Path) -> Result<Vec<PathBuf>> {
        let mut orphans = Vec::new();
        
        // Collect all files in manifest
        let mut active_files = std::collections::HashSet::new();
        if let Some(ref file) = self.lsm_file {
            active_files.insert(file.clone());
        }
        for (_, file) in &self.vector_indexes {
            active_files.insert(file.clone());
        }
        for (_, file) in &self.spatial_indexes {
            active_files.insert(file.clone());
        }
        for (_, file) in &self.text_indexes {
            active_files.insert(file.clone());
        }
        for (_, file) in &self.timestamp_indexes {
            active_files.insert(file.clone());
        }
        for (_, file) in &self.column_indexes {
            active_files.insert(file.clone());
        }
        
        // Scan directory for data files
        for entry in std::fs::read_dir(db_path)? {
            let entry = entry?;
            let path = entry.path();
            
            if path.is_file() {
                let filename = path.file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("");
                
                // Check if it's a data file (not manifest, not wal)
                if filename.ends_with(".sst") || 
                   filename.ends_with(".mmap") || 
                   filename.ends_with(".idx") ||
                   filename.ends_with(".bin") {
                    if !active_files.contains(filename) {
                        orphans.push(path);
                    }
                }
            }
        }
        
        Ok(orphans)
    }
    
    // ✅ P2: Version Garbage Collection
    
    /// List all manifest versions (for cleanup)
    pub fn list_all_versions(db_path: &Path) -> Result<Vec<u64>> {
        let mut versions = Vec::new();
        
        for entry in std::fs::read_dir(db_path)? {
            let entry = entry?;
            let path = entry.path();
            
            if path.is_file() {
                if let Some(filename) = path.file_name().and_then(|n| n.to_str()) {
                    if filename.starts_with("MANIFEST-") && filename != "MANIFEST-CURRENT" {
                        if let Some(version_str) = filename.strip_prefix("MANIFEST-") {
                            if let Ok(version) = version_str.parse::<u64>() {
                                versions.push(version);
                            }
                        }
                    }
                }
            }
        }
        
        versions.sort_unstable();
        Ok(versions)
    }
    
    /// Read a specific version of manifest
    pub fn read_version(db_path: &Path, version: u64) -> Result<Self> {
        let manifest_path = db_path.join(format!("MANIFEST-{:06}", version));
        
        if !manifest_path.exists() {
            return Err(crate::StorageError::FileNotFound(
                manifest_path
            ));
        }
        
        let file = File::open(&manifest_path)?;
        let reader = BufReader::new(file);
        let manifest: Manifest = serde_json::from_reader(reader)?;
        
        Ok(manifest)
    }
    
    /// Cleanup old versions, keeping the latest N versions
    pub fn cleanup_old_versions(db_path: &Path, keep_versions: usize) -> Result<usize> {
        if keep_versions == 0 {
            return Ok(0);
        }
        
        // Read current manifest to get active files
        let current = Self::read_current(db_path)?;
        let mut active_files = std::collections::HashSet::new();
        
        if let Some(ref file) = current.lsm_file {
            active_files.insert(file.clone());
        }
        for (_, file) in &current.vector_indexes {
            active_files.insert(file.clone());
        }
        for (_, file) in &current.spatial_indexes {
            active_files.insert(file.clone());
        }
        for (_, file) in &current.text_indexes {
            active_files.insert(file.clone());
        }
        for (_, file) in &current.timestamp_indexes {
            active_files.insert(file.clone());
        }
        for (_, file) in &current.column_indexes {
            active_files.insert(file.clone());
        }
        
        // Get all versions
        let mut all_versions = Self::list_all_versions(db_path)?;
        all_versions.sort_unstable();
        all_versions.reverse(); // Newest first
        
        if all_versions.len() <= keep_versions {
            return Ok(0); // Nothing to delete
        }
        
        // Keep the latest N versions, delete the rest
        let versions_to_delete = &all_versions[keep_versions..];
        let mut deleted_count = 0;
        
        for &version in versions_to_delete {
            // Read old manifest
            if let Ok(old_manifest) = Self::read_version(db_path, version) {
                // Delete files referenced by old manifest (if not in current)
                if let Some(ref file) = old_manifest.lsm_file {
                    if !active_files.contains(file) {
                        let _ = std::fs::remove_file(db_path.join(file));
                        deleted_count += 1;
                    }
                }
                
                for (_, file) in &old_manifest.vector_indexes {
                    if !active_files.contains(file) {
                        let _ = std::fs::remove_file(db_path.join(file));
                        deleted_count += 1;
                    }
                }
                
                for (_, file) in &old_manifest.spatial_indexes {
                    if !active_files.contains(file) {
                        let _ = std::fs::remove_file(db_path.join(file));
                        deleted_count += 1;
                    }
                }
                
                for (_, file) in &old_manifest.text_indexes {
                    if !active_files.contains(file) {
                        let _ = std::fs::remove_file(db_path.join(file));
                        deleted_count += 1;
                    }
                }
                
                for (_, file) in &old_manifest.timestamp_indexes {
                    if !active_files.contains(file) {
                        let _ = std::fs::remove_file(db_path.join(file));
                        deleted_count += 1;
                    }
                }
                
                for (_, file) in &old_manifest.column_indexes {
                    if !active_files.contains(file) {
                        let _ = std::fs::remove_file(db_path.join(file));
                        deleted_count += 1;
                    }
                }
                
                // Delete old manifest file
                let _ = std::fs::remove_file(db_path.join(format!("MANIFEST-{:06}", version)));
            }
        }
        
        Ok(deleted_count)
    }
    
    // ✅ P3: Incremental Backup
    
    /// Get diff between two versions (returns added/removed files)
    pub fn diff_versions(
        db_path: &Path,
        from_version: u64,
        to_version: u64,
    ) -> Result<(Vec<String>, Vec<String>)> {
        let from_manifest = Self::read_version(db_path, from_version)?;
        let to_manifest = Self::read_version(db_path, to_version)?;
        
        let mut from_files = std::collections::HashSet::new();
        let mut to_files = std::collections::HashSet::new();
        
        // Collect files from both manifests
        Self::collect_files(&from_manifest, &mut from_files);
        Self::collect_files(&to_manifest, &mut to_files);
        
        // Calculate diff
        let added: Vec<String> = to_files.difference(&from_files)
            .cloned()
            .collect();
        let removed: Vec<String> = from_files.difference(&to_files)
            .cloned()
            .collect();
        
        Ok((added, removed))
    }
    
    /// Backup incremental changes between versions
    pub fn backup_incremental(
        db_path: &Path,
        from_version: u64,
        to_version: u64,
        backup_path: &Path,
    ) -> Result<usize> {
        let (added_files, _removed) = Self::diff_versions(db_path, from_version, to_version)?;
        
        // Create backup directory
        std::fs::create_dir_all(backup_path)?;
        
        // Copy added files
        let mut copied_count = 0;
        for file in added_files {
            let src = db_path.join(&file);
            let dst = backup_path.join(&file);
            
            if src.exists() {
                // Create parent directories if needed
                if let Some(parent) = dst.parent() {
                    std::fs::create_dir_all(parent)?;
                }
                
                std::fs::copy(&src, &dst)?;
                copied_count += 1;
            }
        }
        
        // Copy target manifest
        let manifest_src = db_path.join(format!("MANIFEST-{:06}", to_version));
        let manifest_dst = backup_path.join(format!("MANIFEST-{:06}", to_version));
        if manifest_src.exists() {
            std::fs::copy(&manifest_src, &manifest_dst)?;
        }
        
        Ok(copied_count)
    }
    
    /// Restore from incremental backup
    pub fn restore_incremental(
        backup_path: &Path,
        version: u64,
        target_path: &Path,
    ) -> Result<usize> {
        // Create target directory
        std::fs::create_dir_all(target_path)?;
        
        // Read manifest from backup
        let manifest = Self::read_version(backup_path, version)?;
        
        let mut restored_count = 0;
        
        // Restore all files referenced in manifest
        for file in Self::get_all_files(&manifest) {
            let src = backup_path.join(&file);
            let dst = target_path.join(&file);
            
            if src.exists() {
                if let Some(parent) = dst.parent() {
                    std::fs::create_dir_all(parent)?;
                }
                
                std::fs::copy(&src, &dst)?;
                restored_count += 1;
            }
        }
        
        // Restore manifest and set as current
        let manifest_src = backup_path.join(format!("MANIFEST-{:06}", version));
        let manifest_dst = target_path.join("MANIFEST-CURRENT");
        if manifest_src.exists() {
            std::fs::copy(&manifest_src, &manifest_dst)?;
        }
        
        Ok(restored_count)
    }
    
    // Helper: Collect all files from manifest
    fn collect_files(manifest: &Manifest, files: &mut std::collections::HashSet<String>) {
        if let Some(ref file) = manifest.lsm_file {
            files.insert(file.clone());
        }
        for (_, file) in &manifest.vector_indexes {
            files.insert(file.clone());
        }
        for (_, file) in &manifest.spatial_indexes {
            files.insert(file.clone());
        }
        for (_, file) in &manifest.text_indexes {
            files.insert(file.clone());
        }
        for (_, file) in &manifest.timestamp_indexes {
            files.insert(file.clone());
        }
        for (_, file) in &manifest.column_indexes {
            files.insert(file.clone());
        }
    }
    
    // Helper: Get all files from manifest
    fn get_all_files(manifest: &Manifest) -> Vec<String> {
        let mut files = Vec::new();
        
        if let Some(ref file) = manifest.lsm_file {
            files.push(file.clone());
        }
        for (_, file) in &manifest.vector_indexes {
            files.push(file.clone());
        }
        for (_, file) in &manifest.spatial_indexes {
            files.push(file.clone());
        }
        for (_, file) in &manifest.text_indexes {
            files.push(file.clone());
        }
        for (_, file) in &manifest.timestamp_indexes {
            files.push(file.clone());
        }
        for (_, file) in &manifest.column_indexes {
            files.push(file.clone());
        }
        
        files
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    
    #[test]
    fn test_manifest_write_read() {
        let dir = TempDir::new().unwrap();
        let db_path = dir.path();
        
        let mut manifest = Manifest::new(1);
        manifest.lsm_file = Some("data_000001.sst".to_string());
        manifest.vector_indexes.insert("embeddings".to_string(), "vector_000001.sst".to_string());
        manifest.calculate_checksum().unwrap();
        
        manifest.write_atomic(db_path).unwrap();
        
        let read_manifest = Manifest::read_current(db_path).unwrap();
        assert_eq!(read_manifest.version, 1);
        assert_eq!(read_manifest.lsm_file, Some("data_000001.sst".to_string()));
        assert_eq!(read_manifest.checksum, manifest.checksum);
    }
    
    #[test]
    fn test_atomic_commit() {
        let dir = TempDir::new().unwrap();
        let db_path = dir.path();
        
        let manifest1 = Manifest::new(1);
        manifest1.write_atomic(db_path).unwrap();
        
        let manifest2 = Manifest::new(2);
        manifest2.write_atomic(db_path).unwrap();
        
        // Should read latest version
        let current = Manifest::read_current(db_path).unwrap();
        assert_eq!(current.version, 2);
    }
    
    #[test]
    fn test_orphan_detection() {
        let dir = TempDir::new().unwrap();
        let db_path = dir.path();
        
        // Create some files
        std::fs::write(db_path.join("data_000001.sst"), b"data").unwrap();
        std::fs::write(db_path.join("data_000002.sst"), b"data").unwrap();
        std::fs::write(db_path.join("vector_000001.sst"), b"vector").unwrap();
        
        // Manifest only references data_000002.sst
        let mut manifest = Manifest::new(1);
        manifest.lsm_file = Some("data_000002.sst".to_string());
        manifest.write_atomic(db_path).unwrap();
        
        // Should find 2 orphans
        let orphans = manifest.find_orphans(db_path).unwrap();
        assert_eq!(orphans.len(), 2);
    }
}