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 <shared_mutex>
24 #include <thread>
25 #include <vector>
26 
37 
38 namespace gs {
39 
40 class GraphDB;
41 class GraphDBSession;
42 struct SessionLocalContext;
43 
44 struct GraphDBConfig {
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 
57  std::string data_dir;
58  std::string compiler_path;
60  bool warmup;
63 
64  /*
65  0 - sync with disk;
66  1 - mmap virtual memory;
67  2 - preferring hugepages;
68  3 - force hugepages;
69  */
71 };
72 
73 struct QueryCache {
74  bool get(const std::string& key, std::string_view& value) {
75  std::shared_lock<std::shared_mutex> lock(mutex);
76  auto it = cache.find(key);
77  if (it != cache.end()) {
78  value = it->second;
79  return true;
80  }
81  return false;
82  }
83 
84  void put(const std::string& key, const std::string_view& value) {
85  std::unique_lock<std::shared_mutex> lock(mutex);
86  cache[key] = value;
87  }
88  std::shared_mutex mutex;
89  std::unordered_map<std::string, std::string> cache;
90 };
91 
92 class GraphDB {
93  public:
94  GraphDB();
95  ~GraphDB();
96 
97  static GraphDB& get();
98 
107  Result<bool> Open(const Schema& schema, const std::string& data_dir,
108  int32_t thread_num = 1, bool warmup = false,
109  bool memory_only = true,
110  bool enable_auto_compaction = false);
111 
112  Result<bool> Open(const GraphDBConfig& config);
113 
117  void Close();
118 
123  ReadTransaction GetReadTransaction(int thread_id = 0);
124 
130  InsertTransaction GetInsertTransaction(int thread_id = 0);
131 
138  int thread_id = 0);
139 
146 
152  UpdateTransaction GetUpdateTransaction(int thread_id = 0);
153 
154  inline const MutablePropertyFragment& graph() const { return graph_; }
155  inline MutablePropertyFragment& graph() { return graph_; }
156 
157  inline const Schema& schema() const { return graph_.schema(); }
158 
159  inline std::shared_ptr<ColumnBase> get_vertex_property_column(
160  uint8_t label, const std::string& col_name) const {
161  return graph_.get_vertex_table(label).get_column(col_name);
162  }
163 
164  inline std::shared_ptr<RefColumnBase> get_vertex_id_column(
165  uint8_t label) const {
166  return graph_.get_vertex_id_column(label);
167  }
168 
169  AppWrapper CreateApp(uint8_t app_type, int thread_id);
170 
171  void GetAppInfo(Encoder& result);
172 
173  GraphDBSession& GetSession(int thread_id);
174  const GraphDBSession& GetSession(int thread_id) const;
175 
176  int SessionNum() const;
177 
180 
181  QueryCache& getQueryCache() const;
182 
183  std::string work_dir() const { return work_dir_; }
184 
185  void OutputCypherProfiles(const std::string& prefix);
186 
187  private:
188  bool registerApp(const std::string& path, uint8_t index = 0);
189 
190  void ingestWals(const std::vector<std::string>& wals,
191  const std::string& work_dir, int thread_num);
192 
193  void initApps(
194  const std::unordered_map<std::string, std::pair<std::string, uint8_t>>&
195  plugins);
196 
197  void openWalAndCreateContexts(const std::string& data_dir_path,
198  MemoryStrategy allocator_strategy);
199 
200  void showAppMetrics() const;
201 
202  size_t getExecutedQueryNum() const;
203 
204  friend class GraphDBSession;
205 
206  std::string work_dir_;
208 
210 
213 
214  std::array<std::string, 256> app_paths_;
215  std::array<std::shared_ptr<AppFactoryBase>, 256> app_factories_;
216 
218 
219  std::thread monitor_thread_;
221 
224  std::thread compact_thread_;
225 };
226 
227 } // namespace gs
228 
229 #endif // GRAPHSCOPE_DATABASE_GRAPH_DB_H_
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:159
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:207
gs::QueryCache
Definition: graph_db.h:73
gs::GraphDB::graph
const MutablePropertyFragment & graph() const
Definition: graph_db.h:154
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:183
gs::GraphDB::monitor_thread_running_
bool monitor_thread_running_
Definition: graph_db.h:220
gs::GraphDBConfig::thread_num
int thread_num
Definition: graph_db.h:59
gs::QueryCache::get
bool get(const std::string &key, std::string_view &value)
Definition: graph_db.h:74
gs::MutablePropertyFragment::schema
const Schema & schema() const
Definition: mutable_property_fragment.cc:388
gs::GraphDB::get
static GraphDB & get()
Definition: graph_db.cc:68
gs::GraphDB::GetSession
GraphDBSession & GetSession(int thread_id)
Definition: graph_db.cc:292
gs::GraphDB::showAppMetrics
void showAppMetrics() const
Definition: graph_db.cc:483
gs::InsertTransaction
Definition: insert_transaction.h:33
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:209
gs::GraphDB::CreateApp
AppWrapper CreateApp(uint8_t app_type, int thread_id)
Definition: graph_db.cc:309
gs
Definition: adj_list.h:23
single_edge_insert_transaction.h
gs::GraphDB::monitor_thread_
std::thread monitor_thread_
Definition: graph_db.h:219
gs::GraphDB::GetUpdateTransaction
UpdateTransaction GetUpdateTransaction(int thread_id=0)
Create a transaction to update vertices and edges.
Definition: graph_db.cc:288
gs::QueryCache::cache
std::unordered_map< std::string, std::string > cache
Definition: graph_db.h:89
insert_transaction.h
gs::GraphDB::SessionNum
int SessionNum() const
Definition: graph_db.cc:300
gs::GraphDB::version_manager_
VersionManager version_manager_
Definition: graph_db.h:212
gs::SessionLocalContext
Definition: graph_db.cc:34
gs::GraphDB::registerApp
bool registerApp(const std::string &path, uint8_t index=0)
Definition: graph_db.cc:319
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:73
gs::Encoder
Definition: app_utils.h:25
gs::GraphDBConfig::enable_monitoring
bool enable_monitoring
Definition: graph_db.h:61
gs::SingleEdgeInsertTransaction
Definition: single_edge_insert_transaction.h:30
mutable_property_fragment.h
gs::QueryCache::mutex
std::shared_mutex mutex
Definition: graph_db.h:88
gs::GraphDB::GetAppInfo
void GetAppInfo(Encoder &result)
Definition: graph_db.cc:335
gs::MutablePropertyFragment
Definition: mutable_property_fragment.h:37
gs::GraphDB::graph_
MutablePropertyFragment graph_
Definition: graph_db.h:211
gs::GraphDB::GetSingleVertexInsertTransaction
SingleVertexInsertTransaction GetSingleVertexInsertTransaction(int thread_id=0)
Create a transaction to insert a single vertex.
Definition: graph_db.cc:278
gs::GraphDBConfig::warmup
bool warmup
Definition: graph_db.h:60
update_transaction.h
gs::GraphDB::query_cache_
QueryCache query_cache_
Definition: graph_db.h:217
gs::GraphDB::openWalAndCreateContexts
void openWalAndCreateContexts(const std::string &data_dir_path, MemoryStrategy allocator_strategy)
Definition: graph_db.cc:454
gs::GraphDB::app_factories_
std::array< std::shared_ptr< AppFactoryBase >, 256 > app_factories_
Definition: graph_db.h:215
gs::Schema
Definition: schema.h:29
loading_config.h
gs::GraphDBConfig::enable_auto_compaction
bool enable_auto_compaction
Definition: graph_db.h:62
gs::GraphDBSession
Definition: graph_db_session.h:36
gs::GraphDB
Definition: graph_db.h:92
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::GraphDB::ingestWals
void ingestWals(const std::vector< std::string > &wals, const std::string &work_dir, int thread_num)
Definition: graph_db.cc:374
gs::GraphDBConfig::compiler_path
std::string compiler_path
Definition: graph_db.h:58
gs::VersionManager
Definition: version_manager.h:34
gs::GraphDB::getExecutedQueryNum
size_t getExecutedQueryNum() const
Definition: graph_db.cc:502
gs::GraphDB::compact_thread_
std::thread compact_thread_
Definition: graph_db.h:224
gs::GraphDB::GraphDB
GraphDB()
gs::GraphDBConfig::memory_level
int memory_level
Definition: graph_db.h:70
gs::ReadTransaction
Definition: read_transaction.h:311
gs::QueryCache::put
void put(const std::string &key, const std::string_view &value)
Definition: graph_db.h:84
gs::SingleVertexInsertTransaction
Definition: single_vertex_insert_transaction.h:30
gs::GraphDBConfig::schema
Schema schema
Definition: graph_db.h:56
gs::GraphDBConfig::data_dir
std::string data_dir
Definition: graph_db.h:57
gs::GraphDB::initApps
void initApps(const std::unordered_map< std::string, std::pair< std::string, uint8_t >> &plugins)
Definition: graph_db.cc:399
gs::GraphDB::schema
const Schema & schema() const
Definition: graph_db.h:157
gs::GraphDB::GetReadTransaction
ReadTransaction GetReadTransaction(int thread_id=0)
Create a transaction to read vertices and edges.
Definition: graph_db.cc:270
gs::GraphDB::GetSingleEdgeInsertTransaction
SingleEdgeInsertTransaction GetSingleEdgeInsertTransaction(int thread_id=0)
Create a transaction to insert a single edge.
Definition: graph_db.cc:283
gs::GraphDB::Close
void Close()
Close the current opened graph.
Definition: graph_db.cc:247
gs::GraphDB::compact_thread_running_
bool compact_thread_running_
Definition: graph_db.h:223
version_manager.h
gs::GraphDB::app_paths_
std::array< std::string, 256 > app_paths_
Definition: graph_db.h:214
gs::GraphDB::~GraphDB
~GraphDB()
Definition: graph_db.cc:53
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:164
gs::UpdateTransaction
Definition: update_transaction.h:38
gs::GraphDB::graph
MutablePropertyFragment & graph()
Definition: graph_db.h:155
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:274
gs::GraphDB::GetLastCompactionTimestamp
timestamp_t GetLastCompactionTimestamp() const
Definition: graph_db.cc:305
gs::GraphDBConfig
Definition: graph_db.h:44
gs::GraphDB::work_dir_
std::string work_dir_
Definition: graph_db.h:206
gs::GraphDB::last_compaction_ts_
timestamp_t last_compaction_ts_
Definition: graph_db.h:222
read_transaction.h
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:302
gs::GraphDB::getQueryCache
QueryCache & getQueryCache() const
Definition: graph_db.cc:510
gs::GraphDB::OutputCypherProfiles
void OutputCypherProfiles(const std::string &prefix)
Definition: graph_db.cc:512