libfoedus-core
FOEDUS Core Library
hash_storage.cpp
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  */
19 
20 #include <glog/logging.h>
21 
22 #include <iostream>
23 #include <string>
24 
25 #include "foedus/engine.hpp"
31 #include "foedus/thread/thread.hpp"
32 
33 namespace foedus {
34 namespace storage {
35 namespace hash {
36 
39  : Storage<HashStorageControlBlock>(engine, control_block) {
41 }
43  : Storage<HashStorageControlBlock>(engine, control_block) {
45 }
47  : Storage<HashStorageControlBlock>(engine, id) {}
49  : Storage<HashStorageControlBlock>(engine, name) {}
51  : Storage<HashStorageControlBlock>(other.engine_, other.control_block_) {
52 }
54  engine_ = other.engine_;
56  return *this;
57 }
58 
59 uint8_t HashStorage::get_levels() const { return control_block_->levels_; }
60 HashBin HashStorage::get_bin_count() const { return control_block_->bin_count_; }
61 uint8_t HashStorage::get_bin_bits() const { return control_block_->meta_.bin_bits_; }
62 uint8_t HashStorage::get_bin_shifts() const { return control_block_->meta_.get_bin_shifts(); }
63 uint16_t HashStorage::get_root_children() const { return control_block_->get_root_children(); }
64 
66  HashStoragePimpl pimpl(this);
67  return pimpl.create(static_cast<const HashMetadata&>(metadata));
68 }
70  HashStoragePimpl pimpl(this);
71  return pimpl.load(snapshot_block);
72 }
74  HashStoragePimpl pimpl(this);
75  return pimpl.drop();
76 }
77 
79 
81  thread::Thread* context,
82  const void* key,
83  uint16_t key_length,
84  const HashCombo& combo,
85  void* payload,
86  uint16_t* payload_capacity,
87  bool read_only) {
88  HashStoragePimpl pimpl(this);
89  return pimpl.get_record(
90  context,
91  key,
92  key_length,
93  combo,
94  payload,
95  payload_capacity,
96  read_only);
97 }
98 
100  thread::Thread* context,
101  const void* key,
102  uint16_t key_length,
103  const HashCombo& combo,
104  void* payload,
105  uint16_t payload_offset,
106  uint16_t payload_count,
107  bool read_only) {
108  HashStoragePimpl pimpl(this);
109  return pimpl.get_record_part(
110  context,
111  key,
112  key_length,
113  combo,
114  payload,
115  payload_offset,
116  payload_count,
117  read_only);
118 }
119 
120 template <typename PAYLOAD>
122  thread::Thread* context,
123  const void* key,
124  uint16_t key_length,
125  const HashCombo& combo,
126  PAYLOAD* payload,
127  uint16_t payload_offset,
128  bool read_only) {
129  HashStoragePimpl pimpl(this);
130  return pimpl.get_record_primitive(
131  context,
132  key,
133  key_length,
134  combo,
135  payload,
136  payload_offset,
137  read_only);
138 }
139 
141  thread::Thread* context,
142  const void* key,
143  uint16_t key_length,
144  const HashCombo& combo,
145  const void* payload,
146  uint16_t payload_count,
147  uint16_t physical_payload_hint) {
148  HashStoragePimpl pimpl(this);
149  return pimpl.insert_record(
150  context,
151  key,
152  key_length,
153  combo,
154  payload,
155  payload_count,
156  physical_payload_hint);
157 }
158 
160  thread::Thread* context,
161  const void* key,
162  uint16_t key_length,
163  const HashCombo& combo,
164  const void* payload,
165  uint16_t payload_count,
166  uint16_t physical_payload_hint) {
167  HashStoragePimpl pimpl(this);
168  return pimpl.upsert_record(
169  context,
170  key,
171  key_length,
172  combo,
173  payload,
174  payload_count,
175  physical_payload_hint);
176 }
177 
178 
180  thread::Thread* context,
181  const void* key,
182  uint16_t key_length,
183  const HashCombo& combo) {
184  HashStoragePimpl pimpl(this);
185  return pimpl.delete_record(
186  context,
187  key,
188  key_length,
189  combo);
190 }
191 
193  thread::Thread* context,
194  const void* key,
195  uint16_t key_length,
196  const HashCombo& combo,
197  const void* payload,
198  uint16_t payload_offset,
199  uint16_t payload_count) {
200  HashStoragePimpl pimpl(this);
201  return pimpl.overwrite_record(
202  context,
203  key,
204  key_length,
205  combo,
206  payload,
207  payload_offset,
208  payload_count);
209 }
210 
211 template <typename PAYLOAD>
213  thread::Thread* context,
214  const void* key,
215  uint16_t key_length,
216  const HashCombo& combo,
217  PAYLOAD payload,
218  uint16_t payload_offset) {
219  HashStoragePimpl pimpl(this);
220  return pimpl.overwrite_record_primitive(
221  context,
222  key,
223  key_length,
224  combo,
225  payload,
226  payload_offset);
227 }
228 
229 template <typename PAYLOAD>
231  thread::Thread* context,
232  const void* key,
233  uint16_t key_length,
234  const HashCombo& combo,
235  PAYLOAD* value,
236  uint16_t payload_offset) {
237  HashStoragePimpl pimpl(this);
238  return pimpl.increment_record(
239  context,
240  key,
241  key_length,
242  combo,
243  value,
244  payload_offset);
245 }
246 
247 std::ostream& operator<<(std::ostream& o, const HashStorage& v) {
248  o << "<HashStorage>"
249  << "<id>" << v.get_id() << "</id>"
250  << "<name>" << v.get_name() << "</name>"
251  << "<bin_bits>" << static_cast<int>(v.control_block_->meta_.bin_bits_) << "</bin_bits>"
252  << "</HashStorage>";
253  return o;
254 }
255 
257  xct::RwLockableXctId* old_address,
258  xct::WriteXctAccess* write_set) {
259  HashStoragePimpl pimpl(this);
260  return pimpl.track_moved_record(old_address, write_set);
261 }
262 
264  HashStoragePimpl pimpl(this);
265  return pimpl.verify_single_thread(engine);
266 }
267 
269  HashStoragePimpl pimpl(this);
270  return pimpl.verify_single_thread(context);
271 }
272 
274  HashStoragePimpl pimpl(this);
275  return pimpl.hcc_reset_all_temperature_stat();
276 }
277 
278 
280  Engine* engine,
281  bool volatile_only,
282  bool intermediate_only,
283  uint32_t max_pages) {
284  HashStoragePimpl pimpl(this);
285  return pimpl.debugout_single_thread(engine, volatile_only, intermediate_only, max_pages);
286 }
287 
288 // Explicit instantiations for each payload type
289 // @cond DOXYGEN_IGNORE
290 #define EXPIN_2(x) template ErrorCode HashStorage::get_record_primitive< x > \
291  (thread::Thread* context, \
292  const void* key, \
293  uint16_t key_length, \
294  const HashCombo& combo, \
295  x* payload, \
296  uint16_t payload_offset, \
297  bool read_only)
299 
300 #define EXPIN_3(x) template ErrorCode HashStorage::overwrite_record_primitive< x > \
301  (thread::Thread* context, \
302  const void* key, \
303  uint16_t key_length, \
304  const HashCombo& combo, \
305  x payload, \
306  uint16_t payload_offset)
308 
309 #define EXPIN_5(x) template ErrorCode HashStorage::increment_record< x > \
310  (thread::Thread* context, \
311  const void* key, \
312  uint16_t key_length, \
313  const HashCombo& combo, \
314  x* value, \
315  uint16_t payload_offset)
317 // @endcond
318 
319 
320 } // namespace hash
321 } // namespace storage
322 } // namespace foedus
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 increment_record(thread::Thread *context, const void *key, uint16_t key_length, const HashCombo &combo, PAYLOAD *value, uint16_t payload_offset)
ErrorStack debugout_single_thread(Engine *engine, bool volatile_only, bool intermediate_only, uint32_t max_pages)
These are defined in hash_storage_debug.cpp.
StorageType get_type() const
Returns the type of this storage.
Definition: storage.hpp:150
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)
Result of track_moved_record().
Definition: xct_id.hpp:1180
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 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 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)
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...
Engine * engine_
Most attachable object stores an engine pointer (local engine), so we define it here.
Definition: attachable.hpp:107
ErrorStack load(const StorageControlBlock &snapshot_block)
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.
uint8_t bin_bits_
Number of bins in exponent of two.
ErrorStack verify_single_thread(Engine *engine)
ErrorStack create(const HashMetadata &metadata)
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.
ErrorStack verify_single_thread(Engine *engine)
These are defined in hash_storage_verify.cpp.
std::ostream & operator<<(std::ostream &o, const HashCombo &v)
Definition: hash_combo.cpp:37
HashStorageControlBlock * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
const StorageName & get_name() const
Returns the unique name of this storage.
Definition: storage.hpp:155
Declares all log types used in this storage type.
bool exists() const
Returns whether this storage is already created.
Definition: storage.hpp:169
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.
ErrorStack load(const StorageControlBlock &snapshot_block)
xct::TrackMovedRecordResult track_moved_record(xct::RwLockableXctId *old_address, xct::WriteXctAccess *write_set)
StorageId get_id() const
Returns the unique ID of this storage.
Definition: storage.hpp:145
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 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.
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)
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
#define INSTANTIATE_ALL_NUMERIC_TYPES(M)
INSTANTIATE_ALL_TYPES minus std::string.
HashMetadata meta_
metadata of this storage.
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
ErrorCode delete_record(thread::Thread *context, const void *key, uint16_t key_length)
Deletes a record of the given key from this hash storage.
A base layout of shared data for all storage types.
Definition: storage.hpp:53
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)
HashStorage & operator=(const HashStorage &other)
ErrorStack create(const Metadata &metadata)