feat(cpp/db): add background handling

Former-commit-id: c10f382912f3cfbeb45cedc0e17d355f9e2b0bc6
pull/191/head
Xu Peng 2019-04-14 18:51:08 +08:00 committed by xj.lin
parent 0352db4023
commit e4b9a62edf
2 changed files with 45 additions and 1 deletions

View File

@ -6,7 +6,9 @@ namespace vecengine {
DBImpl::DBImpl(const Options& options_, const std::string& name_)
: _dbname(name_),
_env(options_.env),
_options(options_) {
_options(options_),
_bg_work_finish_signal(_mutex),
_bg_compaction_scheduled(false) {
}
Status DBImpl::add_group(const GroupOptions& options_,
@ -17,6 +19,35 @@ Status DBImpl::add_group(const GroupOptions& options_,
}
void DBImpl::try_schedule_compaction() {
if (_bg_compaction_scheduled) return;
if (!_bg_error.ok()) return;
_bg_compaction_scheduled = true;
_env->schedule(&DBImpl::BGWork, this);
}
void DBImpl::BGWork(void* db_) {
reinterpret_cast<DBImpl*>(db_)->background_call();
}
void DBImpl::background_call() {
std::lock_guard<std::mutex> lock(_mutex);
assert(_bg_compaction_scheduled);
if (!_bg_error.ok()) return;
background_compaction();
}
void DBImpl::background_compaction() {
}
void DBImpl::compact_memory() {
}
/*
* DB
*/

View File

@ -1,6 +1,8 @@
#ifndef VECENGINE_DB_IMPL_H_
#define VECENGINE_DB_IMPL_H_
#include <mutex>
#include <condition_variable>
#include "db.h"
namespace vecengine {
@ -15,9 +17,15 @@ public:
const std::string& group_id_,
std::string& gid_) override;
void try_schedule_compaction();
virtual ~DBImpl();
private:
static void BGWork(void* db);
void background_call();
void background_compaction();
Status meta_add_group(const std::string& group_id_);
Status meta_add_group_file(const std::string& group_id_);
@ -25,6 +33,11 @@ private:
Env* const _env;
const Options _options;
std::mutex _mutex;
std::condition_variable _bg_work_finish_signal;
bool _bg_compaction_scheduled;
Status _bg_error;
}; // DBImpl
} // namespace vecengine