Avoid redundant data copy by using JSON objects directly

This commit is contained in:
Chocobo1 2019-08-01 21:00:10 +08:00
parent f6ee96ed83
commit cfe83275d3
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
3 changed files with 51 additions and 50 deletions

View file

@ -95,7 +95,7 @@ void AppController::preferencesAction()
{
const Preferences *const pref = Preferences::instance();
const auto *session = BitTorrent::Session::instance();
QVariantHash data;
QJsonObject data;
// Downloads
// When adding a torrent
@ -116,7 +116,7 @@ void AppController::preferencesAction()
data["export_dir_fin"] = Utils::Fs::toNativePath(session->finishedTorrentExportDirectory());
// Automatically add torrents from
const QVariantHash dirs = pref->getScanDirs();
QVariantHash nativeDirs;
QJsonObject nativeDirs;
for (auto i = dirs.cbegin(); i != dirs.cend(); ++i) {
if (i.value().type() == QVariant::Int)
nativeDirs.insert(Utils::Fs::toNativePath(i.key()), i.value().toInt());
@ -248,7 +248,7 @@ void AppController::preferencesAction()
data["dyndns_domain"] = pref->getDynDomainName();
// RSS settings
data["rss_refresh_interval"] = RSS::Session::instance()->refreshInterval();
data["rss_refresh_interval"] = static_cast<double>(RSS::Session::instance()->refreshInterval());
data["rss_max_articles_per_feed"] = RSS::Session::instance()->maxArticlesPerFeed();
data["rss_processing_enabled"] = RSS::Session::instance()->isProcessingEnabled();
data["rss_auto_downloading_enabled"] = RSS::AutoDownloader::instance()->isProcessingEnabled();
@ -262,7 +262,7 @@ void AppController::preferencesAction()
// Listen on IPv6 address
data["listen_on_ipv6_address"] = session->isIPv6Enabled();
// Save resume data interval
data["save_resume_data_interval"] = session->saveResumeDataInterval();
data["save_resume_data_interval"] = static_cast<double>(session->saveResumeDataInterval());
// Recheck completed torrents
data["recheck_completed_torrents"] = pref->recheckTorrentsOnCompletion();
// Resolve peer countries
@ -311,7 +311,7 @@ void AppController::preferencesAction()
data["announce_to_all_tiers"] = session->announceToAllTiers();
data["announce_ip"] = session->announceIP();
setResult(QJsonObject::fromVariantHash(data));
setResult(data);
}
void AppController::setPreferencesAction()
@ -747,17 +747,17 @@ void AppController::defaultSavePathAction()
void AppController::networkInterfaceListAction()
{
QVariantList ifaceList;
QJsonArray ifaceList;
for (const QNetworkInterface &iface : asConst(QNetworkInterface::allInterfaces())) {
if (!iface.addressEntries().isEmpty()) {
ifaceList.append(QVariantHash {
ifaceList.append(QJsonObject {
{"name", iface.humanReadableName()},
{"value", iface.name()}
});
}
}
setResult(QJsonArray::fromVariantList(ifaceList));
setResult(ifaceList);
}
void AppController::networkInterfaceAddressListAction()
@ -765,7 +765,7 @@ void AppController::networkInterfaceAddressListAction()
checkParams({"iface"});
const QString ifaceName = params().value("iface");
QVariantList addressList;
QJsonArray addressList;
if (ifaceName.isEmpty()) {
for (const QHostAddress &ip : asConst(QNetworkInterface::allAddresses()))
@ -777,5 +777,5 @@ void AppController::networkInterfaceAddressListAction()
addressList.append(entry.ip().toString());
}
setResult(QJsonArray::fromVariantList(addressList));
setResult(addressList);
}

View file

@ -29,6 +29,7 @@
#include "logcontroller.h"
#include <QJsonArray>
#include <QJsonObject>
#include "base/global.h"
#include "base/logger.h"
@ -70,7 +71,7 @@ void LogController::mainAction()
lastKnownId = -1;
Logger *const logger = Logger::instance();
QVariantList msgList;
QJsonArray msgList;
for (const Log::Msg &msg : asConst(logger->getMessages(lastKnownId))) {
if (!((msg.type == Log::NORMAL && isNormal)
@ -79,7 +80,7 @@ void LogController::mainAction()
|| (msg.type == Log::CRITICAL && isCritical)))
continue;
msgList.append(QVariantHash {
msgList.append(QJsonObject {
{KEY_LOG_ID, msg.id},
{KEY_LOG_TIMESTAMP, msg.timestamp},
{KEY_LOG_MSG_TYPE, msg.type},
@ -87,7 +88,7 @@ void LogController::mainAction()
});
}
setResult(QJsonArray::fromVariantList(msgList));
setResult(msgList);
}
// Returns the peer log in JSON format.
@ -110,10 +111,10 @@ void LogController::peersAction()
lastKnownId = -1;
Logger *const logger = Logger::instance();
QVariantList peerList;
QJsonArray peerList;
for (const Log::Peer &peer : asConst(logger->getPeers(lastKnownId))) {
peerList.append(QVariantHash {
peerList.append(QJsonObject {
{KEY_LOG_ID, peer.id},
{KEY_LOG_TIMESTAMP, peer.timestamp},
{KEY_LOG_PEER_IP, peer.ip},
@ -122,5 +123,5 @@ void LogController::peersAction()
});
}
setResult(QJsonArray::fromVariantList(peerList));
setResult(peerList);
}

View file

@ -131,9 +131,9 @@ namespace
}
}
QVariantList getStickyTrackers(const BitTorrent::TorrentHandle *const torrent)
QJsonArray getStickyTrackers(const BitTorrent::TorrentHandle *const torrent)
{
uint seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, leechesDHT = 0, leechesPeX = 0, leechesLSD = 0;
int seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, leechesDHT = 0, leechesPeX = 0, leechesLSD = 0;
for (const BitTorrent::PeerInfo &peer : asConst(torrent->peers())) {
if (peer.isConnecting()) continue;
@ -161,7 +161,7 @@ namespace
const QString privateMsg {QCoreApplication::translate("TrackerListWidget", "This torrent is private")};
const bool isTorrentPrivate = torrent->isPrivate();
const QVariantHash dht {
const QJsonObject dht {
{KEY_TRACKER_URL, "** [DHT] **"},
{KEY_TRACKER_TIER, ""},
{KEY_TRACKER_MSG, (isTorrentPrivate ? privateMsg : "")},
@ -172,7 +172,7 @@ namespace
{KEY_TRACKER_LEECHES_COUNT, leechesDHT}
};
const QVariantHash pex {
const QJsonObject pex {
{KEY_TRACKER_URL, "** [PeX] **"},
{KEY_TRACKER_TIER, ""},
{KEY_TRACKER_MSG, (isTorrentPrivate ? privateMsg : "")},
@ -183,7 +183,7 @@ namespace
{KEY_TRACKER_LEECHES_COUNT, leechesPeX}
};
const QVariantHash lsd {
const QJsonObject lsd {
{KEY_TRACKER_URL, "** [LSD] **"},
{KEY_TRACKER_TIER, ""},
{KEY_TRACKER_MSG, (isTorrentPrivate ? privateMsg : "")},
@ -194,7 +194,7 @@ namespace
{KEY_TRACKER_LEECHES_COUNT, leechesLSD}
};
return QVariantList {dht, pex, lsd};
return {dht, pex, lsd};
}
}
@ -309,14 +309,14 @@ void TorrentsController::propertiesAction()
checkParams({"hash"});
const QString hash {params()["hash"]};
QVariantHash dataDict;
QJsonObject dataDict;
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent)
throw APIError(APIErrorType::NotFound);
dataDict[KEY_PROP_TIME_ELAPSED] = torrent->activeTime();
dataDict[KEY_PROP_SEEDING_TIME] = torrent->seedingTime();
dataDict[KEY_PROP_ETA] = torrent->eta();
dataDict[KEY_PROP_ETA] = static_cast<double>(torrent->eta());
dataDict[KEY_PROP_CONNECT_COUNT] = torrent->connectionsCount();
dataDict[KEY_PROP_CONNECT_COUNT_LIMIT] = torrent->connectionsLimit();
dataDict[KEY_PROP_DOWNLOADED] = torrent->totalDownload();
@ -344,11 +344,11 @@ void TorrentsController::propertiesAction()
dataDict[KEY_PROP_PIECE_SIZE] = torrent->pieceLength();
dataDict[KEY_PROP_PIECES_HAVE] = torrent->piecesHave();
dataDict[KEY_PROP_CREATED_BY] = torrent->creator();
dataDict[KEY_PROP_ADDITION_DATE] = torrent->addedTime().toTime_t();
dataDict[KEY_PROP_ADDITION_DATE] = static_cast<double>(torrent->addedTime().toSecsSinceEpoch());
if (torrent->hasMetadata()) {
dataDict[KEY_PROP_LAST_SEEN] = torrent->lastSeenComplete().isValid() ? static_cast<int>(torrent->lastSeenComplete().toTime_t()) : -1;
dataDict[KEY_PROP_COMPLETION_DATE] = torrent->completedTime().isValid() ? static_cast<int>(torrent->completedTime().toTime_t()) : -1;
dataDict[KEY_PROP_CREATION_DATE] = torrent->creationDate().toTime_t();
dataDict[KEY_PROP_CREATION_DATE] = static_cast<double>(torrent->creationDate().toSecsSinceEpoch());
}
else {
dataDict[KEY_PROP_LAST_SEEN] = -1;
@ -358,7 +358,7 @@ void TorrentsController::propertiesAction()
dataDict[KEY_PROP_SAVE_PATH] = Utils::Fs::toNativePath(torrent->savePath());
dataDict[KEY_PROP_COMMENT] = torrent->comment();
setResult(QJsonObject::fromVariantHash(dataDict));
setResult(dataDict);
}
// Returns the trackers for a torrent in JSON format.
@ -381,13 +381,13 @@ void TorrentsController::trackersAction()
if (!torrent)
throw APIError(APIErrorType::NotFound);
QVariantList trackerList = getStickyTrackers(torrent);
QJsonArray trackerList = getStickyTrackers(torrent);
QHash<QString, BitTorrent::TrackerInfo> trackersData = torrent->trackerInfos();
for (const BitTorrent::TrackerEntry &tracker : asConst(torrent->trackers())) {
const BitTorrent::TrackerInfo data = trackersData.value(tracker.url());
trackerList << QVariantHash {
trackerList << QJsonObject {
{KEY_TRACKER_URL, tracker.url()},
{KEY_TRACKER_TIER, tracker.tier()},
{KEY_TRACKER_STATUS, static_cast<int>(tracker.status())},
@ -399,7 +399,7 @@ void TorrentsController::trackersAction()
};
}
setResult(QJsonArray::fromVariantList(trackerList));
setResult(trackerList);
}
// Returns the web seeds for a torrent in JSON format.
@ -411,18 +411,18 @@ void TorrentsController::webseedsAction()
checkParams({"hash"});
const QString hash {params()["hash"]};
QVariantList webSeedList;
QJsonArray webSeedList;
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent)
throw APIError(APIErrorType::NotFound);
for (const QUrl &webseed : asConst(torrent->urlSeeds())) {
webSeedList.append(QVariantHash {
webSeedList.append(QJsonObject {
{KEY_WEBSEED_URL, webseed.toString()}
});
}
setResult(QJsonArray::fromVariantList(webSeedList));
setResult(webSeedList);
}
// Returns the files in a torrent in JSON format.
@ -440,7 +440,7 @@ void TorrentsController::filesAction()
checkParams({"hash"});
const QString hash {params()["hash"]};
QVariantList fileList;
QJsonArray fileList;
const BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent)
throw APIError(APIErrorType::NotFound);
@ -451,7 +451,7 @@ void TorrentsController::filesAction()
const QVector<qreal> fileAvailability = torrent->availableFileFractions();
const BitTorrent::TorrentInfo info = torrent->info();
for (int i = 0; i < torrent->filesCount(); ++i) {
QVariantHash fileDict = {
QJsonObject fileDict = {
{KEY_FILE_PROGRESS, fp[i]},
{KEY_FILE_PRIORITY, static_cast<int>(priorities[i])},
{KEY_FILE_SIZE, torrent->fileSize(i)},
@ -464,7 +464,7 @@ void TorrentsController::filesAction()
fileDict[KEY_FILE_NAME] = Utils::Fs::toNativePath(fileName);
const BitTorrent::TorrentInfo::PieceRange idx = info.filePieces(i);
fileDict[KEY_FILE_PIECE_RANGE] = QVariantList {idx.first(), idx.last()};
fileDict[KEY_FILE_PIECE_RANGE] = QJsonArray {idx.first(), idx.last()};
if (i == 0)
fileDict[KEY_FILE_IS_SEED] = torrent->isSeed();
@ -473,7 +473,7 @@ void TorrentsController::filesAction()
}
}
setResult(QJsonArray::fromVariantList(fileList));
setResult(fileList);
}
// Returns an array of hashes (of each pieces respectively) for a torrent in JSON format.
@ -483,17 +483,16 @@ void TorrentsController::pieceHashesAction()
checkParams({"hash"});
const QString hash {params()["hash"]};
QVariantList pieceHashes;
QJsonArray pieceHashes;
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent)
throw APIError(APIErrorType::NotFound);
const QVector<QByteArray> hashes = torrent->info().pieceHashes();
pieceHashes.reserve(hashes.size());
for (const QByteArray &hash : hashes)
pieceHashes.append(hash.toHex());
pieceHashes.append(QString(hash.toHex()));
setResult(QJsonArray::fromVariantList(pieceHashes));
setResult(pieceHashes);
}
// Returns an array of states (of each pieces respectively) for a torrent in JSON format.
@ -506,13 +505,12 @@ void TorrentsController::pieceStatesAction()
checkParams({"hash"});
const QString hash {params()["hash"]};
QVariantList pieceStates;
QJsonArray pieceStates;
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent)
throw APIError(APIErrorType::NotFound);
const QBitArray states = torrent->pieces();
pieceStates.reserve(states.size());
for (int i = 0; i < states.size(); ++i)
pieceStates.append(static_cast<int>(states[i]) * 2);
@ -522,7 +520,7 @@ void TorrentsController::pieceStatesAction()
pieceStates[i] = 1;
}
setResult(QJsonArray::fromVariantList(pieceStates));
setResult(pieceStates);
}
void TorrentsController::addAction()
@ -743,7 +741,7 @@ void TorrentsController::uploadLimitAction()
checkParams({"hashes"});
const QStringList hashes {params()["hashes"].split('|')};
QVariantHash map;
QJsonObject map;
for (const QString &hash : hashes) {
int limit = -1;
const BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
@ -752,7 +750,7 @@ void TorrentsController::uploadLimitAction()
map[hash] = limit;
}
setResult(QJsonObject::fromVariantHash(map));
setResult(map);
}
void TorrentsController::downloadLimitAction()
@ -760,7 +758,7 @@ void TorrentsController::downloadLimitAction()
checkParams({"hashes"});
const QStringList hashes {params()["hashes"].split('|')};
QVariantHash map;
QJsonObject map;
for (const QString &hash : hashes) {
int limit = -1;
const BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
@ -769,7 +767,7 @@ void TorrentsController::downloadLimitAction()
map[hash] = limit;
}
setResult(QJsonObject::fromVariantHash(map));
setResult(map);
}
void TorrentsController::setUploadLimitAction()
@ -1103,6 +1101,8 @@ void TorrentsController::deleteTagsAction()
void TorrentsController::tagsAction()
{
const QStringList tags = BitTorrent::Session::instance()->tags().toList();
setResult(QJsonArray::fromStringList(tags));
QJsonArray result;
for (const QString &tag : asConst(BitTorrent::Session::instance()->tags()))
result << tag;
setResult(result);
}