implemented lazy loading for object_ptr and belongs_to
This commit is contained in:
@@ -3,92 +3,76 @@
|
||||
|
||||
#include "matador/object/primary_key_resolver.hpp"
|
||||
|
||||
#include "matador/object/object_resolver.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <stdexcept>
|
||||
#include <mutex>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class basic_object_proxy {
|
||||
public:
|
||||
virtual ~basic_object_proxy() = default;
|
||||
|
||||
[[nodiscard]] virtual void *get() const = 0;
|
||||
};
|
||||
|
||||
template < typename Type >
|
||||
class object_proxy;
|
||||
|
||||
template < typename Type >
|
||||
class object_resolver {
|
||||
public:
|
||||
virtual ~object_resolver() = default;
|
||||
|
||||
virtual Type* resolve(const object_proxy<Type> &proxy) const = 0;
|
||||
};
|
||||
|
||||
template < typename Type >
|
||||
class static_object_resolver final : public object_resolver<Type> {
|
||||
public:
|
||||
static_object_resolver() = default;
|
||||
explicit static_object_resolver(Type* obj)
|
||||
: obj_(obj) {}
|
||||
explicit static_object_resolver(std::unique_ptr<Type> obj)
|
||||
: obj_(std::move(obj)) {}
|
||||
|
||||
Type* resolve(const object_proxy<Type> &/*proxy*/) const override {
|
||||
return obj_.get();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<Type> obj_{};
|
||||
};
|
||||
|
||||
|
||||
template<class Type>
|
||||
class object_proxy final : public basic_object_proxy {
|
||||
class object_proxy final {
|
||||
public:
|
||||
object_proxy() = default;
|
||||
explicit object_proxy(Type* obj)
|
||||
: resolver_(std::make_unique<static_object_resolver<Type>>(obj))
|
||||
, pk_(primary_key_resolver::resolve_object(*obj).pk) {}
|
||||
|
||||
[[nodiscard]] void *get() const override { return static_cast<void*>(pointer()); }
|
||||
|
||||
Type* operator->() const { return pointer(); }
|
||||
Type& operator*() { return *pointer(); }
|
||||
const Type& operator*() const { return *pointer(); }
|
||||
|
||||
Type* pointer() const { return resolve(); }
|
||||
|
||||
Type& ref() { return *pointer(); }
|
||||
const Type& ref() const { return *pointer(); }
|
||||
|
||||
void reset(Type* obj) {
|
||||
pk_ = primary_key_resolver::resolve_object(*obj).pk;
|
||||
resolver_ = std::make_unique<static_object_resolver<Type>>(obj);
|
||||
// Lazy
|
||||
object_proxy(std::weak_ptr<object_resolver<Type>> resolver, utils::identifier id)
|
||||
: resolver_(resolver)
|
||||
, pk_(std::move(id)) {
|
||||
}
|
||||
void reset(std::unique_ptr<Type> obj) {
|
||||
pk_ = primary_key_resolver::resolve_object(*obj).pk;
|
||||
resolver_ = std::make_unique<static_object_resolver<Type>>(std::move(obj));
|
||||
|
||||
// Eager
|
||||
object_proxy(std::weak_ptr<object_resolver<Type>> resolver, std::shared_ptr<Type> obj)
|
||||
: obj_(obj)
|
||||
, resolver_(resolver)
|
||||
, pk_(primary_key_resolver::resolve_object(*obj).pk) {
|
||||
}
|
||||
void reset(std::unique_ptr<object_resolver<Type>> &&resolver) {
|
||||
resolver_ = std::move(resolver);
|
||||
|
||||
// Transient
|
||||
explicit object_proxy(std::shared_ptr<Type> obj)
|
||||
: obj_(obj)
|
||||
, pk_(primary_key_resolver::resolve_object(*obj).pk) {
|
||||
}
|
||||
|
||||
[[nodiscard]] void *raw_pointer() const { return static_cast<void *>(pointer()); }
|
||||
|
||||
Type *operator->() { return pointer(); }
|
||||
Type &operator*() { return *pointer(); }
|
||||
const Type &operator*() const { return *pointer(); }
|
||||
|
||||
Type *pointer() const { return resolve(); }
|
||||
|
||||
[[nodiscard]] bool empty() const { return resolver_ == nullptr; }
|
||||
[[nodiscard]] bool valid() const { return !empty(); }
|
||||
[[nodiscard]] bool has_primary_key() const { return !pk_.is_null(); }
|
||||
[[nodiscard]] const utils::identifier& primary_key() const { return pk_; }
|
||||
[[nodiscard]] const utils::identifier &primary_key() const { return pk_; }
|
||||
void primary_key(const utils::identifier &pk) { pk_ = pk; }
|
||||
|
||||
private:
|
||||
Type* resolve() const {
|
||||
return resolver_->resolve(*this);
|
||||
if (obj_) {
|
||||
return obj_.get();
|
||||
}
|
||||
|
||||
std::lock_guard lock(mutex_);
|
||||
auto resolver = resolver_.lock();
|
||||
if (!resolver) {
|
||||
return nullptr;
|
||||
// Todo: Add states (Detached, Attached, Transient) - if attached an no resolver is available throw runtime exception
|
||||
// throw std::runtime_error("Detached proxy (session expired)");
|
||||
}
|
||||
|
||||
const_cast<std::shared_ptr<Type>&>(obj_) = resolver->resolve(pk_);
|
||||
|
||||
return obj_.get();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<object_resolver<Type>> resolver_{std::make_unique<static_object_resolver<Type>>()};
|
||||
utils::identifier pk_{};
|
||||
std::shared_ptr<Type> obj_{};
|
||||
std::weak_ptr<object_resolver<Type>> resolver_{};
|
||||
utils::identifier pk_{};
|
||||
mutable std::mutex mutex_{};
|
||||
};
|
||||
|
||||
}
|
||||
#endif //OBJECT_PROXY_HPP
|
||||
|
||||
@@ -12,44 +12,53 @@ template <typename Type>
|
||||
class object_ptr {
|
||||
public:
|
||||
object_ptr()
|
||||
: ptr_(std::make_shared<object_proxy<Type>>()) {}
|
||||
explicit object_ptr(Type *obj)
|
||||
: ptr_(std::make_shared<object_proxy<Type>>(obj)) {}
|
||||
object_ptr(const object_ptr &other) : ptr_(other.ptr_) {}
|
||||
object_ptr(object_ptr &&other) noexcept : ptr_(std::move(other.ptr_)) {}
|
||||
: proxy_(std::make_shared<object_proxy<Type>>()) {}
|
||||
explicit object_ptr(std::shared_ptr<Type> obj)
|
||||
: proxy_(std::make_shared<object_proxy<Type>>(obj)) {}
|
||||
explicit object_ptr(std::shared_ptr<object_proxy<Type>> obj)
|
||||
: proxy_(std::move(obj)) {}
|
||||
object_ptr(const object_ptr &other) = default;
|
||||
object_ptr(object_ptr &&other) noexcept = default;
|
||||
object_ptr &operator=(const object_ptr &other) = default;
|
||||
object_ptr &operator=(object_ptr &&other) = default;
|
||||
|
||||
using value_type = Type;
|
||||
Type* operator->() const { return ptr_->pointer(); }
|
||||
Type& operator*() { return ptr_->ref(); }
|
||||
const Type& operator*() const { return ptr_->ref(); }
|
||||
bool operator==(const object_ptr &other) const {
|
||||
return get() == other.get();
|
||||
}
|
||||
bool operator!=(const object_ptr &other) const { return !operator==(other); }
|
||||
|
||||
[[nodiscard]] bool empty() const { return ptr_->pointer() == nullptr; }
|
||||
void reset(Type *obj) {
|
||||
ptr_->reset(obj);
|
||||
using value_type = Type;
|
||||
|
||||
Type *operator->() const { return get(); }
|
||||
Type &operator*() { return *get(); }
|
||||
const Type &operator*() const { return *get(); }
|
||||
|
||||
[[nodiscard]] bool empty() const { return get() == nullptr; }
|
||||
|
||||
Type *get() const {
|
||||
return proxy_ ? proxy_->pointer() : nullptr;
|
||||
}
|
||||
|
||||
Type* get() const { return static_cast<Type*>(ptr_->pointer()); }
|
||||
void reset() { proxy_.reset(); }
|
||||
|
||||
operator bool() { return valid(); }
|
||||
bool valid() { return ptr_ != nullptr; }
|
||||
bool valid() { return proxy_ != nullptr; }
|
||||
|
||||
[[nodiscard]] bool has_primary_key() const { return proxy_->has_primary_key(); }
|
||||
[[nodiscard]] const utils::identifier &primary_key() const { return proxy_->primary_key(); }
|
||||
void primary_key(const utils::identifier &pk) { proxy_->primary_key(pk); }
|
||||
|
||||
[[nodiscard]] bool has_primary_key() const { return ptr_->has_primary_key(); }
|
||||
[[nodiscard]] const utils::identifier& primary_key() const { return ptr_->primary_key(); }
|
||||
void primary_key(const utils::identifier &pk) { ptr_->primary_key(pk); }
|
||||
private:
|
||||
std::shared_ptr<object_proxy<Type>> ptr_{};
|
||||
std::shared_ptr<object_proxy<Type> > proxy_{};
|
||||
};
|
||||
|
||||
template<typename>
|
||||
struct is_object_ptr : std::false_type
|
||||
{};
|
||||
struct is_object_ptr : std::false_type {
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
struct is_object_ptr<object_ptr<Type>> : std::true_type
|
||||
{};
|
||||
|
||||
struct is_object_ptr<object_ptr<Type> > : std::true_type {
|
||||
};
|
||||
}
|
||||
|
||||
#endif //OBJECT_PTR_HPP
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef MATADOR_OBJECT_LOADER_HPP
|
||||
#define MATADOR_OBJECT_LOADER_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <typeindex>
|
||||
|
||||
namespace matador::utils {
|
||||
class identifier;
|
||||
}
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class abstract_object_resolver {
|
||||
public:
|
||||
virtual ~abstract_object_resolver() = default;
|
||||
|
||||
[[nodiscard]] const std::type_index& type() const { return type_; }
|
||||
|
||||
protected:
|
||||
explicit abstract_object_resolver(const std::type_index& ti) : type_(ti) {}
|
||||
|
||||
public:
|
||||
const std::type_index type_;
|
||||
};
|
||||
|
||||
|
||||
template<typename Type>
|
||||
class object_resolver : public abstract_object_resolver {
|
||||
public:
|
||||
object_resolver() : abstract_object_resolver(typeid(Type)) {}
|
||||
|
||||
virtual std::shared_ptr<Type> resolve(const utils::identifier& id) = 0;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_OBJECT_LOADER_HPP
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef MATADOR_OBJECT_RESOLVER_FACTORY_HPP
|
||||
#define MATADOR_OBJECT_RESOLVER_FACTORY_HPP
|
||||
|
||||
#include "matador/object/object_resolver.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::object {
|
||||
class object_resolver_factory {
|
||||
public:
|
||||
virtual ~object_resolver_factory() = default;
|
||||
|
||||
template<class Type>
|
||||
std::shared_ptr<object_resolver<Type>> resolver() {
|
||||
const auto res = acquire_resolver(std::type_index(typeid(Type)));
|
||||
if (!res) {
|
||||
return std::dynamic_pointer_cast<object_resolver<Type>>(res);
|
||||
}
|
||||
|
||||
return std::dynamic_pointer_cast<object_resolver<Type>>(res);
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<abstract_object_resolver> acquire_resolver(const std::type_index &type) = 0;
|
||||
virtual void register_resolver(std::shared_ptr<abstract_object_resolver> &&resolver) = 0;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_OBJECT_RESOLVER_FACTORY_HPP
|
||||
@@ -5,7 +5,6 @@
|
||||
#include "matador/object/foreign_node_completer.hpp"
|
||||
#include "matador/object/many_to_many_relation.hpp"
|
||||
#include "matador/object/join_columns_collector.hpp"
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
#include "matador/object/repository_node.hpp"
|
||||
|
||||
#include "matador/logger/log_manager.hpp"
|
||||
|
||||
Reference in New Issue
Block a user