This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_command.go
226 lines (187 loc) · 6.13 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
// 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 (
"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 readCommandLike interface {
command
handleWriteKeyNotFoundError(ResultCode) error
}
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) readCommand {
return readCommand{
singleCommand: newSingleCommand(cluster, key),
binNames: binNames,
policy: policy,
}
}
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.cluster.getReadNode(&cmd.partition, cmd.policy.ReplicaPolicy, &cmd.replicaSequence)
}
func (cmd *readCommand) parseResult(ifc command, conn *Connection) error {
// Read header.
_, err := conn.Read(cmd.dataBuffer, int(_MSG_TOTAL_HEADER_SIZE))
if err != nil {
Logger.Warn("parse result error: " + 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 := ResultCode(cmd.dataBuffer[13] & 0xFF)
generation := Buffer.BytesToUint32(cmd.dataBuffer, 14)
expiration := 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); err != nil {
return err
}
_, err = conn.Read(cmd.dataBuffer, receiveSize)
if err != nil {
Logger.Warn("parse result error: " + err.Error())
return err
}
}
if resultCode != 0 {
if resultCode == KEY_NOT_FOUND_ERROR && cmd.object == nil {
if rcmd, ok := ifc.(readCommandLike); ok {
return rcmd.handleWriteKeyNotFoundError(resultCode)
}
return nil
}
if resultCode == UDF_BAD_RESPONSE {
cmd.record, _ = cmd.parseRecord(opCount, fieldCount, generation, expiration)
err := cmd.handleUdfError(resultCode)
Logger.Warn("UDF execution error: " + err.Error())
return err
}
return 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
}
cmd.record, err = cmd.parseRecord(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 ResultCode) error {
if ret, exists := cmd.record.Bins["FAILURE"]; exists {
return NewAerospikeError(resultCode, ret.(string))
}
return NewAerospikeError(resultCode)
}
func (cmd *readCommand) parseRecord(
opCount int,
fieldCount int,
generation uint32,
expiration uint32,
) (*Record, error) {
var bins BinMap
receiveOffset := 0
// 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.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.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 := int(opSize - (4 + nameSize))
value, _ := bytesToParticle(particleType, cmd.dataBuffer, receiveOffset, particleBytesSize)
receiveOffset += particleBytesSize
if bins == nil {
bins = make(BinMap, opCount)
}
// for operate list command results
if prev, exists := bins[name]; exists {
if res, ok := prev.([]interface{}); ok {
// List already exists. Add to it.
bins[name] = append(res, value)
} else {
// Make a list to store all values.
bins[name] = []interface{}{prev, value}
}
} else {
bins[name] = value
}
}
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)
}
func (cmd *readCommand) handleWriteKeyNotFoundError(resultCode ResultCode) error {
// command returns no error if key was not found
return nil
}