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:
79  GraphDB();
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_
gs::GraphDB::config
const GraphDBConfig & config() const
Definition: graph_db.h:170
gs::MemoryStrategy
MemoryStrategy
Definition: mmap_array.h:58
gs::GraphDB::get_vertex_property_column
std::shared_ptr< ColumnBase > get_vertex_property_column(uint8_t label, const std::string &col_name) const
Definition: graph_db.h:144
gs::MutablePropertyFragment::get_vertex_table
Table & get_vertex_table(label_t vertex_label)
Definition: mutable_property_fragment.h:67
gs::Result
Definition: result.h:62
gs::GraphDB::contexts_
SessionLocalContext * contexts_
Definition: graph_db.h:194
gs::GraphDB::graph
const MutablePropertyFragment & graph() const
Definition: graph_db.h:139
single_vertex_insert_transaction.h
gs::timestamp_t
uint32_t timestamp_t
Definition: types.h:30
gs::GraphDB::work_dir
std::string work_dir() const
Definition: graph_db.h:166
gs::GraphDB::monitor_thread_running_
bool monitor_thread_running_
Definition: graph_db.h:205
gs::GraphDBConfig::thread_num
int thread_num
Definition: graph_db.h:60
gs::MutablePropertyFragment::schema
const Schema & schema() const
Definition: mutable_property_fragment.cc:391
gs::GraphDB::get
static GraphDB & get()
Definition: graph_db.cc:73
gs::GraphDB::GetSession
GraphDBSession & GetSession(int thread_id)
Definition: graph_db.cc:299
gs::GraphDB::showAppMetrics
void showAppMetrics() const
Definition: graph_db.cc:497
gs::InsertTransaction
Definition: insert_transaction.h:34
gs::MutablePropertyFragment::get_vertex_id_column
std::shared_ptr< RefColumnBase > get_vertex_id_column(uint8_t label) const
Definition: mutable_property_fragment.h:139
gs::GraphDB::thread_num_
int thread_num_
Definition: graph_db.h:196
gs::GraphDBConfig::wal_uri
std::string wal_uri
Definition: graph_db.h:72
gs::GraphDB::CreateApp
AppWrapper CreateApp(uint8_t app_type, int thread_id)
Definition: graph_db.cc:316
gs
Definition: adj_list.h:23
single_edge_insert_transaction.h
gs::GraphDB::monitor_thread_
std::thread monitor_thread_
Definition: graph_db.h:204
gs::GraphDB::GetUpdateTransaction
UpdateTransaction GetUpdateTransaction(int thread_id=0)
Create a transaction to update vertices and edges.
Definition: graph_db.cc:295
insert_transaction.h
gs::GraphDB::SessionNum
int SessionNum() const
Definition: graph_db.cc:307
gs::GraphDB::version_manager_
VersionManager version_manager_
Definition: graph_db.h:199
gs::SessionLocalContext
Definition: graph_db.cc:35
gs::GraphDB::registerApp
bool registerApp(const std::string &path, uint8_t index=0)
Definition: graph_db.cc:326
gs::GraphDB::Open
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:78
gs::Encoder
Definition: app_utils.h:25
gs::GraphDBConfig::enable_monitoring
bool enable_monitoring
Definition: graph_db.h:62
gs::SingleEdgeInsertTransaction
Definition: single_edge_insert_transaction.h:30
mutable_property_fragment.h
gs::GraphDB::GetAppInfo
void GetAppInfo(Encoder &result)
Definition: graph_db.cc:342
gs::MutablePropertyFragment
Definition: mutable_property_fragment.h:37
gs::GraphDB::graph_
MutablePropertyFragment graph_
Definition: graph_db.h:198
gs::GraphDB::GetSingleVertexInsertTransaction
SingleVertexInsertTransaction GetSingleVertexInsertTransaction(int thread_id=0)
Create a transaction to insert a single vertex.
Definition: graph_db.cc:285
gs::GraphDBConfig::warmup
bool warmup
Definition: graph_db.h:61
update_transaction.h
gs::GraphDB::config_
GraphDBConfig config_
Definition: graph_db.h:192
gs::GraphDBConfig::GraphDBConfig
GraphDBConfig()=default
gs::GraphDB::ingestWals
void ingestWals(IWalParser &parser, const std::string &work_dir, int thread_num)
Definition: graph_db.cc:381
gs::GraphDB::app_factories_
std::array< std::shared_ptr< AppFactoryBase >, 256 > app_factories_
Definition: graph_db.h:202
gs::Schema
Definition: schema.h:29
loading_config.h
gs::IWalParser
Definition: wal.h:83
gs::GraphDBConfig::enable_auto_compaction
bool enable_auto_compaction
Definition: graph_db.h:63
gs::GraphDBSession
Definition: graph_db_session.h:36
gs::GraphDB
Definition: graph_db.h:77
gs::GraphDBConfig::GraphDBConfig
GraphDBConfig(const Schema &schema_, const std::string &data_dir_, const std::string &compiler_path_="", int thread_num_=1)
Definition: graph_db.h:45
loader_factory.h
gs::GraphDBConfig::compiler_path
std::string compiler_path
Definition: graph_db.h:59
gs::VersionManager
Definition: version_manager.h:34
gs::GraphDB::getExecutedQueryNum
size_t getExecutedQueryNum() const
Definition: graph_db.cc:516
gs::GraphDB::compact_thread_
std::thread compact_thread_
Definition: graph_db.h:209
gs::GraphDB::GraphDB
GraphDB()
gs::GraphDBConfig::memory_level
int memory_level
Definition: graph_db.h:71
gs::ReadTransaction
Definition: read_transaction.h:375
gs::SingleVertexInsertTransaction
Definition: single_vertex_insert_transaction.h:30
gs::GraphDBConfig::schema
Schema schema
Definition: graph_db.h:57
gs::GraphDBConfig::data_dir
std::string data_dir
Definition: graph_db.h:58
gs::GraphDB::initApps
void initApps(const std::unordered_map< std::string, std::pair< std::string, uint8_t >> &plugins)
Definition: graph_db.cc:405
gs::GraphDB::schema
const Schema & schema() const
Definition: graph_db.h:142
gs::GraphDB::GetReadTransaction
ReadTransaction GetReadTransaction(int thread_id=0)
Create a transaction to read vertices and edges.
Definition: graph_db.cc:277
gs::GraphDB::GetSingleEdgeInsertTransaction
SingleEdgeInsertTransaction GetSingleEdgeInsertTransaction(int thread_id=0)
Create a transaction to insert a single edge.
Definition: graph_db.cc:290
gs::GraphDB::Close
void Close()
Close the current opened graph.
Definition: graph_db.cc:254
gs::GraphDB::compact_thread_running_
bool compact_thread_running_
Definition: graph_db.h:208
version_manager.h
gs::GraphDB::app_paths_
std::array< std::string, 256 > app_paths_
Definition: graph_db.h:201
gs::GraphDB::~GraphDB
~GraphDB()
Definition: graph_db.cc:56
gs::AppWrapper
Definition: app_base.h:78
app_base.h
gs::GraphDB::get_vertex_id_column
std::shared_ptr< RefColumnBase > get_vertex_id_column(uint8_t label) const
Definition: graph_db.h:149
gs::UpdateTransaction
Definition: update_transaction.h:39
gs::GraphDB::graph
MutablePropertyFragment & graph()
Definition: graph_db.h:140
gs::GraphDB::GetInsertTransaction
InsertTransaction GetInsertTransaction(int thread_id=0)
Create a transaction to insert vertices and edges with a default allocator.
Definition: graph_db.cc:281
gs::GraphDB::GetLastCompactionTimestamp
timestamp_t GetLastCompactionTimestamp() const
Definition: graph_db.cc:312
gs::GraphDBConfig
Definition: graph_db.h:43
gs::GraphDB::work_dir_
std::string work_dir_
Definition: graph_db.h:193
gs::GraphDB::last_compaction_ts_
timestamp_t last_compaction_ts_
Definition: graph_db.h:207
read_transaction.h
gs::GraphDB::openWalAndCreateContexts
void openWalAndCreateContexts(const GraphDBConfig &config, const std::string &data_dir, MemoryStrategy allocator_strategy)
Definition: graph_db.cc:460
gs::Table::get_column
std::shared_ptr< ColumnBase > get_column(const std::string &name)
Definition: table.cc:171
gs::GraphDB::UpdateCompactionTimestamp
void UpdateCompactionTimestamp(timestamp_t ts)
Definition: graph_db.cc:309
gs::GraphDB::OutputCypherProfiles
void OutputCypherProfiles(const std::string &prefix)
Definition: graph_db.cc:524