Add update vertices
This commit is contained in:
@@ -260,9 +260,9 @@ struct CreateVerticesRequest {
|
||||
}
|
||||
|
||||
struct NewEdge {
|
||||
1: VertexId src;
|
||||
2: VertexId dest;
|
||||
3: EdgeType type;
|
||||
1: required VertexId src;
|
||||
2: required VertexId dest;
|
||||
3: required EdgeType type;
|
||||
4: map<i64, Value> properties;
|
||||
}
|
||||
|
||||
@@ -272,12 +272,26 @@ struct CreateEdgesRequest {
|
||||
4: list<NewEdge> new_edges;
|
||||
}
|
||||
|
||||
// We still use different request for removing properties and labels
|
||||
struct UpdateProperty {
|
||||
1: required binary name;
|
||||
2: required Value value;
|
||||
}
|
||||
|
||||
struct UpdateVerticesRequest {
|
||||
1: required i64 transaction_id;
|
||||
2: required list<VertexId> vertices_id;
|
||||
3: list<UpdateProperty> updated_props;
|
||||
4: list<binary> added_labels;
|
||||
}
|
||||
|
||||
service Storage {
|
||||
i64 startTransaction()
|
||||
Result commitTransaction(1: i64 transaction_id)
|
||||
void abortTransaction(1: i64 transaction_id)
|
||||
|
||||
Result createVertices(1: CreateVerticesRequest req)
|
||||
Result updateVertices(1: UpdateVerticesRequest req)
|
||||
Result createEdges(1: CreateEdgesRequest req)
|
||||
ScanVerticesResponse scanVertices(1: ScanVerticesRequest req)
|
||||
GetPropertiesResponse getProperties(1: GetPropertiesRequest req)
|
||||
|
||||
@@ -232,6 +232,47 @@ void CreateEdge(const std::shared_ptr<StorageAsyncClient> &client, uint64_t src,
|
||||
.get();
|
||||
}
|
||||
|
||||
void UpdateVerticesProperties(const std::shared_ptr<StorageAsyncClient> &client, std::vector<uint64_t> vertices_id,
|
||||
std::vector<std::string_view> labels, std::vector<std::string_view> property_names) {
|
||||
interface::storage::UpdateVerticesRequest request {};
|
||||
request.added_labels_ref()->assign(labels.cbegin(), labels.cend());
|
||||
request.vertices_id_ref()->assign(vertices_id.cbegin(), vertices_id.cend());
|
||||
|
||||
int prop_count = 10;
|
||||
for (const auto prop : property_names) {
|
||||
interface::storage::UpdateProperty update_prop {};
|
||||
update_prop.name = prop;
|
||||
update_prop.value.set_int_v(prop_count);
|
||||
request.updated_props_ref()->emplace_back(update_prop);
|
||||
prop_count++;
|
||||
}
|
||||
|
||||
client->future_startTransaction()
|
||||
.then([client, request = std::move(request)](folly::Try<int64_t> &&result) mutable {
|
||||
if (result.hasException()) {
|
||||
LOG(INFO) << "FAILED1: " << result.exception().get_exception()->what() << std::endl;
|
||||
return folly::makeFuture<interface::storage::Result>(std::runtime_error("failed to start transaction"));
|
||||
}
|
||||
const auto transaction_id = result.value();
|
||||
request.transaction_id_ref() = transaction_id;
|
||||
LOG(INFO) << "Sending message...";
|
||||
|
||||
return client->future_updateVertices(request).then(
|
||||
[transaction_id, client](folly::Try<interface::storage::Result> &&reply) {
|
||||
LOG(INFO) << "Closing transaction";
|
||||
if (reply.hasException()) {
|
||||
LOG(INFO) << "FAILED2: " << reply.exception().get_exception()->what() << std::endl;
|
||||
return client->future_abortTransaction(transaction_id).then([](folly::Try<void> &&reply) {
|
||||
return folly::makeFuture<interface::storage::Result>(std::runtime_error("updating vertices failed"));
|
||||
});
|
||||
}
|
||||
LOG(INFO) << "SUCCESS\n";
|
||||
return client->future_commitTransaction(transaction_id);
|
||||
});
|
||||
})
|
||||
.get();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
FLAGS_logtostderr = true;
|
||||
folly::init(&argc, &argv);
|
||||
@@ -262,7 +303,10 @@ int main(int argc, char *argv[]) {
|
||||
CreateEdge(client, 1, 2, "type1", {"proop", "prooop2"});
|
||||
CreateEdge(client, 2, 3, "type5", {});
|
||||
// This should fail
|
||||
CreateEdge(client, 12121212, 2121212121, "type5", {});
|
||||
// CreateEdge(client, 12121212, 2121212121, "type5", {});
|
||||
|
||||
UpdateVerticesProperties(client, {0, 1, 2}, {}, {"proop", "new_prop"});
|
||||
UpdateVerticesProperties(client, {0}, {"el_label"}, {"proop", "new_new_prop"});
|
||||
|
||||
const auto transaction_id = client->future_startTransaction().get();
|
||||
std::vector<std::string> props{"proop", "prooop2"};
|
||||
|
||||
@@ -210,6 +210,32 @@ void StorageServiceHandler::createEdges(::interface::storage::Result &result,
|
||||
spdlog::info("Edges creation done!");
|
||||
}
|
||||
|
||||
void StorageServiceHandler::updateVertices(::interface::storage::Result &result,
|
||||
const ::interface::storage::UpdateVerticesRequest &req) {
|
||||
spdlog::info("Updating vertices...");
|
||||
result.success_ref() = false;
|
||||
auto accessor = active_transactions_.at(req.get_transaction_id());
|
||||
|
||||
for (const auto vertex_id : req.get_vertices_id()) {
|
||||
auto vertex = accessor->FindVertex(memgraph::storage::Gid::FromInt(vertex_id), memgraph::storage::View::NEW);
|
||||
if (!vertex) {
|
||||
throw std::runtime_error("Vertex not found in storage");
|
||||
}
|
||||
|
||||
for (auto &updated_prop : req.get_updated_props()) {
|
||||
if (const auto result = vertex->SetProperty(accessor->NameToProperty(updated_prop.name),
|
||||
ThriftValueToPropertyValue(updated_prop.value));
|
||||
result.HasError()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO Add label
|
||||
}
|
||||
}
|
||||
|
||||
spdlog::info("Updating vertices done!");
|
||||
}
|
||||
|
||||
static_assert(sizeof(apache::thrift::optional_field_ref<interface::storage::Values &>) > 16);
|
||||
|
||||
std::function<memgraph::utils::BasicResult<std::string>(memgraph::storage::VertexAccessor)> CreateVertexProcessor(
|
||||
|
||||
@@ -33,6 +33,9 @@ class StorageServiceHandler final : public interface::storage::StorageSvIf {
|
||||
void createVertices(::interface::storage::Result &result,
|
||||
const ::interface::storage::CreateVerticesRequest &req) override;
|
||||
|
||||
void updateVertices(::interface::storage::Result &result,
|
||||
const ::interface::storage::UpdateVerticesRequest &req) override;
|
||||
|
||||
void createEdges(::interface::storage::Result &result, const ::interface::storage::CreateEdgesRequest &req) override;
|
||||
|
||||
void scanVertices(::interface::storage::ScanVerticesResponse &resp,
|
||||
|
||||
Reference in New Issue
Block a user