InputBufferChunkSource.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright 2013-2015 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. namespace MongoDB.Bson.IO
  17. {
  18. /// <summary>
  19. /// Represents a source of chunks optimized for input buffers.
  20. /// </summary>
  21. public sealed class InputBufferChunkSource : IBsonChunkSource
  22. {
  23. // constants
  24. const int DefaultMaxChunkSize = 1 * 1024 * 1024;
  25. const int DefaultMaxUnpooledChunkSize = 4 * 1024;
  26. const int DefaultMinChunkSize = 16 * 1024;
  27. // fields
  28. private readonly IBsonChunkSource _baseSource;
  29. private bool _disposed;
  30. private readonly int _maxChunkSize;
  31. private readonly int _maxUnpooledChunkSize;
  32. private readonly int _minChunkSize;
  33. // constructors
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="InputBufferChunkSource"/> class.
  36. /// </summary>
  37. /// <param name="baseSource">The chunk source.</param>
  38. /// <param name="maxUnpooledChunkSize">The maximum size of an unpooled chunk.</param>
  39. /// <param name="minChunkSize">The minimum size of a chunk.</param>
  40. /// <param name="maxChunkSize">The maximum size of a chunk.</param>
  41. public InputBufferChunkSource(
  42. IBsonChunkSource baseSource,
  43. int maxUnpooledChunkSize = DefaultMaxUnpooledChunkSize,
  44. int minChunkSize = DefaultMinChunkSize,
  45. int maxChunkSize = DefaultMaxChunkSize)
  46. {
  47. if (baseSource == null)
  48. {
  49. throw new ArgumentNullException("baseSource");
  50. }
  51. if (maxUnpooledChunkSize < 0)
  52. {
  53. throw new ArgumentOutOfRangeException("maxUnpooledChunkSize");
  54. }
  55. if (minChunkSize <= 0)
  56. {
  57. throw new ArgumentOutOfRangeException("minChunkSize");
  58. }
  59. if (maxChunkSize <= 0)
  60. {
  61. throw new ArgumentOutOfRangeException("maxChunkSize");
  62. }
  63. if (!PowerOf2.IsPowerOf2(minChunkSize))
  64. {
  65. throw new ArgumentException("minChunkSize is not a power of 2.", "minChunkSize");
  66. }
  67. if (!PowerOf2.IsPowerOf2(maxChunkSize))
  68. {
  69. throw new ArgumentException("maxChunkSize is not a power of 2.", "maxChunkSize");
  70. }
  71. if (maxChunkSize < minChunkSize)
  72. {
  73. throw new ArgumentException("maxChunkSize is less than minChunkSize", "maxChunkSize");
  74. }
  75. _baseSource = baseSource;
  76. _maxUnpooledChunkSize = maxUnpooledChunkSize;
  77. _minChunkSize = minChunkSize;
  78. _maxChunkSize = maxChunkSize;
  79. }
  80. // properties
  81. /// <summary>
  82. /// Gets the base source.
  83. /// </summary>
  84. /// <value>
  85. /// The base source.
  86. /// </value>
  87. public IBsonChunkSource BaseSource
  88. {
  89. get
  90. {
  91. ThrowIfDisposed();
  92. return _baseSource;
  93. }
  94. }
  95. /// <summary>
  96. /// Gets the maximum size of a chunk.
  97. /// </summary>
  98. /// <value>
  99. /// The maximum size of a chunk.
  100. /// </value>
  101. public int MaxChunkSize
  102. {
  103. get { return _maxChunkSize; }
  104. }
  105. /// <summary>
  106. /// Gets the minimum size of a chunk.
  107. /// </summary>
  108. /// <value>
  109. /// The minimum size of a chunk.
  110. /// </value>
  111. public int MinChunkSize
  112. {
  113. get { return _minChunkSize; }
  114. }
  115. /// <summary>
  116. /// Gets the maximum size of an unpooled chunk.
  117. /// </summary>
  118. /// <value>
  119. /// The maximum size of an unpooled chunk.
  120. /// </value>
  121. public int MaxUnpooledChunkSize
  122. {
  123. get { return _maxUnpooledChunkSize; }
  124. }
  125. // methods
  126. /// <inheritdoc/>
  127. public void Dispose()
  128. {
  129. _disposed = true;
  130. }
  131. /// <inheritdoc/>
  132. public IBsonChunk GetChunk(int requestedSize)
  133. {
  134. if (requestedSize <= 0)
  135. {
  136. throw new ArgumentOutOfRangeException("requestedSize");
  137. }
  138. ThrowIfDisposed();
  139. if (requestedSize <= _maxUnpooledChunkSize)
  140. {
  141. return new ByteArrayChunk(requestedSize);
  142. }
  143. var powerOf2Size = PowerOf2.RoundUpToPowerOf2(requestedSize);
  144. if (powerOf2Size - requestedSize > _minChunkSize)
  145. {
  146. powerOf2Size = powerOf2Size / 2;
  147. }
  148. var chunkSize = Math.Max(Math.Min(powerOf2Size, _maxChunkSize), _minChunkSize);
  149. return _baseSource.GetChunk(chunkSize);
  150. }
  151. private void ThrowIfDisposed()
  152. {
  153. if (_disposed)
  154. {
  155. throw new ObjectDisposedException(GetType().Name);
  156. }
  157. }
  158. }
  159. }