project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use crate::sync::Mutex; |
| 2 | use bytes::Bytes; |
| 3 | use loro_kv_store::{mem_store::MemKvConfig, MemKvStore}; |
| 4 | use std::{collections::BTreeSet, ops::Bound, sync::Arc}; |
| 5 | |
| 6 | use crate::kv_store::KvStore; |
| 7 | |
| 8 | /// This thin wrapper aims to limit the ability to modify the kv store and make |
| 9 | /// it easy to find all the modifications. |
| 10 | pub(crate) struct KvWrapper { |
| 11 | kv: Arc<Mutex<dyn KvStore>>, |
| 12 | } |
| 13 | |
| 14 | impl Clone for KvWrapper { |
| 15 | /// Deep clone the inner kv store. |
| 16 | fn clone(&self) -> Self { |
| 17 | Self { |
| 18 | kv: self.kv.lock().clone_store(), |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl KvWrapper { |
| 24 | pub fn new_mem() -> Self { |
| 25 | Self { |
| 26 | // kv: Arc::new(Mutex::new(BTreeMap::new())), |
| 27 | kv: Arc::new(Mutex::new(MemKvStore::new( |
| 28 | // set false because it's depended by GC snapshot's import & export |
| 29 | MemKvConfig::default().should_encode_none(false), |
| 30 | ))), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub fn arc_clone(&self) -> Self { |
| 35 | Self { |
| 36 | kv: self.kv.clone(), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn import(&self, bytes: Bytes) -> Result<(), String> { |
| 41 | let mut kv = self.kv.lock(); |
| 42 | // Snapshot bytes are external input. The document-level checksum does not replace |
| 43 | // validation of the checksums embedded in each SSTable block: a producer can emit a |
| 44 | // self-consistent outer envelope containing an invalid inner table. |
| 45 | kv.import_all(bytes) |
| 46 | } |
| 47 | |
| 48 | pub fn export(&self) -> Bytes { |
| 49 | let mut kv = self.kv.lock(); |
| 50 | kv.export_all() |
| 51 | } |
| 52 | |
| 53 | pub fn get(&self, key: &[u8]) -> Option<Bytes> { |
| 54 | let kv = self.kv.lock(); |
| 55 | kv.get(key) |
| 56 | } |
| 57 | |
| 58 | pub(crate) fn keys(&self) -> BTreeSet<Vec<u8>> { |
| 59 | let kv = self.kv.lock(); |
| 60 | kv.scan(Bound::Unbounded, Bound::Unbounded) |
| 61 | .map(|(k, _)| k.to_vec()) |
| 62 | .collect() |
| 63 | } |
| 64 | |
| 65 | /// Snapshot all entries while holding only the KV lock. |
| 66 | /// |
| 67 | /// Callers that also need the arena lock must use this instead of entering arena code from |
| 68 | /// inside a KV-locked closure. Lazy arena parent resolution can acquire this same KV lock. |
| 69 | pub(crate) fn scan_all_entries(&self) -> Vec<(Bytes, Bytes)> { |
| 70 | let kv = self.kv.lock(); |
| 71 | kv.scan(Bound::Unbounded, Bound::Unbounded).collect() |
| 72 | } |
| 73 | |
| 74 | /// Snapshot the entries whose keys are in `[lower, upper)` while holding |
| 75 | /// only the KV lock. See [`Self::scan_all_entries`] for the lock-ordering |
| 76 | /// rationale. |
| 77 | pub(crate) fn scan_range_entries(&self, lower: &[u8], upper: &[u8]) -> Vec<(Bytes, Bytes)> { |
| 78 | let kv = self.kv.lock(); |
| 79 | kv.scan(Bound::Included(lower), Bound::Excluded(upper)) |
| 80 | .collect() |
| 81 | } |
| 82 | |
| 83 | pub(crate) fn scan_all_keys(&self) -> Vec<Bytes> { |
| 84 | let kv = self.kv.lock(); |
| 85 | kv.scan(Bound::Unbounded, Bound::Unbounded) |
| 86 | .map(|(k, _)| k) |
| 87 | .collect() |
| 88 | } |
| 89 | |
| 90 | pub fn set_all(&self, updates: Vec<(Bytes, Bytes)>) { |
| 91 | let mut kv = self.kv.lock(); |
| 92 | for (k, v) in updates { |
| 93 | kv.set(&k, v); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | #[allow(unused)] |
| 98 | pub(crate) fn contains_key(&self, key: &[u8]) -> bool { |
| 99 | self.kv.lock().contains_key(key) |
| 100 | } |
| 101 | |
| 102 | pub(crate) fn remove_same(&self, old_kv: &KvWrapper) { |
| 103 | if Arc::ptr_eq(&self.kv, &old_kv.kv) { |
| 104 | let mut this = self.kv.lock(); |
| 105 | let keys = this |
| 106 | .scan(Bound::Unbounded, Bound::Unbounded) |
| 107 | .map(|(k, _)| k) |
| 108 | .collect::<Vec<_>>(); |
| 109 | for k in keys { |
| 110 | this.remove(&k); |
| 111 | } |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | let other = old_kv.kv.lock(); |
| 116 | let mut this = self.kv.lock(); |
| 117 | for (k, v) in other.scan(Bound::Unbounded, Bound::Unbounded) { |
| 118 | if this.get(&k) == Some(v) { |
| 119 | this.remove(&k); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | pub(crate) fn remove(&self, k: &[u8]) -> Option<Bytes> { |
| 125 | self.kv.lock().remove(k) |
| 126 | } |
| 127 | |
| 128 | /// Remove all keys not in the given set |
| 129 | pub(crate) fn retain_keys(&self, keys: &BTreeSet<Vec<u8>>) { |
| 130 | let mut kv = self.kv.lock(); |
| 131 | let mut to_remove = BTreeSet::new(); |
| 132 | for (k, _) in kv.scan(Bound::Unbounded, Bound::Unbounded) { |
| 133 | if !keys.contains(&*k) { |
| 134 | to_remove.insert(k); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | for k in to_remove { |
| 139 | kv.remove(&k); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | pub(crate) fn insert(&self, k: &[u8], v: Bytes) { |
| 144 | self.kv.lock().set(k, v); |
| 145 | } |
| 146 | |
| 147 | pub(crate) fn is_empty(&self) -> bool { |
| 148 | self.kv.lock().is_empty() |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | #[cfg(test)] |
| 153 | mod tests { |
| 154 | use super::*; |
| 155 | |
| 156 | #[test] |
| 157 | fn set_all_applies_precollected_updates() { |
| 158 | let kv = KvWrapper::new_mem(); |
| 159 | |
| 160 | kv.set_all(vec![( |
| 161 | Bytes::from_static(b"key"), |
| 162 | Bytes::from_static(b"value"), |
| 163 | )]); |
| 164 | assert_eq!(kv.get(b"key"), Some(Bytes::from_static(b"value"))); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn remove_same_streams_different_stores() { |
| 169 | let kv = KvWrapper::new_mem(); |
| 170 | let old_kv = KvWrapper::new_mem(); |
| 171 | kv.insert(b"same", Bytes::from_static(b"value")); |
| 172 | kv.insert(b"changed", Bytes::from_static(b"new")); |
| 173 | old_kv.insert(b"same", Bytes::from_static(b"value")); |
| 174 | old_kv.insert(b"changed", Bytes::from_static(b"old")); |
| 175 | |
| 176 | kv.remove_same(&old_kv); |
| 177 | |
| 178 | assert_eq!(kv.get(b"same"), None); |
| 179 | assert_eq!(kv.get(b"changed"), Some(Bytes::from_static(b"new"))); |
| 180 | } |
| 181 | } |