BsonChunk.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. namespace MongoDB.Bson.IO
  17. {
  18. /// <summary>
  19. /// Represents a BSON buffer chunk.
  20. /// </summary>
  21. public class BsonChunk
  22. {
  23. // private fields
  24. private readonly byte[] _bytes;
  25. private readonly BsonChunkPool _chunkPool;
  26. private int _referenceCount;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="BsonChunk"/> class.
  30. /// </summary>
  31. /// <param name="bytes">The bytes.</param>
  32. /// <param name="chunkPool">The chunk pool.</param>
  33. /// <exception cref="System.ArgumentNullException">
  34. /// bytes
  35. /// or
  36. /// pool
  37. /// </exception>
  38. public BsonChunk(byte[] bytes, BsonChunkPool chunkPool)
  39. {
  40. if (bytes == null)
  41. {
  42. throw new ArgumentNullException("bytes");
  43. }
  44. if (chunkPool == null)
  45. {
  46. throw new ArgumentNullException("chunkPool");
  47. }
  48. _bytes = bytes;
  49. _chunkPool = chunkPool;
  50. }
  51. // public properties
  52. /// <summary>
  53. /// Gets the bytes.
  54. /// </summary>
  55. /// <value>
  56. /// The bytes.
  57. /// </value>
  58. public byte[] Bytes
  59. {
  60. get { return _bytes; }
  61. }
  62. /// <summary>
  63. /// Gets the reference count.
  64. /// </summary>
  65. /// <value>
  66. /// The reference count.
  67. /// </value>
  68. public int ReferenceCount
  69. {
  70. get { return _referenceCount; }
  71. }
  72. // public methods
  73. /// <summary>
  74. /// Decrements the reference count.
  75. /// </summary>
  76. /// <exception cref="BsonInternalException">Reference count is less than or equal to zero.</exception>
  77. public void DecrementReferenceCount()
  78. {
  79. if (_referenceCount <= 0)
  80. {
  81. throw new BsonInternalException("Reference count is less than or equal to zero.");
  82. }
  83. if (--_referenceCount == 0 && _chunkPool != null)
  84. {
  85. _chunkPool.ReleaseChunk(this);
  86. }
  87. }
  88. /// <summary>
  89. /// Increments the reference count.
  90. /// </summary>
  91. public void IncrementReferenceCount()
  92. {
  93. _referenceCount++;
  94. }
  95. }
  96. }