-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiff.go
247 lines (206 loc) · 5.46 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
package diff
import (
"errors"
"fmt"
"log"
"sync"
)
var (
Debug = false
ErrMustSupportKeysNotSeen = errors.New("referenceIndex must support KeysNotSeen")
ErrMustRecordValues = errors.New("referenceIndex must record values")
)
// Compares a store (currentValues) with a reference (referenceValues), streaming the reference.
//
// The referenceValues channel provide values in the reference store. It will be indexed.
//
// The currentValues channel provide values in the target store. It will be indexed.
//
// The changes channel will receive the changes, including Unchanged.
//
// See other diff implementations for faster and less memory consumming alternatives if
// you can provide better garanties from your stores.
func Diff(referenceValues, currentValues <-chan KeyValue, changes chan Change, cancel <-chan bool) error {
referenceIndex := NewIndex(true)
currentIndex := NewIndex(false)
wg := sync.WaitGroup{}
wg.Add(2)
var err1, err2 error
go func() {
defer wg.Done()
err1 = referenceIndex.Index(referenceValues, nil)
}()
go func() {
defer wg.Done()
err2 = currentIndex.Index(currentValues, nil)
}()
wg.Wait()
if err1 != nil {
return fmt.Errorf("error indexing reference values: %v", err1)
}
if err2 != nil {
return fmt.Errorf("error indexing current values: %v", err2)
}
return DiffIndexIndex(referenceIndex, currentIndex, changes, cancel)
}
// Compares a store (currentValues) with a reference (referenceValues), streaming the reference.
//
// The referenceValues channel provide values in the reference store. It MUST NOT produce duplicate keys.
//
// The currentValues channel provide values in the target store. It will be indexed.
//
// The changes channel will receive the changes, including Unchanged.
func DiffStreamReference(referenceValues, currentValues <-chan KeyValue, changes chan Change, cancel <-chan bool) error {
currentIndex := NewIndex(false)
if err := currentIndex.Index(currentValues, nil); err != nil {
return err
}
return DiffStreamIndex(referenceValues, currentIndex, changes, cancel)
}
// Compares a store (currentIndex) with a reference (referenceValues), streaming the reference.
//
// The referenceValues channel provide values in the reference store. It MUST NOT produce duplicate keys.
//
// The currentIndex is the indexed target store.
//
// The changes channel will receive the changes, including Unchanged.
func DiffStreamIndex(referenceValues <-chan KeyValue, currentIndex Index, changes chan Change, cancel <-chan bool) error {
if Debug {
log.Print("DiffStreamIndex: starting")
defer log.Print("DiffStreamIndex: finished")
}
l:
for {
var (
kv KeyValue
ok bool
)
select {
case <-cancel:
if Debug {
log.Print("DiffStreamIndex: cancelled")
}
return nil
case kv, ok = <-referenceValues:
if !ok {
if Debug {
log.Print("DiffStreamIndex: end of values")
}
break l
}
}
if Debug {
log.Print("DiffStreamIndex: new value")
}
cmp, err := currentIndex.Compare(kv)
if err != nil {
return err
}
switch cmp {
case MissingKey:
changes <- Change{
Type: Created,
Key: kv.Key,
Value: kv.Value,
}
case ModifiedKey:
changes <- Change{
Type: Modified,
Key: kv.Key,
Value: kv.Value,
}
case UnchangedKey:
changes <- Change{
Type: Unchanged,
Key: kv.Key,
}
}
}
keysNotSeen := currentIndex.KeysNotSeen()
if keysNotSeen == nil {
// not supported by the index
return nil
}
if Debug {
log.Print("DiffStreamIndex: deletion phase")
}
for key := range keysNotSeen {
changes <- Change{
Type: Deleted,
Key: key,
}
}
return nil
}
// Compares a store (currentValues) with a reference (referenceIndex), streaming the reference.
//
// The referenceIndex is the indexed target store. It MUST store the values AND support KeysNotSeen.
//
// The currentValues channel provide values in the reference store. It MUST NOT produce duplicate keys.
//
// The changes channel will receive the changes, including Unchanged.
func DiffIndexStream(referenceIndex Index, currentValues <-chan KeyValue, changes chan Change, cancel <-chan bool) error {
if !referenceIndex.DoesRecordValues() {
return ErrMustRecordValues
}
l:
for {
var (
kv KeyValue
ok bool
)
select {
case <-cancel:
return nil
case kv, ok = <-currentValues:
if !ok {
break l
}
}
cmp, err := referenceIndex.Compare(kv)
if err != nil {
return err
}
switch cmp {
case MissingKey:
changes <- Change{
Type: Deleted,
Key: kv.Key,
}
case ModifiedKey:
changes <- Change{
Type: Modified,
Key: kv.Key,
Value: kv.Value,
}
case UnchangedKey:
changes <- Change{
Type: Unchanged,
Key: kv.Key,
}
}
}
keysNotSeen := referenceIndex.KeysNotSeen()
if keysNotSeen == nil {
// not supported by the index
return ErrMustSupportKeysNotSeen
}
for key := range keysNotSeen {
changes <- Change{
Type: Created,
Key: key,
Value: referenceIndex.Value(key),
}
}
return nil
}
// Compares a store (currentValues) with a reference (referenceIndex), streaming the reference.
//
// The referenceIndex is the indexed target store. It MUST store the values.
//
// The currentIndex is the indexed target store.
//
// The changes channel will receive the changes, including Unchanged.
func DiffIndexIndex(referenceIndex Index, currentIndex Index, changes chan Change, cancel <-chan bool) error {
return DiffStreamIndex(referenceIndex.KeyValues(), currentIndex, changes, cancel)
}