-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdiffer.go
793 lines (636 loc) · 19.5 KB
/
differ.go
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
package mendoza
import (
"github.com/sanity-io/mendoza/internal/mendoza"
"unicode/utf8"
)
type differ struct {
left *mendoza.HashList
right *mendoza.HashList
hashIndex *mendoza.HashIndex
options *Options
}
// Creates a patch which can be applied to the left document to produce the right document.
//
// This function uses the default options.
func CreatePatch(left, right interface{}) (Patch, error) {
return DefaultOptions.CreatePatch(left, right)
}
// Creates two patches: The first can be applied to the left document to produce the right document,
// the second can be applied to the right document to produce the left document.
//
// This function uses the default options.
func CreateDoublePatch(left, right interface{}) (Patch, Patch, error) {
return DefaultOptions.CreateDoublePatch(left, right)
}
// Creates a patch which can be applied to the left document to produce the right document.
func (options *Options) CreatePatch(left, right interface{}) (Patch, error) {
if left == nil {
if right == nil {
return Patch{}, nil
}
return Patch{&OpValue{right}}, nil
}
leftList, err := mendoza.HashListFor(left, options.convertFunc)
if err != nil {
return nil, err
}
rightList, err := mendoza.HashListFor(right, options.convertFunc)
if err != nil {
return nil, err
}
hashIndex := mendoza.NewHashIndex(leftList)
differ := differ{
left: leftList,
right: rightList,
hashIndex: hashIndex,
}
return differ.build(), nil
}
// Creates two patches: The first can be applied to the left document to produce the right document,
// the second can be applied to the right document to produce the left document.
func (options *Options) CreateDoublePatch(left, right interface{}) (Patch, Patch, error) {
if left == nil && right == nil {
return Patch{}, Patch{}, nil
}
if left == nil {
return Patch{&OpValue{right}}, Patch{&OpValue{nil}}, nil
}
if right == nil {
return Patch{&OpValue{nil}}, Patch{&OpValue{left}}, nil
}
leftList, err := mendoza.HashListFor(left, options.convertFunc)
if err != nil {
return nil, nil, err
}
rightList, err := mendoza.HashListFor(right, options.convertFunc)
if err != nil {
return nil, nil, err
}
leftHashIndex := mendoza.NewHashIndex(leftList)
leftDiffer := differ{
left: leftList,
right: rightList,
hashIndex: leftHashIndex,
options: options,
}
rightHashIndex := mendoza.NewHashIndex(rightList)
rightDiffer := differ{
left: rightList,
right: leftList,
hashIndex: rightHashIndex,
options: options,
}
return leftDiffer.build(), rightDiffer.build(), nil
}
/*
The main function in the differ is `reconstruct` which takes two parameters.
The first is the target value: a reference to a value in the right-hand side.
The second parameter is a slice of _requests_. A request has an _initial
context_ which is a reference to a value in the left-hand side. The goal of
`reconstruct` is then, for each request, build a patch which converts the
_initial context_ into the target value. Every request also keeps track of the
_best size_.
For arrays/objects this is done in a few steps:
1. First we look at the target (ignoring any context) and find other values that are good candidates.
This is a cheap check which uses the global hash index and only looks for objects/arrays that
shares some fields/elements with the target. This can only check for exact equality.
2. Then we do filtering of candidates based on the requests' context:
- Currently we only care about candidates that are direct children of the context.
- Per request we pick the N best candidates to explore. This is just an heuristic because we only know
"how many fields/elements they have in common" and nothing about the size of representing the patch of
the differences.
3. For each field/element in the target we can then build a list of requests (based on the candidates)
and recurse into `reconstruct`. The more candidates we have in the previous step, the more requests
we'll have in this step, and the time complexity will be higher.
4. At this point we know have full information about each candidate: All patches are calculated.
We then just need to connect each candidate with a request and we're done!
*/
func (d *differ) build() Patch {
root := d.right.Entries[0]
if d.left.Entries[0].Hash == root.Hash {
// Exact same value
return Patch{}
}
reqs := []request{
{
contextIdx: -1,
primaryIdx: 0,
size: root.Size + 1,
},
}
d.reconstruct(0, reqs)
req := reqs[0]
if req.patch == nil {
return Patch{&OpValue{root.Value}}
}
return req.patch
}
type request struct {
contextIdx int
primaryIdx int
size int
patch Patch
outputKey string
}
func (req *request) update(patch Patch, size int, outputKey string) {
if size < req.size {
req.patch = patch
req.size = size
req.outputKey = outputKey
}
}
func (d *differ) reconstruct(idx int, reqs []request) {
if len(reqs) == 0 {
return
}
entry := d.right.Entries[idx]
if entry.IsNonEmptyMap() {
d.reconstructMap(idx, reqs)
return
}
if entry.IsNonEmptySlice() {
d.reconstructSlice(idx, reqs)
return
}
if rightString, ok := entry.Value.(string); ok {
d.reconstructString(idx, rightString, reqs)
return
}
}
func (d *differ) enterBlank(patch *Patch, idx int) {
if idx == 0 {
*patch = append(*patch, &OpBlank{})
return
}
entry := d.left.Entries[idx]
parentEntry := d.left.Entries[entry.Parent]
var op Op
if parentEntry.IsNonEmptyMap() {
op = &OpPushFieldBlank{OpPushField: OpPushField{entry.Reference.Index}}
} else {
op = &OpPushElementBlank{OpPushElement: OpPushElement{entry.Reference.Index}}
}
*patch = append(*patch, op)
}
func (d *differ) enterCopy(patch *Patch, idx int) {
if idx == 0 {
// Root => Already on the stack.
return
}
entry := d.left.Entries[idx]
parentEntry := d.left.Entries[entry.Parent]
var op Op
if parentEntry.IsNonEmptyMap() {
op = &OpPushFieldCopy{OpPushField: OpPushField{entry.Reference.Index}}
} else {
op = &OpPushElementCopy{OpPushElement: OpPushElement{entry.Reference.Index}}
}
*patch = append(*patch, op)
}
/*
Some notes about how we're building up calls from candidates:
An object has multiple fields: {"a": "…", "b": …, "c": …}, and we find candidates of objects that are similar.
Imagine that we found three candidates:
- cand1 has {"a": "…", "b": …, "c": "different"}
- cand2 has {"a": "…", "b": "different", "c": …}
- cand3 has {"a": "…", "b": "different", "c": "different"}
- cand4 has {"a": "…", "b": …, "c": "different2"}
The crucial thing here is that we only need 2 recursive `reconstruct` calls (because `a` is equal in all candidates),
but some of the calls might have multiple requests.
In this case the 2 recursive calls are as follows:
- Reconstruct "c" with requests: [cand1, cand3, cand4]
- Reconstruct "b" with requests: [cand2, cand3]
*/
// This stores information about each candidate map in the left-side document.
type mapCandidate struct {
alias map[string]mapAlias
seenKeys map[string]struct{}
requestIdx int
contextIdx int
}
type mapAlias struct {
fieldIdx int
sameKey bool
}
func (mc *mapCandidate) init(contextIdx int, requestIdx int) {
mc.alias = make(map[string]mapAlias)
mc.seenKeys = make(map[string]struct{})
mc.requestIdx = requestIdx
mc.contextIdx = contextIdx
}
func (mc *mapCandidate) insertAlias(target mendoza.Reference, source mendoza.Reference, size int) {
current, currentOk := mc.alias[target.Key]
mc.seenKeys[target.Key] = struct{}{}
if target.Key == source.Key {
mc.alias[target.Key] = mapAlias{
fieldIdx: source.Index,
sameKey: true,
}
return
}
if currentOk {
if current.sameKey {
// Prefer sameKey
return
}
if current.fieldIdx < source.Index {
// Prefer lowest fieldIdx to ensure consistent patches
return
}
}
mc.alias[target.Key] = mapAlias{
fieldIdx: source.Index,
sameKey: false,
}
}
func (mc *mapCandidate) IsMissing(reference mendoza.Reference) bool {
_, ok := mc.alias[reference.Key]
return !ok
}
func (mc *mapCandidate) RegisterRequest(childIdx int, childRef mendoza.Reference, reqIdx int) {
mc.seenKeys[childRef.Key] = struct{}{}
}
func (d *differ) reconstructMap(idx int, reqs []request) {
// right-index -> list of requests
fieldRequests := [][]request{}
// The input here is a list of requests. Each requests has _context_ and _primary_ which looks like this:
//
// Left Right
// |
// Context
// |
// Primary Target
//
// The mandate of this method is to "assuming the program is currently in _context_; create a patch which
// produces _target_". Note that _primary_ is just a hint for where this method could look for differences.
// It's typically the field with same name, or an element in the same position. We can also assume that
// _primary_ is different from _target_ (otherwise why would you ask for the differences?).
//
// Note that context can either be an array or an object.
// Currently we're only looking at primary.
candidates := make([]mapCandidate, 0, len(reqs))
for i, req := range reqs {
if !d.left.Entries[req.primaryIdx].IsNonEmptyMap() {
continue
}
cand := mapCandidate{}
cand.init(req.primaryIdx, i)
candidates = append(candidates, cand)
}
entry := d.right.Entries[idx]
// Use the xor-index to find fields that differ a bit
for it := d.right.Iter(idx); !it.IsDone(); it.Next() {
fieldEntry := it.GetEntry()
xorHash := entry.XorHash
xorHash.Xor(fieldEntry.Hash)
for _, otherIdx := range d.hashIndex.XorData[xorHash] {
otherEntry := d.left.Entries[otherIdx]
for i, req := range reqs {
if otherEntry.Parent == req.contextIdx && otherIdx != req.primaryIdx {
cand := mapCandidate{}
cand.init(otherIdx, i)
candidates = append(candidates, cand)
}
}
}
}
for it := d.right.Iter(idx); !it.IsDone(); it.Next() {
fieldEntry := it.GetEntry()
fieldRequests = append(fieldRequests, nil)
for _, otherIdx := range d.hashIndex.Data[fieldEntry.Hash] {
otherEntry := d.left.Entries[otherIdx]
for candIdx := range candidates {
cand := &candidates[candIdx]
if cand.contextIdx == otherEntry.Parent {
cand.insertAlias(fieldEntry.Reference, otherEntry.Reference, fieldEntry.Size)
}
}
}
}
// Now build the requests
for _, cand := range candidates {
contextIter := d.left.Iter(cand.contextIdx)
i := 0
for it := d.right.Iter(idx); !it.IsDone(); it.Next() {
fieldEntry := it.GetEntry()
fieldKey := fieldEntry.Reference.Key
cand.seenKeys[fieldKey] = struct{}{}
if _, ok := cand.alias[fieldKey]; !ok {
for !contextIter.IsDone() {
key := contextIter.GetKey()
if key > fieldKey {
break
}
if key == fieldKey {
fieldReqs := fieldRequests[i]
fieldReqs = append(fieldReqs, request{
contextIdx: cand.contextIdx,
primaryIdx: contextIter.GetIndex(),
size: fieldEntry.Size + 1,
})
fieldRequests[i] = fieldReqs
break
}
contextIter.Next()
}
}
i++
}
}
for it := d.right.Iter(idx); !it.IsDone(); it.Next() {
d.reconstruct(it.GetIndex(), fieldRequests[it.GetEntry().Reference.Index])
}
for _, cand := range candidates {
primaryIdx := cand.contextIdx
size := 0
patch := Patch{}
removeKeys := map[string]int{}
for it := d.left.Iter(primaryIdx); !it.IsDone(); it.Next() {
ref := it.GetEntry().Reference
if _, ok := cand.seenKeys[ref.Key]; ok {
// do nothing
} else {
removeKeys[ref.Key] = ref.Index
}
}
removeCount := len(removeKeys)
aliasCount := len(cand.alias)
isCopy := false
if removeCount < aliasCount {
// Note: This doesn't currently take into account that we have two types of aliasing.
// It's shorter to alias a field with the same key (one single copy field).
// For now let's assume that the difference here is pretty small either way.
isCopy = true
}
if isCopy {
d.enterCopy(&patch, primaryIdx)
} else {
d.enterBlank(&patch, primaryIdx)
}
size += 2
if isCopy {
// Delete fields we don't need
for _, removeIdx := range removeKeys {
patch = append(patch, &OpObjectDeleteField{removeIdx})
size += 2
}
}
for it := d.right.Iter(idx); !it.IsDone(); it.Next() {
fieldEntry := it.GetEntry()
fieldKey := fieldEntry.Reference.Key
if alias, ok := cand.alias[fieldKey]; ok {
if alias.sameKey {
if !isCopy {
// We only need this if we're starting with a blank object
patch = append(patch, &OpObjectCopyField{OpPushField: OpPushField{alias.fieldIdx}})
size += 2
}
} else {
patch = append(patch,
&OpPushFieldCopy{OpPushField: OpPushField{alias.fieldIdx}},
&OpReturnIntoObjectPop{OpReturnIntoObject: OpReturnIntoObject{fieldKey}},
)
size += 2 + 1 + len(fieldKey)
}
} else {
didPatch := false
// Not an alias. Look up the real diff.
for _, fieldReq := range fieldRequests[fieldEntry.Reference.Index] {
if fieldReq.contextIdx == cand.contextIdx {
// Match it up with the correct context
if fieldReq.patch != nil {
patch = append(patch, fieldReq.patch...)
size += fieldReq.size
if fieldReq.outputKey == fieldKey {
patch = append(patch, &OpReturnIntoObjectSameKeyPop{})
size += 1
} else {
patch = append(patch, &OpReturnIntoObjectPop{OpReturnIntoObject: OpReturnIntoObject{fieldKey}})
size += 1 + len(fieldKey)
}
didPatch = true
}
break
}
}
if !didPatch {
patch = append(patch, &OpObjectSetFieldValue{
OpValue{fieldEntry.Value},
OpReturnIntoObject{fieldKey},
})
size += 1 + len(fieldKey) + fieldEntry.Size
}
}
}
req := &reqs[cand.requestIdx]
req.update(patch, size, d.left.Entries[primaryIdx].Reference.Key)
}
}
type sliceRequestData struct {
candidates map[int]*sliceCandidate
}
type sliceAlias struct {
elementIdx int
prevIsAdjacent bool
nextIsAdjacent bool
}
type sliceCandidate struct {
alias map[int]sliceAlias
requestIdx int
contextIdx int
}
func (sc *sliceCandidate) init(contextIdx int, requestIdx int) {
sc.alias = map[int]sliceAlias{}
sc.requestIdx = requestIdx
sc.contextIdx = contextIdx
}
func (sc *sliceCandidate) insertAlias(target mendoza.Reference, source mendoza.Reference, size int) {
// We assume here that you'll only invoke this method in the same order
// as you want to build the array.
current, ok := sc.alias[target.Index]
if ok && current.prevIsAdjacent {
// Once we've found something which is adjacent. Don't look any further.
return
}
if prevSource, prevOk := sc.alias[target.Index-1]; prevOk {
if prevSource.elementIdx+1 == source.Index {
// This one is perfectly adjacent. Use it!
sc.alias[target.Index] = sliceAlias{
elementIdx: source.Index,
prevIsAdjacent: true,
}
prevSource.nextIsAdjacent = true
sc.alias[target.Index-1] = prevSource
return
}
if source.Index <= prevSource.elementIdx {
// We want to prefer values that are _after_ the previous index.
if ok {
// However, we can only return if we've already found a value.
// Otherwise we must use this new value.
return
}
}
}
if ok && current.elementIdx < source.Index {
// Prefer smaller over larger
return
}
sc.alias[target.Index] = sliceAlias{
elementIdx: source.Index,
prevIsAdjacent: false,
}
}
func (d *differ) reconstructSlice(idx int, reqs []request) {
// right-index -> requests
elementRequests := [][]request{}
candidates := make([]sliceCandidate, 0, len(reqs))
for i, req := range reqs {
if !d.left.Entries[req.primaryIdx].IsNonEmptySlice() {
continue
}
cand := sliceCandidate{}
cand.init(req.primaryIdx, i)
candidates = append(candidates, cand)
}
for it := d.right.Iter(idx); !it.IsDone(); it.Next() {
elementEntry := it.GetEntry()
elementRequests = append(elementRequests, nil)
for _, otherIdx := range d.hashIndex.Data[elementEntry.Hash] {
otherEntry := d.left.Entries[otherIdx]
for candIdx := range candidates {
cand := &candidates[candIdx]
if cand.contextIdx == otherEntry.Parent {
cand.insertAlias(elementEntry.Reference, otherEntry.Reference, elementEntry.Size)
}
}
}
}
// Now build the requests
for _, cand := range candidates {
contextIter := d.left.Iter(cand.contextIdx)
i := 0
for it := d.right.Iter(idx); !it.IsDone(); it.Next() {
if contextIter.IsDone() {
break
}
elementEntry := it.GetEntry()
if _, ok := cand.alias[elementEntry.Reference.Index]; !ok {
elementReqs := elementRequests[i]
elementReqs = append(elementReqs, request{
contextIdx: cand.contextIdx,
primaryIdx: contextIter.GetIndex(),
size: elementEntry.Size + 1,
})
elementRequests[i] = elementReqs
}
i++
contextIter.Next()
}
}
for it := d.right.Iter(idx); !it.IsDone(); it.Next() {
d.reconstruct(it.GetIndex(), elementRequests[it.GetEntry().Reference.Index])
}
for _, cand := range candidates {
contextIdx := cand.contextIdx
size := 0
patch := Patch{}
d.enterBlank(&patch, contextIdx)
size += 2
startSlice := -1
for it := d.right.Iter(idx); !it.IsDone(); it.Next() {
elementEntry := it.GetEntry()
pos := elementEntry.Reference.Index
if alias, ok := cand.alias[pos]; ok {
if startSlice == -1 {
startSlice = alias.elementIdx
}
if alias.nextIsAdjacent {
// The next one is adjacent. We don't need to do anything!
} else {
patch = append(patch, &OpArrayAppendSlice{startSlice, alias.elementIdx + 1})
size += 3
startSlice = -1
}
} else {
didPatch := false
for _, elementReq := range elementRequests[pos] {
if elementReq.contextIdx == contextIdx {
if elementReq.patch != nil {
patch = append(patch, elementReq.patch...)
size += elementReq.size
patch = append(patch, &OpReturnIntoArrayPop{})
size += 1
didPatch = true
}
break
}
}
if !didPatch {
patch = append(patch, &OpArrayAppendValue{elementEntry.Value})
size += 1 + elementEntry.Size
}
}
}
req := &reqs[cand.requestIdx]
req.update(patch, size, d.left.Entries[contextIdx].Reference.Key)
}
}
// String handling
func commonPrefix(a, b string) int {
i := 0
for i < len(a) && i < len(b) {
ar, size := utf8.DecodeRuneInString(a[i:])
br, _ := utf8.DecodeRuneInString(b[i:])
if ar != br {
break
}
i += size
}
return i
}
func commonSuffix(a, b string, prefix int) int {
i := 0
for i < len(a)-prefix && i < len(b)-prefix {
ar, size := utf8.DecodeLastRuneInString(a[:len(a)-i])
br, _ := utf8.DecodeLastRuneInString(b[:len(b)-i])
if ar != br {
break
}
i += size
}
return i
}
func (d *differ) reconstructString(idx int, rightString string, reqs []request) {
for reqIdx, req := range reqs {
leftEntry := d.left.Entries[req.primaryIdx]
leftString, ok := leftEntry.Value.(string)
if !ok {
continue
}
if leftString == rightString {
panic("unnecessary reconstruction of string")
}
patch := Patch{}
size := 0
d.enterBlank(&patch, req.primaryIdx)
size += 2
prefix := commonPrefix(leftString, rightString)
if prefix > 0 {
patch = append(patch, &OpStringAppendSlice{0, prefix})
size += 3
}
suffix := commonSuffix(leftString, rightString, prefix)
mid := rightString[prefix : len(rightString)-suffix]
if len(mid) > 0 {
patch = append(patch, &OpStringAppendString{mid})
size += 1 + len(mid)
}
if suffix > 0 {
patch = append(patch, &OpStringAppendSlice{len(leftString) - suffix, len(leftString)})
size += 3
}
req := &reqs[reqIdx]
req.update(patch, size, d.left.Entries[req.primaryIdx].Reference.Key)
}
}