From 4ec3f62f4eeb374a4eafc36e90e1978aa83ef62f Mon Sep 17 00:00:00 2001 From: Matija Santl Date: Wed, 22 May 2019 09:54:28 +0200 Subject: [PATCH] Move constraints to common Summary: Preparing unique constraint code to be implemented in HA. To do so, I'm moving everything to `stograge/common/` folder. I also added a new header, `gid.hpp` which does a `ifdef` include of the correct `gid.hpp` based on the product. Reviewers: ipaljak, mferencevic, vkasljevic, teon.banek Reviewed By: mferencevic Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D2079 --- src/CMakeLists.txt | 6 +- .../single_node/graph_db_accessor.hpp | 2 +- .../constraints/common.hpp | 4 +- .../constraints/exceptions.hpp | 0 .../constraints/record.cpp | 8 +- .../constraints/record.hpp | 2 +- .../constraints/unique_constraints.cpp | 4 +- .../constraints/unique_constraints.hpp | 2 +- src/storage/common/mvcc/exceptions.hpp | 15 ++++ src/storage/distributed/mvcc/version_list.hpp | 12 +-- src/storage/gid.hpp | 13 ++++ .../constraints/existence_constraints.cpp | 74 ------------------- src/storage/single_node/mvcc/version_list.hpp | 12 +-- src/storage/single_node/storage.hpp | 2 +- .../single_node_ha/mvcc/version_list.hpp | 12 +-- tests/unit/unique_constraints.cpp | 2 +- 16 files changed, 51 insertions(+), 119 deletions(-) rename src/storage/{single_node => common}/constraints/common.hpp (94%) rename src/storage/{single_node => common}/constraints/exceptions.hpp (100%) rename src/storage/{single_node => common}/constraints/record.cpp (90%) rename src/storage/{single_node => common}/constraints/record.hpp (93%) rename src/storage/{single_node => common}/constraints/unique_constraints.cpp (98%) rename src/storage/{single_node => common}/constraints/unique_constraints.hpp (99%) create mode 100644 src/storage/common/mvcc/exceptions.hpp create mode 100644 src/storage/gid.hpp delete mode 100644 src/storage/single_node/constraints/existence_constraints.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f6044bfee..6ea1aa26f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -63,14 +63,14 @@ set(mg_single_node_sources query/plan/variable_start_planner.cpp query/repl.cpp query/typed_value.cpp - storage/single_node/edge_accessor.cpp + storage/common/constraints/record.cpp + storage/common/constraints/unique_constraints.cpp storage/common/locking/record_lock.cpp storage/common/types/property_value.cpp storage/common/types/property_value_store.cpp + storage/single_node/edge_accessor.cpp storage/single_node/record_accessor.cpp storage/single_node/vertex_accessor.cpp - storage/single_node/constraints/record.cpp - storage/single_node/constraints/unique_constraints.cpp transactions/single_node/engine.cpp memgraph_init.cpp ) diff --git a/src/database/single_node/graph_db_accessor.hpp b/src/database/single_node/graph_db_accessor.hpp index 110403f41..222b5d2b1 100644 --- a/src/database/single_node/graph_db_accessor.hpp +++ b/src/database/single_node/graph_db_accessor.hpp @@ -13,8 +13,8 @@ #include "database/single_node/exceptions.hpp" #include "database/single_node/graph_db.hpp" +#include "storage/common/constraints/exceptions.hpp" #include "storage/common/types/types.hpp" -#include "storage/single_node/constraints/exceptions.hpp" #include "storage/single_node/edge_accessor.hpp" #include "storage/single_node/vertex_accessor.hpp" #include "transactions/transaction.hpp" diff --git a/src/storage/single_node/constraints/common.hpp b/src/storage/common/constraints/common.hpp similarity index 94% rename from src/storage/single_node/constraints/common.hpp rename to src/storage/common/constraints/common.hpp index bc8ef0561..011acd289 100644 --- a/src/storage/single_node/constraints/common.hpp +++ b/src/storage/common/constraints/common.hpp @@ -2,8 +2,10 @@ #pragma once +#include + #include "storage/common/types/property_value_store.hpp" -#include "transactions/single_node/engine.hpp" +#include "transactions/engine.hpp" #include "transactions/snapshot.hpp" namespace storage::constraints::common { diff --git a/src/storage/single_node/constraints/exceptions.hpp b/src/storage/common/constraints/exceptions.hpp similarity index 100% rename from src/storage/single_node/constraints/exceptions.hpp rename to src/storage/common/constraints/exceptions.hpp diff --git a/src/storage/single_node/constraints/record.cpp b/src/storage/common/constraints/record.cpp similarity index 90% rename from src/storage/single_node/constraints/record.cpp rename to src/storage/common/constraints/record.cpp index 443ea369c..3604a79b2 100644 --- a/src/storage/single_node/constraints/record.cpp +++ b/src/storage/common/constraints/record.cpp @@ -1,8 +1,8 @@ -#include "storage/single_node/constraints/record.hpp" +#include "storage/common/constraints/record.hpp" -#include "storage/single_node/constraints/exceptions.hpp" -#include "storage/single_node/mvcc/version_list.hpp" -#include "transactions/single_node/engine.hpp" +#include "storage/common/constraints/exceptions.hpp" +#include "storage/common/mvcc/exceptions.hpp" +#include "transactions/engine.hpp" #include "transactions/transaction.hpp" namespace storage::constraints::impl { diff --git a/src/storage/single_node/constraints/record.hpp b/src/storage/common/constraints/record.hpp similarity index 93% rename from src/storage/single_node/constraints/record.hpp rename to src/storage/common/constraints/record.hpp index 97271ee1c..4dd6db93c 100644 --- a/src/storage/single_node/constraints/record.hpp +++ b/src/storage/common/constraints/record.hpp @@ -3,7 +3,7 @@ #pragma once #include "storage/common/locking/record_lock.hpp" -#include "storage/single_node/gid.hpp" +#include "storage/gid.hpp" #include "transactions/type.hpp" namespace tx { diff --git a/src/storage/single_node/constraints/unique_constraints.cpp b/src/storage/common/constraints/unique_constraints.cpp similarity index 98% rename from src/storage/single_node/constraints/unique_constraints.cpp rename to src/storage/common/constraints/unique_constraints.cpp index 1a193369f..4d9367062 100644 --- a/src/storage/single_node/constraints/unique_constraints.cpp +++ b/src/storage/common/constraints/unique_constraints.cpp @@ -1,8 +1,8 @@ -#include "storage/single_node/constraints/unique_constraints.hpp" +#include "storage/common/constraints/unique_constraints.hpp" #include -#include "storage/single_node/vertex_accessor.hpp" +#include "storage/vertex_accessor.hpp" namespace storage::constraints { diff --git a/src/storage/single_node/constraints/unique_constraints.hpp b/src/storage/common/constraints/unique_constraints.hpp similarity index 99% rename from src/storage/single_node/constraints/unique_constraints.hpp rename to src/storage/common/constraints/unique_constraints.hpp index 263896a6b..a2b311498 100644 --- a/src/storage/single_node/constraints/unique_constraints.hpp +++ b/src/storage/common/constraints/unique_constraints.hpp @@ -7,7 +7,7 @@ #include "storage/common/types/property_value.hpp" #include "storage/common/types/types.hpp" -#include "storage/single_node/constraints/record.hpp" +#include "storage/common/constraints/record.hpp" namespace tx { class Snapshot; diff --git a/src/storage/common/mvcc/exceptions.hpp b/src/storage/common/mvcc/exceptions.hpp new file mode 100644 index 000000000..7332287f5 --- /dev/null +++ b/src/storage/common/mvcc/exceptions.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "utils/exceptions.hpp" + +namespace mvcc { +class SerializationError : public utils::BasicException { + static constexpr const char *default_message = + "Can't serialize due to concurrent operations."; + + public: + using utils::BasicException::BasicException; + SerializationError() : BasicException(default_message) {} +}; + +} // namespace mvcc diff --git a/src/storage/distributed/mvcc/version_list.hpp b/src/storage/distributed/mvcc/version_list.hpp index e42d2d3a8..e21f1142c 100644 --- a/src/storage/distributed/mvcc/version_list.hpp +++ b/src/storage/distributed/mvcc/version_list.hpp @@ -1,21 +1,13 @@ #pragma once -#include "storage/distributed/gid.hpp" #include "storage/common/locking/record_lock.hpp" +#include "storage/common/mvcc/exceptions.hpp" +#include "storage/distributed/gid.hpp" #include "transactions/transaction.hpp" #include "utils/exceptions.hpp" namespace mvcc { -class SerializationError : public utils::BasicException { - static constexpr const char *default_message = - "Can't serialize due to concurrent operations."; - - public: - using utils::BasicException::BasicException; - SerializationError() : BasicException(default_message) {} -}; - template class VersionList { public: diff --git a/src/storage/gid.hpp b/src/storage/gid.hpp new file mode 100644 index 000000000..68d93f641 --- /dev/null +++ b/src/storage/gid.hpp @@ -0,0 +1,13 @@ +#pragma once + +#ifdef MG_SINGLE_NODE +#include "storage/single_node/gid.hpp" +#endif + +#ifdef MG_SINGLE_NODE_HA +#include "storage/single_node_ha/gid.hpp" +#endif + +#ifdef MG_DISTRIBUTED +#include "storage/distributed/gid.hpp" +#endif diff --git a/src/storage/single_node/constraints/existence_constraints.cpp b/src/storage/single_node/constraints/existence_constraints.cpp deleted file mode 100644 index 041bdd6ce..000000000 --- a/src/storage/single_node/constraints/existence_constraints.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "storage/single_node/constraints/existence_constraints.hpp" - -#include "storage/single_node/constraints/common.hpp" - -namespace storage::constraints { -bool Contains(const PropertyValueStore &store, - const std::vector &properties) { - for (auto property : properties) { - if (store.at(property).IsNull()) { - return false; - } - } - - return true; -} - -bool CheckIfSatisfiesExistenceRule(const Vertex *vertex, - const ExistenceRule &rule) { - if (!utils::Contains(vertex->labels_, rule.label)) return true; - if (!Contains(vertex->properties_, rule.properties)) return false; - - return true; -} - -bool ExistenceConstraints::AddConstraint(const ExistenceRule &rule) { - auto found = std::find(constraints_.begin(), constraints_.end(), rule); - if (found != constraints_.end()) return false; - - constraints_.push_back(rule); - return true; -} - -bool ExistenceConstraints::RemoveConstraint(const ExistenceRule &rule) { - auto found = std::find(constraints_.begin(), constraints_.end(), rule); - if (found != constraints_.end()) { - std::swap(*found, constraints_.back()); - constraints_.pop_back(); - return true; - } - - return false; -} - -bool ExistenceConstraints::Exists(const ExistenceRule &rule) const { - auto found = std::find(constraints_.begin(), constraints_.end(), rule); - return found != constraints_.end(); -} - -bool ExistenceConstraints::CheckOnAddLabel(const Vertex *vertex, - storage::Label label) const { - for (auto &constraint : constraints_) { - if (constraint.label == label && - !CheckIfSatisfiesExistenceRule(vertex, constraint)) { - return false; - } - } - return true; -} - -bool ExistenceConstraints::CheckOnRemoveProperty( - const Vertex *vertex, storage::Property property) const { - for (auto &constraint : constraints_) { - if (utils::Contains(constraint.properties, property) && - !CheckIfSatisfiesExistenceRule(vertex, constraint)) { - return false; - } - } - return true; -} - -const std::vector &ExistenceConstraints::ListConstraints() const { - return constraints_; -} -} // namespace storage::constraints diff --git a/src/storage/single_node/mvcc/version_list.hpp b/src/storage/single_node/mvcc/version_list.hpp index 1a57dc7e3..a4dd27c03 100644 --- a/src/storage/single_node/mvcc/version_list.hpp +++ b/src/storage/single_node/mvcc/version_list.hpp @@ -1,22 +1,14 @@ #pragma once -#include "storage/single_node/gid.hpp" #include "storage/common/locking/record_lock.hpp" +#include "storage/common/mvcc/exceptions.hpp" +#include "storage/single_node/gid.hpp" #include "transactions/transaction.hpp" #include "utils/cast.hpp" #include "utils/exceptions.hpp" namespace mvcc { -class SerializationError : public utils::BasicException { - static constexpr const char *default_message = - "Can't serialize due to concurrent operations."; - - public: - using utils::BasicException::BasicException; - SerializationError() : BasicException(default_message) {} -}; - template class VersionList { public: diff --git a/src/storage/single_node/storage.hpp b/src/storage/single_node/storage.hpp index d3152fe57..d4d5f24a1 100644 --- a/src/storage/single_node/storage.hpp +++ b/src/storage/single_node/storage.hpp @@ -4,9 +4,9 @@ #include #include "data_structures/concurrent/concurrent_map.hpp" +#include "storage/common/constraints/unique_constraints.hpp" #include "storage/common/kvstore/kvstore.hpp" #include "storage/common/types/types.hpp" -#include "storage/single_node/constraints/unique_constraints.hpp" #include "storage/single_node/edge.hpp" #include "storage/single_node/indexes/key_index.hpp" #include "storage/single_node/indexes/label_property_index.hpp" diff --git a/src/storage/single_node_ha/mvcc/version_list.hpp b/src/storage/single_node_ha/mvcc/version_list.hpp index 533c72069..4ff7ebef6 100644 --- a/src/storage/single_node_ha/mvcc/version_list.hpp +++ b/src/storage/single_node_ha/mvcc/version_list.hpp @@ -1,22 +1,14 @@ #pragma once -#include "storage/single_node_ha/gid.hpp" #include "storage/common/locking/record_lock.hpp" +#include "storage/common/mvcc/exceptions.hpp" +#include "storage/single_node_ha/gid.hpp" #include "transactions/transaction.hpp" #include "utils/cast.hpp" #include "utils/exceptions.hpp" namespace mvcc { -class SerializationError : public utils::BasicException { - static constexpr const char *default_message = - "Can't serialize due to concurrent operations."; - - public: - using utils::BasicException::BasicException; - SerializationError() : BasicException(default_message) {} -}; - template class VersionList { public: diff --git a/tests/unit/unique_constraints.cpp b/tests/unit/unique_constraints.cpp index 3992f5eaa..3e653fae8 100644 --- a/tests/unit/unique_constraints.cpp +++ b/tests/unit/unique_constraints.cpp @@ -2,7 +2,7 @@ #include "database/single_node/graph_db.hpp" #include "database/single_node/graph_db_accessor.hpp" -#include "storage/single_node/constraints/unique_constraints.hpp" +#include "storage/common/constraints/unique_constraints.hpp" using storage::constraints::ConstraintEntry; using storage::constraints::UniqueConstraints;