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); this->SetStatus(IQuery::Failed);
nlohmann::json result = nlohmann::json::parse(data)["result"]; nlohmann::json result = nlohmann::json::parse(data)["result"];
this->result = std::make_shared<TrackList>(this->library); 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); JsonArrayToSet<std::set<size_t>, size_t>(result["headers"], *this->headers);
this->SetStatus(IQuery::Finished); this->SetStatus(IQuery::Finished);
} }

View File

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

View File

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

View File

@ -38,6 +38,7 @@
using namespace musik::core; using namespace musik::core;
using namespace musik::core::library::query; using namespace musik::core::library::query;
using namespace musik::core::sdk;
namespace musik { namespace core { namespace library { namespace query { 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) \ #define COPY_TRACK_FIELD_TO_JSON(track, json, field) \
json[field] = track->GetString(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; nlohmann::json output;
output["id"] = input->GetId(); output["id"] = input->GetId();
@ -198,22 +199,35 @@ namespace musik { namespace core { namespace library { namespace query {
replayGain.trackPeak = replayGainJson.value("trackPeak", 1.0f); 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; nlohmann::json output;
for (size_t i = 0; i < input.Count(); i++) { if (onlyIds) {
output.push_back(TrackToJson(input.Get(i), 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; 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(); output.Clear();
for (auto& trackJson : input) { if (onlyIds) {
TrackPtr track = std::make_shared<LibraryTrack>(-1LL, library); for (auto& trackId : input) {
output.Add(trackJson["id"].get<int64_t>()); output.Add(trackId.get<int64_t>());
}
}
else {
for (auto& trackJson : input) {
output.Add(trackJson["id"].get<int64_t>());
}
} }
} }

View File

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