| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- /* 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 MongoDB.Bson.IO;
- using MongoDB.Bson.Serialization;
- using MongoDB.Bson.Serialization.Attributes;
- using MongoDB.Bson.Serialization.Serializers;
- namespace MongoDB.Bson
- {
- /// <summary>
- /// Represents a BSON array that is not materialized until you start using it.
- /// </summary>
- [Serializable]
- public abstract class MaterializedOnDemandBsonArray : BsonArray, IDisposable
- {
- // private fields
- private bool _disposed;
- private bool _isMaterialized;
- // constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="MaterializedOnDemandBsonArray"/> class.
- /// </summary>
- protected MaterializedOnDemandBsonArray()
- {
- }
- // public properties
- /// <summary>
- /// Gets or sets the total number of elements the internal data structure can hold without resizing.
- /// </summary>
- public override int Capacity
- {
- get
- {
- EnsureIsMaterialized();
- return base.Capacity;
- }
- set
- {
- EnsureIsMaterialized();
- base.Capacity = value;
- }
- }
- /// <summary>
- /// Gets the count of array elements.
- /// </summary>
- public override int Count
- {
- get
- {
- EnsureIsMaterialized();
- return base.Count;
- }
- }
- /// <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 array elements as raw values (see BsonValue.RawValue).
- /// </summary>
- [Obsolete("Use ToArray to ToList instead.")]
- public override IEnumerable<object> RawValues
- {
- get
- {
- EnsureIsMaterialized();
- return base.RawValues;
- }
- }
- /// <summary>
- /// Gets the array elements.
- /// </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;
- }
- }
- // public methods
- /// <summary>
- /// Adds an element to the array.
- /// </summary>
- /// <param name="value">The value to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray Add(BsonValue value)
- {
- EnsureIsMaterialized();
- return base.Add(value);
- }
- /// <summary>
- /// Adds multiple elements to the array.
- /// </summary>
- /// <param name="values">A list of values to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray AddRange(IEnumerable<bool> values)
- {
- EnsureIsMaterialized();
- return base.AddRange(values);
- }
- /// <summary>
- /// Adds multiple elements to the array.
- /// </summary>
- /// <param name="values">A list of values to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray AddRange(IEnumerable<BsonValue> values)
- {
- EnsureIsMaterialized();
- return base.AddRange(values);
- }
- /// <summary>
- /// Adds multiple elements to the array.
- /// </summary>
- /// <param name="values">A list of values to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray AddRange(IEnumerable<DateTime> values)
- {
- EnsureIsMaterialized();
- return base.AddRange(values);
- }
- /// <summary>
- /// Adds multiple elements to the array.
- /// </summary>
- /// <param name="values">A list of values to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray AddRange(IEnumerable<double> values)
- {
- EnsureIsMaterialized();
- return base.AddRange(values);
- }
- /// <summary>
- /// Adds multiple elements to the array.
- /// </summary>
- /// <param name="values">A list of values to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray AddRange(IEnumerable<int> values)
- {
- EnsureIsMaterialized();
- return base.AddRange(values);
- }
- /// <summary>
- /// Adds multiple elements to the array.
- /// </summary>
- /// <param name="values">A list of values to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray AddRange(IEnumerable<long> values)
- {
- EnsureIsMaterialized();
- return base.AddRange(values);
- }
- /// <summary>
- /// Adds multiple elements to the array.
- /// </summary>
- /// <param name="values">A list of values to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray AddRange(IEnumerable<ObjectId> values)
- {
- EnsureIsMaterialized();
- return base.AddRange(values);
- }
- /// <summary>
- /// Adds multiple elements to the array.
- /// </summary>
- /// <param name="values">A list of values to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray AddRange(IEnumerable<string> values)
- {
- EnsureIsMaterialized();
- return base.AddRange(values);
- }
- /// <summary>
- /// Adds multiple elements to the array.
- /// </summary>
- /// <param name="values">A list of values to add to the array.</param>
- /// <returns>The array (so method calls can be chained).</returns>
- public override BsonArray AddRange(IEnumerable values)
- {
- EnsureIsMaterialized();
- return base.AddRange(values);
- }
- /// <summary>
- /// Clears the array.
- /// </summary>
- public override void Clear()
- {
- EnsureIsMaterialized();
- base.Clear();
- }
- /// <summary>
- /// Creates a shallow clone of the array (see also DeepClone).
- /// </summary>
- /// <returns>
- /// A shallow clone of the array.
- /// </returns>
- public override BsonValue Clone()
- {
- EnsureIsMaterialized();
- return base.Clone();
- }
- /// <summary>
- /// Compares the array to another array.
- /// </summary>
- /// <param name="other">The other array.</param>
- /// <returns>A 32-bit signed integer that indicates whether this array is less than, equal to, or greather than the other.</returns>
- public override int CompareTo(BsonArray other)
- {
- EnsureIsMaterialized();
- return base.CompareTo(other);
- }
- /// <summary>
- /// Compares the array to another BsonValue.
- /// </summary>
- /// <param name="other">The other BsonValue.</param>
- /// <returns>A 32-bit signed integer that indicates whether this array 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 array contains a value.
- /// </summary>
- /// <param name="value">The value to test for.</param>
- /// <returns>True if the array contains the value.</returns>
- public override bool Contains(BsonValue value)
- {
- EnsureIsMaterialized();
- return base.Contains(value);
- }
- /// <summary>
- /// Copies elements from this array to another array.
- /// </summary>
- /// <param name="array">The other array.</param>
- /// <param name="arrayIndex">The zero based index of the other array at which to start copying.</param>
- public override void CopyTo(BsonValue[] array, int arrayIndex)
- {
- EnsureIsMaterialized();
- base.CopyTo(array, arrayIndex);
- }
- /// <summary>
- /// Copies elements from this array to another array as raw values (see BsonValue.RawValue).
- /// </summary>
- /// <param name="array">The other array.</param>
- /// <param name="arrayIndex">The zero based index of the other array at which to start copying.</param>
- [Obsolete("Use ToArray or ToList instead.")]
- public override void CopyTo(object[] array, int arrayIndex)
- {
- EnsureIsMaterialized();
- base.CopyTo(array, arrayIndex);
- }
- /// <summary>
- /// Creates a deep clone of the array (see also Clone).
- /// </summary>
- /// <returns>
- /// A deep clone of the array.
- /// </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 enumerator that can enumerate the elements of the array.
- /// </summary>
- /// <returns>An enumerator.</returns>
- public override IEnumerator<BsonValue> GetEnumerator()
- {
- EnsureIsMaterialized();
- return base.GetEnumerator();
- }
- /// <summary>
- /// Gets the hash code.
- /// </summary>
- /// <returns>The hash code.</returns>
- public override int GetHashCode()
- {
- EnsureIsMaterialized();
- return base.GetHashCode();
- }
- /// <summary>
- /// Gets the index of a value in the array.
- /// </summary>
- /// <param name="value">The value to search for.</param>
- /// <returns>The zero based index of the value (or -1 if not found).</returns>
- public override int IndexOf(BsonValue value)
- {
- EnsureIsMaterialized();
- return base.IndexOf(value);
- }
- /// <summary>
- /// Gets the index of a value in the array.
- /// </summary>
- /// <param name="value">The value to search for.</param>
- /// <param name="index">The zero based index at which to start the search.</param>
- /// <returns>The zero based index of the value (or -1 if not found).</returns>
- public override int IndexOf(BsonValue value, int index)
- {
- EnsureIsMaterialized();
- return base.IndexOf(value, index);
- }
- /// <summary>
- /// Gets the index of a value in the array.
- /// </summary>
- /// <param name="value">The value to search for.</param>
- /// <param name="index">The zero based index at which to start the search.</param>
- /// <param name="count">The number of elements to search.</param>
- /// <returns>The zero based index of the value (or -1 if not found).</returns>
- public override int IndexOf(BsonValue value, int index, int count)
- {
- EnsureIsMaterialized();
- return base.IndexOf(value, index, count);
- }
- /// <summary>
- /// Inserts a new value into the array.
- /// </summary>
- /// <param name="index">The zero based index at which to insert the new value.</param>
- /// <param name="value">The new value.</param>
- public override void Insert(int index, BsonValue value)
- {
- EnsureIsMaterialized();
- base.Insert(index, value);
- }
- /// <summary>
- /// Removes the first occurrence of a value from the array.
- /// </summary>
- /// <param name="value">The value to remove.</param>
- /// <returns>True if the value was removed.</returns>
- public override bool Remove(BsonValue value)
- {
- EnsureIsMaterialized();
- return base.Remove(value);
- }
- /// <summary>
- /// Removes an element from the array.
- /// </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>
- /// Converts the BsonArray to an array of BsonValues.
- /// </summary>
- /// <returns>An array of BsonValues.</returns>
- public override BsonValue[] ToArray()
- {
- EnsureIsMaterialized();
- return base.ToArray();
- }
- /// <summary>
- /// Converts the BsonArray to a list of BsonValues.
- /// </summary>
- /// <returns>A list of BsonValues.</returns>
- public override List<BsonValue> ToList()
- {
- EnsureIsMaterialized();
- return base.ToList();
- }
- /// <summary>
- /// Returns a string representation of the array.
- /// </summary>
- /// <returns>A string representation of the array.</returns>
- public override string ToString()
- {
- EnsureIsMaterialized();
- return base.ToString();
- }
- // 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 BsonArray.
- /// </summary>
- /// <returns>The materialized elements.</returns>
- protected abstract IEnumerable<BsonValue> 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 values = Materialize();
- try
- {
- _isMaterialized = true;
- base.AddRange(values);
- MaterializeCompleted();
- }
- catch
- {
- base.Clear();
- _isMaterialized = false;
- throw;
- }
- }
- }
- }
- }
|