addorupdate_unit.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 IndexAddOrUpdateTest : 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. IndexAddOrUpdateTest() {
  30. table.business_type = 1;
  31. table.partition_set = 1;
  32. }
  33. static void SetUpTestCase() {
  34. index = new DefaultIndexWrapper();
  35. index->DBParams().path =
  36. std::string("/tmp/unit_") + std::string("addorupdate");
  37. index->Config().SetLogLevel(g_debug ? wwsearch::kSearchLogLevelDebug
  38. : wwsearch::kSearchLogLevelError);
  39. auto status = index->Open(g_use_rocksdb, g_use_compression);
  40. assert(status.GetCode() == 0);
  41. }
  42. static void TearDownTestCase() {
  43. if (index != nullptr) {
  44. index->vdb_->DropDB();
  45. delete index;
  46. index = nullptr;
  47. }
  48. }
  49. virtual void SetUp() override { table.partition_set++; }
  50. virtual void TearDown() override {
  51. for (auto du : documents) {
  52. delete du;
  53. }
  54. documents.clear();
  55. }
  56. uint64_t GetDocumentID() { return document_id++; }
  57. private:
  58. };
  59. DefaultIndexWrapper *IndexAddOrUpdateTest::index = nullptr;
  60. DocumentID IndexAddOrUpdateTest::document_id = 1;
  61. TEST_F(IndexAddOrUpdateTest, AddOneDocument) {
  62. for (int i = 0; i < 10; i++)
  63. documents.push_back(
  64. TestUtil::NewDocument(GetDocumentID(), "a", 999, 999, 999));
  65. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  66. nullptr, nullptr);
  67. EXPECT_TRUE(ret);
  68. for (auto du : documents) {
  69. EXPECT_EQ(0, du->Status().GetCode());
  70. }
  71. }
  72. TEST_F(IndexAddOrUpdateTest, AddTwiceDocument) {
  73. for (int i = 0; i < 10; i++)
  74. documents.push_back(
  75. TestUtil::NewDocument(GetDocumentID(), "a", 999, 999, 999));
  76. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  77. nullptr, nullptr);
  78. EXPECT_TRUE(ret);
  79. for (auto du : documents) {
  80. EXPECT_EQ(0, du->Status().GetCode());
  81. }
  82. ret = index->index_writer_->AddDocuments(table, documents, nullptr, nullptr);
  83. EXPECT_FALSE(ret);
  84. for (auto du : documents) {
  85. EXPECT_EQ(wwsearch::kDocumentExistStatus, du->Status().GetCode());
  86. }
  87. }
  88. //
  89. TEST_F(IndexAddOrUpdateTest, AddOrRemoveFieldThenQuery) {
  90. auto document_update =
  91. TestUtil::NewDocument(GetDocumentID(), "old value", 1, 10, 100);
  92. auto doc_id = document_update->New().ID();
  93. documents.push_back(document_update);
  94. {
  95. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  96. nullptr, nullptr);
  97. EXPECT_TRUE(ret);
  98. for (auto du : documents) {
  99. EXPECT_EQ(0, du->Status().GetCode());
  100. delete du;
  101. }
  102. documents.clear();
  103. }
  104. document_update =
  105. TestUtil::NewDocument(GetDocumentID(), "old value", 1, 10, 100);
  106. document_update->New().SetID(doc_id);
  107. documents.push_back(document_update);
  108. {
  109. // update
  110. document_update->New().FindField(1)->SetString("new value");
  111. document_update->New().FindField(2)->SetUint32(2);
  112. InitStringField(document_update->New().AddField(), 5, "add field");
  113. InitUint32Field(document_update->New().AddField(), 6, 20);
  114. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  115. nullptr, nullptr);
  116. EXPECT_TRUE(ret);
  117. for (auto du : documents) {
  118. EXPECT_EQ(0, du->Status().GetCode());
  119. }
  120. }
  121. // query
  122. wwsearch::Searcher searcher(&index->Config());
  123. std::list<DocumentID> match_documentsid;
  124. //
  125. {
  126. match_documentsid.clear();
  127. wwsearch::BooleanQuery query1(1, "old");
  128. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  129. match_documentsid);
  130. EXPECT_EQ(0, status.GetCode());
  131. EXPECT_EQ(0, match_documentsid.size());
  132. }
  133. {
  134. match_documentsid.clear();
  135. wwsearch::BooleanQuery query1(1, "value");
  136. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  137. match_documentsid);
  138. EXPECT_EQ(0, status.GetCode());
  139. EXPECT_EQ(1, match_documentsid.size());
  140. }
  141. {
  142. match_documentsid.clear();
  143. wwsearch::BooleanQuery query1(1, "new");
  144. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  145. match_documentsid);
  146. EXPECT_EQ(0, status.GetCode());
  147. EXPECT_EQ(1, match_documentsid.size());
  148. }
  149. {
  150. match_documentsid.clear();
  151. uint32_t value = 1;
  152. wwsearch::BooleanQuery query1(2, value);
  153. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  154. match_documentsid);
  155. EXPECT_EQ(0, status.GetCode());
  156. EXPECT_EQ(0, match_documentsid.size());
  157. }
  158. {
  159. match_documentsid.clear();
  160. uint32_t value = 2;
  161. wwsearch::BooleanQuery query1(2, value);
  162. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  163. match_documentsid);
  164. EXPECT_EQ(0, status.GetCode());
  165. EXPECT_EQ(1, match_documentsid.size());
  166. }
  167. {
  168. match_documentsid.clear();
  169. // uint32_t value = 2;
  170. wwsearch::BooleanQuery query1(5, "add");
  171. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  172. match_documentsid);
  173. EXPECT_EQ(0, status.GetCode());
  174. EXPECT_EQ(1, match_documentsid.size());
  175. }
  176. {
  177. match_documentsid.clear();
  178. uint32_t value = 20;
  179. wwsearch::BooleanQuery query1(6, value);
  180. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  181. match_documentsid);
  182. EXPECT_EQ(0, status.GetCode());
  183. EXPECT_EQ(1, match_documentsid.size());
  184. }
  185. }
  186. } // namespace wwsearch