Code formatting
This commit is contained in:
142
src/TDB.cpp
142
src/TDB.cpp
@@ -74,26 +74,32 @@ void readTaskmods (std::vector <std::string> &input,
|
||||
|
||||
DEBUG_STR ("reading taskmods from file: ");
|
||||
|
||||
for (;start != input.end(); ++start) {
|
||||
for ( ; start != input.end (); ++start)
|
||||
{
|
||||
line = *start;
|
||||
|
||||
if (line.substr (0, 4) == "time") {
|
||||
if (line.substr (0, 4) == "time")
|
||||
{
|
||||
std::stringstream stream (line.substr (5));
|
||||
long ts;
|
||||
stream >> ts;
|
||||
|
||||
if (stream.fail()) {
|
||||
throw std::string ("Failed to convert \"" + stream.str() + "\" to integer: " + tmod_tmp.getTimeStr() + ".");
|
||||
}
|
||||
if (stream.fail ())
|
||||
throw std::string ("Failed to convert \"" + stream.str ()
|
||||
+ "\" to integer: " + tmod_tmp.getTimeStr () + ".");
|
||||
|
||||
// 'time' is the first line of a modification
|
||||
// thus we will (re)set the taskmod object
|
||||
tmod_tmp.reset (ts);
|
||||
|
||||
} else if (line.substr (0, 3) == "old") {
|
||||
}
|
||||
else if (line.substr (0, 3) == "old")
|
||||
{
|
||||
tmod_tmp.setBefore (Task (line.substr (4)));
|
||||
|
||||
} else if (line.substr (0, 3) == "new") {
|
||||
}
|
||||
else if (line.substr (0, 3) == "new")
|
||||
{
|
||||
tmod_tmp.setAfter (Task (line.substr (4)));
|
||||
|
||||
// 'new' is the last line of a modification,
|
||||
@@ -1078,9 +1084,8 @@ void TDB::merge (const std::string& mergeFile)
|
||||
// will contain the NEW undo.data
|
||||
std::vector <std::string> undo;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
// initialize the files
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////
|
||||
// initialize the files:
|
||||
|
||||
// load merge file (undo file of right/remote branch)
|
||||
std::vector <std::string> r;
|
||||
@@ -1109,11 +1114,11 @@ void TDB::merge (const std::string& mergeFile)
|
||||
rline = *rit;
|
||||
lline = *lit;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
// find the branch-off point
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////
|
||||
// find the branch-off point:
|
||||
|
||||
// first lines are not equal => assuming mergeFile starts at a later point in time
|
||||
// first lines are not equal => assuming mergeFile starts at a
|
||||
// later point in time
|
||||
if (lline.compare (rline) != 0)
|
||||
{
|
||||
// iterate in local file to find rline
|
||||
@@ -1139,18 +1144,20 @@ void TDB::merge (const std::string& mergeFile)
|
||||
rline = *rit;
|
||||
|
||||
// found first non-matching lines?
|
||||
if (lline.compare(rline) != 0) {
|
||||
if (lline.compare (rline) != 0)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// push the line to the new undo.data
|
||||
undo.push_back (lline + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
// branch-off point found
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////
|
||||
// branch-off point found:
|
||||
|
||||
if (found)
|
||||
{
|
||||
@@ -1162,21 +1169,25 @@ void TDB::merge (const std::string& mergeFile)
|
||||
// helper lists
|
||||
std::set<std::string> uuid_new, uuid_left;
|
||||
|
||||
// ------ 1. read tasmods out of the remaining lines
|
||||
// 1. read tasmods out of the remaining lines
|
||||
readTaskmods (l, lit, lmods);
|
||||
readTaskmods (r, rit, rmods);
|
||||
|
||||
// ------ 2. move new uuids into mods
|
||||
|
||||
// 2. move new uuids into mods
|
||||
DEBUG_STR_PART ("adding new uuids (left) to skip list...");
|
||||
|
||||
// modifications on the left side are already in the database
|
||||
// we just need them to merge conflicts, so we add new the mods for
|
||||
// new uuids to the skip-list 'uuid_left'
|
||||
std::list<Taskmod>::iterator lmod_it;
|
||||
for (lmod_it = lmods.begin(); lmod_it != lmods.end(); lmod_it++) {
|
||||
if ((*lmod_it).isNew()) {
|
||||
std::cout << "Skipping the new local task " << (*lmod_it).getUuid() << std::endl;
|
||||
for (lmod_it = lmods.begin (); lmod_it != lmods.end (); lmod_it++)
|
||||
{
|
||||
if ( (*lmod_it).isNew ())
|
||||
{
|
||||
std::cout << "Skipping the new local task "
|
||||
<< (*lmod_it).getUuid()
|
||||
<< std::endl;
|
||||
|
||||
uuid_left.insert ( (*lmod_it).getUuid ());
|
||||
}
|
||||
}
|
||||
@@ -1187,20 +1198,26 @@ void TDB::merge (const std::string& mergeFile)
|
||||
// new items on the right side need to be inserted into the
|
||||
// local database
|
||||
std::list<Taskmod>::iterator rmod_it;
|
||||
for (rmod_it = rmods.begin(); rmod_it != rmods.end();) {
|
||||
for (rmod_it = rmods.begin (); rmod_it != rmods.end (); )
|
||||
{
|
||||
// we have to save and increment the iterator because we may want to delete
|
||||
// the object from the list
|
||||
std::list<Taskmod>::iterator current = rmod_it++;
|
||||
Taskmod tmod = *current;
|
||||
|
||||
// new uuid?
|
||||
if (tmod.isNew()) {
|
||||
std::cout << "Adding the new remote task " << tmod.getUuid() << std::endl;
|
||||
if (tmod.isNew ())
|
||||
{
|
||||
std::cout << "Adding the new remote task "
|
||||
<< tmod.getUuid()
|
||||
<< std::endl;
|
||||
uuid_new.insert (tmod.getUuid ());
|
||||
mods.push_back (tmod);
|
||||
rmods.erase (current);
|
||||
}
|
||||
else if (uuid_new.find(tmod.getUuid()) != uuid_new.end()) { // uuid of modification was new
|
||||
else if (uuid_new.find (tmod.getUuid ()) != uuid_new.end ())
|
||||
{
|
||||
// uuid of modification was new
|
||||
mods.push_back (tmod);
|
||||
rmods.erase (current);
|
||||
}
|
||||
@@ -1208,13 +1225,15 @@ void TDB::merge (const std::string& mergeFile)
|
||||
|
||||
DEBUG_STR_END ("done");
|
||||
|
||||
///////////////////////////////////////
|
||||
// merge modifications:
|
||||
DEBUG_STR ("Merging modifications:");
|
||||
|
||||
// ------ 3. merge modifications
|
||||
// we iterate backwards to resolve conflicts by timestamps (newest one wins)
|
||||
std::list<Taskmod>::reverse_iterator lmod_rit;
|
||||
std::list<Taskmod>::reverse_iterator rmod_rit;
|
||||
for (lmod_rit = lmods.rbegin(); lmod_rit != lmods.rend(); ++lmod_rit) {
|
||||
for (lmod_rit = lmods.rbegin (); lmod_rit != lmods.rend (); ++lmod_rit)
|
||||
{
|
||||
Taskmod tmod_l = *lmod_rit;
|
||||
std::string uuid = tmod_l.getUuid ();
|
||||
|
||||
@@ -1225,36 +1244,41 @@ void TDB::merge (const std::string& mergeFile)
|
||||
|
||||
bool rwin = false;
|
||||
bool lwin = false;
|
||||
for (rmod_rit = rmods.rbegin(); rmod_rit != rmods.rend(); rmod_rit++) {
|
||||
for (rmod_rit = rmods.rbegin (); rmod_rit != rmods.rend (); rmod_rit++)
|
||||
{
|
||||
Taskmod tmod_r = *rmod_rit;
|
||||
|
||||
DEBUG_STR (" right uuid: " + tmod_r.getUuid ());
|
||||
if (tmod_r.getUuid() == uuid) {
|
||||
if (tmod_r.getUuid () == uuid)
|
||||
{
|
||||
DEBUG_STR (" uuid match found for " + uuid);
|
||||
|
||||
// we already decided to take the mods from the right side
|
||||
// but we have to find the first modification newer than
|
||||
// the one on the left side to merge the history too
|
||||
if (rwin) {
|
||||
if (rwin)
|
||||
{
|
||||
DEBUG_STR (" scanning right side");
|
||||
if (tmod_r > tmod_l) {
|
||||
if (tmod_r > tmod_l)
|
||||
mods.push_front (tmod_r);
|
||||
}
|
||||
|
||||
std::list<Taskmod>::iterator tmp_it = rmod_rit.base ();
|
||||
rmods.erase (--tmp_it);
|
||||
rmod_rit--;
|
||||
}
|
||||
else if (lwin) {
|
||||
else if (lwin)
|
||||
{
|
||||
DEBUG_STR (" cleaning up right side");
|
||||
|
||||
std::list<Taskmod>::iterator tmp_it = rmod_rit.base ();
|
||||
rmods.erase (--tmp_it);
|
||||
rmod_rit--;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
// which one is newer?
|
||||
if (tmod_r > tmod_l) {
|
||||
if (tmod_r > tmod_l)
|
||||
{
|
||||
std::cout << "Applying remote changes for uuid " << uuid << std::endl;
|
||||
|
||||
mods.push_front(tmod_r);
|
||||
@@ -1266,7 +1290,8 @@ void TDB::merge (const std::string& mergeFile)
|
||||
|
||||
rwin = true;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
std::cout << "Rejecting remote changes for uuid " << uuid << std::endl;
|
||||
// inserting right mod into history of local database
|
||||
// so that it can be restored later
|
||||
@@ -1297,7 +1322,8 @@ void TDB::merge (const std::string& mergeFile)
|
||||
}
|
||||
} // for
|
||||
|
||||
if (rwin) {
|
||||
if (rwin)
|
||||
{
|
||||
DEBUG_STR (" concat the first match to left branch");
|
||||
// concat the oldest (but still newer) modification on the right
|
||||
// to the endpoint on the left
|
||||
@@ -1328,11 +1354,11 @@ void TDB::merge (const std::string& mergeFile)
|
||||
readTaskmods (r, rit, mods);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// redo command
|
||||
////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////
|
||||
// redo command:
|
||||
|
||||
if (!mods.empty()) {
|
||||
if (!mods.empty ())
|
||||
{
|
||||
std::cout << "Running redo routine..." << std::endl;
|
||||
|
||||
std::string pendingFile = location.data + "/pending.data";
|
||||
@@ -1354,7 +1380,8 @@ void TDB::merge (const std::string& mergeFile)
|
||||
Taskmod tmod = *current;
|
||||
|
||||
// Modification to an existing task.
|
||||
if (!tmod.isNew()) {
|
||||
if (!tmod.isNew ())
|
||||
{
|
||||
std::string uuid = tmod.getUuid ();
|
||||
Task::status statusBefore = tmod.getBefore().getStatus ();
|
||||
Task::status statusAfter = tmod.getAfter().getStatus ();
|
||||
@@ -1382,14 +1409,17 @@ void TDB::merge (const std::string& mergeFile)
|
||||
// normal usage of task, but those kinds of
|
||||
// taskmods may be constructed to merge databases
|
||||
if ( (statusAfter != Task::completed)
|
||||
&& (statusAfter != Task::deleted) ) {
|
||||
&& (statusAfter != Task::deleted) )
|
||||
{
|
||||
// insert task into pending data
|
||||
pending.push_back (newline);
|
||||
|
||||
// remove task from completed data
|
||||
completed.erase (it);
|
||||
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// replace the current line
|
||||
*it = newline;
|
||||
}
|
||||
@@ -1415,14 +1445,17 @@ void TDB::merge (const std::string& mergeFile)
|
||||
|
||||
// does the tasks move to completed data
|
||||
if ( (statusAfter == Task::completed)
|
||||
|| (statusAfter == Task::deleted) ) {
|
||||
|| (statusAfter == Task::deleted) )
|
||||
{
|
||||
// insert task into completed data
|
||||
completed.push_back (newline);
|
||||
|
||||
// remove task from pending data
|
||||
pending.erase (it);
|
||||
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// replace the current line
|
||||
*it = newline;
|
||||
}
|
||||
@@ -1433,13 +1466,15 @@ void TDB::merge (const std::string& mergeFile)
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
if (!found)
|
||||
{
|
||||
std::cout << "Missing " << uuid << std::endl;
|
||||
mods.erase (current);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
// Check for dups.
|
||||
std::string uuid = tmod.getAfter ().get ("uuid");
|
||||
|
||||
@@ -1480,7 +1515,8 @@ void TDB::merge (const std::string& mergeFile)
|
||||
mods.merge (lmods);
|
||||
|
||||
// generate undo.data format
|
||||
for (it = mods.begin(); it != mods.end(); it++) {
|
||||
for (it = mods.begin (); it != mods.end (); it++)
|
||||
{
|
||||
undo.push_back(it->toString ());
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include <assert.h>
|
||||
#include "Taskmod.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Taskmod::Taskmod ()
|
||||
{
|
||||
timestamp = 0;
|
||||
@@ -37,6 +38,7 @@ Taskmod::Taskmod()
|
||||
bBeforeSet = false;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Taskmod::Taskmod (const Taskmod& other)
|
||||
{
|
||||
this->before = other.before;
|
||||
@@ -46,21 +48,24 @@ Taskmod::Taskmod(const Taskmod& other)
|
||||
this->bBeforeSet = other.bBeforeSet;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Taskmod::~Taskmod ()
|
||||
{
|
||||
}
|
||||
|
||||
// OPERATORS
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Taskmod::operator< (const Taskmod &compare)
|
||||
{
|
||||
return (timestamp < compare.getTimestamp ());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Taskmod::operator> (const Taskmod &compare)
|
||||
{
|
||||
return (timestamp > compare.getTimestamp ());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Taskmod::operator== (const Taskmod& compare)
|
||||
{
|
||||
return ( (compare.after == this->after)
|
||||
@@ -68,14 +73,17 @@ bool Taskmod::operator== (const Taskmod& compare)
|
||||
&& (compare.timestamp == this->timestamp) );
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Taskmod::operator!= (const Taskmod& compare)
|
||||
{
|
||||
return !this->operator== (compare);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Taskmod& Taskmod::operator= (const Taskmod& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
if (this != &other)
|
||||
{
|
||||
this->before = other.before;
|
||||
this->after = other.after;
|
||||
this->timestamp = other.timestamp;
|
||||
@@ -86,7 +94,7 @@ Taskmod& Taskmod::operator= (const Taskmod& other)
|
||||
return *this;
|
||||
}
|
||||
|
||||
// HELPER
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Taskmod::reset (long timestamp)
|
||||
{
|
||||
this->bAfterSet = false;
|
||||
@@ -94,35 +102,42 @@ void Taskmod::reset(long timestamp)
|
||||
this->timestamp = timestamp;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Taskmod::isNew ()
|
||||
{
|
||||
return !bBeforeSet;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Taskmod::issetAfter ()
|
||||
{
|
||||
return bAfterSet;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Taskmod::issetBefore ()
|
||||
{
|
||||
return bBeforeSet;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Taskmod::isValid ()
|
||||
{
|
||||
return (timestamp > 0) && (bAfterSet);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string Taskmod::getUuid ()
|
||||
{
|
||||
if (!bAfterSet) {
|
||||
if (!bAfterSet)
|
||||
{
|
||||
throw std::string ("Taskmod::getUuid(): Task object not initialized.");
|
||||
}
|
||||
|
||||
return after.get ("uuid");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string Taskmod::toString ()
|
||||
{
|
||||
assert (bAfterSet);
|
||||
@@ -130,7 +145,8 @@ std::string Taskmod::toString()
|
||||
std::stringstream stream;
|
||||
stream << "time " << timestamp << "\n";
|
||||
|
||||
if (bBeforeSet) {
|
||||
if (bBeforeSet)
|
||||
{
|
||||
stream << "old " << before.composeF4();
|
||||
}
|
||||
|
||||
@@ -140,44 +156,49 @@ std::string Taskmod::toString()
|
||||
return stream.str ();
|
||||
}
|
||||
|
||||
// SETTER
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Taskmod::setAfter (const Task& after)
|
||||
{
|
||||
this->after = after;
|
||||
bAfterSet = true;
|
||||
this->bAfterSet = true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Taskmod::setBefore (const Task& before)
|
||||
{
|
||||
this->before = before;
|
||||
bBeforeSet = true;
|
||||
this->bBeforeSet = true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Taskmod::setTimestamp (long timestamp)
|
||||
{
|
||||
this->timestamp = timestamp;
|
||||
}
|
||||
|
||||
// GETTER
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Task& Taskmod::getAfter ()
|
||||
{
|
||||
return after;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Task& Taskmod::getBefore ()
|
||||
{
|
||||
return before;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
long Taskmod::getTimestamp () const
|
||||
{
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string Taskmod::getTimeStr () const
|
||||
{
|
||||
std::stringstream sstream;
|
||||
sstream << timestamp;
|
||||
|
||||
return sstream.str ();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
#include <Task.h>
|
||||
|
||||
class Taskmod {
|
||||
|
||||
public:
|
||||
Taskmod ();
|
||||
Taskmod (const Taskmod& other);
|
||||
|
||||
Reference in New Issue
Block a user