From 44ea23ae6ae8ace74193ebdc0a6e70f7f43b8e10 Mon Sep 17 00:00:00 2001 From: verybadsoldier Date: Mon, 19 Dec 2016 16:59:40 +0100 Subject: [PATCH] when saving gamelist.xml only save metadata that has been changed (to speed things up) --- es-app/src/Gamelist.cpp | 29 +++++++++++++++++++---------- es-app/src/MetaData.cpp | 15 +++++++++++++-- es-app/src/MetaData.h | 4 ++++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/es-app/src/Gamelist.cpp b/es-app/src/Gamelist.cpp index aeb8d0580..dacf88fd4 100644 --- a/es-app/src/Gamelist.cpp +++ b/es-app/src/Gamelist.cpp @@ -139,6 +139,8 @@ void parseGamelist(SystemData* system) //make sure name gets set if one didn't exist if(file->metadata.get("name").empty()) file->metadata.set("name", defaultName); + + file->metadata.resetChangedFlag(); } } } @@ -207,20 +209,24 @@ void updateGamelist(SystemData* system) FileData* rootFolder = system->getRootFolder(); if (rootFolder != nullptr) { + int numUpdated = 0; + //get only files, no folders std::vector files = rootFolder->getFilesRecursive(GAME | FOLDER); //iterate through all files, checking if they're already in the XML - std::vector::const_iterator fit = files.cbegin(); - while(fit != files.cend()) + for(std::vector::const_iterator fit = files.cbegin(); fit != files.cend(); ++fit) { const char* tag = ((*fit)->getType() == GAME) ? "game" : "folder"; // check if current file has metadata, if no, skip it as it wont be in the gamelist anyway. if ((*fit)->metadata.isDefault()) { - ++fit; continue; } + // do not touch if it wasn't changed anyway + if (!(*fit)->metadata.wasChanged()) + continue; + // check if the file already exists in the XML // if it does, remove it before adding for(pugi::xml_node fileNode = root.child(tag); fileNode; fileNode = fileNode.next_sibling(tag)) @@ -244,18 +250,21 @@ void updateGamelist(SystemData* system) // it was either removed or never existed to begin with; either way, we can add it now addFileDataNode(root, *fit, tag, system); - - ++fit; + ++numUpdated; } //now write the file - //make sure the folders leading up to this path exist (or the write will fail) - boost::filesystem::path xmlWritePath(system->getGamelistPath(true)); - boost::filesystem::create_directories(xmlWritePath.parent_path()); + if (numUpdated > 0) { + //make sure the folders leading up to this path exist (or the write will fail) + boost::filesystem::path xmlWritePath(system->getGamelistPath(true)); + boost::filesystem::create_directories(xmlWritePath.parent_path()); - if (!doc.save_file(xmlWritePath.c_str())) { - LOG(LogError) << "Error saving gamelist.xml to \"" << xmlWritePath << "\" (for system " << system->getName() << ")!"; + LOG(LogInfo) << "Added/Updated " << numUpdated << " entities in '" << xmlReadPath << "'"; + + if (!doc.save_file(xmlWritePath.c_str())) { + LOG(LogError) << "Error saving gamelist.xml to \"" << xmlWritePath << "\" (for system " << system->getName() << ")!"; + } } }else{ LOG(LogError) << "Found no root folder for system \"" << system->getName() << "\"!"; diff --git a/es-app/src/MetaData.cpp b/es-app/src/MetaData.cpp index 3f5da45c6..390ed4efb 100644 --- a/es-app/src/MetaData.cpp +++ b/es-app/src/MetaData.cpp @@ -49,7 +49,7 @@ const std::vector& getMDDByType(MetaDataListType type) MetaDataList::MetaDataList(MetaDataListType type) - : mType(type) + : mType(type), mWasChanged(false) { const std::vector& mdd = getMDD(); for(auto iter = mdd.begin(); iter != mdd.end(); iter++) @@ -110,11 +110,12 @@ void MetaDataList::appendToXML(pugi::xml_node parent, bool ignoreDefaults, const void MetaDataList::set(const std::string& key, const std::string& value) { mMap[key] = value; + mWasChanged = true; } void MetaDataList::setTime(const std::string& key, const boost::posix_time::ptime& time) { - mMap[key] = boost::posix_time::to_iso_string(time); + set(key, boost::posix_time::to_iso_string(time)); } const std::string& MetaDataList::get(const std::string& key) const @@ -145,3 +146,13 @@ bool MetaDataList::isDefault() return true; } + +bool MetaDataList::wasChanged() const +{ + return mWasChanged; +} + +void MetaDataList::resetChangedFlag() +{ + mWasChanged = false; +} diff --git a/es-app/src/MetaData.h b/es-app/src/MetaData.h index 136a8b2f6..df444e9d4 100644 --- a/es-app/src/MetaData.h +++ b/es-app/src/MetaData.h @@ -58,10 +58,14 @@ public: bool isDefault(); + bool wasChanged() const; + void resetChangedFlag(); + inline MetaDataListType getType() const { return mType; } inline const std::vector& getMDD() const { return getMDDByType(getType()); } private: MetaDataListType mType; std::map mMap; + bool mWasChanged; };