|
1 | 1 | //! Join functionality.
|
2 | 2 |
|
3 | 3 | use super::{Relation, Variable};
|
| 4 | +use std::cell::Ref; |
| 5 | +use std::ops::Deref; |
4 | 6 |
|
5 |
| -pub fn join_into<Key: Ord, Val1: Ord, Val2: Ord, Result: Ord>( |
6 |
| - input1: &Variable<(Key, Val1)>, |
7 |
| - input2: &Variable<(Key, Val2)>, |
| 7 | +pub trait JoinInput<'me, Tuple: Ord>: Copy { |
| 8 | + type RecentTuples: Deref<Target = Relation<Tuple>>; |
| 9 | + type StableTuples: Deref<Target = Vec<Relation<Tuple>>>; |
| 10 | + |
| 11 | + fn recent(self) -> Self::RecentTuples; |
| 12 | + fn stable(self) -> Self::StableTuples; |
| 13 | +} |
| 14 | + |
| 15 | +impl<'me, Tuple: Ord> JoinInput<'me, Tuple> for &'me Variable<Tuple> { |
| 16 | + type RecentTuples = Ref<'me, Relation<Tuple>>; |
| 17 | + type StableTuples = Ref<'me, Vec<Relation<Tuple>>>; |
| 18 | + |
| 19 | + fn recent(self) -> Self::RecentTuples { |
| 20 | + self.recent.borrow() |
| 21 | + } |
| 22 | + |
| 23 | + fn stable(self) -> Self::StableTuples { |
| 24 | + self.stable.borrow() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +pub fn join_into<'me, Key: Ord, Val1: Ord, Val2: Ord, Result: Ord>( |
| 29 | + input1: impl JoinInput<'me, (Key, Val1)>, |
| 30 | + input2: impl JoinInput<'me, (Key, Val2)>, |
8 | 31 | output: &Variable<Result>,
|
9 | 32 | mut logic: impl FnMut(&Key, &Val1, &Val2) -> Result,
|
10 | 33 | ) {
|
11 | 34 | let mut results = Vec::new();
|
12 | 35 |
|
13 |
| - let recent1 = input1.recent.borrow(); |
14 |
| - let recent2 = input2.recent.borrow(); |
| 36 | + let recent1 = input1.recent(); |
| 37 | + let recent2 = input2.recent(); |
15 | 38 |
|
16 | 39 | {
|
17 | 40 | // scoped to let `closure` drop borrow of `results`.
|
18 | 41 |
|
19 | 42 | let mut closure = |k: &Key, v1: &Val1, v2: &Val2| results.push(logic(k, v1, v2));
|
20 | 43 |
|
21 |
| - for batch2 in input2.stable.borrow().iter() { |
| 44 | + for batch2 in input2.stable().iter() { |
22 | 45 | join_helper(&recent1, &batch2, &mut closure);
|
23 | 46 | }
|
24 | 47 |
|
25 |
| - for batch1 in input1.stable.borrow().iter() { |
| 48 | + for batch1 in input1.stable().iter() { |
26 | 49 | join_helper(&batch1, &recent2, &mut closure);
|
27 | 50 | }
|
28 | 51 |
|
|
0 commit comments