2016-01-02 19:20:51 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
#include "database/db_transaction.hpp"
|
2016-01-02 19:20:51 +08:00
|
|
|
#include "mvcc/version_list.hpp"
|
2016-08-18 22:34:36 +08:00
|
|
|
#include "storage/indexes/index_record.hpp"
|
2016-01-03 05:20:09 +08:00
|
|
|
#include "storage/model/properties/properties.hpp"
|
2016-08-10 03:29:03 +08:00
|
|
|
#include "storage/model/properties/property.hpp"
|
2016-08-18 22:34:36 +08:00
|
|
|
#include "storage/model/properties/property_family.hpp"
|
2016-08-10 03:29:03 +08:00
|
|
|
#include "transactions/transaction.hpp"
|
2016-01-02 19:20:51 +08:00
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
template <class T, class Derived, class vlist_t = mvcc::VersionList<T>>
|
2016-01-02 19:20:51 +08:00
|
|
|
class RecordAccessor
|
|
|
|
{
|
2016-08-15 07:09:58 +08:00
|
|
|
friend DbAccessor;
|
|
|
|
|
2016-01-02 19:20:51 +08:00
|
|
|
public:
|
2016-08-15 07:09:58 +08:00
|
|
|
RecordAccessor(vlist_t *vlist, DbTransaction &db) : vlist(vlist), db(db)
|
|
|
|
{
|
|
|
|
assert(vlist != nullptr);
|
|
|
|
}
|
2016-01-02 19:20:51 +08:00
|
|
|
|
2016-08-15 07:09:58 +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
|
|
|
}
|
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
RecordAccessor(RecordAccessor const &other) = default;
|
|
|
|
RecordAccessor(RecordAccessor &&other) = default;
|
|
|
|
|
2016-08-10 03:29:03 +08:00
|
|
|
bool empty() const { return record == nullptr; }
|
2016-01-02 19:20:51 +08:00
|
|
|
|
2016-08-15 07:09:58 +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;
|
|
|
|
}
|
|
|
|
|
2016-08-20 01:40:04 +08:00
|
|
|
const Id &id() const { return vlist->id; }
|
2016-01-09 21:43:55 +08:00
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
Derived update() const
|
2016-01-02 19:20:51 +08:00
|
|
|
{
|
|
|
|
assert(!empty());
|
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
return Derived(vlist->update(db.trans), vlist, db);
|
2016-01-02 19:20:51 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
bool remove() const
|
2016-01-02 19:20:51 +08:00
|
|
|
{
|
|
|
|
assert(!empty());
|
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
return vlist->remove(record, db.trans);
|
2016-01-02 19:20:51 +08:00
|
|
|
}
|
|
|
|
|
2016-08-20 01:40:04 +08:00
|
|
|
const Property &at(PropertyFamily &key) const
|
|
|
|
{
|
|
|
|
return properties().at(key);
|
|
|
|
}
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
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>
|
2016-08-18 22:34:36 +08:00
|
|
|
void set(type_key_t<V> &key, Args &&... args)
|
2016-01-02 19:20:51 +08:00
|
|
|
{
|
2016-08-18 22:34:36 +08:00
|
|
|
properties().template set<V>(key, std::forward<Args>(args)...);
|
2016-01-03 05:20:09 +08:00
|
|
|
}
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
void set(prop_key_t &key, Property::sptr value)
|
2016-02-22 05:21:15 +08:00
|
|
|
{
|
2016-08-18 22:34:36 +08:00
|
|
|
properties().set(key, std::move(value));
|
2016-02-22 05:21:15 +08:00
|
|
|
}
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
void clear(prop_key_t &key) { properties().clear(key); }
|
2016-01-02 19:20:51 +08:00
|
|
|
|
2016-08-20 01:40:04 +08:00
|
|
|
void clear(PropertyFamily &key) { properties().clear(key); }
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
template <class Handler>
|
|
|
|
void accept(Handler &handler) const
|
2016-08-15 20:21:38 +08:00
|
|
|
{
|
2016-08-18 22:34:36 +08:00
|
|
|
properties().template accept<Handler>(handler);
|
2016-08-15 20:21:38 +08:00
|
|
|
}
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
Properties &properties() const { return record->data.props; }
|
|
|
|
|
2016-08-10 03:29:03 +08:00
|
|
|
explicit operator bool() const { return record != nullptr; }
|
2016-03-13 03:16:19 +08:00
|
|
|
|
2016-08-15 07:09:58 +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());
|
|
|
|
}
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
template <class K>
|
2016-08-19 00:43:06 +08:00
|
|
|
IndexRecord<T, K> create_index_record(K &&key)
|
2016-08-18 22:34:36 +08:00
|
|
|
{
|
|
|
|
return IndexRecord<T, K>(std::move(key), record, vlist);
|
|
|
|
}
|
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
T *record{nullptr};
|
|
|
|
vlist_t *const vlist;
|
|
|
|
DbTransaction &db;
|
2016-01-02 19:20:51 +08:00
|
|
|
};
|