-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire_get_more.go
81 lines (63 loc) · 1.52 KB
/
wire_get_more.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
package mongowire
import (
"bytes"
"errors"
)
func NewGetMore(ns string, number int32, cursorID int64) Message {
return &getMoreMessage{
header: MessageHeader{
RequestID: 19,
OpCode: OP_GET_MORE,
},
Namespace: ns,
NReturn: number,
CursorId: cursorID,
}
}
func (m *getMoreMessage) HasResponse() bool { return true }
func (m *getMoreMessage) Header() MessageHeader { return m.header }
func (m *getMoreMessage) Scope() *OpScope {
return &OpScope{
Type: m.header.OpCode,
Context: m.Namespace,
}
}
func (m *getMoreMessage) Serialize() []byte {
size := 16 /* header */ + 16 /* query header */
size += len(m.Namespace) + 1
m.header.Size = int32(size)
buf := bytes.NewBuffer(make([]byte, 0, size))
m.header.WriteTo(buf)
writeInt32(0, buf)
writeCString(m.Namespace, buf)
writeInt32(m.NReturn, buf)
writeInt64(m.CursorId, buf)
return buf.Bytes()
}
func (h *MessageHeader) parseGetMoreMessage(buf []byte) (Message, error) {
var (
err error
loc int
)
if len(buf) < 4 {
return nil, errors.New("invalid get more message -- message must have length of at least 4 bytes")
}
qm := &getMoreMessage{
header: *h,
}
qm.Reserved = readInt32(buf)
loc += 4
qm.Namespace, err = readCString(buf[loc:])
if err != nil {
return nil, err
}
loc += len(qm.Namespace) + 1
if len(buf) < loc+12 {
return nil, errors.New("invalid get more message -- message length is too short")
}
qm.NReturn = readInt32(buf[loc:])
loc += 4
qm.CursorId = readInt64(buf[loc:])
loc += 8 // nolint
return qm, nil
}