motedb 0.7.6

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
//! LSM-Tree Storage Engine
//!
//! ## Architecture
//! - **MemTable**: In-memory skip list (write buffer)
//! - **SSTable**: Sorted String Table (disk persistence)
//! - **Compaction**: Background merge/compress
//!
//! ## Performance Targets
//! - Write: 10K+ ops/s
//! - Read: < 1ms (P99)
//! - Space: 2:1 compression ratio

mod blobstore;
mod bloom;
pub(crate) mod columnar; // 🆕 Columnar SSTable (column-oriented storage)
mod compaction;
mod engine;
mod memtable;
mod merging_iterator;
mod sstable;
mod unified_memtable; // 🆕 Unified MemTable (数据 + 向量) // 🚀 流式合并迭代器

pub use blobstore::BlobStore;
pub use bloom::BloomFilter;
pub use columnar::{ColumnarSSTable, ColumnarSSTableBuilder, RowMap};
pub use compaction::{CompactionConfig, CompactionStats, CompactionWorker, Level, SSTableMeta};
pub use engine::{LSMBatchedIterator, LSMEngine}; // 🚀 Export batched iterator
pub use memtable::MemTable;
pub use merging_iterator::MergingIterator;
pub use sstable::{BlockIndex, SSTable, SSTableBuilder, SSTableIterator};
pub use unified_memtable::{DataEntry, UnifiedEntry, UnifiedMemTable}; // 🚀 Export merging iterator

/// Key type (row_id as u64)
///
/// 🔧 优化:从 Vec<u8> 改为 u64
/// - 消除 24 bytes Vec 元数据开销 (↓ 75%)
/// - 零拷贝,无堆分配
/// - BTreeMap<u64> 比 BTreeMap<Vec<u8>> 快 2-3x
pub type Key = u64;

/// Blob reference for large values
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BlobRef {
    /// Blob file ID
    pub file_id: u32,
    /// Offset in blob file
    pub offset: u64,
    /// Size of blob data
    pub size: u32,
}

/// Value storage type
#[derive(Clone, Debug)]
pub enum ValueData {
    /// Inline small value (< blob_threshold) — Arc for O(1) clone during scan
    Inline(std::sync::Arc<Vec<u8>>),
    /// Reference to blob file for large value
    Blob(BlobRef),
}

impl PartialEq for ValueData {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (ValueData::Inline(a), ValueData::Inline(b)) => a.as_slice() == b.as_slice(),
            (ValueData::Blob(a), ValueData::Blob(b)) => a == b,
            _ => false,
        }
    }
}
impl Eq for ValueData {}

impl ValueData {
    /// Get size of value data (for inline: actual data size, for blob: ref size)
    pub fn len(&self) -> usize {
        match self {
            ValueData::Inline(data) => data.len(),
            ValueData::Blob(_) => 16, // BlobRef is 16 bytes (file_id + offset + size)
        }
    }

    pub fn is_empty(&self) -> bool {
        match self {
            ValueData::Inline(data) => data.is_empty(),
            ValueData::Blob(_) => false,
        }
    }
}

/// Value with MVCC metadata
#[derive(Clone, Debug)]
pub struct Value {
    /// Data payload (inline or blob reference)
    pub data: ValueData,

    /// MVCC timestamp (transaction ID)
    pub timestamp: u64,

    /// Tombstone marker (for deletion)
    pub deleted: bool,
}

impl Value {
    pub fn new(data: Vec<u8>, timestamp: u64) -> Self {
        Self {
            data: ValueData::Inline(std::sync::Arc::new(data)),
            timestamp,
            deleted: false,
        }
    }

    /// Create from a shared Arc buffer (avoids re-wrapping for batch inserts
    /// where data is already in an Arc).
    pub fn new_from_arc(data: std::sync::Arc<Vec<u8>>, timestamp: u64) -> Self {
        Self {
            data: ValueData::Inline(data),
            timestamp,
            deleted: false,
        }
    }

    pub fn new_blob(blob_ref: BlobRef, timestamp: u64) -> Self {
        Self {
            data: ValueData::Blob(blob_ref),
            timestamp,
            deleted: false,
        }
    }

    /// Shared empty Arc for tombstones — avoids per-tombstone allocation.
    /// Every tombstone has empty data, so they can all share the same Arc.
    fn empty_arc() -> std::sync::Arc<Vec<u8>> {
        // leak is fine: this is a global singleton, lives for the process lifetime
        static EMPTY: std::sync::OnceLock<std::sync::Arc<Vec<u8>>> = std::sync::OnceLock::new();
        EMPTY
            .get_or_init(|| std::sync::Arc::new(Vec::new()))
            .clone()
    }

    pub fn tombstone(timestamp: u64) -> Self {
        Self {
            data: ValueData::Inline(Self::empty_arc()),
            timestamp,
            deleted: true,
        }
    }

    /// Get inline data if available
    pub fn as_inline(&self) -> Option<&[u8]> {
        match &self.data {
            ValueData::Inline(data) => Some(data.as_slice()),
            ValueData::Blob(_) => None,
        }
    }

    /// Check if this is a blob reference
    pub fn is_blob(&self) -> bool {
        matches!(self.data, ValueData::Blob(_))
    }
}

/// LSM-Tree configuration
/// Compression algorithm for SSTable blocks
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CompressionAlgorithm {
    #[default]
    Zstd,
    Snappy,
    None,
}

#[derive(Debug, Clone)]
pub struct LSMConfig {
    /// MemTable size threshold (default 4MB)
    pub memtable_size: usize,

    /// SSTable block size (default 64KB)
    pub block_size: usize,

    /// Number of levels (default 7)
    pub num_levels: usize,

    /// Level size multiplier (default 10)
    pub level_multiplier: usize,

    /// L0 compaction trigger (default 4 files)
    pub l0_compaction_trigger: usize,

    /// Bloom filter bits per key (default 10)
    pub bloom_bits_per_key: usize,

    /// Enable compression (default true)
    pub enable_compression: bool,

    /// Compression algorithm (default Zstd)
    pub compression_algorithm: CompressionAlgorithm,

    /// Zstd compression level (default 1, range -7..22)
    pub zstd_compression_level: i32,

    /// Blob threshold: values larger than this go to blob files (default 32KB)
    pub blob_threshold: usize,

    /// Blob file size limit (default 256MB)
    pub blob_file_size: usize,

    /// SSTable cache size (number of cached SSTable handles, default 128)
    pub sstable_cache_size: usize,

    /// Memory limit for SSTable cache (MB)
    pub sstable_cache_memory_limit_mb: Option<usize>,

    // --- Compaction throttling ---
    /// Max compaction write rate in bytes/sec (None = unlimited, default 4 MB/s)
    pub compaction_rate_limit: Option<u64>,

    /// Max SSTables open simultaneously during compaction (default 4)
    pub compaction_max_open_sstables: usize,

    /// Sleep 1ms every N blocks during compaction for cooperative yielding (default 4)
    pub compaction_yield_every_n_blocks: usize,

    /// Only compact when write load is idle (default false)
    pub compaction_idle_only: bool,

    /// Tombstone TTL in seconds before entries are physically dropped during compaction.
    /// 0 = drop all tombstones immediately during compaction.
    /// Default: 86400 (24 hours).
    pub tombstone_ttl_secs: u64,
}

impl Default for LSMConfig {
    fn default() -> Self {
        Self {
            memtable_size: 512 * 1024,
            block_size: 64 * 1024,
            num_levels: 7,
            level_multiplier: 10,
            l0_compaction_trigger: 4,
            bloom_bits_per_key: 12,
            enable_compression: true,
            compression_algorithm: CompressionAlgorithm::Zstd,
            zstd_compression_level: 1,
            blob_threshold: 32 * 1024,
            blob_file_size: 256 * 1024 * 1024,
            sstable_cache_size: 32,
            sstable_cache_memory_limit_mb: Some(200),
            compaction_rate_limit: Some(4 * 1024 * 1024), // 4 MB/s
            compaction_max_open_sstables: 4,
            compaction_yield_every_n_blocks: 4,
            compaction_idle_only: false,
            tombstone_ttl_secs: 86400, // 24 hours
        }
    }
}

impl LSMConfig {
    /// Convert DB-level LSMConfig to storage-level LSMConfig
    /// Maps user-facing fields from config::LSMConfig, All other
    /// fields keep their storage-layer defaults.
    pub fn from_db_config(db_config: &crate::config::LSMConfig) -> Self {
        let defaults = Self::default();
        Self {
            memtable_size: db_config.memtable_size_limit,
            l0_compaction_trigger: db_config.level0_compaction_threshold,
            bloom_bits_per_key: db_config.bloom_bits_per_key,
            sstable_cache_size: db_config
                .sstable_cache_size
                .unwrap_or(defaults.sstable_cache_size),
            sstable_cache_memory_limit_mb: db_config
                .sstable_cache_memory_limit_mb
                .or(defaults.sstable_cache_memory_limit_mb),
            block_size: db_config.block_size.unwrap_or(defaults.block_size),
            enable_compression: db_config
                .enable_compression
                .unwrap_or(defaults.enable_compression),
            compression_algorithm: db_config
                .compression_algorithm
                .map(|a| match a {
                    crate::config::CompressionAlgorithm::Zstd => CompressionAlgorithm::Zstd,
                    crate::config::CompressionAlgorithm::Snappy => CompressionAlgorithm::Snappy,
                    crate::config::CompressionAlgorithm::None => CompressionAlgorithm::None,
                })
                .unwrap_or(defaults.compression_algorithm),
            tombstone_ttl_secs: db_config
                .tombstone_ttl_secs
                .unwrap_or(defaults.tombstone_ttl_secs),
            ..defaults
        }
    }

    /// Optimized config for read-heavy workloads
    pub fn read_optimized() -> Self {
        Self {
            memtable_size: 4 * 1024 * 1024,
            block_size: 32 * 1024,
            num_levels: 7,
            level_multiplier: 10,
            l0_compaction_trigger: 2,
            bloom_bits_per_key: 16,
            enable_compression: true,
            blob_threshold: 32 * 1024,
            blob_file_size: 256 * 1024 * 1024,
            sstable_cache_size: 256,
            sstable_cache_memory_limit_mb: Some(400),
            ..Self::default()
        }
    }

    /// Optimized config for write-heavy workloads
    pub fn write_optimized() -> Self {
        Self {
            memtable_size: 16 * 1024 * 1024,
            block_size: 128 * 1024,
            num_levels: 6,
            level_multiplier: 8,
            l0_compaction_trigger: 8,
            bloom_bits_per_key: 8,
            enable_compression: true,
            blob_threshold: 32 * 1024,
            blob_file_size: 256 * 1024 * 1024,
            sstable_cache_size: 64,
            sstable_cache_memory_limit_mb: Some(200),
            ..Self::default()
        }
    }

    /// 🆕 Embedded Mode: 嵌入式设备优化配置
    ///
    /// **目标**: 减少 50% 内存占用,适用于嵌入式数据库
    ///
    /// **优化策略**:
    /// 1. ✅ 更小的 MemTable(2MB vs 4MB)→ 峰值内存 -50%
    /// 2. ✅ 更小的 Block(32KB vs 64KB)→ SSTable -50%
    /// 3. ✅ 强制压缩(Snappy)→ 磁盘占用 -40%
    /// 4. ✅ 激进的 L0 压缩(触发阈值=2)→ 快速合并小文件
    /// 5. ✅ 更小的 Bloom Filter(8 bits)→ 减少元数据开销
    /// 6. ✅ 更小的 Blob 阈值(16KB vs 32KB)→ 更多数据进入 Blob
    /// 7. ✅ 更小的缓存(4个 vs 8个)→ 缓存内存 -50%
    /// 8. ✅ 6 层 LSM(vs 7 层)→ 减少空间放大
    ///
    /// **适用场景**:
    /// - 嵌入式数据库(Electron, Mobile, IoT)
    /// - 内存受限环境(< 512MB RAM)
    /// - 单机应用(无需高并发)
    ///
    /// **性能权衡**:
    /// - 写入吞吐:-20%(更小的 buffer)
    /// - 读取延迟:+10%(更小的 cache)
    /// - 内存占用:**-50%** ✅
    pub fn embedded() -> Self {
        Self {
            memtable_size: 2 * 1024 * 1024,
            block_size: 32 * 1024,
            num_levels: 6,
            level_multiplier: 8,
            l0_compaction_trigger: 2,
            bloom_bits_per_key: 8,
            enable_compression: true,
            blob_threshold: 16 * 1024,
            blob_file_size: 128 * 1024 * 1024,
            sstable_cache_size: 32,
            sstable_cache_memory_limit_mb: Some(40),
            ..Self::default()
        }
    }

    /// 🆕 Tiny Mode: 微型设备优化配置(IoT / 移动端)
    ///
    /// **目标**: 减少 70% 内存占用,总内存 < 20MB
    ///
    /// **适用场景**:
    /// - IoT 设备(< 128MB RAM)
    /// - 移动应用(省电模式)
    /// - 边缘计算设备
    ///
    /// **性能权衡**:
    /// - 写入吞吐:-40%
    /// - 读取延迟:+20%
    /// - 内存占用:**-70%** ✅
    pub fn tiny() -> Self {
        Self {
            memtable_size: 1024 * 1024,
            block_size: 16 * 1024,
            num_levels: 5,
            level_multiplier: 4,
            l0_compaction_trigger: 2,
            bloom_bits_per_key: 6,
            enable_compression: true,
            blob_threshold: 8 * 1024,
            blob_file_size: 64 * 1024 * 1024,
            sstable_cache_size: 8,
            sstable_cache_memory_limit_mb: Some(20),
            ..Self::default()
        }
    }

    /// 🆕 P1 Memory Optimized Config (Low Memory Footprint)
    ///
    /// **Target**: 减少30-50%内存占用
    ///
    /// **适用场景**:
    /// - 内存受限环境(< 512MB)
    /// - 大数据量场景(> 100万条记录)
    /// - 磁盘空间充足但内存紧张
    ///
    /// **性能权衡**:
    /// - 写入延迟: +10-20%(更频繁的flush)
    /// - 查询延迟: +5-10%(更多SSTable文件)
    /// - 内存占用: -30-50% ✅
    /// - 磁盘占用: -30-50% ✅(压缩)
    pub fn memory_optimized() -> Self {
        Self {
            memtable_size: 2 * 1024 * 1024,
            block_size: 32 * 1024,
            num_levels: 7,
            level_multiplier: 10,
            l0_compaction_trigger: 2,
            bloom_bits_per_key: 10,
            enable_compression: true,
            blob_threshold: 16 * 1024,
            blob_file_size: 128 * 1024 * 1024,
            sstable_cache_size: 4,
            sstable_cache_memory_limit_mb: Some(100),
            ..Self::default()
        }
    }

    /// Balanced config (current default)
    pub fn balanced() -> Self {
        Self::default()
    }
}