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.

auto_index.h 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_AUTO_INDEX_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_AUTO_INDEX_H_
  18. #include <atomic>
  19. #include <memory>
  20. #include <utility>
  21. #include <vector>
  22. #include "minddata/dataset/util/btree.h"
  23. #include "minddata/dataset/util/system_pool.h"
  24. namespace mindspore {
  25. namespace dataset {
  26. /// This is a B+ tree with generated int64_t value as key.
  27. /// Use minKey() function to query the min key.
  28. /// Use maxKey() function to query the max key.
  29. /// @tparam T
  30. template <typename T, typename A = std::allocator<T>>
  31. class AutoIndexObj : public BPlusTree<int64_t, T, A> {
  32. public:
  33. using my_tree = BPlusTree<int64_t, T, A>;
  34. using key_type = typename my_tree::key_type;
  35. using value_type = typename my_tree::value_type;
  36. AutoIndexObj() : my_tree::BPlusTree(), inx_(kMinKey) {}
  37. explicit AutoIndexObj(const Allocator<T> &alloc) : my_tree::BPlusTree(alloc), inx_(kMinKey) {}
  38. ~AutoIndexObj() = default;
  39. // Insert an object into the tree.
  40. // @param val
  41. // @return
  42. Status insert(const value_type &val, key_type *key = nullptr) {
  43. key_type my_inx = inx_.fetch_add(1);
  44. if (key != nullptr) {
  45. *key = my_inx;
  46. }
  47. return my_tree::DoInsert(my_inx, val);
  48. }
  49. Status insert(std::unique_ptr<value_type> &&val, key_type *key = nullptr) {
  50. key_type my_inx = inx_.fetch_add(1);
  51. if (key) {
  52. *key = my_inx;
  53. }
  54. return my_tree::DoInsert(my_inx, std::move(val));
  55. }
  56. // Insert a vector of objects into the tree.
  57. // @param v
  58. // @return
  59. Status insert(std::vector<value_type> v) {
  60. uint64_t num_ele = v.size();
  61. if (num_ele > 0) {
  62. // reserve a range of keys rather than getting it one by one.
  63. key_type my_inx = inx_.fetch_add(num_ele);
  64. for (uint64_t i = 0; i < num_ele; i++) {
  65. RETURN_IF_NOT_OK(my_tree::DoInsert(my_inx + i, v.at(i)));
  66. }
  67. }
  68. return Status::OK();
  69. }
  70. // @return the minimum key
  71. key_type min_key() const {
  72. auto it = this->cbegin();
  73. return it.key();
  74. }
  75. // @return the maximum key
  76. key_type max_key() const {
  77. auto it = this->cend();
  78. --it;
  79. return it.key();
  80. }
  81. private:
  82. static constexpr key_type kMinKey = 0;
  83. std::atomic<key_type> inx_;
  84. };
  85. } // namespace dataset
  86. } // namespace mindspore
  87. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_AUTO_INDEX_H_