/
util.js
428 lines (412 loc) · 15.3 KB
/
util.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//
// Test and debug utilities.
//
// Copyright (c) The Dojo Foundation 2011. All Rights Reserved.
// Copyright (c) IBM Corporation 2008, 2011. All Rights Reserved.
//
define([
'coweb/jsoe/OperationEngine'
], function(OperationEngine) {
// track all op engine clients publicly for reset
var util = {};
util.all_clients = [];
// declare client class
var OpEngClient = function(site, state, keepFrozen) {
var all = util.all_clients;
this.eng = new OperationEngine(site);
this.state = state;
this.incoming = [];
all.push(this);
if(!keepFrozen) {
for(var i=0, l=all.length; i<l; i++) {
var item = all[i];
item.eng.thawSite(site);
}
}
};
util.OpEngClient = OpEngClient;
OpEngClient.prototype.send = function(op) {
for(var i=0; i < util.all_clients.length; i++) {
var client = util.all_clients[i];
if(client != this)
client.incoming.push(op);
}
};
OpEngClient.prototype.recv = function() {
var op = this.incoming[0];
this.incoming = this.incoming.slice(1);
if(op) {
this.remote(op);
return true;
}
return false;
};
OpEngClient.prototype.recvSome = function(count) {
while(count-- > 0 && this.incoming.length) {
this.recv();
}
};
OpEngClient.prototype.recvAll = function() {
this.recvSome(this.incoming.length);
};
OpEngClient.prototype.syncWith = function(client) {
this.eng.pushSync(client.eng.siteId, client.eng.copyContextVector());
};
OpEngClient.prototype.getStateFrom = function(client) {
// copy site state
this.state = {};
for(var key in client.state) {
this.state[key] = client.state[key];
}
// copy engine state
// do json encode / decode to produce an independent copy easily
// (and to test speed for real world scenario)
var es = client.eng.getState();
var json = JSON.stringify(es);
es = JSON.parse(json);
this.eng.setState(es);
};
OpEngClient.prototype.purge = function() {
return this.eng.purge();
};
OpEngClient.prototype.local = function(key, value, type, position) {
this._assertValid(key, value, type, position);
var op = this.eng.push(true, key, value, type, position);
this._updateState(op);
return op;
};
OpEngClient.prototype.remote = function(op) {
// make a copy before transforming because everything is local here
op = this.eng.pushRemoteOp(op.copy());
this._updateState(op);
return op;
};
OpEngClient.prototype.remoteRaw = function(key, value, type, position,
site, cv) {
if(this.eng.siteId == site) {
// ignore own incoming events
return;
}
var op = this.eng.push(false, key, value, type, position, site, cv);
this._updateState(op);
return op;
};
OpEngClient.prototype._assertValid = function(key, value, type, position){
var curr = this.state[key];
if(curr === undefined) throw new Error('invalid key for '+type);
if(type == 'update') {
// let -1 mean the whole shebang
if(position >= curr.length || position < -1) {
throw new Error('invalid position for update');
}
} else if(type == 'insert') {
if(position > curr.length || position < 0) {
throw new Error('invalid position for insert');
}
} else if(type == 'delete') {
if(position >= curr.length || position < 0) {
throw new Error('invalid position for delete');
}
}
};
OpEngClient.prototype._updateState = function(op) {
if(!op) {
return;
}
var curr = this.state[op.key] || '';
if(op.type == 'update') {
var p = op.position;
if(p >= 0) {
curr = curr.slice(0,p) + op.value + curr.slice(p+1);
} else {
curr = op.value;
}
} else if(op.type == 'insert') {
curr = curr.slice(0, op.position) + op.value + curr.slice(op.position);
} else if(op.type == 'delete') {
curr = curr.slice(0, op.position) + curr.slice(op.position+1);
}
this.state[op.key] = curr;
};
return util;
});
// tests.util.replayOperations = function(log, keepFrozen) {
// var params = log.params;
// var history = log.history;
//
// var sites = [];
// // build all simulated sites
// for(var i=0; i < params.numSites; i++) {
// var state = {};
// // build initial state as strings
// for(var t=0; t < params.topics.length; t++) {
// var topic = params.topics[t];
// state[topic] = '';
// }
// sites.push(new coweb.util.OpEngClient(i, state, keepFrozen));
// }
//
// var tmpl = 'event {0} ({1}) at site {2}';
// // run events in order of occurrence except the last one
// return {
// typeMap : {
// UpdateOperation : 'update',
// InsertOperation : 'insert',
// DeleteOperation : 'delete'
// },
// total : history.length,
// sites : sites,
// curr : 0,
// next : function(n, skip) {
// var elem;
// n = n || 1;
// for(var i=this.curr; i < this.curr+n; i++) {
// elem = history[i];
// if(elem == undefined) { break; }
// console.log(dojo.replace(tmpl, [i, elem.action, elem.at]));
// if(!skip) {
// this._doOp(elem, i);
// this.dump();
// }
// }
// this.curr += n;
// elem = history[this.curr];
// if(elem) {
// console.log('[next: '+
// dojo.replace(tmpl, [this.curr, elem.action, elem.at])+']');
// }
// },
// dump : function() {
// dojo.forEach(this.sites, function(site) {
// var eng = site.eng;
// console.log('siteId: ', eng.siteId);
// console.log(' state: ', dojo.toJson(site.state));
// console.log(' cv: ', eng.cv.toString());
// console.log(' history size: ', eng.hb.size);
// }, this);
// },
// _doOp : function(item, i) {
// var site = this.sites[item.at];
// if(item.action == 'purge') {
// return site.purge();
// } else if(item.action == 'send') {
// return site.local(item.topic,
// item.value,
// this.typeMap[item.type],
// item.position);
// } else if(item.action == 'recv') {
// return site.remoteRaw(item.topic,
// item.value,
// this.typeMap[item.type],
// item.position,
// item.siteId,
// dojo.fromJson(item.contextVector));
// }
// }
// };
// };
//
// tests.util.scriptOperations = function(log) {
// var params = log.params;
// var history = log.history;
//
// var script = ['var sites = [];', 'var op;'];
// // build all simulated sites
// for(var i=0; i < params.numSites; i++) {
// var state = {};
// // build initial state as strings
// for(var t=0; t < params.topics.length; t++) {
// var topic = params.topics[t];
// state[topic] = '';
// }
// state = dojo.toJson(state);
// script.push('sites.push(new coweb.util.OpEngClient('+i+','+state+'));');
// }
//
// var typeMap = {
// UpdateOperation : 'update',
// InsertOperation : 'insert',
// DeleteOperation : 'delete'
// };
//
// dojo.forEach(history, function(item) {
// var site = 'sites['+item.at+']';
// var tmpl, args = {site : site};
// if(item.action == 'purge') {
// tmpl = '{site}.purge();';
// } else if(item.action == 'send') {
// tmpl = 'op = {site}.local("{topic}", {value}, "{type}", {position});';
// args.topic = item.topic;
// args.value = item.value ? '"'+item.value+'"' : null;
// args.type = typeMap[item.type];
// args.position = item.position;
// script.push(dojo.replace(tmpl, args));
// tmpl = '{site}.send(op);';
// script.push(dojo.replace(tmpl, args));
// } else if(item.action == 'recv') {
// tmpl = '{site}.recv();';
// // args.topic = item.topic;
// // args.value = item.value;
// // args.type = typeMap[item.type];
// // args.position = item.position;
// // args.siteId = item.siteId;
// // args.contextVector = item.contextVector;
// script.push(dojo.replace(tmpl, args));
// }
// });
// return script.join('\n');
// };
//
// tests.util.randomOperations = function(numSites, toSend, topics, prob) {
// var log = {
// sites : [],
// history : [],
// params : {numSites : numSites, toSend : toSend, topics : topics, prob : prob}
// };
// var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
// var randint = function(upper) {
// return Math.floor(Math.random()*upper);
// }
// var genUpdate = function(site, key) {
// var value = site.state[key];
// if(!value) {
// // can't do an update; no content
// return null;
// }
// // pick a random character to flip lower/upper
// var i = randint(value.length);
// if(value.charCodeAt(i) >= 97) {
// var ch = value.charAt(i).toUpperCase();
// } else {
// var ch = value.charAt(i).toLowerCase();
// }
// // apply the op locally
// return site.local(key, ch, 'update', i);
// };
//
// var genInsert = function(site, key) {
// var value = site.state[key];
// if(value.length >= 20) {
// // keep string length bounded for experimentation
// return null;
// }
// // pick a random letter
// var i = randint(letters.length);
// var ch = letters.charAt(i);
// // pick a random location
// i = randint(value.length);
// // apply the op locally
// return site.local(key, ch, 'insert', i);
// };
//
// var genDelete = function(site, key) {
// var value = site.state[key];
// if(value.length <= 0) {
// // can't delete; no content
// return null;
// }
// // pick a random location
// var i = randint(value.length);
// // apply the op locally
// return site.local(key, null, 'delete', i);
// };
//
// var ops = [genUpdate, genInsert, genDelete];
// var sites = log.sites;
// // build all simulated sites
// for(var i=0; i < numSites; i++) {
// var state = new Object();
// // build initial state as strings
// for(var t=0; t < topics.length; t++) {
// var topic = topics[t];
// state[topic] = '';
// }
// sites.push(new coweb.util.OpEngClient(i, state));
// }
//
// var toRecv = 0;
// var forceSend = false;
//
// // interleave sends and receives
// while(toSend > 0 || toRecv > 0) {
// var action, op=null;
// if(forceSend || (Math.random() < prob && toSend > 0)) {
// // pick a random site to send an event
// var sender = sites[randint(numSites)];
// // pick a random event
// while(op == null) {
// var gen = ops[randint(ops.length)];
// var topic = topics[randint(topics.length)];
// op = gen(sender, topic);
// }
// action = {
// action : 'send',
// at : sender.eng.siteId,
// topic : op.key,
// value : op.value,
// type : op.declaredClass.split('.')[4],
// position : op.position,
// siteId : op.siteId,
// seqId : op.seqId,
// contextVector : op.contextVector.toString(),
// stateBefore : sender.state[op.key]
// };
// log.history.push(action);
// // propagate to all sites
// sender.send(op);
// action.stateAfter = sender.state[op.key];
// // decrease number of sends remaining in the test
// toSend--;
// // increase number to receive
// toRecv += numSites;
// forceSend = false;
// } else if(toRecv > 0) {
// // pick a random site to receive an op
// var recvr = sites[toRecv % numSites];
// op = recvr.incoming[0];
// if(op) {
// action = {
// action : 'recv',
// at : recvr.eng.siteId,
// topic : op.key,
// value : op.value,
// type : op.declaredClass.split('.')[4],
// position : op.position,
// siteId : op.siteId,
// seqId : op.seqId,
// contextVector : op.contextVector.toString(),
// stateBefore : recvr.state[op.key]
// };
// log.history.push(action);
// // receive the op
// action.handled = recvr.recv();
// action.stateAfter = recvr.state[op.key];
// }
// // decrease the number of outstanding receives
// toRecv--;
// } else {
// // do a random purge if we're not doing anything else
// var purger = sites[randint(sites.length)];
// action = {action : 'purge', at : purger.eng.siteId};
// log.history.push(action);
// var mcv = purger.purge();
// action.mcv = (mcv) ? mcv.toString() : mcv;
// // force a send as the next event since we're sitting idle
// forceSend = true;
// }
// }
// return log;
// };
//
// tests.util.transformLog = function(name, line, context, scope, arguments) {
// if(context.search('_transform') > -1) {
// console.log('>', name, line, context);
// console.log(' siteId:', arguments[0].siteId, 'seqId:', arguments[0].seqId, 'cd:', arguments[1].getHistoryBufferKeys());
// } else if(context.search('transformWith') > -1) {
// var arr = dojo.map(arguments, 'return item;');
// console.log(name, line, context);
// dojo.forEach([scope, arr[0]], function(item) {
// console.log(item.declaredClass, 'siteId:', item.siteId, 'seqId:', item.seqId, 'contextVector:', item.contextVector.sites.toString(), 'origContextVector:', item.origContextVector.sites.toString(), 'value:', item.value, 'position:', item.position, 'origPosition:', item.origPosition);
// });
// }
// };