renamed schema to repository
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
#ifndef SCHEMA_HPP
|
||||
#define SCHEMA_HPP
|
||||
|
||||
#include "matador/logger/log_manager.hpp"
|
||||
|
||||
#include "matador/object/error_code.hpp"
|
||||
#include "matador/object/foreign_node_completer.hpp"
|
||||
#include "matador/object/relation_completer.hpp"
|
||||
#include "matador/object/repository_node.hpp"
|
||||
#include "matador/object/repository_node_iterator.hpp"
|
||||
|
||||
#include "matador/utils/result.hpp"
|
||||
#include "matador/utils/error.hpp"
|
||||
|
||||
#include "matador/logger/logger.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace matador::object {
|
||||
namespace internal {
|
||||
class shadow_repository;
|
||||
}
|
||||
|
||||
utils::error make_error(error_code ec, const std::string &msg);
|
||||
|
||||
class repository {
|
||||
public:
|
||||
typedef const_repository_node_iterator const_iterator; /**< Shortcut for the list const iterator. */
|
||||
|
||||
using node_ptr = std::shared_ptr<repository_node>;
|
||||
|
||||
/**
|
||||
* Creates an empty schema
|
||||
*/
|
||||
explicit repository(std::string name = "");
|
||||
|
||||
template<typename Type>
|
||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent = "") {
|
||||
if (const auto it = nodes_by_type_.find(typeid(Type)); it == nodes_by_type_.end() ) {
|
||||
// if the type was not found
|
||||
auto node = repository_node::make_node<Type>(*this, name);
|
||||
if (auto result = attach_node(node, parent); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
foreign_node_completer::complete<Type>(node);
|
||||
relation_completer<Type>::complete(node);
|
||||
} else if (!has_node(name)) {
|
||||
it->second->update_name(name);
|
||||
nodes_by_name_[name] = it->second;
|
||||
relation_completer<Type>::complete(it->second);
|
||||
log_.info("attach: update node name to '%s' (type: %s)", it->second->name().c_str(), it->second->type_index().name());
|
||||
} else {
|
||||
// remove_node( )
|
||||
return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
|
||||
}
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
template<typename Type, typename SuperType>
|
||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name) {
|
||||
const auto ti = std::type_index(typeid(SuperType));
|
||||
auto result = find_node(ti);
|
||||
if (!result) {
|
||||
return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + std::string(ti.name()) + "' not found"));
|
||||
}
|
||||
|
||||
return attach<Type>(name, (*result)->name());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Detaches a given node from the schema. If the
|
||||
* node is a parent of other nodes, these nodes are
|
||||
* detached as well.
|
||||
*
|
||||
* @param node Node to detach from schema
|
||||
* @return Result object indicating success or failure
|
||||
*/
|
||||
[[nodiscard]] utils::result<void, utils::error> detach(const node_ptr &node);
|
||||
|
||||
/**
|
||||
* Return the first schema node.
|
||||
*
|
||||
* @return The first schema node iterator.
|
||||
*/
|
||||
[[nodiscard]] const_iterator begin() const;
|
||||
|
||||
/**
|
||||
* Return the last schema node.
|
||||
*
|
||||
* @return The last schema node iterator.
|
||||
*/
|
||||
[[nodiscard]] const_iterator end() const;
|
||||
|
||||
/**
|
||||
* Returns true if the schema contains
|
||||
* no schema nodes.
|
||||
*
|
||||
* @return True if the schema is empty
|
||||
*/
|
||||
[[nodiscard]] bool empty() const;
|
||||
|
||||
/**
|
||||
* Returns the current number of the schema node.
|
||||
*
|
||||
* @return Number of schema nodes
|
||||
*/
|
||||
[[nodiscard]] size_t size() const;
|
||||
|
||||
/**
|
||||
* Returns the name of the schema.
|
||||
*
|
||||
* @return The name of the schema
|
||||
*/
|
||||
[[nodiscard]] std::string name() const;
|
||||
|
||||
[[nodiscard]] bool contains(const std::string &name) const;
|
||||
[[nodiscard]] bool contains(const std::type_index &index) const;
|
||||
template < typename Type >
|
||||
[[nodiscard]] bool contains() const {
|
||||
return contains(std::type_index(typeid(Type)));
|
||||
}
|
||||
|
||||
|
||||
template<typename Type>
|
||||
[[nodiscard]] utils::result<object_info_ref<Type>, utils::error> info() const {
|
||||
auto result = find_node(std::type_index(typeid(Type)));
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(result.value()->info<Type>());
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info() const {
|
||||
auto result = find_node(std::type_index(typeid(Type)));
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
[[nodiscard]] utils::result<std::shared_ptr<attribute_definition>, utils::error> reference_column(
|
||||
const std::type_index &type_index) const;
|
||||
|
||||
void dump(std::ostream &os) const;
|
||||
static void dump(std::ostream &os, const node_ptr& node);
|
||||
|
||||
private:
|
||||
using t_node_map = std::unordered_map<std::string, node_ptr>;
|
||||
using t_type_index_node_map = std::unordered_map<std::type_index, node_ptr>;
|
||||
|
||||
[[nodiscard]] utils::result<node_ptr, utils::error> attach_node(const node_ptr &node, const std::string &parent);
|
||||
// [[nodiscard]] utils::result<node_ptr, utils::error> attach_node(const node_ptr &node, const std::type_index &type_index);
|
||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
|
||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
|
||||
template<typename Type>
|
||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node() const {
|
||||
return find_node(std::type_index(typeid(Type)));
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has_node(const std::string &name) const;
|
||||
[[nodiscard]] bool has_node(const std::type_index &index) const;
|
||||
[[nodiscard]] bool has_node(const node_ptr& node) const;
|
||||
|
||||
static void insert_node(const node_ptr &parent, const node_ptr &child);
|
||||
void remove_node(const node_ptr &node);
|
||||
|
||||
private:
|
||||
friend class internal::shadow_repository;
|
||||
friend class repository_node;
|
||||
friend class attribute_definition_generator;
|
||||
|
||||
std::string name_;
|
||||
std::shared_ptr<repository_node> root_;
|
||||
|
||||
t_node_map nodes_by_name_;
|
||||
t_type_index_node_map nodes_by_type_;
|
||||
logger::logger log_;
|
||||
|
||||
std::unordered_map<std::type_index, std::shared_ptr<attribute_definition> > missing_references_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //SCHEMA_HPP
|
||||
Reference in New Issue
Block a user