/
op_query.go
167 lines (135 loc) · 4.44 KB
/
op_query.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
// 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 (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"github.com/FerretDB/FerretDB/internal/bson"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/types/fjson"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
)
// OpQuery is used to query the database for documents in a collection.
type OpQuery struct {
Flags OpQueryFlags
FullCollectionName string
NumberToSkip int32
NumberToReturn int32
Query *types.Document
ReturnFieldsSelector *types.Document // may be nil
}
func (query *OpQuery) msgbody() {}
// readFrom composes an OpQuery from a buffered reader.
// It may return ValidationError if the document read from bufr is invalid.
func (query *OpQuery) readFrom(bufr *bufio.Reader) error {
if err := binary.Read(bufr, binary.LittleEndian, &query.Flags); err != nil {
return lazyerrors.Errorf("wire.OpQuery.ReadFrom (binary.Read): %w", err)
}
var coll bson.CString
if err := coll.ReadFrom(bufr); err != nil {
return err
}
query.FullCollectionName = string(coll)
if err := binary.Read(bufr, binary.LittleEndian, &query.NumberToSkip); err != nil {
return err
}
if err := binary.Read(bufr, binary.LittleEndian, &query.NumberToReturn); err != nil {
return err
}
var q bson.Document
if err := q.ReadFrom(bufr); err != nil {
return err
}
doc := must.NotFail(types.ConvertDocument(&q))
if err := validateValue(doc); err != nil {
return newValidationError(fmt.Errorf("wire.OpQuery.ReadFrom: validation failed for %v with: %v", doc, err))
}
query.Query = doc
if _, err := bufr.Peek(1); err == nil {
var r bson.Document
if err := r.ReadFrom(bufr); err != nil {
return err
}
tr := must.NotFail(types.ConvertDocument(&r))
query.ReturnFieldsSelector = tr
}
return nil
}
// UnmarshalBinary reads an OpQuery from a byte array.
func (query *OpQuery) UnmarshalBinary(b []byte) error {
br := bytes.NewReader(b)
bufr := bufio.NewReader(br)
if err := query.readFrom(bufr); err != nil {
return lazyerrors.Errorf("wire.OpQuery.UnmarshalBinary: %w", err)
}
if _, err := bufr.Peek(1); err != io.EOF {
return lazyerrors.Errorf("unexpected end of the OpQuery: %v", err)
}
return nil
}
// MarshalBinary writes an OpQuery to a byte array.
func (query *OpQuery) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
bufw := bufio.NewWriter(&buf)
if err := binary.Write(bufw, binary.LittleEndian, query.Flags); err != nil {
return nil, err
}
if err := bson.CString(query.FullCollectionName).WriteTo(bufw); err != nil {
return nil, err
}
if err := binary.Write(bufw, binary.LittleEndian, query.NumberToSkip); err != nil {
return nil, err
}
if err := binary.Write(bufw, binary.LittleEndian, query.NumberToReturn); err != nil {
return nil, err
}
if err := must.NotFail(bson.ConvertDocument(query.Query)).WriteTo(bufw); err != nil {
return nil, err
}
if query.ReturnFieldsSelector != nil {
if err := must.NotFail(bson.ConvertDocument(query.ReturnFieldsSelector)).WriteTo(bufw); err != nil {
return nil, err
}
}
if err := bufw.Flush(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// String returns a string representation for logging.
func (query *OpQuery) String() string {
if query == nil {
return "<nil>"
}
m := map[string]any{
"Flags": query.Flags,
"FullCollectionName": query.FullCollectionName,
"NumberToSkip": query.NumberToSkip,
"NumberToReturn": query.NumberToReturn,
"Query": json.RawMessage(must.NotFail(fjson.Marshal(query.Query))),
}
if query.ReturnFieldsSelector != nil {
m["ReturnFieldsSelector"] = json.RawMessage(must.NotFail(fjson.Marshal(query.ReturnFieldsSelector)))
}
return string(must.NotFail(json.MarshalIndent(m, "", " ")))
}
// check interfaces
var (
_ MsgBody = (*OpQuery)(nil)
)