TrieNameDecoder.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Text;
  16. namespace MongoDB.Bson.IO
  17. {
  18. /// <summary>
  19. /// Represents a Trie-based name decoder that also provides a value.
  20. /// </summary>
  21. /// <typeparam name="TValue">The type of the value.</typeparam>
  22. public class TrieNameDecoder<TValue> : INameDecoder
  23. {
  24. // private fields
  25. private bool _found;
  26. private readonly BsonTrie<TValue> _trie;
  27. private TValue _value;
  28. // constructors
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="TrieNameDecoder{TValue}"/> class.
  31. /// </summary>
  32. /// <param name="trie">The trie.</param>
  33. public TrieNameDecoder(BsonTrie<TValue> trie)
  34. {
  35. _trie = trie;
  36. }
  37. // public properties
  38. /// <summary>
  39. /// Gets a value indicating whether this <see cref="TrieNameDecoder{TValue}"/> is found.
  40. /// </summary>
  41. /// <value>
  42. /// <c>true</c> if found; otherwise, <c>false</c>.
  43. /// </value>
  44. public bool Found
  45. {
  46. get { return _found; }
  47. }
  48. /// <summary>
  49. /// Gets the value.
  50. /// </summary>
  51. /// <value>
  52. /// The value.
  53. /// </value>
  54. public TValue Value
  55. {
  56. get { return _value; }
  57. }
  58. // public methods
  59. /// <summary>
  60. /// Reads the name.
  61. /// </summary>
  62. /// <param name="stream">The stream.</param>
  63. /// <param name="encoding">The encoding.</param>
  64. /// <returns>
  65. /// The name.
  66. /// </returns>
  67. public string Decode(BsonStream stream, UTF8Encoding encoding)
  68. {
  69. BsonTrieNode<TValue> node;
  70. var oldPosition = stream.Position;
  71. if (_trie.TryGetNode(stream, out node))
  72. {
  73. if (node.HasValue)
  74. {
  75. _found = true;
  76. _value = node.Value;
  77. return node.ElementName;
  78. }
  79. stream.Position = oldPosition;
  80. }
  81. _found = false;
  82. _value = default;
  83. return stream.ReadCString(encoding);
  84. }
  85. /// <summary>
  86. /// Informs the decoder of an already decoded name (so the decoder can change state if necessary).
  87. /// </summary>
  88. /// <param name="name">The name.</param>
  89. public void Inform(string name)
  90. {
  91. _found = _trie.TryGetValue(name, out _value);
  92. }
  93. }
  94. }