Skip to content

Commit 5fcdf50

Browse files
authored
feat: extend and extend_ref (#103)
* feat: extend and extend_ref * lint: clippy * lint: fmt
1 parent 7840c2a commit 5fcdf50

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trevm"
3-
version = "0.20.8"
3+
version = "0.20.9"
44
rust-version = "1.83.0"
55
edition = "2021"
66
authors = ["init4"]

src/db/cow/mod.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,9 @@ where
129129
/// - Contracts are overridden with outer contracts
130130
/// - Block hashes are overridden with outer block hashes
131131
pub fn flatten(self) -> Db {
132-
let Self { cache: Cache { accounts, contracts, logs, block_hashes }, mut inner } = self;
132+
let Self { cache, mut inner } = self;
133133

134-
let inner_cache = inner.cache_mut();
135-
136-
inner_cache.accounts.extend(accounts);
137-
inner_cache.contracts.extend(contracts);
138-
inner_cache.logs.extend(logs);
139-
inner_cache.block_hashes.extend(block_hashes);
134+
inner.extend(cache);
140135
inner
141136
}
142137
}
@@ -153,14 +148,9 @@ where
153148
/// - Contracts are overridden with outer contracts
154149
/// - Block hashes are overridden with outer block hashes
155150
pub fn try_flatten(self) -> Result<Db, Db::Error> {
156-
let Self { cache: Cache { accounts, contracts, logs, block_hashes }, mut inner } = self;
157-
158-
let inner_cache = inner.try_cache_mut()?;
151+
let Self { cache, mut inner } = self;
159152

160-
inner_cache.accounts.extend(accounts);
161-
inner_cache.contracts.extend(contracts);
162-
inner_cache.logs.extend(logs);
163-
inner_cache.block_hashes.extend(block_hashes);
153+
inner.try_extend(cache)?;
164154
Ok(inner)
165155
}
166156
}

src/db/traits.rs

+66
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ pub trait CachingDb {
154154

155155
/// Deconstruct into the cache
156156
fn into_cache(self) -> Cache;
157+
158+
/// Extend the cache with the given cache by copying data.
159+
///
160+
/// The behavior is as follows:
161+
/// - Accounts are overridden with outer accounts
162+
/// - Contracts are overridden with outer contracts
163+
/// - Logs are appended
164+
/// - Block hashes are overridden with outer block hashes
165+
fn extend_ref(&mut self, cache: &Cache) {
166+
self.cache_mut().accounts.extend(cache.accounts.iter().map(|(k, v)| (*k, v.clone())));
167+
self.cache_mut().contracts.extend(cache.contracts.iter().map(|(k, v)| (*k, v.clone())));
168+
self.cache_mut().logs.extend(cache.logs.iter().cloned());
169+
self.cache_mut().block_hashes.extend(cache.block_hashes.iter().map(|(k, v)| (*k, *v)));
170+
}
171+
172+
/// Extend the cache with the given cache by moving data.
173+
///
174+
/// The behavior is as follows:
175+
/// - Accounts are overridden with outer accounts
176+
/// - Contracts are overridden with outer contracts
177+
/// - Logs are appended
178+
/// - Block hashes are overridden with outer block hashes
179+
fn extend(&mut self, cache: Cache) {
180+
self.cache_mut().accounts.extend(cache.accounts);
181+
self.cache_mut().contracts.extend(cache.contracts);
182+
self.cache_mut().logs.extend(cache.logs);
183+
self.cache_mut().block_hashes.extend(cache.block_hashes);
184+
}
157185
}
158186

159187
impl<Db> CachingDb for CacheDB<Db> {
@@ -184,6 +212,44 @@ pub trait TryCachingDb {
184212

185213
/// Attempt to deconstruct into the cache
186214
fn try_into_cache(self) -> Result<Cache, Self::Error>;
215+
216+
/// Attempt to fold a cache into the database.
217+
///
218+
/// The behavior is as follows:
219+
/// - Accounts are overridden with outer accounts
220+
/// - Contracts are overridden with outer contracts
221+
/// - Logs are appended
222+
/// - Block hashes are overridden with outer block hashes
223+
fn try_extend_ref(&mut self, cache: &Cache) -> Result<(), Self::Error>
224+
where
225+
Self: Sized,
226+
{
227+
let inner_cache = self.try_cache_mut()?;
228+
inner_cache.accounts.extend(cache.accounts.iter().map(|(k, v)| (*k, v.clone())));
229+
inner_cache.contracts.extend(cache.contracts.iter().map(|(k, v)| (*k, v.clone())));
230+
inner_cache.logs.extend(cache.logs.iter().cloned());
231+
inner_cache.block_hashes.extend(cache.block_hashes.iter().map(|(k, v)| (*k, *v)));
232+
Ok(())
233+
}
234+
235+
/// Attempt to extend the cache with the given cache by moving data.
236+
///
237+
/// The behavior is as follows:
238+
/// - Accounts are overridden with outer accounts
239+
/// - Contracts are overridden with outer contracts
240+
/// - Logs are appended
241+
/// - Block hashes are overridden with outer block hashes
242+
fn try_extend(&mut self, cache: Cache) -> Result<(), Self::Error>
243+
where
244+
Self: Sized,
245+
{
246+
let inner_cache = self.try_cache_mut()?;
247+
inner_cache.accounts.extend(cache.accounts);
248+
inner_cache.contracts.extend(cache.contracts);
249+
inner_cache.logs.extend(cache.logs);
250+
inner_cache.block_hashes.extend(cache.block_hashes);
251+
Ok(())
252+
}
187253
}
188254

189255
impl<Db> TryCachingDb for Arc<Db>

0 commit comments

Comments
 (0)