memgraph/transactions/engine.hpp

120 lines
2.1 KiB
C++
Raw Normal View History

#pragma once
#include <atomic>
#include <vector>
#include "transaction.hpp"
#include "transaction_store.hpp"
#include "commit_log.hpp"
2015-10-04 15:47:15 +08:00
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-10-04 15:47:15 +08:00
namespace tx
{
class TransactionError : std::runtime_error
{
public:
using std::runtime_error::runtime_error;
};
class Engine : Lockable<SpinLock>
2015-07-31 18:36:41 +08:00
{
public:
using sptr = std::shared_ptr<Engine>;
Engine() : counter(1) {}
Transaction& begin()
{
auto guard = this->acquire_unique();
auto id = Id(counter.next());
auto t = new Transaction(id, active, *this);
active.insert(id);
store.put(id, t);
2015-10-04 15:47:15 +08:00
return *t;
}
Transaction& advance(const Id& id)
2015-10-04 15:47:15 +08:00
{
auto guard = this->acquire_unique();
2015-10-04 15:47:15 +08:00
auto* t = store.get(id);
2015-10-04 15:47:15 +08:00
if(t == nullptr)
throw TransactionError("transaction does not exist");
// this is a new command
t->cid++;
return *t;
}
2015-07-31 18:36:41 +08:00
void commit(const Transaction& t)
{
auto guard = this->acquire_unique();
CommitLog::get().set_committed(t.id);
2015-07-31 18:36:41 +08:00
finalize(t);
}
void abort(const Transaction& t)
{
auto guard = this->acquire_unique();
CommitLog::get().set_aborted(t.id);
2015-07-31 18:36:41 +08:00
finalize(t);
}
Id last_known_active()
{
auto guard = this->acquire_unique();
2015-07-31 18:36:41 +08:00
return active.front();
}
// total number of transactions started from the beginning of time
2015-07-31 18:36:41 +08:00
uint64_t count()
{
auto guard = this->acquire_unique();
2015-07-31 18:36:41 +08:00
return counter.count();
}
// the number of currently active transactions
size_t size()
{
auto guard = this->acquire_unique();
return active.size();
}
private:
2015-07-31 18:36:41 +08:00
void finalize(const Transaction& t)
{
active.remove(t.id);
2015-10-04 15:47:15 +08:00
// remove transaction from store
store.del(t.id);
}
2015-07-31 18:36:41 +08:00
SimpleCounter<uint64_t> counter;
Snapshot<Id> active;
TransactionStore<uint64_t> store;
};
void Transaction::commit()
{
engine.commit(*this);
}
void Transaction::abort()
{
engine.abort(*this);
}
2015-10-04 15:47:15 +08:00
}