Skip to content

Commit

Permalink
fix ConstructWithVid
Browse files Browse the repository at this point in the history
  • Loading branch information
lipanpan03 committed Sep 26, 2024
1 parent 4ebaf95 commit 4ae5da5
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 13 deletions.
30 changes: 20 additions & 10 deletions procedures/algo_cpp/mssp_procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,32 @@ using json = nlohmann::json;

extern "C" bool Process(GraphDB& db, const std::string& request, std::string& response) {
auto start_time = get_time();
std::vector<int64_t> root_vids = {0, 1};
std::string label = "node";
std::string field = "id";
std::vector<std::string> root_values = {};
std::string root_label = "node";
std::string root_field = "id";
std::string output_file = "";

// prepare
start_time = get_time();
try {
json input = json::parse(request);
parse_from_json(label, "label", input);
parse_from_json(field, "field", input);
parse_from_json(root_vids, "root_vids", input);
parse_from_json(root_label, "root_label", input);
parse_from_json(root_field, "root_field", input);
for (const auto &item : input["root_values"].array()) {
root_values.push_back(item.get<std::string>());
}
parse_from_json(output_file, "output_file", input);
} catch (std::exception& e) {
throw std::runtime_error("json parse error, root_vid needed");
return false;
throw std::runtime_error("json parse error");
}
auto txn = db.CreateReadTxn();
OlapOnDB<double> olapondb(db, txn, SNAPSHOT_PARALLEL, nullptr, edge_convert_default<double>);

std::vector<size_t> roots;
for (auto ele : root_vids) {
for (const auto& ele : root_values) {
lgraph_api::FieldData root_field_data(ele);
int64_t root_vid = txn.GetVertexIndexIterator
(label, field, root_field_data, root_field_data).GetVid();
(root_label, root_field, root_field_data, root_field_data).GetVid();
roots.push_back(olapondb.MappedVid(root_vid));
}
auto prepare_cost = get_time() - start_time;
Expand All @@ -55,6 +58,13 @@ extern "C" bool Process(GraphDB& db, const std::string& request, std::string& re
MSSPCore(olapondb, roots, distance);
auto core_cost = get_time() - start_time;

if (output_file != "") {
olapondb.WriteToFile<double>(distance, output_file,
[&](size_t vid, double& vdata) -> bool {
return vdata != SSSP_INIT_VALUE;
});
}

// output
start_time = get_time();
auto all_vertices = olapondb.AllocVertexSubset();
Expand Down
2 changes: 1 addition & 1 deletion procedures/algo_cpp/sssp_procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ extern "C" bool Process(GraphDB& db, const std::string& request, std::string& re
return vdata != SSSP_INIT_VALUE;
});
}
txn.Commit();

auto all_vertices = olapondb.AllocVertexSubset();
all_vertices.Fill();
Expand Down Expand Up @@ -126,5 +125,6 @@ extern "C" bool Process(GraphDB& db, const std::string& request, std::string& re
output["total_cost"] = prepare_cost + core_cost + output_cost;
response = output.dump();
}
txn.Commit();
return true;
}
8 changes: 7 additions & 1 deletion procedures/algo_cpp/subgraph_isomorphism_procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ extern "C" bool Process(GraphDB& db, const std::string& request, std::string& re
std::cout << "Input: " << request << std::endl;
try {
json input = json::parse(request);
parse_from_json(query, "query", input);
for (auto &query_item : input["query"].array()) {
std::unordered_set<size_t> q;
for (auto &inner : query_item) {
q.insert(inner.get<int>());
}
query.push_back(q);
}
} catch (std::exception& e) {
response = "json parse error: " + std::string(e.what());
std::cout << response << std::endl;
Expand Down
96 changes: 96 additions & 0 deletions procedures/custom_cpp/mssp_procedure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Copyright 2022 AntGroup CO., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/

#include "lgraph/olap_on_db.h"
#include "tools/json.hpp"
#include "../algo_cpp/algo.h"

using namespace lgraph_api;
using namespace lgraph_api::olap;
using json = nlohmann::json;

extern "C" bool Process(GraphDB& db, const std::string& request, std::string& response) {
auto start_time = get_time();
std::vector<std::string> root_values = {};
std::string root_label = "node";
std::string root_field = "id";
std::string output_file = "";

// prepare
start_time = get_time();
try {
json input = json::parse(request);
parse_from_json(root_label, "root_label", input);
parse_from_json(root_field, "root_field", input);
for (const auto &item : input["root_values"].array()) {
root_values.push_back(item.get<std::string>());
}
parse_from_json(output_file, "output_file", input);
} catch (std::exception& e) {
throw std::runtime_error("json parse error");
}
auto txn = db.CreateReadTxn();
OlapOnDB<double> olapondb(db, txn, SNAPSHOT_PARALLEL, nullptr, edge_convert_default<double>);

std::vector<size_t> roots;
for (const auto& ele : root_values) {
lgraph_api::FieldData root_field_data(ele);
int64_t root_vid = txn.GetVertexIndexIterator
(root_label, root_field, root_field_data, root_field_data).GetVid();
roots.push_back(olapondb.MappedVid(root_vid));
}
auto prepare_cost = get_time() - start_time;

// core
start_time = get_time();
ParallelVector<double> distance = olapondb.AllocVertexArray<double>();
MSSPCore(olapondb, roots, distance);
auto core_cost = get_time() - start_time;

if (output_file != "") {
olapondb.WriteToFile<double>(true, distance, output_file,
[&](size_t vid, double& vdata) -> bool {
return vdata != SSSP_INIT_VALUE;
});
}

// output
start_time = get_time();
auto all_vertices = olapondb.AllocVertexSubset();
all_vertices.Fill();
size_t max_distance_vi = olapondb.ProcessVertexActive<size_t>(
[&](size_t vi) {
if (distance[vi] >= 1e10) {
distance[vi] = -1;
}
return (size_t)vi;
},
all_vertices, 0, [&](size_t a, size_t b) {
return (distance[a] > distance[b] || (distance[a] == distance[b] &&
olapondb.OriginalVid(a) < olapondb.OriginalVid(b))) ? a : b; });
auto output_cost = get_time() - start_time;

// return
json output;
output["max_distance_vid"] = max_distance_vi;
output["max_distance_val"] = distance[max_distance_vi];
output["num_vertices"] = olapondb.NumVertices();
output["num_edges"] = olapondb.NumEdges();
output["prepare_cost"] = prepare_cost;
output["core_cost"] = core_cost;
output["output_cost"] = output_cost;
output["total_cost"] = prepare_cost + core_cost + output_cost;
response = output.dump();
return true;
}
2 changes: 1 addition & 1 deletion procedures/custom_cpp/sssp_procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ extern "C" bool Process(GraphDB& db, const std::string& request, std::string& re
return vdata != SSSP_INIT_VALUE;
});
}
txn.Commit();

auto all_vertices = olapondb.AllocVertexSubset();
all_vertices.Fill();
Expand Down Expand Up @@ -127,5 +126,6 @@ extern "C" bool Process(GraphDB& db, const std::string& request, std::string& re
output["total_cost"] = prepare_cost + core_cost + output_cost;
response = output.dump();
}
txn.Commit();
return true;
}

0 comments on commit 4ae5da5

Please sign in to comment.