feat(db): add count api

Former-commit-id: 9fb2b765a48d1753ba31a68e7b9b207273adb199
This commit is contained in:
Xu Peng 2019-04-24 10:27:59 +08:00
parent 4ce28a1e21
commit 06ab815c7b
9 changed files with 49 additions and 1 deletions

View File

@ -32,6 +32,8 @@ public:
virtual Status drop_all() = 0;
virtual Status count(const std::string& group_id, long& result) = 0;
DB() = default;
DB(const DB&) = delete;
DB& operator=(const DB&) = delete;

View File

@ -348,6 +348,10 @@ Status DBImpl::drop_all() {
return _pMeta->drop_all();
}
Status DBImpl::count(const std::string& group_id, long& result) {
return _pMeta->count(group_id, result);
}
DBImpl::~DBImpl() {
{
std::unique_lock<std::mutex> lock(_mutex);

View File

@ -38,6 +38,8 @@ public:
virtual Status drop_all() override;
virtual Status count(const std::string& group_id, long& result) override;
virtual ~DBImpl();
private:

View File

@ -427,6 +427,31 @@ Status DBMetaImpl::cleanup() {
return Status::OK();
}
Status DBMetaImpl::count(const std::string& group_id, long& result) {
auto selected = ConnectorPtr->select(columns(&GroupFileSchema::rows,
&GroupFileSchema::date),
where((c(&GroupFileSchema::file_type) == (int)GroupFileSchema::RAW or
c(&GroupFileSchema::file_type) == (int)GroupFileSchema::TO_INDEX or
c(&GroupFileSchema::file_type) == (int)GroupFileSchema::INDEX) and
c(&GroupFileSchema::group_id) == group_id));
GroupSchema group_info;
group_info.group_id = group_id;
auto status = get_group_no_lock(group_info);
if (!status.ok()) {
return status;
}
result = 0;
for (auto& file : selected) {
result += std::get<0>(file);
}
result /= group_info.dimension;
return Status::OK();
}
Status DBMetaImpl::drop_all() {
if (boost::filesystem::is_directory(_options.path)) {
boost::filesystem::remove_all(_options.path);

View File

@ -50,6 +50,8 @@ public:
virtual Status drop_all() override;
virtual Status count(const std::string& group_id, long& result) override;
virtual ~DBMetaImpl();
private:

View File

@ -251,6 +251,11 @@ Status LocalMetaImpl::drop_all() {
return Status::OK();
}
Status LocalMetaImpl::count(const std::string& group_id, long& result) {
// PXU TODO
return Status::OK();
}
} // namespace meta
} // namespace engine
} // namespace vecwise

View File

@ -43,6 +43,8 @@ public:
virtual Status cleanup_ttl_files(uint16_t seconds) override;
virtual Status count(const std::string& group_id, long& result) override;
virtual Status drop_all() override;
private:

View File

@ -85,6 +85,8 @@ public:
virtual Status drop_all() = 0;
virtual Status count(const std::string& group_id, long& result) = 0;
static DateT GetDate(const std::time_t& t);
static DateT GetDate();

View File

@ -66,7 +66,7 @@ TEST(DBTest, DB_TEST) {
qxb[d * i] += i / 2000.;
}
int loop = 500000;
int loop = 50000;
for (auto i=0; i<loop; ++i) {
if (i==40) {
@ -78,6 +78,10 @@ TEST(DBTest, DB_TEST) {
std::this_thread::sleep_for(std::chrono::seconds(2));
long count = 0;
db->count(group_name, count);
LOG(DEBUG) << "Count=" << count;
engine::QueryResults results;
int k = 10;
LOG(DEBUG) << "PRE";