1+ //! Wordcount based on flatcontainer.
2+
3+ #[ cfg( feature = "bincode" ) ]
4+ use {
5+ std:: collections:: HashMap ,
6+ timely:: container:: flatcontainer:: { Containerized , FlatStack } ,
7+ timely:: dataflow:: channels:: pact:: { ExchangeCore , Pipeline } ,
8+ timely:: dataflow:: operators:: core:: InputHandle ,
9+ timely:: dataflow:: operators:: { Inspect , Operator , Probe } ,
10+ timely:: dataflow:: ProbeHandle ,
11+ } ;
12+
13+ #[ cfg( feature = "bincode" ) ]
14+ fn main ( ) {
15+ // initializes and runs a timely dataflow.
16+ timely:: execute_from_args ( std:: env:: args ( ) , |worker| {
17+ let mut input =
18+ <InputHandle < _ , FlatStack < <( String , i64 ) as Containerized >:: Region > > >:: new ( ) ;
19+ let mut probe = ProbeHandle :: new ( ) ;
20+
21+ // create a new input, exchange data, and inspect its output
22+ worker. dataflow :: < usize , _ , _ > ( |scope| {
23+ input
24+ . to_stream ( scope)
25+ . unary :: < FlatStack < <( String , i64 ) as Containerized >:: Region > , _ , _ , _ > (
26+ Pipeline ,
27+ "Split" ,
28+ |_cap, _info| {
29+ move |input, output| {
30+ while let Some ( ( time, data) ) = input. next ( ) {
31+ let mut session = output. session ( & time) ;
32+ for ( text, diff) in data. iter ( ) . flat_map ( |( text, diff) | {
33+ text. split_whitespace ( ) . map ( move |s| ( s, diff) )
34+ } ) {
35+ session. give ( ( text, diff) ) ;
36+ }
37+ }
38+ }
39+ } ,
40+ )
41+ . unary_frontier :: < FlatStack < <( String , i64 ) as Containerized >:: Region > , _ , _ , _ > (
42+ ExchangeCore :: new ( |( s, _) : & ( & str , _ ) | s. len ( ) as u64 ) ,
43+ "WordCount" ,
44+ |_capability, _info| {
45+ let mut queues = HashMap :: new ( ) ;
46+ let mut counts = HashMap :: new ( ) ;
47+
48+ move |input, output| {
49+ while let Some ( ( time, data) ) = input. next ( ) {
50+ queues
51+ . entry ( time. retain ( ) )
52+ . or_insert ( Vec :: new ( ) )
53+ . push ( data. take ( ) ) ;
54+ }
55+
56+ for ( key, val) in queues. iter_mut ( ) {
57+ if !input. frontier ( ) . less_equal ( key. time ( ) ) {
58+ let mut session = output. session ( key) ;
59+ for batch in val. drain ( ..) {
60+ for ( word, diff) in batch. iter ( ) {
61+ let entry =
62+ counts. entry ( word. to_string ( ) ) . or_insert ( 0i64 ) ;
63+ * entry += diff;
64+ session. give ( ( word, * entry) ) ;
65+ }
66+ }
67+ }
68+ }
69+
70+ queues. retain ( |_key, val| !val. is_empty ( ) ) ;
71+ }
72+ } ,
73+ )
74+ . inspect ( |x| println ! ( "seen: {:?}" , x) )
75+ . probe_with ( & mut probe) ;
76+ } ) ;
77+
78+ // introduce data and watch!
79+ for round in 0 ..10 {
80+ input. send ( ( "flat container" , 1 ) ) ;
81+ input. advance_to ( round + 1 ) ;
82+ while probe. less_than ( input. time ( ) ) {
83+ worker. step ( ) ;
84+ }
85+ }
86+ } )
87+ . unwrap ( ) ;
88+ }
89+
90+ #[ cfg( not( feature = "bincode" ) ) ]
91+ fn main ( ) {
92+ eprintln ! ( "Example requires feature bincode." ) ;
93+ }
0 commit comments