-
Notifications
You must be signed in to change notification settings - Fork 15
/
diff.go
392 lines (314 loc) · 8.62 KB
/
diff.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
package amt
import (
"bytes"
"context"
"encoding/json"
"fmt"
"golang.org/x/xerrors"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
cbg "github.com/whyrusleeping/cbor-gen"
)
// ChangeType denotes type of change in Change
type ChangeType int
// These constants define the changes that can be applied to a DAG.
const (
Add ChangeType = iota
Remove
Modify
)
// Change represents a change to a DAG and contains a reference to the old and
// new CIDs.
type Change struct {
Type ChangeType
Key uint64
Before *cbg.Deferred
After *cbg.Deferred
}
func (ch Change) String() string {
b, _ := json.Marshal(ch)
return string(b)
}
// Diff returns a set of changes that transform node 'a' into node 'b'. opts are applied to both prev and cur.
func Diff(ctx context.Context, prevBs, curBs cbor.IpldStore, prev, cur cid.Cid, opts ...Option) ([]*Change, error) {
prevAmt, err := LoadAMT(ctx, prevBs, prev, opts...)
if err != nil {
return nil, xerrors.Errorf("loading previous root: %w", err)
}
prevCtx := &nodeContext{
bs: prevBs,
bitWidth: prevAmt.bitWidth,
height: prevAmt.height,
}
curAmt, err := LoadAMT(ctx, curBs, cur, opts...)
if err != nil {
return nil, xerrors.Errorf("loading current root: %w", err)
}
// TODO: remove when https://github.com/filecoin-project/go-amt-ipld/issues/54 is closed.
if curAmt.bitWidth != prevAmt.bitWidth {
return nil, xerrors.Errorf("diffing AMTs with differing bitWidths not supported (prev=%d, cur=%d)", prevAmt.bitWidth, curAmt.bitWidth)
}
curCtx := &nodeContext{
bs: curBs,
bitWidth: curAmt.bitWidth,
height: curAmt.height,
}
// edge case of diffing an empty AMT against non-empty
if prevAmt.count == 0 && curAmt.count != 0 {
return addAll(ctx, curCtx, curAmt.node, 0)
}
if prevAmt.count != 0 && curAmt.count == 0 {
return removeAll(ctx, prevCtx, prevAmt.node, 0)
}
return diffNode(ctx, prevCtx, curCtx, prevAmt.node, curAmt.node, 0)
}
type nodeContext struct {
bs cbor.IpldStore // store containining AMT data
bitWidth uint // bit width of AMT
height int // height of node
}
// nodesAtHeight returns the number of nodes that can be held at the context height
func (nc *nodeContext) nodesAtHeight() uint64 {
return nodesForHeight(nc.bitWidth, nc.height)
}
func diffNode(ctx context.Context, prevCtx, curCtx *nodeContext, prev, cur *node, offset uint64) ([]*Change, error) {
if prev == nil && cur == nil {
return nil, nil
}
if prev == nil {
return addAll(ctx, curCtx, cur, offset)
}
if cur == nil {
return removeAll(ctx, prevCtx, prev, offset)
}
if prevCtx.height == 0 && curCtx.height == 0 {
return diffLeaves(prev, cur, offset)
}
var changes []*Change
if curCtx.height > prevCtx.height {
subCount := curCtx.nodesAtHeight()
for i, ln := range cur.links {
if ln == nil || ln.cid == cid.Undef {
continue
}
subCtx := &nodeContext{
bs: curCtx.bs,
bitWidth: curCtx.bitWidth,
height: curCtx.height - 1,
}
subn, err := ln.load(ctx, subCtx.bs, subCtx.bitWidth, subCtx.height)
if err != nil {
return nil, err
}
offs := offset + (uint64(i) * subCount)
if i == 0 {
cs, err := diffNode(ctx, prevCtx, subCtx, prev, subn, offs)
if err != nil {
return nil, err
}
changes = append(changes, cs...)
} else {
cs, err := addAll(ctx, subCtx, subn, offs)
if err != nil {
return nil, err
}
changes = append(changes, cs...)
}
}
return changes, nil
}
if prevCtx.height > curCtx.height {
subCount := prevCtx.nodesAtHeight()
for i, ln := range prev.links {
if ln == nil || ln.cid == cid.Undef {
continue
}
subCtx := &nodeContext{
bs: prevCtx.bs,
bitWidth: prevCtx.bitWidth,
height: prevCtx.height - 1,
}
subn, err := ln.load(ctx, subCtx.bs, subCtx.bitWidth, subCtx.height)
if err != nil {
return nil, err
}
offs := offset + (uint64(i) * subCount)
if i == 0 {
cs, err := diffNode(ctx, subCtx, curCtx, subn, cur, offs)
if err != nil {
return nil, err
}
changes = append(changes, cs...)
} else {
cs, err := removeAll(ctx, subCtx, subn, offs)
if err != nil {
return nil, err
}
changes = append(changes, cs...)
}
}
return changes, nil
}
// sanity check
if prevCtx.height != curCtx.height {
return nil, fmt.Errorf("comparing non-leaf nodes of unequal heights (%d, %d)", prevCtx.height, curCtx.height)
}
if len(prev.links) != len(cur.links) {
return nil, fmt.Errorf("nodes have different numbers of links (prev=%d, cur=%d)", len(prev.links), len(cur.links))
}
if prev.links == nil || cur.links == nil {
return nil, fmt.Errorf("nodes have no links")
}
subCount := prevCtx.nodesAtHeight()
for i := range prev.links {
// Neither previous or current links are in use
if prev.links[i] == nil && cur.links[i] == nil {
continue
}
// Previous had link, current did not
if prev.links[i] != nil && cur.links[i] == nil {
if prev.links[i].cid == cid.Undef {
continue
}
subCtx := &nodeContext{
bs: prevCtx.bs,
bitWidth: prevCtx.bitWidth,
height: prevCtx.height - 1,
}
subn, err := prev.links[i].load(ctx, subCtx.bs, subCtx.bitWidth, subCtx.height)
if err != nil {
return nil, err
}
offs := offset + (uint64(i) * subCount)
cs, err := removeAll(ctx, subCtx, subn, offs)
if err != nil {
return nil, err
}
changes = append(changes, cs...)
continue
}
// Current has link, previous did not
if prev.links[i] == nil && cur.links[i] != nil {
if cur.links[i].cid == cid.Undef {
continue
}
subCtx := &nodeContext{
bs: curCtx.bs,
bitWidth: curCtx.bitWidth,
height: curCtx.height - 1,
}
subn, err := cur.links[i].load(ctx, subCtx.bs, subCtx.bitWidth, subCtx.height)
if err != nil {
return nil, err
}
offs := offset + (uint64(i) * subCount)
cs, err := addAll(ctx, subCtx, subn, offs)
if err != nil {
return nil, err
}
changes = append(changes, cs...)
continue
}
// Both previous and current have links to diff
if prev.links[i].cid == cur.links[i].cid {
continue
}
prevSubCtx := &nodeContext{
bs: prevCtx.bs,
bitWidth: prevCtx.bitWidth,
height: prevCtx.height - 1,
}
prevSubn, err := prev.links[i].load(ctx, prevSubCtx.bs, prevSubCtx.bitWidth, prevSubCtx.height)
if err != nil {
return nil, err
}
curSubCtx := &nodeContext{
bs: curCtx.bs,
bitWidth: curCtx.bitWidth,
height: curCtx.height - 1,
}
curSubn, err := cur.links[i].load(ctx, curSubCtx.bs, curSubCtx.bitWidth, curSubCtx.height)
if err != nil {
return nil, err
}
offs := offset + (uint64(i) * subCount)
cs, err := diffNode(ctx, prevSubCtx, curSubCtx, prevSubn, curSubn, offs)
if err != nil {
return nil, err
}
changes = append(changes, cs...)
}
return changes, nil
}
func addAll(ctx context.Context, nc *nodeContext, node *node, offset uint64) ([]*Change, error) {
var changes []*Change
err := node.forEachAt(ctx, nc.bs, nc.bitWidth, nc.height, 0, offset, func(index uint64, deferred *cbg.Deferred) error {
changes = append(changes, &Change{
Type: Add,
Key: index,
Before: nil,
After: deferred,
})
return nil
})
if err != nil {
return nil, err
}
return changes, nil
}
func removeAll(ctx context.Context, nc *nodeContext, node *node, offset uint64) ([]*Change, error) {
var changes []*Change
err := node.forEachAt(ctx, nc.bs, nc.bitWidth, nc.height, 0, offset, func(index uint64, deferred *cbg.Deferred) error {
changes = append(changes, &Change{
Type: Remove,
Key: index,
Before: deferred,
After: nil,
})
return nil
})
if err != nil {
return nil, err
}
return changes, nil
}
func diffLeaves(prev, cur *node, offset uint64) ([]*Change, error) {
if len(prev.values) != len(cur.values) {
return nil, fmt.Errorf("node leaves have different numbers of values (prev=%d, cur=%d)", len(prev.values), len(cur.values))
}
var changes []*Change
for i, prevVal := range prev.values {
index := offset + uint64(i)
curVal := cur.values[i]
if prevVal == nil && curVal == nil {
continue
}
if prevVal == nil && curVal != nil {
changes = append(changes, &Change{
Type: Add,
Key: index,
Before: nil,
After: curVal,
})
continue
}
if prevVal != nil && curVal == nil {
changes = append(changes, &Change{
Type: Remove,
Key: index,
Before: prevVal,
After: nil,
})
continue
}
if !bytes.Equal(prevVal.Raw, curVal.Raw) {
changes = append(changes, &Change{
Type: Modify,
Key: index,
Before: prevVal,
After: curVal,
})
}
}
return changes, nil
}