Permalink
Browse files

#9 TI-1: Add TransactionFactory

- CmdUndo pops last transaction from array (and applies undo actions - tbd)
- TransactionFactory reads contents of undo.data into transaction array
- Transaction array is written back to undo.data
  • Loading branch information...
lauft committed Jul 19, 2018
1 parent d8a4b8f commit 76ee22e7e541eda2fc2bc0cf386eda108aaa5bc2
Showing with 175 additions and 0 deletions.
  1. +1 −0 src/CMakeLists.txt
  2. +40 −0 src/Database.cpp
  3. +2 −0 src/Database.h
  4. +5 −0 src/Transaction.cpp
  5. +2 −0 src/Transaction.h
  6. +59 −0 src/TransactionsFactory.cpp
  7. +49 −0 src/TransactionsFactory.h
  8. +17 −0 src/commands/CmdUndo.cpp
@@ -14,6 +14,7 @@ set (timew_SRCS CLI.cpp CLI.h
Range.cpp Range.h
Rules.cpp Rules.h
Transaction.cpp Transaction.h
TransactionsFactory.cpp TransactionsFactory.h
UndoAction.cpp UndoAction.h
data.cpp
dom.cpp
@@ -32,6 +32,7 @@
#include <sstream>
#include <iterator>
#include <iomanip>
#include <TransactionsFactory.h>
#include <ctime>

////////////////////////////////////////////////////////////////////////////////
@@ -236,6 +237,45 @@ void Database::recordIntervalAction (
recordUndoAction ("interval", before, after);
}

////////////////////////////////////////////////////////////////////////////////
Transaction Database::popLastTransaction ()
{
File undo (_location + "/undo.data");

std::vector <std::string> read_lines;
undo.read (read_lines);
undo.close ();

TransactionsFactory transactionsFactory;

for (auto& line: read_lines)
{
transactionsFactory.parseLine (line);
}

std::vector <Transaction> transactions = transactionsFactory.get ();

if (transactions.empty ())
{
return Transaction {};
}

Transaction last = transactions.back ();
transactions.pop_back ();

File::remove (undo._data);
undo.open ();

for (auto& transaction : transactions)
{
undo.append (transaction.toString ());
}

undo.close ();

return last;
}

////////////////////////////////////////////////////////////////////////////////
std::string Database::dump () const
{
@@ -54,6 +54,8 @@ class Database
void recordConfigAction(const std::string&, const std::string&);
void recordIntervalAction(const std::string&, const std::string&);

Transaction popLastTransaction ();

std::string dump () const;

private:
@@ -35,6 +35,11 @@ void Transaction::addUndoAction (
_actions.emplace_back (type, before, after);
}

std::vector<UndoAction> Transaction::getActions ()
{
return _actions;
}

std::string Transaction::toString ()
{
std::string output = "txn:\n";
@@ -37,6 +37,8 @@ class Transaction

std::string toString();

std::vector<UndoAction> getActions ();

private:
std::vector<UndoAction> _actions {};
};
@@ -0,0 +1,59 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 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
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#include <vector>
#include "TransactionsFactory.h"
#include "Transaction.h"

void TransactionsFactory::parseLine (const std::string& line)
{
if (!line.compare(0, 4, "txn:"))
{
_transactions.emplace_back ();
}
else if (!line.compare(0, 7, " type:"))
{
_type = line.substr (8, line.size ());
}
else if (!line.compare(0, 9, " before:"))
{
_before = line.substr (10, line.size ());
}
else if (!line.compare(0, 8, " after:"))
{
_after = line.substr (9, line.size ());
_transactions.back ().addUndoAction (_type, _before, _after);
}
else
{
throw "Cannot handle line '" + line + "'";
}
}

std::vector<Transaction> TransactionsFactory::get ()
{
return _transactions;
}
@@ -0,0 +1,49 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 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
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#ifndef INCLUDED_TRANSACTIONSFACTORY
#define INCLUDED_TRANSACTIONSFACTORY

#include <string>
#include <Transaction.h>

class TransactionsFactory
{
public:
void parseLine(const std::string& line);

std::vector< Transaction > get();

private:
std::string _type;
std::string _before;
std::string _after;

std::vector< Transaction > _transactions {};
};


#endif //INCLUDED_TRANSACTIONSFACTORY
@@ -31,6 +31,23 @@
////////////////////////////////////////////////////////////////////////////////
int CmdUndo (Database& database)
{
Transaction transaction = database.popLastTransaction ();

std::vector <UndoAction> actions = transaction.getActions ();

if (actions.empty ())
{
// No (more) undoing...
}
else
{
for (auto& action : actions)
{
// Select database...
// Rollback action...
}
}

return 0;
}

0 comments on commit 76ee22e

Please sign in to comment.