hash_table.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #ifndef __HASH_TABLE_H
  15. #define __HASH_TABLE_H
  16. #include <list>
  17. #include <boost/shared_ptr.hpp>
  18. #include "locks.h"
  19. #include "hash.h"
  20. #include "hash_key.h"
  21. // Maximum key length if the maximum size of a DNS name
  22. #define MAX_KEY_LENGTH 255
  23. namespace isc {
  24. namespace nsas {
  25. /// \brief Hash Table Slot
  26. ///
  27. /// Describes the entry for the hash table. This is non-copyable (because
  28. /// the mutex is non-copyable), but we need to be able to copy it to initialize
  29. /// a vector of hash table slots. As the copy is only needed for
  30. /// initialization, and as there is no need to copy state when this happens, we
  31. /// cheat: the copy constructor constructs a newly initialized HashTableSlot and
  32. /// does not copy its argument.
  33. template <typename T>
  34. struct HashTableSlot {
  35. /// \brief Type definitions
  36. ///
  37. //@{
  38. typedef typename std::list<boost::shared_ptr<T> >::iterator iterator;
  39. ///< Iterator over elements with same hash
  40. typedef isc::locks::upgradable_mutex mutex_type;
  41. ///< Mutex protecting this slot
  42. //@}
  43. /// \brief Default Constructor
  44. HashTableSlot()
  45. {}
  46. /// \brief Copy Constructor
  47. ///
  48. /// ... which as noted in the class description does not copy.
  49. HashTableSlot(const HashTableSlot<T>&)
  50. { }
  51. public:
  52. mutex_type mutex_; ///< Protection mutex
  53. std::list<boost::shared_ptr<T> > list_; ///< List head
  54. };
  55. /// \brief Comparison Object Class
  56. ///
  57. /// The base class for a comparison object; this object is used to compare
  58. /// an object in the hash table with a key, and indicates whether the two
  59. /// match. All objects used for comparison in hash tables should be derived
  60. /// from this class.
  61. template <typename T>
  62. class HashTableCompare {
  63. public:
  64. /// \brief Constructor
  65. HashTableCompare(){}
  66. /// \brief virtual Destructor
  67. virtual ~HashTableCompare() {}
  68. /// \brief Comparison Function
  69. ///
  70. /// Compares an object against a name in the hash table and reports if the
  71. /// object's name is the same.
  72. ///
  73. /// \param object Pointer to the object
  74. /// \param key Key describing the object
  75. ///
  76. /// \return bool true of the name of the object is equal to the name given.
  77. virtual bool operator()(T* object, const HashKey& key) const = 0;
  78. };
  79. /// \brief Hash Table
  80. ///
  81. /// This class is an implementation of a hash table in which the zones and
  82. /// nameservers of the Nameserver Address Store are held.
  83. ///
  84. /// A special class has been written (rather than use an existing hash table
  85. /// class) to improve concurrency. Rather than lock the entire hash table when
  86. /// an object is added/removed/looked up, only the entry for a particular hash
  87. /// value is locked. To do this, each entry in the hash table is a pair of
  88. /// mutex/STL List; the mutex protects that particular list.
  89. ///
  90. /// \param T Class of object to be stored in the table.
  91. template <typename T>
  92. class HashTable {
  93. public:
  94. /// \brief Type Definitions
  95. ///
  96. //@{
  97. typedef typename
  98. isc::locks::sharable_lock<typename HashTableSlot<T>::mutex_type>
  99. sharable_lock; ///< Type for a scope-limited read-lock
  100. typedef typename
  101. isc::locks::scoped_lock<typename HashTableSlot<T>::mutex_type>
  102. scoped_lock; ///< Type for a scope-limited write-lock
  103. //@}
  104. /// \brief Constructor
  105. ///
  106. /// Initialises the hash table.
  107. ///
  108. /// \param CmpFn Compare function (or object) used to compare an object with
  109. /// to get the name to be used as a key in the table. The object should be
  110. /// created via a "new" as ownership passes to the hash table. The hash
  111. /// table will take the responsibility of deleting it.
  112. /// \param size Size of the hash table. For best result, this should be a
  113. /// prime although that is not checked. The default value is the size used
  114. /// in BIND-9 for its address database.
  115. HashTable(HashTableCompare<T>* cmp, uint32_t size = 1009);
  116. /// \brief Destructor
  117. ///
  118. virtual ~HashTable(){}
  119. /// \brief Get Entry
  120. ///
  121. /// Returns a shared_ptr object pointing to the table entry
  122. ///
  123. /// \param key Name of the object (and class). The hash of this is
  124. /// calculated and used to index the table.
  125. ///
  126. /// \return Shared pointer to the object or NULL if it is not there.
  127. boost::shared_ptr<T> get(const HashKey& key) {
  128. uint32_t index = hash_(key);
  129. sharable_lock lock(table_[index].mutex_);
  130. return getInternal(key, index);
  131. }
  132. /// \brief Remove Entry
  133. ///
  134. /// Remove the specified entry. The shared pointer to the object is
  135. /// destroyed, so if this is the last pointer, the object itself is also
  136. /// destroyed.
  137. ///
  138. /// \param key Name of the object (and class). The hash of this is
  139. /// calculated and used to index the table.
  140. ///
  141. /// \return true if the object was deleted, false if it was not found.
  142. bool remove(const HashKey& key);
  143. /// \brief Add Entry
  144. ///
  145. /// Adds the specified entry to the table. If there is an entry already
  146. /// there, it is either replaced or the addition fails, depending on the
  147. /// setting of the "replace" parameter.
  148. ///
  149. /// \param object Pointer to the object to be added. If the addition is
  150. /// successful, this object will have a shared pointer pointing to it; it
  151. /// should not be deleted by the caller.
  152. /// \param key Key to use to calculate the hash.
  153. /// \param replace If true, when an object is added and an object with the
  154. /// same name already exists, the existing object is replaced. If false,
  155. // the addition fails and a status is returned.
  156. /// \return true if the object was successfully added, false otherwise.
  157. bool add(boost::shared_ptr<T>& object, const HashKey& key,
  158. bool replace = false)
  159. {
  160. uint32_t index = hash_(key);
  161. scoped_lock lock(table_[index].mutex_);
  162. return addInternal(object, key, index, replace);
  163. }
  164. /**
  165. * \brief Attomicly lookup an entry or add a new one if it does not exist.
  166. *
  167. * Looks up an entry specified by key in the table. If it is not there,
  168. * it calls generator() and adds its result to the table under given key.
  169. * It is performed attomically to prevent race conditions.
  170. *
  171. * \param key The entry to lookup.
  172. * \param generator will be called when the item is not there. Its result
  173. * will be added and returned. The generator should return as soon
  174. * as possible, the slot is locked during its execution.
  175. * \return The boolean part of pair tells if the value was added (true
  176. * means new value, false looked up one). The other part is the
  177. * object, either found or created.
  178. * \todo This uses a scoped_lock, which does not allow sharing and is
  179. * used a lot in the code. It might turn out in future that it is a
  180. * problem and that most of the accesses is read only. In that case we
  181. * could split it to fast-slow path - first try to find it with
  182. * shared_lock. If it fails, lock by scoped_lock, try to find again (we
  183. * unlocked it, so it might have appeared) and if it still isn't there,
  184. * create it. Not implemented now as it might or might not help (it
  185. * could even slow it down) and the code would get more complicated.
  186. */
  187. template<class Generator>
  188. std::pair<bool, boost::shared_ptr<T> > getOrAdd(const HashKey& key,
  189. const Generator& generator)
  190. {
  191. uint32_t index = hash_(key);
  192. scoped_lock lock(table_[index].mutex_);
  193. boost::shared_ptr<T> result(getInternal(key, index));
  194. if (result) {
  195. return (std::pair<bool, boost::shared_ptr<T> >(false, result));
  196. } else {
  197. result = generator();
  198. addInternal(result, key, index);
  199. return (std::pair<bool, boost::shared_ptr<T> >(true, result));
  200. }
  201. }
  202. /// \brief Returns Size of Hash Table
  203. ///
  204. /// \return Size of hash table
  205. uint32_t tableSize() const {
  206. return table_.size();
  207. }
  208. protected:
  209. // Internal parts, expect to be already locked
  210. boost::shared_ptr<T> getInternal(const HashKey& key,
  211. uint32_t index);
  212. bool addInternal(boost::shared_ptr<T>& object, const HashKey& key,
  213. uint32_t index, bool replace = false);
  214. private:
  215. Hash hash_; ///< Hashing function
  216. std::vector<HashTableSlot<T> > table_; ///< The hash table itself
  217. boost::shared_ptr<HashTableCompare<T> > compare_; ///< Compare object
  218. };
  219. // Constructor
  220. template <typename T>
  221. HashTable<T>::HashTable(HashTableCompare<T>* compare, uint32_t size) :
  222. hash_(size, MAX_KEY_LENGTH), table_(size), compare_(compare)
  223. {}
  224. // Lookup an object in the table
  225. template <typename T>
  226. boost::shared_ptr<T> HashTable<T>::getInternal(const HashKey& key,
  227. uint32_t index)
  228. {
  229. // Locate the object.
  230. typename HashTableSlot<T>::iterator i;
  231. for (i = table_[index].list_.begin(); i != table_[index].list_.end(); ++i) {
  232. if ((*compare_)(i->get(), key)) {
  233. // Found it, so return the shared pointer object
  234. return (*i);
  235. }
  236. }
  237. // Did not find it, return an empty shared pointer object.
  238. return boost::shared_ptr<T>();
  239. }
  240. // Remove an entry from the hash table
  241. template <typename T>
  242. bool HashTable<T>::remove(const HashKey& key) {
  243. // Calculate the hash value
  244. uint32_t index = hash_(key);
  245. // Access to the elements of this hash slot are accessed under a mutex.
  246. // The mutex will be released when this object goes out of scope and is
  247. // destroyed.
  248. scoped_lock lock(table_[index].mutex_);
  249. // Now search this list to see if the element already exists.
  250. typename HashTableSlot<T>::iterator i;
  251. for (i = table_[index].list_.begin(); i != table_[index].list_.end(); ++i) {
  252. if ((*compare_)(i->get(), key)) {
  253. // Object found so delete it.
  254. table_[index].list_.erase(i);
  255. return true;;
  256. }
  257. }
  258. // When we get here, we know that there is no element with the key in the
  259. // list, so tell the caller.
  260. return false;
  261. }
  262. // Add an entry to the hash table
  263. template <typename T>
  264. bool HashTable<T>::addInternal(boost::shared_ptr<T>& object,
  265. const HashKey& key, uint32_t index, bool replace)
  266. {
  267. // Search this list to see if the element already exists.
  268. typename HashTableSlot<T>::iterator i;
  269. for (i = table_[index].list_.begin(); i != table_[index].list_.end(); ++i) {
  270. if ((*compare_)(i->get(), key)) {
  271. // Object found. If we are not allowed to replace the element,
  272. // return an error. Otherwise erase it from the list and exit the
  273. // loop.
  274. if (replace) {
  275. table_[index].list_.erase(i);
  276. break;
  277. }
  278. else {
  279. return false;
  280. }
  281. }
  282. }
  283. // When we get here, we know that there is no element with the key in the
  284. // list - in which case, add the new object.
  285. table_[index].list_.push_back(object);
  286. return true;
  287. }
  288. } // namespace nsas
  289. } // namespace isc
  290. #endif // __HASH_TABLE_H