-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpartition.js
56 lines (43 loc) · 1.34 KB
/
partition.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
module.exports = Partition
function Partition(partition, peers){
this.length = 0
this.partition = partition
def(this, 'peers', peers, 1)
def(this, 'head', undefined, 1)
def(this, 'tail', undefined, 1)
def(this, 'next', 1, 1)
}
Partition.prototype.slice = function(from){
let sliced = [], next = this.head
while (next && next.ptime != from) {
sliced.unshift(next)
next = next.prev
}
return sliced
}
Partition.prototype.append = function(change) {
if (change.ptime > this.next)
return deb('reject >', this.peers.me, this.partition, this.next, change.ptime), false
if (change.ptime < this.next)
return deb('reject <', this.peers.me, this.partition, this.next, change.ptime), false
if (!change.ptime)
change.ptime = this.next
if (this.head)
this.head.next = change
if (!this.tail)
this.tail = change
change.prev = this.head
this.head = change
this.next = (change.ptime + 1) % this.peers.constants.partitions.history.max
this.length++
// deb('applied', this.partition, this.head.ptime, this.length)
while (this.length > this.peers.constants.partitions.history.max) {
this.tail = this.tail.next
this.tail.prev = this.tail
this.length--
}
return change
}
const deb = require('./deb')('par'.bgRed.bold)
, { last } = require('./utils')
, { def } = require('utilise/pure')