The SwappyItems Key-Values Store
SwappyQueue.hpp
Go to the documentation of this file.
1 #ifndef __SWAPPY_QUEUE_HPP__
2 #define __SWAPPY_QUEUE_HPP__ 1
3 
4 #include <vector>
5 #include <inttypes.h> // uintX_t stuff
6 #include "SwappyItems.hpp"
7 
33 template <
34  class TKEY,
35  class TVALUE,
36 
37  int EACHFILE = 16384,
38  int OLDIES = 8,
39  int RAMSIZE = 3,
40  int BLOOMBITS = 4,
41  int MASKLENGTH = (2* 4*16384)
42 >
43 class SwappyQueue {
44 
45 public:
46  bool empty;
47 
48  struct Item {
49  TVALUE data;
50  uint64_t prio;
51  TKEY parent; // @todo programmer: please do not touch!?!
52  };
53 
54 private:
55 
57 
59  TKEY _headKey;
60 
61 public:
62 
63  SwappyQueue (void) {
64  empty = true;
65  _data = new Heap(42);
66  }
67 
68  ~SwappyQueue (void) {
69  delete(_data);
70  }
71 
80  bool get (TKEY key, Item & result) {
81  typename Heap::Data * p;
82  p = _data->get(key);
83  if (p == nullptr) return false;
84  result = p->first;
85  return true;
86  }
87 
96  bool top (TKEY & resultkey, Item & result) {
97  if (empty) {
98  return false;
99  } else {
100  resultkey = _headKey;
101  return get(_headKey, result);
102  }
103  }
104 
113  bool set (TKEY key, Item & item) {
114  typename Heap::Data * p = _data->get(key);
115 
116  if (p == nullptr) {
117  insert(key, item);
118  return true;
119  } else {
120  if (p->first.prio == item.prio) {
121  _data->set(key, item);
122  } else {
123  del(key);
124  insert(key, item);
125  }
126  return false;
127  }
128  }
129 
137  void del (TKEY key) {
138  typename Heap::statistic_s s = _data->getStatistic();
139  if (s.size == 0) return;
140 
141  if (s.size == 1) {
142  if (key == _headKey) {
143  _data->del(_headKey);
144  empty = true;
145  }
146  return;
147  }
148 
149  if (s.size > 1) {
150  typename Heap::Data * dummy1Pair;
151  typename Heap::Data * dummy2Pair = _data->get(key);
152 
153  if (dummy2Pair == nullptr) return;
154 
155  // = remember the datas from old element
156  Item item = dummy2Pair->first;
157  std::vector<TKEY> siblings(dummy2Pair->second);
158 
159  _data->del(key);
160 
161  std::vector<TKEY> newsiblings;
162  std::vector<TKEY> winners;
163  TKEY champion;
164 
165  if (key != _headKey) {
166  dummy1Pair = _data->get(item.parent);
167  // remove key from siblings of parent
168  for (auto n : dummy1Pair->second) {
169  if (n != key) newsiblings.push_back(n);
170  }
171  _data->set(item.parent, dummy1Pair->first, newsiblings);
172  }
173 
174  // build pairs (1. phase)
175  for (uint64_t i=0; i < siblings.size(); i+=2) {
176  if ((i+1) >= siblings.size()) {
177  // a single element ...
178  winners.push_back(siblings[i]);
179  } else {
180  dummy1Pair = _data->get(siblings[i]);
181  dummy2Pair = _data->get(siblings[i+1]);
182  if (dummy1Pair->first.prio < dummy2Pair->first.prio) {
183  winners.push_back(siblings[i]);
184  dummy2Pair->first.parent = siblings[i];
185  dummy1Pair->second.push_back(siblings[i+1]);
186  //_data->set(...); // hey, we use pointers. the data in _ramList should be accessable and changed!
187  } else {
188  winners.push_back(siblings[i+1]);
189  dummy1Pair->first.parent = siblings[i+1];
190  dummy2Pair->second.push_back(siblings[i]);
191  //_data->set(...); // hey, we use pointers. the data in _ramList should be accessable and changed!
192  }
193  }
194  }
195 
196  // build a single tree (2. phase)
197  if (winners.size() > 0) {
198  champion = winners[0];
199  dummy1Pair = _data->get(champion);
200 
201  for (uint64_t i=1; i < winners.size(); ++i) {
202  dummy2Pair = _data->get(winners[i]);
203  if (dummy2Pair->first.prio < dummy1Pair->first.prio) {
204  // we found a new head/champion
205  dummy2Pair->second.push_back(champion);
206  dummy1Pair->first.parent = winners[i];
207  champion = winners[i];
208  dummy1Pair = dummy2Pair;
209  } else {
210  dummy1Pair->second.push_back(winners[i]);
211  dummy2Pair->first.parent = champion;
212  }
213  }
214 
215  // may set a new head
216  if (key == _headKey) {
217  _headKey = champion; // the winner of all winners
218  dummy1Pair = _data->get(_headKey);
219  dummy1Pair->first.parent = _headKey; // no parent = link to its self!
220  } else {
221  dummy1Pair = _data->get(champion);
222  dummy1Pair->first.parent = item.parent;
223  dummy2Pair = _data->get(item.parent);
224  dummy2Pair->second.push_back(champion);
225  }
226  }
227 
228  }
229  }
230 
231 private:
232 
233  // key should not exist!!!
235  void insert (TKEY key, Item & item) {
236  typename Heap::statistic_s s = _data->getStatistic();
237  if (s.size == 0) {
238  _headKey = key;
239  item.parent = key;
240  _data->set(key, item, std::vector<TKEY>(0));
241  empty = false;
242  } else {
243  typename Heap::Data * headpair = _data->get(_headKey);
244 
245  if (item.prio < headpair->first.prio) {
246  item.parent = key;
247  std::vector<TKEY> siblings;
248  siblings.push_back(_headKey);
249  _data->set(key, item, siblings);
250 
251  headpair->first.parent = key;
252  _data->set(_headKey, headpair->first);
253 
254  _headKey = key;
255  } else {
256  item.parent = _headKey;
257  _data->set(key, item, std::vector<TKEY>(0));
258 
259  headpair->second.push_back(key);
260  _data->set(_headKey, headpair->first, headpair->second);
261  }
262  }
263  }
264 };
265 
266 #endif
Definition: SwappyItems.hpp:49
std::pair< TVALUE, std::vector< TKEY > > Data
Definition: SwappyItems.hpp:52
Data * get(const TKEY &key)
Definition: SwappyItems.hpp:188
bool del(const TKEY &key)
Definition: SwappyItems.hpp:211
struct statistic_s getStatistic(void)
Definition: SwappyItems.hpp:288
bool set(TKEY key, TVALUE value)
Definition: SwappyItems.hpp:123
Definition: SwappyQueue.hpp:43
void insert(TKEY key, Item &item)
Definition: SwappyQueue.hpp:235
Heap * _data
Definition: SwappyQueue.hpp:58
void del(TKEY key)
Definition: SwappyQueue.hpp:137
bool get(TKEY key, Item &result)
Definition: SwappyQueue.hpp:80
TKEY _headKey
Definition: SwappyQueue.hpp:59
bool set(TKEY key, Item &item)
Definition: SwappyQueue.hpp:113
bool top(TKEY &resultkey, Item &result)
Definition: SwappyQueue.hpp:96
SwappyQueue(void)
Definition: SwappyQueue.hpp:63
~SwappyQueue(void)
Definition: SwappyQueue.hpp:68
bool empty
Definition: SwappyQueue.hpp:46
SwappyItems< TKEY, Item, EACHFILE, OLDIES, RAMSIZE, BLOOMBITS, MASKLENGTH > Heap
Definition: SwappyQueue.hpp:56
Definition: SwappyItems.hpp:54
uint64_t size
Definition: SwappyItems.hpp:55
Definition: SwappyQueue.hpp:48
TKEY parent
Definition: SwappyQueue.hpp:51
uint64_t prio
Definition: SwappyQueue.hpp:50
TVALUE data
Definition: SwappyQueue.hpp:49