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 = 252;
35  static constexpr uint8_t HQPS_ADHOC_READ_PLUGIN_ID = 254;
36  static constexpr uint8_t HQPS_ADHOC_WRITE_PLUGIN_ID = 255;
37  static constexpr uint8_t ADHOC_READ_PLUGIN_ID = 253;
38  static constexpr const char* HQPS_ADHOC_READ_PLUGIN_ID_STR = "\xFE";
39  static constexpr const char* HQPS_ADHOC_WRITE_PLUGIN_ID_STR = "\xFF";
40  static constexpr const char* ADHOC_READ_PLUGIN_ID_STR = "\xFD";
41  static constexpr const char* PRIMITIVE_TYPE_KEY = "primitive_type";
42  static constexpr const char* VARCHAR_KEY = "varchar";
43  static constexpr const char* MAX_LENGTH_KEY = "max_length";
44  static constexpr const uint16_t STRING_DEFAULT_MAX_LENGTH = 256;
45 
46  // An array containing all compatible versions of schema.
47  static const std::vector<std::string> COMPATIBLE_VERSIONS;
48  static constexpr const char* DEFAULT_SCHEMA_VERSION = "v0.0";
49 
51  Schema();
52  ~Schema();
53 
54  static const std::vector<std::string>& GetCompatibleVersions();
55 
56  void Clear();
57 
58  void add_vertex_label(
59  const std::string& label, const std::vector<PropertyType>& property_types,
60  const std::vector<std::string>& property_names,
61  const std::vector<std::tuple<PropertyType, std::string, size_t>>&
62  primary_key,
63  const std::vector<StorageStrategy>& strategies = {},
64  size_t max_vnum = static_cast<size_t>(1) << 32,
65  const std::string& description = "");
66 
67  void add_edge_label(const std::string& src_label,
68  const std::string& dst_label,
69  const std::string& edge_label,
70  const std::vector<PropertyType>& properties,
71  const std::vector<std::string>& prop_names,
74  bool oe_mutable = true, bool ie_mutable = true,
75  bool sort_on_compaction = false,
76  const std::string& description = "");
77 
78  label_t vertex_label_num() const;
79 
80  label_t edge_label_num() const;
81 
82  bool contains_vertex_label(const std::string& label) const;
83 
84  label_t get_vertex_label_id(const std::string& label) const;
85 
87  label_t label_id, const std::vector<PropertyType>& types,
88  const std::vector<StorageStrategy>& strategies = {});
89 
90  const std::vector<PropertyType>& get_vertex_properties(
91  const std::string& label) const;
92 
93  const std::vector<std::string>& get_vertex_property_names(
94  const std::string& label) const;
95 
96  const std::string& get_vertex_description(const std::string& label) const;
97 
98  const std::vector<PropertyType>& get_vertex_properties(label_t label) const;
99 
100  const std::vector<std::string>& get_vertex_property_names(
101  label_t label) const;
102 
103  const std::string& get_vertex_description(label_t label) const;
104 
105  const std::vector<StorageStrategy>& get_vertex_storage_strategies(
106  const std::string& label) const;
107 
108  size_t get_max_vnum(const std::string& label) const;
109 
110  bool exist(const std::string& src_label, const std::string& dst_label,
111  const std::string& edge_label) const;
112 
113  bool exist(label_type src_label, label_type dst_label,
114  label_type edge_label) const;
115 
116  const std::vector<PropertyType>& get_edge_properties(
117  const std::string& src_label, const std::string& dst_label,
118  const std::string& label) const;
119 
120  const std::vector<PropertyType>& get_edge_properties(label_t src_label,
121  label_t dst_label,
122  label_t label) const;
123 
124  std::string get_edge_description(const std::string& src_label,
125  const std::string& dst_label,
126  const std::string& label) const;
127 
128  std::string get_edge_description(label_t src_label, label_t dst_label,
129  label_t label) const;
130 
131  PropertyType get_edge_property(label_t src, label_t dst, label_t edge) const;
132 
133  const std::vector<std::string>& get_edge_property_names(
134  const std::string& src_label, const std::string& dst_label,
135  const std::string& label) const;
136 
137  const std::vector<std::string>& get_edge_property_names(
138  const label_t& src_label, const label_t& dst_label,
139  const label_t& label) const;
140 
141  bool vertex_has_property(const std::string& label,
142  const std::string& prop) const;
143 
144  bool vertex_has_primary_key(const std::string& label,
145  const std::string& prop) const;
146 
147  bool edge_has_property(const std::string& src_label,
148  const std::string& dst_label,
149  const std::string& edge_label,
150  const std::string& prop) const;
151 
152  bool has_vertex_label(const std::string& label) const;
153 
154  bool has_edge_label(const std::string& src_label,
155  const std::string& dst_label,
156  const std::string& edge_label) const;
157 
158  bool has_edge_label(label_t src_label, label_t dst_label,
159  label_t edge_label) const;
160 
161  bool valid_edge_property(const std::string& src_label,
162  const std::string& dst_label,
163  const std::string& label) const;
164 
165  EdgeStrategy get_outgoing_edge_strategy(const std::string& src_label,
166  const std::string& dst_label,
167  const std::string& label) const;
168 
169  EdgeStrategy get_incoming_edge_strategy(const std::string& src_label,
170  const std::string& dst_label,
171  const std::string& label) const;
172 
173  bool outgoing_edge_mutable(const std::string& src_label,
174  const std::string& dst_label,
175  const std::string& label) const;
176 
177  bool incoming_edge_mutable(const std::string& src_label,
178  const std::string& dst_label,
179  const std::string& label) const;
180 
181  bool get_sort_on_compaction(const std::string& src_label,
182  const std::string& dst_label,
183  const std::string& label) const;
184 
185  bool contains_edge_label(const std::string& label) const;
186 
187  label_t get_edge_label_id(const std::string& label) const;
188 
189  std::string get_vertex_label_name(label_t index) const;
190 
191  std::string get_edge_label_name(label_t index) const;
192 
193  const std::vector<std::tuple<PropertyType, std::string, size_t>>&
194  get_vertex_primary_key(label_t index) const;
195 
196  const std::string& get_vertex_primary_key_name(label_t index) const;
197 
198  void Serialize(std::unique_ptr<grape::LocalIOAdaptor>& writer) const;
199 
200  void Deserialize(std::unique_ptr<grape::LocalIOAdaptor>& reader);
201 
202  static Result<Schema> LoadFromYaml(const std::string& schema_config);
203 
204  static Result<Schema> LoadFromYamlNode(const YAML::Node& schema_node);
205 
206  bool Equals(const Schema& other) const;
207 
208  // Return the map from plugin name to plugin id
209  const std::unordered_map<std::string, std::pair<std::string, uint8_t>>&
210  GetPlugins() const;
211 
212  bool EmplacePlugins(
213  const std::vector<std::pair<std::string, std::string>>& plugins);
214 
215  void SetPluginDir(const std::string& plugin_dir);
216 
217  void RemovePlugin(const std::string& plugin_name);
218 
219  std::string GetPluginDir() const;
220 
221  std::string GetDescription() const;
222 
223  void SetDescription(const std::string& description);
224 
225  void SetVersion(const std::string& version);
226 
227  std::string GetVersion() const;
228 
229  bool has_multi_props_edge() const;
230 
231  private:
232  label_t vertex_label_to_index(const std::string& label);
233 
234  label_t edge_label_to_index(const std::string& label);
235 
236  uint32_t generate_edge_label(label_t src, label_t dst, label_t edge) const;
237 
240  std::vector<std::vector<PropertyType>> vproperties_;
241  std::vector<std::vector<std::string>> vprop_names_;
242  std::vector<std::string> v_descriptions_;
243  std::vector<std::vector<std::tuple<PropertyType, std::string, size_t>>>
244  v_primary_keys_; // the third element is the index of the property in the
245  // vertex property list
246  std::vector<std::vector<StorageStrategy>> vprop_storage_;
247  std::map<uint32_t, std::vector<PropertyType>> eproperties_;
248  std::map<uint32_t, std::vector<std::string>> eprop_names_;
249  std::map<uint32_t, std::string> e_descriptions_;
250  std::map<uint32_t, EdgeStrategy> oe_strategy_;
251  std::map<uint32_t, EdgeStrategy> ie_strategy_;
252  std::map<uint32_t, bool> oe_mutability_;
253  std::map<uint32_t, bool> ie_mutability_;
254  std::map<uint32_t, bool> sort_on_compactions_;
255  std::vector<size_t> max_vnum_;
256  std::unordered_map<std::string, std::pair<std::string, uint8_t>>
257  plugin_name_to_path_and_id_; // key is plugin_name, value is plugin_path
258  // and plugin_id
259  std::string plugin_dir_;
260  std::string description_;
261  std::string version_;
263 };
264 
265 } // namespace gs
266 
267 #endif // GRAPHSCOPE_FRAGMENT_SCHEMA_H_
gs::Schema::oe_mutability_
std::map< uint32_t, bool > oe_mutability_
Definition: schema.h:252
gs::Schema::STRING_DEFAULT_MAX_LENGTH
static constexpr const uint16_t STRING_DEFAULT_MAX_LENGTH
Definition: schema.h:44
gs::Schema::add_vertex_label
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:46
gs::Schema::get_sort_on_compaction
bool get_sort_on_compaction(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:323
gs::Schema::version_
std::string version_
Definition: schema.h:261
gs::Schema::has_edge_label
bool has_edge_label(const std::string &src_label, const std::string &dst_label, const std::string &edge_label) const
Definition: schema.cc:1378
gs::Schema::MAX_LENGTH_KEY
static constexpr const char * MAX_LENGTH_KEY
Definition: schema.h:43
gs::Schema::get_edge_label_id
label_t get_edge_label_id(const std::string &label) const
Definition: schema.cc:338
gs::Schema::ie_mutability_
std::map< uint32_t, bool > ie_mutability_
Definition: schema.h:253
gs::Schema::outgoing_edge_mutable
bool outgoing_edge_mutable(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:303
gs::Schema::e_descriptions_
std::map< uint32_t, std::string > e_descriptions_
Definition: schema.h:249
gs::Schema::HQPS_ADHOC_READ_PLUGIN_ID_STR
static constexpr const char * HQPS_ADHOC_READ_PLUGIN_ID_STR
Definition: schema.h:38
gs::EdgeStrategy::kMultiple
@ kMultiple
types.h
gs::Schema::LoadFromYaml
static Result< Schema > LoadFromYaml(const std::string &schema_config)
Definition: schema.cc:1401
gs::Schema::ADHOC_READ_PLUGIN_ID
static constexpr uint8_t ADHOC_READ_PLUGIN_ID
Definition: schema.h:37
gs::Schema::get_outgoing_edge_strategy
EdgeStrategy get_outgoing_edge_strategy(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:283
gs::Schema::vertex_label_to_index
label_t vertex_label_to_index(const std::string &label)
Definition: schema.cc:405
gs::Schema::VARCHAR_KEY
static constexpr const char * VARCHAR_KEY
Definition: schema.h:42
gs::Schema::get_edge_property
PropertyType get_edge_property(label_t src, label_t dst, label_t edge) const
Definition: schema.cc:243
gs::Schema::get_incoming_edge_strategy
EdgeStrategy get_incoming_edge_strategy(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:293
gs::Schema::Deserialize
void Deserialize(std::unique_ptr< grape::LocalIOAdaptor > &reader)
Definition: schema.cc:387
gs::Schema::PRIMITIVE_TYPE_KEY
static constexpr const char * PRIMITIVE_TYPE_KEY
Definition: schema.h:41
gs::Schema::description_
std::string description_
Definition: schema.h:260
gs::Schema::vlabel_indexer_
IdIndexer< std::string, label_t > vlabel_indexer_
Definition: schema.h:238
gs
Definition: adj_list.h:23
gs::Schema::EmplacePlugins
bool EmplacePlugins(const std::vector< std::pair< std::string, std::string >> &plugins)
Definition: schema.cc:1221
gs::Schema::get_edge_description
std::string get_edge_description(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:216
gs::Schema::LoadFromYamlNode
static Result< Schema > LoadFromYamlNode(const YAML::Node &schema_node)
Definition: schema.cc:1416
gs::Schema::COMPATIBLE_VERSIONS
static const std::vector< std::string > COMPATIBLE_VERSIONS
Definition: schema.h:47
gs::Schema::Serialize
void Serialize(std::unique_ptr< grape::LocalIOAdaptor > &writer) const
Definition: schema.cc:375
gs::Schema::get_vertex_storage_strategies
const std::vector< StorageStrategy > & get_vertex_storage_strategies(const std::string &label) const
Definition: schema.cc:161
gs::Schema::oe_strategy_
std::map< uint32_t, EdgeStrategy > oe_strategy_
Definition: schema.h:250
gs::Schema::vertex_has_primary_key
bool vertex_has_primary_key(const std::string &label, const std::string &prop) const
Definition: schema.cc:1340
gs::Schema::RESERVED_PLUGIN_NUM
static constexpr uint8_t RESERVED_PLUGIN_NUM
Definition: schema.h:33
gs::Schema::get_edge_property_names
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:249
gs::Schema::get_edge_properties
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:192
gs::Schema::Schema
Schema()
Definition: schema.cc:23
gs::Schema::GetDescription
std::string GetDescription() const
Definition: schema.cc:1317
gs::Schema::max_vnum_
std::vector< size_t > max_vnum_
Definition: schema.h:255
gs::Schema::RemovePlugin
void RemovePlugin(const std::string &plugin_name)
Definition: schema.cc:1311
gs::Schema
Definition: schema.h:29
gs::Schema::valid_edge_property
bool valid_edge_property(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:273
gs::Schema::SetPluginDir
void SetPluginDir(const std::string &plugin_dir)
Definition: schema.cc:1309
gs::Schema::get_vertex_primary_key_name
const std::string & get_vertex_primary_key_name(label_t index) const
gs::Schema::plugin_name_to_path_and_id_
std::unordered_map< std::string, std::pair< std::string, uint8_t > > plugin_name_to_path_and_id_
Definition: schema.h:257
yaml_utils.h
gs::Schema::vertex_label_num
label_t vertex_label_num() const
Definition: schema.cc:91
gs::Schema::GetPluginDir
std::string GetPluginDir() const
Definition: schema.cc:1315
gs::Schema::edge_label_num
label_t edge_label_num() const
Definition: schema.cc:95
gs::IdIndexer< std::string, label_t >
gs::Schema::SetVersion
void SetVersion(const std::string &version)
Definition: schema.cc:1323
result.h
gs::Schema::eprop_names_
std::map< uint32_t, std::vector< std::string > > eprop_names_
Definition: schema.h:248
gs::Schema::set_vertex_properties
void set_vertex_properties(label_t label_id, const std::vector< PropertyType > &types, const std::vector< StorageStrategy > &strategies={})
Definition: schema.cc:111
gs::Schema::get_vertex_label_name
std::string get_vertex_label_name(label_t index) const
Definition: schema.cc:350
gs::Schema::SetDescription
void SetDescription(const std::string &description)
Definition: schema.cc:1319
gs::Schema::GetVersion
std::string GetVersion() const
Definition: schema.cc:1324
gs::Schema::ie_strategy_
std::map< uint32_t, EdgeStrategy > ie_strategy_
Definition: schema.h:251
gs::Schema::has_vertex_label
bool has_vertex_label(const std::string &label) const
Definition: schema.cc:1373
gs::Schema::has_multi_props_edge_
bool has_multi_props_edge_
Definition: schema.h:262
gs::Schema::eproperties_
std::map< uint32_t, std::vector< PropertyType > > eproperties_
Definition: schema.h:247
gs::Schema::Clear
void Clear()
Definition: schema.cc:26
gs::Schema::get_vertex_description
const std::string & get_vertex_description(const std::string &label) const
Definition: schema.cc:146
gs::Schema::MAX_PLUGIN_ID
static constexpr uint8_t MAX_PLUGIN_ID
Definition: schema.h:34
gs::Schema::label_type
label_t label_type
Definition: schema.h:50
gs::Schema::Equals
bool Equals(const Schema &other) const
Definition: schema.cc:436
id_indexer.h
gs::Schema::HQPS_ADHOC_WRITE_PLUGIN_ID
static constexpr uint8_t HQPS_ADHOC_WRITE_PLUGIN_ID
Definition: schema.h:36
types.h
gs::Schema::plugin_dir_
std::string plugin_dir_
Definition: schema.h:259
gs::Schema::get_vertex_property_names
const std::vector< std::string > & get_vertex_property_names(const std::string &label) const
Definition: schema.cc:130
gs::Schema::sort_on_compactions_
std::map< uint32_t, bool > sort_on_compactions_
Definition: schema.h:254
gs::Schema::GetPlugins
const std::unordered_map< std::string, std::pair< std::string, uint8_t > > & GetPlugins() const
Definition: schema.cc:1215
gs::EdgeStrategy
EdgeStrategy
Definition: types.h:24
gs::Schema::get_vertex_properties
const std::vector< PropertyType > & get_vertex_properties(const std::string &label) const
Definition: schema.cc:119
gs::Schema::ADHOC_READ_PLUGIN_ID_STR
static constexpr const char * ADHOC_READ_PLUGIN_ID_STR
Definition: schema.h:40
gs::Schema::vertex_has_property
bool vertex_has_property(const std::string &label, const std::string &prop) const
Definition: schema.cc:1329
gs::Schema::get_edge_label_name
std::string get_edge_label_name(label_t index) const
Definition: schema.cc:358
gs::Schema::contains_vertex_label
bool contains_vertex_label(const std::string &label) const
Definition: schema.cc:99
gs::Schema::add_edge_label
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:64
gs::Schema::generate_edge_label
uint32_t generate_edge_label(label_t src, label_t dst, label_t edge) const
Definition: schema.cc:425
gs::Schema::get_max_vnum
size_t get_max_vnum(const std::string &label) const
Definition: schema.cc:172
gs::Schema::vproperties_
std::vector< std::vector< PropertyType > > vproperties_
Definition: schema.h:240
gs::Schema::exist
bool exist(const std::string &src_label, const std::string &dst_label, const std::string &edge_label) const
Definition: schema.cc:177
gs::Schema::contains_edge_label
bool contains_edge_label(const std::string &label) const
Definition: schema.cc:345
gs::Schema::DEFAULT_SCHEMA_VERSION
static constexpr const char * DEFAULT_SCHEMA_VERSION
Definition: schema.h:48
gs::Schema::GetCompatibleVersions
static const std::vector< std::string > & GetCompatibleVersions()
Definition: schema.cc:1427
gs::label_t
uint8_t label_t
Definition: types.h:32
gs::Schema::incoming_edge_mutable
bool incoming_edge_mutable(const std::string &src_label, const std::string &dst_label, const std::string &label) const
Definition: schema.cc:313
gs::Schema::vprop_names_
std::vector< std::vector< std::string > > vprop_names_
Definition: schema.h:241
gs::Schema::HQPS_ADHOC_WRITE_PLUGIN_ID_STR
static constexpr const char * HQPS_ADHOC_WRITE_PLUGIN_ID_STR
Definition: schema.h:39
gs::Schema::elabel_indexer_
IdIndexer< std::string, label_t > elabel_indexer_
Definition: schema.h:239
table.h
gs::Schema::~Schema
~Schema()
gs::Schema::edge_label_to_index
label_t edge_label_to_index(const std::string &label)
Definition: schema.cc:419
gs::Schema::vprop_storage_
std::vector< std::vector< StorageStrategy > > vprop_storage_
Definition: schema.h:246
gs::Schema::get_vertex_label_id
label_t get_vertex_label_id(const std::string &label) const
Definition: schema.cc:104
gs::Schema::has_multi_props_edge
bool has_multi_props_edge() const
Definition: schema.cc:1326
gs::Schema::v_descriptions_
std::vector< std::string > v_descriptions_
Definition: schema.h:242
gs::Schema::get_vertex_primary_key
const std::vector< std::tuple< PropertyType, std::string, size_t > > & get_vertex_primary_key(label_t index) const
Definition: schema.cc:367
gs::Schema::v_primary_keys_
std::vector< std::vector< std::tuple< PropertyType, std::string, size_t > > > v_primary_keys_
Definition: schema.h:244
gs::Schema::HQPS_ADHOC_READ_PLUGIN_ID
static constexpr uint8_t HQPS_ADHOC_READ_PLUGIN_ID
Definition: schema.h:35
gs::Schema::edge_has_property
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:1354