Fixed out of control memory / CPU usage caused by unnecessary track

parsing.
This commit is contained in:
casey langen 2020-10-04 22:42:29 -07:00
parent 139c669790
commit ed313ecce0
5 changed files with 30 additions and 15 deletions

View File

@ -228,7 +228,7 @@ void CategoryTrackListQuery::DeserializeResult(const std::string& data) {
this->SetStatus(IQuery::Failed);
nlohmann::json result = nlohmann::json::parse(data)["result"];
this->result = std::make_shared<TrackList>(this->library);
TrackListFromJson(result["trackList"], *this->result, this->library);
TrackListFromJson(result["trackList"], *this->result, this->library, true);
JsonArrayToSet<std::set<size_t>, size_t>(result["headers"], *this->headers);
this->SetStatus(IQuery::Finished);
}

View File

@ -193,7 +193,7 @@ void SearchTrackListQuery::DeserializeResult(const std::string& data) {
this->SetStatus(IQuery::Failed);
nlohmann::json result = nlohmann::json::parse(data)["result"];
this->result = std::make_shared<TrackList>(this->library);
TrackListFromJson(result["trackList"], *this->result, this->library);
TrackListFromJson(result["trackList"], *this->result, this->library, true);
JsonArrayToSet<std::set<size_t>, size_t>(result["headers"], *this->headers);
this->SetStatus(IQuery::Finished);
}

View File

@ -177,7 +177,7 @@ void TrackMetadataQuery::DeserializeResult(const std::string& data) {
this->SetStatus(IQuery::Failed);
auto input = nlohmann::json::parse(data);
auto parsedResult = std::make_shared<LibraryTrack>(-1LL, this->library);
TrackFromJson(input["result"], parsedResult);
TrackFromJson(input["result"], parsedResult, false);
this->result = parsedResult;
this->SetStatus(IQuery::Finished);
}

View File

@ -38,6 +38,7 @@
using namespace musik::core;
using namespace musik::core::library::query;
using namespace musik::core::sdk;
namespace musik { namespace core { namespace library { namespace query {
@ -117,7 +118,7 @@ namespace musik { namespace core { namespace library { namespace query {
#define COPY_TRACK_FIELD_TO_JSON(track, json, field) \
json[field] = track->GetString(field);
nlohmann::json TrackToJson(const musik::core::TrackPtr input, bool onlyIds) {
nlohmann::json TrackToJson(const TrackPtr input, bool onlyIds) {
nlohmann::json output;
output["id"] = input->GetId();
@ -198,24 +199,37 @@ namespace musik { namespace core { namespace library { namespace query {
replayGain.trackPeak = replayGainJson.value("trackPeak", 1.0f);
}
output->SetMetadataState(musik::core::sdk::MetadataState::Loaded);
output->SetMetadataState(MetadataState::Loaded);
}
nlohmann::json TrackListToJson(const musik::core::TrackList& input, bool onlyIds) {
nlohmann::json TrackListToJson(const TrackList& input, bool onlyIds) {
nlohmann::json output;
if (onlyIds) {
for (size_t i = 0; i < input.Count(); i++) {
output.push_back(input.GetId(i));
}
}
else {
for (size_t i = 0; i < input.Count(); i++) {
output.push_back(TrackToJson(input.Get(i), onlyIds));
}
}
return output;
}
void TrackListFromJson(const nlohmann::json& input, musik::core::TrackList& output, musik::core::ILibraryPtr library) {
void TrackListFromJson(const nlohmann::json& input, TrackList& output, ILibraryPtr library, bool onlyIds) {
output.Clear();
if (onlyIds) {
for (auto& trackId : input) {
output.Add(trackId.get<int64_t>());
}
}
else {
for (auto& trackJson : input) {
TrackPtr track = std::make_shared<LibraryTrack>(-1LL, library);
output.Add(trackJson["id"].get<int64_t>());
}
}
}
}

View File

@ -74,16 +74,17 @@ namespace musik { namespace core { namespace library { namespace query {
void TrackFromJson(
const nlohmann::json& input,
musik::core::TrackPtr output,
bool onlyIds = false);
bool onlyIds);
nlohmann::json TrackListToJson(
const musik::core::TrackList& input,
bool onlyIds = false);
bool onlyIds);
void TrackListFromJson(
const nlohmann::json& input,
musik::core::TrackList& output,
musik::core::ILibraryPtr library);
musik::core::ILibraryPtr library,
bool onlyIds);
template <typename SetType, typename DataType>
void JsonArrayToSet(const nlohmann::json& input, SetType& output) {