-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathBSONDocumentIterator.swift
354 lines (307 loc) · 13.4 KB
/
BSONDocumentIterator.swift
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import Foundation
import NIO
/// :nodoc:
/// Iterator over a `BSONDocument`. This type is not meant to be used directly; please use `Sequence` protocol methods
/// instead.
public class BSONDocumentIterator: IteratorProtocol {
/// The buffer we are iterating over.
private var buffer: ByteBuffer
private var exhausted: Bool
internal init(over buffer: ByteBuffer) {
self.buffer = buffer
self.exhausted = false
// moves readerIndex to first key's type indicator
self.buffer.moveReaderIndex(to: 4)
}
internal convenience init(over doc: BSONDocument) {
self.init(over: doc.buffer)
}
/// Advances to the next element and returns it, or nil if no next element exists.
/// Returns nil if invalid BSON is encountered.
public func next() -> BSONDocument.KeyValuePair? {
// soft fail on read error by returning nil.
// this should only be possible if invalid BSON bytes were provided via
// BSONDocument.init(fromBSONWithoutValidatingElements:)
try? self.nextThrowing()
}
/**
* Advances to the next element and returns it, or nil if no next element exists.
* - Throws:
* - `InternalError` if the underlying buffer contains invalid BSON
*/
internal func nextThrowing() throws -> BSONDocument.KeyValuePair? {
guard let type = try self.readNextType() else {
return nil
}
let key = try self.buffer.readCString()
guard let bson = try BSON.allBSONTypes[type]?.read(from: &self.buffer) else {
throw BSONIterationError(message: "Encountered invalid BSON type: \(type)")
}
return (key: key, value: bson)
}
/// Get the next key in the iterator, if there is one.
/// This method should only be used for iterating through the keys. It advances to the beginning of the next
/// element, meaning the element associated with the last returned key cannot be accessed via this iterator.
/// Returns nil if invalid BSON is encountered.
private func nextKey() -> String? {
guard let type = try? self.readNextType(), let key = try? self.buffer.readCString() else {
return nil
}
guard self.skipNextValue(type: type) else {
return nil
}
return key
}
/// Assuming the buffer is currently positioned at the start of an element, returns the BSON type for the element.
/// Returns nil if the end of the document has been reached.
/// Throws an error if the byte does not correspond to a BSON type.
internal func readNextType() throws -> BSONType? {
guard !self.exhausted else {
return nil
}
guard let nextByte = self.buffer.readInteger(endianness: .little, as: UInt8.self) else {
throw BSONIterationError(
buffer: self.buffer,
message: "There are no readable bytes remaining, but a null terminator was not encountered"
)
}
guard nextByte != 0 else {
// if we are out of readable bytes, this is the null terminator
guard self.buffer.readableBytes == 0 else {
throw BSONIterationError(
buffer: self.buffer,
message: "Encountered invalid type indicator"
)
}
self.exhausted = true
return nil
}
guard let bsonType = BSONType(rawValue: nextByte) else {
throw BSONIterationError(
buffer: self.buffer,
message: "Encountered invalid BSON type indicator \(nextByte)"
)
}
return bsonType
}
/// Search for the value associated with the given key, returning its type if found and nil otherwise.
/// This moves the iterator right up to the first byte of the value.
/// Returns nil if invalid BSON is encountered.
internal func findValue(forKey key: String) -> BSONType? {
guard !self.exhausted else {
return nil
}
let keyUTF8 = key.utf8
while true {
var bsonType = BSONType.invalid
let matchResult = self.buffer.readWithUnsafeReadableBytes { buffer -> (Int, Bool?) in
var matched = true
var keyIter = keyUTF8.makeIterator()
for (i, byte) in buffer.enumerated() {
// first byte is type of element
guard i != 0 else {
guard let type = BSONType(rawValue: byte), type != .invalid else {
return (1, nil)
}
bsonType = type
continue
}
guard byte != 0 else {
// hit the null terminator
return (i + 1, matched && keyIter.next() == nil)
}
// if matched the key so far, check the next character
if matched {
guard let keyByte = keyIter.next() else {
matched = false
continue
}
matched = byte == keyByte
}
}
// unterminated C string, so we read the whole buffer
return (buffer.count, nil)
}
guard let matched = matchResult else {
// encountered invalid BSON, just return nil
return nil
}
guard matched else {
guard self.skipNextValue(type: bsonType) else {
return nil
}
continue
}
return bsonType
}
}
/// Finds an element with the specified key in the document. Returns nil if the key is not found.
/// Returns nil if invalid BSON is encountered when trying to find the key or read the value.
internal static func find(key: String, in document: BSONDocument) -> BSONDocument.KeyValuePair? {
let iter = document.makeIterator()
guard let bsonType = iter.findValue(forKey: key) else {
return nil
}
// the map contains a value for every valid BSON type.
// swiftlint:disable:next force_unwrapping
guard let bson = try? BSON.allBSONTypes[bsonType]!.read(from: &iter.buffer) else {
return nil
}
return (key: key, value: bson)
}
/// Move the reader index for the underlying buffer forward by the provided amount if possible.
/// Returns true if the index was moved successfully and false otherwise.
///
/// This will only fail if the underlying buffer contains invalid BSON.
private func moveReaderIndexSafely(forwardBy amount: Int) -> Bool {
guard amount > 0 && self.buffer.readerIndex + amount <= self.buffer.writerIndex else {
return false
}
self.buffer.moveReaderIndex(forwardBy: amount)
return true
}
/// Given the type of the encoded value starting at self.buffer.readerIndex, advances the reader index to the index
/// after the end of the element.
///
/// Returns false if invalid BSON is encountered while trying to skip, returns true otherwise.
internal func skipNextValue(type: BSONType) -> Bool {
switch type {
case .invalid:
return false
case .undefined, .null, .minKey, .maxKey:
// no data stored, nothing to skip.
break
case .bool:
return self.moveReaderIndexSafely(forwardBy: 1)
case .double, .int64, .timestamp, .datetime:
return self.moveReaderIndexSafely(forwardBy: 8)
case .objectID:
return self.moveReaderIndexSafely(forwardBy: 12)
case .int32:
return self.moveReaderIndexSafely(forwardBy: 4)
case .string, .code, .symbol:
guard let strLength = buffer.readInteger(endianness: .little, as: Int32.self) else {
return false
}
return self.moveReaderIndexSafely(forwardBy: Int(strLength))
case .regex:
do {
_ = try self.buffer.readCString()
_ = try self.buffer.readCString()
} catch {
return false
}
case .binary:
guard let dataLength = buffer.readInteger(endianness: .little, as: Int32.self) else {
return false
}
return self.moveReaderIndexSafely(forwardBy: Int(dataLength) + 1) // +1 for the binary subtype.
case .document, .array, .codeWithScope:
guard let embeddedDocLength = buffer.readInteger(endianness: .little, as: Int32.self) else {
return false
}
// -4 because the encoded length includes the bytes necessary to store the length itself.
return self.moveReaderIndexSafely(forwardBy: Int(embeddedDocLength) - 4)
case .dbPointer:
// initial string
guard let strLength = buffer.readInteger(endianness: .little, as: Int32.self) else {
return false
}
return self.moveReaderIndexSafely(forwardBy: Int(strLength) + 12)
case .decimal128:
return self.moveReaderIndexSafely(forwardBy: 16)
}
return true
}
/// Finds the key in the underlying buffer, and returns the [startIndex, endIndex) containing the corresponding
/// element.
/// Returns nil if invalid BSON is encountered.
internal static func findByteRange(for searchKey: String, in document: BSONDocument) -> Range<Int>? {
let iter = document.makeIterator()
guard let type = iter.findValue(forKey: searchKey) else {
return nil
}
// move back 1 for type byte, 1 for each byte in key, and 1 for null byte
let startIndex = iter.buffer.readerIndex - 1 - (searchKey.utf8.count + 1)
guard iter.skipNextValue(type: type) else {
return nil
}
let endIndex = iter.buffer.readerIndex
return startIndex..<endIndex
}
/// Retrieves an ordered list of the keys in the provided document buffer.
/// If invalid BSON is encountered while retrieving the keys, any valid keys seen up to that point are returned.
internal static func getKeys(from buffer: ByteBuffer) -> [String] {
let iter = BSONDocumentIterator(over: buffer)
var keys = [String]()
while let key = iter.nextKey() {
keys.append(key)
}
return keys
}
// uses an iterator to copy (key, value) pairs of the provided document from range [startIndex, endIndex) into a new
// document. starts at the startIndex-th pair and ends at the end of the document or the (endIndex-1)th index,
// whichever comes first.
// If invalid BSON is encountered before getting to the ith element, a new, empty document will be returned.
// If invalid BSON is encountered while iterating over elements included in the subsequence, a document containing
// the elements in the subsequence that came before the invalid BSON will be returned.
internal static func subsequence(
of doc: BSONDocument,
startIndex: Int = 0,
endIndex: Int = Int.max
) -> BSONDocument {
// TODO: SWIFT-911 Improve performance
guard endIndex >= startIndex else {
fatalError("endIndex must be >= startIndex")
}
let iter = BSONDocumentIterator(over: doc)
do {
for _ in 0..<startIndex {
guard let type = try iter.readNextType() else {
// we ran out of values
break
}
_ = try iter.buffer.readCString()
guard iter.skipNextValue(type: type) else {
break
}
}
} catch {
// if encountered invalid BSON before reaching the desired portion of the document, just
// return an empty document
return BSONDocument()
}
var newDoc = BSONDocument()
for _ in startIndex..<endIndex {
guard let next = iter.next() else {
// we ran out of values
break
}
newDoc.append(key: next.key, value: next.value)
}
return newDoc
}
}
extension BSONDocument {
// this is an alternative to the built-in `BSONDocument.filter` that returns an `[KeyValuePair]`. this variant is
// called by default, but the other is still accessible by explicitly stating return type:
// `let newDocPairs: [BSONDocument.KeyValuePair] = newDoc.filter { ... }`
/**
* Returns a new document containing the elements of the document that satisfy the given predicate.
*
* - Parameters:
* - isIncluded: A closure that takes a key-value pair as its argument and returns a `Bool` indicating whether
* the pair should be included in the returned document.
*
* - Returns: A document containing the key-value pairs that `isIncluded` allows.
*
* - Throws: An error if `isIncluded` throws an error.
*/
public func filter(_ isIncluded: (KeyValuePair) throws -> Bool) rethrows -> BSONDocument {
var elements: [BSONDocument.KeyValuePair] = []
for pair in self where try isIncluded(pair) {
elements.append(pair)
}
return BSONDocument(keyValuePairs: elements)
}
}