From 345b71ffda932afdc155482d5e6246f176021c1f Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 18 Apr 2022 14:24:20 -0500 Subject: [PATCH] refactor: use std::vector for tr_torrents.removed_ (#2943) --- libtransmission/rpcimpl.cc | 15 +++++---------- libtransmission/session.cc | 1 - libtransmission/session.h | 3 --- libtransmission/torrent.cc | 2 -- libtransmission/torrents.cc | 8 +++++--- libtransmission/torrents.h | 2 +- 6 files changed, 11 insertions(+), 20 deletions(-) diff --git a/libtransmission/rpcimpl.cc b/libtransmission/rpcimpl.cc index ebcd419fb..62e30afae 100644 --- a/libtransmission/rpcimpl.cc +++ b/libtransmission/rpcimpl.cc @@ -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); } } diff --git a/libtransmission/session.cc b/libtransmission/session.cc index b8564e8cb..9ab408a41 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -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 */ diff --git a/libtransmission/session.h b/libtransmission/session.h index 58f2fd6f3..125462a98 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -287,9 +287,6 @@ public: uint8_t peer_id_ttl_hours; - // torrent id, time removed - std::vector> removed_torrents; - bool stalledEnabled; bool queueEnabled[2]; int queueSize[2]; diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index d3485f846..0ed6febae 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -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")); diff --git a/libtransmission/torrents.cc b/libtransmission/torrents.cc index 74fd9b9ea..1ff4ef280 100644 --- a/libtransmission/torrents.cc +++ b/libtransmission/torrents.cc @@ -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 tr_torrents::removedSince(time_t timestamp) const diff --git a/libtransmission/torrents.h b/libtransmission/torrents.h index 98b2081c0..8a4721685 100644 --- a/libtransmission/torrents.h +++ b/libtransmission/torrents.h @@ -116,5 +116,5 @@ private: // may be testing for >0 as a validity check. std::vector by_id_{ nullptr }; - std::map removed_; + std::vector> removed_; };