From fcadfc4797287ced4905bc94c943b47a83b8a862 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Tue, 29 Mar 2022 15:42:36 +0300 Subject: [PATCH 1/3] use binary search to get seq from offset --- index.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 3fc1c00..cd2fafb 100644 --- a/index.js +++ b/index.js @@ -305,6 +305,21 @@ module.exports = function (log, indexesPath) { } } + function getSeqFromOffset(offset) { + if (offset === -1) return 0 + const { tarr, count } = indexes['seq'] + let low = 0 + let high = count - 1 + let mid = 0 + while (low <= high) { + mid = Math.floor((low + high) * 0.5) + if (tarr[mid] < offset) low = mid + 1 + else if (tarr[mid] > offset) high = mid - 1 + else return mid + } + throw new Error(`getSeqFromOffset: offset ${offset} not found`) + } + const undefinedBipf = bipf.allocAndEncode(undefined) function checkEqual(opData, buffer) { @@ -483,17 +498,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 @@ -1510,11 +1516,8 @@ 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) { From 020e69923e9051cfafa1297875f70a1269c56df0 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Tue, 29 Mar 2022 16:43:14 +0300 Subject: [PATCH 2/3] use bsb.eq in getSeqFromOffset --- index.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index cd2fafb..d04600d 100644 --- a/index.js +++ b/index.js @@ -308,16 +308,9 @@ module.exports = function (log, indexesPath) { function getSeqFromOffset(offset) { if (offset === -1) return 0 const { tarr, count } = indexes['seq'] - let low = 0 - let high = count - 1 - let mid = 0 - while (low <= high) { - mid = Math.floor((low + high) * 0.5) - if (tarr[mid] < offset) low = mid + 1 - else if (tarr[mid] > offset) high = mid - 1 - else return mid - } - throw new Error(`getSeqFromOffset: offset ${offset} not found`) + 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) From bcba09eff01981fa8e384883adfafcdbbbd6580f Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Tue, 29 Mar 2022 16:44:02 +0300 Subject: [PATCH 3/3] use seq index.count instead of index.tarr.length --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d04600d..1a6b2cb 100644 --- a/index.js +++ b/index.js @@ -841,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 } @@ -1513,7 +1513,7 @@ module.exports = function (log, indexesPath) { 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 }