Get the value at the given key. Returns `None` if the key isn't present.
(&self, key: Self::Key)
| 158 | /// |
| 159 | /// Returns `None` if the key isn't present. |
| 160 | fn get(&self, key: Self::Key) -> Option<TableValueRef<Self::Value, SmallVec<[u8; 64]>>> { |
| 161 | let key_raw = key.into_raw(); |
| 162 | |
| 163 | // Check cache first if caching is enabled |
| 164 | if Self::CACHE_SIZE > 0 { |
| 165 | let mut cache = self.cache().lock().unwrap(); |
| 166 | if let Some(cached_value) = cache.get(key_raw.as_ref()) { |
| 167 | let value = SmallVec::from_slice(cached_value); |
| 168 | return Some(TableValueRef::new(value)); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Cache miss, get from RocksDB |
| 173 | let mut opts = rocksdb::ReadOptions::default(); |
| 174 | opts.set_readahead_size(0); |
| 175 | opts.set_verify_checksums(false); |
| 176 | let raw = self.raw().get_pinned_opt(key_raw.as_ref(), &opts); |
| 177 | let raw = raw.expect("DB IO error")?; |
| 178 | |
| 179 | // Store in cache if caching is enabled |
| 180 | if Self::CACHE_SIZE > 0 { |
| 181 | let mut cache = self.cache().lock().unwrap(); |
| 182 | cache.put(key_raw.as_ref().to_vec(), raw.as_ref().to_vec()); |
| 183 | } |
| 184 | |
| 185 | let value = SmallVec::from_slice(raw.as_ref()); |
| 186 | Some(TableValueRef::new(value)) |
| 187 | } |
| 188 | |
| 189 | /// Checks whether the given key exists in the DB. |
| 190 | fn contains_key(&self, key: Self::Key) -> bool { |