-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbase.js
74 lines (58 loc) · 2.01 KB
/
base.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
module.exports = {}
module.exports.sort = sort
module.exports.resolution = {}
module.exports.config = {}
// mtu stands for maximum transmission unit
module.exports.config.mtu = 10
module.exports.config.max_history = 10
module.exports.config.sort = sort
module.exports.config.resolve = null
// Update rules are consequential. You need to be careful here. Three examples
// are provided.
module.exports.resolution.lww_vs_clock = lww_vs_clock
module.exports.resolution.lww_vs_current_vers = lww_vs_current_vers
module.exports.resolution.strictly_order_values = strictly_order_values
// Use gossip's .sort method to compare the current value to the value in the update. If the
// current value is sorted to before the update value, apply the update.
function strictly_order_values(gossip, update) {
var val = gossip.get(update.key).value
if(val === null) {
return true
}
return gossip.sort(val, update.value) < 0
}
// Last write wins, but compared against the current version of the key in the
// gossip's state.
function lww_vs_current_vers(gossip, update) {
var last_seen = gossip.get(update.key).version
, current_value = gossip.get(update.key).value
, current
current = {}
current.key = update.key
current.value = current_value === null ? update.value : current_value
current.source_id = gossip.id
current.version = last_seen
return sort(update, current) > 0
}
// Last write wins, but the update is compared against the version number in
// the vector clock
function lww_vs_clock(gossip, update) {
var last_seen = gossip.clock.get(update.source_id)
, current_value = gossip.get(update.key).value
, current
current = {}
current.key = update.key
current.value = current_value === null ? update.value : current_value
current.source_id = gossip.id
current.version = last_seen
return sort(update, current) > 0
}
function sort(A, B) {
var Afirst
if(A.version === B.version) {
Afirst = A.source_id < B.source_id
} else {
Afirst = A.version < B.version
}
return Afirst ? -1 : 1
}