Zero  0.1.0
buffer_pool_hashtable.hpp
Go to the documentation of this file.
1 #ifndef __SM_BUFFER_POOL_HASHTABLE_HPP
2 #define __SM_BUFFER_POOL_HASHTABLE_HPP
3 
4 #include "basics.h"
5 #include <cmath>
6 #include <limits>
7 #include <memory>
8 #include "junction/junction/ConcurrentMap_Leapfrog.h"
9 
10 namespace zero::buffer_pool {
11 
12  class Hashtable {
13  public:
14  Hashtable(bf_idx block_count) :
15  _hashtable(std::make_unique<
16  junction::ConcurrentMap_Leapfrog<PageID, atomic_bf_idx_pair*, HashtableKeyTraits>>(
17  static_cast<size_t>(std::pow(2, std::ceil(std::log2(block_count)))))) {};
18 
19  ~Hashtable() {};
20 
21  void erase(const PageID& pid) {
22  delete (_hashtable->erase(pid));
23  };
24 
25  void erase(PageID&& pid) {
26  delete (_hashtable->erase(pid));
27  };
28 
29  atomic_bf_idx_pair* lookupPair(const PageID& pid) const {
30  return _hashtable->get(pid);
31  };
32 
34  return _hashtable->get(pid);
35  };
36 
37  atomic_bf_idx* lookup(const PageID& pid) const {
38  atomic_bf_idx_pair* indexPair = _hashtable->get(pid);
39  if (indexPair) {
40  return &(indexPair->first);
41  } else {
42  return nullptr;
43  }
44  };
45 
46  atomic_bf_idx* lookup(PageID&& pid) const {
47  atomic_bf_idx_pair* indexPair = _hashtable->get(pid);
48  if (indexPair) {
49  return &(indexPair->first);
50  } else {
51  return nullptr;
52  }
53  };
54 
55  atomic_bf_idx* lookupParent(const PageID& pid) const {
56  atomic_bf_idx_pair* indexPair = _hashtable->get(pid);
57  if (indexPair) {
58  return &(indexPair->second);
59  } else {
60  return nullptr;
61  }
62  };
63 
65  atomic_bf_idx_pair* indexPair = _hashtable->get(pid);
66  if (indexPair) {
67  return &(indexPair->second);
68  } else {
69  return nullptr;
70  }
71  };
72 
73  bool tryInsert(const PageID& pid, atomic_bf_idx_pair* idx_pair) {
74  bool inserted = false;
75  auto mutator = _hashtable->insertOrFind(pid);
76  atomic_bf_idx_pair* value = mutator.getValue();
77  if (!value) {
78  delete (mutator.exchangeValue(idx_pair));
79  inserted = true;
80  }
81  return inserted;
82  }
83 
84  bool tryInsert(PageID&& pid, atomic_bf_idx_pair* indexPair) {
85  bool inserted = false;
86  auto mutator = _hashtable->insertOrFind(pid);
87  atomic_bf_idx_pair* value = mutator.getValue();
88  if (!value) {
89  delete (mutator.exchangeValue(indexPair));
90  inserted = true;
91  }
92  return inserted;
93  }
94 
95  private:
97  typedef PageID Key;
98 
99  typedef typename turf::util::BestFit<PageID>::Unsigned Hash;
100 
101  static const Key NullKey = Key(4294967295);
102 
103  static const Hash NullHash = Hash(2180083513);
104 
105  static Hash hash(PageID key) {
106  return turf::util::avalanche(Hash(key));
107  }
108 
109  static Key dehash(Hash hash) {
110  return (PageID)turf::util::deavalanche(hash);
111  }
112  };
113 
114  std::unique_ptr<junction::ConcurrentMap_Leapfrog<PageID, atomic_bf_idx_pair*, HashtableKeyTraits>> _hashtable;
115  };
116 }
117 
118 #endif // __SM_BUFFER_POOL_HASHTABLE_HPP
atomic_bf_idx * lookupParent(PageID &&pid) const
Definition: buffer_pool_hashtable.hpp:64
Hashtable(bf_idx block_count)
Definition: buffer_pool_hashtable.hpp:14
Definition: buffer_pool.hpp:34
atomic_bf_idx_pair * lookupPair(const PageID &pid) const
Definition: buffer_pool_hashtable.hpp:29
static const Hash NullHash
Definition: buffer_pool_hashtable.hpp:103
STL namespace.
bool tryInsert(const PageID &pid, atomic_bf_idx_pair *idx_pair)
Definition: buffer_pool_hashtable.hpp:73
std::pair< std::atomic< bf_idx >, std::atomic< bf_idx > > atomic_bf_idx_pair
Definition: basics.h:63
uint32_t bf_idx
Definition: basics.h:56
atomic_bf_idx * lookupParent(const PageID &pid) const
Definition: buffer_pool_hashtable.hpp:55
void erase(PageID &&pid)
Definition: buffer_pool_hashtable.hpp:25
atomic_bf_idx_pair * lookupPair(PageID &&pid) const
Definition: buffer_pool_hashtable.hpp:33
uint32_t PageID
Definition: basics.h:45
Definition: buffer_pool_hashtable.hpp:12
PageID Key
Definition: buffer_pool_hashtable.hpp:97
static const Key NullKey
Definition: buffer_pool_hashtable.hpp:101
turf::util::BestFit< PageID >::Unsigned Hash
Definition: buffer_pool_hashtable.hpp:99
Definition: buffer_pool_hashtable.hpp:96
bool tryInsert(PageID &&pid, atomic_bf_idx_pair *indexPair)
Definition: buffer_pool_hashtable.hpp:84
std::unique_ptr< junction::ConcurrentMap_Leapfrog< PageID, atomic_bf_idx_pair *, HashtableKeyTraits > > _hashtable
Definition: buffer_pool_hashtable.hpp:114
atomic_bf_idx * lookup(const PageID &pid) const
Definition: buffer_pool_hashtable.hpp:37
constexpr uint16_t log2(int_type n)
Definition: uniform_int_distribution.hpp:25
~Hashtable()
Definition: buffer_pool_hashtable.hpp:19
std::atomic< uint32_t > atomic_bf_idx
Definition: basics.h:58
void erase(const PageID &pid)
Definition: buffer_pool_hashtable.hpp:21
static Hash hash(PageID key)
Definition: buffer_pool_hashtable.hpp:105
atomic_bf_idx * lookup(PageID &&pid) const
Definition: buffer_pool_hashtable.hpp:46
static Key dehash(Hash hash)
Definition: buffer_pool_hashtable.hpp:109