|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const distance = require('xor-distance') |
| 4 | +const utils = require('./utils') |
| 5 | +const map = require('async/map') |
| 6 | + |
| 7 | +/** |
| 8 | + * Maintains a list of peerIds sorted by distance from a DHT key. |
| 9 | + */ |
| 10 | +class PeerDistanceList { |
| 11 | + /** |
| 12 | + * Creates a new PeerDistanceList. |
| 13 | + * |
| 14 | + * @param {Buffer} originDhtKey - the DHT key from which distance is calculated |
| 15 | + * @param {number} capacity - the maximum size of the list |
| 16 | + */ |
| 17 | + constructor (originDhtKey, capacity) { |
| 18 | + this.originDhtKey = originDhtKey |
| 19 | + this.capacity = capacity |
| 20 | + this.peerDistances = [] |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * The length of the list |
| 25 | + */ |
| 26 | + get length () { |
| 27 | + return this.peerDistances.length |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * The peerIds in the list, in order of distance from the origin key |
| 32 | + */ |
| 33 | + get peers () { |
| 34 | + return this.peerDistances.map(pd => pd.peerId) |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Add a peerId to the list. |
| 39 | + * |
| 40 | + * @param {PeerId} peerId |
| 41 | + * @param {function(Error)} callback |
| 42 | + * @returns {void} |
| 43 | + */ |
| 44 | + add (peerId, callback) { |
| 45 | + if (this.peerDistances.find(pd => pd.peerId.id.equals(peerId.id))) { |
| 46 | + return callback() |
| 47 | + } |
| 48 | + |
| 49 | + utils.convertPeerId(peerId, (err, dhtKey) => { |
| 50 | + if (err) { |
| 51 | + return callback(err) |
| 52 | + } |
| 53 | + |
| 54 | + const el = { |
| 55 | + peerId, |
| 56 | + distance: distance(this.originDhtKey, dhtKey) |
| 57 | + } |
| 58 | + |
| 59 | + this.peerDistances.push(el) |
| 60 | + this.peerDistances.sort((a, b) => distance.compare(a.distance, b.distance)) |
| 61 | + this.peerDistances = this.peerDistances.slice(0, this.capacity) |
| 62 | + |
| 63 | + callback() |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Indicates whether any of the peerIds passed as a parameter are closer |
| 69 | + * to the origin key than the furthest peerId in the PeerDistanceList. |
| 70 | + * |
| 71 | + * @param {Array<PeerId>} peerIds |
| 72 | + * @param {function(Error, Boolean)} callback |
| 73 | + * @returns {void} |
| 74 | + */ |
| 75 | + anyCloser (peerIds, callback) { |
| 76 | + if (!peerIds.length) { |
| 77 | + return callback(null, false) |
| 78 | + } |
| 79 | + |
| 80 | + if (!this.length) { |
| 81 | + return callback(null, true) |
| 82 | + } |
| 83 | + |
| 84 | + map(peerIds, (peerId, cb) => utils.convertPeerId(peerId, cb), (err, dhtKeys) => { |
| 85 | + if (err) { |
| 86 | + return callback(err) |
| 87 | + } |
| 88 | + |
| 89 | + const furthestDistance = this.peerDistances[this.peerDistances.length - 1].distance |
| 90 | + for (const dhtKey of dhtKeys) { |
| 91 | + const keyDistance = distance(this.originDhtKey, dhtKey) |
| 92 | + if (distance.compare(keyDistance, furthestDistance) < 0) { |
| 93 | + return callback(null, true) |
| 94 | + } |
| 95 | + } |
| 96 | + return callback(null, false) |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +module.exports = PeerDistanceList |
0 commit comments