/
three_way_ordered_sequence.go
283 lines (230 loc) · 7.78 KB
/
three_way_ordered_sequence.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
// Copyright 2019 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package merge
import (
"context"
"fmt"
"golang.org/x/sync/errgroup"
"github.com/dolthub/dolt/go/store/d"
"github.com/dolthub/dolt/go/store/types"
)
type applyFunc func(candidate, types.ValueChanged, types.Value) (candidate, error)
func (m *merger) threeWayOrderedSequenceMerge(ctx context.Context, a, b, parent candidate, apply applyFunc, path types.Path) (types.Value, error) {
aChangeChan, bChangeChan := make(chan types.ValueChanged), make(chan types.ValueChanged)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
defer close(aChangeChan)
return a.diff(ctx, parent, aChangeChan)
})
eg.Go(func() error {
defer close(bChangeChan)
return b.diff(ctx, parent, bChangeChan)
})
merged := parent
eg.Go(func() error {
aChange, bChange := types.ValueChanged{}, types.ValueChanged{}
for {
// Get the next change from both a and b. If either diff(a,
// parent) or diff(b, parent) is complete, aChange or bChange
// will get an empty types.ValueChanged containing a nil Value.
// Generally, though, this allows us to proceed through both
// diffs in (key) order, considering the "current" change from
// both diffs at the same time.
if aChange.Key == nil {
select {
case aChange = <-aChangeChan:
case <-ctx.Done():
return ctx.Err()
}
}
if bChange.Key == nil {
select {
case bChange = <-bChangeChan:
case <-ctx.Done():
return ctx.Err()
}
}
// Both channels are producing zero values, so we're done.
if aChange.Key == nil && bChange.Key == nil {
break
}
// Since diff generates changes in key-order, and we
// never skip over a change without processing it, we
// can simply compare the keys at which aChange and
// bChange occurred to determine if either is safe to
// apply to the merge result without further
// processing. This is because if, e.g.
// aChange.V.Less(bChange.V), we know that the diff of
// b will never generate a change at that key. If it
// was going to, it would have done so on an earlier
// iteration of this loop and been processed at that
// time.
//
// It's also obviously OK to apply a change if only one
// diff is generating any changes, e.g. aChange.V is
// non-nil and bChange.V is nil.
if aChange.Key != nil {
var err error
noBOrALessB := bChange.Key == nil
if !noBOrALessB {
noBOrALessB, err = aChange.Key.Less(m.vrw.Format(), bChange.Key)
if err != nil {
return err
}
}
if noBOrALessB {
v, _, err := a.get(ctx, aChange.Key)
if err != nil {
return err
}
merged, err = apply(merged, aChange, v)
if err != nil {
return err
}
aChange = types.ValueChanged{}
continue
}
}
if bChange.Key != nil {
var err error
noAOrBLessA := aChange.Key == nil
if !noAOrBLessA {
noAOrBLessA, err = bChange.Key.Less(m.vrw.Format(), aChange.Key)
if err != nil {
return err
}
}
if noAOrBLessA {
v, _, err := b.get(ctx, bChange.Key)
if err != nil {
return err
}
merged, err = apply(merged, bChange, v)
if err != nil {
return err
}
bChange = types.ValueChanged{}
continue
}
}
if !aChange.Key.Equals(bChange.Key) {
d.Panic("Diffs have skewed!") // Sanity check.
}
change, mergedVal, err := m.mergeChanges(ctx, aChange, bChange, a, b, parent, apply, path)
if err != nil {
return err
}
merged, err = apply(merged, change, mergedVal)
if err != nil {
return err
}
aChange, bChange = types.ValueChanged{}, types.ValueChanged{}
}
return nil
})
if err := eg.Wait(); err != nil {
return nil, err
}
return merged.getValue(), nil
}
func (m *merger) mergeChanges(ctx context.Context, aChange, bChange types.ValueChanged, a, b, p candidate, apply applyFunc, path types.Path) (change types.ValueChanged, mergedVal types.Value, err error) {
path, err = a.pathConcat(ctx, aChange, path)
if err != nil {
return types.ValueChanged{}, nil, err
}
aValue, _, err := a.get(ctx, aChange.Key)
if err != nil {
return types.ValueChanged{}, nil, err
}
bValue, _, err := b.get(ctx, bChange.Key)
if err != nil {
return types.ValueChanged{}, nil, err
}
// If the two diffs generate different kinds of changes at the same key, conflict.
if aChange.ChangeType != bChange.ChangeType {
if change, mergedVal, ok := m.resolve(aChange.ChangeType, bChange.ChangeType, aValue, bValue, path); ok {
// TODO: Correctly encode Old/NewValue with this change report. https://github.com/attic-labs/noms/issues/3467
return types.ValueChanged{ChangeType: change, Key: aChange.Key, OldValue: nil, NewValue: nil}, mergedVal, nil
}
aDesc, err := describeChange(aChange)
if err != nil {
return types.ValueChanged{}, nil, err
}
bDesc, err := describeChange(bChange)
if err != nil {
return types.ValueChanged{}, nil, err
}
return change, nil, newMergeConflict("Conflict:\n%s\nvs\n%s\n", aDesc, bDesc)
}
if aChange.ChangeType == types.DiffChangeRemoved || aValue.Equals(bValue) {
// If both diffs generated a remove, or if the new value is the same in both, merge is fine.
return aChange, aValue, nil
}
// There's one case that might still be OK even if aValue and bValue differ: different, but mergeable, compound values of the same type being added/modified at the same key, e.g. a Map being added to both a and b. If either is a primitive, or Values of different Kinds were added, though, we're in conflict.
if !unmergeable(aValue, bValue) {
v, _, err := p.get(ctx, aChange.Key)
if err != nil {
return types.ValueChanged{}, nil, err
}
// TODO: Add concurrency.
if mergedVal, err = m.threeWay(ctx, aValue, bValue, v, path); err == nil {
return aChange, mergedVal, nil
}
return change, nil, err
}
if change, mergedVal, ok := m.resolve(aChange.ChangeType, bChange.ChangeType, aValue, bValue, path); ok {
// TODO: Correctly encode Old/NewValue with this change report. https://github.com/attic-labs/noms/issues/3467
return types.ValueChanged{ChangeType: change, Key: aChange.Key, OldValue: nil, NewValue: nil}, mergedVal, nil
}
aStr, err := types.EncodedValue(ctx, aValue)
if err != nil {
return types.ValueChanged{}, nil, err
}
aDesc, err := describeChange(aChange)
if err != nil {
return types.ValueChanged{}, nil, err
}
bStr, err := types.EncodedValue(ctx, bValue)
if err != nil {
return types.ValueChanged{}, nil, err
}
bDesc, err := describeChange(bChange)
if err != nil {
return types.ValueChanged{}, nil, err
}
return change, nil, newMergeConflict("Conflict:\n%s = %s\nvs\n%s = %s", aDesc, aStr, bDesc, bStr)
}
func describeChange(change types.ValueChanged) (string, error) {
op := ""
switch change.ChangeType {
case types.DiffChangeAdded:
op = "added"
case types.DiffChangeModified:
op = "modded"
case types.DiffChangeRemoved:
op = "removed"
}
str, err := types.EncodedValue(context.Background(), change.Key)
if err != nil {
return "", err
}
return fmt.Sprintf("%s %s", op, str), nil
}