dbsp 0.287.0

Continuous streaming analytics engine
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
use crate::algebra::{PartialOrder, TotalOrder};
use rkyv::{Archive, Deserialize, Serialize};
use size_of::SizeOf;
use std::{
    fmt::{self, Debug},
    hash::{Hash, Hasher},
    iter::{FromIterator, IntoIterator},
    ops::Deref,
    slice::Iter,
    vec::IntoIter,
};

/// A set of mutually incomparable elements.
///
/// An antichain is a set of partially ordered elements, each of which is
/// incomparable to the others. This antichain implementation allows you to
/// repeatedly introduce elements to the antichain, and which will evict larger
/// elements to maintain the *minimal* antichain, those incomparable elements no
/// greater than any other element.
///
/// Two antichains are equal if they contain the same set of elements, even if
/// in different orders. This can make equality testing quadratic, though linear
/// in the common case that the sequences are identical.
///
/// DBSP logical times are, in generally, only [partially ordered].  That means
/// that a collection of logical times may have multiple different lower bounds,
/// and an antichain is a way to track all of them.
///
/// [partially ordered]: crate::time#comparing-times
#[derive(Default, SizeOf, Archive, Serialize, Deserialize)]
pub struct Antichain<T>
where
    T: 'static,
{
    // TODO: We can specialize containers based on the inner type, meaning we could give ourselves
    //       a more favorable memory footprint for things like `Antichain<()>`
    elements: Vec<T>,
}

impl<T> Antichain<T> {
    /// Creates a new empty `Antichain`.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::<u32>::new();
    /// ```
    pub const fn new() -> Self {
        Self {
            elements: Vec::new(),
        }
    }

    /// Creates a new empty `Antichain` with space for `capacity` elements.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::<u32>::with_capacity(10);
    /// ```
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            elements: Vec::with_capacity(capacity),
        }
    }

    /// Creates a new singleton `Antichain`.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::from_elem(2);
    /// ```
    pub fn from_elem(element: T) -> Self {
        Self {
            elements: vec![element],
        }
    }

    /// Clears the contents of the antichain.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::from_elem(2);
    /// frontier.clear();
    /// assert!(frontier.is_empty());
    /// ```
    pub fn clear(&mut self) {
        self.elements.clear();
    }

    /// Sorts the elements so that comparisons between antichains can be made.
    pub fn sort(&mut self)
    where
        T: Ord,
    {
        self.elements.sort()
    }

    /// Reveals the elements in the antichain.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::from_elem(2);
    /// assert_eq!(frontier.as_slice(), &[2]);
    /// ```
    #[inline]
    pub fn as_slice(&self) -> &[T] {
        &self.elements
    }

    /// Reveals the elements in the antichain.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::from_elem(2);
    /// assert_eq!(&*frontier.as_ref(), &[2]);
    /// ```
    #[inline]
    pub fn as_ref(&self) -> AntichainRef<'_, T> {
        AntichainRef::new(&self.elements)
    }
}

impl<T> Antichain<T>
where
    T: PartialOrder,
{
    /// Updates the `Antichain` if the element is not greater than or equal to
    /// some present element.
    ///
    /// Returns `true` if the element was added to the set
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::new();
    /// assert!(frontier.insert(2));
    /// assert!(!frontier.insert(3));
    /// ```
    pub fn insert(&mut self, element: T) -> bool {
        if !self.elements.iter().any(|x| x.less_equal(&element)) {
            self.elements.retain(|x| !element.less_equal(x));
            self.elements.push(element);
            true
        } else {
            false
        }
    }

    /// Reserves capacity for at least `additional` more elements to be inserted
    /// in the given `Antichain`
    pub fn reserve(&mut self, additional: usize) {
        self.elements.reserve(additional);
    }

    /// Performs a sequence of insertion and return true if any insertions
    /// happen
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::new();
    /// assert!(frontier.extend(Some(3)));
    /// assert!(frontier.extend(vec![2, 5]));
    /// assert!(!frontier.extend(vec![3, 4]));
    /// ```
    pub fn extend<I>(&mut self, iterator: I) -> bool
    where
        I: IntoIterator<Item = T>,
    {
        let mut added = false;
        for element in iterator {
            added |= self.insert(element);
        }

        added
    }

    /// Returns true if any item in the antichain is strictly less than the
    /// argument.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::from_elem(2);
    /// assert!(frontier.less_than(&3));
    /// assert!(!frontier.less_than(&2));
    /// assert!(!frontier.less_than(&1));
    ///
    /// frontier.clear();
    /// assert!(!frontier.less_than(&3));
    /// ```
    #[inline]
    pub fn less_than(&self, time: &T) -> bool {
        self.elements.iter().any(|x| x.less_than(time))
    }

    /// Returns true if any item in the antichain is less than or equal to the
    /// argument.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::Antichain;
    ///
    /// let mut frontier = Antichain::from_elem(2);
    /// assert!(frontier.less_equal(&3));
    /// assert!(frontier.less_equal(&2));
    /// assert!(!frontier.less_equal(&1));
    ///
    /// frontier.clear();
    /// assert!(!frontier.less_equal(&3));
    /// ```
    #[inline]
    pub fn less_equal(&self, time: &T) -> bool {
        self.elements.iter().any(|x| x.less_equal(time))
    }
}

impl<T> Antichain<T>
where
    T: TotalOrder,
{
    /// Convert to the at most one element the antichain contains.
    pub fn into_option(mut self) -> Option<T> {
        debug_assert!(self.len() <= 1);
        self.elements.pop()
    }

    /// Return a reference to the at most one element the antichain contains.
    pub fn as_option(&self) -> Option<&T> {
        debug_assert!(self.len() <= 1);
        self.elements.last()
    }
}

impl<T> PartialEq for Antichain<T>
where
    T: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        // Lengths should be the same, with the option for fast acceptance if identical.
        self.len() == other.len()
            && (self.iter().zip(other.iter()).all(|(t1, t2)| t1 == t2)
                || self.iter().all(|t1| other.iter().any(|t2| t1 == t2)))
    }
}

impl<T> Eq for Antichain<T> where T: Eq {}

impl<T> PartialOrder for Antichain<T>
where
    T: PartialOrder,
{
    fn less_equal(&self, other: &Self) -> bool {
        other
            .iter()
            .all(|t2| self.iter().any(|t1| t1.less_equal(t2)))
    }
}

impl<T> Clone for Antichain<T>
where
    T: Clone,
{
    #[inline]
    fn clone(&self) -> Self {
        Self {
            elements: self.elements.clone(),
        }
    }

    #[inline]
    fn clone_from(&mut self, source: &Self) {
        self.elements.clone_from(&source.elements);
    }
}

impl<T: TotalOrder> TotalOrder for Antichain<T> {}

impl<T> Hash for Antichain<T>
where
    T: Ord + Hash,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        let mut temp = self.elements.iter().collect::<Vec<_>>();
        temp.sort();

        for element in temp {
            element.hash(state);
        }
    }
}

impl<T> From<Vec<T>> for Antichain<T>
where
    T: PartialOrder,
{
    fn from(vec: Vec<T>) -> Self {
        // TODO: We could reuse `vec` with some care.
        let mut temp = Antichain::new();
        for elem in vec.into_iter() {
            temp.insert(elem);
        }

        temp
    }
}

impl<T> From<Antichain<T>> for Vec<T> {
    #[inline]
    fn from(frontier: Antichain<T>) -> Self {
        frontier.elements
    }
}

impl<T> Deref for Antichain<T> {
    type Target = [T];

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.elements
    }
}

impl<T> FromIterator<T> for Antichain<T>
where
    T: PartialOrder,
{
    #[inline]
    fn from_iter<I>(iterator: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        let mut result = Self::new();
        result.extend(iterator);
        result
    }
}

impl<'a, T> IntoIterator for &'a Antichain<T> {
    type Item = &'a T;
    type IntoIter = Iter<'a, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.elements.iter()
    }
}

impl<T> IntoIterator for Antichain<T> {
    type Item = T;
    type IntoIter = IntoIter<T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.elements.into_iter()
    }
}

impl<T> Debug for Antichain<T>
where
    T: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_set().entries(&self.elements).finish()
    }
}

/// A wrapper for the elements of an [`Antichain`].
///
/// Typically obtained via [`Antichain::as_ref`].
#[derive(Default, SizeOf)]
pub struct AntichainRef<'a, T: 'a> {
    /// Elements contained within the antichain
    frontier: &'a [T],
}

impl<'a, T: 'a> AntichainRef<'a, T> {
    /// Create a new `AntichainRef` from a reference to a slice of elements
    /// forming the frontier.
    ///
    /// This method does not check that this antichain has any particular
    /// properties, for example that there are no elements strictly less
    /// than other elements.
    pub const fn new(frontier: &'a [T]) -> Self {
        Self { frontier }
    }

    /// Create an empty `AntichainRef` with zero elements forming the frontier
    pub const fn empty() -> Self {
        Self { frontier: &[] }
    }

    /// Constructs an owned antichain from the antichain reference.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::{Antichain, AntichainRef};
    ///
    /// let frontier = AntichainRef::new(&[1u64]);
    /// assert_eq!(frontier.to_owned(), Antichain::from_elem(1u64));
    /// ```
    pub fn to_owned(&self) -> Antichain<T>
    where
        T: Clone,
    {
        Antichain {
            elements: self.frontier.to_vec(),
        }
    }
}

impl<'a, T> AntichainRef<'a, T>
where
    T: PartialOrder + 'a,
{
    /// Returns true if any item in the `AntichainRef` is strictly less than the
    /// argument.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::AntichainRef;
    ///
    /// let frontier = AntichainRef::new(&[1u64]);
    /// assert!(!frontier.less_than(&0));
    /// assert!(!frontier.less_than(&1));
    /// assert!(frontier.less_than(&2));
    /// ```
    #[inline]
    pub fn less_than(&self, time: &T) -> bool {
        self.iter().any(|x| x.less_than(time))
    }

    /// Returns true if any item in the `AntichainRef` is less than or equal to
    /// the argument.
    ///
    /// # Examples
    ///
    ///```
    /// use dbsp::time::AntichainRef;
    ///
    /// let frontier = AntichainRef::new(&[1u64]);
    /// assert!(!frontier.less_equal(&0));
    /// assert!(frontier.less_equal(&1));
    /// assert!(frontier.less_equal(&2));
    /// ```
    #[inline]
    pub fn less_equal(&self, time: &T) -> bool {
        self.iter().any(|x| x.less_equal(time))
    }
}

impl<'a, T> AntichainRef<'a, T>
where
    T: TotalOrder + 'a,
{
    /// Return a reference to the at most one element the antichain contains
    #[inline]
    pub fn as_option(&self) -> Option<&T> {
        debug_assert!(self.len() <= 1);
        self.frontier.last()
    }
}

impl<'a, T: 'a> Clone for AntichainRef<'a, T> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}

impl<'a, T: 'a> Copy for AntichainRef<'a, T> {}

impl<'a, T> PartialEq for AntichainRef<'a, T>
where
    T: PartialEq + 'a,
{
    fn eq(&self, other: &Self) -> bool {
        // Lengths should be the same, with the option for fast acceptance if identical.
        self.len() == other.len()
            && (self.iter().zip(other.iter()).all(|(t1, t2)| t1 == t2)
                || self.iter().all(|t1| other.iter().any(|t2| t1 == t2)))
    }
}

impl<T> Eq for AntichainRef<'_, T> where T: Eq {}

impl<'a, T> PartialOrder for AntichainRef<'a, T>
where
    T: PartialOrder + 'a,
{
    fn less_equal(&self, other: &Self) -> bool {
        other
            .iter()
            .all(|t2| self.iter().any(|t1| t1.less_equal(t2)))
    }
}

impl<T> TotalOrder for AntichainRef<'_, T> where T: TotalOrder {}

impl<T> AsRef<[T]> for AntichainRef<'_, T> {
    #[inline]
    fn as_ref(&self) -> &[T] {
        self.frontier
    }
}

impl<T> Deref for AntichainRef<'_, T> {
    type Target = [T];

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.frontier
    }
}

impl<'a, T: 'a> IntoIterator for AntichainRef<'a, T> {
    type Item = &'a T;
    type IntoIter = Iter<'a, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.frontier.iter()
    }
}

impl<'a, T: 'a> IntoIterator for &'a AntichainRef<'a, T> {
    type Item = &'a T;
    type IntoIter = Iter<'a, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.frontier.iter()
    }
}

impl<'a, T: 'a> Debug for AntichainRef<'a, T>
where
    T: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_set().entries(self.frontier).finish()
    }
}

#[cfg(test)]
mod tests {
    use crate::{algebra::PartialOrder, time::Antichain};
    use std::collections::HashSet;

    #[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
    struct Elem(char, usize);

    impl PartialOrder for Elem {
        fn less_equal(&self, other: &Self) -> bool {
            self.0 <= other.0 && self.1 <= other.1
        }
    }

    #[test]
    fn antichain_hash() {
        let mut hashed = HashSet::new();
        hashed.insert(Antichain::from(vec![Elem('a', 2), Elem('b', 1)]));

        assert!(hashed.contains(&Antichain::from(vec![Elem('a', 2), Elem('b', 1)])));
        assert!(hashed.contains(&Antichain::from(vec![Elem('b', 1), Elem('a', 2)])));

        assert!(!hashed.contains(&Antichain::from(vec![Elem('a', 2)])));
        assert!(!hashed.contains(&Antichain::from(vec![Elem('a', 1)])));
        assert!(!hashed.contains(&Antichain::from(vec![Elem('b', 2)])));
        assert!(!hashed.contains(&Antichain::from(vec![Elem('a', 1), Elem('b', 2)])));
        assert!(!hashed.contains(&Antichain::from(vec![Elem('c', 3)])));
        assert!(!hashed.contains(&Antichain::from(vec![])));
    }
}