# all files / lib/spdy-transport/ priority.js

100% Statements 94/94
100% Branches 28/28
100% Functions 16/16
100% Lines 94/94

    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

    1×
    1×

    1×
    1×

    1×
    1001×

    1001×
    1001×
    1001×

    1001×
    1001×
    1001×

    1001×

    1001×
    544×

    1×
    160×

    1×
    258×

    1×
    1688×

    1×
    5×

    1×
    553×
    553×
    553×

    553×

    1×
    7×

    7×
    7×

    7×
    3×

    1×
    7×
    7×
    7×

    7×

    1×
    2×
    2×
    2×
    2×

    1×
    1260×
    1260×
    1260×

    1260×
    1260×
    707×
    707×

    707×

    707×

    1×
    457×
    457×
    457×

    457×
    457×

    457×

    1×

    1×
    9×

    1×
    1003×
    1×

    1002×
    1002×
    1×

    1001×

    1001×
    1001×
    2×

    1001×

    1001×

    1001×
    2×
    6×

    1001×
    1001×
    3×
    3×

    1001×
    544×

    1001×

    1×
    36×

    1×
    249×
    249×

    1×
    7×
    7×

     'use strict'

    var transport = require('../spdy-transport')
    var utils = transport.utils

    var assert = require('assert')
    var debug = require('debug')('spdy:priority')

    function PriorityNode (tree, options) {
      this.tree = tree

      this.id = options.id
      this.parent = options.parent
      this.weight = options.weight

      // To be calculated in `addChild`
      this.priorityFrom = 0
      this.priorityTo = 1
      this.priority = 1

      this.children = {
        list: [],
        weight: 0
      }

      if (this.parent !== null) {
        this.parent.addChild(this)
      }
    }

    function compareChildren (a, b) {
      return a.weight === b.weight ? a.id - b.id : a.weight - b.weight
    }

    PriorityNode.prototype.toJSON = function toJSON () {
      return {
        parent: this.parent,
        weight: this.weight,
        exclusive: this.exclusive
      }
    }

    PriorityNode.prototype.getPriority = function getPriority () {
      return this.priority
    }

    PriorityNode.prototype.getPriorityRange = function getPriorityRange () {
      return { from: this.priorityFrom, to: this.priorityTo }
    }

    PriorityNode.prototype.addChild = function addChild (child) {
      child.parent = this
      utils.binaryInsert(this.children.list, child, compareChildren)
      this.children.weight += child.weight

      this._updatePriority(this.priorityFrom, this.priorityTo)
    }

    PriorityNode.prototype.remove = function remove () {
      assert(this.parent, 'Can\'t remove root node')

      this.parent.removeChild(this)
      this.tree._removeNode(this)

      // Move all children to the parent
      for (var i = 0; i < this.children.list.length; i++) {
        this.parent.addChild(this.children.list[i])
      }
    }

    PriorityNode.prototype.removeChild = function removeChild (child) {
      this.children.weight -= child.weight
      var index = utils.binarySearch(this.children.list, child, compareChildren)
      assert(index !== -1)

      // Remove the child
      this.children.list.splice(index, 1)
    }

    PriorityNode.prototype.removeChildren = function removeChildren () {
      var children = this.children.list
      this.children.list = []
      this.children.weight = 0
      return children
    }

    PriorityNode.prototype._updatePriority = function _updatePriority (from, to) {
      this.priority = to - from
      this.priorityFrom = from
      this.priorityTo = to

      var weight = 0
      for (var i = 0; i < this.children.list.length; i++) {
        var node = this.children.list[i]
        var nextWeight = weight + node.weight

        node._updatePriority(
          from + this.priority * (weight / this.children.weight),
          from + this.priority * (nextWeight / this.children.weight)
        )
        weight = nextWeight
      }
    }

    function PriorityTree (options) {
      this.map = {}
      this.list = []
      this.defaultWeight = options.defaultWeight || 16

      this.count = 0
      this.maxCount = options.maxCount

      // Root
      this.root = this.add({
        id: 0,
        parent: null,
        weight: 1
      })
    }
    module.exports = PriorityTree

    PriorityTree.create = function create (options) {
      return new PriorityTree(options)
    }

    PriorityTree.prototype.add = function add (options) {
      if (options.id === options.parent) {
        return this.addDefault(options.id)
      }

      var parent = options.parent === null ? null : this.map[options.parent]
      if (parent === undefined) {
        return this.addDefault(options.id)
      }

      debug('add node=%d parent=%d weight=%d exclusive=%d',
            options.id,
            options.parent === null ? -1 : options.parent,
            options.weight || this.defaultWeight,
            options.exclusive ? 1 : 0)

      var children
      if (options.exclusive) {
        children = parent.removeChildren()
      }

      var node = new PriorityNode(this, {
        id: options.id,
        parent: parent,
        weight: options.weight || this.defaultWeight
      })
      this.map[options.id] = node

      if (options.exclusive) {
        for (var i = 0; i < children.length; i++) {
          node.addChild(children[i])
        }
      }

      this.count++
      if (this.count > this.maxCount) {
        debug('hit maximum remove id=%d', this.list[0].id)
        this.list.shift().remove()
      }

      // Root node is not subject to removal
      if (node.parent !== null) {
        this.list.push(node)
      }

      return node
    }

    // Only for testing, should use `node`'s methods
    PriorityTree.prototype.get = function get (id) {
      return this.map[id]
    }

    PriorityTree.prototype.addDefault = function addDefault (id) {
      debug('creating default node')
      return this.add({ id: id, parent: 0, weight: this.defaultWeight })
    }

    PriorityTree.prototype._removeNode = function _removeNode (node) {
      delete this.map[node.id]
      this.count--
    }