Skip to content

Commit 624d71b

Browse files
committed
introduce JoinInput trait and use to abstract over variables
1 parent f7480ac commit 624d71b

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/join.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
//! Join functionality.
22
33
use super::{Relation, Variable};
4+
use std::cell::Ref;
5+
use std::ops::Deref;
46

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)>,
831
output: &Variable<Result>,
932
mut logic: impl FnMut(&Key, &Val1, &Val2) -> Result,
1033
) {
1134
let mut results = Vec::new();
1235

13-
let recent1 = input1.recent.borrow();
14-
let recent2 = input2.recent.borrow();
36+
let recent1 = input1.recent();
37+
let recent2 = input2.recent();
1538

1639
{
1740
// scoped to let `closure` drop borrow of `results`.
1841

1942
let mut closure = |k: &Key, v1: &Val1, v2: &Val2| results.push(logic(k, v1, v2));
2043

21-
for batch2 in input2.stable.borrow().iter() {
44+
for batch2 in input2.stable().iter() {
2245
join_helper(&recent1, &batch2, &mut closure);
2346
}
2447

25-
for batch1 in input1.stable.borrow().iter() {
48+
for batch1 in input1.stable().iter() {
2649
join_helper(&batch1, &recent2, &mut closure);
2750
}
2851

0 commit comments

Comments
 (0)