From ebfcf66e0034b06a4eae72112c91f2d4ac49fdbc Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Fri, 22 Nov 2024 13:15:58 +0200 Subject: Get memtables working finally --- log_db/src/memtable_secondary.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'log_db/src/memtable_secondary.rs') diff --git a/log_db/src/memtable_secondary.rs b/log_db/src/memtable_secondary.rs index 371cb84..5c6cd92 100644 --- a/log_db/src/memtable_secondary.rs +++ b/log_db/src/memtable_secondary.rs @@ -1,5 +1,7 @@ +use once_cell::sync::Lazy; + use super::*; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashSet}; pub struct SecondaryMemtable { /// Map of records indexed by key. The value is the set of primary key values of records @@ -8,6 +10,8 @@ pub struct SecondaryMemtable { records: BTreeMap, } +static EMPTY_SET: Lazy> = Lazy::new(|| HashSet::new()); + impl SecondaryMemtable { pub fn new() -> SecondaryMemtable { SecondaryMemtable { @@ -15,15 +19,26 @@ impl SecondaryMemtable { } } - pub fn set(&mut self, key: &IndexableValue, value: &IndexableValue) { - unimplemented!(); + pub fn set(&mut self, key: &IndexableValue, value: &LogKey) { + match self.records.get_mut(key) { + Some(set) => { + set.insert(value.clone()); + } + None => { + self.records + .insert(key.clone(), LogKeySet::new_with_initial(&value)); + } + }; } - pub fn set_all(&mut self, key: &IndexableValue, values: &LogKeySet) { - unimplemented!(); + pub fn replace(&mut self, key: &IndexableValue, values: &LogKeySet) { + self.records.insert(key.clone(), values.clone()); } - pub fn find_all(&self, key: &IndexableValue) -> &LogKeySet { - unimplemented!(); + pub fn find_all(&self, key: &IndexableValue) -> &HashSet { + match self.records.get(key) { + Some(set) => set.log_keys(), + None => &EMPTY_SET, + } } } -- cgit v1.3