libfoedus-core
FOEDUS Core Library
hash_storage.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_STORAGE_HASH_HASH_STORAGE_HPP_
19 #define FOEDUS_STORAGE_HASH_HASH_STORAGE_HPP_
20 #include <iosfwd>
21 #include <string>
22 
23 #include "foedus/attachable.hpp"
24 #include "foedus/cxx11.hpp"
25 #include "foedus/fwd.hpp"
26 #include "foedus/storage/fwd.hpp"
32 #include "foedus/thread/fwd.hpp"
33 #include "foedus/xct/fwd.hpp"
34 #include "foedus/xct/xct_id.hpp"
35 
36 namespace foedus {
37 namespace storage {
38 namespace hash {
43 class HashStorage CXX11_FINAL : public Storage<HashStorageControlBlock> {
44  public:
48 
49  HashStorage();
50  HashStorage(Engine* engine, HashStorageControlBlock* control_block);
51  HashStorage(Engine* engine, StorageControlBlock* control_block);
52  HashStorage(Engine* engine, StorageId id);
53  HashStorage(Engine* engine, const StorageName& name);
54  HashStorage(const HashStorage& other);
55  HashStorage& operator=(const HashStorage& other);
56 
57  // Storage interface
58  const HashMetadata* get_hash_metadata() const;
60  uint8_t get_levels() const;
62  HashBin get_bin_count() const;
64  uint8_t get_bin_bits() const;
66  uint8_t get_bin_shifts() const;
68  uint16_t get_root_children() const;
69  ErrorStack create(const Metadata &metadata);
70  ErrorStack load(const StorageControlBlock& snapshot_block);
71  ErrorStack drop();
72  friend std::ostream& operator<<(std::ostream& o, const HashStorage& v);
73 
87  xct::RwLockableXctId* old_address,
88  xct::WriteXctAccess* write_set);
89 
92 
98 
107  Engine* engine,
108  bool volatile_only = false,
109  bool intermediate_only = false,
110  uint32_t max_pages = 1024U);
111 
113  // TASK(Hideaki) Add primitive-optimized versions and increment versions.
114  // Low priority. Most costs are from page-traversal and hashinate.
115 
116 
120  inline HashCombo combo(const void* key, uint16_t key_length) const {
121  return HashCombo(key, key_length, *get_hash_metadata());
122  }
129  template <typename KEY>
130  inline HashCombo combo(KEY* key) const {
131  return HashCombo(key, sizeof(KEY), *get_hash_metadata());
132  }
133 
134 
135  // get_record() methods
136 
155  thread::Thread* context,
156  const void* key,
157  uint16_t key_length,
158  void* payload,
159  uint16_t* payload_capacity,
160  bool read_only) {
161  HashCombo c(combo(key, key_length));
162  return get_record(context, key, key_length, c, payload, payload_capacity, read_only);
163  }
164 
166  template <typename KEY>
168  thread::Thread* context,
169  KEY key,
170  void* payload,
171  uint16_t* payload_capacity,
172  bool read_only) {
173  HashCombo c(combo<KEY>(&key));
174  return get_record(context, &key, sizeof(key), c, payload, payload_capacity, read_only);
175  }
176 
179  thread::Thread* context,
180  const void* key,
181  uint16_t key_length,
182  const HashCombo& combo,
183  void* payload,
184  uint16_t* payload_capacity,
185  bool read_only);
186 
201  thread::Thread* context,
202  const void* key,
203  uint16_t key_length,
204  void* payload,
205  uint16_t payload_offset,
206  uint16_t payload_count,
207  bool read_only) {
208  HashCombo c(combo(key, key_length));
209  return get_record_part(
210  context,
211  key,
212  key_length,
213  c,
214  payload,
215  payload_offset,
216  payload_count,
217  read_only);
218  }
219 
221  template <typename KEY>
223  thread::Thread* context,
224  KEY key,
225  void* payload,
226  uint16_t payload_offset,
227  uint16_t payload_count,
228  bool read_only) {
229  HashCombo c(combo<KEY>(&key));
230  return get_record_part(
231  context,
232  &key,
233  sizeof(key),
234  c,
235  payload,
236  payload_offset,
237  payload_count,
238  read_only);
239  }
240 
243  thread::Thread* context,
244  const void* key,
245  uint16_t key_length,
246  const HashCombo& combo,
247  void* payload,
248  uint16_t payload_offset,
249  uint16_t payload_count,
250  bool read_only);
251 
265  template <typename PAYLOAD>
267  thread::Thread* context,
268  const void* key,
269  uint16_t key_length,
270  PAYLOAD* payload,
271  uint16_t payload_offset,
272  bool read_only) {
273  HashCombo c(combo(key, key_length));
274  return get_record_primitive<PAYLOAD>(
275  context,
276  key,
277  key_length,
278  c,
279  payload,
280  payload_offset,
281  read_only);
282  }
283 
285  template <typename KEY, typename PAYLOAD>
287  thread::Thread* context,
288  KEY key,
289  PAYLOAD* payload,
290  uint16_t payload_offset,
291  bool read_only) {
292  HashCombo c(combo<KEY>(&key));
293  return get_record_primitive<PAYLOAD>(
294  context,
295  &key,
296  sizeof(key),
297  c,
298  payload,
299  payload_offset,
300  read_only);
301  }
302 
304  template <typename PAYLOAD>
306  thread::Thread* context,
307  const void* key,
308  uint16_t key_length,
309  const HashCombo& combo,
310  PAYLOAD* payload,
311  uint16_t payload_offset,
312  bool read_only);
313 
314  // insert_record() methods
315 
328  thread::Thread* context,
329  const void* key,
330  uint16_t key_length,
331  const void* payload,
332  uint16_t payload_count) {
333  HashCombo c(combo(key, key_length));
334  return insert_record(context, key, key_length, c, payload, payload_count, payload_count);
335  }
336 
338  template <typename KEY>
340  thread::Thread* context,
341  KEY key,
342  const void* payload,
343  uint16_t payload_count) {
344  HashCombo c(combo<KEY>(&key));
345  return insert_record(context, &key, sizeof(key), c, payload, payload_count, payload_count);
346  }
347 
356  thread::Thread* context,
357  const void* key,
358  uint16_t key_length,
359  const HashCombo& combo,
360  const void* payload,
361  uint16_t payload_count,
362  uint16_t physical_payload_hint);
363 
364  // delete_record() methods
365 
375  inline ErrorCode delete_record(thread::Thread* context, const void* key, uint16_t key_length) {
376  HashCombo c(combo(key, key_length));
377  return delete_record(context, key, key_length, c);
378  }
379 
381  template <typename KEY>
382  inline ErrorCode delete_record(thread::Thread* context, KEY key) {
383  HashCombo c(combo<KEY>(&key));
384  return delete_record(context, &key, sizeof(key), c);
385  }
386 
389  thread::Thread* context,
390  const void* key,
391  uint16_t key_length,
392  const HashCombo& combo);
393 
394  // upsert_record() methods
395 
413  thread::Thread* context,
414  const void* key,
415  uint16_t key_length,
416  const void* payload,
417  uint16_t payload_count) {
418  HashCombo c(combo(key, key_length));
419  return upsert_record(context, key, key_length, c, payload, payload_count, payload_count);
420  }
421 
423  template <typename KEY>
425  thread::Thread* context,
426  KEY key,
427  const void* payload,
428  uint16_t payload_count) {
429  HashCombo c(combo<KEY>(&key));
430  return upsert_record(context, &key, sizeof(key), c, payload, payload_count, payload_count);
431  }
432 
435  thread::Thread* context,
436  const void* key,
437  uint16_t key_length,
438  const HashCombo& combo,
439  const void* payload,
440  uint16_t payload_count,
441  uint16_t physical_payload_hint);
442 
443  // overwrite_record() methods
444 
460  thread::Thread* context,
461  const void* key,
462  uint16_t key_length,
463  const void* payload,
464  uint16_t payload_offset,
465  uint16_t payload_count) {
466  HashCombo c(combo(key, key_length));
467  return overwrite_record(
468  context,
469  key,
470  key_length,
471  c,
472  payload,
473  payload_offset,
474  payload_count);
475  }
476 
478  template <typename KEY>
480  thread::Thread* context,
481  KEY key,
482  const void* payload,
483  uint16_t payload_offset,
484  uint16_t payload_count) {
485  HashCombo c(combo<KEY>(&key));
486  return overwrite_record(context, &key, sizeof(key), c, payload, payload_offset, payload_count);
487  }
488 
491  thread::Thread* context,
492  const void* key,
493  uint16_t key_length,
494  const HashCombo& combo,
495  const void* payload,
496  uint16_t payload_offset,
497  uint16_t payload_count);
498 
510  template <typename PAYLOAD>
512  thread::Thread* context,
513  const void* key,
514  uint16_t key_length,
515  PAYLOAD payload,
516  uint16_t payload_offset) {
517  HashCombo c(combo(key, key_length));
518  return overwrite_record_primitive(context, key, key_length, c, payload, payload_offset);
519  }
520 
522  template <typename KEY, typename PAYLOAD>
524  thread::Thread* context,
525  KEY key,
526  PAYLOAD payload,
527  uint16_t payload_offset) {
528  HashCombo c(combo<KEY>(&key));
529  return overwrite_record_primitive(context, &key, sizeof(key), c, payload, payload_offset);
530  }
531 
533  template <typename PAYLOAD>
535  thread::Thread* context,
536  const void* key,
537  uint16_t key_length,
538  const HashCombo& combo,
539  PAYLOAD payload,
540  uint16_t payload_offset);
541 
542  // increment_record() methods
543 
556  template <typename PAYLOAD>
558  thread::Thread* context,
559  const void* key,
560  uint16_t key_length,
561  PAYLOAD* value,
562  uint16_t payload_offset) {
563  HashCombo c(combo(key, key_length));
564  return increment_record(context, key, key_length, c, value, payload_offset);
565  }
566 
568  template <typename KEY, typename PAYLOAD>
570  thread::Thread* context,
571  KEY key,
572  PAYLOAD* value,
573  uint16_t payload_offset) {
574  HashCombo c(combo<KEY>(&key));
575  return increment_record(context, &key, sizeof(key), c, value, payload_offset);
576  }
577 
579  template <typename PAYLOAD>
581  thread::Thread* context,
582  const void* key,
583  uint16_t key_length,
584  const HashCombo& combo,
585  PAYLOAD* value,
586  uint16_t payload_offset);
587 };
588 } // namespace hash
589 } // namespace storage
590 } // namespace foedus
591 #endif // FOEDUS_STORAGE_HASH_HASH_STORAGE_HPP_
ErrorCode insert_record(thread::Thread *context, const void *key, uint16_t key_length, const void *payload, uint16_t payload_count)
Inserts a new record of the given key in this hash storage.
ErrorCode delete_record(thread::Thread *context, KEY key)
Overlord to receive key as a primitive type.
ErrorCode get_record_part(thread::Thread *context, KEY key, void *payload, uint16_t payload_offset, uint16_t payload_count, bool read_only)
Overlord to receive key as a primitive type.
Definitions of IDs in this package and a few related constant values.
ErrorCode overwrite_record(thread::Thread *context, KEY key, const void *payload, uint16_t payload_offset, uint16_t payload_count)
Overlord to receive key as a primitive type.
uint32_t StorageId
Unique ID for storage.
Definition: storage_id.hpp:55
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
Represents a record of write-access during a transaction.
Definition: xct_access.hpp:168
Represents one thread running on one NUMA core.
Definition: thread.hpp:48
ErrorCode get_record(thread::Thread *context, KEY key, void *payload, uint16_t *payload_capacity, bool read_only)
Overlord to receive key as a primitive type.
Forward declarations of classes in transaction package.
Result of track_moved_record().
Definition: xct_id.hpp:1180
HashCombo combo(const void *key, uint16_t key_length) const
Prepares a set of information that are used in many places, extracted from the given key...
ErrorCode increment_record(thread::Thread *context, KEY key, PAYLOAD *value, uint16_t payload_offset)
Overlord to receive key as a primitive type.
Forward declarations of classes in root package.
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
ErrorCode get_record(thread::Thread *context, const void *key, uint16_t key_length, void *payload, uint16_t *payload_capacity, bool read_only)
Retrieves an entire payload of the given key in this hash storage.
ErrorCode increment_record(thread::Thread *context, const void *key, uint16_t key_length, PAYLOAD *value, uint16_t payload_offset)
This one further optimizes overwrite methods for the frequent use case of incrementing some data in p...
ErrorStack load(const StorageControlBlock &snapshot_block)
ErrorCode insert_record(thread::Thread *context, KEY key, const void *payload, uint16_t payload_count)
Overlord to receive key as a primitive type.
ErrorCode overwrite_record(thread::Thread *context, const void *key, uint16_t key_length, const void *payload, uint16_t payload_offset, uint16_t payload_count)
Overwrites a part of one record of the given key in this hash storage.
xct::TrackMovedRecordResult track_moved_record(xct::RwLockableXctId *old_address, xct::WriteXctAccess *write_set)
Resolves a "moved" record.
ErrorStack verify_single_thread(Engine *engine)
The MCS reader-writer lock variant of LockableXctId.
Definition: xct_id.hpp:1132
Metadata of one storage.
Definition: metadata.hpp:58
Represents one key-value store.
Definition: storage.hpp:116
ErrorCode get_record_part(thread::Thread *context, const void *key, uint16_t key_length, void *payload, uint16_t payload_offset, uint16_t payload_count, bool read_only)
Retrieves a part of the given key in this hash storage.
Forward declarations of classes in storage package.
Definitions of IDs in this package and a few related constant values.
ErrorCode get_record_primitive(thread::Thread *context, KEY key, PAYLOAD *payload, uint16_t payload_offset, bool read_only)
Overlord to receive key as a primitive type.
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
ErrorCode upsert_record(thread::Thread *context, const void *key, uint16_t key_length, const void *payload, uint16_t payload_count)
Inserts a new record of the given key or replaces the existing one in this hash storage, or so-called upsert.
A set of information that are used in many places, extracted from the given key.
Definition: hash_combo.hpp:48
ErrorCode get_record_primitive(thread::Thread *context, const void *key, uint16_t key_length, PAYLOAD *payload, uint16_t payload_offset, bool read_only)
Retrieves a part of the given key in this storage as a primitive value.
Represents a key-value store based on a dense and regular hash.
ErrorCode upsert_record(thread::Thread *context, KEY key, const void *payload, uint16_t payload_count)
Overlord to receive key as a primitive type.
friend std::ostream & operator<<(std::ostream &o, const HashStorage &v)
ErrorCode overwrite_record_primitive(thread::Thread *context, const void *key, uint16_t key_length, PAYLOAD payload, uint16_t payload_offset)
Overwrites a part of one record of the given key in this storage as a primitive value.
uint64_t HashBin
Represents a bin of a hash value.
Definition: hash_id.hpp:142
ErrorStack hcc_reset_all_temperature_stat()
Resets all volatile pages' temperature stat to be zero in this storage.
Metadata of an hash storage.
ErrorStack debugout_single_thread(Engine *engine, bool volatile_only=false, bool intermediate_only=false, uint32_t max_pages=1024U)
A super-expensive and single-thread only debugging feature to write out gigantic human-readable texts...
const HashMetadata * get_hash_metadata() const
ErrorCode delete_record(thread::Thread *context, const void *key, uint16_t key_length)
Deletes a record of the given key from this hash storage.
Definitions of IDs in this package and a few related constant values.
Forward declarations of classes in thread package.
A base layout of shared data for all storage types.
Definition: storage.hpp:53
ErrorCode overwrite_record_primitive(thread::Thread *context, KEY key, PAYLOAD payload, uint16_t payload_offset)
Overlord to receive key as a primitive type.
HashCombo combo(KEY *key) const
Overlord to receive key as a primitive type.
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85
Forward declarations of classes in hash storage package.
HashStorage & operator=(const HashStorage &other)
Log type of CREATE HASH STORAGE operation.
ErrorStack create(const Metadata &metadata)