25 #include <unordered_map>
29 #include "graphar/arrow/chunk_writer.h"
30 #include "graphar/graph_info.h"
31 #include "graphar/result.h"
39 namespace graphar::builder {
54 explicit Vertex(IdType
id) : id_(id), empty_(false) {}
61 inline IdType
GetId() const noexcept {
return id_; }
68 inline void SetId(IdType
id) { id_ = id; }
75 inline bool Empty() const noexcept {
return empty_; }
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());
122 std::unordered_map<std::string, std::any> properties_;
144 const std::shared_ptr<VertexInfo>& vertex_info,
const std::string& prefix,
145 IdType start_vertex_index = 0,
146 const ValidateLevel& validate_level = ValidateLevel::no_validate)
147 : vertex_info_(std::move(vertex_info)),
149 start_vertex_index_(start_vertex_index),
150 validate_level_(validate_level) {
151 if (validate_level_ == ValidateLevel::default_validate) {
152 throw std::runtime_error(
153 "default_validate is not allowed to be set as the global validate "
154 "level for VerticesBuilder");
176 if (validate_level == ValidateLevel::default_validate) {
179 validate_level_ = validate_level;
212 Vertex& v, IdType index = -1,
213 ValidateLevel validate_level = ValidateLevel::default_validate) {
215 GAR_RETURN_NOT_OK(validate(v, index, validate_level));
218 v.
SetId(vertices_.size());
219 vertices_.push_back(v);
222 if (index >=
static_cast<IdType
>(vertices_.size()))
223 vertices_.resize(index + 1);
224 vertices_[index] = v;
235 IdType
GetNum()
const {
return num_vertices_; }
245 IdType start_chunk_index =
246 start_vertex_index_ / vertex_info_->GetChunkSize();
248 GAR_ASSIGN_OR_RAISE(
auto input_table, convertToTable());
250 GAR_RETURN_NOT_OK(writer.
WriteTable(input_table, start_chunk_index));
267 static Result<std::shared_ptr<VerticesBuilder>>
Make(
268 const std::shared_ptr<VertexInfo>& vertex_info,
const std::string& prefix,
269 IdType start_vertex_index = 0,
270 const ValidateLevel& validate_level = ValidateLevel::no_validate) {
271 return std::make_shared<VerticesBuilder>(
272 vertex_info, prefix, start_vertex_index, validate_level);
284 static Result<std::shared_ptr<VerticesBuilder>>
Make(
285 const std::shared_ptr<GraphInfo>& graph_info,
const std::string& label,
286 IdType start_vertex_index = 0,
287 const ValidateLevel& validate_level = ValidateLevel::no_validate) {
288 const auto vertex_info = graph_info->GetVertexInfo(label);
291 " doesn't exist in graph ", graph_info->GetName(),
294 return Make(vertex_info, graph_info->GetPrefix(), start_vertex_index,
308 ValidateLevel validate_level)
const;
318 Status appendToArray(
const std::shared_ptr<DataType>& type,
319 const std::string& property_name,
320 std::shared_ptr<arrow::Array>& array);
331 Status tryToAppend(
const std::string& property_name,
332 std::shared_ptr<arrow::Array>& array);
337 Result<std::shared_ptr<arrow::Table>> convertToTable();
340 std::shared_ptr<VertexInfo> vertex_info_;
342 std::vector<Vertex> vertices_;
343 IdType start_vertex_index_;
344 IdType num_vertices_;
346 ValidateLevel validate_level_;
Status outcome object (success or error)
static Status KeyError(Args &&... args)
The writer for vertex property group chunks.
Status WriteVerticesNum(const IdType &count, ValidateLevel validate_level=ValidateLevel::default_validate) const
Write the number of vertices into the file.
Status WriteTable(const std::shared_ptr< arrow::Table > &input_table, const std::shared_ptr< PropertyGroup > &property_group, IdType start_chunk_index, ValidateLevel validate_level=ValidateLevel::default_validate) const
Write a single property group for multiple vertex chunks to corresponding files.
Vertex is designed for constructing vertices builder.
void SetId(IdType id)
Set id of the vertex.
const std::any & GetProperty(const std::string &property) const
Get a property of the vertex.
IdType GetId() const noexcept
Get id of the vertex.
void AddProperty(const std::string &name, const std::any &val)
Add a property to the vertex.
bool Empty() const noexcept
Check if the vertex is empty.
bool ContainProperty(const std::string &property)
Check if the vertex contains a property.
const std::unordered_map< std::string, std::any > & GetProperties() const
Get all properties of the vertex.
Vertex(IdType id)
Initialize the vertex with a given id.
VertexBuilder is designed for building and writing a collection of vertices.
void SetValidateLevel(const ValidateLevel &validate_level)
Set the validate level.
void Clear()
Clear the vertices in this VerciesBuilder.
static Result< std::shared_ptr< VerticesBuilder > > Make(const std::shared_ptr< GraphInfo > &graph_info, const std::string &label, IdType start_vertex_index=0, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Construct a VertexBuilder from graph info and vertex label.
Status AddVertex(Vertex &v, IdType index=-1, ValidateLevel validate_level=ValidateLevel::default_validate)
Add a vertex with the given index.
VerticesBuilder(const std::shared_ptr< VertexInfo > &vertex_info, const std::string &prefix, IdType start_vertex_index=0, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Initialize the VerticesBuilder.
static Result< std::shared_ptr< VerticesBuilder > > Make(const std::shared_ptr< VertexInfo > &vertex_info, const std::string &prefix, IdType start_vertex_index=0, const ValidateLevel &validate_level=ValidateLevel::no_validate)
Construct a VertexBuilder from vertex info.
IdType GetNum() const
Get the current number of vertices in the collection.
ValidateLevel GetValidateLevel() const
Get the validate level.
Status Dump()
Dump the collection into files.