From a00ac885ad8984cb6876faa16b3c871815f6b7e1 Mon Sep 17 00:00:00 2001 From: florijan Date: Wed, 19 Jul 2017 13:55:33 +0200 Subject: [PATCH] GraphDbAccessor - validity assertion added to all functions Reviewers: teon.banek, buda Reviewed By: teon.banek Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D571 --- src/database/graph_db_accessor.cpp | 24 ++++++++++++++++++++++++ src/database/graph_db_accessor.hpp | 14 ++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/database/graph_db_accessor.cpp b/src/database/graph_db_accessor.cpp index 4bec1b16b..1205e8e92 100644 --- a/src/database/graph_db_accessor.cpp +++ b/src/database/graph_db_accessor.cpp @@ -19,6 +19,7 @@ GraphDbAccessor::~GraphDbAccessor() { const std::string &GraphDbAccessor::name() const { return db_.name_; } void GraphDbAccessor::advance_command() { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); transaction_->engine_.Advance(transaction_->id_); } @@ -37,10 +38,13 @@ void GraphDbAccessor::abort() { } bool GraphDbAccessor::should_abort() const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return transaction_->should_abort(); } VertexAccessor GraphDbAccessor::insert_vertex() { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); + // create a vertex auto vertex_vlist = new mvcc::VersionList(*transaction_); @@ -52,6 +56,7 @@ VertexAccessor GraphDbAccessor::insert_vertex() { void GraphDbAccessor::update_label_indices( const GraphDbTypes::Label &label, const VertexAccessor &vertex_accessor, const Vertex *const vertex) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); this->db_.labels_index_.Update(label, vertex_accessor.vlist_, vertex); this->db_.label_property_index_.UpdateOnLabel(label, vertex_accessor.vlist_, vertex); @@ -60,22 +65,26 @@ void GraphDbAccessor::update_label_indices( void GraphDbAccessor::update_property_index( const GraphDbTypes::Property &property, const RecordAccessor &record_accessor, const Vertex *const vertex) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); this->db_.label_property_index_.UpdateOnProperty( property, record_accessor.vlist_, vertex); } int64_t GraphDbAccessor::vertices_count() const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return db_.vertices_.access().size(); } int64_t GraphDbAccessor::vertices_count( const GraphDbTypes::Label &label) const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return db_.labels_index_.Count(label); } int64_t GraphDbAccessor::vertices_count( const GraphDbTypes::Label &label, const GraphDbTypes::Property &property) const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); const LabelPropertyIndex::Key key(label, property); debug_assert(db_.label_property_index_.IndexExists(key), "Index doesn't exist."); @@ -85,6 +94,7 @@ int64_t GraphDbAccessor::vertices_count( int64_t GraphDbAccessor::vertices_count(const GraphDbTypes::Label &label, const GraphDbTypes::Property &property, const PropertyValue &value) const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); const LabelPropertyIndex::Key key(label, property); debug_assert(db_.label_property_index_.IndexExists(key), "Index doesn't exist."); @@ -96,6 +106,7 @@ int64_t GraphDbAccessor::vertices_count( const std::experimental::optional> lower, const std::experimental::optional> upper) const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); const LabelPropertyIndex::Key key(label, property); debug_assert(db_.label_property_index_.IndexExists(key), "Index doesn't exist."); @@ -134,6 +145,7 @@ int64_t GraphDbAccessor::vertices_count( } bool GraphDbAccessor::remove_vertex(VertexAccessor &vertex_accessor) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); vertex_accessor.SwitchNew(); // it's possible the vertex was removed already in this transaction // due to it getting matched multiple times by some patterns @@ -147,6 +159,7 @@ bool GraphDbAccessor::remove_vertex(VertexAccessor &vertex_accessor) { } void GraphDbAccessor::detach_remove_vertex(VertexAccessor &vertex_accessor) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); vertex_accessor.SwitchNew(); for (auto edge_accessor : vertex_accessor.in()) remove_edge(edge_accessor); vertex_accessor.SwitchNew(); @@ -158,6 +171,7 @@ void GraphDbAccessor::detach_remove_vertex(VertexAccessor &vertex_accessor) { EdgeAccessor GraphDbAccessor::insert_edge(VertexAccessor &from, VertexAccessor &to, GraphDbTypes::EdgeType edge_type) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); // create an edge auto edge_vlist = new mvcc::VersionList(*transaction_, *from.vlist_, *to.vlist_, edge_type); @@ -186,15 +200,18 @@ EdgeAccessor GraphDbAccessor::insert_edge(VertexAccessor &from, void GraphDbAccessor::update_edge_type_index( const GraphDbTypes::EdgeType &edge_type, const EdgeAccessor &edge_accessor, const Edge *const edge) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); this->db_.edge_types_index_.Update(edge_type, edge_accessor.vlist_, edge); } int64_t GraphDbAccessor::edges_count() const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return db_.edges_.access().size(); } int64_t GraphDbAccessor::edges_count( const GraphDbTypes::EdgeType &edge_type) const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return db_.edge_types_index_.Count(edge_type); } @@ -211,6 +228,7 @@ void swap_out_edge(std::vector *> &edges, } void GraphDbAccessor::remove_edge(EdgeAccessor &edge_accessor) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); // it's possible the edge was removed already in this transaction // due to it getting matched multiple times by some patterns // we can only delete it once, so check if it's already deleted @@ -222,30 +240,36 @@ void GraphDbAccessor::remove_edge(EdgeAccessor &edge_accessor) { } GraphDbTypes::Label GraphDbAccessor::label(const std::string &label_name) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return &(*db_.labels_.access().insert(label_name).first); } const std::string &GraphDbAccessor::label_name( const GraphDbTypes::Label label) const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return *label; } GraphDbTypes::EdgeType GraphDbAccessor::edge_type( const std::string &edge_type_name) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return &(*db_.edge_types_.access().insert(edge_type_name).first); } const std::string &GraphDbAccessor::edge_type_name( const GraphDbTypes::EdgeType edge_type) const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return *edge_type; } GraphDbTypes::Property GraphDbAccessor::property( const std::string &property_name) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return &(*db_.properties_.access().insert(property_name).first); } const std::string &GraphDbAccessor::property_name( const GraphDbTypes::Property property) const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return *property; } diff --git a/src/database/graph_db_accessor.hpp b/src/database/graph_db_accessor.hpp index db0df58c0..8d0007ba9 100644 --- a/src/database/graph_db_accessor.hpp +++ b/src/database/graph_db_accessor.hpp @@ -99,6 +99,7 @@ class GraphDbAccessor { * ignored). */ auto vertices(bool current_state) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); // wrap version lists into accessors, which will look for visible versions auto accessors = iter::imap([this](auto vlist) { return VertexAccessor(*vlist, *this); }, @@ -127,6 +128,7 @@ class GraphDbAccessor { * @return iterable collection */ auto vertices(const GraphDbTypes::Label &label, bool current_state) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return iter::imap( [this, current_state](auto vlist) { return VertexAccessor(*vlist, *this); @@ -147,6 +149,7 @@ class GraphDbAccessor { */ auto vertices(const GraphDbTypes::Label &label, const GraphDbTypes::Property &property, bool current_state) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); debug_assert(db_.label_property_index_.IndexExists( LabelPropertyIndex::Key(label, property)), "Label+property index doesn't exist."); @@ -173,6 +176,7 @@ class GraphDbAccessor { auto vertices(const GraphDbTypes::Label &label, const GraphDbTypes::Property &property, const PropertyValue &value, bool current_state) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); debug_assert(db_.label_property_index_.IndexExists( LabelPropertyIndex::Key(label, property)), "Label+property index doesn't exist."); @@ -217,6 +221,7 @@ class GraphDbAccessor { const std::experimental::optional> lower, const std::experimental::optional> upper, bool current_state) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); debug_assert(db_.label_property_index_.IndexExists( LabelPropertyIndex::Key(label, property)), "Label+property index doesn't exist."); @@ -255,6 +260,8 @@ class GraphDbAccessor { * ignored). */ auto edges(bool current_state) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); + // wrap version lists into accessors, which will look for visible versions auto accessors = iter::imap([this](auto vlist) { return EdgeAccessor(*vlist, *this); }, @@ -283,6 +290,7 @@ class GraphDbAccessor { * @return iterable collection */ auto edges(const GraphDbTypes::EdgeType &edge_type, bool current_state) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return iter::imap([this, current_state]( auto vlist) { return EdgeAccessor(*vlist, *this); }, db_.edge_types_index_.GetVlists(edge_type, *transaction_, @@ -306,6 +314,8 @@ class GraphDbAccessor { */ void BuildIndex(const GraphDbTypes::Label &label, const GraphDbTypes::Property &property) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); + const LabelPropertyIndex::Key key(label, property); if (db_.label_property_index_.CreateIndex(key) == false) { throw IndexExistsException( @@ -352,6 +362,7 @@ class GraphDbAccessor { */ bool LabelPropertyIndexExists(const GraphDbTypes::Label &label, const GraphDbTypes::Property &property) const { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return db_.label_property_index_.IndexExists( LabelPropertyIndex::Key(label, property)); } @@ -360,6 +371,7 @@ class GraphDbAccessor { * @brief - Returns vector of keys of label-property indices. */ std::vector GetIndicesKeys() { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); return db_.label_property_index_.GetIndicesKeys(); } @@ -503,6 +515,7 @@ class GraphDbAccessor { */ template bool Reconstruct(RecordAccessor &accessor) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); accessor.vlist_->find_set_old_new(*transaction_, accessor.old_, accessor.new_); accessor.current_ = accessor.old_ ? accessor.old_ : accessor.new_; @@ -525,6 +538,7 @@ class GraphDbAccessor { */ template void update(RecordAccessor &accessor) { + debug_assert(!commited_ && !aborted_, "Accessor committed or aborted"); // can't update a deleted record if: // - we only have old_ and it hasn't been deleted // - we have new_ and it hasn't been deleted