LazyBsonDocument.cs 6.9 KB

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