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

Q: would you be interested in writing rust bindings for libmdbx? #11

Open
erthink opened this issue Oct 17, 2019 · 6 comments
Open

Q: would you be interested in writing rust bindings for libmdbx? #11

erthink opened this issue Oct 17, 2019 · 6 comments

Comments

@erthink
Copy link

erthink commented Oct 17, 2019

libmdbx is revised and extended descendant of LMDB.

@Kerollmops
Copy link
Member

Kerollmops commented Oct 17, 2019

Why not, I think it is not far from a fork of the mozilla lmdb-rkv-sys library, a replacement of the git submodule and some API modifications to make it fit libmdbx. Even some features flags could be added to enable some specific libmdbx features (e.g. MDBX_UTTERLY_NOSYNC) or a feature that disable TLS and enable read only transactions to be shared between thread safely.

By the way if I do so I will make it a repository for only this library not as a sub folder like the mozilla/lmdb-rs one does.


I have a little question about the mdbx cursors, are those reusable or not? Because in the following sentence it is specified that "any opened cursors can be reused" but also "and must be freed explicitly".

(!) An important difference between MDBX and LMDB is that MDBX required that any opened cursors can be reused and must be freed explicitly, regardless ones was opened in a read-only or write transaction. The REASON for this is eliminates ambiguity which helps to avoid errors such as: use-after-free, double-free, i.e. memory corruption and segfaults.

@erthink
Copy link
Author

erthink commented Oct 17, 2019

Probably my English is not very good:

  1. Any cursor created by mdb_cursor_open(), when it becomes unnecessary, must be explicitly freed by mdbx_cursor_close().
  2. Any cursor created by mdb_cursor_open(), may be reused by mdbx_cursor_renew(), until it explicitly freed by mdbx_cursor_close().

@Kerollmops
Copy link
Member

Kerollmops commented Oct 19, 2019

Hi, I made some progress on building the mdbx bindings in Rust and found out that Rust doesn't like overflowing number like MDBX_RESULT_TRUE: c_uint = 18446744073709551615. And found strange that a c_uint is in fact an unsigned int and is attributed the max value of an unsigned long instead of the unsigned int one. Is it normal? I understand that it work but...

Variables concerned are MDBX_RESULT_TRUE, MDBX_PROBLEM, MDBX_BUSY, MDBX_EMULTIVAL, MDBX_EBADSIGN, MDBX_WANNA_RECOVERY, MDBX_EKEYMISMATCH, MDBX_TOO_LARGE and MDBX_THREAD_MISMATCH.

There is also many defines that Rust doesn't like because those are not upper cased and are translated into Rust consts which should be upper cased: MDBX_subpage_dupfixed_leaf, MDBX_subpage_leaf, MDBX_page_dupfixed_leaf, MDBX_page_leaf, MDBX_page_branch, MDBX_page_large, MDBX_page_meta and MDBX_page_void.

EDIT: I found out how to fix the -1 issue, it is absolutely not related to your code, sorry.

@Kerollmops
Copy link
Member

Kerollmops commented Oct 19, 2019

Ok so here is the mdbx-sys repository with most of all the bindings generated in Rust. I blacklisted (for now) the MDBX_page_type_t, MDBX_pgvisitor_func, mdbx_env_pgwalk types/functions, because of the upper cased problem and will probably #[allow(non_upper_case_globals)].

EDIT: Done!

@erthink
Copy link
Author

erthink commented Oct 20, 2019

  1. The MDBX_page_type_t, MDBX_pgvisitor_func and mdbx_env_pgwalk are an internal/auxiliary API, i.e. shouldn't be used in a regular application.

  2. The problems with error codes since libmdbx defines an aliases for some system error codes:

    • are system-depended, i.e. differ on Linux, *BSD, MacOS, Windows, etc.
    • may be defined as signed integers (Linux, Unix, MacOS) or unsigned (Windows), but signed int is preferable since mdbx functions returns int type.

@Kerollmops
Copy link
Member

Kerollmops commented Oct 20, 2019

  1. I will blacklist those like I did before.
  2. Do you think that I will need to define those by hand? like this part of the lib? Strange because the lmdb-rs repository doesn't seems to take care of the mdb_filehandle_t type on windows.

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

No branches or pull requests

2 participants