Skip to content

Commit

Permalink
fix dropped range issues and user data being mb strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Jan 13, 2025
1 parent 890f2c7 commit ef77a2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
3 changes: 2 additions & 1 deletion lib/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class CoreTX {
}

putUserData (key, value) {
this.changes.push([core.userData(this.core.dataPointer, key), value, null])
const buffer = typeof value === 'string' ? b4a.from(value) : value
this.changes.push([core.userData(this.core.dataPointer, key), buffer, null])
}

deleteUserData (key) {
Expand Down
31 changes: 12 additions & 19 deletions lib/view.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { Readable, getStreamError } = require('streamx')
const b4a = require('b4a')

const DROPPED = [null, null, null]

class OverlayStream extends Readable {
constructor (stream, start, end, reverse, changes, ranges) {
super()
Expand Down Expand Up @@ -139,6 +141,10 @@ class Overlay {
changes.sort(cmp)
ranges.sort(cmp)

while (changes.length > 0 && changes[changes.length - 1] === DROPPED) {
changes.pop()
}

this.indexed = view.indexed
this.changes = changes
this.ranges = ranges
Expand Down Expand Up @@ -225,10 +231,8 @@ class View {
_indexAndGet (read, key) {
this._index()
const change = this.map.get(b4a.toString(key, 'hex'))
if (change !== undefined) return Promise.resolve(ensureBuffer(change[1]))
const range = this._findRange(key)
if (range) return Promise.resolve(range.value)
return read.get(key)
if (change === undefined) return read.get(key)
return Promise.resolve(change[1])
}

_attached () {
Expand Down Expand Up @@ -265,22 +269,13 @@ class View {
const e = b4a.toString(range[2], 'hex')

for (const key of this.map.keys()) {
if (s <= key && key < e) this.map.delete(key)
if (s <= key && key < e) this.map.set(key, DROPPED)
}

if (this.ranges === null) this.ranges = []
this.ranges.push(range)
}

// unsorted so writes are cheap and reads are expensive
_findRange (key) {
if (this.ranges === null) return null
for (const r of this.ranges) {
if (b4a.compare(r[0], key) <= 0 && b4a.compare(r[2], key) > 0) return r
}
return null
}

apply (changes) {
if (this.snap !== null) throw new Error('Illegal to push changes to a snapshot')

Expand Down Expand Up @@ -320,6 +315,9 @@ class View {
module.exports = View

function cmpChange (a, b) {
if (a === DROPPED) return b === DROPPED ? 0 : 1
if (b === DROPPED) return a === DROPPED ? 0 : -1

const c = b4a.compare(a[0], b[0])
return c === 0 ? b4a.compare(a[2], b[2]) : c
}
Expand All @@ -335,8 +333,3 @@ function reverseArray (list) {
for (let i = 0; i < list.length; i++) r[r.length - 1 - i] = list[i]
return r
}

function ensureBuffer (value) {
if (typeof value === 'string') return b4a.from(value)
return value
}

0 comments on commit ef77a2e

Please sign in to comment.