aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/record/mod.rs26
-rw-r--r--src/record/version.rs40
2 files changed, 41 insertions, 25 deletions
diff --git a/src/record/mod.rs b/src/record/mod.rs
index ddec77c..0ce20c5 100644
--- a/src/record/mod.rs
+++ b/src/record/mod.rs
@@ -1,7 +1,7 @@
//! Lockchain record handling module
-//!
+//!
//! A record is a set of key-value store values with a header
-//!
+//!
mod version;
use self::version::{Version, Operation};
@@ -20,12 +20,11 @@ pub enum Payload {
}
/// Describes the header of a record file
-///
+///
/// This part of the record should not be considered safe as it is
/// serialised and cached multiple times.
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Header {
-
/// The name of this record
pub name: String,
@@ -47,13 +46,12 @@ pub struct Header {
}
/// Represents a record inside lockchain
-///
+///
/// A record consists of a header and a body. The body has built-in
/// versioning. The different versions are then flattened to create the
/// latest stage of a record which is exposed to the outside.
#[derive(Debug, Serialize, Deserialize)]
pub struct Record {
-
/// The header for this record
pub header: Header,
@@ -62,7 +60,6 @@ pub struct Record {
}
impl Header {
-
/// Create a new header with a name of a category
pub fn new(name: String, category: String) -> Header {
let me = Header {
@@ -85,7 +82,6 @@ impl PartialEq for Record {
}
impl Record {
-
/// Create a new record
pub fn new(name: &str, category: &str) -> Record {
return Record {
@@ -102,7 +98,21 @@ impl Record {
/// Apply a version to the current record
pub fn apply_version(&mut self, ver: Version) {
+ self.body.push(ver);
+ }
+ /// Flatten all versions down and return a map of *current* data
+ /// stored in this record.
+ ///
+ /// Note: currently the data presented in this map is not sorted
+ /// in the way that the developer intended (insertion-order)
+ pub fn get_data(&self) -> BTreeMap<String, Payload> {
+ let mut first: Version = self.body[0].clone();
+ for version in &self.body[1..] {
+ first.merge(version);
+ }
+
+ return first.flatten();
}
/// Set a simple key-value pair
diff --git a/src/record/version.rs b/src/record/version.rs
index a45be4d..68bb061 100644
--- a/src/record/version.rs
+++ b/src/record/version.rs
@@ -5,6 +5,7 @@
use super::Payload;
use std::collections::BTreeMap;
+use std::ops;
/// An operation that was applied to a version
///
@@ -24,14 +25,14 @@ 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)]
+#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Version {
version: u64,
ops: Vec<Operation>,
}
impl Version {
-
+
/// Create a simple new version
pub fn new(ver: u64) -> Version {
return Version {
@@ -53,20 +54,10 @@ impl Version {
/* 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),
- }
+ map.insert(key.clone(), payload.clone()).unwrap();
}
&Delete(ref key) => {
-
- /* Match the return to log errors */
- match map.remove(key) {
- None => println!("Failed to apply deletion: key doesn't exists!"),
- _ => {}
- }
+ map.remove(key).unwrap();
}
}
}
@@ -76,11 +67,26 @@ impl Version {
}
/// 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) {
-
+ for op in &other.ops {
+ self.ops.push(op.clone());
+ }
}
}
+
+
+impl ops::Add<Version> for Version {
+ type Output = Version;
+
+ /// A rather nice short-hand for calling version.merge(other_version)
+ /// to allow you to call `version += other_version`
+ fn add(self, other: Version) -> Version {
+ let mut s = self.clone();
+ s.merge(&other);
+ return s;
+ }
+} \ No newline at end of file