hash_table.h 12 KB

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