-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.rs
More file actions
48 lines (44 loc) · 1.46 KB
/
decoder.rs
File metadata and controls
48 lines (44 loc) · 1.46 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
use crate::change::RowChange;
use crate::queue::ChangeOperation;
use crate::wal::WalEvent;
use serde_json::json;
use std::time::{SystemTime, UNIX_EPOCH};
/// Temporary decoder that turns WAL growth bytes into placeholder RowChange events.
/// Placeholder until row-level decoding is implemented.
#[derive(Debug, Default, Clone)]
pub struct WalGrowthDecoder;
impl WalGrowthDecoder {
pub fn decode(&self, event: &WalEvent) -> Vec<RowChange> {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("clock should be >= UNIX epoch");
vec![RowChange {
table_name: "__wal__".to_string(),
operation: ChangeOperation::Insert,
primary_key: now.as_nanos().to_string(),
payload: Some(json!({
"kind": "wal_growth",
"bytes_added": event.bytes_added,
"current_size": event.current_size,
"recorded_at": now.as_secs_f64(),
})),
wal_frame: None,
cursor: None,
}]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn produces_placeholder_row_change() {
let decoder = WalGrowthDecoder::default();
let rows = decoder.decode(&WalEvent {
bytes_added: 1024,
current_size: 2048,
});
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].table_name, "__wal__");
assert_eq!(rows[0].operation, ChangeOperation::Insert);
}
}