diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 3b95b20..e26e802 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -55,3 +55,14 @@ This code has async code, to handle input streams and communication with clients - `stream.rs`: Stream managers: start the stream `cmd` and dispatch its stdout lines to its Filter managers. - `filter.rs`: Filter managers: handle lines, persistance, store matches and trigger actions. This is the main piece of runtime logic. - `socket.rs`: The socket task, responsible for communication with clients. + +### `src/waltree` + +Persistence layer. + +This is a database highly adapted to reaction workload, making reaction faster than when used with general purpose key-value databases +(heed, sled and fjall crates ahve been tested). +Its design is explained in the comments of its files: + +- `mod.rs`: main database code, with its two API structs: Tree and Database. +- `raw.rs` low-level part, directly interacting with de/serializisation and files. diff --git a/src/waltree/helpers.rs b/src/waltree/helpers.rs new file mode 100644 index 0000000..4ad7728 --- /dev/null +++ b/src/waltree/helpers.rs @@ -0,0 +1,106 @@ +use std::collections::BTreeSet; + +use chrono::{Local, TimeZone}; +use serde_json::Value; + +use crate::concepts::{Match, Time}; + +/// Tries to convert a [`Value`] into a [`String`] +pub fn to_string(val: &Value) -> Result { + Ok(val.as_str().ok_or("not a string")?.to_owned()) +} + +/// Tries to convert a [`Value`] into a [`Time`] +pub fn to_time(val: &Value) -> Result { + Ok(Local + .timestamp_millis_opt(val.as_i64().ok_or("not a number")?) + .single() + .ok_or("not a valid timestamp")?) +} + +/// Tries to convert a [`Value`] into a [`Match`] +pub fn to_match(val: &Value) -> Result { + val.as_array() + .ok_or("not an array")? + .into_iter() + .map(|v| to_string(v)) + .collect() +} + +/// Tries to convert a [`Value`] into a [`BTreeSet