forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opinsert.go
123 lines (108 loc) · 4.02 KB
/
opinsert.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
/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package protocol
import (
"fmt"
"strings"
"github.com/gravitational/trace"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage"
)
// MessageOpInsert represents parsed OP_INSERT wire message.
//
// https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op_insert
//
// struct {
// MsgHeader header; // standard message header
// int32 flags; // bit vector - see below
// cstring fullCollectionName; // "dbname.collectionname"
// document* documents; // one or more documents to insert into the collection
// }
//
// OP_INSERT is deprecated starting MongoDB 5.0 in favor of OP_MSG.
type MessageOpInsert struct {
Header MessageHeader
Flags int32
FullCollectionName string
Documents []bsoncore.Document
// bytes is the full wire message bytes (incl. header) read from the connection.
bytes []byte
}
// GetHeader returns the wire message header.
func (m *MessageOpInsert) GetHeader() MessageHeader {
return m.Header
}
// GetBytes returns the message raw bytes read from the connection.
func (m *MessageOpInsert) GetBytes() []byte {
return m.bytes
}
// GetDatabase returns the command's database.
func (m *MessageOpInsert) GetDatabase() (string, error) {
// Full collection name has "<db>.<collection>" format.
return strings.Split(m.FullCollectionName, ".")[0], nil
}
// GetCommand returns the message's command.
func (m *MessageOpInsert) GetCommand() (string, error) {
return "insert", nil
}
// String returns the message string representation.
func (m *MessageOpInsert) String() string {
documents := make([]string, 0, len(m.Documents))
for _, document := range m.Documents {
documents = append(documents, document.String())
}
return fmt.Sprintf("OpInsert(FullCollectionName=%v, Documents=%s, Flags=%v)",
m.FullCollectionName, documents, m.Flags)
}
// MoreToCome is whether sender will send another message right after this one.
func (m *MessageOpInsert) MoreToCome(_ Message) bool {
return true
}
// readOpInsert converts OP_INSERT wire message bytes to a structured form.
func readOpInsert(header MessageHeader, payload []byte) (*MessageOpInsert, error) {
flags, rem, ok := readInt32(payload)
if !ok {
return nil, trace.BadParameter("malformed OP_INSERT: missing flags %v", payload)
}
fullCollectionName, rem, ok := readCString(rem)
if !ok {
return nil, trace.BadParameter("malformed OP_INSERT: missing full collection name %v", payload)
}
var documents []bsoncore.Document
for len(rem) > 0 {
var document bsoncore.Document
document, rem, ok = bsoncore.ReadDocument(rem)
if !ok || len(document) == 0 {
return nil, trace.BadParameter("malformed OP_INSERT: missing document %v", payload)
}
documents = append(documents, document)
}
return &MessageOpInsert{
Header: header,
Flags: flags,
FullCollectionName: fullCollectionName,
Documents: documents,
bytes: append(header.bytes[:], payload...),
}, nil
}
// ToWire converts this message to wire protocol message bytes.
func (m *MessageOpInsert) ToWire(responseTo int32) (dst []byte) {
var idx int32
idx, dst = wiremessage.AppendHeaderStart(dst, m.Header.RequestID, responseTo, wiremessage.OpInsert)
dst = appendInt32(dst, m.Flags)
dst = appendCString(dst, m.FullCollectionName)
for _, document := range m.Documents {
dst = bsoncore.AppendDocument(dst, document)
}
return bsoncore.UpdateLength(dst, idx, int32(len(dst[idx:])))
}