refactor range

This commit is contained in:
Andre Staltz 2023-09-08 13:46:18 +03:00
parent 317f7c0a05
commit fc30b3e2d2
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 9 additions and 6 deletions

View File

@ -1,7 +1,7 @@
const { BloomFilter } = require('bloom-filters') const { BloomFilter } = require('bloom-filters')
const MsgV3 = require('ppppp-db/msg-v3') const MsgV3 = require('ppppp-db/msg-v3')
const p = require('util').promisify const p = require('util').promisify
const { isEmptyRange, estimateMsgCount } = require('./range') const { EMPTY_RANGE, isEmptyRange, estimateMsgCount } = require('./range')
const { parseGoal } = require('./goal') const { parseGoal } = require('./goal')
/** /**
@ -27,7 +27,7 @@ class Algorithm {
haveRange(rootID) { haveRange(rootID) {
const rootMsg = this.#peer.db.get(rootID) const rootMsg = this.#peer.db.get(rootID)
if (!rootMsg) return [1, 0] if (!rootMsg) return EMPTY_RANGE
let minDepth = Number.MAX_SAFE_INTEGER let minDepth = Number.MAX_SAFE_INTEGER
let maxDepth = 0 let maxDepth = 0
for (const rec of this.#peer.db.records()) { for (const rec of this.#peer.db.records()) {
@ -62,7 +62,7 @@ class Algorithm {
#wantNewestRange(localHaveRange, remoteHaveRange, count) { #wantNewestRange(localHaveRange, remoteHaveRange, count) {
const [minLocalHave, maxLocalHave] = localHaveRange const [minLocalHave, maxLocalHave] = localHaveRange
const [minRemoteHave, maxRemoteHave] = remoteHaveRange const [minRemoteHave, maxRemoteHave] = remoteHaveRange
if (maxRemoteHave <= maxLocalHave) return [1, 0] if (maxRemoteHave <= maxLocalHave) return EMPTY_RANGE
const maxWant = maxRemoteHave const maxWant = maxRemoteHave
const size = Math.max(maxWant - maxLocalHave, count) const size = Math.max(maxWant - maxLocalHave, count)
const minWant = Math.max(maxWant - size, maxLocalHave + 1, minRemoteHave) const minWant = Math.max(maxWant - size, maxLocalHave + 1, minRemoteHave)
@ -87,8 +87,8 @@ class Algorithm {
* @returns {Range} * @returns {Range}
*/ */
wantRange(localHave, remoteHave, goal) { wantRange(localHave, remoteHave, goal) {
if (!goal) return [1, 0] if (!goal) return EMPTY_RANGE
if (isEmptyRange(remoteHave)) return [1, 0] if (isEmptyRange(remoteHave)) return EMPTY_RANGE
const { type, count } = parseGoal(goal) const { type, count } = parseGoal(goal)
if (type === 'all') { if (type === 'all') {
return this.#wantAllRange(localHave, remoteHave) return this.#wantAllRange(localHave, remoteHave)
@ -148,7 +148,7 @@ class Algorithm {
const tangle = this.#peer.db.getTangle(rootID) const tangle = this.#peer.db.getTangle(rootID)
const sorted = tangle.topoSort() const sorted = tangle.topoSort()
if (sorted.length <= count) return if (sorted.length <= count) return
const msgID = sorted[sorted.length - count] const msgID = sorted[sorted.length - count] // New "oldest dataful msg"
const { deletables, erasables } = tangle.getDeletablesAndErasables(msgID) const { deletables, erasables } = tangle.getDeletablesAndErasables(msgID)
const del = p(this.#peer.db.del) const del = p(this.#peer.db.del)
const erase = p(this.#peer.db.erase) const erase = p(this.#peer.db.erase)

View File

@ -23,7 +23,10 @@ function estimateMsgCount(range) {
else return estimate else return estimate
} }
const EMPTY_RANGE = [1, 0]
module.exports = { module.exports = {
isEmptyRange, isEmptyRange,
estimateMsgCount, estimateMsgCount,
EMPTY_RANGE
} }