motedb 0.1.3

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
//! Chunked Token Dictionary with LRU Cache
//!
//! Design:
//! - Dictionary split into chunks (e.g., 10K entries per chunk)
//! - Each chunk is separately serialized and stored
//! - LRU cache for hot chunks in memory
//! - Lazy loading: only load chunks on demand
//!
//! Benefits:
//! - Reduced memory footprint for large dictionaries
//! - Faster startup (no need to load entire dictionary)
//! - Better cache locality

use crate::{Result, StorageError};
use crate::index::text_types::TermId;
use lru::LruCache;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::{File, create_dir_all};
use std::io::{Read, Write};
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::sync::Arc;

/// Default cache size: number of chunks to keep in memory
#[allow(dead_code)]
const DEFAULT_CACHE_SIZE: usize = 16;

/// A chunk of the dictionary
#[derive(Debug, Clone, Serialize, Deserialize)]
struct DictionaryChunk {
    /// Token -> TermId mapping for this chunk
    entries: HashMap<String, TermId>,
    
    /// Chunk ID
    chunk_id: usize,
    
    /// Dirty flag (needs flush)
    #[serde(skip)]
    dirty: bool,
}

impl DictionaryChunk {
    fn new(chunk_id: usize) -> Self {
        Self {
            entries: HashMap::new(),
            chunk_id,
            dirty: false,
        }
    }
    
    fn insert(&mut self, token: String, term_id: TermId) {
        self.entries.insert(token, term_id);
        self.dirty = true;
    }
    
    fn get(&self, token: &str) -> Option<TermId> {
        self.entries.get(token).copied()
    }
}

/// Metadata for the chunked dictionary
#[derive(Debug, Clone, Serialize, Deserialize)]
struct DictionaryMetadata {
    /// Total number of terms
    total_terms: usize,
    
    /// Number of chunks
    num_chunks: usize,
    
    /// Next available TermId
    next_term_id: TermId,
    
    /// Chunk index: token_prefix -> chunk_id
    /// This helps quickly locate which chunk a token might be in
    chunk_index: HashMap<String, usize>,
}

impl DictionaryMetadata {
    fn new() -> Self {
        Self {
            total_terms: 0,
            num_chunks: 0,
            next_term_id: 0,
            chunk_index: HashMap::new(),
        }
    }
}

/// Chunked token dictionary with LRU cache
pub struct ChunkedDictionary {
    /// Storage directory
    storage_dir: PathBuf,
    
    /// Metadata
    metadata: Arc<RwLock<DictionaryMetadata>>,
    
    /// LRU cache for hot chunks
    cache: Arc<RwLock<LruCache<usize, DictionaryChunk>>>,
    
    // ❌ Removed: reverse_map consumes too much memory
    // For reverse lookup (TermId -> Token), scan chunks on demand
}

impl ChunkedDictionary {
    /// Create or open a chunked dictionary
    pub fn new(storage_dir: PathBuf, cache_size: usize) -> Result<Self> {
        create_dir_all(&storage_dir)?;
        
        let meta_path = storage_dir.join("dict_meta.bin");
        let metadata = if meta_path.exists() {
            Self::load_metadata(&meta_path)?
        } else {
            DictionaryMetadata::new()
        };
        
        Ok(Self {
            storage_dir,
            metadata: Arc::new(RwLock::new(metadata)),
            cache: Arc::new(RwLock::new(LruCache::new(
                NonZeroUsize::new(cache_size.max(1)).unwrap()
            ))),
        })
    }
    
    /// Get or insert a token, returning its TermId
    /// 
    /// 🔧 OPTIMIZATION: Double-checked locking pattern
    /// 1. Fast path: Read-only check (shared lock)
    /// 2. Slow path: Load from disk + insert (exclusive lock)
    /// 
    /// Performance impact:
    /// - Cache hit: 1 read lock (vs 1 write lock) → 10-100x faster under contention
    /// - Cache miss: 1 read + 1 write lock (vs 1 write lock) → minimal overhead
    /// - Insert: 1 read + 1 write lock (vs 1 write lock) → minimal overhead
    pub fn get_or_insert(&self, token: &str) -> TermId {
        let chunk_id = self.guess_chunk_id(token);
        
        // 🚀 FAST PATH: Try read-only lookup first (hot path: 99%+ of calls)
        {
            let cache = self.cache.read();
            if let Some(chunk) = cache.peek(&chunk_id) {
                if let Some(term_id) = chunk.get(token) {
                    return term_id;
                }
            }
        }
        
        // 🐢 SLOW PATH: Not in cache, need to load/insert (cold path: <1% of calls)
        // Acquire write lock only when necessary
        {
            let mut cache = self.cache.write();
            
            // Double-check: Another thread might have loaded it
            if let Some(chunk) = cache.get_mut(&chunk_id) {
                if let Some(term_id) = chunk.get(token) {
                    return term_id;
                }
                // Chunk in cache but token not found, need to insert
                // Don't return here, fall through to insert_new_token
            } else {
                // Chunk not in cache, try loading from disk
                if let Ok(chunk) = self.load_chunk(chunk_id) {
                    if let Some(term_id) = chunk.get(token) {
                        cache.put(chunk_id, chunk);
                        return term_id;
                    }
                    // Chunk loaded but token not found, put it in cache for future inserts
                    cache.put(chunk_id, chunk);
                }
            }
        }
        
        // Token not found anywhere, insert new
        self.insert_new_token(token, chunk_id)
    }
    
    /// Get TermId for a token (returns None if not found)
    /// 
    /// 🔧 OPTIMIZATION: Read-first pattern for better concurrency
    pub fn get(&self, token: &str) -> Option<TermId> {
        let chunk_id = self.guess_chunk_id(token);
        
        
        // Fast path: Read-only lookup
        {
            let cache = self.cache.read();
            if let Some(chunk) = cache.peek(&chunk_id) {
                if let Some(term_id) = chunk.get(token) {
                    return Some(term_id);
                } 
            } 
        }
        
        // Slow path: Try to promote to cache (use get to update LRU)
        {
            let mut cache = self.cache.write();
            if let Some(chunk) = cache.get(&chunk_id) {
                let result = chunk.get(token);
                return result;
            }
        }
        
        // Really slow path: Load from disk
        if let Ok(chunk) = self.load_chunk(chunk_id) {
            let term_id = chunk.get(token);
            self.cache.write().put(chunk_id, chunk);
            return term_id;
        }
        
        None
    }
    
    /// Get token from TermId (reverse lookup, scans chunks on demand)
    pub fn get_token(&self, term_id: TermId) -> Option<String> {
        // Check cache first
        let cache = self.cache.read();
        for (_, chunk) in cache.iter() {
            for (token, tid) in &chunk.entries {
                if *tid == term_id {
                    return Some(token.clone());
                }
            }
        }
        drop(cache);
        
        // Scan all chunks (expensive, but rare operation)
        let meta = self.metadata.read();
        for chunk_id in 0..meta.num_chunks {
            if let Ok(chunk) = self.load_chunk(chunk_id) {
                for (token, tid) in &chunk.entries {
                    if *tid == term_id {
                        return Some(token.clone());
                    }
                }
            }
        }
        
        None
    }
    
    /// Total number of terms
    pub fn len(&self) -> usize {
        self.metadata.read().total_terms
    }
    
    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    
    /// Flush all dirty chunks to disk and clear cache to free memory
    pub fn flush(&self) -> Result<()> {
        let mut cache = self.cache.write();
        
        // Flush all dirty chunks
        for (chunk_id, chunk) in cache.iter() {
            if chunk.dirty {
                self.save_chunk(*chunk_id, chunk)?;
            }
        }
        
        // 🔧 CRITICAL FIX: Don't clear cache - it causes massive performance regression!
        // Problem: Clearing cache forces every subsequent get_or_insert() to reload from disk
        // Impact: Tokenize time: 0.008s → 7.5s (940x slower!)
        // Solution: Keep cache hot, just mark chunks as clean
        //
        // Before fix:
        //   cache.clear();  // ❌ Clears all cached chunks
        //
        // After fix:
        for (_, chunk) in cache.iter_mut() {
            chunk.dirty = false;  // ✅ Mark as clean, keep in cache
        }
        
        // Note: LRU will automatically evict old chunks if cache is full
        // 16 chunks × ~100KB = 1.6 MB memory (acceptable overhead for 2700% perf boost)
        
        // Save metadata
        let meta = self.metadata.read();
        self.save_metadata(&meta)?;
        
        Ok(())
    }
    
    // === Private helper methods ===
    
    /// Guess which chunk a token belongs to (based on hash)
    fn guess_chunk_id(&self, token: &str) -> usize {
        let meta = self.metadata.read();
        
        
        // Check chunk index for prefix match
        if let Some(prefix) = token.chars().take(2).collect::<String>().is_empty().then_some(token) {
            if let Some(&chunk_id) = meta.chunk_index.get(prefix) {
                return chunk_id;
            }
        }
        
        // Use simple hash-based distribution
        let hash = self.hash_token(token);
        
        hash % meta.num_chunks.max(1)
    }
    
    /// Simple hash function for token
    fn hash_token(&self, token: &str) -> usize {
        token.bytes().fold(0usize, |acc, b| {
            acc.wrapping_mul(31).wrapping_add(b as usize)
        })
    }
    
    /// Insert a new token
    /// 
    /// 🔧 OPTIMIZATION: Minimize lock hold time
    /// - Allocate term_id first (short metadata lock)
    /// - Load chunk outside cache lock if needed
    /// - Insert into cache (short cache lock)
    fn insert_new_token(&self, token: &str, chunk_id: usize) -> TermId {
        
        // 1. Allocate term_id (fast, metadata lock only)
        let term_id = {
            let mut meta = self.metadata.write();
            let term_id = meta.next_term_id;
            meta.next_term_id += 1;
            meta.total_terms += 1;
            
            // Ensure we have enough chunks
            if chunk_id >= meta.num_chunks {
                meta.num_chunks = chunk_id + 1;
            }
            
            // Update chunk index (track first 2 chars)
            if token.len() >= 2 {
                let prefix = token.chars().take(2).collect::<String>();
                meta.chunk_index.entry(prefix).or_insert(chunk_id);
            }
            
            term_id
        }; // metadata lock released here
        
        // 2. Prepare chunk (load from disk if needed, outside any lock)
        let mut target_chunk = None;
        {
            let cache = self.cache.read();
            if !cache.contains(&chunk_id) {
                // Chunk not in cache, need to load (do this outside write lock!)
                drop(cache);
                target_chunk = Some(
                    self.load_chunk(chunk_id)
                        .unwrap_or_else(|_| DictionaryChunk::new(chunk_id))
                );
            }
        }
        
        // 3. Insert into cache (short write lock)
        {
            let mut cache = self.cache.write();
            
            
            let chunk = if let Some(loaded_chunk) = target_chunk {
                // We loaded it outside lock, now insert
                cache.put(chunk_id, loaded_chunk);
                let result = cache.get_mut(&chunk_id).unwrap();
                result
            } else {
                let result = cache.get_mut(&chunk_id).unwrap();
                result
            };
            
            chunk.insert(token.to_string(), term_id);
        }
        
        term_id
    }
    
    /// Load a chunk from disk
    fn load_chunk(&self, chunk_id: usize) -> Result<DictionaryChunk> {
        let path = self.chunk_path(chunk_id);
        if !path.exists() {
            return Ok(DictionaryChunk::new(chunk_id));
        }
        
        let mut file = File::open(&path)?;
        let mut data = Vec::new();
        file.read_to_end(&mut data)?;
        
        let mut chunk: DictionaryChunk = bincode::deserialize(&data)
            .map_err(|e| StorageError::Serialization(e.to_string()))?;
        
        chunk.dirty = false; // Freshly loaded chunks are clean
        Ok(chunk)
    }
    
    /// Save a chunk to disk
    fn save_chunk(&self, chunk_id: usize, chunk: &DictionaryChunk) -> Result<()> {
        let path = self.chunk_path(chunk_id);
        
        let data = bincode::serialize(chunk)
            .map_err(|e| StorageError::Serialization(e.to_string()))?;
        
        let mut file = File::create(&path)?;
        file.write_all(&data)?;
        file.sync_all()?;
        
        Ok(())
    }
    
    /// Get chunk file path
    fn chunk_path(&self, chunk_id: usize) -> PathBuf {
        self.storage_dir.join(format!("dict_chunk_{:04}.bin", chunk_id))
    }
    
    /// Load metadata
    fn load_metadata(path: &PathBuf) -> Result<DictionaryMetadata> {
        let mut file = File::open(path)?;
        let mut data = Vec::new();
        file.read_to_end(&mut data)?;
        
        bincode::deserialize(&data)
            .map_err(|e| StorageError::Serialization(e.to_string()))
    }
    
    /// Save metadata
    fn save_metadata(&self, meta: &DictionaryMetadata) -> Result<()> {
        let path = self.storage_dir.join("dict_meta.bin");
        
        let data = bincode::serialize(meta)
            .map_err(|e| StorageError::Serialization(e.to_string()))?;
        
        let mut file = File::create(&path)?;
        file.write_all(&data)?;
        file.sync_all()?;
        
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    
    #[test]
    fn test_chunked_dictionary_basic() {
        let temp_dir = TempDir::new().unwrap();
        let dict = ChunkedDictionary::new(temp_dir.path().to_path_buf(), 4).unwrap();
        
        // Insert tokens
        let id1 = dict.get_or_insert("hello");
        let id2 = dict.get_or_insert("world");
        let id3 = dict.get_or_insert("hello"); // Same token
        
        assert_eq!(id1, id3); // Same ID for same token
        assert_ne!(id1, id2);
        assert_eq!(dict.len(), 2);
    }
    
    #[test]
    fn test_chunked_dictionary_persistence() {
        let temp_dir = TempDir::new().unwrap();
        
        // Create and populate
        {
            let dict = ChunkedDictionary::new(temp_dir.path().to_path_buf(), 4).unwrap();
            dict.get_or_insert("apple");
            dict.get_or_insert("banana");
            dict.flush().unwrap();
        }
        
        // Reopen and verify
        {
            let dict = ChunkedDictionary::new(temp_dir.path().to_path_buf(), 4).unwrap();
            assert_eq!(dict.len(), 2);
            assert!(dict.get("apple").is_some());
            assert!(dict.get("banana").is_some());
        }
    }
    
    #[test]
    fn test_chunked_dictionary_large_scale() {
        let temp_dir = TempDir::new().unwrap();
        let dict = ChunkedDictionary::new(temp_dir.path().to_path_buf(), 8).unwrap();
        
        // Insert 50K tokens
        for i in 0..50_000 {
            let token = format!("token_{}", i);
            dict.get_or_insert(&token);
        }
        
        assert_eq!(dict.len(), 50_000);
        
        // Flush and verify
        dict.flush().unwrap();
        assert!(dict.get("token_12345").is_some());
    }
}