From 73fb498ff7b36041de8a68baf59861026643512c Mon Sep 17 00:00:00 2001 From: Xu Peng Date: Thu, 23 May 2019 18:58:55 +0800 Subject: [PATCH] feat(db): add Archive conf in options Former-commit-id: caa950e7119264fd911f21cd10b4fbb2db896b7d --- cpp/src/db/Options.cpp | 44 +++++++++++++++++++++++++++++++++++ cpp/src/db/Options.h | 17 ++++++++++++++ cpp/unittest/db/db_tests.cpp | 45 ++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/cpp/src/db/Options.cpp b/cpp/src/db/Options.cpp index 2a0c01af..2a15f072 100644 --- a/cpp/src/db/Options.cpp +++ b/cpp/src/db/Options.cpp @@ -3,6 +3,11 @@ * Unauthorized copying of this file, via any medium is strictly prohibited. * Proprietary and confidential. ******************************************************************************/ +#include +#include +#include +#include + #include "Options.h" #include "Env.h" #include "DBMetaImpl.h" @@ -15,6 +20,45 @@ Options::Options() : env(Env::Default()) { } +ArchiveConf::ArchiveConf(const std::string& type, const std::string& criterias) { + ParseType(type); + ParseCritirias(criterias); +} + +void ArchiveConf::ParseCritirias(const std::string& criterias) { + std::stringstream ss(criterias); + std::vector tokens; + + boost::algorithm::split(tokens, criterias, boost::is_any_of(";")); + + if (tokens.size() == 0) { + return; + } + + for (auto& token : tokens) { + std::vector kv; + boost::algorithm::split(kv, token, boost::is_any_of(":")); + if (kv.size() != 2) { + LOG(WARNING) << "Invalid ArchiveConf Criterias: " << token << " Ignore!"; + continue; + } + if (kv[0] != "disk" && kv[0] != "days") { + LOG(WARNING) << "Invalid ArchiveConf Criterias: " << token << " Ignore!"; + continue; + } + auto value = std::stoi(kv[1]); + criterias_[kv[0]] = value; + } +} + +void ArchiveConf::ParseType(const std::string& type) { + if (type != "delete" && type != "swap") { + LOG(ERROR) << "Invalid Archive"; + assert(false); + } + type_ = type; +} + /* DBMetaOptions::DBMetaOptions(const std::string& dbpath, */ /* const std::string& uri) */ /* : path(dbpath), backend_uri(uri) { */ diff --git a/cpp/src/db/Options.h b/cpp/src/db/Options.h index 5bbcf6da..ee2d62f7 100644 --- a/cpp/src/db/Options.h +++ b/cpp/src/db/Options.h @@ -7,6 +7,7 @@ #include #include +#include namespace zilliz { namespace vecwise { @@ -14,6 +15,22 @@ namespace engine { class Env; +struct ArchiveConf { + using CriteriaT = std::map; + + ArchiveConf(const std::string& type, const std::string& criterias = "disk:512"); + + const std::string& GetType() const { return type_; } + const CriteriaT GetCriterias() const { return criterias_; } + +private: + void ParseCritirias(const std::string& type); + void ParseType(const std::string& criterias); + + std::string type_; + CriteriaT criterias_; +}; + struct DBMetaOptions { /* DBMetaOptions(const std::string&, const std::string&); */ std::string path; diff --git a/cpp/unittest/db/db_tests.cpp b/cpp/unittest/db/db_tests.cpp index c9bc958b..8030e574 100644 --- a/cpp/unittest/db/db_tests.cpp +++ b/cpp/unittest/db/db_tests.cpp @@ -12,6 +12,51 @@ using namespace zilliz::vecwise; +TEST_F(DBTest, CONFIG_TEST) { + { + EXPECT_DEATH(engine::ArchiveConf conf("wrong"), ""); + } + { + engine::ArchiveConf conf("delete"); + ASSERT_EQ(conf.GetType(), "delete"); + auto criterias = conf.GetCriterias(); + ASSERT_TRUE(criterias.size() == 1); + ASSERT_TRUE(criterias["disk"] == 512); + } + { + engine::ArchiveConf conf("swap"); + ASSERT_EQ(conf.GetType(), "swap"); + auto criterias = conf.GetCriterias(); + ASSERT_TRUE(criterias.size() == 1); + ASSERT_TRUE(criterias["disk"] == 512); + } + { + ASSERT_ANY_THROW(engine::ArchiveConf conf1("swap", "disk:")); + ASSERT_ANY_THROW(engine::ArchiveConf conf2("swap", "disk:a")); + engine::ArchiveConf conf("swap", "disk:1024"); + auto criterias = conf.GetCriterias(); + ASSERT_TRUE(criterias.size() == 1); + ASSERT_TRUE(criterias["disk"] == 1024); + } + { + ASSERT_ANY_THROW(engine::ArchiveConf conf1("swap", "days:")); + ASSERT_ANY_THROW(engine::ArchiveConf conf2("swap", "days:a")); + engine::ArchiveConf conf("swap", "days:100"); + auto criterias = conf.GetCriterias(); + ASSERT_TRUE(criterias.size() == 1); + ASSERT_TRUE(criterias["days"] == 100); + } + { + ASSERT_ANY_THROW(engine::ArchiveConf conf1("swap", "days:")); + ASSERT_ANY_THROW(engine::ArchiveConf conf2("swap", "days:a")); + engine::ArchiveConf conf("swap", "days:100;disk:200"); + auto criterias = conf.GetCriterias(); + ASSERT_TRUE(criterias.size() == 2); + ASSERT_TRUE(criterias["days"] == 100); + ASSERT_TRUE(criterias["disk"] == 200); + } +} + TEST_F(DBTest, DB_TEST) { static const std::string group_name = "test_group"; -- GitLab