This commit is contained in:
Scott E. Graves 2024-10-28 12:35:46 -05:00
parent 0650790c43
commit 5fcc59434b
2 changed files with 22 additions and 27 deletions

View File

@ -68,7 +68,7 @@ private:
private: private:
bool notified_ = false; bool notified_ = false;
std::size_t read_chunk_index_{}; std::size_t read_chunk_{};
boost::dynamic_bitset<> read_state_; boost::dynamic_bitset<> read_state_;
std::unique_ptr<std::thread> reader_thread_; std::unique_ptr<std::thread> reader_thread_;
std::unique_ptr<std::thread> download_thread_; std::unique_ptr<std::thread> download_thread_;
@ -77,8 +77,8 @@ private:
private: private:
void download_chunk(std::size_t chunk, bool skip_active, bool should_reset); void download_chunk(std::size_t chunk, bool skip_active, bool should_reset);
void download_range(std::size_t start_chunk_index, void download_range(std::size_t start_chunk, std::size_t end_chunk,
std::size_t end_chunk_index, bool should_reset); bool should_reset);
void set_modified(); void set_modified();

View File

@ -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, void open_file::download_range(std::size_t start_chunk, std::size_t end_chunk,
std::size_t end_chunk_index, bool should_reset) { bool should_reset) {
for (std::size_t chunk_index = start_chunk_index; for (std::size_t chunk = start_chunk; chunk <= end_chunk; ++chunk) {
chunk_index <= end_chunk_index; ++chunk_index) { download_chunk(chunk, false, should_reset);
download_chunk(chunk_index, false, should_reset);
if (get_api_error() != api_error::success) { if (get_api_error() != api_error::success) {
return; return;
} }
@ -305,9 +304,8 @@ auto open_file::native_operation(
read_state_.resize(is_empty_file ? 0U : last_chunk + 1U); read_state_.resize(is_empty_file ? 0U : last_chunk + 1U);
if (not is_empty_file) { if (not is_empty_file) {
for (std::size_t chunk_index = old_size; chunk_index <= last_chunk; for (std::size_t chunk = old_size; chunk <= last_chunk; ++chunk) {
++chunk_index) { read_state_.set(chunk);
read_state_.set(chunk_index);
} }
} }
@ -374,14 +372,13 @@ auto open_file::read(std::size_t read_size, std::uint64_t read_offset,
} }
file_lock.unlock(); file_lock.unlock();
const auto start_chunk_index = const auto start_chunk = static_cast<std::size_t>(read_offset / chunk_size_);
static_cast<std::size_t>(read_offset / chunk_size_); const auto end_chunk =
const auto end_chunk_index =
static_cast<std::size_t>((read_size + read_offset) / chunk_size_); static_cast<std::size_t>((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) { if (get_api_error() != api_error::success) {
return get_api_error(); return get_api_error();
} }
@ -495,7 +492,7 @@ void open_file::set_modified() {
void open_file::update_background_reader(std::size_t read_chunk) { void open_file::update_background_reader(std::size_t read_chunk) {
recur_mutex_lock reader_lock(file_mtx_); recur_mutex_lock reader_lock(file_mtx_);
read_chunk_index_ = read_chunk; read_chunk_ = read_chunk;
if (not reader_thread_ && not stop_requested_) { if (not reader_thread_ && not stop_requested_) {
reader_thread_ = std::make_unique<std::thread>([this]() { reader_thread_ = std::make_unique<std::thread>([this]() {
@ -513,10 +510,9 @@ void open_file::update_background_reader(std::size_t read_chunk) {
io_lock.unlock(); io_lock.unlock();
} else { } else {
do { do {
next_chunk = read_chunk_index_ = next_chunk = read_chunk_ =
((read_chunk_index_ + 1U) >= read_state_.size()) ((read_chunk_ + 1U) >= read_state_.size()) ? 0U
? 0U : read_chunk_ + 1U;
: read_chunk_index_ + 1U;
} while ((next_chunk != 0U) && (active_downloads_.find(next_chunk) != } while ((next_chunk != 0U) && (active_downloads_.find(next_chunk) !=
active_downloads_.end())); active_downloads_.end()));
@ -548,15 +544,14 @@ auto open_file::write(std::uint64_t write_offset, const data_buffer &data,
} }
write_lock.unlock(); write_lock.unlock();
const auto start_chunk_index = const auto start_chunk = static_cast<std::size_t>(write_offset / chunk_size_);
static_cast<std::size_t>(write_offset / chunk_size_); const auto end_chunk =
const auto end_chunk_index =
static_cast<std::size_t>((write_offset + data.size()) / chunk_size_); static_cast<std::size_t>((write_offset + data.size()) / chunk_size_);
update_background_reader(start_chunk_index); update_background_reader(start_chunk);
download_range(start_chunk_index, download_range(start_chunk, std::min(read_state_.size() - 1U, end_chunk),
std::min(read_state_.size() - 1U, end_chunk_index), true); true);
if (get_api_error() != api_error::success) { if (get_api_error() != api_error::success) {
return get_api_error(); return get_api_error();
} }