-
Notifications
You must be signed in to change notification settings - Fork 3
/
op_reply.go
182 lines (148 loc) · 4.53 KB
/
op_reply.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
// Copyright 2021 FerretDB 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 wire
import (
"encoding/binary"
"github.com/FerretDB/wire/internal/util/lazyerrors"
"github.com/FerretDB/wire/internal/util/must"
"github.com/FerretDB/wire/wirebson"
)
// OpReply is a deprecated response message type.
//
// Only up to one returned document is supported.
type OpReply struct {
// The order of fields is weird to make the struct smaller due to alignment.
// The wire order is: flags, cursor ID, starting from, documents.
document wirebson.RawDocument
CursorID int64
Flags OpReplyFlags
StartingFrom int32
}
// NewOpReply creates a new OpReply message.
func NewOpReply(doc wirebson.AnyDocument) (*OpReply, error) {
raw, err := doc.Encode()
if err != nil {
return nil, lazyerrors.Error(err)
}
return &OpReply{document: raw}, nil
}
func (reply *OpReply) msgbody() {}
// check implements [MsgBody].
func (reply *OpReply) check() error {
if d := reply.document; d != nil {
if _, err := d.DecodeDeep(); err != nil {
return lazyerrors.Error(err)
}
}
return nil
}
// UnmarshalBinaryNocopy implements [MsgBody].
func (reply *OpReply) UnmarshalBinaryNocopy(b []byte) error {
if len(b) < 20 {
return lazyerrors.Errorf("len=%d", len(b))
}
reply.Flags = OpReplyFlags(binary.LittleEndian.Uint32(b[0:4]))
reply.CursorID = int64(binary.LittleEndian.Uint64(b[4:12]))
reply.StartingFrom = int32(binary.LittleEndian.Uint32(b[12:16]))
numberReturned := int32(binary.LittleEndian.Uint32(b[16:20]))
reply.document = b[20:]
if numberReturned < 0 || numberReturned > 1 {
return lazyerrors.Errorf("numberReturned=%d", numberReturned)
}
if len(reply.document) == 0 {
reply.document = nil
}
if (numberReturned == 0) != (reply.document == nil) {
return lazyerrors.Errorf("numberReturned=%d, document=%v", numberReturned, reply.document)
}
if Debug {
if err := reply.check(); err != nil {
return lazyerrors.Error(err)
}
}
return nil
}
// MarshalBinary implements [MsgBody].
func (reply *OpReply) MarshalBinary() ([]byte, error) {
if Debug {
if err := reply.check(); err != nil {
return nil, lazyerrors.Error(err)
}
}
b := make([]byte, 20+len(reply.document))
binary.LittleEndian.PutUint32(b[0:4], uint32(reply.Flags))
binary.LittleEndian.PutUint64(b[4:12], uint64(reply.CursorID))
binary.LittleEndian.PutUint32(b[12:16], uint32(reply.StartingFrom))
if reply.document == nil {
binary.LittleEndian.PutUint32(b[16:20], uint32(0))
} else {
binary.LittleEndian.PutUint32(b[16:20], uint32(1))
copy(b[20:], reply.document)
}
return b, nil
}
// Document returns reply document.
func (reply *OpReply) Document() (*wirebson.Document, error) {
if reply.document == nil {
return nil, nil
}
return reply.document.Decode()
}
// RawDocument returns raw document.
func (reply *OpReply) RawDocument() wirebson.RawDocument {
return reply.document
}
// SetDocument sets reply document.
func (reply *OpReply) SetDocument(doc *wirebson.Document) {
var err error
reply.document, err = doc.Encode()
if err != nil {
panic(err)
}
}
// logMessage returns a string representation for logging.
func (reply *OpReply) logMessage(logFunc func(v any) string) string {
if reply == nil {
return "<nil>"
}
m := wirebson.MustDocument(
"ResponseFlags", reply.Flags.String(),
"CursorID", reply.CursorID,
"StartingFrom", reply.StartingFrom,
)
if reply.document == nil {
must.NoError(m.Add("NumberReturned", int32(0)))
} else {
must.NoError(m.Add("NumberReturned", int32(1)))
doc, err := reply.document.DecodeDeep()
if err == nil {
must.NoError(m.Add("Document", doc))
} else {
must.NoError(m.Add("DocumentError", err.Error()))
}
}
return logFunc(m)
}
// String returns an string representation for logging.
func (reply *OpReply) String() string {
return reply.logMessage(wirebson.LogMessage)
}
// StringIndent returns an indented string representation for logging.
func (reply *OpReply) StringIndent() string {
return reply.logMessage(wirebson.LogMessageIndent)
}
// check interfaces
var (
_ MsgBody = (*OpReply)(nil)
)