From 67538aceeb7e2c71b0026bbb1acb3ee51eb34627 Mon Sep 17 00:00:00 2001 From: Dominik Gleich Date: Thu, 23 Nov 2017 16:36:54 +0100 Subject: [PATCH] Migrate labels/properties/edgetypes to ids Summary: In preparation for distributed storage we need to have labels/properties/edgetypes uniquely identifiable by their ids, which will be global in near future. The old design has to be abandoned because it's not possible to keep track of global labels/properties/edgetypes while they are local pointers. Reviewers: mislav.bradac, florijan Reviewed By: florijan Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D993 --- src/database/graph_db.hpp | 13 ++- src/database/graph_db_accessor.cpp | 12 +-- src/database/graph_db_datatypes.hpp | 54 ++++++++++- src/query/plan/vertex_count_cache.hpp | 7 +- src/storage/concurrent_id_mapper.hpp | 57 +++++++++++ src/storage/edges.hpp | 7 +- src/utils/datetime/timestamp.hpp | 7 +- tests/manual/query_planner.cpp | 96 ++++++++++++------- tests/property_based/random_graph.cpp | 4 +- tests/unit/bolt_encoder.cpp | 25 ++--- tests/unit/concurrent_id_mapper.cpp | 74 ++++++++++++++ tests/unit/durability.cpp | 37 ++++--- tests/unit/graph_db_accessor.cpp | 4 +- tests/unit/graph_db_accessor_index_api.cpp | 7 +- tests/unit/query_common.hpp | 5 +- tests/unit/query_expression_evaluator.cpp | 21 ++-- .../query_plan_create_set_remove_delete.cpp | 4 +- 17 files changed, 330 insertions(+), 104 deletions(-) create mode 100644 src/storage/concurrent_id_mapper.hpp create mode 100644 tests/unit/concurrent_id_mapper.cpp diff --git a/src/database/graph_db.hpp b/src/database/graph_db.hpp index c0254022a..00061d131 100644 --- a/src/database/graph_db.hpp +++ b/src/database/graph_db.hpp @@ -11,6 +11,7 @@ #include "database/indexes/label_property_index.hpp" #include "durability/wal.hpp" #include "mvcc/version_list.hpp" +#include "storage/concurrent_id_mapper.hpp" #include "storage/deferred_deleter.hpp" #include "storage/edge.hpp" #include "storage/garbage_collector.hpp" @@ -114,9 +115,15 @@ class GraphDb { // unique object stores // TODO this should be also garbage collected - ConcurrentSet labels_; - ConcurrentSet edge_types_; - ConcurrentSet properties_; + ConcurrentIdMapper + labels_; + ConcurrentIdMapper + edge_types_; + ConcurrentIdMapper + properties_; // indexes KeyIndex labels_index_; diff --git a/src/database/graph_db_accessor.cpp b/src/database/graph_db_accessor.cpp index 8f99ae4ae..a98b9cb9f 100644 --- a/src/database/graph_db_accessor.cpp +++ b/src/database/graph_db_accessor.cpp @@ -336,37 +336,37 @@ void GraphDbAccessor::RemoveEdge(EdgeAccessor &edge_accessor, GraphDbTypes::Label GraphDbAccessor::Label(const std::string &label_name) { DCHECK(!commited_ && !aborted_) << "Accessor committed or aborted"; - return &(*db_.labels_.access().insert(label_name).first); + return db_.labels_.insert_value(label_name); } const std::string &GraphDbAccessor::LabelName( const GraphDbTypes::Label label) const { DCHECK(!commited_ && !aborted_) << "Accessor committed or aborted"; - return *label; + return db_.labels_.value_by_id(label); } GraphDbTypes::EdgeType GraphDbAccessor::EdgeType( const std::string &edge_type_name) { DCHECK(!commited_ && !aborted_) << "Accessor committed or aborted"; - return &(*db_.edge_types_.access().insert(edge_type_name).first); + return db_.edge_types_.insert_value(edge_type_name); } const std::string &GraphDbAccessor::EdgeTypeName( const GraphDbTypes::EdgeType edge_type) const { DCHECK(!commited_ && !aborted_) << "Accessor committed or aborted"; - return *edge_type; + return db_.edge_types_.value_by_id(edge_type); } GraphDbTypes::Property GraphDbAccessor::Property( const std::string &property_name) { DCHECK(!commited_ && !aborted_) << "Accessor committed or aborted"; - return &(*db_.properties_.access().insert(property_name).first); + return db_.properties_.insert_value(property_name); } const std::string &GraphDbAccessor::PropertyName( const GraphDbTypes::Property property) const { DCHECK(!commited_ && !aborted_) << "Accessor committed or aborted"; - return *property; + return db_.properties_.value_by_id(property); } int64_t GraphDbAccessor::Counter(const std::string &name) { diff --git a/src/database/graph_db_datatypes.hpp b/src/database/graph_db_datatypes.hpp index 4cd8499a9..9f88a8ee0 100644 --- a/src/database/graph_db_datatypes.hpp +++ b/src/database/graph_db_datatypes.hpp @@ -2,10 +2,54 @@ #include +#include "utils/total_ordering.hpp" + namespace GraphDbTypes { -// definitions for what data types are used for a Label, Property, EdgeType -// TODO: Typedefing pointers is terrible astractions, get rid of it. -using Label = const std::string *; -using EdgeType = const std::string *; -using Property = const std::string *; +template +class Common : TotalOrdering { + public: + using StorageT = uint16_t; + + Common() {} + explicit Common(const StorageT storage) : storage_(storage) {} + friend bool operator==(const TSpecificType &a, const TSpecificType &b) { + return a.storage_ == b.storage_; + } + friend bool operator<(const TSpecificType &a, const TSpecificType &b) { + return a.storage_ < b.storage_; + } + + struct Hash { + std::hash hash{}; + size_t operator()(const TSpecificType &t) const { return hash(t.storage_); } + }; + + private: + StorageT storage_{0}; }; + +class Label : public Common