-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbmanager.cpp
68 lines (60 loc) · 1.74 KB
/
dbmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "dbmanager.h"
#include <string>
#include <vector>
#include <map>
#include <rocksdb/db.h>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <folly/Singleton.h>
namespace cxxdoor {
namespace {
struct DbManagerTag {
};
}
static folly::Singleton<DbManager, DbManagerTag> the_instance;
DbManager::DbManager(const std::string &db_name) : _db_name(db_name) {
LOG(INFO) << "Initializing DbManager";
//rocksdb::DB *db;
rocksdb::Options options;
options.create_if_missing = true;
options.create_missing_column_families = true;
//options.
std::vector<std::string> cfNames;
std::vector<rocksdb::ColumnFamilyDescriptor> cfDescr;
std::vector<rocksdb::ColumnFamilyHandle *> cfHandles;
auto s = rocksdb::DB::ListColumnFamilies(options, _db_name, &cfNames);
for (auto &cfName: cfNames) {
cfDescr.push_back(rocksdb::ColumnFamilyDescriptor(cfName, rocksdb::ColumnFamilyOptions()));
}
rocksdb::DB::Open(options, _db_name, cfDescr, &cfHandles, &_db);
if (!s.ok()) {
LOG(WARNING) << s.ToString();
}
LOG(INFO) << "Listing column families";
for (auto cfHandle: cfHandles) {
LOG(INFO) << cfHandle->GetName();
auto name = cfHandle->GetName();
_columnFamilies[name] = cfHandle;
}
}
std::shared_ptr<DbManager> DbManager::getInstance() {
return the_instance.try_get();
}
DbManager::~DbManager() {
LOG(INFO) << "Destroying DB";
for (const auto &cf: _columnFamilies) {
try {
auto h = cf.second;
LOG(INFO) << "Destroying ColumnFamilyHandle: " << h->GetName();
auto s = _db->DestroyColumnFamilyHandle(h);
// h = nullptr;
if (!s.ok()) {
LOG(WARNING) << s.ToString();
}
} catch (std::exception &e) {
LOG(ERROR) << e.what();
}
}
delete _db;
}
}