Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

segment writer #2873

Merged
merged 29 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 0 additions & 242 deletions core/src/codecs/snapshot/SSAttrsFormat.cpp

This file was deleted.

72 changes: 0 additions & 72 deletions core/src/codecs/snapshot/SSAttrsFormat.h

This file was deleted.

72 changes: 72 additions & 0 deletions core/src/codecs/snapshot/SSBlockFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,78 @@ SSBlockFormat::read(const storage::FSHandlerPtr& fs_ptr, const std::string& file
fs_ptr->reader_ptr_->close();
}

void
SSBlockFormat::read(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, int64_t offset,
int64_t num_bytes, std::vector<uint8_t>& raw) {
if (offset < 0 || num_bytes <= 0) {
std::string err_msg = "Invalid input to read: " + file_path;
LOG_ENGINE_ERROR_ << err_msg;
throw Exception(SERVER_INVALID_ARGUMENT, err_msg);
}

if (!fs_ptr->reader_ptr_->open(file_path.c_str())) {
std::string err_msg = "Failed to open file: " + file_path + ", error: " + std::strerror(errno);
LOG_ENGINE_ERROR_ << err_msg;
throw Exception(SERVER_CANNOT_OPEN_FILE, err_msg);
}

size_t total_num_bytes;
fs_ptr->reader_ptr_->read(&total_num_bytes, sizeof(size_t));

offset += sizeof(size_t); // Beginning of file is num_bytes
if (offset + num_bytes > total_num_bytes) {
std::string err_msg = "Invalid input to read: " + file_path;
LOG_ENGINE_ERROR_ << err_msg;
throw Exception(SERVER_INVALID_ARGUMENT, err_msg);
}

raw.resize(num_bytes);
fs_ptr->reader_ptr_->seekg(offset);
fs_ptr->reader_ptr_->read(raw.data(), num_bytes);
fs_ptr->reader_ptr_->close();
}

void
SSBlockFormat::read(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, const ReadRanges& read_ranges,
std::vector<uint8_t>& raw) {
if (read_ranges.empty()) {
return;
}

if (!fs_ptr->reader_ptr_->open(file_path.c_str())) {
std::string err_msg = "Failed to open file: " + file_path + ", error: " + std::strerror(errno);
LOG_ENGINE_ERROR_ << err_msg;
throw Exception(SERVER_CANNOT_OPEN_FILE, err_msg);
}

size_t total_num_bytes;
fs_ptr->reader_ptr_->read(&total_num_bytes, sizeof(size_t));

int64_t total_bytes = 0;
for (auto& range : read_ranges) {
int64_t offset = range.offset_ + sizeof(size_t);
if (offset + range.num_bytes_ > total_num_bytes) {
std::string err_msg = "Invalid input to read: " + file_path;
LOG_ENGINE_ERROR_ << err_msg;
throw Exception(SERVER_INVALID_ARGUMENT, err_msg);
}

total_bytes += range.num_bytes_;
}

raw.clear();
raw.resize(total_bytes);
int64_t poz = 0;
for (auto& range : read_ranges) {
int64_t offset = range.offset_ + sizeof(size_t);
fs_ptr->reader_ptr_->seekg(offset);
fs_ptr->reader_ptr_->read(raw.data() + poz, range.num_bytes_);
poz += range.num_bytes_;
}

fs_ptr->reader_ptr_->close();
}

void
SSBlockFormat::write(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path,
const std::vector<uint8_t>& raw) {
Expand Down
Loading