mongreldb-core 0.60.2

MongrelDB core: log-structured columnar store with sub-ms writes, learned indexes, and an AI-native access layer.
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
//! Phase 19.1 — Table-level in-process result cache (`query_cached` /
//! `query_columns_native_cached`): repeat queries hit; a `commit()` invalidates.

use mongreldb_core::columnar::NativeColumn;
use mongreldb_core::query::{Condition, Query};
use mongreldb_core::schema::*;
use mongreldb_core::{Table, Value};
use tempfile::tempdir;

fn schema() -> Schema {
    Schema {
        schema_id: 1,
        columns: vec![
            ColumnDef {
                id: 1,
                name: "id".into(),
                ty: TypeId::Int64,
                flags: ColumnFlags::empty().with(ColumnFlags::PRIMARY_KEY),
                default_value: None,
                embedding_source: None,
            },
            ColumnDef {
                id: 2,
                name: "city".into(),
                ty: TypeId::Bytes,
                flags: ColumnFlags::empty(),
                default_value: None,
                embedding_source: None,
            },
            ColumnDef {
                id: 3,
                name: "cost".into(),
                ty: TypeId::Float64,
                flags: ColumnFlags::empty().with(ColumnFlags::NULLABLE),
                default_value: None,
                embedding_source: None,
            },
        ],
        indexes: vec![IndexDef {
            name: "city_bm".into(),
            column_id: 2,
            kind: IndexKind::Bitmap,
            predicate: None,
            options: Default::default(),
        }],
        colocation: vec![],
        constraints: Default::default(),
        clustered: false,
    }
}

#[test]
fn query_cached_hits_then_invalidates_on_commit() {
    let dir = tempdir().unwrap();
    let mut db = Table::create(dir.path(), schema(), 1).unwrap();
    let rows: Vec<Vec<(u16, Value)>> = (0..1000)
        .map(|i| {
            vec![
                (1, Value::Int64(i)),
                (2, Value::Bytes(b"alpha".to_vec())),
                (3, Value::Float64(i as f64)),
            ]
        })
        .collect();
    db.bulk_load(rows).unwrap();

    let q = Query::new().and(Condition::BitmapEq {
        column_id: 2,
        value: b"alpha".to_vec(),
    });
    let r0 = db.query_cached(&q).unwrap();
    assert_eq!(r0.len(), 1000);
    // Second call is a cache hit (same epoch, same conditions).
    let r1 = db.query_cached(&q).unwrap();
    assert_eq!(r1.len(), 1000);
    assert_eq!(r0[0].row_id, r1[0].row_id);

    // A commit (epoch bump) invalidates; the next call re-resolves and reflects
    // a mutation.
    db.put(vec![
        (1, Value::Int64(5000)),
        (2, Value::Bytes(b"alpha".to_vec())),
        (3, Value::Float64(9.0)),
    ])
    .unwrap();
    db.commit().unwrap();
    let r2 = db.query_cached(&q).unwrap();
    assert_eq!(r2.len(), 1001, "post-commit query sees the new row");
}

#[test]
fn query_columns_native_cached_hits() {
    let dir = tempdir().unwrap();
    let mut db = Table::create(dir.path(), schema(), 1).unwrap();
    let cols = vec![
        (
            1,
            NativeColumn::Int64 {
                data: (0..500).collect(),
                validity: vec![0xFF; 500usize.div_ceil(8)],
            },
        ),
        (
            2,
            NativeColumn::Bytes {
                offsets: (0..=500).map(|i| (i * 3) as u32).collect(),
                values: b"abc".repeat(500),
                validity: vec![0xFF; 500usize.div_ceil(8)],
            },
        ),
        (
            3,
            NativeColumn::Float64 {
                data: (0..500).map(|i| i as f64).collect(),
                validity: vec![0xFF; 500usize.div_ceil(8)],
            },
        ),
    ];
    db.bulk_load_columns(cols).unwrap();

    let snap = db.snapshot();
    let cond = [Condition::RangeF64 {
        column_id: 3,
        lo: 0.0,
        lo_inclusive: true,
        hi: 10.0,
        hi_inclusive: true,
    }];
    let proj = [1u16, 3];
    let c0 = db
        .query_columns_native_cached(&cond, Some(&proj), snap)
        .unwrap()
        .expect("served");
    // The Range resolves via the page-pruned scan; only rows with cost in [0,10]
    // survive (11 rows).
    let n0 = match &c0[1].1 {
        NativeColumn::Float64 { data, .. } => data.len(),
        _ => panic!(),
    };
    assert_eq!(n0, 11);

    // Cache hit — identical result.
    let c1 = db
        .query_columns_native_cached(&cond, Some(&proj), snap)
        .unwrap()
        .expect("served");
    let n1 = match &c1[1].1 {
        NativeColumn::Float64 { data, .. } => data.len(),
        _ => panic!(),
    };
    assert_eq!(n0, n1);
}

#[test]
fn lru_promote_on_hit_and_configurable_budget() {
    let dir = tempdir().unwrap();
    let mut db = Table::create(dir.path(), schema(), 1).unwrap();
    let rows: Vec<Vec<(u16, Value)>> = (0..300)
        .map(|i| {
            vec![
                (1, Value::Int64(i)),
                (
                    2,
                    Value::Bytes((if i % 3 == 0 { b"alpha" } else { b"beta!" }).to_vec()),
                ),
                (3, Value::Float64(i as f64)),
            ]
        })
        .collect();
    db.bulk_load(rows).unwrap();

    let q_alpha = Query::new().and(Condition::BitmapEq {
        column_id: 2,
        value: b"alpha".to_vec(),
    });
    let q_beta = Query::new().and(Condition::BitmapEq {
        column_id: 2,
        value: b"beta!".to_vec(),
    });

    // Both queries cache fine under the default budget.
    let ra = db.query_cached(&q_alpha).unwrap();
    let rb = db.query_cached(&q_beta).unwrap();
    assert_eq!(ra.len(), 100);
    assert_eq!(rb.len(), 200);

    // Shrink the budget aggressively — existing entries get evicted down.
    db.set_result_cache_max_bytes(1);
    // Results are still correct after eviction (re-resolved from scratch).
    let ra2 = db.query_cached(&q_alpha).unwrap();
    assert_eq!(ra2.len(), 100);
    assert_eq!(ra2[0].row_id, ra[0].row_id);

    // Grow it back; both cache and hit again.
    db.set_result_cache_max_bytes(256 * 1024 * 1024);
    db.query_cached(&q_beta).unwrap();
    let rb2 = db.query_cached(&q_beta).unwrap();
    assert_eq!(rb2.len(), 200);

    // Promote-on-hit: touch alpha, then shrink to ~one-entry budget.
    // The previously-MRU beta is now LRU relative to alpha and gets evicted;
    // alpha survives. We can't inspect the cache directly, but we verify the
    // results are always correct regardless of eviction order.
    db.query_cached(&q_alpha).unwrap();
    db.set_result_cache_max_bytes(1);
    let ra3 = db.query_cached(&q_alpha).unwrap();
    assert_eq!(ra3.len(), 100, "correct results after promote + eviction");
}

#[test]
fn fine_grained_invalidation_delete_survivor() {
    let dir = tempdir().unwrap();
    let mut db = Table::create(dir.path(), schema(), 1).unwrap();
    let rows: Vec<Vec<(u16, Value)>> = (0..300)
        .map(|i| {
            vec![
                (1, Value::Int64(i)),
                (
                    2,
                    Value::Bytes((if i % 3 == 0 { b"alpha" } else { b"beta!" }).to_vec()),
                ),
                (3, Value::Float64(i as f64)),
            ]
        })
        .collect();
    db.bulk_load(rows).unwrap();

    // Cache a query: city = "alpha" → 100 survivors (i=0,3,6,...,297).
    let q_city = Query::new().and(Condition::BitmapEq {
        column_id: 2,
        value: b"alpha".to_vec(),
    });
    let r0 = db.query_cached(&q_city).unwrap();
    assert_eq!(r0.len(), 100);

    // Delete a survivor → its RowId is in the footprint → entry invalidated.
    db.delete(r0[0].row_id).unwrap();
    db.commit().unwrap();
    let r1 = db.query_cached(&q_city).unwrap();
    assert_eq!(r1.len(), 99, "delete of a survivor drops one row");
}

#[test]
fn fine_grained_invalidation_column_aware_insert() {
    let dir = tempdir().unwrap();
    let mut db = Table::create(dir.path(), schema(), 1).unwrap();
    // Two columns: city (2) and cost (3). Bitmap index on city only.
    let rows: Vec<Vec<(u16, Value)>> = (0..300)
        .map(|i| {
            vec![
                (1, Value::Int64(i)),
                (
                    2,
                    Value::Bytes((if i % 3 == 0 { b"alpha" } else { b"beta!" }).to_vec()),
                ),
                (3, Value::Float64(i as f64)),
            ]
        })
        .collect();
    db.bulk_load(rows).unwrap();

    // Cache a query on cost (column 3) — condition_cols = {3}.
    let q_cost = Query::new().and(Condition::RangeF64 {
        column_id: 3,
        lo: 0.0,
        lo_inclusive: true,
        hi: 50.0,
        hi_inclusive: true,
    });
    let r0 = db.query_cached(&q_cost).unwrap();
    assert_eq!(r0.len(), 51);

    // Commit a put that does NOT touch column 3.
    // Since put writes all columns (the caller provides values for all),
    // column 3 IS written → the cost entry IS invalidated. This is correct.
    // To test a non-invalidating write, we'd need a put that skips column 3.
    // A put with only columns 1 and 2:
    db.put(vec![
        (1, Value::Int64(9999)),
        (2, Value::Bytes(b"alpha".to_vec())),
    ])
    .unwrap();
    db.commit().unwrap();

    // Column 3 was not written → pending_put_cols = {1, 2}.
    // The cost query's condition_cols = {3} → no intersection → entry survives!
    let r1 = db.query_cached(&q_cost).unwrap();
    assert_eq!(
        r1.len(),
        51,
        "cost query survives a write that doesn't touch column 3"
    );
}

#[test]
fn fine_grained_invalidation_multi_run_delete() {
    let dir = tempdir().unwrap();
    let mut db = Table::create(dir.path(), schema(), 1).unwrap();
    let rows: Vec<Vec<(u16, Value)>> = (0..100)
        .map(|i| {
            vec![
                (1, Value::Int64(i)),
                (
                    2,
                    Value::Bytes((if i % 2 == 0 { b"alpha" } else { b"beta!" }).to_vec()),
                ),
                (3, Value::Float64(i as f64)),
            ]
        })
        .collect();
    db.bulk_load(rows).unwrap();

    // Force a second run via flush with a tiny spill threshold.
    db.set_mutable_run_spill_bytes(1);
    db.put(vec![
        (1, Value::Int64(100)),
        (2, Value::Bytes(b"alpha".to_vec())),
        (3, Value::Float64(100.0)),
    ])
    .unwrap();
    db.flush().unwrap();
    assert!(db.run_count() >= 2);

    let snap = db.snapshot();
    let cond = [Condition::BitmapEq {
        column_id: 2,
        value: b"alpha".to_vec(),
    }];
    let proj = [2u16];
    let c0 = db
        .query_columns_native_cached(&cond, Some(&proj), snap)
        .unwrap()
        .expect("served");
    let n0 = match &c0[0].1 {
        NativeColumn::Bytes { offsets, .. } => offsets.len() - 1,
        _ => panic!("expected bytes"),
    };
    assert!(n0 > 0);

    // Delete a survivor. Footprint is empty (multi-run) → conservative path
    // (any delete → evict entries with empty footprint) must fire.
    db.delete(mongreldb_core::RowId(0)).unwrap();
    db.commit().unwrap();

    // New snapshot sees the delete (MVCC: old snapshot would still see the row).
    let snap2 = db.snapshot();
    let c1 = db
        .query_columns_native_cached(&cond, Some(&proj), snap2)
        .unwrap()
        .expect("served");
    let n1 = match &c1[0].1 {
        NativeColumn::Bytes { offsets, .. } => offsets.len() - 1,
        _ => panic!("expected bytes"),
    };
    assert_eq!(n1, n0 - 1, "multi-run cached entry must reflect the delete");
}

#[test]
fn persistent_tier_survives_restart() {
    let dir = tempdir().unwrap();
    let rcache_path = dir.path().join("_rcache");

    {
        let mut db = Table::create(dir.path(), schema(), 1).unwrap();
        let rows: Vec<Vec<(u16, Value)>> = (0..200)
            .map(|i| {
                vec![
                    (1, Value::Int64(i)),
                    (
                        2,
                        Value::Bytes((if i % 2 == 0 { b"alpha" } else { b"beta!" }).to_vec()),
                    ),
                    (3, Value::Float64(i as f64)),
                ]
            })
            .collect();
        db.bulk_load(rows).unwrap();

        let q = Query::new().and(Condition::BitmapEq {
            column_id: 2,
            value: b"alpha".to_vec(),
        });
        let r = db.query_cached(&q).unwrap();
        assert_eq!(r.len(), 100);

        let files: Vec<_> = std::fs::read_dir(&rcache_path)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("bin"))
            .collect();
        assert!(!files.is_empty(), "persistent cache files written");
    }

    {
        let mut db = Table::open(dir.path()).unwrap();
        let q = Query::new().and(Condition::BitmapEq {
            column_id: 2,
            value: b"alpha".to_vec(),
        });
        let r = db.query_cached(&q).unwrap();
        assert_eq!(r.len(), 100, "persistent cache hit after restart");
    }
}