-
Notifications
You must be signed in to change notification settings - Fork 198
/
operation.go
194 lines (167 loc) · 5.35 KB
/
operation.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
// Copyright 2014-2022 Aerospike, Inc.
//
// 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 aerospike
import (
kvs "github.com/aerospike/aerospike-client-go/v7/proto/kvs"
)
// OperationType determines operation type
type OperationType struct {
op byte
isWrite bool
enumDist byte // make the values like enum to distinguish them from each other
}
type operationSubType *int
// Valid OperationType values that can be used to create custom Operations.
// The names are self-explanatory.
var (
_READ = OperationType{1, false, 0}
_READ_HEADER = OperationType{1, false, 1}
_WRITE = OperationType{2, true, 2}
_CDT_READ = OperationType{3, false, 3}
_CDT_MODIFY = OperationType{4, true, 4}
_MAP_READ = OperationType{3, false, 5}
_MAP_MODIFY = OperationType{4, true, 6}
_ADD = OperationType{5, true, 7}
_EXP_READ = OperationType{7, false, 8}
_EXP_MODIFY = OperationType{8, true, 9}
_APPEND = OperationType{9, true, 10}
_PREPEND = OperationType{10, true, 11}
_TOUCH = OperationType{11, true, 12}
_BIT_READ = OperationType{12, false, 13}
_BIT_MODIFY = OperationType{13, true, 14}
_DELETE = OperationType{14, true, 15}
_HLL_READ = OperationType{15, false, 16}
_HLL_MODIFY = OperationType{16, true, 17}
)
func (o *Operation) grpc_op_type() kvs.OperationType {
// case _READ: return kvs.OperationType_READ
switch o.opType {
case _READ:
return kvs.OperationType_READ
case _READ_HEADER:
return kvs.OperationType_READ_HEADER
case _WRITE:
return kvs.OperationType_WRITE
case _CDT_READ:
return kvs.OperationType_CDT_READ
case _CDT_MODIFY:
return kvs.OperationType_CDT_MODIFY
case _MAP_READ:
return kvs.OperationType_MAP_READ
case _MAP_MODIFY:
return kvs.OperationType_MAP_MODIFY
case _ADD:
return kvs.OperationType_ADD
case _EXP_READ:
return kvs.OperationType_EXP_READ
case _EXP_MODIFY:
return kvs.OperationType_EXP_MODIFY
case _APPEND:
return kvs.OperationType_APPEND
case _PREPEND:
return kvs.OperationType_PREPEND
case _TOUCH:
return kvs.OperationType_TOUCH
case _BIT_READ:
return kvs.OperationType_BIT_READ
case _BIT_MODIFY:
return kvs.OperationType_BIT_MODIFY
case _DELETE:
return kvs.OperationType_DELETE
case _HLL_READ:
return kvs.OperationType_HLL_READ
case _HLL_MODIFY:
return kvs.OperationType_HLL_MODIFY
}
panic(unreachable)
}
// Operation contains operation definition.
// This struct is used in client's operate() method.
type Operation struct {
// OpType determines type of operation.
opType OperationType
// used in CDT commands
opSubType operationSubType
// CDT context for nested types
ctx []*CDTContext
encoder func(*Operation, BufferEx) (int, Error)
// binName (Optional) determines the name of bin used in operation.
binName string
// binValue (Optional) determines bin value used in operation.
binValue Value
}
// size returns the size of the operation on the wire protocol.
func (op *Operation) size() (int, Error) {
size := len(op.binName)
// Simple case
if op.encoder == nil {
valueLength, err := op.binValue.EstimateSize()
if err != nil {
return -1, err
}
size += valueLength + 8
return size, nil
}
// Complex case, for CDTs
valueLength, err := op.encoder(op, nil)
if err != nil {
return -1, err
}
size += valueLength + 8
return size, nil
}
func (op *Operation) grpc() *kvs.Operation {
BinName := op.binName
return &kvs.Operation{
Type: op.grpc_op_type(),
BinName: &BinName,
Value: grpcValuePacked(op.binValue),
}
}
// GetBinOp creates read bin database operation.
func GetBinOp(binName string) *Operation {
return &Operation{opType: _READ, binName: binName, binValue: NewNullValue()}
}
// GetOp creates read all record bins database operation.
func GetOp() *Operation {
return &Operation{opType: _READ, binValue: NewNullValue()}
}
// GetHeaderOp creates read record header database operation.
func GetHeaderOp() *Operation {
return &Operation{opType: _READ_HEADER, binValue: NewNullValue()}
}
// PutOp creates set database operation.
func PutOp(bin *Bin) *Operation {
return &Operation{opType: _WRITE, binName: bin.Name, binValue: bin.Value}
}
// AppendOp creates string append database operation.
func AppendOp(bin *Bin) *Operation {
return &Operation{opType: _APPEND, binName: bin.Name, binValue: bin.Value}
}
// PrependOp creates string prepend database operation.
func PrependOp(bin *Bin) *Operation {
return &Operation{opType: _PREPEND, binName: bin.Name, binValue: bin.Value}
}
// AddOp creates integer add database operation.
func AddOp(bin *Bin) *Operation {
return &Operation{opType: _ADD, binName: bin.Name, binValue: bin.Value}
}
// TouchOp creates touch record database operation.
func TouchOp() *Operation {
return &Operation{opType: _TOUCH, binValue: NewNullValue()}
}
// DeleteOp creates delete record database operation.
func DeleteOp() *Operation {
return &Operation{opType: _DELETE, binValue: NewNullValue()}
}