| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- /* 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.Linq;
- using MongoDB.Bson.IO;
- using MongoDB.Bson.Serialization;
- using MongoDB.Bson.Serialization.Attributes;
- using MongoDB.Bson.Serialization.Serializers;
- namespace MongoDB.Bson
- {
- /// <summary>
- /// Represents an immutable BSON array that is represented using only the raw bytes.
- /// </summary>
- [Serializable]
- [BsonSerializer(typeof(RawBsonArraySerializer))]
- public class RawBsonArray : BsonArray, IDisposable
- {
- // private fields
- private bool _disposed;
- private IByteBuffer _slice;
- private List<IDisposable> _disposableItems = new List<IDisposable>();
- private BsonBinaryReaderSettings _readerSettings = BsonBinaryReaderSettings.Defaults;
- // constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="RawBsonArray"/> class.
- /// </summary>
- /// <param name="slice">The slice.</param>
- /// <exception cref="System.ArgumentNullException">slice</exception>
- /// <exception cref="System.ArgumentException">RawBsonArray cannot be used with an IByteBuffer that needs disposing.</exception>
- public RawBsonArray(IByteBuffer slice)
- {
- if (slice == null)
- {
- throw new ArgumentNullException("slice");
- }
- _slice = slice;
- }
- // 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
- {
- ThrowIfDisposed();
- return _slice.Capacity;
- }
- set
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- }
- /// <summary>
- /// Gets the count of array elements.
- /// </summary>
- public override int Count
- {
- get
- {
- ThrowIfDisposed();
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
- {
- var count = 0;
- bsonReader.ReadStartDocument();
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- bsonReader.SkipName();
- bsonReader.SkipValue();
- count++;
- }
- bsonReader.ReadEndDocument();
- return count;
- }
- }
- }
- /// <summary>
- /// Gets whether the array is read-only.
- /// </summary>
- public override bool IsReadOnly
- {
- get { return true; }
- }
- /// <summary>
- /// Gets the array elements as raw values (see BsonValue.RawValue).
- /// </summary>
- [Obsolete("Use ToArray to ToList instead.")]
- public override IEnumerable<object> RawValues
- {
- get
- {
- ThrowIfDisposed();
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
- {
- bsonReader.ReadStartDocument();
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- bsonReader.SkipName();
- yield return DeserializeBsonValue(bsonReader).RawValue;
- }
- bsonReader.ReadEndDocument();
- }
- }
- }
- /// <summary>
- /// Gets the slice.
- /// </summary>
- /// <value>
- /// The slice.
- /// </value>
- public IByteBuffer Slice
- {
- get { return _slice; }
- }
- /// <summary>
- /// Gets the array elements.
- /// </summary>
- public override IEnumerable<BsonValue> Values
- {
- get
- {
- ThrowIfDisposed();
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
- {
- bsonReader.ReadStartDocument();
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- bsonReader.SkipName();
- yield return DeserializeBsonValue(bsonReader);
- }
- bsonReader.ReadEndDocument();
- }
- }
- }
- // 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
- {
- if (index < 0)
- {
- throw new ArgumentOutOfRangeException("index");
- }
- ThrowIfDisposed();
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
- {
- bsonReader.ReadStartDocument();
- var i = 0;
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- bsonReader.SkipName();
- if (i == index)
- {
- return DeserializeBsonValue(bsonReader);
- }
- bsonReader.SkipValue();
- i++;
- }
- bsonReader.ReadEndDocument();
- throw new ArgumentOutOfRangeException("index");
- }
- }
- set
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- }
- // 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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <summary>
- /// Creates a shallow clone of the array (see also DeepClone).
- /// </summary>
- /// <returns>A shallow clone of the array.</returns>
- public override BsonValue Clone()
- {
- return new RawBsonArray(CloneSlice());
- }
- /// <summary>
- /// Clears the array.
- /// </summary>
- public override void Clear()
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- ThrowIfDisposed();
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
- {
- bsonReader.ReadStartDocument();
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- bsonReader.SkipName();
- if (DeserializeBsonValue(bsonReader).Equals(value))
- {
- return true;
- }
- }
- bsonReader.ReadEndDocument();
- return false;
- }
- }
- /// <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)
- {
- ThrowIfDisposed();
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
- {
- bsonReader.ReadStartDocument();
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- bsonReader.SkipName();
- array[arrayIndex++] = DeserializeBsonValue(bsonReader);
- }
- bsonReader.ReadEndDocument();
- }
- }
- /// <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)
- {
- ThrowIfDisposed();
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
- {
- bsonReader.ReadStartDocument();
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- bsonReader.SkipName();
- array[arrayIndex++] = DeserializeBsonValue(bsonReader).RawValue;
- }
- bsonReader.ReadEndDocument();
- }
- }
- /// <summary>
- /// Creates a deep clone of the array (see also Clone).
- /// </summary>
- /// <returns>A deep clone of the array.</returns>
- public override BsonValue DeepClone()
- {
- ThrowIfDisposed();
- return new RawBsonArray(CloneSlice());
- }
- /// <summary>
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- /// </summary>
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- /// <summary>
- /// Gets an enumerator that can enumerate the elements of the array.
- /// </summary>
- /// <returns>An enumerator.</returns>
- public override IEnumerator<BsonValue> GetEnumerator()
- {
- ThrowIfDisposed();
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
- {
- bsonReader.ReadStartDocument();
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- bsonReader.SkipName();
- yield return DeserializeBsonValue(bsonReader);
- }
- bsonReader.ReadEndDocument();
- }
- }
- /// <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)
- {
- return IndexOf(value, 0, int.MaxValue);
- }
- /// <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)
- {
- return IndexOf(value, index, int.MaxValue);
- }
- /// <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)
- {
- ThrowIfDisposed();
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
- {
- bsonReader.ReadStartDocument();
- var i = 0;
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- bsonReader.SkipName();
- if (i >= index)
- {
- if (count == 0)
- {
- return -1;
- }
- if (DeserializeBsonValue(bsonReader).Equals(value))
- {
- return i;
- }
- count--;
- }
- else
- {
- bsonReader.SkipValue();
- }
- i++;
- }
- bsonReader.ReadEndDocument();
- return -1;
- }
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <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)
- {
- throw new NotSupportedException("RawBsonArray instances are immutable.");
- }
- /// <summary>
- /// Converts the BsonArray to an array of BsonValues.
- /// </summary>
- /// <returns>An array of BsonValues.</returns>
- public override BsonValue[] ToArray()
- {
- ThrowIfDisposed();
- return Values.ToArray();
- }
- /// <summary>
- /// Converts the BsonArray to a list of BsonValues.
- /// </summary>
- /// <returns>A list of BsonValues.</returns>
- public override List<BsonValue> ToList()
- {
- ThrowIfDisposed();
- return Values.ToList();
- }
- /// <summary>
- /// Returns a string representation of the array.
- /// </summary>
- /// <returns>A string representation of the array.</returns>
- public override string ToString()
- {
- ThrowIfDisposed();
- var parts = Values.Select(v => v.ToString()).ToArray();
- return string.Format("[{0}]", string.Join(", ", parts));
- }
- // 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)
- {
- if (!_disposed)
- {
- if (disposing)
- {
- if (_slice != null)
- {
- _slice.Dispose();
- _slice = null;
- }
- if (_disposableItems != null)
- {
- _disposableItems.ForEach(x => x.Dispose());
- _disposableItems = null;
- }
- }
- _disposed = true;
- }
- }
- /// <summary>
- /// Throws if disposed.
- /// </summary>
- /// <exception cref="System.ObjectDisposedException"></exception>
- protected void ThrowIfDisposed()
- {
- if (_disposed)
- {
- throw new ObjectDisposedException(GetType().Name);
- }
- }
- // private methods
- private IByteBuffer CloneSlice()
- {
- return _slice.GetSlice(0, _slice.Length);
- }
- private RawBsonArray DeserializeRawBsonArray(BsonBinaryReader bsonReader)
- {
- var slice = bsonReader.ReadRawBsonArray();
- var nestedArray = new RawBsonArray(slice);
- _disposableItems.Add(nestedArray);
- return nestedArray;
- }
- private RawBsonDocument DeserializeRawBsonDocument(BsonBinaryReader bsonReader)
- {
- var slice = bsonReader.ReadRawBsonDocument();
- var nestedDocument = new RawBsonDocument(slice);
- _disposableItems.Add(nestedDocument);
- return nestedDocument;
- }
- private BsonValue DeserializeBsonValue(BsonBinaryReader bsonReader)
- {
- switch (bsonReader.GetCurrentBsonType())
- {
- case BsonType.Array: return DeserializeRawBsonArray(bsonReader);
- case BsonType.Document: return DeserializeRawBsonDocument(bsonReader);
- default: return (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
- }
- }
- }
- }
|