lora-store 0.16.1

In-memory graph store with property indexes for LoraDB.
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
//! Copy-on-write collections for secondary indexes.
//!
//! Writes that cannot run in place work on a clone of the graph, so an
//! index clone has to be cheap, and a write should copy only the part of
//! an index it changes. Both types here split their contents into shards
//! behind `Arc`: cloning bumps one refcount per shard, and a mutation
//! copies just the shard it lands in (`Arc::make_mut`).
//!
//! [`CowMap`] and [`CowIdMap`] do not iterate in key order (the trigram,
//! point, hash-bucket and per-entity maps only need lookups, unions and
//! intersections); [`CowOrdMap`] does, for range and prefix scans.

use std::collections::{BTreeMap, HashMap};
use std::hash::{BuildHasherDefault, Hash, Hasher};
use std::sync::Arc;

/// FNV-1a: stable, fast, and good enough to spread short strings,
/// trigrams, index keys and grid cells. Used to pick a shard and inside
/// each shard's hash table.
#[derive(Default)]
struct Fnv(u64);

impl Hasher for Fnv {
    fn finish(&self) -> u64 {
        self.0
    }

    fn write(&mut self, bytes: &[u8]) {
        if self.0 == 0 {
            self.0 = 0xcbf2_9ce4_8422_2325;
        }
        for b in bytes {
            self.0 ^= u64::from(*b);
            self.0 = self.0.wrapping_mul(0x0100_0000_01b3);
        }
    }
}

type FnvHashMap<K, V> = HashMap<K, V, BuildHasherDefault<Fnv>>;

/// Shards per [`CowMap`].
const SHARDS: usize = 256;
/// Shards per large [`CowIdMap`].
const ID_SHARDS: usize = 64;
/// A [`CowIdMap`] splits into [`SHARDS`] shards once it holds more ids
/// than this; below it a single map is smaller and faster.
const SPLIT_AT: usize = 1024;

/// A hash map sharded by key hash, each shard a hash table behind `Arc`.
/// Empty maps allocate nothing.
pub(super) struct CowMap<K, V> {
    shards: Vec<Arc<FnvHashMap<K, V>>>,
    len: usize,
}

impl<K, V> Default for CowMap<K, V> {
    fn default() -> Self {
        Self {
            shards: Vec::new(),
            len: 0,
        }
    }
}

impl<K, V> Clone for CowMap<K, V> {
    fn clone(&self) -> Self {
        Self {
            shards: self.shards.clone(),
            len: self.len,
        }
    }
}

impl<K: std::fmt::Debug, V: std::fmt::Debug> std::fmt::Debug for CowMap<K, V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_map()
            .entries(self.shards.iter().flat_map(|shard| shard.iter()))
            .finish()
    }
}

impl<K: Eq + Hash, V: PartialEq> PartialEq for CowMap<K, V> {
    fn eq(&self, other: &Self) -> bool {
        self.len == other.len
            && self
                .shards
                .iter()
                .flat_map(|shard| shard.iter())
                .all(|(k, v)| {
                    other.shards.get(shard_of(k)).and_then(|shard| shard.get(k)) == Some(v)
                })
    }
}

fn shard_of<Q: Hash + ?Sized>(key: &Q) -> usize {
    let mut hasher = Fnv::default();
    key.hash(&mut hasher);
    (hasher.finish() as usize) % SHARDS
}

impl<K: Eq + Hash + Clone, V: Clone> CowMap<K, V> {
    pub(super) fn len(&self) -> usize {
        self.len
    }

    pub(super) fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub(super) fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: std::borrow::Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.shards.get(shard_of(key))?.get(key)
    }

    pub(super) fn contains_key<Q>(&self, key: &Q) -> bool
    where
        K: std::borrow::Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.get(key).is_some()
    }

    /// Mutable access to an existing entry; copies its shard if shared.
    pub(super) fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: std::borrow::Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        let shard = self.shards.get_mut(shard_of(key))?;
        if !shard.contains_key(key) {
            return None;
        }
        Arc::make_mut(shard).get_mut(key)
    }

    /// Mutable access to `key`'s value, inserting `make()` first if absent.
    pub(super) fn get_or_insert_with(&mut self, key: K, make: impl FnOnce() -> V) -> &mut V {
        if self.shards.is_empty() {
            self.shards = (0..SHARDS)
                .map(|_| Arc::new(FnvHashMap::default()))
                .collect();
        }
        let shard = Arc::make_mut(&mut self.shards[shard_of(&key)]);
        let len = &mut self.len;
        shard.entry(key).or_insert_with(|| {
            *len += 1;
            make()
        })
    }

    pub(super) fn insert(&mut self, key: K, value: V) -> Option<V> {
        let mut value = Some(value);
        let slot = self.get_or_insert_with(key, || value.take().expect("value unused"));
        value.map(|v| std::mem::replace(slot, v))
    }

    pub(super) fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: std::borrow::Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        let shard = self.shards.get_mut(shard_of(key))?;
        if !shard.contains_key(key) {
            return None;
        }
        let removed = Arc::make_mut(shard).remove(key);
        if removed.is_some() {
            self.len -= 1;
        }
        removed
    }

    /// Entries in no particular order.
    pub(super) fn iter(&self) -> impl Iterator<Item = (&K, &V)> + '_ {
        self.shards.iter().flat_map(|shard| shard.iter())
    }

    pub(super) fn values(&self) -> impl Iterator<Item = &V> + '_ {
        self.iter().map(|(_, v)| v)
    }
}

/// Entity ids with a value each (a posting list). Small lists are one
/// shared map; past [`SPLIT_AT`] ids they split into [`SHARDS`] shards by
/// id, so updating one entity in a posting shared by most of the graph
/// copies a few hundred entries, not all of them.
pub(super) struct CowIdMap<V> {
    /// The shard table is itself shared, so cloning a posting list (as
    /// happens for every entry of a copied [`CowMap`] shard) is one
    /// refcount bump rather than an allocation.
    shards: Arc<Vec<Arc<BTreeMap<u64, V>>>>,
    len: usize,
}

impl<V> Default for CowIdMap<V> {
    fn default() -> Self {
        Self {
            shards: Arc::new(Vec::new()),
            len: 0,
        }
    }
}

impl<V> Clone for CowIdMap<V> {
    fn clone(&self) -> Self {
        Self {
            shards: Arc::clone(&self.shards),
            len: self.len,
        }
    }
}

impl<V: std::fmt::Debug> std::fmt::Debug for CowIdMap<V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_map()
            .entries(self.shards.iter().flat_map(|shard| shard.iter()))
            .finish()
    }
}

impl<V: Clone> CowIdMap<V> {
    #[inline]
    fn shard(&self, id: u64) -> usize {
        (id % self.shards.len() as u64) as usize
    }

    pub(super) fn len(&self) -> usize {
        self.len
    }

    pub(super) fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub(super) fn get(&self, id: &u64) -> Option<&V> {
        if self.shards.is_empty() {
            return None;
        }
        self.shards[self.shard(*id)].get(id)
    }

    pub(super) fn contains_key(&self, id: &u64) -> bool {
        self.get(id).is_some()
    }

    pub(super) fn insert(&mut self, id: u64, value: V) -> Option<V> {
        if self.shards.is_empty() {
            Arc::make_mut(&mut self.shards).push(Arc::new(BTreeMap::new()));
        }
        let idx = self.shard(id);
        let table = Arc::make_mut(&mut self.shards);
        let previous = Arc::make_mut(&mut table[idx]).insert(id, value);
        if previous.is_none() {
            self.len += 1;
            if self.shards.len() == 1 && self.len > SPLIT_AT {
                self.split();
            }
        }
        previous
    }

    pub(super) fn remove(&mut self, id: &u64) -> Option<V> {
        if self.shards.is_empty() {
            return None;
        }
        let idx = self.shard(*id);
        if !self.shards[idx].contains_key(id) {
            return None;
        }
        let table = Arc::make_mut(&mut self.shards);
        let removed = Arc::make_mut(&mut table[idx]).remove(id);
        if removed.is_some() {
            self.len -= 1;
        }
        removed
    }

    /// Entries in no particular order.
    pub(super) fn iter(&self) -> impl Iterator<Item = (&u64, &V)> + '_ {
        self.shards.iter().flat_map(|shard| shard.iter())
    }

    pub(super) fn keys(&self) -> impl Iterator<Item = u64> + '_ {
        self.iter().map(|(id, _)| *id)
    }

    fn split(&mut self) {
        let single = Arc::make_mut(&mut self.shards).pop().expect("one shard");
        let mut shards: Vec<BTreeMap<u64, V>> = (0..ID_SHARDS).map(|_| BTreeMap::new()).collect();
        for (id, value) in Arc::unwrap_or_clone(single) {
            shards[(id % ID_SHARDS as u64) as usize].insert(id, value);
        }
        self.shards = Arc::new(shards.into_iter().map(Arc::new).collect());
    }

    /// Heap bytes estimate for the memory report.
    pub(super) fn heap_bytes(&self) -> usize {
        self.shards.len() * 40
            + self.len * (std::mem::size_of::<u64>() + std::mem::size_of::<V>() + 12)
    }
}

/// Largest partition of a [`CowOrdMap`] before it splits in two.
const PART_MAX: usize = 1024;

/// An ordered map split into key-range partitions behind `Arc`: a
/// two-level B-tree. Cloning bumps one refcount per partition (about one
/// per thousand entries), and a write copies only the partition holding
/// its key. Unlike [`CowMap`] it keeps key order, so range scans (the
/// sorted property index behind RANGE indexes and uniqueness constraints)
/// work in both directions.
pub(super) struct CowOrdMap<K, V> {
    /// Non-empty partitions, each covering a contiguous key range, in
    /// ascending order.
    parts: Arc<Vec<Arc<BTreeMap<K, V>>>>,
    len: usize,
}

impl<K, V> Default for CowOrdMap<K, V> {
    fn default() -> Self {
        Self {
            parts: Arc::new(Vec::new()),
            len: 0,
        }
    }
}

impl<K, V> Clone for CowOrdMap<K, V> {
    fn clone(&self) -> Self {
        Self {
            parts: Arc::clone(&self.parts),
            len: self.len,
        }
    }
}

impl<K: std::fmt::Debug, V: std::fmt::Debug> std::fmt::Debug for CowOrdMap<K, V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_map()
            .entries(self.parts.iter().flat_map(|part| part.iter()))
            .finish()
    }
}

impl<K: Ord + Clone, V: Clone> CowOrdMap<K, V> {
    #[cfg(test)]
    pub(super) fn len(&self) -> usize {
        self.len
    }

    /// Index of the partition that holds (or would hold) `key`.
    fn part_for<Q>(&self, key: &Q) -> usize
    where
        K: std::borrow::Borrow<Q>,
        Q: Ord + ?Sized,
    {
        // First partition whose smallest key is greater than `key`, minus
        // one: the last partition starting at or before `key`.
        let after = self.parts.partition_point(|part| {
            part.keys()
                .next()
                .is_some_and(|first| first.borrow() <= key)
        });
        after.saturating_sub(1)
    }

    pub(super) fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: std::borrow::Borrow<Q>,
        Q: Ord + ?Sized,
    {
        if self.parts.is_empty() {
            return None;
        }
        self.parts[self.part_for(key)].get(key)
    }

    pub(super) fn contains_key<Q>(&self, key: &Q) -> bool
    where
        K: std::borrow::Borrow<Q>,
        Q: Ord + ?Sized,
    {
        self.get(key).is_some()
    }

    /// Mutable access to an existing entry; copies its partition if shared.
    pub(super) fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: std::borrow::Borrow<Q>,
        Q: Ord + ?Sized,
    {
        if !self.contains_key(key) {
            return None;
        }
        let idx = self.part_for(key);
        let parts = Arc::make_mut(&mut self.parts);
        Arc::make_mut(&mut parts[idx]).get_mut(key)
    }

    /// Mutable access to `key`'s value, inserting `make()` first if absent.
    pub(super) fn get_or_insert_with(&mut self, key: K, make: impl FnOnce() -> V) -> &mut V {
        if !self.contains_key(&key) {
            self.insert_new(key.clone(), make());
        }
        self.get_mut(&key).expect("present after insert")
    }

    fn insert_new(&mut self, key: K, value: V) {
        let parts = Arc::make_mut(&mut self.parts);
        if parts.is_empty() {
            parts.push(Arc::new(BTreeMap::new()));
        }
        let idx = {
            let after =
                parts.partition_point(|part| part.keys().next().is_some_and(|first| *first <= key));
            after.saturating_sub(1)
        };
        let part = Arc::make_mut(&mut parts[idx]);
        part.insert(key, value);
        if part.len() > PART_MAX {
            let middle = part.keys().nth(part.len() / 2).cloned().expect("non-empty");
            let upper = part.split_off(&middle);
            parts.insert(idx + 1, Arc::new(upper));
        }
        self.len += 1;
    }

    pub(super) fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: std::borrow::Borrow<Q>,
        Q: Ord + ?Sized,
    {
        if !self.contains_key(key) {
            return None;
        }
        let idx = self.part_for(key);
        let parts = Arc::make_mut(&mut self.parts);
        let part = Arc::make_mut(&mut parts[idx]);
        let removed = part.remove(key);
        if part.is_empty() {
            parts.remove(idx);
        }
        self.len -= 1;
        removed
    }

    /// Entries with keys in `(lower, upper)`, in key order (reversible).
    /// An empty or inverted range (`x > 5 AND x < 5`) yields nothing:
    /// `BTreeMap::range` would panic on it, and a panic aborts the host.
    pub(super) fn range(
        &self,
        lower: std::ops::Bound<K>,
        upper: std::ops::Bound<K>,
    ) -> impl DoubleEndedIterator<Item = (&K, &V)> + '_ {
        use std::ops::Bound;
        let empty = match (&lower, &upper) {
            (Bound::Included(l), Bound::Included(u)) => l > u,
            (Bound::Included(l) | Bound::Excluded(l), Bound::Included(u) | Bound::Excluded(u)) => {
                l >= u
            }
            _ => false,
        };
        let first = match &lower {
            Bound::Included(k) | Bound::Excluded(k) => self.part_for(k),
            Bound::Unbounded => 0,
        };
        let last = match &upper {
            Bound::Included(k) | Bound::Excluded(k) => self.part_for(k),
            Bound::Unbounded => self.parts.len().saturating_sub(1),
        };
        let parts: &[Arc<BTreeMap<K, V>>] = if empty || self.parts.is_empty() || first > last {
            &[]
        } else {
            &self.parts[first..=last]
        };
        parts
            .iter()
            .flat_map(move |part| part.range((lower.clone(), upper.clone())))
    }

    pub(super) fn iter(&self) -> impl DoubleEndedIterator<Item = (&K, &V)> + '_ {
        self.parts.iter().flat_map(|part| part.iter())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ord_map_empty_and_inverted_ranges_yield_nothing() {
        use std::ops::Bound::{Excluded, Included, Unbounded};
        let mut m: CowOrdMap<i64, ()> = CowOrdMap::default();
        for i in 0..2000 {
            m.get_or_insert_with(i, || ());
        }
        for (lo, hi) in [
            (Excluded(5), Excluded(5)),
            (Included(5), Excluded(5)),
            (Excluded(5), Included(5)),
            (Included(9), Included(3)),
            (Excluded(1500), Excluded(10)),
        ] {
            assert_eq!(m.range(lo, hi).count(), 0);
            assert_eq!(m.range(lo, hi).rev().count(), 0);
        }
        assert_eq!(m.range(Included(5), Included(5)).count(), 1);
        assert_eq!(m.range(Unbounded, Excluded(0)).count(), 0);
    }

    #[test]
    fn cow_map_behaves_like_a_map_and_shares_on_clone() {
        let mut m: CowMap<String, u32> = CowMap::default();
        for i in 0..500u32 {
            assert!(m.insert(format!("k{i}"), i).is_none());
        }
        assert_eq!(m.insert("k7".into(), 70), Some(7));
        assert_eq!(m.len(), 500);
        let snapshot = m.clone();
        *m.get_mut("k1").unwrap() = 11;
        assert_eq!(m.remove("k2"), Some(2));
        assert_eq!(snapshot.get("k1"), Some(&1));
        assert_eq!(snapshot.get("k2"), Some(&2));
        assert_eq!(m.get("k1"), Some(&11));
        assert!(!m.contains_key("k2"));
        assert_eq!(m.len(), 499);
    }

    #[test]
    fn cow_ord_map_matches_btreemap_in_order_and_ranges() {
        use std::ops::Bound::{Excluded, Included, Unbounded};
        let mut ours: CowOrdMap<u64, u64> = CowOrdMap::default();
        let mut reference = BTreeMap::new();
        // Scrambled insert order exercises splits in the middle.
        for i in 0..5_000u64 {
            let k = (i * 7919) % 5_000;
            *ours.get_or_insert_with(k, || 0) += k;
            reference.insert(k, k);
        }
        let snapshot = ours.clone();
        for k in (0..5_000u64).step_by(3) {
            assert_eq!(ours.remove(&k), reference.remove(&k));
        }
        *ours.get_mut(&1).unwrap() = 99;
        reference.insert(1, 99);
        assert_eq!(ours.len(), reference.len());
        assert!(ours.iter().eq(reference.iter()));
        assert!(ours.iter().rev().eq(reference.iter().rev()));
        for (lo, hi) in [
            (Included(100), Excluded(2_000)),
            (Excluded(4_990), Unbounded),
            (Unbounded, Included(17)),
            (Included(2_500), Included(2_500)),
        ] {
            assert!(ours.range(lo, hi).eq(reference.range((lo, hi))));
            assert!(ours.range(lo, hi).rev().eq(reference.range((lo, hi)).rev()));
        }
        assert_eq!(snapshot.get(&3), Some(&3), "the clone is unaffected");
        assert_eq!(snapshot.len(), 5_000);
    }

    #[test]
    fn cow_id_map_splits_and_stays_correct() {
        let mut m: CowIdMap<u32> = CowIdMap::default();
        for id in 0..5_000u64 {
            m.insert(id, id as u32);
        }
        assert_eq!(m.len(), 5_000);
        let snapshot = m.clone();
        m.insert(42, 0);
        m.remove(&43);
        assert_eq!(snapshot.get(&42), Some(&42));
        assert_eq!(snapshot.get(&43), Some(&43));
        assert_eq!(m.get(&42), Some(&0));
        assert!(!m.contains_key(&43));
        assert_eq!(m.len(), 4_999);
        let mut ids: Vec<u64> = m.keys().collect();
        ids.sort();
        assert_eq!(ids.len(), 4_999);
        assert_eq!(ids[0], 0);
    }
}