libfoedus-core
FOEDUS Core Library
masstree_partitioner_impl.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_MASSTREE_MASSTREE_PARTITIONER_IMPL_HPP_
19 #define FOEDUS_STORAGE_MASSTREE_MASSTREE_PARTITIONER_IMPL_HPP_
20 
21 #include <stdint.h>
22 
23 #include <algorithm>
24 #include <cstring>
25 #include <iosfwd>
26 #include <queue>
27 #include <string>
28 #include <vector>
29 
30 #include "foedus/fwd.hpp"
31 #include "foedus/initializable.hpp"
33 #include "foedus/memory/fwd.hpp"
41 
42 namespace foedus {
43 namespace storage {
44 namespace masstree {
98 class MasstreePartitioner final {
99  public:
100  explicit MasstreePartitioner(Partitioner* parent);
101 
103  bool is_partitionable() const;
105  void sort_batch(const Partitioner::SortBatchArguments& args) const;
106 
107  friend std::ostream& operator<<(std::ostream& o, const MasstreePartitioner& v);
108 
109  private:
110  Engine* const engine_;
111  const StorageId id_;
112  PartitionerMetadata* const metadata_;
114 
120  ErrorStack design_partition_first(const MasstreeIntermediatePage* root);
121 
122  void sort_batch_8bytes(const Partitioner::SortBatchArguments& args) const;
123  void sort_batch_general(const Partitioner::SortBatchArguments& args) const;
124 
130  ErrorStack read_page_safe(MasstreePage* src, MasstreePage* out);
131 };
132 
139  // only for reinterpret_cast
140  MasstreePartitionerData() = delete;
141  ~MasstreePartitionerData() = delete;
142 
144  uint16_t find_partition(const char* key, uint16_t key_length) const;
145 
149 };
150 
155 struct OwnerSamples {
156  OwnerSamples(uint32_t nodes, uint32_t subtrees) : nodes_(nodes), subtrees_(subtrees) {
157  occurrences_ = new uint32_t[nodes * subtrees];
158  std::memset(occurrences_, 0, sizeof(uint32_t) * nodes * subtrees);
159  assignments_ = new uint32_t[subtrees];
160  std::memset(assignments_, 0, sizeof(uint32_t) * subtrees);
161  }
163  delete[] occurrences_;
164  occurrences_ = nullptr;
165  delete[] assignments_;
166  assignments_ = nullptr;
167  }
168 
170  const uint32_t nodes_;
172  const uint32_t subtrees_;
174  uint32_t* occurrences_;
176  uint32_t* assignments_;
177 
178  void increment(uint32_t node, uint32_t subtree_id) {
179  ASSERT_ND(node < nodes_);
180  ASSERT_ND(subtree_id < subtrees_);
181  ++occurrences_[subtree_id * nodes_ + node];
182  }
183  uint32_t at(uint32_t node, uint32_t subtree_id) const {
184  ASSERT_ND(node < nodes_);
185  ASSERT_ND(subtree_id < subtrees_);
186  return occurrences_[subtree_id * nodes_ + node];
187  }
188 
189  uint32_t get_assignment(uint32_t subtree_id) const { return assignments_[subtree_id]; }
190 
192  void assign_owners();
193 
194  friend std::ostream& operator<<(std::ostream& o, const OwnerSamples& v);
195 };
196 
202 
203 inline int compare_keys(
204  const char* left,
205  uint16_t left_length,
206  const char* right,
207  uint16_t right_length) {
208  uint16_t cmp_length = std::min(left_length, right_length);
209  int result = std::memcmp(left, right, cmp_length);
210  if (result != 0) {
211  return result;
212  } else if (left_length < right_length) {
213  return -1;
214  } else if (left_length > right_length) {
215  return 1;
216  } else {
217  return 0;
218  }
219 }
220 
222  const snapshot::LogBuffer& log_buffer,
224  const MasstreeCommonLogType* rec = reinterpret_cast<const MasstreeCommonLogType*>(
225  log_buffer.resolve(pos));
230  return rec;
231 }
232 
233 } // namespace masstree
234 } // namespace storage
235 } // namespace foedus
236 #endif // FOEDUS_STORAGE_MASSTREE_MASSTREE_PARTITIONER_IMPL_HPP_
Packages handling of 4-bytes representation of position in log buffers.
Definition: log_buffer.hpp:38
0x0033 : foedus::storage::masstree::MasstreeInsertLogType .
Definition: log_type.hpp:127
uint16_t find_partition(const char *key, uint16_t key_length) const
Returns the partition (node ID) that should contain the key.
Definitions of IDs in this package and a few related constant values.
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
uint32_t at(uint32_t node, uint32_t subtree_id) const
uint32_t BufferPosition
Represents a position in some buffer.
Definition: snapshot_id.hpp:72
Declares all log types used in this storage type.
uint32_t * assignments_
node_id to be the owner of the subtree
Forward declarations of classes in root package.
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
uint64_t KeySlice
Each key slice is an 8-byte integer.
A base class for MasstreeInsertLogType/MasstreeDeleteLogType/MasstreeOverwriteLogType.
MasstreePartitioner(Partitioner *parent)
MasstreePartitioner methods.
Definitions of IDs in this package and a few related constant values.
Common base of MasstreeIntermediatePage and MasstreeBorderPage.
Number of vol pages in each node sampled per pointer in the root page.
0x0032 : foedus::storage::masstree::MasstreeOverwriteLogType .
Definition: log_type.hpp:126
void sort_batch(const Partitioner::SortBatchArguments &args) const
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
void assign_owners()
Determine assignments based on the samples.
uint32_t get_assignment(uint32_t subtree_id) const
const MasstreeCommonLogType * resolve_log(const snapshot::LogBuffer &log_buffer, snapshot::BufferPosition pos)
friend std::ostream & operator<<(std::ostream &o, const OwnerSamples &v)
uint32_t * occurrences_
number of occurences of a volatile page in the node.
Forward declarations of classes in memory package.
0x0034 : foedus::storage::masstree::MasstreeDeleteLogType .
Definition: log_type.hpp:128
Tiny metadata of partitioner for every storage used while log gleaning.
void increment(uint32_t node, uint32_t subtree_id)
0x0035 : foedus::storage::masstree::MasstreeUpdateLogType .
Definition: log_type.hpp:129
Partitioning and sorting logic for one storage.
Definition: partitioner.hpp:70
const uint32_t kMaxIntermediatePointers
Max number of pointers (if completely filled) stored in an intermediate pages.
LogCode get_type() const __attribute__((always_inline))
Convenience method to cast into LogCode.
const uint32_t subtrees_
number of pointers to children in the root page
Represents one intermediate page in Masstree 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
OwnerSamples(uint32_t nodes, uint32_t subtrees)
ErrorStack design_partition(const Partitioner::DesignPartitionArguments &args)
log::RecordLogType * resolve(BufferPosition position) const
Definition: log_buffer.hpp:42
void partition_batch(const Partitioner::PartitionBatchArguments &args) const
friend std::ostream & operator<<(std::ostream &o, const MasstreePartitioner &v)
int compare_keys(const char *left, uint16_t left_length, const char *right, uint16_t right_length)
Local utility functions.