libfoedus-core
FOEDUS Core Library
hash_metadata.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 <iostream>
21 #include <sstream>
22 #include <string>
23 
25 
26 namespace foedus {
27 namespace storage {
28 namespace hash {
29 std::string HashMetadata::describe() const {
30  std::stringstream o;
31  o << HashMetadataSerializer(const_cast<HashMetadata*>(this));
32  return o.str();
33 }
34 std::ostream& operator<<(std::ostream& o, const HashMetadata& v) {
35  o << HashMetadataSerializer(const_cast<HashMetadata*>(&v));
36  return o;
37 }
38 
39 ErrorStack HashMetadataSerializer::load(tinyxml2::XMLElement* element) {
40  CHECK_ERROR(load_base(element));
41  CHECK_ERROR(get_element(element, "bin_bits_", &data_casted_->bin_bits_))
42  return kRetOk;
43 }
44 
45 ErrorStack HashMetadataSerializer::save(tinyxml2::XMLElement* element) const {
46  CHECK_ERROR(save_base(element));
47  CHECK_ERROR(add_element(element, "bin_bits_", "", data_casted_->bin_bits_));
48  return kRetOk;
49 }
50 
51 void HashMetadata::set_capacity(uint64_t expected_records, double preferred_records_per_bin) {
52  if (expected_records == 0) {
53  expected_records = 1;
54  }
55  if (preferred_records_per_bin < 1) {
56  preferred_records_per_bin = 1;
57  }
58  uint64_t bin_count = expected_records / preferred_records_per_bin;
59  uint8_t bits;
60  for (bits = 0; bits < kHashMaxBinBits && ((1ULL << bits) < bin_count); ++bits) {
61  continue;
62  }
63  if (bits < kHashMinBinBits) {
64  bits = kHashMinBinBits;
65  }
66  bin_bits_ = bits;
68  ASSERT_ND(bin_bits_ <= kHashMaxBinBits);
69 }
70 
71 
72 } // namespace hash
73 } // namespace storage
74 } // namespace foedus
ErrorStack load(tinyxml2::XMLElement *element) override
Reads the content of this object from the given XML element.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
ErrorStack load_base(tinyxml2::XMLElement *element)
common routine for the implementation of load()
Definition: metadata.cpp:50
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
void set_capacity(uint64_t expected_records, double preferred_records_per_bin=5.0)
Use this method to set an appropriate value for bin_bits_.
uint8_t bin_bits_
Number of bins in exponent of two.
ErrorStack save(tinyxml2::XMLElement *element) const override
Writes the content of this object to the given XML element.
std::ostream & operator<<(std::ostream &o, const HashCombo &v)
Definition: hash_combo.cpp:37
ErrorStack save_base(tinyxml2::XMLElement *element) const
common routine for the implementation of save()
Definition: metadata.cpp:66
const uint8_t kHashMinBinBits
Minimum number allowed for bin-bits.
Definition: hash_id.hpp:150
#define CHECK_ERROR(x)
This macro calls x and checks its returned value.
const ErrorStack kRetOk
Normal return value for no-error case.
static ErrorStack get_element(tinyxml2::XMLElement *parent, const std::string &tag, T *out, bool optional=false, T value=0)
Only declaration in header.
Metadata of an hash storage.
const uint8_t kHashMaxBinBits
Maximum number allowed for bin-bits.
Definition: hash_id.hpp:159
#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
static ErrorStack add_element(tinyxml2::XMLElement *parent, const std::string &tag, const std::string &comment, T value)
Only declaration in header.