You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

btree_test.cc 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <sstream>
  17. #include "minddata/dataset/util/btree.h"
  18. #include "minddata/dataset/util/auto_index.h"
  19. #include "minddata/dataset/util/system_pool.h"
  20. #include "minddata/dataset/util/task_manager.h"
  21. #include "common/common.h"
  22. #include "gtest/gtest.h"
  23. #include "utils/log_adapter.h"
  24. using namespace mindspore::dataset;
  25. using mindspore::MsLogLevel::INFO;
  26. using mindspore::ExceptionType::NoExceptionType;
  27. using mindspore::LogStream;
  28. // For testing purposes, we will make the branching factor very low.
  29. struct mytraits {
  30. using slot_type = uint16_t;
  31. static const slot_type kLeafSlots = 6;
  32. static const slot_type kInnerSlots = 3;
  33. };
  34. class MindDataTestBPlusTree : public UT::Common {
  35. public:
  36. MindDataTestBPlusTree() = default;
  37. };
  38. // Test serial insert.
  39. TEST_F(MindDataTestBPlusTree, Test1) {
  40. Allocator<std::string> alloc(std::make_shared<SystemPool>());
  41. BPlusTree<uint64_t, std::string, Allocator<std::string>, std::less<uint64_t>, mytraits> btree(alloc);
  42. Status rc;
  43. for (int i = 0; i < 100; i++) {
  44. uint64_t key = 2 * i;
  45. std::ostringstream oss;
  46. oss << "Hello World. I am " << key;
  47. rc = btree.DoInsert(key, oss.str());
  48. EXPECT_TRUE(rc.IsOk());
  49. }
  50. for (int i = 0; i < 100; i++) {
  51. uint64_t key = 2 * i + 1;
  52. std::ostringstream oss;
  53. oss << "Hello World. I am " << key;
  54. rc = btree.DoInsert(key, oss.str());
  55. EXPECT_TRUE(rc.IsOk());
  56. }
  57. EXPECT_EQ(btree.size(), 200);
  58. // Test iterator
  59. {
  60. int cnt = 0;
  61. auto it = btree.begin();
  62. uint64_t prev = it.key();
  63. ++it;
  64. ++cnt;
  65. while (it != btree.end()) {
  66. uint64_t cur = it.key();
  67. std::string val = "Hello World. I am " + std::to_string(cur);
  68. EXPECT_TRUE(prev < cur);
  69. EXPECT_EQ(it.value(), val);
  70. prev = cur;
  71. ++it;
  72. ++cnt;
  73. }
  74. EXPECT_EQ(cnt, 200);
  75. // Now go backward
  76. for (int i = 0; i < 10; i++) {
  77. --it;
  78. EXPECT_EQ(199 - i, it.key());
  79. }
  80. }
  81. // Test search
  82. {
  83. MS_LOG(INFO) << "Locate key " << 100 << " Expect found.";
  84. auto r = btree.Search(100);
  85. auto &it = r.first;
  86. EXPECT_TRUE(r.second);
  87. EXPECT_EQ(it.key(), 100);
  88. EXPECT_EQ(it.value(), "Hello World. I am 100");
  89. MS_LOG(INFO) << "Locate key " << 300 << " Expect not found.";
  90. auto q = btree.Search(300);
  91. EXPECT_FALSE(q.second);
  92. }
  93. // Test duplicate key
  94. {
  95. rc = btree.DoInsert(100, "Expect error");
  96. EXPECT_EQ(rc, Status(StatusCode::kDuplicateKey));
  97. }
  98. }
  99. // Test concurrent insert.
  100. TEST_F(MindDataTestBPlusTree, Test2) {
  101. Allocator<std::string> alloc(std::make_shared<SystemPool>());
  102. BPlusTree<uint64_t, std::string, Allocator<std::string>, std::less<uint64_t>, mytraits> btree(alloc);
  103. TaskGroup vg;
  104. auto f = [&](int k) -> Status {
  105. TaskManager::FindMe()->Post();
  106. for (int i = 0; i < 100; i++) {
  107. uint64_t key = k * 100 + i;
  108. std::ostringstream oss;
  109. oss << "Hello World. I am " << key;
  110. Status rc = btree.DoInsert(key, oss.str());
  111. EXPECT_TRUE(rc.IsOk());
  112. }
  113. return Status::OK();
  114. };
  115. auto g = [&](int k) -> Status {
  116. TaskManager::FindMe()->Post();
  117. for (int i = 0; i < 1000; i++) {
  118. uint64_t key = rand() % 10000;;
  119. auto it = btree.Search(key);
  120. }
  121. return Status::OK();
  122. };
  123. // Spawn multiple threads to do insert.
  124. for (int k = 0; k < 100; k++) {
  125. vg.CreateAsyncTask("Concurrent Insert", std::bind(f, k));
  126. }
  127. // Spawn a few threads to do random search.
  128. for (int k = 0; k < 2; k++) {
  129. vg.CreateAsyncTask("Concurrent search", std::bind(g, k));
  130. }
  131. vg.join_all();
  132. EXPECT_EQ(btree.size(), 10000);
  133. // Test iterator
  134. {
  135. int cnt = 0;
  136. auto it = btree.begin();
  137. uint64_t prev = it.key();
  138. ++it;
  139. ++cnt;
  140. while (it != btree.end()) {
  141. uint64_t cur = it.key();
  142. std::string val = "Hello World. I am " + std::to_string(cur);
  143. EXPECT_TRUE(prev < cur);
  144. EXPECT_EQ(it.value(), val);
  145. prev = cur;
  146. ++it;
  147. ++cnt;
  148. }
  149. EXPECT_EQ(cnt, 10000);
  150. }
  151. // Test search
  152. {
  153. MS_LOG(INFO) << "Locating key from 0 to 9999. Expect found.";
  154. for (int i = 0; i < 10000; i++) {
  155. auto r = btree.Search(i);
  156. EXPECT_TRUE(r.second);
  157. if (r.second) {
  158. auto &it = r.first;
  159. EXPECT_EQ(it.key(), i);
  160. std::string val = "Hello World. I am " + std::to_string(i);
  161. EXPECT_EQ(it.value(), val);
  162. }
  163. }
  164. MS_LOG(INFO) << "Locate key " << 10000 << ". Expect not found";
  165. auto q = btree.Search(10000);
  166. EXPECT_FALSE(q.second);
  167. }
  168. }
  169. TEST_F(MindDataTestBPlusTree, Test3) {
  170. Allocator<std::string> alloc(std::make_shared<SystemPool>());
  171. AutoIndexObj<std::string, Allocator<std::string>> ai(alloc);
  172. Status rc;
  173. rc = ai.insert("Hello World");
  174. EXPECT_TRUE(rc.IsOk());
  175. rc = ai.insert({"a", "b", "c"});
  176. EXPECT_TRUE(rc.IsOk());
  177. uint64_t min = ai.min_key();
  178. uint64_t max = ai.max_key();
  179. EXPECT_EQ(min, 0);
  180. EXPECT_EQ(max, 3);
  181. auto r = ai.Search(2);
  182. auto &it = r.first;
  183. EXPECT_EQ(it.value(), "b");
  184. MS_LOG(INFO) << "Dump all the values using [] operator.";
  185. for (uint64_t i = min; i <= max; i++) {
  186. MS_LOG(DEBUG) << ai[i] << std::endl;
  187. }
  188. }
  189. TEST_F(MindDataTestBPlusTree, Test4) {
  190. Allocator<int64_t> alloc(std::make_shared<SystemPool>());
  191. AutoIndexObj<int64_t, Allocator<int64_t>> ai(alloc);
  192. Status rc;
  193. for (int i = 0; i < 1000; i++) {
  194. rc = ai.insert(std::make_unique<int64_t>(i));
  195. EXPECT_TRUE(rc.IsOk());
  196. }
  197. // Test iterator
  198. {
  199. int cnt = 0;
  200. auto it = ai.begin();
  201. uint64_t prev = it.key();
  202. ++it;
  203. ++cnt;
  204. while (it != ai.end()) {
  205. uint64_t cur = it.key();
  206. EXPECT_TRUE(prev < cur);
  207. EXPECT_EQ(it.value(), cnt);
  208. prev = cur;
  209. ++it;
  210. ++cnt;
  211. }
  212. EXPECT_EQ(cnt, 1000);
  213. }
  214. }