-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
291 lines (230 loc) · 6.42 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (C) MongoDB, Inc. 2017-present.
//
// 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
package birch
import (
"bytes"
"fmt"
"strconv"
"github.com/tychoish/birch/bsonerr"
"github.com/tychoish/birch/bsontype"
"github.com/tychoish/birch/elements"
"github.com/tychoish/fun"
"github.com/tychoish/fun/dt"
)
// Array represents an array in BSON. The methods of this type are more
// expensive than those on Document because they require potentially updating
// multiple keys to ensure the array stays valid at all times.
type Array struct {
doc *Document
}
// NewArray creates a new array with the specified value.
func NewArray(values ...*Value) *Array {
doc := DC.Make(len(values))
for _, v := range values {
doc.Append(&Element{value: v})
}
return &Array{doc: doc}
}
// MakeArray creates a new array with the size hint (capacity)
// specified.
func MakeArray(size int) *Array { return &Array{doc: DC.Make(size)} }
// Len returns the number of elements in the array.
func (a *Array) Len() int {
return len(a.doc.elems)
}
// Reset clears all elements from the array.
func (a *Array) Reset() {
a.doc.Reset()
}
// Validate ensures that the array's underlying BSON is valid. It returns the the number of bytes
// in the underlying BSON if it is valid or an error if it isn't.
func (a *Array) Validate() (uint32, error) {
size := uint32(4 + 1)
for i, elem := range a.doc.elems {
n, err := elem.value.validate(false)
if err != nil {
return 0, err
}
// type
size++
// key
size += uint32(len(strconv.Itoa(i))) + 1
// value
size += n
}
return size, nil
}
// LookupErr returns the value at the specified index, returning an
// OutOfBounds error if that element doesn't exist.
func (a *Array) Lookup(index uint) (*Value, error) {
v, ok := a.doc.ElementAtOK(index)
if !ok {
return nil, bsonerr.OutOfBounds
}
return v.value, nil
}
// LookupElement returns the element at the specified index,
// returning an OutOfBounds error if that element doesn't exist.
func (a *Array) LookupElemdent(index uint) (*Element, error) {
v, ok := a.doc.ElementAtOK(index)
if !ok {
return nil, bsonerr.OutOfBounds
}
return v, nil
}
func (a *Array) findElementForStrKey(key ...string) *Element {
if len(key) == 0 || a == nil {
return nil
}
idx, err := strconv.Atoi(key[0])
if err != nil {
return nil
}
val, err := a.Lookup(uint(idx))
if err != nil || val == nil {
return nil
}
if len(key) == 1 {
return EC.Value(key[0], val)
}
if sd, ok := val.MutableDocumentOK(); ok {
if el, err := sd.Search(key[1:]...); err == nil {
return el
}
}
if sar, ok := val.MutableArrayOK(); ok {
return sar.findElementForStrKey(key[1:]...)
}
return nil
}
func (a *Array) lookupTraverse(index uint, keys ...string) (*Value, error) {
value, err := a.Lookup(index)
if err != nil {
return nil, err
}
if len(keys) == 0 {
return value, nil
}
switch value.Type() {
case bsontype.EmbeddedDocument:
element, err := value.MutableDocument().Search(keys...)
if err != nil {
return nil, err
}
return element.Value(), nil
case bsontype.Array:
index, err := strconv.ParseUint(keys[0], 10, 0)
if err != nil {
return nil, bsonerr.InvalidArrayKey
}
val, err := value.MutableArray().lookupTraverse(uint(index), keys[1:]...)
if err != nil {
return nil, err
}
return val, nil
default:
return nil, bsonerr.InvalidDepthTraversal
}
}
// Append adds the given values to the end of the array. It returns a reference to itself.
func (a *Array) Append(values ...*Value) *Array {
a.doc.Append(elemsFromValues(values)...)
return a
}
// Set replaces the value at the given index with the parameter value. It panics if the index is
// out of bounds.
func (a *Array) Set(index uint, value *Value) *Array {
if index >= uint(len(a.doc.elems)) {
panic(bsonerr.OutOfBounds)
}
a.doc.elems[index] = &Element{value}
return a
}
// Extend adds the values from the second array to the first array,
// returning the original array for chaining.
func (a *Array) Extend(ar2 *Array) *Array { a.doc.Append(ar2.doc.elems...); return a }
// ExtendFromDocument adds the values from the elements in the
// document returning the array for chaining.
func (a *Array) ExtendFromDocument(doc *Document) *Array { a.doc.Append(doc.elems...); return a }
// Delete removes the value at the given index from the array.
func (a *Array) Delete(index uint) *Value {
if index >= uint(len(a.doc.elems)) {
return nil
}
elem := a.doc.elems[index]
a.doc.elems = append(a.doc.elems[:index], a.doc.elems[index+1:]...)
return elem.value
}
// String implements the fmt.Stringer interface.
func (a *Array) String() string {
bufbuf := dt.Sliceify(bufpool.Get())
defer bufpool.Put(bufbuf)
buf := bytes.NewBuffer(bufbuf)
buf.WriteString("bson.Array[")
for idx, elem := range a.doc.elems {
if idx > 0 {
buf.WriteString(", ")
}
fmt.Fprintf(buf, "%s", elem.value.Interface())
}
buf.WriteByte(']')
return buf.String()
}
// writeByteSlice handles serializing this array to a slice of bytes starting
// at the given start position.
func (a *Array) writeByteSlice(start uint, size uint32, b []byte) (int64, error) {
var total int64
pos := start
if len(b) < int(start)+int(size) {
return 0, errTooSmall
}
n, err := elements.Int32.Encode(start, b, int32(size))
total += int64(n)
pos += uint(n)
if err != nil {
return total, err
}
for i, elem := range a.doc.elems {
b[pos] = elem.value.data[elem.value.start]
total++
pos++
key := []byte(strconv.Itoa(i))
key = append(key, 0)
copy(b[pos:], key)
total += int64(len(key))
pos += uint(len(key))
n, err := elem.writeElement(false, pos, b)
total += n
pos += uint(n)
if err != nil {
return total, err
}
}
n, err = elements.Byte.Encode(pos, b, '\x00')
total += int64(n)
if err != nil {
return total, err
}
return total, nil
}
// MarshalBSON implements the Marshaler interface.
func (a *Array) MarshalBSON() ([]byte, error) {
size, err := a.Validate()
if err != nil {
return nil, err
}
b := dt.Sliceify(bufpool.Make())
b.Grow(int(size))
if _, err = a.writeByteSlice(0, size, b); err != nil {
return nil, err
}
return b, nil
}
// Iterator returns a ArrayIterator that can be used to iterate through the
// elements of this Array.
func (a *Array) Iterator() *fun.Iterator[*Value] {
return newArrayIterator(a)
}