mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-11 21:04:11 +00:00
Caching DB backend
This commit is contained in:
parent
00c4df6153
commit
62260c786d
44
nimbus/db/backends/caching_backend.nim
Normal file
44
nimbus/db/backends/caching_backend.nim
Normal file
@ -0,0 +1,44 @@
|
||||
import ranges, eth_trie, tables, sets
|
||||
import ../storage_types
|
||||
|
||||
type
|
||||
CachingDB* = ref object of RootObj
|
||||
backing: TrieDatabaseRef
|
||||
changed: Table[seq[byte], seq[byte]]
|
||||
deleted: HashSet[seq[byte]]
|
||||
|
||||
proc newCachingDB*(backing: TrieDatabaseRef): CachingDB =
|
||||
result.new()
|
||||
result.backing = backing
|
||||
result.changed = initTable[seq[byte], seq[byte]]()
|
||||
result.deleted = initSet[seq[byte]]()
|
||||
|
||||
proc get*(db: CachingDB, key: openarray[byte]): seq[byte] =
|
||||
let key = @key
|
||||
result = db.changed.getOrDefault(key)
|
||||
if result.len == 0 and key notin db.deleted:
|
||||
result = db.backing.get(key)
|
||||
|
||||
proc put*(db: CachingDB, key, value: openarray[byte]) =
|
||||
let key = @key
|
||||
db.deleted.excl(key)
|
||||
db.changed[key] = @value
|
||||
|
||||
proc contains*(db: CachingDB, key: openarray[byte]): bool =
|
||||
let key = @key
|
||||
result = key in db.changed
|
||||
if not result and key notin db.deleted:
|
||||
result = db.backing.contains(key)
|
||||
|
||||
proc del*(db: CachingDB, key: openarray[byte]) =
|
||||
let key = @key
|
||||
db.changed.del(key)
|
||||
db.deleted.incl(key)
|
||||
|
||||
proc commit*(db: CachingDB) =
|
||||
for k in db.deleted:
|
||||
db.backing.del(k)
|
||||
|
||||
for k, v in db.changed:
|
||||
db.backing.put(k, v)
|
||||
|
@ -11,5 +11,6 @@ import ./test_code_stream,
|
||||
./test_stack,
|
||||
./test_opcode,
|
||||
./test_storage_backends,
|
||||
./test_caching_db_backend,
|
||||
./test_genesis,
|
||||
./test_vm_json
|
||||
|
45
tests/test_caching_db_backend.nim
Normal file
45
tests/test_caching_db_backend.nim
Normal file
@ -0,0 +1,45 @@
|
||||
|
||||
import ../nimbus/db/backends/caching_backend, eth_trie, eth_trie/memdb, unittest
|
||||
|
||||
let
|
||||
key1 = [0.byte, 0, 1]
|
||||
key2 = [0.byte, 0, 2]
|
||||
key3 = [0.byte, 0, 3]
|
||||
key4 = [0.byte, 0, 4]
|
||||
value1 = [1.byte, 0, 1]
|
||||
value2 = [1.byte, 0, 2]
|
||||
value3 = [1.byte, 0, 3]
|
||||
value4 = [1.byte, 0, 4]
|
||||
|
||||
suite "Caching DB backend":
|
||||
test "Basic test":
|
||||
let mdb = newMemDB()
|
||||
mdb.put(key1, value1)
|
||||
mdb.put(key2, value2)
|
||||
let cdb = newCachingDB(trieDB(mdb))
|
||||
check:
|
||||
cdb.get(key1) == @value1
|
||||
cdb.get(key2) == @value2
|
||||
|
||||
cdb.del(key1)
|
||||
check:
|
||||
key1 notin cdb
|
||||
mdb.get(key1) == @value1
|
||||
|
||||
cdb.put(key3, value3)
|
||||
check:
|
||||
cdb.get(key3) == @value3
|
||||
key3 notin mdb
|
||||
|
||||
cdb.put(key4, value4)
|
||||
cdb.del(key4)
|
||||
check(key4 notin cdb)
|
||||
|
||||
cdb.commit()
|
||||
|
||||
check:
|
||||
key1 notin mdb
|
||||
mdb.get(key2) == @value2
|
||||
mdb.get(key3) == @value3
|
||||
key4 notin mdb
|
||||
|
Loading…
x
Reference in New Issue
Block a user