sdbHash.c 7.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "sdbInt.h"

S
Shengliang Guan 已提交
19 20
static SHashObj *sdbGetHash(SSdb *pSdb, int32_t type) {
  if (type >= SDB_MAX || type <= SDB_START) {
S
Shengliang Guan 已提交
21 22 23 24
    terrno = TSDB_CODE_SDB_INVALID_TABLE_TYPE;
    return NULL;
  }

S
Shengliang Guan 已提交
25
  SHashObj *hash = pSdb->hashObjs[type];
S
Shengliang Guan 已提交
26 27 28 29 30 31 32 33
  if (hash == NULL) {
    terrno = TSDB_CODE_SDB_APP_ERROR;
    return NULL;
  }

  return hash;
}

S
Shengliang Guan 已提交
34
static int32_t sdbGetkeySize(SSdb *pSdb, ESdbType type, void *pKey) {
S
Shengliang Guan 已提交
35
  int32_t  keySize;
S
Shengliang Guan 已提交
36
  EKeyType keyType = pSdb->keyTypes[type];
S
Shengliang Guan 已提交
37 38 39 40 41 42 43 44 45 46 47 48

  if (keyType == SDB_KEY_INT32) {
    keySize = sizeof(int32_t);
  } else if (keyType == SDB_KEY_BINARY) {
    keySize = strlen(pKey) + 1;
  } else {
    keySize = sizeof(int64_t);
  }

  return keySize;
}

S
Shengliang Guan 已提交
49 50 51
static int32_t sdbInsertRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow *pRow, int32_t keySize) {
  int32_t code = 0;

S
Shengliang Guan 已提交
52
  SRWLatch *pLock = &pSdb->locks[pRow->type];
S
Shengliang Guan 已提交
53 54
  taosWLockLatch(pLock);

S
Shengliang Guan 已提交
55 56
  SSdbRow *pOldRow = taosHashGet(hash, pRow->pObj, keySize);
  if (pOldRow != NULL) {
S
Shengliang Guan 已提交
57
    taosWUnLockLatch(pLock);
58
    sdbFreeRow(pRow);
S
Shengliang Guan 已提交
59 60
    terrno = TSDB_CODE_SDB_OBJ_ALREADY_THERE;
    return terrno;
S
Shengliang Guan 已提交
61 62
  }

S
Shengliang Guan 已提交
63
  pRow->refCount = 1;
S
Shengliang Guan 已提交
64 65 66
  pRow->status = pRaw->status;

  if (taosHashPut(hash, pRow->pObj, keySize, &pRow, sizeof(void *)) != 0) {
S
Shengliang Guan 已提交
67
    taosWUnLockLatch(pLock);
68
    sdbFreeRow(pRow);
S
Shengliang Guan 已提交
69 70
    terrno = TSDB_CODE_SDB_OBJ_ALREADY_THERE;
    return terrno;
S
Shengliang Guan 已提交
71 72
  }

S
Shengliang Guan 已提交
73 74
  taosWUnLockLatch(pLock);

S
Shengliang Guan 已提交
75
  SdbInsertFp insertFp = pSdb->insertFps[pRow->type];
S
Shengliang Guan 已提交
76
  if (insertFp != NULL) {
S
Shengliang Guan 已提交
77 78
    code = (*insertFp)(pSdb, pRow->pObj);
    if (code != 0) {
S
Shengliang Guan 已提交
79
      taosWLockLatch(pLock);
S
Shengliang Guan 已提交
80
      taosHashRemove(hash, pRow->pObj, keySize);
S
Shengliang Guan 已提交
81
      taosWUnLockLatch(pLock);
82
      sdbFreeRow(pRow);
S
Shengliang Guan 已提交
83 84
      terrno = code;
      return terrno;
S
Shengliang Guan 已提交
85 86 87 88 89 90
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
91
static int32_t sdbUpdateRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow *pNewRow, int32_t keySize) {
S
Shengliang Guan 已提交
92 93
  int32_t code = 0;

S
Shengliang Guan 已提交
94
  SRWLatch *pLock = &pSdb->locks[pNewRow->type];
S
Shengliang Guan 已提交
95 96
  taosRLockLatch(pLock);

S
Shengliang Guan 已提交
97 98
  SSdbRow **ppOldRow = taosHashGet(hash, pNewRow->pObj, keySize);
  if (ppOldRow == NULL || *ppOldRow == NULL) {
S
Shengliang Guan 已提交
99
    taosRUnLockLatch(pLock);
S
Shengliang Guan 已提交
100
    return sdbInsertRow(pSdb, hash, pRaw, pNewRow, keySize);
S
Shengliang Guan 已提交
101
  }
S
Shengliang Guan 已提交
102
  SSdbRow *pOldRow = *ppOldRow;
S
Shengliang Guan 已提交
103

S
Shengliang Guan 已提交
104
  pOldRow->status = pRaw->status;
S
Shengliang Guan 已提交
105 106
  taosRUnLockLatch(pLock);

S
Shengliang Guan 已提交
107
  SdbUpdateFp updateFp = pSdb->updateFps[pNewRow->type];
S
Shengliang Guan 已提交
108
  if (updateFp != NULL) {
S
Shengliang Guan 已提交
109
    code = (*updateFp)(pSdb, pOldRow->pObj, pNewRow->pObj);
S
Shengliang Guan 已提交
110 111
  }

S
Shengliang Guan 已提交
112
  sdbFreeRow(pNewRow);
S
Shengliang Guan 已提交
113
  return code;
S
Shengliang Guan 已提交
114 115
}

S
Shengliang Guan 已提交
116 117 118
static int32_t sdbDeleteRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow *pRow, int32_t keySize) {
  int32_t code = 0;

S
Shengliang Guan 已提交
119
  SRWLatch *pLock = &pSdb->locks[pRow->type];
S
Shengliang Guan 已提交
120 121
  taosWLockLatch(pLock);

S
Shengliang Guan 已提交
122 123
  SSdbRow **ppOldRow = taosHashGet(hash, pRow->pObj, keySize);
  if (ppOldRow == NULL || *ppOldRow == NULL) {
S
Shengliang Guan 已提交
124
    taosWUnLockLatch(pLock);
125
    sdbFreeRow(pRow);
S
Shengliang Guan 已提交
126 127
    terrno = TSDB_CODE_SDB_OBJ_NOT_THERE;
    return terrno;
S
Shengliang Guan 已提交
128
  }
S
Shengliang Guan 已提交
129
  SSdbRow *pOldRow = *ppOldRow;
S
Shengliang Guan 已提交
130

S
Shengliang Guan 已提交
131 132
  pOldRow->status = pRaw->status;
  taosHashRemove(hash, pOldRow->pObj, keySize);
S
Shengliang Guan 已提交
133 134
  taosWUnLockLatch(pLock);

S
Shengliang Guan 已提交
135
  sdbRelease(pSdb, pOldRow->pObj);
136
  sdbFreeRow(pRow);
S
Shengliang Guan 已提交
137
  return code;
S
Shengliang Guan 已提交
138 139
}

S
Shengliang Guan 已提交
140
int32_t sdbWriteNotFree(SSdb *pSdb, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
141
  SHashObj *hash = sdbGetHash(pSdb, pRaw->type);
S
Shengliang Guan 已提交
142
  if (hash == NULL) return terrno;
S
Shengliang Guan 已提交
143

S
Shengliang Guan 已提交
144
  SdbDecodeFp decodeFp = pSdb->decodeFps[pRaw->type];
S
Shengliang Guan 已提交
145 146
  SSdbRow    *pRow = (*decodeFp)(pRaw);
  if (pRow == NULL) {
S
Shengliang Guan 已提交
147
    return terrno;
S
Shengliang Guan 已提交
148 149
  }

S
Shengliang Guan 已提交
150
  pRow->type = pRaw->type;
S
Shengliang Guan 已提交
151

S
Shengliang Guan 已提交
152
  int32_t keySize = sdbGetkeySize(pSdb, pRow->type, pRow->pObj);
S
Shengliang Guan 已提交
153
  int32_t code = TSDB_CODE_SDB_INVALID_ACTION_TYPE;
S
Shengliang Guan 已提交
154 155 156

  switch (pRaw->status) {
    case SDB_STATUS_CREATING:
S
Shengliang Guan 已提交
157
      code = sdbInsertRow(pSdb, hash, pRaw, pRow, keySize);
S
Shengliang Guan 已提交
158
      break;
S
Shengliang Guan 已提交
159
    case SDB_STATUS_UPDATING:
S
Shengliang Guan 已提交
160
    case SDB_STATUS_READY:
S
Shengliang Guan 已提交
161
    case SDB_STATUS_DROPPING:
S
Shengliang Guan 已提交
162
      code = sdbUpdateRow(pSdb, hash, pRaw, pRow, keySize);
S
Shengliang Guan 已提交
163 164
      break;
    case SDB_STATUS_DROPPED:
S
Shengliang Guan 已提交
165
      code = sdbDeleteRow(pSdb, hash, pRaw, pRow, keySize);
S
Shengliang Guan 已提交
166
      break;
S
Shengliang Guan 已提交
167 168
  }

169
  return code;
S
Shengliang Guan 已提交
170 171
}

S
Shengliang Guan 已提交
172
int32_t sdbWrite(SSdb *pSdb, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
173
  int32_t code = sdbWriteNotFree(pSdb, pRaw);
S
Shengliang Guan 已提交
174 175 176 177
  sdbFreeRaw(pRaw);
  return code;
}

S
Shengliang Guan 已提交
178 179
void *sdbAcquire(SSdb *pSdb, ESdbType type, void *pKey) {
  SHashObj *hash = sdbGetHash(pSdb, type);
S
Shengliang Guan 已提交
180 181 182
  if (hash == NULL) return NULL;

  void   *pRet = NULL;
S
Shengliang Guan 已提交
183
  int32_t keySize = sdbGetkeySize(pSdb, type, pKey);
S
Shengliang Guan 已提交
184

S
Shengliang Guan 已提交
185
  SRWLatch *pLock = &pSdb->locks[type];
S
Shengliang Guan 已提交
186 187 188
  taosRLockLatch(pLock);

  SSdbRow **ppRow = taosHashGet(hash, pKey, keySize);
S
Shengliang Guan 已提交
189
  if (ppRow == NULL || *ppRow == NULL) {
S
Shengliang Guan 已提交
190
    taosRUnLockLatch(pLock);
S
Shengliang Guan 已提交
191
    terrno = TSDB_CODE_SDB_OBJ_NOT_THERE;
S
Shengliang Guan 已提交
192 193 194
    return NULL;
  }

S
Shengliang Guan 已提交
195 196 197 198 199
  SSdbRow *pRow = *ppRow;
  switch (pRow->status) {
    case SDB_STATUS_READY:
      atomic_add_fetch_32(&pRow->refCount, 1);
      pRet = pRow->pObj;
S
Shengliang Guan 已提交
200
      break;
S
Shengliang Guan 已提交
201 202
    case SDB_STATUS_CREATING:
      terrno = TSDB_CODE_SDB_OBJ_CREATING;
S
Shengliang Guan 已提交
203
      break;
S
Shengliang Guan 已提交
204
    case SDB_STATUS_DROPPING:
S
Shengliang Guan 已提交
205
      terrno = TSDB_CODE_SDB_OBJ_DROPPING;
S
Shengliang Guan 已提交
206 207
      break;
    default:
S
Shengliang Guan 已提交
208 209
      terrno = TSDB_CODE_SDB_APP_ERROR;
      break;
S
Shengliang Guan 已提交
210 211
  }

S
Shengliang Guan 已提交
212 213
  taosRUnLockLatch(pLock);
  return pRet;
S
Shengliang Guan 已提交
214 215
}

S
Shengliang Guan 已提交
216
void sdbRelease(SSdb *pSdb, void *pObj) {
S
Shengliang Guan 已提交
217 218
  if (pObj == NULL) return;

S
Shengliang Guan 已提交
219
  SSdbRow *pRow = (SSdbRow *)((char *)pObj - sizeof(SSdbRow));
S
Shengliang Guan 已提交
220
  if (pRow->type >= SDB_MAX || pRow->type <= SDB_START) return;
S
Shengliang Guan 已提交
221

S
Shengliang Guan 已提交
222
  SRWLatch *pLock = &pSdb->locks[pRow->type];
S
Shengliang Guan 已提交
223 224 225 226
  taosRLockLatch(pLock);

  int32_t ref = atomic_sub_fetch_32(&pRow->refCount, 1);
  if (ref <= 0 && pRow->status == SDB_STATUS_DROPPED) {
S
Shengliang Guan 已提交
227 228 229 230 231
    SdbDeleteFp deleteFp = pSdb->deleteFps[pRow->type];
    if (deleteFp != NULL) {
      (*deleteFp)(pSdb, pRow->pObj);
    }

S
Shengliang Guan 已提交
232 233 234 235
    sdbFreeRow(pRow);
  }

  taosRUnLockLatch(pLock);
S
Shengliang Guan 已提交
236 237
}

S
Shengliang Guan 已提交
238 239
void *sdbFetch(SSdb *pSdb, ESdbType type, void *pIter, void **ppObj) {
  SHashObj *hash = sdbGetHash(pSdb, type);
S
Shengliang Guan 已提交
240
  if (hash == NULL) return NULL;
S
Shengliang Guan 已提交
241

S
Shengliang Guan 已提交
242
  SRWLatch *pLock = &pSdb->locks[type];
S
Shengliang Guan 已提交
243
  taosRLockLatch(pLock);
S
Shengliang Guan 已提交
244

S
Shengliang Guan 已提交
245 246 247 248 249 250 251 252 253
  if (pIter != NULL) {
    SSdbRow *pLastRow = *(SSdbRow **)pIter;
    int32_t  ref = atomic_sub_fetch_32(&pLastRow->refCount, 1);
    if (ref <= 0 && pLastRow->status == SDB_STATUS_DROPPED) {
      sdbFreeRow(pLastRow);
    }
  }

  SSdbRow **ppRow = taosHashIterate(hash, pIter);
S
Shengliang Guan 已提交
254 255 256 257 258 259 260 261 262 263 264
  while (ppRow != NULL) {
    SSdbRow *pRow = *ppRow;
    if (pRow == NULL || pRow->status != SDB_STATUS_READY) {
      ppRow = taosHashIterate(hash, ppRow);
      continue;
    }

    atomic_add_fetch_32(&pRow->refCount, 1);
    *ppObj = pRow->pObj;
    break;
  }
S
Shengliang Guan 已提交
265 266
  taosRUnLockLatch(pLock);

S
Shengliang Guan 已提交
267
  return ppRow;
S
Shengliang Guan 已提交
268 269
}

S
Shengliang Guan 已提交
270
void sdbCancelFetch(SSdb *pSdb, void *pIter) {
S
Shengliang Guan 已提交
271 272
  if (pIter == NULL) return;
  SSdbRow  *pRow = *(SSdbRow **)pIter;
S
Shengliang Guan 已提交
273
  SHashObj *hash = sdbGetHash(pSdb, pRow->type);
S
Shengliang Guan 已提交
274
  if (hash == NULL) return;
S
Shengliang Guan 已提交
275

S
Shengliang Guan 已提交
276
  SRWLatch *pLock = &pSdb->locks[pRow->type];
S
Shengliang Guan 已提交
277
  taosRLockLatch(pLock);
S
Shengliang Guan 已提交
278
  taosHashCancelIterate(hash, pIter);
S
Shengliang Guan 已提交
279
  taosRUnLockLatch(pLock);
S
Shengliang Guan 已提交
280 281
}

S
Shengliang Guan 已提交
282 283
int32_t sdbGetSize(SSdb *pSdb, ESdbType type) {
  SHashObj *hash = sdbGetHash(pSdb, type);
S
Shengliang Guan 已提交
284
  if (hash == NULL) return 0;
S
Shengliang Guan 已提交
285

S
Shengliang Guan 已提交
286
  SRWLatch *pLock = &pSdb->locks[type];
S
Shengliang Guan 已提交
287 288 289 290 291
  taosRLockLatch(pLock);
  int32_t size = taosHashGetSize(hash);
  taosRUnLockLatch(pLock);

  return size;
S
Shengliang Guan 已提交
292
}