2016-07-05 11:01:22 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
#include "database/db_transaction.hpp"
|
2016-07-05 11:01:22 +08:00
|
|
|
#include "mvcc/version_list.hpp"
|
|
|
|
#include "utils/total_ordering.hpp"
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
// class DbTransaction;
|
|
|
|
// namespace tx
|
|
|
|
// {
|
|
|
|
// class Transaction;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// T type of record.
|
|
|
|
// K key on which record is ordered.
|
|
|
|
template <class T, class K>
|
|
|
|
class IndexRecord : public TotalOrdering<IndexRecord<T, K>>
|
2016-07-05 11:01:22 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using vlist_t = mvcc::VersionList<T>;
|
|
|
|
|
|
|
|
IndexRecord() = default;
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
IndexRecord(K key, T *record, vlist_t *vlist)
|
|
|
|
: key(std::move(key)), record(record), vlist(vlist)
|
2016-07-05 11:01:22 +08:00
|
|
|
{
|
|
|
|
assert(record != nullptr);
|
|
|
|
assert(vlist != nullptr);
|
|
|
|
}
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
friend bool operator<(const IndexRecord &lhs, const IndexRecord &rhs)
|
2016-07-05 11:01:22 +08:00
|
|
|
{
|
2016-08-18 22:34:36 +08:00
|
|
|
return lhs.key < rhs.key ||
|
|
|
|
(lhs.key == rhs.key && lhs.vlist == rhs.vlist &&
|
|
|
|
lhs.record < rhs.record);
|
2016-07-05 11:01:22 +08:00
|
|
|
}
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
friend bool operator==(const IndexRecord &lhs, const IndexRecord &rhs)
|
2016-07-05 11:01:22 +08:00
|
|
|
{
|
2016-08-18 22:34:36 +08:00
|
|
|
return lhs.key == rhs.key &&
|
|
|
|
(lhs.vlist != rhs.vlist || lhs.record == rhs.record);
|
2016-07-05 11:01:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const { return record == nullptr; }
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
bool is_valid(tx::Transaction &t) const
|
|
|
|
{
|
|
|
|
assert(!empty());
|
|
|
|
return record == vlist->find(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto access(DbTransaction &db) const
|
|
|
|
{
|
|
|
|
return T::Accessor::create(record, vlist, db);
|
|
|
|
}
|
2016-07-05 11:01:22 +08:00
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
const K key;
|
|
|
|
|
|
|
|
private:
|
2016-07-05 11:01:22 +08:00
|
|
|
T *const record{nullptr};
|
|
|
|
vlist_t *const vlist{nullptr};
|
|
|
|
};
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
template <class K>
|
|
|
|
using VertexIndexRecord = IndexRecord<Vertex, K>;
|
|
|
|
|
|
|
|
template <class K>
|
|
|
|
using EdgeIndexRecord = IndexRecord<Edge, K>;
|