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  Cardinality
41  cardinality; // cardinality of the property, only use in vertex info
42 
43  Property() = default;
44 
45  explicit Property(const std::string& name,
46  const std::shared_ptr<DataType>& type = nullptr,
47  bool is_primary = false, bool is_nullable = true,
48  Cardinality cardinality = Cardinality::SINGLE)
49  : name(name),
50  type(type),
51  is_primary(is_primary),
52  is_nullable(!is_primary && is_nullable),
53  cardinality(cardinality) {}
54 };
55 
56 bool operator==(const Property& lhs, const Property& rhs);
57 
66  public:
76  explicit PropertyGroup(const std::vector<Property>& properties,
77  FileType file_type, const std::string& prefix = "");
78 
84  const std::vector<Property>& GetProperties() const;
85 
86  bool HasProperty(const std::string& property_name) const;
87 
92  inline FileType GetFileType() const { return file_type_; }
93 
98  inline const std::string& GetPrefix() const { return prefix_; }
99 
103  bool IsValidated() const;
104 
105  friend std::ostream& operator<<(std::ostream& stream,
106  const PropertyGroup& pg) {
107  for (size_t i = 0; i < pg.properties_.size(); ++i) {
108  stream << pg.properties_[i].name;
109  if (i != pg.properties_.size() - 1) {
110  stream << "_";
111  }
112  }
113  return stream;
114  }
115 
116  private:
117  std::vector<Property> properties_;
118  FileType file_type_;
119  std::string prefix_;
120 };
121 
122 bool operator==(const PropertyGroup& lhs, const PropertyGroup& rhs);
123 
128  public:
138  explicit AdjacentList(AdjListType type, FileType file_type,
139  const std::string& prefix = "");
140 
146  inline AdjListType GetType() const { return type_; }
147 
153  inline FileType GetFileType() const { return file_type_; }
154 
160  inline const std::string& GetPrefix() const { return prefix_; }
161 
167  bool IsValidated() const;
168 
169  private:
170  AdjListType type_;
171  FileType file_type_;
172  std::string prefix_;
173 };
174 
180 class VertexInfo {
181  public:
194  explicit VertexInfo(const std::string& type, IdType chunk_size,
195  const PropertyGroupVector& property_groups,
196  const std::vector<std::string>& labels = {},
197  const std::string& prefix = "",
198  std::shared_ptr<const InfoVersion> version = nullptr);
199 
200  ~VertexInfo();
201 
207  Result<std::shared_ptr<VertexInfo>> AddPropertyGroup(
208  std::shared_ptr<PropertyGroup> property_group) const;
209 
215  const std::string& GetType() const;
216 
222  IdType GetChunkSize() const;
223 
229  const std::string& GetPrefix() const;
230 
236  const std::shared_ptr<const InfoVersion>& version() const;
237 
242  const std::vector<std::string>& GetLabels() const;
243 
249  int PropertyGroupNum() const;
250 
254  const PropertyGroupVector& GetPropertyGroups() const;
255 
262  std::shared_ptr<PropertyGroup> GetPropertyGroup(
263  const std::string& property_name) const;
264 
271  std::shared_ptr<PropertyGroup> GetPropertyGroupByIndex(int index) const;
272 
280  Result<std::shared_ptr<DataType>> GetPropertyType(
281  const std::string& property_name) const;
282 
283  Result<Cardinality> GetPropertyCardinality(
284  const std::string& property_name) const;
291  bool HasProperty(const std::string& property_name) const;
292 
299  Status Save(const std::string& file_name) const;
300 
307  Result<std::string> Dump() const noexcept;
308 
315  bool IsPrimaryKey(const std::string& property_name) const;
316 
323  bool IsNullableKey(const std::string& property_name) const;
324 
332  bool HasPropertyGroup(
333  const std::shared_ptr<PropertyGroup>& property_group) const;
334 
343  Result<std::string> GetFilePath(std::shared_ptr<PropertyGroup> property_group,
344  IdType chunk_index) const;
345 
353  Result<std::string> GetPathPrefix(
354  std::shared_ptr<PropertyGroup> property_group) const;
355 
361  Result<std::string> GetVerticesNumFilePath() const;
362 
368  bool IsValidated() const;
369 
377  static Result<std::shared_ptr<VertexInfo>> Load(std::shared_ptr<Yaml> yaml);
378 
385  static Result<std::shared_ptr<VertexInfo>> Load(const std::string& input);
386 
387  private:
388  class Impl;
389  std::unique_ptr<Impl> impl_;
390 };
391 
398 class EdgeInfo {
399  public:
417  explicit EdgeInfo(const std::string& src_type, const std::string& edge_type,
418  const std::string& dst_type, IdType chunk_size,
419  IdType src_chunk_size, IdType dst_chunk_size, bool directed,
420  const AdjacentListVector& adjacent_lists,
421  const PropertyGroupVector& property_groups,
422  const std::string& prefix = "",
423  std::shared_ptr<const InfoVersion> version = nullptr);
424 
425  ~EdgeInfo();
426 
434  Result<std::shared_ptr<EdgeInfo>> AddAdjacentList(
435  std::shared_ptr<AdjacentList> adj_list) const;
436 
442  Result<std::shared_ptr<EdgeInfo>> AddPropertyGroup(
443  std::shared_ptr<PropertyGroup> property_group) const;
444 
449  const std::string& GetSrcType() const;
450 
455  const std::string& GetEdgeType() const;
456 
461  const std::string& GetDstType() const;
462 
467  IdType GetChunkSize() const;
468 
473  IdType GetSrcChunkSize() const;
474 
479  IdType GetDstChunkSize() const;
480 
485  const std::string& GetPrefix() const;
486 
491  bool IsDirected() const;
492 
497  const std::shared_ptr<const InfoVersion>& version() const;
498 
506  bool HasAdjacentListType(AdjListType adj_list_type) const;
507 
514  bool HasProperty(const std::string& property_name) const;
515 
519  bool HasPropertyGroup(
520  const std::shared_ptr<PropertyGroup>& property_group) const;
521 
522  std::shared_ptr<AdjacentList> GetAdjacentList(
523  AdjListType adj_list_type) const;
524 
528  int PropertyGroupNum() const;
529 
534  const PropertyGroupVector& GetPropertyGroups() const;
535 
542  std::shared_ptr<PropertyGroup> GetPropertyGroup(
543  const std::string& property) const;
544 
551  std::shared_ptr<PropertyGroup> GetPropertyGroupByIndex(int index) const;
552 
560  Result<std::string> GetVerticesNumFilePath(AdjListType adj_list_type) const;
561 
570  Result<std::string> GetEdgesNumFilePath(IdType vertex_chunk_index,
571  AdjListType adj_list_type) const;
572 
580  Result<std::string> GetAdjListFilePath(IdType vertex_chunk_index,
581  IdType edge_chunk_index,
582  AdjListType adj_list_type) const;
583 
591  Result<std::string> GetAdjListPathPrefix(AdjListType adj_list_type) const;
592 
600  Result<std::string> GetAdjListOffsetFilePath(IdType vertex_chunk_index,
601  AdjListType adj_list_type) const;
602 
610  Result<std::string> GetOffsetPathPrefix(AdjListType adj_list_type) const;
611 
622  Result<std::string> GetPropertyFilePath(
623  const std::shared_ptr<PropertyGroup>& property_group,
624  AdjListType adj_list_type, IdType vertex_chunk_index,
625  IdType edge_chunk_index) const;
626 
635  Result<std::string> GetPropertyGroupPathPrefix(
636  const std::shared_ptr<PropertyGroup>& property_group,
637  AdjListType adj_list_type) const;
638 
646  Result<std::shared_ptr<DataType>> GetPropertyType(
647  const std::string& property_name) const;
654  bool IsPrimaryKey(const std::string& property_name) const;
655 
662  bool IsNullableKey(const std::string& property_name) const;
663 
670  Status Save(const std::string& file_name) const;
671 
678  Result<std::string> Dump() const noexcept;
679 
685  bool IsValidated() const;
686 
688  static Result<std::shared_ptr<EdgeInfo>> Load(std::shared_ptr<Yaml> yaml);
689 
696  static Result<std::shared_ptr<EdgeInfo>> Load(const std::string& input);
697 
698  private:
699  class Impl;
700  std::unique_ptr<Impl> impl_;
701 };
702 
706 class GraphInfo {
707  public:
719  explicit GraphInfo(
720  const std::string& graph_name, VertexInfoVector vertex_infos,
721  EdgeInfoVector edge_infos, const std::vector<std::string>& labels = {},
722  const std::string& prefix = "./",
723  std::shared_ptr<const InfoVersion> version = nullptr,
724  const std::unordered_map<std::string, std::string>& extra_info = {});
725 
726  ~GraphInfo();
727 
734  static Result<std::shared_ptr<GraphInfo>> Load(const std::string& path);
735 
743  static Result<std::shared_ptr<GraphInfo>> Load(
744  const std::string& input, const std::string& relative_path);
745 
754  Result<std::shared_ptr<GraphInfo>> AddVertex(
755  std::shared_ptr<VertexInfo> vertex_info) const;
756 
765  Result<std::shared_ptr<GraphInfo>> AddEdge(
766  std::shared_ptr<EdgeInfo> edge_info) const;
767 
772  const std::string& GetName() const;
773 
778  const std::vector<std::string>& GetLabels() const;
779 
784  const std::string& GetPrefix() const;
785 
791  const std::shared_ptr<const InfoVersion>& version() const;
792 
798  const std::unordered_map<std::string, std::string>& GetExtraInfo() const;
799 
805  std::shared_ptr<VertexInfo> GetVertexInfo(const std::string& type) const;
806 
815  std::shared_ptr<EdgeInfo> GetEdgeInfo(const std::string& src_type,
816  const std::string& edge_type,
817  const std::string& dst_type) const;
818 
822  int GetVertexInfoIndex(const std::string& type) const;
823 
828  int GetEdgeInfoIndex(const std::string& src_type,
829  const std::string& edge_type,
830  const std::string& dst_type) const;
831 
835  int VertexInfoNum() const;
836 
840  int EdgeInfoNum() const;
841 
848  const std::shared_ptr<VertexInfo> GetVertexInfoByIndex(int index) const;
849 
856  const std::shared_ptr<EdgeInfo> GetEdgeInfoByIndex(int index) const;
857 
863  const VertexInfoVector& GetVertexInfos() const;
864 
870  const EdgeInfoVector& GetEdgeInfos() const;
871 
878  Status Save(const std::string& path) const;
879 
886  Result<std::string> Dump() const;
887 
893  bool IsValidated() const;
894 
895  private:
896  class Impl;
897  std::unique_ptr<Impl> impl_;
898 };
899 
900 } // namespace graphar
const std::string & GetPrefix() const
Get the prefix of adjacent list.
Definition: graph_info.h:160
FileType GetFileType() const
Get the file type of adjacent list.
Definition: graph_info.h:153
AdjListType GetType() const
Get the type of adjacent list.
Definition: graph_info.h:146
bool IsValidated() const
Definition: graph_info.cc:176
AdjacentList(AdjListType type, FileType file_type, const std::string &prefix="")
Definition: graph_info.cc:168
EdgeInfo is a class to describe the edge information, including the source vertex type,...
Definition: graph_info.h:398
const std::string & GetEdgeType() const
Definition: graph_info.cc:658
static Result< std::shared_ptr< EdgeInfo > > Load(std::shared_ptr< Yaml > yaml)
Definition: graph_info.cc:886
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:746
Status Save(const std::string &file_name) const
Definition: graph_info.cc:1027
Result< std::shared_ptr< DataType > > GetPropertyType(const std::string &property_name) const
Definition: graph_info.cc:807
bool IsValidated() const
Definition: graph_info.cc:867
const std::string & GetPrefix() const
Definition: graph_info.cc:668
Result< std::string > GetEdgesNumFilePath(IdType vertex_chunk_index, AdjListType adj_list_type) const
Definition: graph_info.cc:738
Result< std::string > GetPropertyGroupPathPrefix(const std::shared_ptr< PropertyGroup > &property_group, AdjListType adj_list_type) const
Definition: graph_info.cc:795
bool IsPrimaryKey(const std::string &property_name) const
Definition: graph_info.cc:816
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:764
IdType GetChunkSize() const
Definition: graph_info.cc:662
bool HasProperty(const std::string &property_name) const
Returns whether the edge info contains the given property.
Definition: graph_info.cc:681
Result< std::shared_ptr< EdgeInfo > > AddAdjacentList(std::shared_ptr< AdjacentList > adj_list) const
Definition: graph_info.cc:832
Result< std::string > Dump() const noexcept
Definition: graph_info.cc:972
bool IsNullableKey(const std::string &property_name) const
Definition: graph_info.cc:824
std::shared_ptr< PropertyGroup > GetPropertyGroupByIndex(int index) const
Get the property group at the specified index.
Definition: graph_info.cc:722
bool HasPropertyGroup(const std::shared_ptr< PropertyGroup > &property_group) const
Returns whether the edge info contains the given property group.
Definition: graph_info.cc:686
const std::shared_ptr< const InfoVersion > & version() const
Definition: graph_info.cc:672
Result< std::shared_ptr< EdgeInfo > > AddPropertyGroup(std::shared_ptr< PropertyGroup > property_group) const
Definition: graph_info.cc:848
const std::string & GetSrcType() const
Definition: graph_info.cc:656
bool HasAdjacentListType(AdjListType adj_list_type) const
Definition: graph_info.cc:676
Result< std::string > GetOffsetPathPrefix(AdjListType adj_list_type) const
Definition: graph_info.cc:772
bool IsDirected() const
Definition: graph_info.cc:670
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:756
EdgeInfo(const std::string &src_type, const std::string &edge_type, const std::string &dst_type, 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:643
IdType GetDstChunkSize() const
Definition: graph_info.cc:666
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:780
const std::string & GetDstType() const
Definition: graph_info.cc:660
IdType GetSrcChunkSize() const
Definition: graph_info.cc:664
const PropertyGroupVector & GetPropertyGroups() const
Get the property groups.
Definition: graph_info.cc:712
Result< std::string > GetVerticesNumFilePath(AdjListType adj_list_type) const
Get the file path for the number of vertices.
Definition: graph_info.cc:730
std::shared_ptr< PropertyGroup > GetPropertyGroup(const std::string &property) const
Get the property group containing the given property.
Definition: graph_info.cc:716
int PropertyGroupNum() const
Get the number of property groups.
Definition: graph_info.cc:708
int GetVertexInfoIndex(const std::string &type) const
Get the vertex info index with the given type.
Definition: graph_info.cc:1217
const EdgeInfoVector & GetEdgeInfos() const
Get the edge infos of graph info.
Definition: graph_info.cc:1262
GraphInfo(const std::string &graph_name, VertexInfoVector vertex_infos, EdgeInfoVector edge_infos, const std::vector< std::string > &labels={}, 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:1184
Status Save(const std::string &path) const
Definition: graph_info.cc:1384
const std::shared_ptr< VertexInfo > GetVertexInfoByIndex(int index) const
Get the vertex info at the specified index.
Definition: graph_info.cc:1243
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:1268
const std::string & GetPrefix() const
Get the absolute path prefix of the chunk files.
Definition: graph_info.cc:1200
static Result< std::shared_ptr< GraphInfo > > Load(const std::string &path)
Loads the input file as a GraphInfo instance.
Definition: graph_info.cc:1308
bool IsValidated() const
Definition: graph_info.cc:1266
Result< std::string > Dump() const
Definition: graph_info.cc:1334
std::shared_ptr< EdgeInfo > GetEdgeInfo(const std::string &src_type, const std::string &edge_type, const std::string &dst_type) const
Get the edge info with the given source vertex type, edge type, and destination vertex type.
Definition: graph_info.cc:1221
int EdgeInfoNum() const
Get the number of edge infos.
Definition: graph_info.cc:1239
std::shared_ptr< VertexInfo > GetVertexInfo(const std::string &type) const
Get the vertex info with the given type.
Definition: graph_info.cc:1211
const std::vector< std::string > & GetLabels() const
Get the vertex labels of the graph.
Definition: graph_info.cc:1196
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:1281
const VertexInfoVector & GetVertexInfos() const
Get the vertex infos of graph info.
Definition: graph_info.cc:1258
const std::unordered_map< std::string, std::string > & GetExtraInfo() const
Get the extra metadata of the graph info object.
Definition: graph_info.cc:1206
int GetEdgeInfoIndex(const std::string &src_type, const std::string &edge_type, const std::string &dst_type) const
Get the edge info index with the given source vertex type, edge type, and destination type.
Definition: graph_info.cc:1228
int VertexInfoNum() const
Get the number of vertex infos.
Definition: graph_info.cc:1235
const std::string & GetName() const
Get the name of the graph.
Definition: graph_info.cc:1194
const std::shared_ptr< const InfoVersion > & version() const
Get the version info of the graph info object.
Definition: graph_info.cc:1202
const std::shared_ptr< EdgeInfo > GetEdgeInfoByIndex(int index) const
Get the edge info at the specified index.
Definition: graph_info.cc:1251
const std::string & GetPrefix() const
Definition: graph_info.h:98
bool IsValidated() const
Definition: graph_info.cc:118
FileType GetFileType() const
Definition: graph_info.h:92
PropertyGroup(const std::vector< Property > &properties, FileType file_type, const std::string &prefix="")
Definition: graph_info.cc:94
const std::vector< Property > & GetProperties() const
Definition: graph_info.cc:105
Status outcome object (success or error)
Definition: status.h:123
VertexInfo is a class to describe the vertex information, including the vertex type,...
Definition: graph_info.h:180
const std::string & GetType() const
Definition: graph_info.cc:275
std::shared_ptr< PropertyGroup > GetPropertyGroup(const std::string &property_name) const
Definition: graph_info.cc:314
Result< std::string > GetPathPrefix(std::shared_ptr< PropertyGroup > property_group) const
Definition: graph_info.cc:298
const std::vector< std::string > & GetLabels() const
Definition: graph_info.cc:281
IdType GetChunkSize() const
Definition: graph_info.cc:277
const std::shared_ptr< const InfoVersion > & version() const
Definition: graph_info.cc:285
Result< std::shared_ptr< VertexInfo > > AddPropertyGroup(std::shared_ptr< PropertyGroup > property_group) const
Definition: graph_info.cc:384
bool HasProperty(const std::string &property_name) const
Definition: graph_info.cc:348
bool HasPropertyGroup(const std::shared_ptr< PropertyGroup > &property_group) const
Definition: graph_info.cc:353
Result< std::string > GetFilePath(std::shared_ptr< PropertyGroup > property_group, IdType chunk_index) const
Definition: graph_info.cc:289
bool IsPrimaryKey(const std::string &property_name) const
Definition: graph_info.cc:332
bool IsValidated() const
Definition: graph_info.cc:401
int PropertyGroupNum() const
Definition: graph_info.cc:310
const std::string & GetPrefix() const
Definition: graph_info.cc:279
Result< std::string > GetVerticesNumFilePath() const
Definition: graph_info.cc:306
Result< std::shared_ptr< DataType > > GetPropertyType(const std::string &property_name) const
Definition: graph_info.cc:366
VertexInfo(const std::string &type, IdType chunk_size, const PropertyGroupVector &property_groups, const std::vector< std::string > &labels={}, const std::string &prefix="", std::shared_ptr< const InfoVersion > version=nullptr)
Definition: graph_info.cc:265
bool IsNullableKey(const std::string &property_name) const
Definition: graph_info.cc:340
Result< std::string > Dump() const noexcept
Definition: graph_info.cc:482
static Result< std::shared_ptr< VertexInfo > > Load(std::shared_ptr< Yaml > yaml)
Definition: graph_info.cc:415
std::shared_ptr< PropertyGroup > GetPropertyGroupByIndex(int index) const
Definition: graph_info.cc:320
Status Save(const std::string &file_name) const
Definition: graph_info.cc:530
const PropertyGroupVector & GetPropertyGroups() const
Definition: graph_info.cc:328