-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
trie -> kv store #677
Merged
trie -> kv store #677
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Simple Key-Value store database interface | ||
|
||
import | ||
tables, hashes, sets | ||
|
||
type | ||
MemoryStoreRef* = ref object of RootObj | ||
records: Table[seq[byte], seq[byte]] | ||
|
||
DataProc* = proc(val: openArray[byte]) | ||
PutProc = proc (db: RootRef, key, val: openArray[byte]) {.gcsafe.} | ||
GetProc = proc (db: RootRef, key: openArray[byte], onData: DataProc): bool {.gcsafe.} | ||
DelProc = proc (db: RootRef, key: openArray[byte]) {.gcsafe.} | ||
ContainsProc = proc (db: RootRef, key: openArray[byte]): bool {.gcsafe.} | ||
|
||
KVStoreRef* = ref object | ||
## Key-Value store virtual interface | ||
obj: RootRef | ||
putProc: PutProc | ||
getProc: GetProc | ||
delProc: DelProc | ||
containsProc: ContainsProc | ||
|
||
template put*(db: KVStoreRef, key, val: openArray[byte]) = | ||
## Store ``value`` at ``key`` - overwrites existing value if already present | ||
db.putProc(db.obj, key, val) | ||
|
||
template get*(db: KVStoreRef, key: openArray[byte], onData: untyped): bool = | ||
## Retrive value at ``key`` and call ``onData`` with the value. The data is | ||
## valid for the duration of the callback. | ||
## ``onData``: ``proc(data: openArray[byte])`` | ||
## returns true if found and false otherwise. | ||
db.getProc(db.obj, key, onData) | ||
|
||
template del*(db: KVStoreRef, key: openArray[byte]) = | ||
## Remove value at ``key`` from store - do nothing if the value is not present | ||
db.delProc(db.obj, key) | ||
|
||
template contains*(db: KVStoreRef, key: openArray[byte]): bool = | ||
## Return true iff ``key`` has a value in store | ||
db.containsProc(db.obj, key) | ||
|
||
proc get*(db: MemoryStoreRef, key: openArray[byte], onData: DataProc): bool = | ||
let key = @key | ||
db.records.withValue(key, v): | ||
onData(v[]) | ||
return true | ||
|
||
proc del*(db: MemoryStoreRef, key: openArray[byte]) = | ||
# TODO: This is quite inefficient and it won't be necessary once | ||
# https://github.com/nim-lang/Nim/issues/7457 is developed. | ||
let key = @key | ||
db.records.del(key) | ||
|
||
proc contains*(db: MemoryStoreRef, key: openArray[byte]): bool = | ||
db.records.contains(@key) | ||
|
||
proc put*(db: MemoryStoreRef, key, val: openArray[byte]) = | ||
# TODO: This is quite inefficient and it won't be necessary once | ||
# https://github.com/nim-lang/Nim/issues/7457 is developed. | ||
let key = @key | ||
db.records[key] = @val | ||
|
||
proc init*(T: type MemoryStoreRef): T = | ||
T( | ||
records: initTable[seq[byte], seq[byte]]() | ||
) | ||
|
||
proc putImpl[T](db: RootRef, key, val: openArray[byte]) = | ||
mixin put | ||
put(T(db), key, val) | ||
|
||
proc getImpl[T](db: RootRef, key: openArray[byte], onData: DataProc): bool = | ||
mixin get | ||
get(T(db), key, onData) | ||
|
||
proc delImpl[T](db: RootRef, key: openArray[byte]) = | ||
mixin del | ||
del(T(db), key) | ||
|
||
proc containsImpl[T](db: RootRef, key: openArray[byte]): bool = | ||
mixin contains | ||
contains(T(db), key) | ||
|
||
func kvStore*[T: RootRef](x: T): KVStoreRef = | ||
mixin del, get, put, contains | ||
|
||
KVStoreRef( | ||
obj: x, | ||
putProc: putImpl[T], | ||
getProc: getImpl[T], | ||
delProc: delImpl[T], | ||
containsProc: containsImpl[T] | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TrieDatabaseRef
may have been named inappropriately, but it's already a simple key-value store. The only difference is that it comes with an additional support for discardable in-memory transactions, which doesn't complicate the API at all when it's not used.I assume the
get
proc signature was changed as an optimisation (to avoid copying the result bytes to a newly allocated sequence). This optimisation could have been added toTrieDatabaseRef
as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it also has logic for maintaining an empty rlp item (thus pulling in the RLP code) and does refcounting in memory, none of which are needed here - this PR is about eliminating surface area for bugs by making sure the code does what it needs to do in the simplest way possible.
get changed for that reason, but there's a semantic change as well which in theory allows for empty values to happen. I do not have enough confidence in the test suite to perform this change on nim-eth itself - that can easily be done by someone more familiar with that code if need be, but with that said nim-beacon-chain and nimbus1 have completely different storage needs - they are likely to continue using different storage engines..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fwiw, a practical outcome of the complexity of
TrieDatabaseRef
is that every implementation of it is faulty in the face of exceptions: leaks, reader/writer transaction blocking - the code raises exceptions but is not written for exception safety