libfoedus-core
FOEDUS Core Library
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 <tinyxml2.h>
21 #include <glog/logging.h>
22 
23 #include <string>
24 
31 
32 namespace foedus {
33 namespace storage {
34 
35 std::string Metadata::describe(const Metadata& metadata) {
36  switch (metadata.type_) {
37  case kArrayStorage:
38  return static_cast<const array::ArrayMetadata&>(metadata).describe();
39  case kHashStorage:
40  return static_cast<const hash::HashMetadata&>(metadata).describe();
41  case kMasstreeStorage:
42  return static_cast<const masstree::MasstreeMetadata&>(metadata).describe();
43  case kSequentialStorage:
44  return static_cast<const sequential::SequentialMetadata&>(metadata).describe();
45  default:
46  return "Unknown metadata type";
47  }
48 }
49 
50 ErrorStack MetadataSerializer::load_base(tinyxml2::XMLElement* element) {
51  CHECK_ERROR(get_element(element, "id_", &data_->id_))
52  CHECK_ERROR(get_enum_element(element, "type_", &data_->type_))
53  CHECK_ERROR(get_element(element, "name_", &data_->name_))
54  CHECK_ERROR(get_element(element, "root_snapshot_page_id_", &data_->root_snapshot_page_id_))
56  element,
57  "snapshot_trigger_threshold_",
60  element,
61  "snapshot_keep_threshold_",
63  return kRetOk;
64 }
65 
66 ErrorStack MetadataSerializer::save_base(tinyxml2::XMLElement* element) const {
67  CHECK_ERROR(add_element(element, "id_", "", data_->id_));
68  CHECK_ERROR(add_enum_element(element, "type_", "", data_->type_));
69  CHECK_ERROR(add_element(element, "name_", "", data_->name_));
70  CHECK_ERROR(add_element(element, "root_snapshot_page_id_", "", data_->root_snapshot_page_id_));
72  element,
73  "snapshot_trigger_threshold_",
74  "",
77  element,
78  "snapshot_keep_threshold_",
79  "",
81  return kRetOk;
82 }
83 
84 
85 ErrorStack load_from_xml_array(tinyxml2::XMLElement* element, Metadata* data) {
86  array::ArrayMetadataSerializer serializer(reinterpret_cast<array::ArrayMetadata*>(data));
87  return serializer.load(element);
88 }
89 ErrorStack load_from_xml_hash(tinyxml2::XMLElement* element, Metadata* data) {
90  hash::HashMetadataSerializer serializer(reinterpret_cast<hash::HashMetadata*>(data));
91  return serializer.load(element);
92 }
93 ErrorStack load_from_xml_masstree(tinyxml2::XMLElement* element, Metadata* data) {
95  reinterpret_cast<masstree::MasstreeMetadata*>(data));
96  return serializer.load(element);
97 }
98 ErrorStack load_from_xml_sequential(tinyxml2::XMLElement* element, Metadata* data) {
100  reinterpret_cast<sequential::SequentialMetadata*>(data));
101  return serializer.load(element);
102 }
103 
104 const char* kChildTagName = "storage";
105 
106 ErrorStack save_to_xml_array(tinyxml2::XMLElement* parent, Metadata* data) {
107  array::ArrayMetadataSerializer serializer(reinterpret_cast<array::ArrayMetadata*>(data));
108  return externalize::Externalizable::add_child_element(parent, kChildTagName, "", serializer);
109 }
110 ErrorStack save_to_xml_hash(tinyxml2::XMLElement* parent, Metadata* data) {
111  hash::HashMetadataSerializer serializer(reinterpret_cast<hash::HashMetadata*>(data));
112  return externalize::Externalizable::add_child_element(parent, kChildTagName, "", serializer);
113 }
114 ErrorStack save_to_xml_masstree(tinyxml2::XMLElement* parent, Metadata* data) {
116  reinterpret_cast<masstree::MasstreeMetadata*>(data));
117  return externalize::Externalizable::add_child_element(parent, kChildTagName, "", serializer);
118 }
119 ErrorStack save_to_xml_sequential(tinyxml2::XMLElement* parent, Metadata* data) {
121  reinterpret_cast<sequential::SequentialMetadata*>(data));
122  return externalize::Externalizable::add_child_element(parent, kChildTagName, "", serializer);
123 }
124 
126  storage::StorageId largest_storage_id,
127  tinyxml2::XMLElement* parent,
128  StorageControlBlock* blocks) {
129  uint32_t loaded_count = 0;
130  for (tinyxml2::XMLElement* element = parent->FirstChildElement(kChildTagName);
131  element;
132  element = element->NextSiblingElement(kChildTagName)) {
133  StorageId id;
134  CHECK_ERROR(get_element(element, "id_", &id))
135  ASSERT_ND(id > 0);
136  ASSERT_ND(id <= largest_storage_id);
138  CHECK_ERROR(get_enum_element(element, "type_", &type));
139 
140  VLOG(0) << "Loading metadata of storage-" << id << " from xml";
141  ASSERT_ND(blocks[id].status_ == kNotExists);
142  Metadata* data = &blocks[id].meta_;
143  blocks[id].status_ = kExists;
144  switch (type) {
145  case kArrayStorage:
146  CHECK_ERROR(load_from_xml_array(element, data));
147  break;
148  case kHashStorage:
149  CHECK_ERROR(load_from_xml_hash(element, data));
150  break;
151  case kMasstreeStorage:
152  CHECK_ERROR(load_from_xml_masstree(element, data));
153  break;
154  case kSequentialStorage:
155  CHECK_ERROR(load_from_xml_sequential(element, data));
156  break;
157  default:
159  }
160  ++loaded_count;
161  }
162  LOG(INFO) << "Loaded metadata of " << loaded_count << " storages";
163  return kRetOk;
164 }
165 
167  storage::StorageId largest_storage_id,
168  tinyxml2::XMLElement* parent,
169  StorageControlBlock* blocks) {
170  uint32_t saved_count = 0;
171  for (storage::StorageId id = 1; id <= largest_storage_id; ++id) {
172  ASSERT_ND(blocks[id].is_valid_status());
173  if (!blocks[id].exists()) {
174  continue;
175  }
176  StorageType type = blocks[id].meta_.type_;
177  Metadata* data = &blocks[id].meta_;
178  switch (type) {
179  case kArrayStorage:
180  CHECK_ERROR(save_to_xml_array(parent, data));
181  break;
182  case kHashStorage:
183  CHECK_ERROR(save_to_xml_hash(parent, data));
184  break;
185  case kMasstreeStorage:
186  CHECK_ERROR(save_to_xml_masstree(parent, data));
187  break;
188  case kSequentialStorage:
189  CHECK_ERROR(save_to_xml_sequential(parent, data));
190  break;
191  default:
193  }
194  ++saved_count;
195  }
196  LOG(INFO) << "Written metadata of " << saved_count << " storages";
197  return kRetOk;
198 }
199 
200 } // namespace storage
201 } // namespace foedus
Metadata meta_
common part of the metadata.
Definition: storage.hpp:84
ErrorStack load(tinyxml2::XMLElement *element) override
Reads the content of this object from the given XML element.
Definitions of IDs in this package and a few related constant values.
ErrorStack save_to_xml_hash(tinyxml2::XMLElement *parent, Metadata *data)
Definition: metadata.cpp:110
#define ERROR_STACK(e)
Instantiates ErrorStack with the given foedus::error_code, creating an error stack with the current f...
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
ErrorStack load_base(tinyxml2::XMLElement *element)
common routine for the implementation of load()
Definition: metadata.cpp:50
ErrorStack load(tinyxml2::XMLElement *element) override
Reads the content of this object from the given XML element.
static ErrorStack add_child_element(tinyxml2::XMLElement *parent, const std::string &tag, const std::string &comment, const Externalizable &child)
child Externalizable version
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
The storage has been created and ready for use.
Definition: storage_id.hpp:158
0 indicates invalid type.
Definition: storage_id.hpp:124
ErrorStack save_to_xml_masstree(tinyxml2::XMLElement *parent, Metadata *data)
Definition: metadata.cpp:114
ErrorStack load_from_xml_sequential(tinyxml2::XMLElement *element, Metadata *data)
Definition: metadata.cpp:98
static ErrorStack add_enum_element(tinyxml2::XMLElement *parent, const std::string &tag, const std::string &comment, ENUM value)
enum version
Metadata of one storage.
Definition: metadata.hpp:58
ErrorStack save_to_xml_sequential(tinyxml2::XMLElement *parent, Metadata *data)
Definition: metadata.cpp:119
uint32_t snapshot_keep_threshold_
[Only partially implemented] When a log gleaner created new snapshot pages for this storage...
Definition: metadata.hpp:76
StorageName name_
the unique name of this storage.
Definition: metadata.hpp:107
Metadata of an array storage.
bool exists(const Path &p)
Returns if the file exists.
Definition: filesystem.hpp:128
ErrorStack load_from_xml_hash(tinyxml2::XMLElement *element, Metadata *data)
Definition: metadata.cpp:89
SnapshotThresholds snapshot_thresholds_
Definition: metadata.hpp:114
ErrorStack save_to_xml_array(tinyxml2::XMLElement *parent, Metadata *data)
Definition: metadata.cpp:106
StorageType
Type of the storage, such as hash.
Definition: storage_id.hpp:122
StorageType type_
type of the storage.
Definition: metadata.hpp:105
static ErrorStack save_all_storages_to_xml(storage::StorageId largest_storage_id, tinyxml2::XMLElement *element, StorageControlBlock *blocks)
Definition: metadata.cpp:166
ErrorStack save_base(tinyxml2::XMLElement *element) const
common routine for the implementation of save()
Definition: metadata.cpp:66
static std::string describe(const Metadata &metadata)
to_string operator of all Metadata objects.
Definition: metadata.cpp:35
#define CHECK_ERROR(x)
This macro calls x and checks its returned value.
const char * kChildTagName
Definition: metadata.cpp:104
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.
uint32_t snapshot_trigger_threshold_
[Not implemented yet] If this storage has more than this number of volatile pages, log gleaner will soon start to drop some of the volatile pages.
Definition: metadata.hpp:67
Metadata of an hash storage.
Initial state, which means the storage has not been created yet.
Definition: storage_id.hpp:156
ErrorStack load(tinyxml2::XMLElement *element) override
Reads the content of this object from the given XML element.
0x0807 : "STORAGE: This metadata type is not yet supported" .
Definition: error_code.hpp:173
ErrorStack load(tinyxml2::XMLElement *element) override
Reads the content of this object from the given XML element.
static ErrorStack get_enum_element(tinyxml2::XMLElement *parent, const std::string &tag, ENUM *out, bool optional=false, ENUM default_value=static_cast< ENUM >(0))
enum version
#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
StorageStatus status_
Status of the storage.
Definition: storage.hpp:80
ErrorStack load_from_xml_masstree(tinyxml2::XMLElement *element, Metadata *data)
Definition: metadata.cpp:93
A base layout of shared data for all storage types.
Definition: storage.hpp:53
StorageId id_
the unique ID of this storage.
Definition: metadata.hpp:103
ErrorStack load_from_xml_array(tinyxml2::XMLElement *element, Metadata *data)
Definition: metadata.cpp:85
static ErrorStack add_element(tinyxml2::XMLElement *parent, const std::string &tag, const std::string &comment, T value)
Only declaration in header.
static ErrorStack load_all_storages_from_xml(storage::StorageId largest_storage_id, tinyxml2::XMLElement *element, StorageControlBlock *blocks)
Definition: metadata.cpp:125
SnapshotPagePointer root_snapshot_page_id_
Pointer to a snapshotted page this storage is rooted at.
Definition: metadata.hpp:112