palimpsest_dataflow/algorithms/identifiers.rs
1//! Assign unique identifiers to records.
2
3use timely::dataflow::Scope;
4
5use crate::difference::Abelian;
6use crate::lattice::Lattice;
7use crate::operators::*;
8use crate::{ExchangeData, Hashable, VecCollection};
9
10/// Assign unique identifiers to elements of a collection.
11pub trait Identifiers<G: Scope, D: ExchangeData, R: ExchangeData + Abelian> {
12 /// Assign unique identifiers to elements of a collection.
13 ///
14 /// # Example
15 /// ```
16 /// use palimpsest_dataflow::input::Input;
17 /// use palimpsest_dataflow::algorithms::identifiers::Identifiers;
18 /// use palimpsest_dataflow::operators::Threshold;
19 ///
20 /// ::timely::example(|scope| {
21 ///
22 /// let identifiers =
23 /// scope.new_collection_from(1 .. 10).1
24 /// .identifiers()
25 /// // assert no conflicts
26 /// .map(|(data, id)| id)
27 /// .threshold(|_id,cnt| if cnt > &1 { *cnt } else { 0 })
28 /// .assert_empty();
29 /// });
30 /// ```
31 fn identifiers(&self) -> VecCollection<G, (D, u64), R>;
32}
33
34impl<G, D, R> Identifiers<G, D, R> for VecCollection<G, D, R>
35where
36 G: Scope<Timestamp: Lattice>,
37 D: ExchangeData + ::std::hash::Hash,
38 R: ExchangeData + Abelian,
39{
40 fn identifiers(&self) -> VecCollection<G, (D, u64), R> {
41 // The design here is that we iteratively develop a collection
42 // of pairs (round, record), where each pair is a proposal that
43 // the hash for record should be (round, record).hashed().
44 //
45 // Iteratively, any colliding pairs establish a winner (the one
46 // with the lower round, breaking ties by record), and indicate
47 // that the losers should increment their round and try again.
48 //
49 // Non-obviously, this happens via a `reduce` operator that yields
50 // additions and subtractions of losers, rather than reproducing
51 // the winners. This is done under the premise that losers are
52 // very rare, and maintaining winners in both the input and output
53 // of `reduce` is an unnecessary duplication.
54
55 use crate::collection::AsCollection;
56
57 let init = self.map(|record| (0, record));
58 timely::dataflow::operators::generic::operator::empty(&init.scope())
59 .as_collection()
60 .iterate(|diff| {
61 init.enter(&diff.scope())
62 .concat(diff)
63 .map(|pair| (pair.hashed(), pair))
64 .reduce(|_hash, input, output| {
65 // keep round-positive records as changes.
66 let ((round, record), count) = &input[0];
67 if *round > 0 {
68 let mut neg_count = count.clone();
69 neg_count.negate();
70 output.push(((0, record.clone()), neg_count));
71 output.push(((*round, record.clone()), count.clone()));
72 }
73 // if any losers, increment their rounds.
74 for ((round, record), count) in input[1..].iter() {
75 let mut neg_count = count.clone();
76 neg_count.negate();
77 output.push(((0, record.clone()), neg_count));
78 output.push(((*round + 1, record.clone()), count.clone()));
79 }
80 })
81 .map(|(_hash, pair)| pair)
82 })
83 .concat(&init)
84 .map(|pair| {
85 let hash = pair.hashed();
86 (pair.1, hash)
87 })
88 }
89}
90
91#[cfg(test)]
92mod tests {
93
94 #[test]
95 fn are_unique() {
96 // It is hard to test the above method, because we would want
97 // to exercise the case with hash collisions. Instead, we test
98 // a version with a crippled hash function to see that even if
99 // there are collisions, everyone gets a unique identifier.
100
101 use crate::input::Input;
102 use crate::operators::iterate::Iterate;
103 use crate::operators::{Reduce, Threshold};
104
105 ::timely::example(|scope| {
106 let input = scope.new_collection_from(1..4).1;
107
108 use crate::collection::AsCollection;
109
110 let init = input.map(|record| (0, record));
111 timely::dataflow::operators::generic::operator::empty(&init.scope())
112 .as_collection()
113 .iterate(|diff| {
114 init.enter(&diff.scope())
115 .concat(&diff)
116 .map(|(round, num)| ((round + num) / 10, (round, num)))
117 .reduce(|_hash, input, output| {
118 println!("Input: {:?}", input);
119 // keep round-positive records as changes.
120 let ((round, record), count) = &input[0];
121 if *round > 0 {
122 output.push(((0, record.clone()), -*count));
123 output.push(((*round, record.clone()), *count));
124 }
125 // if any losers, increment their rounds.
126 for ((round, record), count) in input[1..].iter() {
127 output.push(((0, record.clone()), -*count));
128 output.push(((*round + 1, record.clone()), *count));
129 }
130 })
131 .inspect(|x| println!("{:?}", x))
132 .map(|(_hash, pair)| pair)
133 })
134 .concat(&init)
135 .map(|(round, num)| (num, (round + num) / 10))
136 .map(|(_data, id)| id)
137 .threshold(|_id, cnt| if cnt > &1 { *cnt } else { 0 })
138 .assert_empty();
139 });
140 }
141}