refactor
This commit is contained in:
parent
0650790c43
commit
5fcc59434b
@ -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<std::thread> reader_thread_;
|
||||
std::unique_ptr<std::thread> 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();
|
||||
|
||||
|
@ -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<std::size_t>(read_offset / chunk_size_);
|
||||
const auto end_chunk_index =
|
||||
const auto start_chunk = static_cast<std::size_t>(read_offset / chunk_size_);
|
||||
const auto end_chunk =
|
||||
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) {
|
||||
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<std::thread>([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<std::size_t>(write_offset / chunk_size_);
|
||||
const auto end_chunk_index =
|
||||
const auto start_chunk = static_cast<std::size_t>(write_offset / chunk_size_);
|
||||
const auto end_chunk =
|
||||
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,
|
||||
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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user