From 9c05910e68b2f3dd8bb7fb5f8f5bbb386b566658 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 15 Nov 2022 18:25:12 +0100 Subject: [PATCH] Adapt storage to new erro handling --- src/storage/v3/bindings/db_accessor.hpp | 10 +-- src/storage/v3/edge_accessor.cpp | 31 +++---- src/storage/v3/edge_accessor.hpp | 10 +-- src/storage/v3/result.hpp | 4 +- src/storage/v3/schema_validator.cpp | 2 +- src/storage/v3/shard.cpp | 42 ++++----- src/storage/v3/shard.hpp | 11 ++- src/storage/v3/shard_rsm.cpp | 98 +++++++------------- src/storage/v3/vertex_accessor.cpp | 113 ++++++++++++------------ src/storage/v3/vertex_accessor.hpp | 48 +++++----- 10 files changed, 170 insertions(+), 199 deletions(-) diff --git a/src/storage/v3/bindings/db_accessor.hpp b/src/storage/v3/bindings/db_accessor.hpp index 186a1b5c5..1a5e846fa 100644 --- a/src/storage/v3/bindings/db_accessor.hpp +++ b/src/storage/v3/bindings/db_accessor.hpp @@ -78,8 +78,8 @@ class DbAccessor final { return VerticesIterable(accessor_->Vertices(label, property, lower, upper, view)); } - storage::v3::Result InsertEdge(VertexAccessor *from, VertexAccessor *to, - const storage::v3::EdgeTypeId &edge_type) { + storage::v3::ShardResult InsertEdge(VertexAccessor *from, VertexAccessor *to, + const storage::v3::EdgeTypeId &edge_type) { static constexpr auto kDummyGid = storage::v3::Gid::FromUint(0); auto maybe_edge = accessor_->CreateEdge(from->Id(storage::v3::View::NEW).GetValue(), to->Id(storage::v3::View::NEW).GetValue(), edge_type, kDummyGid); @@ -87,7 +87,7 @@ class DbAccessor final { return EdgeAccessor(*maybe_edge); } - storage::v3::Result> RemoveEdge(EdgeAccessor *edge) { + storage::v3::ShardResult> RemoveEdge(EdgeAccessor *edge) { auto res = accessor_->DeleteEdge(edge->FromVertex(), edge->ToVertex(), edge->Gid()); if (res.HasError()) { return res.GetError(); @@ -101,7 +101,7 @@ class DbAccessor final { return std::make_optional(*value); } - storage::v3::Result>>> DetachRemoveVertex( + storage::v3::ShardResult>>> DetachRemoveVertex( VertexAccessor *vertex_accessor) { using ReturnType = std::pair>; @@ -125,7 +125,7 @@ class DbAccessor final { return std::make_optional(vertex, std::move(deleted_edges)); } - storage::v3::Result> RemoveVertex(VertexAccessor *vertex_accessor) { + storage::v3::ShardResult> RemoveVertex(VertexAccessor *vertex_accessor) { auto res = accessor_->DeleteVertex(vertex_accessor); if (res.HasError()) { return res.GetError(); diff --git a/src/storage/v3/edge_accessor.cpp b/src/storage/v3/edge_accessor.cpp index ed4abd1fe..95dd6875a 100644 --- a/src/storage/v3/edge_accessor.cpp +++ b/src/storage/v3/edge_accessor.cpp @@ -15,6 +15,7 @@ #include "storage/v3/mvcc.hpp" #include "storage/v3/property_value.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/schema_validator.hpp" #include "storage/v3/vertex_accessor.hpp" #include "utils/memory_tracker.hpp" @@ -54,13 +55,13 @@ const VertexId &EdgeAccessor::FromVertex() const { return from_vertex_; } const VertexId &EdgeAccessor::ToVertex() const { return to_vertex_; } -Result EdgeAccessor::SetProperty(PropertyId property, const PropertyValue &value) { +ShardResult EdgeAccessor::SetProperty(PropertyId property, const PropertyValue &value) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!config_.properties_on_edges) return Error::PROPERTIES_DISABLED; + if (!config_.properties_on_edges) return SHARD_ERROR(ErrorCode::PROPERTIES_DISABLED); - if (!PrepareForWrite(transaction_, edge_.ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (edge_.ptr->deleted) return Error::DELETED_OBJECT; + if (edge_.ptr->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto current_value = edge_.ptr->properties.GetProperty(property); // We could skip setting the value if the previous one is the same to the new @@ -75,12 +76,12 @@ Result EdgeAccessor::SetProperty(PropertyId property, const Prope return std::move(current_value); } -Result> EdgeAccessor::ClearProperties() { - if (!config_.properties_on_edges) return Error::PROPERTIES_DISABLED; +ShardResult> EdgeAccessor::ClearProperties() { + if (!config_.properties_on_edges) return SHARD_ERROR(ErrorCode::PROPERTIES_DISABLED); - if (!PrepareForWrite(transaction_, edge_.ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (edge_.ptr->deleted) return Error::DELETED_OBJECT; + if (edge_.ptr->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto properties = edge_.ptr->properties.Properties(); for (const auto &property : properties) { @@ -92,11 +93,11 @@ Result> EdgeAccessor::ClearProperties() { return std::move(properties); } -Result EdgeAccessor::GetProperty(View view, PropertyId property) const { +ShardResult EdgeAccessor::GetProperty(View view, PropertyId property) const { return GetProperty(property, view); } -Result EdgeAccessor::GetProperty(PropertyId property, View view) const { +ShardResult EdgeAccessor::GetProperty(PropertyId property, View view) const { if (!config_.properties_on_edges) return PropertyValue(); auto exists = true; auto deleted = edge_.ptr->deleted; @@ -128,12 +129,12 @@ Result EdgeAccessor::GetProperty(PropertyId property, View view) break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(value); } -Result> EdgeAccessor::Properties(View view) const { +ShardResult> EdgeAccessor::Properties(View view) const { if (!config_.properties_on_edges) return std::map{}; auto exists = true; auto deleted = edge_.ptr->deleted; @@ -174,8 +175,8 @@ Result> EdgeAccessor::Properties(View view) break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(properties); } diff --git a/src/storage/v3/edge_accessor.hpp b/src/storage/v3/edge_accessor.hpp index 546a09200..105d28fa4 100644 --- a/src/storage/v3/edge_accessor.hpp +++ b/src/storage/v3/edge_accessor.hpp @@ -56,19 +56,19 @@ class EdgeAccessor final { /// Set a property value and return the old value. /// @throw std::bad_alloc - Result SetProperty(PropertyId property, const PropertyValue &value); + ShardResult SetProperty(PropertyId property, const PropertyValue &value); /// Remove all properties and return old values for each removed property. /// @throw std::bad_alloc - Result> ClearProperties(); + ShardResult> ClearProperties(); /// @throw std::bad_alloc - Result GetProperty(PropertyId property, View view) const; + ShardResult GetProperty(PropertyId property, View view) const; - Result GetProperty(View view, PropertyId property) const; + ShardResult GetProperty(View view, PropertyId property) const; /// @throw std::bad_alloc - Result> Properties(View view) const; + ShardResult> Properties(View view) const; Gid Gid() const noexcept { if (config_.properties_on_edges) { diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index ecec4ad1e..b13677f5d 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -40,6 +40,8 @@ struct ShardError { ShardError(ErrorCode code, std::string message, std::string source) : code{code}, message{std::move(message)}, source{std::move(source)} {} + ShardError(ErrorCode code, std::string source) : code{code}, source{std::move(source)} {} + ErrorCode code; // TODO Maybe add category std::string message; @@ -49,6 +51,6 @@ struct ShardError { #define SHARD_ERROR(...) memgraph::storage::v3::ShardError(__VA_ARGS__, fmt::format("{}:{}", __FILE__, __LINE__)) template -using Result = utils::BasicResult; +using ShardResult = utils::BasicResult; } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/schema_validator.cpp b/src/storage/v3/schema_validator.cpp index 48fd95eba..64df35a7e 100644 --- a/src/storage/v3/schema_validator.cpp +++ b/src/storage/v3/schema_validator.cpp @@ -94,7 +94,7 @@ std::optional SchemaValidator::ValidateLabelUpdate(const LabelId lab return std::nullopt; } -const Schemas::Schema *SchemaValidator::GetSchema(LabelId label) const { return schemas_.GetSchema(label); } +const Schemas::Schema *SchemaValidator::GetSchema(LabelId label) const { return schemas_->GetSchema(label); } VertexValidator::VertexValidator(const SchemaValidator &schema_validator, const LabelId primary_label) : schema_validator{&schema_validator}, primary_label_{primary_label} {} diff --git a/src/storage/v3/shard.cpp b/src/storage/v3/shard.cpp index e4d993ffa..0fafe4bf6 100644 --- a/src/storage/v3/shard.cpp +++ b/src/storage/v3/shard.cpp @@ -33,8 +33,8 @@ #include "storage/v3/mvcc.hpp" #include "storage/v3/name_id_mapper.hpp" #include "storage/v3/property_value.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/schema_validator.hpp" -#include "storage/v3/shard_operation_result.hpp" #include "storage/v3/transaction.hpp" #include "storage/v3/vertex.hpp" #include "storage/v3/vertex_accessor.hpp" @@ -345,7 +345,7 @@ Shard::~Shard() {} Shard::Accessor::Accessor(Shard &shard, Transaction &transaction) : shard_(&shard), transaction_(&transaction), config_(shard_->config_.items) {} -ShardOperationResult Shard::Accessor::CreateVertexAndValidate( +ShardResult Shard::Accessor::CreateVertexAndValidate( const std::vector &labels, const std::vector &primary_properties, const std::vector> &properties) { OOMExceptionEnabler oom_exception; @@ -364,7 +364,7 @@ ShardOperationResult Shard::Accessor::CreateVertexAndValidate( VertexAccessor vertex_acc{&it->vertex, transaction_, &shard_->indices_, config_, shard_->vertex_validator_}; if (!inserted) { - return {Error::VERTEX_ALREADY_INSERTED}; + return SHARD_ERROR(ErrorCode::VERTEX_ALREADY_INSERTED); } MG_ASSERT(it != acc.end(), "Invalid Vertex accessor!"); @@ -395,19 +395,19 @@ std::optional Shard::Accessor::FindVertex(std::vectorvertex, transaction_, &shard_->indices_, config_, shard_->vertex_validator_, view); } -Result> Shard::Accessor::DeleteVertex(VertexAccessor *vertex) { +ShardResult> Shard::Accessor::DeleteVertex(VertexAccessor *vertex) { MG_ASSERT(vertex->transaction_ == transaction_, "VertexAccessor must be from the same transaction as the storage " "accessor when deleting a vertex!"); auto *vertex_ptr = vertex->vertex_; - if (!PrepareForWrite(transaction_, vertex_ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); if (vertex_ptr->deleted) { return std::optional{}; } - if (!vertex_ptr->in_edges.empty() || !vertex_ptr->out_edges.empty()) return Error::VERTEX_HAS_EDGES; + if (!vertex_ptr->in_edges.empty() || !vertex_ptr->out_edges.empty()) return SHARD_ERROR(ErrorCode::VERTEX_HAS_EDGES); CreateAndLinkDelta(transaction_, vertex_ptr, Delta::RecreateObjectTag()); vertex_ptr->deleted = true; @@ -416,7 +416,7 @@ Result> Shard::Accessor::DeleteVertex(VertexAccess shard_->vertex_validator_, true); } -Result>>> Shard::Accessor::DetachDeleteVertex( +ShardResult>>> Shard::Accessor::DetachDeleteVertex( VertexAccessor *vertex) { using ReturnType = std::pair>; @@ -429,7 +429,7 @@ Result>>> Shar std::vector out_edges; { - if (!PrepareForWrite(transaction_, vertex_ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); if (vertex_ptr->deleted) return std::optional{}; @@ -444,7 +444,7 @@ Result>>> Shar EdgeAccessor e(edge, edge_type, from_vertex, vertex_id, transaction_, &shard_->indices_, config_); auto ret = DeleteEdge(e.FromVertex(), e.ToVertex(), e.Gid()); if (ret.HasError()) { - MG_ASSERT(ret.GetError() == Error::SERIALIZATION_ERROR, "Invalid database state!"); + MG_ASSERT(ret.GetError().code == ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); return ret.GetError(); } @@ -457,7 +457,7 @@ Result>>> Shar EdgeAccessor e(edge, edge_type, vertex_id, to_vertex, transaction_, &shard_->indices_, config_); auto ret = DeleteEdge(e.FromVertex(), e.ToVertex(), e.Gid()); if (ret.HasError()) { - MG_ASSERT(ret.GetError() == Error::SERIALIZATION_ERROR, "Invalid database state!"); + MG_ASSERT(ret.GetError().code == ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); return ret.GetError(); } @@ -470,7 +470,7 @@ Result>>> Shar // vertex. Some other transaction could have modified the vertex in the // meantime if we didn't have any edges to delete. - if (!PrepareForWrite(transaction_, vertex_ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); MG_ASSERT(!vertex_ptr->deleted, "Invalid database state!"); @@ -482,8 +482,8 @@ Result>>> Shar std::move(deleted_edges)); } -Result Shard::Accessor::CreateEdge(VertexId from_vertex_id, VertexId to_vertex_id, - const EdgeTypeId edge_type, const Gid gid) { +ShardResult Shard::Accessor::CreateEdge(VertexId from_vertex_id, VertexId to_vertex_id, + const EdgeTypeId edge_type, const Gid gid) { OOMExceptionEnabler oom_exception; Vertex *from_vertex{nullptr}; Vertex *to_vertex{nullptr}; @@ -507,12 +507,12 @@ Result Shard::Accessor::CreateEdge(VertexId from_vertex_id, Vertex } if (from_is_local) { - if (!PrepareForWrite(transaction_, from_vertex)) return Error::SERIALIZATION_ERROR; - if (from_vertex->deleted) return Error::DELETED_OBJECT; + if (!PrepareForWrite(transaction_, from_vertex)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (from_vertex->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } if (to_is_local && to_vertex != from_vertex) { - if (!PrepareForWrite(transaction_, to_vertex)) return Error::SERIALIZATION_ERROR; - if (to_vertex->deleted) return Error::DELETED_OBJECT; + if (!PrepareForWrite(transaction_, to_vertex)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (to_vertex->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } EdgeRef edge(gid); @@ -541,8 +541,8 @@ Result Shard::Accessor::CreateEdge(VertexId from_vertex_id, Vertex &shard_->indices_, config_); } -Result> Shard::Accessor::DeleteEdge(VertexId from_vertex_id, VertexId to_vertex_id, - const Gid edge_id) { +ShardResult> Shard::Accessor::DeleteEdge(VertexId from_vertex_id, VertexId to_vertex_id, + const Gid edge_id) { Vertex *from_vertex{nullptr}; Vertex *to_vertex{nullptr}; @@ -567,13 +567,13 @@ Result> Shard::Accessor::DeleteEdge(VertexId from_ve if (from_is_local) { if (!PrepareForWrite(transaction_, from_vertex)) { - return Error::SERIALIZATION_ERROR; + return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); } MG_ASSERT(!from_vertex->deleted, "Invalid database state!"); } if (to_is_local && to_vertex != from_vertex) { if (!PrepareForWrite(transaction_, to_vertex)) { - return Error::SERIALIZATION_ERROR; + return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); } MG_ASSERT(!to_vertex->deleted, "Invalid database state!"); } diff --git a/src/storage/v3/shard.hpp b/src/storage/v3/shard.hpp index 778c92abd..5c025ae57 100644 --- a/src/storage/v3/shard.hpp +++ b/src/storage/v3/shard.hpp @@ -38,7 +38,6 @@ #include "storage/v3/result.hpp" #include "storage/v3/schema_validator.hpp" #include "storage/v3/schemas.hpp" -#include "storage/v3/shard_operation_result.hpp" #include "storage/v3/transaction.hpp" #include "storage/v3/vertex.hpp" #include "storage/v3/vertex_accessor.hpp" @@ -207,7 +206,7 @@ class Shard final { public: /// @throw std::bad_alloc - ShardOperationResult CreateVertexAndValidate( + ShardResult CreateVertexAndValidate( const std::vector &labels, const std::vector &primary_properties, const std::vector> &properties); @@ -262,19 +261,19 @@ class Shard final { /// @return Accessor to the deleted vertex if a deletion took place, std::nullopt otherwise /// @throw std::bad_alloc - Result> DeleteVertex(VertexAccessor *vertex); + ShardResult> DeleteVertex(VertexAccessor *vertex); /// @return Accessor to the deleted vertex and deleted edges if a deletion took place, std::nullopt otherwise /// @throw std::bad_alloc - Result>>> DetachDeleteVertex( + ShardResult>>> DetachDeleteVertex( VertexAccessor *vertex); /// @throw std::bad_alloc - Result CreateEdge(VertexId from_vertex_id, VertexId to_vertex_id, EdgeTypeId edge_type, Gid gid); + ShardResult CreateEdge(VertexId from_vertex_id, VertexId to_vertex_id, EdgeTypeId edge_type, Gid gid); /// Accessor to the deleted edge if a deletion took place, std::nullopt otherwise /// @throw std::bad_alloc - Result> DeleteEdge(VertexId from_vertex_id, VertexId to_vertex_id, Gid edge_id); + ShardResult> DeleteEdge(VertexId from_vertex_id, VertexId to_vertex_id, Gid edge_id); LabelId NameToLabel(std::string_view name) const; diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index f288e5e27..f3d879ef4 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -34,6 +34,7 @@ #include "storage/v3/key_store.hpp" #include "storage/v3/property_value.hpp" #include "storage/v3/request_helper.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/schemas.hpp" #include "storage/v3/shard.hpp" #include "storage/v3/shard_rsm.hpp" @@ -468,6 +469,37 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { return edge_filler; } +void LogResultError(const ShardError &error, const std::string_view action = "") { + switch (error.code) { + case ErrorCode::DELETED_OBJECT: + spdlog::debug("{} failed with error: DELETED_OBJECT, at {}", action, error.source); + break; + case ErrorCode::NONEXISTENT_OBJECT: + spdlog::debug("{} failed with error: NONEXISTENT_OBJECT, at {}", action, error.source); + break; + case ErrorCode::SERIALIZATION_ERROR: + spdlog::debug("{} failed with error: SERIALIZATION_ERROR, at {}", action, error.source); + break; + case ErrorCode::PROPERTIES_DISABLED: + spdlog::debug("{} failed with error: PROPERTIES_DISABLED, at {}", action, error.source); + break; + case ErrorCode::VERTEX_HAS_EDGES: + spdlog::debug("{} failed with error: VERTEX_HAS_EDGES, at {}", action, error.source); + break; + case ErrorCode::VERTEX_ALREADY_INSERTED: + spdlog::debug("{} failed with error: VERTEX_ALREADY_INSERTED, at {}", action, error.source); + break; + case ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: + case ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: + case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: + case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: + case ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: + case ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: + spdlog::debug("Schema violation: {} at {}", error.message, error.source); + break; + } +} + }; // namespace msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); @@ -494,38 +526,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { if (result_schema.HasError()) { auto &error = result_schema.GetError(); - - std::visit( - [](T &&error) { - using ErrorType = std::remove_cvref_t; - if constexpr (std::is_same_v) { - spdlog::debug("Creating vertex failed with error: SchemaViolation"); - } else if constexpr (std::is_same_v) { - switch (error) { - case Error::DELETED_OBJECT: - spdlog::debug("Creating vertex failed with error: DELETED_OBJECT"); - break; - case Error::NONEXISTENT_OBJECT: - spdlog::debug("Creating vertex failed with error: NONEXISTENT_OBJECT"); - break; - case Error::SERIALIZATION_ERROR: - spdlog::debug("Creating vertex failed with error: SERIALIZATION_ERROR"); - break; - case Error::PROPERTIES_DISABLED: - spdlog::debug("Creating vertex failed with error: PROPERTIES_DISABLED"); - break; - case Error::VERTEX_HAS_EDGES: - spdlog::debug("Creating vertex failed with error: VERTEX_HAS_EDGES"); - break; - case Error::VERTEX_ALREADY_INSERTED: - spdlog::debug("Creating vertex failed with error: VERTEX_ALREADY_INSERTED"); - break; - } - } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); - } - }, - error); + spdlog::debug("Creating vertex failed with error: VERTEX_ALREADY_INSERTED"); action_successful = false; break; @@ -558,38 +559,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { vertex_to_update->SetPropertyAndValidate(update_prop.first, ToPropertyValue(std::move(update_prop.second))); if (result_schema.HasError()) { auto &error = result_schema.GetError(); - - std::visit( - [](T &&error) { - using ErrorType = std::remove_cvref_t; - if constexpr (std::is_same_v) { - spdlog::debug("Updating vertex failed with error: SchemaViolation"); - } else if constexpr (std::is_same_v) { - switch (error) { - case Error::DELETED_OBJECT: - spdlog::debug("Updating vertex failed with error: DELETED_OBJECT"); - break; - case Error::NONEXISTENT_OBJECT: - spdlog::debug("Updating vertex failed with error: NONEXISTENT_OBJECT"); - break; - case Error::SERIALIZATION_ERROR: - spdlog::debug("Updating vertex failed with error: SERIALIZATION_ERROR"); - break; - case Error::PROPERTIES_DISABLED: - spdlog::debug("Updating vertex failed with error: PROPERTIES_DISABLED"); - break; - case Error::VERTEX_HAS_EDGES: - spdlog::debug("Updating vertex failed with error: VERTEX_HAS_EDGES"); - break; - case Error::VERTEX_ALREADY_INSERTED: - spdlog::debug("Updating vertex failed with error: VERTEX_ALREADY_INSERTED"); - break; - } - } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); - } - }, - error); + LogResultError(error); action_successful = false; diff --git a/src/storage/v3/vertex_accessor.cpp b/src/storage/v3/vertex_accessor.cpp index 543caa88a..6949c08cc 100644 --- a/src/storage/v3/vertex_accessor.cpp +++ b/src/storage/v3/vertex_accessor.cpp @@ -21,8 +21,8 @@ #include "storage/v3/key_store.hpp" #include "storage/v3/mvcc.hpp" #include "storage/v3/property_value.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/shard.hpp" -#include "storage/v3/shard_operation_result.hpp" #include "storage/v3/vertex.hpp" #include "utils/logging.hpp" #include "utils/memory_tracker.hpp" @@ -80,12 +80,12 @@ bool VertexAccessor::IsVisible(View view) const { return exists && (for_deleted_ || !deleted); } -Result VertexAccessor::AddLabel(LabelId label) { +ShardResult VertexAccessor::AddLabel(LabelId label) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return Error::DELETED_OBJECT; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); if (std::find(vertex_->labels.begin(), vertex_->labels.end(), label) != vertex_->labels.end()) return false; @@ -98,15 +98,15 @@ Result VertexAccessor::AddLabel(LabelId label) { return true; } -ShardOperationResult VertexAccessor::AddLabelAndValidate(LabelId label) { +ShardResult VertexAccessor::AddLabelAndValidate(LabelId label) { if (const auto maybe_violation_error = vertex_validator_->ValidateAddLabel(label); maybe_violation_error) { return {*maybe_violation_error}; } utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return {Error::SERIALIZATION_ERROR}; + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return {Error::DELETED_OBJECT}; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); if (std::find(vertex_->labels.begin(), vertex_->labels.end(), label) != vertex_->labels.end()) return false; @@ -119,10 +119,10 @@ ShardOperationResult VertexAccessor::AddLabelAndValidate(LabelId label) { return true; } -Result VertexAccessor::RemoveLabel(LabelId label) { - if (!PrepareForWrite(transaction_, vertex_)) return Error::SERIALIZATION_ERROR; +ShardResult VertexAccessor::RemoveLabel(LabelId label) { + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return Error::DELETED_OBJECT; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto it = std::find(vertex_->labels.begin(), vertex_->labels.end(), label); if (it == vertex_->labels.end()) return false; @@ -134,14 +134,14 @@ Result VertexAccessor::RemoveLabel(LabelId label) { return true; } -ShardOperationResult VertexAccessor::RemoveLabelAndValidate(LabelId label) { +ShardResult VertexAccessor::RemoveLabelAndValidate(LabelId label) { if (const auto maybe_violation_error = vertex_validator_->ValidateRemoveLabel(label); maybe_violation_error) { return {*maybe_violation_error}; } - if (!PrepareForWrite(transaction_, vertex_)) return {Error::SERIALIZATION_ERROR}; + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return {Error::DELETED_OBJECT}; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto it = std::find(vertex_->labels.begin(), vertex_->labels.end(), label); if (it == vertex_->labels.end()) return false; @@ -153,9 +153,9 @@ ShardOperationResult VertexAccessor::RemoveLabelAndValidate(LabelId label) return true; } -Result VertexAccessor::HasLabel(View view, LabelId label) const { return HasLabel(label, view); } +ShardResult VertexAccessor::HasLabel(View view, LabelId label) const { return HasLabel(label, view); } -Result VertexAccessor::HasLabel(LabelId label, View view) const { +ShardResult VertexAccessor::HasLabel(LabelId label, View view) const { bool exists = true; bool deleted = false; bool has_label = false; @@ -197,12 +197,12 @@ Result VertexAccessor::HasLabel(LabelId label, View view) const { break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return has_label; } -Result VertexAccessor::PrimaryLabel(const View view) const { +ShardResult VertexAccessor::PrimaryLabel(const View view) const { if (const auto result = CheckVertexExistence(view); result.HasError()) { return result.GetError(); } @@ -210,21 +210,21 @@ Result VertexAccessor::PrimaryLabel(const View view) const { return vertex_validator_->primary_label_; } -Result VertexAccessor::PrimaryKey(const View view) const { +ShardResult VertexAccessor::PrimaryKey(const View view) const { if (const auto result = CheckVertexExistence(view); result.HasError()) { return result.GetError(); } return vertex_->keys.Keys(); } -Result VertexAccessor::Id(View view) const { +ShardResult VertexAccessor::Id(View view) const { if (const auto result = CheckVertexExistence(view); result.HasError()) { return result.GetError(); } return VertexId{vertex_validator_->primary_label_, vertex_->keys.Keys()}; }; -Result> VertexAccessor::Labels(View view) const { +ShardResult> VertexAccessor::Labels(View view) const { bool exists = true; bool deleted = false; std::vector labels; @@ -267,17 +267,17 @@ Result> VertexAccessor::Labels(View view) const { break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(labels); } -Result VertexAccessor::SetProperty(PropertyId property, const PropertyValue &value) { +ShardResult VertexAccessor::SetProperty(PropertyId property, const PropertyValue &value) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return Error::DELETED_OBJECT; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto current_value = vertex_->properties.GetProperty(property); // We could skip setting the value if the previous one is the same to the new @@ -294,7 +294,7 @@ Result VertexAccessor::SetProperty(PropertyId property, const Pro return std::move(current_value); } -Result VertexAccessor::CheckVertexExistence(View view) const { +ShardResult VertexAccessor::CheckVertexExistence(View view) const { bool exists = true; bool deleted = false; Delta *delta = nullptr; @@ -323,27 +323,26 @@ Result VertexAccessor::CheckVertexExistence(View view) const { } }); if (!exists) { - return Error::NONEXISTENT_OBJECT; + return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); } if (!for_deleted_ && deleted) { - return Error::DELETED_OBJECT; + return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } return {}; } -ShardOperationResult VertexAccessor::SetPropertyAndValidate(PropertyId property, - const PropertyValue &value) { +ShardResult VertexAccessor::SetPropertyAndValidate(PropertyId property, const PropertyValue &value) { if (auto maybe_violation_error = vertex_validator_->ValidatePropertyUpdate(property); maybe_violation_error) { return {*maybe_violation_error}; } utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; if (!PrepareForWrite(transaction_, vertex_)) { - return {Error::SERIALIZATION_ERROR}; + return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); } if (vertex_->deleted) { - return {Error::DELETED_OBJECT}; + return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } auto current_value = vertex_->properties.GetProperty(property); @@ -361,10 +360,10 @@ ShardOperationResult VertexAccessor::SetPropertyAndValidate(Prope return std::move(current_value); } -Result> VertexAccessor::ClearProperties() { - if (!PrepareForWrite(transaction_, vertex_)) return Error::SERIALIZATION_ERROR; +ShardResult> VertexAccessor::ClearProperties() { + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return Error::DELETED_OBJECT; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto properties = vertex_->properties.Properties(); for (const auto &property : properties) { @@ -377,7 +376,7 @@ Result> VertexAccessor::ClearProperties() { return std::move(properties); } -Result VertexAccessor::GetProperty(View view, PropertyId property) const { +ShardResult VertexAccessor::GetProperty(View view, PropertyId property) const { return GetProperty(property, view).GetValue(); } @@ -407,7 +406,7 @@ PropertyValue VertexAccessor::GetPropertyValue(PropertyId property, View view) c return value; } -Result VertexAccessor::GetProperty(PropertyId property, View view) const { +ShardResult VertexAccessor::GetProperty(PropertyId property, View view) const { bool exists = true; bool deleted = false; PropertyValue value; @@ -442,12 +441,12 @@ Result VertexAccessor::GetProperty(PropertyId property, View view break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(value); } -Result> VertexAccessor::Properties(View view) const { +ShardResult> VertexAccessor::Properties(View view) const { bool exists = true; bool deleted = false; std::map properties; @@ -492,13 +491,13 @@ Result> VertexAccessor::Properties(View view break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(properties); } -Result> VertexAccessor::InEdges(View view, const std::vector &edge_types, - const VertexId *destination_id) const { +ShardResult> VertexAccessor::InEdges(View view, const std::vector &edge_types, + const VertexId *destination_id) const { bool exists = true; bool deleted = false; std::vector in_edges; @@ -564,8 +563,8 @@ Result> VertexAccessor::InEdges(View view, const std:: break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); std::vector ret; if (in_edges.empty()) { return ret; @@ -579,8 +578,8 @@ Result> VertexAccessor::InEdges(View view, const std:: return ret; } -Result> VertexAccessor::OutEdges(View view, const std::vector &edge_types, - const VertexId *destination_id) const { +ShardResult> VertexAccessor::OutEdges(View view, const std::vector &edge_types, + const VertexId *destination_id) const { bool exists = true; bool deleted = false; std::vector out_edges; @@ -644,8 +643,8 @@ Result> VertexAccessor::OutEdges(View view, const std: break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); std::vector ret; if (out_edges.empty()) { return ret; @@ -659,7 +658,7 @@ Result> VertexAccessor::OutEdges(View view, const std: return ret; } -Result VertexAccessor::InDegree(View view) const { +ShardResult VertexAccessor::InDegree(View view) const { bool exists = true; bool deleted = false; size_t degree = 0; @@ -691,12 +690,12 @@ Result VertexAccessor::InDegree(View view) const { break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return degree; } -Result VertexAccessor::OutDegree(View view) const { +ShardResult VertexAccessor::OutDegree(View view) const { bool exists = true; bool deleted = false; size_t degree = 0; @@ -728,8 +727,8 @@ Result VertexAccessor::OutDegree(View view) const { break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return degree; } diff --git a/src/storage/v3/vertex_accessor.hpp b/src/storage/v3/vertex_accessor.hpp index 682a04e39..20ee9955b 100644 --- a/src/storage/v3/vertex_accessor.hpp +++ b/src/storage/v3/vertex_accessor.hpp @@ -17,7 +17,7 @@ #include "storage/v3/id_types.hpp" #include "storage/v3/key_store.hpp" #include "storage/v3/result.hpp" -#include "storage/v3/shard_operation_result.hpp" +#include "storage/v3/schema_validator.hpp" #include "storage/v3/transaction.hpp" #include "storage/v3/vertex.hpp" #include "storage/v3/vertex_id.hpp" @@ -55,61 +55,61 @@ class VertexAccessor final { /// `false` is returned if the label already existed, or SchemaViolation /// if adding the label has violated one of the schema constraints. /// @throw std::bad_alloc - ShardOperationResult AddLabelAndValidate(LabelId label); + ShardResult AddLabelAndValidate(LabelId label); /// Remove a label and return `true` if deletion took place. /// `false` is returned if the vertex did not have a label already. or SchemaViolation /// if adding the label has violated one of the schema constraints. /// @throw std::bad_alloc - ShardOperationResult RemoveLabelAndValidate(LabelId label); + ShardResult RemoveLabelAndValidate(LabelId label); - Result HasLabel(View view, LabelId label) const; + ShardResult HasLabel(View view, LabelId label) const; - Result HasLabel(LabelId label, View view) const; + ShardResult HasLabel(LabelId label, View view) const; /// @throw std::bad_alloc /// @throw std::length_error if the resulting vector exceeds /// std::vector::max_size(). - Result> Labels(View view) const; + ShardResult> Labels(View view) const; - Result PrimaryLabel(View view) const; + ShardResult PrimaryLabel(View view) const; - Result PrimaryKey(View view) const; + ShardResult PrimaryKey(View view) const; - Result Id(View view) const; + ShardResult Id(View view) const; /// Set a property value and return the old value or error. /// @throw std::bad_alloc - ShardOperationResult SetPropertyAndValidate(PropertyId property, const PropertyValue &value); + ShardResult SetPropertyAndValidate(PropertyId property, const PropertyValue &value); /// Remove all properties and return the values of the removed properties. /// @throw std::bad_alloc - Result> ClearProperties(); + ShardResult> ClearProperties(); /// @throw std::bad_alloc - Result GetProperty(PropertyId property, View view) const; + ShardResult GetProperty(PropertyId property, View view) const; // TODO Remove this - Result GetProperty(View view, PropertyId property) const; + ShardResult GetProperty(View view, PropertyId property) const; /// @throw std::bad_alloc - Result> Properties(View view) const; + ShardResult> Properties(View view) const; /// @throw std::bad_alloc /// @throw std::length_error if the resulting vector exceeds /// std::vector::max_size(). - Result> InEdges(View view, const std::vector &edge_types = {}, - const VertexId *destination_id = nullptr) const; + ShardResult> InEdges(View view, const std::vector &edge_types = {}, + const VertexId *destination_id = nullptr) const; /// @throw std::bad_alloc /// @throw std::length_error if the resulting vector exceeds /// std::vector::max_size(). - Result> OutEdges(View view, const std::vector &edge_types = {}, - const VertexId *destination_id = nullptr) const; + ShardResult> OutEdges(View view, const std::vector &edge_types = {}, + const VertexId *destination_id = nullptr) const; - Result InDegree(View view) const; + ShardResult InDegree(View view) const; - Result OutDegree(View view) const; + ShardResult OutDegree(View view) const; const SchemaValidator *GetSchemaValidator() const; @@ -122,20 +122,20 @@ class VertexAccessor final { /// Add a label and return `true` if insertion took place. /// `false` is returned if the label already existed. /// @throw std::bad_alloc - Result AddLabel(LabelId label); + ShardResult AddLabel(LabelId label); /// Remove a label and return `true` if deletion took place. /// `false` is returned if the vertex did not have a label already. /// @throw std::bad_alloc - Result RemoveLabel(LabelId label); + ShardResult RemoveLabel(LabelId label); /// Set a property value and return the old value. /// @throw std::bad_alloc - Result SetProperty(PropertyId property, const PropertyValue &value); + ShardResult SetProperty(PropertyId property, const PropertyValue &value); PropertyValue GetPropertyValue(PropertyId property, View view) const; - Result CheckVertexExistence(View view) const; + ShardResult CheckVertexExistence(View view) const; Vertex *vertex_; Transaction *transaction_;