Merge branch 'project-pineapples' into T1083-MG-limit-and-order-expand-one_v3
This commit is contained in:
commit
e77843f2ec
@ -452,10 +452,12 @@ struct ExpandOneResponse {
|
|||||||
std::vector<ExpandOneResultRow> result;
|
std::vector<ExpandOneResultRow> result;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct UpdateVertexProp {
|
struct UpdateVertex {
|
||||||
PrimaryKey primary_key;
|
PrimaryKey primary_key;
|
||||||
// This should be a map
|
// Labels are first added and then removed from vertices
|
||||||
std::vector<std::pair<PropertyId, Value>> property_updates;
|
std::vector<LabelId> add_labels;
|
||||||
|
std::vector<LabelId> remove_labels;
|
||||||
|
std::map<PropertyId, Value> property_updates;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct UpdateEdgeProp {
|
struct UpdateEdgeProp {
|
||||||
@ -498,7 +500,7 @@ struct DeleteVerticesResponse {
|
|||||||
|
|
||||||
struct UpdateVerticesRequest {
|
struct UpdateVerticesRequest {
|
||||||
Hlc transaction_id;
|
Hlc transaction_id;
|
||||||
std::vector<UpdateVertexProp> new_properties;
|
std::vector<UpdateVertex> update_vertices;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct UpdateVerticesResponse {
|
struct UpdateVerticesResponse {
|
||||||
|
@ -522,4 +522,38 @@ std::vector<Element<EdgeAccessor>> OrderByEdges(DbAccessor &dba, std::vector<Edg
|
|||||||
return ordered;
|
return ordered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LogResultError(const ResultErrorType &error, const std::string_view action) {
|
||||||
|
std::visit(
|
||||||
|
[action]<typename T>(T &&error) {
|
||||||
|
using ErrorType = std::remove_cvref_t<T>;
|
||||||
|
if constexpr (std::is_same_v<ErrorType, SchemaViolation>) {
|
||||||
|
spdlog::debug("{} failed with error: SchemaViolation", action);
|
||||||
|
} else if constexpr (std::is_same_v<ErrorType, Error>) {
|
||||||
|
switch (error) {
|
||||||
|
case Error::DELETED_OBJECT:
|
||||||
|
spdlog::debug("{} failed with error: DELETED_OBJECT", action);
|
||||||
|
break;
|
||||||
|
case Error::NONEXISTENT_OBJECT:
|
||||||
|
spdlog::debug("{} failed with error: NONEXISTENT_OBJECT", action);
|
||||||
|
break;
|
||||||
|
case Error::SERIALIZATION_ERROR:
|
||||||
|
spdlog::debug("{} failed with error: SERIALIZATION_ERROR", action);
|
||||||
|
break;
|
||||||
|
case Error::PROPERTIES_DISABLED:
|
||||||
|
spdlog::debug("{} failed with error: PROPERTIES_DISABLED", action);
|
||||||
|
break;
|
||||||
|
case Error::VERTEX_HAS_EDGES:
|
||||||
|
spdlog::debug("{} failed with error: VERTEX_HAS_EDGES", action);
|
||||||
|
break;
|
||||||
|
case Error::VERTEX_ALREADY_INSERTED:
|
||||||
|
spdlog::debug("{} failed with error: VERTEX_ALREADY_INSERTED", action);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
static_assert(utils::kAlwaysFalse<T>, "Missing type from variant visitor");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace memgraph::storage::v3
|
} // namespace memgraph::storage::v3
|
||||||
|
@ -21,6 +21,8 @@
|
|||||||
#include "storage/v3/expr.hpp"
|
#include "storage/v3/expr.hpp"
|
||||||
#include "storage/v3/shard.hpp"
|
#include "storage/v3/shard.hpp"
|
||||||
#include "storage/v3/vertex_accessor.hpp"
|
#include "storage/v3/vertex_accessor.hpp"
|
||||||
|
#include "utils/template_utils.hpp"
|
||||||
|
|
||||||
namespace memgraph::storage::v3 {
|
namespace memgraph::storage::v3 {
|
||||||
using EdgeAccessors = std::vector<storage::v3::EdgeAccessor>;
|
using EdgeAccessors = std::vector<storage::v3::EdgeAccessor>;
|
||||||
using EdgeUniqunessFunction = std::function<EdgeAccessors(EdgeAccessors &&, msgs::EdgeDirection)>;
|
using EdgeUniqunessFunction = std::function<EdgeAccessors(EdgeAccessors &&, msgs::EdgeDirection)>;
|
||||||
@ -160,6 +162,8 @@ std::vector<Element<VertexAccessor>> OrderByVertices(DbAccessor &dba, TIterable
|
|||||||
return ordered;
|
return ordered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LogResultError(const ResultErrorType &error, std::string_view action);
|
||||||
|
|
||||||
std::vector<Element<EdgeAccessor>> OrderByEdges(DbAccessor &dba, std::vector<EdgeAccessor> &iterable,
|
std::vector<Element<EdgeAccessor>> OrderByEdges(DbAccessor &dba, std::vector<EdgeAccessor> &iterable,
|
||||||
std::vector<msgs::OrderBy> &order_by_edges,
|
std::vector<msgs::OrderBy> &order_by_edges,
|
||||||
const VertexAccessor &vertex_acc);
|
const VertexAccessor &vertex_acc);
|
||||||
|
@ -77,42 +77,10 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) {
|
|||||||
PrimaryKey transformed_pk;
|
PrimaryKey transformed_pk;
|
||||||
std::transform(new_vertex.primary_key.begin(), new_vertex.primary_key.end(), std::back_inserter(transformed_pk),
|
std::transform(new_vertex.primary_key.begin(), new_vertex.primary_key.end(), std::back_inserter(transformed_pk),
|
||||||
[](msgs::Value &val) { return ToPropertyValue(std::move(val)); });
|
[](msgs::Value &val) { return ToPropertyValue(std::move(val)); });
|
||||||
auto result_schema = acc.CreateVertexAndValidate(converted_label_ids, transformed_pk, converted_property_map);
|
|
||||||
|
|
||||||
if (result_schema.HasError()) {
|
if (auto result_schema = acc.CreateVertexAndValidate(converted_label_ids, transformed_pk, converted_property_map);
|
||||||
auto &error = result_schema.GetError();
|
result_schema.HasError()) {
|
||||||
|
LogResultError(result_schema.GetError(), "Creating Vertex");
|
||||||
std::visit(
|
|
||||||
[]<typename T>(T &&error) {
|
|
||||||
using ErrorType = std::remove_cvref_t<T>;
|
|
||||||
if constexpr (std::is_same_v<ErrorType, SchemaViolation>) {
|
|
||||||
spdlog::debug("Creating vertex failed with error: SchemaViolation");
|
|
||||||
} else if constexpr (std::is_same_v<ErrorType, Error>) {
|
|
||||||
switch (error) {
|
|
||||||
case Error::DELETED_OBJECT:
|
|
||||||
spdlog::debug("Creating vertex failed with error: DELETED_OBJECT");
|
|
||||||
break;
|
|
||||||
case Error::NONEXISTENT_OBJECT:
|
|
||||||
spdlog::debug("Creating vertex failed with error: NONEXISTENT_OBJECT");
|
|
||||||
break;
|
|
||||||
case Error::SERIALIZATION_ERROR:
|
|
||||||
spdlog::debug("Creating vertex failed with error: SERIALIZATION_ERROR");
|
|
||||||
break;
|
|
||||||
case Error::PROPERTIES_DISABLED:
|
|
||||||
spdlog::debug("Creating vertex failed with error: PROPERTIES_DISABLED");
|
|
||||||
break;
|
|
||||||
case Error::VERTEX_HAS_EDGES:
|
|
||||||
spdlog::debug("Creating vertex failed with error: VERTEX_HAS_EDGES");
|
|
||||||
break;
|
|
||||||
case Error::VERTEX_ALREADY_INSERTED:
|
|
||||||
spdlog::debug("Creating vertex failed with error: VERTEX_ALREADY_INSERTED");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
static_assert(kAlwaysFalse<T>, "Missing type from variant visitor");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error);
|
|
||||||
|
|
||||||
action_successful = false;
|
action_successful = false;
|
||||||
break;
|
break;
|
||||||
@ -127,7 +95,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) {
|
|||||||
|
|
||||||
bool action_successful = true;
|
bool action_successful = true;
|
||||||
|
|
||||||
for (auto &vertex : req.new_properties) {
|
for (auto &vertex : req.update_vertices) {
|
||||||
if (!action_successful) {
|
if (!action_successful) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -140,46 +108,27 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &update_prop : vertex.property_updates) {
|
for (const auto label : vertex.add_labels) {
|
||||||
auto result_schema =
|
if (const auto maybe_error = vertex_to_update->AddLabelAndValidate(label); maybe_error.HasError()) {
|
||||||
vertex_to_update->SetPropertyAndValidate(update_prop.first, ToPropertyValue(std::move(update_prop.second)));
|
LogResultError(maybe_error.GetError(), "Add vertex labels");
|
||||||
if (result_schema.HasError()) {
|
|
||||||
auto &error = result_schema.GetError();
|
|
||||||
|
|
||||||
std::visit(
|
|
||||||
[]<typename T>(T &&error) {
|
|
||||||
using ErrorType = std::remove_cvref_t<T>;
|
|
||||||
if constexpr (std::is_same_v<ErrorType, SchemaViolation>) {
|
|
||||||
spdlog::debug("Updating vertex failed with error: SchemaViolation");
|
|
||||||
} else if constexpr (std::is_same_v<ErrorType, Error>) {
|
|
||||||
switch (error) {
|
|
||||||
case Error::DELETED_OBJECT:
|
|
||||||
spdlog::debug("Updating vertex failed with error: DELETED_OBJECT");
|
|
||||||
break;
|
|
||||||
case Error::NONEXISTENT_OBJECT:
|
|
||||||
spdlog::debug("Updating vertex failed with error: NONEXISTENT_OBJECT");
|
|
||||||
break;
|
|
||||||
case Error::SERIALIZATION_ERROR:
|
|
||||||
spdlog::debug("Updating vertex failed with error: SERIALIZATION_ERROR");
|
|
||||||
break;
|
|
||||||
case Error::PROPERTIES_DISABLED:
|
|
||||||
spdlog::debug("Updating vertex failed with error: PROPERTIES_DISABLED");
|
|
||||||
break;
|
|
||||||
case Error::VERTEX_HAS_EDGES:
|
|
||||||
spdlog::debug("Updating vertex failed with error: VERTEX_HAS_EDGES");
|
|
||||||
break;
|
|
||||||
case Error::VERTEX_ALREADY_INSERTED:
|
|
||||||
spdlog::debug("Updating vertex failed with error: VERTEX_ALREADY_INSERTED");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
static_assert(kAlwaysFalse<T>, "Missing type from variant visitor");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error);
|
|
||||||
|
|
||||||
action_successful = false;
|
action_successful = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const auto label : vertex.remove_labels) {
|
||||||
|
if (const auto maybe_error = vertex_to_update->RemoveLabelAndValidate(label); maybe_error.HasError()) {
|
||||||
|
LogResultError(maybe_error.GetError(), "Remove vertex labels");
|
||||||
|
action_successful = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &update_prop : vertex.property_updates) {
|
||||||
|
if (const auto result_schema = vertex_to_update->SetPropertyAndValidate(
|
||||||
|
update_prop.first, ToPropertyValue(std::move(update_prop.second)));
|
||||||
|
result_schema.HasError()) {
|
||||||
|
action_successful = false;
|
||||||
|
LogResultError(result_schema.GetError(), "Update vertex properties");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,6 @@
|
|||||||
|
|
||||||
namespace memgraph::storage::v3 {
|
namespace memgraph::storage::v3 {
|
||||||
|
|
||||||
template <typename>
|
|
||||||
constexpr auto kAlwaysFalse = false;
|
|
||||||
|
|
||||||
class ShardRsm {
|
class ShardRsm {
|
||||||
std::unique_ptr<Shard> shard_;
|
std::unique_ptr<Shard> shard_;
|
||||||
|
|
||||||
|
19
src/utils/template_utils.hpp
Normal file
19
src/utils/template_utils.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace memgraph::utils {
|
||||||
|
|
||||||
|
template <typename>
|
||||||
|
constexpr auto kAlwaysFalse{false};
|
||||||
|
|
||||||
|
} // namespace memgraph::utils
|
@ -183,19 +183,53 @@ bool AttemptToDeleteVertex(ShardClient &client, int64_t value) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AttemptToUpdateVertex(ShardClient &client, int64_t value) {
|
bool AttemptToUpdateVertex(ShardClient &client, int64_t vertex_primary_key, std::vector<LabelId> add_labels = {},
|
||||||
auto vertex_id = GetValuePrimaryKeysWithValue(value)[0];
|
std::vector<LabelId> remove_labels = {}) {
|
||||||
|
auto vertex_id = GetValuePrimaryKeysWithValue(vertex_primary_key)[0];
|
||||||
|
|
||||||
std::vector<std::pair<PropertyId, msgs::Value>> property_updates;
|
std::vector<std::pair<PropertyId, msgs::Value>> property_updates;
|
||||||
auto property_update = std::make_pair(PropertyId::FromUint(5), msgs::Value(static_cast<int64_t>(10000)));
|
auto property_update = std::make_pair(PropertyId::FromUint(5), msgs::Value(static_cast<int64_t>(10000)));
|
||||||
|
|
||||||
auto vertex_prop = msgs::UpdateVertexProp{};
|
msgs::UpdateVertex update_vertex;
|
||||||
vertex_prop.primary_key = vertex_id;
|
update_vertex.primary_key = vertex_id;
|
||||||
vertex_prop.property_updates = {property_update};
|
update_vertex.property_updates = {property_update};
|
||||||
|
update_vertex.add_labels = add_labels;
|
||||||
|
update_vertex.remove_labels = remove_labels;
|
||||||
|
|
||||||
auto update_req = msgs::UpdateVerticesRequest{};
|
msgs::UpdateVerticesRequest update_req;
|
||||||
update_req.transaction_id.logical_id = GetTransactionId();
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
update_req.new_properties = {vertex_prop};
|
update_req.update_vertices = {update_vertex};
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
auto write_res = client.SendWriteRequest(update_req);
|
||||||
|
if (write_res.HasError()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto write_response_result = write_res.GetValue();
|
||||||
|
auto write_response = std::get<msgs::UpdateVerticesResponse>(write_response_result);
|
||||||
|
|
||||||
|
Commit(client, update_req.transaction_id);
|
||||||
|
return write_response.success;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AttemptToRemoveVertexProperty(ShardClient &client, int64_t primary_key, std::vector<LabelId> add_labels = {},
|
||||||
|
std::vector<LabelId> remove_labels = {}) {
|
||||||
|
auto vertex_id = GetValuePrimaryKeysWithValue(primary_key)[0];
|
||||||
|
|
||||||
|
std::vector<std::pair<PropertyId, msgs::Value>> property_updates;
|
||||||
|
auto property_update = std::make_pair(PropertyId::FromUint(5), msgs::Value());
|
||||||
|
|
||||||
|
msgs::UpdateVertex update_vertex;
|
||||||
|
update_vertex.primary_key = vertex_id;
|
||||||
|
update_vertex.property_updates = {property_update};
|
||||||
|
update_vertex.add_labels = add_labels;
|
||||||
|
update_vertex.remove_labels = remove_labels;
|
||||||
|
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
update_req.update_vertices = {update_vertex};
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
auto write_res = client.SendWriteRequest(update_req);
|
auto write_res = client.SendWriteRequest(update_req);
|
||||||
@ -966,7 +1000,9 @@ void TestCreateAndUpdateVertices(ShardClient &client) {
|
|||||||
auto unique_prop_val = GetUniqueInteger();
|
auto unique_prop_val = GetUniqueInteger();
|
||||||
|
|
||||||
MG_ASSERT(AttemptToCreateVertex(client, unique_prop_val));
|
MG_ASSERT(AttemptToCreateVertex(client, unique_prop_val));
|
||||||
MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val));
|
MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val, {LabelId::FromInt(3)}, {}));
|
||||||
|
MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val, {}, {LabelId::FromInt(3)}));
|
||||||
|
MG_ASSERT(AttemptToRemoveVertexProperty(client, unique_prop_val));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestCreateEdge(ShardClient &client) {
|
void TestCreateEdge(ShardClient &client) {
|
||||||
|
@ -343,6 +343,9 @@ target_link_libraries(${test_prefix}storage_v3_edge mg-storage-v3)
|
|||||||
add_unit_test(storage_v3_isolation_level.cpp)
|
add_unit_test(storage_v3_isolation_level.cpp)
|
||||||
target_link_libraries(${test_prefix}storage_v3_isolation_level mg-storage-v3)
|
target_link_libraries(${test_prefix}storage_v3_isolation_level mg-storage-v3)
|
||||||
|
|
||||||
|
add_unit_test(storage_v3_shard_rsm.cpp)
|
||||||
|
target_link_libraries(${test_prefix}storage_v3_shard_rsm mg-storage-v3)
|
||||||
|
|
||||||
add_unit_test(replication_persistence_helper.cpp)
|
add_unit_test(replication_persistence_helper.cpp)
|
||||||
target_link_libraries(${test_prefix}replication_persistence_helper mg-storage-v2)
|
target_link_libraries(${test_prefix}replication_persistence_helper mg-storage-v2)
|
||||||
|
|
||||||
|
316
tests/unit/storage_v3_shard_rsm.cpp
Normal file
316
tests/unit/storage_v3_shard_rsm.cpp
Normal file
@ -0,0 +1,316 @@
|
|||||||
|
// 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 <gmock/gmock-matchers.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <fmt/format.h>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <variant>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "common/types.hpp"
|
||||||
|
#include "query/v2/requests.hpp"
|
||||||
|
#include "storage/v3/id_types.hpp"
|
||||||
|
#include "storage/v3/property_value.hpp"
|
||||||
|
#include "storage/v3/schema_validator.hpp"
|
||||||
|
#include "storage/v3/schemas.hpp"
|
||||||
|
#include "storage/v3/shard_rsm.hpp"
|
||||||
|
#include "storage/v3/temporal.hpp"
|
||||||
|
#include "storage/v3/vertex_id.hpp"
|
||||||
|
|
||||||
|
using testing::Pair;
|
||||||
|
using testing::UnorderedElementsAre;
|
||||||
|
using SchemaType = memgraph::common::SchemaType;
|
||||||
|
|
||||||
|
namespace memgraph::storage::v3::tests {
|
||||||
|
|
||||||
|
uint64_t GetTransactionId() {
|
||||||
|
static uint64_t transaction_id = 0;
|
||||||
|
return transaction_id++;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ShardRSMTest : public testing::Test {
|
||||||
|
private:
|
||||||
|
NameIdMapper id_mapper_{{{1, "primary_label"},
|
||||||
|
{2, "primary_label2"},
|
||||||
|
{3, "label"},
|
||||||
|
{4, "primary_prop1"},
|
||||||
|
{5, "primary_prop2"},
|
||||||
|
{6, "prop"}}};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ShardRSMTest() {
|
||||||
|
PropertyValue min_pk(static_cast<int64_t>(0));
|
||||||
|
std::vector<PropertyValue> min_prim_key = {min_pk};
|
||||||
|
PropertyValue max_pk(static_cast<int64_t>(10000000));
|
||||||
|
std::vector<PropertyValue> max_prim_key = {max_pk};
|
||||||
|
|
||||||
|
auto shard_ptr1 = std::make_unique<Shard>(primary_label, min_prim_key, max_prim_key, std::vector{schema_prop});
|
||||||
|
shard_ptr1->StoreMapping({{1, "primary_label"},
|
||||||
|
{2, "primary_label2"},
|
||||||
|
{3, "label"},
|
||||||
|
{4, "primary_prop1"},
|
||||||
|
{5, "primary_prop2"},
|
||||||
|
{6, "prop"}});
|
||||||
|
shard_ptr1->CreateSchema(primary_label2, {{primary_property2, SchemaType::INT}});
|
||||||
|
shard_rsm = std::make_unique<ShardRsm>(std::move(shard_ptr1));
|
||||||
|
}
|
||||||
|
|
||||||
|
LabelId NameToLabel(const std::string &name) { return LabelId::FromUint(id_mapper_.NameToId(name)); }
|
||||||
|
|
||||||
|
PropertyId NameToProperty(const std::string &name) { return PropertyId::FromUint(id_mapper_.NameToId(name)); }
|
||||||
|
|
||||||
|
auto Commit(const auto &req) {
|
||||||
|
const coordinator::Hlc commit_timestamp{GetTransactionId()};
|
||||||
|
msgs::CommitRequest commit_req;
|
||||||
|
commit_req.transaction_id = req.transaction_id;
|
||||||
|
commit_req.commit_timestamp = commit_timestamp;
|
||||||
|
return shard_rsm->Apply(commit_req);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CreateVertex(const msgs::PrimaryKey &primary_key, const std::vector<msgs::Label> labels,
|
||||||
|
const std::vector<std::pair<msgs::PropertyId, msgs::Value>> &properties) {
|
||||||
|
msgs::NewVertex vertex = {labels, primary_key, properties};
|
||||||
|
msgs::CreateVerticesRequest create_req;
|
||||||
|
create_req.new_vertices = {vertex};
|
||||||
|
create_req.new_vertices = {vertex};
|
||||||
|
create_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
auto write_res = shard_rsm->Apply(create_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::CreateVerticesResponse>(write_res));
|
||||||
|
|
||||||
|
auto commit_res = Commit(create_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::CommitResponse>(commit_res));
|
||||||
|
ASSERT_TRUE(std::get<msgs::CommitResponse>(commit_res).success);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AssertVertexExists(const msgs::PrimaryKey &primary_key, const std::vector<msgs::Label> &labels,
|
||||||
|
const std::vector<std::pair<msgs::PropertyId, msgs::Value>> &properties) {
|
||||||
|
msgs::ScanVerticesRequest scan_req;
|
||||||
|
scan_req.props_to_return = std::nullopt;
|
||||||
|
scan_req.start_id = msgs::VertexId{msgs::Label{.id = primary_label}, primary_key};
|
||||||
|
scan_req.storage_view = msgs::StorageView::OLD;
|
||||||
|
scan_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
// Make request
|
||||||
|
auto maybe_read_res = shard_rsm->Read(scan_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::ScanVerticesResponse>(maybe_read_res));
|
||||||
|
const auto read_res = std::get<msgs::ScanVerticesResponse>(maybe_read_res);
|
||||||
|
EXPECT_TRUE(read_res.success);
|
||||||
|
EXPECT_EQ(read_res.results.size(), 1);
|
||||||
|
|
||||||
|
// Read results
|
||||||
|
const auto res = read_res.results[0];
|
||||||
|
const auto vtx_id = msgs::VertexId{msgs::Label{.id = primary_label}, primary_key};
|
||||||
|
EXPECT_EQ(res.vertex.id, vtx_id);
|
||||||
|
EXPECT_EQ(res.vertex.labels, labels);
|
||||||
|
EXPECT_EQ(res.props, properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
LabelId primary_label{NameToLabel("primary_label")};
|
||||||
|
LabelId primary_label2{NameToLabel("primary_label2")};
|
||||||
|
LabelId label{NameToLabel("label")};
|
||||||
|
PropertyId primary_property1{NameToProperty("primary_prop1")};
|
||||||
|
PropertyId primary_property2{NameToProperty("primary_prop2")};
|
||||||
|
PropertyId prop{NameToProperty("prop")};
|
||||||
|
SchemaProperty schema_prop{primary_property1, SchemaType::INT};
|
||||||
|
std::unique_ptr<ShardRsm> shard_rsm;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(ShardRSMTest, TestUpdateVertexSecondaryProperty) {
|
||||||
|
const msgs::Value primary_key_val{static_cast<int64_t>(1)};
|
||||||
|
const msgs::PrimaryKey pk{primary_key_val};
|
||||||
|
|
||||||
|
// Create Vertex
|
||||||
|
CreateVertex(pk, {}, {});
|
||||||
|
|
||||||
|
// Add property prop
|
||||||
|
static constexpr int64_t updated_vertex_id{10};
|
||||||
|
{
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
update_req.update_vertices =
|
||||||
|
std::vector<msgs::UpdateVertex>{{pk, {}, {}, {{msgs::PropertyId(prop), msgs::Value(updated_vertex_id)}}}};
|
||||||
|
|
||||||
|
const auto write_res = shard_rsm->Apply(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::UpdateVerticesResponse>(write_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::UpdateVerticesResponse>(write_res).success);
|
||||||
|
|
||||||
|
const auto commit_res = Commit(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::CommitResponse>(commit_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::CommitResponse>(commit_res).success);
|
||||||
|
}
|
||||||
|
AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}, {prop, msgs::Value(updated_vertex_id)}});
|
||||||
|
|
||||||
|
// Update property prop
|
||||||
|
static constexpr int64_t updated_vertex_id_2{101};
|
||||||
|
{
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
update_req.update_vertices =
|
||||||
|
std::vector<msgs::UpdateVertex>{{pk, {}, {}, {{msgs::PropertyId(prop), msgs::Value(updated_vertex_id_2)}}}};
|
||||||
|
|
||||||
|
const auto write_res = shard_rsm->Apply(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::UpdateVerticesResponse>(write_res));
|
||||||
|
ASSERT_TRUE(std::get<msgs::UpdateVerticesResponse>(write_res).success);
|
||||||
|
|
||||||
|
const auto commit_res = Commit(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::CommitResponse>(commit_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::CommitResponse>(commit_res).success);
|
||||||
|
AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}, {prop, msgs::Value(updated_vertex_id_2)}});
|
||||||
|
}
|
||||||
|
AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}, {prop, msgs::Value(updated_vertex_id_2)}});
|
||||||
|
|
||||||
|
// Remove property prop
|
||||||
|
{
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
update_req.update_vertices =
|
||||||
|
std::vector<msgs::UpdateVertex>{{pk, {}, {}, {{msgs::PropertyId(prop), msgs::Value()}}}};
|
||||||
|
|
||||||
|
const auto write_res = shard_rsm->Apply(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::UpdateVerticesResponse>(write_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::UpdateVerticesResponse>(write_res).success);
|
||||||
|
|
||||||
|
const auto commit_res = Commit(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::CommitResponse>(commit_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::CommitResponse>(commit_res).success);
|
||||||
|
}
|
||||||
|
AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}});
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ShardRSMTest, TestUpdateVertexPrimaryProperty) {
|
||||||
|
const msgs::Value primary_key_val{static_cast<int64_t>(1)};
|
||||||
|
const msgs::PrimaryKey pk{primary_key_val};
|
||||||
|
|
||||||
|
// Create Vertex
|
||||||
|
CreateVertex(pk, {}, {});
|
||||||
|
|
||||||
|
// Try to update primary property
|
||||||
|
static constexpr int64_t updated_vertex_id{10};
|
||||||
|
{
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
update_req.update_vertices = std::vector<msgs::UpdateVertex>{
|
||||||
|
{pk, {}, {}, {{msgs::PropertyId(primary_property1), msgs::Value(updated_vertex_id)}}}};
|
||||||
|
|
||||||
|
const auto write_res = shard_rsm->Apply(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::UpdateVerticesResponse>(write_res));
|
||||||
|
EXPECT_FALSE(std::get<msgs::UpdateVerticesResponse>(write_res).success);
|
||||||
|
}
|
||||||
|
AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}});
|
||||||
|
// Try to update primary property of another schema
|
||||||
|
{
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
update_req.update_vertices = std::vector<msgs::UpdateVertex>{
|
||||||
|
{pk, {}, {}, {{msgs::PropertyId(primary_property2), msgs::Value(updated_vertex_id)}}}};
|
||||||
|
|
||||||
|
const auto write_res = shard_rsm->Apply(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::UpdateVerticesResponse>(write_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::UpdateVerticesResponse>(write_res).success);
|
||||||
|
|
||||||
|
const auto commit_res = Commit(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::CommitResponse>(commit_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::CommitResponse>(commit_res).success);
|
||||||
|
}
|
||||||
|
AssertVertexExists(pk, {},
|
||||||
|
{{primary_property1, primary_key_val}, {primary_property2, msgs::Value(updated_vertex_id)}});
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ShardRSMTest, TestUpdateSecondaryLabel) {
|
||||||
|
const msgs::Value primary_key_val{static_cast<int64_t>(1)};
|
||||||
|
const msgs::PrimaryKey pk{primary_key_val};
|
||||||
|
|
||||||
|
// Create Vertex
|
||||||
|
CreateVertex(pk, {}, {});
|
||||||
|
|
||||||
|
// Add label label
|
||||||
|
const msgs::Label secondary_label{label};
|
||||||
|
{
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
update_req.update_vertices = std::vector<msgs::UpdateVertex>{{pk, {label}, {}, {}}};
|
||||||
|
|
||||||
|
const auto write_res = shard_rsm->Apply(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::UpdateVerticesResponse>(write_res));
|
||||||
|
ASSERT_TRUE(std::get<msgs::UpdateVerticesResponse>(write_res).success);
|
||||||
|
|
||||||
|
const auto commit_res = Commit(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::CommitResponse>(commit_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::CommitResponse>(commit_res).success);
|
||||||
|
}
|
||||||
|
AssertVertexExists(pk, {secondary_label}, {{primary_property1, primary_key_val}});
|
||||||
|
|
||||||
|
// Remove primary label
|
||||||
|
{
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
update_req.update_vertices = std::vector<msgs::UpdateVertex>{{pk, {}, {label}, {}}};
|
||||||
|
|
||||||
|
const auto write_res = shard_rsm->Apply(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::UpdateVerticesResponse>(write_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::UpdateVerticesResponse>(write_res).success);
|
||||||
|
|
||||||
|
const auto commit_res = Commit(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::CommitResponse>(commit_res));
|
||||||
|
EXPECT_TRUE(std::get<msgs::CommitResponse>(commit_res).success);
|
||||||
|
}
|
||||||
|
AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}});
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ShardRSMTest, TestUpdatePrimaryLabel) {
|
||||||
|
const msgs::Value primary_key_val{static_cast<int64_t>(1)};
|
||||||
|
const msgs::PrimaryKey pk{primary_key_val};
|
||||||
|
|
||||||
|
// Create Vertex
|
||||||
|
CreateVertex(pk, {}, {});
|
||||||
|
|
||||||
|
// Remove primary label
|
||||||
|
{
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
update_req.update_vertices = std::vector<msgs::UpdateVertex>{{pk, {}, {primary_label}, {}}};
|
||||||
|
|
||||||
|
const auto write_res = shard_rsm->Apply(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::UpdateVerticesResponse>(write_res));
|
||||||
|
EXPECT_FALSE(std::get<msgs::UpdateVerticesResponse>(write_res).success);
|
||||||
|
}
|
||||||
|
AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}});
|
||||||
|
|
||||||
|
// Add different primary label
|
||||||
|
{
|
||||||
|
msgs::UpdateVerticesRequest update_req;
|
||||||
|
update_req.transaction_id.logical_id = GetTransactionId();
|
||||||
|
|
||||||
|
update_req.update_vertices = std::vector<msgs::UpdateVertex>{{pk, {primary_label2}, {}, {}}};
|
||||||
|
|
||||||
|
const auto write_res = shard_rsm->Apply(update_req);
|
||||||
|
ASSERT_TRUE(std::holds_alternative<msgs::UpdateVerticesResponse>(write_res));
|
||||||
|
EXPECT_FALSE(std::get<msgs::UpdateVerticesResponse>(write_res).success);
|
||||||
|
}
|
||||||
|
AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}});
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace memgraph::storage::v3::tests
|
Loading…
Reference in New Issue
Block a user