-
Notifications
You must be signed in to change notification settings - Fork 55
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
Custom key comparison functions #90
Conversation
Perfect solution: ask LMDB to change the signature to add a Hack: pass the rust function via a Keep in mind: https://matklad.github.io/2020/10/03/fast-thread-locals-in-rust.html |
Yeah, for just static functions (not closures) this is perfect, and if users want to capture any state, they have to do global passing themselves |
fb10aed
to
d0b9995
Compare
Thank you for the help @myfreeweb, Indeed it is not easy to accept a user-defined Rust closure as the custom key comparison function. A lot of people helped me on Reddit and some of them were proposing this solution (using a trait as a generic parameter to an
And yes, this can be a good enough solution for those who really need a Rust closure with a context or something. |
8b165c1
to
841333d
Compare
841333d
to
890bdc6
Compare
65a60cc
to
ece52d8
Compare
The complexity is more there than what I thought, introducing a custom key comparison function to the database (that is in fact based on the LMDB one) introduces a lot of little behavior like the fact that we can't just advance and retreat a key by only using the bytes of it as it is not necessarily the way it works (think about integer in little-endian). So I am closing this now as I don't want to take more time on that problem for now. |
This PR adds some new method for database opening to let the user specify a custom key comparison function if necessary and fixes #86.
extern "C"
one).CustomKeyCmp
trait into theheed-trait
crate.CustomKeyCmp
trait inputs to be byte slices again, this way we avoid the decoding overheads.Expose a Rust API instead of the LMDB required function signature
The current exposed API for the custom key comparison function is an ugly
extern "C"
function, I would like to let the user give a real Rust function instead. It is ugly in the sense that I don't want to expose theMDB_val
type (a struct with start and length fields) and therefore thefrom_val
function (transform anMDB_val
into a Rust slice).As it is not quite possible to pass the user-defined function to a wrapper function with the LMDB required signature, I thought that we could maybe be able to "generate" a function with the required signature that is monomorphized to call the user-defined function (no hidden parameter), but is it possible to do that, I am not sure, I tried by using a closure and a
Box::into_raw
call, but it didn't work well (dyn Fn
can't be cast intoextern "C" fn
).Ask for a typed key comparison function instead of a function that takes byte slices
The current exposed API asks for a Rust function that takes two bytes slices as arguments and returns an
Ordering
, it is a good enough solution but it would maybe be better to ask for a function that takes decoded types instead, using the database key codec.