diff --git a/example.mjs b/example.mjs index c2b5616..120f9b0 100644 --- a/example.mjs +++ b/example.mjs @@ -3,17 +3,17 @@ import CoreStorage from './index.js' const s = new CoreStorage('/tmp/rocks') const c = s.get(Buffer.alloc(32)) -if (!(await c.open())) await c.create() +if (!(await c.open())) await c.create({ key: Buffer.alloc(32) }) const w = c.createWriteBatch() -w.addTreeNode({ +w.putTreeNode({ index: 42, hash: Buffer.alloc(32), size: 10 }) -w.addTreeNode({ +w.putTreeNode({ index: 43, hash: Buffer.alloc(32), size: 10 diff --git a/index.js b/index.js index 72776fa..aaaa628 100644 --- a/index.js +++ b/index.js @@ -377,8 +377,13 @@ class HypercoreStorage { db.setDataInfo({ version: 0 }) } - createReadBatch () { - return new ReadBatch(this, this.db.read()) + snapshot () { + return this.db.snapshot() + } + + createReadBatch (opts) { + const snapshot = opts && opts.snapshot + return new ReadBatch(this, this.db.read({ snapshot })) } createWriteBatch () { @@ -556,7 +561,7 @@ function ensureSlab (size) { } function encodeIndexRange (pointer, type, opts) { - const bounded = { gt: null, gte: null, lte: null, lt: null, reverse: !!opts.reverse, limit: opts.limit || Infinity } + const bounded = { snapshot: opts.snapshot, gt: null, gte: null, lte: null, lt: null, reverse: !!opts.reverse, limit: opts.limit || Infinity } if (opts.gt || opts.gt === 0) bounded.gt = encodeDataIndex(pointer, type, opts.gt) else if (opts.gte) bounded.gte = encodeDataIndex(pointer, type, opts.gte)