-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorClock.js
More file actions
53 lines (41 loc) · 1 KB
/
vectorClock.js
File metadata and controls
53 lines (41 loc) · 1 KB
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
class VectorClock {
constructor() {
this.clock = {}
}
increment(serverId) {
if(this.clock[serverId]) {
this.clock[serverId]++
} else {
this.clock[serverId] = 1
}
}
static compare(clock1, clock2) {
let clock1IsBefore = false
let clock2IsBefore = false
// Loop through all the nodes in clock1 and compare each with clock2's corresponding node
for(let node in clock1) {
if(clock1[node] < (clock2[node] || 0)) {
clock1IsBefore = true
} else if(clock1[node] > (clock2[node] || 0)) {
clock2IsBefore = true
}
}
for(let node in clock2) {
if((clock1[node] || 0) < clock2[node]) {
clock1IsBefore = true
} else if((clock1[node] || 0) > clock2[node]) {
clock2IsBefore = true
}
}
if(clock1IsBefore && clock2IsBefore) {
return 0
} else if( clock1IsBefore) {
return -1
} else if( clock2IsBefore) {
return 1
}
}
}
module.exports = {
VectorClock
}