-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync9.js
1368 lines (1229 loc) · 45.4 KB
/
sync9.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
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
// Greg: This version was adapted from sync9_041.html
module.exports = {create: sync9_create,
add_version: sync9_add_version,
read: sync9_read_version,
get_ancestors: sync9_get_ancestors,
extract_versions: sync9_extract_versions,
prune: sync9_prune,
prune2: sync9_prune2,
trav_space_dag: sync9_trav_space_dag,
get_space_dag: sync9_space_dag_get,
sink
}
function sink() {
var s9 = sync9_create()
// The `version` object
function version(ids) {
// console.log('version: creating for', ids)
if (typeof ids === 'string') {
var id = ids
ids = [id]
} else {
if (Object.keys(ids).length === 0)
return null
var id = Object.keys(ids).sort().join('-')
}
return {
id: id,
parents: () => Object.keys(s9.T[id]).map(version),
parent: () => {
var parents = {}
for (var k in ids) for (var j in s9.T[k])
parents[j] = true
return version(parents)
},
toString: () => id
}
}
var now = () => version(s9.leaves)
// The `sink` object
return {
s9:s9,
set: (patch, version, parents) => {
version = version || sync9_guid()
parents = parents || s9.leaves
if (!Array.isArray(patch))
patch = [patch]
return sync9_add_version(s9, version, parents, patch)
},
get: (version) => sync9_read_version(s9, version),
time: s9.T,
current_version: now,
now: now
}
}
function sync9_create_peer(p_funcs) {
var self = {}
self.s9 = sync9_create()
// work here : creating single string within json
if (true) {
sync9_add_version(self.s9, 'v1', {root: true}, [' = ""'])
sync9_prune(self.s9, (a, b) => true, (a, b) => true)
delete self.s9.T.v1
self.s9.leaves = {root: true}
}
self.id = sync9_guid()
self.peers = {}
self.fissures = {}
self.conn_leaves = {}
self.ack_leaves = {}
self.valid_leaves = {}
self.seen_local = {}
self.valid_local = {}
self.joiners = {}
self.connect = (sndr, alpha) => {
if (alpha) {
self.peers[sndr] = {conn_id: sync9_guid()}
p_funcs.get(sndr, self.id, self.peers[sndr].conn_id)
} else {
self.peers[sndr] = {}
}
}
self.disconnect = sndr => {
if (!self.peers[sndr]) return
if (self.peers[sndr].b) {
var versions = {}
var ack_versions = sync9_get_ancestors(self.s9, self.ack_leaves)
Object.keys(self.s9.T).forEach(v => {
if (!ack_versions[v] || self.ack_leaves[v])
versions[v] = true
})
var fissure_parents = {}
Object.keys(self.fissures).forEach(x => {
fissure_parents[x] = true
})
self.fissure(sndr, {
a: self.id,
b: self.peers[sndr].b,
conn_id: self.peers[sndr].conn_id,
versions,
parents: fissure_parents
})
}
delete self.peers[sndr]
}
// "True peers" are the ones where we have a complete connection going.
// Because it's possible that we could have created half a connection and
// sent some stuff over the network before it was actually there.
//
// So they are "true" because this is what would actually exist in a real
// network, vs. the simulation. In this simulation, there is a
// "connection" that might not really exist.
function get_true_peers() {
return Object.entries(self.peers).filter(x => x[1].b).map(x => x[0])
}
self.fissure = (sndr, fissure) => {
var key = fissure.a + ':' + fissure.b + ':' + fissure.conn_id
if (!self.fissures[key]) {
self.fissures[key] = fissure
self.seen_local = {}
get_true_peers().forEach(p => {
if (p != sndr) p_funcs.fissure(p, fissure)
})
if (fissure.b == self.id) {
self.fissure(null, {
a: self.id,
b: fissure.a,
conn_id: fissure.conn_id,
versions: fissure.versions,
parents: {}
})
}
}
}
// Right now, the get sends the sender_id. But perhaps this should happen
// when creating the connection.
self.get = (sndr, sender_id, conn_id) => {
// sender_id happens to == sndr
self.peers[sndr].b = sender_id
if (conn_id) {
self.peers[sndr].conn_id = conn_id
p_funcs.get(sndr, self.id)
}
var versions = sync9_extract_versions(self.s9, x => x == 'root')
var fissures = Object.values(self.fissures)
p_funcs.set_multi(sndr, versions, fissures)
}
self.set_multi = (sndr, versions, fissures, conn_leaves, min_leaves) => {
var new_versions = [] // Versions I haven't seen yet, so we'll
// forward to other peers
var versions_T = {} // This helps deal with receiving versions
// where some are squashed
// Filter out the versions we will actually add to ourself.
// Versions_T will be the set of versions that we want to add.
versions.forEach(v => versions_T[v.vid] = v.parents)
versions.forEach(v => {
if (self.s9.T[v.vid]) {
function f(v) {
if (versions_T[v]) {
Object.keys(versions_T[v]).forEach(f)
delete versions_T[v]
}
}
f(v.vid)
}
})
// Now add to ourself all versions that are in versions_T
versions.forEach(v => {
if (versions_T[v.vid]) {
new_versions.push(v)
sync9_add_version(self.s9, v.vid, v.parents, v.patches)
}
})
var new_fissures = [] // The fissures we have not seen yet
var gen_fissures = [] // Generated fissures. For when we are the
// other side of a fissure we're receiving
// Add the new fissures to ourself, and generated any missing ones
fissures.forEach(f => {
var key = f.a + ':' + f.b + ':' + f.conn_id
if (!self.fissures[key]) {
new_fissures.push(f)
self.fissures[key] = f
// Generate a fissure if we are the other side of one
if (f.b == self.id) gen_fissures.push({
a: self.id,
b: f.a,
conn_id: f.conn_id,
versions: f.versions,
parents: {}
})
}
})
// There might be some global acknowledgements that were found in our
// sub-network that are no longer valid now that we're reconnecting
// with someone ... who has not yet acknowledged those versions. So
// we mark a boundary of time -- the "connection boundary" or
// conn_leaves -- before which any acknowledgements are not actually
// valid anymore. This is how we un-acknowledge stuff, now that
// there's a new participant who needs to be acknowledging everything.
if (!conn_leaves)
conn_leaves = Object.assign({}, self.s9.leaves)
var our_conn_versions = sync9_get_ancestors(self.s9, self.conn_leaves)
var new_conn_versions = sync9_get_ancestors(self.s9, conn_leaves)
Object.keys(self.conn_leaves).forEach(x => {
if (new_conn_versions[x] && !conn_leaves[x])
delete self.conn_leaves[x]
})
Object.keys(conn_leaves).forEach(x => {
if (!our_conn_versions[x]) self.conn_leaves[x] = true
})
if (!min_leaves) {
min_leaves = {}
var min = versions.filter(v => !versions_T[v.vid])
min.forEach(v => min_leaves[v.vid] = true)
min.forEach(v => {
Object.keys(v.parents).forEach(p => {
delete min_leaves[p]
})
})
}
// The min_versions are the top of time in the "unacknowledge" region.
// This becomes the new ack boundary. It is calculated as the
// intersection of this reconnecting peer's set of versions and our
// versions.
var min_versions = sync9_get_ancestors(self.s9, min_leaves)
var ack_versions = sync9_get_ancestors(self.s9, self.ack_leaves)
Object.keys(self.ack_leaves).forEach(x => {
if (!min_versions[x]) delete self.ack_leaves[x]
})
Object.keys(min_leaves).forEach(x => {
if (ack_versions[x]) self.ack_leaves[x] = true // Set the new ack boundary
})
// Now let's wipe out the current record of local acks! Because any
// global ack will have to include this new peer.
self.seen_local = {}
// Now broadcast the set_multi to all of our peers
if (new_versions.length > 0 || new_fissures.length > 0) {
get_true_peers().forEach(p => {
if (p != sndr) p_funcs.set_multi(p, new_versions, new_fissures, conn_leaves, min_leaves)
})
}
// We also have to tell everyone about the fissures we generated
gen_fissures.forEach(f => self.fissure(null, f))
}
function add_full_ack_leaf(vid) {
var marks = {}
function f(v) {
if (!marks[v]) {
marks[v] = true
delete self.conn_leaves[v]
delete self.ack_leaves[v]
delete self.seen_local[v]
delete self.joiners[v]
Object.keys(self.s9.T[v]).forEach(f)
}
}
f(vid)
self.ack_leaves[vid] = true
self.prune()
}
// Sets the value internally, and sends a message to all peers
self.local_set = (vid, parents, patches, joiner_num) => {
sync9_add_version(self.s9, vid, parents, patches)
var ps = get_true_peers()
self.seen_local[vid] = {origin: null, count: ps.length}
self.valid_local[vid] = {origin: null, count: 0}
// Now call any handlers, which add to valid.count
// seen_local -> waiting_to_see
// valid_local -> waiting_to_validate
if (joiner_num) self.joiners[vid] = joiner_num
ps.forEach(p => {
p_funcs.set(p, vid, parents, patches, joiner_num)
})
check_ack_count(vid)
}
// This is like local_set, but does not send the patch back to the
// originating peer `sndr`.
// This also counts the set as an ack from that peer. (The "seen_local" count.)
self.set = (sndr, vid, parents, patches, joiner_num) => {
if (!self.s9.T[vid] || (joiner_num > self.joiners[vid])) {
sync9_add_version(self.s9, vid, parents, patches)
var ps = get_true_peers()
self.seen_local[vid] = {origin: sndr, count: ps.length - 1}
self.valid_local[vid] = {origin: null, count: 0}
// Now call any handlers, which add to valid.count
// seen_local -> waiting_to_see
// valid_local -> waiting_to_validate
if (joiner_num) self.joiners[vid] = joiner_num
ps.forEach(p => {
if (p != sndr)
p_funcs.set(p, vid, parents, patches, joiner_num)
})
} else if (self.seen_local[vid] && (joiner_num == self.joiners[vid]))
self.seen_local[vid].count--
check_ack_count(vid)
}
self.ack = (sndr, vid, joiner_num) => {
if (self.seen_local[vid] && (joiner_num == self.joiners[vid])) {
self.seen_local[vid].count--
check_ack_count(vid)
}
}
function check_ack_count(vid) {
if (self.seen_local[vid] && self.seen_local[vid].count == 0) {
if (self.seen_local[vid].origin)
p_funcs.ack(self.seen_local[vid].origin, vid, self.joiners[vid])
else {
add_full_ack_leaf(vid)
get_true_peers().forEach(p => {
p_funcs.full_ack(p, vid)
})
}
}
}
self.full_ack = (sndr, vid) => {
if (!self.s9.T[vid]) return
var ancs = sync9_get_ancestors(self.s9, self.conn_leaves)
if (ancs[vid]) return
var ancs = sync9_get_ancestors(self.s9, self.ack_leaves)
if (ancs[vid]) return
add_full_ack_leaf(vid)
get_true_peers().forEach(p => {
if (p != sndr) p_funcs.full_ack(p, vid)
})
}
self.local_valid_ack = (sndr, vid) => {
if (self.valid_local[vid]) {
self.valid_local[vid].count--
check_valid_count(vid)
}
}
function check_valid_count (vid) {
if (self.valid_local[vid] && self.valid_local[vid].count == 0) {
if (self.valid_local[vid].origin)
p_funcs.local_ack_valid(self.valid_local[vid].origin, vid)
else {
console.log('Validate!!!! (fill this in)')
console.log('Does validating a leaf make all ancestors valid?')
get_true_peers().forEach(p => {
p_funcs.global_valid_ack(p, vid)
})
}
}
}
self.global_valid_ack = (sndr, vid) => {
add_global_valid_ack_leaf(vid)
get_true_peers().forEach(p => {
if (p != sndr) p_funcs.global_valid_ack(p, vid)
})
}
function add_global_valid_ack_leaf(vid) {
var marks = {}
function f(v) {
if (!marks[v]) {
marks[v] = true
delete self.valid_leaves[v]
delete self.valid_local[v]
Object.keys(self.s9.T[v]).forEach(f)
}
}
f(vid)
self.valid_leaves[vid] = true
}
self.prune = () => {
var tags = {}
var frozen = {root: true}
Object.keys(self.s9.T).forEach(vid => {
tags[vid] = {tags: {}}
})
function tag(vid, t) {
if (!tags[vid].tags[t]) {
tags[vid].tags[t] = true
Object.keys(self.s9.T[vid]).forEach(vid => tag(vid, t))
}
}
Object.entries(self.fissures).forEach(x => {
Object.keys(x[1].versions).forEach(v => {
if (!self.s9.T[v]) return
tag(v, v)
frozen[v] = true
Object.keys(self.s9.T[v]).forEach(v => {
tag(v, v)
frozen[v] = true
})
})
})
var acked = sync9_get_ancestors(self.s9, self.ack_leaves)
Object.keys(self.s9.T).forEach(x => {
if (!acked[x] || self.ack_leaves[x]) {
tag(x, x)
frozen[x] = true
Object.keys(self.s9.T[x]).forEach(v => {
tag(v, v)
frozen[v] = true
})
}
})
Object.entries(tags).forEach(x => {
var keys = Object.keys(x[1].tags)
if (keys.length == 0) {
frozen[x[0]] = true
} else if (!frozen[x[0]]) {
x[1].tag = keys.sort().join(',')
}
})
var q = (a, b) => !frozen[a] && !frozen[b] && (tags[a].tag == tags[b].tag)
sync9_prune2(self.s9, q)
var unremovable = {}
Object.entries(self.fissures).forEach(x => {
if (!self.fissures[x[1].b + ':' + x[1].a + ':' + x[1].conn_id]) {
function f(y) {
if (!unremovable[y.a + ':' + y.b + ':' + y.conn_id]) {
unremovable[y.a + ':' + y.b + ':' + y.conn_id] = true
unremovable[y.b + ':' + y.a + ':' + y.conn_id] = true
Object.keys(y.parents).forEach(p => {
if (self.fissures[p]) f(self.fissures[p])
})
}
}
f(x[1])
}
})
var acked = sync9_get_ancestors(self.s9, self.ack_leaves)
var done = {}
Object.entries(self.fissures).forEach(x => {
var other_key = x[1].b + ':' + x[1].a + ':' + x[1].conn_id
var other = self.fissures[other_key]
if (other && !done[x[0]] && !unremovable[x[0]]) {
done[x[0]] = true
done[other_key] = true
if (Object.keys(x[1].versions).every(x => acked[x] || !self.s9.T[x])) {
delete self.fissures[x[0]]
delete self.fissures[other_key]
}
}
})
var ls = Object.keys(self.s9.leaves)
var as = Object.keys(self.ack_leaves)
if (ls.length == 1 &&
as.length == 1 &&
ls[0] == as[0] &&
Object.keys(self.fissures).length == 0 &&
Object.keys(self.s9.T).length > 1) {
var new_s9 = sync9_create()
// work here : creating single string within json
if (true) {
sync9_add_version(new_s9, 'v1', {root: true}, [' = ""'])
sync9_prune(new_s9, (a, b) => true, (a, b) => true)
delete new_s9.T.v1
new_s9.leaves = {root: true}
}
sync9_add_version(new_s9, ls[0], {root: true}, ['[0:0] = ' + JSON.stringify(sync9_read(self.s9))])
self.s9.T = new_s9.T
self.s9.leaves = new_s9.leaves
self.s9.val = new_s9.val
}
}
self.create_joiner = () => {
var vid = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(Object.keys(self.s9.leaves).sort().join(':')))
var joiner_num = Math.random()
self.local_set(vid, Object.assign({}, self.s9.leaves), [], joiner_num)
}
return self
}
function sync9_extract_versions(x, is_anc, is_new_anc) {
var versions = sync9_space_dag_extract_versions(sync9_space_dag_get(x.val.S, 0).S, x, is_anc, is_new_anc)
versions.forEach(x => {
x.patches = x.splices.map(x => {
return `[${x[0]}:${x[0] + x[1]}] = ${JSON.stringify(x[2])}`
})
delete x.splices
})
return versions
}
function sync9_space_dag_extract_versions(S, s9, is_anc) {
return Object.keys(s9.T).filter(x => !is_anc(x)).map(vid => {
var v = {
vid,
parents : Object.assign({}, s9.T[vid]),
splices : []
}
function add_result(offset, del, ins) {
if (v.splices.length > 0) {
var prev = v.splices[v.splices.length - 1]
if (prev[0] + prev[1] == offset) {
prev[1] += del
prev[2] = prev[2].concat(ins)
return
}
}
v.splices.push([offset, del, ins])
}
var ancs = sync9_get_ancestors(s9, {[vid]: true})
delete ancs[vid]
var offset = 0
function helper(node, vid) {
if (vid == v.vid) {
add_result(offset, 0, node.elems.slice(0))
} else if (node.deleted_by[v.vid] && node.elems.length > 0) {
add_result(offset, node.elems.length, node.elems.slice(0, 0))
}
if (ancs[vid] && !Object.keys(node.deleted_by).some(x => ancs[x])) {
offset += node.elems.length
}
for (var next of node.nexts)
helper(next, next.vid)
if (node.next) helper(node.next, vid)
}
helper(S, S.vid)
return v
})
}
function sync9_prune2(x, has_everyone_whos_seen_a_seen_b) {
var seen_versions = {}
var did_something = true
function rec(x) {
if (x && typeof(x) == 'object') {
if (!x.t && x.val) {
rec(x.val)
} else if (x.t == 'val') {
if (sync9_space_dag_prune2(x.S, has_everyone_whos_seen_a_seen_b, seen_versions)) did_something = true
sync9_trav_space_dag(x.S, () => true, node => {
node.elems.forEach(rec)
}, true)
} else if (x.t == 'obj') {
Object.values(x.S).forEach(v => rec(v))
} else if (x.t == 'arr') {
if (sync9_space_dag_prune2(x.S, has_everyone_whos_seen_a_seen_b, seen_versions)) did_something = true
sync9_trav_space_dag(x.S, () => true, node => {
node.elems.forEach(rec)
}, true)
} else if (x.t == 'str') {
if (sync9_space_dag_prune2(x.S, has_everyone_whos_seen_a_seen_b, seen_versions)) did_something = true
}
}
}
while (did_something) {
seen_versions = {}
did_something = false
rec(x)
}
var delete_us = {}
var children = {}
Object.keys(x.T).forEach(y => {
Object.keys(x.T[y]).forEach(z => {
if (!children[z]) children[z] = {}
children[z][y] = true
})
})
Object.keys(x.T).forEach(y => {
if (!seen_versions[y] && Object.keys(children[y] || {}).some(z => has_everyone_whos_seen_a_seen_b(y, z))) delete_us[y] = true
})
var visited = {}
var forwards = {}
function g(vid) {
if (visited[vid]) return
visited[vid] = true
if (delete_us[vid])
forwards[vid] = {}
Object.keys(x.T[vid]).forEach(pid => {
g(pid)
if (delete_us[vid]) {
if (delete_us[pid])
Object.assign(forwards[vid], forwards[pid])
else
forwards[vid][pid] = true
} else if (delete_us[pid]) {
delete x.T[vid][pid]
Object.assign(x.T[vid], forwards[pid])
}
})
}
Object.keys(x.leaves).forEach(g)
Object.keys(delete_us).forEach(vid => delete x.T[vid])
return delete_us
}
function sync9_space_dag_prune2(S, has_everyone_whos_seen_a_seen_b, seen_versions) {
function set_nnnext(node, next) {
while (node.next) node = node.next
node.next = next
}
function process_node(node, offset, vid, prev) {
var nexts = node.nexts
var next = node.next
var all_nexts_prunable = nexts.every(x => has_everyone_whos_seen_a_seen_b(vid, x.vid))
if (nexts.length > 0 && all_nexts_prunable) {
var first_prunable = 0
var gamma = next
if (first_prunable + 1 < nexts.length) {
gamma = sync9_create_space_dag_node(null, typeof(node.elems) == 'string' ? '' : [])
gamma.nexts = nexts.slice(first_prunable + 1)
gamma.next = next
}
if (first_prunable == 0) {
if (nexts[0].elems.length == 0 && !nexts[0].end_cap && nexts[0].nexts.length > 0) {
var beta = gamma
if (nexts[0].next) {
beta = nexts[0].next
set_nnnext(beta, gamma)
}
node.nexts = nexts[0].nexts
node.next = beta
} else {
delete node.end_cap
node.nexts = []
node.next = nexts[0]
node.next.vid = null
set_nnnext(node, gamma)
}
} else {
node.nexts = nexts.slice(0, first_prunable)
node.next = nexts[first_prunable]
node.next.vid = null
set_nnnext(node, gamma)
}
return true
}
if (Object.keys(node.deleted_by).some(k => has_everyone_whos_seen_a_seen_b(vid, k))) {
node.deleted_by = {}
node.elems = node.elems.slice(0, 0)
delete node.gash
return true
} else {
Object.assign(seen_versions, node.deleted_by)
}
if (next && !next.nexts[0] && (Object.keys(next.deleted_by).some(k => has_everyone_whos_seen_a_seen_b(vid, k)) || next.elems.length == 0)) {
node.next = next.next
return true
}
if (nexts.length == 0 && next &&
!(next.elems.length == 0 && !next.end_cap && next.nexts.length > 0) &&
Object.keys(node.deleted_by).every(x => next.deleted_by[x]) &&
Object.keys(next.deleted_by).every(x => node.deleted_by[x])) {
node.elems = node.elems.concat(next.elems)
node.end_cap = next.end_cap
node.nexts = next.nexts
node.next = next.next
return true
}
}
var did_something = false
sync9_trav_space_dag(S, () => true, (node, offset, has_nexts, prev, vid) => {
if (!prev) seen_versions[vid] = true
while (process_node(node, offset, vid, prev)) {
did_something = true
}
}, true)
return did_something
}
function sync9_create() {
return {
T: {root : {}},
leaves: {root: true},
val: sync9_create_val()
}
}
function sync9_add_version(x, vid, parents, patches, is_anc) {
if (x.T[vid]) return
x.T[vid] = Object.assign({}, parents)
Object.keys(parents).forEach(k => {
if (x.leaves[k]) delete x.leaves[k]
})
x.leaves[vid] = true
if (!is_anc) {
if (parents == x.leaves) {
is_anc = (_vid) => _vid != vid
} else {
var ancs = sync9_get_ancestors(x, parents)
is_anc = _vid => ancs[_vid]
}
}
patches.forEach(patch => {
var parse = sync9_parse_patch(patch)
var cur = x.val
parse.keys.forEach((key, i) => {
if (cur.t == 'val') cur = sync9_space_dag_get(cur.S, 0, is_anc)
if (!cur) throw 'bad'
if (typeof(key) == 'string' && cur.t == 'obj') {
if (!cur.S[key]) cur.S[key] = sync9_create_val()
cur = cur.S[key]
} else if (typeof(key) == 'number') {
if (i == parse.keys.length - 1) {
parse.range = [key, key + 1]
parse.val = [parse.val]
} else if (i < parse.keys.length - 1 && cur.t == 'arr') {
cur = sync9_space_dag_get(cur.S, key, is_anc)
} else throw 'bad'
} else {
throw 'bad'
}
})
if (!parse.range) {
if (cur.t != 'val') throw 'bad'
var len = sync9_space_dag_length(cur.S, is_anc)
sync9_space_dag_add_version(cur.S, vid, [[0, len, [sync9_wrap(parse.val, vid)]]], is_anc)
} else {
if (cur.t == 'val') cur = sync9_space_dag_get(cur.S, 0, is_anc)
if (parse.val instanceof Array && cur.t != 'arr') throw 'bad'
if (parse.val instanceof String && cur.t != 'str') throw 'bad'
if (parse.val instanceof Array) parse.val = parse.val.map(x => sync9_wrap(x, vid))
sync9_space_dag_add_version(cur.S, vid, [[parse.range[0], parse.range[1] - parse.range[0], parse.val]], is_anc)
}
})
}
function sync9_read(x, is_anc) {
if (!is_anc) is_anc = () => true
if (x && typeof(x) == 'object') {
if (!x.t && x.val) return sync9_read(x.val, is_anc)
if (x.t == 'val') return sync9_read(sync9_space_dag_get(x.S, 0, is_anc), is_anc)
if (x.t == 'obj') {
var o = {}
Object.entries(x.S).forEach(([k, v]) => {
o[k] = sync9_read(v, is_anc)
})
return o
}
if (x.t == 'arr') {
var a = []
sync9_trav_space_dag(x.S, is_anc, (node) => {
node.elems.forEach((e) => {
a.push(sync9_read(e, is_anc))
})
})
return a
}
if (x.t == 'str') {
var s = []
sync9_trav_space_dag(x.S, is_anc, (node) => {
s.push(node.elems)
})
return s.join('')
}
} return x
}
function sync9_read_version(s9, version) {
if (version)
var ancs = sync9_get_ancestors(s9, {[version]:true})
return sync9_read(s9, ancs && (x=>ancs[x]))
}
function sync9_wrap(x, vid) {
if (typeof(x) == 'number' || x == null || typeof(x) == 'boolean') {
return x
} else if (typeof(x) == 'string') {
var s = sync9_create_str()
sync9_space_dag_add_version(s.S, vid, [[0, 0, x]], _vid => _vid != vid)
return s
} else if (typeof(x) == 'object') {
if (x instanceof Array) {
var a = sync9_create_arr()
sync9_space_dag_add_version(a.S, vid, [[0, 0, x.map(x => sync9_wrap(x, vid))]], _vid => _vid != vid)
return a
} else {
var o = sync9_create_obj()
Object.entries(x).forEach(([k, v]) => {
var val = sync9_create_val()
sync9_space_dag_add_version(val.S, vid, [[0, 0, [sync9_wrap(v, vid)]]], _vid => _vid != vid)
o.S[k] = val
})
return o
}
} else throw 'bad'
}
function sync9_create_val() {
return {
t : 'val',
S : sync9_create_space_dag_node('root', [])
}
}
function sync9_create_obj() {
return {
t : 'obj',
S : {}
}
}
function sync9_create_arr() {
return {
t : 'arr',
S : sync9_create_space_dag_node('root', [])
}
}
function sync9_create_str() {
return {
t : 'str',
S : sync9_create_space_dag_node('root', '')
}
}
function sync9_create_space_dag_node(vid, elems, end_cap) {
return {
vid : vid,
elems : elems,
deleted_by : {},
end_cap : end_cap,
nexts : [],
next : null
}
}
function sync9_space_dag_get(S, i, is_anc) {
var ret = null
var offset = 0
sync9_trav_space_dag(S, is_anc ? is_anc : () => true, (node) => {
if (i - offset < node.elems.length) {
ret = node.elems[i - offset]
return false
}
offset += node.elems.length
})
return ret
}
function sync9_space_dag_length(S, is_anc) {
var count = 0
sync9_trav_space_dag(S, is_anc ? is_anc : () => true, node => {
count += node.elems.length
})
return count
}
function sync9_space_dag_break_node(node, x, end_cap, new_next) {
function subseq(x, start, stop) {
return (x instanceof Array) ?
x.slice(start, stop) :
x.substring(start, stop)
}
var tail = sync9_create_space_dag_node(null, subseq(node.elems, x), node.end_cap)
Object.assign(tail.deleted_by, node.deleted_by)
tail.nexts = node.nexts
tail.next = node.next
node.elems = subseq(node.elems, 0, x)
node.end_cap = end_cap
if (end_cap) tail.gash = true
node.nexts = new_next ? [new_next] : []
node.next = tail
return tail
}
function sync9_space_dag_add_version(S, vid, splices, is_anc) {
function add_to_nexts(nexts, to) {
var i = binarySearch(nexts, function (x) {
if (to.vid < x.vid) return -1
if (to.vid > x.vid) return 1
return 0
})
nexts.splice(i, 0, to)
}
var si = 0
var delete_up_to = 0
var cb = (node, offset, has_nexts, prev, _vid, deleted) => {
var s = splices[si]
if (!s) return false
if (deleted) {
if (s[1] == 0 && s[0] == offset) {
if (node.elems.length == 0 && !node.end_cap && has_nexts) return
var new_node = sync9_create_space_dag_node(vid, s[2])
if (node.elems.length == 0 && !node.end_cap)
add_to_nexts(node.nexts, new_node)
else
sync9_space_dag_break_node(node, 0, undefined, new_node)
si++
}
return
}
if (s[1] == 0) {
var d = s[0] - (offset + node.elems.length)
if (d > 0) return
if (d == 0 && !node.end_cap && has_nexts) return
var new_node = sync9_create_space_dag_node(vid, s[2])
if (d == 0 && !node.end_cap) {
add_to_nexts(node.nexts, new_node)
} else {
sync9_space_dag_break_node(node, s[0] - offset, undefined, new_node)
}
si++
return
}
if (delete_up_to <= offset) {
var d = s[0] - (offset + node.elems.length)
if (d >= 0) return
delete_up_to = s[0] + s[1]
if (s[2]) {
var new_node = sync9_create_space_dag_node(vid, s[2])
if (s[0] == offset && node.gash) {
if (!prev.end_cap) throw 'no end_cap?'
add_to_nexts(prev.nexts, new_node)
} else {
sync9_space_dag_break_node(node, s[0] - offset, true, new_node)
return
}
} else {
if (s[0] == offset) {
} else {
sync9_space_dag_break_node(node, s[0] - offset)
return
}