-
Notifications
You must be signed in to change notification settings - Fork 19
/
wire_insert.go
78 lines (57 loc) · 1.28 KB
/
wire_insert.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
package mongonet
func (m *InsertMessage) HasResponse() bool {
return false
}
func (m *InsertMessage) Header() MessageHeader {
return m.header
}
func (m *InsertMessage) Serialize() []byte {
size := 16 /* header */ + 4 /* update header */
size += len(m.Namespace) + 1
for _, d := range m.Docs {
size += int(d.Size)
}
m.header.Size = int32(size)
buf := make([]byte, size)
m.header.WriteInto(buf)
loc := 16
writeInt32(m.Flags, buf, loc)
loc += 4
writeCString(m.Namespace, buf, &loc)
for _, d := range m.Docs {
d.Copy(&loc, buf)
}
return buf
}
func parseInsertMessage(header MessageHeader, buf []byte) (Message, error) {
m := &InsertMessage{}
m.header = header
var err error
loc := 0
m.Flags = readInt32(buf[loc:])
loc += 4
m.Namespace, err = readCString(buf[loc:])
if err != nil {
return m, err
}
loc += len(m.Namespace) + 1
for loc < len(buf) {
doc, err := parseSimpleBSON(buf[loc:])
if err != nil {
return nil, err
}
m.Docs = append(m.Docs, doc)
loc += int(doc.Size)
}
return m, nil
}
func NewInsertMessage(namespace string, docs ...SimpleBSON) *InsertMessage {
im := &InsertMessage{}
im.header.RequestID = 17 // TODO
im.header.ResponseTo = 0
im.header.OpCode = OP_INSERT
im.Flags = 0
im.Namespace = namespace
im.Docs = docs
return im
}