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 = 251;
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  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  // The builtin plugins are reserved for the system.
47  static constexpr uint8_t BUILTIN_PLUGIN_NUM = 1;
48  static constexpr uint8_t BUILTIN_COUNT_VERTICES_PLUGIN_ID = 252;
49  static constexpr const char* BUILTIN_COUNT_VERTICES_PLUGIN_NAME =
50  "count_vertices";
51  static constexpr const char* BUILTIN_PLUGIN_NAMES[BUILTIN_PLUGIN_NUM] = {
53  static constexpr uint8_t BUILTIN_PLUGIN_IDS[BUILTIN_PLUGIN_NUM] = {
55 
56  // An array containing all compatible versions of schema.
57  static const std::vector<std::string> COMPATIBLE_VERSIONS;
58  static constexpr const char* DEFAULT_SCHEMA_VERSION = "v0.0";
59 
60  static bool IsBuiltinPlugin(const std::string& plugin_name);
61 
63  Schema();
64  ~Schema();
65 
66  static const std::vector<std::string>& GetCompatibleVersions();
67 
68  void Clear();
69 
70  void add_vertex_label(
71  const std::string& label, const std::vector<PropertyType>& property_types,
72  const std::vector<std::string>& property_names,
73  const std::vector<std::tuple<PropertyType, std::string, size_t>>&
74  primary_key,
75  const std::vector<StorageStrategy>& strategies = {},
76  size_t max_vnum = static_cast<size_t>(1) << 32,
77  const std::string& description = "");
78 
79  void add_edge_label(const std::string& src_label,
80  const std::string& dst_label,
81  const std::string& edge_label,
82  const std::vector<PropertyType>& properties,
83  const std::vector<std::string>& prop_names,
86  bool oe_mutable = true, bool ie_mutable = true,
87  bool sort_on_compaction = false,
88  const std::string& description = "");
89 
90  label_t vertex_label_num() const;
91 
92  label_t edge_label_num() const;
93 
94  bool contains_vertex_label(const std::string& label) const;
95 
96  label_t get_vertex_label_id(const std::string& label) const;
97 
99  label_t label_id, const std::vector<PropertyType>& types,
100  const std::vector<StorageStrategy>& strategies = {});
101 
102  const std::vector<PropertyType>& get_vertex_properties(
103  const std::string& label) const;
104 
105  const std::vector<std::string>& get_vertex_property_names(
106  const std::string& label) const;
107 
108  const std::string& get_vertex_description(const std::string& label) const;
109 
110  const std::vector<PropertyType>& get_vertex_properties(label_t label) const;
111 
112  const std::vector<std::string>& get_vertex_property_names(
113  label_t label) const;
114 
115  const std::string& get_vertex_description(label_t label) const;
116 
117  const std::vector<StorageStrategy>& get_vertex_storage_strategies(
118  const std::string& label) const;
119 
120  size_t get_max_vnum(const std::string& label) const;
121 
122  bool exist(const std::string& src_label, const std::string& dst_label,
123  const std::string& edge_label) const;
124 
125  bool exist(label_type src_label, label_type dst_label,
126  label_type edge_label) const;
127 
128  const std::vector<PropertyType>& get_edge_properties(
129  const std::string& src_label, const std::string& dst_label,
130  const std::string& label) const;
131 
132  const std::vector<PropertyType>& get_edge_properties(label_t src_label,
133  label_t dst_label,
134  label_t label) const;
135 
136  std::string get_edge_description(const std::string& src_label,
137  const std::string& dst_label,
138  const std::string& label) const;
139 
140  std::string get_edge_description(label_t src_label, label_t dst_label,
141  label_t label) const;
142 
143  PropertyType get_edge_property(label_t src, label_t dst, label_t edge) const;
144 
145  const std::vector<std::string>& get_edge_property_names(
146  const std::string& src_label, const std::string& dst_label,
147  const std::string& label) const;
148 
149  const std::vector<std::string>& get_edge_property_names(
150  const label_t& src_label, const label_t& dst_label,
151  const label_t& label) const;
152 
153  bool vertex_has_property(const std::string& label,
154  const std::string& prop) const;
155 
156  bool vertex_has_primary_key(const std::string& label,
157  const std::string& prop) const;
158 
159  bool edge_has_property(const std::string& src_label,
160  const std::string& dst_label,
161  const std::string& edge_label,
162  const std::string& prop) const;
163 
164  bool has_vertex_label(const std::string& label) const;
165 
166  bool has_edge_label(const std::string& src_label,
167  const std::string& dst_label,
168  const std::string& edge_label) const;
169 
170  bool has_edge_label(label_t src_label, label_t dst_label,
171  label_t edge_label) const;
172 
173  bool valid_edge_property(const std::string& src_label,
174  const std::string& dst_label,
175  const std::string& label) const;
176 
177  EdgeStrategy get_outgoing_edge_strategy(const std::string& src_label,
178  const std::string& dst_label,
179  const std::string& label) const;
180 
181  EdgeStrategy get_incoming_edge_strategy(const std::string& src_label,
182  const std::string& dst_label,
183  const std::string& label) const;
184 
185  bool outgoing_edge_mutable(const std::string& src_label,
186  const std::string& dst_label,
187  const std::string& label) const;
188 
189  bool incoming_edge_mutable(const std::string& src_label,
190  const std::string& dst_label,
191  const std::string& label) const;
192 
193  bool get_sort_on_compaction(const std::string& src_label,
194  const std::string& dst_label,
195  const std::string& label) const;
196 
197  bool contains_edge_label(const std::string& label) const;
198 
199  label_t get_edge_label_id(const std::string& label) const;
200 
201  std::string get_vertex_label_name(label_t index) const;
202 
203  std::string get_edge_label_name(label_t index) const;
204 
205  const std::vector<std::tuple<PropertyType, std::string, size_t>>&
206  get_vertex_primary_key(label_t index) const;
207 
208  const std::string& get_vertex_primary_key_name(label_t index) const;
209 
210  void Serialize(std::unique_ptr<grape::LocalIOAdaptor>& writer) const;
211 
212  void Deserialize(std::unique_ptr<grape::LocalIOAdaptor>& reader);
213 
214  static Result<Schema> LoadFromYaml(const std::string& schema_config);
215 
216  static Result<Schema> LoadFromYamlNode(const YAML::Node& schema_node);
217 
218  bool Equals(const Schema& other) const;
219 
220  // Return the map from plugin name to plugin id
221  const std::unordered_map<std::string, std::pair<std::string, uint8_t>>&
222  GetPlugins() const;
223 
224  bool EmplacePlugins(
225  const std::vector<std::pair<std::string, std::string>>& plugins);
226 
227  void SetPluginDir(const std::string& plugin_dir);
228 
229  void RemovePlugin(const std::string& plugin_name);
230 
231  std::string GetPluginDir() const;
232 
233  std::string GetDescription() const;
234 
235  void SetDescription(const std::string& description);
236 
237  void SetVersion(const std::string& version);
238 
239  std::string GetVersion() const;
240 
241  bool has_multi_props_edge() const;
242 
243  private:
244  label_t vertex_label_to_index(const std::string& label);
245 
246  label_t edge_label_to_index(const std::string& label);
247 
248  uint32_t generate_edge_label(label_t src, label_t dst, label_t edge) const;
249 
252  std::vector<std::vector<PropertyType>> vproperties_;
253  std::vector<std::vector<std::string>> vprop_names_;
254  std::vector<std::string> v_descriptions_;
255  std::vector<std::vector<std::tuple<PropertyType, std::string, size_t>>>
256  v_primary_keys_; // the third element is the index of the property in the
257  // vertex property list
258  std::vector<std::vector<StorageStrategy>> vprop_storage_;
259  std::map<uint32_t, std::vector<PropertyType>> eproperties_;
260  std::map<uint32_t, std::vector<std::string>> eprop_names_;
261  std::map<uint32_t, std::string> e_descriptions_;
262  std::map<uint32_t, EdgeStrategy> oe_strategy_;
263  std::map<uint32_t, EdgeStrategy> ie_strategy_;
264  std::map<uint32_t, bool> oe_mutability_;
265  std::map<uint32_t, bool> ie_mutability_;
266  std::map<uint32_t, bool> sort_on_compactions_;
267  std::vector<size_t> max_vnum_;
268  std::unordered_map<std::string, std::pair<std::string, uint8_t>>
269  plugin_name_to_path_and_id_; // key is plugin_name, value is plugin_path
270  // and plugin_id
271  std::string plugin_dir_;
272  std::string description_;
273  std::string version_;
275 };
276 
277 } // namespace gs
278 
279 #endif // GRAPHSCOPE_FRAGMENT_SCHEMA_H_
gs::Schema::oe_mutability_
std::map< uint32_t, bool > oe_mutability_
Definition: schema.h:264
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:55
gs::Schema::BUILTIN_PLUGIN_NUM
static constexpr uint8_t BUILTIN_PLUGIN_NUM
Definition: schema.h:47
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:332
gs::Schema::version_
std::string version_
Definition: schema.h:273
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:1398
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:347
gs::Schema::ie_mutability_
std::map< uint32_t, bool > ie_mutability_
Definition: schema.h:265
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:312
gs::Schema::e_descriptions_
std::map< uint32_t, std::string > e_descriptions_
Definition: schema.h:261
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:1421
gs::Schema::ADHOC_READ_PLUGIN_ID
static constexpr uint8_t ADHOC_READ_PLUGIN_ID
Definition: schema.h:35
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:292
gs::Schema::vertex_label_to_index
label_t vertex_label_to_index(const std::string &label)
Definition: schema.cc:414
gs::Schema::VARCHAR_KEY
static constexpr const char * VARCHAR_KEY
Definition: schema.h:42
gs::Schema::BUILTIN_COUNT_VERTICES_PLUGIN_ID
static constexpr uint8_t BUILTIN_COUNT_VERTICES_PLUGIN_ID
Definition: schema.h:48
gs::Schema::get_edge_property
PropertyType get_edge_property(label_t src, label_t dst, label_t edge) const
Definition: schema.cc:252
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:302
gs::Schema::Deserialize
void Deserialize(std::unique_ptr< grape::LocalIOAdaptor > &reader)
Definition: schema.cc:396
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:272
gs::Schema::vlabel_indexer_
IdIndexer< std::string, label_t > vlabel_indexer_
Definition: schema.h:250
gs
Definition: adj_list.h:23
gs::Schema::EmplacePlugins
bool EmplacePlugins(const std::vector< std::pair< std::string, std::string >> &plugins)
Definition: schema.cc:1230
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:225
gs::Schema::LoadFromYamlNode
static Result< Schema > LoadFromYamlNode(const YAML::Node &schema_node)
Definition: schema.cc:1436
gs::Schema::COMPATIBLE_VERSIONS
static const std::vector< std::string > COMPATIBLE_VERSIONS
Definition: schema.h:57
gs::Schema::Serialize
void Serialize(std::unique_ptr< grape::LocalIOAdaptor > &writer) const
Definition: schema.cc:384
gs::Schema::get_vertex_storage_strategies
const std::vector< StorageStrategy > & get_vertex_storage_strategies(const std::string &label) const
Definition: schema.cc:170
gs::Schema::oe_strategy_
std::map< uint32_t, EdgeStrategy > oe_strategy_
Definition: schema.h:262
gs::Schema::vertex_has_primary_key
bool vertex_has_primary_key(const std::string &label, const std::string &prop) const
Definition: schema.cc:1360
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:258
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:201
gs::Schema::Schema
Schema()
Definition: schema.cc:32
gs::Schema::GetDescription
std::string GetDescription() const
Definition: schema.cc:1337
gs::Schema::max_vnum_
std::vector< size_t > max_vnum_
Definition: schema.h:267
gs::Schema::RemovePlugin
void RemovePlugin(const std::string &plugin_name)
Definition: schema.cc:1331
gs::Schema::BUILTIN_COUNT_VERTICES_PLUGIN_NAME
static constexpr const char * BUILTIN_COUNT_VERTICES_PLUGIN_NAME
Definition: schema.h:49
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:282
gs::Schema::SetPluginDir
void SetPluginDir(const std::string &plugin_dir)
Definition: schema.cc:1329
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:269
yaml_utils.h
gs::Schema::vertex_label_num
label_t vertex_label_num() const
Definition: schema.cc:100
gs::Schema::BUILTIN_PLUGIN_IDS
static constexpr uint8_t BUILTIN_PLUGIN_IDS[BUILTIN_PLUGIN_NUM]
Definition: schema.h:53
gs::Schema::GetPluginDir
std::string GetPluginDir() const
Definition: schema.cc:1335
gs::Schema::edge_label_num
label_t edge_label_num() const
Definition: schema.cc:104
gs::IdIndexer< std::string, label_t >
gs::Schema::SetVersion
void SetVersion(const std::string &version)
Definition: schema.cc:1343
result.h
gs::Schema::eprop_names_
std::map< uint32_t, std::vector< std::string > > eprop_names_
Definition: schema.h:260
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:120
gs::Schema::get_vertex_label_name
std::string get_vertex_label_name(label_t index) const
Definition: schema.cc:359
gs::Schema::SetDescription
void SetDescription(const std::string &description)
Definition: schema.cc:1339
gs::Schema::GetVersion
std::string GetVersion() const
Definition: schema.cc:1344
gs::Schema::ie_strategy_
std::map< uint32_t, EdgeStrategy > ie_strategy_
Definition: schema.h:263
gs::Schema::has_vertex_label
bool has_vertex_label(const std::string &label) const
Definition: schema.cc:1393
gs::Schema::has_multi_props_edge_
bool has_multi_props_edge_
Definition: schema.h:274
gs::Schema::eproperties_
std::map< uint32_t, std::vector< PropertyType > > eproperties_
Definition: schema.h:259
gs::Schema::Clear
void Clear()
Definition: schema.cc:35
gs::Schema::get_vertex_description
const std::string & get_vertex_description(const std::string &label) const
Definition: schema.cc:155
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:62
gs::Schema::Equals
bool Equals(const Schema &other) const
Definition: schema.cc:445
id_indexer.h
gs::Schema::HQPS_ADHOC_WRITE_PLUGIN_ID
static constexpr uint8_t HQPS_ADHOC_WRITE_PLUGIN_ID
Definition: schema.h:37
types.h
gs::Schema::plugin_dir_
std::string plugin_dir_
Definition: schema.h:271
gs::Schema::get_vertex_property_names
const std::vector< std::string > & get_vertex_property_names(const std::string &label) const
Definition: schema.cc:139
gs::Schema::sort_on_compactions_
std::map< uint32_t, bool > sort_on_compactions_
Definition: schema.h:266
gs::Schema::GetPlugins
const std::unordered_map< std::string, std::pair< std::string, uint8_t > > & GetPlugins() const
Definition: schema.cc:1224
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:128
gs::Schema::BUILTIN_PLUGIN_NAMES
static constexpr const char * BUILTIN_PLUGIN_NAMES[BUILTIN_PLUGIN_NUM]
Definition: schema.h:51
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:1349
gs::Schema::get_edge_label_name
std::string get_edge_label_name(label_t index) const
Definition: schema.cc:367
gs::Schema::contains_vertex_label
bool contains_vertex_label(const std::string &label) const
Definition: schema.cc:108
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:73
gs::Schema::generate_edge_label
uint32_t generate_edge_label(label_t src, label_t dst, label_t edge) const
Definition: schema.cc:434
gs::Schema::get_max_vnum
size_t get_max_vnum(const std::string &label) const
Definition: schema.cc:181
gs::Schema::vproperties_
std::vector< std::vector< PropertyType > > vproperties_
Definition: schema.h:252
gs::Schema::exist
bool exist(const std::string &src_label, const std::string &dst_label, const std::string &edge_label) const
Definition: schema.cc:186
gs::Schema::contains_edge_label
bool contains_edge_label(const std::string &label) const
Definition: schema.cc:354
gs::Schema::DEFAULT_SCHEMA_VERSION
static constexpr const char * DEFAULT_SCHEMA_VERSION
Definition: schema.h:58
gs::Schema::GetCompatibleVersions
static const std::vector< std::string > & GetCompatibleVersions()
Definition: schema.cc:1447
gs::label_t
uint8_t label_t
Definition: types.h:32
gs::Schema::IsBuiltinPlugin
static bool IsBuiltinPlugin(const std::string &plugin_name)
Definition: schema.cc:23
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:322
gs::Schema::vprop_names_
std::vector< std::vector< std::string > > vprop_names_
Definition: schema.h:253
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:251
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:428
gs::Schema::vprop_storage_
std::vector< std::vector< StorageStrategy > > vprop_storage_
Definition: schema.h:258
gs::Schema::get_vertex_label_id
label_t get_vertex_label_id(const std::string &label) const
Definition: schema.cc:113
gs::Schema::has_multi_props_edge
bool has_multi_props_edge() const
Definition: schema.cc:1346
gs::Schema::v_descriptions_
std::vector< std::string > v_descriptions_
Definition: schema.h:254
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:376
gs::Schema::v_primary_keys_
std::vector< std::vector< std::tuple< PropertyType, std::string, size_t > > > v_primary_keys_
Definition: schema.h:256
gs::Schema::HQPS_ADHOC_READ_PLUGIN_ID
static constexpr uint8_t HQPS_ADHOC_READ_PLUGIN_ID
Definition: schema.h:36
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:1374