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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "dataset/util/btree.h"
  18. #include "dataset/util/auto_index.h"
  19. #include "dataset/util/system_pool.h"
  20. #include "dataset/util/task_manager.h"
  21. #include "common/common.h"
  22. #include "gtest/gtest.h"
  23. #include "dataset/util/de_error.h"
  24. #include "utils/log_adapter.h"
  25. using namespace mindspore::dataset;
  26. using mindspore::MsLogLevel::INFO;
  27. using mindspore::ExceptionType::NoExceptionType;
  28. using mindspore::LogStream;
  29. // For testing purposes, we will make the branching factor very low.
  30. struct mytraits {
  31. using slot_type = uint16_t;
  32. static const slot_type kLeafSlots = 6;
  33. static const slot_type kInnerSlots = 3;
  34. };
  35. class MindDataTestBPlusTree : public UT::Common {
  36. public:
  37. MindDataTestBPlusTree() = default;
  38. };
  39. // Test serial insert.
  40. TEST_F(MindDataTestBPlusTree, Test1) {
  41. Allocator<std::string> alloc(std::make_shared<SystemPool>());
  42. BPlusTree<uint64_t, std::string, Allocator<std::string>, std::less<uint64_t>, mytraits> btree(alloc);
  43. Status rc;
  44. for (int i = 0; i < 100; i++) {
  45. uint64_t key = 2 * i;
  46. std::ostringstream oss;
  47. oss << "Hello World. I am " << key;
  48. rc = btree.DoInsert(key, oss.str());
  49. EXPECT_TRUE(rc.IsOk());
  50. }
  51. for (int i = 0; i < 100; i++) {
  52. uint64_t key = 2 * i + 1;
  53. std::ostringstream oss;
  54. oss << "Hello World. I am " << key;
  55. rc = btree.DoInsert(key, oss.str());
  56. EXPECT_TRUE(rc.IsOk());
  57. }
  58. EXPECT_EQ(btree.size(), 200);
  59. // Test iterator
  60. {
  61. int cnt = 0;
  62. auto it = btree.begin();
  63. uint64_t prev = it.key();
  64. ++it;
  65. ++cnt;
  66. while (it != btree.end()) {
  67. uint64_t cur = it.key();
  68. std::string val = "Hello World. I am " + std::to_string(cur);
  69. EXPECT_TRUE(prev < cur);
  70. EXPECT_EQ(it.value(), val);
  71. prev = cur;
  72. ++it;
  73. ++cnt;
  74. }
  75. EXPECT_EQ(cnt, 200);
  76. // Now go backward
  77. for (int i = 0; i < 10; i++) {
  78. --it;
  79. EXPECT_EQ(199 - i, it.key());
  80. }
  81. }
  82. // Test search
  83. {
  84. MS_LOG(INFO) << "Locate key " << 100 << " Expect found.";
  85. auto r = btree.Search(100);
  86. auto &it = r.first;
  87. EXPECT_TRUE(r.second);
  88. EXPECT_EQ(it.key(), 100);
  89. EXPECT_EQ(it.value(), "Hello World. I am 100");
  90. MS_LOG(INFO) << "Locate key " << 300 << " Expect not found.";
  91. auto q = btree.Search(300);
  92. EXPECT_FALSE(q.second);
  93. }
  94. // Test duplicate key
  95. {
  96. rc = btree.DoInsert(100, "Expect error");
  97. EXPECT_EQ(rc, Status(StatusCode::kDuplicateKey));
  98. }
  99. }
  100. // Test concurrent insert.
  101. TEST_F(MindDataTestBPlusTree, Test2) {
  102. Allocator<std::string> alloc(std::make_shared<SystemPool>());
  103. BPlusTree<uint64_t, std::string, Allocator<std::string>, std::less<uint64_t>, mytraits> btree(alloc);
  104. TaskGroup vg;
  105. auto f = [&](int k) -> Status {
  106. TaskManager::FindMe()->Post();
  107. for (int i = 0; i < 100; i++) {
  108. uint64_t key = k * 100 + i;
  109. std::ostringstream oss;
  110. oss << "Hello World. I am " << key;
  111. Status rc = btree.DoInsert(key, oss.str());
  112. EXPECT_TRUE(rc.IsOk());
  113. }
  114. return Status::OK();
  115. };
  116. auto g = [&](int k) -> Status {
  117. TaskManager::FindMe()->Post();
  118. for (int i = 0; i < 1000; i++) {
  119. uint64_t key = rand() % 10000;;
  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, 1);
  181. EXPECT_EQ(max, 4);
  182. auto r = ai.Search(3);
  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. }