libfoedus-core
FOEDUS Core Library
snapshot_file_set.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 <map>
21 #include <ostream>
22 #include <utility>
23 
24 #include "foedus/engine.hpp"
28 #include "foedus/fs/path.hpp"
29 #include "foedus/storage/page.hpp"
30 
31 namespace foedus {
32 namespace cache {
33 
34 SnapshotFileSet::SnapshotFileSet(Engine* engine) : engine_(engine) {
35 }
36 
38  return kRetOk;
39 }
40 
42  ErrorStackBatch batch;
43  close_all();
44  return SUMMARIZE_ERROR_BATCH(batch);
45 }
46 
48  for (auto& files_in_a_snapshot : files_) {
49  auto& values = files_in_a_snapshot.second;
50  for (auto& file : values) {
51  file.second->close();
52  delete file.second;
53  }
54  values.clear();
55  }
56  files_.clear();
57 }
58 
60  snapshot::SnapshotId snapshot_id,
61  thread::ThreadGroupId node_id,
62  fs::DirectIoFile** out) {
63  *out = nullptr;
64  auto snapshot = files_.find(snapshot_id);
65  if (snapshot == files_.end()) {
66  files_.insert(
67  std::pair<snapshot::SnapshotId, std::map< thread::ThreadGroupId, fs::DirectIoFile* > >(
68  snapshot_id, std::map< thread::ThreadGroupId, fs::DirectIoFile* >()));
69  snapshot = files_.find(snapshot_id);
70  }
71  ASSERT_ND(snapshot != files_.end());
72  ASSERT_ND(snapshot->first == snapshot_id);
73  auto& the_map = snapshot->second;
74  auto node = the_map.find(node_id);
75  if (node != the_map.end()) {
76  *out = node->second;
77  return kErrorCodeOk;
78  } else {
80  snapshot_id,
81  node_id));
82  fs::DirectIoFile* file = new fs::DirectIoFile(path);
83  ErrorCode open_error = file->open(true, false, false, false);
84  if (open_error != kErrorCodeOk) {
85  delete file;
86  return open_error;
87  }
88  std::pair< thread::ThreadGroupId, fs::DirectIoFile* > entry(node_id, file);
89  the_map.insert(entry);
90  *out = file;
91  return kErrorCodeOk;
92  }
93 }
94 
96  fs::DirectIoFile* file;
97  CHECK_ERROR_CODE(get_or_open_file(page_id, &file));
98  storage::SnapshotLocalPageId local_page_id
101  file->seek(local_page_id * sizeof(storage::Page), fs::DirectIoFile::kDirectIoSeekSet));
102  CHECK_ERROR_CODE(file->read_raw(sizeof(storage::Page), out));
103  ASSERT_ND(reinterpret_cast<storage::Page*>(out)->get_header().page_id_ == page_id);
104  return kErrorCodeOk;
105 }
106 
108  storage::SnapshotPagePointer page_id_begin,
109  uint32_t page_count,
110  void* out) {
111  fs::DirectIoFile* file;
112  CHECK_ERROR_CODE(get_or_open_file(page_id_begin, &file));
113  storage::SnapshotLocalPageId local_page_id_begin
116  file->seek(local_page_id_begin * sizeof(storage::Page), fs::DirectIoFile::kDirectIoSeekSet));
117  CHECK_ERROR_CODE(file->read_raw(sizeof(storage::Page) * page_count, out));
118 #ifndef NDEBUG
119  storage::Page* pages = reinterpret_cast<storage::Page*>(out);
120  for (uint32_t i = 0; i < page_count; ++i) {
121  ASSERT_ND(pages[i].get_header().page_id_ == page_id_begin + i);
122  }
123 #endif // NDEBUG
124  return kErrorCodeOk;
125 }
126 
127 std::ostream& operator<<(std::ostream& o, const SnapshotFileSet& v) {
128  o << "<SnapshotFileSet>";
129  for (const auto& snapshot : v.files_) {
130  o << "<snapshot id=\"" << snapshot.first << "\">";
131  for (const auto& entry : snapshot.second) {
132  o << "<node id=\"" << entry.first
133  << "\" fd=\"" << entry.second->get_descriptor() << "\" />";
134  }
135  o << "</snapshot>";
136  }
137  return o;
138 }
139 
140 } // namespace cache
141 } // namespace foedus
ErrorStack uninitialize_once() override
uint64_t SnapshotLocalPageId
Represents a local page ID in each one snapshot file in some NUMA node.
Definition: storage_id.hpp:89
SnapshotLocalPageId extract_local_page_id_from_snapshot_pointer(SnapshotPagePointer pointer)
Definition: storage_id.hpp:91
ErrorCode read_page(storage::SnapshotPagePointer page_id, void *out)
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
std::ostream & operator<<(std::ostream &o, const HashFunc &v)
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
Holds a set of read-only file objects for snapshot files.
const EngineOptions & get_options() const
Definition: engine.cpp:39
ErrorCode read_raw(uint64_t desired_bytes, void *buffer)
A version that receives a raw pointer that has to be aligned (be careful to use this ver)...
Batches zero or more ErrorStack objects to represent in one ErrorStack.
0 means no-error.
Definition: error_code.hpp:87
Analogue of boost::filesystem::path.
Definition: path.hpp:37
uint64_t SnapshotPagePointer
Page ID of a snapshot page.
Definition: storage_id.hpp:79
snapshot::SnapshotOptions snapshot_
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
ErrorStack initialize_once() override
ErrorCode seek(uint64_t offset, SeekType seek_type)
Sets the position of the next byte to be written/extracted from/to the stream.
Just a marker to denote that the memory region represents a data page.
Definition: page.hpp:334
ErrorCode read_pages(storage::SnapshotPagePointer page_id_begin, uint32_t page_count, void *out)
Read contiguous pages in one shot.
uint16_t SnapshotId
Unique ID of Snapshot.
Definition: snapshot_id.hpp:43
std::string construct_snapshot_file_path(int snapshot_id, int node) const
'folder_path'/snapshot_'snapshot-id'node'node-id'.data.
#define SUMMARIZE_ERROR_BATCH(x)
This macro calls ErrorStackBatch::summarize() with automatically provided parameters.
#define CHECK_ERROR_CODE(x)
This macro calls x and checks its returned error code.
Definition: error_code.hpp:155
ErrorCode get_or_open_file(storage::SnapshotPagePointer page_pointer, fs::DirectIoFile **out)
Represents an I/O stream on one file without filesystem caching.
const ErrorStack kRetOk
Normal return value for no-error case.
#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
uint8_t ThreadGroupId
Typedef for an ID of ThreadGroup (NUMA node).
Definition: thread_id.hpp:38
The offset is set to offset bytes.
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85