Skip to content

Commit

Permalink
Support snapshots before db.ready() (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager authored Sep 3, 2024
1 parent 2f561a5 commit 6bec4db
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {

RocksDB._instances.add(this)

return promise
await promise

for (const snapshot of this._snapshots) snapshot._init()

function onopen (err) {
if (err) req.reject(new Error(err))
Expand All @@ -98,7 +100,7 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {

RocksDB._instances.delete(this)

return promise
await promise

function onclose (err) {
if (err) req.reject(new Error(err))
Expand Down
4 changes: 2 additions & 2 deletions lib/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {
this._snapshot = snapshot
}

async _init () {
_init () {
this._handle = binding.readInit(this)
this._buffer = binding.readBuffer(this._handle, this._capacity)
}
Expand Down Expand Up @@ -161,7 +161,7 @@ exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {
}

exports.WriteBatch = class RocksDBWriteBatch extends RocksDBBatch {
async _init () {
_init () {
this._handle = binding.writeInit(this)
this._buffer = binding.writeBuffer(this._handle, this._capacity)
}
Expand Down
15 changes: 9 additions & 6 deletions lib/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ const binding = require('../binding')

module.exports = class RocksDBSnapshot {
constructor (db) {
if (db.opened === false) throw new Error('Database is not open')

this._db = db
this._db._snapshots.add(this)

this._handle = binding.snapshotCreate(db._handle)
this._handle = null

this._db._snapshots.add(this)
if (db.opened === true) this._init()
}

_init () {
this._handle = binding.snapshotCreate(this._db._handle)
}

destroy () {
this._db._snapshots.delete(this)

if (this._handle === null) return

binding.snapshotDestroy(this._handle)

this._handle = null

this._db._snapshots.delete(this)
}
}
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,42 @@ test('iterator with snapshot', async (t) => {
await db.close()
})

test('iterator with snapshot before db open', async (t) => {
const db = new RocksDB(await tmp(t))

const snapshot = db.snapshot()

await db.ready()

const batch = db.write()
batch.put('aa', 'ba')
batch.put('ab', 'bb')
batch.put('ac', 'bc')
await batch.flush()

const entries = []

for await (const entry of db.iterator({ gte: 'a', lt: 'b' }, { snapshot })) {
entries.push(entry)
}

snapshot.destroy()

t.alike(entries, [])

await db.close()
})

test('destroy snapshot before db open', async (t) => {
const db = new RocksDB(await tmp(t))

const snapshot = db.snapshot()
snapshot.destroy()

await db.ready()
await db.close()
})

test('peek', async (t) => {
const db = new RocksDB(await tmp(t))
await db.ready()
Expand Down

0 comments on commit 6bec4db

Please sign in to comment.