Flex  0.17.9
graph_db.h
Go to the documentation of this file.
1 
16 #ifndef GRAPHSCOPE_DATABASE_GRAPH_DB_H_
17 #define GRAPHSCOPE_DATABASE_GRAPH_DB_H_
18 
19 #include <dlfcn.h>
20 
21 #include <map>
22 #include <mutex>
23 #include <thread>
24 #include <vector>
25 
36 
37 namespace gs {
38 
39 class GraphDB;
40 class GraphDBSession;
41 struct SessionLocalContext;
42 
43 struct GraphDBConfig {
44  GraphDBConfig() = default;
45  GraphDBConfig(const Schema& schema_, const std::string& data_dir_,
46  const std::string& compiler_path_ = "", int thread_num_ = 1)
47  : schema(schema_),
48  data_dir(data_dir_),
49  compiler_path(compiler_path_),
50  thread_num(thread_num_),
51  warmup(false),
52  enable_monitoring(false),
54  memory_level(1),
55  wal_uri("") {}
56 
58  std::string data_dir;
59  std::string compiler_path;
61  bool warmup;
64 
65  /*
66  0 - sync with disk;
67  1 - mmap virtual memory;
68  2 - preferring hugepages;
69  3 - force hugepages;
70  */
72  std::string wal_uri; // Indicate the where shall we store the wal files.
73  // could be file://{GRAPH_DATA_DIR}/wal or other scheme
74  // that interactive supports
75 };
76 
77 class GraphDB {
78  public:
80  ~GraphDB();
81 
82  static GraphDB& get();
83 
92  Result<bool> Open(const Schema& schema, const std::string& data_dir,
93  int32_t thread_num = 1, bool warmup = false,
94  bool memory_only = true,
95  bool enable_auto_compaction = false);
96 
98 
102  void Close();
103 
108  ReadTransaction GetReadTransaction(int thread_id = 0);
109 
115  InsertTransaction GetInsertTransaction(int thread_id = 0);
116 
123  int thread_id = 0);
124 
131 
137  UpdateTransaction GetUpdateTransaction(int thread_id = 0);
138 
139  inline const MutablePropertyFragment& graph() const { return graph_; }
140  inline MutablePropertyFragment& graph() { return graph_; }
141 
142  inline const Schema& schema() const { return graph_.schema(); }
143 
144  inline std::shared_ptr<ColumnBase> get_vertex_property_column(
145  uint8_t label, const std::string& col_name) const {
146  return graph_.get_vertex_table(label).get_column(col_name);
147  }
148 
149  inline std::shared_ptr<RefColumnBase> get_vertex_id_column(
150  uint8_t label) const {
151  return graph_.get_vertex_id_column(label);
152  }
153 
154  AppWrapper CreateApp(uint8_t app_type, int thread_id);
155 
156  void GetAppInfo(Encoder& result);
157 
158  GraphDBSession& GetSession(int thread_id);
159  const GraphDBSession& GetSession(int thread_id) const;
160 
161  int SessionNum() const;
162 
165 
166  std::string work_dir() const { return work_dir_; }
167 
168  void OutputCypherProfiles(const std::string& prefix);
169 
170  inline const GraphDBConfig& config() const { return config_; }
171 
172  private:
173  bool registerApp(const std::string& path, uint8_t index = 0);
174 
175  void ingestWals(IWalParser& parser, const std::string& work_dir,
176  int thread_num);
177 
178  void initApps(
179  const std::unordered_map<std::string, std::pair<std::string, uint8_t>>&
180  plugins);
181 
183  const std::string& data_dir,
184  MemoryStrategy allocator_strategy);
185 
186  void showAppMetrics() const;
187 
188  size_t getExecutedQueryNum() const;
189 
190  friend class GraphDBSession;
191 
193  std::string work_dir_;
195 
197 
200 
201  std::array<std::string, 256> app_paths_;
202  std::array<std::shared_ptr<AppFactoryBase>, 256> app_factories_;
203 
204  std::thread monitor_thread_;
206 
209  std::thread compact_thread_;
210 };
211 
212 } // namespace gs
213 
214 #endif // GRAPHSCOPE_DATABASE_GRAPH_DB_H_
Definition: app_base.h:78
Definition: app_utils.h:25
Definition: graph_db_session.h:36
Definition: graph_db.h:77
std::thread compact_thread_
Definition: graph_db.h:209
std::array< std::string, 256 > app_paths_
Definition: graph_db.h:201
const Schema & schema() const
Definition: graph_db.h:142
Result< bool > Open(const Schema &schema, const std::string &data_dir, int32_t thread_num=1, bool warmup=false, bool memory_only=true, bool enable_auto_compaction=false)
Load the graph from data directory.
Definition: graph_db.cc:82
UpdateTransaction GetUpdateTransaction(int thread_id=0)
Create a transaction to update vertices and edges.
Definition: graph_db.cc:299
int SessionNum() const
Definition: graph_db.cc:311
void initApps(const std::unordered_map< std::string, std::pair< std::string, uint8_t >> &plugins)
Definition: graph_db.cc:409
std::string work_dir() const
Definition: graph_db.h:166
void openWalAndCreateContexts(const GraphDBConfig &config, const std::string &data_dir, MemoryStrategy allocator_strategy)
Definition: graph_db.cc:464
AppWrapper CreateApp(uint8_t app_type, int thread_id)
Definition: graph_db.cc:320
GraphDBSession & GetSession(int thread_id)
Definition: graph_db.cc:303
GraphDBConfig config_
Definition: graph_db.h:192
int thread_num_
Definition: graph_db.h:196
timestamp_t GetLastCompactionTimestamp() const
Definition: graph_db.cc:316
MutablePropertyFragment & graph()
Definition: graph_db.h:140
ReadTransaction GetReadTransaction(int thread_id=0)
Create a transaction to read vertices and edges.
Definition: graph_db.cc:281
~GraphDB()
Definition: graph_db.cc:60
void Close()
Close the current opened graph.
Definition: graph_db.cc:258
VersionManager version_manager_
Definition: graph_db.h:199
std::shared_ptr< RefColumnBase > get_vertex_id_column(uint8_t label) const
Definition: graph_db.h:149
size_t getExecutedQueryNum() const
Definition: graph_db.cc:520
SingleVertexInsertTransaction GetSingleVertexInsertTransaction(int thread_id=0)
Create a transaction to insert a single vertex.
Definition: graph_db.cc:289
std::shared_ptr< ColumnBase > get_vertex_property_column(uint8_t label, const std::string &col_name) const
Definition: graph_db.h:144
void UpdateCompactionTimestamp(timestamp_t ts)
Definition: graph_db.cc:313
void ingestWals(IWalParser &parser, const std::string &work_dir, int thread_num)
Definition: graph_db.cc:385
void GetAppInfo(Encoder &result)
Definition: graph_db.cc:346
SessionLocalContext * contexts_
Definition: graph_db.h:194
SingleEdgeInsertTransaction GetSingleEdgeInsertTransaction(int thread_id=0)
Create a transaction to insert a single edge.
Definition: graph_db.cc:294
bool registerApp(const std::string &path, uint8_t index=0)
Definition: graph_db.cc:330
timestamp_t last_compaction_ts_
Definition: graph_db.h:207
void showAppMetrics() const
Definition: graph_db.cc:501
static GraphDB & get()
Definition: graph_db.cc:77
const MutablePropertyFragment & graph() const
Definition: graph_db.h:139
bool monitor_thread_running_
Definition: graph_db.h:205
InsertTransaction GetInsertTransaction(int thread_id=0)
Create a transaction to insert vertices and edges with a default allocator.
Definition: graph_db.cc:285
std::thread monitor_thread_
Definition: graph_db.h:204
std::string work_dir_
Definition: graph_db.h:193
void OutputCypherProfiles(const std::string &prefix)
Definition: graph_db.cc:528
MutablePropertyFragment graph_
Definition: graph_db.h:198
bool compact_thread_running_
Definition: graph_db.h:208
std::array< std::shared_ptr< AppFactoryBase >, 256 > app_factories_
Definition: graph_db.h:202
const GraphDBConfig & config() const
Definition: graph_db.h:170
Definition: wal.h:83
Definition: insert_transaction.h:34
Definition: mutable_property_fragment.h:37
Table & get_vertex_table(label_t vertex_label)
Definition: mutable_property_fragment.h:67
const Schema & schema() const
Definition: mutable_property_fragment.cc:391
std::shared_ptr< RefColumnBase > get_vertex_id_column(uint8_t label) const
Definition: mutable_property_fragment.h:139
Definition: read_transaction.h:375
Definition: result.h:63
Definition: schema.h:29
Definition: single_edge_insert_transaction.h:30
Definition: single_vertex_insert_transaction.h:30
std::shared_ptr< ColumnBase > get_column(const std::string &name)
Definition: table.cc:171
Definition: update_transaction.h:39
Definition: version_manager.h:34
Definition: adj_list.h:23
MemoryStrategy
Definition: mmap_array.h:58
uint32_t timestamp_t
Definition: types.h:30
Definition: graph_db.h:43
std::string data_dir
Definition: graph_db.h:58
bool warmup
Definition: graph_db.h:61
GraphDBConfig()=default
std::string wal_uri
Definition: graph_db.h:72
bool enable_auto_compaction
Definition: graph_db.h:63
bool enable_monitoring
Definition: graph_db.h:62
std::string compiler_path
Definition: graph_db.h:59
int thread_num
Definition: graph_db.h:60
Schema schema
Definition: graph_db.h:57
GraphDBConfig(const Schema &schema_, const std::string &data_dir_, const std::string &compiler_path_="", int thread_num_=1)
Definition: graph_db.h:45
int memory_level
Definition: graph_db.h:71
Definition: graph_db.cc:35