lighthouse/beacon_node/store/src/memory_store.rs
Jimmy Chen 36d8849813 Add commmand for pruning states (#4835)
## Issue Addressed

Closes #4481. 

(Continuation of #4648)

## Proposed Changes

- [x] Add `lighthouse db prune-states`
- [x] Make it work
- [x] Ensure block roots are handled correctly (to be addressed in 4735)
- [x] Check perf on mainnet/Goerli/Gnosis (takes a few seconds max)
- [x] Run block root healing logic (#4875 ) at the beginning
- [x] Add some tests
- [x] Update docs
- [x] Add `--freezer` flag and other improvements to `lighthouse db inspect`

Co-authored-by: Michael Sproul <michael@sigmaprime.io>
Co-authored-by: Jimmy Chen <jimmy@sigmaprime.io>
Co-authored-by: Michael Sproul <micsproul@gmail.com>
2023-11-03 00:12:19 +00:00

117 lines
3.9 KiB
Rust

use crate::{
get_key_for_col, leveldb_store::BytesKey, ColumnIter, ColumnKeyIter, DBColumn, Error,
ItemStore, Key, KeyValueStore, KeyValueStoreOp,
};
use parking_lot::{Mutex, MutexGuard, RwLock};
use std::collections::BTreeMap;
use std::marker::PhantomData;
use types::*;
type DBMap = BTreeMap<BytesKey, Vec<u8>>;
/// A thread-safe `BTreeMap` wrapper.
pub struct MemoryStore<E: EthSpec> {
db: RwLock<DBMap>,
transaction_mutex: Mutex<()>,
_phantom: PhantomData<E>,
}
impl<E: EthSpec> MemoryStore<E> {
/// Create a new, empty database.
pub fn open() -> Self {
Self {
db: RwLock::new(BTreeMap::new()),
transaction_mutex: Mutex::new(()),
_phantom: PhantomData,
}
}
}
impl<E: EthSpec> KeyValueStore<E> for MemoryStore<E> {
/// Get the value of some key from the database. Returns `None` if the key does not exist.
fn get_bytes(&self, col: &str, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
let column_key = BytesKey::from_vec(get_key_for_col(col, key));
Ok(self.db.read().get(&column_key).cloned())
}
/// Puts a key in the database.
fn put_bytes(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), Error> {
let column_key = BytesKey::from_vec(get_key_for_col(col, key));
self.db.write().insert(column_key, val.to_vec());
Ok(())
}
fn put_bytes_sync(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), Error> {
self.put_bytes(col, key, val)
}
fn sync(&self) -> Result<(), Error> {
// no-op
Ok(())
}
/// Return true if some key exists in some column.
fn key_exists(&self, col: &str, key: &[u8]) -> Result<bool, Error> {
let column_key = BytesKey::from_vec(get_key_for_col(col, key));
Ok(self.db.read().contains_key(&column_key))
}
/// Delete some key from the database.
fn key_delete(&self, col: &str, key: &[u8]) -> Result<(), Error> {
let column_key = BytesKey::from_vec(get_key_for_col(col, key));
self.db.write().remove(&column_key);
Ok(())
}
fn do_atomically(&self, batch: Vec<KeyValueStoreOp>) -> Result<(), Error> {
for op in batch {
match op {
KeyValueStoreOp::PutKeyValue(key, value) => {
self.db.write().insert(BytesKey::from_vec(key), value);
}
KeyValueStoreOp::DeleteKey(key) => {
self.db.write().remove(&BytesKey::from_vec(key));
}
}
}
Ok(())
}
fn iter_column_from<K: Key>(&self, column: DBColumn, from: &[u8]) -> ColumnIter<K> {
// We use this awkward pattern because we can't lock the `self.db` field *and* maintain a
// reference to the lock guard across calls to `.next()`. This would be require a
// struct with a field (the iterator) which references another field (the lock guard).
let start_key = BytesKey::from_vec(get_key_for_col(column.as_str(), from));
let col = column.as_str();
let keys = self
.db
.read()
.range(start_key..)
.take_while(|(k, _)| k.remove_column_variable(column).is_some())
.filter_map(|(k, _)| k.remove_column_variable(column).map(|k| k.to_vec()))
.collect::<Vec<_>>();
Box::new(keys.into_iter().filter_map(move |key| {
self.get_bytes(col, &key).transpose().map(|res| {
let k = K::from_bytes(&key)?;
let v = res?;
Ok((k, v))
})
}))
}
fn iter_column_keys<K: Key>(&self, column: DBColumn) -> ColumnKeyIter<K> {
Box::new(self.iter_column(column).map(|res| res.map(|(k, _)| k)))
}
fn begin_rw_transaction(&self) -> MutexGuard<()> {
self.transaction_mutex.lock()
}
fn compact(&self) -> Result<(), Error> {
Ok(())
}
}
impl<E: EthSpec> ItemStore<E> for MemoryStore<E> {}