forked from TuGraph-family/tugraph-db
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
committed
Sep 6, 2024
1 parent
c7cb577
commit 392bd4f
Showing
19 changed files
with
1,901 additions
and
454 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* 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.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) { | ||
double start_time; | ||
|
||
// prepare | ||
start_time = get_time(); | ||
size_t samples = 10; | ||
std::string output_file = ""; | ||
try { | ||
json input = json::parse(request); | ||
parse_from_json(samples, "samples", input); | ||
parse_from_json(output_file, "output_file", input); | ||
} catch (std::exception& e) { | ||
response = "json parse error: " + std::string(e.what()); | ||
std::cout << response << std::endl; | ||
return false; | ||
} | ||
auto txn = db.CreateReadTxn(); | ||
OlapOnDB<Empty> olapondb(db, txn, SNAPSHOT_PARALLEL); | ||
auto prepare_cost = get_time() - start_time; | ||
|
||
// core | ||
start_time = get_time(); | ||
auto score = olapondb.AllocVertexArray<double>(); | ||
score.Fill(0.0); | ||
size_t max_score_vi = BCCore(olapondb, samples, score); | ||
auto core_cost = get_time() - start_time; | ||
auto vit = txn.GetVertexIterator(olapondb.OriginalVid(max_score_vi), false); | ||
auto vit_label = vit.GetLabel(); | ||
auto primary_field = txn.GetVertexPrimaryField(vit_label); | ||
auto field_data = vit.GetField(primary_field); | ||
|
||
// output | ||
start_time = get_time(); | ||
if (output_file != "") { | ||
olapondb.WriteToFile(true, score, output_file); | ||
} | ||
auto output_cost = get_time() - start_time; | ||
|
||
// return | ||
{ | ||
json output; | ||
output["max_score_vid"] = olapondb.OriginalVid(max_score_vi); | ||
output["max_score_label"] = vit_label; | ||
output["max_score_primaryfield"] = primary_field; | ||
output["max_score_fielddata"] = field_data.ToString(); | ||
output["max_score"] = score[max_score_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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* 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.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(); | ||
|
||
// prepare | ||
start_time = get_time(); | ||
std::string root_value = "0"; | ||
std::string root_label = "node"; | ||
std::string root_field = "id"; | ||
std::string output_file = ""; | ||
std::cout << "Input: " << request << std::endl; | ||
try { | ||
json input = json::parse(request); | ||
parse_from_json(root_value, "root_value", input); | ||
parse_from_json(root_label, "root_label", input); | ||
parse_from_json(root_field, "root_field", input); | ||
parse_from_json(output_file, "output_file", input); | ||
} catch (std::exception& e) { | ||
response = "json parse error: " + std::string(e.what()); | ||
std::cout << response << std::endl; | ||
return false; | ||
} | ||
|
||
auto txn = db.CreateReadTxn(); | ||
int64_t root_vid = | ||
txn.GetVertexIndexIterator(root_label, root_field, root_value, root_value).GetVid(); | ||
OlapOnDB<Empty> olapondb(db, txn, SNAPSHOT_PARALLEL); | ||
auto prepare_cost = get_time() - start_time; | ||
|
||
// core | ||
start_time = get_time(); | ||
ParallelVector<size_t> parent = olapondb.AllocVertexArray<size_t>(); | ||
size_t count = BFSCore(olapondb, olapondb.MappedVid(root_vid), parent); | ||
printf("found_vertices = %ld\n", count); | ||
auto core_cost = get_time() - start_time; | ||
|
||
// output | ||
start_time = get_time(); | ||
// TODO(any): write parent back to graph | ||
if (output_file != "") { | ||
olapondb.WriteToFile<size_t>(true, parent, output_file, | ||
[&](size_t vid, size_t vdata) -> bool { | ||
return vdata != (size_t)-1; | ||
}); | ||
} | ||
|
||
auto output_cost = get_time() - start_time; | ||
|
||
// return | ||
{ | ||
json output; | ||
output["found_vertices"] = count; | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* 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.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) { | ||
double start_time; | ||
|
||
// prepare | ||
start_time = get_time(); | ||
size_t samples = 64; | ||
std::string output_file = ""; | ||
try { | ||
json input = json::parse(request); | ||
parse_from_json(samples, "samples", input); | ||
parse_from_json(output_file, "output_file", input); | ||
} catch (std::exception& e) { | ||
response = "json parse error: " + std::string(e.what()); | ||
std::cout << response << std::endl; | ||
return false; | ||
} | ||
auto txn = db.CreateReadTxn(); | ||
|
||
OlapOnDB<double> olapondb(db, txn, SNAPSHOT_PARALLEL, nullptr, edge_convert_default<double>); | ||
auto prepare_cost = get_time() - start_time; | ||
|
||
// core | ||
start_time = get_time(); | ||
auto score = olapondb.AllocVertexArray<double>(); | ||
auto path_num = olapondb.AllocVertexArray<size_t>(); | ||
CLCECore(olapondb, samples, score, path_num); | ||
auto active_all = olapondb.AllocVertexSubset(); | ||
active_all.Fill(); | ||
size_t max_score_vi = 0; | ||
olapondb.ProcessVertexActive<size_t>([&](size_t vi) { | ||
if (path_num[vi] > samples / 5 && score[vi] > score[max_score_vi]) { | ||
max_score_vi = vi; | ||
} | ||
return 0; | ||
}, active_all); | ||
double max_length = score[max_score_vi]; | ||
auto core_cost = get_time() - start_time; | ||
auto vit = txn.GetVertexIterator(olapondb.OriginalVid(max_score_vi), false); | ||
auto vit_label = vit.GetLabel(); | ||
auto primary_field = txn.GetVertexPrimaryField(vit_label); | ||
auto field_data = vit.GetField(primary_field); | ||
|
||
// output | ||
start_time = get_time(); | ||
if (output_file != "") { | ||
olapondb.WriteToFile<double>(true, score, output_file); | ||
} | ||
auto output_cost = get_time() - start_time; | ||
|
||
// return | ||
{ | ||
json output; | ||
output["max_length_vid"] = olapondb.OriginalVid(max_score_vi); | ||
output["max_length_label"] = vit_label; | ||
output["max_length_primaryfield"] = primary_field; | ||
output["max_length_fielddata"] = field_data.ToString(); | ||
output["max_length"] = max_length; | ||
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; | ||
} |
Oops, something went wrong.