2016-08-30 07:45:07 +08:00
|
|
|
#pragma once
|
|
|
|
|
2017-06-06 23:04:49 +08:00
|
|
|
#include <algorithm>
|
2017-03-14 20:26:48 +08:00
|
|
|
#include <memory>
|
2017-06-06 23:04:49 +08:00
|
|
|
#include <vector>
|
2017-03-14 20:26:48 +08:00
|
|
|
|
2017-06-07 21:23:08 +08:00
|
|
|
#include "gflags/gflags.h"
|
|
|
|
|
2016-08-30 07:45:07 +08:00
|
|
|
#include "data_structures/concurrent/concurrent_map.hpp"
|
2017-02-04 16:01:15 +08:00
|
|
|
#include "database/graph_db.hpp"
|
2017-02-15 21:10:16 +08:00
|
|
|
#include "database/graph_db_accessor.hpp"
|
2017-06-06 23:04:49 +08:00
|
|
|
#include "durability/recovery.hpp"
|
2017-02-15 21:10:16 +08:00
|
|
|
|
2017-06-09 16:34:47 +08:00
|
|
|
DECLARE_string(snapshot_directory);
|
|
|
|
DECLARE_bool(recover_on_startup);
|
2017-06-06 23:04:49 +08:00
|
|
|
|
|
|
|
namespace fs = std::experimental::filesystem;
|
2017-02-18 18:54:37 +08:00
|
|
|
class Dbms {
|
|
|
|
public:
|
|
|
|
Dbms() {
|
2017-06-09 16:34:47 +08:00
|
|
|
if (FLAGS_recover_on_startup) {
|
|
|
|
if (fs::exists(fs::path(FLAGS_snapshot_directory))) {
|
2017-06-07 21:23:08 +08:00
|
|
|
auto accessor = dbs.access();
|
|
|
|
for (auto &snapshot_db :
|
2017-06-09 16:34:47 +08:00
|
|
|
fs::directory_iterator(FLAGS_snapshot_directory)) {
|
2017-06-07 21:23:08 +08:00
|
|
|
// create db and set it active
|
|
|
|
active(snapshot_db.path().filename(), snapshot_db);
|
|
|
|
}
|
2017-06-06 23:04:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// create the default database and set is a active
|
|
|
|
active("default");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an accessor to the active database.
|
|
|
|
*/
|
2017-03-14 20:26:48 +08:00
|
|
|
std::unique_ptr<GraphDbAccessor> active();
|
2017-02-18 18:54:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the database with the given name to be active.
|
|
|
|
* If there is no database with the given name,
|
2017-06-06 23:04:49 +08:00
|
|
|
* it's created. If snapshooting is true, snapshooter starts
|
|
|
|
* snapshooting on database creation.
|
2017-02-18 18:54:37 +08:00
|
|
|
*
|
|
|
|
* @return an accessor to the database with the given name.
|
|
|
|
*/
|
2017-06-06 23:04:49 +08:00
|
|
|
std::unique_ptr<GraphDbAccessor> active(
|
|
|
|
const std::string &name, const fs::path &snapshot_db_dir = fs::path());
|
2017-02-18 18:54:37 +08:00
|
|
|
|
|
|
|
// TODO: DELETE action
|
|
|
|
|
|
|
|
private:
|
|
|
|
// dbs container
|
|
|
|
ConcurrentMap<std::string, GraphDb> dbs;
|
|
|
|
|
|
|
|
// currently active database
|
|
|
|
std::atomic<GraphDb *> active_db;
|
2016-08-30 07:45:07 +08:00
|
|
|
};
|