meilisearch / heed

A fully typed LMDB wrapper with minimum overhead 🐦
https://docs.rs/heed
MIT License
569 stars 52 forks source link

Make `RoIter` also `Send` when `read-txn-no-tls` feature is enabled #213

Closed xiaoyawei closed 6 months ago

xiaoyawei commented 11 months ago

With similar reasoning that RoTxn can be moved among threads when MDB_NOTLS flag is on, my understanding it that the corresponding RoCursor and RoIter (and other readonly iterators like range iterators) can be moved among threads as well, and hereby can implement Send.

By looking at the source code of lmdb, looks like cursors is actually a field of a transaction, and since different threads can access to the same db with the transaction pointer not simultaneously, they are also able to use a specific cursor in that transaction for data access.

I could be mistaken but would love to hear about opinions from heed maintainers. Thanks!

Kerollmops commented 9 months ago

I am pretty sure you are right, it seems to me that when MDB_NOTLS is enabled the transaction is not stored in a particular thread. Rust prevents us from touching the content of transaction (thanks !Sync) and therefore I am pretty sure that it is safe.

Kerollmops commented 6 months ago

Hey @xiaoyawei 👋

I will be working on that in a PR. Thanks to your emails, we can make the cursors implement Send.

xiaoyawei commented 6 months ago

@Kerollmops That sounds great! My apologies for missing your previous comment but glad this idea can implemented, thanks!