From 94e7e87d7054356184d3fac8ea24ca31d450ea00 Mon Sep 17 00:00:00 2001 From: Wang Zhiyong Date: Fri, 15 Mar 2024 05:38:42 +0000 Subject: [PATCH] update --- osgraph/api.cpp | 137 +++++++++++++++++++++++++++++ src/BuildLGraphServer.cmake | 1 + src/restful/server/rest_server.cpp | 132 +-------------------------- 3 files changed, 139 insertions(+), 131 deletions(-) create mode 100644 osgraph/api.cpp diff --git a/osgraph/api.cpp b/osgraph/api.cpp new file mode 100644 index 0000000000..ae098f8998 --- /dev/null +++ b/osgraph/api.cpp @@ -0,0 +1,137 @@ +#include "tools/lgraph_log.h" +#include "core/data_type.h" +namespace lgraph { + +lgraph_api::FieldData JsonToFieldData(const nlohmann::json& j_object) { + if (j_object.is_number_integer()) { + return lgraph_api::FieldData(j_object.get()); + } else if (j_object.is_string()) { + return lgraph_api::FieldData(j_object.get()); + } else if (j_object.is_number_float()) { + return lgraph_api::FieldData(j_object.get()); + } else if (j_object.is_boolean()) { + return lgraph_api::FieldData(j_object.get()); + } + throw std::runtime_error("unexpected json type, obj: " + j_object.dump()); +}; + +struct VertexLine { + size_t vertexLabel; + size_t primaryFieldId; + lgraph_api::FieldData primaryFieldValue; + std::vector field_ids; + std::vector field_values; +}; + +bool update_vertex(lgraph_api::GraphDB &db, const std::string &request, std::string &response) { + using namespace std; + std::string vertex_type; + try { + int add_nodes = 0; + int update_nodes = 0; + int filter_nodes = 0; + nlohmann::json input = nlohmann::json::parse(request); + vertex_type = input["type"].get(); + string primary_field = input["primary"].get(); + auto vertexes_count = input["vertexes"].size(); + if (vertexes_count == 0) { + nlohmann::json output; + output["ok"] = true; + output["response"]= "No vertexes to update."; + response = output.dump(); + return true; + } + std::vector lines; + { + auto txn = db.CreateReadTxn(); + auto vertexLabel = txn.GetVertexLabelId(vertex_type); + auto vertexPrimary = txn.GetVertexFieldId(vertexLabel, primary_field); + for (auto& vertex : input["vertexes"]) { + try { + auto id = JsonToFieldData(vertex[primary_field]); + vector field_names; + vector field_values; + for (auto& el : vertex.items()) { + if (el.key() == primary_field) { + continue; + } + field_names.push_back(el.key()); + field_values.push_back(JsonToFieldData(el.value())); + } + auto field_ids = txn.GetVertexFieldIds(vertexLabel, field_names); + VertexLine line; + line.vertexLabel = vertexLabel; + line.primaryFieldId = vertexPrimary; + line.primaryFieldValue = id; + line.field_ids = std::move(field_ids); + line.field_values = std::move(field_values); + lines.emplace_back(std::move(line)); + } catch (const exception &e) { + continue; + } + } + txn.Abort(); + } + + if (!lines.empty()) { + auto txn = db.CreateWriteTxn(); + for (auto& line : lines) { + try { + // try to find the vertex and update the property + auto vit = txn.GetVertexByUniqueIndex(line.vertexLabel, line.primaryFieldId, line.primaryFieldValue); + if (!line.field_ids.empty()) { + bool need_update = false; + auto fields = vit.GetFields(line.field_ids); + for (size_t i = 0; i < fields.size(); i++) { + if (fields[i] != line.field_values[i]) { + need_update = true; + break; + } + } + if (need_update) { + vit.SetFields(line.field_ids, line.field_values); + update_nodes++; + } else { + filter_nodes++; + } + } else { + filter_nodes++; + } + } catch (const runtime_error& re) { + // not found, insert + line.field_ids.push_back(line.primaryFieldId); + line.field_values.push_back(line.primaryFieldValue); + try { + txn.AddVertex(line.vertexLabel, line.field_ids, line.field_values); + } catch (const exception& e) { + LOG_WARN() << "AddVertex, type: " << vertex_type << ", e: " << e.what() << ", re: " << re.what(); + throw e; + } + add_nodes++; + } catch (const std::exception& e) { + LOG_WARN() << "GetVertexByUniqueIndex, type: " << vertex_type << ", e: " << e.what(); + } + } + txn.Commit(); + } + + nlohmann::json output; + output["ok"] = true; + output["response"]["type"] = vertex_type; + output["response"]["add_nodes"] = add_nodes; + output["response"]["update_nodes"] = update_nodes; + output["response"]["filter_nodes"] = filter_nodes; + response = output.dump(); + return true; + } catch (const exception &e) { + auto err = e.what(); + LOG_WARN() << vertex_type << " update_nodes, exception: " << err; + nlohmann::json output; + output["ok"] = false; + output["response"] = std::string("Error on vertex ") + vertex_type + (" processing: ") + err; + response = output.dump(); + return false; + } +} + +} \ No newline at end of file diff --git a/src/BuildLGraphServer.cmake b/src/BuildLGraphServer.cmake index c3fcd3394f..f6c001b31b 100644 --- a/src/BuildLGraphServer.cmake +++ b/src/BuildLGraphServer.cmake @@ -52,6 +52,7 @@ add_library(${TARGET_SERVER_LIB} STATIC import/import_v2.cpp import/import_v3.cpp restful/server/rest_server.cpp + ${LGRAPH_ROOT_DIR}/osgraph/api.cpp restful/server/stdafx.cpp http/http_server.cpp http/import_manager.cpp diff --git a/src/restful/server/rest_server.cpp b/src/restful/server/rest_server.cpp index a4d9752a5d..8268f1d4f6 100644 --- a/src/restful/server/rest_server.cpp +++ b/src/restful/server/rest_server.cpp @@ -1966,137 +1966,7 @@ void RestServer::HandlePostIndex(const std::string& user, const std::string& tok return RespondRSMError(request, proto_resp, relative_path, "VertexIndex"); } -static lgraph_api::FieldData JsonToFieldData(const nlohmann::json& j_object) { - if (j_object.is_number_integer()) { - return lgraph_api::FieldData(j_object.get()); - } else if (j_object.is_string()) { - return lgraph_api::FieldData(j_object.get()); - } else if (j_object.is_number_float()) { - return lgraph_api::FieldData(j_object.get()); - } else if (j_object.is_boolean()) { - return lgraph_api::FieldData(j_object.get()); - } - throw std::runtime_error("unexpected json type, obj: " + j_object.dump()); -}; - -struct VertexLine { - size_t vertexLabel; - size_t primaryFieldId; - lgraph_api::FieldData primaryFieldValue; - std::vector field_ids; - std::vector field_values; -}; - -bool update_vertex(lgraph_api::GraphDB &db, const std::string &request, std::string &response) { - using namespace std; - std::string vertex_type; - try { - int add_nodes = 0; - int update_nodes = 0; - int filter_nodes = 0; - nlohmann::json input = nlohmann::json::parse(request); - vertex_type = input["type"].get(); - string primary_field = input["primary"].get(); - auto vertexes_count = input["vertexes"].size(); - if (vertexes_count == 0) { - nlohmann::json output; - output["ok"] = true; - output["response"]= "No vertexes to update."; - response = output.dump(); - return true; - } - std::vector lines; - { - auto txn = db.CreateReadTxn(); - auto vertexLabel = txn.GetVertexLabelId(vertex_type); - auto vertexPrimary = txn.GetVertexFieldId(vertexLabel, primary_field); - for (auto& vertex : input["vertexes"]) { - try { - auto id = JsonToFieldData(vertex[primary_field]); - vector field_names; - vector field_values; - for (auto& el : vertex.items()) { - if (el.key() == primary_field) { - continue; - } - field_names.push_back(el.key()); - field_values.push_back(JsonToFieldData(el.value())); - } - auto field_ids = txn.GetVertexFieldIds(vertexLabel, field_names); - VertexLine line; - line.vertexLabel = vertexLabel; - line.primaryFieldId = vertexPrimary; - line.primaryFieldValue = id; - line.field_ids = std::move(field_ids); - line.field_values = std::move(field_values); - lines.emplace_back(std::move(line)); - } catch (const exception &e) { - continue; - } - } - txn.Abort(); - } - - if (!lines.empty()) { - auto txn = db.CreateWriteTxn(); - for (auto& line : lines) { - try { - // try to find the vertex and update the property - auto vit = txn.GetVertexByUniqueIndex(line.vertexLabel, line.primaryFieldId, line.primaryFieldValue); - if (!line.field_ids.empty()) { - bool need_update = false; - auto fields = vit.GetFields(line.field_ids); - for (size_t i = 0; i < fields.size(); i++) { - if (fields[i] != line.field_values[i]) { - need_update = true; - break; - } - } - if (need_update) { - vit.SetFields(line.field_ids, line.field_values); - update_nodes++; - } else { - filter_nodes++; - } - } else { - filter_nodes++; - } - } catch (const runtime_error& re) { - // not found, insert - line.field_ids.push_back(line.primaryFieldId); - line.field_values.push_back(line.primaryFieldValue); - try { - txn.AddVertex(line.vertexLabel, line.field_ids, line.field_values); - } catch (const exception& e) { - LOG_WARN() << "AddVertex, type: " << vertex_type << ", e: " << e.what() << ", re: " << re.what(); - throw e; - } - add_nodes++; - } catch (const std::exception& e) { - LOG_WARN() << "GetVertexByUniqueIndex, type: " << vertex_type << ", e: " << e.what(); - } - } - txn.Commit(); - } - - nlohmann::json output; - output["ok"] = true; - output["response"]["type"] = vertex_type; - output["response"]["add_nodes"] = add_nodes; - output["response"]["update_nodes"] = update_nodes; - output["response"]["filter_nodes"] = filter_nodes; - response = output.dump(); - return true; - } catch (const exception &e) { - auto err = e.what(); - LOG_WARN() << vertex_type << " update_nodes, exception: " << err; - nlohmann::json output; - output["ok"] = false; - output["response"] = std::string("Error on vertex ") + vertex_type + (" processing: ") + err; - response = output.dump(); - return false; - } -} +bool update_vertex(lgraph_api::GraphDB &db, const std::string &request, std::string &response); void RestServer::HandlePostOSGraph(const std::string& user, const std::string& token, const web::http::http_request& request,