libfoedus-core
FOEDUS Core Library
storage.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_STORAGE_HPP_
19 #define FOEDUS_STORAGE_STORAGE_HPP_
20 #include <iosfwd>
21 #include <string>
22 
23 #include "foedus/attachable.hpp"
24 #include "foedus/cxx11.hpp"
25 #include "foedus/epoch.hpp"
26 #include "foedus/error_stack.hpp"
27 #include "foedus/fwd.hpp"
28 #include "foedus/initializable.hpp"
29 #include "foedus/log/fwd.hpp"
31 #include "foedus/storage/fwd.hpp"
34 #include "foedus/thread/fwd.hpp"
35 #include "foedus/xct/fwd.hpp"
36 
37 namespace foedus {
38 namespace storage {
39 
40 StorageControlBlock* get_storage_control_block(Engine* engine, StorageId id);
41 StorageControlBlock* get_storage_control_block(Engine* engine, const StorageName& name);
42 
54  // this is backed by shared memory. not instantiation. just reinterpret_cast.
57 
58  bool exists() const { return status_ == kExists; }
59  bool is_valid_status() const {
61  }
62 
63  void initialize() {
68  }
69  void uninitialize() {
71  }
72 
85 
87  char padding_[
88  4096 - sizeof(soc::SharedMutex) - 8 - sizeof(DualPagePointer) - sizeof(Metadata)];
89 };
90 
115 template <typename CONTROL_BLOCK>
116 class Storage : public Attachable<CONTROL_BLOCK> {
117  public:
118  Storage() : Attachable<CONTROL_BLOCK>() {}
119  Storage(Engine* engine, CONTROL_BLOCK* control_block)
120  : Attachable<CONTROL_BLOCK>(engine, control_block) {}
121  Storage(Engine* engine, StorageControlBlock* control_block)
122  : Attachable<CONTROL_BLOCK>(engine, reinterpret_cast<CONTROL_BLOCK*>(control_block)) {}
124  Storage(Engine* engine, StorageId id)
125  : Attachable<CONTROL_BLOCK>(
126  engine,
127  reinterpret_cast<CONTROL_BLOCK*>(get_storage_control_block(engine, id))) {}
129  Storage(Engine* engine, const StorageName& name)
130  : Attachable<CONTROL_BLOCK>(
131  engine,
132  reinterpret_cast<CONTROL_BLOCK*>(get_storage_control_block(engine, name))) {}
133 
134  Storage(const Storage& other)
135  : Attachable<CONTROL_BLOCK>(other.engine_, other.control_block_) {}
136  Storage& operator=(const Storage& other) {
137  this->engine_ = other.engine_;
138  this->control_block_ = other.control_block_;
139  return *this;
140  }
141 
145  StorageId get_id() const { return get_metadata()->id_; }
146 
150  StorageType get_type() const { return get_metadata()->type_; }
151 
155  const StorageName& get_name() const { return get_metadata()->name_; }
156 
162  const Metadata* get_metadata() const {
163  return &(reinterpret_cast<const StorageControlBlock*>(this->control_block_)->meta_);
164  }
165 
169  bool exists() const {
170  return this->control_block_ != CXX11_NULLPTR &&
171  reinterpret_cast<const StorageControlBlock*>(this->control_block_)->exists();
172  }
173 
174  // The following only defines the "interface", it's not actual virtual method
175  // you can invoke in a polymorphic manner. No virtual methods in shared memory, unluckily.
176  // In other words, the followings are just 'concepts', tho we don't actually define template
177  // concepts which is taking forever to get into the c++ standard.
178 
189  // ErrorStack create(const Metadata &metadata);
190 
192  // ErrorStack drop();
193 };
194 
195 
196 CXX11_STATIC_ASSERT(sizeof(StorageControlBlock) == 1 << 12, "StorageControlBlock is not 4kb");
197 } // namespace storage
198 } // namespace foedus
199 #endif // FOEDUS_STORAGE_STORAGE_HPP_
Metadata meta_
common part of the metadata.
Definition: storage.hpp:84
Storage(Engine *engine, StorageId id)
Shorthand for engine->get_storage_manager()->get_storage(id)
Definition: storage.hpp:124
Represents a pointer to another page (usually a child page).
Definition: storage_id.hpp:271
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.
char padding_[ 4096-sizeof(soc::SharedMutex)-8-sizeof(DualPagePointer)-sizeof(Metadata)]
Just to make this exactly 4kb.
Definition: storage.hpp:88
Forward declarations of classes in log manager package.
#define CXX11_NULLPTR
Used in public headers in place of "nullptr" of C++11.
Definition: cxx11.hpp:132
Storage(Engine *engine, StorageControlBlock *control_block)
Definition: storage.hpp:121
soc::SharedMutex status_mutex_
The mutext to protect changing the status.
Definition: storage.hpp:78
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
void initialize(bool recursive=false)
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
Forward declarations of classes in transaction package.
Storage(Engine *engine, const StorageName &name)
Shorthand for engine->get_storage_manager()->get_storage(name)
Definition: storage.hpp:129
Forward declarations of classes in root package.
Storage(Engine *engine, CONTROL_BLOCK *control_block)
Definition: storage.hpp:119
DualPagePointer root_page_pointer_
Points to the root page (or something equivalent).
Definition: storage.hpp:82
Engine * engine_
Most attachable object stores an engine pointer (local engine), so we define it here.
Definition: attachable.hpp:107
The storage has been created and ready for use.
Definition: storage_id.hpp:158
const Metadata * get_metadata() const
Returns the metadata of this storage.
Definition: storage.hpp:162
A mutex that can be placed in shared memory and used from multiple processes.
Metadata of one storage.
Definition: metadata.hpp:58
Represents one key-value store.
Definition: storage.hpp:116
VolatilePagePointer volatile_pointer_
Definition: storage_id.hpp:308
StorageControlBlock * get_storage_control_block(Engine *engine, StorageId id)
Definition: storage.cpp:26
Forward declarations of classes in storage package.
CONTROL_BLOCK * 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
bool exists() const
Returns whether this storage is already created.
Definition: storage.hpp:169
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
assorted::FixedString< 60 > StorageName
Represents a unique name of a storage.
Definition: storage_id.hpp:58
SnapshotPagePointer snapshot_pointer_
Definition: storage_id.hpp:307
StorageName name_
the unique name of this storage.
Definition: metadata.hpp:107
StorageId get_id() const
Returns the unique ID of this storage.
Definition: storage.hpp:145
#define CXX11_STATIC_ASSERT(expr, message)
Used in public headers in place of "static_assert" of C++11.
Definition: cxx11.hpp:135
Attachable Resources on Shared Memory.
Definition: attachable.hpp:58
Storage & operator=(const Storage &other)
Definition: storage.hpp:136
StorageType
Type of the storage, such as hash.
Definition: storage_id.hpp:122
StorageType type_
type of the storage.
Definition: metadata.hpp:105
#define CXX11_FUNC_DELETE
Used in public headers in place of " = delete" of C++11.
Definition: cxx11.hpp:128
Initial state, which means the storage has not been created yet.
Definition: storage_id.hpp:156
StorageStatus status_
Status of the storage.
Definition: storage.hpp:80
Forward declarations of classes in thread package.
A base layout of shared data for all storage types.
Definition: storage.hpp:53
Storage(const Storage &other)
Definition: storage.hpp:134
StorageId id_
the unique ID of this storage.
Definition: metadata.hpp:103
StorageStatus
Status of a storage.
Definition: storage_id.hpp:154