/
array.go
262 lines (215 loc) · 6 KB
/
array.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// 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 types
import (
"fmt"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/FerretDB/FerretDB/internal/util/must"
)
// Array represents BSON array: an ordered collection of BSON values, accessed by 0-based indexes.
//
// Zero value is a valid empty array.
type Array struct {
s []any
frozen bool
}
// MakeArray creates an empty array with set capacity.
func MakeArray(capacity int) *Array {
if capacity == 0 {
return new(Array)
}
return &Array{s: make([]any, 0, capacity)}
}
// NewArray creates an array with the given values.
//
// It panics if any of the values is not a valid BSON type.
func NewArray(values ...any) (*Array, error) {
for _, v := range values {
assertType(v)
}
return &Array{s: values}, nil
}
func (a *Array) compositeType() {}
// Freeze prevents array from further modifications.
// Any methods that would modify the array will panic.
//
// It is safe to call Freeze multiple times.
func (a *Array) Freeze() {
if a != nil {
a.frozen = true
}
}
// checkFrozen panics if array is frozen.
func (a *Array) checkFrozen() {
if a.frozen {
panic("array is frozen and can't be modified")
}
}
// DeepCopy returns an unfrozen deep copy of this Array.
func (a *Array) DeepCopy() *Array {
if a == nil {
panic("types.Array.DeepCopy: nil array")
}
return deepCopy(a).(*Array)
}
// Len returns the number of values in the array.
//
// It returns 0 for nil Array.
func (a *Array) Len() int {
if a == nil {
return 0
}
return len(a.s)
}
// Iterator returns an iterator for the array.
func (a *Array) Iterator() iterator.Interface[int, any] {
return newArrayIterator(a)
}
// Get returns a value at the given index.
func (a *Array) Get(index int) (any, error) {
if l := a.Len(); index < 0 || index >= l {
return nil, fmt.Errorf("types.Array.Get: index %d is out of bounds [0-%d)", index, l)
}
return a.s[index], nil
}
// GetByPath returns a value by path.
func (a *Array) GetByPath(path Path) (any, error) {
return getByPath(a, path)
}
// Set sets the value at the given index.
//
// It panics if the value is not a valid BSON type.
func (a *Array) Set(index int, value any) error {
assertType(value)
a.checkFrozen()
if l := a.Len(); index < 0 || index >= l {
return fmt.Errorf("types.Array.Set: index %d is out of bounds [0-%d)", index, l)
}
a.s[index] = value
return nil
}
// Append appends given values to the array.
//
// It panics if any of the values is not a valid BSON type.
func (a *Array) Append(values ...any) {
for _, v := range values {
assertType(v)
}
a.checkFrozen()
if a.s == nil {
a.s = values
return
}
a.s = append(a.s, values...)
}
// RemoveByPath removes (cuts) value by path, doing nothing if path points to nothing.
func (a *Array) RemoveByPath(path Path) {
a.checkFrozen()
removeByPath(a, path)
}
// Min returns the minimum value from the array.
func (a *Array) Min() any {
if a == nil || a.Len() == 0 {
panic("cannot get Min value; array is nil or empty")
}
min := must.NotFail(a.Get(0))
for i := 1; i < a.Len(); i++ {
value := must.NotFail(a.Get(i))
if CompareOrder(min, value, Ascending) == Greater {
min = value
}
}
return min
}
// Max returns the maximum value from the array.
func (a *Array) Max() any {
if a == nil || a.Len() == 0 {
panic("cannot get Max value; array is nil or empty")
}
max := must.NotFail(a.Get(0))
for i := 1; i < a.Len(); i++ {
value := must.NotFail(a.Get(i))
if CompareOrder(max, value, Ascending) == Less {
max = value
}
}
return max
}
// FilterArrayByType returns a new array which contains
// only values of the same BSON type as ref.
// All numbers are treated as the same type.
func (a *Array) FilterArrayByType(ref any) *Array {
refType := detectDataType(ref)
arr := MakeArray(0)
for i := 0; i < a.Len(); i++ {
value := must.NotFail(a.Get(i))
vType := detectDataType(value)
if refType == vType {
arr.Append(value)
}
}
return arr
}
// Contains checks if the Array contains the given value.
//
// It panics if the filterValue is not a valid BSON type.
func (a *Array) Contains(filterValue any) bool {
assertType(filterValue)
switch filterValue := filterValue.(type) {
case *Document, *Array:
// filterValue is a composite type, so either a and filterValue must be equal
// or at least one element of a must be equal with filterValue.
if res := Compare(a, filterValue); res == Equal {
return true
}
for _, elem := range a.s {
if res := Compare(elem, filterValue); res == Equal {
return true
}
}
return false
default:
// filterValue is a scalar, so we compare it to each scalar element of a
for _, elem := range a.s {
switch elem := elem.(type) {
case *Document, *Array:
// we need elem and filterValue to be exactly equal, so we do nothing here
default:
if compareScalars(elem, filterValue) == Equal {
return true
}
}
}
return false
}
}
// ContainsAll checks if Array a contains all the given values of Array b.
// Currently, this algorithm is O(n^2) without any performance tuning.
// This place can be significantly improved if a more performant algorithm is chosen.
func (a *Array) ContainsAll(b *Array) bool {
for _, v := range b.s {
if !a.Contains(v) {
return false
}
}
return true
}
// Remove removes the value at the given index.
func (a *Array) Remove(index int) {
a.checkFrozen()
if l := a.Len(); index < 0 || index >= l {
panic("types.Array.Remove: index is out of bounds")
}
a.s = append(a.s[:index], a.s[index+1:]...)
}