Skip to content
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

sqlite kvstore backend #749

Merged
merged 1 commit into from
Feb 20, 2020
Merged

sqlite kvstore backend #749

merged 1 commit into from
Feb 20, 2020

Conversation

arnetheduck
Copy link
Member

No description provided.

@tersec
Copy link
Contributor

tersec commented Feb 17, 2020

This would resolve #688 and #732

@tersec
Copy link
Contributor

tersec commented Feb 18, 2020

Works for me in my tests so far -- switching to the daemon, it make eth2_network_simulation finalizes just as well as with devel now.

@arnetheduck arnetheduck merged commit c25f8d4 into devel Feb 20, 2020
@delete-merged-branch delete-merged-branch bot deleted the kvstore-sqlite branch February 20, 2020 10:49
@@ -141,3 +141,6 @@
url = https://github.com/status-im/nim-libbacktrace.git
ignore = dirty
branch = master
[submodule "vendor/nim-sqlite3-abi"]
path = vendor/nim-sqlite3-abi
url = https://github.com/arnetheduck/nim-sqlite3-abi.git
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should strive to have all submodules pointing to repos in the status-im org. Otherwise, dealing with any discovered issues becomes harder.


func raiseError(err: cint) {.noreturn.} =
let tmp = sqlite3_errstr(err)
raise (ref SqliteError)(msg: $tmp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have avoided this awkward syntax if SqliteError was defined as ref oject of CatchableError and you used raise newException(SqliteError, $tmp) here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hold on..

exceptions should be non-ref:

https://nim-lang.org/docs/manual.html#exception-handling-custom-exceptions
(manual recommends using Exception as base class btw).

newException turns non-ref exception type into ref - that's why it exists, no?
https://github.com/nim-lang/Nim/blob/devel/lib/system.nim#L1948

one of the often touted reasons to use exceptions is to include additional data - newException prevents you from doing this elegantly - you have to create a partially initialized object then fill it in (somewhat irrelevantly, newException itself is poorly implemented in this regard - it first allocates the exception then fills it in piece by piece)

this syntax is shorter, clearly states that a ref is raised instead of hiding it in a template (ie that heap memory is involved and all the other ref-counting issues) and provides access to all the fields of the error object - seems like a win?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, right.
I guess I have to get used to that syntax until something better comes along.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insertStmt = prepare "INSERT OR REPLACE INTO kvstore(key, value) VALUES (?, ?);":
discard sqlite3_finalize(selectStmt)
deleteStmt = prepare "DELETE FROM kvstore WHERE key = ?;":
discard sqlite3_finalize(selectStmt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a good opportunity to try out Nim's destructors ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd need move support as well, because you'd have to disarm the source when copying the held c pointer, and a way to prevent spurious copies (ie delete copy constructor in C++).. is that part of the package?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The move support would be an additional simplification. For now, we can easily stick to release() procs in the style of std::auto_ptr/unique_ptr.

case v
of SQLITE_ROW:
let
p = cast[ptr UncheckedArray[byte]](sqlite3_column_blob(db.selectStmt, 0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have been nice if the sqlite3_column_blob function was returning ptr UncheckedArray[byte]. That's a more accurate description of the result and no casting will be necessary here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blob returns a void* though - it's technically not an ptr UncheckedArray[byte]*, the byte part is not quite true - though I guess there's no practical difference. it would require manually adjusting what the wrapper generates - once you start doing that, there's a lot you could improve here (ie ptr sqlite3 is not very nim:ish either)

Copy link
Contributor

@zah zah Feb 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this could justify the dependency on a fancier tool such as Nimterop for the purposes of creating the wrapper. I could imagine adding a single rule that changes the return type of all functions ending with "_blob" here:

https://github.com/arnetheduck/nim-sqlite3-abi/blob/master/wrap.nim#L22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants