diff --git a/src/storage/v2/schemas.cpp b/src/storage/v2/schemas.cpp index 2fd6746a1..4eed35c21 100644 --- a/src/storage/v2/schemas.cpp +++ b/src/storage/v2/schemas.cpp @@ -14,12 +14,14 @@ #include #include +#include "storage/v2/property_value.hpp" #include "storage/v2/schemas.hpp" namespace memgraph::storage { -Schemas::CreationStatus Schemas::AddSchema(const LabelId label, const std::vector &property_ids) { - auto res = schemas_.insert({std::move(label), property_ids}).second; +Schemas::CreationStatus Schemas::CreateSchema(const LabelId label, + const std::pair &property_ids) { + auto res = schemas_.insert({label, property_ids}).second; return res ? Schemas::CreationStatus::SUCCESS : Schemas::CreationStatus::FAIL; } @@ -33,7 +35,22 @@ Schemas::ValidationStatus ValidateVertex(const LabelId primary_label, const Vert if (!schemas_.contains(primary_label)) { return Schemas::ValidationStatus::NO_SCHEMA_DEFINED_FOR_LABEL; } - auto &[schema_label, schema_properties] = schemas_[primary_label]; + if (!utils::Contains(vertex.labels, primary_label)) { + return Schemas::ValidationStatus::VERTEX_HAS_NO_PRIMARY_LABEL; + } + + auto &[property_id, property_type] = schemas_[primary_label]; + // Property existence check + if (!vertex.properties.HasProperty(property_id)) { + return Schemas::ValidationStatus::VERTEX_HAS_NO_PROPERTY; + } + // Property type check + if (vertex.properties.GetProperty(property_id).type_() != property_type) { + return Schemas::ValidationStatus::VERTEX_PROPERTY_WRONG_TYPE; + } + + // TODO after the introduction of vertex hashing introduce check for vertex + // primary key uniqueness return Schemas::ValidationStatus::SUCCESS; } diff --git a/src/storage/v2/schemas.hpp b/src/storage/v2/schemas.hpp index 70540669e..72cdc5dba 100644 --- a/src/storage/v2/schemas.hpp +++ b/src/storage/v2/schemas.hpp @@ -12,8 +12,10 @@ #pragma once #include +#include #include +#include "storage/v2/indices.hpp" #include "storage/v2/property_value.hpp" #include "storage/v2/transaction.hpp" #include "storage/v2/vertex.hpp" @@ -38,17 +40,22 @@ class Schemas { enum class ValidationStatus : uint8_t { SUCCESS, VERTEX_DELETED, + VERTEX_HAS_NO_PRIMARY_LABEL, + VERTEX_HAS_NO_PROPERTY, NO_SCHEMA_DEFINED_FOR_LABEL, - VERTEX_HAS_NO_PRIMARY_LABEL + VERTEX_PROPERTY_WRONG_TYPE }; - CreationStatus AddSchema(LabelId label, const std::vector &property_ids); + CreationStatus CreateSchema(LabelId label, const std::vector &property_ids); + DeletionStatus DeleteSchema(LabelId label); + ValidationStatus ValidateVertex(LabelId primary_label, const Vertex &vertex, const Transaction &tx, uint64_t commit_timestamp); private: - std::unordered_map> schemas_; + std::unordered_map> schemas_; + std::unordered_map label_property_indices_; }; } // namespace memgraph::storage