| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797 |
- /* Copyright 2010-2014 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>
- {
- }
- }
|