hyperbee
Advanced tools
Comparing version 2.19.0 to 2.19.1
39
index.js
@@ -8,2 +8,3 @@ const codecs = require('codecs') | ||
const debounce = require('debounceify') | ||
const Xache = require('xache') | ||
@@ -39,2 +40,31 @@ const RangeIterator = require('./iterators/range') | ||
class KeyCache { | ||
constructor () { | ||
this.keys = new Xache({ maxSize: 65536 }) | ||
this.length = 0 | ||
} | ||
get (seq) { | ||
return this.keys.get(seq) || null | ||
} | ||
set (seq, key) { | ||
this.keys.set(seq, key) | ||
if (seq >= this.length) this.length = seq + 1 | ||
} | ||
gc (length) { | ||
// if we need to "work" more than 128 ticks, just bust the cache... | ||
if (this.length - length > 128) { | ||
this.keys.clear() | ||
} else { | ||
for (let i = length; i < this.length; i++) { | ||
this.keys.delete(i) | ||
} | ||
} | ||
this.length = length | ||
} | ||
} | ||
class Pointers { | ||
@@ -355,2 +385,3 @@ constructor (buf) { | ||
this._sessions = opts.sessions !== false | ||
this._keyCache = new KeyCache() | ||
@@ -532,2 +563,4 @@ this._batches = [] | ||
} | ||
this._keyCache.gc(this.core.length) | ||
} | ||
@@ -689,3 +722,7 @@ | ||
async getKey (seq) { | ||
return (await this.getBlock(seq)).key | ||
const k = this.tree._keyCache.get(seq) | ||
if (k !== null) return k | ||
const key = (await this.getBlock(seq)).key | ||
this.tree._keyCache.set(seq, key) | ||
return key | ||
} | ||
@@ -692,0 +729,0 @@ |
{ | ||
"name": "hyperbee", | ||
"version": "2.19.0", | ||
"version": "2.19.1", | ||
"description": "An append-only B-tree running on a Hypercore.", | ||
@@ -20,3 +20,4 @@ "main": "index.js", | ||
"safety-catch": "^1.0.2", | ||
"streamx": "^2.12.4" | ||
"streamx": "^2.12.4", | ||
"xache": "^1.2.1" | ||
}, | ||
@@ -23,0 +24,0 @@ "devDependencies": { |
95631
2646
5
10
+ Addedxache@^1.2.1
+ Addedxache@1.2.1(transitive)