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
/*!
A `Compactor` is like a `Vec`, but it automatically reduces the resolution of
the data as it gets older.
You specify a compaction policy, like this:
```rust
# use compactor::{Compactor, policy::PolicyError, Resolution};
# fn foo() -> Result<(), PolicyError> {
# struct MyData(usize);
let mut compactor = Compactor::<MyData>::new()
.keep_for_days(7, Resolution::FiveMinute)
.keep_for_days(30, Resolution::Hour)
.keep_for_days(100, Resolution::Day)
.build()?;
# Ok(())
# }
```
You specify how to compact your data, like this:
```rust
# use compactor::Aggregate;
# #[derive(Copy, Clone)]
# struct MyData(usize);
# impl std::ops::Add for MyData { type Output = MyData; fn add(self, other: MyData) -> MyData { MyData(self.0 + other.0) } }
# impl std::ops::Div<usize> for MyData { type Output = MyData; fn div(self, other: usize) -> MyData { MyData(self.0 / other) } }
impl Aggregate for MyData {
fn merge(&mut self, other: MyData) {
*self = (*self + other) / 2;
}
}
```
...and then you can start pushing data into the compactor.
In this example, data will initially be stored at "five-minute" resolution,
meaning that pushed values will be merged into the previous value if they belong
to the same five-minute bucket.
After 7 days, the data will be compacted further, down to "one-hour" resolution.
This means that any values within the same one-hour bucket will be merged into
a single value. Data older than 30 days will be compacted again. Finally, data
older than 100 days will be deleted.
*/
pub use crateAggregate;
pub use crate;
pub use crate;