hash_key.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #ifndef __HASH_KEY_H
  16. #define __HASH_KEY_H
  17. #include <dns/rrclass.h>
  18. #include <stdint.h>
  19. #include <string>
  20. #include <config.h>
  21. namespace isc {
  22. namespace nsas {
  23. /// \brief Hash Key
  24. ///
  25. /// In the nameserver address store, an object is placed into a hash table
  26. /// according to its key (name) and class.
  27. ///
  28. /// The key comprises two elements, a pointer to the start of a char string
  29. /// holding the data that describes the key and a length. This has been
  30. /// chosen over a std::string because:
  31. ///
  32. /// # The key may not be a string, it may be binary data
  33. /// # The overhead of creating std::string objects from such data.
  34. ///
  35. /// "key" is declared as "const char*" - rather than the more semantically
  36. /// correct "const uint8_t*" - simply because if std::strings are used, then
  37. /// the c_str function will return a "const char*".
  38. ///
  39. /// To avoid passing round three elements (key, key length, and class), they
  40. /// have been combined into this simple struct.
  41. struct HashKey {
  42. /// \brief Constructor
  43. ///
  44. /// Basic constructor to make the hash key.
  45. ///
  46. /// \param the_key Array of bytes for which key is to be constructed
  47. /// \param the_keylen Length of the byte array
  48. /// \param the_class_code Class of this entry
  49. HashKey(const char* the_key, uint32_t the_keylen,
  50. const isc::dns::RRClass& the_class_code) :
  51. key(the_key),
  52. keylen(the_keylen),
  53. class_code(the_class_code)
  54. {}
  55. /// \brief String Constructor
  56. ///
  57. /// Convenience constructor using a std::string.
  58. ///
  59. /// \param the_key Name to use as the key for the hash
  60. /// \param the_class_code Class of this entry
  61. HashKey(const std::string& the_key,
  62. const isc::dns::RRClass& the_class_code) :
  63. key(the_key.c_str()),
  64. keylen(the_key.size()),
  65. class_code(the_class_code)
  66. {}
  67. /// \brief Equality
  68. ///
  69. /// Convenience for unit testing, this matches two hash keys as being
  70. /// equal if the key strings match on a case-independent basis and the
  71. /// classes match.
  72. ///
  73. /// Note that the class strings may include null bytes; the match is
  74. /// done on a byte-by-byte basis, with codes in the range 'A' to 'Z' being
  75. /// mapped to 'a' to 'z'.
  76. ///
  77. /// \param other Hash key to compare against.
  78. ///
  79. /// \return true if the two hash key objects are the same.
  80. bool operator==(const isc::nsas::HashKey& other);
  81. const char* key; ///< Pointer to the start of the key string
  82. uint32_t keylen; ///< Length of the key string
  83. isc::dns::RRClass class_code; ///< Class associated with the key
  84. };
  85. } // namespace nsas
  86. } // namespace isc
  87. #endif // __HASH_KEY_H