Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/src/lens/row.rs

9.8 KiB, 1 run

created by r1870400018:65644, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! The event envelope of Daimond's feed, `{v, d, n, b, t}` and then the payload.
2//!
3//! `v` is the schema version, `d` a short id for the device or source, `n` a persisted
4//! per-source sequence number that makes redelivery idempotent for the reader, `b` the build tag
5//! and `t` the wall clock in milliseconds. In `www/js/debugshare.js` an event goes to the wire as
6//! a row `{ts, tag: "ev <kind>", data: <that JSON>}`, and `Row::entry` makes the same row.
7
8use oxedyne_fe2o3_core::prelude::*;
9use oxedyne_fe2o3_jdat::{
10 prelude::*,
11 string::enc::escape_json_string,
12};
13
14
15// The envelope's own names. A payload key of the same name is dropped, so a caller cannot
16// overwrite `n` or `t` by accident.
17const ENVELOPE: [&str; 5] = ["v", "d", "n", "b", "t"];
18
19/// One event: the envelope and the payload fields in the order they were added.
20#[derive(Clone, Debug)]
21pub struct Row {
22 pub v: u8, // schema version
23 pub d: String, // short device or source id
24 pub n: u64, // per-source sequence number
25 pub b: String, // build tag
26 pub t: u64, // wall clock, milliseconds since the epoch
27 pub kind: String, // the `<kind>` of the tag `ev <kind>`
28 pub body: Vec<(String, Dat)>, // payload fields, never named v, d, n, b or t
29}
30
31/// A row as it goes to the wire and to the file: Daimond's `{ts, tag, data}`.
32#[derive(Clone, Debug)]
33pub struct Entry {
34 pub ts: i64, // the client's own clock, kept beside the receive time to show skew
35 pub tag: String,
36 pub data: String,
37}
38
39impl Row {
40
41 pub const VERSION: u8 = 1;
42
43 pub fn new(d: &str, n: u64, b: &str, t: u64, kind: &str) -> Self {
44 Self {
45 v: Self::VERSION,
46 d: d.to_string(),
47 n,
48 b: b.to_string(),
49 t,
50 kind: kind.to_string(),
51 body: Vec::new(),
52 }
53 }
54
55 /// Adds a payload field, or replaces one of the same name where it stands. A name in the
56 /// envelope is dropped.
57 pub fn with(mut self, name: &str, val: Dat) -> Self {
58 if ENVELOPE.contains(&name) {
59 return self;
60 }
61 self.set(name, val);
62 self
63 }
64
65 fn set(&mut self, name: &str, val: Dat) {
66 for (k, v) in self.body.iter_mut() {
67 if k == name {
68 *v = val;
69 return;
70 }
71 }
72 self.body.push((name.to_string(), val));
73 }
74
75 /// The tag of the wire row, `ev <kind>`.
76 pub fn tag(&self) -> String {
77 fmt!("ev {}", self.kind)
78 }
79
80 /// The event as one JSON object, the envelope first.
81 pub fn json(&self) -> Outcome<String> {
82 let mut s = fmt!(
83 "{{\"v\":{},\"d\":\"{}\",\"n\":{},\"b\":\"{}\",\"t\":{}",
84 self.v, escape_json_string(&self.d), self.n, escape_json_string(&self.b), self.t);
85 for (k, v) in &self.body {
86 s.push_str(&fmt!(",\"{}\":{}", escape_json_string(k), res!(compact(v))));
87 }
88 s.push('}');
89 Ok(s)
90 }
91
92 /// The event's JSON cut to `max` bytes, as `fit` does in `debugshare.js`.
93 ///
94 /// The largest string field goes first and only far enough to fit, so a short tool name
95 /// survives a long error message being trimmed, and `tr:1` marks that something was cut. The
96 /// envelope is never sacrificed: an event with no payload left still says which source,
97 /// which build and where in the sequence it sits.
98 pub fn fit(&self, max: usize) -> Outcome<String> {
99 let s = res!(self.json());
100 if s.len() <= max {
101 return Ok(s);
102 }
103 let mut o = self.clone();
104 o.set("tr", Dat::U8(1));
105 for _ in 0..32 {
106 let s = res!(o.json());
107 if s.len() <= max {
108 return Ok(s);
109 }
110 let over = s.len() - max;
111 // The first of equally large strings, and never the marker.
112 let mut big: Option<(usize, usize)> = None;
113 for (i, (k, v)) in o.body.iter().enumerate() {
114 if k == "tr" {
115 continue;
116 }
117 if let Dat::Str(x) = v {
118 if x.len() > big.map_or(0, |(_, n)| n) {
119 big = Some((i, x.len()));
120 }
121 }
122 }
123 let i = match big {
124 Some((i, _)) => i,
125 None => break,
126 };
127 // Characters, not bytes: a multi-byte string loses at least `over` bytes this way,
128 // never fewer, so the loop always converges.
129 let chars = match &o.body[i].1 {
130 Dat::Str(x) => x.chars().count(),
131 _ => 0,
132 };
133 if chars > over + 1 {
134 let keep = chars - over - 1;
135 if let Dat::Str(x) = &mut o.body[i].1 {
136 *x = x.chars().take(keep).collect();
137 }
138 } else {
139 o.body.remove(i);
140 }
141 }
142 let s = res!(o.json());
143 if s.len() <= max {
144 return Ok(s);
145 }
146 // Nothing left but the envelope, and it is still worth sending.
147 let mut bare = self.clone();
148 bare.body.clear();
149 bare.set("tr", Dat::U8(1));
150 bare.json()
151 }
152
153 /// The wire row for this event, its JSON fitted to `max` bytes.
154 pub fn entry(&self, max: usize) -> Outcome<Entry> {
155 Ok(Entry {
156 ts: self.t as i64,
157 tag: self.tag(),
158 data: res!(self.fit(max)),
159 })
160 }
161}
162
163/// A `Dat` as compact JSON text, the way `JSON.stringify` writes it, with nothing between tokens.
164///
165/// A map's keys are taken as their text. A kind JSON has no word for, such as a float or a byte
166/// string, is left to `Dat::json`.
167pub fn compact(d: &Dat) -> Outcome<String> {
168 let mut out = String::new();
169 res!(compact_into(d, &mut out));
170 Ok(out)
171}
172
173fn compact_into(d: &Dat, out: &mut String) -> Outcome<()> {
174 match d {
175 Dat::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
176 Dat::U8(n) => out.push_str(&fmt!("{}", n)),
177 Dat::U16(n) => out.push_str(&fmt!("{}", n)),
178 Dat::U32(n) => out.push_str(&fmt!("{}", n)),
179 Dat::U64(n) => out.push_str(&fmt!("{}", n)),
180 Dat::U128(n) => out.push_str(&fmt!("{}", n)),
181 Dat::I8(n) => out.push_str(&fmt!("{}", n)),
182 Dat::I16(n) => out.push_str(&fmt!("{}", n)),
183 Dat::I32(n) => out.push_str(&fmt!("{}", n)),
184 Dat::I64(n) => out.push_str(&fmt!("{}", n)),
185 Dat::I128(n) => out.push_str(&fmt!("{}", n)),
186 Dat::Str(s) => {
187 out.push('"');
188 out.push_str(&escape_json_string(s));
189 out.push('"');
190 },
191 Dat::Empty => out.push_str("null"),
192 Dat::Opt(o) => match &**o {
193 Some(x) => res!(compact_into(x, out)),
194 None => out.push_str("null"),
195 },
196 Dat::Box(b) => res!(compact_into(b, out)),
197 Dat::List(l) => {
198 out.push('[');
199 for (i, x) in l.iter().enumerate() {
200 if i > 0 {
201 out.push(',');
202 }
203 res!(compact_into(x, out));
204 }
205 out.push(']');
206 },
207 Dat::Map(m) => {
208 let pairs: Vec<(&Dat, &Dat)> = m.iter().collect();
209 res!(compact_members(&pairs, out));
210 },
211 Dat::OrdMap(m) => {
212 // Insertion order, which the map keeps in its keys' ordinals.
213 let mut pairs: Vec<(u64, &Dat, &Dat)> = m.iter().map(|(k, v)| (k.ord(), k.dat(), v)).collect();
214 pairs.sort_by_key(|p| p.0);
215 let pairs: Vec<(&Dat, &Dat)> = pairs.into_iter().map(|(_, k, v)| (k, v)).collect();
216 res!(compact_members(&pairs, out));
217 },
218 other => out.push_str(&res!(other.json())),
219 }
220 Ok(())
221}
222
223fn compact_members(pairs: &[(&Dat, &Dat)], out: &mut String) -> Outcome<()> {
224 out.push('{');
225 for (i, (k, v)) in pairs.iter().enumerate() {
226 if i > 0 {
227 out.push(',');
228 }
229 let name = match k {
230 Dat::Str(s) => s.clone(),
231 other => fmt!("{}", other),
232 };
233 out.push('"');
234 out.push_str(&escape_json_string(&name));
235 out.push_str("\":");
236 res!(compact_into(v, out));
237 }
238 out.push('}');
239 Ok(())
240}
241
242impl Entry {
243
244 /// One NDJSON line, newline included: the receive time, the source's label, then the row.
245 ///
246 /// This is what Daimond's text block carried (`===== <recv ms> ... device=<d> =====` and a
247 /// row under it), one object to a line. The data stays a string, as it is on the wire, so a
248 /// clipped event is still a valid line.
249 pub fn line(&self, device: &str, recv_ms: u64) -> String {
250 fmt!(
251 "{{\"bt\":{},\"device\":\"{}\",\"ts\":{},\"tag\":\"{}\",\"data\":\"{}\"}}\n",
252 recv_ms,
253 escape_json_string(device),
254 self.ts,
255 escape_json_string(&self.tag),
256 escape_json_string(&self.data))
257 }
258}
259
260
261#[cfg(test)]
262mod tests {
263 use super::*;
264
265 #[test]
266 fn compact_writes_what_json_stringify_writes() {
267 let d = create_dat_ordmap(vec![
268 (Dat::Str("b".to_string()), Dat::List(vec![Dat::U8(1), Dat::I64(-2), Dat::Bool(true), Dat::Empty])),
269 (Dat::Str("a".to_string()), Dat::Str("q\"\n\\".to_string())),
270 (Dat::Str("c".to_string()), Dat::Opt(Box::new(None))),
271 (Dat::Str("d".to_string()), create_dat_ordmap(Vec::new())),
272 ]);
273 let got = compact(&d).unwrap_or_default();
274 // The members stand in the order they were added, which a plain map would not keep.
275 assert_eq!(got, "{\"b\":[1,-2,true,null],\"a\":\"q\\\"\\n\\\\\",\"c\":null,\"d\":{}}");
276 }
277}