sync_raft_node_map.c 2.2 KB
Newer Older
L
lichuang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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"

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

23 24 25
void syncRaftClearNodeMap(SSyncRaftNodeMap* nodeMap) {
  taosHashClear(nodeMap->nodeIdMap);
}
L
lichuang 已提交
26

27 28 29 30 31 32
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 已提交
33 34 35
}

void syncRaftCopyNodeMap(const SSyncRaftNodeMap* nodeMap, SSyncRaftNodeMap* to) {
36 37 38 39 40
  SyncNodeId** ppId = (SyncNodeId**)taosHashIterate(nodeMap->nodeIdMap, NULL);
  while (ppId) {
    taosHashPut(to->nodeIdMap, ppId, sizeof(SyncNodeId*), ppId, sizeof(SyncNodeId*));
    ppId = taosHashIterate(nodeMap->nodeIdMap, ppId);
  }
L
lichuang 已提交
41 42
}

43 44 45 46 47
bool syncRaftIterateNodeMap(const SSyncRaftNodeMap* nodeMap, SyncNodeId *pId) {
  SyncNodeId **ppId = taosHashIterate(nodeMap->nodeIdMap, pId);
  if (ppId == NULL) {
    return true;
  }
L
lichuang 已提交
48

49 50 51
  *pId = *(*ppId);
  return false;
}
L
lichuang 已提交
52

53 54
void syncRaftUnionNodeMap(const SSyncRaftNodeMap* nodeMap, SSyncRaftNodeMap* to) {
  syncRaftCopyNodeMap(nodeMap, to);
L
lichuang 已提交
55 56 57
}

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

61 62 63
void syncRaftRemoveFromNodeMap(SSyncRaftNodeMap* nodeMap, SyncNodeId nodeId) {
  taosHashRemove(nodeMap->nodeIdMap, &nodeId, sizeof(SyncNodeId*));
}
L
lichuang 已提交
64

65 66
int32_t syncRaftNodeMapSize(const SSyncRaftNodeMap* nodeMap) {
  return taosHashGetSize(nodeMap);
L
lichuang 已提交
67
}