MaterializedOnDemandBsonArray.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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.IO;
  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 a BSON array that is not materialized until you start using it.
  27. /// </summary>
  28. [Serializable]
  29. public abstract class MaterializedOnDemandBsonArray : BsonArray, IDisposable
  30. {
  31. // private fields
  32. private bool _disposed;
  33. private bool _isMaterialized;
  34. // constructors
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="MaterializedOnDemandBsonArray"/> class.
  37. /// </summary>
  38. protected MaterializedOnDemandBsonArray()
  39. {
  40. }
  41. // public properties
  42. /// <summary>
  43. /// Gets or sets the total number of elements the internal data structure can hold without resizing.
  44. /// </summary>
  45. public override int Capacity
  46. {
  47. get
  48. {
  49. EnsureIsMaterialized();
  50. return base.Capacity;
  51. }
  52. set
  53. {
  54. EnsureIsMaterialized();
  55. base.Capacity = value;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets the count of array elements.
  60. /// </summary>
  61. public override int Count
  62. {
  63. get
  64. {
  65. EnsureIsMaterialized();
  66. return base.Count;
  67. }
  68. }
  69. /// <summary>
  70. /// Gets a value indicating whether this instance is disposed.
  71. /// </summary>
  72. /// <value>
  73. /// <c>true</c> if this instance is disposed; otherwise, <c>false</c>.
  74. /// </value>
  75. public bool IsDisposed
  76. {
  77. get { return _disposed; }
  78. }
  79. /// <summary>
  80. /// Gets a value indicating whether this instance is materialized.
  81. /// </summary>
  82. /// <value>
  83. /// <c>true</c> if this instance is materialized; otherwise, <c>false</c>.
  84. /// </value>
  85. public bool IsMaterialized
  86. {
  87. get { return _isMaterialized; }
  88. }
  89. /// <summary>
  90. /// Gets the array elements as raw values (see BsonValue.RawValue).
  91. /// </summary>
  92. [Obsolete("Use ToArray to ToList instead.")]
  93. public override IEnumerable<object> RawValues
  94. {
  95. get
  96. {
  97. EnsureIsMaterialized();
  98. return base.RawValues;
  99. }
  100. }
  101. /// <summary>
  102. /// Gets the array elements.
  103. /// </summary>
  104. public override IEnumerable<BsonValue> Values
  105. {
  106. get
  107. {
  108. EnsureIsMaterialized();
  109. return base.Values;
  110. }
  111. }
  112. // public indexers
  113. /// <summary>
  114. /// Gets or sets a value by position.
  115. /// </summary>
  116. /// <param name="index">The position.</param>
  117. /// <returns>The value.</returns>
  118. public override BsonValue this[int index]
  119. {
  120. get
  121. {
  122. EnsureIsMaterialized();
  123. return base[index];
  124. }
  125. set
  126. {
  127. EnsureIsMaterialized();
  128. base[index] = value;
  129. }
  130. }
  131. // public methods
  132. /// <summary>
  133. /// Adds an element to the array.
  134. /// </summary>
  135. /// <param name="value">The value to add to the array.</param>
  136. /// <returns>The array (so method calls can be chained).</returns>
  137. public override BsonArray Add(BsonValue value)
  138. {
  139. EnsureIsMaterialized();
  140. return base.Add(value);
  141. }
  142. /// <summary>
  143. /// Adds multiple elements to the array.
  144. /// </summary>
  145. /// <param name="values">A list of values to add to the array.</param>
  146. /// <returns>The array (so method calls can be chained).</returns>
  147. public override BsonArray AddRange(IEnumerable<bool> values)
  148. {
  149. EnsureIsMaterialized();
  150. return base.AddRange(values);
  151. }
  152. /// <summary>
  153. /// Adds multiple elements to the array.
  154. /// </summary>
  155. /// <param name="values">A list of values to add to the array.</param>
  156. /// <returns>The array (so method calls can be chained).</returns>
  157. public override BsonArray AddRange(IEnumerable<BsonValue> values)
  158. {
  159. EnsureIsMaterialized();
  160. return base.AddRange(values);
  161. }
  162. /// <summary>
  163. /// Adds multiple elements to the array.
  164. /// </summary>
  165. /// <param name="values">A list of values to add to the array.</param>
  166. /// <returns>The array (so method calls can be chained).</returns>
  167. public override BsonArray AddRange(IEnumerable<DateTime> values)
  168. {
  169. EnsureIsMaterialized();
  170. return base.AddRange(values);
  171. }
  172. /// <summary>
  173. /// Adds multiple elements to the array.
  174. /// </summary>
  175. /// <param name="values">A list of values to add to the array.</param>
  176. /// <returns>The array (so method calls can be chained).</returns>
  177. public override BsonArray AddRange(IEnumerable<double> values)
  178. {
  179. EnsureIsMaterialized();
  180. return base.AddRange(values);
  181. }
  182. /// <summary>
  183. /// Adds multiple elements to the array.
  184. /// </summary>
  185. /// <param name="values">A list of values to add to the array.</param>
  186. /// <returns>The array (so method calls can be chained).</returns>
  187. public override BsonArray AddRange(IEnumerable<int> values)
  188. {
  189. EnsureIsMaterialized();
  190. return base.AddRange(values);
  191. }
  192. /// <summary>
  193. /// Adds multiple elements to the array.
  194. /// </summary>
  195. /// <param name="values">A list of values to add to the array.</param>
  196. /// <returns>The array (so method calls can be chained).</returns>
  197. public override BsonArray AddRange(IEnumerable<long> values)
  198. {
  199. EnsureIsMaterialized();
  200. return base.AddRange(values);
  201. }
  202. /// <summary>
  203. /// Adds multiple elements to the array.
  204. /// </summary>
  205. /// <param name="values">A list of values to add to the array.</param>
  206. /// <returns>The array (so method calls can be chained).</returns>
  207. public override BsonArray AddRange(IEnumerable<ObjectId> values)
  208. {
  209. EnsureIsMaterialized();
  210. return base.AddRange(values);
  211. }
  212. /// <summary>
  213. /// Adds multiple elements to the array.
  214. /// </summary>
  215. /// <param name="values">A list of values to add to the array.</param>
  216. /// <returns>The array (so method calls can be chained).</returns>
  217. public override BsonArray AddRange(IEnumerable<string> values)
  218. {
  219. EnsureIsMaterialized();
  220. return base.AddRange(values);
  221. }
  222. /// <summary>
  223. /// Adds multiple elements to the array.
  224. /// </summary>
  225. /// <param name="values">A list of values to add to the array.</param>
  226. /// <returns>The array (so method calls can be chained).</returns>
  227. public override BsonArray AddRange(IEnumerable values)
  228. {
  229. EnsureIsMaterialized();
  230. return base.AddRange(values);
  231. }
  232. /// <summary>
  233. /// Clears the array.
  234. /// </summary>
  235. public override void Clear()
  236. {
  237. EnsureIsMaterialized();
  238. base.Clear();
  239. }
  240. /// <summary>
  241. /// Creates a shallow clone of the array (see also DeepClone).
  242. /// </summary>
  243. /// <returns>
  244. /// A shallow clone of the array.
  245. /// </returns>
  246. public override BsonValue Clone()
  247. {
  248. EnsureIsMaterialized();
  249. return base.Clone();
  250. }
  251. /// <summary>
  252. /// Compares the array to another array.
  253. /// </summary>
  254. /// <param name="other">The other array.</param>
  255. /// <returns>A 32-bit signed integer that indicates whether this array is less than, equal to, or greather than the other.</returns>
  256. public override int CompareTo(BsonArray other)
  257. {
  258. EnsureIsMaterialized();
  259. return base.CompareTo(other);
  260. }
  261. /// <summary>
  262. /// Compares the array to another BsonValue.
  263. /// </summary>
  264. /// <param name="other">The other BsonValue.</param>
  265. /// <returns>A 32-bit signed integer that indicates whether this array is less than, equal to, or greather than the other BsonValue.</returns>
  266. public override int CompareTo(BsonValue other)
  267. {
  268. EnsureIsMaterialized();
  269. return base.CompareTo(other);
  270. }
  271. /// <summary>
  272. /// Tests whether the array contains a value.
  273. /// </summary>
  274. /// <param name="value">The value to test for.</param>
  275. /// <returns>True if the array contains the value.</returns>
  276. public override bool Contains(BsonValue value)
  277. {
  278. EnsureIsMaterialized();
  279. return base.Contains(value);
  280. }
  281. /// <summary>
  282. /// Copies elements from this array to another array.
  283. /// </summary>
  284. /// <param name="array">The other array.</param>
  285. /// <param name="arrayIndex">The zero based index of the other array at which to start copying.</param>
  286. public override void CopyTo(BsonValue[] array, int arrayIndex)
  287. {
  288. EnsureIsMaterialized();
  289. base.CopyTo(array, arrayIndex);
  290. }
  291. /// <summary>
  292. /// Copies elements from this array to another array as raw values (see BsonValue.RawValue).
  293. /// </summary>
  294. /// <param name="array">The other array.</param>
  295. /// <param name="arrayIndex">The zero based index of the other array at which to start copying.</param>
  296. [Obsolete("Use ToArray or ToList instead.")]
  297. public override void CopyTo(object[] array, int arrayIndex)
  298. {
  299. EnsureIsMaterialized();
  300. base.CopyTo(array, arrayIndex);
  301. }
  302. /// <summary>
  303. /// Creates a deep clone of the array (see also Clone).
  304. /// </summary>
  305. /// <returns>
  306. /// A deep clone of the array.
  307. /// </returns>
  308. public override BsonValue DeepClone()
  309. {
  310. EnsureIsMaterialized();
  311. return base.DeepClone();
  312. }
  313. /// <summary>
  314. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  315. /// </summary>
  316. public void Dispose()
  317. {
  318. Dispose(true);
  319. GC.SuppressFinalize(this);
  320. }
  321. /// <summary>
  322. /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
  323. /// </summary>
  324. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  325. /// <returns>
  326. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  327. /// </returns>
  328. public override bool Equals(object obj)
  329. {
  330. EnsureIsMaterialized();
  331. return base.Equals(obj);
  332. }
  333. /// <summary>
  334. /// Gets an enumerator that can enumerate the elements of the array.
  335. /// </summary>
  336. /// <returns>An enumerator.</returns>
  337. public override IEnumerator<BsonValue> GetEnumerator()
  338. {
  339. EnsureIsMaterialized();
  340. return base.GetEnumerator();
  341. }
  342. /// <summary>
  343. /// Gets the hash code.
  344. /// </summary>
  345. /// <returns>The hash code.</returns>
  346. public override int GetHashCode()
  347. {
  348. EnsureIsMaterialized();
  349. return base.GetHashCode();
  350. }
  351. /// <summary>
  352. /// Gets the index of a value in the array.
  353. /// </summary>
  354. /// <param name="value">The value to search for.</param>
  355. /// <returns>The zero based index of the value (or -1 if not found).</returns>
  356. public override int IndexOf(BsonValue value)
  357. {
  358. EnsureIsMaterialized();
  359. return base.IndexOf(value);
  360. }
  361. /// <summary>
  362. /// Gets the index of a value in the array.
  363. /// </summary>
  364. /// <param name="value">The value to search for.</param>
  365. /// <param name="index">The zero based index at which to start the search.</param>
  366. /// <returns>The zero based index of the value (or -1 if not found).</returns>
  367. public override int IndexOf(BsonValue value, int index)
  368. {
  369. EnsureIsMaterialized();
  370. return base.IndexOf(value, index);
  371. }
  372. /// <summary>
  373. /// Gets the index of a value in the array.
  374. /// </summary>
  375. /// <param name="value">The value to search for.</param>
  376. /// <param name="index">The zero based index at which to start the search.</param>
  377. /// <param name="count">The number of elements to search.</param>
  378. /// <returns>The zero based index of the value (or -1 if not found).</returns>
  379. public override int IndexOf(BsonValue value, int index, int count)
  380. {
  381. EnsureIsMaterialized();
  382. return base.IndexOf(value, index, count);
  383. }
  384. /// <summary>
  385. /// Inserts a new value into the array.
  386. /// </summary>
  387. /// <param name="index">The zero based index at which to insert the new value.</param>
  388. /// <param name="value">The new value.</param>
  389. public override void Insert(int index, BsonValue value)
  390. {
  391. EnsureIsMaterialized();
  392. base.Insert(index, value);
  393. }
  394. /// <summary>
  395. /// Removes the first occurrence of a value from the array.
  396. /// </summary>
  397. /// <param name="value">The value to remove.</param>
  398. /// <returns>True if the value was removed.</returns>
  399. public override bool Remove(BsonValue value)
  400. {
  401. EnsureIsMaterialized();
  402. return base.Remove(value);
  403. }
  404. /// <summary>
  405. /// Removes an element from the array.
  406. /// </summary>
  407. /// <param name="index">The zero based index of the element to remove.</param>
  408. public override void RemoveAt(int index)
  409. {
  410. EnsureIsMaterialized();
  411. base.RemoveAt(index);
  412. }
  413. /// <summary>
  414. /// Converts the BsonArray to an array of BsonValues.
  415. /// </summary>
  416. /// <returns>An array of BsonValues.</returns>
  417. public override BsonValue[] ToArray()
  418. {
  419. EnsureIsMaterialized();
  420. return base.ToArray();
  421. }
  422. /// <summary>
  423. /// Converts the BsonArray to a list of BsonValues.
  424. /// </summary>
  425. /// <returns>A list of BsonValues.</returns>
  426. public override List<BsonValue> ToList()
  427. {
  428. EnsureIsMaterialized();
  429. return base.ToList();
  430. }
  431. /// <summary>
  432. /// Returns a string representation of the array.
  433. /// </summary>
  434. /// <returns>A string representation of the array.</returns>
  435. public override string ToString()
  436. {
  437. EnsureIsMaterialized();
  438. return base.ToString();
  439. }
  440. // protected methods
  441. /// <summary>
  442. /// Releases unmanaged and - optionally - managed resources.
  443. /// </summary>
  444. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  445. protected virtual void Dispose(bool disposing)
  446. {
  447. _disposed = true;
  448. }
  449. /// <summary>
  450. /// Materializes the BsonArray.
  451. /// </summary>
  452. /// <returns>The materialized elements.</returns>
  453. protected abstract IEnumerable<BsonValue> Materialize();
  454. /// <summary>
  455. /// Informs subclasses that the Materialize process completed so they can free any resources related to the unmaterialized state.
  456. /// </summary>
  457. protected abstract void MaterializeCompleted();
  458. /// <summary>
  459. /// Throws if disposed.
  460. /// </summary>
  461. /// <exception cref="System.ObjectDisposedException"></exception>
  462. protected void ThrowIfDisposed()
  463. {
  464. if (_disposed)
  465. {
  466. throw new ObjectDisposedException(GetType().Name);
  467. }
  468. }
  469. // private methods
  470. private void EnsureIsMaterialized()
  471. {
  472. ThrowIfDisposed();
  473. if (!_isMaterialized)
  474. {
  475. var values = Materialize();
  476. try
  477. {
  478. _isMaterialized = true;
  479. base.AddRange(values);
  480. MaterializeCompleted();
  481. }
  482. catch
  483. {
  484. base.Clear();
  485. _isMaterialized = false;
  486. throw;
  487. }
  488. }
  489. }
  490. }
  491. }