-
Notifications
You must be signed in to change notification settings - Fork 7
/
crdt.go
435 lines (399 loc) · 9.71 KB
/
crdt.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package riaken_core
import (
"errors"
"github.com/golang/protobuf/proto"
"github.com/riaken/riaken-core/rpb"
)
type CrdtCounter struct {
crdt *Crdt
amount int64 // pending value
Value int64 // current value
}
// Increment counter.
func (c *CrdtCounter) Increment(amount int64) {
c.amount = amount
}
// Decrement counter.
func (c *CrdtCounter) Decrement(amount int64) {
c.amount = -amount
}
// Commit changes to database.
func (c *CrdtCounter) Commit() (*rpb.DtUpdateResp, error) {
opts := &rpb.DtUpdateReq{
Op: &rpb.DtOp{
CounterOp: &rpb.CounterOp{
Increment: proto.Int64(c.amount),
},
},
ReturnBody: proto.Bool(true), // return so the latest value can be registered
}
res, err := c.crdt.Do(opts).Update()
c.Value = res.GetCounterValue()
return res, err
}
type CrdtSet struct {
crdt *Crdt
adds [][]byte
removes [][]byte
Values []string // current values
}
// set [][]byte values to []string.
func (s *CrdtSet) set(values [][]byte) {
for _, v := range values {
s.Values = append(s.Values, string(v))
}
}
// Add a new value to the set.
func (s *CrdtSet) Add(data string) {
s.adds = append(s.adds, []byte(data))
s.Values = append(s.Values, data)
}
// Remove a value from the set.
func (s *CrdtSet) Remove(data string) {
s.removes = append(s.removes, []byte(data))
for i, v := range s.Values {
if v == data {
s.Values = append(s.Values[:i], s.Values[i+1:]...)
break
}
}
}
// Commit changes to the database.
func (s *CrdtSet) Commit() (*rpb.DtUpdateResp, error) {
opts := &rpb.DtUpdateReq{
Op: &rpb.DtOp{
SetOp: &rpb.SetOp{
Adds: s.adds,
Removes: s.removes,
},
},
ReturnBody: proto.Bool(true), // return so the latest value can be registered
}
res, err := s.crdt.Do(opts).Update()
s.set(res.GetSetValue())
s.adds = nil
s.removes = nil
return res, err
}
type CrdtMapType byte
const (
CRDT_MAP_FLAG CrdtMapType = 1
CRDT_MAP_REGISTER CrdtMapType = 2
CRDT_MAP_COUNTER CrdtMapType = 3
CRDT_MAP_SET CrdtMapType = 4
CRDT_MAP_MAP CrdtMapType = 5
)
type CrdtMap struct {
crdt *Crdt
remove CrdtMapRemove
Flags map[string]bool
Registers map[string]string
Counters map[string]*CrdtCounter
Sets map[string]*CrdtSet
Maps map[string]*CrdtMap
}
type CrdtMapRemove struct {
Flags []string
Registers []string
Counters []string
Sets []string
Maps []string
}
// unpack data from database.
func (m *CrdtMap) unpack(dt *Crdt, mes []*rpb.MapEntry) {
m.crdt = dt
for _, me := range mes {
name := string(me.GetField().GetName())
switch me.GetField().GetType() {
case rpb.MapField_FLAG:
m.Flags[name] = me.GetFlagValue()
case rpb.MapField_REGISTER:
m.Registers[name] = string(me.GetRegisterValue())
case rpb.MapField_COUNTER:
m.Counters[name] = dt.NewCounter()
m.Counters[name].Value = me.GetCounterValue()
case rpb.MapField_SET:
m.Sets[name] = dt.NewSet()
m.Sets[name].set(me.GetSetValue())
case rpb.MapField_MAP:
m.Maps[name] = dt.NewMap()
m.Maps[name].unpack(dt, me.GetMapValue())
}
}
}
// removes returns the list of fields to remove from the Map.
func (m *CrdtMap) removes() []*rpb.MapField {
out := []*rpb.MapField{}
for _, v := range m.remove.Flags {
t := rpb.MapField_FLAG
r := &rpb.MapField{
Name: []byte(v),
Type: &t,
}
out = append(out, r)
}
for _, v := range m.remove.Registers {
t := rpb.MapField_REGISTER
r := &rpb.MapField{
Name: []byte(v),
Type: &t,
}
out = append(out, r)
}
for _, v := range m.remove.Counters {
t := rpb.MapField_COUNTER
r := &rpb.MapField{
Name: []byte(v),
Type: &t,
}
out = append(out, r)
}
for _, v := range m.remove.Sets {
t := rpb.MapField_SET
r := &rpb.MapField{
Name: []byte(v),
Type: &t,
}
out = append(out, r)
}
for _, v := range m.remove.Maps {
t := rpb.MapField_MAP
r := &rpb.MapField{
Name: []byte(v),
Type: &t,
}
out = append(out, r)
}
return out
}
// pack data to send to database.
func (m *CrdtMap) pack() []*rpb.MapUpdate {
out := []*rpb.MapUpdate{}
for k, v := range m.Flags {
t := rpb.MapField_FLAG
flag := &rpb.MapUpdate{
Field: &rpb.MapField{
Name: []byte(k),
Type: &t,
},
}
var o rpb.MapUpdate_FlagOp
if v {
o = rpb.MapUpdate_ENABLE
} else {
o = rpb.MapUpdate_DISABLE
}
flag.FlagOp = &o
out = append(out, flag)
}
for k, v := range m.Registers {
t := rpb.MapField_REGISTER
register := &rpb.MapUpdate{
Field: &rpb.MapField{
Name: []byte(k),
Type: &t,
},
RegisterOp: []byte(v),
}
out = append(out, register)
}
for k, v := range m.Counters {
t := rpb.MapField_COUNTER
counter := &rpb.MapUpdate{
Field: &rpb.MapField{
Name: []byte(k),
Type: &t,
},
CounterOp: &rpb.CounterOp{
Increment: proto.Int64(v.amount),
},
}
out = append(out, counter)
}
for k, v := range m.Sets {
t := rpb.MapField_SET
set := &rpb.MapUpdate{
Field: &rpb.MapField{
Name: []byte(k),
Type: &t,
},
SetOp: &rpb.SetOp{
Adds: v.adds,
Removes: v.removes,
},
}
out = append(out, set)
}
for k, v := range m.Maps {
t := rpb.MapField_MAP
mp := &rpb.MapUpdate{
Field: &rpb.MapField{
Name: []byte(k),
Type: &t,
},
MapOp: &rpb.MapOp{
Removes: v.removes(),
Updates: v.pack(),
},
}
out = append(out, mp)
}
return out
}
// Remove field with name and type t.
func (m *CrdtMap) Remove(t CrdtMapType, name string) {
switch t {
case CRDT_MAP_FLAG:
m.remove.Flags = append(m.remove.Flags, name)
delete(m.Flags, name)
case CRDT_MAP_REGISTER:
m.remove.Registers = append(m.remove.Registers, name)
delete(m.Registers, name)
case CRDT_MAP_COUNTER:
m.remove.Counters = append(m.remove.Counters, name)
delete(m.Counters, name)
case CRDT_MAP_SET:
m.remove.Sets = append(m.remove.Sets, name)
delete(m.Sets, name)
case CRDT_MAP_MAP:
m.remove.Maps = append(m.remove.Maps, name)
delete(m.Maps, name)
}
}
// Commit changes to the database.
func (m *CrdtMap) Commit() (*rpb.DtUpdateResp, error) {
opts := &rpb.DtUpdateReq{
Op: &rpb.DtOp{
MapOp: &rpb.MapOp{
Removes: m.removes(),
Updates: m.pack(),
},
},
ReturnBody: proto.Bool(true), // return so the latest value can be registered
}
res, err := m.crdt.Do(opts).Update()
m.unpack(m.crdt, res.GetMapValue())
m.remove = CrdtMapRemove{} // reset
return res, err
}
// Crdt can manually take raw opts via Do() and execute Fetch() or Update(),
// however, it is preferred to call the Counter(), Set(), or Map() methods
// and work with each object through those interfaces.
type Crdt struct {
bucket *Bucket // bucket this object is associated with
key string // key this object is associated with
context []byte // dt context
opts interface{} // rpb.Dt* options
Counter *CrdtCounter // counter result for this object
Set *CrdtSet // set result for this object
Map *CrdtMap // map result for this object
}
func (dt *Crdt) reset() {
dt.context = nil
dt.opts = nil
}
func (dt *Crdt) GetOpts() interface{} {
return dt.opts
}
// Do allows opts to be passed to a method. This call should be chained.
func (dt *Crdt) Do(opts interface{}) *Crdt {
dt.opts = opts
return dt
}
// NewCounter returns a new CRDT Counter.
func (dt *Crdt) NewCounter() *CrdtCounter {
return &CrdtCounter{
crdt: dt,
}
}
// NewSet returns a new CRDT Set.
func (dt *Crdt) NewSet() *CrdtSet {
return &CrdtSet{
crdt: dt,
}
}
// NewMap returns a new CRDT Map.
func (dt *Crdt) NewMap() *CrdtMap {
return &CrdtMap{
crdt: dt,
Flags: make(map[string]bool),
Registers: make(map[string]string),
Counters: make(map[string]*CrdtCounter),
Sets: make(map[string]*CrdtSet),
Maps: make(map[string]*CrdtMap),
}
}
func (dt *Crdt) processCounter(res int64) {
dt.Counter = dt.NewCounter()
dt.Counter.Value = res
}
func (dt *Crdt) processSet(res [][]byte) {
dt.Set = dt.NewSet()
dt.Set.set(res)
}
func (dt *Crdt) processMap(res []*rpb.MapEntry) {
dt.Map = dt.NewMap()
dt.Map.unpack(dt, res)
}
// Fetch returns the data for this object at key.
func (dt *Crdt) Fetch() (*rpb.DtFetchResp, error) {
defer dt.reset()
opts := new(rpb.DtFetchReq)
if dt.opts != nil {
if _, ok := dt.opts.(*rpb.DtFetchReq); !ok {
return nil, errors.New("Called Do() with wrong opts. Should be DtFetchReq")
} else {
opts = dt.opts.(*rpb.DtFetchReq)
}
}
opts.Bucket = []byte(dt.bucket.name)
opts.Key = []byte(dt.key)
if opts.Type == nil {
opts.Type = dt.bucket.btype
}
in, err := proto.Marshal(opts)
if err != nil {
return nil, err
}
out, err := dt.bucket.session.execute(Messages["DtFetchReq"], in)
if err != nil {
return nil, err
}
dt.context = out.(*rpb.DtFetchResp).Context
dt.processCounter(out.(*rpb.DtFetchResp).GetValue().GetCounterValue())
dt.processSet(out.(*rpb.DtFetchResp).GetValue().GetSetValue())
dt.processMap(out.(*rpb.DtFetchResp).GetValue().GetMapValue())
return out.(*rpb.DtFetchResp), nil
}
// Update adds or replaces data for this object.
func (dt *Crdt) Update() (*rpb.DtUpdateResp, error) {
defer dt.reset()
opts := new(rpb.DtUpdateReq)
if dt.opts != nil {
if _, ok := dt.opts.(*rpb.DtUpdateReq); !ok {
return nil, errors.New("Called Do() with wrong opts. Should be DtUpdateReq")
} else {
opts = dt.opts.(*rpb.DtUpdateReq)
}
}
opts.Bucket = []byte(dt.bucket.name)
opts.Key = []byte(dt.key)
if opts.Type == nil {
opts.Type = dt.bucket.btype
}
if opts.Context == nil {
opts.Context = dt.context
}
in, err := proto.Marshal(opts)
if err != nil {
return nil, err
}
out, err := dt.bucket.session.execute(Messages["DtUpdateReq"], in)
if err != nil {
return nil, err
}
dt.processCounter(out.(*rpb.DtUpdateResp).GetCounterValue())
dt.processSet(out.(*rpb.DtUpdateResp).GetSetValue())
dt.processMap(out.(*rpb.DtUpdateResp).GetMapValue())
return out.(*rpb.DtUpdateResp), nil
}