-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathcrdtLinear.js
231 lines (190 loc) · 6.08 KB
/
crdtLinear.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
import Identifier from './identifier';
import Char from './char';
class CRDT {
constructor(controller, base=32, boundary=10, strategy='random', mult=2) {
this.controller = controller;
this.vector = controller.vector;
this.struct = [];
this.siteId = controller.siteId;
this.text = "";
this.base = base;
this.boundary = boundary;
this.strategy = strategy;
this.strategyCache = [];
this.mult = mult;
}
handleLocalInsert(val, index) {
this.vector.increment();
const char = this.generateChar(val, index);
this.insertChar(index, char);
this.insertText(char.value, index);
this.controller.broadcastInsertion(char);
}
handleRemoteInsert(char) {
const index = this.findInsertIndex(char);
this.insertChar(index, char);
this.insertText(char.value, index);
this.controller.insertIntoEditor(char.value, index, char.siteId);
}
insertChar(index, char) {
this.struct.splice(index, 0, char);
}
handleLocalDelete(idx) {
this.vector.increment();
const char = this.struct.splice(idx, 1)[0];
this.deleteText(idx);
this.controller.broadcastDeletion(char);
}
handleRemoteDelete(char, siteId) {
const index = this.findIndexByPosition(char);
this.struct.splice(index, 1);
this.controller.deleteFromEditor(char.value, index, siteId);
this.deleteText(index);
}
findInsertIndex(char) {
let left = 0;
let right = this.struct.length - 1;
let mid, compareNum;
if (this.struct.length === 0 || char.compareTo(this.struct[left]) < 0) {
return left;
} else if (char.compareTo(this.struct[right]) > 0) {
return this.struct.length;
}
while (left + 1 < right) {
mid = Math.floor(left + (right - left) / 2);
compareNum = char.compareTo(this.struct[mid]);
if (compareNum === 0) {
return mid;
} else if (compareNum > 0) {
left = mid;
} else {
right = mid;
}
}
return char.compareTo(this.struct[left]) === 0 ? left : right;
}
findIndexByPosition(char) {
let left = 0;
let right = this.struct.length - 1;
let mid, compareNum;
if (this.struct.length === 0) {
throw new Error("Character does not exist in CRDT.");
}
while (left + 1 < right) {
mid = Math.floor(left + (right - left) / 2);
compareNum = char.compareTo(this.struct[mid]);
if (compareNum === 0) {
return mid;
} else if (compareNum > 0) {
left = mid;
} else {
right = mid;
}
}
if (char.compareTo(this.struct[left]) === 0) {
return left;
} else if (char.compareTo(this.struct[right]) === 0) {
return right;
} else {
throw new Error("Character does not exist in CRDT.");
}
}
generateChar(val, index) {
const posBefore = (this.struct[index - 1] && this.struct[index - 1].position) || [];
const posAfter = (this.struct[index] && this.struct[index].position) || [];
const newPos = this.generatePosBetween(posBefore, posAfter);
const localCounter = this.vector.localVersion.counter;
return new Char(val, localCounter, this.siteId, newPos);
}
retrieveStrategy(level) {
if (this.strategyCache[level]) return this.strategyCache[level];
let strategy;
switch (this.strategy) {
case 'plus':
strategy = '+';
break;
case 'minus':
strategy = '-';
break;
case 'random':
strategy = Math.round(Math.random()) === 0 ? '+' : '-';
break;
case 'every2nd':
strategy = ((level+1) % 2) === 0 ? '-' : '+';
break;
case 'every3rd':
strategy = ((level+1) % 3) === 0 ? '-' : '+';
break;
default:
strategy = ((level+1) % 2) === 0 ? '-' : '+';
break;
}
this.strategyCache[level] = strategy;
return strategy;
}
generatePosBetween(pos1, pos2, newPos=[], level=0) {
let base = Math.pow(this.mult, level) * this.base;
let boundaryStrategy = this.retrieveStrategy(level);
let id1 = pos1[0] || new Identifier(0, this.siteId);
let id2 = pos2[0] || new Identifier(base, this.siteId);
if (id2.digit - id1.digit > 1) {
let newDigit = this.generateIdBetween(id1.digit, id2.digit, boundaryStrategy);
newPos.push(new Identifier(newDigit, this.siteId));
return newPos;
} else if (id2.digit - id1.digit === 1) {
newPos.push(id1);
return this.generatePosBetween(pos1.slice(1), [], newPos, level+1);
} else if (id1.digit === id2.digit) {
if (id1.siteId < id2.siteId) {
newPos.push(id1);
return this.generatePosBetween(pos1.slice(1), [], newPos, level+1);
} else if (id1.siteId === id2.siteId) {
newPos.push(id1);
return this.generatePosBetween(pos1.slice(1), pos2.slice(1), newPos, level+1);
} else {
throw new Error("Fix Position Sorting");
}
}
}
/*
Math.random gives you a range that is inclusive of the min and exclusive of the max
so have to add and subtract ones to get them all into that format
if max - min <= boundary, the boundary doesn't matter
newDigit > min, newDigit < max
ie (min+1...max)
so, min = min + 1
if max - min > boundary and the boundary is negative
min = max - boundary
newDigit >= min, newDigit < max
ie (min...max)
if max - min > boundary and the boundary is positive
max = min + boundary
newDigit > min, newDigit <= max
ie (min+1...max+1)
so, min = min + 1 and max = max + 1
now all are (min...max)
*/
generateIdBetween(min, max, boundaryStrategy) {
if ((max - min) < this.boundary) {
min = min + 1;
} else {
if (boundaryStrategy === '-') {
min = max - this.boundary;
} else {
min = min + 1;
max = min + this.boundary;
}
}
return Math.floor(Math.random() * (max - min)) + min;
}
insertText(val, index) {
this.text = this.text.slice(0, index) + val + this.text.slice(index);
}
deleteText(index) {
this.text = this.text.slice(0, index) + this.text.slice(index + 1);
}
populateText() {
this.text = this.struct.map(char => char.value).join('');
}
}
export default CRDT;