Flex  0.17.9
schema.h
Go to the documentation of this file.
1 
16 #ifndef GRAPHSCOPE_FRAGMENT_SCHEMA_H_
17 #define GRAPHSCOPE_FRAGMENT_SCHEMA_H_
18 
19 #include "flex/engines/hqps_db/core/utils/hqps_utils.h"
21 #include "flex/utils/id_indexer.h"
24 #include "flex/utils/result.h"
25 #include "flex/utils/yaml_utils.h"
26 
27 namespace gs {
28 
29 class Schema {
30  public:
31  // How many built-in plugins are there.
32  // Currently only one builtin plugin, SERVER_APP is supported.
33  static constexpr uint8_t RESERVED_PLUGIN_NUM = 1;
34  static constexpr uint8_t MAX_PLUGIN_ID = 245;
35  static constexpr uint8_t ADHOC_READ_PLUGIN_ID = 253;
36  static constexpr uint8_t HQPS_ADHOC_READ_PLUGIN_ID = 254;
37  static constexpr uint8_t HQPS_ADHOC_WRITE_PLUGIN_ID = 255;
38 
39  static constexpr uint8_t CYPHER_READ_PLUGIN_ID = 248;
40  static constexpr uint8_t CYPHER_WRITE_PLUGIN_ID = 247;
41  static constexpr uint8_t CYPHER_READ_DEBUG_PLUGIN_ID = 246;
42  static constexpr const char* HQPS_ADHOC_READ_PLUGIN_ID_STR = "\xFE";
43  static constexpr const char* HQPS_ADHOC_WRITE_PLUGIN_ID_STR = "\xFF";
44  static constexpr const char* ADHOC_READ_PLUGIN_ID_STR = "\xFD";
45  static constexpr const char* CYPHER_READ_DEBUG_PLUGIN_ID_STR = "\xF6";
46  static constexpr const char* PRIMITIVE_TYPE_KEY = "primitive_type";
47  static constexpr const char* VARCHAR_KEY = "varchar";
48  static constexpr const char* MAX_LENGTH_KEY = "max_length";
49 
50  // The builtin plugins are reserved for the system.
51  static constexpr uint8_t BUILTIN_PLUGIN_NUM = 4;
52 
53  static constexpr uint8_t BUILTIN_COUNT_VERTICES_PLUGIN_ID = 252;
54  static constexpr const char* BUILTIN_COUNT_VERTICES_PLUGIN_NAME =
55  "count_vertices";
56  static constexpr uint8_t BUILTIN_PAGERANK_PLUGIN_ID = 251;
57  static constexpr const char* BUILTIN_PAGERANK_PLUGIN_NAME = "pagerank";
58  static constexpr uint8_t BUILTIN_K_DEGREE_NEIGHBORS_PLUGIN_ID = 250;
59  static constexpr const char* BUILTIN_K_DEGREE_NEIGHBORS_PLUGIN_NAME =
60  "k_neighbors";
61  static constexpr uint8_t BUILTIN_TVSP_PLUGIN_ID = 249;
62  static constexpr const char* BUILTIN_TVSP_PLUGIN_NAME =
63  "shortest_path_among_three";
64  static constexpr const char* BUILTIN_PLUGIN_NAMES[BUILTIN_PLUGIN_NUM] = {
67  static constexpr uint8_t BUILTIN_PLUGIN_IDS[BUILTIN_PLUGIN_NUM] = {
70 
71  // An array containing all compatible versions of schema.
72  static const std::vector<std::string> COMPATIBLE_VERSIONS;
73  static constexpr const char* DEFAULT_SCHEMA_VERSION = "v0.0";
74 
75  static bool IsBuiltinPlugin(const std::string& plugin_name);
76 
78  Schema();
80 
81  static const std::vector<std::string>& GetCompatibleVersions();
82 
83  void Clear();
84 
85  void add_vertex_label(
86  const std::string& label, const std::vector<PropertyType>& property_types,
87  const std::vector<std::string>& property_names,
88  const std::vector<std::tuple<PropertyType, std::string, size_t>>&
89  primary_key,
90  const std::vector<StorageStrategy>& strategies = {},
91  size_t max_vnum = static_cast<size_t>(1) << 32,
92  const std::string& description = "");
93 
94  void add_edge_label(const std::string& src_label,
95  const std::string& dst_label,
96  const std::string& edge_label,
97  const std::vector<PropertyType>& properties,
98  const std::vector<std::string>& prop_names,
101  bool oe_mutable = true, bool ie_mutable = true,
102  bool sort_on_compaction = false,
103  const std::string& description = "");
104 
105  label_t vertex_label_num() const;
106 
107  label_t edge_label_num() const;
108 
109  bool contains_vertex_label(const std::string& label) const;
110 
111  label_t get_vertex_label_id(const std::string& label) const;
112 
114  label_t label_id, const std::vector<PropertyType>& types,
115  const std::vector<StorageStrategy>& strategies = {});
116 
117  const std::vector<PropertyType>& get_vertex_properties(
118  const std::string& label) const;
119 
120  const std::vector<std::string>& get_vertex_property_names(
121  const std::string& label) const;
122 
123  const std::string& get_vertex_description(const std::string& label) const;
124 
125  const std::vector<PropertyType>& get_vertex_properties(label_t label) const;
126 
127  const std::vector<std::string>& get_vertex_property_names(
128  label_t label) const;
129 
130  const std::string& get_vertex_description(label_t label) const;
131 
132  const std::vector<StorageStrategy>& get_vertex_storage_strategies(
133  const std::string& label) const;
134 
135  size_t get_max_vnum(const std::string& label) const;
136 
137  bool exist(const std::string& src_label, const std::string& dst_label,
138  const std::string& edge_label) const;
139 
140  bool exist(label_type src_label, label_type dst_label,
141  label_type edge_label) const;
142 
143  const std::vector<PropertyType>& get_edge_properties(
144  const std::string& src_label, const std::string& dst_label,
145  const std::string& label) const;
146 
147  const std::vector<PropertyType>& get_edge_properties(label_t src_label,
148  label_t dst_label,
149  label_t label) const;
150 
151  const std::string& get_compiler_path() const;
152 
153  void set_compiler_path(const std::string& compiler_path);
154 
155  std::string get_edge_description(const std::string& src_label,
156  const std::string& dst_label,
157  const std::string& label) const;
158 
159  std::string get_edge_description(label_t src_label, label_t dst_label,
160  label_t label) const;
161 
162  PropertyType get_edge_property(label_t src, label_t dst, label_t edge) const;
163 
164  const std::vector<std::string>& get_edge_property_names(
165  const std::string& src_label, const std::string& dst_label,
166  const std::string& label) const;
167 
168  const std::vector<std::string>& get_edge_property_names(
169  const label_t& src_label, const label_t& dst_label,
170  const label_t& label) const;
171 
172  bool vertex_has_property(const std::string& label,
173  const std::string& prop) const;
174 
175  bool vertex_has_primary_key(const std::string& label,
176  const std::string& prop) const;
177 
178  bool edge_has_property(const std::string& src_label,
179  const std::string& dst_label,
180  const std::string& edge_label,
181  const std::string& prop) const;
182 
183  bool has_vertex_label(const std::string& label) const;
184 
185  bool has_edge_label(const std::string& src_label,
186  const std::string& dst_label,
187  const std::string& edge_label) const;
188 
189  bool has_edge_label(label_t src_label, label_t dst_label,
190  label_t edge_label) const;
191 
192  bool valid_edge_property(const std::string& src_label,
193  const std::string& dst_label,
194  const std::string& label) const;
195 
196  EdgeStrategy get_outgoing_edge_strategy(const std::string& src_label,
197  const std::string& dst_label,
198  const std::string& label) const;
199 
200  EdgeStrategy get_incoming_edge_strategy(const std::string& src_label,
201  const std::string& dst_label,
202  const std::string& label) const;
203 
205  label_t dst_label,
206  label_t label) const {
207  uint32_t index = generate_edge_label(src_label, dst_label, label);
208  return oe_strategy_.at(index);
209  }
210 
212  label_t dst_label,
213  label_t label) const {
214  uint32_t index = generate_edge_label(src_label, dst_label, label);
215  return ie_strategy_.at(index);
216  }
217 
218  bool outgoing_edge_mutable(const std::string& src_label,
219  const std::string& dst_label,
220  const std::string& label) const;
221 
222  bool incoming_edge_mutable(const std::string& src_label,
223  const std::string& dst_label,
224  const std::string& label) const;
225 
226  bool get_sort_on_compaction(const std::string& src_label,
227  const std::string& dst_label,
228  const std::string& label) const;
229 
230  bool contains_edge_label(const std::string& label) const;
231 
232  label_t get_edge_label_id(const std::string& label) const;
233 
234  std::string get_vertex_label_name(label_t index) const;
235 
236  std::string get_edge_label_name(label_t index) const;
237 
238  const std::vector<std::tuple<PropertyType, std::string, size_t>>&
239  get_vertex_primary_key(label_t index) const;
240 
241  const std::string& get_vertex_primary_key_name(label_t index) const;
242 
243  void Serialize(std::unique_ptr<grape::LocalIOAdaptor>& writer) const;
244 
245  void Deserialize(std::unique_ptr<grape::LocalIOAdaptor>& reader);
246 
247  static Result<Schema> LoadFromYaml(const std::string& schema_config);
248 
249  static Result<Schema> LoadFromYamlNode(const YAML::Node& schema_node);
250 
251  bool Equals(const Schema& other) const;
252 
253  // Return the map from plugin name to plugin id
254  const std::unordered_map<std::string, std::pair<std::string, uint8_t>>&
255  GetPlugins() const;
256 
257  bool EmplacePlugins(
258  const std::vector<std::pair<std::string, std::string>>& plugins);
259 
260  void SetPluginDir(const std::string& plugin_dir);
261 
262  void RemovePlugin(const std::string& plugin_name);
263 
264  std::string GetPluginDir() const;
265 
266  inline void SetGraphName(const std::string& name) { name_ = name; }
267 
268  inline void SetGraphId(const std::string& id) { id_ = id; }
269 
270  inline std::string GetGraphName() const { return name_; }
271 
272  inline std::string GetGraphId() const { return id_; }
273 
274  std::string GetDescription() const;
275 
276  void SetDescription(const std::string& description);
277 
278  void SetRemotePath(const std::string& remote_path);
279 
280  inline std::string GetRemotePath() const { return remote_path_; }
281 
282  void SetVersion(const std::string& version);
283 
284  std::string GetVersion() const;
285 
286  bool has_multi_props_edge() const;
287 
288  const std::unordered_map<std::string, std::pair<PropertyType, uint8_t>>&
290 
291  private:
292  label_t vertex_label_to_index(const std::string& label);
293 
294  label_t edge_label_to_index(const std::string& label);
295 
296  uint32_t generate_edge_label(label_t src, label_t dst, label_t edge) const;
297 
298  std::string name_, id_;
301  std::vector<std::vector<PropertyType>> vproperties_;
302  std::vector<std::vector<std::string>> vprop_names_;
303  std::vector<std::string> v_descriptions_;
304  std::vector<std::vector<std::tuple<PropertyType, std::string, size_t>>>
305  v_primary_keys_; // the third element is the index of the property in the
306  // vertex property list
307  std::vector<std::vector<StorageStrategy>> vprop_storage_;
308  std::map<uint32_t, std::vector<PropertyType>> eproperties_;
309  std::map<uint32_t, std::vector<std::string>> eprop_names_;
310  std::map<uint32_t, std::string> e_descriptions_;
311  std::map<uint32_t, EdgeStrategy> oe_strategy_;
312  std::map<uint32_t, EdgeStrategy> ie_strategy_;
313  std::map<uint32_t, bool> oe_mutability_;
314  std::map<uint32_t, bool> ie_mutability_;
315  std::map<uint32_t, bool> sort_on_compactions_;
316  std::vector<std::unordered_map<std::string, std::pair<PropertyType, uint8_t>>>
318  std::vector<size_t> max_vnum_;
319  std::unordered_map<std::string, std::pair<std::string, uint8_t>>
320  plugin_name_to_path_and_id_; // key is plugin_name, value is plugin_path
321  // and plugin_id
322  std::string plugin_dir_;
323  std::string description_;
324  std::string version_;
325  std::string compiler_path_;
326  std::string remote_path_; // The path to the data on the remote storage
328 };
329 
330 } // namespace gs
331 
332 #endif // GRAPHSCOPE_FRAGMENT_SCHEMA_H_
Definition: result.h:63
Definition: schema.h:29
bool get_sort_on_compaction(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:347
const std::string & get_vertex_description(const std::string &label) const
Definition: schema.cc:165
const std::vector< PropertyType > & get_vertex_properties(const std::string &label) const
Definition: schema.cc:138
static constexpr const char * MAX_LENGTH_KEY
Definition: schema.h:48
std::map< uint32_t, bool > oe_mutability_
Definition: schema.h:313
std::map< uint32_t, std::string > e_descriptions_
Definition: schema.h:310
bool contains_edge_label(const std::string &label) const
Definition: schema.cc:369
bool has_edge_label(const std::string &src_label, const std::string &dst_label, const std::string &edge_label) const
Definition: schema.cc:1465
std::string GetVersion() const
Definition: schema.cc:1411
static constexpr uint8_t BUILTIN_COUNT_VERTICES_PLUGIN_ID
Definition: schema.h:53
void SetDescription(const std::string &description)
Definition: schema.cc:1402
EdgeStrategy get_outgoing_edge_strategy(label_t src_label, label_t dst_label, label_t label) const
Definition: schema.h:204
bool edge_has_property(const std::string &src_label, const std::string &dst_label, const std::string &edge_label, const std::string &prop) const
Definition: schema.cc:1441
const std::vector< PropertyType > & get_edge_properties(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:216
const std::unordered_map< std::string, std::pair< std::string, uint8_t > > & GetPlugins() const
Definition: schema.cc:1274
std::vector< std::vector< std::tuple< PropertyType, std::string, size_t > > > v_primary_keys_
Definition: schema.h:305
std::vector< std::unordered_map< std::string, std::pair< PropertyType, uint8_t > > > vprop_name_to_type_and_index_
Definition: schema.h:317
std::string GetGraphId() const
Definition: schema.h:272
static const std::vector< std::string > COMPATIBLE_VERSIONS
Definition: schema.h:72
std::map< uint32_t, bool > ie_mutability_
Definition: schema.h:314
void set_vertex_properties(label_t label_id, const std::vector< PropertyType > &types, const std::vector< StorageStrategy > &strategies={})
Definition: schema.cc:130
const std::vector< StorageStrategy > & get_vertex_storage_strategies(const std::string &label) const
Definition: schema.cc:180
bool vertex_has_primary_key(const std::string &label, const std::string &prop) const
Definition: schema.cc:1427
static constexpr const char * BUILTIN_PAGERANK_PLUGIN_NAME
Definition: schema.h:57
const std::string & get_compiler_path() const
Definition: schema.cc:39
std::vector< std::string > v_descriptions_
Definition: schema.h:303
bool incoming_edge_mutable(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:337
void Clear()
Definition: schema.cc:41
IdIndexer< std::string, label_t > elabel_indexer_
Definition: schema.h:300
void SetRemotePath(const std::string &remote_path)
Definition: schema.cc:1406
static constexpr const char * HQPS_ADHOC_WRITE_PLUGIN_ID_STR
Definition: schema.h:43
const std::string & get_vertex_primary_key_name(label_t index) const
Definition: schema.cc:398
static constexpr uint8_t BUILTIN_TVSP_PLUGIN_ID
Definition: schema.h:61
static constexpr uint8_t BUILTIN_PLUGIN_NUM
Definition: schema.h:51
std::string version_
Definition: schema.h:324
std::map< uint32_t, bool > sort_on_compactions_
Definition: schema.h:315
size_t get_max_vnum(const std::string &label) const
Definition: schema.cc:191
static constexpr uint8_t ADHOC_READ_PLUGIN_ID
Definition: schema.h:35
EdgeStrategy get_outgoing_edge_strategy(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:307
label_t vertex_label_to_index(const std::string &label)
Definition: schema.cc:449
void SetGraphName(const std::string &name)
Definition: schema.h:266
IdIndexer< std::string, label_t > vlabel_indexer_
Definition: schema.h:299
void SetVersion(const std::string &version)
Definition: schema.cc:1410
std::unordered_map< std::string, std::pair< std::string, uint8_t > > plugin_name_to_path_and_id_
Definition: schema.h:320
std::map< uint32_t, std::vector< PropertyType > > eproperties_
Definition: schema.h:308
static constexpr uint8_t MAX_PLUGIN_ID
Definition: schema.h:34
static constexpr const char * BUILTIN_TVSP_PLUGIN_NAME
Definition: schema.h:62
void SetGraphId(const std::string &id)
Definition: schema.h:268
label_t edge_label_num() const
Definition: schema.cc:114
static constexpr uint8_t CYPHER_READ_PLUGIN_ID
Definition: schema.h:39
bool has_vertex_label(const std::string &label) const
Definition: schema.cc:1460
void Deserialize(std::unique_ptr< grape::LocalIOAdaptor > &reader)
Definition: schema.cc:422
label_t get_vertex_label_id(const std::string &label) const
Definition: schema.cc:123
bool has_multi_props_edge() const
Definition: schema.cc:1413
static constexpr const char * ADHOC_READ_PLUGIN_ID_STR
Definition: schema.h:44
bool vertex_has_property(const std::string &label, const std::string &prop) const
Definition: schema.cc:1416
std::string get_vertex_label_name(label_t index) const
Definition: schema.cc:374
std::map< uint32_t, std::vector< std::string > > eprop_names_
Definition: schema.h:309
std::string id_
Definition: schema.h:298
label_t label_type
Definition: schema.h:77
void Serialize(std::unique_ptr< grape::LocalIOAdaptor > &writer) const
Definition: schema.cc:409
label_t edge_label_to_index(const std::string &label)
Definition: schema.cc:464
static const std::vector< std::string > & GetCompatibleVersions()
Definition: schema.cc:1514
static constexpr const char * BUILTIN_K_DEGREE_NEIGHBORS_PLUGIN_NAME
Definition: schema.h:59
static constexpr uint8_t RESERVED_PLUGIN_NUM
Definition: schema.h:33
static constexpr uint8_t CYPHER_WRITE_PLUGIN_ID
Definition: schema.h:40
void RemovePlugin(const std::string &plugin_name)
Definition: schema.cc:1394
static constexpr const char * BUILTIN_PLUGIN_NAMES[BUILTIN_PLUGIN_NUM]
Definition: schema.h:64
static constexpr uint8_t CYPHER_READ_DEBUG_PLUGIN_ID
Definition: schema.h:41
static Result< Schema > LoadFromYaml(const std::string &schema_config)
Definition: schema.cc:1488
std::vector< std::vector< std::string > > vprop_names_
Definition: schema.h:302
static constexpr const char * VARCHAR_KEY
Definition: schema.h:47
static Result< Schema > LoadFromYamlNode(const YAML::Node &schema_node)
Definition: schema.cc:1503
void add_vertex_label(const std::string &label, const std::vector< PropertyType > &property_types, const std::vector< std::string > &property_names, const std::vector< std::tuple< PropertyType, std::string, size_t >> &primary_key, const std::vector< StorageStrategy > &strategies={}, size_t max_vnum=static_cast< size_t >(1)<< 32, const std::string &description="")
Definition: schema.cc:61
std::vector< std::vector< StorageStrategy > > vprop_storage_
Definition: schema.h:307
std::string plugin_dir_
Definition: schema.h:322
uint32_t generate_edge_label(label_t src, label_t dst, label_t edge) const
Definition: schema.cc:470
std::string remote_path_
Definition: schema.h:326
EdgeStrategy get_incoming_edge_strategy(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:317
bool exist(const std::string &src_label, const std::string &dst_label, const std::string &edge_label) const
Definition: schema.cc:201
std::string GetDescription() const
Definition: schema.cc:1400
static constexpr uint8_t HQPS_ADHOC_WRITE_PLUGIN_ID
Definition: schema.h:37
label_t vertex_label_num() const
Definition: schema.cc:110
std::vector< size_t > max_vnum_
Definition: schema.h:318
void SetPluginDir(const std::string &plugin_dir)
Definition: schema.cc:1392
EdgeStrategy get_incoming_edge_strategy(label_t src_label, label_t dst_label, label_t label) const
Definition: schema.h:211
bool outgoing_edge_mutable(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:327
PropertyType get_edge_property(label_t src, label_t dst, label_t edge) const
Definition: schema.cc:267
std::map< uint32_t, EdgeStrategy > oe_strategy_
Definition: schema.h:311
std::string compiler_path_
Definition: schema.h:325
std::string description_
Definition: schema.h:323
static bool IsBuiltinPlugin(const std::string &plugin_name)
Definition: schema.cc:23
label_t get_edge_label_id(const std::string &label) const
Definition: schema.cc:362
static constexpr const char * DEFAULT_SCHEMA_VERSION
Definition: schema.h:73
void set_compiler_path(const std::string &compiler_path)
Definition: schema.cc:35
bool valid_edge_property(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:297
static constexpr const char * CYPHER_READ_DEBUG_PLUGIN_ID_STR
Definition: schema.h:45
std::string name_
Definition: schema.h:298
std::map< uint32_t, EdgeStrategy > ie_strategy_
Definition: schema.h:312
static constexpr const char * PRIMITIVE_TYPE_KEY
Definition: schema.h:46
static constexpr const char * HQPS_ADHOC_READ_PLUGIN_ID_STR
Definition: schema.h:42
void add_edge_label(const std::string &src_label, const std::string &dst_label, const std::string &edge_label, const std::vector< PropertyType > &properties, const std::vector< std::string > &prop_names, EdgeStrategy oe=EdgeStrategy::kMultiple, EdgeStrategy ie=EdgeStrategy::kMultiple, bool oe_mutable=true, bool ie_mutable=true, bool sort_on_compaction=false, const std::string &description="")
Definition: schema.cc:83
bool EmplacePlugins(const std::vector< std::pair< std::string, std::string >> &plugins)
Definition: schema.cc:1280
std::string GetGraphName() const
Definition: schema.h:270
bool Equals(const Schema &other) const
Definition: schema.cc:481
const std::vector< std::string > & get_vertex_property_names(const std::string &label) const
Definition: schema.cc:149
bool contains_vertex_label(const std::string &label) const
Definition: schema.cc:118
static constexpr uint8_t HQPS_ADHOC_READ_PLUGIN_ID
Definition: schema.h:36
std::string GetRemotePath() const
Definition: schema.h:280
std::string get_edge_description(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:240
const std::vector< std::tuple< PropertyType, std::string, size_t > > & get_vertex_primary_key(label_t index) const
Definition: schema.cc:391
static constexpr uint8_t BUILTIN_PLUGIN_IDS[BUILTIN_PLUGIN_NUM]
Definition: schema.h:67
static constexpr uint8_t BUILTIN_PAGERANK_PLUGIN_ID
Definition: schema.h:56
static constexpr const char * BUILTIN_COUNT_VERTICES_PLUGIN_NAME
Definition: schema.h:54
const std::unordered_map< std::string, std::pair< PropertyType, uint8_t > > & get_vprop_name_to_type_and_index(label_t label) const
Definition: schema.cc:197
std::vector< std::vector< PropertyType > > vproperties_
Definition: schema.h:301
Schema()
Definition: schema.cc:32
std::string get_edge_label_name(label_t index) const
Definition: schema.cc:382
std::string GetPluginDir() const
Definition: schema.cc:1398
bool has_multi_props_edge_
Definition: schema.h:327
static constexpr uint8_t BUILTIN_K_DEGREE_NEIGHBORS_PLUGIN_ID
Definition: schema.h:58
const std::vector< std::string > & get_edge_property_names(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:273
Definition: adj_list.h:23
EdgeStrategy
Definition: types.h:24
uint8_t label_t
Definition: types.h:32