-
Notifications
You must be signed in to change notification settings - Fork 132
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
Sketch of a declarative query library for redb #793
Comments
Oh yeah, in particular I'm curious about efficiency. A downside of this approach is that the database has to open and close tables for each query, instead of reusing them. |
One cool thing would be if you could express table definitions fully in the type system, i.e., by using the table name as a const generic, in which case you wouldn't need wrappers, but const generics don't support strings. I think you could do it though if table names were integers. |
I haven't benchmarked it, but my guess is this is fine. Opening and closing tables should be cheap, so as long as the queries are non-trivial it's probably fine |
I haven't looked at axum, but this definitely seems like an interesting idea! When I first started redb, I was actually thinking it would be a little more like sqlite than lmdb, but then I quickly gave up on that idea. One thing that seems a bit off about the proposed |
I added an example in the tests using a closure, which allows you to pass in outside arguments. I also added an example using a struct and implementing query manually, which is pretty gross but probably becomes okay once you have enough things that the boilerplate of opening tables is greater than the boilerplate of the trait implementation. |
Ah yes, I see how it works. Are you using it in |
I'm not, but I'll try it out and see how it works in practice. |
I was inspired by axum to try to write a library for easily writing redb queries, called redbql. The "ql" is for query library, since it's not really a language.
It introduces a few new traits.
Query
is a function that runs against a read transaction.Statement
is a function that runs against a write transaction.StatementArg
is something that can produce a value from a write transaction, andQueryArg
is something that can produce a value from a read transaction.You can write a
Query
orStatement
implementation manually, but the nice thing is that it can be auto-implemented for functions which take types which implementQueryArg
/StatementArg
:And a boilerplate macro for declaring tables with corresponding
QueryArg
andStatrementArg
types:And finally, what this buys is, is that we can write functions that implement our queries, and the tables are opened for us:
What do you think? I just finished the implementation, and want to play with it a bit more to see how it works inside of
ord
. I think this gets very close to the convenience of SQL statements, where you just use the tables you want, and the database takes care of opening them for you.I also wanted to write a function on
Database
which would take a query an execute it without needing to start a transaction, but I ran into lifetime errors and gave up T_T I'm sure someone clever can come up with a working version tho.The text was updated successfully, but these errors were encountered: