-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpair.go
More file actions
295 lines (265 loc) · 6.71 KB
/
pair.go
File metadata and controls
295 lines (265 loc) · 6.71 KB
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
/* ****************************************************************************
* Copyright 2020 51 Degrees Mobile Experts Limited (51degrees.com)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
* ***************************************************************************/
package swift
import (
"bytes"
"encoding/base64"
"fmt"
"strings"
"time"
)
const (
conflictInvalid = iota // Used to ensure the byte has been initialized
conflictOldest = iota
conflictNewest = iota
conflictAdd = iota
)
// An empty pair referenced in the resolveConflict method if both parameters are
// null.
var emptyValue pair
// Pair from a storage operation.
type Pair struct {
key string // The name of the key associated with the value
created time.Time // The UTC time that the value was created
expires time.Time // The UTC time that the value will expire
values [][]byte // The values as byte arrays
}
// pair used internally and adds more information for the operation.
type pair struct {
Pair
conflict byte // Flag for conflict resolution
cookieWriteTime time.Time // Last time the cookie was written to
}
// Key readonly accessor to the pair's key.
func (p *Pair) Key() string { return p.key }
// Created readonly accessor to the pair's created time.
func (p *Pair) Created() time.Time { return p.created }
// Expires readonly accessor to the pair's expiry time.
func (p *Pair) Expires() time.Time { return p.expires }
// Value readonly accessor to the pair's value.
func (p *Pair) Values() [][]byte { return p.values }
// Value returns the value as string. Used with HTML templates or JSON
// serialization.
func (p *Pair) Value() string {
var s = make([]string, len(p.values))
for i, v := range p.values {
s[i] = base64.StdEncoding.EncodeToString(v)
}
return strings.Join(s, "\r\n")
}
// Conflict returns conflict policy as a string. Used with HTML templates.
func (p *pair) Conflict() string {
switch p.conflict {
case conflictInvalid:
return "invalid"
case conflictNewest:
return "newest"
case conflictOldest:
return "oldest"
case conflictAdd:
return "add"
}
return ""
}
func (p *pair) setFromBuffer(b *bytes.Buffer) error {
var err error
p.key, err = readString(b)
if err != nil {
return err
}
p.conflict, err = readByte(b)
if err != nil {
return err
}
p.created, err = readTime(b)
if err != nil {
return err
}
p.expires, err = readDate(b)
if err != nil {
return err
}
p.values, err = readByteArrayArray(b)
if err != nil {
return err
}
return nil
}
func (p *pair) writeToBuffer(b *bytes.Buffer) error {
err := writeString(b, p.key)
if err != nil {
return err
}
err = writeByte(b, p.conflict)
if err != nil {
return err
}
err = writeTime(b, p.created)
if err != nil {
return err
}
err = writeDate(b, p.expires)
if err != nil {
return err
}
err = writeByteArrayArray(b, p.values)
if err != nil {
return err
}
return nil
}
func (p *pair) present() bool {
return p.created.IsZero() == false
}
func (p *pair) isValid() bool {
return p.expires.After(time.Now().UTC())
}
// isEmpty treats any pair without any values as empty. A pair with values, but
// those values are empty byte array is not considered any empty value.
func (p *pair) isEmpty() bool {
return p.values == nil || len(p.values) == 0
}
// equals returns true if the key and all values match exactly, otherwise false.
func (p *pair) equals(o *pair) bool {
// Check that the keys are equal.
if p.key != o.key {
return false
}
// Check that the number of values are the same.
if len(p.values) != len(o.values) {
return false
}
// Check that the values are all identical.
for i := 0; i < len(p.values); i++ {
if bytes.Equal(p.values[i], o.values[i]) == false {
return false
}
}
return true
}
// Performs a distinct merge of the values in the two pairs. Duplicates are
// removed.
func mergeValues(o *pair, c *pair) [][]byte {
// Make an array of values that has sufficient capacity to support all
// the values.
v := make([][]byte, 0, len(o.values)+len(c.values))
// Add the values from pair o. Assumes that o does not contain any
// duplicates.
for _, a := range o.values {
v = append(v, a)
}
// Add any values from pair c that are not in v.
for _, a := range c.values {
f := false
for _, b := range v {
if bytes.Equal(a, b) {
f = true
break
}
}
if f == false {
v = append(v, a)
}
}
return v
}
func mergePairs(o *pair, c *pair) *pair {
if valuesEqual(o.values, c.values) == false {
var n pair
n.conflict = conflictAdd
n.created = time.Now().UTC()
if o.expires.After(c.expires) {
n.expires = o.expires
} else {
n.expires = c.expires
}
n.key = o.key
n.values = mergeValues(o, c)
return &n
}
return c
}
func valuesEqual(a [][]byte, b [][]byte) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if bytes.Equal(a[i], b[i]) == false {
return false
}
}
return true
}
func resolveConflictOldest(o *pair, c *pair) *pair {
if o.created.Before(c.created) {
return o
}
if c.created.Before(o.created) {
return c
}
if o.cookieWriteTime.After(c.cookieWriteTime) {
return o
}
return c
}
func resolveConflictNewest(o *pair, c *pair) *pair {
if o.created.After(c.created) {
return o
}
if c.created.After(o.created) {
return c
}
if o.cookieWriteTime.After(c.cookieWriteTime) {
return o
}
return c
}
// Where there are two pairs for the same key determine which one should be used
// for the next operation in the storage operation.
// o is the pair from the storage operation
// c is the pair stored in a cookie for the current node
func resolveConflict(o *pair, c *pair) (*pair, error) {
var p *pair
if o == nil && c == nil {
// Neither has any information.
p = &emptyValue
} else if o != nil && c == nil {
// o is the only valid pair.
p = o
} else if o == nil && c != nil {
// c is the only valid pair.
p = c
} else {
// Resolve any conflict using o's conflict flag.
switch o.conflict {
case conflictInvalid:
return nil, fmt.Errorf("Conflict flag is not initialized")
case conflictNewest:
p = resolveConflictNewest(o, c)
break
case conflictOldest:
p = resolveConflictOldest(o, c)
break
case conflictAdd:
p = mergePairs(o, c)
break
default:
p = o
break
}
}
return p, nil
}