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

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