From 2943126bda2b68e70b852cf0b8c215fdea2790c5 Mon Sep 17 00:00:00 2001 From: Xu Peng Date: Mon, 22 Apr 2019 15:08:26 +0800 Subject: [PATCH] feat(db): add ttl files cleanup api Former-commit-id: bb84f6f2baa8c20c9f0e745c67213af236609507 --- cpp/src/db/DBImpl.cpp | 2 ++ cpp/src/db/DBMetaImpl.cpp | 32 ++++++++++++++++++++++++++++++++ cpp/src/db/DBMetaImpl.h | 2 ++ cpp/src/db/LocalMetaImpl.cpp | 5 +++++ cpp/src/db/LocalMetaImpl.h | 2 ++ cpp/src/db/Meta.h | 1 + 6 files changed, 44 insertions(+) diff --git a/cpp/src/db/DBImpl.cpp b/cpp/src/db/DBImpl.cpp index 8f0fc277..bffea82e 100644 --- a/cpp/src/db/DBImpl.cpp +++ b/cpp/src/db/DBImpl.cpp @@ -242,6 +242,8 @@ Status DBImpl::background_merge_files(const std::string& group_id) { try_build_index(); } + _pMeta->cleanup_ttl_files(1); + return Status::OK(); } diff --git a/cpp/src/db/DBMetaImpl.cpp b/cpp/src/db/DBMetaImpl.cpp index 194570d1..45a92fee 100644 --- a/cpp/src/db/DBMetaImpl.cpp +++ b/cpp/src/db/DBMetaImpl.cpp @@ -366,6 +366,38 @@ Status DBMetaImpl::update_files(GroupFilesSchema& files) { return Status::OK(); } +Status DBMetaImpl::cleanup_ttl_files(uint16_t seconds) { + auto now = GetMicroSecTimeStamp(); + auto selected = ConnectorPtr->select(columns(&GroupFileSchema::id, + &GroupFileSchema::group_id, + &GroupFileSchema::file_id, + &GroupFileSchema::file_type, + &GroupFileSchema::rows, + &GroupFileSchema::date), + where(c(&GroupFileSchema::file_type) == (int)GroupFileSchema::TO_DELETE and + c(&GroupFileSchema::updated_time) > now - 1000000*seconds)); + + GroupFilesSchema updated; + + for (auto& file : selected) { + GroupFileSchema group_file; + group_file.id = std::get<0>(file); + group_file.group_id = std::get<1>(file); + group_file.file_id = std::get<2>(file); + group_file.file_type = std::get<3>(file); + group_file.rows = std::get<4>(file); + group_file.date = std::get<5>(file); + GetGroupFilePath(group_file); + if (group_file.file_type == GroupFileSchema::TO_DELETE) { + boost::filesystem::remove(group_file.location); + } + ConnectorPtr->remove(group_file.id); + std::cout << "Removing deleted id=" << group_file.id << " location=" << group_file.location << std::endl; + } + + return Status::OK(); +} + Status DBMetaImpl::cleanup() { auto selected = ConnectorPtr->select(columns(&GroupFileSchema::id, &GroupFileSchema::group_id, diff --git a/cpp/src/db/DBMetaImpl.h b/cpp/src/db/DBMetaImpl.h index 726ce15b..7ac04433 100644 --- a/cpp/src/db/DBMetaImpl.h +++ b/cpp/src/db/DBMetaImpl.h @@ -46,6 +46,8 @@ public: virtual Status cleanup() override; + virtual Status cleanup_ttl_files(uint16_t seconds) override; + virtual ~DBMetaImpl(); private: diff --git a/cpp/src/db/LocalMetaImpl.cpp b/cpp/src/db/LocalMetaImpl.cpp index 07a26240..f7192a05 100644 --- a/cpp/src/db/LocalMetaImpl.cpp +++ b/cpp/src/db/LocalMetaImpl.cpp @@ -241,6 +241,11 @@ Status LocalMetaImpl::cleanup() { return Status::OK(); } +Status LocalMetaImpl::cleanup_ttl_files(uint16_t seconds) { + // PXU TODO + return Status::OK(); +} + } // namespace meta } // namespace engine } // namespace vecwise diff --git a/cpp/src/db/LocalMetaImpl.h b/cpp/src/db/LocalMetaImpl.h index 8d6bb570..6c687500 100644 --- a/cpp/src/db/LocalMetaImpl.h +++ b/cpp/src/db/LocalMetaImpl.h @@ -41,6 +41,8 @@ public: virtual Status files_to_index(GroupFilesSchema&) override; + virtual Status cleanup_ttl_files(uint16_t seconds) override; + private: Status GetGroupMetaInfoByPath(const std::string& path, GroupSchema& group_info); diff --git a/cpp/src/db/Meta.h b/cpp/src/db/Meta.h index c630ab21..4dbc275f 100644 --- a/cpp/src/db/Meta.h +++ b/cpp/src/db/Meta.h @@ -81,6 +81,7 @@ public: virtual Status files_to_index(GroupFilesSchema&) = 0; virtual Status cleanup() = 0; + virtual Status cleanup_ttl_files(uint16_t) = 0; static DateT GetDate(const std::time_t& t); static DateT GetDate(); -- GitLab