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-25 18:26:51 +00:00
|
|
|
#include <algorithm>
|
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>
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2021-11-25 18:26:51 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "completion.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2007-11-12 22:26:10 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::computeHasValid() 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
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
for (tr_piece_index_t piece = 0, n_pieces = block_info_->pieceCount(); piece < n_pieces; ++piece)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (hasPiece(piece))
|
|
|
|
{
|
2021-11-26 19:33:56 +00:00
|
|
|
size += block_info_->pieceSize(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
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::hasValid() 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
|
|
|
{
|
2022-06-01 16:56:59 +00:00
|
|
|
auto const val = computeHasValid();
|
|
|
|
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
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::computeSizeWhenDone() const
|
|
|
|
{
|
|
|
|
if (hasAll())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
return block_info_->totalSize();
|
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 };
|
2022-04-14 01:22:59 +00:00
|
|
|
for (tr_piece_index_t piece = 0, n_pieces = block_info_->pieceCount(); piece < n_pieces; ++piece)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
if (tor_->pieceIsWanted(piece))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-26 19:33:56 +00:00
|
|
|
size += block_info_->pieceSize(piece);
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-04 16:48:54 +00:00
|
|
|
size += countHasBytesInPiece(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
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::sizeWhenDone() 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
|
|
|
{
|
2022-06-01 16:56:59 +00:00
|
|
|
auto const value = computeSizeWhenDone();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void tr_completion::amountDone(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
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
std::vector<uint8_t> tr_completion::createPieceBitfield() const
|
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
size_t const n = block_info_->pieceCount();
|
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
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
flags[piece] = hasPiece(piece);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2021-11-28 01:58:35 +00:00
|
|
|
pieces.setFromBools(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
|
|
|
|
|
|
|
void tr_completion::addBlock(tr_block_index_t block)
|
2009-01-12 21:59:53 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (hasBlock(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);
|
2021-11-26 19:33:56 +00:00
|
|
|
size_now_ += block_info_->blockSize(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
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void tr_completion::setBlocks(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);
|
2022-07-04 16:48:54 +00:00
|
|
|
size_now_ = countHasBytesInSpan({ 0, block_info_->totalSize() });
|
2021-11-25 18:26:51 +00:00
|
|
|
size_when_done_.reset();
|
|
|
|
has_valid_.reset();
|
|
|
|
}
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
void tr_completion::setHasAll() noexcept
|
2022-02-14 19:17:51 +00:00
|
|
|
{
|
|
|
|
auto const total_size = block_info_->totalSize();
|
|
|
|
|
|
|
|
blocks_.setHasAll();
|
|
|
|
size_now_ = total_size;
|
|
|
|
size_when_done_ = total_size;
|
|
|
|
has_valid_ = total_size;
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void tr_completion::addPiece(tr_piece_index_t piece)
|
|
|
|
{
|
2022-08-02 23:34:53 +00:00
|
|
|
auto const span = block_info_->blockSpanForPiece(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
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
addBlock(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-02-14 01:51:23 +00:00
|
|
|
void tr_completion::removeBlock(tr_block_index_t block)
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-02-14 01:51:23 +00:00
|
|
|
if (!hasBlock(block))
|
|
|
|
{
|
|
|
|
return; // already didn't have it
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks_.unset(block);
|
|
|
|
size_now_ -= block_info_->blockSize(block);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void tr_completion::removePiece(tr_piece_index_t piece)
|
|
|
|
{
|
|
|
|
auto const [begin, end] = block_info_->blockSpanForPiece(piece);
|
|
|
|
|
|
|
|
for (auto block = begin; block < end; ++block)
|
|
|
|
{
|
|
|
|
removeBlock(block);
|
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2011-01-31 16:43:37 +00:00
|
|
|
|
2021-12-26 18:43:27 +00:00
|
|
|
uint64_t tr_completion::countHasBytesInSpan(tr_byte_span_t span) const
|
|
|
|
{
|
|
|
|
// confirm the span is valid
|
2022-04-14 01:22:59 +00:00
|
|
|
span.begin = std::clamp(span.begin, uint64_t{ 0 }, block_info_->totalSize());
|
|
|
|
span.end = std::clamp(span.end, uint64_t{ 0 }, block_info_->totalSize());
|
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
|
2022-02-18 23:17:19 +00:00
|
|
|
auto const begin_block = block_info_->byteLoc(begin_byte).block;
|
|
|
|
auto const final_block = block_info_->byteLoc(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)
|
|
|
|
{
|
|
|
|
return hasBlock(begin_block) ? end_byte - begin_byte : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto total = uint64_t{};
|
|
|
|
|
|
|
|
// the first block
|
|
|
|
if (hasBlock(begin_block))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if (hasBlock(final_block))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|