selene-db-graph 1.2.0

In-memory property-graph storage core (ArcSwap + imbl CoW, label/typed indexes, write funnel) for selene-db.
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
//! Chunked column vector primitive per spec 03 section 3.2.
//!
//! Values are grouped into 2048-element chunks. Frozen chunks live as
//! `Arc<[T]>` and share cheaply across snapshots; the currently-filling chunk
//! (the tail) is held as an `Arc<Vec<T>>` so that cloning the column shares
//! the tail by refcount bump instead of deep-copying its elements. Mutations
//! use `Arc::make_mut` throughout: a write clones only the affected frozen
//! chunk or the tail, and only when a snapshot still holds it, so a clone
//! taken before a mutation never observes the mutation (B1 tail COW).

use std::marker::PhantomData;
use std::sync::Arc;

/// Number of column entries per chunk.
pub const CHUNK_SIZE: usize = 2048;

/// Column storage split into independently clone-on-write chunks.
#[derive(Clone, Debug)]
pub struct ChunkedVec<T> {
    chunks: Vec<Arc<[T]>>,
    tail: Arc<Vec<T>>,
    len: usize,
    _marker: PhantomData<T>,
}

impl<T: Clone> ChunkedVec<T> {
    /// Construct an empty column.
    #[must_use]
    pub fn new() -> Self {
        Self {
            chunks: Vec::new(),
            tail: Arc::new(Vec::new()),
            len: 0,
            _marker: PhantomData,
        }
    }

    /// Construct an empty column with enough chunk slots for `capacity`.
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            chunks: Vec::with_capacity(capacity.div_ceil(CHUNK_SIZE)),
            tail: Arc::new(Vec::new()),
            len: 0,
            _marker: PhantomData,
        }
    }

    /// Number of entries in the column.
    #[must_use]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Return true when the column contains no entries.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Return the value at `index`, if it exists.
    #[must_use]
    pub fn get(&self, index: usize) -> Option<&T> {
        if index >= self.len {
            return None;
        }
        let (chunk_index, offset) = locate(index);
        if chunk_index < self.chunks.len() {
            self.chunks[chunk_index].get(offset)
        } else {
            self.tail.get(offset)
        }
    }

    /// Append `value` to the column in amortized O(1) time.
    ///
    /// Pushes go into the tail buffer through `Arc::make_mut`: while the tail
    /// Arc is unique this is a plain `Vec::push`; the first push after a clone
    /// pays one tail clone (≤ `CHUNK_SIZE - 1` elements) and subsequent pushes
    /// reuse the now-unique buffer. When the tail reaches `CHUNK_SIZE` it
    /// freezes into an `Arc<[T]>` immediately and a fresh tail starts on the
    /// next push. Cloning the column shares frozen chunks *and* the tail via
    /// Arc refcount bumps — no element is copied at clone time.
    pub fn push(&mut self, value: T) {
        let tail = Arc::make_mut(&mut self.tail);
        if tail.capacity() == 0 {
            tail.reserve(CHUNK_SIZE);
        }
        tail.push(value);
        self.len += 1;
        if tail.len() == CHUNK_SIZE {
            let frozen = std::mem::take(tail);
            self.chunks.push(Arc::from(frozen));
        }
    }

    /// Replace the value at `index`.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    pub fn set(&mut self, index: usize, value: T) {
        assert!(
            index < self.len,
            "ChunkedVec::set index {index} out of bounds for len {}",
            self.len
        );
        let (chunk_index, offset) = locate(index);
        if chunk_index < self.chunks.len() {
            let chunk = Arc::make_mut(&mut self.chunks[chunk_index]);
            chunk[offset] = value;
        } else {
            Arc::make_mut(&mut self.tail)[offset] = value;
        }
    }

    /// Iterate all values in insertion order.
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.chunks
            .iter()
            .flat_map(|chunk| chunk.iter())
            .chain(self.tail.iter())
    }

    #[cfg(test)]
    pub(crate) fn chunk_count(&self) -> usize {
        self.chunks.len() + if self.tail.is_empty() { 0 } else { 1 }
    }

    #[cfg(test)]
    pub(crate) fn chunk_capacity(&self) -> usize {
        self.chunks.capacity() + 1
    }

    #[cfg(test)]
    pub(crate) fn chunk_arc(&self, index: usize) -> Arc<[T]> {
        if index < self.chunks.len() {
            Arc::clone(&self.chunks[index])
        } else {
            Arc::from(self.tail.as_slice())
        }
    }

    #[cfg(test)]
    pub(crate) fn slow_equals(&self, other: &Self) -> bool
    where
        T: PartialEq,
    {
        self.iter().eq(other.iter())
    }
}

impl<T: Clone> Default for ChunkedVec<T> {
    fn default() -> Self {
        Self::new()
    }
}

fn locate(index: usize) -> (usize, usize) {
    (index / CHUNK_SIZE, index % CHUNK_SIZE)
}

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

    use super::*;

    #[test]
    fn new_is_empty() {
        let vec = ChunkedVec::<u64>::new();
        assert_eq!(vec.len(), 0);
        assert!(vec.is_empty());
        assert_eq!(vec.chunk_count(), 0);
    }

    #[test]
    fn push_grows_and_get_returns_pushed_values() {
        let mut vec = ChunkedVec::new();
        for value in 0..5000 {
            vec.push(value);
        }
        assert_eq!(vec.len(), 5000);
        for value in 0..5000 {
            assert_eq!(vec.get(value), Some(&value));
        }
        // 5000 = 2 frozen full chunks (4096) + 904 in tail.
        assert_eq!(vec.chunk_count(), 3);
    }

    #[test]
    fn push_does_not_clone_tail_per_call() {
        // Pushing into a non-full tail must NOT touch frozen chunks' Arcs.
        let mut vec = ChunkedVec::new();
        for value in 0..CHUNK_SIZE {
            vec.push(value);
        }
        // First chunk just froze; tail is empty.
        assert_eq!(vec.chunks.len(), 1);
        let first_chunk_handle = Arc::clone(&vec.chunks[0]);
        // Push 100 more into the (now-fresh) tail; the frozen chunk's Arc
        // strong count must stay 2 (vec.chunks[0] + first_chunk_handle).
        for value in 0..100 {
            vec.push(CHUNK_SIZE + value);
        }
        assert_eq!(Arc::strong_count(&first_chunk_handle), 2);
        assert_eq!(vec.len(), CHUNK_SIZE + 100);
    }

    #[test]
    fn set_replaces_value_at_index() {
        let mut vec = ChunkedVec::new();
        vec.push(1);
        vec.push(2);
        vec.set(1, 9);
        assert_eq!(vec.get(1), Some(&9));
    }

    #[test]
    fn set_only_clones_affected_chunk() {
        let mut original = ChunkedVec::new();
        for value in 0..4096 {
            original.push(value);
        }
        // Two full frozen chunks, empty tail.
        let mut cloned = original.clone();
        assert!(original.slow_equals(&cloned));
        let original_chunk_0 = original.chunk_arc(0);
        let original_chunk_1 = original.chunk_arc(1);
        cloned.set(CHUNK_SIZE + 1, 99);
        assert!(!original.slow_equals(&cloned));
        assert_eq!(Arc::strong_count(&original_chunk_0), 3);
        assert_eq!(Arc::strong_count(&original_chunk_1), 2);
        assert_eq!(original.get(CHUNK_SIZE + 1), Some(&(CHUNK_SIZE + 1)));
        assert_eq!(cloned.get(CHUNK_SIZE + 1), Some(&99));
    }

    #[test]
    fn set_in_tail_does_not_touch_frozen_chunks() {
        let mut vec = ChunkedVec::new();
        for value in 0..CHUNK_SIZE + 50 {
            vec.push(value);
        }
        let frozen_handle = Arc::clone(&vec.chunks[0]);
        vec.set(CHUNK_SIZE + 10, 999);
        // The set targeted the tail; frozen chunk's strong count stays 2.
        assert_eq!(Arc::strong_count(&frozen_handle), 2);
        assert_eq!(vec.get(CHUNK_SIZE + 10), Some(&999));
    }

    #[test]
    fn iter_yields_all_values_in_order() {
        let mut vec = ChunkedVec::new();
        for value in 0..4096 {
            vec.push(value);
        }
        assert_eq!(
            vec.iter().copied().collect::<Vec<_>>(),
            (0..4096).collect::<Vec<_>>()
        );
    }

    #[test]
    fn iter_includes_tail() {
        let mut vec = ChunkedVec::new();
        for value in 0..CHUNK_SIZE + 5 {
            vec.push(value);
        }
        assert_eq!(
            vec.iter().copied().collect::<Vec<_>>(),
            (0..CHUNK_SIZE + 5).collect::<Vec<_>>()
        );
    }

    #[test]
    fn with_capacity_does_not_overallocate_entries() {
        let vec = ChunkedVec::<u64>::with_capacity(CHUNK_SIZE * 3 + 1);
        assert_eq!(vec.len(), 0);
        assert_eq!(vec.chunk_count(), 0);
        assert!(vec.chunk_capacity() >= 4);
    }

    #[test]
    fn get_out_of_bounds_returns_none() {
        let mut vec = ChunkedVec::new();
        vec.push(1);
        assert_eq!(vec.get(1), None);
    }

    #[test]
    #[should_panic(expected = "ChunkedVec::set index 1 out of bounds for len 1")]
    fn set_out_of_bounds_panics_clearly() {
        let mut vec = ChunkedVec::new();
        vec.push(1);
        vec.set(1, 2);
    }

    #[test]
    fn clone_shares_tail_without_copying() {
        // B1: cloning the column must share the tail allocation by refcount
        // bump — read-only clones never duplicate tail elements.
        let mut original = ChunkedVec::new();
        for value in 0..100 {
            original.push(value);
        }
        let cloned = original.clone();
        assert!(Arc::ptr_eq(&original.tail, &cloned.tail));
        assert_eq!(Arc::strong_count(&original.tail), 2);
        assert!(original.slow_equals(&cloned));
    }

    #[test]
    fn clone_then_push_isolates_original_snapshot() {
        // B1: a push on the clone must COW the tail; the original (the
        // "snapshot") keeps its pre-mutation contents and its own tail Arc.
        let mut original = ChunkedVec::new();
        for value in 0..10 {
            original.push(value);
        }
        let snapshot_tail = Arc::clone(&original.tail);
        let mut cloned = original.clone();
        cloned.push(999);
        // Original snapshot unchanged.
        assert_eq!(original.len(), 10);
        assert_eq!(original.get(10), None);
        for value in 0..10 {
            assert_eq!(original.get(value), Some(&value));
        }
        // Clone diverged onto its own tail allocation.
        assert_eq!(cloned.get(10), Some(&999));
        assert!(!Arc::ptr_eq(&original.tail, &cloned.tail));
        // snapshot_tail + original.tail — the clone no longer shares it.
        assert_eq!(Arc::strong_count(&snapshot_tail), 2);
    }

    #[test]
    fn clone_then_set_in_tail_isolates_original_snapshot() {
        let mut original = ChunkedVec::new();
        for value in 0..10 {
            original.push(value);
        }
        let mut cloned = original.clone();
        cloned.set(3, 777);
        assert_eq!(original.get(3), Some(&3));
        assert_eq!(cloned.get(3), Some(&777));
        assert!(!Arc::ptr_eq(&original.tail, &cloned.tail));
    }

    #[test]
    fn first_write_makes_tail_unique_then_reuses_buffer() {
        // The first push after a clone pays the one tail COW; subsequent
        // pushes mutate the now-unique buffer in place (no further clones).
        let mut original = ChunkedVec::new();
        for value in 0..10 {
            original.push(value);
        }
        let mut cloned = original.clone();
        cloned.push(100);
        // Compare raw Arc allocation pointers — holding a probe Arc clone
        // would itself force `make_mut` to clone on the next write.
        let unique_tail_ptr = Arc::as_ptr(&cloned.tail);
        cloned.push(101);
        cloned.set(0, 42);
        // Still the same (unique) allocation: no further COW clones happened.
        assert_eq!(Arc::as_ptr(&cloned.tail), unique_tail_ptr);
        assert_eq!(Arc::strong_count(&cloned.tail), 1);
        assert_eq!(cloned.get(0), Some(&42));
        assert_eq!(original.get(0), Some(&0));
    }

    #[test]
    fn freeze_under_share_preserves_both_columns() {
        // Fill a clone up to the CHUNK_SIZE freeze boundary while the original
        // still holds the pre-freeze tail Arc. The freeze must COW first, so
        // the original keeps its short tail and the clone freezes correctly.
        let mut original = ChunkedVec::new();
        for value in 0..CHUNK_SIZE - 1 {
            original.push(value);
        }
        let mut cloned = original.clone();
        cloned.push(CHUNK_SIZE - 1); // triggers freeze in the clone
        for value in CHUNK_SIZE..CHUNK_SIZE + 5 {
            cloned.push(value);
        }
        // Original: one (unfrozen) tail of CHUNK_SIZE - 1 entries.
        assert_eq!(original.len(), CHUNK_SIZE - 1);
        assert_eq!(original.chunks.len(), 0);
        assert_eq!(original.tail.len(), CHUNK_SIZE - 1);
        for value in 0..CHUNK_SIZE - 1 {
            assert_eq!(original.get(value), Some(&value));
        }
        // Clone: one frozen chunk plus a 5-entry fresh tail.
        assert_eq!(cloned.len(), CHUNK_SIZE + 5);
        assert_eq!(cloned.chunks.len(), 1);
        assert_eq!(cloned.tail.len(), 5);
        for value in 0..CHUNK_SIZE + 5 {
            assert_eq!(cloned.get(value), Some(&value));
        }
    }

    proptest! {
        #[test]
        fn random_push_set_sequence_preserves_latest_values(ops in proptest::collection::vec((any::<bool>(), 0_usize..128, any::<u16>()), 1..256)) {
            let mut vec = ChunkedVec::new();
            let mut expected = Vec::new();
            for (set_existing, index, value) in ops {
                if set_existing && !expected.is_empty() {
                    let idx = index % expected.len();
                    vec.set(idx, value);
                    expected[idx] = value;
                } else {
                    vec.push(value);
                    expected.push(value);
                }
                prop_assert_eq!(vec.len(), expected.len());
                for (idx, expected_value) in expected.iter().enumerate() {
                    prop_assert_eq!(vec.get(idx), Some(expected_value));
                }
            }
        }

        #[test]
        fn clone_then_mutate_never_disturbs_snapshot(
            seed_ops in proptest::collection::vec((any::<bool>(), 0_usize..4096, any::<u16>()), 1..512),
            post_ops in proptest::collection::vec((any::<bool>(), 0_usize..4096, any::<u16>()), 1..512),
        ) {
            // B1 snapshot isolation: a clone taken at an arbitrary fill point
            // (including straddling chunk freezes) must never observe pushes
            // or sets applied to the live column afterwards.
            let mut live = ChunkedVec::new();
            let mut model = Vec::new();
            for (set_existing, index, value) in seed_ops {
                if set_existing && !model.is_empty() {
                    let idx = index % model.len();
                    live.set(idx, value);
                    model[idx] = value;
                } else {
                    live.push(value);
                    model.push(value);
                }
            }
            let snapshot = live.clone();
            let snapshot_model = model.clone();
            for (set_existing, index, value) in post_ops {
                if set_existing && !model.is_empty() {
                    let idx = index % model.len();
                    live.set(idx, value);
                    model[idx] = value;
                } else {
                    live.push(value);
                    model.push(value);
                }
            }
            // Snapshot still matches the pre-clone model exactly.
            prop_assert_eq!(snapshot.len(), snapshot_model.len());
            for (idx, expected_value) in snapshot_model.iter().enumerate() {
                prop_assert_eq!(snapshot.get(idx), Some(expected_value));
            }
            prop_assert_eq!(snapshot.get(snapshot_model.len()), None);
            // Live column matches the post-mutation model.
            prop_assert_eq!(live.len(), model.len());
            for (idx, expected_value) in model.iter().enumerate() {
                prop_assert_eq!(live.get(idx), Some(expected_value));
            }
        }
    }
}