cberner / redb

An embedded key-value database in pure Rust
https://www.redb.org
Apache License 2.0
3.07k stars 137 forks source link

Example for multithreaded usage #804

Closed lquenti closed 2 months ago

lquenti commented 2 months ago

Hi,

could you add an example for multithreaded use? Especially because of this in the README:

MVCC support for concurrent readers & writer, without blocking

This confuses me with the definition of the struct:

pub struct Database {
    mem: Arc<TransactionalMemory>,
    transaction_tracker: Arc<TransactionTracker>,
}

Having an Arc and "concurrent readers" reads to me like I can just clone it to other threads/routes, but it does not implement clone!

geniot commented 2 months ago

Hi, I just looked at the benchmark test, perhaps it can help you further: https://github.com/cberner/redb/blob/master/benches/lmdb_benchmark.rs#L224

cberner commented 2 months ago

This file is a good example of concurrent writing: https://github.com/cberner/redb/blob/master/benches%2Fmultithreaded_insert_benchmark.rs

You can't open a database file twice, but concurrent access to the Database and Transaction objects should be safe. As long as it compiles it should be correct 😊