forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdt_list.go
289 lines (244 loc) · 10.9 KB
/
cdt_list.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
// Copyright 2013-2017 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 (
. "github.com/aerospike/aerospike-client-go/types"
)
// List bin operations. Create list operations used by client.Operate command.
// List operations support negative indexing. If the index is negative, the
// resolved index starts backwards from end of list.
//
// Index/Range examples:
//
// Index 0: First item in list.
// Index 4: Fifth item in list.
// Index -1: Last item in list.
// Index -3: Third to last item in list.
// Index 1 Count 2: Second and third items in list.
// Index -3 Count 3: Last three items in list.
// Index -5 Count 4: Range between fifth to last item to second to last item inclusive.
//
// If an index is out of bounds, a parameter error will be returned. If a range is partially
// out of bounds, the valid part of the range will be returned.
const (
_CDT_LIST_APPEND = 1
_CDT_LIST_APPEND_ITEMS = 2
_CDT_LIST_INSERT = 3
_CDT_LIST_INSERT_ITEMS = 4
_CDT_LIST_POP = 5
_CDT_LIST_POP_RANGE = 6
_CDT_LIST_REMOVE = 7
_CDT_LIST_REMOVE_RANGE = 8
_CDT_LIST_SET = 9
_CDT_LIST_TRIM = 10
_CDT_LIST_CLEAR = 11
_CDT_LIST_SIZE = 16
_CDT_LIST_GET = 17
_CDT_LIST_GET_RANGE = 18
)
func packCDTParamsAsArray(packer BufferEx, opType int16, params ...Value) (int, error) {
size := 0
n, err := __PackShortRaw(packer, opType)
if err != nil {
return n, err
}
size += n
if len(params) > 0 {
if n, err = __PackArrayBegin(packer, len(params)); err != nil {
return size + n, err
}
size += n
for i := range params {
if n, err = params[i].pack(packer); err != nil {
return size + n, err
}
size += n
}
}
return size, nil
}
func packCDTIfcParamsAsArray(packer BufferEx, opType int16, params ListValue) (int, error) {
return packCDTIfcVarParamsAsArray(packer, opType, []interface{}(params)...)
}
func packCDTIfcVarParamsAsArray(packer BufferEx, opType int16, params ...interface{}) (int, error) {
size := 0
n, err := __PackShortRaw(packer, opType)
if err != nil {
return n, err
}
size += n
if len(params) > 0 {
if n, err = __PackArrayBegin(packer, len(params)); err != nil {
return size + n, err
}
size += n
for i := range params {
if n, err = __PackObject(packer, params[i], false); err != nil {
return size + n, err
}
size += n
}
}
return size, nil
}
func listAppendOpEncoder(op *Operation, packer BufferEx) (int, error) {
params := op.binValue.(ListValue)
if len(params) == 1 {
return packCDTIfcVarParamsAsArray(packer, _CDT_LIST_APPEND, params[0])
} else if len(params) > 1 {
return packCDTParamsAsArray(packer, _CDT_LIST_APPEND_ITEMS, params)
}
return -1, NewAerospikeError(PARAMETER_ERROR, "At least one value must be provided for ListAppendOp")
}
// ListAppendOp creates a list append operation.
// Server appends values to end of list bin.
// Server returns list size on bin name.
// It will panic is no values have been passed.
func ListAppendOp(binName string, values ...interface{}) *Operation {
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: ListValue(values), encoder: listAppendOpEncoder}
}
func listInsertOpEncoder(op *Operation, packer BufferEx) (int, error) {
args := op.binValue.(ValueArray)
params := args[1].(ListValue)
if len(params) == 1 {
return packCDTIfcVarParamsAsArray(packer, _CDT_LIST_INSERT, args[0], params[0])
} else if len(params) > 1 {
return packCDTParamsAsArray(packer, _CDT_LIST_INSERT_ITEMS, args[0], params)
}
return -1, NewAerospikeError(PARAMETER_ERROR, "At least one value must be provided for ListInsertOp")
}
// ListInsertOp creates a list insert operation.
// Server inserts value to specified index of list bin.
// Server returns list size on bin name.
// It will panic is no values have been passed.
func ListInsertOp(binName string, index int, values ...interface{}) *Operation {
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: ValueArray([]Value{IntegerValue(index), ListValue(values)}), encoder: listInsertOpEncoder}
}
func listPopOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_POP, op.binValue)
}
// ListPopOp creates list pop operation.
// Server returns item at specified index and removes item from list bin.
func ListPopOp(binName string, index int) *Operation {
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: IntegerValue(index), encoder: listPopOpEncoder}
}
func listPopRangeOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_POP_RANGE, op.binValue.(ValueArray)...)
}
// ListPopRangeOp creates a list pop range operation.
// Server returns items starting at specified index and removes items from list bin.
func ListPopRangeOp(binName string, index int, count int) *Operation {
if count == 1 {
return ListPopOp(binName, index)
}
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: ValueArray([]Value{IntegerValue(index), IntegerValue(count)}), encoder: listPopRangeOpEncoder}
}
func listPopRangeFromOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_POP_RANGE, op.binValue)
}
// ListPopRangeFromOp creates a list pop range operation.
// Server returns items starting at specified index to the end of list and removes items from list bin.
func ListPopRangeFromOp(binName string, index int) *Operation {
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: IntegerValue(index), encoder: listPopRangeFromOpEncoder}
}
func listRemoveOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_REMOVE, op.binValue)
}
// ListRemoveOp creates a list remove operation.
// Server removes item at specified index from list bin.
// Server returns number of items removed.
func ListRemoveOp(binName string, index int) *Operation {
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: IntegerValue(index), encoder: listRemoveOpEncoder}
}
func listRemoveRangeOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_REMOVE_RANGE, op.binValue.(ValueArray)...)
}
// ListRemoveRangeOp creates a list remove range operation.
// Server removes "count" items starting at specified index from list bin.
// Server returns number of items removed.
func ListRemoveRangeOp(binName string, index int, count int) *Operation {
if count == 1 {
return ListRemoveOp(binName, index)
}
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: ValueArray([]Value{IntegerValue(index), IntegerValue(count)}), encoder: listRemoveRangeOpEncoder}
}
func listRemoveRangeFromOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_REMOVE_RANGE, op.binValue)
}
// ListRemoveRangeFromOp creates a list remove range operation.
// Server removes all items starting at specified index to the end of list.
// Server returns number of items removed.
func ListRemoveRangeFromOp(binName string, index int) *Operation {
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: IntegerValue(index), encoder: listRemoveRangeFromOpEncoder}
}
func listSetOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTIfcParamsAsArray(packer, _CDT_LIST_SET, op.binValue.(ListValue))
}
// ListSetOp creates a list set operation.
// Server sets item value at specified index in list bin.
// Server does not return a result by default.
func ListSetOp(binName string, index int, value interface{}) *Operation {
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: ListValue([]interface{}{IntegerValue(index), value}), encoder: listSetOpEncoder}
}
func listTrimOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_TRIM, op.binValue.(ValueArray)...)
}
// ListTrimOp creates a list trim operation.
// Server removes "count" items in list bin that do not fall into range specified
// by index and count range. If the range is out of bounds, then all items will be removed.
// Server returns number of elemts that were removed.
func ListTrimOp(binName string, index int, count int) *Operation {
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: ValueArray([]Value{IntegerValue(index), IntegerValue(count)}), encoder: listTrimOpEncoder}
}
func listClearOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_CLEAR)
}
// ListClearOp creates a list clear operation.
// Server removes all items in list bin.
// Server does not return a result by default.
func ListClearOp(binName string) *Operation {
return &Operation{opType: CDT_MODIFY, binName: binName, binValue: NewNullValue(), encoder: listClearOpEncoder}
}
func listSizeOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_SIZE)
}
// ListSizeOp creates a list size operation.
// Server returns size of list on bin name.
func ListSizeOp(binName string) *Operation {
return &Operation{opType: CDT_READ, binName: binName, binValue: NewNullValue(), encoder: listSizeOpEncoder}
}
func listGetOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_GET, op.binValue)
}
// ListGetOp creates a list get operation.
// Server returns item at specified index in list bin.
func ListGetOp(binName string, index int) *Operation {
return &Operation{opType: CDT_READ, binName: binName, binValue: IntegerValue(index), encoder: listGetOpEncoder}
}
func listGetRangeOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_GET_RANGE, op.binValue.(ValueArray)...)
}
// ListGetRangeOp creates a list get range operation.
// Server returns "count" items starting at specified index in list bin.
func ListGetRangeOp(binName string, index int, count int) *Operation {
return &Operation{opType: CDT_READ, binName: binName, binValue: ValueArray([]Value{IntegerValue(index), IntegerValue(count)}), encoder: listGetRangeOpEncoder}
}
func listGetRangeFromOpEncoder(op *Operation, packer BufferEx) (int, error) {
return packCDTParamsAsArray(packer, _CDT_LIST_GET_RANGE, op.binValue)
}
// ListGetRangeFromOp creates a list get range operation.
// Server returns items starting at specified index to the end of list.
func ListGetRangeFromOp(binName string, index int) *Operation {
return &Operation{opType: CDT_READ, binName: binName, binValue: IntegerValue(index), encoder: listGetRangeFromOpEncoder}
}