memgraph/include/storage/record_accessor.hpp

124 lines
3.1 KiB
C++
Raw Normal View History

2016-01-02 19:20:51 +08:00
#pragma once
#include "database/db_transaction.hpp"
2016-01-02 19:20:51 +08:00
#include "mvcc/version_list.hpp"
#include "storage/indexes/index_record.hpp"
#include "storage/model/properties/properties.hpp"
#include "storage/model/properties/property.hpp"
#include "storage/model/properties/property_family.hpp"
#include "transactions/transaction.hpp"
2016-01-02 19:20:51 +08:00
template <class T, class Derived, class vlist_t = mvcc::VersionList<T>>
2016-01-02 19:20:51 +08:00
class RecordAccessor
{
friend DbAccessor;
2016-01-02 19:20:51 +08:00
public:
RecordAccessor(vlist_t *vlist, DbTransaction &db) : vlist(vlist), db(db)
{
assert(vlist != nullptr);
}
2016-01-02 19:20:51 +08:00
RecordAccessor(T *t, vlist_t *vlist, DbTransaction &db)
: record(t), vlist(vlist), db(db)
2016-01-02 19:20:51 +08:00
{
2016-01-09 21:43:55 +08:00
assert(record != nullptr);
assert(vlist != nullptr);
2016-01-02 19:20:51 +08:00
}
RecordAccessor(RecordAccessor const &other) = default;
RecordAccessor(RecordAccessor &&other) = default;
bool empty() const { return record == nullptr; }
2016-01-02 19:20:51 +08:00
// Fills accessor and returns true if there is valid data for current
// transaction false otherwise.
bool fill() const
{
const_cast<RecordAccessor *>(this)->record = vlist->find(db.trans);
return record != nullptr;
}
const Id &id() const { return vlist->id; }
2016-01-09 21:43:55 +08:00
Derived update() const
2016-01-02 19:20:51 +08:00
{
assert(!empty());
return Derived(vlist->update(db.trans), vlist, db);
2016-01-02 19:20:51 +08:00
}
bool remove() const
2016-01-02 19:20:51 +08:00
{
assert(!empty());
return vlist->remove(record, db.trans);
2016-01-02 19:20:51 +08:00
}
const Property &at(PropertyFamily &key) const
{
return properties().at(key);
}
const Property &at(prop_key_t &key) const { return properties().at(key); }
template <class V>
auto at(type_key_t<V> &key) const;
2016-01-02 19:20:51 +08:00
template <class V, class... Args>
void set(type_key_t<V> &key, Args &&... args)
2016-01-02 19:20:51 +08:00
{
properties().template set<V>(key, std::forward<Args>(args)...);
}
void set(prop_key_t &key, Property::sptr value)
{
properties().set(key, std::move(value));
}
void clear(prop_key_t &key) { properties().clear(key); }
2016-01-02 19:20:51 +08:00
void clear(PropertyFamily &key) { properties().clear(key); }
template <class Handler>
void accept(Handler &handler) const
{
properties().template accept<Handler>(handler);
}
Properties &properties() const { return record->data.props; }
explicit operator bool() const { return record != nullptr; }
2016-03-13 03:16:19 +08:00
T const *operator->() const { return record; }
T *operator->() { return record; }
// Assumes same transaction
friend bool operator==(const RecordAccessor &a, const RecordAccessor &b)
{
return a.vlist == b.vlist;
}
// Assumes same transaction
friend bool operator!=(const RecordAccessor &a, const RecordAccessor &b)
{
return !(a == b);
}
protected:
2016-08-19 00:43:06 +08:00
IndexRecord<T, std::nullptr_t> create_index_record()
{
return create_index_record(std::nullptr_t());
}
template <class K>
2016-08-19 00:43:06 +08:00
IndexRecord<T, K> create_index_record(K &&key)
{
return IndexRecord<T, K>(std::move(key), record, vlist);
}
T *record{nullptr};
vlist_t *const vlist;
DbTransaction &db;
2016-01-02 19:20:51 +08:00
};