codd 0.1.0

codd is a minimal in-memory database with relational algebraic expressions as queries.
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
use super::{evaluate, expression_ext::ExpressionExt, helpers::gallop, Database};
use crate::{expression::Expression, Error, Tuple};
use std::any::Any;
use std::{
    cell::{Ref, RefCell},
    ops::Deref,
    rc::Rc,
};

/// Is a wrapper around a vector of tuples. As an invariant, the content of `Tuples` is sorted.
///
/// **Note**: `Tuples` is borrowed from `Relation` in [`datafrog`].
///
/// [`datafrog`]: https://github.com/rust-lang/datafrog
#[derive(Clone, Debug, PartialEq)]
pub struct Tuples<T: Tuple> {
    /// Is the vector of tuples in this instance.
    items: Vec<T>,
}

impl<T: Tuple, I: IntoIterator<Item = T>> From<I> for Tuples<T> {
    fn from(iterator: I) -> Self {
        let mut items: Vec<T> = iterator.into_iter().collect();
        items.sort_unstable();
        items.dedup();
        Tuples { items }
    }
}

impl<T: Tuple> Tuples<T> {
    /// Merges the instances of the reciver with `other` and returns a new `Tuples`
    /// instance.
    pub(crate) fn merge(self, other: Self) -> Self {
        let mut tuples = Vec::with_capacity(self.items.len() + other.items.len());
        tuples.extend(self.items.into_iter());
        tuples.extend(other.items.into_iter());
        tuples.into()
    }

    /// Returns an immutable reference to the tuples of the receiver.
    pub fn items(&self) -> &[T] {
        &self.items
    }

    /// Consumes the receiver and returns the underlying (sorted) vector of tuples.
    #[inline(always)]
    pub fn into_tuples(self) -> Vec<T> {
        self.items
    }
}

impl<T: Tuple> Deref for Tuples<T> {
    type Target = Vec<T>;

    fn deref(&self) -> &Self::Target {
        &self.items
    }
}

impl<T: Tuple> core::ops::DerefMut for Tuples<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.items
    }
}

/// Is used to store database `Instance`s in a map by hiding their (generic) type.
pub(super) trait DynInstance {
    /// Returns the instance as `Any`
    fn as_any(&self) -> &dyn Any;

    /// Returns true if the instance has been affected by last updates. It also moves all
    /// `to_add` tuples to `recent` and `recent` tuples to `stable`.
    fn changed(&self) -> bool;

    /// Clones the instance in a `Box`.
    fn clone_box(&self) -> Box<dyn DynInstance>;
}

/// Is used to store `ViewInstance`s in a map by hiding their (generic) types.
pub(super) trait DynViewInstance {
    /// Returns the view instance as `Any`.
    fn as_any(&self) -> &dyn Any;

    /// Returns the `Instance` storing the tuples of the view as a trait object.
    fn instance(&self) -> &dyn DynInstance;

    /// Initializes the view with the existing tuples in `db`.
    fn initialize(&self, db: &Database) -> Result<(), Error>;

    /// Stabilizes the view from the `recent` tuples in the instances of `db`.
    fn stabilize(&self, db: &Database) -> Result<(), Error>;

    /// Clones the instance in a `Box`.
    fn clone_box(&self) -> Box<dyn DynViewInstance>;
}

/// Contains the tuples of a relation in the database.
///
/// **Note**: `Instance` is a replica of `Variable` in [`datafrog`].
///
/// [`datafrog`]: https://github.com/rust-lang/datafrog
#[derive(Debug, PartialEq)]
pub(super) struct Instance<T: Tuple> {
    /// Is the set of tuples that are already considered when updating views.
    stable: Rc<RefCell<Vec<Tuples<T>>>>,

    /// Is the set of tuples that have not yet been reflected in views.
    recent: Rc<RefCell<Tuples<T>>>,

    /// Is the set of tuples to add: they may be duplicates of existing tuples
    /// in which case they are ignored.
    to_add: Rc<RefCell<Vec<Tuples<T>>>>,
}

impl<T: Tuple> Instance<T> {
    /// Creates a new empty isntance.
    pub fn new() -> Self {
        Self {
            stable: Rc::new(RefCell::new(Vec::new())),
            recent: Rc::new(RefCell::new(Vec::new().into())),
            to_add: Rc::new(RefCell::new(Vec::new())),
        }
    }

    /// Adds a `Tuples` instance to `to_add` tuples. These tuples will be ultimately
    /// added to the instance if they already don't exist.
    pub fn insert(&self, tuples: Tuples<T>) {
        if !tuples.is_empty() {
            self.to_add.borrow_mut().push(tuples);
        }
    }

    /// Returns an immutable reference (of type `std::cell::Ref`) to the stable tuples
    /// of this instance.
    #[inline(always)]
    pub fn stable(&self) -> Ref<Vec<Tuples<T>>> {
        self.stable.borrow()
    }

    /// Returns an immutable reference (of type `std::cell::Ref`) to the recent tuples
    /// of this instance.
    #[inline(always)]
    pub fn recent(&self) -> Ref<Tuples<T>> {
        self.recent.borrow()
    }

    /// Returns an immutable reference (of type `std::cell::Ref`) to the candidates to
    /// be added to the recent tuples of this instance (if they already don't exist).
    #[inline(always)]
    pub fn to_add(&self) -> Ref<Vec<Tuples<T>>> {
        self.to_add.borrow()
    }
}

impl<T: Tuple> Clone for Instance<T> {
    fn clone(&self) -> Self {
        Self {
            stable: Rc::new(RefCell::new(self.stable.borrow().clone())),
            recent: Rc::new(RefCell::new(self.recent.borrow().clone())),
            to_add: Rc::new(RefCell::new(self.to_add.borrow().clone())),
        }
    }
}

impl<T> DynInstance for Instance<T>
where
    T: Tuple + 'static,
{
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn changed(&self) -> bool {
        if !self.recent.borrow().is_empty() {
            let mut recent =
                ::std::mem::replace(&mut (*self.recent.borrow_mut()), Vec::new().into());
            while self
                .stable
                .borrow()
                .last()
                .map(|x| x.len() <= 2 * recent.len())
                == Some(true)
            {
                let last = self.stable.borrow_mut().pop().unwrap();
                recent = recent.merge(last);
            }
            self.stable.borrow_mut().push(recent);
        }

        let to_add = self.to_add.borrow_mut().pop();
        if let Some(mut to_add) = to_add {
            while let Some(to_add_more) = self.to_add.borrow_mut().pop() {
                to_add = to_add.merge(to_add_more);
            }
            for batch in self.stable.borrow().iter() {
                let mut slice = &batch[..];
                to_add.items.retain(|x| {
                    slice = gallop(slice, |y| y < x);
                    slice.is_empty() || &slice[0] != x
                });
            }
            *self.recent.borrow_mut() = to_add;
        }

        !self.recent.borrow().is_empty()
    }

    fn clone_box(&self) -> Box<dyn DynInstance> {
        let mut to_add = Vec::new();
        for batch in self.to_add.borrow().iter() {
            to_add.push(batch.clone());
        }

        let recent = (*self.recent.borrow()).clone();

        let mut stable: Vec<Tuples<T>> = Vec::new();
        for batch in self.stable.borrow().iter() {
            stable.push(batch.clone());
        }

        Box::new(Self {
            stable: Rc::new(RefCell::new(stable)),
            recent: Rc::new(RefCell::new(recent)),
            to_add: Rc::new(RefCell::new(to_add)),
        })
    }
}

/// Is a wrapper around the `Instance` storing the tuples of a view and
/// the relational expression to which the view evaluates.
pub(super) struct ViewInstance<T, E>
where
    T: Tuple,
    E: Expression<T>,
{
    /// Is the `Instance` storing the tuples of the view.
    instance: Instance<T>,

    /// Is the view expression.
    expression: E,
}

impl<T, E> ViewInstance<T, E>
where
    T: Tuple,
    E: Expression<T>,
{
    pub fn new(expression: E) -> Self {
        Self {
            instance: Instance::new(),
            expression,
        }
    }

    /// Returns the `Instance` storing the tuples of this view.
    pub fn instance(&self) -> &Instance<T> {
        &self.instance
    }
}

impl<T, E> DynViewInstance for ViewInstance<T, E>
where
    T: Tuple + 'static,
    E: ExpressionExt<T> + 'static,
{
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn instance(&self) -> &dyn DynInstance {
        &self.instance
    }

    fn initialize(&self, db: &Database) -> Result<(), Error> {
        let incremental = evaluate::IncrementalCollector::new(db);
        let stable = self.expression.collect_stable(&incremental)?;

        for batch in stable {
            self.instance.insert(batch);
        }
        Ok(())
    }

    fn stabilize(&self, db: &Database) -> Result<(), Error> {
        let incremental = evaluate::IncrementalCollector::new(db);
        let recent = self.expression.collect_recent(&incremental)?;

        self.instance.insert(recent);
        Ok(())
    }

    fn clone_box(&self) -> Box<dyn DynViewInstance> {
        Box::new(Self {
            instance: self.instance.clone(),
            expression: self.expression.clone(),
        })
    }
}

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

    #[test]
    fn test_clone_instance() {
        {
            let instance = Instance::<bool>::new();
            assert_eq!(instance, instance.clone());
        }
        {
            let instance = Instance::<i32> {
                stable: Rc::new(RefCell::new(vec![vec![1, 2].into()])),
                recent: Rc::new(RefCell::new(vec![2, 3, 4].into())),
                to_add: Rc::new(RefCell::new(vec![vec![4, 5].into()])),
            };
            let cloned = instance.clone();
            assert_eq!(instance, cloned);
        }
    }

    #[test]
    fn test_tuples_from_list() {
        {
            let tuples = Tuples::<i32>::from(vec![]);
            assert_eq!(Vec::<i32>::new(), tuples.items());
        }
        {
            let tuples = Tuples::<i32>::from(vec![5, 4, 2, 1, 3]);
            assert_eq!(vec![1, 2, 3, 4, 5], tuples.items());
        }
        {
            let tuples = Tuples::<i32>::from(vec![3, 2, 2, 1, 3]);
            assert_eq!(vec![1, 2, 3], tuples.items());
        }
    }

    #[test]
    fn test_tuples_merge() {
        {
            let tuples = Tuples::<i32>::from(vec![]);
            assert_eq!(Vec::<i32>::new(), tuples.merge(vec![].into()).items());
        }
        {
            let tuples = Tuples::<i32>::from(vec![5, 4]);
            assert_eq!(vec![2, 3, 4, 5], tuples.merge(vec![2, 3].into()).items());
        }
        {
            let tuples = Tuples::<i32>::from(vec![5, 4, 4]);
            assert_eq!(vec![3, 4, 5], tuples.merge(vec![5, 3].into()).items());
        }
    }

    #[test]
    fn test_instance_insert() {
        {
            let relation = Instance::<i32> {
                stable: Rc::new(RefCell::new(vec![])),
                recent: Rc::new(RefCell::new(vec![].into())),
                to_add: Rc::new(RefCell::new(vec![])),
            };
            relation.insert(vec![].into());
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.stable.borrow());
            assert_eq!(Vec::<i32>::new(), relation.recent.borrow().items);
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.to_add.borrow());
        }

        {
            let relation: Instance<i32> = Instance {
                stable: Rc::new(RefCell::new(vec![])),
                recent: Rc::new(RefCell::new(vec![1, 2, 3].into())),
                to_add: Rc::new(RefCell::new(vec![])),
            };
            relation.insert(vec![].into());
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.stable.borrow());
            assert_eq!(vec![1, 2, 3], relation.recent.borrow().items);
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.to_add.borrow());
        }

        {
            let relation: Instance<i32> = Instance {
                stable: Rc::new(RefCell::new(vec![])),
                recent: Rc::new(RefCell::new(vec![1, 2, 3].into())),
                to_add: Rc::new(RefCell::new(vec![])),
            };
            relation.insert(vec![5, 4].into());
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.stable.borrow());
            assert_eq!(vec![1, 2, 3], relation.recent.borrow().items);
            assert_eq!(
                Vec::<Tuples<i32>>::from(vec![vec![4, 5].into()]),
                *relation.to_add.borrow(),
            );
        }
    }

    #[test]
    fn test_instance_changed() {
        {
            let relation: Instance<i32> = Instance {
                stable: Rc::new(RefCell::new(vec![])),
                recent: Rc::new(RefCell::new(vec![].into())),
                to_add: Rc::new(RefCell::new(vec![])),
            };
            relation.changed();
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.stable.borrow());
            assert_eq!(Vec::<i32>::new(), relation.recent.borrow().items);
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.to_add.borrow());
        }

        {
            let relation = Instance::<i32> {
                stable: Rc::new(RefCell::new(vec![])),
                recent: Rc::new(RefCell::new(vec![].into())),
                to_add: Rc::new(RefCell::new(vec![vec![1, 2].into()])),
            };
            assert!(relation.changed());
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.stable.borrow());
            assert_eq!(vec![1, 2], relation.recent.borrow().items);
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.to_add.borrow());
        }

        {
            let relation = Instance::<i32> {
                stable: Rc::new(RefCell::new(vec![])),
                recent: Rc::new(RefCell::new(vec![1, 2].into())),
                to_add: Rc::new(RefCell::new(vec![])),
            };
            assert!(!relation.changed());
            assert_eq!(
                Vec::<Tuples<i32>>::from(vec![vec![1, 2].into()]),
                *relation.stable.borrow()
            );
            assert_eq!(Vec::<i32>::new(), relation.recent.borrow().items);
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.to_add.borrow());
        }

        {
            let relation = Instance::<i32> {
                stable: Rc::new(RefCell::new(vec![])),
                recent: Rc::new(RefCell::new(vec![1, 2].into())),
                to_add: Rc::new(RefCell::new(vec![vec![3, 4].into()])),
            };
            assert!(relation.changed());
            assert_eq!(
                Vec::<Tuples<i32>>::from(vec![vec![1, 2].into()]),
                *relation.stable.borrow()
            );
            assert_eq!(vec![3, 4], relation.recent.borrow().items);
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.to_add.borrow());
        }

        {
            let relation = Instance::<i32> {
                stable: Rc::new(RefCell::new(vec![vec![1, 2].into()])),
                recent: Rc::new(RefCell::new(vec![2, 3, 4].into())),
                to_add: Rc::new(RefCell::new(vec![vec![4, 5].into()])),
            };
            assert!(relation.changed());
            assert_eq!(
                Vec::<Tuples<i32>>::from(vec![vec![1, 2, 3, 4].into()]),
                *relation.stable.borrow()
            );
            assert_eq!(vec![5], relation.recent.borrow().items);
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.to_add.borrow());
        }

        {
            let relation = Instance::<i32> {
                stable: Rc::new(RefCell::new(vec![vec![1, 2].into()])),
                recent: Rc::new(RefCell::new(vec![2, 3, 4].into())),
                to_add: Rc::new(RefCell::new(vec![vec![1, 5].into()])),
            };
            assert!(relation.changed());
            assert_eq!(
                Vec::<Tuples<i32>>::from(vec![vec![1, 2, 3, 4].into()]),
                *relation.stable.borrow()
            );
            assert_eq!(vec![5], relation.recent.borrow().items);
            assert_eq!(Vec::<Tuples<i32>>::new(), *relation.to_add.borrow());
        }
    }
}