-
Notifications
You must be signed in to change notification settings - Fork 1
/
records.go
163 lines (135 loc) · 2.73 KB
/
records.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
package farm
import (
"sort"
"github.com/pkg/errors"
"github.com/SimonRichardson/coherence/pkg/selectors"
)
type changeSetRecords struct {
changeSet selectors.ChangeSet
set bool
err error
}
func (r *changeSetRecords) Add(v selectors.ChangeSet) {
if r.set && !r.changeSet.Equal(v) {
r.err = errors.New("variance detected from replication")
return
}
r.changeSet = v
r.set = true
}
func (r *changeSetRecords) Err() error {
return r.err
}
func (r *changeSetRecords) ChangeSet() selectors.ChangeSet {
return r.changeSet
}
type keysRecords struct {
keys []selectors.Key
set bool
err error
}
func (r *keysRecords) Add(v []selectors.Key) {
match := func(a, b []selectors.Key) bool {
if len(a) != len(b) {
return false
}
sort.Slice(a, func(i, j int) bool {
return a[i].String() < a[j].String()
})
sort.Slice(b, func(i, j int) bool {
return b[i].String() < b[j].String()
})
for k, v := range a {
if v != b[k] {
return false
}
}
return true
}
if r.set && !match(r.keys, v) {
r.err = errors.New("variance detected from replication")
return
}
r.keys = v
r.set = true
}
func (r *keysRecords) Err() error {
return r.err
}
func (r *keysRecords) Keys() []selectors.Key {
return r.keys
}
type fieldsRecords struct {
fields []selectors.Field
set bool
err error
}
func (r *fieldsRecords) Add(v []selectors.Field) {
match := func(a, b []selectors.Field) bool {
if len(a) != len(b) {
return false
}
sort.Slice(a, func(i, j int) bool {
return a[i].String() < a[j].String()
})
sort.Slice(b, func(i, j int) bool {
return b[i].String() < b[j].String()
})
for k, v := range a {
if v != b[k] {
return false
}
}
return true
}
if r.set && !match(r.fields, v) {
r.err = errors.New("variance detected from replication")
return
}
r.fields = v
r.set = true
}
func (r *fieldsRecords) Err() error {
return r.err
}
func (r *fieldsRecords) Fields() []selectors.Field {
return r.fields
}
type int64Records struct {
integer int64
set bool
err error
}
func (r *int64Records) Add(v int64) {
if r.set && r.integer != v {
r.err = errors.New("variance detected from replication")
return
}
r.integer = v
r.set = true
}
func (r *int64Records) Err() error {
return r.err
}
func (r *int64Records) Int64() int64 {
return r.integer
}
type presenceRecords struct {
presence selectors.Presence
set bool
err error
}
func (r *presenceRecords) Add(v selectors.Presence) {
if r.set && !r.presence.Equal(v) {
r.err = errors.New("variance detected from replication")
return
}
r.presence = v
r.set = true
}
func (r *presenceRecords) Err() error {
return r.err
}
func (r *presenceRecords) Presence() selectors.Presence {
return r.presence
}