hayson-engine 0.25.0

Storage engine for hayson document databases — the embeddable core behind hayson-server, hayson-cli and hayson-browser
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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
use std::fs::{File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, PoisonError};

use super::record::{parse_entry, LogEntry};
use crate::errors::HaysonLogReplacedError;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Slot {
    pub offset: u64,
    pub length: u64,
}

pub struct ScannedEntry {
    pub entry: LogEntry,
    pub slot: Slot,
}

pub struct ScanResult {
    pub entries: Vec<ScannedEntry>,
    pub end_offset: u64,
    pub truncated_bytes: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LogProbe {
    pub size: u64,
    /// `None` where the host reports no inode: identity then rests on size alone, which detects
    /// growth and truncation but not a same-size replacement of the file behind the path.
    pub inode: Option<u64>,
}

const NEWLINE: u8 = b'\n';

fn open_at(path: &Path, readonly: bool) -> io::Result<File> {
    if readonly {
        OpenOptions::new().read(true).open(path)
    } else {
        OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(path)
    }
}

#[cfg(unix)]
fn inode_of_file(file: &File) -> Option<u64> {
    use std::os::unix::fs::MetadataExt;
    file.metadata().ok().map(|metadata| metadata.ino())
}

#[cfg(unix)]
fn inode_at(path: &Path) -> Option<u64> {
    use std::os::unix::fs::MetadataExt;
    std::fs::metadata(path).ok().map(|metadata| metadata.ino())
}

// NOTE: std's wasi MetadataExt is unstable; the raw `fd_filestat_get` carries the same `ino`.
// NOTE: a wasi host that fills no inode reports 0 for every file — that is no identity at all.
#[cfg(target_os = "wasi")]
fn inode_of_file(file: &File) -> Option<u64> {
    use std::os::fd::AsRawFd;
    let stat = unsafe { wasi::fd_filestat_get(file.as_raw_fd() as wasi::Fd) }.ok()?;
    Some(stat.ino).filter(|&ino| ino != 0)
}

#[cfg(target_os = "wasi")]
fn inode_at(path: &Path) -> Option<u64> {
    File::open(path).ok().and_then(|file| inode_of_file(&file))
}

#[cfg(not(any(unix, target_os = "wasi")))]
fn inode_of_file(_file: &File) -> Option<u64> {
    None
}

#[cfg(not(any(unix, target_os = "wasi")))]
fn inode_at(_path: &Path) -> Option<u64> {
    None
}

/// Append-only log file for one table (or one partition of one), matching the TS `LogFile`
/// contract: every read and every append names its own absolute offset, so no caller can be
/// disturbed by another's position in the file. The descriptor itself does carry a cursor —
/// stable Rust has no portable positional I/O — so the seek and the transfer that follows it are
/// held together under one mutex, which is what makes the offsets behave as if they were.
pub struct LogFile {
    path: PathBuf,
    file: Mutex<File>,
    size: u64,
    inode: Option<u64>,
    readonly: bool,
    fsync: bool,
    /// How many `fsync`s this handle actually issued. Test-only: the `sync: "never"` contract is
    /// "no `fsync` reaches the disk", and no portable observable outside the process can show the
    /// absence of one — `/proc/self/io` counts the whole process, not this handle.
    #[cfg(test)]
    fsyncs: std::sync::atomic::AtomicU64,
}

impl LogFile {
    pub fn open(path: impl AsRef<Path>, readonly: bool) -> io::Result<Self> {
        let path = path.as_ref().to_path_buf();
        let file = open_at(&path, readonly)?;
        let metadata = file.metadata()?;
        Ok(LogFile {
            size: metadata.len(),
            inode: inode_of_file(&file),
            path,
            file: Mutex::new(file),
            readonly,
            fsync: true,
            #[cfg(test)]
            fsyncs: std::sync::atomic::AtomicU64::new(0),
        })
    }

    /// Whether [`LogFile::sync`] reaches the disk at all. On (the default) is the catalog's
    /// `sync: "commit"`; off is `sync: "never"`, which hands durability to the kernel and makes
    /// every `sync` a no-op. `table::Table::set_sync` translates the catalog mode into this flag —
    /// `storage/` sits below `catalog/` and does not name its types.
    pub fn set_fsync(&mut self, fsync: bool) {
        self.fsync = fsync;
    }

    // NOTE: a poisoned guard is recovered, not propagated — a panic mid-read leaves no torn state
    // here, only a cursor the next seek overwrites anyway.
    fn handle(&self) -> MutexGuard<'_, File> {
        self.file.lock().unwrap_or_else(PoisonError::into_inner)
    }

    fn read_exact_at(&self, buffer: &mut [u8], offset: u64) -> io::Result<()> {
        let mut file = self.handle();
        file.seek(SeekFrom::Start(offset))?;
        file.read_exact(buffer)
    }

    pub fn size(&self) -> u64 {
        self.size
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Whether the file `probe` found at this handle's path is no longer the one it holds open: a
    /// different inode (an atomic rewrite — how `compaction::compact_log` lands — or a file swapped
    /// in from outside, which is what a `git checkout` of a tracked data directory does), or bytes
    /// gone from a file this engine only ever appends to. Without an inode on either side only the
    /// shrink is visible, so a same-size or longer replacement passes as "nothing happened" there.
    pub fn replaced_by(&self, probe: &LogProbe) -> bool {
        match (self.inode, probe.inode) {
            (Some(held), Some(found)) => held != found,
            _ => probe.size < self.size,
        }
    }

    /// An append onto a file that was unlinked or replaced under this handle would land in the
    /// orphaned inode and vanish with it, while every read from memory kept showing the record —
    /// silent data loss. So every append first checks that the path still leads to the open file.
    /// A snapshot and a compaction ask the same question before they start, for the same reason:
    /// the one would describe the orphaned inode, the other would overwrite the file now at the
    /// path with that inode's contents.
    pub(crate) fn refuse_if_replaced(&self) -> io::Result<()> {
        let replaced = match std::fs::metadata(&self.path) {
            Ok(info) => self.replaced_by(&LogProbe {
                size: info.len(),
                inode: inode_at(&self.path),
            }),
            Err(error) if error.kind() == io::ErrorKind::NotFound => true,
            Err(error) => return Err(error),
        };
        if replaced {
            return Err(io::Error::other(HaysonLogReplacedError {
                path: self.path.display().to_string(),
            }));
        }
        Ok(())
    }

    /// Adopts a size this handle did not write itself — the tracked size another process's append
    /// left behind, or the end of the last complete line after a scan met a half-written tail.
    /// Unlike [`LogFile::truncate`] it touches nothing on disk: it only moves where this handle
    /// believes the file ends, which is where its next scan starts and its next append lands.
    pub fn adopt_size(&mut self, size: u64) {
        self.size = size;
    }

    pub fn probe(&self) -> Option<LogProbe> {
        let info = std::fs::metadata(&self.path).ok()?;
        Some(LogProbe {
            size: info.len(),
            inode: inode_at(&self.path),
        })
    }

    pub fn append(&mut self, lines: &[String]) -> io::Result<Vec<Slot>> {
        if self.readonly {
            return Err(io::Error::other("append on a readonly log"));
        }
        if lines.is_empty() {
            return Ok(Vec::new());
        }
        self.refuse_if_replaced()?;
        let payload = format!("{}\n", lines.join("\n"));
        {
            let mut file = self.handle();
            file.seek(SeekFrom::Start(self.size))?;
            file.write_all(payload.as_bytes())?;
        }

        let mut slots = Vec::with_capacity(lines.len());
        let mut cursor = self.size;
        for line in lines {
            let length = line.len() as u64;
            slots.push(Slot { offset: cursor, length });
            cursor += length + 1;
        }
        self.size = cursor;
        Ok(slots)
    }

    pub fn read_slice(&self, slot: &Slot) -> io::Result<String> {
        let mut buffer = vec![0u8; slot.length as usize];
        self.read_exact_at(&mut buffer, slot.offset)?;
        String::from_utf8(buffer).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
    }

    // NOTE: raw bytes, not read_slice — an arbitrary byte window can split a multi-byte char.
    pub fn read_bytes(&self, offset: u64, length: u64) -> io::Result<Vec<u8>> {
        let mut buffer = vec![0u8; length as usize];
        self.read_exact_at(&mut buffer, offset)?;
        Ok(buffer)
    }

    /// Scans from `from_offset` to the current tracked size, yielding one entry per full line.
    /// An unterminated tail is reported via `truncated_bytes`, never parsed.
    pub fn scan(&self, from_offset: u64) -> io::Result<ScanResult> {
        let mut entries = Vec::new();
        if from_offset >= self.size {
            return Ok(ScanResult {
                entries,
                end_offset: from_offset,
                truncated_bytes: 0,
            });
        }
        let mut buffer = vec![0u8; (self.size - from_offset) as usize];
        self.read_exact_at(&mut buffer, from_offset)?;

        let mut line_start = from_offset;
        let mut search_from = 0usize;
        while let Some(rel) = memchr(NEWLINE, &buffer[search_from..]) {
            let break_at = search_from + rel;
            let raw = &buffer[search_from..break_at];
            let length = raw.len() as u64;
            if length > 0 {
                let line = std::str::from_utf8(raw).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
                let entry = parse_entry(line, self.path.to_string_lossy().as_ref(), line_start)
                    .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
                entries.push(ScannedEntry {
                    entry,
                    slot: Slot {
                        offset: line_start,
                        length,
                    },
                });
            }
            line_start += length + 1;
            search_from = break_at + 1;
        }

        Ok(ScanResult {
            entries,
            end_offset: line_start,
            truncated_bytes: (buffer.len() - search_from) as u64,
        })
    }

    pub fn truncate(&mut self, size: u64) -> io::Result<()> {
        self.handle().set_len(size)?;
        self.size = size;
        Ok(())
    }

    /// Gets rid of the half-written tail `scan` met past its last complete line — the bytes a
    /// killed writer or a full disk left behind. Nothing to do when the scan found none.
    ///
    /// A writable handle cuts them off the file itself: without that, `size` stays at the physical
    /// end and the next [`LogFile::append`] lands straight after an unterminated line, gluing the
    /// two into a single line no later scan can parse — a rebuild without a snapshot then loses the
    /// whole table, not just the write that was interrupted.
    ///
    /// A readonly handle owns no right to change the file, so it only moves its own end marker
    /// ([`LogFile::adopt_size`]); the tail stays on disk until a writable handle opens the log.
    pub fn discard_partial_tail(&mut self, scan: &ScanResult) -> io::Result<()> {
        if scan.truncated_bytes == 0 {
            return Ok(());
        }
        if self.readonly {
            self.adopt_size(scan.end_offset);
            return Ok(());
        }
        self.truncate(scan.end_offset)?;
        self.sync()
    }

    pub fn sync(&self) -> io::Result<()> {
        if self.readonly || !self.fsync {
            return Ok(());
        }
        self.handle().sync_data()?;
        #[cfg(test)]
        self.fsyncs.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        Ok(())
    }

    #[cfg(test)]
    pub fn fsyncs(&self) -> u64 {
        self.fsyncs.load(std::sync::atomic::Ordering::Relaxed)
    }

    pub fn reopen(&mut self) -> io::Result<()> {
        let file = open_at(&self.path, self.readonly)?;
        self.size = file.metadata()?.len();
        self.inode = inode_of_file(&file);
        *self.handle() = file;
        Ok(())
    }
}

fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
    haystack.iter().position(|&b| b == needle)
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::io::Write;

    use super::super::record::{serialize_entry, LogMeta, LogOp};
    use super::*;
    use crate::schema::HaysonRecord;

    fn line(key: &str, seq: u64, name: &str) -> String {
        let mut data = HaysonRecord::new();
        data.insert("name".into(), serde_json::Value::String(name.into()));
        serialize_entry(
            &LogMeta {
                key: key.into(),
                op: LogOp::Put,
                seq,
                ts: 1,
            },
            Some(&data),
        )
    }

    fn log_replaced_of(error: &io::Error) -> Option<&HaysonLogReplacedError> {
        error.get_ref()?.downcast_ref::<HaysonLogReplacedError>()
    }

    /// Same bytes, new inode — what `git checkout` does to a tracked file. Only the inode can tell,
    /// so the test needs a host that reports one.
    #[cfg(unix)]
    #[test]
    fn refuses_to_append_once_the_file_at_its_path_is_a_different_one() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();
        log.append(&[line("a", 1, "Ania")]).unwrap();
        let swapped = dir.path().join("users.jsonl.swap");
        fs::copy(&path, &swapped).unwrap();
        fs::rename(&swapped, &path).unwrap();

        let error = log.append(&[line("b", 2, "Piotr")]).unwrap_err();

        let replaced = log_replaced_of(&error).expect("a typed refusal, not a plain io error");
        assert_eq!(replaced.path, path.display().to_string());
        assert_eq!(
            fs::read_to_string(&path).unwrap().lines().count(),
            1,
            "nothing landed in the orphaned inode either"
        );

        log.reopen().unwrap();
        log.append(&[line("b", 2, "Piotr")]).unwrap();
        assert_eq!(fs::read_to_string(&path).unwrap().lines().count(), 2);
    }

    #[test]
    fn refuses_to_append_once_the_file_is_gone_from_its_path() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();
        log.append(&[line("a", 1, "Ania")]).unwrap();
        fs::remove_file(&path).unwrap();

        let error = log.append(&[line("b", 2, "Piotr")]).unwrap_err();

        assert!(log_replaced_of(&error).is_some(), "{error}");
        assert!(!path.exists(), "the refusal did not recreate the file");
    }

    #[test]
    fn refuses_to_append_once_the_file_at_its_path_is_shorter_than_what_it_wrote() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();
        log.append(&[line("a", 1, "Ania"), line("b", 2, "Piotr")]).unwrap();
        let swapped = dir.path().join("users.jsonl.swap");
        fs::write(&swapped, format!("{}\n", line("a", 1, "Ania"))).unwrap();
        fs::rename(&swapped, &path).unwrap();

        let error = log.append(&[line("c", 3, "Zosia")]).unwrap_err();

        assert!(log_replaced_of(&error).is_some(), "{error}");
    }

    #[test]
    fn returns_byte_offsets_that_survive_multibyte_text() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();

        let first = line("a", 1, "Łódź");
        let second = line("b", 2, "Ania");
        let slots = log.append(&[first.clone(), second.clone()]).unwrap();

        assert_eq!(slots[0].offset, 0);
        assert_eq!(slots[0].length, first.len() as u64);
        assert_eq!(slots[1].offset, first.len() as u64 + 1);

        assert_eq!(log.read_slice(&slots[0]).unwrap(), first);
        assert_eq!(log.read_slice(&slots[1]).unwrap(), second);
    }

    #[test]
    fn reconstructs_state_after_reopening_the_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");

        {
            let mut log = LogFile::open(&path, false).unwrap();
            log.append(&[line("a", 1, "Ania"), line("b", 2, "Piotr")]).unwrap();
            log.sync().unwrap();
        }

        let mut reopened = LogFile::open(&path, false).unwrap();
        assert_eq!(reopened.size(), fs::metadata(&path).unwrap().len());

        let scan = reopened.scan(0).unwrap();
        assert_eq!(scan.entries.len(), 2);
        assert_eq!(scan.truncated_bytes, 0);
        assert_eq!(scan.entries[0].entry.meta.key, "a");
        assert_eq!(scan.entries[1].entry.meta.key, "b");
        assert_eq!(
            scan.entries[0].entry.data.as_ref().unwrap().get("name"),
            Some(&serde_json::Value::String("Ania".into()))
        );

        reopened.append(&[line("c", 3, "Zosia")]).unwrap();
        reopened.reopen().unwrap();
        let after_reopen = reopened.scan(0).unwrap();
        assert_eq!(after_reopen.entries.len(), 3);
        assert_eq!(after_reopen.entries[2].entry.meta.key, "c");
    }

    #[test]
    fn reads_to_the_last_full_line_when_the_tail_is_truncated_mid_write() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        {
            let mut log = LogFile::open(&path, false).unwrap();
            log.append(&[line("a", 1, "Ania")]).unwrap();
        }
        {
            let mut file = std::fs::OpenOptions::new().append(true).open(&path).unwrap();
            file.write_all(br#"{"_k":"b","_op":"put","_seq":2,"_ts"#).unwrap();
        }

        let log = LogFile::open(&path, false).unwrap();
        let scan = log.scan(0).unwrap();
        assert_eq!(scan.entries.len(), 1);
        assert_eq!(scan.entries[0].entry.meta.key, "a");
        assert!(scan.truncated_bytes > 0);
        assert_eq!(scan.end_offset, scan.entries[0].slot.length + 1);
    }

    /// One complete line plus the unterminated start of a second, and where the complete one ends.
    fn torn_log(path: &Path) -> u64 {
        let mut log = LogFile::open(path, false).unwrap();
        let healthy = log.append(&[line("a", 1, "Ania")]).unwrap()[0].length + 1;
        let mut file = std::fs::OpenOptions::new().append(true).open(path).unwrap();
        file.write_all(br#"{"_k":"b","_op":"put","_seq":2,"_ts"#).unwrap();
        healthy
    }

    #[test]
    fn a_writable_handle_cuts_the_torn_tail_so_the_next_append_lands_on_its_own_line() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let healthy = torn_log(&path);

        let mut log = LogFile::open(&path, false).unwrap();
        let scan = log.scan(0).unwrap();
        log.discard_partial_tail(&scan).unwrap();

        assert_eq!(log.size(), healthy);
        assert_eq!(fs::metadata(&path).unwrap().len(), healthy);

        log.append(&[line("c", 3, "Zosia")]).unwrap();
        let after = LogFile::open(&path, false).unwrap().scan(0).unwrap();
        assert_eq!(after.truncated_bytes, 0);
        assert_eq!(after.entries.len(), 2);
        assert_eq!(after.entries[1].entry.meta.key, "c");
    }

    #[test]
    fn a_readonly_handle_forgets_the_torn_tail_without_touching_the_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let healthy = torn_log(&path);
        let torn_size = fs::metadata(&path).unwrap().len();

        let mut log = LogFile::open(&path, true).unwrap();
        let scan = log.scan(0).unwrap();
        log.discard_partial_tail(&scan).unwrap();

        assert_eq!(log.size(), healthy);
        assert_eq!(fs::metadata(&path).unwrap().len(), torn_size);
    }

    #[test]
    fn sync_never_issues_no_fsync_while_the_default_commit_still_does() {
        let dir = tempfile::tempdir().unwrap();
        let payload = [line("a", 1, "Ania"), line("b", 2, "Piotr")];

        let quiet_path = dir.path().join("quiet.jsonl");
        let mut quiet = LogFile::open(&quiet_path, false).unwrap();
        quiet.set_fsync(false);
        quiet.append(&payload).unwrap();
        quiet.sync().unwrap();
        quiet.sync().unwrap();

        let loud_path = dir.path().join("loud.jsonl");
        let mut loud = LogFile::open(&loud_path, false).unwrap();
        loud.append(&payload).unwrap();
        loud.sync().unwrap();
        loud.sync().unwrap();

        assert_eq!(quiet.fsyncs(), 0, "sync on a never-fsync log must not reach the disk");
        assert_eq!(loud.fsyncs(), 2, "the default log must still fsync once per sync");
        assert_eq!(fs::read(&quiet_path).unwrap(), fs::read(&loud_path).unwrap());
    }

    #[test]
    fn a_never_fsync_log_still_reads_back_everything_it_was_given() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();
        log.set_fsync(false);

        let first = line("a", 1, "Ania");
        let slots = log.append(&[first.clone(), line("b", 2, "Piotr")]).unwrap();
        log.sync().unwrap();

        assert_eq!(log.read_slice(&slots[0]).unwrap(), first);
        assert_eq!(log.size(), fs::metadata(&path).unwrap().len());
        let scan = LogFile::open(&path, false).unwrap().scan(0).unwrap();
        assert_eq!(scan.entries.len(), 2);
        assert_eq!(scan.truncated_bytes, 0);
    }

    #[test]
    fn cutting_a_torn_tail_off_a_never_fsync_log_still_cuts_it_without_an_fsync() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let healthy = torn_log(&path);

        let mut log = LogFile::open(&path, false).unwrap();
        log.set_fsync(false);
        let scan = log.scan(0).unwrap();
        log.discard_partial_tail(&scan).unwrap();

        assert_eq!(
            fs::metadata(&path).unwrap().len(),
            healthy,
            "the tail is cut whatever sync says"
        );
        assert_eq!(log.fsyncs(), 0);
    }

    #[test]
    fn reopening_keeps_the_durability_the_handle_was_configured_with() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();
        log.set_fsync(false);
        log.append(&[line("a", 1, "Ania")]).unwrap();
        log.reopen().unwrap();

        log.append(&[line("b", 2, "Piotr")]).unwrap();
        log.sync().unwrap();
        assert_eq!(log.fsyncs(), 0, "reopen must not silently rearm fsync");
    }

    #[test]
    fn probe_reflects_size_and_inode_from_the_path_not_the_handle() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();
        log.append(&[line("a", 1, "Ania")]).unwrap();

        let probe = log.probe().unwrap();
        assert_eq!(probe.size, log.size());
        #[cfg(unix)]
        assert!(probe.inode.is_some());
    }

    #[test]
    fn concurrent_readers_sharing_one_handle_each_get_their_own_offset() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();

        let written: Vec<String> = (0..64)
            .map(|i| line(&format!("k{i}"), i, &format!("name-{i}")))
            .collect();
        let slots = log.append(&written).unwrap();

        let log = &log;
        let slots = &slots;
        let written = &written;
        std::thread::scope(|scope| {
            for lane in 0..8usize {
                scope.spawn(move || {
                    for round in 0..50 {
                        // NOTE: each lane walks the slots in its own order, so a shared cursor
                        // leaking between threads would show up as a mismatched line here.
                        let i = (lane * 7 + round * 13) % slots.len();
                        assert_eq!(log.read_slice(&slots[i]).unwrap(), written[i]);
                        let raw = log.read_bytes(slots[i].offset, slots[i].length).unwrap();
                        assert_eq!(raw, written[i].as_bytes());
                    }
                });
            }
        });
    }

    #[test]
    fn a_read_between_two_appends_does_not_disturb_where_the_next_append_lands() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();

        let first = line("a", 1, "Ania");
        let first_slots = log.append(std::slice::from_ref(&first)).unwrap();
        assert_eq!(log.read_slice(&first_slots[0]).unwrap(), first);

        let second = line("b", 2, "Piotr");
        let second_slots = log.append(std::slice::from_ref(&second)).unwrap();
        assert_eq!(second_slots[0].offset, first.len() as u64 + 1);
        assert_eq!(log.read_slice(&second_slots[0]).unwrap(), second);
        assert_eq!(log.size(), fs::metadata(&path).unwrap().len());

        let scan = log.scan(0).unwrap();
        assert_eq!(scan.entries.len(), 2);
        assert_eq!(scan.truncated_bytes, 0);
    }

    #[test]
    fn a_scan_after_a_truncate_reads_only_what_is_left() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("users.jsonl");
        let mut log = LogFile::open(&path, false).unwrap();

        let first = line("a", 1, "Ania");
        log.append(&[first.clone(), line("b", 2, "Piotr")]).unwrap();
        log.truncate(first.len() as u64 + 1).unwrap();

        let scan = log.scan(0).unwrap();
        assert_eq!(scan.entries.len(), 1);
        assert_eq!(scan.entries[0].entry.meta.key, "a");

        let third = line("c", 3, "Zosia");
        let slots = log.append(std::slice::from_ref(&third)).unwrap();
        assert_eq!(slots[0].offset, first.len() as u64 + 1);
        assert_eq!(log.read_slice(&slots[0]).unwrap(), third);
    }
}