From 113306a60fc95ac93cbf3e3b1b88604333d12c1a Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Mon, 4 Apr 2022 14:14:16 +0300 Subject: [PATCH 1/3] use bipf 1.6.0 seekKeyCached --- index.js | 121 ++++++++++++++++++++---------------------------- package.json | 2 +- test/helpers.js | 107 +++++++++++++++++++----------------------- test/prefix.js | 5 +- 4 files changed, 101 insertions(+), 134 deletions(-) diff --git a/index.js b/index.js index 2ba017e..26453c3 100644 --- a/index.js +++ b/index.js @@ -257,28 +257,29 @@ module.exports = function (log, indexesPath) { } } - function seekMinTimestamp(buffer, pValue) { - var p = 0 // note you pass in p! - p = bipf.seekKey(buffer, p, B_TIMESTAMP) - const arrivalTimestamp = bipf.decode(buffer, p) - p = bipf.seekKey(buffer, pValue, B_TIMESTAMP) - const declaredTimestamp = bipf.decode(buffer, p) + function seekMinTimestamp(buffer) { + const pTimestamp = bipf.seekKey(buffer, 0, B_TIMESTAMP) + const arrivalTimestamp = bipf.decode(buffer, pTimestamp) + const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValueTimestamp = bipf.seekKey(buffer, pValue, B_TIMESTAMP) + const declaredTimestamp = bipf.decode(buffer, pValueTimestamp) return Math.min(arrivalTimestamp, declaredTimestamp) } - function seekSequence(buffer, pValue) { - var p = bipf.seekKey(buffer, pValue, B_SEQUENCE) + function seekSequence(buffer) { + const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const p = bipf.seekKey(buffer, pValue, B_SEQUENCE) return bipf.decode(buffer, p) } - function updateTimestampIndex(seq, offset, buffer, pValue) { + function updateTimestampIndex(seq, offset, buffer) { if (seq > indexes['timestamp'].count - 1) { if (seq > indexes['timestamp'].tarr.length - 1) growTarrIndex(indexes['timestamp'], Float64Array) indexes['timestamp'].offset = offset - const timestamp = seekMinTimestamp(buffer, pValue) + const timestamp = seekMinTimestamp(buffer) indexes['timestamp'].tarr[seq] = timestamp indexes['timestamp'].count = seq + 1 @@ -286,14 +287,14 @@ module.exports = function (log, indexesPath) { } } - function updateSequenceIndex(seq, offset, buffer, pValue) { + function updateSequenceIndex(seq, offset, buffer) { if (seq > indexes['sequence'].count - 1) { if (seq > indexes['sequence'].tarr.length - 1) growTarrIndex(indexes['sequence'], Uint32Array) indexes['sequence'].offset = offset - const sequence = seekSequence(buffer, pValue) + const sequence = seekSequence(buffer) indexes['sequence'].tarr[seq] = sequence indexes['sequence'].count = seq + 1 @@ -311,8 +312,8 @@ module.exports = function (log, indexesPath) { const undefinedBipf = bipf.allocAndEncode(undefined) - function checkEqual(opData, buffer, pValue) { - const fieldStart = opData.seek(buffer, 0, pValue) + function checkEqual(opData, buffer) { + const fieldStart = opData.seek(buffer) if (fieldStart === -1 && opData.value.equals(undefinedBipf)) return true else return bipf.compare(buffer, fieldStart, opData.value, 0) === 0 @@ -330,13 +331,11 @@ module.exports = function (log, indexesPath) { } function checkComparison(op, buffer) { - const pValue = bipf.seekKey(buffer, 0, B_VALUE) - if (op.data.indexName === 'timestamp') { - const timestamp = seekMinTimestamp(buffer, pValue) + const timestamp = seekMinTimestamp(buffer) return compareWithRangeOp(op, timestamp) } else if (op.data.indexName === 'sequence') { - const sequence = seekSequence(buffer, pValue) + const sequence = seekSequence(buffer) return compareWithRangeOp(op, sequence) } else { console.warn( @@ -346,21 +345,21 @@ module.exports = function (log, indexesPath) { } } - function checkPredicate(opData, buffer, pValue) { - const fieldStart = opData.seek(buffer, 0, pValue) + function checkPredicate(opData, buffer) { + const fieldStart = opData.seek(buffer) const predicateFn = opData.value if (fieldStart < 0) return false const fieldValue = bipf.decode(buffer, fieldStart) return predicateFn(fieldValue) } - function checkAbsent(opData, buffer, pValue) { - const fieldStart = opData.seek(buffer, 0, pValue) + function checkAbsent(opData, buffer) { + const fieldStart = opData.seek(buffer) return fieldStart < 0 } - function checkIncludes(opData, buffer, pValue) { - const fieldStart = opData.seek(buffer, 0, pValue) + function checkIncludes(opData, buffer) { + const fieldStart = opData.seek(buffer) if (!~fieldStart) return false const type = bipf.getEncodedType(buffer, fieldStart) @@ -376,7 +375,7 @@ module.exports = function (log, indexesPath) { } }) return found - } else return checkEqual(opData, buffer, pValue) + } else return checkEqual(opData, buffer) } function safeReadUint32(buf, prefixOffset = 0) { @@ -398,9 +397,9 @@ module.exports = function (log, indexesPath) { arr.push(seq) } - function updatePrefixMapIndex(opData, index, buffer, seq, offset, pValue) { + function updatePrefixMapIndex(opData, index, buffer, seq, offset) { if (seq > index.count - 1) { - const fieldStart = opData.seek(buffer, 0, pValue) + const fieldStart = opData.seek(buffer) if (~fieldStart) { const buf = bipf.slice(buffer, fieldStart) if (buf.length) { @@ -414,11 +413,11 @@ module.exports = function (log, indexesPath) { } } - function updatePrefixIndex(opData, index, buffer, seq, offset, pValue) { + function updatePrefixIndex(opData, index, buffer, seq, offset) { if (seq > index.count - 1) { if (seq > index.tarr.length - 1) growTarrIndex(index, Uint32Array) - const fieldStart = opData.seek(buffer, 0, pValue) + const fieldStart = opData.seek(buffer) if (~fieldStart) { const buf = bipf.slice(buffer, fieldStart) index.tarr[seq] = buf.length @@ -432,19 +431,19 @@ module.exports = function (log, indexesPath) { } } - function updateIndexValue(op, index, buffer, seq, pValue) { - if (op.type === 'EQUAL' && checkEqual(op.data, buffer, pValue)) + function updateIndexValue(op, index, buffer, seq) { + if (op.type === 'EQUAL' && checkEqual(op.data, buffer)) index.bitset.add(seq) - else if (op.type === 'PREDICATE' && checkPredicate(op.data, buffer, pValue)) + else if (op.type === 'PREDICATE' && checkPredicate(op.data, buffer)) index.bitset.add(seq) - else if (op.type === 'ABSENT' && checkAbsent(op.data, buffer, pValue)) + else if (op.type === 'ABSENT' && checkAbsent(op.data, buffer)) index.bitset.add(seq) - else if (op.type === 'INCLUDES' && checkIncludes(op.data, buffer, pValue)) + else if (op.type === 'INCLUDES' && checkIncludes(op.data, buffer)) index.bitset.add(seq) } - function updateAllIndexValue(opData, newIndexes, buffer, seq, pValue) { - const fieldStart = opData.seek(buffer, 0, pValue) + function updateAllIndexValue(opData, newIndexes, buffer, seq) { + const fieldStart = opData.seek(buffer) const value = bipf.decode(buffer, fieldStart) const indexName = safeFilename(opData.indexType + '_' + value) @@ -539,20 +538,18 @@ module.exports = function (log, indexesPath) { return } - const pValue = bipf.seekKey(buffer, 0, B_VALUE) - - if (updateTimestampIndex(seq, offset, buffer, pValue)) + if (updateTimestampIndex(seq, offset, buffer)) updatedTimestampIndex = true - if (updateSequenceIndex(seq, offset, buffer, pValue)) + if (updateSequenceIndex(seq, offset, buffer)) updatedSequenceIndex = true if (indexNeedsUpdate) { if (op.data.prefix && op.data.useMap) - updatePrefixMapIndex(op.data, index, buffer, seq, offset, pValue) + updatePrefixMapIndex(op.data, index, buffer, seq, offset) else if (op.data.prefix) - updatePrefixIndex(op.data, index, buffer, seq, offset, pValue) - else updateIndexValue(op, index, buffer, seq, pValue) + updatePrefixIndex(op.data, index, buffer, seq, offset) + else updateIndexValue(op, index, buffer, seq) } if (seq % 1000 === 0) { @@ -673,12 +670,10 @@ module.exports = function (log, indexesPath) { return } - const pValue = bipf.seekKey(buffer, 0, B_VALUE) - - if (updateTimestampIndex(seq, offset, buffer, pValue)) + if (updateTimestampIndex(seq, offset, buffer)) updatedTimestampIndex = true - if (updateSequenceIndex(seq, offset, buffer, pValue)) + if (updateSequenceIndex(seq, offset, buffer)) updatedSequenceIndex = true opsMissingIdx.forEach((op) => { @@ -688,8 +683,7 @@ module.exports = function (log, indexesPath) { newIndexes[op.data.indexName], buffer, seq, - offset, - pValue + offset ) else if (op.data.prefix) updatePrefixIndex( @@ -697,19 +691,11 @@ module.exports = function (log, indexesPath) { newIndexes[op.data.indexName], buffer, seq, - offset, - pValue + offset ) else if (op.data.indexAll) - updateAllIndexValue(op.data, newIndexes, buffer, seq, pValue) - else - updateIndexValue( - op, - newIndexes[op.data.indexName], - buffer, - seq, - pValue - ) + updateAllIndexValue(op.data, newIndexes, buffer, seq) + else updateIndexValue(op, newIndexes[op.data.indexName], buffer, seq) }) if (seq % 1000 === 0) { @@ -882,8 +868,7 @@ module.exports = function (log, indexesPath) { function checker(value) { if (!value) return false // deleted - const pValue = bipf.seekKey(value, 0, B_VALUE) - const fieldStart = seek(value, 0, pValue) + const fieldStart = seek(value) if (target) return bipf.compare(value, fieldStart, target, 0) === 0 else if (~fieldStart) return false @@ -1150,17 +1135,13 @@ module.exports = function (log, indexesPath) { } function isValueOk(ops, value, isOr) { - const pValue = bipf.seekKey(value, 0, B_VALUE) - for (let i = 0; i < ops.length; ++i) { const op = ops[i] let ok = false - if (op.type === 'EQUAL') ok = checkEqual(op.data, value, pValue) - else if (op.type === 'PREDICATE') - ok = checkPredicate(op.data, value, pValue) - else if (op.type === 'ABSENT') ok = checkAbsent(op.data, value, pValue) - else if (op.type === 'INCLUDES') - ok = checkIncludes(op.data, value, pValue) + if (op.type === 'EQUAL') ok = checkEqual(op.data, value) + else if (op.type === 'PREDICATE') ok = checkPredicate(op.data, value) + else if (op.type === 'ABSENT') ok = checkAbsent(op.data, value) + else if (op.type === 'INCLUDES') ok = checkIncludes(op.data, value) else if (op.type === 'NOT') ok = !isValueOk(op.data, value, false) else if (op.type === 'AND') ok = isValueOk(op.data, value, false) else if (op.type === 'OR') ok = isValueOk(op.data, value, true) diff --git a/package.json b/package.json index dc6e3c3..cdd48a6 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "dependencies": { "atomic-file-rw": "^0.2.1", "binary-search-bounds": "^2.0.4", - "bipf": "^1.5.4", + "bipf": "^1.6.0", "crc": "3.6.0", "debug": "^4.2.0", "fastpriorityqueue": "^0.7.1", diff --git a/test/helpers.js b/test/helpers.js index 79878c8..3393a34 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -19,84 +19,71 @@ const B_PRIVATE = Buffer.from('private') const B_CHANNEL = Buffer.from('channel') module.exports = { - toBipf: function (value) { + toBipf(value) { return bipf.allocAndEncode(value) }, - seekKey: function (buffer) { - var p = 0 // note you pass in p! - return bipf.seekKey(buffer, p, B_KEY) + seekKey(buffer) { + return bipf.seekKey(buffer, 0, B_KEY) }, - seekAuthor: function (buffer) { - var p = 0 // note you pass in p! - p = bipf.seekKey(buffer, p, B_VALUE) - - if (~p) return bipf.seekKey(buffer, p, B_AUTHOR) + seekAuthor(buffer) { + const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + if (pValue < 0) return + return bipf.seekKey(buffer, pValue, B_AUTHOR) }, - seekVoteLink: function (buffer) { - var p = 0 // note you pass in p! - p = bipf.seekKey(buffer, p, B_VALUE) - if (!~p) return - p = bipf.seekKey(buffer, p, B_CONTENT) - if (!~p) return - p = bipf.seekKey(buffer, p, B_VOTE) - if (!~p) return - return bipf.seekKey(buffer, p, B_LINK) + seekVoteLink(buffer) { + const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + if (pValue < 0) return + const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + if (pValueContent < 0) return + const pValueContentVote = bipf.seekKey(buffer, pValueContent, B_VOTE) + if (pValueContentVote < 0) return + return bipf.seekKey(buffer, pValueContentVote, B_LINK) }, - seekType: function (buffer) { - var p = 0 // note you pass in p! - p = bipf.seekKey(buffer, p, B_VALUE) - - if (~p) { - p = bipf.seekKey(buffer, p, B_CONTENT) - if (~p) return bipf.seekKey(buffer, p, B_TYPE) - } + seekType(buffer) { + const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + if (pValue < 0) return + const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + if (pValueContent < 0) return + return bipf.seekKey(buffer, pValueContent, B_TYPE) }, - seekAnimals: function (buffer) { - var p = 0 // note you pass in p! - p = bipf.seekKey(buffer, p, B_VALUE) - if (!~p) return - p = bipf.seekKey(buffer, p, B_CONTENT) - if (!~p) return - return bipf.seekKey(buffer, p, B_ANIMALS) + seekAnimals(buffer) { + const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + if (pValue < 0) return + const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + if (pValueContent < 0) return + return bipf.seekKey(buffer, pValueContent, B_ANIMALS) }, - pluckWord: function (buffer, start) { - var p = start - return bipf.seekKey(buffer, p, B_WORD) + pluckWord(buffer, start) { + return bipf.seekKey(buffer, start, B_WORD) }, - seekRoot: function (buffer) { - var p = 0 // note you pass in p! - p = bipf.seekKey(buffer, p, B_VALUE) - - if (~p) { - p = bipf.seekKey(buffer, p, B_CONTENT) - if (~p) return bipf.seekKey(buffer, p, B_ROOT) - } + seekRoot(buffer) { + const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + if (pValue < 0) return + const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + if (pValueContent < 0) return + return bipf.seekKey(buffer, pValueContent, B_ROOT) }, - seekPrivate: function (buffer) { - var p = 0 // note you pass in p! - p = bipf.seekKey(buffer, p, B_VALUE) - - if (~p) { - p = bipf.seekKey(buffer, p, B_META) - if (~p) return bipf.seekKey(buffer, p, B_PRIVATE) - } + seekPrivate(buffer) { + const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + if (pValue < 0) return + const pValueMeta = bipf.seekKeyCached(buffer, pValue, B_META) + if (pValueMeta < 0) return + return bipf.seekKey(buffer, pValueMeta, B_PRIVATE) }, - seekChannel: function (buffer) { - var p = 0 // note you pass in p! - p = bipf.seekKey(buffer, p, B_VALUE) - - if (~p) { - p = bipf.seekKey(buffer, p, B_CONTENT) - if (~p) return bipf.seekKey(buffer, p, B_CHANNEL) - } + seekChannel(buffer) { + const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + if (pValue < 0) return + const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + if (pValueContent < 0) return + return bipf.seekKey(buffer, pValueContent, B_CHANNEL) }, } diff --git a/test/prefix.js b/test/prefix.js index 6861598..24f8eb7 100644 --- a/test/prefix.js +++ b/test/prefix.js @@ -277,9 +277,8 @@ prepareAndRunTest('Prefix equal', dir, (t, db, raf) => { value: helpers.toBipf( '%wOtfXXopI3mTHL6F7Y3XXNtpxws9mQdaEocNJuKtAZo=.sha256' ), - indexType: 'vote', - indexName: - 'value_content_vote_link_%wOtfXXopI3mTHL6F7Y3XXNtpxws9mQdaEocNJuKtAZo=.sha256', + indexType: 'value_content_vote_link', + indexName: 'value_content_vote_link', prefix: 32, }, } From b5954b7d7d1d41a08925b6a179a74fba81a7b15b Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Mon, 4 Apr 2022 16:26:19 +0300 Subject: [PATCH 2/3] use bipf.seekKeyCached with string target arg --- index.js | 5 ++--- package.json | 2 +- test/helpers.js | 60 +++++++++++++++++++------------------------------ 3 files changed, 26 insertions(+), 41 deletions(-) diff --git a/index.js b/index.js index 26453c3..5a79a13 100644 --- a/index.js +++ b/index.js @@ -84,7 +84,6 @@ module.exports = function (log, indexesPath) { const B_TIMESTAMP = Buffer.from('timestamp') const B_SEQUENCE = Buffer.from('sequence') - const B_VALUE = Buffer.from('value') function loadIndexes(cb) { function parseIndexes(err, files) { @@ -260,14 +259,14 @@ module.exports = function (log, indexesPath) { function seekMinTimestamp(buffer) { const pTimestamp = bipf.seekKey(buffer, 0, B_TIMESTAMP) const arrivalTimestamp = bipf.decode(buffer, pTimestamp) - const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValue = bipf.seekKeyCached(buffer, 0, 'value') const pValueTimestamp = bipf.seekKey(buffer, pValue, B_TIMESTAMP) const declaredTimestamp = bipf.decode(buffer, pValueTimestamp) return Math.min(arrivalTimestamp, declaredTimestamp) } function seekSequence(buffer) { - const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValue = bipf.seekKeyCached(buffer, 0, 'value') const p = bipf.seekKey(buffer, pValue, B_SEQUENCE) return bipf.decode(buffer, p) } diff --git a/package.json b/package.json index cdd48a6..49fbad9 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "dependencies": { "atomic-file-rw": "^0.2.1", "binary-search-bounds": "^2.0.4", - "bipf": "^1.6.0", + "bipf": "^1.6.1", "crc": "3.6.0", "debug": "^4.2.0", "fastpriorityqueue": "^0.7.1", diff --git a/test/helpers.js b/test/helpers.js index 3393a34..d78957a 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -4,86 +4,72 @@ const bipf = require('bipf') -const B_KEY = Buffer.from('key') -const B_VALUE = Buffer.from('value') -const B_VOTE = Buffer.from('vote') -const B_LINK = Buffer.from('link') -const B_AUTHOR = Buffer.from('author') -const B_CONTENT = Buffer.from('content') -const B_TYPE = Buffer.from('type') -const B_ROOT = Buffer.from('root') -const B_META = Buffer.from('meta') -const B_ANIMALS = Buffer.from('animals') -const B_WORD = Buffer.from('word') -const B_PRIVATE = Buffer.from('private') -const B_CHANNEL = Buffer.from('channel') - module.exports = { toBipf(value) { return bipf.allocAndEncode(value) }, seekKey(buffer) { - return bipf.seekKey(buffer, 0, B_KEY) + return bipf.seekKey(buffer, 0, 'key') }, seekAuthor(buffer) { - const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValue = bipf.seekKeyCached(buffer, 0, 'value') if (pValue < 0) return - return bipf.seekKey(buffer, pValue, B_AUTHOR) + return bipf.seekKey(buffer, pValue, 'author') }, seekVoteLink(buffer) { - const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValue = bipf.seekKeyCached(buffer, 0, 'value') if (pValue < 0) return - const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + const pValueContent = bipf.seekKeyCached(buffer, pValue, 'content') if (pValueContent < 0) return - const pValueContentVote = bipf.seekKey(buffer, pValueContent, B_VOTE) + const pValueContentVote = bipf.seekKey(buffer, pValueContent, 'vote') if (pValueContentVote < 0) return - return bipf.seekKey(buffer, pValueContentVote, B_LINK) + return bipf.seekKey(buffer, pValueContentVote, 'link') }, seekType(buffer) { - const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValue = bipf.seekKeyCached(buffer, 0, 'value') if (pValue < 0) return - const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + const pValueContent = bipf.seekKeyCached(buffer, pValue, 'content') if (pValueContent < 0) return - return bipf.seekKey(buffer, pValueContent, B_TYPE) + return bipf.seekKey(buffer, pValueContent, 'type') }, seekAnimals(buffer) { - const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValue = bipf.seekKeyCached(buffer, 0, 'value') if (pValue < 0) return - const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + const pValueContent = bipf.seekKeyCached(buffer, pValue, 'content') if (pValueContent < 0) return - return bipf.seekKey(buffer, pValueContent, B_ANIMALS) + return bipf.seekKey(buffer, pValueContent, 'animals') }, pluckWord(buffer, start) { - return bipf.seekKey(buffer, start, B_WORD) + return bipf.seekKey(buffer, start, 'word') }, seekRoot(buffer) { - const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValue = bipf.seekKeyCached(buffer, 0, 'value') if (pValue < 0) return - const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + const pValueContent = bipf.seekKeyCached(buffer, pValue, 'content') if (pValueContent < 0) return - return bipf.seekKey(buffer, pValueContent, B_ROOT) + return bipf.seekKey(buffer, pValueContent, 'root') }, seekPrivate(buffer) { - const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValue = bipf.seekKeyCached(buffer, 0, 'value') if (pValue < 0) return - const pValueMeta = bipf.seekKeyCached(buffer, pValue, B_META) + const pValueMeta = bipf.seekKeyCached(buffer, pValue, 'meta') if (pValueMeta < 0) return - return bipf.seekKey(buffer, pValueMeta, B_PRIVATE) + return bipf.seekKey(buffer, pValueMeta, 'private') }, seekChannel(buffer) { - const pValue = bipf.seekKeyCached(buffer, 0, B_VALUE) + const pValue = bipf.seekKeyCached(buffer, 0, 'value') if (pValue < 0) return - const pValueContent = bipf.seekKeyCached(buffer, pValue, B_CONTENT) + const pValueContent = bipf.seekKeyCached(buffer, pValue, 'content') if (pValueContent < 0) return - return bipf.seekKey(buffer, pValueContent, B_CHANNEL) + return bipf.seekKey(buffer, pValueContent, 'channel') }, } From 9da245d2d53c431ae7ed4bbe6d1a35666fce87bb Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Mon, 4 Apr 2022 17:00:55 +0300 Subject: [PATCH 3/3] also seekKeyCached sequence and value.timestamp --- index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 5a79a13..4920919 100644 --- a/index.js +++ b/index.js @@ -82,9 +82,6 @@ module.exports = function (log, indexesPath) { else waiting.push(cb) } - const B_TIMESTAMP = Buffer.from('timestamp') - const B_SEQUENCE = Buffer.from('sequence') - function loadIndexes(cb) { function parseIndexes(err, files) { push( @@ -256,18 +253,20 @@ module.exports = function (log, indexesPath) { } } + const B_TIMESTAMP = Buffer.from('timestamp') + function seekMinTimestamp(buffer) { const pTimestamp = bipf.seekKey(buffer, 0, B_TIMESTAMP) const arrivalTimestamp = bipf.decode(buffer, pTimestamp) const pValue = bipf.seekKeyCached(buffer, 0, 'value') - const pValueTimestamp = bipf.seekKey(buffer, pValue, B_TIMESTAMP) + const pValueTimestamp = bipf.seekKeyCached(buffer, pValue, 'timestamp') const declaredTimestamp = bipf.decode(buffer, pValueTimestamp) return Math.min(arrivalTimestamp, declaredTimestamp) } function seekSequence(buffer) { const pValue = bipf.seekKeyCached(buffer, 0, 'value') - const p = bipf.seekKey(buffer, pValue, B_SEQUENCE) + const p = bipf.seekKeyCached(buffer, pValue, 'sequence') return bipf.decode(buffer, p) }