libfoedus-core
FOEDUS Core Library
snapshot_writer_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_SNAPSHOT_SNAPSHOT_WRITER_IMPL_HPP_
19 #define FOEDUS_SNAPSHOT_SNAPSHOT_WRITER_IMPL_HPP_
20 #include <stdint.h>
21 
22 #include <iosfwd>
23 #include <string>
24 
25 #include "foedus/assert_nd.hpp"
26 #include "foedus/compiler.hpp"
27 #include "foedus/error_stack.hpp"
28 #include "foedus/fwd.hpp"
29 #include "foedus/fs/fwd.hpp"
30 #include "foedus/fs/path.hpp"
32 #include "foedus/snapshot/fwd.hpp"
34 #include "foedus/storage/page.hpp"
37 
38 namespace foedus {
39 namespace snapshot {
76 class SnapshotWriter final {
77  public:
79  Engine* engine,
80  uint16_t numa_node,
81  SnapshotId snapshot_id,
82  memory::AlignedMemory* pool_memory,
83  memory::AlignedMemory* intermediate_memory,
84  bool append = false);
86 
88  ErrorStack open();
89  bool is_opened() const { return snapshot_file_; }
94  bool close();
95 
96  SnapshotWriter() = delete;
97  SnapshotWriter(const SnapshotWriter &other) = delete;
98  SnapshotWriter& operator=(const SnapshotWriter &other) = delete;
99 
100 
101  uint16_t get_numa_node() const { return numa_node_; }
103  return reinterpret_cast<storage::Page*>(pool_memory_->get_block());
104  }
106  return pool_memory_->get_size() / storage::kPageSize;
107  }
109  return reinterpret_cast<storage::Page*>(intermediate_memory_->get_block());
110  }
112  return intermediate_memory_->get_size() / storage::kPageSize;
113  }
114 
115  storage::SnapshotPagePointer get_next_page_id() const { return next_page_id_; }
116  SnapshotId get_snapshot_id() const { return snapshot_id_; }
117 
119  ASSERT_ND(offset > 0);
120  ASSERT_ND(offset < get_page_size());
121  return get_page_base() + offset;
122  }
124  memory::PagePoolOffset offset = page - get_page_base();
125  ASSERT_ND(offset > 0);
126  ASSERT_ND(offset < get_page_size());
127  return offset;
128  }
129 
135  ErrorCode dump_pages(memory::PagePoolOffset from_page, uint32_t count) {
136  return dump_general(pool_memory_, from_page, count);
137  }
138 
145  return dump_general(intermediate_memory_, from_page, count);
146  }
147 
148  std::string to_string() const {
149  return "SnapshotWriter-" + std::to_string(numa_node_) + (append_ ? "(append)" : "");
150  }
151 
165  ErrorCode expand_pool_memory(uint32_t required_pages, bool retain_content);
166 
180  ErrorCode expand_intermediate_memory(uint32_t required_pages, bool retain_content);
181 
182  friend std::ostream& operator<<(std::ostream& o, const SnapshotWriter& v);
183 
184  private:
185  Engine* const engine_;
187  const uint16_t numa_node_;
189  const bool append_;
191  const SnapshotId snapshot_id_;
192 
197  memory::AlignedMemory* const pool_memory_;
198 
206  memory::AlignedMemory* const intermediate_memory_;
207 
209  fs::DirectIoFile* snapshot_file_;
210 
214  storage::SnapshotPagePointer next_page_id_;
215 
216  fs::Path get_snapshot_file_path() const;
217  ErrorCode dump_general(
218  memory::AlignedMemory* buffer,
219  memory::PagePoolOffset from_page,
220  uint32_t count);
221 };
222 } // namespace snapshot
223 } // namespace foedus
224 #endif // FOEDUS_SNAPSHOT_SNAPSHOT_WRITER_IMPL_HPP_
memory::PagePoolOffset resolve(storage::Page *page) __attribute__((always_inline))
Forward declarations of classes in filesystem package.
bool close()
Close the file and makes sure all writes become durable (including the directory entry).
Definitions of IDs in this package and a few related constant values.
ErrorCode expand_intermediate_memory(uint32_t required_pages, bool retain_content)
Expands intermediate_memory_ in case it is too small.
Typedefs of ID types used in snapshot package.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
uint32_t PagePoolOffset
Offset in PagePool that compactly represents the page address (unlike 8 bytes pointer).
Definition: memory_id.hpp:44
Typedefs of ID types used in thread package.
Forward declarations of classes in root package.
memory::PagePoolOffset get_page_size() const __attribute__((always_inline))
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
Forward declarations of classes in snapshot manager package.
ErrorCode dump_intermediates(memory::PagePoolOffset from_page, uint32_t count)
Write out pages that are contiguous in the sub intermediate page pool.
ErrorStack open()
Open the file so that the writer can start writing.
Analogue of boost::filesystem::path.
Definition: path.hpp:37
SnapshotWriter & operator=(const SnapshotWriter &other)=delete
uint64_t SnapshotPagePointer
Page ID of a snapshot page.
Definition: storage_id.hpp:79
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
memory::PagePoolOffset get_intermediate_size() const __attribute__((always_inline))
Just a marker to denote that the memory region represents a data page.
Definition: page.hpp:334
storage::Page * get_page_base() __attribute__((always_inline))
void * get_block() const
Returns the memory block.
friend std::ostream & operator<<(std::ostream &o, const SnapshotWriter &v)
uint16_t SnapshotId
Unique ID of Snapshot.
Definition: snapshot_id.hpp:43
uint64_t get_size() const
Returns the byte size of the memory block.
storage::Page * get_intermediate_base() __attribute__((always_inline))
storage::Page * resolve(memory::PagePoolOffset offset) __attribute__((always_inline))
Represents an I/O stream on one file without filesystem caching.
Represents one memory block aligned to actual OS/hardware pages.
ErrorCode expand_pool_memory(uint32_t required_pages, bool retain_content)
Expands pool_memory_ in case it is too small.
#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
ErrorCode dump_pages(memory::PagePoolOffset from_page, uint32_t count)
Write out pages that are contiguous in the main page pool.
#define ALWAYS_INLINE
A function suffix to hint that the function should always be inlined.
Definition: compiler.hpp:106
storage::SnapshotPagePointer get_next_page_id() const
const uint16_t kPageSize
A constant defining the page size (in bytes) of both snapshot pages and volatile pages.
Definition: storage_id.hpp:45
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85
Writes out one snapshot file for all data pages in one reducer.