refactor: use std::vector for tr_torrents.removed_ (#2943)

This commit is contained in:
Charles Kerr 2022-04-18 14:24:20 -05:00 committed by GitHub
parent 9c0038ac8e
commit 345b71ffda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 20 deletions

View File

@ -874,17 +874,12 @@ static char const* torrentGet(tr_session* session, tr_variant* args_in, tr_varia
if (tr_variantDictFindStrView(args_in, TR_KEY_ids, &sv) && sv == "recently-active"sv)
{
time_t const now = tr_time();
auto const interval = RecentlyActiveSeconds;
auto const& removed = session->removed_torrents;
tr_variant* removed_out = tr_variantDictAddList(args_out, TR_KEY_removed, std::size(removed));
for (auto const& [id, time_removed] : removed)
auto const cutoff = tr_time() - RecentlyActiveSeconds;
auto const ids = session->torrents().removedSince(cutoff);
auto* const out = tr_variantDictAddList(args_out, TR_KEY_removed, std::size(ids));
for (auto const& id : ids)
{
if (time_removed >= now - interval)
{
tr_variantListAddInt(removed_out, id);
}
tr_variantListAddInt(out, id);
}
}

View File

@ -606,7 +606,6 @@ tr_session* tr_sessionInit(char const* config_dir, bool messageQueuingEnabled, t
session->cache = tr_cacheNew(1024 * 1024 * 2);
session->magicNumber = SESSION_MAGIC_NUMBER;
session->session_id = tr_session_id_new();
session->removed_torrents.clear();
bandwidthGroupRead(session, config_dir);
/* nice to start logging at the very beginning */

View File

@ -287,9 +287,6 @@ public:
uint8_t peer_id_ttl_hours;
// torrent id, time removed
std::vector<std::pair<int, time_t>> removed_torrents;
bool stalledEnabled;
bool queueEnabled[2];
int queueSize[2];

View File

@ -1605,8 +1605,6 @@ static void closeTorrent(tr_torrent* const tor)
TR_ASSERT(tr_isTorrent(tor));
TR_ASSERT(tr_amInEventThread(tor->session));
tor->session->removed_torrents.emplace_back(tor->uniqueId, tr_time());
if (!tor->session->isClosing())
{
tr_logAddInfoTor(tor, _("Removing torrent"));

View File

@ -51,9 +51,11 @@ tr_torrent* tr_torrents::get(int id)
return nullptr;
}
auto* tor = by_id_.at(id);
auto* const tor = by_id_.at(id);
TR_ASSERT(tor == nullptr || tor->uniqueId == id);
TR_ASSERT(removed_.count(id) == (tor == nullptr ? 1 : 0));
TR_ASSERT(
std::count_if(std::begin(removed_), std::end(removed_), [&id](auto const& removed) { return id == removed.first; }) ==
(tor == nullptr ? 1 : 0));
return tor;
}
@ -91,7 +93,7 @@ void tr_torrents::remove(tr_torrent const* tor, time_t timestamp)
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);
removed_.insert_or_assign(tor->uniqueId, timestamp);
removed_.emplace_back(tor->uniqueId, timestamp);
}
std::set<int> tr_torrents::removedSince(time_t timestamp) const

View File

@ -116,5 +116,5 @@ private:
// may be testing for >0 as a validity check.
std::vector<tr_torrent*> by_id_{ nullptr };
std::map<int, time_t> removed_;
std::vector<std::pair<int, time_t>> removed_;
};