Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
graph_info.h
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #pragma once
21 
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "graphar/fwd.h"
28 
29 namespace graphar {
30 
34 class Property {
35  public:
36  std::string name; // property name
37  std::shared_ptr<DataType> type; // property data type
38  bool is_primary; // primary key tag
39  bool is_nullable; // nullable tag for non-primary key
40 
41  explicit Property(const std::string& name,
42  const std::shared_ptr<DataType>& type = nullptr,
43  bool is_primary = false, bool is_nullable = true)
44  : name(name),
45  type(type),
46  is_primary(is_primary),
47  is_nullable(!is_primary && is_nullable) {}
48 };
49 
50 bool operator==(const Property& lhs, const Property& rhs);
51 
60  public:
70  explicit PropertyGroup(const std::vector<Property>& properties,
71  FileType file_type, const std::string& prefix = "");
72 
78  const std::vector<Property>& GetProperties() const;
79 
80  bool HasProperty(const std::string& property_name) const;
81 
86  inline FileType GetFileType() const { return file_type_; }
87 
92  inline const std::string& GetPrefix() const { return prefix_; }
93 
97  bool IsValidated() const;
98 
99  friend std::ostream& operator<<(std::ostream& stream,
100  const PropertyGroup& pg) {
101  for (size_t i = 0; i < pg.properties_.size(); ++i) {
102  stream << pg.properties_[i].name;
103  if (i != pg.properties_.size() - 1) {
104  stream << "_";
105  }
106  }
107  return stream;
108  }
109 
110  private:
111  std::vector<Property> properties_;
112  FileType file_type_;
113  std::string prefix_;
114 };
115 
116 bool operator==(const PropertyGroup& lhs, const PropertyGroup& rhs);
117 
122  public:
132  explicit AdjacentList(AdjListType type, FileType file_type,
133  const std::string& prefix = "");
134 
140  inline AdjListType GetType() const { return type_; }
141 
147  inline FileType GetFileType() const { return file_type_; }
148 
154  inline const std::string& GetPrefix() const { return prefix_; }
155 
161  bool IsValidated() const;
162 
163  private:
164  AdjListType type_;
165  FileType file_type_;
166  std::string prefix_;
167 };
168 
174 class VertexInfo {
175  public:
187  explicit VertexInfo(const std::string& label, IdType chunk_size,
188  const PropertyGroupVector& property_groups,
189  const std::string& prefix = "",
190  std::shared_ptr<const InfoVersion> version = nullptr);
191 
192  ~VertexInfo();
193 
199  Result<std::shared_ptr<VertexInfo>> AddPropertyGroup(
200  std::shared_ptr<PropertyGroup> property_group) const;
201 
207  const std::string& GetLabel() const;
208 
214  IdType GetChunkSize() const;
215 
221  const std::string& GetPrefix() const;
222 
228  const std::shared_ptr<const InfoVersion>& version() const;
229 
235  int PropertyGroupNum() const;
236 
240  const PropertyGroupVector& GetPropertyGroups() const;
241 
248  std::shared_ptr<PropertyGroup> GetPropertyGroup(
249  const std::string& property_name) const;
250 
257  std::shared_ptr<PropertyGroup> GetPropertyGroupByIndex(int index) const;
258 
266  Result<std::shared_ptr<DataType>> GetPropertyType(
267  const std::string& property_name) const;
268 
275  bool HasProperty(const std::string& property_name) const;
276 
283  Status Save(const std::string& file_name) const;
284 
291  Result<std::string> Dump() const noexcept;
292 
299  bool IsPrimaryKey(const std::string& property_name) const;
300 
307  bool IsNullableKey(const std::string& property_name) const;
308 
316  bool HasPropertyGroup(
317  const std::shared_ptr<PropertyGroup>& property_group) const;
318 
327  Result<std::string> GetFilePath(std::shared_ptr<PropertyGroup> property_group,
328  IdType chunk_index) const;
329 
337  Result<std::string> GetPathPrefix(
338  std::shared_ptr<PropertyGroup> property_group) const;
339 
345  Result<std::string> GetVerticesNumFilePath() const;
346 
352  bool IsValidated() const;
353 
361  static Result<std::shared_ptr<VertexInfo>> Load(std::shared_ptr<Yaml> yaml);
362 
369  static Result<std::shared_ptr<VertexInfo>> Load(const std::string& input);
370 
371  private:
372  class Impl;
373  std::unique_ptr<Impl> impl_;
374 };
375 
382 class EdgeInfo {
383  public:
401  explicit EdgeInfo(const std::string& src_label, const std::string& edge_label,
402  const std::string& dst_label, IdType chunk_size,
403  IdType src_chunk_size, IdType dst_chunk_size, bool directed,
404  const AdjacentListVector& adjacent_lists,
405  const PropertyGroupVector& property_groups,
406  const std::string& prefix = "",
407  std::shared_ptr<const InfoVersion> version = nullptr);
408 
409  ~EdgeInfo();
410 
418  Result<std::shared_ptr<EdgeInfo>> AddAdjacentList(
419  std::shared_ptr<AdjacentList> adj_list) const;
420 
426  Result<std::shared_ptr<EdgeInfo>> AddPropertyGroup(
427  std::shared_ptr<PropertyGroup> property_group) const;
428 
433  const std::string& GetSrcLabel() const;
434 
439  const std::string& GetEdgeLabel() const;
440 
445  const std::string& GetDstLabel() const;
446 
451  IdType GetChunkSize() const;
452 
457  IdType GetSrcChunkSize() const;
458 
463  IdType GetDstChunkSize() const;
464 
469  const std::string& GetPrefix() const;
470 
475  bool IsDirected() const;
476 
481  const std::shared_ptr<const InfoVersion>& version() const;
482 
490  bool HasAdjacentListType(AdjListType adj_list_type) const;
491 
498  bool HasProperty(const std::string& property_name) const;
499 
503  bool HasPropertyGroup(
504  const std::shared_ptr<PropertyGroup>& property_group) const;
505 
506  std::shared_ptr<AdjacentList> GetAdjacentList(
507  AdjListType adj_list_type) const;
508 
512  int PropertyGroupNum() const;
513 
518  const PropertyGroupVector& GetPropertyGroups() const;
519 
526  std::shared_ptr<PropertyGroup> GetPropertyGroup(
527  const std::string& property) const;
528 
535  std::shared_ptr<PropertyGroup> GetPropertyGroupByIndex(int index) const;
536 
544  Result<std::string> GetVerticesNumFilePath(AdjListType adj_list_type) const;
545 
554  Result<std::string> GetEdgesNumFilePath(IdType vertex_chunk_index,
555  AdjListType adj_list_type) const;
556 
564  Result<std::string> GetAdjListFilePath(IdType vertex_chunk_index,
565  IdType edge_chunk_index,
566  AdjListType adj_list_type) const;
567 
575  Result<std::string> GetAdjListPathPrefix(AdjListType adj_list_type) const;
576 
584  Result<std::string> GetAdjListOffsetFilePath(IdType vertex_chunk_index,
585  AdjListType adj_list_type) const;
586 
594  Result<std::string> GetOffsetPathPrefix(AdjListType adj_list_type) const;
595 
606  Result<std::string> GetPropertyFilePath(
607  const std::shared_ptr<PropertyGroup>& property_group,
608  AdjListType adj_list_type, IdType vertex_chunk_index,
609  IdType edge_chunk_index) const;
610 
619  Result<std::string> GetPropertyGroupPathPrefix(
620  const std::shared_ptr<PropertyGroup>& property_group,
621  AdjListType adj_list_type) const;
622 
630  Result<std::shared_ptr<DataType>> GetPropertyType(
631  const std::string& property_name) const;
638  bool IsPrimaryKey(const std::string& property_name) const;
639 
646  bool IsNullableKey(const std::string& property_name) const;
647 
654  Status Save(const std::string& file_name) const;
655 
662  Result<std::string> Dump() const noexcept;
663 
669  bool IsValidated() const;
670 
672  static Result<std::shared_ptr<EdgeInfo>> Load(std::shared_ptr<Yaml> yaml);
673 
680  static Result<std::shared_ptr<EdgeInfo>> Load(const std::string& input);
681 
682  private:
683  class Impl;
684  std::unique_ptr<Impl> impl_;
685 };
686 
690 class GraphInfo {
691  public:
702  explicit GraphInfo(
703  const std::string& graph_name, VertexInfoVector vertex_infos,
704  EdgeInfoVector edge_infos, const std::string& prefix = "./",
705  std::shared_ptr<const InfoVersion> version = nullptr,
706  const std::unordered_map<std::string, std::string>& extra_info = {});
707 
708  ~GraphInfo();
709 
716  static Result<std::shared_ptr<GraphInfo>> Load(const std::string& path);
717 
725  static Result<std::shared_ptr<GraphInfo>> Load(
726  const std::string& input, const std::string& relative_path);
727 
736  Result<std::shared_ptr<GraphInfo>> AddVertex(
737  std::shared_ptr<VertexInfo> vertex_info) const;
738 
747  Result<std::shared_ptr<GraphInfo>> AddEdge(
748  std::shared_ptr<EdgeInfo> edge_info) const;
749 
754  const std::string& GetName() const;
755 
760  const std::string& GetPrefix() const;
761 
767  const std::shared_ptr<const InfoVersion>& version() const;
768 
774  const std::unordered_map<std::string, std::string>& GetExtraInfo() const;
775 
781  std::shared_ptr<VertexInfo> GetVertexInfo(const std::string& label) const;
782 
791  std::shared_ptr<EdgeInfo> GetEdgeInfo(const std::string& src_label,
792  const std::string& edge_label,
793  const std::string& dst_label) const;
794 
798  int GetVertexInfoIndex(const std::string& label) const;
799 
804  int GetEdgeInfoIndex(const std::string& src_label,
805  const std::string& edge_label,
806  const std::string& dst_label) const;
807 
811  int VertexInfoNum() const;
812 
816  int EdgeInfoNum() const;
817 
824  const std::shared_ptr<VertexInfo> GetVertexInfoByIndex(int index) const;
825 
832  const std::shared_ptr<EdgeInfo> GetEdgeInfoByIndex(int index) const;
833 
839  const VertexInfoVector& GetVertexInfos() const;
840 
846  const EdgeInfoVector& GetEdgeInfos() const;
847 
854  Status Save(const std::string& path) const;
855 
862  Result<std::string> Dump() const;
863 
869  bool IsValidated() const;
870 
871  private:
872  class Impl;
873  std::unique_ptr<Impl> impl_;
874 };
875 
876 } // namespace graphar
const std::string & GetPrefix() const
Get the prefix of adjacent list.
Definition: graph_info.h:154
FileType GetFileType() const
Get the file type of adjacent list.
Definition: graph_info.h:147
AdjListType GetType() const
Get the type of adjacent list.
Definition: graph_info.h:140
bool IsValidated() const
Definition: graph_info.cc:169
AdjacentList(AdjListType type, FileType file_type, const std::string &prefix="")
Definition: graph_info.cc:161
EdgeInfo is a class to describe the edge information, including the source vertex label,...
Definition: graph_info.h:382
static Result< std::shared_ptr< EdgeInfo > > Load(std::shared_ptr< Yaml > yaml)
Definition: graph_info.cc:830
Result< std::string > GetAdjListFilePath(IdType vertex_chunk_index, IdType edge_chunk_index, AdjListType adj_list_type) const
Get the file path of adj list topology chunk.
Definition: graph_info.cc:689
Status Save(const std::string &file_name) const
Definition: graph_info.cc:966
Result< std::shared_ptr< DataType > > GetPropertyType(const std::string &property_name) const
Definition: graph_info.cc:750
bool IsValidated() const
Definition: graph_info.cc:810
const std::string & GetPrefix() const
Definition: graph_info.cc:611
Result< std::string > GetEdgesNumFilePath(IdType vertex_chunk_index, AdjListType adj_list_type) const
Definition: graph_info.cc:681
Result< std::string > GetPropertyGroupPathPrefix(const std::shared_ptr< PropertyGroup > &property_group, AdjListType adj_list_type) const
Definition: graph_info.cc:738
bool IsPrimaryKey(const std::string &property_name) const
Definition: graph_info.cc:759
Result< std::string > GetAdjListOffsetFilePath(IdType vertex_chunk_index, AdjListType adj_list_type) const
Get the adjacency list offset chunk file path of vertex chunk the offset chunks is aligned with the v...
Definition: graph_info.cc:707
IdType GetChunkSize() const
Definition: graph_info.cc:605
bool HasProperty(const std::string &property_name) const
Returns whether the edge info contains the given property.
Definition: graph_info.cc:624
Result< std::shared_ptr< EdgeInfo > > AddAdjacentList(std::shared_ptr< AdjacentList > adj_list) const
Definition: graph_info.cc:775
Result< std::string > Dump() const noexcept
Definition: graph_info.cc:911
bool IsNullableKey(const std::string &property_name) const
Definition: graph_info.cc:767
std::shared_ptr< PropertyGroup > GetPropertyGroupByIndex(int index) const
Get the property group at the specified index.
Definition: graph_info.cc:665
bool HasPropertyGroup(const std::shared_ptr< PropertyGroup > &property_group) const
Returns whether the edge info contains the given property group.
Definition: graph_info.cc:629
const std::shared_ptr< const InfoVersion > & version() const
Definition: graph_info.cc:615
Result< std::shared_ptr< EdgeInfo > > AddPropertyGroup(std::shared_ptr< PropertyGroup > property_group) const
Definition: graph_info.cc:791
bool HasAdjacentListType(AdjListType adj_list_type) const
Definition: graph_info.cc:619
Result< std::string > GetOffsetPathPrefix(AdjListType adj_list_type) const
Definition: graph_info.cc:715
bool IsDirected() const
Definition: graph_info.cc:613
Result< std::string > GetAdjListPathPrefix(AdjListType adj_list_type) const
Get the path prefix of the adjacency list topology chunk for the given adjacency list type.
Definition: graph_info.cc:699
IdType GetDstChunkSize() const
Definition: graph_info.cc:609
Result< std::string > GetPropertyFilePath(const std::shared_ptr< PropertyGroup > &property_group, AdjListType adj_list_type, IdType vertex_chunk_index, IdType edge_chunk_index) const
Get the chunk file path of adj list property group the property group chunks is aligned with the adj ...
Definition: graph_info.cc:723
IdType GetSrcChunkSize() const
Definition: graph_info.cc:607
const std::string & GetSrcLabel() const
Definition: graph_info.cc:599
const PropertyGroupVector & GetPropertyGroups() const
Get the property groups.
Definition: graph_info.cc:655
const std::string & GetDstLabel() const
Definition: graph_info.cc:603
Result< std::string > GetVerticesNumFilePath(AdjListType adj_list_type) const
Get the file path for the number of vertices.
Definition: graph_info.cc:673
std::shared_ptr< PropertyGroup > GetPropertyGroup(const std::string &property) const
Get the property group containing the given property.
Definition: graph_info.cc:659
int PropertyGroupNum() const
Get the number of property groups.
Definition: graph_info.cc:651
const std::string & GetEdgeLabel() const
Definition: graph_info.cc:601
EdgeInfo(const std::string &src_label, const std::string &edge_label, const std::string &dst_label, IdType chunk_size, IdType src_chunk_size, IdType dst_chunk_size, bool directed, const AdjacentListVector &adjacent_lists, const PropertyGroupVector &property_groups, const std::string &prefix="", std::shared_ptr< const InfoVersion > version=nullptr)
Construct an EdgeInfo object with the given information and property groups.
Definition: graph_info.cc:586
int GetVertexInfoIndex(const std::string &label) const
Get the vertex info index with the given label.
Definition: graph_info.cc:1140
const EdgeInfoVector & GetEdgeInfos() const
Get the edge infos of graph info.
Definition: graph_info.cc:1185
std::shared_ptr< EdgeInfo > GetEdgeInfo(const std::string &src_label, const std::string &edge_label, const std::string &dst_label) const
Get the edge info with the given source vertex label, edge label, and destination vertex label.
Definition: graph_info.cc:1144
Status Save(const std::string &path) const
Definition: graph_info.cc:1300
const std::shared_ptr< VertexInfo > GetVertexInfoByIndex(int index) const
Get the vertex info at the specified index.
Definition: graph_info.cc:1166
Result< std::shared_ptr< GraphInfo > > AddVertex(std::shared_ptr< VertexInfo > vertex_info) const
Adds a vertex info to the GraphInfo instance and returns a new GraphInfo.
Definition: graph_info.cc:1191
const std::string & GetPrefix() const
Get the absolute path prefix of the chunk files.
Definition: graph_info.cc:1123
static Result< std::shared_ptr< GraphInfo > > Load(const std::string &path)
Loads the input file as a GraphInfo instance.
Definition: graph_info.cc:1231
int GetEdgeInfoIndex(const std::string &src_label, const std::string &edge_label, const std::string &dst_label) const
Get the edge info index with the given source vertex label, edge label, and destination label.
Definition: graph_info.cc:1151
bool IsValidated() const
Definition: graph_info.cc:1189
Result< std::string > Dump() const
Definition: graph_info.cc:1257
int EdgeInfoNum() const
Get the number of edge infos.
Definition: graph_info.cc:1162
Result< std::shared_ptr< GraphInfo > > AddEdge(std::shared_ptr< EdgeInfo > edge_info) const
Adds an edge info to the GraphInfo instance and returns a new GraphInfo.
Definition: graph_info.cc:1204
const VertexInfoVector & GetVertexInfos() const
Get the vertex infos of graph info.
Definition: graph_info.cc:1181
const std::unordered_map< std::string, std::string > & GetExtraInfo() const
Get the extra metadata of the graph info object.
Definition: graph_info.cc:1129
int VertexInfoNum() const
Get the number of vertex infos.
Definition: graph_info.cc:1158
const std::string & GetName() const
Get the name of the graph.
Definition: graph_info.cc:1121
GraphInfo(const std::string &graph_name, VertexInfoVector vertex_infos, EdgeInfoVector edge_infos, const std::string &prefix="./", std::shared_ptr< const InfoVersion > version=nullptr, const std::unordered_map< std::string, std::string > &extra_info={})
Constructs a GraphInfo instance.
Definition: graph_info.cc:1111
const std::shared_ptr< const InfoVersion > & version() const
Get the version info of the graph info object.
Definition: graph_info.cc:1125
std::shared_ptr< VertexInfo > GetVertexInfo(const std::string &label) const
Get the vertex info with the given label.
Definition: graph_info.cc:1134
const std::shared_ptr< EdgeInfo > GetEdgeInfoByIndex(int index) const
Get the edge info at the specified index.
Definition: graph_info.cc:1174
const std::string & GetPrefix() const
Definition: graph_info.h:92
bool IsValidated() const
Definition: graph_info.cc:116
FileType GetFileType() const
Definition: graph_info.h:86
PropertyGroup(const std::vector< Property > &properties, FileType file_type, const std::string &prefix="")
Definition: graph_info.cc:92
const std::vector< Property > & GetProperties() const
Definition: graph_info.cc:103
Status outcome object (success or error)
Definition: status.h:123
VertexInfo is a class to describe the vertex information, including the vertex label,...
Definition: graph_info.h:174
std::shared_ptr< PropertyGroup > GetPropertyGroup(const std::string &property_name) const
Definition: graph_info.cc:296
Result< std::string > GetPathPrefix(std::shared_ptr< PropertyGroup > property_group) const
Definition: graph_info.cc:280
IdType GetChunkSize() const
Definition: graph_info.cc:263
const std::shared_ptr< const InfoVersion > & version() const
Definition: graph_info.cc:267
Result< std::shared_ptr< VertexInfo > > AddPropertyGroup(std::shared_ptr< PropertyGroup > property_group) const
Definition: graph_info.cc:357
bool HasProperty(const std::string &property_name) const
Definition: graph_info.cc:330
bool HasPropertyGroup(const std::shared_ptr< PropertyGroup > &property_group) const
Definition: graph_info.cc:335
Result< std::string > GetFilePath(std::shared_ptr< PropertyGroup > property_group, IdType chunk_index) const
Definition: graph_info.cc:271
bool IsPrimaryKey(const std::string &property_name) const
Definition: graph_info.cc:314
bool IsValidated() const
Definition: graph_info.cc:374
int PropertyGroupNum() const
Definition: graph_info.cc:292
const std::string & GetPrefix() const
Definition: graph_info.cc:265
const std::string & GetLabel() const
Definition: graph_info.cc:261
Result< std::string > GetVerticesNumFilePath() const
Definition: graph_info.cc:288
VertexInfo(const std::string &label, IdType chunk_size, const PropertyGroupVector &property_groups, const std::string &prefix="", std::shared_ptr< const InfoVersion > version=nullptr)
Definition: graph_info.cc:253
Result< std::shared_ptr< DataType > > GetPropertyType(const std::string &property_name) const
Definition: graph_info.cc:348
bool IsNullableKey(const std::string &property_name) const
Definition: graph_info.cc:322
Result< std::string > Dump() const noexcept
Definition: graph_info.cc:442
static Result< std::shared_ptr< VertexInfo > > Load(std::shared_ptr< Yaml > yaml)
Definition: graph_info.cc:387
std::shared_ptr< PropertyGroup > GetPropertyGroupByIndex(int index) const
Definition: graph_info.cc:302
Status Save(const std::string &file_name) const
Definition: graph_info.cc:480
const PropertyGroupVector & GetPropertyGroups() const
Definition: graph_info.cc:310