diff --git a/cpp/src/db/DBImpl.cpp b/cpp/src/db/DBImpl.cpp index 94a2e76c9427aaa24a6a2216b1f046002fb7aca6..6f6cfcb81bd1046254328dd82aee71ebde041d6d 100644 --- a/cpp/src/db/DBImpl.cpp +++ b/cpp/src/db/DBImpl.cpp @@ -408,10 +408,10 @@ void DBImpl::BackgroundCompaction(std::set table_ids) { meta_ptr_->CleanUpFilesWithTTL(ttl); } -void DBImpl::StartBuildIndexTask() { +void DBImpl::StartBuildIndexTask(bool force) { static uint64_t index_clock_tick = 0; index_clock_tick++; - if(index_clock_tick%INDEX_ACTION_INTERVAL != 0) { + if(!force && (index_clock_tick%INDEX_ACTION_INTERVAL != 0)) { return; } @@ -431,8 +431,20 @@ void DBImpl::StartBuildIndexTask() { } Status DBImpl::BuildIndex(const std::string& table_id) { - meta_ptr_->UpdateTableFilesToIndex(table_id); - return BuildIndexByTable(table_id); + bool has = false; + meta_ptr_->HasNonIndexFiles(table_id, has); + int times = 1; + + while (has) { + ENGINE_LOG_DEBUG << "Non index files detected! Will build index " << times; + meta_ptr_->UpdateTableFilesToIndex(table_id); + StartBuildIndexTask(true); + std::this_thread::sleep_for(std::chrono::milliseconds(std::min(10*1000, times*100))); + meta_ptr_->HasNonIndexFiles(table_id, has); + times++; + } + return Status::OK(); + /* return BuildIndexByTable(table_id); */ } Status DBImpl::BuildIndex(const meta::TableFileSchema& file) { diff --git a/cpp/src/db/DBImpl.h b/cpp/src/db/DBImpl.h index e5042354ea860d10e6673aba6d880544c2ad364b..064d6d00933c0ac37306d704db40f2ad799e2b05 100644 --- a/cpp/src/db/DBImpl.h +++ b/cpp/src/db/DBImpl.h @@ -109,7 +109,7 @@ class DBImpl : public DB { Status BackgroundMergeFiles(const std::string &table_id); void BackgroundCompaction(std::set table_ids); - void StartBuildIndexTask(); + void StartBuildIndexTask(bool force=false); void BackgroundBuildIndex(); Status diff --git a/cpp/src/db/DBMetaImpl.cpp b/cpp/src/db/DBMetaImpl.cpp index 0e2b52dbe20e7c244120f5fd0eb4eae0df92d361..551073ff17e5a282ff45dd21a4cbc243211126dd 100644 --- a/cpp/src/db/DBMetaImpl.cpp +++ b/cpp/src/db/DBMetaImpl.cpp @@ -316,6 +316,28 @@ Status DBMetaImpl::DescribeTable(TableSchema &table_schema) { return Status::OK(); } +Status DBMetaImpl::HasNonIndexFiles(const std::string& table_id, bool& has) { + has = false; + try { + auto selected = ConnectorPtr->select(columns(&TableFileSchema::id_), + where((c(&TableFileSchema::file_type_) == (int) TableFileSchema::RAW + or + c(&TableFileSchema::file_type_) == (int) TableFileSchema::TO_INDEX) + and c(&TableFileSchema::table_id_) == table_id + )); + + if (selected.size() >= 1) { + has = true; + } else { + has = false; + } + + } catch (std::exception &e) { + return HandleException("Encounter exception when check non index files", e); + } + return Status::OK(); +} + Status DBMetaImpl::HasTable(const std::string &table_id, bool &has_or_not) { has_or_not = false; diff --git a/cpp/src/db/DBMetaImpl.h b/cpp/src/db/DBMetaImpl.h index 597800c07b7d06fa69b6f4bab855f072339f9c06..f0b17c4106d0f0c5dc1d612e603816d95c437c31 100644 --- a/cpp/src/db/DBMetaImpl.h +++ b/cpp/src/db/DBMetaImpl.h @@ -35,6 +35,8 @@ public: const std::vector& ids, TableFilesSchema& table_files) override; + virtual Status HasNonIndexFiles(const std::string& table_id, bool& has) override; + virtual Status UpdateTableFilesToIndex(const std::string& table_id) override; virtual Status UpdateTableFile(TableFileSchema& file_schema) override; diff --git a/cpp/src/db/Meta.h b/cpp/src/db/Meta.h index beab6e22a251688d65c05457b2576d85cba062f5..1d83817f5d7db03479cf18b668689763a1b36be2 100644 --- a/cpp/src/db/Meta.h +++ b/cpp/src/db/Meta.h @@ -58,6 +58,8 @@ public: virtual Status FilesToIndex(TableFilesSchema&) = 0; + virtual Status HasNonIndexFiles(const std::string& table_id, bool& has) = 0; + virtual Status CleanUp() = 0; virtual Status CleanUpFilesWithTTL(uint16_t) = 0; diff --git a/cpp/src/db/MySQLMetaImpl.cpp b/cpp/src/db/MySQLMetaImpl.cpp index 4d4b858deabe2506b162938c95e0d6f907874618..14ce38c743114bba3175923102f195267da11cb7 100644 --- a/cpp/src/db/MySQLMetaImpl.cpp +++ b/cpp/src/db/MySQLMetaImpl.cpp @@ -436,6 +436,11 @@ namespace meta { return Status::OK(); } + Status MySQLMetaImpl::HasNonIndexFiles(const std::string& table_id, bool& has) { + // TODO + return Status::OK(); + } + Status MySQLMetaImpl::DeleteTable(const std::string& table_id) { // std::lock_guard lock(mysql_mutex); diff --git a/cpp/src/db/MySQLMetaImpl.h b/cpp/src/db/MySQLMetaImpl.h index f8591c25b978561851ce577b89aaecb1fc926c2e..9daa9f83cbc3d881a44807095f889d70003355ab 100644 --- a/cpp/src/db/MySQLMetaImpl.h +++ b/cpp/src/db/MySQLMetaImpl.h @@ -40,6 +40,8 @@ namespace meta { const std::vector& ids, TableFilesSchema& table_files) override; + virtual Status HasNonIndexFiles(const std::string& table_id, bool& has) override; + virtual Status UpdateTableFile(TableFileSchema& file_schema) override; virtual Status UpdateTableFilesToIndex(const std::string& table_id) override;