slatedb 0.10.0

A cloud native embedded storage engine built on object storage.
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
use std::{
    cmp::{max, min, Ordering},
    hash::{Hash, Hasher},
    ops::{Bound, RangeBounds},
};

use serde::{ser::SerializeStruct, Serialize, Serializer};

#[derive(Debug, Eq)]
pub(crate) struct StartBound<T: Ord> {
    inner: Bound<T>,
}

impl<T: Ord + Clone> Clone for StartBound<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<T: Ord + Clone> StartBound<&T> {
    pub(crate) fn cloned(&self) -> StartBound<T> {
        StartBound {
            inner: self.inner.cloned(),
        }
    }
}

impl<T: Ord + Serialize> Serialize for StartBound<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.inner.serialize(serializer)
    }
}

impl<T: Ord + Hash> Hash for StartBound<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.inner.hash(state)
    }
}

impl<T: Ord> From<Bound<T>> for StartBound<T> {
    fn from(bound: Bound<T>) -> Self {
        Self { inner: bound }
    }
}

impl<T: Ord> From<StartBound<T>> for Bound<T> {
    fn from(bound: StartBound<T>) -> Self {
        bound.inner
    }
}

impl<T: Ord> PartialEq for StartBound<T> {
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

impl<T: Ord> PartialOrd for StartBound<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: Ord> Ord for StartBound<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        cmp_bound(&self.inner, &other.inner, true)
    }
}

#[derive(Debug, Eq)]
pub(crate) struct EndBound<T: Ord> {
    inner: Bound<T>,
}

impl<T: Ord + Clone> Clone for EndBound<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<T: Ord + Clone> EndBound<&T> {
    pub(crate) fn cloned(&self) -> EndBound<T> {
        EndBound {
            inner: self.inner.cloned(),
        }
    }
}

impl<T: Ord + Serialize> Serialize for EndBound<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.inner.serialize(serializer)
    }
}

impl<T: Ord + Hash> Hash for EndBound<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.inner.hash(state)
    }
}

impl<T: Ord> From<Bound<T>> for EndBound<T> {
    fn from(bound: Bound<T>) -> Self {
        Self { inner: bound }
    }
}

impl<T: Ord> From<EndBound<T>> for Bound<T> {
    fn from(bound: EndBound<T>) -> Self {
        bound.inner
    }
}

impl<T: Ord> PartialEq for EndBound<T> {
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

impl<T: Ord> PartialOrd for EndBound<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: Ord> Ord for EndBound<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        cmp_bound(&self.inner, &other.inner, false)
    }
}

fn cmp_bound<T: Ord>(a: &Bound<T>, b: &Bound<T>, start: bool) -> Ordering {
    match (a, b) {
        (Bound::Included(a), Bound::Included(b)) | (Bound::Excluded(a), Bound::Excluded(b)) => {
            a.cmp(b)
        }
        (Bound::Included(a), Bound::Excluded(b)) => match a.cmp(b) {
            Ordering::Equal => {
                if start {
                    Ordering::Less
                } else {
                    Ordering::Greater
                }
            }
            other => other,
        },
        (Bound::Excluded(a), Bound::Included(b)) => match a.cmp(b) {
            Ordering::Equal => {
                if start {
                    Ordering::Greater
                } else {
                    Ordering::Less
                }
            }
            other => other,
        },
        (Bound::Unbounded, Bound::Unbounded) => Ordering::Equal,
        (Bound::Unbounded, _) => {
            if start {
                Ordering::Less
            } else {
                Ordering::Greater
            }
        }
        (_, Bound::Unbounded) => {
            if start {
                Ordering::Greater
            } else {
                Ordering::Less
            }
        }
    }
}

#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
pub(crate) struct ComparableRange<T: Ord> {
    start: StartBound<T>,
    end: EndBound<T>,
}

impl<T: Ord> ComparableRange<T> {
    pub(crate) fn new(start: Bound<T>, end: Bound<T>) -> Self {
        Self {
            start: StartBound::from(start),
            end: EndBound::from(end),
        }
    }
}

impl<T: Ord + Serialize> Serialize for ComparableRange<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_struct("ComparableRange", 2)?;
        seq.serialize_field("start", &self.start)?;
        seq.serialize_field("end", &self.end)?;
        seq.end()
    }
}

impl<T: Ord + Hash> Hash for ComparableRange<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.start.hash(state);
        self.end.hash(state);
    }
}

impl<T: Ord + Clone> ComparableRange<T> {
    #[cfg(test)]
    pub(crate) fn from_range<R: RangeBounds<T>>(range: R) -> Self {
        Self::new(range.start_bound().cloned(), range.end_bound().cloned())
    }

    pub(crate) fn intersect(&self, other: &Self) -> Option<Self> {
        let max_start = max(&self.start, &other.start);
        let min_end = min(&self.end, &other.end);
        let intersection = Self {
            start: max_start.clone(),
            end: min_end.clone(),
        };
        if intersection.non_empty() {
            Some(intersection)
        } else {
            None
        }
    }

    #[allow(dead_code)]
    pub(crate) fn union(&self, other: &Self) -> Option<Self> {
        // Sort the ranges to make the function commutative
        let (first, second) = if self < other {
            (self, other)
        } else {
            (other, self)
        };
        // If the ranges are not intersecting and they are not adjacent, no union is possible
        if first.intersect(second).is_none() && !first.are_adjacent(second) {
            return None;
        }
        // Take the minimum of start bounds and maximum of end bounds
        Some(Self {
            start: min(&first.start, &other.start).clone(),
            end: max(&first.end, &second.end).clone(),
        })
    }

    fn are_adjacent(&self, other: &Self) -> bool {
        match (&self.end.inner, &other.start.inner) {
            (Bound::Included(a), Bound::Excluded(b)) => a == b,
            (Bound::Excluded(a), Bound::Included(b)) => a == b,
            _ => false,
        }
    }

    pub(crate) fn non_empty(&self) -> bool {
        match (&self.start.inner, &self.end.inner) {
            (Bound::Included(a), Bound::Included(b)) => a <= b,
            (Bound::Included(a), Bound::Excluded(b)) => a < b,
            (Bound::Excluded(a), Bound::Excluded(b)) => a < b,
            (Bound::Excluded(a), Bound::Included(b)) => a < b,
            (Bound::Unbounded, _) => true,
            (_, Bound::Unbounded) => true,
        }
    }

    pub(crate) fn comparable_start_bound(&self) -> StartBound<&T> {
        StartBound {
            inner: self.start_bound(),
        }
    }

    pub(crate) fn comparable_end_bound(&self) -> EndBound<&T> {
        EndBound {
            inner: self.end_bound(),
        }
    }
}

impl<T: Ord + Clone> Clone for ComparableRange<T> {
    fn clone(&self) -> Self {
        Self::new(self.start.inner.clone(), self.end.inner.clone())
    }
}

impl<T: Ord> RangeBounds<T> for ComparableRange<T> {
    fn start_bound(&self) -> Bound<&T> {
        self.start.inner.as_ref()
    }

    fn end_bound(&self) -> Bound<&T> {
        self.end.inner.as_ref()
    }
}

#[cfg(test)]
pub(crate) mod tests {

    use std::{
        cmp::Ordering,
        ops::{Bound, RangeBounds},
    };

    use rand::seq::SliceRandom;
    use rstest::rstest;

    use crate::comparable_range::{ComparableRange, EndBound, StartBound};

    struct TestCase(Bound<u32>, Bound<u32>, Ordering);

    #[rstest]
    #[case(TestCase(Bound::Included(1), Bound::Included(1), Ordering::Equal))]
    // [1, 100) vs. (1, 100) => (1 <= n < 100) vs. (1 < n < 100)
    #[case(TestCase(Bound::Included(1), Bound::Excluded(1), Ordering::Less))]
    // (1, 100) vs. [1, 100) =>  (1 < n < 100) vs. (1 <= n < 100)
    #[case(TestCase(Bound::Excluded(1), Bound::Included(1), Ordering::Greater))]
    // For start bound, unbounded represents -Inf
    #[case(TestCase(Bound::Unbounded, Bound::Included(1), Ordering::Less))]
    #[case(TestCase(Bound::Unbounded, Bound::Excluded(1), Ordering::Less))]
    #[case(TestCase(Bound::Included(1), Bound::Unbounded, Ordering::Greater))]
    #[case(TestCase(Bound::Excluded(1), Bound::Unbounded, Ordering::Greater))]
    fn test_start_bound_cmp(#[case] test_case: TestCase) {
        let lhs = StartBound::from(test_case.0);
        let rhs = StartBound::from(test_case.1);
        assert_eq!(lhs.cmp(&rhs), test_case.2);
    }

    #[rstest]
    #[case(TestCase(Bound::Included(100), Bound::Included(100), Ordering::Equal))]
    // (1, 100] vs. (1, 100) => (1 < n <= 100) vs. (1 < n < 100)
    #[case(TestCase(Bound::Included(100), Bound::Excluded(100), Ordering::Greater))]
    // (1, 100) vs. (1, 100] =>  (1 < n < 100) vs. (1 < n <= 100)
    #[case(TestCase(Bound::Excluded(100), Bound::Included(100), Ordering::Less))]
    // For end bound, unbounded represents +Inf
    #[case(TestCase(Bound::Unbounded, Bound::Included(1), Ordering::Greater))]
    #[case(TestCase(Bound::Unbounded, Bound::Excluded(1), Ordering::Greater))]
    #[case(TestCase(Bound::Included(1), Bound::Unbounded, Ordering::Less))]
    #[case(TestCase(Bound::Excluded(1), Bound::Unbounded, Ordering::Less))]
    fn test_end_bound_cmp(#[case] test_case: TestCase) {
        let lhs = EndBound::from(test_case.0);
        let rhs = EndBound::from(test_case.1);
        assert_eq!(lhs.cmp(&rhs), test_case.2);
    }

    #[test]
    fn test_range() {
        let ranges = vec![
            ComparableRange::from_range(..10),
            ComparableRange::from_range(..1000),
            ComparableRange::from_range(..),
            ComparableRange::from_range(1..5),
            ComparableRange::from_range(1..10),
            ComparableRange::from_range(1..),
            ComparableRange::from_range(2..3),
            ComparableRange::from_range(2..),
            ComparableRange::from_range(100..123),
        ];
        let mut shuffled_ranges = ranges.clone();
        // Shuffle the ranges to ensure the order is random
        shuffled_ranges.shuffle(&mut rand::rng());
        // Sort the ranges to ensure the order is deterministic
        shuffled_ranges.sort();

        assert_eq!(shuffled_ranges, ranges);
    }

    struct TwoRangeOperation<T: Ord + Clone> {
        first: ComparableRange<T>,
        second: ComparableRange<T>,
        result: Option<ComparableRange<T>>,
    }

    impl<T: Ord + Clone> TwoRangeOperation<T> {
        fn some<R1, R2, RI>(first: R1, second: R2, intersection: RI) -> Self
        where
            R1: RangeBounds<T>,
            R2: RangeBounds<T>,
            RI: RangeBounds<T>,
        {
            Self {
                first: ComparableRange::from_range(first),
                second: ComparableRange::from_range(second),
                result: Some(ComparableRange::from_range(intersection)),
            }
        }

        fn none<R1, R2>(first: R1, second: R2) -> Self
        where
            R1: RangeBounds<T>,
            R2: RangeBounds<T>,
        {
            Self {
                first: ComparableRange::from_range(first),
                second: ComparableRange::from_range(second),
                result: None,
            }
        }
    }

    #[rstest]
    #[case(TwoRangeOperation::some(0..10, 0..10, 0..10))]
    #[case(TwoRangeOperation::some(0..10, 1..10, 1..10))]
    #[case(TwoRangeOperation::some(0..10, 0..9, 0..9))]
    #[case(TwoRangeOperation::some(0..10, 0..=9, 0..=9))]
    #[case(TwoRangeOperation::some(..=1337, 10..15, 10..15))]
    #[allow(clippy::reversed_empty_ranges)]
    #[case(TwoRangeOperation::none(50..40, 10..60))]
    fn test_intersection(#[case] test_case: TwoRangeOperation<u32>) {
        for (first, second) in [
            (&test_case.first, &test_case.second),
            (&test_case.second, &test_case.first),
        ] {
            let intersection = first.intersect(second);
            assert_eq!(intersection, test_case.result);
        }
    }

    #[rstest]
    #[case(TwoRangeOperation::some(0..10, 10..100, 0..100))]
    #[case(TwoRangeOperation::some(0..=10, 10..100, 0..100))]
    #[case(TwoRangeOperation::none(0..10, 11..100))]
    #[case(TwoRangeOperation::some(..100, 5..=100, ..=100))]
    #[case(TwoRangeOperation::some(..100, 5.., ..))]
    #[case(TwoRangeOperation::some(0..=10, (Bound::Excluded(10), Bound::Included(100)), 0..=100))]
    #[allow(clippy::reversed_empty_ranges)]
    #[case::empty_range(TwoRangeOperation::none(5..0, 0..10))]
    #[case::empty_range(TwoRangeOperation::none((Bound::Excluded(5), Bound::Excluded(5)), 0..10))]
    fn test_union(#[case] test_case: TwoRangeOperation<u32>) {
        for (first, second) in [
            (&test_case.first, &test_case.second),
            (&test_case.second, &test_case.first),
        ] {
            let union = first.union(second);
            assert_eq!(union, test_case.result);
        }
    }

    #[test]
    fn test_is_non_empty() {
        struct TestCase(Bound<i32>, Bound<i32>, bool);
        let cases = vec![
            TestCase(Bound::Included(1), Bound::Included(1), true),
            TestCase(Bound::Included(1), Bound::Excluded(1), false),
            TestCase(Bound::Excluded(1), Bound::Included(1), false),
            TestCase(Bound::Excluded(1), Bound::Excluded(1), false),
            TestCase(Bound::Excluded(1), Bound::Excluded(2), true),
            TestCase(Bound::Excluded(2), Bound::Excluded(1), false),
            TestCase(Bound::Unbounded, Bound::Included(1), true),
            TestCase(Bound::Unbounded, Bound::Excluded(1), true),
            TestCase(Bound::Included(1), Bound::Unbounded, true),
            TestCase(Bound::Excluded(1), Bound::Unbounded, true),
            TestCase(Bound::Unbounded, Bound::Unbounded, true),
        ];
        for case in cases {
            assert_eq!(ComparableRange::new(case.0, case.1).non_empty(), case.2);
        }
    }
}