memgraph/mvcc/transaction.hpp

48 lines
1.4 KiB
C++
Raw Normal View History

#ifndef MEMGRAPH_MVCC_TRANSACTION_HPP
#define MEMGRAPH_MVCC_TRANSACTION_HPP
#include <cstdlib>
2015-07-31 18:36:41 +08:00
#include <cstdint>
#include <vector>
#include "commit_log.hpp"
2015-07-31 18:36:41 +08:00
struct Transaction
{
2015-07-31 18:36:41 +08:00
Transaction(uint64_t id, std::vector<uint64_t> active)
2015-07-07 22:18:26 +08:00
: id(id), cid(1), active(std::move(active)) {}
// index of this transaction
2015-07-31 18:36:41 +08:00
uint64_t id;
2015-07-07 22:18:26 +08:00
// index of the current command in the current transaction;
uint8_t cid;
// the ids of the currently active transactions used by the mvcc
2015-07-07 22:18:26 +08:00
// implementation for snapshot transaction isolation.
// std::vector is much faster than std::set for fewer number of items
// we don't expect the number of active transactions getting too large.
2015-07-31 18:36:41 +08:00
std::vector<uint64_t> active;
2015-07-07 22:18:26 +08:00
// check weather the transaction with the xid looks committed from the
// database snapshot given to this transaction
2015-07-31 18:36:41 +08:00
bool committed(uint64_t xid) const
{
2015-07-31 18:36:41 +08:00
// transaction xid is newer than id and therefore not visible at all
if (xid > id)
return false;
// transaction xid is not visible if it's currently active. the
// active transactions are sorted ascending and therefore we can stop
// looking as soon as we hit the active transaction with id greater
// than xid
2015-07-07 22:18:26 +08:00
for(size_t i = 0; i < active.size(); ++i)
2015-07-31 18:36:41 +08:00
if(xid <= active[i])
2015-07-07 22:18:26 +08:00
return false;
return true;
}
};
#endif