Skip to content

Commit

Permalink
Merge pull request #142 from ssbc/better-seekers
Browse files Browse the repository at this point in the history
Improve the db2 seekers in operators.js
  • Loading branch information
Powersource committed Oct 24, 2023
2 parents 165967e + b78469a commit d4d7079
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions lib/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
const { allocAndEncode, seekKey2, iterate } = require('bipf')
const { equal } = require('jitdb/operators')

const B_VALUE = allocAndEncode('value')
const B_CONTENT = allocAndEncode('content')
const B_TANGLES = allocAndEncode('tangles')
const B_ROOT = allocAndEncode('root')
Expand All @@ -21,17 +20,15 @@ module.exports = {
}
const B_TANGLE = B_TANGLE_MAP.get(tangle)

function seekTangleRoot(buffer, start, p) {
if (p < 0) return -1
p = seekKey2(buffer, 0, B_VALUE, 0)
if (p < 0) return -1
p = seekKey2(buffer, p, B_CONTENT, 0)
if (p < 0) return -1
p = seekKey2(buffer, p, B_TANGLES, 0)
if (p < 0) return -1
p = seekKey2(buffer, p, B_TANGLE, 0)
if (p < 0) return -1
return seekKey2(buffer, p, B_ROOT, 0)
function seekTangleRoot(buffer, start, pValue) {
if (pValue < 0) return -1
const pValueContent = seekKey2(buffer, pValue, B_CONTENT, 0)
if (pValueContent < 0) return -1
const pValueContentTangles = seekKey2(buffer, pValueContent, B_TANGLES, 0)
if (pValueContentTangles < 0) return -1
const pValueContentTanglesTangle = seekKey2(buffer, pValueContentTangles, B_TANGLE, 0)
if (pValueContentTanglesTangle < 0) return -1
return seekKey2(buffer, pValueContentTanglesTangle, B_ROOT, 0)
}

return equal(seekTangleRoot, value, {
Expand All @@ -47,18 +44,18 @@ module.exports = {
},
}

function seekFirstRecp(buffer, start, p) {
if (p < 0) return -1
p = seekKey2(buffer, p, B_CONTENT, 0)
if (p < 0) return -1
p = seekKey2(buffer, p, B_RECPS, 0)
if (p < 0) return -1
function seekFirstRecp(buffer, start, pValue) {
if (pValue < 0) return -1
const pValueContent = seekKey2(buffer, pValue, B_CONTENT, 0)
if (pValueContent < 0) return -1
const pValueContentRecps = seekKey2(buffer, pValueContent, B_RECPS, 0)
if (pValueContentRecps < 0) return -1

let pValueFirstRecp
const error = iterate(buffer, p, (_, pointer) => {
pValueFirstRecp = pointer
let pValueContentRecps0
const error = iterate(buffer, pValueContentRecps, (_, pointer) => {
pValueContentRecps0 = pointer
return true
})
if (error === -1) return -1
return pValueFirstRecp
return pValueContentRecps0
}

0 comments on commit d4d7079

Please sign in to comment.