Skip to main content

ethrex_storage/backend/
in_memory.rs

1use crate::api::{
2    PrefixResult, StorageBackend, StorageLockedView, StorageReadView, StorageWriteBatch,
3};
4use crate::error::StoreError;
5use rustc_hash::FxHashMap;
6use std::path::Path;
7use std::sync::{Arc, RwLock};
8
9type Table = FxHashMap<Vec<u8>, Vec<u8>>;
10type Database = FxHashMap<&'static str, Table>;
11
12#[derive(Debug)]
13pub struct InMemoryBackend {
14    // RCU-style snapshot store: readers clone the inner Arc and then read lock-free.
15    // Writes run under the outer write lock and use Arc::make_mut for copy-on-write.
16    // If read snapshots are still alive, writes may clone the full Database.
17    inner: Arc<RwLock<Arc<Database>>>,
18}
19
20impl InMemoryBackend {
21    pub fn open() -> Result<Self, StoreError> {
22        Ok(Self {
23            inner: Arc::new(RwLock::new(Arc::new(Database::default()))),
24        })
25    }
26}
27
28impl StorageBackend for InMemoryBackend {
29    fn clear_table(&self, table: &str) -> Result<(), StoreError> {
30        let mut db = self
31            .inner
32            .write()
33            .map_err(|_| StoreError::Custom("Failed to acquire write lock".to_string()))?;
34
35        let db_mut = Arc::make_mut(&mut *db);
36        if let Some(table_ref) = db_mut.get_mut(table) {
37            table_ref.clear();
38        }
39        Ok(())
40    }
41
42    fn begin_read(&self) -> Result<Arc<dyn StorageReadView>, StoreError> {
43        let snapshot = self
44            .inner
45            .read()
46            .map_err(|_| StoreError::Custom("Failed to acquire read lock".to_string()))?
47            .clone();
48        Ok(Arc::new(InMemoryReadTx { snapshot }))
49    }
50
51    fn begin_write(&self) -> Result<Box<dyn StorageWriteBatch + 'static>, StoreError> {
52        Ok(Box::new(InMemoryWriteTx {
53            backend: self.inner.clone(),
54        }))
55    }
56
57    fn begin_locked(
58        &self,
59        table_name: &'static str,
60    ) -> Result<Box<dyn StorageLockedView>, StoreError> {
61        let snapshot = self
62            .inner
63            .read()
64            .map_err(|_| StoreError::Custom("Failed to acquire read lock".to_string()))?
65            .clone();
66        Ok(Box::new(InMemoryLocked {
67            snapshot,
68            table_name,
69        }))
70    }
71
72    fn create_checkpoint(&self, _path: &Path) -> Result<(), StoreError> {
73        // Checkpoints are not supported for the InMemory DB
74        // Silently ignoring the request to create a checkpoint is harmless
75        Ok(())
76    }
77}
78
79pub struct InMemoryLocked {
80    snapshot: Arc<Database>,
81    table_name: &'static str,
82}
83
84impl StorageLockedView for InMemoryLocked {
85    fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, StoreError> {
86        Ok(self
87            .snapshot
88            .get(&self.table_name)
89            .and_then(|table_ref| table_ref.get(key))
90            .cloned())
91    }
92}
93
94pub struct InMemoryReadTx {
95    snapshot: Arc<Database>,
96}
97
98impl StorageReadView for InMemoryReadTx {
99    fn get(&self, table: &str, key: &[u8]) -> Result<Option<Vec<u8>>, StoreError> {
100        Ok(self
101            .snapshot
102            .get(table)
103            .and_then(|table_ref| table_ref.get(key))
104            .cloned())
105    }
106
107    fn prefix_iterator(
108        &self,
109        table: &str,
110        prefix: &[u8],
111    ) -> Result<Box<dyn Iterator<Item = PrefixResult> + '_>, StoreError> {
112        let table_data = self.snapshot.get(table).cloned().unwrap_or_default();
113
114        let mut entries: Vec<(Vec<u8>, Vec<u8>)> = table_data
115            .into_iter()
116            .filter(|(key, _)| key.starts_with(prefix))
117            .collect();
118        entries.sort_unstable_by(|(left, _), (right, _)| left.cmp(right));
119
120        let results: Vec<PrefixResult> = entries
121            .into_iter()
122            .map(|(k, v)| Ok((k.into_boxed_slice(), v.into_boxed_slice())))
123            .collect();
124
125        Ok(Box::new(results.into_iter()))
126    }
127
128    fn first_key(&self, table: &'static str) -> Result<Option<Vec<u8>>, StoreError> {
129        let Some(table_data) = self.snapshot.get(table) else {
130            return Ok(None);
131        };
132        Ok(table_data.keys().min().cloned())
133    }
134
135    fn last_key(&self, table: &'static str) -> Result<Option<Vec<u8>>, StoreError> {
136        let Some(table_data) = self.snapshot.get(table) else {
137            return Ok(None);
138        };
139        Ok(table_data.keys().max().cloned())
140    }
141}
142
143pub struct InMemoryWriteTx {
144    backend: Arc<RwLock<Arc<Database>>>,
145}
146
147impl StorageWriteBatch for InMemoryWriteTx {
148    fn put_batch(
149        &mut self,
150        table: &'static str,
151        batch: Vec<(Vec<u8>, Vec<u8>)>,
152    ) -> Result<(), StoreError> {
153        let mut db = self
154            .backend
155            .write()
156            .map_err(|_| StoreError::Custom("Failed to acquire write lock".to_string()))?;
157
158        // Copy-on-write update of the current snapshot.
159        let db_mut = Arc::make_mut(&mut *db);
160        let table_ref = db_mut.entry(table).or_default();
161
162        for (key, value) in batch {
163            table_ref.insert(key, value);
164        }
165
166        Ok(())
167    }
168
169    fn delete(&mut self, table: &str, key: &[u8]) -> Result<(), StoreError> {
170        let mut db = self
171            .backend
172            .write()
173            .map_err(|_| StoreError::Custom("Failed to acquire write lock".to_string()))?;
174
175        let db_mut = Arc::make_mut(&mut *db);
176        if let Some(table_ref) = db_mut.get_mut(table) {
177            table_ref.remove(key);
178        }
179        Ok(())
180    }
181
182    fn delete_range(
183        &mut self,
184        table: &'static str,
185        start: &[u8],
186        end: &[u8],
187    ) -> Result<(), StoreError> {
188        let mut db = self
189            .backend
190            .write()
191            .map_err(|_| StoreError::Custom("Failed to acquire write lock".to_string()))?;
192
193        let db_mut = Arc::make_mut(&mut *db);
194        if let Some(table_ref) = db_mut.get_mut(table) {
195            table_ref.retain(|k, _| !(k.as_slice() >= start && k.as_slice() < end));
196        }
197        Ok(())
198    }
199
200    fn merge(&mut self, table: &'static str, key: &[u8], operand: &[u8]) -> Result<(), StoreError> {
201        // InMemory has no native merge operator, so apply the merge inline.
202        // Only TRANSACTION_LOCATIONS uses merge today; dispatch by table.
203        if table != crate::api::tables::TRANSACTION_LOCATIONS {
204            return Err(StoreError::Custom(format!(
205                "merge not supported for table {table}"
206            )));
207        }
208        let mut db = self
209            .backend
210            .write()
211            .map_err(|_| StoreError::Custom("Failed to acquire write lock".to_string()))?;
212        let db_mut = Arc::make_mut(&mut *db);
213        let table_ref = db_mut.entry(table).or_default();
214        let existing = table_ref.get(key).map(|v| v.as_slice());
215        let merged = crate::store::tx_locations_merge(existing, std::iter::once(operand))
216            .ok_or_else(|| StoreError::Custom("tx_locations_merge returned None".to_string()))?;
217        table_ref.insert(key.to_vec(), merged);
218        Ok(())
219    }
220
221    fn commit(&mut self) -> Result<(), StoreError> {
222        // NOTE: every `put`, `delete`, and `delete_range` above mutates the live
223        // `Arc<Database>` immediately under the write lock, so `commit` is a no-op
224        // and multi-op sequences (e.g. the journal entry + trie writes in
225        // `commit_to_disk`, or the `delete_range` + finalized-number update in
226        // `forkchoice_update_inner`) are not atomic. That's acceptable here: this
227        // backend is RAM-backed (dev/test only), and atomicity only guards crash
228        // recovery — a process death loses all in-memory state anyway, so a
229        // half-applied batch is never observable.
230        Ok(())
231    }
232}