1
0

node_index.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  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 __DTC_NODE_INDEX_H
  17. #define __DTC_NODE_INDEX_H
  18. #include "namespace.h"
  19. #include "global.h"
  20. DTC_BEGIN_NAMESPACE
  21. #define INDEX_1_SIZE \
  22. (((1UL << 8) * sizeof(MEM_HANDLE_T)) + \
  23. sizeof(FIRST_INDEX_T)) // first-index size
  24. #define INDEX_2_SIZE \
  25. (((1UL << 16) * sizeof(MEM_HANDLE_T)) + \
  26. sizeof(SECOND_INDEX_T)) // second-index size
  27. #define OFFSET1(id) ((id) >> 24) //高8位,一级index
  28. #define OFFSET2(id) (((id)&0xFFFF00) >> 8) //中间16位,二级index
  29. #define OFFSET3(id) ((id)&0xFF) //低8位
  30. struct first_index {
  31. uint32_t fi_used; //一级index使用个数
  32. MEM_HANDLE_T fi_h[0]; //存放二级index的handle
  33. };
  34. typedef struct first_index FIRST_INDEX_T;
  35. struct second_index {
  36. uint32_t si_used;
  37. MEM_HANDLE_T si_h[0];
  38. };
  39. typedef struct second_index SECOND_INDEX_T;
  40. class Node;
  41. class NodeIndex {
  42. public:
  43. NodeIndex();
  44. ~NodeIndex();
  45. static NodeIndex *instance();
  46. static void destroy();
  47. int do_insert(Node);
  48. Node do_search(NODE_ID_T id);
  49. int pre_allocate_index(size_t size);
  50. const MEM_HANDLE_T get_handle() const
  51. {
  52. return M_HANDLE(_firstIndex);
  53. }
  54. const char *error() const
  55. {
  56. return errmsg_;
  57. }
  58. ///* 内存区块操作函数 */
  59. int do_init(size_t mem_size);
  60. int do_attach(MEM_HANDLE_T handle);
  61. int do_detach(void);
  62. private:
  63. FIRST_INDEX_T *_firstIndex;
  64. char errmsg_[256];
  65. };
  66. DTC_END_NAMESPACE
  67. #endif