db_unit.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/document.h"
  20. #include "include/document_writer.h"
  21. #include "include/index_wrapper.h"
  22. #include "include/search_status.h"
  23. #include "include/search_util.h"
  24. #include "unittest_util.h"
  25. extern bool g_debug;
  26. extern bool g_use_rocksdb;
  27. extern bool g_use_compression;
  28. namespace wwsearch {
  29. class DbTest : public ::testing::Test {
  30. public:
  31. static DefaultIndexWrapper *index_;
  32. static uint64_t document_id_;
  33. static uint64_t numeric_value_;
  34. wwsearch::TableID table_;
  35. std::vector<DocumentUpdater *> documents_;
  36. std::list<DocumentID> match_documentsid_;
  37. public:
  38. DbTest() {
  39. table_.business_type = 1;
  40. table_.partition_set = 1;
  41. }
  42. static void SetUpTestCase() {
  43. index_ = new DefaultIndexWrapper();
  44. index_->DBParams().path = std::string("/tmp/unit_") + std::string("db");
  45. index_->Config().SetLogLevel(g_debug ? wwsearch::kSearchLogLevelDebug
  46. : wwsearch::kSearchLogLevelError);
  47. auto status = index_->Open();
  48. ASSERT_TRUE(status.GetCode() == 0);
  49. }
  50. static void TearDownTestCase() {
  51. if (index_ != nullptr) {
  52. index_->vdb_->DropDB();
  53. delete index_;
  54. index_ = nullptr;
  55. }
  56. }
  57. virtual void SetUp() override {
  58. table_.partition_set++;
  59. match_documentsid_.clear();
  60. }
  61. virtual void TearDown() override {
  62. for (auto du : documents_) {
  63. delete du;
  64. }
  65. documents_.clear();
  66. match_documentsid_.clear();
  67. }
  68. uint64_t GetDocumentID() { return document_id_++; }
  69. uint64_t GetNumeric(uint64_t alloc_len = 1000) {
  70. auto temp = numeric_value_;
  71. numeric_value_ += alloc_len;
  72. return temp;
  73. }
  74. private:
  75. };
  76. DefaultIndexWrapper *DbTest::index_ = nullptr;
  77. DocumentID DbTest::document_id_ = 1;
  78. DocumentID DbTest::numeric_value_ = 1;
  79. static std::string key{"hello"};
  80. static std::string key1{"hello1"};
  81. static std::string end_key{"hellp"};
  82. static std::string value{"world"};
  83. static std::string value1{"world1"};
  84. TEST_F(DbTest, WriteAndReadCfKv) {
  85. {
  86. // write kv
  87. WriteBuffer *write_buffer = index_->vdb_->NewWriteBuffer(nullptr);
  88. write_buffer->Put(kMetaColumn, key, value);
  89. index_->vdb_->FlushBuffer(write_buffer);
  90. index_->vdb_->ReleaseWriteBuffer(write_buffer);
  91. }
  92. {
  93. // read kv
  94. VirtualDBSnapshot *snapshot = index_->vdb_->NewSnapshot();
  95. std::string read_value;
  96. SearchStatus s = index_->vdb_->Get(kMetaColumn, key, read_value, snapshot);
  97. EXPECT_TRUE(s.OK());
  98. EXPECT_TRUE(value.compare(read_value) == 0);
  99. index_->vdb_->ReleaseSnapshot(snapshot);
  100. }
  101. }
  102. TEST_F(DbTest, WriteAndDeleteCfKv) {
  103. {
  104. // write kv
  105. WriteBuffer *write_buffer = index_->vdb_->NewWriteBuffer(nullptr);
  106. write_buffer->Put(kMetaColumn, key, value);
  107. index_->vdb_->FlushBuffer(write_buffer);
  108. index_->vdb_->ReleaseWriteBuffer(write_buffer);
  109. }
  110. {
  111. // write kv
  112. WriteBuffer *write_buffer = index_->vdb_->NewWriteBuffer(nullptr);
  113. write_buffer->Put(kMetaColumn, key1, value1);
  114. index_->vdb_->FlushBuffer(write_buffer);
  115. index_->vdb_->ReleaseWriteBuffer(write_buffer);
  116. }
  117. {
  118. // read kv
  119. VirtualDBSnapshot *snapshot = index_->vdb_->NewSnapshot();
  120. std::string read_value;
  121. SearchStatus s = index_->vdb_->Get(kMetaColumn, key, read_value, snapshot);
  122. EXPECT_TRUE(s.OK());
  123. EXPECT_TRUE(value.compare(read_value) == 0);
  124. index_->vdb_->ReleaseSnapshot(snapshot);
  125. }
  126. {
  127. // delete kv
  128. WriteBuffer *write_buffer = index_->vdb_->NewWriteBuffer(nullptr);
  129. write_buffer->Delete(kMetaColumn, key);
  130. index_->vdb_->FlushBuffer(write_buffer);
  131. index_->vdb_->ReleaseWriteBuffer(write_buffer);
  132. }
  133. {
  134. // read kv
  135. VirtualDBSnapshot *snapshot = index_->vdb_->NewSnapshot();
  136. std::string read_value;
  137. SearchStatus s = index_->vdb_->Get(kMetaColumn, key, read_value, snapshot);
  138. EXPECT_TRUE(s.DocumentNotExist());
  139. index_->vdb_->ReleaseSnapshot(snapshot);
  140. }
  141. {
  142. // read kv
  143. VirtualDBSnapshot *snapshot = index_->vdb_->NewSnapshot();
  144. std::string read_value;
  145. SearchStatus s = index_->vdb_->Get(kMetaColumn, key1, read_value, snapshot);
  146. EXPECT_TRUE(s.OK());
  147. EXPECT_TRUE(value1.compare(read_value) == 0);
  148. index_->vdb_->ReleaseSnapshot(snapshot);
  149. }
  150. }
  151. TEST_F(DbTest, WriteAndDeleteRangeCfKv) {
  152. {
  153. // write kv
  154. WriteBuffer *write_buffer = index_->vdb_->NewWriteBuffer(nullptr);
  155. write_buffer->Put(kMetaColumn, key, value);
  156. index_->vdb_->FlushBuffer(write_buffer);
  157. index_->vdb_->ReleaseWriteBuffer(write_buffer);
  158. }
  159. {
  160. // write kv
  161. WriteBuffer *write_buffer = index_->vdb_->NewWriteBuffer(nullptr);
  162. write_buffer->Put(kMetaColumn, key1, value1);
  163. index_->vdb_->FlushBuffer(write_buffer);
  164. index_->vdb_->ReleaseWriteBuffer(write_buffer);
  165. }
  166. {
  167. // read kv
  168. VirtualDBSnapshot *snapshot = index_->vdb_->NewSnapshot();
  169. std::string read_value;
  170. SearchStatus s = index_->vdb_->Get(kMetaColumn, key, read_value, snapshot);
  171. EXPECT_TRUE(s.OK());
  172. EXPECT_TRUE(value.compare(read_value) == 0);
  173. index_->vdb_->ReleaseSnapshot(snapshot);
  174. }
  175. {
  176. // multi - read kv
  177. VirtualDBSnapshot *snapshot = index_->vdb_->NewSnapshot();
  178. std::vector<std::string> keys{key, key1};
  179. std::vector<std::string> read_values;
  180. std::vector<SearchStatus> ss;
  181. index_->vdb_->MultiGet(
  182. std::vector<StorageColumnType>{kMetaColumn, kMetaColumn}, keys,
  183. read_values, ss, snapshot);
  184. EXPECT_TRUE(read_values.size() == ss.size());
  185. for (auto &s : ss) {
  186. EXPECT_TRUE(s.OK());
  187. }
  188. EXPECT_TRUE(value.compare(read_values[0]) == 0);
  189. EXPECT_TRUE(value1.compare(read_values[1]) == 0);
  190. index_->vdb_->ReleaseSnapshot(snapshot);
  191. }
  192. {
  193. // delete kv
  194. WriteBuffer *write_buffer = index_->vdb_->NewWriteBuffer(nullptr);
  195. write_buffer->DeleteRange(kMetaColumn, key, end_key);
  196. index_->vdb_->FlushBuffer(write_buffer);
  197. index_->vdb_->ReleaseWriteBuffer(write_buffer);
  198. }
  199. {
  200. // read kv
  201. VirtualDBSnapshot *snapshot = index_->vdb_->NewSnapshot();
  202. std::string read_value;
  203. SearchStatus s = index_->vdb_->Get(kMetaColumn, key, read_value, snapshot);
  204. EXPECT_TRUE(s.DocumentNotExist());
  205. index_->vdb_->ReleaseSnapshot(snapshot);
  206. }
  207. {
  208. // read kv
  209. VirtualDBSnapshot *snapshot = index_->vdb_->NewSnapshot();
  210. std::string read_value;
  211. SearchStatus s = index_->vdb_->Get(kMetaColumn, key1, read_value, snapshot);
  212. EXPECT_TRUE(s.DocumentNotExist());
  213. index_->vdb_->ReleaseSnapshot(snapshot);
  214. }
  215. }
  216. } // namespace wwsearch