2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright 2021-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-11-28 01:58:35 +00:00
|
|
|
#include <memory>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2021-11-09 03:30:03 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2021-11-25 18:26:51 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/completion.h"
|
|
|
|
#include "libtransmission/torrent.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
2007-11-12 22:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
uint64_t tr_completion::compute_has_valid() const
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t size = 0;
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
for (tr_piece_index_t piece = 0, n_pieces = block_info_->piece_count(); piece < n_pieces; ++piece)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (has_piece(piece))
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
size += block_info_->piece_size(piece);
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return size;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
uint64_t tr_completion::has_valid() const
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (!has_valid_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const val = compute_has_valid();
|
2022-06-01 16:56:59 +00:00
|
|
|
has_valid_ = val;
|
|
|
|
return val;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return *has_valid_;
|
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
uint64_t tr_completion::compute_size_when_done() const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (has_all())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return block_info_->total_size();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
// count bytes that we want or that we already have
|
2022-01-07 16:48:08 +00:00
|
|
|
auto size = uint64_t{ 0 };
|
2023-04-23 01:25:55 +00:00
|
|
|
for (tr_piece_index_t piece = 0, n_pieces = block_info_->piece_count(); piece < n_pieces; ++piece)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (tor_->piece_is_wanted(piece))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
size += block_info_->piece_size(piece);
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
size += count_has_bytes_in_piece(piece);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return size;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
uint64_t tr_completion::size_when_done() const
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (!size_when_done_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const value = compute_size_when_done();
|
2022-06-01 16:56:59 +00:00
|
|
|
size_when_done_ = value;
|
|
|
|
return value;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
|
|
|
|
return *size_when_done_;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void tr_completion::amount_done(float* tab, size_t n_tabs) const
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (n_tabs < 1)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const blocks_per_tab = std::size(blocks_) / n_tabs;
|
|
|
|
for (size_t i = 0; i < n_tabs; ++i)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2021-11-29 04:11:30 +00:00
|
|
|
auto const begin = i * blocks_per_tab;
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const end = std::min(begin + blocks_per_tab, std::size(blocks_));
|
|
|
|
auto const numerator = blocks_.count(begin, end);
|
2021-12-17 05:47:51 +00:00
|
|
|
tab[i] = float(numerator) / (end - begin);
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2007-09-26 01:55:04 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
std::vector<uint8_t> tr_completion::create_piece_bitfield() const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
size_t const n = block_info_->piece_count();
|
2021-11-25 18:26:51 +00:00
|
|
|
auto pieces = tr_bitfield{ n };
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2022-08-26 18:35:28 +00:00
|
|
|
// NOLINTNEXTLINE modernize-avoid-c-arrays
|
2021-11-28 01:58:35 +00:00
|
|
|
auto flags = std::make_unique<bool[]>(n);
|
2021-11-25 18:26:51 +00:00
|
|
|
for (tr_piece_index_t piece = 0; piece < n; ++piece)
|
2021-10-14 19:26:38 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
flags[piece] = has_piece(piece);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
pieces.set_from_bools(flags.get(), n);
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return pieces.raw();
|
2009-01-02 17:01:55 +00:00
|
|
|
}
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- mutators
|
2021-11-25 18:26:51 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void tr_completion::add_block(tr_block_index_t block)
|
2009-01-12 21:59:53 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (has_block(block))
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return; // already had it
|
2012-12-15 04:28:19 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
blocks_.set(block);
|
2023-04-23 01:25:55 +00:00
|
|
|
size_now_ += block_info_->block_size(block);
|
2021-11-25 18:26:51 +00:00
|
|
|
|
2022-07-04 16:48:54 +00:00
|
|
|
size_when_done_.reset();
|
2021-11-25 18:26:51 +00:00
|
|
|
has_valid_.reset();
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void tr_completion::set_blocks(tr_bitfield blocks)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
TR_ASSERT(std::size(blocks_) == std::size(blocks));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
blocks_ = std::move(blocks);
|
2023-04-23 01:25:55 +00:00
|
|
|
size_now_ = count_has_bytes_in_span({ 0, block_info_->total_size() });
|
2021-11-25 18:26:51 +00:00
|
|
|
size_when_done_.reset();
|
|
|
|
has_valid_.reset();
|
|
|
|
}
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void tr_completion::set_has_all() noexcept
|
2022-02-14 19:17:51 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const total_size = block_info_->total_size();
|
2022-02-14 19:17:51 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
blocks_.set_has_all();
|
2022-02-14 19:17:51 +00:00
|
|
|
size_now_ = total_size;
|
|
|
|
size_when_done_ = total_size;
|
|
|
|
has_valid_ = total_size;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void tr_completion::add_piece(tr_piece_index_t piece)
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const span = block_info_->block_span_for_piece(piece);
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2022-08-02 23:34:53 +00:00
|
|
|
for (tr_block_index_t block = span.begin; block < span.end; ++block)
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
add_block(block);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void tr_completion::remove_block(tr_block_index_t block)
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (!has_block(block))
|
2023-02-14 01:51:23 +00:00
|
|
|
{
|
|
|
|
return; // already didn't have it
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks_.unset(block);
|
2023-04-23 01:25:55 +00:00
|
|
|
size_now_ -= block_info_->block_size(block);
|
2023-02-14 01:51:23 +00:00
|
|
|
|
2022-07-04 16:48:54 +00:00
|
|
|
size_when_done_.reset();
|
2021-11-25 18:26:51 +00:00
|
|
|
has_valid_.reset();
|
2023-02-14 01:51:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void tr_completion::remove_piece(tr_piece_index_t piece)
|
2023-02-14 01:51:23 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
for (auto [block, end] = block_info_->block_span_for_piece(piece); block < end; ++block)
|
2023-02-14 01:51:23 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
remove_block(block);
|
2023-02-14 01:51:23 +00:00
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2011-01-31 16:43:37 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
uint64_t tr_completion::count_has_bytes_in_span(tr_byte_span_t span) const
|
2021-12-26 18:43:27 +00:00
|
|
|
{
|
|
|
|
// confirm the span is valid
|
2023-04-23 01:25:55 +00:00
|
|
|
span.begin = std::clamp(span.begin, uint64_t{ 0 }, block_info_->total_size());
|
|
|
|
span.end = std::clamp(span.end, uint64_t{ 0 }, block_info_->total_size());
|
2021-12-26 18:43:27 +00:00
|
|
|
auto const [begin_byte, end_byte] = span;
|
|
|
|
if (begin_byte >= end_byte)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the block span of the byte span
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const begin_block = block_info_->byte_loc(begin_byte).block;
|
|
|
|
auto const final_block = block_info_->byte_loc(end_byte - 1).block;
|
2021-12-26 18:43:27 +00:00
|
|
|
|
|
|
|
// if the entire span is in a single block
|
|
|
|
if (begin_block == final_block)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return has_block(begin_block) ? end_byte - begin_byte : 0;
|
2021-12-26 18:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto total = uint64_t{};
|
|
|
|
|
|
|
|
// the first block
|
2023-04-23 01:25:55 +00:00
|
|
|
if (has_block(begin_block))
|
2021-12-26 18:43:27 +00:00
|
|
|
{
|
|
|
|
uint64_t u = begin_block + 1;
|
2022-02-23 20:25:06 +00:00
|
|
|
u *= tr_block_info::BlockSize;
|
2021-12-26 18:43:27 +00:00
|
|
|
u -= begin_byte;
|
|
|
|
total += u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the middle blocks
|
|
|
|
if (begin_block + 1 < final_block)
|
|
|
|
{
|
|
|
|
uint64_t u = blocks_.count(begin_block + 1, final_block);
|
2022-02-23 20:25:06 +00:00
|
|
|
u *= tr_block_info::BlockSize;
|
2021-12-26 18:43:27 +00:00
|
|
|
total += u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the last block
|
2023-04-23 01:25:55 +00:00
|
|
|
if (has_block(final_block))
|
2021-12-26 18:43:27 +00:00
|
|
|
{
|
|
|
|
uint64_t u = final_block;
|
2022-02-23 20:25:06 +00:00
|
|
|
u *= tr_block_info::BlockSize;
|
2021-12-26 18:43:27 +00:00
|
|
|
total += end_byte - u;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|