-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathBsonDocumentReader.cs
562 lines (510 loc) · 19.4 KB
/
BsonDocumentReader.cs
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* Copyright 2010-present MongoDB 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.
*/
using System;
using System.IO;
namespace MongoDB.Bson.IO
{
/// <summary>
/// Represents a BSON reader for a BsonDocument.
/// </summary>
public class BsonDocumentReader : BsonReader
{
// private fields
private BsonDocumentReaderContext _context;
private BsonValue _currentValue;
// constructors
/// <summary>
/// Initializes a new instance of the BsonDocumentReader class.
/// </summary>
/// <param name="document">A BsonDocument.</param>
public BsonDocumentReader(BsonDocument document)
: this(document, BsonDocumentReaderSettings.Defaults)
{
}
/// <summary>
/// Initializes a new instance of the BsonDocumentReader class.
/// </summary>
/// <param name="document">A BsonDocument.</param>
/// <param name="settings">The reader settings.</param>
public BsonDocumentReader(BsonDocument document, BsonDocumentReaderSettings settings)
: base(settings)
{
if (document == null)
{
throw new ArgumentNullException("document");
}
_context = new BsonDocumentReaderContext(null, ContextType.TopLevel, document);
_currentValue = document;
}
// public methods
/// <summary>
/// Closes the reader.
/// </summary>
public override void Close()
{
// Close can be called on Disposed objects
State = BsonReaderState.Closed;
}
/// <summary>
/// Gets a bookmark to the reader's current position and state.
/// </summary>
/// <returns>A bookmark.</returns>
public override BsonReaderBookmark GetBookmark()
{
return new BsonDocumentReaderBookmark(State, CurrentBsonType, CurrentName, _context, _currentValue);
}
/// <summary>
/// Determines whether this reader is at end of file.
/// </summary>
/// <returns>
/// Whether this reader is at end of file.
/// </returns>
public override bool IsAtEndOfFile()
{
return State == BsonReaderState.Done;
}
/// <summary>
/// Reads BSON binary data from the reader.
/// </summary>
/// <returns>A BsonBinaryData.</returns>
public override BsonBinaryData ReadBinaryData()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadBinaryData", BsonType.Binary);
State = GetNextState();
return _currentValue.AsBsonBinaryData;
}
/// <summary>
/// Reads a BSON boolean from the reader.
/// </summary>
/// <returns>A Boolean.</returns>
public override bool ReadBoolean()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadBoolean", BsonType.Boolean);
State = GetNextState();
return _currentValue.AsBoolean;
}
/// <summary>
/// Reads a BsonType from the reader.
/// </summary>
/// <returns>A BsonType.</returns>
public override BsonType ReadBsonType()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (State == BsonReaderState.Initial || State == BsonReaderState.ScopeDocument)
{
// there is an implied type of Document for the top level and for scope documents
CurrentBsonType = BsonType.Document;
State = BsonReaderState.Value;
return CurrentBsonType;
}
if (State != BsonReaderState.Type)
{
ThrowInvalidState("ReadBsonType", BsonReaderState.Type);
}
switch (_context.ContextType)
{
case ContextType.Array:
if (!_context.TryGetNextValue(out _currentValue))
{
State = BsonReaderState.EndOfArray;
return BsonType.EndOfDocument;
}
State = BsonReaderState.Value;
break;
case ContextType.Document:
BsonElement currentElement;
if (!_context.TryGetNextElement(out currentElement))
{
State = BsonReaderState.EndOfDocument;
return BsonType.EndOfDocument;
}
CurrentName = currentElement.Name;
_currentValue = currentElement.Value;
State = BsonReaderState.Name;
break;
default:
throw new BsonInternalException("Invalid ContextType.");
}
CurrentBsonType = _currentValue.BsonType;
return CurrentBsonType;
}
/// <summary>
/// Reads BSON binary data from the reader.
/// </summary>
/// <returns>A byte array.</returns>
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
public override byte[] ReadBytes()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadBytes", BsonType.Binary);
State = GetNextState();
var binaryData = _currentValue.AsBsonBinaryData;
var subType = binaryData.SubType;
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary)
{
var message = string.Format("ReadBytes requires the binary sub type to be Binary, not {0}.", subType);
throw new FormatException(message);
}
return binaryData.Bytes;
}
#pragma warning restore 618
/// <summary>
/// Reads a BSON DateTime from the reader.
/// </summary>
/// <returns>The number of milliseconds since the Unix epoch.</returns>
public override long ReadDateTime()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadDateTime", BsonType.DateTime);
State = GetNextState();
return _currentValue.AsBsonDateTime.MillisecondsSinceEpoch;
}
/// <inheritdoc />
public override Decimal128 ReadDecimal128()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType(nameof(ReadDecimal128), BsonType.Decimal128);
State = GetNextState();
return _currentValue.AsDecimal128;
}
/// <summary>
/// Reads a BSON Double from the reader.
/// </summary>
/// <returns>A Double.</returns>
public override double ReadDouble()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadDouble", BsonType.Double);
State = GetNextState();
return _currentValue.AsDouble;
}
/// <summary>
/// Reads the end of a BSON array from the reader.
/// </summary>
public override void ReadEndArray()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (_context.ContextType != ContextType.Array)
{
ThrowInvalidContextType("ReadEndArray", _context.ContextType, ContextType.Array);
}
if (State == BsonReaderState.Type)
{
ReadBsonType(); // will set state to EndOfArray if at end of array
}
if (State != BsonReaderState.EndOfArray)
{
ThrowInvalidState("ReadEndArray", BsonReaderState.EndOfArray);
}
_context = _context.PopContext();
switch (_context.ContextType)
{
case ContextType.Array: State = BsonReaderState.Type; break;
case ContextType.Document: State = BsonReaderState.Type; break;
case ContextType.TopLevel: State = BsonReaderState.Done; break;
default: throw new BsonInternalException("Unexpected ContextType.");
}
}
/// <summary>
/// Reads the end of a BSON document from the reader.
/// </summary>
public override void ReadEndDocument()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (_context.ContextType != ContextType.Document && _context.ContextType != ContextType.ScopeDocument)
{
ThrowInvalidContextType("ReadEndDocument", _context.ContextType, ContextType.Document, ContextType.ScopeDocument);
}
if (State == BsonReaderState.Type)
{
ReadBsonType(); // will set state to EndOfDocument if at end of document
}
if (State != BsonReaderState.EndOfDocument)
{
ThrowInvalidState("ReadEndDocument", BsonReaderState.EndOfDocument);
}
_context = _context.PopContext();
switch (_context.ContextType)
{
case ContextType.Array: State = BsonReaderState.Type; break;
case ContextType.Document: State = BsonReaderState.Type; break;
case ContextType.TopLevel: State = BsonReaderState.Done; break;
default: throw new BsonInternalException("Unexpected ContextType.");
}
}
/// <summary>
/// Reads a BSON Int32 from the reader.
/// </summary>
/// <returns>An Int32.</returns>
public override int ReadInt32()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadInt32", BsonType.Int32);
State = GetNextState();
return _currentValue.AsInt32;
}
/// <summary>
/// Reads a BSON Int64 from the reader.
/// </summary>
/// <returns>An Int64.</returns>
public override long ReadInt64()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadInt64", BsonType.Int64);
State = GetNextState();
return _currentValue.AsInt64;
}
/// <summary>
/// Reads a BSON JavaScript from the reader.
/// </summary>
/// <returns>A string.</returns>
public override string ReadJavaScript()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadJavaScript", BsonType.JavaScript);
State = GetNextState();
return _currentValue.AsBsonJavaScript.Code;
}
/// <summary>
/// Reads a BSON JavaScript with scope from the reader (call ReadStartDocument next to read the scope).
/// </summary>
/// <returns>A string.</returns>
public override string ReadJavaScriptWithScope()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);
State = BsonReaderState.ScopeDocument;
return _currentValue.AsBsonJavaScriptWithScope.Code;
}
/// <summary>
/// Reads a BSON MaxKey from the reader.
/// </summary>
public override void ReadMaxKey()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadMaxKey", BsonType.MaxKey);
State = GetNextState();
}
/// <summary>
/// Reads a BSON MinKey from the reader.
/// </summary>
public override void ReadMinKey()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadMinKey", BsonType.MinKey);
State = GetNextState();
}
/// <summary>
/// Reads the name of an element from the reader.
/// </summary>
/// <param name="nameDecoder">The name decoder.</param>
/// <returns>
/// The name of the element.
/// </returns>
public override string ReadName(INameDecoder nameDecoder)
{
if (nameDecoder == null)
{
throw new ArgumentNullException("nameDecoder");
}
if (Disposed) { ThrowObjectDisposedException(); }
if (State == BsonReaderState.Type)
{
ReadBsonType();
}
if (State != BsonReaderState.Name)
{
ThrowInvalidState("ReadName", BsonReaderState.Name);
}
nameDecoder.Inform(CurrentName);
State = BsonReaderState.Value;
return CurrentName;
}
/// <summary>
/// Reads a BSON null from the reader.
/// </summary>
public override void ReadNull()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadNull", BsonType.Null);
State = GetNextState();
}
/// <summary>
/// Reads a BSON ObjectId from the reader.
/// </summary>
/// <returns>An ObjectId.</returns>
public override ObjectId ReadObjectId()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadObjectId", BsonType.ObjectId);
State = GetNextState();
return _currentValue.AsObjectId;
}
/// <summary>
/// Reads a BSON regular expression from the reader.
/// </summary>
/// <returns>A BsonRegularExpression.</returns>
public override BsonRegularExpression ReadRegularExpression()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadRegularExpression", BsonType.RegularExpression);
State = GetNextState();
return _currentValue.AsBsonRegularExpression;
}
/// <summary>
/// Reads the start of a BSON array.
/// </summary>
public override void ReadStartArray()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadStartArray", BsonType.Array);
var array = _currentValue.AsBsonArray;
_context = _context.PushContext(ContextType.Array, array);
State = BsonReaderState.Type;
}
/// <summary>
/// Reads the start of a BSON document.
/// </summary>
public override void ReadStartDocument()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadStartDocument", BsonType.Document);
BsonDocument document;
var script = _currentValue as BsonJavaScriptWithScope;
if (script != null)
{
document = script.Scope;
}
else
{
document = _currentValue.AsBsonDocument;
}
_context = _context.PushContext(ContextType.Document, document);
State = BsonReaderState.Type;
}
/// <summary>
/// Reads a BSON string from the reader.
/// </summary>
/// <returns>A String.</returns>
public override string ReadString()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadString", BsonType.String);
State = GetNextState();
return _currentValue.AsString;
}
/// <summary>
/// Reads a BSON symbol from the reader.
/// </summary>
/// <returns>A string.</returns>
public override string ReadSymbol()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadSymbol", BsonType.Symbol);
State = GetNextState();
return _currentValue.AsBsonSymbol.Name;
}
/// <summary>
/// Reads a BSON timestamp from the reader.
/// </summary>
/// <returns>The combined timestamp/increment.</returns>
public override long ReadTimestamp()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadTimestamp", BsonType.Timestamp);
State = GetNextState();
return _currentValue.AsBsonTimestamp.Value;
}
/// <summary>
/// Reads a BSON undefined from the reader.
/// </summary>
public override void ReadUndefined()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadUndefined", BsonType.Undefined);
State = GetNextState();
}
/// <summary>
/// Returns the reader to previously bookmarked position and state.
/// </summary>
/// <param name="bookmark">The bookmark.</param>
public override void ReturnToBookmark(BsonReaderBookmark bookmark)
{
var documentReaderBookmark = (BsonDocumentReaderBookmark)bookmark;
State = documentReaderBookmark.State;
CurrentBsonType = documentReaderBookmark.CurrentBsonType;
CurrentName = documentReaderBookmark.CurrentName;
_context = documentReaderBookmark.CloneContext();
_currentValue = documentReaderBookmark.CurrentValue;
}
/// <summary>
/// Skips the name (reader must be positioned on a name).
/// </summary>
public override void SkipName()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (State != BsonReaderState.Name)
{
ThrowInvalidState("SkipName", BsonReaderState.Name);
}
State = BsonReaderState.Value;
}
/// <summary>
/// Skips the value (reader must be positioned on a value).
/// </summary>
public override void SkipValue()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (State != BsonReaderState.Value)
{
ThrowInvalidState("SkipValue", BsonReaderState.Value);
}
State = BsonReaderState.Type;
}
// protected methods
/// <summary>
/// Disposes of any resources used by the reader.
/// </summary>
/// <param name="disposing">True if called from Dispose.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
try
{
Close();
}
catch { } // ignore exceptions
}
base.Dispose(disposing);
}
// private methods
private BsonReaderState GetNextState()
{
switch (_context.ContextType)
{
case ContextType.Array:
case ContextType.Document:
return BsonReaderState.Type;
case ContextType.TopLevel:
return BsonReaderState.Done;
default:
throw new BsonInternalException("Unexpected ContextType.");
}
}
}
}