Skip to content
Open
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,14 @@ Current version.
#### `await db.ready()`

Makes sure internal state is loaded. Call this once before checking the version if you haven't called any of the other APIs.

#### `await db.getOperation(seq)`

Similar to [createHistoryStream](#stream--dbcreatehistorystreamoptions) but returns a single operation at a given sequence.

If the sequence is `0` it will return null, as the first entry is a header and not a part of the tree operations.

```js
await db.getOperation(db.feed.length - 1)
// { seq: 1, key: 'a', value: 'hello', type: 'put' }
```
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ class BlockEntry {
}
}

operation () {
return {
type: this.isDeletion() ? 'del' : 'put',
...this.final()
}
}

getTreeNode (offset) {
if (this.index === null) {
this.index = inflate(this.indexBuffer)
Expand Down Expand Up @@ -308,6 +315,14 @@ class Hyperbee {
return iteratorPeek(this.createRangeIterator({ ...opts, limit: 1 }))
}

async getOperation (seq, opts) {
if (seq === 0) return null

const b = new Batch(this, this.feed.snapshot(), null, false, opts)
const block = await b.getBlock(seq)
return block.operation()
}

createRangeIterator (opts = {}) {
const extension = (opts.extension === false && opts.limit !== 0) ? null : this.extension

Expand Down
9 changes: 2 additions & 7 deletions iterators/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,17 @@ module.exports = class HistoryIterator {

if (this.reverse) {
if (this.lt <= 1) return null
return final(await this.batch.getBlock(--this.lt, this.options))
return (await this.batch.getBlock(--this.lt, this.options)).operation()
}

return final(await this.batch.getBlock(this.gte++, this.options))
return (await this.batch.getBlock(this.gte++, this.options)).operation()
}

close () {
return this.batch._closeSnapshot()
}
}

function final (node) {
const type = node.isDeletion() ? 'del' : 'put'
return { type, ...node.final() }
}

function gte (opts, version) {
if (opts.gt) return (opts.gt < 0 ? (opts.gt + version) : opts.gt) + 1
const gte = opts.gte || opts.since || 1
Expand Down
Loading