From a7da0438ca9d91a72323db77e80e484de71510d4 Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Tue, 31 Jul 2018 07:03:22 +0200 Subject: [PATCH] Connect database and tag-info database --- src/Database.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/Database.h | 5 +++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Database.cpp b/src/Database.cpp index 7a199a09..73ad1b39 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// // -// Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez. +// Copyright 2015 - 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -29,11 +29,15 @@ #include #include #include +#include +#include +#include //////////////////////////////////////////////////////////////////////////////// void Database::initialize (const std::string& location) { _location = location; + initializeTagDatabase (); } //////////////////////////////////////////////////////////////////////////////// @@ -41,6 +45,8 @@ void Database::commit () { for (auto& file : _files) file.commit (); + + File::write (_location + "/tags.data", _tagInfoDatabase.toJson ()); } //////////////////////////////////////////////////////////////////////////////// @@ -92,6 +98,16 @@ std::vector Database::allLines () //////////////////////////////////////////////////////////////////////////////// void Database::addInterval (const Interval& interval) { + auto tags = interval.tags (); + + for (auto& tag : tags) + { + if (_tagInfoDatabase.incrementTag (tag) == -1) + { + std::cout << "Note: '" << quoteIfNeeded (tag) << "' is a new tag." << std::endl; + } + } + if (interval.range.is_open ()) { // Get the index into _files for the appropriate Datafile, which may be @@ -125,6 +141,13 @@ void Database::addInterval (const Interval& interval) //////////////////////////////////////////////////////////////////////////////// void Database::deleteInterval (const Interval& interval) { + auto tags = interval.tags (); + + for (auto& tag : tags) + { + _tagInfoDatabase.decrementTag (tag); + } + auto intervalRange = interval.range; for (auto& segment : segmentRange (intervalRange)) { @@ -357,6 +380,28 @@ std::vector Database::segmentRange (const Range& range) return segments; } +//////////////////////////////////////////////////////////////////////////////// +void Database::initializeTagDatabase () +{ + std::string content; + + if (!File::read (_location + "/tags.data", content)) + { + return; + } + + auto* json = (json::object*) json::parse (content); + + for (auto& pair : json->_data) + { + auto key = pair.first; + auto* value = (json::object*) pair.second; + auto* number = (json::number*) value->_data["count"]; + + _tagInfoDatabase.add (key, TagInfo {(unsigned int) number->_dvalue}); + } +} + //////////////////////////////////////////////////////////////////////////////// void Database::initializeDatafiles () { diff --git a/src/Database.h b/src/Database.h index 9f9373b4..c0ff4770 100644 --- a/src/Database.h +++ b/src/Database.h @@ -33,6 +33,7 @@ #include #include #include +#include class Database { @@ -62,14 +63,14 @@ private: unsigned int getDatafile (int, int); std::vector segmentRange (const Range&); void initializeDatafiles (); + void initializeTagDatabase (); void recordUndoAction (const std::string &, const std::string &, const std::string &); private: std::string _location {"~/.timewarrior/data"}; std::vector _files {}; - int _txn {0}; - + TagInfoDatabase _tagInfoDatabase {}; std::shared_ptr _currentTransaction = nullptr; };