-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSyncIt.js
More file actions
1359 lines (1235 loc) · 38.3 KB
/
Copy pathSyncIt.js
File metadata and controls
1359 lines (1235 loc) · 38.3 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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module.exports = (function(SyncIt_Constant, addEvents, addLocking, updateResult) {
"use strict";
// Author: Matthew Forrester <matt_at_keyboardwritescode.com>
// Copyright: Matthew Forrester
// License: MIT/BSD-style
var LOCKING = SyncIt_Constant.Locking;
var ERROR = SyncIt_Constant.Error;
var FOLLOW_INFORMATION_TYPE = SyncIt_Constant.FollowInformationType;
/**
* **_map()**
*
* Simple map function for when Array.map() is not available.
*
* * **@param {Array} `arr`** The array to filter.
* * **@param {Function} `filterFunc`** The function to use for filtering
*/
var _map = function(arr, func) {
if (arr.map) { return arr.map(func); }
var i, l,
r = [];
for (i=0, l=arr.length; i<l; i++) {
r.push(func(arr[i]));
}
return r;
};
/**
* **_filter()**
*
* Simple filter function for when Array.filter() is not available.
*
* * **@param {Array} `arr`** The array to filter.
* * **@param {Function} `filterFunc`** The function to use for filtering
*/
var _filter = function(arr,filterFunc) {
if (arr.filter) { return arr.filter(filterFunc); }
var i = 0,
l = 0,
r = [];
for (i = 0, l = arr.length; i < l; i++) {
if (filterFunc(arr[i])) {
r.push(arr[i]);
}
}
return r;
};
/**
* **_shallowCopyKeys()**
*
* Will make a shallow copy of `ob` containing `keysToCopy`. If `copyNullEtc`
* is true it will not copy `null` or `undefined` values.
*
* * **@param {Object} `ob`**
* * **@param {Array} `keysToCopy`**
* * **@param {Boolean} `copyNullEtc`**
*/
var _shallowCopyKeys = function(ob,keysToCopy,copyNullEtc) {
var k = '',
r = {};
for (k in ob) {
if (
ob.hasOwnProperty(k) &&
(
(keysToCopy === undefined) ||
(keysToCopy.indexOf(k) > -1)
)
) {
if (copyNullEtc || ((ob[k] !== null) && (ob[k] !== undefined))) {
r[k] = ob[k];
}
}
}
return r;
};
/**
* ## SyncIt
*
* ### new SyncIt()
*
* Constructor
*
* #### Parameters
*
* * **@param {SyncIt_Path_AsyncLocalStorage} `pathstore`** The instance to use for storage.
* * **@param {Modifier} `modifier`** The UNIQUE User/Device which is using the instance of SyncIt.
*/
var SyncIt = function(pathstore, modifier) {
this._ps = pathstore;
this._modifier = modifier;
this._cloneObj = function(ob) { return JSON.parse(JSON.stringify(ob)); };
this._autoClean = true;
};
/**
* **SyncIt._getPathWatcher()**
*
* This is used within SyncIt to connect information about that data stored in
* SyncIt._ps (An instance of SyncIt_Path_AsyncLocalStorage) and the logical
* values that are stored should all Pathitem be processed.
*/
SyncIt.prototype._getPathWatcher = function() {
var val = this._getEmptyStorerecord();
return {
/**
* **getWatcher()**
*
* Watches as SyncIt_Path_AsyncLocalStorage follows the path of Pathitem
* and collects information.
*
* This will also prevent
*/
getWatcher: function() { return function(key,item,inWhere) {
var k = '';
var keyInfo = key.split('.');
// Info stored at the root only exists once and can probably be
// seen as metadata, so just connect it.
if (inWhere == FOLLOW_INFORMATION_TYPE.INFO) {
val.j = item;
return ERROR.OK;
}
// This is not infact the true root, but the root of the Path, if
// the `dataset`/`datakey` has never been `SyncIt.advance()`'d then
// there will be no root so this will not be called.
if (inWhere == FOLLOW_INFORMATION_TYPE.ROOTITEM) {
for (k in item) {
if (item.hasOwnProperty(k)) {
val[k] = item[k];
}
}
val.s = keyInfo[0];
val.k = keyInfo[1];
if (item.hasOwnProperty('r') && item.r) {
return ERROR.DATA_ALREADY_REMOVED ;
}
return ERROR.OK;
}
// getWatcher is always used for collecting information about one
// path inside a `dataset`/`datakey`, however there is an
// opportunity to collect information about whether other paths
// exist or not. This will be an array of the non-follwed path.
if (inWhere == FOLLOW_INFORMATION_TYPE.OTHER_PATHS) {
val.p = item;
}
// For every Pathitem that is encounted, we will use updateResult
// to figure out the logical value should all the Pathitem be
// applied.
if (inWhere == FOLLOW_INFORMATION_TYPE.PATHITEM) {
if (!item.hasOwnProperty('t')) {
item.t = this._ps.getKeyTimeDecoder().call(
this._ps,
key
);
}
if (!item.hasOwnProperty('m')) {
item.m = this.getModifier();
}
val.q.push(item);
val = updateResult(
val,
item,
this._cloneObj
);
val.s = keyInfo[0];
val.k = keyInfo[1];
if (item.o == 'remove') {
return ERROR.DATA_ALREADY_REMOVED ;
}
}
return ERROR.OK;
}.bind(this); }.bind(this),
/**
* **getReaditem**
*
* Returns the data collected by `getWatcher`.
*/
getReaditem: function() {
return val;
}
};
};
/**
* ### SyncIt.setCloneFunction()
*
* Sometimes, SyncIt wants a deep copy of an Object, this function will allow
* you to change what function does that deep copying.
*
* #### Parameters
*
* @param {Function} cloneFunction
*/
SyncIt.prototype.setCloneFunction = function(cloneFunction) {
this._cloneObj = cloneFunction;
};
/**
* ### SyncIt.getModifier()
*
* #### Returns
*
* * **@return {Modifier}** The User/Device which is using the instance of SyncIt.
*/
SyncIt.prototype.getModifier = function() {
return this._modifier;
};
/**
* ### SyncIt.listenForConflictResolutionAddedToPath()
*
* Adds a listener for when data is added to the *Queue* during conflict resolution.
*
* #### Parameters
*
* * **@param {Function} `listener`** Signature: `function(dataset, datakey, queueitem, newStoreRecord)`.
* * **@param {Dataset} `listener.dataset`** The *dataset* of the updated.
* * **@param {Datakey} `listener.datakey`** The *datakey* that was updated.
* * **@param {Pathitem} `listener.queueitem`** The *queueitem* that was just added.
* * **@param {Storerecord} `listener.newStorerecord`** The *storerecord* which is now stored.
*/
SyncIt.prototype.listenForConflictResolutionAddedToPath = function(listener) {
return this.listen('conflict_resolution_items_added_to_queue', listener);
};
/**
* ### SyncIt.listenForDataChange()
*
* Adds a listener for when data is changed, through whatever method.
*
* NOTE: This callback is not in the normal form... no queueitem is specified in the callback.
*
* #### Parameters
*
* * **@param {Function} `listener`** Signature: `function(newStoreRecord, queueitem)`.
* * **@param {Storerecord} `listener.newStorerecord`** The *storerecord* which is now stored.
* * **@param {Pathitem} `listener.queueitem`** The *queueitem* which caused the update. Note this may not map to the Storerecord when conflict resolution is occuring.
*/
SyncIt.prototype.listenForDataChange = function(listener) {
return this.listen('data_change', listener);
};
/**
* ### SyncIt.listenForAddedToPath()
*
* Adds a listener for when data is added to the *Queue* under normal circumstances (ie not during conflict resolution).
*
* #### Parameters
*
* * **@param {Function} `listener`** Signature: `function(dataset, datakey, queueitem, newStoreRecord)`.
* * **@param {Dataset} `listener.dataset`** The *dataset* of the updated.
* * **@param {Datakey} `listener.datakey`** The *datakey* that was updated.
* * **@param {Pathitem} `listener.queueitem`** The *queueitem* that was just added.
* * **@param {Storerecord} `listener.newStorerecord`** The *storerecord* which is now stored.
*/
SyncIt.prototype.listenForAddedToPath = function(listener) {
return this.listen('added_to_queue', listener);
};
/**
* ### SyncIt.listenForAdvanced()
*
* Adds a listener for when data is advanced to the *Store*.
*
* #### Parameters
*
* * **@param {Function} `listener`** Signature: `function(dataset, datakey, queueitem, newStorerecord)`.
* * **@param {Dataset} `listener.dataset`** The *dataset* of the advanced.
* * **@param {Datakey} `listener.datakey`** The *datakey* that was advanced.
* * **@param {Pathitem} `listener.queueitem`** The *queueitem* that was advanced.
* * **@param {Storerecord} `listener.newStorerecord`** The *storerecord* which is now stored.
*/
SyncIt.prototype.listenForAdvanced = function(listener) {
return this.listen('advanced', listener);
};
/**
* ### SyncIt.listenForFed()
*
* Adds a listener for when data is fed using [SyncIt.feed()](#syncit.feed--)
*
* #### Parameters
*
* * **@param {Function} `listener`** Signature: `function(queueitem, newStorerecord)`.
* * **@param {String} `listener.dataset`** The dataset of the just fed Queueitem.
* * **@param {String} `listener.datakey`** The datakey of the just fed Queueitem.
* * **@param {Queueitem} `listener.queueitem`** The *queueitem* that was advanced.
* * **@param {Storerecord} `listener.newStorerecord`** The *storerecord* which is now stored.
*/
SyncIt.prototype.listenForFed = function(listener) {
return this.listen('fed', listener);
};
/**
* ### SyncIt.set()
*
* Will add a *Pathitem* that represents a complete overwrite of any existing data.
*
* #### Parameters
*
* * **@param {Dataset} `dataset`**
* * **@param {Datakey} `datakey`**
* * **@param {Update} `update`**
* * **@param {Function} `whenAddedToQueue`** Fired after the *Queue* has been updated. See [SyncIt._addToQueue()](#syncit._addtoqueue--)** for documentation.
*/
SyncIt.prototype.set = function(dataset, datakey, update, whenAddedToQueue) {
return this._addToQueue(
'set',
dataset,
datakey,
update,
whenAddedToQueue
);
};
/**
* ### SyncIt.remove()
*
* This will add a Pathitem to the Queue that represents the removal of data stored at a Dataset/Datakey.
*
* #### Parameters
*
* * **@param {Dataset} `dataset`**
* * **@param {Datakey} `datakey`**
* * **@param {Function} `whenAddedToQueue`** Fired after the *Queue* has been updated. See [SyncIt._addToQueue()](#syncit._addtoqueue--)** for documentation.
*/
SyncIt.prototype.remove = function(dataset, datakey, whenAddedToQueue) {
return this._addToQueue(
'remove',
dataset,
datakey,
{},
whenAddedToQueue
);
};
/**
* ### SyncIt.update()
*
* This can update one or more parts of the the data at a single *dataset* / *datakey* using something similar to the MongoDB update syntax.
*
* #### Example
*
* ```
* syncIt.update(
* 'user',
* 'jack',
* {'$set': {'eyes.color': 'blue'}},
* function(err, dataset, datakey, queueitem) {
* // The data now includes { eyes: { color: "blue" } } but the rest of
* // the data has been preserved
* }
* );
* ```
*
* #### Parameters
*
* * **@param {Dataset} `dataset`**
* * **@param {Datakey} `datakey`**
* * **@param {Update} `update`**
* * **@param {Function} `whenAddedToQueue`** Fired after the *Queue* has been updated. See [SyncIt._addToQueue()](#syncit._addtoqueue--) for documentation.
*/
SyncIt.prototype.update = function(dataset, datakey, update, whenAddedToQueue) {
return this._addToQueue(
'update',
dataset,
datakey,
update,
whenAddedToQueue
);
};
/**
* ### SyncIt.feed()
*
* This function is for feeding in external Queueitem from the *Respository*.
*
* #### Parameters
*
* * **@param {Array} `feedQueueitems`** These are the items which are being fed from the *Server*.
* * **@param {Function} `resolutionFunction`** Called when conflict occurs, Signature: `function(dataset, datakey, storerecord, serverQueueitems, localPathitems, resolved)`.
* * **@param {Array} `resolutionFunction.dataset`** The *Dataset* of the conflict.
* * **@param {Array} `resolutionFunction.datakey`** The *Datakey* of the conflict.
* * **@param {Array} `resolutionFunction.storerecord`** What is in the local *Store* for that *Dataset* / *Datakey*.
* * **@param {Array} `resolutionFunction.localPathitems`** The *Pathitem* that has been added using functions such as [SyncIt.set()](#syncit.set--) but is now conflicting with the data from the *Server*.
* * **@param {Array} `resolutionFunction.serverQueueitems`** The extra *Queueitem* that are on the Server.
* * **@param {Function} `resolutionFunction.resolved`** This should be called from inside resolutionFunction and will add *Pathitem* after the *Server* supplied *Queueitem*. Signature: `function(resolved, mergedLocalsToDoAfterwards)`
* * **@param {Boolean} `resolutionFunction.resolved.resolved`** use false to halt the feeding, true otherwise
* * **@param {Array} `resolutionFunction.resolved.mergedLocalsToDoAfterwards`** These will be added to the *Queue* __after__ (currently) all serverPathitems have been advanced to the *Store*.
* * **@param {Function} `feedDone`** Callback for when done. Signature: `function(err, fedItemsFailed)`;
* * **@param {Errorcode} `feedDone.err`** See SyncIt_Constant.Error.
* * **@param {Array} `feedDone.fedItemsFailed`** Array of items fed from the *Server* which could not be processed.
*/
SyncIt.prototype.feed = function(feedQueueitems, resolutionFunction, feedDone) {
// Make a shallow copy of feedQueueitems so when we `shift()` we are not
// fiddling with users data.
var feedQueue = (function(items) {
var r = [];
for (var i = 0, l = items.length; i < l; i++) {
r.push(_shallowCopyKeys(
items[i],
['s','k','u','t','m','o','b'],
false
));
}
return r;
})(feedQueueitems);
// If a conflict occurs we want to feed the resolutionFunction only items from
// the same dataset / datakey that are based on a version higher than the one
// currently in the Pathroot.
var prepareServerQueueItemForResolutionFunction = function(storerecord, feedQueue) {
var r = [],
i = 0,
l = 0,
firstQueueitem = feedQueue[0];
var filterFunc = function(elem) {
if (storerecord === null) {
return true;
}
return (elem.b >= storerecord.v);
};
for (i=0, l=feedQueue.length; i<l; i++) {
if (
(feedQueue[i].s != firstQueueitem.s) ||
(feedQueue[i].k != firstQueueitem.k)
) {
return _filter(r,filterFunc);
}
r.push(feedQueue[i]);
}
return _filter(r,filterFunc);
};
// Simple helper function that will unlock SyncIt and call the feedDone callback.
var unlockAndError = function(err) {
this._unlockFor(LOCKING.FEEDING);
return feedDone(
err,
feedQueue
);
}.bind(this);
// This function is called from resolutionFunction.resolved and will check
// that resolutionFunction actually did resolve the conflict, if it did it
// will add any resolving PathItem to the "c" (conflict) branch so they can
// be applied later and remove all local Pathitem from the "a" branch as
// they conflict with the items sent from the server.
var perhapsResolved = function(storerecord,fedForSameDatasetAndDatakey,resolved,mergePathitem,next) {
var sanatizeMergeItem = function(queueitem) {
var r = {},
copyKeys = ['o','u','t'],
disallowedKeys = ['i','j','q','r','v'],
i;
for (i=0; i<disallowedKeys.length; i++) {
if (queueitem.hasOwnProperty(disallowedKeys[i])) {
throw new Error("Merge queue cannot include any " + disallowedKeys.join(', '));
}
}
for (i=0; i<copyKeys.length; i++) {
if (queueitem.hasOwnProperty(copyKeys[i])) {
r[copyKeys[i]] = queueitem[copyKeys[i]];
}
}
if (queueitem.hasOwnProperty('s') && (queueitem.s != storerecord.s)) {
throw new Error("Merge queue cannot use different dataset to stored record");
}
if (queueitem.hasOwnProperty('k') && (queueitem.k != storerecord.k)) {
throw new Error("Merge queue cannot use different datakey to stored record");
}
return r;
};
if (!resolved) {
unlockAndError(ERROR.NOT_RESOLVED);
}
if (!mergePathitem.length) {
return this._ps.removePathitemFromPath(feedQueue[0].s,feedQueue[0].k,'a',this._autoClean,next);
}
// TODO: Sanitize the merge queue
this._ps.pushPathitemsToNewPath(
feedQueue[0].s,
feedQueue[0].k,
'c',
_map(mergePathitem,sanatizeMergeItem),
function(err) {
if (err) {
return unlockAndError(err,feedQueue);
}
var info = {cv: storerecord.v + fedForSameDatasetAndDatakey.length};
this._ps.setInfo(feedQueue[0].s,feedQueue[0].k,info,function(err) {
if (err !== ERROR.OK) {
unlockAndError(err);
}
return this._ps.removePathitemFromPath(
feedQueue[0].s,
feedQueue[0].k,
'a',
this._autoClean,
next
);
}.bind(this));
}.bind(this)
);
}.bind(this);
// One by one, process the Pathitem which have been fed.
var feedOne = function() {
if (feedQueue.length === 0) {
this._unlockFor(LOCKING.FEEDING);
return feedDone(ERROR.OK,[]);
}
var storerecord = this._getEmptyStorerecord();
var otherpaths = [];
var queue = [];
var info = {};
// Read the path, collecting information.
this._ps.followPath(
feedQueue[0].s,
feedQueue[0].k,
'a',
function(key,item,inWhere) {
if (inWhere == FOLLOW_INFORMATION_TYPE.INFO) {
info = item;
return;
}
if (inWhere == FOLLOW_INFORMATION_TYPE.ROOTITEM) {
storerecord = item;
return;
}
if (inWhere == FOLLOW_INFORMATION_TYPE.OTHER_PATHS) {
otherpaths = item;
}
if (inWhere == FOLLOW_INFORMATION_TYPE.PATHITEM) {
queue.push(this._addObviousInforation(
feedQueue[0].s,
feedQueue[0].k,
key.replace(/.*\./,''),
item
));
}
}.bind(this),
function(err) {
if ((err !== ERROR.NO_DATA_FOUND) && (err !== ERROR.OK)) {
unlockAndError(err);
}
// It might be that we are trying to feed data which is
// based on an old storerecord, if we are just skip over it
if (feedQueue[0].b < storerecord.v) {
feedQueue.shift();
return feedOne();
}
// If we have items in our local queue with a basedonversion which is
// lower than what we are being fed, it is likely that we have unadvanced
// items which we have already uploaded.
if (queue.length && (queue[0].b < feedQueue[0].b)) {
return unlockAndError(
Error.BASED_ON_IN_QUEUE_LESS_THAN_BASED_IN_BEING_FED,
feedQueue
);
}
// Continue with the Feed passing the information found from
// this._ps.followPath();Continue with the Feed.
return feedOneWorker(storerecord,queue,info,otherpaths);
}.bind(this)
);
}.bind(this);
// All data is now collected about what is in the Path so we can go ahead and
// take appropriate action...
var feedOneWorker = function(storerecord,queue,info) {
// If there is items in the Path then feed them into resolutionFunction
if (queue.length) {
var fedForSameDatasetAndDatakey = prepareServerQueueItemForResolutionFunction(
storerecord,
feedQueue
);
return resolutionFunction.call(
this,
feedQueue[0].s,
feedQueue[0].k,
(function() {
if (storerecord.v === 0) {
return null;
}
storerecord.s = feedQueue[0].s;
storerecord.k = feedQueue[0].k;
if (!storerecord.hasOwnProperty('m')) {
storerecord.m = this.getModifier();
}
return storerecord;
}.bind(this)()),
queue,
fedForSameDatasetAndDatakey,
function(resolved,mergePathitem) {
perhapsResolved(storerecord,fedForSameDatasetAndDatakey,resolved,mergePathitem,feedOne);
}
);
}
// Just make sure that the Pathitem is the correct version to apply.
if (storerecord.v != feedQueue[0].b) {
if ((storerecord.v === 0) && (feedQueue[0].o == 'remove')) {
// Requests to remove data which is not there... this is probably seeing
// a refeed of the delete of queue data which __was__ there, but has been
// deleted. In this situation just continue.
feedQueue.shift();
return feedOne();
}
return unlockAndError(
ERROR.FEED_VERSION_ERROR,
feedQueue
);
}
// This will basically apply the first Pathitem to the existing
// Pathroot, giving the new Pathroot
var newRoot = _shallowCopyKeys(
updateResult(
storerecord,
feedQueue[0],
this._cloneObj
),
['i','v','m','t','r']
);
var joinCPathToA = function(dataset,datakey,baseV,next) {
return this._ps.changePath(dataset,datakey,'c','a',this._autoClean,function(err) {
if (err) {
return unlockAndError(err,feedQueue);
}
var conflictPathWatcher = this._getPathWatcher();
var conflictPathWatch = conflictPathWatcher.getWatcher();
return this._ps.followPath(
dataset,
datakey,
'a',
function(storagekey,item,itemtype) {
conflictPathWatch(storagekey,item,itemtype);
if (itemtype !== FOLLOW_INFORMATION_TYPE.PATHITEM) {
return ERROR.OK;
}
var storerecord = this._makeFullReaditem(conflictPathWatcher.getReaditem());
var queueitem = this._addObviousInforation(
dataset,
datakey,
storagekey.replace(/.*\./,''),
item,
{ b: baseV++ }
);
this._emit(
'conflict_resolution_items_added_to_queue',
dataset,
datakey,
queueitem,
storerecord
);
this._emit('data_change', storerecord, queueitem);
return ERROR.OK;
}.bind(this),
function(err) {
if (err) {
return unlockAndError(err,feedQueue);
}
next();
}
);
}.bind(this));
}.bind(this);
// Set the new Pathroot, once this is done, emit the fact an item has
// been fed and check that if we should be applying the conflict path
// (c), if we should, do so. Once all this is done, go back and call
// `feedOne()` to go and get the next item.
this._ps.setPathroot(
feedQueue[0].s,
feedQueue[0].k,
'a',
newRoot,
function(err) {
if (err !== ERROR.OK) {
return unlockAndError(err);
}
var dataset = feedQueue[0].s;
var datakey = feedQueue[0].k;
var storerecord = this._addObviousInforation(
feedQueue[0].s,
feedQueue[0].k,
null,
newRoot
);
this._emit(
'fed',
dataset,
datakey,
feedQueue[0],
storerecord
);
this._emit('data_change', storerecord, feedQueue[0]);
feedQueue.shift();
if (newRoot.v == info.cv) {
return joinCPathToA(dataset,datakey,newRoot.v,feedOne);
}
feedOne();
}.bind(this)
);
}.bind(this);
var i=0,
l=0,
queueitemValidationError = 0;
// If locked, just exit.
if (this.isLocked()) {
return feedDone(
SyncIt_Constant.Error.UNABLE_TO_PROCESS_BECAUSE_LOCKED,
feedQueueitems
);
}
// Perform basic validation.
for (i=0, l=feedQueueitems.length;i<l;i++) {
queueitemValidationError = this._basicValidationForQueueitem(feedQueueitems[i]);
if (queueitemValidationError != SyncIt_Constant.Error.OK) {
return feedDone(
queueitemValidationError,
feedQueueitems
);
}
}
// Then lock
this._lockFor(LOCKING.FEEDING);
// Process the first item.
feedOne();
};
SyncIt.prototype._basicValidationForQueueitem = function(queueitem,skips) {
var k,
requiredFields = {
s: SyncIt_Constant.Error.INVALID_DATASET,
k: SyncIt_Constant.Error.INVALID_DATAKEY,
o: SyncIt_Constant.Error.INVALID_OPERATION
};
for (k in requiredFields) {
if (
requiredFields.hasOwnProperty(k) &&
!queueitem.hasOwnProperty(k)
) {
return requiredFields[k];
}
}
if (queueitem.s.match(SyncIt_Constant.Validation.DATASET_REGEXP) === null) {
return SyncIt_Constant.Error.INVALID_DATASET;
}
if (queueitem.k.match(SyncIt_Constant.Validation.DATAKEY_REGEXP) === null) {
return SyncIt_Constant.Error.INVALID_DATAKEY;
}
if (queueitem.o.match(SyncIt_Constant.Validation.OPERATION_REGEXP) === null) {
return SyncIt_Constant.Error.INVALID_OPERATION;
}
if ( (skips !== undefined) && (skips.indexOf('m') != -1) ) {
return SyncIt_Constant.Error.OK;
}
if (queueitem.m.match(SyncIt_Constant.Validation.MODIFIER_REGEXP) === null) {
return SyncIt_Constant.Error.INVALID_MODIFIER;
}
return SyncIt_Constant.Error.OK;
};
/**
* **SyncIt._makeFullReaditem()**
*
* Adds information which is not necessary to store in the Pathitems itself.
*/
SyncIt.prototype._makeFullReaditem = function(dataFromStore) {
var k = '';
var defaults = {
m: this.getModifier(),
r: false
};
for (k in defaults) {
if (!dataFromStore.hasOwnProperty(k)) {
dataFromStore[k] = defaults[k];
}
}
return dataFromStore;
};
/**
* **SyncIt._addToQueue()**
*
* Adds a Pathitem to the Queue.
*
* **Parameters**
*
* * **@param {Operation} `operation`**
* * **@param {Dataset} `dataset`**
* * **@param {Datakey} `datakey`**
* * **@param {Update} `update`**
* * **@param {Modifier} `modifier`**
* * **@param {Basedonversion} `basedonversion`**
* * **@param {Function} `whenAddedToQueue`** Callback for when adding is complete. Signature: `function(errorCode, dataset, datakey, queueitem)`
* * **@param {Errorcode} `whenAddedToQueue.errorCode`** See SyncIt_Constant.Error.
* * **@param {Dataset} `whenAddedToQueue.dataset`** The Dataset of the Pathitem.
* * **@param {Datakey} `whenAddedToQueue.datakey`** The Datakey of the Pathitem.
* * **@param {Pathitem} `whenAddedToQueue.queueitem`** The Pathitem that has just been added.
* * **@param {Readrecord} `whenAddedToQueue.readrecord`** The now logical value of the data.
*/
SyncIt.prototype._addToQueue = function(operation, dataset, datakey, update, whenAddedToQueue) {
// If locked, exit.
if (this.isLocked()) {
whenAddedToQueue(ERROR.UNABLE_TO_PROCESS_BECAUSE_LOCKED);
return false;
}
var queueitem = {
o: operation,
u: update
};
var pathWatcher = this._getPathWatcher();
this._lockFor(LOCKING.ADDING_TO_QUEUE);
this._ps.push(
dataset,
datakey,
'a',
queueitem,
true,
pathWatcher.getWatcher(),
function(err,ref) {
this._unlockFor(LOCKING.ADDING_TO_QUEUE);
if (err !== ERROR.OK) {
return whenAddedToQueue(err);
}
var pathWatchedItem = pathWatcher.getReaditem(),
storerecord = this._makeFullReaditem(pathWatchedItem),
emitQueueitem = this._addObviousInforation(
dataset,
datakey,
ref,
queueitem,
pathWatchedItem
);
this._emit(
'added_to_queue',
dataset,
datakey,
emitQueueitem,
storerecord
);
this._emit('data_change', storerecord, emitQueueitem);
whenAddedToQueue(
err,
dataset,
datakey,
queueitem,
storerecord
);
}.bind(this)
);
};
/**
* **SyncIt._getEmptyStorerecord()**
*
* When advancing a *Pathitem* either because SyncIt is moving data from the *Queue* to the *Store* or just because it is processing a `[SyncIt.get()](#syncit.get--) it is possible that no data already exists at the *Store* for that *Dataset* / *Datakey*. It's handy to use this function to get something that looks like stored data to limit code complexity.
*/
SyncIt.prototype._getEmptyStorerecord = function() {
return {
i:{},
v:0,
j:{},
p:[],
q:[],
r:false,
t:(new Date()).getTime()
};
};
/**
* ### SyncIt.advance()
*
* Applies the very first *Pathitem* in the *Queue* onto the data already in the *Store* for that *Dataset* / *Datakey*.
*
* #### Parameters
*
* * **@param {Function} `done`** Callback when the operation is complete (or not). Signature: `function(errorCode, queueitem, storerecord)`
* * **@param {ErrorCode} `done.errorCode`** See SyncIt_Constant.Error.
* * **@param {Dataset} `done.dataset`**
* * **@param {Datakey} `done.datakey`**
* * **@param {Pathitem} `done.queueitem`** The *Pathitem* that was just advanced
* * **@param {Storerecord} `done.storerecord`** The new *Storerecord*
*/
SyncIt.prototype.advance = function(done) {
if (this.isLocked()) {
done(ERROR.UNABLE_TO_PROCESS_BECAUSE_LOCKED);
return false;
}
this._lockFor(LOCKING.ADVANCING);
var addedPathkey = '';
this._ps.findFirstDatasetDatakey(null,'a',function(err,dataset,datakey) {
if (err !== ERROR.OK) {
this._unlockFor(LOCKING.ADVANCING);
return done(err);
}
var newRoot = {};
this._ps.advance(
dataset,
datakey,
this._autoClean,
function(pathRoot,key,item,newRootCb) {
if (pathRoot === null) {
pathRoot = this._getEmptyStorerecord();
}
addedPathkey = key;
// Filter to just keys r, v, i and possibly t anything else can
// be recreated.
newRoot = updateResult(pathRoot,item,this._cloneObj);
newRoot = _shallowCopyKeys(
this._addObviousInforation(dataset,datakey,addedPathkey,newRoot),
['i','t','v','r'],
false
);
newRootCb(newRoot);
}.bind(this),
function(err,item) {
this._unlockFor(LOCKING.ADVANCING);
if (err !== ERROR.OK) {
return done(err,dataset,datakey);
}
this._emit(
'advanced',
dataset,
datakey,
this._addObviousInforation(dataset,datakey,addedPathkey,item,newRoot),
this._addObviousInforation(dataset,datakey,addedPathkey,newRoot)
);
return done(
err,
dataset,
datakey,
this._addObviousInforation(dataset,datakey,addedPathkey,item,newRoot),
this._addObviousInforation(dataset,datakey,addedPathkey,newRoot)
);
}.bind(this)
);
}.bind(this));
};
/**
* **SyncIt._addObviousInforation**
*