forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opgetmore.go
120 lines (105 loc) · 3.99 KB
/
opgetmore.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
/*
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"
)
// MessageOpGetMore represents parsed OP_GET_MORE wire message.
//
// https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op_get_more
//
// struct {
// MsgHeader header; // standard message header
// int32 ZERO; // 0 - reserved for future use
// cstring fullCollectionName; // "dbname.collectionname"
// int32 numberToReturn; // number of documents to return
// int64 cursorID; // cursorID from the OP_REPLY
// }
//
// OP_GET_MORE is deprecated starting MongoDB 5.0 in favor of OP_MSG.
type MessageOpGetMore struct {
Header MessageHeader
Zero int32
FullCollectionName string
NumberToReturn int32
CursorID int64
// bytes is the full wire message bytes (incl. header) read from the connection.
bytes []byte
}
// GetHeader returns the wire message header.
func (m *MessageOpGetMore) GetHeader() MessageHeader {
return m.Header
}
// GetBytes returns the message raw bytes read from the connection.
func (m *MessageOpGetMore) GetBytes() []byte {
return m.bytes
}
// GetDatabase returns the command's database.
func (m *MessageOpGetMore) 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 *MessageOpGetMore) GetCommand() (string, error) {
return "getMore", nil
}
// String returns the message string representation.
func (m *MessageOpGetMore) String() string {
return fmt.Sprintf("OpGetMore(FullCollectionName=%v, NumberToReturn=%v, CursorID=%v)",
m.FullCollectionName, m.NumberToReturn, m.CursorID)
}
// MoreToCome is whether sender will send another message right after this one.
func (m *MessageOpGetMore) MoreToCome(_ Message) bool {
return false
}
// readOpGetMore converts OP_GET_MORE wire message bytes to a structured form.
func readOpGetMore(header MessageHeader, payload []byte) (*MessageOpGetMore, error) {
zero, rem, ok := readInt32(payload)
if !ok {
return nil, trace.BadParameter("malformed OP_GET_MORE: missing zero %v", payload)
}
fullCollectionName, rem, ok := readCString(rem)
if !ok {
return nil, trace.BadParameter("malformed OP_GET_MORE: missing full collection name %v", payload)
}
numberToReturn, rem, ok := readInt32(rem)
if !ok {
return nil, trace.BadParameter("malformed OP_GET_MORE: missing number to return %v", payload)
}
cursorID, _, ok := readInt64(rem)
if !ok {
return nil, trace.BadParameter("malformed OP_GET_MORE: missing cursor ID %v", payload)
}
return &MessageOpGetMore{
Header: header,
Zero: zero,
FullCollectionName: fullCollectionName,
NumberToReturn: numberToReturn,
CursorID: cursorID,
bytes: append(header.bytes[:], payload...),
}, nil
}
// ToWire converts this message to wire protocol message bytes.
func (m *MessageOpGetMore) ToWire(responseTo int32) (dst []byte) {
var idx int32
idx, dst = wiremessage.AppendHeaderStart(dst, m.Header.RequestID, responseTo, wiremessage.OpGetMore)
dst = appendInt32(dst, m.Zero)
dst = appendCString(dst, m.FullCollectionName)
dst = appendInt32(dst, m.NumberToReturn)
dst = appendInt64(dst, m.CursorID)
return bsoncore.UpdateLength(dst, idx, int32(len(dst[idx:])))
}