Skip to content

Commit

Permalink
cleanup naming and now use a batch for more speeeeed
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Sep 13, 2024
1 parent 210c294 commit 7ea26b7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
28 changes: 18 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class HyperDB {
}

function map (entries) {
return engine.getRange(snap, entries)
return engine.getIndirectRange(snap, entries)
}
}

Expand Down Expand Up @@ -325,13 +325,23 @@ class HyperDB {

async _getPrev (key, collection) {
const st = collection.stats === true ? this.updates.prestats(collection) : null
if (st !== null && !st.promise) {
st.key = this.definition.resolveCollection(STATS).encodeKey({ id: collection.id })
st.promise = this.engine.get(this.engineSnapshot, st.key)
st.promise.catch(noop) // handled below

if (st !== null && !st.promise && !st.value) {
const statsCollection = this.definition.resolveCollection(STATS)

st.key = statsCollection.encodeKey({ id: collection.id })
st.promise = this.engine.getBatch(this.engineSnapshot, [key, st.key])

const [value, stats] = await st.promise

st.value = stats === null ? statsCollection.encodeValue(this.version, { count: 0 }) : stats
st.promise = null

return value
}

const value = await this.engine.get(this.engineSnapshot, key)
if (st !== null) st.value = await st.promise
if (st !== null && st.promise !== null) await st.promise
return value
}

Expand Down Expand Up @@ -435,11 +445,11 @@ class HyperDB {
_applyStats () {
const statsCollection = this.definition.resolveCollection(STATS)
for (const [collection, { key, value }] of this.updates.stats) {
const stats = value === null ? { count: 0 } : statsCollection.reconstruct(key, value)
const stats = statsCollection.reconstruct(this.version, key, value)
const overlay = this.updates.collectionStatsOverlay(collection)
stats.count += overlay.count
const updatedValue = statsCollection.encodeValue(this.version, stats)
if (value !== null && b4a.equal(value, updatedValue)) continue
if (b4a.equals(value, updatedValue)) continue
this.updates.update(statsCollection, key, updatedValue)
}
}
Expand Down Expand Up @@ -525,6 +535,4 @@ function diffKeys (a, b) {
return res
}

function noop () {}

module.exports = HyperDB
14 changes: 13 additions & 1 deletion lib/engine/rocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = class RocksEngine {
return new RocksSnapshot(this.db.snapshot())
}

getRange (snapshot, entries) {
getIndirectRange (snapshot, entries) {
const read = this.db.read({ snapshot: getSnapshot(snapshot) })
const promises = new Array(entries.length)

Expand All @@ -55,6 +55,18 @@ module.exports = class RocksEngine {
return promises
}

getBatch (snapshot, keys) {
const read = this.db.read({ snapshot: getSnapshot(snapshot) })
const promises = new Array(keys.length)

for (let i = 0; i < promises.length; i++) {
promises[i] = read.get(keys[i])
}

read.tryFlush()
return Promise.all(promises)
}

get (snapshot, key) {
return this.db.get(key, { snapshot: getSnapshot(snapshot) })
}
Expand Down

0 comments on commit 7ea26b7

Please sign in to comment.