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
//! Column Index Operations
//!
//! Extracted from database_legacy.rs
//! Provides column value indexing for WHERE clause optimization

use crate::database::core::MoteDB;
use crate::types::{Row, RowId, Value};
use crate::{Result, StorageError};
use crate::index::column_value::{ColumnValueIndex, ColumnValueIndexConfig};
use parking_lot::RwLock;
use std::sync::Arc;

impl MoteDB {
    /// Create a column value index for WHERE clause optimization
    /// 
    /// 🚀 **方案B(高性能)**: 使用scan_range一次性扫描LSM,避免全表加载到内存
    /// 
    /// # Performance
    /// - Point queries: 40x faster (1ms vs 40ms)
    /// - Range queries: Efficient B-Tree scan
    /// - CREATE INDEX: O(N) 顺序扫描,避免内存溢出
    /// 
    /// # Example
    /// ```ignore
    /// db.create_column_index("users", "email")?;
    /// // Now queries like WHERE email = 'foo@bar.com' are 40x faster
    /// ```
    pub fn create_column_index(&self, table_name: &str, column_name: &str) -> Result<()> {
        // Use default index name format: {table}.{column}
        let index_name = format!("{}.{}", table_name, column_name);
        self.create_column_index_with_name(table_name, column_name, &index_name)
    }
    
    /// Create a column value index with custom name
    /// 
    /// 🚀 **方案B(高性能)**: 使用scan_range一次性扫描LSM,避免全表加载到内存
    /// 
    /// # Performance
    /// - Point queries: 40x faster (1ms vs 40ms)
    /// - Range queries: Efficient B-Tree scan
    /// - CREATE INDEX: O(N) 顺序扫描,避免内存溢出
    /// 
    /// # Example
    /// ```ignore
    /// db.create_column_index_with_name("users", "email", "idx_users_email")?;
    /// // Now queries like WHERE email = 'foo@bar.com' are 40x faster
    /// ```
    pub fn create_column_index_with_name(&self, table_name: &str, column_name: &str, index_name: &str) -> Result<()> {
        // 🎯 统一路径:{db}.mote/indexes/column_{index_name}.idx
        let indexes_dir = self.path.join("indexes");
        std::fs::create_dir_all(&indexes_dir)?;
        let index_path = indexes_dir.join(format!("column_{}.idx", index_name));
        
        let config = ColumnValueIndexConfig::default();
        let index = ColumnValueIndex::create(
            index_path, 
            table_name.to_string(), 
            column_name.to_string(), 
            config
        )?;
        
        let index_arc = Arc::new(RwLock::new(index));
        self.column_indexes.insert(index_name.to_string(), index_arc.clone());
        
        // 🚀 方案B:使用scan_range高性能扫描
        // 获取列在schema中的位置
        if let Ok(schema) = self.table_registry.get_table(table_name) {
            if let Some(col_def) = schema.columns.iter().find(|c| c.name == column_name) {
                let col_position = col_def.position;
                
                debug_log!("[create_column_index] 🔍 使用scan_range扫描LSM(方案B)...");
                let start_time = std::time::Instant::now();
                
                // 计算表的key范围
                use std::collections::hash_map::DefaultHasher;
                use std::hash::{Hash, Hasher};
                let mut hasher = DefaultHasher::new();
                table_name.hash(&mut hasher);
                let table_hash = hasher.finish() & 0xFFFFFFFF;
                
                let start_key = table_hash << 32;
                let end_key = (table_hash + 1) << 32;
                
                // 一次scan_range扫描所有数据
                let mut indexed_count = 0;
                const BATCH_SIZE: usize = 500; // 批量flush,避免内存溢出
                
                match self.lsm_engine.scan_range(start_key, end_key) {
                    Ok(entries) => {
                        for (batch_idx, chunk) in entries.chunks(BATCH_SIZE).enumerate() {
                            for (composite_key, value) in chunk {
                                let row_id = (composite_key & 0xFFFFFFFF) as RowId;
                                
                                let data_bytes: Vec<u8> = match &value.data {
                                    crate::storage::lsm::ValueData::Inline(bytes) => bytes.clone(),
                                    crate::storage::lsm::ValueData::Blob(blob_ref) => {
                                        match self.lsm_engine.resolve_blob(blob_ref) {
                                            Ok(data) => data,
                                            Err(e) => {
                                                eprintln!("[create_column_index] Failed to resolve blob for row {}: {}", row_id, e);
                                                continue;
                                            }
                                        }
                                    }
                                };
                                
                                if let Ok(row) = bincode::deserialize::<Row>(&data_bytes) {
                                    if let Some(value) = row.get(col_position) {
                                        if let Err(e) = index_arc.write().insert(value, row_id) {
                                            eprintln!("[create_column_index] ⚠️ 插入失败 row_id={}: {}", row_id, e);
                                        } else {
                                            indexed_count += 1;
                                        }
                                    }
                                }
                            }
                            
                            // 每4个batch或最后一个batch时flush
                            if (batch_idx + 1) % 4 == 0 || (batch_idx + 1) * BATCH_SIZE >= entries.len() {
                                if let Err(e) = index_arc.write().flush() {
                                    eprintln!("[create_column_index] ⚠️ Flush失败: {}", e);
                                }
                            }
                        }
                    }
                    Err(e) => {
                        eprintln!("[create_column_index] ⚠️ scan_range失败: {}", e);
                    }
                }
                
                let scan_time = start_time.elapsed();
                
                if indexed_count > 0 {
                    debug_log!("[create_column_index] 🚀 扫描完成:{} 个值,耗时 {:?}", 
                             indexed_count, scan_time);
                    debug_log!("[create_column_index] ✅ 批量建索引完成!");
                } else {
                    debug_log!("[create_column_index] ⚠️ 未找到任何数据(扫描耗时 {:?})", scan_time);
                }
            } else {
                println!("  ✓ Created empty column index '{}' (column not found in schema)", index_name);
            }
        } else {
            println!("  ✓ Created empty column index '{}' (table not found)", index_name);
        }
        
        Ok(())
    }
    
    /// Insert value into column index
    /// 
    /// Should be called after insert_table_row() if column index exists
    pub fn insert_column_value(&self, table_name: &str, column_name: &str, row_id: RowId, value: &Value) -> Result<()> {
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;
        
        index_ref.value().write().insert(value, row_id)?;
        Ok(())
    }
    
    /// 🚀 P2 优化:批量插入列索引值
    /// 
    /// ## 性能优化
    /// - 批量排序 + 批量插入 B-Tree
    /// - 减少锁竞争(单次加锁)
    /// - 更好的 B-Tree 局部性
    /// 
    /// ## 预期效果
    /// - 1000 条插入:1000 次加锁 → 1 次加锁
    /// - 性能提升:2-3 倍
    pub fn batch_insert_column_values(&self, table_name: &str, column_name: &str, items: Vec<(RowId, Value)>) -> Result<()> {
        if items.is_empty() {
            return Ok(());
        }
        
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;
        
        // Convert to (Value, RowId) for batch_insert API
        let batch: Vec<(Value, RowId)> = items.into_iter()
            .map(|(row_id, value)| (value, row_id))
            .collect();
        
        index_ref.value().write().batch_insert(batch)?;
        Ok(())
    }
    
    /// Get all column indexes for a table
    /// 
    /// Returns list of column names that have indexes
    pub fn get_table_column_indexes(&self, table_name: &str) -> Vec<String> {
        let prefix = format!("{}.", table_name);
        
        self.column_indexes.iter()
            .filter(|entry| entry.key().starts_with(&prefix))
            .map(|entry| entry.key().strip_prefix(&prefix).unwrap().to_string())
            .collect()
    }
    
    /// Delete value from column index
    /// 
    /// Should be called after delete_row() if column index exists
    pub fn delete_column_value(&self, table_name: &str, column_name: &str, row_id: RowId, value: &Value) -> Result<()> {
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;
        
        index_ref.value().write().delete(value, row_id)?;
        Ok(())
    }
    
    /// Update value in column index (delete old + insert new)
    /// 
    /// Should be called after update_row() if column index exists
    pub fn update_column_value(&self, table_name: &str, column_name: &str, row_id: RowId, 
                                old_value: &Value, new_value: &Value) -> Result<()> {
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;
        
        let mut index_guard = index_ref.value().write();
        index_guard.delete(old_value, row_id)?;
        index_guard.insert(new_value, row_id)?;
        Ok(())
    }
    
    /// Flush column index to disk (for bulk insert operations)
    pub fn flush_column_index(&self, table_name: &str, column_name: &str) -> Result<()> {
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;
        
        index_ref.value().write().flush()?;
        Ok(())
    }
    
    /// Query by column value (point query)
    /// 
    /// # Performance
    /// - With index: <1ms
    /// - Without index: 40ms (table scan)
    /// 
    /// # LSM Architecture
    /// - Queries both SSTable (via index) and MemTable (live data)
    /// - Merges and deduplicates results
    /// 
    /// # Example
    /// ```ignore
    /// let row_ids = db.query_by_column("users", "email", &Value::Text("foo@bar.com".into()))?;
    /// ```
    pub fn query_by_column(&self, table_name: &str, column_name: &str, value: &Value) -> Result<Vec<RowId>> {
        let index_name = format!("{}.{}", table_name, column_name);

        // Query the in-memory B+Tree index directly.
        // The index is maintained synchronously on INSERT/UPDATE/DELETE,
        // so it already covers both MemTable and SSTable data — no need
        // for a redundant MemTable scan.
        let row_ids = {
            let index_ref = self.column_indexes.get(&index_name)
                .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;

            let index_guard = index_ref.value().read();
            index_guard.get(value)?
        };

        Ok(row_ids)
    }
    
    /// Query column value index with range (WHERE col >= start AND col <= end)
    /// Returns matching row IDs sorted by value
    /// 
    /// # LSM Architecture
    /// - Queries both SSTable (via index) and MemTable (live data)
    /// 
    /// # Example
    /// ```ignore
    /// let row_ids = db.query_by_column_range("users", "age", 
    ///     &Value::Integer(20), &Value::Integer(30))?;
    /// ```
    pub fn query_by_column_range(&self, table_name: &str, column_name: &str, 
                                start: &Value, end: &Value) -> Result<Vec<RowId>> {
        self.query_by_column_between(table_name, column_name, start, true, end, true)
    }
    
    /// 🚀 Query column value index: WHERE col < value
    /// 
    /// # Example
    /// ```ignore
    /// // Get all users with age < 30
    /// let row_ids = db.query_by_column_less_than("users", "age", &Value::Integer(30))?;
    /// ```
    pub fn query_by_column_less_than(&self, table_name: &str, column_name: &str,
                                    value: &Value) -> Result<Vec<RowId>> {
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;

        let index_guard = index_ref.value().read();
        index_guard.query_less_than(value)
    }
    
    /// 🚀 Query column value index: WHERE col > value
    /// 
    /// # Example
    /// ```ignore
    /// // Get all users with age > 18
    /// let row_ids = db.query_by_column_greater_than("users", "age", &Value::Integer(18))?;
    /// ```
    pub fn query_by_column_greater_than(&self, table_name: &str, column_name: &str,
                                       value: &Value) -> Result<Vec<RowId>> {
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;

        let index_guard = index_ref.value().read();
        index_guard.query_greater_than(value)
    }
    
    /// 🚀 Query column value index: WHERE col <= value
    pub fn query_by_column_less_than_or_equal(&self, table_name: &str, column_name: &str,
                                             value: &Value) -> Result<Vec<RowId>> {
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;

        let index_guard = index_ref.value().read();
        index_guard.query_less_than_or_equal(value)
    }
    
    /// 🚀 Query column value index: WHERE col >= value
    pub fn query_by_column_greater_than_or_equal(&self, table_name: &str, column_name: &str,
                                                value: &Value) -> Result<Vec<RowId>> {
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;

        let index_guard = index_ref.value().read();
        index_guard.query_greater_than_or_equal(value)
    }
    
    /// 🚀 Query column value index: dual-bound range query with flexible boundaries
    /// 
    /// **Use case**: `WHERE col > X AND col < Y`, `WHERE col >= X AND col <= Y`
    /// 
    /// **Performance**: O(log N + K) - single B-Tree scan (much faster than intersecting two queries)
    /// 
    /// # Example
    /// ```ignore
    /// // Get robots created between timestamps (exclusive)
    /// let row_ids = db.query_by_column_between("robots", "created_at",
    ///     &Value::Integer(100000), false,
    ///     &Value::Integer(200000), false)?;
    /// ```
    pub fn query_by_column_between(&self, table_name: &str, column_name: &str,
                                  lower_bound: &Value, lower_inclusive: bool,
                                  upper_bound: &Value, upper_inclusive: bool) -> Result<Vec<RowId>> {
        let index_name = format!("{}.{}", table_name, column_name);
        let index_ref = self.column_indexes.get(&index_name)
            .ok_or_else(|| StorageError::Index(format!("Column index '{}' not found", index_name)))?;

        let index_guard = index_ref.value().read();
        index_guard.query_between(lower_bound, lower_inclusive, upper_bound, upper_inclusive)
    }
    
    /// 🔧 LSM Helper: Scan MemTable for rows matching a column predicate
    /// 
    /// This scans the active + immutable MemTables to find rows where the specified
    /// column satisfies the given predicate.
    /// 
    /// # Arguments
    /// - `table_name`: Table to scan
    /// - `column_name`: Column to check
    /// - `predicate`: Function that returns true if the column value matches
    /// 
    /// # Returns
    /// Vector of row IDs that match the predicate
    #[allow(dead_code)]
    fn scan_memtable_for_column<F>(&self, table_name: &str, column_name: &str, predicate: F) -> Result<Vec<RowId>>
    where
        F: Fn(&Value) -> bool,
    {
        // Get table schema to find column index
        let schema = self.table_registry.get_table(table_name)?;
        let column_index = schema.columns.iter()
            .position(|col| col.name == column_name)
            .ok_or_else(|| StorageError::InvalidData(format!("Column '{}' not found in table '{}'", column_name, table_name)))?;
        
        let mut matching_ids = Vec::new();
        let mut scanned_count = 0;
        
        // Scan incremental MemTable (active + immutable)
        self.lsm_engine.scan_memtable_incremental_with(|composite_key, row_data| {
            scanned_count += 1;
            
            // Extract row_id from composite key (lower 32 bits)
            let row_id = (composite_key & 0xFFFFFFFF) as RowId;
            
            // Deserialize row
            let row: Row = bincode::deserialize(row_data)
                .map_err(|e| StorageError::Serialization(e.to_string()))?;
            
            // Check if column matches predicate
            if column_index < row.len() {
                let col_value = &row[column_index];
                if predicate(col_value) {
                    matching_ids.push(row_id);
                }
            }
            
            Ok(())
        })?;
        
        debug_log!("[scan_memtable_for_column] 扫描了 {} 条MemTable数据,匹配 {} 条", 
                 scanned_count, matching_ids.len());
        
        Ok(matching_ids)
    }
}