hash_key.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <stdint.h>
  18. #include <string>
  19. #include <config.h>
  20. namespace isc {
  21. namespace nsas {
  22. /// \brief Hash Key
  23. ///
  24. /// In the nameserver address store, an object is placed into a hash table
  25. /// according to its key (name) and class.
  26. ///
  27. /// The key comprises two elements, a pointer to the start of a char string
  28. /// holding the data that describes the key and a length. This has been
  29. /// chosen over a std::string because:
  30. ///
  31. /// # The key may not be a string, it may be binary data
  32. /// # The overhead of creating std::string objects from such data.
  33. ///
  34. /// "key" is declared as "const char*" - rather than the more semantically
  35. /// correct "const uint8_t*" - simply because if std::strings are used, then
  36. /// the c_str function will return a "const char*".
  37. ///
  38. /// To avoid passing round three elements (key, key length, and class), they
  39. /// have been combined into this simple struct.
  40. struct HashKey {
  41. /// \brief Constructor
  42. ///
  43. /// Basic constructor to make the hash key.
  44. ///
  45. /// \param the_key Array of bytes for which key is to be constructed
  46. /// \param the_keylen Length of the byte array
  47. /// \param the_class_code Class of this entry
  48. HashKey(const char* the_key, uint32_t the_keylen, uint16_t the_class_code) :
  49. key(the_key), keylen(the_keylen), class_code(the_class_code)
  50. {}
  51. /// \brief String Constructor
  52. ///
  53. /// Convenience constructor using a std::string.
  54. ///
  55. /// \param the_key Name to use as the key for the hash
  56. /// \param the_class_code Class of this entry
  57. HashKey(const std::string& the_key, uint16_t the_class_code) :
  58. key(the_key.c_str()), keylen(the_key.size()), class_code(the_class_code)
  59. {}
  60. /// \brief Equality
  61. ///
  62. /// Convenience for unit testing, this matches two hash keys as being
  63. /// equal if the key strings match on a case-independent basis and the
  64. /// classes match.
  65. ///
  66. /// Note that the class strings may include null bytes; the match is
  67. /// done on a byte-by-byte basis, with codes in the range 'A' to 'Z' being
  68. /// mapped to 'a' to 'z'.
  69. ///
  70. /// \param other Hash key to compare against.
  71. ///
  72. /// \return true if the two hash key objects are the same.
  73. bool operator==(const isc::nsas::HashKey& other);
  74. const char* key; ///< Pointer to the start of the key string
  75. uint32_t keylen; ///< Length of the key string
  76. uint16_t class_code; ///< Class associated with the key
  77. };
  78. } // namespace nsas
  79. } // namespace isc
  80. #endif // __HASH_KEY_H