-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathorgchart.rs
More file actions
178 lines (158 loc) · 4.84 KB
/
orgchart.rs
File metadata and controls
178 lines (158 loc) · 4.84 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
//! Simple DBSP example.
//!
//! This is similar to the motivating example in chapter 1 of the Differential
//! Dataflow book at <https://timelydataflow.github.io/differential-dataflow/chapter_0/chapter_0.html>. It takes a
//! collection of `Manages` structs that map from an employee ID to the
//! employee's manager, and outputs a collection of `SkipLevel` structs that
//! also include the employee's second-level manager.
use anyhow::Result;
use clap::Parser;
use dbsp::{OrdZSet, OutputHandle, Runtime, Stream, typed_batch::IndexedZSetReader};
use feldera_macros::IsNone;
use rkyv::{Archive, Deserialize, Serialize};
use size_of::SizeOf;
use std::hash::Hash;
type EmployeeID = u64;
/// Indicates that `manager` is the immediate manager of `employee`.
///
/// If `manager == employee` then `manager` is the CEO.
#[derive(
Default,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Debug,
SizeOf,
Archive,
Serialize,
Deserialize,
IsNone,
)]
#[archive_attr(derive(Ord, Eq, PartialEq, PartialOrd))]
#[archive(compare(PartialEq, PartialOrd))]
struct Manages {
manager: EmployeeID,
employee: EmployeeID,
}
/// Indicates that `manager` is the immediate manager of `employee` and that
/// `grandmanager` is the immedate manager of `manager`.
#[derive(
Default,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Debug,
SizeOf,
Archive,
Serialize,
Deserialize,
IsNone,
)]
#[archive_attr(derive(Ord, Eq, PartialEq, PartialOrd))]
#[archive(compare(PartialEq, PartialOrd))]
struct SkipLevel {
grandmanager: EmployeeID,
manager: EmployeeID,
employee: EmployeeID,
}
type SkipLevels = OrdZSet<SkipLevel>;
fn print_output(output: &OutputHandle<OrdZSet<SkipLevel>>) {
for (key, _value, weight) in output.consolidate().iter() {
println!(
" ({}, {}, {}) {:+}",
key.grandmanager, key.manager, key.employee, weight
);
}
println!();
}
#[derive(Debug, Clone, Parser)]
struct Args {
/// Number of employees.
#[clap(long, default_value = "10")]
size: u64,
/// Number of threads.
#[clap(long, default_value = "2")]
threads: usize,
}
fn main() -> Result<()> {
let Args { threads, size } = Args::parse();
let (mut dbsp, (hmanages, output)) = Runtime::init_circuit(threads, |circuit| {
let (manages, hmanages) = circuit.add_input_zset::<Manages>();
let manages_by_manager = manages.map_index(|m| (m.manager, m.clone()));
let manages_by_employee = manages.map_index(|m| (m.employee, m.clone()));
// If Manages { manager, employee: common } and Manages { manager: common,
// employee } then SkipLevel { grandmanager: manager, manager: common,
// employee }.
let skiplevels: Stream<_, SkipLevels> =
manages_by_employee.join(&manages_by_manager, |common, m1, m2| SkipLevel {
grandmanager: m1.manager,
manager: *common,
employee: m2.employee,
});
Ok((hmanages, skiplevels.output()))
})
.unwrap();
// Initially, let each manager be the employee's ID divided by 2 (yielding a
// binary tree management structure). Then run it through DBSP in a single
// step.
for employee in 0..size {
hmanages.push(
Manages {
manager: employee / 2,
employee,
},
1,
);
}
dbsp.transaction().unwrap();
println!("Initialization:");
print_output(&output);
// Second, replace the binary management structure by a ternary one. This time,
// print the changes after each step, just to show how that works.
for employee in 1..size {
hmanages.push(
Manages {
manager: employee / 2,
employee,
},
-1,
);
hmanages.push(
Manages {
manager: employee / 3,
employee,
},
1,
);
dbsp.transaction().unwrap();
println!("Changes from adjusting {employee}'s manager:");
print_output(&output);
// let profile = dbsp.retrieve_profile().unwrap();
// println!("total used bytes: {}", profile.total_used_bytes().unwrap());
// println!(
// "total allocated bytes: {}",
// profile.total_allocated_bytes().unwrap()
// );
// println!(
// "total shared bytes: {}",
// profile.total_shared_bytes().unwrap()
// );
// println!(
// "num table entries: {}",
// profile.total_relation_size().unwrap()
// );
}
// let profile = dbsp.retrieve_profile().unwrap();
// println!(
// "used bytes profile: {:?}",
// profile.used_bytes_profile().unwrap()
// );
dbsp.kill().unwrap();
Ok(())
}