1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-04 02:28:03 +00:00

refactor: use std::vector<tr_quark> for torrent labels (#2942)

This commit is contained in:
Charles Kerr 2022-04-18 00:53:48 -05:00 committed by GitHub
parent 3a8cac3914
commit 18da7eb125
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 30 deletions

View file

@ -104,7 +104,7 @@ static void saveLabels(tr_variant* dict, tr_torrent const* tor)
tr_variant* list = tr_variantDictAddList(dict, TR_KEY_labels, std::size(labels)); tr_variant* list = tr_variantDictAddList(dict, TR_KEY_labels, std::size(labels));
for (auto const& label : labels) for (auto const& label : labels)
{ {
tr_variantListAddStr(list, label); tr_variantListAddQuark(list, label);
} }
} }
@ -116,16 +116,19 @@ static auto loadLabels(tr_variant* dict, tr_torrent* tor)
return tr_resume::fields_t{}; return tr_resume::fields_t{};
} }
int const n = tr_variantListSize(list); auto const n = tr_variantListSize(list);
for (int i = 0; i < n; ++i) auto labels = std::vector<tr_quark>{};
labels.reserve(n);
for (size_t i = 0; i < n; ++i)
{ {
auto sv = std::string_view{}; auto sv = std::string_view{};
if (tr_variantGetStrView(tr_variantListChild(list, i), &sv) && !std::empty(sv)) if (tr_variantGetStrView(tr_variantListChild(list, i), &sv) && !std::empty(sv))
{ {
tor->labels.emplace(sv); labels.emplace_back(tr_quark_new(sv));
} }
} }
tor->setLabels(std::data(labels), std::size(labels));
return tr_resume::Labels; return tr_resume::Labels;
} }

View file

@ -363,7 +363,7 @@ static void addLabels(tr_torrent const* tor, tr_variant* list)
tr_variantInitList(list, std::size(tor->labels)); tr_variantInitList(list, std::size(tor->labels));
for (auto const& label : tor->labels) for (auto const& label : tor->labels)
{ {
tr_variantListAddStr(list, label); tr_variantListAddQuark(list, label);
} }
} }
@ -941,10 +941,12 @@ static char const* torrentGet(tr_session* session, tr_variant* args_in, tr_varia
**** ****
***/ ***/
static std::pair<tr_labels_t, char const* /*errmsg*/> makeLabels(tr_variant* list) static std::pair<std::vector<tr_quark>, char const* /*errmsg*/> makeLabels(tr_variant* list)
{ {
auto labels = tr_labels_t{}; auto labels = std::vector<tr_quark>{};
size_t const n = tr_variantListSize(list); size_t const n = tr_variantListSize(list);
labels.reserve(n);
for (size_t i = 0; i < n; ++i) for (size_t i = 0; i < n; ++i)
{ {
auto label = std::string_view{}; auto label = std::string_view{};
@ -964,7 +966,7 @@ static std::pair<tr_labels_t, char const* /*errmsg*/> makeLabels(tr_variant* lis
return { {}, "labels cannot contain comma (,) character" }; return { {}, "labels cannot contain comma (,) character" };
} }
labels.emplace(label); labels.emplace_back(tr_quark_new(label));
} }
return { labels, nullptr }; return { labels, nullptr };
@ -979,7 +981,7 @@ static char const* setLabels(tr_torrent* tor, tr_variant* list)
return errmsg; return errmsg;
} }
tr_torrentSetLabels(tor, std::move(labels)); tor->setLabels(std::data(labels), std::size(labels));
return nullptr; return nullptr;
} }
@ -1666,7 +1668,7 @@ static char const* torrentAdd(tr_session* session, tr_variant* args_in, tr_varia
return errmsg; return errmsg;
} }
tr_ctorSetLabels(ctor, std::move(labels)); tr_ctorSetLabels(ctor, std::data(labels), std::size(labels));
} }
tr_logAddTrace(fmt::format("torrentAdd: filename is '{}'", filename)); tr_logAddTrace(fmt::format("torrentAdd: filename is '{}'", filename));

View file

@ -41,7 +41,7 @@ struct tr_ctor
tr_priority_t priority = TR_PRI_NORMAL; tr_priority_t priority = TR_PRI_NORMAL;
tr_labels_t labels = {}; tr_torrent::labels_t labels = {};
struct optional_args optional_args[2]; struct optional_args optional_args[2];
@ -329,12 +329,12 @@ tr_priority_t tr_ctorGetBandwidthPriority(tr_ctor const* ctor)
**** ****
***/ ***/
void tr_ctorSetLabels(tr_ctor* ctor, tr_labels_t&& labels) void tr_ctorSetLabels(tr_ctor* ctor, tr_quark const* labels, size_t n_labels)
{ {
ctor->labels = std::move(labels); ctor->labels = { labels, labels + n_labels };
} }
tr_labels_t tr_ctorGetLabels(tr_ctor const* ctor) tr_torrent::labels_t const& tr_ctorGetLabels(tr_ctor const* ctor)
{ {
return ctor->labels; return ctor->labels;
} }

View file

@ -717,7 +717,8 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor)
tor->error = TR_STAT_OK; tor->error = TR_STAT_OK;
tor->finishedSeedingByIdle = false; tor->finishedSeedingByIdle = false;
tor->labels = tr_ctorGetLabels(ctor); auto const& labels = tr_ctorGetLabels(ctor);
tor->setLabels(std::data(labels), std::size(labels));
tor->uniqueId = session->torrents().add(tor); tor->uniqueId = session->torrents().add(tor);
@ -1926,13 +1927,13 @@ void tr_torrentSetFileDLs(tr_torrent* tor, tr_file_index_t const* files, tr_file
**** ****
***/ ***/
void tr_torrentSetLabels(tr_torrent* tor, tr_labels_t&& labels) void tr_torrent::setLabels(tr_quark const* new_labels, size_t n_labels)
{ {
TR_ASSERT(tr_isTorrent(tor)); auto const lock = unique_lock();
auto const lock = tor->unique_lock(); auto const sorted_unique = std::set<tr_quark>{ new_labels, new_labels + n_labels };
this->labels = { std::begin(sorted_unique), std::end(sorted_unique) };
tor->labels = std::move(labels); this->labels.shrink_to_fit();
tor->setDirty(); this->setDirty();
} }
/*** /***

View file

@ -14,7 +14,6 @@
#include <optional> #include <optional>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <unordered_set>
#include <vector> #include <vector>
#include "transmission.h" #include "transmission.h"
@ -41,8 +40,6 @@ struct tr_session;
struct tr_torrent; struct tr_torrent;
struct tr_torrent_announcer; struct tr_torrent_announcer;
using tr_labels_t = std::unordered_set<std::string>;
/** /**
*** Package-visible ctor API *** Package-visible ctor API
**/ **/
@ -59,14 +56,10 @@ tr_session* tr_ctorGetSession(tr_ctor const* ctor);
bool tr_ctorGetIncompleteDir(tr_ctor const* ctor, char const** setmeIncompleteDir); bool tr_ctorGetIncompleteDir(tr_ctor const* ctor, char const** setmeIncompleteDir);
tr_labels_t tr_ctorGetLabels(tr_ctor const* ctor);
/** /**
*** ***
**/ **/
void tr_torrentSetLabels(tr_torrent* tor, tr_labels_t&& labels);
void tr_torrentChangeMyPort(tr_torrent* session); void tr_torrentChangeMyPort(tr_torrent* session);
tr_torrent* tr_torrentFindFromObfuscatedHash(tr_session* session, tr_sha1_digest_t const& hash); tr_torrent* tr_torrentFindFromObfuscatedHash(tr_session* session, tr_sha1_digest_t const& hash);
@ -573,6 +566,8 @@ public:
void setDateActive(time_t t); void setDateActive(time_t t);
void setLabels(tr_quark const* labels, size_t n_labels);
/** Return the mime-type (e.g. "audio/x-flac") that matches more of the /** Return the mime-type (e.g. "audio/x-flac") that matches more of the
torrent's content than any other mime-type. */ torrent's content than any other mime-type. */
[[nodiscard]] std::string_view primaryMimeType() const; [[nodiscard]] std::string_view primaryMimeType() const;
@ -707,7 +702,8 @@ public:
tr_idlelimit idleLimitMode = TR_IDLELIMIT_GLOBAL; tr_idlelimit idleLimitMode = TR_IDLELIMIT_GLOBAL;
bool finishedSeedingByIdle = false; bool finishedSeedingByIdle = false;
tr_labels_t labels; using labels_t = std::vector<tr_quark>;
labels_t labels;
std::string group; std::string group;
/* Set the bandwidth group the torrent belongs to */ /* Set the bandwidth group the torrent belongs to */
@ -761,7 +757,8 @@ tr_torrent_metainfo tr_ctorStealMetainfo(tr_ctor* ctor);
bool tr_ctorSetMetainfoFromFile(tr_ctor* ctor, std::string const& filename, tr_error** error); bool tr_ctorSetMetainfoFromFile(tr_ctor* ctor, std::string const& filename, tr_error** error);
bool tr_ctorSetMetainfoFromMagnetLink(tr_ctor* ctor, std::string const& filename, tr_error** error); bool tr_ctorSetMetainfoFromMagnetLink(tr_ctor* ctor, std::string const& filename, tr_error** error);
void tr_ctorSetLabels(tr_ctor* ctor, tr_labels_t&& labels); void tr_ctorSetLabels(tr_ctor* ctor, tr_quark const* labels, size_t n_labels);
tr_torrent::labels_t const& tr_ctorGetLabels(tr_ctor const* ctor);
#define tr_logAddCriticalTor(tor, msg) tr_logAddCritical(msg, (tor)->name()) #define tr_logAddCriticalTor(tor, msg) tr_logAddCritical(msg, (tor)->name())
#define tr_logAddErrorTor(tor, msg) tr_logAddError(msg, (tor)->name()) #define tr_logAddErrorTor(tor, msg) tr_logAddError(msg, (tor)->name())