RawBsonArray.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using MongoDB.Bson.IO;
  20. using MongoDB.Bson.Serialization;
  21. using MongoDB.Bson.Serialization.Attributes;
  22. using MongoDB.Bson.Serialization.Serializers;
  23. namespace MongoDB.Bson
  24. {
  25. /// <summary>
  26. /// Represents an immutable BSON array that is represented using only the raw bytes.
  27. /// </summary>
  28. [Serializable]
  29. [BsonSerializer(typeof(RawBsonArraySerializer))]
  30. public class RawBsonArray : BsonArray, IDisposable
  31. {
  32. // private fields
  33. private bool _disposed;
  34. private IByteBuffer _slice;
  35. private List<IDisposable> _disposableItems = new List<IDisposable>();
  36. private BsonBinaryReaderSettings _readerSettings = BsonBinaryReaderSettings.Defaults;
  37. // constructors
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="RawBsonArray"/> class.
  40. /// </summary>
  41. /// <param name="slice">The slice.</param>
  42. /// <exception cref="System.ArgumentNullException">slice</exception>
  43. /// <exception cref="System.ArgumentException">RawBsonArray cannot be used with an IByteBuffer that needs disposing.</exception>
  44. public RawBsonArray(IByteBuffer slice)
  45. {
  46. if (slice == null)
  47. {
  48. throw new ArgumentNullException("slice");
  49. }
  50. _slice = slice;
  51. }
  52. // public properties
  53. /// <summary>
  54. /// Gets or sets the total number of elements the internal data structure can hold without resizing.
  55. /// </summary>
  56. public override int Capacity
  57. {
  58. get
  59. {
  60. ThrowIfDisposed();
  61. return _slice.Capacity;
  62. }
  63. set
  64. {
  65. throw new NotSupportedException("RawBsonArray instances are immutable.");
  66. }
  67. }
  68. /// <summary>
  69. /// Gets the count of array elements.
  70. /// </summary>
  71. public override int Count
  72. {
  73. get
  74. {
  75. ThrowIfDisposed();
  76. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
  77. {
  78. var count = 0;
  79. bsonReader.ReadStartDocument();
  80. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  81. {
  82. bsonReader.SkipName();
  83. bsonReader.SkipValue();
  84. count++;
  85. }
  86. bsonReader.ReadEndDocument();
  87. return count;
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// Gets whether the array is read-only.
  93. /// </summary>
  94. public override bool IsReadOnly
  95. {
  96. get { return true; }
  97. }
  98. /// <summary>
  99. /// Gets the array elements as raw values (see BsonValue.RawValue).
  100. /// </summary>
  101. [Obsolete("Use ToArray to ToList instead.")]
  102. public override IEnumerable<object> RawValues
  103. {
  104. get
  105. {
  106. ThrowIfDisposed();
  107. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
  108. {
  109. bsonReader.ReadStartDocument();
  110. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  111. {
  112. bsonReader.SkipName();
  113. yield return DeserializeBsonValue(bsonReader).RawValue;
  114. }
  115. bsonReader.ReadEndDocument();
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Gets the slice.
  121. /// </summary>
  122. /// <value>
  123. /// The slice.
  124. /// </value>
  125. public IByteBuffer Slice
  126. {
  127. get { return _slice; }
  128. }
  129. /// <summary>
  130. /// Gets the array elements.
  131. /// </summary>
  132. public override IEnumerable<BsonValue> Values
  133. {
  134. get
  135. {
  136. ThrowIfDisposed();
  137. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
  138. {
  139. bsonReader.ReadStartDocument();
  140. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  141. {
  142. bsonReader.SkipName();
  143. yield return DeserializeBsonValue(bsonReader);
  144. }
  145. bsonReader.ReadEndDocument();
  146. }
  147. }
  148. }
  149. // public indexers
  150. /// <summary>
  151. /// Gets or sets a value by position.
  152. /// </summary>
  153. /// <param name="index">The position.</param>
  154. /// <returns>The value.</returns>
  155. public override BsonValue this[int index]
  156. {
  157. get
  158. {
  159. if (index < 0)
  160. {
  161. throw new ArgumentOutOfRangeException("index");
  162. }
  163. ThrowIfDisposed();
  164. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
  165. {
  166. bsonReader.ReadStartDocument();
  167. var i = 0;
  168. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  169. {
  170. bsonReader.SkipName();
  171. if (i == index)
  172. {
  173. return DeserializeBsonValue(bsonReader);
  174. }
  175. bsonReader.SkipValue();
  176. i++;
  177. }
  178. bsonReader.ReadEndDocument();
  179. throw new ArgumentOutOfRangeException("index");
  180. }
  181. }
  182. set
  183. {
  184. throw new NotSupportedException("RawBsonArray instances are immutable.");
  185. }
  186. }
  187. // public methods
  188. /// <summary>
  189. /// Adds an element to the array.
  190. /// </summary>
  191. /// <param name="value">The value to add to the array.</param>
  192. /// <returns>The array (so method calls can be chained).</returns>
  193. public override BsonArray Add(BsonValue value)
  194. {
  195. throw new NotSupportedException("RawBsonArray instances are immutable.");
  196. }
  197. /// <summary>
  198. /// Adds multiple elements to the array.
  199. /// </summary>
  200. /// <param name="values">A list of values to add to the array.</param>
  201. /// <returns>The array (so method calls can be chained).</returns>
  202. public override BsonArray AddRange(IEnumerable<bool> values)
  203. {
  204. throw new NotSupportedException("RawBsonArray instances are immutable.");
  205. }
  206. /// <summary>
  207. /// Adds multiple elements to the array.
  208. /// </summary>
  209. /// <param name="values">A list of values to add to the array.</param>
  210. /// <returns>The array (so method calls can be chained).</returns>
  211. public override BsonArray AddRange(IEnumerable<BsonValue> values)
  212. {
  213. throw new NotSupportedException("RawBsonArray instances are immutable.");
  214. }
  215. /// <summary>
  216. /// Adds multiple elements to the array.
  217. /// </summary>
  218. /// <param name="values">A list of values to add to the array.</param>
  219. /// <returns>The array (so method calls can be chained).</returns>
  220. public override BsonArray AddRange(IEnumerable<DateTime> values)
  221. {
  222. throw new NotSupportedException("RawBsonArray instances are immutable.");
  223. }
  224. /// <summary>
  225. /// Adds multiple elements to the array.
  226. /// </summary>
  227. /// <param name="values">A list of values to add to the array.</param>
  228. /// <returns>The array (so method calls can be chained).</returns>
  229. public override BsonArray AddRange(IEnumerable<double> values)
  230. {
  231. throw new NotSupportedException("RawBsonArray instances are immutable.");
  232. }
  233. /// <summary>
  234. /// Adds multiple elements to the array.
  235. /// </summary>
  236. /// <param name="values">A list of values to add to the array.</param>
  237. /// <returns>The array (so method calls can be chained).</returns>
  238. public override BsonArray AddRange(IEnumerable<int> values)
  239. {
  240. throw new NotSupportedException("RawBsonArray instances are immutable.");
  241. }
  242. /// <summary>
  243. /// Adds multiple elements to the array.
  244. /// </summary>
  245. /// <param name="values">A list of values to add to the array.</param>
  246. /// <returns>The array (so method calls can be chained).</returns>
  247. public override BsonArray AddRange(IEnumerable<long> values)
  248. {
  249. throw new NotSupportedException("RawBsonArray instances are immutable.");
  250. }
  251. /// <summary>
  252. /// Adds multiple elements to the array.
  253. /// </summary>
  254. /// <param name="values">A list of values to add to the array.</param>
  255. /// <returns>The array (so method calls can be chained).</returns>
  256. public override BsonArray AddRange(IEnumerable<ObjectId> values)
  257. {
  258. throw new NotSupportedException("RawBsonArray instances are immutable.");
  259. }
  260. /// <summary>
  261. /// Adds multiple elements to the array.
  262. /// </summary>
  263. /// <param name="values">A list of values to add to the array.</param>
  264. /// <returns>The array (so method calls can be chained).</returns>
  265. public override BsonArray AddRange(IEnumerable<string> values)
  266. {
  267. throw new NotSupportedException("RawBsonArray instances are immutable.");
  268. }
  269. /// <summary>
  270. /// Adds multiple elements to the array.
  271. /// </summary>
  272. /// <param name="values">A list of values to add to the array.</param>
  273. /// <returns>The array (so method calls can be chained).</returns>
  274. public override BsonArray AddRange(IEnumerable values)
  275. {
  276. throw new NotSupportedException("RawBsonArray instances are immutable.");
  277. }
  278. /// <summary>
  279. /// Creates a shallow clone of the array (see also DeepClone).
  280. /// </summary>
  281. /// <returns>A shallow clone of the array.</returns>
  282. public override BsonValue Clone()
  283. {
  284. return new RawBsonArray(CloneSlice());
  285. }
  286. /// <summary>
  287. /// Clears the array.
  288. /// </summary>
  289. public override void Clear()
  290. {
  291. throw new NotSupportedException("RawBsonArray instances are immutable.");
  292. }
  293. /// <summary>
  294. /// Tests whether the array contains a value.
  295. /// </summary>
  296. /// <param name="value">The value to test for.</param>
  297. /// <returns>True if the array contains the value.</returns>
  298. public override bool Contains(BsonValue value)
  299. {
  300. ThrowIfDisposed();
  301. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
  302. {
  303. bsonReader.ReadStartDocument();
  304. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  305. {
  306. bsonReader.SkipName();
  307. if (DeserializeBsonValue(bsonReader).Equals(value))
  308. {
  309. return true;
  310. }
  311. }
  312. bsonReader.ReadEndDocument();
  313. return false;
  314. }
  315. }
  316. /// <summary>
  317. /// Copies elements from this array to another array.
  318. /// </summary>
  319. /// <param name="array">The other array.</param>
  320. /// <param name="arrayIndex">The zero based index of the other array at which to start copying.</param>
  321. public override void CopyTo(BsonValue[] array, int arrayIndex)
  322. {
  323. ThrowIfDisposed();
  324. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
  325. {
  326. bsonReader.ReadStartDocument();
  327. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  328. {
  329. bsonReader.SkipName();
  330. array[arrayIndex++] = DeserializeBsonValue(bsonReader);
  331. }
  332. bsonReader.ReadEndDocument();
  333. }
  334. }
  335. /// <summary>
  336. /// Copies elements from this array to another array as raw values (see BsonValue.RawValue).
  337. /// </summary>
  338. /// <param name="array">The other array.</param>
  339. /// <param name="arrayIndex">The zero based index of the other array at which to start copying.</param>
  340. [Obsolete("Use ToArray or ToList instead.")]
  341. public override void CopyTo(object[] array, int arrayIndex)
  342. {
  343. ThrowIfDisposed();
  344. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
  345. {
  346. bsonReader.ReadStartDocument();
  347. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  348. {
  349. bsonReader.SkipName();
  350. array[arrayIndex++] = DeserializeBsonValue(bsonReader).RawValue;
  351. }
  352. bsonReader.ReadEndDocument();
  353. }
  354. }
  355. /// <summary>
  356. /// Creates a deep clone of the array (see also Clone).
  357. /// </summary>
  358. /// <returns>A deep clone of the array.</returns>
  359. public override BsonValue DeepClone()
  360. {
  361. ThrowIfDisposed();
  362. return new RawBsonArray(CloneSlice());
  363. }
  364. /// <summary>
  365. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  366. /// </summary>
  367. public void Dispose()
  368. {
  369. Dispose(true);
  370. GC.SuppressFinalize(this);
  371. }
  372. /// <summary>
  373. /// Gets an enumerator that can enumerate the elements of the array.
  374. /// </summary>
  375. /// <returns>An enumerator.</returns>
  376. public override IEnumerator<BsonValue> GetEnumerator()
  377. {
  378. ThrowIfDisposed();
  379. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
  380. {
  381. bsonReader.ReadStartDocument();
  382. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  383. {
  384. bsonReader.SkipName();
  385. yield return DeserializeBsonValue(bsonReader);
  386. }
  387. bsonReader.ReadEndDocument();
  388. }
  389. }
  390. /// <summary>
  391. /// Gets the index of a value in the array.
  392. /// </summary>
  393. /// <param name="value">The value to search for.</param>
  394. /// <returns>The zero based index of the value (or -1 if not found).</returns>
  395. public override int IndexOf(BsonValue value)
  396. {
  397. return IndexOf(value, 0, int.MaxValue);
  398. }
  399. /// <summary>
  400. /// Gets the index of a value in the array.
  401. /// </summary>
  402. /// <param name="value">The value to search for.</param>
  403. /// <param name="index">The zero based index at which to start the search.</param>
  404. /// <returns>The zero based index of the value (or -1 if not found).</returns>
  405. public override int IndexOf(BsonValue value, int index)
  406. {
  407. return IndexOf(value, index, int.MaxValue);
  408. }
  409. /// <summary>
  410. /// Gets the index of a value in the array.
  411. /// </summary>
  412. /// <param name="value">The value to search for.</param>
  413. /// <param name="index">The zero based index at which to start the search.</param>
  414. /// <param name="count">The number of elements to search.</param>
  415. /// <returns>The zero based index of the value (or -1 if not found).</returns>
  416. public override int IndexOf(BsonValue value, int index, int count)
  417. {
  418. ThrowIfDisposed();
  419. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
  420. {
  421. bsonReader.ReadStartDocument();
  422. var i = 0;
  423. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  424. {
  425. bsonReader.SkipName();
  426. if (i >= index)
  427. {
  428. if (count == 0)
  429. {
  430. return -1;
  431. }
  432. if (DeserializeBsonValue(bsonReader).Equals(value))
  433. {
  434. return i;
  435. }
  436. count--;
  437. }
  438. else
  439. {
  440. bsonReader.SkipValue();
  441. }
  442. i++;
  443. }
  444. bsonReader.ReadEndDocument();
  445. return -1;
  446. }
  447. }
  448. /// <summary>
  449. /// Inserts a new value into the array.
  450. /// </summary>
  451. /// <param name="index">The zero based index at which to insert the new value.</param>
  452. /// <param name="value">The new value.</param>
  453. public override void Insert(int index, BsonValue value)
  454. {
  455. throw new NotSupportedException("RawBsonArray instances are immutable.");
  456. }
  457. /// <summary>
  458. /// Removes the first occurrence of a value from the array.
  459. /// </summary>
  460. /// <param name="value">The value to remove.</param>
  461. /// <returns>True if the value was removed.</returns>
  462. public override bool Remove(BsonValue value)
  463. {
  464. throw new NotSupportedException("RawBsonArray instances are immutable.");
  465. }
  466. /// <summary>
  467. /// Removes an element from the array.
  468. /// </summary>
  469. /// <param name="index">The zero based index of the element to remove.</param>
  470. public override void RemoveAt(int index)
  471. {
  472. throw new NotSupportedException("RawBsonArray instances are immutable.");
  473. }
  474. /// <summary>
  475. /// Converts the BsonArray to an array of BsonValues.
  476. /// </summary>
  477. /// <returns>An array of BsonValues.</returns>
  478. public override BsonValue[] ToArray()
  479. {
  480. ThrowIfDisposed();
  481. return Values.ToArray();
  482. }
  483. /// <summary>
  484. /// Converts the BsonArray to a list of BsonValues.
  485. /// </summary>
  486. /// <returns>A list of BsonValues.</returns>
  487. public override List<BsonValue> ToList()
  488. {
  489. ThrowIfDisposed();
  490. return Values.ToList();
  491. }
  492. /// <summary>
  493. /// Returns a string representation of the array.
  494. /// </summary>
  495. /// <returns>A string representation of the array.</returns>
  496. public override string ToString()
  497. {
  498. ThrowIfDisposed();
  499. var parts = Values.Select(v => v.ToString()).ToArray();
  500. return string.Format("[{0}]", string.Join(", ", parts));
  501. }
  502. // protected methods
  503. /// <summary>
  504. /// Releases unmanaged and - optionally - managed resources.
  505. /// </summary>
  506. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  507. protected virtual void Dispose(bool disposing)
  508. {
  509. if (!_disposed)
  510. {
  511. if (disposing)
  512. {
  513. if (_slice != null)
  514. {
  515. _slice.Dispose();
  516. _slice = null;
  517. }
  518. if (_disposableItems != null)
  519. {
  520. _disposableItems.ForEach(x => x.Dispose());
  521. _disposableItems = null;
  522. }
  523. }
  524. _disposed = true;
  525. }
  526. }
  527. /// <summary>
  528. /// Throws if disposed.
  529. /// </summary>
  530. /// <exception cref="System.ObjectDisposedException"></exception>
  531. protected void ThrowIfDisposed()
  532. {
  533. if (_disposed)
  534. {
  535. throw new ObjectDisposedException(GetType().Name);
  536. }
  537. }
  538. // private methods
  539. private IByteBuffer CloneSlice()
  540. {
  541. return _slice.GetSlice(0, _slice.Length);
  542. }
  543. private RawBsonArray DeserializeRawBsonArray(BsonBinaryReader bsonReader)
  544. {
  545. var slice = bsonReader.ReadRawBsonArray();
  546. var nestedArray = new RawBsonArray(slice);
  547. _disposableItems.Add(nestedArray);
  548. return nestedArray;
  549. }
  550. private RawBsonDocument DeserializeRawBsonDocument(BsonBinaryReader bsonReader)
  551. {
  552. var slice = bsonReader.ReadRawBsonDocument();
  553. var nestedDocument = new RawBsonDocument(slice);
  554. _disposableItems.Add(nestedDocument);
  555. return nestedDocument;
  556. }
  557. private BsonValue DeserializeBsonValue(BsonBinaryReader bsonReader)
  558. {
  559. switch (bsonReader.GetCurrentBsonType())
  560. {
  561. case BsonType.Array: return DeserializeRawBsonArray(bsonReader);
  562. case BsonType.Document: return DeserializeRawBsonDocument(bsonReader);
  563. default: return (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
  564. }
  565. }
  566. }
  567. }