Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit cb9800f

Browse files
Andronik Ordiandvdplm
andauthored
rename inject to drain_transaction_overlay (#11657)
* Drain the transaction overlay * journaldb: rename inject to drain_transaction_overlay Co-authored-by: David Palm <[email protected]>
1 parent 114074c commit cb9800f

File tree

6 files changed

+32
-46
lines changed

6 files changed

+32
-46
lines changed

ethcore/snapshot/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,8 @@ impl StateRebuilder {
435435
}
436436
}
437437

438-
let backing = self.db.backing().clone();
439-
let mut batch = backing.transaction();
440-
// Drain the transaction overlay and put the data into the batch.
441-
self.db.inject(&mut batch)?;
442-
backing.write_buffered(batch);
443-
438+
let batch = self.db.drain_transaction_overlay()?;
439+
self.db.backing().write(batch)?;
444440
Ok(())
445441
}
446442

util/journaldb/src/archivedb.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ impl JournalDB for ArchiveDB {
142142
Ok(0)
143143
}
144144

145-
fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
146-
let mut inserts = 0usize;
147-
let mut deletes = 0usize;
145+
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction> {
146+
let mut batch = DBTransaction::new();
148147

149148
for i in self.overlay.drain() {
150149
let (key, (value, rc)) = i;
@@ -153,19 +152,17 @@ impl JournalDB for ArchiveDB {
153152
return Err(error_key_already_exists(&key));
154153
}
155154
batch.put(self.column, key.as_bytes(), &value);
156-
inserts += 1;
157155
}
158156
if rc < 0 {
159157
assert!(rc == -1);
160158
if self.backing.get(self.column, key.as_bytes())?.is_none() {
161159
return Err(error_negatively_reference_hash(&key));
162160
}
163161
batch.delete(self.column, key.as_bytes());
164-
deletes += 1;
165162
}
166163
}
167164

168-
Ok((inserts + deletes) as u32)
165+
Ok(batch)
169166
}
170167

171168
fn latest_era(&self) -> Option<u64> { self.latest_era }
@@ -209,7 +206,7 @@ mod tests {
209206
use hash_db::{HashDB, EMPTY_PREFIX};
210207
use super::*;
211208
use kvdb_memorydb;
212-
use crate::{JournalDB, inject_batch, commit_batch};
209+
use crate::{JournalDB, drain_overlay, commit_batch};
213210

214211
#[test]
215212
fn insert_same_in_fork() {
@@ -463,11 +460,11 @@ mod tests {
463460
fn inject() {
464461
let mut jdb = ArchiveDB::new(Arc::new(kvdb_memorydb::create(1)), 0);
465462
let key = jdb.insert(EMPTY_PREFIX, b"dog");
466-
inject_batch(&mut jdb).unwrap();
463+
drain_overlay(&mut jdb).unwrap();
467464

468465
assert_eq!(jdb.get(&key, EMPTY_PREFIX).unwrap(), b"dog".to_vec());
469466
jdb.remove(&key, EMPTY_PREFIX);
470-
inject_batch(&mut jdb).unwrap();
467+
drain_overlay(&mut jdb).unwrap();
471468

472469
assert!(jdb.get(&key, EMPTY_PREFIX).is_none());
473470
}

util/journaldb/src/earlymergedb.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,9 @@ impl JournalDB for EarlyMergeDB {
474474
Ok(0)
475475
}
476476

477-
fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
478-
let mut ops = 0;
477+
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction> {
478+
let mut batch = DBTransaction::new();
479479
for (key, (value, rc)) in self.overlay.drain() {
480-
if rc != 0 { ops += 1 }
481-
482480
match rc {
483481
0 => {}
484482
1 => {
@@ -497,7 +495,7 @@ impl JournalDB for EarlyMergeDB {
497495
}
498496
}
499497

500-
Ok(ops)
498+
Ok(batch)
501499
}
502500

503501
fn consolidate(&mut self, with: super::MemoryDB) {
@@ -529,7 +527,7 @@ mod tests {
529527
use hash_db::{HashDB, EMPTY_PREFIX};
530528
use super::*;
531529
use kvdb_memorydb;
532-
use crate::{inject_batch, commit_batch};
530+
use crate::{drain_overlay, commit_batch};
533531

534532
#[test]
535533
fn insert_same_in_fork() {
@@ -1050,11 +1048,11 @@ mod tests {
10501048
fn inject() {
10511049
let mut jdb = new_db();
10521050
let key = jdb.insert(EMPTY_PREFIX, b"dog");
1053-
inject_batch(&mut jdb).unwrap();
1051+
drain_overlay(&mut jdb).unwrap();
10541052

10551053
assert_eq!(jdb.get(&key, EMPTY_PREFIX).unwrap(), b"dog".to_vec());
10561054
jdb.remove(&key, EMPTY_PREFIX);
1057-
inject_batch(&mut jdb).unwrap();
1055+
drain_overlay(&mut jdb).unwrap();
10581056

10591057
assert!(jdb.get(&key, EMPTY_PREFIX).is_none());
10601058
}

util/journaldb/src/lib.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,8 @@ pub trait JournalDB: HashDB<KeccakHasher, DBValue> {
7272
/// Commit all queued insert and delete operations without affecting any journalling -- this requires that all insertions
7373
/// and deletions are indeed canonical and will likely lead to an invalid database if that assumption is violated.
7474
///
75-
/// Any keys or values inserted or deleted must be completely independent of those affected
76-
/// by any previous `commit` operations. Essentially, this means that `inject` can be used
77-
/// either to restore a state to a fresh database, or to insert data which may only be journalled
78-
/// from this point onwards.
79-
fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32>;
75+
/// Returns a transaction to be committed.
76+
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction>;
8077

8178
/// State data query
8279
fn state(&self, _id: &H256) -> Option<Bytes>;
@@ -213,12 +210,11 @@ pub fn new_memory_db() -> MemoryDB {
213210
MemoryDB::from_null_node(&rlp::NULL_RLP, rlp::NULL_RLP.as_ref().into())
214211
}
215212

216-
#[cfg(test)]
217213
/// Inject all changes in a single batch.
218-
pub fn inject_batch(jdb: &mut dyn JournalDB) -> io::Result<u32> {
219-
let mut batch = jdb.backing().transaction();
220-
let res = jdb.inject(&mut batch)?;
221-
jdb.backing().write(batch).map(|_| res).map_err(Into::into)
214+
#[cfg(test)]
215+
pub fn drain_overlay(jdb: &mut dyn JournalDB) -> io::Result<()> {
216+
let batch = jdb.drain_transaction_overlay()?;
217+
jdb.backing().write(batch).map_err(Into::into)
222218
}
223219

224220
/// Commit all changes in a single batch

util/journaldb/src/overlayrecentdb.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,9 @@ impl JournalDB for OverlayRecentDB {
397397
Ok(ops as u32)
398398
}
399399

400-
fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
401-
let mut ops = 0;
400+
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction> {
401+
let mut batch = DBTransaction::new();
402402
for (key, (value, rc)) in self.transaction_overlay.drain() {
403-
if rc != 0 { ops += 1 }
404-
405403
match rc {
406404
0 => {}
407405
_ if rc > 0 => {
@@ -417,7 +415,7 @@ impl JournalDB for OverlayRecentDB {
417415
}
418416
}
419417

420-
Ok(ops)
418+
Ok(batch)
421419
}
422420

423421
fn state(&self, key: &H256) -> Option<Bytes> {
@@ -507,7 +505,7 @@ mod tests {
507505
use super::*;
508506
use hash_db::{HashDB, EMPTY_PREFIX};
509507
use kvdb_memorydb;
510-
use crate::{JournalDB, inject_batch, commit_batch};
508+
use crate::{JournalDB, drain_overlay, commit_batch};
511509

512510
fn new_db() -> OverlayRecentDB {
513511
let backing = Arc::new(kvdb_memorydb::create(1));
@@ -1026,11 +1024,11 @@ mod tests {
10261024
fn inject() {
10271025
let mut jdb = new_db();
10281026
let key = jdb.insert(EMPTY_PREFIX, b"dog");
1029-
inject_batch(&mut jdb).unwrap();
1027+
drain_overlay(&mut jdb).unwrap();
10301028

10311029
assert_eq!(jdb.get(&key, EMPTY_PREFIX).unwrap(), b"dog".to_vec());
10321030
jdb.remove(&key, EMPTY_PREFIX);
1033-
inject_batch(&mut jdb).unwrap();
1031+
drain_overlay(&mut jdb).unwrap();
10341032

10351033
assert!(jdb.get(&key, EMPTY_PREFIX).is_none());
10361034
}

util/journaldb/src/refcounteddb.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,13 @@ impl JournalDB for RefCountedDB {
193193
Ok(r)
194194
}
195195

196-
fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
196+
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction> {
197197
self.inserts.clear();
198198
for remove in self.removes.drain(..) {
199199
self.forward.remove(&remove, EMPTY_PREFIX);
200200
}
201-
self.forward.commit_to_batch(batch)
201+
let mut batch = DBTransaction::new();
202+
self.forward.commit_to_batch(&mut batch).map(|_| batch)
202203
}
203204

204205
fn consolidate(&mut self, mut with: super::MemoryDB) {
@@ -224,7 +225,7 @@ mod tests {
224225
use hash_db::{HashDB, EMPTY_PREFIX};
225226
use super::*;
226227
use kvdb_memorydb;
227-
use crate::{JournalDB, inject_batch, commit_batch};
228+
use crate::{JournalDB, drain_overlay, commit_batch};
228229

229230
fn new_db() -> RefCountedDB {
230231
let backing = Arc::new(kvdb_memorydb::create(1));
@@ -338,11 +339,11 @@ mod tests {
338339
fn inject() {
339340
let mut jdb = new_db();
340341
let key = jdb.insert(EMPTY_PREFIX, b"dog");
341-
inject_batch(&mut jdb).unwrap();
342+
drain_overlay(&mut jdb).unwrap();
342343

343344
assert_eq!(jdb.get(&key, EMPTY_PREFIX).unwrap(), b"dog".to_vec());
344345
jdb.remove(&key, EMPTY_PREFIX);
345-
inject_batch(&mut jdb).unwrap();
346+
drain_overlay(&mut jdb).unwrap();
346347

347348
assert!(jdb.get(&key, EMPTY_PREFIX).is_none());
348349
}

0 commit comments

Comments
 (0)