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();
167 const std::shared_ptr<EdgeInfo>& edge_info,
const std::string& prefix,
168 AdjListType adj_list_type, IdType num_vertices,
169 const ValidateLevel& validate_level = ValidateLevel::no_validate)
170 : edge_info_(std::move(edge_info)),
172 adj_list_type_(adj_list_type),
173 num_vertices_(num_vertices),
174 validate_level_(validate_level) {
175 if (validate_level_ == ValidateLevel::default_validate) {
176 throw std::runtime_error(
177 "default_validate is not allowed to be set as the global validate "
178 "level for EdgesBuilder");
183 switch (adj_list_type) {
184 case AdjListType::unordered_by_source:
185 vertex_chunk_size_ = edge_info_->GetSrcChunkSize();
187 case AdjListType::ordered_by_source:
188 vertex_chunk_size_ = edge_info_->GetSrcChunkSize();
190 case AdjListType::unordered_by_dest:
191 vertex_chunk_size_ = edge_info_->GetDstChunkSize();
193 case AdjListType::ordered_by_dest:
194 vertex_chunk_size_ = edge_info_->GetDstChunkSize();
197 vertex_chunk_size_ = edge_info_->GetSrcChunkSize();
207 if (validate_level == ValidateLevel::default_validate) {
210 validate_level_ = validate_level;
251 ValidateLevel::default_validate) {
253 GAR_RETURN_NOT_OK(validate(e, validate_level));
255 IdType vertex_chunk_index = getVertexChunkIndex(e);
256 edges_[vertex_chunk_index].push_back(e);
266 IdType
GetNum()
const {
return num_edges_; }
285 static Result<std::shared_ptr<EdgesBuilder>>
Make(
286 const std::shared_ptr<EdgeInfo>& edge_info,
const std::string& prefix,
287 AdjListType adj_list_type, IdType num_vertices,
288 const ValidateLevel& validate_level = ValidateLevel::no_validate) {
289 if (!edge_info->HasAdjacentListType(adj_list_type)) {
291 "The adjacent list type ", AdjListTypeToString(adj_list_type),
292 " doesn't exist in edge ", edge_info->GetEdgeLabel(),
".");
294 return std::make_shared<EdgesBuilder>(edge_info, prefix, adj_list_type,
295 num_vertices, validate_level);
310 static Result<std::shared_ptr<EdgesBuilder>>
Make(
311 const std::shared_ptr<GraphInfo>& graph_info,
312 const std::string& src_label,
const std::string& edge_label,
313 const std::string& dst_label,
const AdjListType& adj_list_type,
315 const ValidateLevel& validate_level = ValidateLevel::no_validate) {
316 auto edge_info = graph_info->GetEdgeInfo(src_label, edge_label, dst_label);
319 dst_label,
" doesn't exist.");
321 return Make(edge_info, graph_info->GetPrefix(), adj_list_type, num_vertices,
332 IdType getVertexChunkIndex(
const Edge& e) {
333 switch (adj_list_type_) {
334 case AdjListType::unordered_by_source:
335 return e.
GetSource() / vertex_chunk_size_;
336 case AdjListType::ordered_by_source:
337 return e.
GetSource() / vertex_chunk_size_;
338 case AdjListType::unordered_by_dest:
340 case AdjListType::ordered_by_dest:
343 return e.
GetSource() / vertex_chunk_size_;
354 Status validate(
const Edge& e, ValidateLevel validate_level)
const;
365 Status appendToArray(
const std::shared_ptr<DataType>& type,
366 const std::string& property_name,
367 std::shared_ptr<arrow::Array>& array,
368 const std::vector<Edge>& edges);
381 Status tryToAppend(
const std::string& property_name,
382 std::shared_ptr<arrow::Array>& array,
383 const std::vector<Edge>& edges);
394 Status tryToAppend(
int src_or_dest,
395 std::shared_ptr<arrow::Array>& array,
396 const std::vector<Edge>& edges);
404 Result<std::shared_ptr<arrow::Table>> convertToTable(
405 const std::vector<Edge>& edges);
413 Result<std::shared_ptr<arrow::Table>> getOffsetTable(
414 IdType vertex_chunk_index,
const std::vector<Edge>& edges);
417 std::shared_ptr<EdgeInfo> edge_info_;
419 AdjListType adj_list_type_;
420 std::unordered_map<IdType, std::vector<Edge>> edges_;
421 IdType vertex_chunk_size_;
422 IdType num_vertices_;
425 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.
EdgesBuilder(const std::shared_ptr< EdgeInfo > &edge_info, const std::string &prefix, AdjListType adj_list_type, IdType num_vertices, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Initialize the EdgesBuilder.
IdType GetNum() const
Get the current number of edges in the collection.
static Result< std::shared_ptr< EdgesBuilder > > Make(const std::shared_ptr< GraphInfo > &graph_info, const std::string &src_label, const std::string &edge_label, const std::string &dst_label, const AdjListType &adj_list_type, IdType num_vertices, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Construct an EdgesBuilder from graph info.
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, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Construct an EdgesBuilder from edge info.
void SetValidateLevel(const ValidateLevel &validate_level)
Set the validate level.
void Clear()
Clear the edges in this EdgesBuilder.
ValidateLevel GetValidateLevel() const
Get the validate level.
Status Dump()
Dump the collection into files.
Status AddEdge(const Edge &e, const ValidateLevel &validate_level=ValidateLevel::default_validate)
Add an edge to the collection.