Skip to content

Commit b67a4e5

Browse files
Seulgi Kimsgkim126
authored andcommitted
Remove HashDB::emplace()
The emplace method is hard to use safely. The HashDB's key must be the blake2b hash of the value, but it might not be when the user uses emplace because the emplace passes the key.
1 parent 4bc81b4 commit b67a4e5

File tree

3 files changed

+4
-31
lines changed

3 files changed

+4
-31
lines changed

util/hashdb/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ pub trait HashDB: AsHashDB + Send + Sync {
4040
/// is considered dead.
4141
fn insert(&mut self, value: &[u8]) -> H256;
4242

43-
/// Like `insert()` , except you provide the key and the data is all moved.
44-
fn emplace(&mut self, key: H256, value: DBValue);
45-
4643
/// Remove a datum previously inserted. Insertions can be "owed" such that the same number of `insert()`s may
4744
/// happen without the data being eventually being inserted into the DB. It can be "owed" more than once.
4845
fn remove(&mut self, key: &H256);

util/journaldb/src/archivedb.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ impl HashDB for ArchiveDB {
9696
self.overlay.insert(value)
9797
}
9898

99-
fn emplace(&mut self, key: H256, value: DBValue) {
100-
self.overlay.emplace(key, value);
101-
}
102-
10399
fn remove(&mut self, key: &H256) {
104100
self.overlay.remove(key);
105101
}
@@ -357,15 +353,14 @@ mod tests {
357353
#[test]
358354
fn reopen() {
359355
let shared_db = Arc::new(kvdb_memorydb::create(0));
360-
let bar_hash = H256::random();
361356

362-
let foo_hash = {
357+
let (foo_hash, bar_hash) = {
363358
let mut jdb = ArchiveDB::new(shared_db.clone(), None);
364359
// history is 1
365360
let foo_hash = jdb.insert(b"foo");
366-
jdb.emplace(bar_hash, b"bar".to_vec());
361+
let bar_hash = jdb.insert(b"bar");
367362
jdb.commit_batch(0, &blake256(b"0"), None).unwrap();
368-
foo_hash
363+
(foo_hash, bar_hash)
369364
};
370365

371366
{

util/memorydb/src/lib.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -229,25 +229,6 @@ impl HashDB for MemoryDB {
229229
key
230230
}
231231

232-
fn emplace(&mut self, key: H256, value: DBValue) {
233-
if *value == NULL_RLP {
234-
return
235-
}
236-
237-
match self.data.entry(key) {
238-
Entry::Occupied(mut entry) => {
239-
let &mut (ref mut old_value, ref mut rc) = entry.get_mut();
240-
if *rc <= 0 {
241-
*old_value = value;
242-
}
243-
*rc += 1;
244-
}
245-
Entry::Vacant(entry) => {
246-
entry.insert((value, 1));
247-
}
248-
}
249-
}
250-
251232
fn remove(&mut self, key: &H256) {
252233
if key == &BLAKE_NULL_RLP {
253234
return
@@ -308,7 +289,7 @@ mod tests {
308289
main.remove(&remove_key);
309290

310291
let insert_key = other.insert(b"arf");
311-
main.emplace(insert_key, b"arf".to_vec());
292+
main.insert(b"arf");
312293

313294
let negative_remove_key = other.insert(b"negative");
314295
other.remove(&negative_remove_key); // ref cnt: 0

0 commit comments

Comments
 (0)