Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use binary search to get seq from offset #200

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ module.exports = function (log, indexesPath) {
}
}

function getSeqFromOffset(offset) {
if (offset === -1) return 0
const { tarr, count } = indexes['seq']
const seq = bsb.eq(tarr, offset, 0, count - 1)
if (seq < 0) throw new Error(`getSeqFromOffset(${offset}) not found`)
return seq
}

const undefinedBipf = bipf.allocAndEncode(undefined)

function checkEqual(opData, buffer) {
Expand Down Expand Up @@ -483,17 +491,8 @@ module.exports = function (log, indexesPath) {
index.count = 0
}

// find the next possible seq
let seq = 0
if (index.offset !== -1) {
const { tarr } = indexes['seq']
const indexOffset = index.offset
for (const len = tarr.length; seq < len; ++seq)
if (tarr[seq] === indexOffset) {
seq++
break
}
}
// Find the next possible seq
let seq = index.offset === -1 ? 0 : getSeqFromOffset(index.offset) + 1

let updatedSeqIndex = false
let updatedTimestampIndex = false
Expand Down Expand Up @@ -842,8 +841,8 @@ module.exports = function (log, indexesPath) {
const seqs = []
opOffsets.sort((x, y) => x - y)
const opOffsetsLen = opOffsets.length
const { tarr } = indexes['seq']
for (let seq = 0, len = tarr.length; seq < len; ++seq) {
const { tarr, count } = indexes['seq']
for (let seq = 0; seq < count; ++seq) {
if (bsb.eq(opOffsets, tarr[seq]) !== -1) seqs.push(seq)
if (seqs.length === opOffsetsLen) break
}
Expand Down Expand Up @@ -1510,14 +1509,11 @@ module.exports = function (log, indexesPath) {
if (offset === 0 || offset === -1) {
prevOffset = -1
} else {
const { tarr } = indexes['seq']
for (const len = tarr.length; seq < len; ++seq) {
if (tarr[seq] === offset) break
else prevOffset = tarr[seq]
}
seq = getSeqFromOffset(offset)
prevOffset = indexes['seq'].tarr[seq - 1]
}

if (prevOffset === 0 && seq === indexes['seq'].tarr.length) {
if (prevOffset === 0 && seq === indexes['seq'].count) {
// not found
seq = 1
}
Expand Down