Forbid stores from being shared - #719
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds a new test case to verify that a RocksDatabase instance can be successfully initialized using the store of another instance. The review feedback suggests expanding this test to verify two-way functionality by ensuring that write operations on the new database instance are also readable from the original instance.
📊 Benchmark Resultsget-sync.bench.tsgetSync() > random keys - small key size (100 records)
getSync() > sequential keys - small key size (100 records)
ranges.bench.tsgetRange() > small range (100 records, 50 range)
realistic-load.bench.tsRealistic write load with workers > write variable records with transaction log
transaction-log.bench.tsTransaction log > read 100 iterators while write log with 100 byte records
Transaction log > read one entry from random position from log with 1000 100 byte records
worker-put-sync.bench.tsputSync() > random keys - small key size (100 records, 10 workers)
worker-transaction-log.bench.tsTransaction log with workers > write log with 100 byte records
Results from commit 54baa6c |
| } | ||
| }); | ||
|
|
||
| it('should allow sharing the store between databases', () => |
There was a problem hiding this comment.
LGTM, do you think we should update this?:
Could we reconcile this supported-behavior test with the Store JSDoc at src/store.ts:141, which currently says that a store should not be shared between RocksDatabase instances? If sharing is intended, please update that contract here and document the shared-close lifetime this test asserts; otherwise future users and maintainers have two contradictory ownership rules.
A Store owns a single NativeDatabase handle and is 1:1 with a RocksDatabase. Reusing one store across two RocksDatabase instances aliased the handle (surprising close-cascade and shared encoder state). Enforce the invariant: each RocksDatabase claims its store on construction via Store#claim(), which throws if the store is already owned by another RocksDatabase. The claim is durable across close/reopen so a temporarily-closed owner does not release it. Sharing a store with the owning database's Transaction instances is unaffected. Document in the Store jsdoc, the constructor `store` option, and the Custom Store README section. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
While reviewing https://github.com/HarperFast/harper/pull/1888/changes#diff-4ff51263bd99981165fc179149c3195ea0c0ca3e2f360bafee2a6bb356964377R605, I realized it's technically possible to share stores between
RocksDatabaseinstances. The store owns the native DB handle and sharing could lead to bugs. It's best to forbid stores from being shared.