/
command_put.go
89 lines (81 loc) · 2.12 KB
/
command_put.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
// Copyright JAMF Software, LLC
package fsm
import (
"github.com/jamf/regatta/regattapb"
)
type commandPut struct {
*regattapb.Command
}
func (c commandPut) handle(ctx *updateContext) (UpdateResult, *regattapb.CommandResult, error) {
resp, err := handlePut(ctx, ®attapb.RequestOp_Put{
Key: c.Kv.Key,
Value: c.Kv.Value,
PrevKv: c.PrevKvs,
})
if err != nil {
return ResultFailure, nil, err
}
return ResultSuccess, ®attapb.CommandResult{
Revision: ctx.index,
Responses: []*regattapb.ResponseOp{wrapResponseOp(resp)},
}, nil
}
func handlePut(ctx *updateContext, put *regattapb.RequestOp_Put) (*regattapb.ResponseOp_Put, error) {
resp := ®attapb.ResponseOp_Put{}
keyBuf := bufferPool.Get()
defer bufferPool.Put(keyBuf)
if err := encodeUserKey(keyBuf, put.Key); err != nil {
return nil, err
}
if put.PrevKv {
if err := ctx.EnsureIndexed(); err != nil {
return nil, err
}
rng, err := singleLookup(ctx.batch, ®attapb.RequestOp_Range{Key: put.Key})
if err != nil {
return nil, err
}
if len(rng.Kvs) == 1 {
resp.PrevKv = rng.Kvs[0]
}
}
if err := ctx.batch.Set(keyBuf.Bytes(), put.Value, nil); err != nil {
return nil, err
}
return resp, nil
}
type commandPutBatch struct {
*regattapb.Command
}
func (c commandPutBatch) handle(ctx *updateContext) (UpdateResult, *regattapb.CommandResult, error) {
req := make([]*regattapb.RequestOp_Put, len(c.Batch))
for i, kv := range c.Batch {
req[i] = ®attapb.RequestOp_Put{
Key: kv.Key,
Value: kv.Value,
}
}
rop, err := handlePutBatch(ctx, req)
if err != nil {
return ResultFailure, nil, err
}
res := make([]*regattapb.ResponseOp, 0, len(c.Batch))
for _, put := range rop {
res = append(res, wrapResponseOp(put))
}
return ResultSuccess, ®attapb.CommandResult{
Revision: ctx.index,
Responses: res,
}, nil
}
func handlePutBatch(ctx *updateContext, ops []*regattapb.RequestOp_Put) ([]*regattapb.ResponseOp_Put, error) {
results := make([]*regattapb.ResponseOp_Put, len(ops))
for i, op := range ops {
res, err := handlePut(ctx, op)
if err != nil {
return nil, err
}
results[i] = res
}
return results, nil
}