sync_raft_node_map.c 2.6 KB
Newer Older
L
lichuang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <cli@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "sync_raft_node_map.h"
#include "sync_type.h"
18
#include "sync_raft_progress.h"
L
lichuang 已提交
19

20 21 22
void syncRaftInitNodeMap(SSyncRaftNodeMap* nodeMap) {
  nodeMap->nodeIdMap = taosHashInit(TSDB_MAX_REPLICA, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
}
L
lichuang 已提交
23

24 25 26 27
void syncRaftFreeNodeMap(SSyncRaftNodeMap* nodeMap) {
  taosHashCleanup(nodeMap->nodeIdMap);
}

28 29 30
void syncRaftClearNodeMap(SSyncRaftNodeMap* nodeMap) {
  taosHashClear(nodeMap->nodeIdMap);
}
L
lichuang 已提交
31

32 33 34 35 36 37
bool syncRaftIsInNodeMap(const SSyncRaftNodeMap* nodeMap, SyncNodeId nodeId) {
  SyncNodeId** ppId = (SyncNodeId**)taosHashGet(nodeMap->nodeIdMap, &nodeId, sizeof(SyncNodeId*));
  if (ppId == NULL) {
    return false;
  }
  return true;
L
lichuang 已提交
38 39
}

40 41 42 43
void syncRaftCopyNodeMap(SSyncRaftNodeMap* from, SSyncRaftNodeMap* to) {
  SyncNodeId *pId = NULL;
  while (!syncRaftIterateNodeMap(from, pId)) {
    taosHashPut(to->nodeIdMap, &pId, sizeof(SyncNodeId*), &pId, sizeof(SyncNodeId*));
44
  }
L
lichuang 已提交
45 46
}

47 48 49 50 51
bool syncRaftIterateNodeMap(const SSyncRaftNodeMap* nodeMap, SyncNodeId *pId) {
  SyncNodeId **ppId = taosHashIterate(nodeMap->nodeIdMap, pId);
  if (ppId == NULL) {
    return true;
  }
L
lichuang 已提交
52

53 54 55
  *pId = *(*ppId);
  return false;
}
L
lichuang 已提交
56

57
bool syncRaftIsAllNodeInProgressMap(SSyncRaftNodeMap* nodeMap, SSyncRaftProgressMap* progressMap) {
58 59 60 61 62 63 64 65 66 67
  SyncNodeId *pId = NULL;
  while (!syncRaftIterateNodeMap(nodeMap, pId)) {
    if (!syncRaftIsInProgressMap(progressMap, *pId)) {
      return false;
    }
  }

  return true;
}

68
void syncRaftUnionNodeMap(SSyncRaftNodeMap* nodeMap, SSyncRaftNodeMap* to) {
69
  syncRaftCopyNodeMap(nodeMap, to);
L
lichuang 已提交
70 71 72
}

void syncRaftAddToNodeMap(SSyncRaftNodeMap* nodeMap, SyncNodeId nodeId) {
73 74
  taosHashPut(nodeMap->nodeIdMap, &nodeId, sizeof(SyncNodeId*), &nodeId, sizeof(SyncNodeId*));
}
L
lichuang 已提交
75

76 77 78
void syncRaftRemoveFromNodeMap(SSyncRaftNodeMap* nodeMap, SyncNodeId nodeId) {
  taosHashRemove(nodeMap->nodeIdMap, &nodeId, sizeof(SyncNodeId*));
}
L
lichuang 已提交
79

80
int32_t syncRaftNodeMapSize(const SSyncRaftNodeMap* nodeMap) {
81
  return taosHashGetSize(nodeMap->nodeIdMap);
L
lichuang 已提交
82
}