-
Notifications
You must be signed in to change notification settings - Fork 44
/
map.go
148 lines (131 loc) · 3.62 KB
/
map.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
package diff
import (
"context"
logging "github.com/ipfs/go-log/v2"
typegen "github.com/whyrusleeping/cbor-gen"
"github.com/filecoin-project/go-hamt-ipld/v3"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lily/chain/actors/adt"
"github.com/filecoin-project/lily/chain/actors/adt/diff"
"github.com/filecoin-project/lily/tasks"
)
var log = logging.Logger("lily/actor/diff")
type MapModification struct {
Key []byte
Type tasks.ChangeType
Previous *typegen.Deferred
Current *typegen.Deferred
}
type MapModifications []*MapModification
func DiffMap(ctx context.Context, store adt.Store, child, parent adt.Map, childOpts, parentOpts *adt.MapOpts) (MapModifications, error) {
if !childOpts.Equal(parentOpts) {
diffContainer := &GenericMapDiff{
Added: []*MapModification{},
Modified: []*MapModification{},
Removed: []*MapModification{},
}
log.Warn("diffing array using slow comparison")
if err := diff.CompareMap(child, parent, diffContainer); err != nil {
return nil, err
}
return diffContainer.AsMapModifications()
}
changes, err := diff.Hamt(ctx, parent, child, store, store, hamt.UseHashFunction(hamt.HashFunc(childOpts.HashFunc)), hamt.UseTreeBitWidth(childOpts.Bitwidth))
if err != nil {
return nil, err
}
out := make(MapModifications, len(changes))
for i, change := range changes {
out[i] = &MapModification{
Key: []byte(change.Key),
Type: hamtChangeTypeToGeneric(change.Type),
Previous: change.Before,
Current: change.After,
}
}
return out, nil
}
func hamtChangeTypeToGeneric(c hamt.ChangeType) tasks.ChangeType {
switch c {
case hamt.Add:
return tasks.ChangeTypeAdd
case hamt.Remove:
return tasks.ChangeTypeRemove
case hamt.Modify:
return tasks.ChangeTypeModify
}
panic("developer error")
}
type GenericMapDiff struct {
Added MapModifications
Modified MapModifications
Removed MapModifications
}
func (t *GenericMapDiff) AsMapModifications() (MapModifications, error) {
out := make(MapModifications, len(t.Added)+len(t.Removed)+len(t.Modified))
idx := 0
for _, change := range t.Added {
out[idx] = &MapModification{
Key: change.Key,
Type: tasks.ChangeTypeAdd,
Previous: change.Previous,
Current: change.Current,
}
idx++
}
for _, change := range t.Removed {
out[idx] = &MapModification{
Key: change.Key,
Type: tasks.ChangeTypeRemove,
Previous: change.Previous,
Current: change.Current,
}
idx++
}
for _, change := range t.Modified {
out[idx] = &MapModification{
Key: change.Key,
Type: tasks.ChangeTypeModify,
Previous: change.Previous,
Current: change.Current,
}
idx++
}
return out, nil
}
var _ diff.MapDiffer = &GenericMapDiff{}
// An adt.Map key that just preserves the underlying string.
type StringKey string
func (k StringKey) Key() string {
return string(k)
}
func (t *GenericMapDiff) AsKey(key string) (abi.Keyer, error) {
return StringKey(key), nil
}
func (t *GenericMapDiff) Add(key string, val *typegen.Deferred) error {
t.Added = append(t.Added, &MapModification{
Key: []byte(key),
Type: tasks.ChangeTypeAdd,
Previous: nil,
Current: val,
})
return nil
}
func (t *GenericMapDiff) Modify(key string, from, to *typegen.Deferred) error {
t.Modified = append(t.Added, &MapModification{
Key: []byte(key),
Type: tasks.ChangeTypeModify,
Previous: from,
Current: to,
})
return nil
}
func (t *GenericMapDiff) Remove(key string, val *typegen.Deferred) error {
t.Removed = append(t.Added, &MapModification{
Key: []byte(key),
Type: tasks.ChangeTypeRemove,
Previous: val,
Current: nil,
})
return nil
}