hayson-engine 0.9.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
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use serde_json::Value;

use crate::schema::HaysonRecord;

/// Approximate document size at or above which a record is cached in the large-document tier
/// instead of the main one — four kilobytes, which no record of scalars and short strings reaches
/// and every sealed `cypher` envelope or paragraph-length `fulltext` field does.
pub const DEFAULT_LARGE_BYTES: usize = 4 * 1024;

/// How many parsed documents a table keeps in memory in front of its log — the read cache
/// [`crate::table::Table::get`] answers a repeated read from instead of re-reading and re-parsing
/// the line the registry points at.
///
/// `Disabled` is the default everywhere, including [`crate::database::OpenOptions`]: caching a
/// document is a memory bet a caller has to place on purpose.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CachePolicy {
    #[default]
    Disabled,
    Enabled {
        /// Most documents held below the `large_bytes` line, evicting least-recently-read first.
        entries: usize,
        /// Most documents held at or above the `large_bytes` line — its own, smaller budget, so a
        /// handful of big records cannot evict every small one.
        large_entries: usize,
        /// The size, as [`approx_bytes`] measures it, that separates the two tiers.
        large_bytes: usize,
    },
}

impl CachePolicy {
    /// `entries` small documents, an eighth as many large ones (never fewer than one) and
    /// `DEFAULT_LARGE_BYTES` between the tiers — the shape for a caller who has a rough memory
    /// budget rather than an opinion about tiers. `0` is [`CachePolicy::Disabled`].
    pub fn with_entries(entries: usize) -> Self {
        if entries == 0 {
            return CachePolicy::Disabled;
        }
        CachePolicy::Enabled {
            entries,
            large_entries: (entries / 8).max(1),
            large_bytes: DEFAULT_LARGE_BYTES,
        }
    }
}

/// Roughly how much a parsed document holds, without serializing it: string and field-name lengths
/// counted for real, every scalar counted as eight bytes. It only has to tell a long `fulltext`
/// field or a base64 `cypher` envelope from a handful of numbers, which is all the tier split asks
/// of it.
fn approx_bytes(record: &HaysonRecord) -> usize {
    record
        .iter()
        .map(|(field, value)| field.len() + value_bytes(value))
        .sum()
}

fn value_bytes(value: &Value) -> usize {
    match value {
        Value::Null | Value::Bool(_) | Value::Number(_) => 8,
        Value::String(text) => text.len(),
        Value::Array(items) => items.iter().map(value_bytes).sum::<usize>() + 8,
        Value::Object(fields) => {
            fields
                .iter()
                .map(|(field, value)| field.len() + value_bytes(value))
                .sum::<usize>()
                + 8
        }
    }
}

struct Node {
    key: Arc<str>,
    record: HaysonRecord,
    prev: Option<usize>,
    next: Option<usize>,
}

/// A fixed-capacity LRU over a slab of nodes: the map finds a key, the nodes are threaded
/// most-recently-used first, and eviction hands its slot back to `free` for the next insert.
///
/// Every operation is O(1) on purpose — a cache sized in the tens of thousands must not turn a
/// read miss into a scan of everything cached, which is the shape a "least recently used wins"
/// timestamp sweep would have.
struct Lru {
    capacity: usize,
    map: HashMap<Arc<str>, usize>,
    nodes: Vec<Node>,
    free: Vec<usize>,
    head: Option<usize>,
    tail: Option<usize>,
}

impl Lru {
    fn new(capacity: usize) -> Self {
        Lru {
            capacity,
            map: HashMap::new(),
            nodes: Vec::new(),
            free: Vec::new(),
            head: None,
            tail: None,
        }
    }

    fn get(&mut self, key: &str) -> Option<&HaysonRecord> {
        let at = *self.map.get(key)?;
        self.unlink(at);
        self.push_front(at);
        Some(&self.nodes[at].record)
    }

    fn insert(&mut self, key: &str, record: HaysonRecord) {
        if self.capacity == 0 {
            return;
        }
        if let Some(&at) = self.map.get(key) {
            self.nodes[at].record = record;
            self.unlink(at);
            self.push_front(at);
            return;
        }
        if self.map.len() >= self.capacity {
            self.evict_tail();
        }
        let key: Arc<str> = Arc::from(key);
        let node = Node {
            key: Arc::clone(&key),
            record,
            prev: None,
            next: None,
        };
        let at = match self.free.pop() {
            Some(at) => {
                self.nodes[at] = node;
                at
            }
            None => {
                self.nodes.push(node);
                self.nodes.len() - 1
            }
        };
        self.map.insert(key, at);
        self.push_front(at);
    }

    fn remove(&mut self, key: &str) {
        let Some(at) = self.map.remove(key) else { return };
        self.unlink(at);
        self.release(at);
    }

    fn clear(&mut self) {
        self.map.clear();
        self.nodes.clear();
        self.free.clear();
        self.head = None;
        self.tail = None;
    }

    #[cfg(test)]
    fn len(&self) -> usize {
        self.map.len()
    }

    fn evict_tail(&mut self) {
        let Some(at) = self.tail else { return };
        let key = Arc::clone(&self.nodes[at].key);
        self.map.remove(&key);
        self.unlink(at);
        self.release(at);
    }

    /// Empties the slot without shrinking the slab, so the document it held stops taking memory
    /// before another insert reuses the slot.
    fn release(&mut self, at: usize) {
        self.nodes[at].key = Arc::from("");
        self.nodes[at].record = HaysonRecord::new();
        self.free.push(at);
    }

    // NOTE: only sound for a node currently in the list — `prev == None` is read as "this is head".
    fn unlink(&mut self, at: usize) {
        let prev = self.nodes[at].prev.take();
        let next = self.nodes[at].next.take();
        match prev {
            Some(prev) => self.nodes[prev].next = next,
            None => self.head = next,
        }
        match next {
            Some(next) => self.nodes[next].prev = prev,
            None => self.tail = prev,
        }
    }

    fn push_front(&mut self, at: usize) {
        self.nodes[at].prev = None;
        self.nodes[at].next = self.head;
        if let Some(head) = self.head {
            self.nodes[head].prev = Some(at);
        }
        self.head = Some(at);
        if self.tail.is_none() {
            self.tail = Some(at);
        }
    }
}

struct Tiers {
    small: Lru,
    large: Lru,
}

/// The bounded cache of parsed documents a table reads through, in the two tiers
/// [`CachePolicy`] describes. A key lives in exactly one of them, chosen by [`approx_bytes`] of the
/// record when it was cached.
///
/// Interior mutability, because `Table::get` takes `&self` and a cache hit still has to move the
/// key it hit to the front of the list. A `Mutex` rather than a `RefCell`: `Database` is
/// `Send + Sync` and pinned as such by a test. Nothing but this module's own code ever runs under
/// the lock, so it is a leaf — no other lock is ever taken while it is held.
pub(crate) struct RecordCache {
    policy: CachePolicy,
    tiers: Mutex<Tiers>,
}

impl RecordCache {
    pub(crate) fn new(policy: CachePolicy) -> Self {
        let (entries, large_entries) = match policy {
            CachePolicy::Disabled => (0, 0),
            CachePolicy::Enabled {
                entries, large_entries, ..
            } => (entries, large_entries),
        };
        RecordCache {
            policy,
            tiers: Mutex::new(Tiers {
                small: Lru::new(entries),
                large: Lru::new(large_entries),
            }),
        }
    }

    pub(crate) fn policy(&self) -> CachePolicy {
        self.policy
    }

    fn enabled(&self) -> bool {
        self.policy != CachePolicy::Disabled
    }

    /// The cached document under `key`, moved to the front of its tier. Cloned out: the record
    /// cannot leave the lock by reference, and cloning a parsed document is still what a read of
    /// the log would end up doing after slicing and parsing the line.
    pub(crate) fn get(&self, key: &str) -> Option<HaysonRecord> {
        if !self.enabled() {
            return None;
        }
        let mut tiers = self.locked();
        if let Some(record) = tiers.small.get(key) {
            return Some(record.clone());
        }
        tiers.large.get(key).cloned()
    }

    pub(crate) fn insert(&self, key: &str, record: &HaysonRecord) {
        let CachePolicy::Enabled { large_bytes, .. } = self.policy else {
            return;
        };
        let large = approx_bytes(record) >= large_bytes;
        let mut tiers = self.locked();
        let tier = if large { &mut tiers.large } else { &mut tiers.small };
        tier.insert(key, record.clone());
    }

    pub(crate) fn invalidate(&self, key: &str) {
        if !self.enabled() {
            return;
        }
        let mut tiers = self.locked();
        tiers.small.remove(key);
        tiers.large.remove(key);
    }

    pub(crate) fn clear(&self) {
        if !self.enabled() {
            return;
        }
        let mut tiers = self.locked();
        tiers.small.clear();
        tiers.large.clear();
    }

    #[cfg(test)]
    pub(crate) fn len(&self) -> (usize, usize) {
        let tiers = self.locked();
        (tiers.small.len(), tiers.large.len())
    }

    // NOTE: a poisoned cache is taken anyway — dropping an invalidation here would serve stale docs.
    fn locked(&self) -> std::sync::MutexGuard<'_, Tiers> {
        self.tiers.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn record(name: &str) -> HaysonRecord {
        let mut record = HaysonRecord::new();
        record.insert("name".to_string(), json!(name));
        record
    }

    fn big(bytes: usize) -> HaysonRecord {
        let mut record = HaysonRecord::new();
        record.insert("body".to_string(), json!("x".repeat(bytes)));
        record
    }

    fn cache_of(entries: usize, large_entries: usize) -> RecordCache {
        RecordCache::new(CachePolicy::Enabled {
            entries,
            large_entries,
            large_bytes: DEFAULT_LARGE_BYTES,
        })
    }

    #[test]
    fn a_disabled_cache_answers_nothing_and_keeps_nothing() {
        let cache = RecordCache::new(CachePolicy::Disabled);
        cache.insert("u1", &record("Ania"));
        assert_eq!(cache.get("u1"), None);
        assert_eq!(cache.len(), (0, 0));
    }

    #[test]
    fn with_entries_derives_a_smaller_large_tier_and_reads_zero_as_disabled() {
        assert_eq!(CachePolicy::with_entries(0), CachePolicy::Disabled);
        assert_eq!(
            CachePolicy::with_entries(64),
            CachePolicy::Enabled {
                entries: 64,
                large_entries: 8,
                large_bytes: DEFAULT_LARGE_BYTES,
            }
        );
        assert_eq!(
            CachePolicy::with_entries(1),
            CachePolicy::Enabled {
                entries: 1,
                large_entries: 1,
                large_bytes: DEFAULT_LARGE_BYTES,
            }
        );
    }

    #[test]
    fn what_was_inserted_reads_back_until_it_is_invalidated() {
        let cache = cache_of(4, 2);
        cache.insert("u1", &record("Ania"));
        assert_eq!(cache.get("u1"), Some(record("Ania")));
        cache.invalidate("u1");
        assert_eq!(cache.get("u1"), None);
    }

    #[test]
    fn a_second_insert_on_one_key_replaces_the_document_without_taking_a_second_slot() {
        let cache = cache_of(4, 2);
        cache.insert("u1", &record("Ania"));
        cache.insert("u1", &record("Zosia"));
        assert_eq!(cache.get("u1"), Some(record("Zosia")));
        assert_eq!(cache.len(), (1, 0));
    }

    #[test]
    fn a_full_tier_evicts_the_least_recently_read_key_and_keeps_the_one_just_read() {
        let cache = cache_of(2, 1);
        cache.insert("u1", &record("Ania"));
        cache.insert("u2", &record("Piotr"));
        // NOTE: this read is what makes u2, not u1, the eviction candidate.
        assert_eq!(cache.get("u1"), Some(record("Ania")));
        cache.insert("u3", &record("Zosia"));

        assert_eq!(cache.get("u2"), None);
        assert_eq!(cache.get("u1"), Some(record("Ania")));
        assert_eq!(cache.get("u3"), Some(record("Zosia")));
        assert_eq!(cache.len(), (2, 0));
    }

    #[test]
    fn a_large_document_lands_in_its_own_tier_and_evicts_only_large_ones() {
        let cache = cache_of(2, 1);
        cache.insert("u1", &record("Ania"));
        cache.insert("d1", &big(DEFAULT_LARGE_BYTES));
        cache.insert("d2", &big(DEFAULT_LARGE_BYTES));

        assert_eq!(cache.len(), (1, 1));
        assert_eq!(cache.get("d1"), None);
        assert_eq!(cache.get("d2"), Some(big(DEFAULT_LARGE_BYTES)));
        assert_eq!(cache.get("u1"), Some(record("Ania")));
    }

    #[test]
    fn a_document_one_byte_under_the_line_is_still_a_small_one() {
        let cache = cache_of(2, 1);
        cache.insert("d1", &big(DEFAULT_LARGE_BYTES - "body".len() - 1));
        assert_eq!(cache.len(), (1, 0));
    }

    #[test]
    fn clear_empties_both_tiers() {
        let cache = cache_of(2, 1);
        cache.insert("u1", &record("Ania"));
        cache.insert("d1", &big(DEFAULT_LARGE_BYTES));
        cache.clear();
        assert_eq!(cache.len(), (0, 0));
        assert_eq!(cache.get("u1"), None);
        assert_eq!(cache.get("d1"), None);
    }

    #[test]
    fn invalidating_a_key_nobody_cached_changes_nothing() {
        let cache = cache_of(2, 1);
        cache.insert("u1", &record("Ania"));
        cache.invalidate("u404");
        assert_eq!(cache.len(), (1, 0));
        assert_eq!(cache.get("u1"), Some(record("Ania")));
    }

    #[test]
    fn a_tier_keeps_its_capacity_across_eviction_and_reinsertion() {
        let cache = cache_of(2, 1);
        for round in 0..10 {
            cache.insert(&format!("u{round}"), &record("Ania"));
            assert!(cache.len().0 <= 2);
        }
        assert_eq!(cache.len(), (2, 0));
        assert_eq!(cache.get("u9"), Some(record("Ania")));
        assert_eq!(cache.get("u8"), Some(record("Ania")));
        assert_eq!(cache.get("u7"), None);
    }

    #[test]
    fn approx_bytes_counts_strings_for_real_and_nested_values_too() {
        let mut record = HaysonRecord::new();
        record.insert("id".to_string(), json!("u1"));
        record.insert("tags".to_string(), json!(["aaa", "bbb"]));
        record.insert("meta".to_string(), json!({ "note": "hello" }));
        assert_eq!(approx_bytes(&record), 2 + 2 + 4 + (3 + 3 + 8) + 4 + (4 + 5 + 8));
    }
}