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
use super::tracer::{Tracer, TracerEvent};
use derive_more::{Deref, DerefMut};
use rill_protocol::provider::{Description, Path, RillData, RillEvent, StreamType, Timestamp};
use std::collections::HashMap;
use std::time::SystemTime;
#[derive(Debug)]
pub enum DictRecord {
Association { key: String, value: String },
}
#[derive(Debug)]
pub struct Record {
timestamp: Timestamp,
value: String,
}
#[derive(Debug, Default)]
pub struct DictState {
map: HashMap<String, Record>,
last_event: Option<RillEvent>,
}
impl TracerEvent for DictRecord {
type State = DictState;
fn aggregate(self, state: &mut Self::State, timestamp: Timestamp) -> Option<&RillEvent> {
match self {
Self::Association { key, value } => {
let record = Record {
timestamp: timestamp.clone(),
value: value.clone(),
};
state.map.insert(key.clone(), record);
let data = RillData::DictRecord { key, value };
let last_event = RillEvent { timestamp, data };
state.last_event = Some(last_event);
state.last_event.as_ref()
}
}
}
fn make_snapshot(state: &Self::State) -> Vec<RillEvent> {
state
.map
.iter()
.map(|(key, record)| {
let data = RillData::DictRecord {
key: key.clone(),
value: record.value.clone(),
};
RillEvent {
timestamp: record.timestamp.clone(),
data,
}
})
.collect()
}
}
#[derive(Debug, Deref, DerefMut)]
pub struct DictTracer {
tracer: Tracer<DictRecord>,
}
impl DictTracer {
pub fn new(path: Path) -> Self {
let info = format!("{} dictionary", path);
let description = Description {
path,
info,
stream_type: StreamType::DictStream,
};
let tracer = Tracer::new(description);
Self { tracer }
}
pub fn set(&self, key: impl ToString, value: impl ToString, timestamp: Option<SystemTime>) {
let data = DictRecord::Association {
key: key.to_string(),
value: value.to_string(),
};
self.tracer.send(data, timestamp);
}
}