diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-01-06 15:51:45 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-01-06 15:51:45 +0200 |
| commit | b5abd6dfea8b26b73cc9b489695ffdd6de6fe1cd (patch) | |
| tree | cb3ae25796dd1a0ebccbc5796a7374a7ff9ab8ff /log_db/src/memtable_secondary.rs | |
| parent | 4f80ddfccc4339b12c97648a1a00b72b4dfa3ef0 (diff) | |
Implement remove
Diffstat (limited to 'log_db/src/memtable_secondary.rs')
| -rw-r--r-- | log_db/src/memtable_secondary.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/log_db/src/memtable_secondary.rs b/log_db/src/memtable_secondary.rs index 4479516..a7a9a8a 100644 --- a/log_db/src/memtable_secondary.rs +++ b/log_db/src/memtable_secondary.rs @@ -38,7 +38,41 @@ impl SecondaryMemtable { } } - pub fn remove(&mut self, key: &IndexableValue) -> Option<LogKeySet> { + // Remove all log keys associated with the given key + pub fn remove_all(&mut self, key: &IndexableValue) -> Option<LogKeySet> { self.records.remove(key) } + + // Remove a single log key associated with the given key. Returns `true` + // if the log key existed and was removed, `false` otherwise. + pub fn remove(&mut self, key: &IndexableValue, log_key: &LogKey) -> bool { + let set = match self.records.get_mut(key) { + Some(set) => set, + None => return false, + }; + if set.len() == 1 && set.contains(log_key) { + self.records.remove(key); + true + } else { + return match set.remove(log_key) { + Ok(_) => true, + Err(LogKeySetError::NotFoundError) => false, + Err(e) => panic!("{:?}", e), + }; + } + } + + // Remove all log keys associated with the given log key + // Note: This is a linear time operation, prefer using the `remove` method + // if you know the secondary key associated with the log key. + pub fn scan_remove(&mut self, log_key: &LogKey) -> u64 { + let mut removed = 0; + self.records.iter_mut().for_each(|(_, set)| { + if let Ok(_) = set.remove(&log_key) { + removed += 1; + } + }); + + removed + } } |
