memgraph/mvcc/transaction_engine.hpp

86 lines
1.7 KiB
C++
Raw Normal View History

#ifndef MEMGRAPH_MVCC_TRANSACTIONENGINE_HPP
#define MEMGRAPH_MVCC_TRANSACTIONENGINE_HPP
#include <cstdlib>
#include <atomic>
#include <mutex>
#include <vector>
#include <set>
#include <list>
2015-07-31 18:36:41 +08:00
#include <algorithm>
#include "transaction.hpp"
2015-07-31 18:36:41 +08:00
#include "utils/counters/simple_counter.hpp"
#include "threading/sync/spinlock.hpp"
#include "threading/sync/lockable.hpp"
2015-07-31 18:36:41 +08:00
class TransactionEngine : Lockable<SpinLock>
{
public:
2015-07-31 18:36:41 +08:00
TransactionEngine(uint64_t n) : counter(n) {}
2015-07-31 18:36:41 +08:00
Transaction begin()
{
2015-07-31 18:36:41 +08:00
auto guard = this->acquire();
2015-07-31 18:36:41 +08:00
auto id = counter.next();
auto t = Transaction(id, active);
active.push_back(id);
return t;
}
2015-07-31 18:36:41 +08:00
void commit(const Transaction& t)
{
2015-07-31 18:36:41 +08:00
auto guard = this->acquire();
2015-07-31 18:36:41 +08:00
finalize(t);
}
2015-07-31 18:36:41 +08:00
void rollback(const Transaction& t)
{
2015-07-31 18:36:41 +08:00
auto guard = this->acquire();
// what to do here?
2015-07-31 18:36:41 +08:00
finalize(t);
}
2015-07-31 18:36:41 +08:00
uint64_t last_known_active()
{
2015-07-31 18:36:41 +08:00
auto guard = this->acquire();
return active.front();
}
// total number of transactions started from the beginning of time
2015-07-31 18:36:41 +08:00
uint64_t count()
{
2015-07-31 18:36:41 +08:00
auto guard = this->acquire();
return counter.count();
}
// the number of currently active transactions
size_t size()
{
2015-07-31 18:36:41 +08:00
auto guard = this->acquire();
return active.size();
}
private:
2015-07-31 18:36:41 +08:00
void finalize(const Transaction& t)
{
2015-07-31 18:36:41 +08:00
auto x = t.id;
// remove transaction from the active transactions list
2015-07-31 18:36:41 +08:00
auto last = std::remove(active.begin(), active.end(), x);
active.erase(last, active.end());
}
2015-07-31 18:36:41 +08:00
SimpleCounter<uint64_t> counter;
2015-07-31 18:36:41 +08:00
std::vector<uint64_t> active;
};
#endif