diff options
author | Alex Auvolat <alex@adnab.me> | 2020-04-23 18:16:33 +0000 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-04-23 18:16:52 +0000 |
commit | 44a1089d9569b442c098c2ceebb3f691816e52d2 (patch) | |
tree | 58c5ebf9965e47a94bddff34a42636c6c30d1758 /src/store/version_table.rs | |
parent | c9c6b0dbd41e20d19b91c6615c46da6f45925bca (diff) | |
download | garage-44a1089d9569b442c098c2ceebb3f691816e52d2.tar.gz garage-44a1089d9569b442c098c2ceebb3f691816e52d2.zip |
Make table objects slightly more fool-proof; add key table
Diffstat (limited to 'src/store/version_table.rs')
-rw-r--r-- | src/store/version_table.rs | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/store/version_table.rs b/src/store/version_table.rs index d25a56ca..6d304cda 100644 --- a/src/store/version_table.rs +++ b/src/store/version_table.rs @@ -18,7 +18,7 @@ pub struct Version { // Actual data: the blocks for this version pub deleted: bool, - pub blocks: Vec<VersionBlock>, + blocks: Vec<VersionBlock>, // Back link to bucket+key so that we can figure if // this was deleted later on @@ -26,6 +26,42 @@ pub struct Version { pub key: String, } +impl Version { + pub fn new( + uuid: UUID, + bucket: String, + key: String, + deleted: bool, + blocks: Vec<VersionBlock>, + ) -> Self { + let mut ret = Self { + uuid, + deleted, + blocks: vec![], + bucket, + key, + }; + for b in blocks { + ret.add_block(b) + .expect("Twice the same VersionBlock in Version constructor"); + } + ret + } + /// Adds a block if it wasn't already present + pub fn add_block(&mut self, new: VersionBlock) -> Result<(), ()> { + match self.blocks.binary_search_by(|b| b.offset.cmp(&new.offset)) { + Err(i) => { + self.blocks.insert(i, new); + Ok(()) + } + Ok(_) => Err(()), + } + } + pub fn blocks(&self) -> &[VersionBlock] { + &self.blocks[..] + } +} + #[derive(PartialEq, Clone, Debug, Serialize, Deserialize)] pub struct VersionBlock { pub offset: u64, |