-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire_command.go
133 lines (108 loc) · 2.57 KB
/
wire_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
package mongowire
import (
"bytes"
"errors"
"github.com/tychoish/birch"
)
func NewCommand(db, name string, args, metadata *birch.Document, inputs []birch.Document) Message {
return &CommandMessage{
header: MessageHeader{
OpCode: OP_COMMAND,
RequestID: 19,
},
DB: db,
CmdName: name,
CommandArgs: args,
Metadata: metadata,
InputDocs: inputs,
}
}
func (m *CommandMessage) HasResponse() bool { return true }
func (m *CommandMessage) Header() MessageHeader { return m.header }
func (m *CommandMessage) Scope() *OpScope {
return &OpScope{
Type: m.header.OpCode,
Context: m.DB,
Command: m.CmdName,
}
}
func (m *CommandMessage) Serialize() []byte {
size := 16 /* header */
size += len(m.DB) + 1
size += len(m.CmdName) + 1
size += getDocSize(m.CommandArgs)
size += getDocSize(m.Metadata)
for _, d := range m.InputDocs {
size += getDocSize(&d)
}
m.header.Size = int32(size)
buf := bytes.NewBuffer(make([]byte, 0, size))
m.header.WriteTo(buf)
writeCString(m.DB, buf)
writeCString(m.CmdName, buf)
m.CommandArgs.WriteTo(buf)
m.Metadata.WriteTo(buf)
for _, d := range m.InputDocs {
d.WriteTo(buf)
}
return buf.Bytes()
}
func (h *MessageHeader) parseCommandMessage(buf []byte) (Message, error) {
var err error
cmd := &CommandMessage{
header: *h,
}
cmd.DB, err = readCString(buf)
if err != nil {
return cmd, err
}
if len(buf) < len(cmd.DB)+1 {
return nil, errors.New("invalid command message -- message length is too short")
}
buf = buf[len(cmd.DB)+1:]
cmd.CmdName, err = readCString(buf)
if err != nil {
return nil, err
}
if len(buf) < len(cmd.CmdName)+1 {
return nil, errors.New("invalid command message -- message length is too short")
}
buf = buf[len(cmd.CmdName)+1:]
cmd.CommandArgs, err = birch.ReadDocument(buf)
if err != nil {
return nil, err
}
size, err := cmd.CommandArgs.Validate()
if err != nil {
return nil, err
}
if len(buf) < int(size) {
return cmd, errors.New("invalid command message -- message length is too short")
}
buf = buf[size:]
cmd.Metadata, err = birch.ReadDocument(buf)
if err != nil {
return nil, err
}
size, err = cmd.Metadata.Validate()
if err != nil {
return nil, err
}
if len(buf) < int(size) {
return cmd, errors.New("invalid command message -- message length is too short")
}
buf = buf[size:]
for len(buf) > 0 {
doc, err := birch.ReadDocument(buf)
if err != nil {
return nil, err
}
size, err = doc.Validate()
if err != nil {
return nil, err
}
buf = buf[size:]
cmd.InputDocs = append(cmd.InputDocs, *doc.Copy())
}
return cmd, nil
}