LazyBsonDocument.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 System.Linq;
  20. using MongoDB.Bson.IO;
  21. using MongoDB.Bson.Serialization;
  22. using MongoDB.Bson.Serialization.Attributes;
  23. using MongoDB.Bson.Serialization.Serializers;
  24. namespace MongoDB.Bson
  25. {
  26. /// <summary>
  27. /// Represents a BSON document that is deserialized lazily.
  28. /// </summary>
  29. [Serializable]
  30. [BsonSerializer(typeof(LazyBsonDocumentSerializer))]
  31. public class LazyBsonDocument : MaterializedOnDemandBsonDocument
  32. {
  33. // private fields
  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="LazyBsonDocument"/> class.
  40. /// </summary>
  41. /// <param name="slice">The slice.</param>
  42. /// <exception cref="System.ArgumentNullException">slice</exception>
  43. /// <exception cref="System.ArgumentException">LazyBsonDocument cannot be used with an IByteBuffer that needs disposing.</exception>
  44. public LazyBsonDocument(IByteBuffer slice)
  45. {
  46. if (slice == null)
  47. {
  48. throw new ArgumentNullException("slice");
  49. }
  50. _slice = slice;
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="LazyBsonDocument"/> class.
  54. /// </summary>
  55. /// <param name="bytes">The bytes.</param>
  56. public LazyBsonDocument(byte[] bytes)
  57. : this(new ByteArrayBuffer(bytes, 0, bytes.Length, true))
  58. {
  59. }
  60. // public properties
  61. /// <summary>
  62. /// Gets the slice.
  63. /// </summary>
  64. /// <value>
  65. /// The slice.
  66. /// </value>
  67. public IByteBuffer Slice
  68. {
  69. get { return _slice; }
  70. }
  71. /// <summary>
  72. /// Creates a shallow clone of the document (see also DeepClone).
  73. /// </summary>
  74. /// <returns>
  75. /// A shallow clone of the document.
  76. /// </returns>
  77. public override BsonValue Clone()
  78. {
  79. if (_slice != null)
  80. {
  81. return new LazyBsonDocument(CloneSlice());
  82. }
  83. else
  84. {
  85. return base.Clone();
  86. }
  87. }
  88. /// <summary>
  89. /// Creates a deep clone of the document (see also Clone).
  90. /// </summary>
  91. /// <returns>
  92. /// A deep clone of the document.
  93. /// </returns>
  94. public override BsonValue DeepClone()
  95. {
  96. if (_slice != null)
  97. {
  98. return new LazyBsonDocument(CloneSlice());
  99. }
  100. else
  101. {
  102. return base.DeepClone();
  103. }
  104. }
  105. // protected methods
  106. /// <summary>
  107. /// Releases unmanaged and - optionally - managed resources.
  108. /// </summary>
  109. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  110. protected override void Dispose(bool disposing)
  111. {
  112. if (!IsDisposed)
  113. {
  114. if (disposing)
  115. {
  116. if (_slice != null)
  117. {
  118. _slice.Dispose();
  119. _slice = null;
  120. }
  121. if (_disposableItems != null)
  122. {
  123. _disposableItems.ForEach(x => x.Dispose());
  124. _disposableItems = null;
  125. }
  126. }
  127. }
  128. base.Dispose(disposing);
  129. }
  130. /// <summary>
  131. /// Materializes the BsonDocument.
  132. /// </summary>
  133. /// <returns>The materialized elements.</returns>
  134. protected override IEnumerable<BsonElement> Materialize()
  135. {
  136. return MaterializeThisLevel();
  137. }
  138. /// <summary>
  139. /// Informs subclasses that the Materialize process completed so they can free any resources related to the unmaterialized state.
  140. /// </summary>
  141. protected override void MaterializeCompleted()
  142. {
  143. var slice = _slice;
  144. _slice = null;
  145. slice.Dispose();
  146. }
  147. // private methods
  148. private IByteBuffer CloneSlice()
  149. {
  150. return _slice.GetSlice(0, _slice.Length);
  151. }
  152. private LazyBsonArray DeserializeLazyBsonArray(BsonBinaryReader bsonReader)
  153. {
  154. var slice = bsonReader.ReadRawBsonArray();
  155. var nestedArray = new LazyBsonArray(slice);
  156. _disposableItems.Add(nestedArray);
  157. return nestedArray;
  158. }
  159. private LazyBsonDocument DeserializeLazyBsonDocument(BsonBinaryReader bsonReader)
  160. {
  161. var slice = bsonReader.ReadRawBsonDocument();
  162. var nestedDocument = new LazyBsonDocument(slice);
  163. _disposableItems.Add(nestedDocument);
  164. return nestedDocument;
  165. }
  166. private IEnumerable<BsonElement> MaterializeThisLevel()
  167. {
  168. var elements = new List<BsonElement>();
  169. var readerSettings = _readerSettings.Clone();
  170. readerSettings.MaxDocumentSize = _slice.Length;
  171. using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, readerSettings))
  172. {
  173. bsonReader.ReadStartDocument();
  174. BsonType bsonType;
  175. while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
  176. {
  177. var name = bsonReader.ReadName();
  178. BsonValue value;
  179. switch (bsonType)
  180. {
  181. case BsonType.Array: value = DeserializeLazyBsonArray(bsonReader); break;
  182. case BsonType.Document: value = DeserializeLazyBsonDocument(bsonReader); break;
  183. default: value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); break;
  184. }
  185. elements.Add(new BsonElement(name, value));
  186. }
  187. bsonReader.ReadEndDocument();
  188. }
  189. return elements;
  190. }
  191. }
  192. }