aboutsummaryrefslogtreecommitdiffstats
path: root/log_db/src/memtable_secondary.rs
diff options
context:
space:
mode:
authorJan Tuomi <jan@jantuomi.fi>2024-11-09 22:30:51 +0200
committerJan Tuomi <jan@jantuomi.fi>2024-11-10 00:02:32 +0200
commitbfa8fab553eab1b0daa062010fb2ccf5d365d21f (patch)
treeebbb5575dcf361c176dc721a4d1c2b4183de90fa /log_db/src/memtable_secondary.rs
parent2975980717626b12eb959c1ecd93038b5be1d7c6 (diff)
Refresh indexes on read or at maintenance
Diffstat (limited to 'log_db/src/memtable_secondary.rs')
-rw-r--r--log_db/src/memtable_secondary.rs46
1 files changed, 16 insertions, 30 deletions
diff --git a/log_db/src/memtable_secondary.rs b/log_db/src/memtable_secondary.rs
index 3f9863b..8300ada 100644
--- a/log_db/src/memtable_secondary.rs
+++ b/log_db/src/memtable_secondary.rs
@@ -1,14 +1,15 @@
use super::*;
+use once_cell::sync::Lazy;
use std::collections::BTreeMap;
use std::collections::HashSet;
pub struct SecondaryMemtable {
- /// Map of records indexed by key. The value is the set of primary key values of records
- /// that have the secondary key value. The actual `Record` objects are stored in the
- /// primary memtable, which acts as the shared heap.
- records: BTreeMap<IndexableValue, HashSet<IndexableValue>>,
+ /// Map of records indexed by key. Values are non-empty sets of `LogKey` values.
+ records: BTreeMap<IndexableValue, LogKeySet>,
}
+static EMPTY_SET: Lazy<HashSet<LogKey>> = Lazy::new(|| HashSet::new());
+
impl SecondaryMemtable {
pub fn new() -> SecondaryMemtable {
SecondaryMemtable {
@@ -16,10 +17,12 @@ impl SecondaryMemtable {
}
}
- pub fn set(&mut self, key: &IndexableValue, value: &IndexableValue) {
+ pub fn set(&mut self, key: &IndexableValue, value: &LogKey) {
debug!(
- "Inserting/updating record in secondary memtable with key {:?} = {:?}",
- &key, &value,
+ "Inserting/updating record in secondary memtable with key {:?} = segment {} index {}",
+ &key,
+ &value.segment_num(),
+ &value.index()
);
match self.records.get_mut(key) {
@@ -32,44 +35,27 @@ impl SecondaryMemtable {
}
None => {
debug!("No existing entry found, creating one.");
- let mut set = HashSet::with_capacity(1);
- set.insert(value.clone());
+ let set = LogKeySet::new_with_initial(&value);
self.records.insert(key.clone(), set);
}
}
}
- pub fn set_all(&mut self, key: &IndexableValue, values: &[IndexableValue]) {
+ pub fn set_all(&mut self, key: &IndexableValue, values: &[LogKey]) {
debug!(
"Replacing set of records in secondary memtable with key {:?} ({} values)",
&key,
&values.len(),
);
- let mut set = HashSet::with_capacity(values.len());
- values.iter().for_each(|value| {
- set.insert(value.clone());
- });
-
+ let set = LogKeySet::from_slice(values);
self.records.insert(key.clone(), set);
}
- pub fn find_all(
- &mut self,
- primary_memtable: &PrimaryMemtable,
- key: &IndexableValue,
- ) -> Vec<Record> {
+ pub fn find_all(&mut self, key: &IndexableValue) -> &HashSet<LogKey> {
match self.records.get(key) {
- None => vec![],
- Some(set) => set
- .iter()
- .map(|key| {
- primary_memtable
- .get_without_update(key)
- .expect("Record not found")
- .clone()
- })
- .collect(),
+ Some(set) => set.log_keys(),
+ None => &EMPTY_SET,
}
}
}