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"
|
2016-01-03 05:20:09 +08:00
|
|
|
#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());
|
|
|
|
|
2016-06-26 00:26:26 +08:00
|
|
|
return Derived(vlist->update(t), vlist, store);
|
2016-01-02 19:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool remove(tx::Transaction& t) const
|
|
|
|
{
|
|
|
|
assert(!empty());
|
|
|
|
|
2016-06-26 00:26:26 +08:00
|
|
|
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
|
|
|
{
|
2016-01-03 05:20:09 +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)
|
|
|
|
{
|
2016-01-03 05:20:09 +08:00
|
|
|
record->data.props.template set<V>(key, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-02-22 05:21:15 +08:00
|
|
|
void property(const std::string& key, Property::sptr value)
|
|
|
|
{
|
|
|
|
record->data.props.set(key, std::move(value));
|
|
|
|
}
|
|
|
|
|
2016-01-03 05:20:09 +08:00
|
|
|
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};
|
|
|
|
};
|