-
Notifications
You must be signed in to change notification settings - Fork 54
/
headers.go
294 lines (254 loc) · 6.94 KB
/
headers.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
290
291
292
293
294
package protocol
import (
"fmt"
"math"
"github.com/SAP/go-hdb/driver/internal/protocol/encoding"
)
// Message header (size: 32 bytes).
type messageHeader struct {
sessionID int64
packetCount int32
varPartLength uint32
varPartSize uint32
noOfSegm int16
}
func (h *messageHeader) String() string {
return fmt.Sprintf("session id %d packetCount %d varPartLength %d, varPartSize %d noOfSegm %d",
h.sessionID,
h.packetCount,
h.varPartLength,
h.varPartSize,
h.noOfSegm)
}
func (h *messageHeader) encode(enc *encoding.Encoder) error {
enc.Int64(h.sessionID)
enc.Int32(h.packetCount)
enc.Uint32(h.varPartLength)
enc.Uint32(h.varPartSize)
enc.Int16(h.noOfSegm)
enc.Zeroes(10) // size: 32 bytes
return nil
}
func (h *messageHeader) decode(dec *encoding.Decoder) error {
h.sessionID = dec.Int64()
h.packetCount = dec.Int32()
h.varPartLength = dec.Uint32()
h.varPartSize = dec.Uint32()
h.noOfSegm = dec.Int16()
dec.Skip(10) // size: 32 bytes
return dec.Error()
}
const (
segmentHeaderSize = 24
)
type segmentKind int8
const (
skInvalid segmentKind = 0
skRequest segmentKind = 1
skReply segmentKind = 2
skError segmentKind = 5
)
type commandOptions int8
const (
coNil commandOptions = 0x00
coSelfetchOff commandOptions = 0x01
coScrollableCursorOn commandOptions = 0x02
coNoResultsetCloseNeeded commandOptions = 0x04
coHoldCursorOverCommtit commandOptions = 0x08
coExecuteLocally commandOptions = 0x10
)
var (
coList = []commandOptions{coNil, coSelfetchOff, coScrollableCursorOn, coNoResultsetCloseNeeded, coHoldCursorOverCommtit, coExecuteLocally}
coListText = []string{"", "selfetchOff", "scrollableCursorOn", "noResltsetCloseNeeded", "holdCursorOverCommit", "executLocally"}
)
func (k commandOptions) String() string {
var s []string
for i, option := range coList {
if (k & option) != 0 {
s = append(s, coListText[i])
}
}
return fmt.Sprintf("%v", s)
}
// segment header.
type segmentHeader struct {
segmentLength int32
segmentOfs int32
noOfParts int16
segmentNo int16
segmentKind segmentKind
messageType MessageType
commit bool
commandOptions commandOptions
functionCode FunctionCode
}
func (h *segmentHeader) String() string {
switch h.segmentKind {
default: // error
return fmt.Sprintf(
"segmentLength %d segmentOfs %d noOfParts %d, segmentNo %d segmentKind %s",
h.segmentLength,
h.segmentOfs,
h.noOfParts,
h.segmentNo,
h.segmentKind,
)
case skRequest:
return fmt.Sprintf(
"segmentLength %d segmentOfs %d noOfParts %d, segmentNo %d segmentKind %s messageType %s commit %t commandOptions %s",
h.segmentLength,
h.segmentOfs,
h.noOfParts,
h.segmentNo,
h.segmentKind,
h.messageType,
h.commit,
h.commandOptions,
)
case skReply:
return fmt.Sprintf(
"segmentLength %d segmentOfs %d noOfParts %d, segmentNo %d segmentKind %s functionCode %s",
h.segmentLength,
h.segmentOfs,
h.noOfParts,
h.segmentNo,
h.segmentKind,
h.functionCode,
)
}
}
// request.
func (h *segmentHeader) encode(enc *encoding.Encoder) error {
enc.Int32(h.segmentLength)
enc.Int32(h.segmentOfs)
enc.Int16(h.noOfParts)
enc.Int16(h.segmentNo)
enc.Int8(int8(h.segmentKind))
switch h.segmentKind {
default: // error
enc.Zeroes(11) // segmentHeaderLength
case skRequest:
enc.Int8(int8(h.messageType))
enc.Bool(h.commit)
enc.Int8(int8(h.commandOptions))
enc.Zeroes(8) // segmentHeaderSize
case skReply:
enc.Zeroes(1) // reserved
enc.Int16(int16(h.functionCode))
enc.Zeroes(8) // segmentHeaderSize
}
return nil
}
// reply || error.
func (h *segmentHeader) decode(dec *encoding.Decoder) error {
h.segmentLength = dec.Int32()
h.segmentOfs = dec.Int32()
h.noOfParts = dec.Int16()
h.segmentNo = dec.Int16()
h.segmentKind = segmentKind(dec.Int8())
switch h.segmentKind {
default: // error
dec.Skip(11) // segmentHeaderLength
case skRequest:
h.messageType = MessageType(dec.Int8())
h.commit = dec.Bool()
h.commandOptions = commandOptions(dec.Int8())
dec.Skip(8) // segmentHeaderLength
case skReply:
dec.Skip(1) // reserved
h.functionCode = FunctionCode(dec.Int16())
dec.Skip(8) // segmentHeaderLength
}
return dec.Error()
}
const (
partHeaderSize = 16
bigNumArgInd = -1
)
// MaxNumArg is the maximum number of arguments allowed to send in a part.
const MaxNumArg = math.MaxInt32
// PartAttributes represents the part attributes.
type PartAttributes int8
const (
paLastPacket PartAttributes = 0x01
paNextPacket PartAttributes = 0x02
paFirstPacket PartAttributes = 0x04
paRowNotFound PartAttributes = 0x08
paResultsetClosed PartAttributes = 0x10
)
var (
paList = [...]PartAttributes{paLastPacket, paNextPacket, paFirstPacket, paRowNotFound, paResultsetClosed}
paListText = [...]string{"lastPacket", "nextPacket", "firstPacket", "rowNotFound", "resultsetClosed"}
)
func (k PartAttributes) String() string {
var s []string
for i, attr := range paList {
if (k & attr) != 0 {
s = append(s, paListText[i])
}
}
return fmt.Sprintf("%v", s)
}
// ResultsetClosed returns true if the result set is closed, false otherwise.
func (k PartAttributes) ResultsetClosed() bool { return (k & paResultsetClosed) == paResultsetClosed }
// LastPacket returns true if the last packet is sent, false otherwise.
func (k PartAttributes) LastPacket() bool { return (k & paLastPacket) == paLastPacket }
// partHeader represents the part header.
type partHeader struct {
partKind PartKind
partAttributes PartAttributes
argumentCount int16
bigArgumentCount int32
bufferLength int32
bufferSize int32
}
func (h *partHeader) String() string {
return fmt.Sprintf("kind %s partAttributes %s argumentCount %d bigArgumentCount %d bufferLength %d bufferSize %d",
h.partKind,
h.partAttributes,
h.argumentCount,
h.bigArgumentCount,
h.bufferLength,
h.bufferSize,
)
}
func (h *partHeader) setNumArg(numArg int) error {
switch {
default:
return fmt.Errorf("maximum number of arguments %d exceeded", numArg)
case numArg <= math.MaxInt16:
h.argumentCount = int16(numArg)
h.bigArgumentCount = 0
case numArg <= math.MaxInt32:
h.argumentCount = bigNumArgInd
h.bigArgumentCount = int32(numArg)
}
return nil
}
func (h *partHeader) numArg() int {
if h.argumentCount == bigNumArgInd {
return int(h.bigArgumentCount)
}
return int(h.argumentCount)
}
func (h *partHeader) bufLen() int { return int(h.bufferLength) }
func (h *partHeader) encode(enc *encoding.Encoder) error {
enc.Int8(int8(h.partKind))
enc.Int8(int8(h.partAttributes))
enc.Int16(h.argumentCount)
enc.Int32(h.bigArgumentCount)
enc.Int32(h.bufferLength)
enc.Int32(h.bufferSize)
// no filler
return nil
}
func (h *partHeader) decode(dec *encoding.Decoder) error {
h.partKind = PartKind(dec.Int8())
h.partAttributes = PartAttributes(dec.Int8())
h.argumentCount = dec.Int16()
h.bigArgumentCount = dec.Int32()
h.bufferLength = dec.Int32()
h.bufferSize = dec.Int32()
// no filler
return dec.Error()
}