-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
510 lines (458 loc) · 16.8 KB
/
cache.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
var _ = require('underscore'),
util = require('util'),
events = require('events').EventEmitter,
CLOCKSPEED = 1000
;
/*
==============================================================================================================
Helper functions.
Not in the global scope per-se, as node.js will create a closure for this Module.
==============================================================================================================
*/
var regexForDoubleExpression = /(\d+)\:(\d+)(?:>(\d+)\:(\d+))?/,
regexForIntExpression = /(\d+)(?:-(\d+))?(?:>(\d+)(?:-(\d+))?)?/;
// parse the definition of an Item Property to yeild a PropertySchema object for the Property.
function parsePropertyDefinition(propname, propval, schemaFn) {
var propertySchema = new PropertySchema(propname, propval);
if (propval.match(/^[$\[]/)) {
if (!propval.match(/^\$?\[(?:string|int|double)\([^\)]+\)/)) {
propval.val = 'invalid expression';
return propertySchema;
}
var defn = propval.match(/\[([^\]]+)\]/)[1];
var type = propertySchema.type = defn.match(/^[a-z]+/)[0];
var expression = defn.match(/\(([^\)]+)\)/)[1];
propertySchema.template = expression;
var match;
if(match = expression.match(/=(.+)/)) {
propertySchema.template = match[1];
propertySchema.updateFn = buildAdHocExpression(propertySchema);
if(type === 'double') {
var refProp;
var refPropMatch = propertySchema.template.match(/^([^\+\-\*\/]+)/);
if(refPropMatch) {
propertySchema.numericType = getNumericTypeFromRefProp(schemaFn, refPropMatch[1]);
if(!propertySchema.numericType) {
propval.val = 'invalid expression';
return propertySchema;
}
}
}
}
else if(match = expression.match(/\^(.+)/)) {
propertySchema.template = match[1];
propertySchema.updateFn = getChangeForCacheProp;
propertySchema.reportMode = '*';
if(type === 'double') {
propertySchema.numericType = getNumericTypeFromRefProp(schemaFn, propertySchema.template);
if(!propertySchema.numericType) {
propval.val = 'invalid expression';
return propertySchema;
}
}
}
else {
if(propertySchema.type === 'string') {
propertySchema.updateFn = getStringForCacheProp;
}
else if(propertySchema.type === 'int') {
propertySchema.updateFn = getIntForCacheProp;
}
else if(propertySchema.type === 'double') {
propertySchema.updateFn = getDoubleForCacheProp;
var numericTypeMatch = expression.match(regexForDoubleExpression);
propertySchema.numericType = {
scale:numericTypeMatch[1],
precision:numericTypeMatch[2]
};
}
}
if (propval.match(/^\$/))
propertySchema.isVolatile = false;
if (match = propval.match(/([^\]]+)$/))
propertySchema.reportMode = match[1];
}
return propertySchema;
}
function getNumericTypeFromRefProp(schemaFn, refPropName) {
if(refPropName) {
var refProp = schemaFn.prototype['__' + refPropName];
if(refProp) return refProp.numericType;
}
return null;
}
function buildAdHocExpression(propertySchema) {
var fn = '(function getExpressionForCacheProp(cacheprop){ var val; with(cacheprop.item){ val = ' + propertySchema.template + ' } return val; })';
fn = eval(fn);
return fn;
}
function getChangeForCacheProp(cacheprop) {
var template = cacheprop.schema.template;
var res = cacheprop.item['_' + template].change;
return res;
}
function camelCase(string) {
return string[0].toUpperCase() + string.substring(1);
}
function getStringForCacheProp(cacheprop) {
var template = cacheprop.schema.template;
var src = 'abcdefghijklmnopqrstuvwxyz';
var res = template.replace(/{[^}]+}/g, function (match, number) {
//console.log('match=' + match);
var matchdetails = /(\d+)(?:\:(\w))/.exec(match);
//console.log('matchdetails=' + matchdetails);
var length, modifier;
length = parseInt(matchdetails[1]);
if (matchdetails.length == 3) {
modifier = matchdetails[2];
}
var start = Math.floor((Math.random() * (src.length - length)) + 1);
//console.log('match=' + match + ', start=' + start + ', length=' + length);
var s = src.substring(start, start + length);
if (modifier === 'U') {
s = s.toUpperCase();
}
else if (modifier === 'C') {
s = camelCase(s);
}
return s;
});
return res;
}
function getNumberForCacheProp(cacheprop, regex, fnGet) {
// call cached update fn if we have already been here. Note that this is a per-instance cached fn
// and is different to the schema.updateFn (which is actually *this* function).
if(cacheprop.updateFn)
return cacheprop.updateFn();
var template = cacheprop.schema.template;
var match = regex.exec(template);
// check if we are using a 'first>then' strategy
if(template.match(/>/)) {
var fn = fnGet(match, 3);
// create cached update function for the subsequent calls which use the '>then' strategy
cacheprop.updateFn = function() {
var num = fn();
if(num % 2 == 1) num *= -1;
num = cacheprop.val + num;
return num;
};
}
else {
var fn = fnGet(match, 1);
// only a 'first' strategy; cache a simple function to return random number.
cacheprop.updateFn = function() {
return fn();
};
}
// first time - need to call the number generator to initiliase the property
var info = {}; // grab a copy of any relevant numeric info (scale/precision)
var num = fnGet(match, 1, info)();
cacheprop.numericInfo = info;
return num;
}
function getDoubleForCacheProp(cacheprop) {
return getNumberForCacheProp(cacheprop, regexForDoubleExpression, getDoubleFnFromMatch);
}
function getIntForCacheProp(cacheprop) {
return getNumberForCacheProp(cacheprop, regexForIntExpression, getIntFnFromMatch);
}
function getIntFnFromMatch(match, idx) {
var from = 0, to = parseInt(match[idx]);
if(match[idx + 1]) {
from = to;
to = parseInt(match[idx + 1]);
}
return function(){ return getInt(from, to); };
}
function getInt(min, max) {
return Math.floor((Math.random() * max) + min);
}
function getDoubleFnFromMatch(match, idx, info) {
var scale = parseInt(match[idx]);
var precision = parseInt(match[idx + 1]);
if(info) {
info.scale = scale;
info.precision = precision;
}
return function(){ return getDouble(scale, precision); };
}
function getDouble(scale, precision) {
var num = Math.random();
while(num < 0.1) num *= 10;
num = '' + (num * Math.pow(10, scale-precision));
var rex = '\\d+\\.\\d{' + precision + '}';
num = num.match(new RegExp(rex))[0];
return parseFloat(num);
}
var PropertySchema = function(propname, propval) {
this.name = propname;
this.type = null;
this.numericType = null;
this.val = propval;
this.isVolatile = true;
this.template = null;
this.updateFn = null;
this.reportMode = null;
};
PropertySchema.prototype.isNumeric = function() {
return 'int,double'.indexOf(this.type) > -1;
};
function isNumeric(val) {
return ('' + val).match(/^[\d\.]+$/);
}
/*
==============================================================================================================
CacheProperty: an instance of an Item property.
An Item instance will comprise one or more CacheProperties.
==============================================================================================================
*/
var CacheProperty = function(item, schema) {
this.item = item; // parent
this.schema = schema; // definition from __proto__
this.val = schema.val;
this.updateCount = 0;
this.change = 0;
this.initialUpdate = true;
this.init();
};
CacheProperty.prototype.update = function (iteration) {
if (this.schema.isVolatile) {
return this.updateProperty();
}
return false;
};
CacheProperty.prototype.applyChange = function (change) {
this.change = change;
this.val += change;
};
CacheProperty.prototype.init = function () {
this.updateProperty();
this.isNumeric = this.schema.isNumeric();
};
CacheProperty.prototype.updateProperty = function () {
if (this.schema.updateFn) {
var newval = this.schema.updateFn(this);
if(this.isNumeric)
this.change = newval - this.val;
var propname = this.schema.name;
this.val = this.item[propname] = newval;
this.updateCount++;
//console.log(this.item.id + '::' + propname + ' set to ' + this.val);
return true;
}
return false;
};
/*
==============================================================================================================
asSchemaFn: a mixin function to all common behaviour to schema functions.
A schemaFn is a constuctor function which creates a new Item based on the registered schema definition
for a prefix.
==============================================================================================================
*/
var asSchemaFn = function () {
this.proplist = [];
this.enabled = true;
this.iteratePropNames = function (cb) {
var my = this;
var i = 0;
_.each(this.proplist, function (prop) {
cb(prop, i++);
});
}
this.iterateCacheProperties = function (cb) {
var my = this;
var i = 0;
_.each(this.proplist, function (prop) {
cb(my['_' + prop], i++);
});
}
this.update = function (iteration) {
if(!this.enabled) return;
var updatedProps = 0;
if(iteration % this.volatility == 0) {
this.iterateCacheProperties(function(prop, propnum){
if(prop.update(iteration)) {
var propmask = (1 << propnum);
updatedProps |= propmask;
}
});
}
return updatedProps;
}
this.init = function () {
//console.log('schemaFn init');
var my = this;
this.iteratePropNames(function(prop){
var cachePropName = '_' + prop;
var schemaPropName = '__' + prop;
my[cachePropName] = new CacheProperty(my, my[schemaPropName]);
})
//console.log('initialised: ' + util.inspect(my));
}
this.report = function (initial, updatedProps, cbReport) {
var my = this;
var report = { isInitial: initial };
var my = this;
this.iterateCacheProperties(function(prop, propnum){
var propname = prop.schema.name;
if(prop.schema.reportMode === '*' || my.reportPropYN(initial, updatedProps, propnum)) {
var val = my[propname];
if(prop.schema.type === 'double') {
val = val.toFixed(prop.schema.numericType.precision);
}
report[propname] = '' + val;
}
})
cbReport(report);
}
this.reportPropYN = function (initial, updatedProps, propnum) {
if(initial) return true;
if(updatedProps) {
var propmask = (1 << propnum);
return (propmask & updatedProps) == propmask;
}
return false;
}
this.applyOverrides = function(options, my) {
if (options.overrides) {
for (prop in options.overrides) {
if (options.overrides.hasOwnProperty(prop)) {
var propval = options.overrides[prop];
var myprop = '_' + prop;
if (my[myprop]) {
my[myprop].val = propval;
my[prop] = propval;
}
}
}
}
}
return this;
};
/*
==============================================================================================================
Cache: the master repository where Items are created.
There is one Cache per connection. Each Cache contains:
- a Schema dictionary containing Item ctor functions for each registered prefix.
- an array (collection) of instantiated Items.
- an index of (collectionIdex) to instantiated Items, keyed by Id.
Schemas must first be Registered (by prefix - the leading characters of an id, e.g. 'si100' has a prefix
of 'si') before Items can be created via the Subscribe method.
e.g., (i) Register prefix of 'si' for a given schema.
(ii) Subscribe 'si100', 'si101' and 'si102'
==============================================================================================================
*/
var Cache = function (id) {
this.id = id;
this.reset();
};
util.inherits(Cache, events);
Cache.prototype.reset = function () {
this.schema = {};
this.collection = [];
this.collectionIndex = {};
this.started = false;
this.iteration = 0;
};
Cache.prototype.register = function (prefix, schema) {
if(!this.schema[prefix]) {
this.log('register', prefix + ' ' + util.inspect(schema));
var itemSchema = this.parseSchema(schema);
this.schema[prefix] = itemSchema;
}
};
Cache.prototype.subscribe = function (id, requestId, options) {
options = _.extend({volatility:null, overrides: null}, options);
var item = this.collectionIndex[id];
if(item) {
item.enabled = true;
}
else
{
this.log('subscribe', id + ' - creating new subscription');
var prefix = id.match(/^\D+/)[0];
//console.log('prefix=' + prefix);
var schema = this.schema[prefix];
if(schema) {
item = new schema(id, options);
this.collection.push(item);
this.collectionIndex[id] = item;
}
}
this.reportItem(item, true, null, requestId);
this.start();
};
Cache.prototype.unSubscribe = function (id, requestId) {
var item = this.collectionIndex[id];
if(item) {
item.enabled = false;
}
};
Cache.prototype.unsubscribeAll = function (id, requestId) {
_.each(this.collection, function(item){
item.enabled = false;
});
};
Cache.prototype.updateCache = function () {
var my = this;
var iteration = this.iteration++;
_.each(this.collection, function(item){
if(item.enabled){
var updatedprops = item.update(iteration);
if(updatedprops) {
my.reportItem(item, false, updatedprops);
}
}
});
};
Cache.prototype.reportItem = function(item, initial, updatedprops, requestId) {
var my = this;
item.report(initial, updatedprops, function(report){
my.emit('update', {update: report, requestId: requestId || 0});
});
};
Cache.prototype.start = function () {
if(this.started) return;
this.log('starting cache');
this.interval = setInterval(this.updateCache.bind(this), CLOCKSPEED);
this.started = true;
};
Cache.prototype.stop = function () {
this.log('stopping cache');
clearInterval(this.interval);
this.reset();
};
Cache.prototype.destroy = function () {
this.stop();
this.removeAllListeners();
};
Cache.prototype.log = function (type, detail) {
console.log('(Cache ' + this.id + ') ' + type.toUpperCase() + ' - ' + (detail || ''));
};
Cache.prototype.parseSchema = function (schema) {
// create unique ctor function for creating Items which conform to this schema
var schemaFn = function (id, options) {
this.id = id;
this.init();
this.applyOverrides(options, this); // schema properties can be overriden at construction-time
this.volatility = options.volatility || getInt(1,10);
}
// mixin some common schema function behaviour to the prototype of the unique schemaFn
asSchemaFn.call(schemaFn.prototype);
var propCount = 0;
for (prop in schema) {
if (schema.hasOwnProperty(prop)) {
var propval = schema[prop];
var propSchema = parsePropertyDefinition(prop, propval, schemaFn);
var protoSchemaProp = '__' + prop;
schemaFn.prototype[protoSchemaProp] = propSchema;
schemaFn.prototype.proplist.push(prop);
propCount += 1;
if(propCount > 32)
throw new Exception('Maximum limit of 32 Properties per Item exceded');
}
}
//this.log('schemaFn.prototype', util.inspect(schemaFn.prototype));
return schemaFn;
};
module.exports = Cache;