aboutsummaryrefslogtreecommitdiff
path: root/src/record/version.rs
blob: a45be4d0fd1e5c566cdbe0367d838c4908c915b7 (plain)
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
//! A version of a record
//!
//! A set of version can be flattened to represent the latest set
//! of changes of a record

use super::Payload;
use std::collections::BTreeMap;

/// An operation that was applied to a version
///
/// 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(String, Payload),
    Delete(String),
}

use self::Operation::{Insert, Delete};

/// Represents a series of operations done in sequence
/// that are applied to a record to preserve history of state
#[derive(Debug, Serialize, Deserialize)]
pub struct Version {
    version: u64,
    ops: Vec<Operation>,
}

impl Version {

    /// Create a simple new 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;
    }

    /// A utility function which merges two versions onto &self
    /// 
    /// - If a key is present in `other`, `self.key` is overwritten
    /// - If a key is missing in `other`, `self.key` is deleted
    /// 
    pub fn merge(&mut self, other: &Version) {

    }
}