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 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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::LogStream;
  26. using mindspore::ExceptionType::NoExceptionType;
  27. using mindspore::MsLogLevel::INFO;
  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<>, 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::kMDDuplicateKey));
  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<>, 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. ;
  120. auto it = btree.Search(key);
  121. }
  122. return Status::OK();
  123. };
  124. // Spawn multiple threads to do insert.
  125. for (int k = 0; k < 100; k++) {
  126. vg.CreateAsyncTask("Concurrent Insert", std::bind(f, k));
  127. }
  128. // Spawn a few threads to do random search.
  129. for (int k = 0; k < 2; k++) {
  130. vg.CreateAsyncTask("Concurrent search", std::bind(g, k));
  131. }
  132. vg.join_all();
  133. EXPECT_EQ(btree.size(), 10000);
  134. // Test iterator
  135. {
  136. int cnt = 0;
  137. auto it = btree.begin();
  138. uint64_t prev = it.key();
  139. ++it;
  140. ++cnt;
  141. while (it != btree.end()) {
  142. uint64_t cur = it.key();
  143. std::string val = "Hello World. I am " + std::to_string(cur);
  144. EXPECT_TRUE(prev < cur);
  145. EXPECT_EQ(it.value(), val);
  146. prev = cur;
  147. ++it;
  148. ++cnt;
  149. }
  150. EXPECT_EQ(cnt, 10000);
  151. }
  152. // Test search
  153. {
  154. MS_LOG(INFO) << "Locating key from 0 to 9999. Expect found.";
  155. for (int i = 0; i < 10000; i++) {
  156. auto r = btree.Search(i);
  157. EXPECT_TRUE(r.second);
  158. if (r.second) {
  159. auto &it = r.first;
  160. EXPECT_EQ(it.key(), i);
  161. std::string val = "Hello World. I am " + std::to_string(i);
  162. EXPECT_EQ(it.value(), val);
  163. }
  164. }
  165. MS_LOG(INFO) << "Locate key " << 10000 << ". Expect not found";
  166. auto q = btree.Search(10000);
  167. EXPECT_FALSE(q.second);
  168. }
  169. }
  170. TEST_F(MindDataTestBPlusTree, Test3) {
  171. Allocator<std::string> alloc(std::make_shared<SystemPool>());
  172. AutoIndexObj<std::string, Allocator<std::string>> ai(alloc);
  173. Status rc;
  174. rc = ai.insert("Hello World");
  175. EXPECT_TRUE(rc.IsOk());
  176. rc = ai.insert({"a", "b", "c"});
  177. EXPECT_TRUE(rc.IsOk());
  178. uint64_t min = ai.min_key();
  179. uint64_t max = ai.max_key();
  180. EXPECT_EQ(min, 0);
  181. EXPECT_EQ(max, 3);
  182. auto r = ai.Search(2);
  183. auto &it = r.first;
  184. EXPECT_EQ(it.value(), "b");
  185. MS_LOG(INFO) << "Dump all the values using [] operator.";
  186. for (uint64_t i = min; i <= max; i++) {
  187. MS_LOG(DEBUG) << ai[i] << std::endl;
  188. }
  189. }
  190. TEST_F(MindDataTestBPlusTree, Test4) {
  191. Allocator<int64_t> alloc(std::make_shared<SystemPool>());
  192. AutoIndexObj<int64_t, Allocator<int64_t>> ai(alloc);
  193. Status rc;
  194. for (int i = 0; i < 1000; i++) {
  195. rc = ai.insert(std::make_unique<int64_t>(i));
  196. EXPECT_TRUE(rc.IsOk());
  197. }
  198. // Test iterator
  199. {
  200. int cnt = 0;
  201. auto it = ai.begin();
  202. uint64_t prev = it.key();
  203. ++it;
  204. ++cnt;
  205. while (it != ai.end()) {
  206. uint64_t cur = it.key();
  207. EXPECT_TRUE(prev < cur);
  208. EXPECT_EQ(it.value(), cnt);
  209. prev = cur;
  210. ++it;
  211. ++cnt;
  212. }
  213. EXPECT_EQ(cnt, 1000);
  214. }
  215. }
  216. TEST_F(MindDataTestBPlusTree, TestPerfNoLocking) {
  217. AutoIndexObj<int64_t> btree;
  218. // No locking test
  219. btree.SetLocking(false);
  220. // Insert a million entries using the default traits.
  221. for (auto i = 0; i < 1000000; ++i) {
  222. ASSERT_TRUE(btree.insert(i));
  223. }
  224. std::cout << "Tree height : " << btree.GetHeight() << std::endl;
  225. std::cout << "Tree Order : " << btree.GetOrder() << std::endl;
  226. std::cout << "Number of leaves : " << btree.GetNumLeaves() << std::endl;
  227. std::cout << "Number of inner nodes : " << btree.GetNumInnerNodes() << std::endl;
  228. auto r = btree.Search(3);
  229. EXPECT_TRUE(r.second);
  230. r = btree.Search(999999);
  231. EXPECT_TRUE(r.second);
  232. }