libfoedus-core
FOEDUS Core Library
foedus::snapshot::DumpFileSortedBuffer Class Referencefinal

Implementation of SortedBuffer that is backed by a dumped file. More...

Detailed Description

Implementation of SortedBuffer that is backed by a dumped file.

When the reducer's buffer becomes full, it sorts all entries in it and dumps it to a file (sorted run file). This stream sequentially reads from the file.

Definition at line 247 of file log_buffer.hpp.

#include <log_buffer.hpp>

Inheritance diagram for foedus::snapshot::DumpFileSortedBuffer:
Collaboration diagram for foedus::snapshot::DumpFileSortedBuffer:

Public Member Functions

 DumpFileSortedBuffer (fs::DirectIoFile *file, memory::AlignedMemorySlice io_buffer)
 wraps the given file as a buffer. More...
 
 ~DumpFileSortedBuffer () override
 
std::string to_string () const override
 Returns a short string that briefly describes this object. More...
 
ErrorCode wind (uint64_t next_absolute_pos) override
 Loads next data block to be consumed by the caller (composer). More...
 
void describe (std::ostream *o) const override
 Writes out a detailed description of this object to stream. More...
 
fs::DirectIoFileget_file () const
 
const memory::AlignedMemorySliceget_io_buffer () const
 
- Public Member Functions inherited from foedus::snapshot::SortedBuffer
 SortedBuffer (char *buffer, uint64_t buffer_size, uint64_t total_size)
 
virtual ~SortedBuffer ()
 
uint64_t to_relative_pos (uint64_t absolute_pos) const
 
uint64_t to_absolute_pos (uint64_t relative_pos) const
 
const char * get_buffer () const
 Returns the buffer memory. More...
 
uint64_t get_buffer_size () const
 Returns the size of buffer memory. More...
 
uint64_t get_offset () const
 Returns the absolute byte position of the buffer's beginning in the entire file. More...
 
uint64_t get_total_size () const
 Returns the total size of the underlying stream (eg file size). More...
 
storage::StorageId get_cur_block_storage_id () const
 
uint32_t get_cur_block_log_count () const
 
uint64_t get_cur_block_abosulte_begin () const
 Current storage block's beginning in absolute byte position in the file. More...
 
uint64_t get_cur_block_abosulte_end () const
 Current storage block's end in absolute byte position in the file. More...
 
uint32_t get_cur_block_shortest_key_length () const
 Current storage block's shortest key length. More...
 
uint32_t get_cur_block_longest_key_length () const
 Current storage block's longest key length. More...
 
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)
 
bool is_valid_current_block () const
 
void invalidate_current_block ()
 
void assert_checks ()
 

Additional Inherited Members

- Protected Member Functions inherited from foedus::snapshot::SortedBuffer
void describe_base_elements (std::ostream *o) const
 
- Protected Attributes inherited from foedus::snapshot::SortedBuffer
char *const buffer_
 
const uint64_t buffer_size_
 
uint64_t offset_
 see get_offset() More...
 
const uint64_t total_size_
 see get_total_size() More...
 
storage::StorageId cur_block_storage_id_
 If this is zero, this buffer is not set for reading any block. More...
 
uint32_t cur_block_log_count_
 
uint64_t cur_block_abosulte_begin_
 see get_cur_block_abosulte_begin() More...
 
uint64_t cur_block_abosulte_end_
 see get_cur_block_abosulte_end() More...
 
uint32_t cur_block_shortest_key_length_
 additional statistics for masstree/hash More...
 
uint32_t cur_block_longest_key_length_
 additional statistics for masstree/hash More...
 

Constructor & Destructor Documentation

foedus::snapshot::DumpFileSortedBuffer::DumpFileSortedBuffer ( fs::DirectIoFile file,
memory::AlignedMemorySlice  io_buffer 
)

wraps the given file as a buffer.

note that this object does NOT take ownership of anything given to this constructor. it doesn't delete/open file or memory. it's caller's responsibility (in other words, this object is just a view. so is InMemorySortedBuffer).

Definition at line 57 of file log_buffer.cpp.

References ASSERT_ND, foedus::snapshot::SortedBuffer::buffer_size_, and foedus::snapshot::SortedBuffer::total_size_.

59  : SortedBuffer(
60  reinterpret_cast<char*>(io_buffer.get_block()),
61  io_buffer.get_size(),
62  fs::file_size(file->get_path())),
63  file_(file),
64  io_buffer_(io_buffer) {
65  ASSERT_ND(buffer_size_ % kAlignment == 0);
66  ASSERT_ND(total_size_ % kAlignment == 0);
67 }
const uint64_t total_size_
see get_total_size()
Definition: log_buffer.hpp:199
uint64_t file_size(const Path &p)
Returns size of the file.
Definition: filesystem.cpp:120
SortedBuffer(char *buffer, uint64_t buffer_size, uint64_t total_size)
Definition: log_buffer.hpp:83
#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
foedus::snapshot::DumpFileSortedBuffer::~DumpFileSortedBuffer ( )
inlineoverride

Definition at line 255 of file log_buffer.hpp.

255 {}

Member Function Documentation

void foedus::snapshot::DumpFileSortedBuffer::describe ( std::ostream *  o) const
overridevirtual

Writes out a detailed description of this object to stream.

Implements foedus::snapshot::SortedBuffer.

Definition at line 73 of file log_buffer.cpp.

References foedus::snapshot::SortedBuffer::describe_base_elements().

73  {
74  std::ostream& o = *optr;
75  o << "<DumpFileSortedBuffer>";
77  o << "<file_>" << file_ << "</file_>";
78  o << "<io_buffer_>" << io_buffer_ << "</io_buffer_>";
79  o << "</DumpFileSortedBuffer>";
80 }
void describe_base_elements(std::ostream *o) const
Definition: log_buffer.cpp:37

Here is the call graph for this function:

fs::DirectIoFile* foedus::snapshot::DumpFileSortedBuffer::get_file ( ) const
inline

Definition at line 261 of file log_buffer.hpp.

261 { return file_; }
const memory::AlignedMemorySlice& foedus::snapshot::DumpFileSortedBuffer::get_io_buffer ( ) const
inline

Definition at line 262 of file log_buffer.hpp.

262 { return io_buffer_; }
std::string foedus::snapshot::DumpFileSortedBuffer::to_string ( ) const
overridevirtual

Returns a short string that briefly describes this object.

Implements foedus::snapshot::SortedBuffer.

Definition at line 69 of file log_buffer.cpp.

References foedus::fs::DirectIoFile::get_path(), and foedus::fs::Path::string().

69  {
70  return std::string("DumpFileSortedBuffer: ") + file_->get_path().string();
71 }
const std::string & string() const
Definition: path.hpp:65

Here is the call graph for this function:

ErrorCode foedus::snapshot::DumpFileSortedBuffer::wind ( uint64_t  next_absolute_pos)
overridevirtual

Loads next data block to be consumed by the caller (composer).

Parameters
[in]next_absolute_posthe absolute byte position that will be guaranteed to be within the buffer after this method call. Note that this position might not become 0-th byte position after this method call because the buffer might be backed by an aligned File I/O.
Precondition
next_absolute_pos <= get_total_size()
next_absolute_pos >= get_offset() : we allow foward-only iteration.
next_absolute_pos <= get_offset() + get_buffer_size() : we allow contiguous-read only.
Postcondition
get_offset() + get_buffer_size() > next_absolute_pos >= get_offset()
Returns
File I/O related errors only

Implements foedus::snapshot::SortedBuffer.

Definition at line 82 of file log_buffer.cpp.

References foedus::snapshot::SortedBuffer::assert_checks(), ASSERT_ND, foedus::snapshot::SortedBuffer::buffer_, foedus::snapshot::SortedBuffer::buffer_size_, CHECK_ERROR_CODE, foedus::fs::DirectIoFile::get_current_offset(), foedus::kErrorCodeInvalidParameter, foedus::kErrorCodeOk, foedus::snapshot::SortedBuffer::offset_, foedus::fs::DirectIoFile::read(), and foedus::snapshot::SortedBuffer::total_size_.

82  {
83  ASSERT_ND(offset_ % kAlignment == 0);
84  assert_checks();
85  if (next_absolute_pos == offset_ || offset_ + buffer_size_ >= total_size_) {
86  return kErrorCodeOk; // nothing to do then
87  } else if (next_absolute_pos < offset_ || // backward wind
88  next_absolute_pos >= offset_ + buffer_size_ || // jumps more than one buffer
89  next_absolute_pos >= total_size_) { // jumps exceeding the end of file
90  // this class doesn't support these operations. We shouldn't need them in any case.
91  LOG(FATAL) << " wtf next_absolute_pos=" << next_absolute_pos << ", offset=" << offset_
92  << ", buffer_size=" << buffer_size_ << ", total_size_=" << total_size_;
94  }
95 
96  // suppose buf=64M and we have read second window(64M-128M) and now moving on to third window.
97  // in the easiest case, current offset_=64M, next_absolute_pos=128M. we just read 64M.
98  // but, probably next_absolute_pos=128M-alpha, further alpha might not be 4k-aligned.
99  // the following code takes care of those cases.
101  uint64_t retained_bytes
102  = assorted::align<uint64_t, kAlignment>(offset_ + buffer_size_ - next_absolute_pos);
103  ASSERT_ND(retained_bytes <= buffer_size_);
104  std::memmove(buffer_, buffer_ + buffer_size_ - retained_bytes, retained_bytes);
105 
106  // we read as much as possible. note that "next_absolute_pos + buffer_size_" might become larger
107  // than cur_block_abosulte_end_. It's fine, the next DumpFileSortedBuffer object for another
108  // storage will take care (and make use) of the excessive bytes.
109  uint64_t desired_reads = std::min(
110  buffer_size_ - retained_bytes,
112  memory::AlignedMemorySlice sub_slice(io_buffer_, retained_bytes, desired_reads);
113  CHECK_ERROR_CODE(file_->read(desired_reads, sub_slice));
114  offset_ = offset_ + buffer_size_ - retained_bytes;
115 
116  ASSERT_ND(offset_ % kAlignment == 0);
117  ASSERT_ND(next_absolute_pos >= offset_);
118  assert_checks();
119  return kErrorCodeOk;
120 }
0x0002 : "GENERAL: Invalid parameter given" .
Definition: error_code.hpp:106
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
uint64_t get_current_offset() const
0 means no-error.
Definition: error_code.hpp:87
ErrorCode read(uint64_t desired_bytes, foedus::memory::AlignedMemory *buffer)
Sequentially read the given amount of contents from the current position.
#define CHECK_ERROR_CODE(x)
This macro calls x and checks its returned error code.
Definition: error_code.hpp:155
#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

Here is the call graph for this function:


The documentation for this class was generated from the following files: