libfoedus-core
FOEDUS Core Library
hash_storage_pimpl.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_PIMPL_HPP_
19 #define FOEDUS_STORAGE_HASH_HASH_STORAGE_PIMPL_HPP_
20 #include <stdint.h>
21 
22 #include <string>
23 #include <vector>
24 
25 #include "foedus/attachable.hpp"
26 #include "foedus/compiler.hpp"
27 #include "foedus/cxx11.hpp"
28 #include "foedus/fwd.hpp"
31 #include "foedus/cache/fwd.hpp"
32 #include "foedus/memory/fwd.hpp"
34 #include "foedus/storage/fwd.hpp"
42 #include "foedus/thread/fwd.hpp"
43 #include "foedus/xct/fwd.hpp"
44 
45 namespace foedus {
46 namespace storage {
47 namespace hash {
50  // this is backed by shared memory. not instantiation. just reinterpret_cast.
51  HashStorageControlBlock() = delete;
52  ~HashStorageControlBlock() = delete;
53 
54  bool exists() const { return status_ == kExists || status_ == kMarkedForDeath; }
56  uint16_t get_root_children() const {
58  }
59 
67 
68  // Do NOT reorder members up to here. The layout must be compatible with StorageControlBlock
69  // Type-specific shared members below.
70 
71  // these are static auxiliary variables. doesn't have to be shared actually, but easier.
76  uint64_t bin_count_;
82  uint8_t levels_;
83  char padding_[7];
84 };
85 
93 class HashStoragePimpl final : public Attachable<HashStorageControlBlock> {
94  public:
96  explicit HashStoragePimpl(HashStorage* storage)
98  storage->get_engine(),
99  storage->get_control_block()) {}
100 
104  HashIntermediatePage* page,
105  VolatilePagePointer volatile_page_id);
108  HashIntermediatePage* page,
109  VolatilePagePointer volatile_page_id);
112  HashDataPage* page,
113  VolatilePagePointer volatile_page_id);
114 
115 
117  xct::RwLockableXctId* old_address,
118  xct::WriteXctAccess* write_set);
120  HashDataPage* page,
121  const void* key,
122  uint16_t key_length,
123  const HashCombo& combo);
124 
130 
135 
138  Engine* engine,
139  bool volatile_only,
140  bool intermediate_only,
141  uint32_t max_pages);
143  Engine* engine,
144  cache::SnapshotFileSet* fileset,
145  HashIntermediatePage* parent,
146  bool follow_volatile,
147  bool intermediate_only,
148  uint32_t* remaining_pages);
150  Engine* engine,
151  cache::SnapshotFileSet* fileset,
152  HashDataPage* head,
153  bool follow_volatile,
154  uint32_t* remaining_pages);
155 
156  ErrorStack create(const HashMetadata& metadata);
157  ErrorStack load(const StorageControlBlock& snapshot_block);
158  ErrorStack drop();
159 
160  bool exists() const { return control_block_->exists(); }
161  StorageId get_id() const { return control_block_->meta_.id_; }
162  const StorageName& get_name() const { return control_block_->meta_.name_; }
163  const HashMetadata& get_meta() const { return control_block_->meta_; }
164  uint8_t get_levels() const { return control_block_->levels_; }
165  HashBin get_bin_count() const { return get_meta().get_bin_count(); }
166  uint8_t get_bin_bits() const { return get_meta().bin_bits_; }
167  uint8_t get_bin_shifts() const { return get_meta().get_bin_shifts(); }
168 
171  thread::Thread* context,
172  const void* key,
173  uint16_t key_length,
174  const HashCombo& combo,
175  void* payload,
176  uint16_t* payload_capacity,
177  bool read_only);
178 
180  template <typename PAYLOAD>
182  thread::Thread* context,
183  const void* key,
184  uint16_t key_length,
185  const HashCombo& combo,
186  PAYLOAD* payload,
187  uint16_t payload_offset,
188  bool read_only) {
189  // at this point, there isn't enough benefit to do optimization specific to this case.
190  // hash-lookup is anyway dominant. memcpy-vs-primitive is not the issue.
191  return get_record_part(
192  context,
193  key,
194  key_length,
195  combo,
196  payload,
197  payload_offset,
198  sizeof(PAYLOAD),
199  read_only);
200  }
201 
204  thread::Thread* context,
205  const void* key,
206  uint16_t key_length,
207  const HashCombo& combo,
208  void* payload,
209  uint16_t payload_offset,
210  uint16_t payload_count,
211  bool read_only);
212 
213 
216  thread::Thread* context,
217  const RecordLocation& location,
218  log::RecordLogType* log_entry);
219 
222  thread::Thread* context,
223  const void* key,
224  uint16_t key_length,
225  const HashCombo& combo,
226  const void* payload,
227  uint16_t payload_count,
228  uint16_t physical_payload_hint);
229 
232  thread::Thread* context,
233  const void* key,
234  uint16_t key_length,
235  const HashCombo& combo);
236 
239  thread::Thread* context,
240  const void* key,
241  uint16_t key_length,
242  const HashCombo& combo,
243  const void* payload,
244  uint16_t payload_count,
245  uint16_t physical_payload_hint);
246 
249  thread::Thread* context,
250  const void* key,
251  uint16_t key_length,
252  const HashCombo& combo,
253  const void* payload,
254  uint16_t payload_offset,
255  uint16_t payload_count);
256 
258  template <typename PAYLOAD>
260  thread::Thread* context,
261  const void* key,
262  uint16_t key_length,
263  const HashCombo& combo,
264  PAYLOAD payload,
265  uint16_t payload_offset) {
266  // same above. still handy as an API, though.
267  return overwrite_record(
268  context,
269  key,
270  key_length,
271  combo,
272  &payload,
273  payload_offset,
274  sizeof(payload));
275  }
276 
278  template <typename PAYLOAD>
280  thread::Thread* context,
281  const void* key,
282  uint16_t key_length,
283  const HashCombo& combo,
284  PAYLOAD* value,
285  uint16_t payload_offset);
286 
291  thread::Thread* context,
292  bool for_write,
293  HashIntermediatePage** root);
296  thread::Thread* context,
297  bool for_write,
298  HashIntermediatePage* parent,
299  uint16_t index_in_parent,
300  Page** page);
303  thread::Thread* context,
304  bool for_write,
305  HashIntermediatePage* parent,
306  uint16_t index_in_parent,
307  Page** page);
308 
334  thread::Thread* context,
335  bool for_write,
336  const HashCombo& combo,
337  HashDataPage** bin_head);
338 
365  thread::Thread* context,
366  bool for_write,
367  bool physical_only,
368  bool create_if_notfound,
369  uint16_t create_payload_length,
370  const void* key,
371  uint16_t key_length,
372  const HashCombo& combo,
373  HashDataPage* bin_head,
374  RecordLocation* result);
375 
378  thread::Thread* context,
379  bool for_write,
380  bool create_if_notfound,
381  uint16_t create_payload_length,
382  const void* key,
383  uint16_t key_length,
384  const HashCombo& combo,
385  HashDataPage* bin_head,
386  RecordLocation* result) {
387  return locate_record(
388  context,
389  for_write,
390  true,
391  create_if_notfound,
392  create_payload_length,
393  key,
394  key_length,
395  combo,
396  bin_head,
397  result);
398  }
401  thread::Thread* context,
402  bool for_write,
403  bool create_if_notfound,
404  uint16_t create_payload_length,
405  const void* key,
406  uint16_t key_length,
407  const HashCombo& combo,
408  HashDataPage* bin_head,
409  RecordLocation* result) {
410  return locate_record(
411  context,
412  for_write,
413  false,
414  create_if_notfound,
415  create_payload_length,
416  key,
417  key_length,
418  combo,
419  bin_head,
420  result);
421  }
422 
425  thread::Thread* context,
426  const void* key,
427  uint16_t key_length,
428  const HashCombo& combo,
429  HashDataPage* bin_head,
430  RecordLocation* result);
431 
449  thread::Thread* context,
450  const void* key,
451  uint16_t key_length,
452  const HashCombo& combo,
453  uint16_t payload_length,
454  HashDataPage** page_in_out,
455  uint16_t examined_records,
456  DataPageSlotIndex* new_location);
457 };
458 static_assert(sizeof(HashStoragePimpl) <= kPageSize, "HashStoragePimpl is too large");
459 static_assert(
460  sizeof(HashStorageControlBlock) <= soc::GlobalMemoryAnchors::kStorageMemorySize,
461  "HashStorageControlBlock is too large.");
462 } // namespace hash
463 } // namespace storage
464 } // namespace foedus
465 #endif // FOEDUS_STORAGE_HASH_HASH_STORAGE_PIMPL_HPP_
ErrorCode increment_record(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, PAYLOAD *value, uint16_t payload_offset)
ErrorCode locate_record_physical_only(thread::Thread *context, bool for_write, bool create_if_notfound, uint16_t create_payload_length, const void *key, uint16_t key_length, const HashCombo &combo, HashDataPage *bin_head, RecordLocation *result)
locate_record()'s physical_only version.
uint8_t levels_
Number of levels of pages.
Represents a pointer to another page (usually a child page).
Definition: storage_id.hpp:271
ErrorStack debugout_single_thread(Engine *engine, bool volatile_only, bool intermediate_only, uint32_t max_pages)
These are defined in hash_storage_debug.cpp.
The storage has been marked for drop and can't be used.
Definition: storage_id.hpp:164
Definitions of IDs in this package and a few related constant values.
StorageStatus status_
Status of the storage.
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
ErrorCode overwrite_record(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, const void *payload, uint16_t payload_offset, uint16_t payload_count)
Represents one thread running on one NUMA core.
Definition: thread.hpp:48
ErrorCode get_record_part(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, void *payload, uint16_t payload_offset, uint16_t payload_count, bool read_only)
ErrorCode locate_record_logical(thread::Thread *context, bool for_write, bool create_if_notfound, uint16_t create_payload_length, const void *key, uint16_t key_length, const HashCombo &combo, HashDataPage *bin_head, RecordLocation *result)
locate_record()'s logical+physical version.
Forward declarations of classes in transaction package.
Result of track_moved_record().
Definition: xct_id.hpp:1180
Represents a pointer to a volatile page with modification count for preventing ABA.
Definition: storage_id.hpp:194
ErrorCode follow_page_bin_head(thread::Thread *context, bool for_write, HashIntermediatePage *parent, uint16_t index_in_parent, Page **page)
subroutine to follow a pointer to head of bin from a volatile parent
ErrorCode get_root_page(thread::Thread *context, bool for_write, HashIntermediatePage **root)
Retrieves the root page of this storage.
Forward declarations of classes in root package.
ErrorCode follow_page(thread::Thread *context, bool for_write, HashIntermediatePage *parent, uint16_t index_in_parent, Page **page)
for non-root
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
ErrorStack hcc_reset_all_temperature_stat()
For stupid reasons (I'm lazy!) these are defined in _debug.cpp.
ErrorCode insert_record(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, const void *payload, uint16_t payload_count, uint16_t physical_payload_hint)
The storage has been created and ready for use.
Definition: storage_id.hpp:158
Holds a set of read-only file objects for snapshot files.
ErrorCode locate_record_reserve_physical(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, uint16_t payload_length, HashDataPage **page_in_out, uint16_t examined_records, DataPageSlotIndex *new_location)
Subroutine of locate_record() to create/migrate a physical record of the given key in the page or its...
void release_pages_recursive_data(memory::PageReleaseBatch *batch, HashDataPage *page, VolatilePagePointer volatile_page_id)
Forward declarations of classes in cache package.
uint8_t bin_bits_
Number of bins in exponent of two.
A mutex that can be placed in shared memory and used from multiple processes.
ErrorStack hcc_reset_all_temperature_stat_intermediate(VolatilePagePointer intermediate_page_id)
ErrorStack create(const HashMetadata &metadata)
The MCS reader-writer lock variant of LockableXctId.
Definition: xct_id.hpp:1132
const HashMetadata & get_meta() const
ErrorStack verify_single_thread(Engine *engine)
These are defined in hash_storage_verify.cpp.
ErrorStack debugout_single_thread_data(Engine *engine, cache::SnapshotFileSet *fileset, HashDataPage *head, bool follow_volatile, uint32_t *remaining_pages)
Forward declarations of classes in storage package.
HashStorageControlBlock * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
A helper class to return a bunch of pages to individual nodes.
Definition: page_pool.hpp:276
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
ErrorStack verify_single_thread_data(Engine *engine, HashDataPage *head)
ErrorStack load(const StorageControlBlock &snapshot_block)
Just a marker to denote that the memory region represents a data page.
Definition: page.hpp:334
ErrorCode locate_bin(thread::Thread *context, bool for_write, const HashCombo &combo, HashDataPage **bin_head)
Find a pointer to the bin that contains records for the hash.
uint16_t DataPageSlotIndex
Definition: hash_id.hpp:196
xct::TrackMovedRecordResult track_moved_record(xct::RwLockableXctId *old_address, xct::WriteXctAccess *write_set)
A set of information that are used in many places, extracted from the given key.
Definition: hash_combo.hpp:48
void release_pages_recursive_root(memory::PageReleaseBatch *batch, HashIntermediatePage *page, VolatilePagePointer volatile_page_id)
Used only from uninitialize()
Attachable Resources on Shared Memory.
Definition: attachable.hpp:58
ErrorStack hcc_reset_all_temperature_stat_data(VolatilePagePointer head_page_id)
Represents a key-value store based on a dense and regular hash.
Forward declarations of classes in memory package.
ErrorCode locate_record(thread::Thread *context, bool for_write, bool physical_only, bool create_if_notfound, uint16_t create_payload_length, const void *key, uint16_t key_length, const HashCombo &combo, HashDataPage *bin_head, RecordLocation *result)
Usually follows locate_bin to locate the exact physical record for the key, or create a new one if no...
ErrorStack debugout_single_thread_intermediate(Engine *engine, cache::SnapshotFileSet *fileset, HashIntermediatePage *parent, bool follow_volatile, bool intermediate_only, uint32_t *remaining_pages)
Represents an intermediate page in Hashtable Storage.
ErrorCode register_record_write_log(thread::Thread *context, const RecordLocation &location, log::RecordLogType *log_entry)
Used in the following methods.
ErrorStack verify_single_thread_intermediate(Engine *engine, HashIntermediatePage *page)
void release_pages_recursive_intermediate(memory::PageReleaseBatch *batch, HashIntermediatePage *page, VolatilePagePointer volatile_page_id)
DualPagePointer root_page_pointer_
Points to the root page (or something equivalent).
uint64_t HashBin
Represents a bin of a hash value.
Definition: hash_id.hpp:142
Represents an individual data page in Hashtable Storage.
uint64_t bin_count_
How many hash bins this storage has.
ErrorCode get_record_primitive(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, PAYLOAD *payload, uint16_t payload_offset, bool read_only)
return value of locate_record().
Metadata of an hash storage.
uint64_t get_bin_count() const
Number of bins in this hash storage.
int64_t int_div_ceil(int64_t dividee, int64_t dividor)
Efficient ceil(dividee/dividor) for integer.
HashMetadata meta_
metadata of this storage.
const uint64_t kHashMaxBins[]
kHashTotalBins[n] gives the maximum number of hash bins n-level hash can hold.
Definition: hash_id.hpp:74
Base class for log type of record-wise operation.
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
HashStorageControlBlock * get_control_block() const
Definition: attachable.hpp:97
const uint16_t kPageSize
A constant defining the page size (in bytes) of both snapshot pages and volatile pages.
Definition: storage_id.hpp:45
ErrorCode delete_record(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo)
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85
ErrorCode overwrite_record_primitive(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, PAYLOAD payload, uint16_t payload_offset)
ErrorCode upsert_record(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, const void *payload, uint16_t payload_count, uint16_t physical_payload_hint)
ErrorCode get_record(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, void *payload, uint16_t *payload_capacity, bool read_only)
Forward declarations of classes in hash storage package.
ErrorCode locate_record_in_snapshot(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, HashDataPage *bin_head, RecordLocation *result)
Simpler version of locate_record for when we are in snapshot world.
StorageStatus
Status of a storage.
Definition: storage_id.hpp:154
xct::TrackMovedRecordResult track_moved_record_search(HashDataPage *page, const void *key, uint16_t key_length, const HashCombo &combo)