This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
forked from aerospike/aerospike-client-go
/
read_command.go
265 lines (223 loc) · 7.52 KB
/
read_command.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
// Copyright 2014-2021 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 (
"fmt"
"reflect"
"github.com/aerospike/aerospike-client-go/logger"
"github.com/aerospike/aerospike-client-go/types"
Buffer "github.com/aerospike/aerospike-client-go/utils/buffer"
)
type readCommand struct {
singleCommand
policy *BasePolicy
binNames []string
record *Record
// pointer to the object that's going to be unmarshalled
object *reflect.Value
replicaSequence int
}
// this method uses reflection.
// Will not be set if performance flag is passed for the build.
var objectParser func(
cmd *readCommand,
opCount int,
fieldCount int,
generation uint32,
expiration uint32,
) error
func newReadCommand(cluster *Cluster, policy *BasePolicy, key *Key, binNames []string, partition *Partition) (readCommand, error) {
var err error
if partition == nil {
partition, err = PartitionForRead(cluster, policy, key)
if err != nil {
return readCommand{}, err
}
}
return readCommand{
singleCommand: newSingleCommand(cluster, key, partition),
binNames: binNames,
policy: policy,
}, nil
}
func (cmd *readCommand) getPolicy(ifc command) Policy {
return cmd.policy
}
func (cmd *readCommand) writeBuffer(ifc command) error {
return cmd.setRead(cmd.policy, cmd.key, cmd.binNames)
}
func (cmd *readCommand) getNode(ifc command) (*Node, error) {
return cmd.partition.GetNodeRead(cmd.cluster)
}
func (cmd *readCommand) prepareRetry(ifc command, isTimeout bool) bool {
cmd.partition.PrepareRetryRead(isTimeout)
return true
}
func (cmd *readCommand) parseResult(ifc command, conn *Connection) error {
// Read proto and check if compressed
if _, err := conn.Read(cmd.dataBuffer, 8); err != nil {
logger.Logger.Debug("Connection error reading data for ReadCommand: %s", err.Error())
return err
}
if compressedSize := cmd.compressedSize(); compressedSize > 0 {
// Read compressed size
if _, err := conn.Read(cmd.dataBuffer, 8); err != nil {
logger.Logger.Debug("Connection error reading data for ReadCommand: %s", err.Error())
return err
}
if err := cmd.conn.initInflater(true, compressedSize); err != nil {
return types.NewAerospikeError(types.PARSE_ERROR, fmt.Sprintf("Error setting up zlib inflater for size `%d`: %s", compressedSize, err.Error()))
}
// Read header.
if _, err := conn.Read(cmd.dataBuffer, int(_MSG_TOTAL_HEADER_SIZE)); err != nil {
logger.Logger.Debug("Connection error reading data for ReadCommand: %s", err.Error())
return err
}
} else {
// Read header.
if _, err := conn.Read(cmd.dataBuffer[8:], int(_MSG_TOTAL_HEADER_SIZE)-8); err != nil {
logger.Logger.Debug("Connection error reading data for ReadCommand: %s", err.Error())
return err
}
}
// A number of these are commented out because we just don't care enough to read
// that section of the header. If we do care, uncomment and check!
sz := Buffer.BytesToInt64(cmd.dataBuffer, 0)
// Validate header to make sure we are at the beginning of a message
if err := cmd.validateHeader(sz); err != nil {
return err
}
headerLength := int(cmd.dataBuffer[8])
resultCode := types.ResultCode(cmd.dataBuffer[13] & 0xFF)
generation := Buffer.BytesToUint32(cmd.dataBuffer, 14)
expiration := types.TTL(Buffer.BytesToUint32(cmd.dataBuffer, 18))
fieldCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 26)) // almost certainly 0
opCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 28))
receiveSize := int((sz & 0xFFFFFFFFFFFF) - int64(headerLength))
// Read remaining message bytes.
if receiveSize > 0 {
if err := cmd.sizeBufferSz(receiveSize, false); err != nil {
return err
}
if _, err := conn.Read(cmd.dataBuffer, receiveSize); err != nil {
logger.Logger.Debug("Connection error reading data for ReadCommand: %s", err.Error())
return err
}
}
if resultCode != 0 {
if resultCode == types.KEY_NOT_FOUND_ERROR {
return types.ErrKeyNotFound
} else if resultCode == types.FILTERED_OUT {
return types.ErrFilteredOut
} else if resultCode == types.UDF_BAD_RESPONSE {
cmd.record, _ = cmd.parseRecord(ifc, opCount, fieldCount, generation, expiration)
err := cmd.handleUdfError(resultCode)
logger.Logger.Debug("UDF execution error: " + err.Error())
return err
}
return types.NewAerospikeError(resultCode)
}
if cmd.object == nil {
if opCount == 0 {
// data Bin was not returned
cmd.record = newRecord(cmd.node, cmd.key, nil, generation, expiration)
return nil
}
var err error
cmd.record, err = cmd.parseRecord(ifc, opCount, fieldCount, generation, expiration)
if err != nil {
return err
}
} else if objectParser != nil {
if err := objectParser(cmd, opCount, fieldCount, generation, expiration); err != nil {
return err
}
}
return nil
}
func (cmd *readCommand) handleUdfError(resultCode types.ResultCode) error {
if ret, exists := cmd.record.Bins["FAILURE"]; exists {
return types.NewAerospikeError(resultCode, ret.(string))
}
return types.NewAerospikeError(resultCode)
}
func (cmd *readCommand) parseRecord(
ifc command,
opCount int,
fieldCount int,
generation uint32,
expiration uint32,
) (*Record, error) {
var bins BinMap
receiveOffset := 0
type opList []interface{}
_, isOperate := ifc.(*operateCommand)
var binNamesSet []string
// There can be fields in the response (setname etc).
// But for now, ignore them. Expose them to the API if needed in the future.
//logger.Logger.Debug("field count: %d, databuffer: %v", fieldCount, cmd.dataBuffer)
if fieldCount > 0 {
// Just skip over all the fields
for i := 0; i < fieldCount; i++ {
//logger.Logger.Debug("%d", receiveOffset)
fieldSize := int(Buffer.BytesToUint32(cmd.dataBuffer, receiveOffset))
receiveOffset += (4 + fieldSize)
}
}
if opCount > 0 {
bins = make(BinMap, opCount)
}
for i := 0; i < opCount; i++ {
opSize := int(Buffer.BytesToUint32(cmd.dataBuffer, receiveOffset))
particleType := int(cmd.dataBuffer[receiveOffset+5])
nameSize := int(cmd.dataBuffer[receiveOffset+7])
name := string(cmd.dataBuffer[receiveOffset+8 : receiveOffset+8+nameSize])
receiveOffset += 4 + 4 + nameSize
particleBytesSize := opSize - (4 + nameSize)
value, _ := bytesToParticle(particleType, cmd.dataBuffer, receiveOffset, particleBytesSize)
receiveOffset += particleBytesSize
if bins == nil {
bins = make(BinMap, opCount)
}
if isOperate {
// for operate list command results
if prev, exists := bins[name]; exists {
if res, ok := prev.(opList); ok {
// List already exists. Add to it.
bins[name] = append(res, value)
} else {
// Make a list to store all values.
bins[name] = opList{prev, value}
binNamesSet = append(binNamesSet, name)
}
} else {
bins[name] = value
}
} else {
bins[name] = value
}
}
if isOperate {
for i := range binNamesSet {
bins[binNamesSet[i]] = []interface{}(bins[binNamesSet[i]].(opList))
}
}
return newRecord(cmd.node, cmd.key, bins, generation, expiration), nil
}
func (cmd *readCommand) GetRecord() *Record {
return cmd.record
}
func (cmd *readCommand) Execute() error {
return cmd.execute(cmd, true)
}