/
op_msg.go
290 lines (249 loc) · 5.65 KB
/
op_msg.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
package message
import (
"encoding/json"
"fmt"
"github.com/annchain/BlockDB/common/bytes"
"github.com/globalsign/mgo/bson"
)
type MsgMessage struct {
Header *MessageHeader `json:"header"`
Flags msgFlags `json:"flags"`
Sections []section `json:"sections"`
CheckSum uint32 `json:"check_sum"`
}
func NewMsgMessage(header *MessageHeader, b []byte) (*MsgMessage, error) {
p := make([]byte, len(b))
copy(p, b)
pos := HeaderLen
// read flags
flags := newMsgFlags(p, pos)
pos += 4
// read sections
sectionsBytes := int(header.MessageSize) - pos
if flags.CheckSumPresent {
// reduce the length of checkSum
sectionsBytes -= 4
}
var secs []section
for sectionsBytes > 0 {
sec, offset, err := newSection(p, pos)
if err != nil {
return nil, err
}
secs = append(secs, sec)
pos += offset
sectionsBytes -= offset
}
// read check sum
checkSum := uint32(0)
if flags.CheckSumPresent {
checkSum = bytes.GetUInt32(p, pos)
}
mm := &MsgMessage{}
mm.Header = header
mm.Flags = flags
mm.Sections = secs
mm.CheckSum = checkSum
return mm, nil
}
func (mm *MsgMessage) ExtractBasic() (user, db, collection, op, docId string) {
for _, s := range mm.Sections {
switch sec := s.(type) {
case *sectionBody:
user, db, op, collection = mm.extractFromBody(sec)
case *sectionDocumentSequence:
docId = mm.extractFromSeq(sec)
default:
return
}
}
return
}
func (mm *MsgMessage) extractFromBody(secBody *sectionBody) (user, db, op, collection string) {
doc := secBody.Document
// user
if v, ok := doc["saslSupportedMechs"]; ok {
if user, ok = v.(string); !ok {
fmt.Println("saslSupportedMechs not string: ", doc)
}
}
// db
if v, ok := doc["$db"]; ok {
if db, ok = v.(string); !ok {
fmt.Println("$db not string: ", doc)
}
}
// op and collection
if v, ok := doc["update"]; ok {
op = "update"
collection, ok = v.(string)
if !ok {
fmt.Println("collection not string: ", doc)
}
} else if v, ok := doc["insert"]; ok {
op = "insert"
collection, ok = v.(string)
if !ok {
fmt.Println("collection not string: ", doc)
}
} else if v, ok := doc["query"]; ok {
op = "query"
collection, ok = v.(string)
if !ok {
fmt.Println("collection not string: ", doc)
}
} else if v, ok := doc["delete"]; ok {
op = "delete"
collection, ok = v.(string)
if !ok {
fmt.Println("collection not string: ", doc)
}
}
return
}
func (mm *MsgMessage) extractFromSeq(secSeq *sectionDocumentSequence) (docId string) {
docs := secSeq.Documents
if len(docs) < 1 {
return
}
var idI interface{}
var ok bool
for _, doc := range docs {
if idI, ok = doc["_id"]; ok {
break
}
v, ok := doc["q"]
if !ok {
continue
}
vb, ok := v.(bson.D)
if !ok {
continue
}
if idI, ok = (vb.Map())["_id"]; ok {
break
}
}
if idI == nil {
return
}
switch id := idI.(type) {
case bson.ObjectId:
return id.Hex()
case string:
return id
case int:
return string(id)
default:
return ""
}
}
type msgFlags struct {
CheckSumPresent bool `json:"check_sum"`
MoreToCome bool `json:"more_to_come"`
ExhaustAllowed bool `json:"exhaust_allowed"`
}
func newMsgFlags(b []byte, pos int) msgFlags {
flag := msgFlags{
CheckSumPresent: isFlagSetUInt32(b, pos, 0),
MoreToCome: isFlagSetUInt32(b, pos, 1),
ExhaustAllowed: isFlagSetUInt32(b, pos, 16),
}
return flag
}
func (mf *msgFlags) MarshalJSON() ([]byte, error) {
r := map[string]bool{}
if mf.CheckSumPresent {
r["check_sum"] = true
}
if mf.MoreToCome {
r["more_to_come"] = true
}
if mf.ExhaustAllowed {
r["exhaust_allowed"] = true
}
return json.Marshal(r)
}
type section interface {
kind() sectionType
}
func newSection(b []byte, pos int) (section, int, error) {
if len(b) < pos+1 {
return nil, 0, fmt.Errorf("document too small for section type")
}
sType := sectionType(b[pos])
pos++
switch sType {
case singleDocument:
doc, size, err := readDocument(b, pos)
if err != nil {
return nil, 0, err
}
s := §ionBody{
PayloadType: singleDocument,
Document: doc.Map(),
}
return s, 1 + int(size), nil
case documentSequence:
// read doc sequence size
if len(b) < 4 {
return nil, 0, fmt.Errorf("document too small for docSeq size")
}
size := bytes.GetUInt32(b, pos)
if len(b) < pos+int(size) {
return nil, 0, fmt.Errorf("document too small for docSeq")
}
pos += 4
// read identifier
identifier, idSize, err := readCString(b, pos)
if err != nil {
return nil, 0, fmt.Errorf("read cstring error: %v", err)
}
pos += idSize
// read documents
var docs []bson.M
bytesLeft := int(size) - 4 - idSize
for bytesLeft > 0 {
doc, docSize, err := readDocument(b, pos)
if err != nil {
return nil, 0, fmt.Errorf("read doc error: %v", err)
}
docs = append(docs, doc.Map())
bytesLeft -= docSize
}
s := §ionDocumentSequence{
PayloadType: documentSequence,
Size: size,
Identifier: identifier,
Documents: docs,
}
return s, 1 + int(size), nil
default:
return nil, 0, fmt.Errorf("unknown section type: %v", sType)
}
}
type sectionBody struct {
PayloadType sectionType `json:"type"`
Document bson.M `json:"document"`
}
func (s *sectionBody) kind() sectionType {
return s.PayloadType
}
type sectionDocumentSequence struct {
PayloadType sectionType `json:"type"`
Size uint32 `json:"size"`
Identifier string `json:"identifier"`
Documents []bson.M `json:"documents"`
}
func (s *sectionDocumentSequence) kind() sectionType {
return s.PayloadType
}
type sectionType byte
const (
singleDocument sectionType = iota
documentSequence
)
//func (m *MsgMessage) ParseCommand() []*processors.LogEvent {
//
// return nil
//}