Integrate schema and keystore (#497)

- Integrate schema and keystore on vertex creation
- Add GC test for storage v3
- Add tests for accessors
- Fix all tests related to this except for query v2
- Fix labels not returning primary label
This commit is contained in:
Jure Bajic 2022-08-29 14:38:25 +02:00 committed by GitHub
parent 68b26275a3
commit 95dbc022c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 3305 additions and 2911 deletions

View File

@ -133,7 +133,8 @@ storage::v3::Result<Value> ToBoltValue(const query::v2::TypedValue &value, const
storage::v3::Result<communication::bolt::Vertex> ToBoltVertex(const storage::v3::VertexAccessor &vertex,
const storage::v3::Storage &db, storage::v3::View view) {
auto id = communication::bolt::Id::FromUint(vertex.Gid().AsUint());
// TODO(jbajic) Fix bolt communication
auto id = communication::bolt::Id::FromUint(0);
auto maybe_labels = vertex.Labels(view);
if (maybe_labels.HasError()) return maybe_labels.GetError();
std::vector<std::string> labels;
@ -152,9 +153,10 @@ storage::v3::Result<communication::bolt::Vertex> ToBoltVertex(const storage::v3:
storage::v3::Result<communication::bolt::Edge> ToBoltEdge(const storage::v3::EdgeAccessor &edge,
const storage::v3::Storage &db, storage::v3::View view) {
auto id = communication::bolt::Id::FromUint(edge.Gid().AsUint());
auto from = communication::bolt::Id::FromUint(edge.FromVertex().Gid().AsUint());
auto to = communication::bolt::Id::FromUint(edge.ToVertex().Gid().AsUint());
// TODO(jbajic) Fix bolt communication
auto id = communication::bolt::Id::FromUint(0);
auto from = communication::bolt::Id::FromUint(0);
auto to = communication::bolt::Id::FromUint(0);
const auto &type = db.EdgeTypeToName(edge.EdgeType());
auto maybe_properties = edge.Properties(view);
if (maybe_properties.HasError()) return maybe_properties.GetError();

View File

@ -86,6 +86,18 @@ concept AccessorWithSetProperty = requires(T accessor, const storage::v3::Proper
{ accessor.SetProperty(key, new_value) } -> std::same_as<storage::v3::Result<storage::v3::PropertyValue>>;
};
template <typename T>
concept AccessorWithSetPropertyAndValidate = requires(T accessor, const storage::v3::PropertyId key,
const storage::v3::PropertyValue new_value) {
{
accessor.SetPropertyAndValidate(key, new_value)
} -> std::same_as<storage::v3::ResultSchema<storage::v3::PropertyValue>>;
};
template <typename TRecordAccessor>
concept RecordAccessor =
AccessorWithSetProperty<TRecordAccessor> || AccessorWithSetPropertyAndValidate<TRecordAccessor>;
inline void HandleSchemaViolation(const storage::v3::SchemaViolation &schema_violation, const DbAccessor &dba) {
switch (schema_violation.status) {
case storage::v3::SchemaViolation::ValidationStatus::VERTEX_HAS_NO_PRIMARY_PROPERTY: {
@ -109,13 +121,14 @@ inline void HandleSchemaViolation(const storage::v3::SchemaViolation &schema_vio
*schema_violation.violated_property_value,
dba.LabelToName(schema_violation.label)));
}
case storage::v3::SchemaViolation::ValidationStatus::VERTEX_MODIFY_PRIMARY_LABEL: {
throw SchemaViolationException(fmt::format("Cannot add or remove label :{} since it is a primary label",
dba.LabelToName(schema_violation.label)));
case storage::v3::SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL: {
throw SchemaViolationException(fmt::format(
"Adding primary label as secondary or removing primary label:", *schema_violation.violated_property_value,
dba.LabelToName(schema_violation.label)));
}
case storage::v3::SchemaViolation::ValidationStatus::VERTEX_SECONDARY_LABEL_IS_PRIMARY: {
throw SchemaViolationException(
fmt::format("Cannot create vertex with secondary label :{}", dba.LabelToName(schema_violation.label)));
throw SchemaViolationException(fmt::format("Cannot create vertex where primary label is secondary:{}",
dba.LabelToName(schema_violation.label)));
}
}
}
@ -137,7 +150,7 @@ inline void HandleErrorOnPropertyUpdate(const storage::v3::Error error) {
/// Set a property `value` mapped with given `key` on a `record`.
///
/// @throw QueryRuntimeException if value cannot be set as a property value
template <AccessorWithSetProperty T>
template <RecordAccessor T>
storage::v3::PropertyValue PropsSetChecked(T *record, const DbAccessor &dba, const storage::v3::PropertyId &key,
const TypedValue &value) {
try {

View File

@ -11,6 +11,7 @@
#pragma once
#include <cstdint>
#include <optional>
#include <vector>
@ -19,6 +20,7 @@
#include "query/v2/exceptions.hpp"
#include "storage/v3/id_types.hpp"
#include "storage/v3/key_store.hpp"
#include "storage/v3/property_value.hpp"
#include "storage/v3/result.hpp"
@ -109,13 +111,17 @@ class VertexAccessor final {
auto PrimaryLabel(storage::v3::View view) const { return impl_.PrimaryLabel(view); }
storage::v3::Result<bool> AddLabel(storage::v3::LabelId label) { return impl_.AddLabel(label); }
auto PrimaryKey(storage::v3::View view) const { return impl_.PrimaryKey(view); }
storage::v3::ResultSchema<bool> AddLabel(storage::v3::LabelId label) { return impl_.AddLabelAndValidate(label); }
storage::v3::ResultSchema<bool> AddLabelAndValidate(storage::v3::LabelId label) {
return impl_.AddLabelAndValidate(label);
}
storage::v3::Result<bool> RemoveLabel(storage::v3::LabelId label) { return impl_.RemoveLabel(label); }
storage::v3::ResultSchema<bool> RemoveLabel(storage::v3::LabelId label) {
return impl_.RemoveLabelAndValidate(label);
}
storage::v3::ResultSchema<bool> RemoveLabelAndValidate(storage::v3::LabelId label) {
return impl_.RemoveLabelAndValidate(label);
@ -132,9 +138,9 @@ class VertexAccessor final {
return impl_.GetProperty(key, view);
}
storage::v3::Result<storage::v3::PropertyValue> SetProperty(storage::v3::PropertyId key,
const storage::v3::PropertyValue &value) {
return impl_.SetProperty(key, value);
storage::v3::ResultSchema<storage::v3::PropertyValue> SetProperty(storage::v3::PropertyId key,
const storage::v3::PropertyValue &value) {
return impl_.SetPropertyAndValidate(key, value);
}
storage::v3::ResultSchema<storage::v3::PropertyValue> SetPropertyAndValidate(
@ -188,9 +194,8 @@ class VertexAccessor final {
storage::v3::Result<size_t> OutDegree(storage::v3::View view) const { return impl_.OutDegree(view); }
int64_t CypherId() const { return impl_.Gid().AsInt(); }
storage::v3::Gid Gid() const noexcept { return impl_.Gid(); }
// TODO(jbajic) Fix Remove Gid
static int64_t CypherId() { return 1; }
bool operator==(const VertexAccessor &v) const noexcept {
static_assert(noexcept(impl_ == v.impl_));
@ -241,8 +246,12 @@ class DbAccessor final {
public:
explicit DbAccessor(storage::v3::Storage::Accessor *accessor) : accessor_(accessor) {}
std::optional<VertexAccessor> FindVertex(storage::v3::Gid gid, storage::v3::View view) {
auto maybe_vertex = accessor_->FindVertex(gid, view);
// TODO(jbajic) Fix Remove Gid
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
std::optional<VertexAccessor> FindVertex(uint64_t /*unused*/) { return std::nullopt; }
std::optional<VertexAccessor> FindVertex(storage::v3::PrimaryKey &primary_key, storage::v3::View view) {
auto maybe_vertex = accessor_->FindVertex(primary_key, view);
if (maybe_vertex) return VertexAccessor(*maybe_vertex);
return std::nullopt;
}
@ -270,9 +279,6 @@ class DbAccessor final {
return VerticesIterable(accessor_->Vertices(label, property, lower, upper, view));
}
// TODO Remove when query modules have been fixed
[[deprecated]] VertexAccessor InsertVertex() { return VertexAccessor(accessor_->CreateVertex()); }
storage::v3::ResultSchema<VertexAccessor> InsertVertexAndValidate(
const storage::v3::LabelId primary_label, const std::vector<storage::v3::LabelId> &labels,
const std::vector<std::pair<storage::v3::PropertyId, storage::v3::PropertyValue>> &properties) {

View File

@ -607,17 +607,16 @@ ACCEPT_WITH_INPUT(ScanAllById)
UniqueCursorPtr ScanAllById::MakeCursor(utils::MemoryResource *mem) const {
EventCounter::IncrementCounter(EventCounter::ScanAllByIdOperator);
// TODO Reimplement when we have reliable conversion between hash value and pk
auto vertices = [this](Frame &frame, ExecutionContext &context) -> std::optional<std::vector<VertexAccessor>> {
auto *db = context.db_accessor;
ExpressionEvaluator evaluator(&frame, context.symbol_table, context.evaluation_context, context.db_accessor, view_);
auto value = expression_->Accept(evaluator);
if (!value.IsNumeric()) return std::nullopt;
int64_t id = value.IsInt() ? value.ValueInt() : value.ValueDouble();
if (value.IsDouble() && id != value.ValueDouble()) return std::nullopt;
auto maybe_vertex = db->FindVertex(storage::v3::Gid::FromInt(id), view_);
if (!maybe_vertex) return std::nullopt;
return std::vector<VertexAccessor>{*maybe_vertex};
// auto *db = context.db_accessor;
// ExpressionEvaluator evaluator(&frame, context.symbol_table, context.evaluation_context, context.db_accessor,
// view_); auto value = expression_->Accept(evaluator); if (!value.IsNumeric()) return std::nullopt; int64_t id =
// value.IsInt() ? value.ValueInt() : value.ValueDouble(); if (value.IsDouble() && id != value.ValueDouble()) return
// std::nullopt; auto maybe_vertex = db->FindVertex(storage::v3::Gid::FromInt(id), view_); auto maybe_vertex =
// nullptr; if (!maybe_vertex) return std::nullopt;
return std::nullopt;
// return std::vector<VertexAccessor>{*maybe_vertex};
};
return MakeUniqueCursorPtr<ScanAllCursor<decltype(vertices)>>(mem, output_symbol_, input_->MakeCursor(mem),
std::move(vertices), "ScanAllById");
@ -2134,7 +2133,7 @@ concept AccessorWithProperties = requires(T value, storage::v3::PropertyId prope
///
/// @tparam TRecordAccessor Either RecordAccessor<Vertex> or
/// RecordAccessor<Edge>
template <AccessorWithProperties TRecordAccessor>
template <RecordAccessor TRecordAccessor>
void SetPropertiesOnRecord(TRecordAccessor *record, const TypedValue &rhs, SetProperties::Op op,
ExecutionContext *context) {
std::optional<std::map<storage::v3::PropertyId, storage::v3::PropertyValue>> old_values;
@ -2198,23 +2197,26 @@ void SetPropertiesOnRecord(TRecordAccessor *record, const TypedValue &rhs, SetPr
auto set_props = [&, record](auto properties) {
for (auto &kv : properties) {
auto maybe_error = record->SetProperty(kv.first, kv.second);
if (maybe_error.HasError()) {
switch (maybe_error.GetError()) {
case storage::v3::Error::DELETED_OBJECT:
throw QueryRuntimeException("Trying to set properties on a deleted graph element.");
case storage::v3::Error::SERIALIZATION_ERROR:
throw TransactionSerializationException();
case storage::v3::Error::PROPERTIES_DISABLED:
throw QueryRuntimeException("Can't set property because properties on edges are disabled.");
case storage::v3::Error::VERTEX_HAS_EDGES:
case storage::v3::Error::NONEXISTENT_OBJECT:
throw QueryRuntimeException("Unexpected error when setting properties.");
if constexpr (AccessorWithSetPropertyAndValidate<TRecordAccessor>) {
const auto maybe_error = record->SetPropertyAndValidate(kv.first, storage::v3::PropertyValue(kv.second));
if (maybe_error.HasError()) {
std::visit(utils::Overloaded{[](const storage::v3::Error error) { HandleErrorOnPropertyUpdate(error); },
[&context](const storage::v3::SchemaViolation &schema_violation) {
HandleSchemaViolation(schema_violation, *context->db_accessor);
}},
maybe_error.GetError());
}
if (should_register_change) {
register_set_property(std::move(*maybe_error), kv.first, std::move(kv.second));
}
} else {
auto maybe_error = record->SetProperty(kv.first, kv.second);
if (maybe_error.HasError()) {
HandleErrorOnPropertyUpdate(maybe_error.GetError());
}
if (should_register_change) {
register_set_property(std::move(*maybe_error), kv.first, std::move(kv.second));
}
}
if (should_register_change) {
register_set_property(std::move(*maybe_error), kv.first, std::move(kv.second));
}
}
};

View File

@ -27,6 +27,7 @@
#include "query/v2/procedure/cypher_types.hpp"
#include "query/v2/procedure/mg_procedure_helpers.hpp"
#include "query/v2/stream/common.hpp"
#include "storage/v3/id_types.hpp"
#include "storage/v3/property_value.hpp"
#include "storage/v3/view.hpp"
#include "utils/algorithm.hpp"
@ -1508,8 +1509,9 @@ mgp_error mgp_properties_iterator_next(mgp_properties_iterator *it, mgp_property
result);
}
// TODO(jbajic) Fix Remove Gid
mgp_error mgp_vertex_get_id(mgp_vertex *v, mgp_vertex_id *result) {
return WrapExceptions([v] { return mgp_vertex_id{.as_int = v->impl.Gid().AsInt()}; }, result);
return WrapExceptions([] { return mgp_vertex_id{.as_int = 0}; }, result);
}
mgp_error mgp_vertex_underlying_graph_is_mutable(mgp_vertex *v, int *result) {
@ -1584,17 +1586,19 @@ mgp_error mgp_vertex_set_property(struct mgp_vertex *v, const char *property_nam
const auto prop_key = v->graph->impl->NameToProperty(property_name);
const auto result = v->impl.SetProperty(prop_key, ToPropertyValue(*property_value));
if (result.HasError()) {
switch (result.GetError()) {
case memgraph::storage::v3::Error::DELETED_OBJECT:
throw DeletedObjectException{"Cannot set the properties of a deleted vertex!"};
case memgraph::storage::v3::Error::NONEXISTENT_OBJECT:
LOG_FATAL("Query modules shouldn't have access to nonexistent objects when setting a property of a vertex!");
case memgraph::storage::v3::Error::PROPERTIES_DISABLED:
case memgraph::storage::v3::Error::VERTEX_HAS_EDGES:
LOG_FATAL("Unexpected error when setting a property of a vertex.");
case memgraph::storage::v3::Error::SERIALIZATION_ERROR:
throw SerializationException{"Cannot serialize setting a property of a vertex."};
}
// TODO(jbajic) Fix query modules
// switch (result.GetError()) {
// case memgraph::storage::v3::Error::DELETED_OBJECT:
// throw DeletedObjectException{"Cannot set the properties of a deleted vertex!"};
// case memgraph::storage::v3::Error::NONEXISTENT_OBJECT:
// LOG_FATAL("Query modules shouldn't have access to nonexistent objects when setting a property of a
// vertex!");
// case memgraph::storage::v3::Error::PROPERTIES_DISABLED:
// case memgraph::storage::v3::Error::VERTEX_HAS_EDGES:
// LOG_FATAL("Unexpected error when setting a property of a vertex.");
// case memgraph::storage::v3::Error::SERIALIZATION_ERROR:
// throw SerializationException{"Cannot serialize setting a property of a vertex."};
// }
}
auto &ctx = v->graph->ctx;
@ -1625,17 +1629,18 @@ mgp_error mgp_vertex_add_label(struct mgp_vertex *v, mgp_label label) {
const auto result = v->impl.AddLabel(label_id);
if (result.HasError()) {
switch (result.GetError()) {
case memgraph::storage::v3::Error::DELETED_OBJECT:
throw DeletedObjectException{"Cannot add a label to a deleted vertex!"};
case memgraph::storage::v3::Error::NONEXISTENT_OBJECT:
LOG_FATAL("Query modules shouldn't have access to nonexistent objects when adding a label to a vertex!");
case memgraph::storage::v3::Error::PROPERTIES_DISABLED:
case memgraph::storage::v3::Error::VERTEX_HAS_EDGES:
LOG_FATAL("Unexpected error when adding a label to a vertex.");
case memgraph::storage::v3::Error::SERIALIZATION_ERROR:
throw SerializationException{"Cannot serialize adding a label to a vertex."};
}
// TODO(jbajic) Fix query modules
// switch (result.GetError()) {
// case memgraph::storage::v3::Error::DELETED_OBJECT:
// throw DeletedObjectException{"Cannot add a label to a deleted vertex!"};
// case memgraph::storage::v3::Error::NONEXISTENT_OBJECT:
// LOG_FATAL("Query modules shouldn't have access to nonexistent objects when adding a label to a vertex!");
// case memgraph::storage::v3::Error::PROPERTIES_DISABLED:
// case memgraph::storage::v3::Error::VERTEX_HAS_EDGES:
// LOG_FATAL("Unexpected error when adding a label to a vertex.");
// case memgraph::storage::v3::Error::SERIALIZATION_ERROR:
// throw SerializationException{"Cannot serialize adding a label to a vertex."};
// }
}
auto &ctx = v->graph->ctx;
@ -1657,17 +1662,19 @@ mgp_error mgp_vertex_remove_label(struct mgp_vertex *v, mgp_label label) {
const auto result = v->impl.RemoveLabel(label_id);
if (result.HasError()) {
switch (result.GetError()) {
case memgraph::storage::v3::Error::DELETED_OBJECT:
throw DeletedObjectException{"Cannot remove a label from a deleted vertex!"};
case memgraph::storage::v3::Error::NONEXISTENT_OBJECT:
LOG_FATAL("Query modules shouldn't have access to nonexistent objects when removing a label from a vertex!");
case memgraph::storage::v3::Error::PROPERTIES_DISABLED:
case memgraph::storage::v3::Error::VERTEX_HAS_EDGES:
LOG_FATAL("Unexpected error when removing a label from a vertex.");
case memgraph::storage::v3::Error::SERIALIZATION_ERROR:
throw SerializationException{"Cannot serialize removing a label from a vertex."};
}
// TODO(jbajic) Fix query modules
// switch (result.GetError()) {
// case memgraph::storage::v3::Error::DELETED_OBJECT:
// throw DeletedObjectException{"Cannot remove a label from a deleted vertex!"};
// case memgraph::storage::v3::Error::NONEXISTENT_OBJECT:
// LOG_FATAL("Query modules shouldn't have access to nonexistent objects when removing a label from a
// vertex!");
// case memgraph::storage::v3::Error::PROPERTIES_DISABLED:
// case memgraph::storage::v3::Error::VERTEX_HAS_EDGES:
// LOG_FATAL("Unexpected error when removing a label from a vertex.");
// case memgraph::storage::v3::Error::SERIALIZATION_ERROR:
// throw SerializationException{"Cannot serialize removing a label from a vertex."};
// }
}
auto &ctx = v->graph->ctx;
@ -2070,11 +2077,12 @@ mgp_error mgp_edge_iter_properties(mgp_edge *e, mgp_memory *memory, mgp_properti
mgp_error mgp_graph_get_vertex_by_id(mgp_graph *graph, mgp_vertex_id id, mgp_memory *memory, mgp_vertex **result) {
return WrapExceptions(
[graph, id, memory]() -> mgp_vertex * {
auto maybe_vertex = graph->impl->FindVertex(memgraph::storage::v3::Gid::FromInt(id.as_int), graph->view);
if (maybe_vertex) {
return NewRawMgpObject<mgp_vertex>(memory, *maybe_vertex, graph);
}
[]() -> mgp_vertex * {
// TODO(jbajic) Fix Remove Gid
// auto maybe_vertex = graph->impl->FindVertex(0);
// if (maybe_vertex) {
// return NewRawMgpObject<mgp_vertex>(memory, *maybe_vertex, graph);
// }
return nullptr;
},
result);
@ -2085,23 +2093,25 @@ mgp_error mgp_graph_is_mutable(mgp_graph *graph, int *result) {
return mgp_error::MGP_ERROR_NO_ERROR;
};
mgp_error mgp_graph_create_vertex(struct mgp_graph *graph, mgp_memory *memory, mgp_vertex **result) {
return WrapExceptions(
[=] {
if (!MgpGraphIsMutable(*graph)) {
throw ImmutableObjectException{"Cannot create a vertex in an immutable graph!"};
}
auto vertex = graph->impl->InsertVertex();
// TODO(jbajic) Fix Remove Gid
mgp_error mgp_graph_create_vertex(struct mgp_graph * /*graph*/, mgp_memory * /*memory*/, mgp_vertex ** /*result*/) {
// return WrapExceptions(
// [=] {
// if (!MgpGraphIsMutable(*graph)) {
// throw ImmutableObjectException{"Cannot create a vertex in an immutable graph!"};
// }
// auto vertex = graph->impl->InsertVertex();
auto &ctx = graph->ctx;
ctx->execution_stats[memgraph::query::v2::ExecutionStats::Key::CREATED_NODES] += 1;
// auto &ctx = graph->ctx;
// ctx->execution_stats[memgraph::query::v2::ExecutionStats::Key::CREATED_NODES] += 1;
if (ctx->trigger_context_collector) {
ctx->trigger_context_collector->RegisterCreatedObject(vertex);
}
return NewRawMgpObject<mgp_vertex>(memory, vertex, graph);
},
result);
// if (ctx->trigger_context_collector) {
// ctx->trigger_context_collector->RegisterCreatedObject(vertex);
// }
// return NewRawMgpObject<mgp_vertex>(memory, nullptr, graph);
// },
// result);
return mgp_error::MGP_ERROR_NO_ERROR;
}
mgp_error mgp_graph_delete_vertex(struct mgp_graph *graph, mgp_vertex *vertex) {
@ -2173,11 +2183,11 @@ mgp_error mgp_graph_detach_delete_vertex(struct mgp_graph *graph, mgp_vertex *ve
if (!trigger_ctx_collector) {
return;
}
trigger_ctx_collector->RegisterDeletedObject((*result)->first);
if (!trigger_ctx_collector->ShouldRegisterDeletedObject<memgraph::query::v2::EdgeAccessor>()) {
return;
}
// TODO(jbajic) Fix Remove Gid
// trigger_ctx_collector->RegisterDeletedObject((*result)->first);
// if (!trigger_ctx_collector->ShouldRegisterDeletedObject<memgraph::query::v2::EdgeAccessor>()) {
// return;
// }
for (const auto &edge : (*result)->second) {
trigger_ctx_collector->RegisterDeletedObject(edge);
}

View File

@ -336,9 +336,10 @@ PyObject *PyGraphCreateVertex(PyGraph *self, PyObject *Py_UNUSED(ignored)) {
MG_ASSERT(PyGraphIsValidImpl(*self));
MG_ASSERT(self->memory);
MgpUniquePtr<mgp_vertex> new_vertex{nullptr, mgp_vertex_destroy};
if (RaiseExceptionFromErrorCode(CreateMgpObject(new_vertex, mgp_graph_create_vertex, self->graph, self->memory))) {
return nullptr;
}
// TODO(jbajic) Fix query module
// if (RaiseExceptionFromErrorCode(CreateMgpObject(new_vertex, mgp_graph_create_vertex, self->graph, self->memory))) {
// return nullptr;
// }
auto *py_vertex = MakePyVertexWithoutCopy(*new_vertex, self);
if (py_vertex != nullptr) {
static_cast<void>(new_vertex.release());

View File

@ -264,8 +264,8 @@ void TriggerContext::AdaptForAccessor(DbAccessor *accessor) {
{
// adapt created_vertices_
auto it = created_vertices_.begin();
for (auto &created_vertex : created_vertices_) {
if (auto maybe_vertex = accessor->FindVertex(created_vertex.object.Gid(), storage::v3::View::OLD); maybe_vertex) {
for ([[maybe_unused]] auto &created_vertex : created_vertices_) {
if (auto maybe_vertex = accessor->FindVertex(kFakeVertexGid); maybe_vertex) {
*it = detail::CreatedObject{*maybe_vertex};
++it;
}
@ -280,7 +280,7 @@ void TriggerContext::AdaptForAccessor(DbAccessor *accessor) {
const auto adapt_context_with_vertex = [accessor](auto *values) {
auto it = values->begin();
for (auto &value : *values) {
if (auto maybe_vertex = accessor->FindVertex(value.object.Gid(), storage::v3::View::OLD); maybe_vertex) {
if (auto maybe_vertex = accessor->FindVertex(kFakeVertexGid); maybe_vertex) {
*it = std::move(value);
it->object = *maybe_vertex;
++it;
@ -298,7 +298,7 @@ void TriggerContext::AdaptForAccessor(DbAccessor *accessor) {
// adapt created_edges
auto it = created_edges_.begin();
for (auto &created_edge : created_edges_) {
const auto maybe_from_vertex = accessor->FindVertex(created_edge.object.From().Gid(), storage::v3::View::OLD);
const auto maybe_from_vertex = accessor->FindVertex(kFakeVertexGid);
if (!maybe_from_vertex) {
continue;
}
@ -322,7 +322,7 @@ void TriggerContext::AdaptForAccessor(DbAccessor *accessor) {
const auto adapt_context_with_edge = [accessor](auto *values) {
auto it = values->begin();
for (const auto &value : *values) {
if (auto maybe_vertex = accessor->FindVertex(value.object.From().Gid(), storage::v3::View::OLD); maybe_vertex) {
if (auto maybe_vertex = accessor->FindVertex(kFakeVertexGid); maybe_vertex) {
auto maybe_out_edges = maybe_vertex->OutEdges(storage::v3::View::OLD);
MG_ASSERT(maybe_out_edges.HasValue());
for (const auto &edge : *maybe_out_edges) {
@ -435,7 +435,7 @@ bool TriggerContext::ShouldEventTrigger(const TriggerEventType event_type) const
void TriggerContextCollector::UpdateLabelMap(const VertexAccessor vertex, const storage::v3::LabelId label_id,
const LabelChange change) {
auto &registry = GetRegistry<VertexAccessor>();
if (!registry.should_register_updated_objects || registry.created_objects.count(vertex.Gid())) {
if (!registry.should_register_updated_objects || registry.created_objects.count(kFakeVertexGid)) {
return;
}

View File

@ -22,12 +22,16 @@
#include "query/v2/db_accessor.hpp"
#include "query/v2/typed_value.hpp"
#include "storage/v3/key_store.hpp"
#include "storage/v3/property_value.hpp"
#include "storage/v3/view.hpp"
#include "utils/concepts.hpp"
#include "utils/fnv.hpp"
namespace memgraph::query::v2 {
// TODO(jbajic) Fix triggers
inline constexpr uint64_t kFakeVertexGid{0};
namespace detail {
template <typename T>
concept ObjectAccessor = utils::SameAsAnyOf<T, VertexAccessor, EdgeAccessor>;
@ -223,8 +227,13 @@ class TriggerContextCollector {
struct HashPairWithAccessor {
template <detail::ObjectAccessor TAccessor, typename T2>
size_t operator()(const std::pair<TAccessor, T2> &pair) const {
using GidType = decltype(std::declval<TAccessor>().Gid());
return utils::HashCombine<GidType, T2>{}(pair.first.Gid(), pair.second);
// TODO(jbajic) Fix Remove Gid
if constexpr (std::is_same_v<TAccessor, VertexAccessor>) {
return utils::HashCombine<uint64_t, T2>{}(kFakeVertexGid, pair.second);
} else {
using UniqueIdentifierType = decltype(std::declval<TAccessor>().Gid());
return utils::HashCombine<UniqueIdentifierType, T2>{}(pair.first.Gid(), pair.second);
}
}
};
@ -239,10 +248,12 @@ class TriggerContextCollector {
template <detail::ObjectAccessor TAccessor>
struct Registry {
using UniqueIdentifier =
typename std::conditional_t<(std::is_same_v<TAccessor, VertexAccessor>), uint64_t, storage::v3::Gid>;
bool should_register_created_objects{false};
bool should_register_deleted_objects{false};
bool should_register_updated_objects{false}; // Set/removed properties (and labels for vertices)
std::unordered_map<storage::v3::Gid, detail::CreatedObject<TAccessor>> created_objects;
std::unordered_map<UniqueIdentifier, detail::CreatedObject<TAccessor>> created_objects;
std::vector<detail::DeletedObject<TAccessor>> deleted_objects;
// During the transaction, a single property on a single object could be changed multiple times.
// We want to register only the global change, at the end of the transaction. The change consists of
@ -268,7 +279,11 @@ class TriggerContextCollector {
if (!registry.should_register_created_objects) {
return;
}
registry.created_objects.emplace(created_object.Gid(), detail::CreatedObject{created_object});
if constexpr (std::is_same_v<TAccessor, VertexAccessor>) {
registry.created_objects.emplace(kFakeVertexGid, detail::CreatedObject{created_object});
} else {
registry.created_objects.emplace(created_object.Gid(), detail::CreatedObject{created_object});
}
}
template <detail::ObjectAccessor TAccessor>
@ -276,9 +291,8 @@ class TriggerContextCollector {
return GetRegistry<TAccessor>().should_register_deleted_objects;
}
template <detail::ObjectAccessor TAccessor>
void RegisterDeletedObject(const TAccessor &deleted_object) {
auto &registry = GetRegistry<TAccessor>();
void RegisterDeletedObject(const EdgeAccessor &deleted_object) {
auto &registry = GetRegistry<EdgeAccessor>();
if (!registry.should_register_deleted_objects || registry.created_objects.count(deleted_object.Gid())) {
return;
}
@ -286,6 +300,10 @@ class TriggerContextCollector {
registry.deleted_objects.emplace_back(deleted_object);
}
void RegisterDeletedObject(const VertexAccessor &deleted_object) {
// TODO(jbajic) Fix Remove Gid
}
template <detail::ObjectAccessor TAccessor>
bool ShouldRegisterObjectPropertyChange() const {
return GetRegistry<TAccessor>().should_register_updated_objects;
@ -298,9 +316,14 @@ class TriggerContextCollector {
if (!registry.should_register_updated_objects) {
return;
}
if (registry.created_objects.count(object.Gid())) {
return;
if constexpr (std::is_same_v<TAccessor, VertexAccessor>) {
if (registry.created_objects.count(kFakeVertexGid)) {
return;
}
} else {
if (registry.created_objects.count(object.Gid())) {
return;
}
}
if (auto it = registry.property_changes.find({object, key}); it != registry.property_changes.end()) {

View File

@ -1083,7 +1083,8 @@ size_t TypedValue::Hash::operator()(const TypedValue &value) const {
return hash;
}
case TypedValue::Type::Vertex:
return value.ValueVertex().Gid().AsUint();
// TODO(jbajic) Fix vertex hashing
return 0;
case TypedValue::Type::Edge:
return value.ValueEdge().Gid().AsUint();
case TypedValue::Type::Path: {

View File

@ -19,6 +19,7 @@
#include "storage/v3/edge_accessor.hpp"
#include "storage/v3/edge_ref.hpp"
#include "storage/v3/mvcc.hpp"
#include "storage/v3/schemas.hpp"
#include "storage/v3/vertex_accessor.hpp"
#include "storage/v3/vertices_skip_list.hpp"
#include "utils/file_locker.hpp"
@ -299,48 +300,48 @@ RecoveredSnapshot LoadSnapshot(const std::filesystem::path &path, VerticesSkipLi
}
// Insert vertex.
auto gid = snapshot.ReadUint();
if (!gid) throw RecoveryFailure("Invalid snapshot data!");
if (i > 0 && *gid <= last_vertex_gid) {
throw RecoveryFailure("Invalid snapshot data!");
}
last_vertex_gid = *gid;
spdlog::debug("Recovering vertex {}.", *gid);
auto [it, inserted] = vertex_acc.insert({Vertex{Gid::FromUint(*gid), nullptr}});
if (!inserted) throw RecoveryFailure("The vertex must be inserted here!");
// auto gid = snapshot.ReadUint();
// if (!gid) throw RecoveryFailure("Invalid snapshot data!");
// if (i > 0 && *gid <= last_vertex_gid) {
// throw RecoveryFailure("Invalid snapshot data!");
// }
// last_vertex_gid = *gid;
// spdlog::debug("Recovering vertex {}.", *gid);
// auto [it, inserted] = vertex_acc.insert({Vertex{Gid::FromUint(*gid), nullptr}});
// if (!inserted) throw RecoveryFailure("The vertex must be inserted here!");
// Recover labels.
spdlog::trace("Recovering labels for vertex {}.", *gid);
{
auto labels_size = snapshot.ReadUint();
if (!labels_size) throw RecoveryFailure("Invalid snapshot data!");
auto &labels = it->vertex.labels;
labels.reserve(*labels_size);
for (uint64_t j = 0; j < *labels_size; ++j) {
auto label = snapshot.ReadUint();
if (!label) throw RecoveryFailure("Invalid snapshot data!");
SPDLOG_TRACE("Recovered label \"{}\" for vertex {}.", name_id_mapper->IdToName(snapshot_id_map.at(*label)),
*gid);
labels.emplace_back(get_label_from_id(*label));
}
}
// spdlog::trace("Recovering labels for vertex {}.", *gid);
// {
// auto labels_size = snapshot.ReadUint();
// if (!labels_size) throw RecoveryFailure("Invalid snapshot data!");
// auto &labels = it->vertex.labels;
// labels.reserve(*labels_size);
// for (uint64_t j = 0; j < *labels_size; ++j) {
// auto label = snapshot.ReadUint();
// if (!label) throw RecoveryFailure("Invalid snapshot data!");
// SPDLOG_TRACE("Recovered label \"{}\" for vertex {}.", name_id_mapper->IdToName(snapshot_id_map.at(*label)),
// *gid);
// labels.emplace_back(get_label_from_id(*label));
// }
// }
// Recover properties.
spdlog::trace("Recovering properties for vertex {}.", *gid);
{
auto props_size = snapshot.ReadUint();
if (!props_size) throw RecoveryFailure("Invalid snapshot data!");
auto &props = it->vertex.properties;
for (uint64_t j = 0; j < *props_size; ++j) {
auto key = snapshot.ReadUint();
if (!key) throw RecoveryFailure("Invalid snapshot data!");
auto value = snapshot.ReadPropertyValue();
if (!value) throw RecoveryFailure("Invalid snapshot data!");
SPDLOG_TRACE("Recovered property \"{}\" with value \"{}\" for vertex {}.",
name_id_mapper->IdToName(snapshot_id_map.at(*key)), *value, *gid);
props.SetProperty(get_property_from_id(*key), *value);
}
}
// spdlog::trace("Recovering properties for vertex {}.", *gid);
// {
// auto props_size = snapshot.ReadUint();
// if (!props_size) throw RecoveryFailure("Invalid snapshot data!");
// auto &props = it->vertex.properties;
// for (uint64_t j = 0; j < *props_size; ++j) {
// auto key = snapshot.ReadUint();
// if (!key) throw RecoveryFailure("Invalid snapshot data!");
// auto value = snapshot.ReadPropertyValue();
// if (!value) throw RecoveryFailure("Invalid snapshot data!");
// SPDLOG_TRACE("Recovered property \"{}\" with value \"{}\" for vertex {}.",
// name_id_mapper->IdToName(snapshot_id_map.at(*key)), *value, *gid);
// props.SetProperty(get_property_from_id(*key), *value);
// }
// }
// Skip in edges.
{
@ -380,11 +381,11 @@ RecoveredSnapshot LoadSnapshot(const std::filesystem::path &path, VerticesSkipLi
if (!marker || *marker != Marker::SECTION_VERTEX) throw RecoveryFailure("Invalid snapshot data!");
}
spdlog::trace("Recovering connectivity for vertex {}.", vertex.Gid().AsUint());
// spdlog::trace("Recovering connectivity for vertex {}.", vertex.Gid().AsUint());
// Check vertex.
auto gid = snapshot.ReadUint();
if (!gid) throw RecoveryFailure("Invalid snapshot data!");
if (gid != vertex.Gid().AsUint()) throw RecoveryFailure("Invalid snapshot data!");
// auto gid = snapshot.ReadUint();
// if (!gid) throw RecoveryFailure("Invalid snapshot data!");
// if (gid != vertex.Gid().AsUint()) throw RecoveryFailure("Invalid snapshot data!");
// Skip labels.
{
@ -410,7 +411,8 @@ RecoveredSnapshot LoadSnapshot(const std::filesystem::path &path, VerticesSkipLi
// Recover in edges.
{
spdlog::trace("Recovering inbound edges for vertex {}.", vertex.Gid().AsUint());
// TODO Fix Gid
spdlog::trace("Recovering inbound edges for vertex {}.", 1);
auto in_size = snapshot.ReadUint();
if (!in_size) throw RecoveryFailure("Invalid snapshot data!");
vertex.in_edges.reserve(*in_size);
@ -438,15 +440,17 @@ RecoveredSnapshot LoadSnapshot(const std::filesystem::path &path, VerticesSkipLi
edge_ref = EdgeRef(&*edge);
}
}
// TODO Fix Gid
SPDLOG_TRACE("Recovered inbound edge {} with label \"{}\" from vertex {}.", *edge_gid,
name_id_mapper->IdToName(snapshot_id_map.at(*edge_type)), from_vertex->vertex.Gid().AsUint());
name_id_mapper->IdToName(snapshot_id_map.at(*edge_type)), 1);
vertex.in_edges.emplace_back(get_edge_type_from_id(*edge_type), &from_vertex->vertex, edge_ref);
}
}
// Recover out edges.
{
spdlog::trace("Recovering outbound edges for vertex {}.", vertex.Gid().AsUint());
// TODO Fix Gid
spdlog::trace("Recovering outbound edges for vertex {}.", 1);
auto out_size = snapshot.ReadUint();
if (!out_size) throw RecoveryFailure("Invalid snapshot data!");
vertex.out_edges.reserve(*out_size);
@ -474,8 +478,9 @@ RecoveredSnapshot LoadSnapshot(const std::filesystem::path &path, VerticesSkipLi
edge_ref = EdgeRef(&*edge);
}
}
// TODO Fix Gid
SPDLOG_TRACE("Recovered outbound edge {} with label \"{}\" to vertex {}.", *edge_gid,
name_id_mapper->IdToName(snapshot_id_map.at(*edge_type)), to_vertex->vertex.Gid().AsUint());
name_id_mapper->IdToName(snapshot_id_map.at(*edge_type)), 1);
vertex.out_edges.emplace_back(get_edge_type_from_id(*edge_type), &to_vertex->vertex, edge_ref);
}
// Increment edge count. We only increment the count here because the
@ -765,7 +770,8 @@ void CreateSnapshot(Transaction *transaction, const std::filesystem::path &snaps
// Store the vertex.
{
snapshot.WriteMarker(Marker::SECTION_VERTEX);
snapshot.WriteUint(lgo_vertex.vertex.Gid().AsUint());
// TODO Fix Gid
snapshot.WriteUint(1);
const auto &labels = maybe_labels.GetValue();
snapshot.WriteUint(labels.size());
for (const auto &item : labels) {
@ -779,16 +785,17 @@ void CreateSnapshot(Transaction *transaction, const std::filesystem::path &snaps
}
const auto &in_edges = maybe_in_edges.GetValue();
snapshot.WriteUint(in_edges.size());
// TODO Disabled serialization for vertices
for (const auto &item : in_edges) {
snapshot.WriteUint(item.Gid().AsUint());
snapshot.WriteUint(item.FromVertex().Gid().AsUint());
snapshot.WriteUint(1);
write_mapping(item.EdgeType());
}
const auto &out_edges = maybe_out_edges.GetValue();
snapshot.WriteUint(out_edges.size());
for (const auto &item : out_edges) {
snapshot.WriteUint(item.Gid().AsUint());
snapshot.WriteUint(item.ToVertex().Gid().AsUint());
snapshot.WriteUint(1);
write_mapping(item.EdgeType());
}
}

View File

@ -486,93 +486,93 @@ void EncodeDelta(BaseEncoder *encoder, NameIdMapper *name_id_mapper, Config::Ite
// When converting a Delta to a WAL delta the logic is inverted. That is
// because the Delta's represent undo actions and we want to store redo
// actions.
encoder->WriteMarker(Marker::SECTION_DELTA);
encoder->WriteUint(timestamp);
std::lock_guard<utils::SpinLock> guard(vertex.lock);
switch (delta.action) {
case Delta::Action::DELETE_OBJECT:
case Delta::Action::RECREATE_OBJECT: {
encoder->WriteMarker(VertexActionToMarker(delta.action));
encoder->WriteUint(vertex.Gid().AsUint());
break;
}
case Delta::Action::SET_PROPERTY: {
encoder->WriteMarker(Marker::DELTA_VERTEX_SET_PROPERTY);
encoder->WriteUint(vertex.Gid().AsUint());
encoder->WriteString(name_id_mapper->IdToName(delta.property.key.AsUint()));
// The property value is the value that is currently stored in the
// vertex.
// TODO (mferencevic): Mitigate the memory allocation introduced here
// (with the `GetProperty` call). It is the only memory allocation in the
// entire WAL file writing logic.
encoder->WritePropertyValue(vertex.properties.GetProperty(delta.property.key));
break;
}
case Delta::Action::ADD_LABEL:
case Delta::Action::REMOVE_LABEL: {
encoder->WriteMarker(VertexActionToMarker(delta.action));
encoder->WriteUint(vertex.Gid().AsUint());
encoder->WriteString(name_id_mapper->IdToName(delta.label.AsUint()));
break;
}
case Delta::Action::ADD_OUT_EDGE:
case Delta::Action::REMOVE_OUT_EDGE: {
encoder->WriteMarker(VertexActionToMarker(delta.action));
if (items.properties_on_edges) {
encoder->WriteUint(delta.vertex_edge.edge.ptr->gid.AsUint());
} else {
encoder->WriteUint(delta.vertex_edge.edge.gid.AsUint());
}
encoder->WriteString(name_id_mapper->IdToName(delta.vertex_edge.edge_type.AsUint()));
encoder->WriteUint(vertex.Gid().AsUint());
encoder->WriteUint(delta.vertex_edge.vertex->Gid().AsUint());
break;
}
case Delta::Action::ADD_IN_EDGE:
case Delta::Action::REMOVE_IN_EDGE:
// These actions are already encoded in the *_OUT_EDGE actions. This
// function should never be called for this type of deltas.
LOG_FATAL("Invalid delta action!");
}
// encoder->WriteMarker(Marker::SECTION_DELTA);
// encoder->WriteUint(timestamp);
// std::lock_guard<utils::SpinLock> guard(vertex.lock);
// switch (delta.action) {
// case Delta::Action::DELETE_OBJECT:
// case Delta::Action::RECREATE_OBJECT: {
// encoder->WriteMarker(VertexActionToMarker(delta.action));
// encoder->WriteUint(vertex.Gid().AsUint());
// break;
// }
// case Delta::Action::SET_PROPERTY: {
// encoder->WriteMarker(Marker::DELTA_VERTEX_SET_PROPERTY);
// encoder->WriteUint(vertex.Gid().AsUint());
// encoder->WriteString(name_id_mapper->IdToName(delta.property.key.AsUint()));
// // The property value is the value that is currently stored in the
// // vertex.
// // TODO (mferencevic): Mitigate the memory allocation introduced here
// // (with the `GetProperty` call). It is the only memory allocation in the
// // entire WAL file writing logic.
// encoder->WritePropertyValue(vertex.properties.GetProperty(delta.property.key));
// break;
// }
// case Delta::Action::ADD_LABEL:
// case Delta::Action::REMOVE_LABEL: {
// encoder->WriteMarker(VertexActionToMarker(delta.action));
// encoder->WriteUint(vertex.Gid().AsUint());
// encoder->WriteString(name_id_mapper->IdToName(delta.label.AsUint()));
// break;
// }
// case Delta::Action::ADD_OUT_EDGE:
// case Delta::Action::REMOVE_OUT_EDGE: {
// encoder->WriteMarker(VertexActionToMarker(delta.action));
// if (items.properties_on_edges) {
// encoder->WriteUint(delta.vertex_edge.edge.ptr->gid.AsUint());
// } else {
// encoder->WriteUint(delta.vertex_edge.edge.gid.AsUint());
// }
// encoder->WriteString(name_id_mapper->IdToName(delta.vertex_edge.edge_type.AsUint()));
// encoder->WriteUint(vertex.Gid().AsUint());
// encoder->WriteUint(delta.vertex_edge.vertex->Gid().AsUint());
// break;
// }
// case Delta::Action::ADD_IN_EDGE:
// case Delta::Action::REMOVE_IN_EDGE:
// // These actions are already encoded in the *_OUT_EDGE actions. This
// // function should never be called for this type of deltas.
// LOG_FATAL("Invalid delta action!");
// }
}
void EncodeDelta(BaseEncoder *encoder, NameIdMapper *name_id_mapper, const Delta &delta, const Edge &edge,
uint64_t timestamp) {
// When converting a Delta to a WAL delta the logic is inverted. That is
// because the Delta's represent undo actions and we want to store redo
// actions.
encoder->WriteMarker(Marker::SECTION_DELTA);
encoder->WriteUint(timestamp);
std::lock_guard<utils::SpinLock> guard(edge.lock);
switch (delta.action) {
case Delta::Action::SET_PROPERTY: {
encoder->WriteMarker(Marker::DELTA_EDGE_SET_PROPERTY);
encoder->WriteUint(edge.gid.AsUint());
encoder->WriteString(name_id_mapper->IdToName(delta.property.key.AsUint()));
// The property value is the value that is currently stored in the
// edge.
// TODO (mferencevic): Mitigate the memory allocation introduced here
// (with the `GetProperty` call). It is the only memory allocation in the
// entire WAL file writing logic.
encoder->WritePropertyValue(edge.properties.GetProperty(delta.property.key));
break;
}
case Delta::Action::DELETE_OBJECT:
case Delta::Action::RECREATE_OBJECT:
// These actions are already encoded in vertex *_OUT_EDGE actions. Also,
// these deltas don't contain any information about the from vertex, to
// vertex or edge type so they are useless. This function should never
// be called for this type of deltas.
LOG_FATAL("Invalid delta action!");
case Delta::Action::ADD_LABEL:
case Delta::Action::REMOVE_LABEL:
case Delta::Action::ADD_OUT_EDGE:
case Delta::Action::REMOVE_OUT_EDGE:
case Delta::Action::ADD_IN_EDGE:
case Delta::Action::REMOVE_IN_EDGE:
// These deltas shouldn't appear for edges.
LOG_FATAL("Invalid database state!");
}
// // actions.
// encoder->WriteMarker(Marker::SECTION_DELTA);
// encoder->WriteUint(timestamp);
// std::lock_guard<utils::SpinLock> guard(edge.lock);
// switch (delta.action) {
// case Delta::Action::SET_PROPERTY: {
// encoder->WriteMarker(Marker::DELTA_EDGE_SET_PROPERTY);
// encoder->WriteUint(edge.gid.AsUint());
// encoder->WriteString(name_id_mapper->IdToName(delta.property.key.AsUint()));
// // The property value is the value that is currently stored in the
// // edge.
// // TODO (mferencevic): Mitigate the memory allocation introduced here
// // (with the `GetProperty` call). It is the only memory allocation in the
// // entire WAL file writing logic.
// encoder->WritePropertyValue(edge.properties.GetProperty(delta.property.key));
// break;
// }
// case Delta::Action::DELETE_OBJECT:
// case Delta::Action::RECREATE_OBJECT:
// // These actions are already encoded in vertex *_OUT_EDGE actions. Also,
// // these deltas don't contain any information about the from vertex, to
// // vertex or edge type so they are useless. This function should never
// // be called for this type of deltas.
// LOG_FATAL("Invalid delta action!");
// case Delta::Action::ADD_LABEL:
// case Delta::Action::REMOVE_LABEL:
// case Delta::Action::ADD_OUT_EDGE:
// case Delta::Action::REMOVE_OUT_EDGE:
// case Delta::Action::ADD_IN_EDGE:
// case Delta::Action::REMOVE_IN_EDGE:
// // These deltas shouldn't appear for edges.
// LOG_FATAL("Invalid database state!");
// }
}
void EncodeTransactionEnd(BaseEncoder *encoder, uint64_t timestamp) {
@ -645,237 +645,241 @@ RecoveryInfo LoadWal(const std::filesystem::path &path, RecoveredIndicesAndConst
auto edge_acc = edges->access();
auto vertex_acc = vertices->access();
spdlog::info("WAL file contains {} deltas.", info.num_deltas);
for (uint64_t i = 0; i < info.num_deltas; ++i) {
// Read WAL delta header to find out the delta timestamp.
auto timestamp = ReadWalDeltaHeader(&wal);
// for (uint64_t i = 0; i < info.num_deltas; ++i) {
// // Read WAL delta header to find out the delta timestamp.
// auto timestamp = ReadWalDeltaHeader(&wal);
if (!last_loaded_timestamp || timestamp > *last_loaded_timestamp) {
// This delta should be loaded.
auto delta = ReadWalDeltaData(&wal);
switch (delta.type) {
case WalDeltaData::Type::VERTEX_CREATE: {
auto [vertex, inserted] = vertex_acc.insert({Vertex{delta.vertex_create_delete.gid, nullptr}});
if (!inserted) throw RecoveryFailure("The vertex must be inserted here!");
// if (!last_loaded_timestamp || timestamp > *last_loaded_timestamp) {
// // This delta should be loaded.
// auto delta = ReadWalDeltaData(&wal);
// switch (delta.type) {
// case WalDeltaData::Type::VERTEX_CREATE: {
// auto [vertex, inserted] = vertex_acc.insert({Vertex{delta.vertex_create_delete.gid, nullptr}});
// if (!inserted) throw RecoveryFailure("The vertex must be inserted here!");
ret.next_vertex_id = std::max(ret.next_vertex_id, delta.vertex_create_delete.gid.AsUint() + 1);
// ret.next_vertex_id = std::max(ret.next_vertex_id, delta.vertex_create_delete.gid.AsUint() + 1);
break;
}
case WalDeltaData::Type::VERTEX_DELETE: {
auto lgo_vertex_it = vertex_acc.find(std::vector{PropertyValue{delta.vertex_create_delete.gid.AsInt()}});
if (lgo_vertex_it == vertex_acc.end()) {
throw RecoveryFailure("The vertex doesn't exist!");
}
auto &vertex = lgo_vertex_it->vertex;
if (!vertex.in_edges.empty() || !vertex.out_edges.empty())
throw RecoveryFailure("The vertex can't be deleted because it still has edges!");
// break;
// }
// case WalDeltaData::Type::VERTEX_DELETE: {
// auto lgo_vertex_it = vertex_acc.find(std::vector{PropertyValue{delta.vertex_create_delete.gid.AsInt()}});
// if (lgo_vertex_it == vertex_acc.end()) {
// throw RecoveryFailure("The vertex doesn't exist!");
// }
// auto &vertex = lgo_vertex_it->vertex;
// if (!vertex.in_edges.empty() || !vertex.out_edges.empty())
// throw RecoveryFailure("The vertex can't be deleted because it still has edges!");
if (!vertex_acc.remove(std::vector{PropertyValue{delta.vertex_create_delete.gid.AsInt()}}))
throw RecoveryFailure("The vertex must be removed here!");
// if (!vertex_acc.remove(std::vector{PropertyValue{delta.vertex_create_delete.gid.AsInt()}}))
// throw RecoveryFailure("The vertex must be removed here!");
break;
}
case WalDeltaData::Type::VERTEX_ADD_LABEL:
case WalDeltaData::Type::VERTEX_REMOVE_LABEL: {
auto lgo_vertex_it = vertex_acc.find(std::vector{PropertyValue{delta.vertex_add_remove_label.gid.AsInt()}});
if (lgo_vertex_it == vertex_acc.end()) {
throw RecoveryFailure("The vertex doesn't exist!");
}
auto &vertex = lgo_vertex_it->vertex;
// break;
// }
// case WalDeltaData::Type::VERTEX_ADD_LABEL:
// case WalDeltaData::Type::VERTEX_REMOVE_LABEL: {
// auto lgo_vertex_it =
// vertex_acc.find(std::vector{PropertyValue{delta.vertex_add_remove_label.gid.AsInt()}}); if (lgo_vertex_it
// == vertex_acc.end()) {
// throw RecoveryFailure("The vertex doesn't exist!");
// }
// auto &vertex = lgo_vertex_it->vertex;
auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.vertex_add_remove_label.label));
auto it = std::find(vertex.labels.begin(), vertex.labels.end(), label_id);
// auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.vertex_add_remove_label.label));
// auto it = std::find(vertex.labels.begin(), vertex.labels.end(), label_id);
if (delta.type == WalDeltaData::Type::VERTEX_ADD_LABEL) {
if (it != vertex.labels.end()) throw RecoveryFailure("The vertex already has the label!");
vertex.labels.push_back(label_id);
} else {
if (it == vertex.labels.end()) throw RecoveryFailure("The vertex doesn't have the label!");
std::swap(*it, vertex.labels.back());
vertex.labels.pop_back();
}
// if (delta.type == WalDeltaData::Type::VERTEX_ADD_LABEL) {
// if (it != vertex.labels.end()) throw RecoveryFailure("The vertex already has the label!");
// vertex.labels.push_back(label_id);
// } else {
// if (it == vertex.labels.end()) throw RecoveryFailure("The vertex doesn't have the label!");
// std::swap(*it, vertex.labels.back());
// vertex.labels.pop_back();
// }
break;
}
case WalDeltaData::Type::VERTEX_SET_PROPERTY: {
auto lgo_vertex_it = vertex_acc.find(std::vector{PropertyValue{delta.vertex_edge_set_property.gid.AsInt()}});
if (lgo_vertex_it == vertex_acc.end()) {
throw RecoveryFailure("The vertex doesn't exist!");
}
// break;
// }
// case WalDeltaData::Type::VERTEX_SET_PROPERTY: {
// auto lgo_vertex_it =
// vertex_acc.find(std::vector{PropertyValue{delta.vertex_edge_set_property.gid.AsInt()}}); if (lgo_vertex_it
// == vertex_acc.end()) {
// throw RecoveryFailure("The vertex doesn't exist!");
// }
auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.vertex_edge_set_property.property));
auto &property_value = delta.vertex_edge_set_property.value;
// auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.vertex_edge_set_property.property));
// auto &property_value = delta.vertex_edge_set_property.value;
lgo_vertex_it->vertex.properties.SetProperty(property_id, property_value);
// lgo_vertex_it->vertex.properties.SetProperty(property_id, property_value);
break;
}
case WalDeltaData::Type::EDGE_CREATE: {
auto from_lgo_vertex =
vertex_acc.find(std::vector{PropertyValue{delta.edge_create_delete.from_vertex.AsInt()}});
if (from_lgo_vertex == vertex_acc.end()) {
throw RecoveryFailure("The from vertex doesn't exist!");
}
auto to_lgo_vertex = vertex_acc.find(std::vector{PropertyValue{delta.edge_create_delete.to_vertex.AsInt()}});
if (to_lgo_vertex == vertex_acc.end()) {
throw RecoveryFailure("The to vertex doesn't exist!");
}
auto &from_vertex = from_lgo_vertex->vertex;
auto &to_vertex = to_lgo_vertex->vertex;
// break;
// }
// case WalDeltaData::Type::EDGE_CREATE: {
// auto from_lgo_vertex =
// vertex_acc.find(std::vector{PropertyValue{delta.edge_create_delete.from_vertex.AsInt()}});
// if (from_lgo_vertex == vertex_acc.end()) {
// throw RecoveryFailure("The from vertex doesn't exist!");
// }
// auto to_lgo_vertex =
// vertex_acc.find(std::vector{PropertyValue{delta.edge_create_delete.to_vertex.AsInt()}}); if (to_lgo_vertex
// == vertex_acc.end()) {
// throw RecoveryFailure("The to vertex doesn't exist!");
// }
// auto &from_vertex = from_lgo_vertex->vertex;
// auto &to_vertex = to_lgo_vertex->vertex;
auto edge_gid = delta.edge_create_delete.gid;
auto edge_type_id = EdgeTypeId::FromUint(name_id_mapper->NameToId(delta.edge_create_delete.edge_type));
EdgeRef edge_ref(edge_gid);
if (items.properties_on_edges) {
auto [edge, inserted] = edge_acc.insert(Edge{edge_gid, nullptr});
if (!inserted) throw RecoveryFailure("The edge must be inserted here!");
edge_ref = EdgeRef(&*edge);
}
{
std::tuple<EdgeTypeId, Vertex *, EdgeRef> link{edge_type_id, &to_vertex, edge_ref};
auto it = std::find(from_vertex.out_edges.begin(), from_vertex.out_edges.end(), link);
if (it != from_vertex.out_edges.end()) throw RecoveryFailure("The from vertex already has this edge!");
from_vertex.out_edges.push_back(link);
}
{
std::tuple<EdgeTypeId, Vertex *, EdgeRef> link{edge_type_id, &from_vertex, edge_ref};
auto it = std::find(to_vertex.in_edges.begin(), to_vertex.in_edges.end(), link);
if (it != to_vertex.in_edges.end()) throw RecoveryFailure("The to vertex already has this edge!");
to_vertex.in_edges.push_back(link);
}
// auto edge_gid = delta.edge_create_delete.gid;
// auto edge_type_id = EdgeTypeId::FromUint(name_id_mapper->NameToId(delta.edge_create_delete.edge_type));
// EdgeRef edge_ref(edge_gid);
// if (items.properties_on_edges) {
// auto [edge, inserted] = edge_acc.insert(Edge{edge_gid, nullptr});
// if (!inserted) throw RecoveryFailure("The edge must be inserted here!");
// edge_ref = EdgeRef(&*edge);
// }
// {
// std::tuple<EdgeTypeId, Vertex *, EdgeRef> link{edge_type_id, &to_vertex, edge_ref};
// auto it = std::find(from_vertex.out_edges.begin(), from_vertex.out_edges.end(), link);
// if (it != from_vertex.out_edges.end()) throw RecoveryFailure("The from vertex already has this edge!");
// from_vertex.out_edges.push_back(link);
// }
// {
// std::tuple<EdgeTypeId, Vertex *, EdgeRef> link{edge_type_id, &from_vertex, edge_ref};
// auto it = std::find(to_vertex.in_edges.begin(), to_vertex.in_edges.end(), link);
// if (it != to_vertex.in_edges.end()) throw RecoveryFailure("The to vertex already has this edge!");
// to_vertex.in_edges.push_back(link);
// }
ret.next_edge_id = std::max(ret.next_edge_id, edge_gid.AsUint() + 1);
// ret.next_edge_id = std::max(ret.next_edge_id, edge_gid.AsUint() + 1);
// Increment edge count.
edge_count->fetch_add(1, std::memory_order_acq_rel);
// // Increment edge count.
// edge_count->fetch_add(1, std::memory_order_acq_rel);
break;
}
case WalDeltaData::Type::EDGE_DELETE: {
auto from_lgo_vertex =
vertex_acc.find(std::vector{PropertyValue{delta.edge_create_delete.from_vertex.AsInt()}});
if (from_lgo_vertex == vertex_acc.end()) {
throw RecoveryFailure("The from vertex doesn't exist!");
}
auto to_lgo_vertex = vertex_acc.find(std::vector{PropertyValue{delta.edge_create_delete.to_vertex.AsInt()}});
if (to_lgo_vertex == vertex_acc.end()) {
throw RecoveryFailure("The to vertex doesn't exist!");
}
auto &from_vertex = from_lgo_vertex->vertex;
auto &to_vertex = to_lgo_vertex->vertex;
// break;
// }
// case WalDeltaData::Type::EDGE_DELETE: {
// auto from_lgo_vertex =
// vertex_acc.find(std::vector{PropertyValue{delta.edge_create_delete.from_vertex.AsInt()}});
// if (from_lgo_vertex == vertex_acc.end()) {
// throw RecoveryFailure("The from vertex doesn't exist!");
// }
// auto to_lgo_vertex =
// vertex_acc.find(std::vector{PropertyValue{delta.edge_create_delete.to_vertex.AsInt()}}); if (to_lgo_vertex
// == vertex_acc.end()) {
// throw RecoveryFailure("The to vertex doesn't exist!");
// }
// auto &from_vertex = from_lgo_vertex->vertex;
// auto &to_vertex = to_lgo_vertex->vertex;
auto edge_gid = delta.edge_create_delete.gid;
auto edge_type_id = EdgeTypeId::FromUint(name_id_mapper->NameToId(delta.edge_create_delete.edge_type));
EdgeRef edge_ref(edge_gid);
if (items.properties_on_edges) {
auto edge = edge_acc.find(edge_gid);
if (edge == edge_acc.end()) throw RecoveryFailure("The edge doesn't exist!");
edge_ref = EdgeRef(&*edge);
}
{
std::tuple<EdgeTypeId, Vertex *, EdgeRef> link{edge_type_id, &to_vertex, edge_ref};
auto it = std::find(from_vertex.out_edges.begin(), from_vertex.out_edges.end(), link);
if (it == from_vertex.out_edges.end()) throw RecoveryFailure("The from vertex doesn't have this edge!");
std::swap(*it, from_vertex.out_edges.back());
from_vertex.out_edges.pop_back();
}
{
std::tuple<EdgeTypeId, Vertex *, EdgeRef> link{edge_type_id, &from_vertex, edge_ref};
auto it = std::find(to_vertex.in_edges.begin(), to_vertex.in_edges.end(), link);
if (it == to_vertex.in_edges.end()) throw RecoveryFailure("The to vertex doesn't have this edge!");
std::swap(*it, to_vertex.in_edges.back());
to_vertex.in_edges.pop_back();
}
if (items.properties_on_edges) {
if (!edge_acc.remove(edge_gid)) throw RecoveryFailure("The edge must be removed here!");
}
// auto edge_gid = delta.edge_create_delete.gid;
// auto edge_type_id = EdgeTypeId::FromUint(name_id_mapper->NameToId(delta.edge_create_delete.edge_type));
// EdgeRef edge_ref(edge_gid);
// if (items.properties_on_edges) {
// auto edge = edge_acc.find(edge_gid);
// if (edge == edge_acc.end()) throw RecoveryFailure("The edge doesn't exist!");
// edge_ref = EdgeRef(&*edge);
// }
// {
// std::tuple<EdgeTypeId, Vertex *, EdgeRef> link{edge_type_id, &to_vertex, edge_ref};
// auto it = std::find(from_vertex.out_edges.begin(), from_vertex.out_edges.end(), link);
// if (it == from_vertex.out_edges.end()) throw RecoveryFailure("The from vertex doesn't have this edge!");
// std::swap(*it, from_vertex.out_edges.back());
// from_vertex.out_edges.pop_back();
// }
// {
// std::tuple<EdgeTypeId, Vertex *, EdgeRef> link{edge_type_id, &from_vertex, edge_ref};
// auto it = std::find(to_vertex.in_edges.begin(), to_vertex.in_edges.end(), link);
// if (it == to_vertex.in_edges.end()) throw RecoveryFailure("The to vertex doesn't have this edge!");
// std::swap(*it, to_vertex.in_edges.back());
// to_vertex.in_edges.pop_back();
// }
// if (items.properties_on_edges) {
// if (!edge_acc.remove(edge_gid)) throw RecoveryFailure("The edge must be removed here!");
// }
// Decrement edge count.
edge_count->fetch_add(-1, std::memory_order_acq_rel);
// // Decrement edge count.
// edge_count->fetch_add(-1, std::memory_order_acq_rel);
break;
}
case WalDeltaData::Type::EDGE_SET_PROPERTY: {
if (!items.properties_on_edges)
throw RecoveryFailure(
"The WAL has properties on edges, but the storage is "
"configured without properties on edges!");
auto edge = edge_acc.find(delta.vertex_edge_set_property.gid);
if (edge == edge_acc.end()) throw RecoveryFailure("The edge doesn't exist!");
auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.vertex_edge_set_property.property));
auto &property_value = delta.vertex_edge_set_property.value;
edge->properties.SetProperty(property_id, property_value);
break;
}
case WalDeltaData::Type::TRANSACTION_END:
break;
case WalDeltaData::Type::LABEL_INDEX_CREATE: {
auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label.label));
AddRecoveredIndexConstraint(&indices_constraints->indices.label, label_id, "The label index already exists!");
break;
}
case WalDeltaData::Type::LABEL_INDEX_DROP: {
auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label.label));
RemoveRecoveredIndexConstraint(&indices_constraints->indices.label, label_id,
"The label index doesn't exist!");
break;
}
case WalDeltaData::Type::LABEL_PROPERTY_INDEX_CREATE: {
auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.label));
auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.property));
AddRecoveredIndexConstraint(&indices_constraints->indices.label_property, {label_id, property_id},
"The label property index already exists!");
break;
}
case WalDeltaData::Type::LABEL_PROPERTY_INDEX_DROP: {
auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.label));
auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.property));
RemoveRecoveredIndexConstraint(&indices_constraints->indices.label_property, {label_id, property_id},
"The label property index doesn't exist!");
break;
}
case WalDeltaData::Type::EXISTENCE_CONSTRAINT_CREATE: {
auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.label));
auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.property));
AddRecoveredIndexConstraint(&indices_constraints->constraints.existence, {label_id, property_id},
"The existence constraint already exists!");
break;
}
case WalDeltaData::Type::EXISTENCE_CONSTRAINT_DROP: {
auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.label));
auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.property));
RemoveRecoveredIndexConstraint(&indices_constraints->constraints.existence, {label_id, property_id},
"The existence constraint doesn't exist!");
break;
}
case WalDeltaData::Type::UNIQUE_CONSTRAINT_CREATE: {
auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_properties.label));
std::set<PropertyId> property_ids;
for (const auto &prop : delta.operation_label_properties.properties) {
property_ids.insert(PropertyId::FromUint(name_id_mapper->NameToId(prop)));
}
AddRecoveredIndexConstraint(&indices_constraints->constraints.unique, {label_id, property_ids},
"The unique constraint already exists!");
break;
}
case WalDeltaData::Type::UNIQUE_CONSTRAINT_DROP: {
auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_properties.label));
std::set<PropertyId> property_ids;
for (const auto &prop : delta.operation_label_properties.properties) {
property_ids.insert(PropertyId::FromUint(name_id_mapper->NameToId(prop)));
}
RemoveRecoveredIndexConstraint(&indices_constraints->constraints.unique, {label_id, property_ids},
"The unique constraint doesn't exist!");
break;
}
}
ret.next_timestamp = std::max(ret.next_timestamp, timestamp + 1);
++deltas_applied;
} else {
// This delta should be skipped.
SkipWalDeltaData(&wal);
}
}
// break;
// }
// case WalDeltaData::Type::EDGE_SET_PROPERTY: {
// if (!items.properties_on_edges)
// throw RecoveryFailure(
// "The WAL has properties on edges, but the storage is "
// "configured without properties on edges!");
// auto edge = edge_acc.find(delta.vertex_edge_set_property.gid);
// if (edge == edge_acc.end()) throw RecoveryFailure("The edge doesn't exist!");
// auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.vertex_edge_set_property.property));
// auto &property_value = delta.vertex_edge_set_property.value;
// edge->properties.SetProperty(property_id, property_value);
// break;
// }
// case WalDeltaData::Type::TRANSACTION_END:
// break;
// case WalDeltaData::Type::LABEL_INDEX_CREATE: {
// auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label.label));
// AddRecoveredIndexConstraint(&indices_constraints->indices.label, label_id, "The label index already
// exists!"); break;
// }
// case WalDeltaData::Type::LABEL_INDEX_DROP: {
// auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label.label));
// RemoveRecoveredIndexConstraint(&indices_constraints->indices.label, label_id,
// "The label index doesn't exist!");
// break;
// }
// case WalDeltaData::Type::LABEL_PROPERTY_INDEX_CREATE: {
// auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.label));
// auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.property));
// AddRecoveredIndexConstraint(&indices_constraints->indices.label_property, {label_id, property_id},
// "The label property index already exists!");
// break;
// }
// case WalDeltaData::Type::LABEL_PROPERTY_INDEX_DROP: {
// auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.label));
// auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.property));
// RemoveRecoveredIndexConstraint(&indices_constraints->indices.label_property, {label_id, property_id},
// "The label property index doesn't exist!");
// break;
// }
// case WalDeltaData::Type::EXISTENCE_CONSTRAINT_CREATE: {
// auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.label));
// auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.property));
// AddRecoveredIndexConstraint(&indices_constraints->constraints.existence, {label_id, property_id},
// "The existence constraint already exists!");
// break;
// }
// case WalDeltaData::Type::EXISTENCE_CONSTRAINT_DROP: {
// auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.label));
// auto property_id = PropertyId::FromUint(name_id_mapper->NameToId(delta.operation_label_property.property));
// RemoveRecoveredIndexConstraint(&indices_constraints->constraints.existence, {label_id, property_id},
// "The existence constraint doesn't exist!");
// break;
// }
// case WalDeltaData::Type::UNIQUE_CONSTRAINT_CREATE: {
// auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_properties.label));
// std::set<PropertyId> property_ids;
// for (const auto &prop : delta.operation_label_properties.properties) {
// property_ids.insert(PropertyId::FromUint(name_id_mapper->NameToId(prop)));
// }
// AddRecoveredIndexConstraint(&indices_constraints->constraints.unique, {label_id, property_ids},
// "The unique constraint already exists!");
// break;
// }
// case WalDeltaData::Type::UNIQUE_CONSTRAINT_DROP: {
// auto label_id = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_label_properties.label));
// std::set<PropertyId> property_ids;
// for (const auto &prop : delta.operation_label_properties.properties) {
// property_ids.insert(PropertyId::FromUint(name_id_mapper->NameToId(prop)));
// }
// RemoveRecoveredIndexConstraint(&indices_constraints->constraints.unique, {label_id, property_ids},
// "The unique constraint doesn't exist!");
// break;
// }
// }
// ret.next_timestamp = std::max(ret.next_timestamp, timestamp + 1);
// ++deltas_applied;
// } else {
// // This delta should be skipped.
// SkipWalDeltaData(&wal);
// }
// }
spdlog::info("Applied {} deltas from WAL. Skipped {} deltas, because they were too old.", deltas_applied,
info.num_deltas - deltas_applied);

View File

@ -11,6 +11,7 @@
#pragma once
#include <cstdint>
#include <functional>
#include <type_traits>

View File

@ -16,6 +16,7 @@
#include "storage/v3/mvcc.hpp"
#include "storage/v3/property_value.hpp"
#include "storage/v3/schema_validator.hpp"
#include "storage/v3/schemas.hpp"
#include "utils/bound.hpp"
#include "utils/logging.hpp"
#include "utils/memory_tracker.hpp"

View File

@ -219,12 +219,12 @@ class LabelPropertyIndex {
Iterable Vertices(LabelId label, PropertyId property, const std::optional<utils::Bound<PropertyValue>> &lower_bound,
const std::optional<utils::Bound<PropertyValue>> &upper_bound, View view, Transaction *transaction,
const SchemaValidator &schema_validator_) {
const SchemaValidator &schema_validator) {
auto it = index_.find({label, property});
MG_ASSERT(it != index_.end(), "Index for label {} and property {} doesn't exist", label.AsUint(),
property.AsUint());
return {it->second.access(), label, property, lower_bound, upper_bound, view,
transaction, indices_, constraints_, config_, schema_validator_};
return {it->second.access(), label, property, lower_bound, upper_bound, view,
transaction, indices_, constraints_, config_, schema_validator};
}
int64_t ApproximateVertexCount(LabelId label, PropertyId property) const {

View File

@ -18,7 +18,7 @@
namespace memgraph::storage::v3 {
KeyStore::KeyStore(const std::vector<PropertyValue> &key_values) {
KeyStore::KeyStore(const PrimaryKey &key_values) {
for (auto i = 0; i < key_values.size(); ++i) {
MG_ASSERT(!key_values[i].IsNull());
store_.SetProperty(PropertyId::FromInt(i), key_values[i]);
@ -27,9 +27,9 @@ KeyStore::KeyStore(const std::vector<PropertyValue> &key_values) {
PropertyValue KeyStore::GetKey(const size_t index) const { return store_.GetProperty(PropertyId::FromUint(index)); }
std::vector<PropertyValue> KeyStore::Keys() const {
PrimaryKey KeyStore::Keys() const {
auto keys_map = store_.Properties();
std::vector<PropertyValue> keys;
PrimaryKey keys;
keys.reserve(keys_map.size());
std::ranges::transform(
keys_map, std::back_inserter(keys),

View File

@ -20,9 +20,12 @@
namespace memgraph::storage::v3 {
// Primary key is a collection of primary properties.
using PrimaryKey = std::vector<PropertyValue>;
class KeyStore {
public:
explicit KeyStore(const std::vector<PropertyValue> &key_values);
explicit KeyStore(const PrimaryKey &key_values);
KeyStore(const KeyStore &) = delete;
KeyStore(KeyStore &&other) noexcept = default;
@ -33,7 +36,7 @@ class KeyStore {
PropertyValue GetKey(size_t index) const;
std::vector<PropertyValue> Keys() const;
PrimaryKey Keys() const;
friend bool operator<(const KeyStore &lhs, const KeyStore &rhs) {
// TODO(antaljanosbenjamin): also compare the schema
@ -44,14 +47,12 @@ class KeyStore {
return std::ranges::equal(lhs.Keys(), rhs.Keys());
}
friend bool operator<(const KeyStore &lhs, const std::vector<PropertyValue> &rhs) {
friend bool operator<(const KeyStore &lhs, const PrimaryKey &rhs) {
// TODO(antaljanosbenjamin): also compare the schema
return std::ranges::lexicographical_compare(lhs.Keys(), rhs, std::less<PropertyValue>{});
}
friend bool operator==(const KeyStore &lhs, const std::vector<PropertyValue> &rhs) {
return std::ranges::equal(lhs.Keys(), rhs);
}
friend bool operator==(const KeyStore &lhs, const PrimaryKey &rhs) { return std::ranges::equal(lhs.Keys(), rhs); }
private:
PropertyStore store_;

View File

@ -171,20 +171,20 @@ void Storage::ReplicationServer::SnapshotHandler(slk::Reader *req_reader, slk::B
spdlog::debug("Loading snapshot");
auto recovered_snapshot = durability::RecoveredSnapshot{};
durability::LoadSnapshot(*maybe_snapshot_path, &storage_->vertices_, &storage_->edges_, &storage_->epoch_history_,
&storage_->name_id_mapper_, &storage_->edge_count_, storage_->config_.items);
// durability::LoadSnapshot(*maybe_snapshot_path, &storage_->vertices_, &storage_->edges_,
// &storage_->epoch_history_,
// &storage_->name_id_mapper_, &storage_->edge_count_, storage_->config_.items);
spdlog::debug("Snapshot loaded successfully");
// If this step is present it should always be the first step of
// the recovery so we use the UUID we read from snasphost
storage_->uuid_ = std::move(recovered_snapshot.snapshot_info.uuid);
storage_->epoch_id_ = std::move(recovered_snapshot.snapshot_info.epoch_id);
const auto &recovery_info = recovered_snapshot.recovery_info;
storage_->vertex_id_ = recovery_info.next_vertex_id;
storage_->edge_id_ = recovery_info.next_edge_id;
storage_->timestamp_ = std::max(storage_->timestamp_, recovery_info.next_timestamp);
durability::RecoverIndicesAndConstraints(recovered_snapshot.indices_constraints, &storage_->indices_,
&storage_->constraints_, &storage_->vertices_);
// durability::RecoverIndicesAndConstraints(recovered_snapshot.indices_constraints, &storage_->indices_,
// &storage_->constraints_, &storage_->vertices_);
} catch (const durability::RecoveryFailure &e) {
LOG_FATAL("Couldn't load the snapshot because of: {}", e.what());
}
@ -295,7 +295,7 @@ Storage::ReplicationServer::~ReplicationServer() {
}
uint64_t Storage::ReplicationServer::ReadAndApplyDelta(durability::BaseDecoder *decoder) {
auto edge_acc = storage_->edges_.access();
auto vertex_acc = storage_->vertices_.access();
// auto vertex_acc = storage_->vertices_.access();
std::optional<std::pair<uint64_t, Storage::Accessor>> commit_timestamp_and_accessor;
auto get_transaction = [this, &commit_timestamp_and_accessor](uint64_t commit_timestamp) {
@ -322,250 +322,252 @@ uint64_t Storage::ReplicationServer::ReadAndApplyDelta(durability::BaseDecoder *
continue;
}
SPDLOG_INFO(" Delta {}", applied_deltas);
switch (delta.type) {
case durability::WalDeltaData::Type::VERTEX_CREATE: {
spdlog::trace(" Create vertex {}", delta.vertex_create_delete.gid.AsUint());
auto *transaction = get_transaction(timestamp);
transaction->CreateVertex(delta.vertex_create_delete.gid);
break;
}
case durability::WalDeltaData::Type::VERTEX_DELETE: {
spdlog::trace(" Delete vertex {}", delta.vertex_create_delete.gid.AsUint());
auto *transaction = get_transaction(timestamp);
auto vertex = transaction->FindVertex(delta.vertex_create_delete.gid, View::NEW);
if (!vertex) throw utils::BasicException("Invalid transaction!");
auto ret = transaction->DeleteVertex(&*vertex);
if (ret.HasError() || !ret.GetValue()) throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::VERTEX_ADD_LABEL: {
spdlog::trace(" Vertex {} add label {}", delta.vertex_add_remove_label.gid.AsUint(),
delta.vertex_add_remove_label.label);
auto *transaction = get_transaction(timestamp);
auto vertex = transaction->FindVertex(delta.vertex_add_remove_label.gid, View::NEW);
if (!vertex) throw utils::BasicException("Invalid transaction!");
auto ret = vertex->AddLabel(transaction->NameToLabel(delta.vertex_add_remove_label.label));
if (ret.HasError() || !ret.GetValue()) throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::VERTEX_REMOVE_LABEL: {
spdlog::trace(" Vertex {} remove label {}", delta.vertex_add_remove_label.gid.AsUint(),
delta.vertex_add_remove_label.label);
auto *transaction = get_transaction(timestamp);
auto vertex = transaction->FindVertex(delta.vertex_add_remove_label.gid, View::NEW);
if (!vertex) throw utils::BasicException("Invalid transaction!");
auto ret = vertex->RemoveLabel(transaction->NameToLabel(delta.vertex_add_remove_label.label));
if (ret.HasError() || !ret.GetValue()) throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::VERTEX_SET_PROPERTY: {
spdlog::trace(" Vertex {} set property {} to {}", delta.vertex_edge_set_property.gid.AsUint(),
delta.vertex_edge_set_property.property, delta.vertex_edge_set_property.value);
auto *transaction = get_transaction(timestamp);
auto vertex = transaction->FindVertex(delta.vertex_edge_set_property.gid, View::NEW);
if (!vertex) throw utils::BasicException("Invalid transaction!");
auto ret = vertex->SetProperty(transaction->NameToProperty(delta.vertex_edge_set_property.property),
delta.vertex_edge_set_property.value);
if (ret.HasError()) throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::EDGE_CREATE: {
spdlog::trace(" Create edge {} of type {} from vertex {} to vertex {}",
delta.edge_create_delete.gid.AsUint(), delta.edge_create_delete.edge_type,
delta.edge_create_delete.from_vertex.AsUint(), delta.edge_create_delete.to_vertex.AsUint());
auto *transaction = get_transaction(timestamp);
auto from_vertex = transaction->FindVertex(delta.edge_create_delete.from_vertex, View::NEW);
if (!from_vertex) throw utils::BasicException("Invalid transaction!");
auto to_vertex = transaction->FindVertex(delta.edge_create_delete.to_vertex, View::NEW);
if (!to_vertex) throw utils::BasicException("Invalid transaction!");
auto edge = transaction->CreateEdge(&*from_vertex, &*to_vertex,
transaction->NameToEdgeType(delta.edge_create_delete.edge_type),
delta.edge_create_delete.gid);
if (edge.HasError()) throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::EDGE_DELETE: {
spdlog::trace(" Delete edge {} of type {} from vertex {} to vertex {}",
delta.edge_create_delete.gid.AsUint(), delta.edge_create_delete.edge_type,
delta.edge_create_delete.from_vertex.AsUint(), delta.edge_create_delete.to_vertex.AsUint());
auto *transaction = get_transaction(timestamp);
auto from_vertex = transaction->FindVertex(delta.edge_create_delete.from_vertex, View::NEW);
if (!from_vertex) throw utils::BasicException("Invalid transaction!");
auto to_vertex = transaction->FindVertex(delta.edge_create_delete.to_vertex, View::NEW);
if (!to_vertex) throw utils::BasicException("Invalid transaction!");
auto edges = from_vertex->OutEdges(View::NEW, {transaction->NameToEdgeType(delta.edge_create_delete.edge_type)},
&*to_vertex);
if (edges.HasError()) throw utils::BasicException("Invalid transaction!");
if (edges->size() != 1) throw utils::BasicException("Invalid transaction!");
auto &edge = (*edges)[0];
auto ret = transaction->DeleteEdge(&edge);
if (ret.HasError()) throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::EDGE_SET_PROPERTY: {
spdlog::trace(" Edge {} set property {} to {}", delta.vertex_edge_set_property.gid.AsUint(),
delta.vertex_edge_set_property.property, delta.vertex_edge_set_property.value);
// SPDLOG_INFO(" Delta {}", applied_deltas);
// switch (delta.type) {
// case durability::WalDeltaData::Type::VERTEX_CREATE: {
// spdlog::trace(" Create vertex {}", delta.vertex_create_delete.gid.AsUint());
// auto *transaction = get_transaction(timestamp);
// transaction->CreateVertex(delta.vertex_create_delete.gid);
// break;
// }
// case durability::WalDeltaData::Type::VERTEX_DELETE: {
// spdlog::trace(" Delete vertex {}", delta.vertex_create_delete.gid.AsUint());
// auto *transaction = get_transaction(timestamp);
// auto vertex = transaction->FindVertex(delta.vertex_create_delete.gid, View::NEW);
// if (!vertex) throw utils::BasicException("Invalid transaction!");
// auto ret = transaction->DeleteVertex(&*vertex);
// if (ret.HasError() || !ret.GetValue()) throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::VERTEX_ADD_LABEL: {
// spdlog::trace(" Vertex {} add label {}", delta.vertex_add_remove_label.gid.AsUint(),
// delta.vertex_add_remove_label.label);
// auto *transaction = get_transaction(timestamp);
// auto vertex = transaction->FindVertex(delta.vertex_add_remove_label.gid, View::NEW);
// if (!vertex) throw utils::BasicException("Invalid transaction!");
// auto ret = vertex->AddLabel(transaction->NameToLabel(delta.vertex_add_remove_label.label));
// if (ret.HasError() || !ret.GetValue()) throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::VERTEX_REMOVE_LABEL: {
// spdlog::trace(" Vertex {} remove label {}", delta.vertex_add_remove_label.gid.AsUint(),
// delta.vertex_add_remove_label.label);
// auto *transaction = get_transaction(timestamp);
// auto vertex = transaction->FindVertex(delta.vertex_add_remove_label.gid, View::NEW);
// if (!vertex) throw utils::BasicException("Invalid transaction!");
// auto ret = vertex->RemoveLabel(transaction->NameToLabel(delta.vertex_add_remove_label.label));
// if (ret.HasError() || !ret.GetValue()) throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::VERTEX_SET_PROPERTY: {
// spdlog::trace(" Vertex {} set property {} to {}", delta.vertex_edge_set_property.gid.AsUint(),
// delta.vertex_edge_set_property.property, delta.vertex_edge_set_property.value);
// auto *transaction = get_transaction(timestamp);
// auto vertex = transaction->FindVertex(delta.vertex_edge_set_property.gid, View::NEW);
// if (!vertex) throw utils::BasicException("Invalid transaction!");
// auto ret = vertex->SetProperty(transaction->NameToProperty(delta.vertex_edge_set_property.property),
// delta.vertex_edge_set_property.value);
// if (ret.HasError()) throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::EDGE_CREATE: {
// spdlog::trace(" Create edge {} of type {} from vertex {} to vertex {}",
// delta.edge_create_delete.gid.AsUint(), delta.edge_create_delete.edge_type,
// delta.edge_create_delete.from_vertex.AsUint(), delta.edge_create_delete.to_vertex.AsUint());
// auto *transaction = get_transaction(timestamp);
// auto from_vertex = transaction->FindVertex(delta.edge_create_delete.from_vertex, View::NEW);
// if (!from_vertex) throw utils::BasicException("Invalid transaction!");
// auto to_vertex = transaction->FindVertex(delta.edge_create_delete.to_vertex, View::NEW);
// if (!to_vertex) throw utils::BasicException("Invalid transaction!");
// auto edge = transaction->CreateEdge(&*from_vertex, &*to_vertex,
// transaction->NameToEdgeType(delta.edge_create_delete.edge_type),
// delta.edge_create_delete.gid);
// if (edge.HasError()) throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::EDGE_DELETE: {
// spdlog::trace(" Delete edge {} of type {} from vertex {} to vertex {}",
// delta.edge_create_delete.gid.AsUint(), delta.edge_create_delete.edge_type,
// delta.edge_create_delete.from_vertex.AsUint(), delta.edge_create_delete.to_vertex.AsUint());
// auto *transaction = get_transaction(timestamp);
// auto from_vertex = transaction->FindVertex(delta.edge_create_delete.from_vertex, View::NEW);
// if (!from_vertex) throw utils::BasicException("Invalid transaction!");
// auto to_vertex = transaction->FindVertex(delta.edge_create_delete.to_vertex, View::NEW);
// if (!to_vertex) throw utils::BasicException("Invalid transaction!");
// auto edges = from_vertex->OutEdges(View::NEW,
// {transaction->NameToEdgeType(delta.edge_create_delete.edge_type)},
// &*to_vertex);
// if (edges.HasError()) throw utils::BasicException("Invalid transaction!");
// if (edges->size() != 1) throw utils::BasicException("Invalid transaction!");
// auto &edge = (*edges)[0];
// auto ret = transaction->DeleteEdge(&edge);
// if (ret.HasError()) throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::EDGE_SET_PROPERTY: {
// spdlog::trace(" Edge {} set property {} to {}", delta.vertex_edge_set_property.gid.AsUint(),
// delta.vertex_edge_set_property.property, delta.vertex_edge_set_property.value);
if (!storage_->config_.items.properties_on_edges)
throw utils::BasicException(
"Can't set properties on edges because properties on edges "
"are disabled!");
// if (!storage_->config_.items.properties_on_edges)
// throw utils::BasicException(
// "Can't set properties on edges because properties on edges "
// "are disabled!");
auto *transaction = get_transaction(timestamp);
// auto *transaction = get_transaction(timestamp);
// The following block of code effectively implements `FindEdge` and
// yields an accessor that is only valid for managing the edge's
// properties.
auto edge = edge_acc.find(delta.vertex_edge_set_property.gid);
if (edge == edge_acc.end()) throw utils::BasicException("Invalid transaction!");
// The edge visibility check must be done here manually because we
// don't allow direct access to the edges through the public API.
{
bool is_visible = true;
Delta *delta = nullptr;
{
std::lock_guard<utils::SpinLock> guard(edge->lock);
is_visible = !edge->deleted;
delta = edge->delta;
}
ApplyDeltasForRead(&transaction->transaction_, delta, View::NEW, [&is_visible](const Delta &delta) {
switch (delta.action) {
case Delta::Action::ADD_LABEL:
case Delta::Action::REMOVE_LABEL:
case Delta::Action::SET_PROPERTY:
case Delta::Action::ADD_IN_EDGE:
case Delta::Action::ADD_OUT_EDGE:
case Delta::Action::REMOVE_IN_EDGE:
case Delta::Action::REMOVE_OUT_EDGE:
break;
case Delta::Action::RECREATE_OBJECT: {
is_visible = true;
break;
}
case Delta::Action::DELETE_OBJECT: {
is_visible = false;
break;
}
}
});
if (!is_visible) throw utils::BasicException("Invalid transaction!");
}
EdgeRef edge_ref(&*edge);
// Here we create an edge accessor that we will use to get the
// properties of the edge. The accessor is created with an invalid
// type and invalid from/to pointers because we don't know them
// here, but that isn't an issue because we won't use that part of
// the API here.
auto ea = EdgeAccessor{edge_ref,
EdgeTypeId::FromUint(0UL),
nullptr,
nullptr,
&transaction->transaction_,
&storage_->indices_,
&storage_->constraints_,
storage_->config_.items,
storage_->schema_validator_};
// // The following block of code effectively implements `FindEdge` and
// // yields an accessor that is only valid for managing the edge's
// // properties.
// auto edge = edge_acc.find(delta.vertex_edge_set_property.gid);
// if (edge == edge_acc.end()) throw utils::BasicException("Invalid transaction!");
// // The edge visibility check must be done here manually because we
// // don't allow direct access to the edges through the public API.
// {
// bool is_visible = true;
// Delta *delta = nullptr;
// {
// std::lock_guard<utils::SpinLock> guard(edge->lock);
// is_visible = !edge->deleted;
// delta = edge->delta;
// }
// ApplyDeltasForRead(&transaction->transaction_, delta, View::NEW, [&is_visible](const Delta &delta) {
// switch (delta.action) {
// case Delta::Action::ADD_LABEL:
// case Delta::Action::REMOVE_LABEL:
// case Delta::Action::SET_PROPERTY:
// case Delta::Action::ADD_IN_EDGE:
// case Delta::Action::ADD_OUT_EDGE:
// case Delta::Action::REMOVE_IN_EDGE:
// case Delta::Action::REMOVE_OUT_EDGE:
// break;
// case Delta::Action::RECREATE_OBJECT: {
// is_visible = true;
// break;
// }
// case Delta::Action::DELETE_OBJECT: {
// is_visible = false;
// break;
// }
// }
// });
// if (!is_visible) throw utils::BasicException("Invalid transaction!");
// }
// EdgeRef edge_ref(&*edge);
// // Here we create an edge accessor that we will use to get the
// // properties of the edge. The accessor is created with an invalid
// // type and invalid from/to pointers because we don't know them
// // here, but that isn't an issue because we won't use that part of
// // the API here.
// auto ea = EdgeAccessor{edge_ref,
// EdgeTypeId::FromUint(0UL),
// nullptr,
// nullptr,
// &transaction->transaction_,
// &storage_->indices_,
// &storage_->constraints_,
// storage_->config_.items,
// storage_->schema_validator_,
// storage_->schemas_};
auto ret = ea.SetProperty(transaction->NameToProperty(delta.vertex_edge_set_property.property),
delta.vertex_edge_set_property.value);
if (ret.HasError()) throw utils::BasicException("Invalid transaction!");
break;
}
// auto ret = ea.SetProperty(transaction->NameToProperty(delta.vertex_edge_set_property.property),
// delta.vertex_edge_set_property.value);
// if (ret.HasError()) throw utils::BasicException("Invalid transaction!");
// break;
// }
case durability::WalDeltaData::Type::TRANSACTION_END: {
spdlog::trace(" Transaction end");
if (!commit_timestamp_and_accessor || commit_timestamp_and_accessor->first != timestamp)
throw utils::BasicException("Invalid data!");
auto ret = commit_timestamp_and_accessor->second.Commit(commit_timestamp_and_accessor->first);
if (ret.HasError()) throw utils::BasicException("Invalid transaction!");
commit_timestamp_and_accessor = std::nullopt;
break;
}
// case durability::WalDeltaData::Type::TRANSACTION_END: {
// spdlog::trace(" Transaction end");
// if (!commit_timestamp_and_accessor || commit_timestamp_and_accessor->first != timestamp)
// throw utils::BasicException("Invalid data!");
// auto ret = commit_timestamp_and_accessor->second.Commit(commit_timestamp_and_accessor->first);
// if (ret.HasError()) throw utils::BasicException("Invalid transaction!");
// commit_timestamp_and_accessor = std::nullopt;
// break;
// }
case durability::WalDeltaData::Type::LABEL_INDEX_CREATE: {
spdlog::trace(" Create label index on :{}", delta.operation_label.label);
// Need to send the timestamp
if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
if (!storage_->CreateIndex(storage_->NameToLabel(delta.operation_label.label), timestamp))
throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::LABEL_INDEX_DROP: {
spdlog::trace(" Drop label index on :{}", delta.operation_label.label);
if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
if (!storage_->DropIndex(storage_->NameToLabel(delta.operation_label.label), timestamp))
throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::LABEL_PROPERTY_INDEX_CREATE: {
spdlog::trace(" Create label+property index on :{} ({})", delta.operation_label_property.label,
delta.operation_label_property.property);
if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
if (!storage_->CreateIndex(storage_->NameToLabel(delta.operation_label_property.label),
storage_->NameToProperty(delta.operation_label_property.property), timestamp))
throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::LABEL_PROPERTY_INDEX_DROP: {
spdlog::trace(" Drop label+property index on :{} ({})", delta.operation_label_property.label,
delta.operation_label_property.property);
if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
if (!storage_->DropIndex(storage_->NameToLabel(delta.operation_label_property.label),
storage_->NameToProperty(delta.operation_label_property.property), timestamp))
throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::EXISTENCE_CONSTRAINT_CREATE: {
spdlog::trace(" Create existence constraint on :{} ({})", delta.operation_label_property.label,
delta.operation_label_property.property);
if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
auto ret = storage_->CreateExistenceConstraint(
storage_->NameToLabel(delta.operation_label_property.label),
storage_->NameToProperty(delta.operation_label_property.property), timestamp);
if (!ret.HasValue() || !ret.GetValue()) throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::EXISTENCE_CONSTRAINT_DROP: {
spdlog::trace(" Drop existence constraint on :{} ({})", delta.operation_label_property.label,
delta.operation_label_property.property);
if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
if (!storage_->DropExistenceConstraint(storage_->NameToLabel(delta.operation_label_property.label),
storage_->NameToProperty(delta.operation_label_property.property),
timestamp))
throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::UNIQUE_CONSTRAINT_CREATE: {
std::stringstream ss;
utils::PrintIterable(ss, delta.operation_label_properties.properties);
spdlog::trace(" Create unique constraint on :{} ({})", delta.operation_label_properties.label, ss.str());
if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
std::set<PropertyId> properties;
for (const auto &prop : delta.operation_label_properties.properties) {
properties.emplace(storage_->NameToProperty(prop));
}
auto ret = storage_->CreateUniqueConstraint(storage_->NameToLabel(delta.operation_label_properties.label),
properties, timestamp);
if (!ret.HasValue() || ret.GetValue() != UniqueConstraints::CreationStatus::SUCCESS)
throw utils::BasicException("Invalid transaction!");
break;
}
case durability::WalDeltaData::Type::UNIQUE_CONSTRAINT_DROP: {
std::stringstream ss;
utils::PrintIterable(ss, delta.operation_label_properties.properties);
spdlog::trace(" Drop unique constraint on :{} ({})", delta.operation_label_properties.label, ss.str());
if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
std::set<PropertyId> properties;
for (const auto &prop : delta.operation_label_properties.properties) {
properties.emplace(storage_->NameToProperty(prop));
}
auto ret = storage_->DropUniqueConstraint(storage_->NameToLabel(delta.operation_label_properties.label),
properties, timestamp);
if (ret != UniqueConstraints::DeletionStatus::SUCCESS) throw utils::BasicException("Invalid transaction!");
break;
}
}
// case durability::WalDeltaData::Type::LABEL_INDEX_CREATE: {
// spdlog::trace(" Create label index on :{}", delta.operation_label.label);
// // Need to send the timestamp
// if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
// if (!storage_->CreateIndex(storage_->NameToLabel(delta.operation_label.label), timestamp))
// throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::LABEL_INDEX_DROP: {
// spdlog::trace(" Drop label index on :{}", delta.operation_label.label);
// if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
// if (!storage_->DropIndex(storage_->NameToLabel(delta.operation_label.label), timestamp))
// throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::LABEL_PROPERTY_INDEX_CREATE: {
// spdlog::trace(" Create label+property index on :{} ({})", delta.operation_label_property.label,
// delta.operation_label_property.property);
// if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
// if (!storage_->CreateIndex(storage_->NameToLabel(delta.operation_label_property.label),
// storage_->NameToProperty(delta.operation_label_property.property), timestamp))
// throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::LABEL_PROPERTY_INDEX_DROP: {
// spdlog::trace(" Drop label+property index on :{} ({})", delta.operation_label_property.label,
// delta.operation_label_property.property);
// if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
// if (!storage_->DropIndex(storage_->NameToLabel(delta.operation_label_property.label),
// storage_->NameToProperty(delta.operation_label_property.property), timestamp))
// throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::EXISTENCE_CONSTRAINT_CREATE: {
// spdlog::trace(" Create existence constraint on :{} ({})", delta.operation_label_property.label,
// delta.operation_label_property.property);
// if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
// auto ret = storage_->CreateExistenceConstraint(
// storage_->NameToLabel(delta.operation_label_property.label),
// storage_->NameToProperty(delta.operation_label_property.property), timestamp);
// if (!ret.HasValue() || !ret.GetValue()) throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::EXISTENCE_CONSTRAINT_DROP: {
// spdlog::trace(" Drop existence constraint on :{} ({})", delta.operation_label_property.label,
// delta.operation_label_property.property);
// if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
// if (!storage_->DropExistenceConstraint(storage_->NameToLabel(delta.operation_label_property.label),
// storage_->NameToProperty(delta.operation_label_property.property),
// timestamp))
// throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::UNIQUE_CONSTRAINT_CREATE: {
// std::stringstream ss;
// utils::PrintIterable(ss, delta.operation_label_properties.properties);
// spdlog::trace(" Create unique constraint on :{} ({})", delta.operation_label_properties.label,
// ss.str()); if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
// std::set<PropertyId> properties;
// for (const auto &prop : delta.operation_label_properties.properties) {
// properties.emplace(storage_->NameToProperty(prop));
// }
// auto ret = storage_->CreateUniqueConstraint(storage_->NameToLabel(delta.operation_label_properties.label),
// properties, timestamp);
// if (!ret.HasValue() || ret.GetValue() != UniqueConstraints::CreationStatus::SUCCESS)
// throw utils::BasicException("Invalid transaction!");
// break;
// }
// case durability::WalDeltaData::Type::UNIQUE_CONSTRAINT_DROP: {
// std::stringstream ss;
// utils::PrintIterable(ss, delta.operation_label_properties.properties);
// spdlog::trace(" Drop unique constraint on :{} ({})", delta.operation_label_properties.label,
// ss.str()); if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid transaction!");
// std::set<PropertyId> properties;
// for (const auto &prop : delta.operation_label_properties.properties) {
// properties.emplace(storage_->NameToProperty(prop));
// }
// auto ret = storage_->DropUniqueConstraint(storage_->NameToLabel(delta.operation_label_properties.label),
// properties, timestamp);
// if (ret != UniqueConstraints::DeletionStatus::SUCCESS) throw utils::BasicException("Invalid transaction!");
// break;
// }
// }
}
if (commit_timestamp_and_accessor) throw utils::BasicException("Invalid data!");

View File

@ -98,7 +98,7 @@ SchemaValidator::SchemaValidator(Schemas &schemas) : schemas_{schemas} {}
[[nodiscard]] std::optional<SchemaViolation> SchemaValidator::ValidateLabelUpdate(const LabelId label) const {
const auto *schema = schemas_.GetSchema(label);
if (schema) {
return SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_MODIFY_PRIMARY_LABEL, label);
return SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, label);
}
return std::nullopt;
}

View File

@ -27,7 +27,7 @@ struct SchemaViolation {
NO_SCHEMA_DEFINED_FOR_LABEL,
VERTEX_PROPERTY_WRONG_TYPE,
VERTEX_UPDATE_PRIMARY_KEY,
VERTEX_MODIFY_PRIMARY_LABEL,
VERTEX_UPDATE_PRIMARY_LABEL,
VERTEX_SECONDARY_LABEL_IS_PRIMARY,
};

View File

@ -11,10 +11,12 @@
#include "storage/v3/schemas.hpp"
#include <algorithm>
#include <unordered_map>
#include <vector>
#include "storage/v3/property_value.hpp"
#include "utils/exceptions.hpp"
namespace memgraph::storage::v3 {
@ -47,6 +49,15 @@ bool Schemas::CreateSchema(const LabelId primary_label, const std::vector<Schema
bool Schemas::DropSchema(const LabelId primary_label) { return schemas_.erase(primary_label); }
bool Schemas::IsPropertyKey(const LabelId primary_label, const PropertyId property_id) const {
if (const auto schema = schemas_.find(primary_label); schema != schemas_.end()) {
return std::ranges::find_if(schema->second, [property_id](const auto &elem) {
return elem.property_id == property_id;
}) != schema->second.end();
}
throw utils::BasicException("Schema not found!");
}
std::optional<common::SchemaType> PropertyTypeToSchemaType(const PropertyValue &property_value) {
switch (property_value.type()) {
case PropertyValue::Type::Bool: {

View File

@ -53,11 +53,15 @@ class Schemas {
// Returns true if it was successfully created or false if the schema
// already exists
[[nodiscard]] bool CreateSchema(LabelId label, const std::vector<SchemaProperty> &schemas_types);
[[nodiscard]] bool CreateSchema(LabelId primary_label, const std::vector<SchemaProperty> &schemas_types);
// Returns true if it was successfully dropped or false if the schema
// does not exist
[[nodiscard]] bool DropSchema(LabelId label);
[[nodiscard]] bool DropSchema(LabelId primary_label);
// Returns true if property is part of schema defined
// by primary label
[[nodiscard]] bool IsPropertyKey(LabelId primary_label, PropertyId property_id) const;
private:
SchemasMap schemas_;

View File

@ -13,11 +13,14 @@
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <iterator>
#include <memory>
#include <mutex>
#include <optional>
#include <variant>
#include <bits/ranges_algo.h>
#include <gflags/gflags.h>
#include <spdlog/spdlog.h>
@ -31,14 +34,18 @@
#include "storage/v3/edge_accessor.hpp"
#include "storage/v3/id_types.hpp"
#include "storage/v3/indices.hpp"
#include "storage/v3/key_store.hpp"
#include "storage/v3/mvcc.hpp"
#include "storage/v3/property_value.hpp"
#include "storage/v3/replication/config.hpp"
#include "storage/v3/replication/replication_client.hpp"
#include "storage/v3/replication/replication_server.hpp"
#include "storage/v3/replication/rpc.hpp"
#include "storage/v3/schema_validator.hpp"
#include "storage/v3/transaction.hpp"
#include "storage/v3/vertex.hpp"
#include "storage/v3/vertex_accessor.hpp"
#include "storage/v3/vertices_skip_list.hpp"
#include "utils/exceptions.hpp"
#include "utils/file.hpp"
#include "utils/logging.hpp"
@ -46,6 +53,7 @@
#include "utils/message.hpp"
#include "utils/result.hpp"
#include "utils/rw_lock.hpp"
#include "utils/skip_list.hpp"
#include "utils/spin_lock.hpp"
#include "utils/stat.hpp"
#include "utils/uuid.hpp"
@ -56,6 +64,8 @@ using OOMExceptionEnabler = utils::MemoryTracker::OutOfMemoryExceptionEnabler;
namespace {
inline constexpr uint16_t kEpochHistoryRetention = 1000;
void InsertVertexPKIntoList(auto &container, const PrimaryKey &primary_key) { container.push_back(primary_key); }
} // namespace
auto AdvanceToVisibleVertex(VerticesSkipList::Iterator it, VerticesSkipList::Iterator end,
@ -75,7 +85,7 @@ auto AdvanceToVisibleVertex(VerticesSkipList::Iterator it, VerticesSkipList::Ite
AllVerticesIterable::Iterator::Iterator(AllVerticesIterable *self, VerticesSkipList::Iterator it)
: self_(self),
it_(AdvanceToVisibleVertex(it, self->vertices_accessor_.end(), &self->vertex_, self->transaction_, self->view_,
self->indices_, self_->constraints_, self->config_, *self->schema_validator_)) {}
self->indices_, self_->constraints_, self->config_, *self_->schema_validator_)) {}
VertexAccessor AllVerticesIterable::Iterator::operator*() const { return *self_->vertex_; }
@ -351,11 +361,10 @@ Storage::Storage(Config config)
if (config_.durability.recover_on_startup) {
auto info = std::optional<durability::RecoveryInfo>{};
durability::RecoverData(snapshot_directory_, wal_directory_, &uuid_, &epoch_id_, &epoch_history_, &vertices_,
&edges_, &edge_count_, &name_id_mapper_, &indices_, &constraints_, config_.items,
&wal_seq_num_);
// durability::RecoverData(snapshot_directory_, wal_directory_, &uuid_, &epoch_id_, &epoch_history_, &vertices_,
// &edges_, &edge_count_, &name_id_mapper_, &indices_, &constraints_, config_.items,
// &wal_seq_num_);
if (info) {
vertex_id_ = info->next_vertex_id;
edge_id_ = info->next_edge_id;
timestamp_ = std::max(timestamp_, info->next_timestamp);
if (info->last_commit_timestamp) {
@ -471,42 +480,6 @@ Storage::Accessor::~Accessor() {
FinalizeTransaction();
}
// TODO Remove when import csv is fixed
[[deprecated]] VertexAccessor Storage::Accessor::CreateVertex() {
OOMExceptionEnabler oom_exception;
auto gid = storage_->vertex_id_.fetch_add(1, std::memory_order_acq_rel);
auto acc = storage_->vertices_.access();
auto *delta = CreateDeleteObjectDelta(&transaction_);
// TODO(antaljanosbenjamin): handle keys and schema
auto [it, inserted] = acc.insert({Vertex{Gid::FromUint(gid), delta}});
MG_ASSERT(inserted, "The vertex must be inserted here!");
MG_ASSERT(it != acc.end(), "Invalid Vertex accessor!");
delta->prev.Set(&it->vertex);
return {
&it->vertex, &transaction_, &storage_->indices_, &storage_->constraints_, config_, storage_->schema_validator_};
}
// TODO Remove when replication is fixed
VertexAccessor Storage::Accessor::CreateVertex(Gid gid) {
OOMExceptionEnabler oom_exception;
// NOTE: When we update the next `vertex_id_` here we perform a RMW
// (read-modify-write) operation that ISN'T atomic! But, that isn't an issue
// because this function is only called from the replication delta applier
// that runs single-threadedly and while this instance is set-up to apply
// threads (it is the replica), it is guaranteed that no other writes are
// possible.
storage_->vertex_id_.store(std::max(storage_->vertex_id_.load(std::memory_order_acquire), gid.AsUint() + 1),
std::memory_order_release);
auto acc = storage_->vertices_.access();
auto *delta = CreateDeleteObjectDelta(&transaction_);
auto [it, inserted] = acc.insert({Vertex{gid, delta}});
MG_ASSERT(inserted, "The vertex must be inserted here!");
MG_ASSERT(it != acc.end(), "Invalid Vertex accessor!");
delta->prev.Set(&it->vertex);
return {
&it->vertex, &transaction_, &storage_->indices_, &storage_->constraints_, config_, storage_->schema_validator_};
}
ResultSchema<VertexAccessor> Storage::Accessor::CreateVertexAndValidate(
LabelId primary_label, const std::vector<LabelId> &labels,
const std::vector<std::pair<PropertyId, PropertyValue>> &properties) {
@ -515,36 +488,41 @@ ResultSchema<VertexAccessor> Storage::Accessor::CreateVertexAndValidate(
return {std::move(*maybe_schema_violation)};
}
OOMExceptionEnabler oom_exception;
auto gid = storage_->vertex_id_.fetch_add(1, std::memory_order_acq_rel);
// Extract key properties
std::vector<PropertyValue> primary_properties;
for ([[maybe_unused]] const auto &[property_id, property_type] : storage_->GetSchema(primary_label)->second) {
// We know there definitely is key in properties since we have validated
primary_properties.push_back(
std::ranges::find_if(properties, [property_id = property_id](const auto &property_pair) {
return property_pair.first == property_id;
})->second);
}
// Get secondary properties
std::vector<std::pair<PropertyId, PropertyValue>> secondary_properties;
for (const auto &[property_id, property_value] : properties) {
if (!storage_->schemas_.IsPropertyKey(primary_label, property_id)) {
secondary_properties.emplace_back(property_id, property_value);
}
}
auto acc = storage_->vertices_.access();
auto *delta = CreateDeleteObjectDelta(&transaction_);
auto [it, inserted] = acc.insert({Vertex{Gid::FromUint(gid), delta, primary_label}});
auto [it, inserted] = acc.insert({Vertex{delta, primary_label, primary_properties, labels, secondary_properties}});
MG_ASSERT(inserted, "The vertex must be inserted here!");
MG_ASSERT(it != acc.end(), "Invalid Vertex accessor!");
delta->prev.Set(&it->vertex);
auto va = VertexAccessor{
return VertexAccessor{
&it->vertex, &transaction_, &storage_->indices_, &storage_->constraints_, config_, storage_->schema_validator_};
for (const auto label : labels) {
const auto maybe_error = va.AddLabel(label);
if (maybe_error.HasError()) {
return {maybe_error.GetError()};
}
}
// Set properties
for (auto [property_id, property_value] : properties) {
const auto maybe_error = va.SetProperty(property_id, property_value);
if (maybe_error.HasError()) {
return {maybe_error.GetError()};
}
}
return va;
}
std::optional<VertexAccessor> Storage::Accessor::FindVertex(Gid gid, View view) {
std::optional<VertexAccessor> Storage::Accessor::FindVertex(std::vector<PropertyValue> primary_key, View view) {
auto acc = storage_->vertices_.access();
auto it = acc.find(std::vector{PropertyValue{gid.AsInt()}});
if (it == acc.end()) return std::nullopt;
// Later on use label space
auto it = acc.find(primary_key);
if (it == acc.end()) {
return std::nullopt;
}
return VertexAccessor::Create(&it->vertex, &transaction_, &storage_->indices_, &storage_->constraints_, config_,
storage_->schema_validator_, view);
}
@ -996,7 +974,7 @@ void Storage::Accessor::Abort() {
// We collect vertices and edges we've created here and then splice them into
// `deleted_vertices_` and `deleted_edges_` lists, instead of adding them one
// by one and acquiring lock every time.
std::list<Gid> my_deleted_vertices;
std::list<PrimaryKey> my_deleted_vertices;
std::list<Gid> my_deleted_edges;
for (const auto &delta : transaction_.deltas) {
@ -1072,7 +1050,7 @@ void Storage::Accessor::Abort() {
}
case Delta::Action::DELETE_OBJECT: {
vertex->deleted = true;
my_deleted_vertices.push_back(vertex->Gid());
InsertVertexPKIntoList(my_deleted_vertices, vertex->keys.Keys());
break;
}
case Delta::Action::RECREATE_OBJECT: {
@ -1187,7 +1165,8 @@ EdgeTypeId Storage::NameToEdgeType(const std::string_view name) {
bool Storage::CreateIndex(LabelId label, const std::optional<uint64_t> desired_commit_timestamp) {
std::unique_lock<utils::RWLock> storage_guard(main_lock_);
if (!indices_.label_index.CreateIndex(label, vertices_.access())) return false;
// TODO Fix Index
return false;
const auto commit_timestamp = CommitTimestamp(desired_commit_timestamp);
AppendToWal(durability::StorageGlobalOperation::LABEL_INDEX_CREATE, label, {}, commit_timestamp);
commit_log_->MarkFinished(commit_timestamp);
@ -1197,7 +1176,9 @@ bool Storage::CreateIndex(LabelId label, const std::optional<uint64_t> desired_c
bool Storage::CreateIndex(LabelId label, PropertyId property, const std::optional<uint64_t> desired_commit_timestamp) {
std::unique_lock<utils::RWLock> storage_guard(main_lock_);
if (!indices_.label_property_index.CreateIndex(label, property, vertices_.access())) return false;
// TODO Fix Index
// if (!indices_.label_property_index.CreateIndex(label, property, labelspace.access())) return false;
return false;
const auto commit_timestamp = CommitTimestamp(desired_commit_timestamp);
AppendToWal(durability::StorageGlobalOperation::LABEL_PROPERTY_INDEX_CREATE, label, {property}, commit_timestamp);
commit_log_->MarkFinished(commit_timestamp);
@ -1235,8 +1216,10 @@ IndicesInfo Storage::ListAllIndices() const {
utils::BasicResult<ConstraintViolation, bool> Storage::CreateExistenceConstraint(
LabelId label, PropertyId property, const std::optional<uint64_t> desired_commit_timestamp) {
std::unique_lock<utils::RWLock> storage_guard(main_lock_);
auto ret = ::memgraph::storage::v3::CreateExistenceConstraint(&constraints_, label, property, vertices_.access());
if (ret.HasError() || !ret.GetValue()) return ret;
// TODO Fix constraints
// auto ret = ::memgraph::storage::v3::CreateExistenceConstraint(&constraints_, label, property, vertices_.access());
// if (ret.HasError() || !ret.GetValue()) return ret;
return false;
const auto commit_timestamp = CommitTimestamp(desired_commit_timestamp);
AppendToWal(durability::StorageGlobalOperation::EXISTENCE_CONSTRAINT_CREATE, label, {property}, commit_timestamp);
commit_log_->MarkFinished(commit_timestamp);
@ -1258,10 +1241,12 @@ bool Storage::DropExistenceConstraint(LabelId label, PropertyId property,
utils::BasicResult<ConstraintViolation, UniqueConstraints::CreationStatus> Storage::CreateUniqueConstraint(
LabelId label, const std::set<PropertyId> &properties, const std::optional<uint64_t> desired_commit_timestamp) {
std::unique_lock<utils::RWLock> storage_guard(main_lock_);
auto ret = constraints_.unique_constraints.CreateConstraint(label, properties, vertices_.access());
if (ret.HasError() || ret.GetValue() != UniqueConstraints::CreationStatus::SUCCESS) {
return ret;
}
// TODO Fix constraints
// auto ret = constraints_.unique_constraints.CreateConstraint(label, properties, vertices_.access());
// if (ret.HasError() || ret.GetValue() != UniqueConstraints::CreationStatus::SUCCESS) {
// return ret;
// }
return UniqueConstraints::CreationStatus::ALREADY_EXISTS;
const auto commit_timestamp = CommitTimestamp(desired_commit_timestamp);
AppendToWal(durability::StorageGlobalOperation::UNIQUE_CONSTRAINT_CREATE, label, properties, commit_timestamp);
commit_log_->MarkFinished(commit_timestamp);
@ -1410,7 +1395,7 @@ void Storage::CollectGarbage() {
// will do it after cleaning-up the indices. That way we are sure that all
// vertices that appear in an index also exist in main storage.
std::list<Gid> current_deleted_edges;
std::list<Gid> current_deleted_vertices;
std::list<PrimaryKey> current_deleted_vertices;
deleted_vertices_->swap(current_deleted_vertices);
deleted_edges_->swap(current_deleted_edges);
@ -1482,7 +1467,7 @@ void Storage::CollectGarbage() {
}
vertex->delta = nullptr;
if (vertex->deleted) {
current_deleted_vertices.push_back(vertex->Gid());
InsertVertexPKIntoList(current_deleted_vertices, vertex->keys.Keys());
}
break;
}
@ -1600,17 +1585,13 @@ void Storage::CollectGarbage() {
if constexpr (force) {
// if force is set to true, then we have unique_lock and no transactions are active
// so we can clean all of the deleted vertices
std::vector<PropertyValue> key(1);
while (!garbage_vertices_.empty()) {
key.front() = PropertyValue{garbage_vertices_.front().second.AsInt()};
MG_ASSERT(vertex_acc.remove(key), "Invalid database state!");
MG_ASSERT(vertex_acc.remove(garbage_vertices_.front().second), "Invalid database state!");
garbage_vertices_.pop_front();
}
} else {
std::vector<PropertyValue> key(1);
while (!garbage_vertices_.empty() && garbage_vertices_.front().first < oldest_active_start_timestamp) {
key.front() = PropertyValue{garbage_vertices_.front().second.AsInt()};
MG_ASSERT(vertex_acc.remove(key), "Invalid database state!");
MG_ASSERT(vertex_acc.remove(garbage_vertices_.front().second), "Invalid database state!");
garbage_vertices_.pop_front();
}
}
@ -1867,10 +1848,10 @@ utils::BasicResult<Storage::CreateSnapshotError> Storage::CreateSnapshot() {
auto transaction = CreateTransaction(IsolationLevel::SNAPSHOT_ISOLATION);
// Create snapshot.
durability::CreateSnapshot(&transaction, snapshot_directory_, wal_directory_,
config_.durability.snapshot_retention_count, &vertices_, &edges_, &name_id_mapper_,
&indices_, &constraints_, config_.items, schema_validator_, uuid_, epoch_id_,
epoch_history_, &file_retainer_);
// durability::CreateSnapshot(&transaction, snapshot_directory_, wal_directory_,
// config_.durability.snapshot_retention_count, &vertices_, &edges_,
// &name_id_mapper_, &indices_, &constraints_, config_.items, schema_validator_,
// uuid_, epoch_id_, epoch_history_, &file_retainer_);
// Finalize snapshot transaction.
commit_log_->MarkFinished(transaction.start_timestamp);

View File

@ -12,7 +12,10 @@
#pragma once
#include <atomic>
#include <cstdint>
#include <filesystem>
#include <map>
#include <numeric>
#include <optional>
#include <shared_mutex>
#include <variant>
@ -30,6 +33,7 @@
#include "storage/v3/id_types.hpp"
#include "storage/v3/indices.hpp"
#include "storage/v3/isolation_level.hpp"
#include "storage/v3/key_store.hpp"
#include "storage/v3/lexicographically_ordered_vertex.hpp"
#include "storage/v3/mvcc.hpp"
#include "storage/v3/name_id_mapper.hpp"
@ -76,6 +80,7 @@ class AllVerticesIterable final {
Constraints *constraints_;
Config::Items config_;
const SchemaValidator *schema_validator_;
const Schemas *schemas_;
std::optional<VertexAccessor> vertex_;
public:
@ -97,14 +102,14 @@ class AllVerticesIterable final {
AllVerticesIterable(VerticesSkipList::Accessor vertices_accessor, Transaction *transaction, View view,
Indices *indices, Constraints *constraints, Config::Items config,
SchemaValidator *schema_validator)
const SchemaValidator &schema_validator)
: vertices_accessor_(std::move(vertices_accessor)),
transaction_(transaction),
view_(view),
indices_(indices),
constraints_(constraints),
config_(config),
schema_validator_(schema_validator) {}
schema_validator_{&schema_validator} {}
Iterator begin() { return {this, vertices_accessor_.begin()}; }
Iterator end() { return {this, vertices_accessor_.end()}; }
@ -226,21 +231,17 @@ class Storage final {
~Accessor();
VertexAccessor CreateVertex();
VertexAccessor CreateVertex(Gid gid);
/// @throw std::bad_alloc
ResultSchema<VertexAccessor> CreateVertexAndValidate(
LabelId primary_label, const std::vector<LabelId> &labels,
const std::vector<std::pair<PropertyId, PropertyValue>> &properties);
std::optional<VertexAccessor> FindVertex(Gid gid, View view);
std::optional<VertexAccessor> FindVertex(std::vector<PropertyValue> primary_key, View view);
VerticesIterable Vertices(View view) {
return VerticesIterable(AllVerticesIterable(storage_->vertices_.access(), &transaction_, view,
&storage_->indices_, &storage_->constraints_, storage_->config_.items,
&storage_->schema_validator_));
storage_->schema_validator_));
}
VerticesIterable Vertices(LabelId label, View view);
@ -520,7 +521,6 @@ class Storage final {
// Main object storage
VerticesSkipList vertices_;
utils::SkipList<Edge> edges_;
std::atomic<uint64_t> vertex_id_{0};
std::atomic<uint64_t> edge_id_{0};
// Even though the edge count is already kept in the `edges_` SkipList, the
// list is used only when properties are enabled for edges. Because of that we
@ -556,11 +556,11 @@ class Storage final {
// Vertices that are logically deleted but still have to be removed from
// indices before removing them from the main storage.
utils::Synchronized<std::list<Gid>, utils::SpinLock> deleted_vertices_;
utils::Synchronized<std::list<PrimaryKey>, utils::SpinLock> deleted_vertices_;
// Vertices that are logically deleted and removed from indices and now wait
// to be removed from the main storage.
std::list<std::pair<uint64_t, Gid>> garbage_vertices_;
std::list<std::pair<uint64_t, PrimaryKey>> garbage_vertices_;
// Edges that are logically deleted and wait to be removed from the main
// storage.

View File

@ -28,43 +28,49 @@
namespace memgraph::storage::v3 {
struct Vertex {
Vertex(Gid gid, Delta *delta, LabelId primary_label)
: primary_label{primary_label}, keys{{PropertyValue{gid.AsInt()}}}, deleted(false), delta(delta) {
Vertex(Delta *delta, LabelId primary_label, const std::vector<PropertyValue> &primary_properties)
: primary_label{primary_label}, keys{primary_properties}, delta{delta} {
MG_ASSERT(delta == nullptr || delta->action == Delta::Action::DELETE_OBJECT,
"Vertex must be created with an initial DELETE_OBJECT delta!");
}
// TODO remove this when import replication is solved
Vertex(Gid gid, LabelId primary_label)
: primary_label{primary_label}, keys{{PropertyValue{gid.AsInt()}}}, deleted(false) {
Vertex(Delta *delta, LabelId primary_label, const std::vector<PropertyValue> &primary_properties,
const std::vector<LabelId> &secondary_labels,
const std::vector<std::pair<PropertyId, PropertyValue>> &secondary_properties)
: primary_label{primary_label}, keys{primary_properties}, labels{secondary_labels}, delta{delta} {
MG_ASSERT(delta == nullptr || delta->action == Delta::Action::DELETE_OBJECT,
"Vertex must be created with an initial DELETE_OBJECT delta!");
for (const auto &[property_id, property_value] : secondary_properties) {
properties.SetProperty(property_id, property_value);
}
}
Vertex(LabelId primary_label, const std::vector<PropertyValue> &primary_properties)
: primary_label{primary_label}, keys(primary_properties) {
MG_ASSERT(delta == nullptr || delta->action == Delta::Action::DELETE_OBJECT,
"Vertex must be created with an initial DELETE_OBJECT delta!");
}
// TODO remove this when import csv is solved
Vertex(Gid gid, Delta *delta) : keys{{PropertyValue{gid.AsInt()}}}, deleted(false), delta(delta) {
Vertex(LabelId primary_label, const std::vector<PropertyValue> &primary_properties,
const std::vector<LabelId> &secondary_labels,
const std::vector<std::pair<PropertyId, PropertyValue>> &secondary_properties)
: primary_label{primary_label}, keys{primary_properties}, labels{secondary_labels} {
MG_ASSERT(delta == nullptr || delta->action == Delta::Action::DELETE_OBJECT,
"Vertex must be created with an initial DELETE_OBJECT delta!");
for (const auto &[property_id, property_value] : secondary_properties) {
properties.SetProperty(property_id, property_value);
}
}
// TODO remove this when import replication is solved
explicit Vertex(Gid gid) : keys{{PropertyValue{gid.AsInt()}}}, deleted(false) {
MG_ASSERT(delta == nullptr || delta->action == Delta::Action::DELETE_OBJECT,
"Vertex must be created with an initial DELETE_OBJECT delta!");
}
Gid Gid() const { return Gid::FromInt(keys.GetKey(0).ValueInt()); }
LabelId primary_label;
KeyStore keys;
std::vector<LabelId> labels;
PropertyStore properties;
std::vector<std::tuple<EdgeTypeId, Vertex *, EdgeRef>> in_edges;
std::vector<std::tuple<EdgeTypeId, Vertex *, EdgeRef>> out_edges;
mutable utils::SpinLock lock;
bool deleted;
bool deleted{false};
// uint8_t PAD;
// uint16_t PAD;

View File

@ -16,6 +16,7 @@
#include "storage/v3/edge_accessor.hpp"
#include "storage/v3/id_types.hpp"
#include "storage/v3/indices.hpp"
#include "storage/v3/key_store.hpp"
#include "storage/v3/mvcc.hpp"
#include "storage/v3/property_value.hpp"
#include "storage/v3/schema_validator.hpp"
@ -237,6 +238,44 @@ Result<LabelId> VertexAccessor::PrimaryLabel(const View view) const {
return vertex_->primary_label;
}
Result<PrimaryKey> VertexAccessor::PrimaryKey(const View view) const {
bool exists = true;
bool deleted = false;
Delta *delta = nullptr;
{
std::lock_guard<utils::SpinLock> guard(vertex_->lock);
deleted = vertex_->deleted;
delta = vertex_->delta;
}
ApplyDeltasForRead(transaction_, delta, view, [&exists, &deleted](const Delta &delta) {
switch (delta.action) {
case Delta::Action::DELETE_OBJECT: {
exists = false;
break;
}
case Delta::Action::RECREATE_OBJECT: {
deleted = false;
break;
}
case Delta::Action::ADD_LABEL:
case Delta::Action::REMOVE_LABEL:
case Delta::Action::SET_PROPERTY:
case Delta::Action::ADD_IN_EDGE:
case Delta::Action::ADD_OUT_EDGE:
case Delta::Action::REMOVE_IN_EDGE:
case Delta::Action::REMOVE_OUT_EDGE:
break;
}
});
if (!exists) {
return Error::NONEXISTENT_OBJECT;
}
if (!for_deleted_ && deleted) {
return Error::DELETED_OBJECT;
}
return vertex_->keys.Keys();
}
Result<std::vector<LabelId>> VertexAccessor::Labels(View view) const {
bool exists = true;
bool deleted = false;

View File

@ -13,13 +13,13 @@
#include <optional>
#include "storage/v3/id_types.hpp"
#include "storage/v3/schema_validator.hpp"
#include "storage/v3/vertex.hpp"
#include "storage/v3/config.hpp"
#include "storage/v3/id_types.hpp"
#include "storage/v3/key_store.hpp"
#include "storage/v3/result.hpp"
#include "storage/v3/schema_validator.hpp"
#include "storage/v3/transaction.hpp"
#include "storage/v3/vertex.hpp"
#include "storage/v3/view.hpp"
namespace memgraph::storage::v3 {
@ -68,22 +68,12 @@ class VertexAccessor final {
/// @return true if the object is visible from the current transaction
bool IsVisible(View view) const;
/// Add a label and return `true` if insertion took place.
/// `false` is returned if the label already existed.
/// @throw std::bad_alloc
Result<bool> AddLabel(LabelId label);
/// Add a label and return `true` if insertion took place.
/// `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
ResultSchema<bool> 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.
/// @throw std::bad_alloc
Result<bool> RemoveLabel(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.
@ -99,9 +89,7 @@ class VertexAccessor final {
Result<LabelId> PrimaryLabel(View view) const;
/// Set a property value and return the old value.
/// @throw std::bad_alloc
Result<PropertyValue> SetProperty(PropertyId property, const PropertyValue &value);
Result<PrimaryKey> PrimaryKey(View view) const;
/// Set a property value and return the old value or error.
/// @throw std::bad_alloc
@ -133,11 +121,6 @@ class VertexAccessor final {
Result<size_t> OutDegree(View view) const;
Gid Gid() const noexcept {
// TODO(antaljanosbenjamin): remove this whole function.
return vertex_->Gid();
}
const SchemaValidator *GetSchemaValidator() const;
bool operator==(const VertexAccessor &other) const noexcept {
@ -146,6 +129,20 @@ class VertexAccessor final {
bool operator!=(const VertexAccessor &other) const noexcept { return !(*this == other); }
private:
/// Add a label and return `true` if insertion took place.
/// `false` is returned if the label already existed.
/// @throw std::bad_alloc
Result<bool> 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<bool> RemoveLabel(LabelId label);
/// Set a property value and return the old value.
/// @throw std::bad_alloc
Result<PropertyValue> SetProperty(PropertyId property, const PropertyValue &value);
Vertex *vertex_;
Transaction *transaction_;
Indices *indices_;
@ -168,6 +165,6 @@ class VertexAccessor final {
namespace std {
template <>
struct hash<memgraph::storage::v3::VertexAccessor> {
size_t operator()(const memgraph::storage::v3::VertexAccessor &v) const noexcept { return v.Gid().AsUint(); }
size_t operator()(const memgraph::storage::v3::VertexAccessor & /*v*/) const noexcept { return 0; }
};
} // namespace std

View File

@ -334,34 +334,36 @@ target_link_libraries(${test_prefix}storage_v3_property_store mg-storage-v3 fmt)
add_unit_test(storage_v3_key_store.cpp)
target_link_libraries(${test_prefix}storage_v3_key_store mg-storage-v3 rapidcheck rapidcheck_gtest)
add_unit_test(storage_v3_vertex_accessors.cpp)
target_link_libraries(${test_prefix}storage_v3_vertex_accessors mg-storage-v3)
# Test mg-query-v3
add_unit_test(query_v2_interpreter.cpp ${CMAKE_SOURCE_DIR}/src/glue/v2/communication.cpp)
target_link_libraries(${test_prefix}query_v2_interpreter mg-storage-v3 mg-query-v2 mg-communication)
add_unit_test(query_v2_query_plan_accumulate_aggregate.cpp)
target_link_libraries(${test_prefix}query_v2_query_plan_accumulate_aggregate mg-query-v2)
# add_unit_test(query_v2_query_plan_accumulate_aggregate.cpp)
# target_link_libraries(${test_prefix}query_v2_query_plan_accumulate_aggregate mg-query-v2)
add_unit_test(query_v2_query_plan_create_set_remove_delete.cpp)
target_link_libraries(${test_prefix}query_v2_query_plan_create_set_remove_delete mg-query-v2)
# add_unit_test(query_v2_query_plan_create_set_remove_delete.cpp)
# target_link_libraries(${test_prefix}query_v2_query_plan_create_set_remove_delete mg-query-v2)
add_unit_test(query_v2_query_plan_bag_semantics.cpp)
target_link_libraries(${test_prefix}query_v2_query_plan_bag_semantics mg-query-v2)
# add_unit_test(query_v2_query_plan_bag_semantics.cpp)
# target_link_libraries(${test_prefix}query_v2_query_plan_bag_semantics mg-query-v2)
add_unit_test(query_v2_query_plan_edge_cases.cpp ${CMAKE_SOURCE_DIR}/src/glue/v2/communication.cpp)
target_link_libraries(${test_prefix}query_v2_query_plan_edge_cases mg-communication mg-query-v2)
# add_unit_test(query_v2_query_plan_edge_cases.cpp ${CMAKE_SOURCE_DIR}/src/glue/v2/communication.cpp)
# target_link_libraries(${test_prefix}query_v2_query_plan_edge_cases mg-communication mg-query-v2)
add_unit_test(query_v2_query_plan_v2_create_set_remove_delete.cpp)
target_link_libraries(${test_prefix}query_v2_query_plan_v2_create_set_remove_delete mg-query-v2)
# add_unit_test(query_v2_query_plan_v2_create_set_remove_delete.cpp)
# target_link_libraries(${test_prefix}query_v2_query_plan_v2_create_set_remove_delete mg-query-v2)
add_unit_test(query_v2_query_plan_match_filter_return.cpp)
target_link_libraries(${test_prefix}query_v2_query_plan_match_filter_return mg-query-v2)
# add_unit_test(query_v2_query_plan_match_filter_return.cpp)
# target_link_libraries(${test_prefix}query_v2_query_plan_match_filter_return mg-query-v2)
add_unit_test(query_v2_cypher_main_visitor.cpp)
target_link_libraries(${test_prefix}query_v2_cypher_main_visitor mg-query-v2)
add_unit_test(query_v2_query_required_privileges.cpp)
target_link_libraries(${test_prefix}query_v2_query_required_privileges mg-query-v2)
# add_unit_test(query_v2_cypher_main_visitor.cpp)
# target_link_libraries(${test_prefix}query_v2_cypher_main_visitor mg-query-v2)
# add_unit_test(query_v2_query_required_privileges.cpp)
# target_link_libraries(${test_prefix}query_v2_query_required_privileges mg-query-v2)
add_unit_test(replication_persistence_helper.cpp)
target_link_libraries(${test_prefix}replication_persistence_helper mg-storage-v2)

File diff suppressed because it is too large Load Diff

View File

@ -94,7 +94,7 @@ TEST_F(QueryPlanCRUDTest, ScanAll) {
auto dba = db.Access();
for (int i = 0; i < 42; ++i) {
auto v = *dba.CreateVertexAndValidate(label, {}, {{property, storage::v3::PropertyValue(i)}});
ASSERT_TRUE(v.SetProperty(property, storage::v3::PropertyValue(i)).HasValue());
ASSERT_TRUE(v.SetPropertyAndValidate(property, storage::v3::PropertyValue(i)).HasValue());
}
EXPECT_FALSE(dba.Commit().HasError());
}
@ -120,13 +120,13 @@ TEST_F(QueryPlanCRUDTest, ScanAllByLabel) {
// Add some unlabeled vertices
for (int i = 0; i < 12; ++i) {
auto v = *dba.CreateVertexAndValidate(label, {}, {{property, storage::v3::PropertyValue(i)}});
ASSERT_TRUE(v.SetProperty(property, storage::v3::PropertyValue(i)).HasValue());
ASSERT_TRUE(v.SetPropertyAndValidate(property, storage::v3::PropertyValue(i)).HasValue());
}
// Add labeled vertices
for (int i = 0; i < 42; ++i) {
auto v = *dba.CreateVertexAndValidate(label, {}, {{property, storage::v3::PropertyValue(i)}});
ASSERT_TRUE(v.SetProperty(property, storage::v3::PropertyValue(i)).HasValue());
ASSERT_TRUE(v.AddLabel(label2).HasValue());
ASSERT_TRUE(v.SetPropertyAndValidate(property, storage::v3::PropertyValue(i)).HasValue());
ASSERT_TRUE(v.AddLabelAndValidate(label2).HasValue());
}
EXPECT_FALSE(dba.Commit().HasError());
}

File diff suppressed because it is too large Load Diff

View File

@ -281,13 +281,13 @@ TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdateLabel) {
const auto schema_violation = schema_validator.ValidateLabelUpdate(label1);
ASSERT_NE(schema_violation, std::nullopt);
EXPECT_EQ(*schema_violation,
SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_MODIFY_PRIMARY_LABEL, label1));
SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, label1));
}
{
const auto schema_violation = schema_validator.ValidateLabelUpdate(label2);
ASSERT_NE(schema_violation, std::nullopt);
EXPECT_EQ(*schema_violation,
SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_MODIFY_PRIMARY_LABEL, label2));
SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, label2));
}
EXPECT_EQ(schema_validator.ValidateLabelUpdate(NameToLabel("test")), std::nullopt);
}

View File

@ -0,0 +1,209 @@
// Copyright 2022 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <limits>
#include <variant>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "common/types.hpp"
#include "storage/v3/delta.hpp"
#include "storage/v3/id_types.hpp"
#include "storage/v3/property_value.hpp"
#include "storage/v3/result.hpp"
#include "storage/v3/schema_validator.hpp"
#include "storage/v3/storage.hpp"
#include "storage/v3/vertex_accessor.hpp"
#include "storage_v3_test_utils.hpp"
using testing::UnorderedElementsAre;
namespace memgraph::storage::v3::tests {
class StorageV3Accessor : public ::testing::Test {
protected:
void SetUp() override {
ASSERT_TRUE(storage.CreateSchema(primary_label, {SchemaProperty{primary_property, common::SchemaType::INT}}));
}
VertexAccessor CreateVertexAndValidate(Storage::Accessor &acc, LabelId primary_label,
const std::vector<LabelId> &labels,
const std::vector<std::pair<PropertyId, PropertyValue>> &properties) {
auto vtx = acc.CreateVertexAndValidate(primary_label, labels, properties);
EXPECT_TRUE(vtx.HasValue());
return *vtx;
}
Storage storage;
const LabelId primary_label{storage.NameToLabel("label")};
const PropertyId primary_property{storage.NameToProperty("property")};
};
TEST_F(StorageV3Accessor, TestPrimaryLabel) {
{
auto acc = storage.Access();
const auto vertex = CreateVertexAndValidate(acc, primary_label, {}, {{primary_property, PropertyValue(0)}});
ASSERT_TRUE(vertex.PrimaryLabel(View::NEW).HasValue());
const auto vertex_primary_label = vertex.PrimaryLabel(View::NEW).GetValue();
ASSERT_FALSE(vertex.PrimaryLabel(View::OLD).HasValue());
EXPECT_EQ(vertex_primary_label, primary_label);
}
{
auto acc = storage.Access();
const auto vertex = CreateVertexAndValidate(acc, primary_label, {}, {{primary_property, PropertyValue(1)}});
ASSERT_TRUE(vertex.PrimaryLabel(View::OLD).HasError());
const auto error_primary_label = vertex.PrimaryLabel(View::OLD).GetError();
ASSERT_FALSE(vertex.PrimaryLabel(View::NEW).HasError());
EXPECT_EQ(error_primary_label, Error::NONEXISTENT_OBJECT);
}
{
auto acc = storage.Access();
CreateVertexAndValidate(acc, primary_label, {}, {{primary_property, PropertyValue(2)}});
ASSERT_FALSE(acc.Commit().HasError());
}
{
auto acc = storage.Access();
const auto vertex = acc.FindVertex({PropertyValue{2}}, View::OLD);
ASSERT_TRUE(vertex.has_value());
ASSERT_TRUE(acc.FindVertex({PropertyValue{2}}, View::NEW).has_value());
ASSERT_TRUE(vertex->PrimaryLabel(View::NEW).HasValue());
ASSERT_TRUE(vertex->PrimaryLabel(View::OLD).HasValue());
const auto vertex_primary_label = vertex->PrimaryLabel(View::NEW).GetValue();
EXPECT_EQ(vertex_primary_label, primary_label);
}
}
TEST_F(StorageV3Accessor, TestAddLabels) {
{
auto acc = storage.Access();
const auto label1 = storage.NameToLabel("label1");
const auto label2 = storage.NameToLabel("label2");
const auto label3 = storage.NameToLabel("label3");
const auto vertex =
CreateVertexAndValidate(acc, primary_label, {label1, label2, label3}, {{primary_property, PropertyValue(0)}});
ASSERT_TRUE(vertex.Labels(View::NEW).HasValue());
ASSERT_FALSE(vertex.Labels(View::OLD).HasValue());
EXPECT_THAT(vertex.Labels(View::NEW).GetValue(), UnorderedElementsAre(label1, label2, label3));
}
{
auto acc = storage.Access();
const auto label1 = storage.NameToLabel("label1");
const auto label2 = storage.NameToLabel("label2");
const auto label3 = storage.NameToLabel("label3");
auto vertex = CreateVertexAndValidate(acc, primary_label, {label1}, {{primary_property, PropertyValue(1)}});
ASSERT_TRUE(vertex.Labels(View::NEW).HasValue());
ASSERT_FALSE(vertex.Labels(View::OLD).HasValue());
EXPECT_THAT(vertex.Labels(View::NEW).GetValue(), UnorderedElementsAre(label1));
EXPECT_TRUE(vertex.AddLabelAndValidate(label2).HasValue());
EXPECT_TRUE(vertex.AddLabelAndValidate(label3).HasValue());
ASSERT_TRUE(vertex.Labels(View::NEW).HasValue());
ASSERT_FALSE(vertex.Labels(View::OLD).HasValue());
EXPECT_THAT(vertex.Labels(View::NEW).GetValue(), UnorderedElementsAre(label1, label2, label3));
}
{
auto acc = storage.Access();
const auto label1 = storage.NameToLabel("label");
auto vertex = acc.CreateVertexAndValidate(primary_label, {label1}, {{primary_property, PropertyValue(2)}});
ASSERT_TRUE(vertex.HasError());
ASSERT_TRUE(std::holds_alternative<SchemaViolation>(vertex.GetError()));
EXPECT_EQ(std::get<SchemaViolation>(vertex.GetError()),
SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_SECONDARY_LABEL_IS_PRIMARY, label1));
}
{
auto acc = storage.Access();
const auto label1 = storage.NameToLabel("label");
auto vertex = acc.CreateVertexAndValidate(primary_label, {}, {{primary_property, PropertyValue(3)}});
ASSERT_TRUE(vertex.HasValue());
const auto schema_violation = vertex->AddLabelAndValidate(label1);
ASSERT_TRUE(schema_violation.HasError());
ASSERT_TRUE(std::holds_alternative<SchemaViolation>(schema_violation.GetError()));
EXPECT_EQ(std::get<SchemaViolation>(schema_violation.GetError()),
SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, label1));
}
}
TEST_F(StorageV3Accessor, TestRemoveLabels) {
{
auto acc = storage.Access();
const auto label1 = storage.NameToLabel("label1");
const auto label2 = storage.NameToLabel("label2");
const auto label3 = storage.NameToLabel("label3");
auto vertex =
CreateVertexAndValidate(acc, primary_label, {label1, label2, label3}, {{primary_property, PropertyValue(0)}});
ASSERT_TRUE(vertex.Labels(View::NEW).HasValue());
EXPECT_THAT(vertex.Labels(View::NEW).GetValue(), UnorderedElementsAre(label1, label2, label3));
const auto res1 = vertex.RemoveLabelAndValidate(label2);
ASSERT_TRUE(res1.HasValue());
EXPECT_TRUE(res1.GetValue());
EXPECT_THAT(vertex.Labels(View::NEW).GetValue(), UnorderedElementsAre(label1, label3));
const auto res2 = vertex.RemoveLabelAndValidate(label1);
ASSERT_TRUE(res2.HasValue());
EXPECT_TRUE(res2.GetValue());
EXPECT_THAT(vertex.Labels(View::NEW).GetValue(), UnorderedElementsAre(label3));
const auto res3 = vertex.RemoveLabelAndValidate(label3);
ASSERT_TRUE(res3.HasValue());
ASSERT_TRUE(res3.HasValue());
EXPECT_TRUE(res3.GetValue());
EXPECT_TRUE(vertex.Labels(View::NEW).GetValue().empty());
}
{
auto acc = storage.Access();
const auto label1 = storage.NameToLabel("label1");
auto vertex = CreateVertexAndValidate(acc, primary_label, {}, {{primary_property, PropertyValue(1)}});
ASSERT_TRUE(vertex.Labels(View::NEW).HasValue());
EXPECT_TRUE(vertex.Labels(View::NEW).GetValue().empty());
const auto res1 = vertex.RemoveLabelAndValidate(label1);
ASSERT_TRUE(res1.HasValue());
EXPECT_FALSE(res1.GetValue());
}
{
auto acc = storage.Access();
auto vertex = CreateVertexAndValidate(acc, primary_label, {}, {{primary_property, PropertyValue(2)}});
const auto res1 = vertex.RemoveLabelAndValidate(primary_label);
ASSERT_TRUE(res1.HasError());
ASSERT_TRUE(std::holds_alternative<SchemaViolation>(res1.GetError()));
EXPECT_EQ(std::get<SchemaViolation>(res1.GetError()),
SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, primary_label));
}
}
TEST_F(StorageV3Accessor, TestSetKeysAndProperties) {
{
auto acc = storage.Access();
const PropertyId prop1{storage.NameToProperty("prop1")};
auto vertex = CreateVertexAndValidate(acc, primary_label, {}, {{primary_property, PropertyValue(0)}});
const auto res = vertex.SetPropertyAndValidate(prop1, PropertyValue(1));
ASSERT_TRUE(res.HasValue());
}
{
auto acc = storage.Access();
auto vertex = CreateVertexAndValidate(acc, primary_label, {}, {{primary_property, PropertyValue(1)}});
const auto res = vertex.SetPropertyAndValidate(primary_property, PropertyValue(1));
ASSERT_TRUE(res.HasError());
ASSERT_TRUE(std::holds_alternative<SchemaViolation>(res.GetError()));
EXPECT_EQ(std::get<SchemaViolation>(res.GetError()),
SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_KEY, primary_label,
SchemaProperty{primary_property, common::SchemaType::INT}));
}
{
auto acc = storage.Access();
auto vertex = CreateVertexAndValidate(acc, primary_label, {}, {{primary_property, PropertyValue(2)}});
const auto res = vertex.SetPropertyAndValidate(primary_property, PropertyValue());
ASSERT_TRUE(res.HasError());
ASSERT_TRUE(std::holds_alternative<SchemaViolation>(res.GetError()));
EXPECT_EQ(std::get<SchemaViolation>(res.GetError()),
SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_KEY, primary_label,
SchemaProperty{primary_property, common::SchemaType::INT}));
}
}
} // namespace memgraph::storage::v3::tests