forked from FerretDB/FerretDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinct.go
148 lines (122 loc) · 3.86 KB
/
distinct.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
// 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 common
import (
"errors"
"fmt"
"strings"
"go.uber.org/zap"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/handlers/commonparams"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)
// DistinctParams contains `distinct` command parameters supported by at least one handler.
//
//nolint:vet // for readability
type DistinctParams struct {
DB string `ferretdb:"$db"`
Collection string `ferretdb:"collection"`
Key string `ferretdb:"key"`
Filter *types.Document `ferretdb:"-"`
Comment string `ferretdb:"comment,opt"`
Query any `ferretdb:"query,opt"`
Collation *types.Document `ferretdb:"collation,unimplemented"`
ReadConcern *types.Document `ferretdb:"readConcern,ignored"`
}
// GetDistinctParams returns `distinct` command parameters.
func GetDistinctParams(document *types.Document, l *zap.Logger) (*DistinctParams, error) {
var dp DistinctParams
err := commonparams.ExtractParams(document, "distinct", &dp, l)
if err != nil {
return nil, err
}
switch filter := dp.Query.(type) {
case *types.Document:
dp.Filter = filter
case types.NullType, nil:
dp.Filter = types.MakeDocument(0)
default:
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrTypeMismatch,
fmt.Sprintf(
"BSON field 'distinct.query' is the wrong type '%s', expected type 'object'",
commonparams.AliasFromType(dp.Query),
),
"distinct",
)
}
if dp.Key == "" {
return nil, commonerrors.NewCommandErrorMsg(
commonerrors.ErrEmptyFieldPath,
"FieldPath cannot be constructed with empty string",
)
}
return &dp, nil
}
// FilterDistinctValues returns distinct values from the given slice of documents with the given key.
//
// If the key is not found in the document, the document is ignored.
//
// If the key is found in the document, and the value is an array, each element of the array is added to the result.
// Otherwise, the value itself is added to the result.
func FilterDistinctValues(iter types.DocumentsIterator, key string) (*types.Array, error) {
distinct := types.MakeArray(0)
defer iter.Close()
for {
_, doc, err := iter.Next()
if errors.Is(err, iterator.ErrIteratorDone) {
break
}
if err != nil {
return nil, lazyerrors.Error(err)
}
// docsAtSuffix contains all documents exist at the suffix key.
docsAtSuffix := []*types.Document{doc}
suffix := key
if strings.ContainsRune(key, '.') {
path, err := types.NewPathFromString(key)
if err != nil {
return nil, lazyerrors.Error(err)
}
// Multiple documents may be found at suffix by array dot notation.
suffix, docsAtSuffix = getDocumentsAtSuffix(doc, path)
}
for _, doc := range docsAtSuffix {
val, err := doc.Get(suffix)
if err != nil {
continue
}
switch v := val.(type) {
case *types.Array:
for i := 0; i < v.Len(); i++ {
el, err := v.Get(i)
if err != nil {
return nil, lazyerrors.Error(err)
}
if !distinct.Contains(el) {
distinct.Append(el)
}
}
default:
if !distinct.Contains(v) {
distinct.Append(v)
}
}
}
}
SortArray(distinct, types.Ascending)
return distinct, nil
}