spacejam/sled

Weak Db handle

Opened this issue · 0 comments

My understanding is that various types like Tree hold references to internal Sled structures, so a database will remain "open" until all instances of such types are dropped, and it's not safe/possible to open the database again while it is already open. If this is incorrect, then the rest of this won't make any sense. 😅

Use Case:

I have a use case where code that wants to use a Sled database doesn't know if it has already been opened by someone else in the same process. So I added a central registry of open databases and hand out Arc<Db>s, only opening the database if it has not already been opened by someone else.

I'd like to be able to have databases close when nobody is using them anymore, but the Arc<Db> I store in my registry will keep it open forever. And I don't have enough information to know when I can drop it and then assume the database is closed even if I use a Weak<Db>, because there are other things that can keep the database open outside of my own bookkeeping.

Proposed Change:

Sled could expose a weak database handle, conceptually equivalent to Arc's Weak counterpart. Internally it would hold multiple Weak<Whatever> (page cache, flusher, whatever) and would only upgrade successfully to a Db if it is able to upgrade all of its internal Weaks.

Alternative Approaches:

Sled could store an internal global map of open databases and use it to do the equivalent of this internally. I.e. no extra types in the public API, and instead open would just magically succeed if somebody else in the same process already opened it.