thash.c 4.4 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@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 <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tmempool.h"

typedef struct _long_hash_t {
  unsigned int         id;
  struct _long_hash_t *prev;
  struct _long_hash_t *next;
  uint64_t             cont;
} SLongHash;

typedef struct {
  SLongHash **longHashList;
  mpool_h     longHashMemPool;
  int (*hashFp)(void *, uint64_t);
  int             maxSessions;
  pthread_mutex_t mutex;
} SHashObj;

uint64_t taosHashUInt64(uint64_t handle) {
  uint64_t hash = handle >> 16;
  hash += handle & 0xFFFF;
  return hash;
}

int taosHashLong(void *handle, uint64_t ip) {
  SHashObj *pObj = (SHashObj *)handle;
  int       hash = 0;

  hash = (int)(ip >> 16);
  hash += (int)(ip & 0xFFFF);

  hash = hash % pObj->maxSessions;

  return hash;
}

int taosAddHash(void *handle, uint64_t cont, unsigned int id) {
  int        hash;
  SLongHash *pNode;
  SHashObj * pObj;

  pObj = (SHashObj *)handle;
  if (pObj == NULL || pObj->maxSessions == 0) return -1;

  pthread_mutex_lock(&pObj->mutex);

  hash = (*pObj->hashFp)(pObj, cont);

  pNode = (SLongHash *)taosMemPoolMalloc(pObj->longHashMemPool);
  pNode->cont = cont;
  pNode->id = id;
  pNode->prev = 0;
  pNode->next = pObj->longHashList[hash];

  if (pObj->longHashList[hash] != 0) (pObj->longHashList[hash])->prev = pNode;
  pObj->longHashList[hash] = pNode;

  pthread_mutex_unlock(&pObj->mutex);

  return 0;
}

void taosDeleteHash(void *handle, uint64_t cont) {
  int        hash;
  SLongHash *pNode;
  SHashObj * pObj;

  pObj = (SHashObj *)handle;
  if (pObj == NULL || pObj->maxSessions == 0) return;

  hash = (*pObj->hashFp)(pObj, cont);

  pthread_mutex_lock(&pObj->mutex);

  pNode = pObj->longHashList[hash];
  while (pNode) {
    if (pNode->cont == cont) break;

    pNode = pNode->next;
  }

  if (pNode) {
    if (pNode->prev) {
      pNode->prev->next = pNode->next;
    } else {
      pObj->longHashList[hash] = pNode->next;
    }

    if (pNode->next) {
      pNode->next->prev = pNode->prev;
    }

    taosMemPoolFree(pObj->longHashMemPool, (char *)pNode);
  }

  pthread_mutex_unlock(&pObj->mutex);
}

int32_t taosGetIdFromHash(void *handle, uint64_t cont) {
  int        hash;
  SLongHash *pNode;
  SHashObj * pObj;

  pObj = (SHashObj *)handle;
  if (pObj == NULL || pObj->maxSessions == 0) return -1;

  hash = (*pObj->hashFp)(pObj, cont);

  pthread_mutex_lock(&pObj->mutex);

  pNode = pObj->longHashList[hash];

  while (pNode) {
    if (pNode->cont == cont) {
      break;
    }

    pNode = pNode->next;
  }

  pthread_mutex_unlock(&pObj->mutex);

  if (pNode) return (int32_t)pNode->id;

  return -1;
}

void *taosOpenHash(int maxSessions, int (*fp)(void *, uint64_t)) {
  SLongHash **longHashList;
  mpool_h     longHashMemPool;
  SHashObj *  pObj;

  longHashMemPool = taosMemPoolInit(maxSessions, sizeof(SLongHash));
  if (longHashMemPool == 0) return NULL;

  longHashList = calloc(sizeof(SLongHash *), (size_t)maxSessions);
  if (longHashList == 0) {
    taosMemPoolCleanUp(longHashMemPool);
    return NULL;
  }

  pObj = malloc(sizeof(SHashObj));
  if (pObj == NULL) {
    taosMemPoolCleanUp(longHashMemPool);
    free(longHashList);
    return NULL;
  }

  pObj->maxSessions = maxSessions;
  pObj->longHashMemPool = longHashMemPool;
  pObj->longHashList = longHashList;
  pObj->hashFp = fp;

  pthread_mutex_init(&pObj->mutex, NULL);

  return pObj;
}

void taosCloseHash(void *handle) {
  SHashObj *pObj;

  pObj = (SHashObj *)handle;
  if (pObj == NULL || pObj->maxSessions == 0) return;

  pthread_mutex_lock(&pObj->mutex);

  if (pObj->longHashMemPool) taosMemPoolCleanUp(pObj->longHashMemPool);

  if (pObj->longHashList) free(pObj->longHashList);

  pthread_mutex_unlock(&pObj->mutex);

  pthread_mutex_destroy(&pObj->mutex);

  memset(pObj, 0, sizeof(SHashObj));
  free(pObj);
}