From 5fcc59434baed5ab6372161bfdfa536959f37c5e Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Mon, 28 Oct 2024 12:35:46 -0500 Subject: [PATCH] refactor --- .../include/file_manager/open_file.hpp | 6 +-- .../src/file_manager/open_file.cpp | 43 ++++++++----------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/repertory/librepertory/include/file_manager/open_file.hpp b/repertory/librepertory/include/file_manager/open_file.hpp index 42a101c1..3e031d01 100644 --- a/repertory/librepertory/include/file_manager/open_file.hpp +++ b/repertory/librepertory/include/file_manager/open_file.hpp @@ -68,7 +68,7 @@ private: private: bool notified_ = false; - std::size_t read_chunk_index_{}; + std::size_t read_chunk_{}; boost::dynamic_bitset<> read_state_; std::unique_ptr reader_thread_; std::unique_ptr download_thread_; @@ -77,8 +77,8 @@ private: private: void download_chunk(std::size_t chunk, bool skip_active, bool should_reset); - void download_range(std::size_t start_chunk_index, - std::size_t end_chunk_index, bool should_reset); + void download_range(std::size_t start_chunk, std::size_t end_chunk, + bool should_reset); void set_modified(); diff --git a/repertory/librepertory/src/file_manager/open_file.cpp b/repertory/librepertory/src/file_manager/open_file.cpp index 3903f392..3bfbc3ec 100644 --- a/repertory/librepertory/src/file_manager/open_file.cpp +++ b/repertory/librepertory/src/file_manager/open_file.cpp @@ -207,11 +207,10 @@ void open_file::download_chunk(std::size_t chunk, bool skip_active, } } -void open_file::download_range(std::size_t start_chunk_index, - std::size_t end_chunk_index, bool should_reset) { - for (std::size_t chunk_index = start_chunk_index; - chunk_index <= end_chunk_index; ++chunk_index) { - download_chunk(chunk_index, false, should_reset); +void open_file::download_range(std::size_t start_chunk, std::size_t end_chunk, + bool should_reset) { + for (std::size_t chunk = start_chunk; chunk <= end_chunk; ++chunk) { + download_chunk(chunk, false, should_reset); if (get_api_error() != api_error::success) { return; } @@ -305,9 +304,8 @@ auto open_file::native_operation( read_state_.resize(is_empty_file ? 0U : last_chunk + 1U); if (not is_empty_file) { - for (std::size_t chunk_index = old_size; chunk_index <= last_chunk; - ++chunk_index) { - read_state_.set(chunk_index); + for (std::size_t chunk = old_size; chunk <= last_chunk; ++chunk) { + read_state_.set(chunk); } } @@ -374,14 +372,13 @@ auto open_file::read(std::size_t read_size, std::uint64_t read_offset, } file_lock.unlock(); - const auto start_chunk_index = - static_cast(read_offset / chunk_size_); - const auto end_chunk_index = + const auto start_chunk = static_cast(read_offset / chunk_size_); + const auto end_chunk = static_cast((read_size + read_offset) / chunk_size_); - update_background_reader(start_chunk_index); + update_background_reader(start_chunk); - download_range(start_chunk_index, end_chunk_index, true); + download_range(start_chunk, end_chunk, true); if (get_api_error() != api_error::success) { return get_api_error(); } @@ -495,7 +492,7 @@ void open_file::set_modified() { void open_file::update_background_reader(std::size_t read_chunk) { recur_mutex_lock reader_lock(file_mtx_); - read_chunk_index_ = read_chunk; + read_chunk_ = read_chunk; if (not reader_thread_ && not stop_requested_) { reader_thread_ = std::make_unique([this]() { @@ -513,10 +510,9 @@ void open_file::update_background_reader(std::size_t read_chunk) { io_lock.unlock(); } else { do { - next_chunk = read_chunk_index_ = - ((read_chunk_index_ + 1U) >= read_state_.size()) - ? 0U - : read_chunk_index_ + 1U; + next_chunk = read_chunk_ = + ((read_chunk_ + 1U) >= read_state_.size()) ? 0U + : read_chunk_ + 1U; } while ((next_chunk != 0U) && (active_downloads_.find(next_chunk) != active_downloads_.end())); @@ -548,15 +544,14 @@ auto open_file::write(std::uint64_t write_offset, const data_buffer &data, } write_lock.unlock(); - const auto start_chunk_index = - static_cast(write_offset / chunk_size_); - const auto end_chunk_index = + const auto start_chunk = static_cast(write_offset / chunk_size_); + const auto end_chunk = static_cast((write_offset + data.size()) / chunk_size_); - update_background_reader(start_chunk_index); + update_background_reader(start_chunk); - download_range(start_chunk_index, - std::min(read_state_.size() - 1U, end_chunk_index), true); + download_range(start_chunk, std::min(read_state_.size() - 1U, end_chunk), + true); if (get_api_error() != api_error::success) { return get_api_error(); }