dendro 0.3.1

A segmented-parquet archive with a write-ahead log, in a single SQLite file
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
//! The caller's time-keyed store: rows the archive keeps against
//! `(source, stream, ts)` and never decodes.
//!
//! What it must do, and what it must leave alone: read back in order within
//! a range, survive compaction untouched, travel with a copy under the same
//! stream and range filters, go with retention at the same cutoff or from a
//! caller's floor, and never
//! make a stream exist.

use std::collections::BTreeMap;
use std::sync::Arc;

use dendro::archive::{Archive, ArchiveMut, CallerRow, SegmentMeta, SourceMeta, WalRow};
use dendro::rewrite::{compact, copy_sources_into, CompactSpec, CopySpec};
use dendro::segment::{EncodeResult, Segment, SegmentEncoder};

/// Rows as a comma-joined list of timestamps: readable, and not parquet, so
/// a copy passes them through and a compaction test uses [`Parquet`].
struct Tags;

impl SegmentEncoder for Tags {
    fn encode(&self, _stream: &str, rows: &[WalRow]) -> EncodeResult {
        if rows.is_empty() {
            return Ok(None);
        }
        let ts: Vec<String> = rows.iter().map(|r| r.ts.to_string()).collect();
        Ok(Some(Segment {
            bytes: ts.join(",").into_bytes(),
            rows: rows.len() as u64,
            first_ts: rows[0].ts,
            last_ts: rows[rows.len() - 1].ts,
            index: None,
        }))
    }
}

/// One `ts` column, as real parquet, for the compaction case.
struct Parquet;

impl SegmentEncoder for Parquet {
    fn encode(&self, _stream: &str, rows: &[WalRow]) -> EncodeResult {
        if rows.is_empty() {
            return Ok(None);
        }
        use arrow::array::{ArrayRef, Int64Array};
        use arrow::datatypes::{DataType, Field, Schema};
        use arrow::record_batch::RecordBatch;
        let ts: Vec<i64> = rows.iter().map(|r| r.ts).collect();
        let schema = Arc::new(Schema::new(vec![Field::new("ts", DataType::Int64, false)]));
        let batch = RecordBatch::try_new(
            schema.clone(),
            vec![Arc::new(Int64Array::from(ts)) as ArrayRef],
        )
        .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
        let bytes = dendro::segment::encode_batch(schema, &batch)
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
        Ok(Some(Segment {
            bytes,
            rows: rows.len() as u64,
            first_ts: rows[0].ts,
            last_ts: rows[rows.len() - 1].ts,
            index: None,
        }))
    }
}

fn source() -> SourceMeta {
    SourceMeta {
        labels: BTreeMap::new(),
        metadata: BTreeMap::new(),
        clock_anchor_wall_ns: 0,
    }
}

fn row(ts: i64, blob: &[u8]) -> CallerRow {
    CallerRow {
        ts,
        blob: blob.to_vec(),
    }
}

fn wal_row(stream: &str, ts: i64) -> WalRow {
    WalRow {
        stream: stream.to_string(),
        ts,
        wall_offset: 0,
        row: vec![1],
    }
}

/// A source with one sealed segment on `s` (ts 1..=3) and two live rows
/// (4, 5), plus caller rows on `s` and on a name no stream uses.
fn fixture(path: &std::path::Path) -> i64 {
    let mut db = ArchiveMut::create(path).unwrap();
    let id = db.insert_source(&source()).unwrap();
    db.transaction(|tx| {
        tx.insert_segment(
            id,
            "s",
            0,
            &SegmentMeta {
                rows: 3,
                first_ts: 1,
                last_ts: 3,
            },
            b"1,2,3",
        )?;
        tx.insert_wal_rows(id, &[wal_row("s", 4), wal_row("s", 5)])?;
        tx.insert_caller_rows(
            id,
            "s",
            &[row(1, b"a"), row(3, b"b"), row(3, b"c"), row(5, b"d")],
        )?;
        tx.insert_caller_rows(id, "notes", &[row(2, b"n2"), row(4, b"n4")])
    })
    .unwrap();
    id
}

fn blobs(rows: &[CallerRow]) -> Vec<(i64, String)> {
    rows.iter()
        .map(|r| (r.ts, String::from_utf8_lossy(&r.blob).to_string()))
        .collect()
}

#[test]
fn rows_read_back_in_range_in_order_and_several_per_timestamp() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = fixture(&path);
    let db = Archive::open(&path).unwrap();

    assert_eq!(
        blobs(&db.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap()),
        vec![
            (1, "a".to_string()),
            (3, "b".to_string()),
            (3, "c".to_string()),
            (5, "d".to_string())
        ],
        "oldest first, and insertion order within a timestamp"
    );
    assert_eq!(
        blobs(&db.read_caller_rows(id, "s", 3, 4).unwrap()),
        vec![(3, "b".to_string()), (3, "c".to_string())],
        "the range is inclusive at both ends"
    );
    assert!(db
        .read_caller_rows(id, "missing", i64::MIN, i64::MAX)
        .unwrap()
        .is_empty());
    assert_eq!(
        db.caller_row_streams(id).unwrap(),
        vec!["notes".to_string(), "s".to_string()]
    );
}

#[test]
fn the_last_row_a_predicate_accepts_walks_back_from_a_timestamp() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = fixture(&path);
    let db = Archive::open(&path).unwrap();
    let last = |upto: i64, want: &[u8]| {
        db.last_caller_row_at_or_before(id, "s", upto, |b| b == want)
            .unwrap()
            .map(|r| (r.ts, String::from_utf8_lossy(&r.blob).to_string()))
    };
    // `upto` is inclusive, and a later row is never returned.
    assert_eq!(last(5, b"d"), Some((5, "d".to_string())));
    assert_eq!(last(4, b"d"), None);
    // The walk passes rows the predicate refuses, across timestamps.
    assert_eq!(last(5, b"a"), Some((1, "a".to_string())));
    // Within a timestamp, the last inserted is met first.
    let mut seen = Vec::new();
    db.last_caller_row_at_or_before(id, "s", 3, |b| {
        seen.push(String::from_utf8_lossy(b).to_string());
        false
    })
    .unwrap();
    assert_eq!(seen, vec!["c", "b", "a"]);
    // Streams are separate, and a missing stream has none.
    assert_eq!(
        db.last_caller_row_at_or_before(id, "notes", i64::MAX, |_| true)
            .unwrap()
            .map(|r| r.ts),
        Some(4)
    );
    assert!(db
        .last_caller_row_at_or_before(id, "missing", i64::MAX, |_| true)
        .unwrap()
        .is_none());
}

#[test]
fn a_store_row_does_not_make_a_stream_exist() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = fixture(&path);
    let db = Archive::open(&path).unwrap();
    assert_eq!(
        db.all_streams(id).unwrap(),
        vec!["s".to_string()],
        "`notes` holds caller rows and nothing else, and is not a stream"
    );
    let catalog = dendro::read::catalog(&db).unwrap();
    assert_eq!(catalog[0].streams.len(), 1);
}

#[test]
fn whole_source_retention_evicts_by_the_same_cutoff() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = fixture(&path);
    let mut db = ArchiveMut::open(&path).unwrap();
    let evicted = db.evict_before(id, 4).unwrap();
    assert_eq!(evicted.caller_rows, 4, "a, b, c on `s` and n2 on `notes`");
    assert_eq!(
        blobs(&db.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap()),
        vec![(5, "d".to_string())]
    );
    assert_eq!(
        blobs(
            &db.read_caller_rows(id, "notes", i64::MIN, i64::MAX)
                .unwrap()
        ),
        vec![(4, "n4".to_string())]
    );
}

#[test]
fn per_stream_retention_evicts_only_the_named_streams_and_sees_store_only_names() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = fixture(&path);
    let mut db = ArchiveMut::open(&path).unwrap();

    let evicted = db
        .evict_streams_before(id, i64::MAX, &|name| name == "notes")
        .unwrap();
    assert_eq!(
        evicted.caller_rows, 2,
        "`notes` is not a stream, and is still evictable by name"
    );
    assert_eq!(evicted.segments, 0);
    assert!(db
        .read_caller_rows(id, "notes", i64::MIN, i64::MAX)
        .unwrap()
        .is_empty());
    assert_eq!(
        db.read_caller_rows(id, "s", i64::MIN, i64::MAX)
            .unwrap()
            .len(),
        4,
        "`s` was not named and is untouched"
    );

    let evicted = db.evict_streams_before(id, 4, &|name| name == "s").unwrap();
    assert_eq!(evicted.caller_rows, 3);
    assert_eq!(
        blobs(&db.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap()),
        vec![(5, "d".to_string())]
    );
}

/// The floor a log of full statements and deltas asks for: the latest full
/// statement at or before the oldest row the name still holds, or its whole
/// history when it has none.
fn latest_full_at_or_before(fulls: &[i64], oldest: Option<i64>) -> i64 {
    let Some(oldest) = oldest else {
        return i64::MAX;
    };
    fulls
        .iter()
        .rev()
        .find(|&&ts| ts <= oldest)
        .copied()
        .unwrap_or(i64::MIN)
}

/// A segment spanning the cutoff keeps rows older than it, and the floor is
/// asked with that segment's `first_ts`, so the full statement those rows
/// depend on survives. Flooring at "the latest full statement at or before
/// the cutoff" (3 here) would have deleted `full@1` and `delta@2` while rows 1
/// and 2 stayed readable.
#[test]
fn the_floor_is_asked_with_the_oldest_row_that_survives() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let mut db = ArchiveMut::create(&path).unwrap();
    let id = db.insert_source(&source()).unwrap();
    db.transaction(|tx| {
        tx.insert_segment(
            id,
            "s",
            0,
            &SegmentMeta {
                rows: 4,
                first_ts: 1,
                last_ts: 4,
            },
            b"1,2,3,4",
        )?;
        tx.insert_caller_rows(
            id,
            "s",
            &[
                row(1, b"full"),
                row(2, b"delta"),
                row(3, b"full"),
                row(4, b"delta"),
            ],
        )
    })
    .unwrap();

    let asked = std::cell::RefCell::new(Vec::new());
    let evicted = db
        .evict_before_with_floor(id, 3, &|name, oldest| {
            asked.borrow_mut().push((name.to_string(), oldest));
            latest_full_at_or_before(&[1, 3], oldest)
        })
        .unwrap();
    assert_eq!(asked.into_inner(), vec![("s".to_string(), Some(1))]);
    assert_eq!(evicted.segments, 0, "the segment spans the cutoff");
    assert_eq!(evicted.caller_rows, 0);
}

/// Once the spanning segment goes, the floor moves up to the full statement
/// the surviving rows need, and `notes`, which holds no rows, is asked with
/// `None`.
#[test]
fn a_floor_keeps_a_names_rows_from_the_floor() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = fixture(&path);
    let mut db = ArchiveMut::open(&path).unwrap();
    let asked = std::cell::RefCell::new(Vec::new());
    let evicted = db
        .evict_before_with_floor(id, 4, &|name, oldest| {
            asked.borrow_mut().push((name.to_string(), oldest));
            match name {
                "s" => 3,
                _ => i64::MAX,
            }
        })
        .unwrap();
    assert_eq!(
        asked.into_inner(),
        vec![("notes".to_string(), None), ("s".to_string(), Some(4))],
        "the segment (1..=3) went, so `s` holds rows from the WAL row at 4"
    );
    assert_eq!(evicted.caller_rows, 2, "a on `s` and n2 on `notes`");
    assert_eq!(evicted.segments, 1, "the floor does not keep segments");
    assert_eq!(
        blobs(&db.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap()),
        vec![
            (3, "b".to_string()),
            (3, "c".to_string()),
            (5, "d".to_string())
        ]
    );
    assert_eq!(
        blobs(
            &db.read_caller_rows(id, "notes", i64::MIN, i64::MAX)
                .unwrap()
        ),
        vec![(4, "n4".to_string())]
    );
}

#[test]
fn a_floor_above_the_cutoff_deletes_no_more_than_the_cutoff() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = fixture(&path);
    let mut db = ArchiveMut::open(&path).unwrap();
    let evicted = db.evict_before_with_floor(id, 4, &|_, _| i64::MAX).unwrap();
    assert_eq!(evicted.caller_rows, 4, "the same as `evict_before` at 4");
    assert_eq!(
        blobs(&db.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap()),
        vec![(5, "d".to_string())]
    );
}

#[test]
fn a_floor_of_min_keeps_a_names_whole_history() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = fixture(&path);
    let mut db = ArchiveMut::open(&path).unwrap();
    db.evict_before_with_floor(id, i64::MAX, &|name, _| match name {
        "s" => i64::MIN,
        _ => i64::MAX,
    })
    .unwrap();
    assert_eq!(
        db.read_caller_rows(id, "s", i64::MIN, i64::MAX)
            .unwrap()
            .len(),
        4
    );
    assert!(db
        .read_caller_rows(id, "notes", i64::MIN, i64::MAX)
        .unwrap()
        .is_empty());
}

#[test]
fn per_stream_retention_asks_the_floor_only_for_the_names_it_touches() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = fixture(&path);
    let mut db = ArchiveMut::open(&path).unwrap();
    let asked = std::cell::RefCell::new(Vec::new());
    let evicted = db
        .evict_streams_before_with_floor(id, 4, &|name| name == "s", &|name, _| {
            asked.borrow_mut().push(name.to_string());
            3
        })
        .unwrap();
    assert_eq!(asked.into_inner(), vec!["s".to_string()]);
    assert_eq!(evicted.caller_rows, 1, "only a, on `s`");
    assert_eq!(
        db.read_caller_rows(id, "notes", i64::MIN, i64::MAX)
            .unwrap()
            .len(),
        2,
        "`notes` was not named"
    );
}

#[test]
fn a_copy_carries_the_store_under_the_same_filters() {
    let dir = tempfile::tempdir().unwrap();
    let src_path = dir.path().join("src.dendro");
    let id = fixture(&src_path);
    let src = Archive::open(&src_path).unwrap();

    // Everything: both names, every row, verbatim.
    let all_path = dir.path().join("all.dendro");
    let mut all = ArchiveMut::create(&all_path).unwrap();
    all.transaction(|tx| copy_sources_into(&src, tx, &CopySpec::everything(), &Tags))
        .unwrap();
    assert_eq!(
        all.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap(),
        src.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap()
    );
    assert_eq!(
        all.read_caller_rows(id, "notes", i64::MIN, i64::MAX)
            .unwrap(),
        src.read_caller_rows(id, "notes", i64::MIN, i64::MAX)
            .unwrap(),
        "a series under a name no stream uses is carried too"
    );

    // Ranged, and filtered to one name.
    let some_path = dir.path().join("some.dendro");
    let mut some = ArchiveMut::create(&some_path).unwrap();
    let keep = |name: &str| name == "s";
    let spec = CopySpec {
        start: 3,
        end: 4,
        keep_streams: Some(&keep),
        ..CopySpec::everything()
    };
    some.transaction(|tx| copy_sources_into(&src, tx, &spec, &Tags))
        .unwrap();
    assert_eq!(
        blobs(&some.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap()),
        vec![(3, "b".to_string()), (3, "c".to_string())],
        "the copy's time bound applies to the store"
    );
    assert!(
        some.read_caller_rows(id, "notes", i64::MIN, i64::MAX)
            .unwrap()
            .is_empty(),
        "the stream filter applies to store-only names"
    );
}

#[test]
fn compaction_leaves_the_store_exactly_as_it_was() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("a.dendro");
    let id = {
        let mut db = ArchiveMut::create(&path).unwrap();
        let id = db.insert_source(&source()).unwrap();
        // Four one-row parquet segments, then caller rows across their span.
        for ts in 1..=4i64 {
            let seg = Parquet.encode("s", &[wal_row("s", ts)]).unwrap().unwrap();
            db.transaction(|tx| {
                tx.insert_segment(
                    id,
                    "s",
                    ts as u64 - 1,
                    &SegmentMeta {
                        rows: 1,
                        first_ts: ts,
                        last_ts: ts,
                    },
                    &seg.bytes,
                )
            })
            .unwrap();
        }
        db.insert_caller_rows(id, "s", &[row(1, b"x"), row(2, b"y"), row(4, b"z")])
            .unwrap();
        id
    };
    let before = Archive::open(&path)
        .unwrap()
        .read_caller_rows(id, "s", i64::MIN, i64::MAX)
        .unwrap();

    let mut db = ArchiveMut::open(&path).unwrap();
    let done = compact(&mut db, &CompactSpec::to_rows(100)).unwrap();
    assert_eq!((done.before, done.after), (4, 1), "the segments merged");
    assert_eq!(
        db.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap(),
        before,
        "the store is keyed by time, not by segment"
    );
}

#[cfg(feature = "write")]
#[test]
fn the_writer_lands_rows_in_order_with_the_ticks() {
    use dendro::writer::Writer;
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("w.dendro");
    let mut writer = Writer::create(&path, Box::new(Tags)).unwrap();
    let mut w = writer.add_source(source()).unwrap();
    let id = w.source_id();
    w.wal(vec![wal_row("s", 1)]).unwrap();
    w.caller_rows("s", vec![row(1, b"slot 3 = task 17")])
        .unwrap();
    w.wal(vec![wal_row("s", 2)]).unwrap();
    w.caller_rows(
        "s",
        vec![row(2, b"slot 3 = task 18"), row(2, b"slot 4 = task 19")],
    )
    .unwrap();
    w.caller_rows("s", vec![])
        .expect("an empty batch is a health check, not an error");
    w.sync().unwrap();

    let db = Archive::open(&path).unwrap();
    assert_eq!(
        blobs(&db.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap()),
        vec![
            (1, "slot 3 = task 17".to_string()),
            (2, "slot 3 = task 18".to_string()),
            (2, "slot 4 = task 19".to_string()),
        ]
    );
    assert_eq!(
        db.live_wal(id, "s").unwrap().len(),
        2,
        "the ticks around the store writes landed too"
    );
    w.finalize((2, 0)).unwrap();
    writer.join().unwrap();
}

/// The case floors exist for, through the writer: a stream's identity log is
/// a full statement followed by deltas, and a retention pass whose cutoff
/// falls after the full statement must not take it while deltas that depend
/// on it remain. The rows are sealed first, as a rolling buffer seals before
/// it evicts.
#[cfg(feature = "write")]
#[test]
fn the_writer_keeps_a_full_statement_a_retained_delta_depends_on() {
    use dendro::writer::Writer;
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("w.dendro");
    let mut writer = Writer::create(&path, Box::new(Tags)).unwrap();
    let mut w = writer.add_source(source()).unwrap();
    let id = w.source_id();
    w.wal(vec![wal_row("s", 1), wal_row("s", 2)]).unwrap();
    w.seal(vec!["s".to_string()]).unwrap();
    w.wal(vec![wal_row("s", 3), wal_row("s", 4)]).unwrap();
    w.seal(vec!["s".to_string()]).unwrap();
    w.caller_rows(
        "s",
        vec![row(1, b"full"), row(2, b"delta"), row(4, b"delta")],
    )
    .unwrap();

    let evicted = w
        .evict_before_with_floor(
            3,
            Box::new(|_, oldest| latest_full_at_or_before(&[1], oldest)),
        )
        .unwrap();
    assert_eq!(evicted.segments, 1, "the segment holding 1 and 2");
    assert_eq!(evicted.live_rows, 0, "everything evicted was sealed");
    assert_eq!(evicted.caller_rows, 0, "delta@4 still depends on full@1");
    w.sync().unwrap();
    let db = Archive::open(&path).unwrap();
    assert_eq!(
        blobs(&db.read_caller_rows(id, "s", i64::MIN, i64::MAX).unwrap()),
        vec![
            (1, "full".to_string()),
            (2, "delta".to_string()),
            (4, "delta".to_string())
        ]
    );
    w.finalize((4, 0)).unwrap();
    writer.join().unwrap();
}