ttl_unit.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #include <gtest/gtest.h>
  19. #include "include/index_wrapper.h"
  20. #include "unittest_util.h"
  21. namespace wwsearch {
  22. class DBTTLTest : public ::testing::Test {
  23. public:
  24. static DefaultIndexWrapper *index;
  25. static uint64_t document_id;
  26. wwsearch::TableID table;
  27. std::vector<DocumentUpdater *> documents;
  28. public:
  29. DBTTLTest() {
  30. table.business_type = 1;
  31. table.partition_set = 1;
  32. }
  33. static void OpenDB() {
  34. index = new DefaultIndexWrapper();
  35. index->DBParams().path = std::string("/tmp/unit_") + std::string("ttl");
  36. index->Config().SetLogLevel(g_debug ? wwsearch::kSearchLogLevelDebug
  37. : wwsearch::kSearchLogLevelError);
  38. auto status = index->Open(g_use_rocksdb, g_use_compression);
  39. ASSERT_TRUE(status.GetCode() == 0);
  40. }
  41. static void SetUpTestCase() { OpenDB(); }
  42. static void CloseDB(bool drop = false) {
  43. if (index != nullptr) {
  44. if (drop) {
  45. auto status = index->vdb_->DropDB();
  46. EXPECT_EQ(0, status.GetCode());
  47. }
  48. delete index;
  49. index = nullptr;
  50. }
  51. }
  52. static void TearDownTestCase() { CloseDB(true); }
  53. virtual void SetUp() override { table.partition_set++; }
  54. virtual void TearDown() override {
  55. for (auto du : documents) {
  56. delete du;
  57. }
  58. documents.clear();
  59. }
  60. uint64_t GetDocumentID() { return document_id++; }
  61. private:
  62. };
  63. DefaultIndexWrapper *DBTTLTest::index = nullptr;
  64. DocumentID DBTTLTest::document_id = 1;
  65. TEST_F(DBTTLTest, TTLDeleteRecords) {
  66. auto db = index->vdb_;
  67. std::string key, value;
  68. auto write_buffer = db->NewWriteBuffer(nullptr);
  69. ASSERT_TRUE(write_buffer->Put(kPaxosLogColumn, "key", "value").OK());
  70. ASSERT_TRUE(db->FlushBuffer(write_buffer).OK());
  71. db->ReleaseWriteBuffer(write_buffer);
  72. key = "key";
  73. ASSERT_TRUE(db->Get(kPaxosLogColumn, key, value, nullptr).OK());
  74. // sleep(5);
  75. // WARNING: TTL only trigger when compaction.
  76. // see: https://github.com/facebook/rocksdb/wiki/Time-to-Live
  77. // so it does not work in this test case.
  78. // CloseDB();
  79. // OpenDB();
  80. // auto status = db->Get(kPaxosLogColumn, key, value);
  81. // ASSERT_FALSE(status.OK());
  82. // ASSERT_TRUE(status.DocumentNotExist());
  83. }
  84. } // namespace wwsearch