hash_table.h 12 KB

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