forked from TimelyDataflow/timely-dataflow
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcolumnar.rs
More file actions
250 lines (216 loc) · 9.99 KB
/
columnar.rs
File metadata and controls
250 lines (216 loc) · 9.99 KB
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
//! Wordcount based on the `columnar` crate.
use std::collections::HashMap;
use columnar::Index;
use timely::Accountable;
use timely::container::CapacityContainerBuilder;
use timely::dataflow::channels::pact::{ExchangeCore, Pipeline};
use timely::dataflow::InputHandle;
use timely::dataflow::operators::{InspectCore, Operator, Probe};
use timely::dataflow::ProbeHandle;
// Creates `WordCountContainer` and `WordCountReference` structs,
// as well as various implementations relating them to `WordCount`.
#[derive(columnar::Columnar)]
struct WordCount {
text: String,
diff: i64,
}
fn main() {
type InnerContainer = <WordCount as columnar::Columnar>::Container;
type Container = Column<InnerContainer>;
use columnar::Len;
let config = timely::Config {
communication: timely::CommunicationConfig::ProcessBinary(3),
worker: timely::WorkerConfig::default(),
};
// initializes and runs a timely dataflow.
timely::execute(config, |worker| {
let mut input = <InputHandle<_, CapacityContainerBuilder<Container>>>::new();
let probe = ProbeHandle::new();
// create a new input, exchange data, and inspect its output
worker.dataflow::<usize, _, _>(|scope| {
input
.to_stream(scope)
.unary(
Pipeline,
"Split",
|_cap, _info| {
move |input, output| {
input.for_each_time(|time, data| {
let mut session = output.session(&time);
for data in data {
for wordcount in data.borrow().into_index_iter().flat_map(|wordcount| {
wordcount.text.split(|b| b.is_ascii_whitespace()).filter(|s| !s.is_empty()).map(move |text| WordCountReference { text, diff: wordcount.diff })
}) {
session.give(wordcount);
}
}
});
}
},
)
.container::<Container>()
.unary_frontier(
ExchangeCore::<ColumnBuilder<InnerContainer>,_>::new_core(|x: &WordCountReference<&[u8],&i64>| x.text.len() as u64),
"WordCount",
|_capability, _info| {
let mut queues = HashMap::new();
let mut counts = HashMap::new();
move |(input, frontier), output| {
input.for_each_time(|time, data| {
queues
.entry(time.retain(output.output_index()))
.or_insert(Vec::new())
.extend(data.map(std::mem::take));
});
for (key, val) in queues.iter_mut() {
if !frontier.less_equal(key.time()) {
let mut session = output.session(key);
for batch in val.drain(..) {
for wordcount in batch.borrow().into_index_iter() {
let total =
if let Some(count) = counts.get_mut(wordcount.text) {
*count += wordcount.diff;
*count
}
else {
counts.insert(wordcount.text.to_vec(), *wordcount.diff);
*wordcount.diff
};
session.give(WordCountReference { text: wordcount.text, diff: total });
}
}
}
}
queues.retain(|_key, val| !val.is_empty());
}
},
)
.container::<Container>()
.inspect_container(|x| {
match x {
Ok((time, data)) => {
println!("seen at: {:?}\t{:?} records", time, data.record_count());
for wc in data.borrow().into_index_iter() {
println!(" {}: {}", std::str::from_utf8(wc.text).unwrap_or("<invalid utf8>"), wc.diff);
}
},
Err(frontier) => println!("frontier advanced to {:?}", frontier),
}
})
.probe_with(&probe);
});
// introduce data and watch!
for round in 0..10 {
input.send(WordCountReference { text: "flat container", diff: 1 });
input.advance_to(round + 1);
while probe.less_than(input.time()) {
worker.step();
}
}
})
.unwrap();
}
pub use container::Column;
mod container {
use columnar::bytes::stash::Stash;
#[derive(Clone, Default)]
pub struct Column<C> { pub stash: Stash<C, timely_bytes::arc::Bytes> }
use columnar::{Len, Index};
use columnar::bytes::indexed;
use columnar::common::IterOwn;
impl<C: columnar::ContainerBytes> Column<C> {
/// Borrows the contents no matter their representation.
#[inline(always)] pub fn borrow(&self) -> C::Borrowed<'_> { self.stash.borrow() }
}
impl<C: columnar::ContainerBytes> timely::Accountable for Column<C> {
#[inline] fn record_count(&self) -> i64 { i64::try_from(self.borrow().len()).unwrap() }
#[inline] fn is_empty(&self) -> bool { self.borrow().is_empty() }
}
impl<C: columnar::ContainerBytes> timely::container::DrainContainer for Column<C> {
type Item<'a> = C::Ref<'a>;
type DrainIter<'a> = IterOwn<C::Borrowed<'a>>;
fn drain<'a>(&'a mut self) -> Self::DrainIter<'a> { self.borrow().into_index_iter() }
}
impl<C: columnar::ContainerBytes> timely::container::SizableContainer for Column<C> {
fn at_capacity(&self) -> bool {
match &self.stash {
Stash::Typed(t) => {
let length_in_bytes = 8 * indexed::length_in_words(&t.borrow());
length_in_bytes >= (1 << 20)
},
Stash::Bytes(_) => true,
Stash::Align(_) => true,
}
}
fn ensure_capacity(&mut self, _stash: &mut Option<Self>) { }
}
impl<C: columnar::Container + columnar::ContainerBytes, T> timely::container::PushInto<T> for Column<C> where C: columnar::Push<T> {
#[inline] fn push_into(&mut self, item: T) { use columnar::Push; self.stash.push(item) }
}
impl<C: columnar::ContainerBytes> timely::dataflow::channels::ContainerBytes for Column<C> {
fn from_bytes(bytes: timely::bytes::arc::Bytes) -> Self { Self { stash: Stash::try_from_bytes(bytes).expect("valid columnar data") } }
fn length_in_bytes(&self) -> usize { self.stash.length_in_bytes() }
fn into_bytes<W: ::std::io::Write>(&self, writer: &mut W) { self.stash.write_bytes(writer).expect("write failed") }
}
}
use builder::ColumnBuilder;
mod builder {
use std::collections::VecDeque;
use columnar::bytes::{indexed, stash::Stash};
use super::Column;
/// A container builder for `Column<C>`.
#[derive(Default)]
pub struct ColumnBuilder<C> {
/// Container that we're writing to.
current: C,
/// Empty allocation.
empty: Option<Column<C>>,
/// Completed containers pending to be sent.
pending: VecDeque<Column<C>>,
}
impl<C: columnar::ContainerBytes, T> timely::container::PushInto<T> for ColumnBuilder<C> where C: columnar::Push<T> {
#[inline]
fn push_into(&mut self, item: T) {
self.current.push(item);
// If there is less than 10% slop with 2MB backing allocations, mint a container.
let words = indexed::length_in_words(&self.current.borrow());
let round = (words + ((1 << 18) - 1)) & !((1 << 18) - 1);
if round - words < round / 10 {
let mut alloc = Vec::with_capacity(round);
indexed::encode(&mut alloc, &self.current.borrow());
self.pending.push_back(Column { stash: Stash::Align(alloc.into_boxed_slice().into()) });
self.current.clear();
}
}
}
use timely::container::{ContainerBuilder, LengthPreservingContainerBuilder};
impl<C: columnar::ContainerBytes> ContainerBuilder for ColumnBuilder<C> {
type Container = Column<C>;
#[inline]
fn extract(&mut self) -> Option<&mut Self::Container> {
if let Some(container) = self.pending.pop_front() {
self.empty = Some(container);
self.empty.as_mut()
} else {
None
}
}
#[inline]
fn finish(&mut self) -> Option<&mut Self::Container> {
if !self.current.is_empty() {
self.pending.push_back(Column { stash: Stash::Typed(std::mem::take(&mut self.current)) });
}
self.empty = self.pending.pop_front();
self.empty.as_mut()
}
#[inline]
fn relax(&mut self) {
// The caller is responsible for draining all contents; assert that we are empty.
// The assertion is not strictly necessary, but it helps catch bugs.
assert!(self.current.is_empty());
assert!(self.pending.is_empty());
*self = Self::default();
}
}
impl<C: columnar::ContainerBytes> LengthPreservingContainerBuilder for ColumnBuilder<C> { }
}