Skip to content

Commit

Permalink
change output format to json
Browse files Browse the repository at this point in the history
  • Loading branch information
ncbd committed Aug 15, 2024
1 parent 5cf5db1 commit 94554fa
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions procedures/algo_cpp/louvain_procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
*/

#include <cstddef>
#include <cstdio>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#include "lgraph/olap_on_db.h"
#include "tools/json.hpp"
#include "./algo.h"
Expand Down Expand Up @@ -52,33 +55,40 @@ extern "C" bool Process(GraphDB& db, const std::string& request, std::string& re
auto label = olapondb.AllocVertexArray<size_t>();
auto modularity = LouvainCore(olapondb, label, active_threshold, is_sync);
auto core_cost = get_time() - start_time;
std::unordered_map<size_t,
std::tuple<std::string, std::string, std::string>> vid_primaryKey_map;
for (size_t i = 0; i < olapondb.NumVertices(); i++) {
auto vit = txn.GetVertexIterator(i, false);
auto vit_label = vit.GetLabel();
auto primary_field = txn.GetVertexPrimaryField(vit_label);
auto field_data = vit.GetField(primary_field);
vid_primaryKey_map[i] = std::make_tuple(vit_label, primary_field, field_data.ToString());
}
// output
start_time = get_time();
if (output_file != "") {
FILE* fout = fopen(output_file.c_str(), "w");
#pragma omp parallel for
json arr = json::array();
json cur;
json curNode;
json communityNode;
for (size_t i = 0; i < olapondb.NumVertices(); i++) {
if (label[i]) {
fprintf(fout, "%ld, %s.%s, %s, %ld, %s.%s, %s\n",
i,
std::get<0>(vid_primaryKey_map[i]).c_str(),
std::get<1>(vid_primaryKey_map[i]).c_str(),
std::get<2>(vid_primaryKey_map[i]).c_str(),
label[i],
std::get<0>(vid_primaryKey_map[label[i]]).c_str(),
std::get<1>(vid_primaryKey_map[label[i]]).c_str(),
std::get<2>(vid_primaryKey_map[label[i]]).c_str());
auto vit = txn.GetVertexIterator(i, false);
auto vit_label = vit.GetLabel();
auto primary_field = txn.GetVertexPrimaryField(vit_label);
auto field_data = vit.GetField(primary_field);
curNode["vid"] = i;
curNode["label"] = vit_label;
curNode["primary_field"] = primary_field;
curNode["field_data"] = field_data.ToString();

vit = txn.GetVertexIterator(label[i], false);
vit_label = vit.GetLabel();
primary_field = txn.GetVertexPrimaryField(vit_label);
field_data = vit.GetField(primary_field);
communityNode["vid"] = label[i];
communityNode["label"] = vit_label;
communityNode["primary_field"] = primary_field;
communityNode["field_data"] = field_data.ToString();

cur["cur"] = curNode;
cur["community"] = communityNode;
arr.push_back(cur);
}
}
fprintf(fout, "%s\n", arr.dump(4).c_str());
fclose(fout);
}

Expand Down

0 comments on commit 94554fa

Please sign in to comment.