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

Could `Database` be `Clone`? #789

Closed casey closed 3 months ago

casey commented 3 months ago

I noticed that both fields of Database are Arcs, and was wondering if the Database could implement Clone, since it should be cheap, and since creating read and write transactions don't rely on borrowing the Database, it should be safe.

cberner commented 3 months ago

No. compact() takes a &mut self

On Mon, Apr 1, 2024 at 12:55 AM Casey Rodarmor @.***> wrote:

I noticed that both fields of Database are Arcs, and was wondering if the Database could implement Clone, since it should be cheap, and since creating read and write transactions don't rely on borrowing the Database, it should be safe.

— Reply to this email directly, view it on GitHub https://github.com/cberner/redb/issues/789, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGNXQHRPBFFYYDV4SMW6YLY3EHHVAVCNFSM6AAAAABFRHJOY6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGIYTONZVGUYTOOA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

casey commented 3 months ago

Ahh, darn.

tqwewe commented 2 months ago

So can the database be shared throughout an app? If yes, then is it assumed we should wrap it in our own Arc?

adamreichold commented 2 months ago

Yes, ideally you can just pass &Database around in your components as the methods to start transactions take &self and Database is Send + Sync.

This could mean that you have to wrap it inside an Arc, for example if the value is not part of a larger struct and has its lifecycle determined by that.