-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
MaterializedOnDemandBsonDocument.cs
797 lines (734 loc) · 27.5 KB
/
MaterializedOnDemandBsonDocument.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/* 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.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Serializers;
namespace MongoDB.Bson
{
/// <summary>
/// Represents a BSON document that is not materialized until you start using it.
/// </summary>
[BsonSerializer(typeof(MaterializedOnDemandBsonDocumentSerializer))]
public abstract class MaterializedOnDemandBsonDocument : BsonDocument, IDisposable
{
// private fields
private bool _disposed;
private bool _isMaterialized;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="MaterializedOnDemandBsonDocument"/> class.
/// </summary>
protected MaterializedOnDemandBsonDocument()
{
}
// public properties
/// <summary>
/// Gets the number of elements.
/// </summary>
public override int ElementCount
{
get
{
EnsureIsMaterialized();
return base.ElementCount;
}
}
/// <summary>
/// Gets the elements.
/// </summary>
public override IEnumerable<BsonElement> Elements
{
get
{
EnsureIsMaterialized();
return base.Elements;
}
}
/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposed; otherwise, <c>false</c>.
/// </value>
public bool IsDisposed
{
get { return _disposed; }
}
/// <summary>
/// Gets a value indicating whether this instance is materialized.
/// </summary>
/// <value>
/// <c>true</c> if this instance is materialized; otherwise, <c>false</c>.
/// </value>
public bool IsMaterialized
{
get { return _isMaterialized; }
}
/// <summary>
/// Gets the element names.
/// </summary>
public override IEnumerable<string> Names
{
get
{
EnsureIsMaterialized();
return base.Names;
}
}
/// <summary>
/// Gets the raw values (see BsonValue.RawValue).
/// </summary>
[Obsolete("Use Values instead.")]
public override IEnumerable<object> RawValues
{
get
{
EnsureIsMaterialized();
return base.RawValues;
}
}
/// <summary>
/// Gets the values.
/// </summary>
public override IEnumerable<BsonValue> Values
{
get
{
EnsureIsMaterialized();
return base.Values;
}
}
// public indexers
/// <summary>
/// Gets or sets a value by position.
/// </summary>
/// <param name="index">The position.</param>
/// <returns>The value.</returns>
public override BsonValue this[int index]
{
get
{
EnsureIsMaterialized();
return base[index];
}
set
{
EnsureIsMaterialized();
base[index] = value;
}
}
/// <summary>
/// Gets the value of an element or a default value if the element is not found.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="defaultValue">The default value to return if the element is not found.</param>
/// <returns>Teh value of the element or a default value if the element is not found.</returns>
[Obsolete("Use GetValue(string name, BsonValue defaultValue) instead.")]
public override BsonValue this[string name, BsonValue defaultValue]
{
get
{
EnsureIsMaterialized();
return base[name, defaultValue];
}
}
/// <summary>
/// Gets or sets a value by name.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>The value.</returns>
public override BsonValue this[string name]
{
get
{
EnsureIsMaterialized();
return base[name];
}
set
{
EnsureIsMaterialized();
base[name] = value;
}
}
// public methods
/// <summary>
/// Adds an element to the document.
/// </summary>
/// <param name="element">The element to add.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument Add(BsonElement element)
{
EnsureIsMaterialized();
return base.Add(element);
}
/// <summary>
/// Adds elements to the document from a dictionary of key/value pairs.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <returns>The document (so method calls can be chained).</returns>
[Obsolete("Use AddRange instead.")]
public override BsonDocument Add(Dictionary<string, object> dictionary)
{
EnsureIsMaterialized();
return base.Add(dictionary);
}
/// <summary>
/// Adds elements to the document from a dictionary of key/value pairs.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <param name="keys">Which keys of the hash table to add.</param>
/// <returns>The document (so method calls can be chained).</returns>
[Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
public override BsonDocument Add(Dictionary<string, object> dictionary, IEnumerable<string> keys)
{
EnsureIsMaterialized();
return base.Add(dictionary, keys);
}
/// <summary>
/// Adds elements to the document from a dictionary of key/value pairs.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <returns>The document (so method calls can be chained).</returns>
[Obsolete("Use AddRange instead.")]
public override BsonDocument Add(IDictionary<string, object> dictionary)
{
EnsureIsMaterialized();
return base.Add(dictionary);
}
/// <summary>
/// Adds elements to the document from a dictionary of key/value pairs.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <param name="keys">Which keys of the hash table to add.</param>
/// <returns>The document (so method calls can be chained).</returns>
[Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
public override BsonDocument Add(IDictionary<string, object> dictionary, IEnumerable<string> keys)
{
EnsureIsMaterialized();
return base.Add(dictionary, keys);
}
/// <summary>
/// Adds elements to the document from a dictionary of key/value pairs.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <returns>The document (so method calls can be chained).</returns>
[Obsolete("Use AddRange instead.")]
public override BsonDocument Add(IDictionary dictionary)
{
EnsureIsMaterialized();
return base.Add(dictionary);
}
/// <summary>
/// Adds elements to the document from a dictionary of key/value pairs.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <param name="keys">Which keys of the hash table to add.</param>
/// <returns>The document (so method calls can be chained).</returns>
[Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
public override BsonDocument Add(IDictionary dictionary, IEnumerable keys)
{
EnsureIsMaterialized();
return base.Add(dictionary, keys);
}
/// <summary>
/// Adds a list of elements to the document.
/// </summary>
/// <param name="elements">The list of elements.</param>
/// <returns>The document (so method calls can be chained).</returns>
[Obsolete("Use AddRange instead.")]
public override BsonDocument Add(IEnumerable<BsonElement> elements)
{
EnsureIsMaterialized();
return base.Add(elements);
}
/// <summary>
/// Adds a list of elements to the document.
/// </summary>
/// <param name="elements">The list of elements.</param>
/// <returns>The document (so method calls can be chained).</returns>
[Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
public override BsonDocument Add(params BsonElement[] elements)
{
EnsureIsMaterialized();
return base.Add(elements);
}
/// <summary>
/// Creates and adds an element to the document.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="value">The value of the element.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument Add(string name, BsonValue value)
{
EnsureIsMaterialized();
return base.Add(name, value);
}
/// <summary>
/// Creates and adds an element to the document, but only if the condition is true.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="value">The value of the element.</param>
/// <param name="condition">Whether to add the element to the document.</param>
/// <returns>The document (so method calls can be chained).</returns>
public override BsonDocument Add(string name, BsonValue value, bool condition)
{
EnsureIsMaterialized();
return base.Add(name, value, condition);
}
/// <summary>
/// Creates and adds an element to the document, but only if the condition is true.
/// If the condition is false the value factory is not called at all.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="valueFactory">A delegate called to compute the value of the element if condition is true.</param>
/// <param name="condition">Whether to add the element to the document.</param>
/// <returns>The document (so method calls can be chained).</returns>
public override BsonDocument Add(string name, Func<BsonValue> valueFactory, bool condition)
{
EnsureIsMaterialized();
return base.Add(name, valueFactory, condition);
}
/// <summary>
/// Adds elements to the document from a dictionary of key/value pairs.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument AddRange(Dictionary<string, object> dictionary)
{
EnsureIsMaterialized();
return base.AddRange(dictionary);
}
/// <summary>
/// Adds elements to the document from a dictionary of key/value pairs.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument AddRange(IDictionary dictionary)
{
EnsureIsMaterialized();
return base.AddRange(dictionary);
}
/// <summary>
/// Adds a list of elements to the document.
/// </summary>
/// <param name="elements">The list of elements.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument AddRange(IEnumerable<BsonElement> elements)
{
EnsureIsMaterialized();
return base.AddRange(elements);
}
/// <summary>
/// Adds elements to the document from a dictionary of key/value pairs.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument AddRange(IEnumerable<KeyValuePair<string, object>> dictionary)
{
EnsureIsMaterialized();
return base.AddRange(dictionary);
}
/// <summary>
/// Clears the document (removes all elements).
/// </summary>
public override void Clear()
{
EnsureIsMaterialized();
base.Clear();
}
/// <summary>
/// Creates a shallow clone of the document (see also DeepClone).
/// </summary>
/// <returns>
/// A shallow clone of the document.
/// </returns>
public override BsonValue Clone()
{
EnsureIsMaterialized();
return base.Clone();
}
/// <summary>
/// Compares this document to another document.
/// </summary>
/// <param name="other">The other document.</param>
/// <returns>
/// A 32-bit signed integer that indicates whether this document is less than, equal to, or greather than the other.
/// </returns>
public override int CompareTo(BsonDocument other)
{
EnsureIsMaterialized();
return base.CompareTo(other);
}
/// <summary>
/// Compares the BsonDocument to another BsonValue.
/// </summary>
/// <param name="other">The other BsonValue.</param>
/// <returns>A 32-bit signed integer that indicates whether this BsonDocument is less than, equal to, or greather than the other BsonValue.</returns>
public override int CompareTo(BsonValue other)
{
EnsureIsMaterialized();
return base.CompareTo(other);
}
/// <summary>
/// Tests whether the document contains an element with the specified name.
/// </summary>
/// <param name="name">The name of the element to look for.</param>
/// <returns>
/// True if the document contains an element with the specified name.
/// </returns>
public override bool Contains(string name)
{
EnsureIsMaterialized();
return base.Contains(name);
}
/// <summary>
/// Tests whether the document contains an element with the specified value.
/// </summary>
/// <param name="value">The value of the element to look for.</param>
/// <returns>
/// True if the document contains an element with the specified value.
/// </returns>
public override bool ContainsValue(BsonValue value)
{
EnsureIsMaterialized();
return base.ContainsValue(value);
}
/// <summary>
/// Creates a deep clone of the document (see also Clone).
/// </summary>
/// <returns>
/// A deep clone of the document.
/// </returns>
public override BsonValue DeepClone()
{
EnsureIsMaterialized();
return base.DeepClone();
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
EnsureIsMaterialized();
return base.Equals(obj);
}
/// <summary>
/// Gets an element of this document.
/// </summary>
/// <param name="index">The zero based index of the element.</param>
/// <returns>
/// The element.
/// </returns>
public override BsonElement GetElement(int index)
{
EnsureIsMaterialized();
return base.GetElement(index);
}
/// <summary>
/// Gets an element of this document.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <returns>
/// A BsonElement.
/// </returns>
public override BsonElement GetElement(string name)
{
EnsureIsMaterialized();
return base.GetElement(name);
}
/// <summary>
/// Gets an enumerator that can be used to enumerate the elements of this document.
/// </summary>
/// <returns>
/// An enumerator.
/// </returns>
public override IEnumerator<BsonElement> GetEnumerator()
{
EnsureIsMaterialized();
return base.GetEnumerator();
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
EnsureIsMaterialized();
return base.GetHashCode();
}
/// <summary>
/// Gets the value of an element.
/// </summary>
/// <param name="index">The zero based index of the element.</param>
/// <returns>
/// The value of the element.
/// </returns>
public override BsonValue GetValue(int index)
{
EnsureIsMaterialized();
return base.GetValue(index);
}
/// <summary>
/// Gets the value of an element.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <returns>
/// The value of the element.
/// </returns>
public override BsonValue GetValue(string name)
{
EnsureIsMaterialized();
return base.GetValue(name);
}
/// <summary>
/// Gets the value of an element or a default value if the element is not found.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="defaultValue">The default value returned if the element is not found.</param>
/// <returns>
/// The value of the element or the default value if the element is not found.
/// </returns>
public override BsonValue GetValue(string name, BsonValue defaultValue)
{
EnsureIsMaterialized();
return base.GetValue(name, defaultValue);
}
/// <summary>
/// Inserts a new element at a specified position.
/// </summary>
/// <param name="index">The position of the new element.</param>
/// <param name="element">The element.</param>
public override void InsertAt(int index, BsonElement element)
{
EnsureIsMaterialized();
base.InsertAt(index, element);
}
/// <summary>
/// Merges another document into this one. Existing elements are not overwritten.
/// </summary>
/// <param name="document">The other document.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument Merge(BsonDocument document)
{
EnsureIsMaterialized();
return base.Merge(document);
}
/// <summary>
/// Merges another document into this one, specifying whether existing elements are overwritten.
/// </summary>
/// <param name="document">The other document.</param>
/// <param name="overwriteExistingElements">Whether to overwrite existing elements.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument Merge(BsonDocument document, bool overwriteExistingElements)
{
EnsureIsMaterialized();
return base.Merge(document, overwriteExistingElements);
}
/// <summary>
/// Removes an element from this document (if duplicate element names are allowed
/// then all elements with this name will be removed).
/// </summary>
/// <param name="name">The name of the element to remove.</param>
public override void Remove(string name)
{
EnsureIsMaterialized();
base.Remove(name);
}
/// <summary>
/// Removes an element from this document.
/// </summary>
/// <param name="index">The zero based index of the element to remove.</param>
public override void RemoveAt(int index)
{
EnsureIsMaterialized();
base.RemoveAt(index);
}
/// <summary>
/// Removes an element from this document.
/// </summary>
/// <param name="element">The element to remove.</param>
public override void RemoveElement(BsonElement element)
{
EnsureIsMaterialized();
base.RemoveElement(element);
}
/// <summary>
/// Sets the value of an element.
/// </summary>
/// <param name="index">The zero based index of the element whose value is to be set.</param>
/// <param name="value">The new value.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument Set(int index, BsonValue value)
{
EnsureIsMaterialized();
return base.Set(index, value);
}
/// <summary>
/// Sets the value of an element (an element will be added if no element with this name is found).
/// </summary>
/// <param name="name">The name of the element whose value is to be set.</param>
/// <param name="value">The new value.</param>
/// <returns>
/// The document (so method calls can be chained).
/// </returns>
public override BsonDocument Set(string name, BsonValue value)
{
EnsureIsMaterialized();
return base.Set(name, value);
}
/// <summary>
/// Sets an element of the document (replaces any existing element with the same name or adds a new element if an element with the same name is not found).
/// </summary>
/// <param name="element">The new element.</param>
/// <returns>
/// The document.
/// </returns>
public override BsonDocument SetElement(BsonElement element)
{
EnsureIsMaterialized();
return base.SetElement(element);
}
/// <summary>
/// Sets an element of the document (replacing the existing element at that position).
/// </summary>
/// <param name="index">The zero based index of the element to replace.</param>
/// <param name="element">The new element.</param>
/// <returns>
/// The document.
/// </returns>
public override BsonDocument SetElement(int index, BsonElement element)
{
EnsureIsMaterialized();
return base.SetElement(index, element);
}
/// <summary>
/// Tries to get an element of this document.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="value">The element.</param>
/// <returns>
/// True if an element with that name was found.
/// </returns>
public override bool TryGetElement(string name, out BsonElement value)
{
EnsureIsMaterialized();
return base.TryGetElement(name, out value);
}
/// <summary>
/// Tries to get the value of an element of this document.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="value">The value of the element.</param>
/// <returns>
/// True if an element with that name was found.
/// </returns>
public override bool TryGetValue(string name, out BsonValue value)
{
EnsureIsMaterialized();
return base.TryGetValue(name, out value);
}
// protected methods
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
_disposed = true;
}
/// <summary>
/// Materializes the BsonDocument.
/// </summary>
/// <returns>The materialized elements.</returns>
protected abstract IEnumerable<BsonElement> Materialize();
/// <summary>
/// Informs subclasses that the Materialize process completed so they can free any resources related to the unmaterialized state.
/// </summary>
protected abstract void MaterializeCompleted();
/// <summary>
/// Throws if disposed.
/// </summary>
/// <exception cref="System.ObjectDisposedException"></exception>
protected void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
}
// private methods
private void EnsureIsMaterialized()
{
ThrowIfDisposed();
if (!_isMaterialized)
{
var elements = Materialize();
try
{
_isMaterialized = true;
base.AddRange(elements);
MaterializeCompleted();
}
catch
{
base.Clear();
_isMaterialized = false;
throw;
}
}
}
}
internal class MaterializedOnDemandBsonDocumentSerializer : AbstractClassSerializer<MaterializedOnDemandBsonDocument>
{
}
}