libfoedus-core
FOEDUS Core Library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
log_buffer.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_LOG_BUFFER_HPP_
19 #define FOEDUS_SNAPSHOT_LOG_BUFFER_HPP_
20 
21 #include <iosfwd>
22 #include <string>
23 
24 #include "foedus/assert_nd.hpp"
25 #include "foedus/cxx11.hpp"
26 #include "foedus/error_code.hpp"
27 #include "foedus/fs/fwd.hpp"
31 
32 namespace foedus {
33 namespace snapshot {
38 struct LogBuffer {
39  explicit LogBuffer(char* base_address) : base_address_(base_address) {}
40  char* const base_address_;
41 
42  inline log::RecordLogType* resolve(BufferPosition position) const {
43  return reinterpret_cast<log::RecordLogType*>(
44  base_address_ + from_buffer_position(position));
45  }
46  inline BufferPosition compact(const log::RecordLogType* address) const {
47  return to_buffer_position(reinterpret_cast<const char*>(address) - base_address_);
48  }
49 };
50 
81 class SortedBuffer {
82  public:
83  SortedBuffer(char* buffer, uint64_t buffer_size, uint64_t total_size)
84  : buffer_(buffer),
85  buffer_size_(buffer_size),
86  offset_(0),
87  total_size_(total_size),
92  assert_checks();
93  }
94  virtual ~SortedBuffer() {}
95 
96  uint64_t to_relative_pos(uint64_t absolute_pos) const {
97  ASSERT_ND(absolute_pos >= offset_);
98  ASSERT_ND(absolute_pos <= offset_ + buffer_size_);
99  return absolute_pos - offset_;
100  }
101 
102  uint64_t to_absolute_pos(uint64_t relative_pos) const {
103  ASSERT_ND(relative_pos < buffer_size_);
104  return relative_pos + offset_;
105  }
106 
108  const char* get_buffer() const { return buffer_; }
109 
111  uint64_t get_buffer_size() const { return buffer_size_; }
112 
120  uint64_t get_offset() const { return offset_; }
121 
123  uint64_t get_total_size() const { return total_size_; }
124 
126  uint32_t get_cur_block_log_count() const { return cur_block_log_count_; }
127 
136 
138  storage::StorageId storage_id,
139  uint32_t log_count,
140  uint64_t abosulte_begin,
141  uint64_t abosulte_end,
142  uint32_t shortest_key_length,
143  uint32_t longest_key_length) {
144  cur_block_storage_id_ = storage_id;
145  cur_block_log_count_ = log_count;
146  cur_block_abosulte_begin_ = abosulte_begin;
147  cur_block_abosulte_end_ = abosulte_end;
148  cur_block_shortest_key_length_ = shortest_key_length;
149  cur_block_longest_key_length_ = longest_key_length;
150  assert_checks();
151  }
152  bool is_valid_current_block() const { return cur_block_storage_id_ != 0; }
154 
167  virtual ErrorCode wind(uint64_t next_absolute_pos) = 0;
168 
170  virtual std::string to_string() const = 0;
171 
173  virtual void describe(std::ostream* o) const = 0;
174 
175  friend std::ostream& operator<<(std::ostream& o, const SortedBuffer& v);
176 
177  void assert_checks() {
179  if (cur_block_storage_id_ != 0) {
183 
184  // did we overlook the begin/end of the block?
190  }
191  }
192 
193  protected:
194  char* const buffer_;
195  const uint64_t buffer_size_;
197  uint64_t offset_;
199  const uint64_t total_size_;
200 
212 
213  void describe_base_elements(std::ostream* o) const;
214 };
215 
226  public:
227  InMemorySortedBuffer(char* buffer, uint64_t buffer_size)
228  : SortedBuffer(buffer, buffer_size, buffer_size) {
229  }
231 
232  std::string to_string() const CXX11_OVERRIDE { return "<in-memory buffer>"; }
233  ErrorCode wind(uint64_t next_absolute_pos) CXX11_OVERRIDE {
234  ASSERT_ND(next_absolute_pos <= buffer_size_);
235  return kErrorCodeOk;
236  }
237  void describe(std::ostream* o) const CXX11_OVERRIDE;
238 };
239 
248  public:
256 
257  std::string to_string() const CXX11_OVERRIDE;
258  ErrorCode wind(uint64_t next_absolute_pos) CXX11_OVERRIDE;
259  void describe(std::ostream* o) const CXX11_OVERRIDE;
260 
261  fs::DirectIoFile* get_file() const { return file_; }
262  const memory::AlignedMemorySlice& get_io_buffer() const { return io_buffer_; }
263 
264  private:
265  enum Constants {
266  kAlignment = 1 << 12,
267  };
268  fs::DirectIoFile* const file_;
269  memory::AlignedMemorySlice const io_buffer_;
270 };
271 
272 } // namespace snapshot
273 } // namespace foedus
274 #endif // FOEDUS_SNAPSHOT_LOG_BUFFER_HPP_
Packages handling of 4-bytes representation of position in log buffers.
Definition: log_buffer.hpp:38
uint64_t to_absolute_pos(uint64_t relative_pos) const
Definition: log_buffer.hpp:102
ErrorCode wind(uint64_t next_absolute_pos) override
Loads next data block to be consumed by the caller (composer).
Definition: log_buffer.cpp:82
DumpFileSortedBuffer(fs::DirectIoFile *file, memory::AlignedMemorySlice io_buffer)
wraps the given file as a buffer.
Definition: log_buffer.cpp:57
Forward declarations of classes in filesystem package.
BufferPosition to_buffer_position(uint64_t byte_position)
Definition: snapshot_id.hpp:74
ErrorCode wind(uint64_t next_absolute_pos) override
Loads next data block to be consumed by the caller (composer).
Definition: log_buffer.hpp:233
Typedefs of ID types used in snapshot package.
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
uint64_t get_total_size() const
Returns the total size of the underlying stream (eg file size).
Definition: log_buffer.hpp:123
void describe(std::ostream *o) const override
Writes out a detailed description of this object to stream.
Definition: log_buffer.cpp:73
const uint64_t total_size_
see get_total_size()
Definition: log_buffer.hpp:199
uint64_t offset_
see get_offset()
Definition: log_buffer.hpp:197
const char * get_buffer() const
Returns the buffer memory.
Definition: log_buffer.hpp:108
uint32_t BufferPosition
Represents a position in some buffer.
Definition: snapshot_id.hpp:72
virtual ErrorCode wind(uint64_t next_absolute_pos)=0
Loads next data block to be consumed by the caller (composer).
STL namespace.
uint32_t get_cur_block_longest_key_length() const
Current storage block's longest key length.
Definition: log_buffer.hpp:135
std::string to_string() const override
Returns a short string that briefly describes this object.
Definition: log_buffer.cpp:69
Represents one input stream of sorted log entries.
Definition: log_buffer.hpp:81
Declares common log types used in all packages.
uint64_t cur_block_abosulte_begin_
see get_cur_block_abosulte_begin()
Definition: log_buffer.hpp:205
uint64_t get_offset() const
Returns the absolute byte position of the buffer's beginning in the entire file.
Definition: log_buffer.hpp:120
void describe_base_elements(std::ostream *o) const
Definition: log_buffer.cpp:37
uint64_t from_buffer_position(BufferPosition buffer_position)
Definition: snapshot_id.hpp:78
uint64_t get_cur_block_abosulte_begin() const
Current storage block's beginning in absolute byte position in the file.
Definition: log_buffer.hpp:129
0 means no-error.
Definition: error_code.hpp:87
virtual std::string to_string() const =0
Returns a short string that briefly describes this object.
fs::DirectIoFile * get_file() const
Definition: log_buffer.hpp:261
friend std::ostream & operator<<(std::ostream &o, const SortedBuffer &v)
Definition: log_buffer.cpp:32
storage::StorageId cur_block_storage_id_
If this is zero, this buffer is not set for reading any block.
Definition: log_buffer.hpp:202
virtual void describe(std::ostream *o) const =0
Writes out a detailed description of this object to stream.
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
uint32_t get_cur_block_shortest_key_length() const
Current storage block's shortest key length.
Definition: log_buffer.hpp:133
void describe(std::ostream *o) const override
Writes out a detailed description of this object to stream.
Definition: log_buffer.cpp:49
InMemorySortedBuffer(char *buffer, uint64_t buffer_size)
Definition: log_buffer.hpp:227
uint32_t get_cur_block_log_count() const
Definition: log_buffer.hpp:126
A slice of foedus::memory::AlignedMemory.
uint64_t to_relative_pos(uint64_t absolute_pos) const
Definition: log_buffer.hpp:96
Implementation of SortedBuffer that is backed by a dumped file.
Definition: log_buffer.hpp:247
uint32_t cur_block_shortest_key_length_
additional statistics for masstree/hash
Definition: log_buffer.hpp:209
std::string to_string() const override
Returns a short string that briefly describes this object.
Definition: log_buffer.hpp:232
BufferPosition compact(const log::RecordLogType *address) const
Definition: log_buffer.hpp:46
LogBuffer(char *base_address)
Definition: log_buffer.hpp:39
#define CXX11_OVERRIDE
Used in public headers in place of "override" of C++11.
Definition: cxx11.hpp:134
SortedBuffer(char *buffer, uint64_t buffer_size, uint64_t total_size)
Definition: log_buffer.hpp:83
uint32_t cur_block_longest_key_length_
additional statistics for masstree/hash
Definition: log_buffer.hpp:211
Represents an I/O stream on one file without filesystem caching.
uint64_t get_cur_block_abosulte_end() const
Current storage block's end in absolute byte position in the file.
Definition: log_buffer.hpp:131
const memory::AlignedMemorySlice & get_io_buffer() const
Definition: log_buffer.hpp:262
Base class for log type of record-wise operation.
#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
void set_current_block(storage::StorageId storage_id, uint32_t log_count, uint64_t abosulte_begin, uint64_t abosulte_end, uint32_t shortest_key_length, uint32_t longest_key_length)
Definition: log_buffer.hpp:137
Implementation of SortedBuffer that is backed by fully in-memory buffer.
Definition: log_buffer.hpp:225
bool is_valid_current_block() const
Definition: log_buffer.hpp:152
uint64_t cur_block_abosulte_end_
see get_cur_block_abosulte_end()
Definition: log_buffer.hpp:207
log::RecordLogType * resolve(BufferPosition position) const
Definition: log_buffer.hpp:42
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85
storage::StorageId get_cur_block_storage_id() const
Definition: log_buffer.hpp:125
uint64_t get_buffer_size() const
Returns the size of buffer memory.
Definition: log_buffer.hpp:111