diff options
author | Katharina Fey <kookie@spacekookie.de> | 2017-12-13 16:00:56 +0100 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2017-12-13 16:00:56 +0100 |
commit | a05d8418a6da1507aa6b533b3441eaf2d020f645 (patch) | |
tree | 819363312cfb64629654076db50b41841b522c7a /src | |
parent | ec9a520c7b7751c5fb0e2e8278a092788e263384 (diff) |
Adding a flatten utility function to Versions
Diffstat (limited to 'src')
-rw-r--r-- | src/record/version.rs | 63 |
1 files changed, 58 insertions, 5 deletions
diff --git a/src/record/version.rs b/src/record/version.rs index 9c5b505..1f0dd4f 100644 --- a/src/record/version.rs +++ b/src/record/version.rs @@ -3,18 +3,71 @@ //! A set of version can be flattened to represent the latest set //! of changes of a record -use std::collections::BTreeMap; use super::Payload; +use std::collections::BTreeMap; /// An operation that was applied to a version -#[derive(Debug, Serialize, Deserialize)] +/// +/// An operation is either an insert or a delete. +/// It also carries a string key and a payload value inside +/// a tuple. These are then summed together as a vector. +/// +/// This means that if data contradicts itself in the same +/// version the later edit (call) will override the previous +#[derive(Debug, Serialize, Deserialize, Clone)] pub enum Operation { - Insert(BTreeMap<String, Payload>), - Delete(BTreeMap<String, Payload>), + Insert(String, Payload), + Delete(String), } +use self::Operation::{Insert, Delete}; + #[derive(Debug, Serialize, Deserialize)] pub struct Version { version: u64, - operation: Operation, + ops: Vec<Operation>, +} + +impl Version { + pub fn new(ver: u64) -> Version { + return Version { + version: ver, + ops: Vec::new(), + }; + } + + /// Take a version full of operations and flatten it to a single + /// binary search tree that can be included into an embedded record + /// + /// Non-mutable on the version itself + pub fn flatten(&self) -> BTreeMap<String, Payload> { + let mut map = BTreeMap::new(); + + /* For all operations, process them in order */ + for op in &self.ops { + + /* Match the operation */ + match op { + &Insert(ref key, ref payload) => { + + /* Match the return to log errors */ + match map.insert(key.clone(), payload.clone()) { + None => {} + _ => println!("Overriding value {}", key), + } + } + &Delete(ref key) => { + + /* Match the return to log errors */ + match map.remove(key) { + None => println!("Failed to apply deletion: key doesn't exists!"), + _ => {} + } + } + } + } + + /* Return the map */ + return map; + } } |