TrieNameDecoder.cs 3.1 KB

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