forked from tolsen/mongonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire_query.go
98 lines (77 loc) · 1.9 KB
/
wire_query.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
package mongonet
func (m *QueryMessage) HasResponse() bool {
return true
}
func (m *QueryMessage) IsExhaust() bool {
return m.Flags&(1<<6) != 0
}
func (m *QueryMessage) Header() MessageHeader {
return m.header
}
func (m *QueryMessage) Serialize() []byte {
size := 16 /* header */ + 12 /* query header */
size += len(m.Namespace) + 1
size += int(m.Query.Size)
size += int(m.Project.Size)
m.header.Size = int32(size)
buf := make([]byte, size)
m.header.WriteInto(buf)
writeInt32(m.Flags, buf, 16)
loc := 20
writeCString(m.Namespace, buf, &loc)
writeInt32(m.Skip, buf, loc)
loc += 4
writeInt32(m.NReturn, buf, loc)
loc += 4
m.Query.Copy(&loc, buf)
m.Project.Copy(&loc, buf)
return buf
}
func parseQueryMessage(header MessageHeader, buf []byte) (Message, error) {
qm := &QueryMessage{}
qm.header = header
loc := 0
if len(buf) < 4 {
return qm, NewStackErrorf("invalid query message -- message must have length of at least 4 bytes.")
}
qm.Flags = readInt32(buf)
loc += 4
tmp, err := readCString(buf[loc:])
qm.Namespace = tmp
if err != nil {
return nil, err
}
loc += len(qm.Namespace) + 1
if len(buf) < loc+8 {
return qm, NewStackErrorf("invalid query message -- message length is too short.")
}
qm.Skip = readInt32(buf[loc:])
loc += 4
qm.NReturn = readInt32(buf[loc:])
loc += 4
qm.Query, err = parseSimpleBSON(buf[loc:])
if err != nil {
return nil, err
}
loc += int(qm.Query.Size)
if loc < len(buf) {
qm.Project, err = parseSimpleBSON(buf[loc:])
if err != nil {
return nil, err
}
loc += int(qm.Project.Size)
}
return qm, nil
}
func NewQueryMessage(ns string, flags int32, skip int32, toReturn int32, query SimpleBSON, project SimpleBSON) *QueryMessage {
qm := &QueryMessage{}
qm.header.RequestID = 17 // TODO
qm.header.OpCode = OP_QUERY
qm.Flags = flags
qm.Namespace = ns
qm.Skip = skip
qm.NReturn = toReturn
qm.Query = query
qm.Project = project
return qm
}