-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlww_e_set.go
212 lines (176 loc) · 3.54 KB
/
lww_e_set.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
package hob
import (
"encoding/json"
"errors"
"time"
)
var (
ErrInvalidBias = errors.New("invalid bias, must be a or r")
)
type Pair struct {
Add string `json:"add"`
Remove string `json:"remove"`
}
type LWWESet struct {
Type string `json:"type"`
Bias string `json:"bias"`
Data map[string]*Pair `json:"-"`
JSONData [][3]string `json:"e"`
}
func NewLWWESet(bias string) (lwwset *LWWESet, err error) {
if bias != "a" && bias != "r" {
err = ErrInvalidBias
}
data := make(map[string]*Pair)
lwwset = &LWWESet{
Type: "lww-e-set",
Bias: bias,
Data: data,
}
return
}
func (lwwset *LWWESet) Add(value string) (err error) {
data := lwwset.Data
if pair, ok := lwwset.Data[value]; ok {
pair.Add = Timestamp()
return
}
data[value] = &Pair{
Add: Timestamp(),
}
return
}
func (lwwset *LWWESet) Remove(value string) (err error) {
data := lwwset.Data
if pair, ok := lwwset.Data[value]; ok {
pair.Remove = Timestamp()
return
}
data[value] = &Pair{
Remove: Timestamp(),
}
return
}
func (lwwset *LWWESet) Test(value string) (is_member bool, err error) {
if pair, ok := lwwset.Data[value]; ok {
if remove := pair.Remove; remove != "" {
remove_time, err := time.Parse(time.RFC3339, pair.Remove)
if err != nil {
return false, err
}
add_time, err := time.Parse(time.RFC3339, pair.Add)
if err != nil {
return false, err
}
bias := lwwset.Bias
if bias == "a" {
if add_time.Before(remove_time) == false {
is_member = true
} else {
is_member = false
}
} else {
if add_time.After(remove_time) == true {
is_member = true
} else {
is_member = false
}
}
return is_member, err
}
is_member = true
} else {
is_member = false
}
return is_member, nil
}
func (lwwset *LWWESet) ToSet() (keys []string, err error) {
return
}
func (lwwset *LWWESet) Clone() (clone *LWWESet, err error) {
clone = &LWWESet{
Type: lwwset.Type,
Bias: lwwset.Bias,
Data: lwwset.Data,
}
return
}
func (lwwset *LWWESet) Merge(olwwset *LWWESet) (merged_set *LWWESet, err error) {
merged_set, err = lwwset.Clone()
if err != nil {
return nil, err
}
for k, v := range olwwset.Data {
if pair, ok := lwwset.Data[k]; ok {
merged, err := pair.merge(v)
if err != nil {
return nil, err
}
merged_set.Data[k] = merged
} else {
merged_set.Data[k] = v
}
}
return
}
func (lwwset *LWWESet) JSON() (json_bytes []byte, err error) {
for k, v := range lwwset.Data {
inner := [3]string{k, v.Add, v.Remove}
lwwset.JSONData = append(lwwset.JSONData, inner)
}
json_bytes, err = json.Marshal(lwwset)
return
}
func newPair(value string) (p *Pair) {
p = &Pair{
Add: "",
Remove: "",
}
return
}
func (p *Pair) merge(op *Pair) (merged *Pair, err error) {
merged = &Pair{
Add: "",
Remove: "",
}
add, err := time.Parse(time.RFC3339, p.Add)
if err != nil {
return nil, err
}
oadd, err := time.Parse(time.RFC3339, op.Add)
if err != nil {
return nil, err
}
if add.After(oadd) == true {
merged.Add = p.Add
} else {
merged.Add = op.Add
}
var remove, oremove time.Time
if p.Remove != "" {
remove, _ = time.Parse(time.RFC3339, p.Remove)
} else {
if op.Remove == "" {
return
} else {
merged.Remove = op.Remove
return
}
}
if op.Remove != "" {
oremove, _ = time.Parse(time.RFC3339, op.Remove)
} else {
if p.Remove == "" {
return
} else {
merged.Remove = p.Remove
return
}
}
if remove.After(oremove) != true {
merged.Remove = p.Remove
} else {
merged.Remove = op.Remove
}
return
}