26 #include <unordered_map>
30 #include "graphar/arrow/chunk_writer.h"
31 #include "graphar/fwd.h"
32 #include "graphar/graph_info.h"
33 #include "graphar/types.h"
39 namespace graphar::builder {
53 explicit Edge(IdType src_id, IdType dst_id)
54 : src_id_(src_id), dst_id_(dst_id), empty_(true) {}
61 inline bool Empty() const noexcept {
return empty_; }
68 inline IdType
GetSource() const noexcept {
return src_id_; }
84 inline void AddProperty(
const std::string& name,
const std::any& val) {
86 properties_[name] = val;
95 inline const std::any&
GetProperty(
const std::string& property)
const {
96 return properties_.at(property);
116 return (properties_.find(property) != properties_.end());
120 IdType src_id_, dst_id_;
122 std::unordered_map<std::string, std::any> properties_;
132 inline bool cmp_src(
const Edge& a,
const Edge& b) {
133 return a.GetSource() < b.GetSource();
143 inline bool cmp_dst(
const Edge& a,
const Edge& b) {
144 return a.GetDestination() < b.GetDestination();
169 const std::shared_ptr<EdgeInfo>& edge_info,
const std::string& prefix,
170 AdjListType adj_list_type, IdType num_vertices,
171 std::shared_ptr<WriterOptions> writerOptions =
nullptr,
172 const ValidateLevel& validate_level = ValidateLevel::no_validate)
173 : edge_info_(std::move(edge_info)),
175 adj_list_type_(adj_list_type),
176 num_vertices_(num_vertices),
177 writer_options_(writerOptions),
178 validate_level_(validate_level) {
179 if (validate_level_ == ValidateLevel::default_validate) {
180 throw std::runtime_error(
181 "default_validate is not allowed to be set as the global validate "
182 "level for EdgesBuilder");
187 switch (adj_list_type) {
188 case AdjListType::unordered_by_source:
189 vertex_chunk_size_ = edge_info_->GetSrcChunkSize();
191 case AdjListType::ordered_by_source:
192 vertex_chunk_size_ = edge_info_->GetSrcChunkSize();
194 case AdjListType::unordered_by_dest:
195 vertex_chunk_size_ = edge_info_->GetDstChunkSize();
197 case AdjListType::ordered_by_dest:
198 vertex_chunk_size_ = edge_info_->GetDstChunkSize();
201 vertex_chunk_size_ = edge_info_->GetSrcChunkSize();
211 if (validate_level == ValidateLevel::default_validate) {
214 validate_level_ = validate_level;
224 this->writer_options_ = writer_options;
233 return this->writer_options_;
274 ValidateLevel::default_validate) {
276 GAR_RETURN_NOT_OK(validate(e, validate_level));
278 IdType vertex_chunk_index = getVertexChunkIndex(e);
279 edges_[vertex_chunk_index].push_back(e);
289 IdType
GetNum()
const {
return num_edges_; }
310 static Result<std::shared_ptr<EdgesBuilder>>
Make(
311 const std::shared_ptr<EdgeInfo>& edge_info,
const std::string& prefix,
312 AdjListType adj_list_type, IdType num_vertices,
313 std::shared_ptr<WriterOptions> writer_options,
314 const ValidateLevel& validate_level = ValidateLevel::no_validate) {
315 if (!edge_info->HasAdjacentListType(adj_list_type)) {
317 "The adjacent list type ", AdjListTypeToString(adj_list_type),
318 " doesn't exist in edge ", edge_info->GetEdgeType(),
".");
320 return std::make_shared<EdgesBuilder>(edge_info, prefix, adj_list_type,
321 num_vertices, writer_options,
325 static Result<std::shared_ptr<EdgesBuilder>>
Make(
326 const std::shared_ptr<EdgeInfo>& edge_info,
const std::string& prefix,
327 AdjListType adj_list_type, IdType num_vertices,
328 const ValidateLevel& validate_level = ValidateLevel::no_validate) {
329 if (!edge_info->HasAdjacentListType(adj_list_type)) {
331 "The adjacent list type ", AdjListTypeToString(adj_list_type),
332 " doesn't exist in edge ", edge_info->GetEdgeType(),
".");
334 return std::make_shared<EdgesBuilder>(edge_info, prefix, adj_list_type,
335 num_vertices,
nullptr,
351 static Result<std::shared_ptr<EdgesBuilder>>
Make(
352 const std::shared_ptr<GraphInfo>& graph_info,
const std::string& src_type,
353 const std::string& edge_type,
const std::string& dst_type,
354 const AdjListType& adj_list_type, IdType num_vertices,
355 std::shared_ptr<WriterOptions> writer_options,
356 const ValidateLevel& validate_level = ValidateLevel::no_validate) {
357 auto edge_info = graph_info->GetEdgeInfo(src_type, edge_type, dst_type);
360 dst_type,
" doesn't exist.");
362 return Make(edge_info, graph_info->GetPrefix(), adj_list_type, num_vertices,
363 writer_options, validate_level);
366 static Result<std::shared_ptr<EdgesBuilder>>
Make(
367 const std::shared_ptr<GraphInfo>& graph_info,
const std::string& src_type,
368 const std::string& edge_type,
const std::string& dst_type,
369 const AdjListType& adj_list_type, IdType num_vertices,
370 const ValidateLevel& validate_level = ValidateLevel::no_validate) {
371 auto edge_info = graph_info->GetEdgeInfo(src_type, edge_type, dst_type);
374 dst_type,
" doesn't exist.");
376 return Make(edge_info, graph_info->GetPrefix(), adj_list_type, num_vertices,
377 nullptr, validate_level);
387 IdType getVertexChunkIndex(
const Edge& e) {
388 switch (adj_list_type_) {
389 case AdjListType::unordered_by_source:
390 return e.GetSource() / vertex_chunk_size_;
391 case AdjListType::ordered_by_source:
392 return e.GetSource() / vertex_chunk_size_;
393 case AdjListType::unordered_by_dest:
394 return e.GetDestination() / vertex_chunk_size_;
395 case AdjListType::ordered_by_dest:
396 return e.GetDestination() / vertex_chunk_size_;
398 return e.GetSource() / vertex_chunk_size_;
409 Status validate(
const Edge& e, ValidateLevel validate_level)
const;
420 Status appendToArray(
const std::shared_ptr<DataType>& type,
421 const std::string& property_name,
422 std::shared_ptr<arrow::Array>& array,
423 const std::vector<Edge>& edges);
436 Status tryToAppend(
const std::string& property_name,
437 std::shared_ptr<arrow::Array>& array,
438 const std::vector<Edge>& edges);
449 Status tryToAppend(
int src_or_dest,
450 std::shared_ptr<arrow::Array>& array,
451 const std::vector<Edge>& edges);
459 Result<std::shared_ptr<arrow::Table>> convertToTable(
460 const std::vector<Edge>& edges);
468 Result<std::shared_ptr<arrow::Table>> getOffsetTable(
469 IdType vertex_chunk_index,
const std::vector<Edge>& edges);
472 std::shared_ptr<EdgeInfo> edge_info_;
474 AdjListType adj_list_type_;
475 std::unordered_map<IdType, std::vector<Edge>> edges_;
476 IdType vertex_chunk_size_;
477 IdType num_vertices_;
480 std::shared_ptr<WriterOptions> writer_options_;
481 ValidateLevel validate_level_;
Edge contains information of certain edge.
Status outcome object (success or error)
static Status KeyError(Args &&... args)
Edge is designed for constructing edges builder.
const std::any & GetProperty(const std::string &property) const
Get a property of the edge.
const std::unordered_map< std::string, std::any > & GetProperties() const
Get all properties of the edge.
IdType GetSource() const noexcept
Get source id of the edge.
bool Empty() const noexcept
Check if the edge is empty.
void AddProperty(const std::string &name, const std::any &val)
Add a property to the edge.
IdType GetDestination() const noexcept
Get destination id of the edge.
Edge(IdType src_id, IdType dst_id)
Initialize the edge with its source and destination.
bool ContainProperty(const std::string &property) const
Check if the edge contains a property.
EdgeBuilder is designed for building and writing a collection of edges.
IdType GetNum() const
Get the current number of edges in the collection.
EdgesBuilder(const std::shared_ptr< EdgeInfo > &edge_info, const std::string &prefix, AdjListType adj_list_type, IdType num_vertices, std::shared_ptr< WriterOptions > writerOptions=nullptr, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Initialize the EdgesBuilder.
static Result< std::shared_ptr< EdgesBuilder > > Make(const std::shared_ptr< EdgeInfo > &edge_info, const std::string &prefix, AdjListType adj_list_type, IdType num_vertices, std::shared_ptr< WriterOptions > writer_options, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Construct an EdgesBuilder from edge info.
std::shared_ptr< WriterOptions > GetWriterOptions()
Set the writerOptions.
void SetValidateLevel(const ValidateLevel &validate_level)
Set the validate level.
static Result< std::shared_ptr< EdgesBuilder > > Make(const std::shared_ptr< GraphInfo > &graph_info, const std::string &src_type, const std::string &edge_type, const std::string &dst_type, const AdjListType &adj_list_type, IdType num_vertices, std::shared_ptr< WriterOptions > writer_options, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Construct an EdgesBuilder from graph info.
void Clear()
Clear the edges in this EdgesBuilder.
ValidateLevel GetValidateLevel() const
Get the validate level.
Status Dump()
Dump the collection into files.
void SetWriterOptions(std::shared_ptr< WriterOptions > writer_options)
Set the writerOptions.
Status AddEdge(const Edge &e, const ValidateLevel &validate_level=ValidateLevel::default_validate)
Add an edge to the collection.