Skip to content

Commit

Permalink
add business_cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Sep 6, 2024
1 parent c7cb577 commit 392bd4f
Show file tree
Hide file tree
Showing 19 changed files with 1,901 additions and 454 deletions.
37 changes: 37 additions & 0 deletions include/lgraph/olap_on_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,43 @@ class OlapOnDB : public OlapBase<EdgeData> {
}
}

/**
* @brief Write vertex data(include label、primary_field、field_data) to a file.
*
* @param detail_output always true
* @param vertex_data The parallel vector storing the vertex data.
* @param output_file The path to the output file.
*
*/
template <typename VertexData>
void WriteToFile(bool detail_output, ParallelVector<VertexData> &vertex_data,
const std::string &output_file,
std::function<bool(size_t vid, VertexData &vdata)> output_filter = nullptr) {
if (!detail_output) {
THROW_CODE(InputError, "Just support deatail output!");
}
FILE* fout = fopen(output_file.c_str(), "w");
if (fout == nullptr) {
THROW_CODE(InputError, "Unable to open file for writting!");
}
for (size_t i = 0; i < this->num_vertices_; ++i) {
if (output_filter != nullptr && !output_filter(i, vertex_data[i])) {
continue;
}
auto vit = txn_.GetVertexIterator(OriginalVid(i));
auto vit_label = vit.GetLabel();
auto primary_field = txn_.GetVertexPrimaryField(vit_label);
auto field_data = vit.GetField(primary_field);
json curJson;
curJson["vid"] = OriginalVid(i);
curJson["label"] = vit_label;
curJson["primary_field"] = primary_field;
curJson["field_data"] = field_data.ToString();
curJson["result"] = vertex_data[i];
fprintf(fout, "%s\n", curJson.dump().c_str());
}
}

/**
* @brief Write vertex data to the graph database.
*
Expand Down
1 change: 0 additions & 1 deletion procedures/algo_cpp/bc_procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ extern "C" bool Process(GraphDB& db, const std::string& request, std::string& re

// prepare
start_time = get_time();
std::string output_file = "";
size_t samples = 10;
std::string output_file = "";
try {
Expand Down
24 changes: 3 additions & 21 deletions procedures/algo_cpp/cn_procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,11 @@ extern "C" bool Process(GraphDB& db, const std::string& request, std::string& re

// core
start_time = get_time();
std::vector< std::tuple<size_t, std::string, std::string, std::string,
size_t, std::string, std::string, std::string, double> > result_list;
std::vector< std::tuple<size_t, size_t, double> > result_list;

for (auto search_pair : search_list) {
size_t count_common = CNCore(olapondb, search_pair);
auto vit_first = txn.GetVertexIterator(search_pair.first, false);
auto vit_first_label = vit_first.GetLabel();
auto vit_first_primary_field = txn.GetVertexPrimaryField(vit_first_label);
auto vit_first_field_data = vit_first.GetField(vit_first_primary_field);

auto vit_second = txn.GetVertexIterator(search_pair.second, false);
auto vit_second_label = vit_second.GetLabel();
auto vit_second_primary_field = txn.GetVertexPrimaryField(vit_second_label);
auto vit_second_field_data = vit_second.GetField(vit_second_primary_field);
result_list.push_back(std::make_tuple(
search_pair.first,
vit_first_label,
vit_first_primary_field,
vit_first_field_data.ToString(),
search_pair.second,
vit_second_label,
vit_second_primary_field,
vit_second_field_data.ToString(),
count_common));
result_list.push_back(std::make_tuple(search_pair.first, search_pair.second, count_common));
}
auto core_cost = get_time() - start_time;

Expand Down
78 changes: 78 additions & 0 deletions procedures/business_cpp/bc_procedure.cpp
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;
}
83 changes: 83 additions & 0 deletions procedures/business_cpp/bfs_procedure.cpp
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;
}
89 changes: 89 additions & 0 deletions procedures/business_cpp/clce_procedure.cpp
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;
}
Loading

0 comments on commit 392bd4f

Please sign in to comment.