memgraph/src/storage/record_accessor.hpp

81 lines
1.7 KiB
C++
Raw Normal View History

2016-01-02 19:20:51 +08:00
#pragma once
#include "transactions/transaction.hpp"
#include "mvcc/version_list.hpp"
#include "storage/model/properties/property.hpp"
#include "storage/model/properties/properties.hpp"
2016-01-02 19:20:51 +08:00
template <class T, class Store, class Derived>
class RecordAccessor
{
protected:
using vlist_t = mvcc::VersionList<T>;
public:
RecordAccessor() = default;
RecordAccessor(T* record, vlist_t* vlist, Store* store)
2016-01-09 21:43:55 +08:00
: record(record), vlist(vlist), store(store)
2016-01-02 19:20:51 +08:00
{
2016-01-09 21:43:55 +08:00
assert(record != nullptr);
assert(vlist != nullptr);
assert(store != nullptr);
2016-01-02 19:20:51 +08:00
}
bool empty() const
{
return record == nullptr;
}
2016-01-09 21:43:55 +08:00
const Id& id() const
{
assert(!empty());
2016-03-13 22:39:15 +08:00
return vlist->id;
2016-01-09 21:43:55 +08:00
}
2016-01-02 19:20:51 +08:00
Derived update(tx::Transaction& t) const
{
assert(!empty());
return Derived(vlist->update(t), vlist, store);
2016-01-02 19:20:51 +08:00
}
bool remove(tx::Transaction& t) const
{
assert(!empty());
return vlist->remove(record, t);
2016-01-02 19:20:51 +08:00
}
2016-03-13 03:16:19 +08:00
const Property& property(const std::string& key) const
2016-01-02 19:20:51 +08:00
{
return record->data.props.at(key);
2016-01-02 19:20:51 +08:00
}
template <class V, class... Args>
void property(const std::string& key, Args&&... args)
{
record->data.props.template set<V>(key, std::forward<Args>(args)...);
}
void property(const std::string& key, Property::sptr value)
{
record->data.props.set(key, std::move(value));
}
Properties& properties() const
{
return record->data.props;
2016-01-02 19:20:51 +08:00
}
2016-03-13 03:16:19 +08:00
explicit operator bool() const
{
return record != nullptr;
}
// protected:
2016-01-02 19:20:51 +08:00
T* const record {nullptr};
vlist_t* const vlist {nullptr};
Store* const store {nullptr};
};