Do not write tag database if it is not changed.

Database objects were unconditionally writing out the tags database
file. For commands that are not modifying any tags, this is unnecessary.
We will now track if there have been modifications so we can use it to
determine if we need to save the tags to a file.

Closes #284
This commit is contained in:
Shaun Ruffell 2020-02-16 12:52:13 -06:00 committed by lauft
parent 4b3a907cbb
commit aa7a674ca6
3 changed files with 30 additions and 1 deletions

View file

@ -258,7 +258,10 @@ void Database::commit ()
file.commit ();
}
AtomicFile::write (_location + "/tags.data", _tagInfoDatabase.toJson ());
if (_tagInfoDatabase.is_modified ())
{
AtomicFile::write (_location + "/tags.data", _tagInfoDatabase.toJson ());
}
}
////////////////////////////////////////////////////////////////////////////////
@ -460,6 +463,10 @@ void Database::initializeTagDatabase ()
if (!File::read (_location + "/tags.data", content))
{
// We always want the tag database file to exists.
_tagInfoDatabase = TagInfoDatabase();
AtomicFile::write (_location + "/tags.data", _tagInfoDatabase.toJson ());
auto it = Database::begin ();
auto end = Database::end ();
@ -497,6 +504,11 @@ void Database::initializeTagDatabase ()
auto number = dynamic_cast<json::number *> (iter->second);
_tagInfoDatabase.add (key, TagInfo{(unsigned int) number->_dvalue});
}
// Since we just loaded the database from the file, there we can clear the
// modified state so that we will not write it back out unless there is a
// new change.
_tagInfoDatabase.clear_modified ();
}
}

View file

@ -46,6 +46,7 @@ int TagInfoDatabase::incrementTag (const std::string& tag)
return -1;
}
_is_modified = true;
return search->second.increment ();
}
@ -63,6 +64,7 @@ int TagInfoDatabase::decrementTag (const std::string& tag)
throw format ("Trying to decrement non-existent tag '{1}'", tag);
}
_is_modified = true;
return search->second.decrement ();
}
@ -71,6 +73,7 @@ int TagInfoDatabase::decrementTag (const std::string& tag)
//
void TagInfoDatabase::add (const std::string& tag, const TagInfo& tagInfo)
{
_is_modified = true;
_tagInformation.emplace (tag, tagInfo);
}
@ -89,6 +92,16 @@ std::set <std::string> TagInfoDatabase::tags () const
return tags;
}
bool TagInfoDatabase::is_modified () const
{
return _is_modified;
}
void TagInfoDatabase::clear_modified ()
{
_is_modified = false;
}
///////////////////////////////////////////////////////////////////////////////
std::string TagInfoDatabase::toJson ()
{

View file

@ -44,8 +44,12 @@ public:
std::string toJson ();
bool is_modified () const;
void clear_modified ();
private:
std::map <std::string, TagInfo> _tagInformation {};
bool _is_modified {false};
};
#endif