Code coverage report for node/peer_heap.js

Statements: 78.81% (119 / 151)      Branches: 71.74% (33 / 46)      Functions: 81.25% (13 / 16)      Lines: 78.81% (119 / 151)      Ignored: none     

All files » node/ » peer_heap.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295                                            1       1 620   620     620     1 3304   3304 19     3285 3285 743   2542     3285 727                     2558 2558   2558       1 2542   2542 2542       2542     1 743           743 743 767   767 767       767 16     751 751 16 16 12 8   4     4       727   727     1 1240   1240 693 693 693 693 693   1240     1 696   696 696 696 696     1                   1                       1 3   3       3 3 3                           1 696   696 696 696 696   696 696     1                                       1 4106   4106 4274 4274 3842     432 432 432   110     432 168 168   264         1 2244   2244 720 720 240 240   480       1764     1 408   408 408   408 408 408 408     1 696   696 696 696 696     1 1555   1555 7     1548       1548 1548 1548    
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
'use strict';
 
module.exports = PeerHeap;
 
// A max-score (pre-computed) for peer selection
 
function PeerHeap() {
    var self = this;
 
    self.array = [];
    // TODO: worth it to keep a tail free list like TimeHeap?
    // self.end = 0;
    self._stack = [];
}
 
PeerHeap.prototype.choose = function choose(threshold, filter) {
    var self = this;
 
    if (!self.array.length) {
        return null;
    }
 
    var el;
    if (filter) {
        el = self._chooseFilteredEl(threshold, filter);
    } else {
        el = self._chooseEl(threshold);
    }
 
    if (!el) {
        return null;
    }
 
    // TODO: rescore could be deferred:
    // - store a pendingRescore bit
    // - set a next tick to rescore and clear bit
    // - if asked to choose a peer while pendingRescore is still set, take the
    //   hit and rescore then (do not clear bit)
    //
    // However such cleverness might just slow down the throughput of a very
    // busy server rather than help.
    el.score = el.peer.state.shouldRequest();
    self.siftdown(0);
 
    return el.peer;
 
};
 
PeerHeap.prototype._chooseEl = function _chooseEl(threshold) {
    var self = this;
 
    var el = self.array[0];
    Iif (el.score <= threshold) { // TODO: why inclusive?
        return null;
    }
 
    return el;
};
 
PeerHeap.prototype._chooseFilteredEl = function _chooseFilteredEl(threshold, filter) {
    var self = this;
 
    // TODO: is it worth it to unroll the first iteration of the loop below so
    // that we incur minimal overhead for "the top of heap is okay" case?
 
    // NOTE: assumes self._stack starts off empty
    self._stack.push(0);
    while (self._stack.length) {
        var i = self._stack.shift();
 
        var el = self.array[i];
        Iif (el.score <= threshold) { // TODO: why inclusive?
            break;
        }
 
        if (!filter || filter(el.peer)) {
            return el;
        }
 
        var left = 2 * i + 1;
        if (left < self.array.length) {
            var right = left + 1;
            if (right < self.array.length) {
                if (self.array[right].score > self.array[left].score) {
                    self._stack.push(right, left);
                } else {
                    self._stack.push(left, left);
                }
            } else {
                self._stack.push(left);
            }
        }
    }
    self._stack.length = 0;
 
    return null;
};
 
PeerHeap.prototype.clear = function clear() {
    var self = this;
 
    for (var i = 0; i < self.array.length; i++) {
        var el = self.array[i];
        el.heap = null;
        el.peer = null;
        el.score = 0;
        el.index = 0;
    }
    self.array.length = 0;
};
 
PeerHeap.prototype.add = function add(peer) {
    var self = this;
 
    var score = peer.state.shouldRequest();
    var i = self.push(peer, score);
    var el = self.array[i];
    return el;
};
 
PeerHeap.prototype.rescore = function rescore() {
    var self = this;
 
    for (var i = 0; i < self.array.length; i++) {
        var el = self.array[i];
        el.score = el.peer.state.shouldRequest();
    }
    self.heapify();
};
 
PeerHeap.prototype.heapify = function heapify() {
    var self = this;
 
    if (self.array.length <= 1) {
        return;
    }
 
    for (var i = Math.floor(self.array.length / 2 - 1); i >= 0; i--) {
        self.siftdown(i);
    }
};
 
PeerHeap.prototype.remove = function remove(i) {
    var self = this;
 
    Iif (i >= self.array.length) {
        return;
    }
 
    Eif (self.array.length === 1) {
        self.array.pop();
        return;
    }
 
    var j = self.array.length - 1;
    if (i === j) {
        self.array.pop();
        return;
    }
 
    self.swap(i, j);
    self.array.pop();
    self.siftup(i);
};
 
PeerHeap.prototype.push = function push(peer, score) {
    var self = this;
 
    var el = new PeerHeapElement(self);
    el.peer = peer;
    el.score = score;
    el.index = self.array.length;
 
    self.array.push(el);
    return self.siftup(el.index);
};
 
PeerHeap.prototype.pop = function pop() {
    var self = this;
    var peer = null;
 
    if (!self.array.length) {
        return peer;
    }
 
    if (self.array.length === 1) {
        peer = self.array.pop();
        return peer;
    }
 
    peer = self.array[0].peer;
    self.array[0] = self.array.pop();
    self.siftdown(0);
 
    return peer;
};
 
PeerHeap.prototype.siftdown = function siftdown(i) {
    var self = this;
 
    while (true) {
        var left = (2 * i) + 1;
        if (left >= self.array.length) {
            return i;
        }
 
        var right = left + 1;
        var child = left;
        if (right < self.array.length &&
            self.array[right].score > self.array[left].score) {
            child = right;
        }
 
        if (self.array[child].score > self.array[i].score) {
            self.swap(i, child);
            i = child;
        } else {
            return i;
        }
    }
};
 
PeerHeap.prototype.siftup = function siftup(i) {
    var self = this;
 
    while (i > 0) {
        var par = Math.floor((i - 1) / 2);
        if (self.array[i].score > self.array[par].score) {
            self.swap(i, par);
            i = par;
        } else {
            return i;
        }
    }
 
    return 0;
};
 
PeerHeap.prototype.swap = function swap(i, j) {
    var self = this;
 
    var a = self.array[i];
    var b = self.array[j];
 
    self.array[i] = b;
    self.array[j] = a;
    b.index = i;
    a.index = j;
};
 
function PeerHeapElement(heap) {
    var self = this;
 
    self.heap = heap;
    self.peer = null;
    self.score = 0;
    self.index = 0;
}
 
PeerHeapElement.prototype.rescore = function rescore(score) {
    var self = this;
 
    if (!self.heap) {
        return;
    }
 
    Iif (score === undefined) {
        score = self.peer.state.shouldRequest();
    }
 
    self.score = score;
    self.index = self.heap.siftup(self.index);
    self.index = self.heap.siftdown(self.index);
};