2022-03-01 23:06:29 +00:00
|
|
|
// This file Copyright © 2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#include "magnet-metainfo.h"
|
|
|
|
#include "torrent.h"
|
|
|
|
#include "torrents.h"
|
|
|
|
#include "tr-assert.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
struct CompareTorrentByHash
|
|
|
|
{
|
|
|
|
bool operator()(tr_sha1_digest_t const& a, tr_sha1_digest_t const& b) const
|
|
|
|
{
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(tr_torrent const* a, tr_torrent const* b) const
|
|
|
|
{
|
|
|
|
return (*this)(a->infoHash(), b->infoHash());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(tr_torrent const* a, tr_sha1_digest_t const& b) const
|
|
|
|
{
|
|
|
|
return (*this)(a->infoHash(), b);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(tr_sha1_digest_t const& a, tr_torrent const* b) const
|
|
|
|
{
|
|
|
|
return (*this)(a, b->infoHash());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
tr_torrent* tr_torrents::get(int id)
|
|
|
|
{
|
2022-03-04 20:00:42 +00:00
|
|
|
TR_ASSERT(0 < id);
|
2022-03-01 23:06:29 +00:00
|
|
|
TR_ASSERT(static_cast<size_t>(id) < std::size(by_id_));
|
|
|
|
if (static_cast<size_t>(id) >= std::size(by_id_))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-04-18 19:24:20 +00:00
|
|
|
auto* const tor = by_id_.at(id);
|
2022-03-01 23:06:29 +00:00
|
|
|
TR_ASSERT(tor == nullptr || tor->uniqueId == id);
|
2022-04-18 19:24:20 +00:00
|
|
|
TR_ASSERT(
|
|
|
|
std::count_if(std::begin(removed_), std::end(removed_), [&id](auto const& removed) { return id == removed.first; }) ==
|
|
|
|
(tor == nullptr ? 1 : 0));
|
2022-03-01 23:06:29 +00:00
|
|
|
return tor;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent* tr_torrents::get(std::string_view magnet_link)
|
|
|
|
{
|
|
|
|
auto magnet = tr_magnet_metainfo{};
|
|
|
|
return magnet.parseMagnet(magnet_link) ? get(magnet.infoHash()) : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent* tr_torrents::get(tr_sha1_digest_t const& hash)
|
|
|
|
{
|
|
|
|
auto [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), hash, CompareTorrentByHash{});
|
|
|
|
return begin == end ? nullptr : *begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent const* tr_torrents::get(tr_sha1_digest_t const& hash) const
|
|
|
|
{
|
|
|
|
auto [begin, end] = std::equal_range(std::cbegin(by_hash_), std::cend(by_hash_), hash, CompareTorrentByHash{});
|
|
|
|
return begin == end ? nullptr : *begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tr_torrents::add(tr_torrent* tor)
|
|
|
|
{
|
2022-03-24 05:18:41 +00:00
|
|
|
auto const id = static_cast<int>(std::size(by_id_));
|
2022-03-01 23:06:29 +00:00
|
|
|
by_id_.push_back(tor);
|
|
|
|
by_hash_.insert(std::lower_bound(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash{}), tor);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_torrents::remove(tr_torrent const* tor, time_t timestamp)
|
|
|
|
{
|
|
|
|
TR_ASSERT(tor != nullptr);
|
|
|
|
TR_ASSERT(get(tor->uniqueId) == tor);
|
|
|
|
|
|
|
|
by_id_[tor->uniqueId] = nullptr;
|
|
|
|
auto const [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash{});
|
|
|
|
by_hash_.erase(begin, end);
|
2022-04-18 19:24:20 +00:00
|
|
|
removed_.emplace_back(tor->uniqueId, timestamp);
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 13:08:02 +00:00
|
|
|
std::vector<int> tr_torrents::removedSince(time_t timestamp) const
|
2022-03-01 23:06:29 +00:00
|
|
|
{
|
2022-04-22 13:08:02 +00:00
|
|
|
auto ids = std::set<int>{};
|
2022-03-01 23:06:29 +00:00
|
|
|
|
|
|
|
for (auto const& [id, removed_at] : removed_)
|
|
|
|
{
|
|
|
|
if (removed_at >= timestamp)
|
|
|
|
{
|
2022-04-22 13:08:02 +00:00
|
|
|
ids.insert(id);
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 13:08:02 +00:00
|
|
|
return { std::begin(ids), std::end(ids) };
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|