node_index.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include <string.h>
  17. #include <stdio.h>
  18. #include "node_index.h"
  19. #include "algorithm/singleton.h"
  20. #include "node.h"
  21. DTC_USING_NAMESPACE
  22. NodeIndex::NodeIndex() : _firstIndex(NULL)
  23. {
  24. memset(errmsg_, 0, sizeof(errmsg_));
  25. }
  26. NodeIndex::~NodeIndex()
  27. {
  28. }
  29. NodeIndex *NodeIndex::instance()
  30. {
  31. return Singleton<NodeIndex>::instance();
  32. }
  33. void NodeIndex::destroy()
  34. {
  35. Singleton<NodeIndex>::destory();
  36. }
  37. int NodeIndex::pre_allocate_index(size_t mem_size)
  38. {
  39. /*
  40. * 按所有节点全为空节点来分配2级NodeIndex
  41. * 一个空节点占用44 bytes
  42. */
  43. uint32_t n = 65536 * 256 * 44;
  44. n = mem_size / n + 1;
  45. n = n > 256 ? 256 : n;
  46. for (uint32_t i = 0; i < n; ++i) {
  47. _firstIndex->fi_h[i] = M_CALLOC(INDEX_2_SIZE);
  48. if (INVALID_HANDLE == _firstIndex->fi_h[i]) {
  49. log4cplus_error("PANIC: PrepareNodeIndex[%u] failed",
  50. i);
  51. return DTC_CODE_FAILED;
  52. }
  53. }
  54. return DTC_CODE_SUCCESS;
  55. }
  56. int NodeIndex::do_insert(Node node)
  57. {
  58. NODE_ID_T id = node.node_id();
  59. if (INVALID_HANDLE == _firstIndex->fi_h[OFFSET1(id)]) {
  60. _firstIndex->fi_h[OFFSET1(id)] = M_CALLOC(INDEX_2_SIZE);
  61. if (INVALID_HANDLE == _firstIndex->fi_h[OFFSET1(id)]) {
  62. log4cplus_error(
  63. "PANIC: do_insert node=%u to NodeIndex failed",
  64. id);
  65. return DTC_CODE_FAILED;
  66. }
  67. }
  68. SECOND_INDEX_T *p =
  69. M_POINTER(SECOND_INDEX_T, _firstIndex->fi_h[OFFSET1(id)]);
  70. p->si_used++;
  71. p->si_h[OFFSET2(id)] = M_HANDLE(node.Owner());
  72. return DTC_CODE_SUCCESS;
  73. }
  74. Node NodeIndex::do_search(NODE_ID_T id)
  75. {
  76. if (INVALID_NODE_ID == id)
  77. return Node(NULL, 0);
  78. if (INVALID_HANDLE == _firstIndex->fi_h[OFFSET1(id)])
  79. return Node(NULL, 0);
  80. SECOND_INDEX_T *p =
  81. M_POINTER(SECOND_INDEX_T, _firstIndex->fi_h[OFFSET1(id)]);
  82. if (INVALID_HANDLE == p->si_h[OFFSET2(id)])
  83. return Node(NULL, 0);
  84. NODE_SET *NS = M_POINTER(NODE_SET, p->si_h[OFFSET2(id)]);
  85. int index = (id - NS->ng_nid);
  86. if (index < 0 || index > 255)
  87. return Node(NULL, 0);
  88. return Node(NS, index);
  89. }
  90. int NodeIndex::do_init(size_t mem_size)
  91. {
  92. MEM_HANDLE_T v = M_CALLOC(INDEX_1_SIZE);
  93. if (INVALID_HANDLE == v) {
  94. log4cplus_error("Create get_index-1 failed");
  95. return DTC_CODE_FAILED;
  96. }
  97. _firstIndex = M_POINTER(FIRST_INDEX_T, v);
  98. return pre_allocate_index(mem_size);
  99. }
  100. int NodeIndex::do_attach(MEM_HANDLE_T handle)
  101. {
  102. if (INVALID_HANDLE == handle) {
  103. log4cplus_error("attach index-1 failed, memory handle=0");
  104. return DTC_CODE_FAILED;
  105. }
  106. _firstIndex = M_POINTER(FIRST_INDEX_T, handle);
  107. return DTC_CODE_SUCCESS;
  108. }
  109. int NodeIndex::do_detach(void)
  110. {
  111. _firstIndex = 0;
  112. return DTC_CODE_SUCCESS;
  113. }