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

S
Shengliang Guan 已提交
19
static void sdbCheckRow(SSdb *pSdb, SSdbRow *pRow);
20

S
Shengliang Guan 已提交
21
const char *sdbTableName(ESdbType type) {
S
Shengliang Guan 已提交
22 23 24 25 26 27 28
  switch (type) {
    case SDB_TRANS:
      return "trans";
    case SDB_CLUSTER:
      return "cluster";
    case SDB_MNODE:
      return "mnode";
S
Shengliang Guan 已提交
29 30 31 32 33 34
    case SDB_QNODE:
      return "qnode";
    case SDB_SNODE:
      return "snode";
    case SDB_BNODE:
      return "bnode";
S
Shengliang Guan 已提交
35 36 37 38 39 40 41 42
    case SDB_DNODE:
      return "dnode";
    case SDB_USER:
      return "user";
    case SDB_AUTH:
      return "auth";
    case SDB_ACCT:
      return "acct";
L
add log  
Liu Jicong 已提交
43 44 45 46
    case SDB_STREAM:
      return "stream";
    case SDB_OFFSET:
      return "offset";
L
Liu Jicong 已提交
47 48
    case SDB_SUBSCRIBE:
      return "subscribe";
S
Shengliang Guan 已提交
49 50
    case SDB_CONSUMER:
      return "consumer";
S
Shengliang Guan 已提交
51 52 53
    case SDB_TOPIC:
      return "topic";
    case SDB_VGROUP:
54 55 56
      return "vgroup";
    case SDB_SMA:
      return "sma";
S
Shengliang Guan 已提交
57 58 59 60 61 62 63 64 65 66 67
    case SDB_STB:
      return "stb";
    case SDB_DB:
      return "db";
    case SDB_FUNC:
      return "func";
    default:
      return "undefine";
  }
}

S
Shengliang Guan 已提交
68
const char *sdbStatusName(ESdbStatus status) {
S
Shengliang Guan 已提交
69 70 71 72 73 74 75 76 77
  switch (status) {
    case SDB_STATUS_CREATING:
      return "creating";
    case SDB_STATUS_DROPPING:
      return "dropping";
    case SDB_STATUS_READY:
      return "ready";
    case SDB_STATUS_DROPPED:
      return "dropped";
78 79
    case SDB_STATUS_INIT:
      return "init";
S
Shengliang Guan 已提交
80 81 82 83 84
    default:
      return "undefine";
  }
}

S
Shengliang Guan 已提交
85
void sdbPrintOper(SSdb *pSdb, SSdbRow *pRow, const char *oper) {
86
#if 0
S
Shengliang Guan 已提交
87 88 89
  EKeyType keyType = pSdb->keyTypes[pRow->type];

  if (keyType == SDB_KEY_BINARY) {
90
    mTrace("%s:%s, ref:%d oper:%s row:%p status:%s", sdbTableName(pRow->type), (char *)pRow->pObj, pRow->refCount, oper,
S
Shengliang Guan 已提交
91
           pRow->pObj, sdbStatusName(pRow->status));
S
Shengliang Guan 已提交
92
  } else if (keyType == SDB_KEY_INT32) {
93
    mTrace("%s:%d, ref:%d oper:%s row:%p status:%s", sdbTableName(pRow->type), *(int32_t *)pRow->pObj, pRow->refCount,
S
Shengliang Guan 已提交
94
           oper, pRow->pObj, sdbStatusName(pRow->status));
S
Shengliang Guan 已提交
95
  } else if (keyType == SDB_KEY_INT64) {
96
    mTrace("%s:%" PRId64 ", ref:%d oper:%s row:%p status:%s", sdbTableName(pRow->type), *(int64_t *)pRow->pObj,
S
Shengliang Guan 已提交
97
           pRow->refCount, oper, pRow->pObj, sdbStatusName(pRow->status));
S
Shengliang Guan 已提交
98 99
  } else {
  }
100
#endif
S
Shengliang Guan 已提交
101 102
}

S
Shengliang Guan 已提交
103
static SHashObj *sdbGetHash(SSdb *pSdb, int32_t type) {
S
Shengliang Guan 已提交
104
  if (type >= SDB_MAX || type < 0) {
S
Shengliang Guan 已提交
105 106 107 108
    terrno = TSDB_CODE_SDB_INVALID_TABLE_TYPE;
    return NULL;
  }

S
Shengliang Guan 已提交
109
  SHashObj *hash = pSdb->hashObjs[type];
S
Shengliang Guan 已提交
110 111 112 113 114 115 116 117
  if (hash == NULL) {
    terrno = TSDB_CODE_SDB_APP_ERROR;
    return NULL;
  }

  return hash;
}

L
Liu Jicong 已提交
118
static int32_t sdbGetkeySize(SSdb *pSdb, ESdbType type, const void *pKey) {
S
Shengliang Guan 已提交
119
  int32_t  keySize = 0;
S
Shengliang Guan 已提交
120
  EKeyType keyType = pSdb->keyTypes[type];
S
Shengliang Guan 已提交
121 122 123 124 125 126 127 128 129 130 131 132

  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 已提交
133
static int32_t sdbInsertRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow *pRow, int32_t keySize) {
134 135
  TdThreadRwlock *pLock = &pSdb->locks[pRow->type];
  taosThreadRwlockWrlock(pLock);
S
Shengliang Guan 已提交
136

S
Shengliang Guan 已提交
137 138
  SSdbRow *pOldRow = taosHashGet(hash, pRow->pObj, keySize);
  if (pOldRow != NULL) {
139
    taosThreadRwlockUnlock(pLock);
140
    sdbFreeRow(pSdb, pRow, false);
S
Shengliang Guan 已提交
141 142
    terrno = TSDB_CODE_SDB_OBJ_ALREADY_THERE;
    return terrno;
S
Shengliang Guan 已提交
143 144
  }

S
Shengliang Guan 已提交
145
  pRow->refCount = 0;
S
Shengliang Guan 已提交
146
  pRow->status = pRaw->status;
147
  sdbPrintOper(pSdb, pRow, "insert");
S
Shengliang Guan 已提交
148 149

  if (taosHashPut(hash, pRow->pObj, keySize, &pRow, sizeof(void *)) != 0) {
150
    taosThreadRwlockUnlock(pLock);
151
    sdbFreeRow(pSdb, pRow, false);
S
Shengliang Guan 已提交
152
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
153
    return terrno;
S
Shengliang Guan 已提交
154 155
  }

156
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
157

S
Shengliang Guan 已提交
158
  int32_t     code = 0;
S
Shengliang Guan 已提交
159
  SdbInsertFp insertFp = pSdb->insertFps[pRow->type];
S
Shengliang Guan 已提交
160
  if (insertFp != NULL) {
S
Shengliang Guan 已提交
161 162
    code = (*insertFp)(pSdb, pRow->pObj);
    if (code != 0) {
S
Shengliang Guan 已提交
163
      code = terrno;
164
      taosThreadRwlockWrlock(pLock);
S
Shengliang Guan 已提交
165
      taosHashRemove(hash, pRow->pObj, keySize);
166
      taosThreadRwlockUnlock(pLock);
167
      sdbFreeRow(pSdb, pRow, false);
S
Shengliang Guan 已提交
168 169
      terrno = code;
      return terrno;
S
Shengliang Guan 已提交
170 171 172
    }
  }

S
Shengliang Guan 已提交
173
  if (pSdb->keyTypes[pRow->type] == SDB_KEY_INT32) {
dengyihao's avatar
dengyihao 已提交
174
    pSdb->maxId[pRow->type] = TMAX(pSdb->maxId[pRow->type], *((int32_t *)pRow->pObj));
S
Shengliang Guan 已提交
175 176
  }
  if (pSdb->keyTypes[pRow->type] == SDB_KEY_INT64) {
dengyihao's avatar
dengyihao 已提交
177
    pSdb->maxId[pRow->type] = TMAX(pSdb->maxId[pRow->type], *((int32_t *)pRow->pObj));
S
Shengliang Guan 已提交
178 179 180
  }
  pSdb->tableVer[pRow->type]++;

S
Shengliang Guan 已提交
181 182 183
  return 0;
}

S
Shengliang Guan 已提交
184
static int32_t sdbUpdateRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow *pNewRow, int32_t keySize) {
185 186
  TdThreadRwlock *pLock = &pSdb->locks[pNewRow->type];
  taosThreadRwlockWrlock(pLock);
S
Shengliang Guan 已提交
187

S
Shengliang Guan 已提交
188 189
  SSdbRow **ppOldRow = taosHashGet(hash, pNewRow->pObj, keySize);
  if (ppOldRow == NULL || *ppOldRow == NULL) {
190
    taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
191
    return sdbInsertRow(pSdb, hash, pRaw, pNewRow, keySize);
S
Shengliang Guan 已提交
192 193
  }

S
Shengliang Guan 已提交
194
  SSdbRow *pOldRow = *ppOldRow;
S
Shengliang Guan 已提交
195
  pOldRow->status = pRaw->status;
196
  sdbPrintOper(pSdb, pOldRow, "update");
197
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
198

S
Shengliang Guan 已提交
199
  int32_t     code = 0;
S
Shengliang Guan 已提交
200
  SdbUpdateFp updateFp = pSdb->updateFps[pNewRow->type];
S
Shengliang Guan 已提交
201
  if (updateFp != NULL) {
S
Shengliang Guan 已提交
202
    code = (*updateFp)(pSdb, pOldRow->pObj, pNewRow->pObj);
S
Shengliang Guan 已提交
203 204
  }

205
  sdbFreeRow(pSdb, pNewRow, false);
S
Shengliang Guan 已提交
206 207

  pSdb->tableVer[pOldRow->type]++;
S
Shengliang Guan 已提交
208
  return code;
S
Shengliang Guan 已提交
209 210
}

S
Shengliang Guan 已提交
211
static int32_t sdbDeleteRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow *pRow, int32_t keySize) {
212 213
  TdThreadRwlock *pLock = &pSdb->locks[pRow->type];
  taosThreadRwlockWrlock(pLock);
S
Shengliang Guan 已提交
214

S
Shengliang Guan 已提交
215 216
  SSdbRow **ppOldRow = taosHashGet(hash, pRow->pObj, keySize);
  if (ppOldRow == NULL || *ppOldRow == NULL) {
217
    taosThreadRwlockUnlock(pLock);
218
    sdbFreeRow(pSdb, pRow, false);
S
Shengliang Guan 已提交
219 220
    terrno = TSDB_CODE_SDB_OBJ_NOT_THERE;
    return terrno;
S
Shengliang Guan 已提交
221
  }
S
Shengliang Guan 已提交
222
  SSdbRow *pOldRow = *ppOldRow;
S
Shengliang Guan 已提交
223

S
Shengliang Guan 已提交
224
  pOldRow->status = pRaw->status;
225
  sdbPrintOper(pSdb, pOldRow, "delete");
S
Shengliang Guan 已提交
226

S
Shengliang Guan 已提交
227
  taosHashRemove(hash, pOldRow->pObj, keySize);
228
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
229

S
Shengliang Guan 已提交
230
  pSdb->tableVer[pOldRow->type]++;
231
  sdbFreeRow(pSdb, pRow, false);
232

S
Shengliang Guan 已提交
233
  sdbCheckRow(pSdb, pOldRow);
S
Shengliang Guan 已提交
234
  return 0;
S
Shengliang Guan 已提交
235 236
}

237
int32_t sdbWriteWithoutFree(SSdb *pSdb, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
238
  SHashObj *hash = sdbGetHash(pSdb, pRaw->type);
S
Shengliang Guan 已提交
239
  if (hash == NULL) return terrno;
S
Shengliang Guan 已提交
240

S
Shengliang Guan 已提交
241
  SdbDecodeFp decodeFp = pSdb->decodeFps[pRaw->type];
S
Shengliang Guan 已提交
242 243
  SSdbRow    *pRow = (*decodeFp)(pRaw);
  if (pRow == NULL) {
S
Shengliang Guan 已提交
244
    return terrno;
S
Shengliang Guan 已提交
245 246
  }

S
Shengliang Guan 已提交
247
  pRow->type = pRaw->type;
S
Shengliang Guan 已提交
248

S
Shengliang Guan 已提交
249
  int32_t keySize = sdbGetkeySize(pSdb, pRow->type, pRow->pObj);
S
Shengliang Guan 已提交
250
  int32_t code = TSDB_CODE_SDB_INVALID_ACTION_TYPE;
S
Shengliang Guan 已提交
251 252 253

  switch (pRaw->status) {
    case SDB_STATUS_CREATING:
S
Shengliang Guan 已提交
254
      code = sdbInsertRow(pSdb, hash, pRaw, pRow, keySize);
S
Shengliang Guan 已提交
255 256
      break;
    case SDB_STATUS_READY:
S
Shengliang Guan 已提交
257
    case SDB_STATUS_DROPPING:
S
Shengliang Guan 已提交
258
      code = sdbUpdateRow(pSdb, hash, pRaw, pRow, keySize);
S
Shengliang Guan 已提交
259 260
      break;
    case SDB_STATUS_DROPPED:
S
Shengliang Guan 已提交
261
      code = sdbDeleteRow(pSdb, hash, pRaw, pRow, keySize);
S
Shengliang Guan 已提交
262
      break;
S
Shengliang Guan 已提交
263 264
  }

265
  return code;
S
Shengliang Guan 已提交
266 267
}

S
Shengliang Guan 已提交
268
int32_t sdbWrite(SSdb *pSdb, SSdbRaw *pRaw) {
269
  int32_t code = sdbWriteWithoutFree(pSdb, pRaw);
S
Shengliang Guan 已提交
270 271 272 273
  sdbFreeRaw(pRaw);
  return code;
}

L
Liu Jicong 已提交
274
void *sdbAcquire(SSdb *pSdb, ESdbType type, const void *pKey) {
275 276
  terrno = 0;

S
Shengliang Guan 已提交
277
  SHashObj *hash = sdbGetHash(pSdb, type);
S
Shengliang Guan 已提交
278 279 280
  if (hash == NULL) return NULL;

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

283 284
  TdThreadRwlock *pLock = &pSdb->locks[type];
  taosThreadRwlockRdlock(pLock);
S
Shengliang Guan 已提交
285 286

  SSdbRow **ppRow = taosHashGet(hash, pKey, keySize);
S
Shengliang Guan 已提交
287
  if (ppRow == NULL || *ppRow == NULL) {
288
    taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
289
    terrno = TSDB_CODE_SDB_OBJ_NOT_THERE;
S
Shengliang Guan 已提交
290 291 292
    return NULL;
  }

S
Shengliang Guan 已提交
293 294 295 296 297
  SSdbRow *pRow = *ppRow;
  switch (pRow->status) {
    case SDB_STATUS_READY:
      atomic_add_fetch_32(&pRow->refCount, 1);
      pRet = pRow->pObj;
298
      sdbPrintOper(pSdb, pRow, "acquire");
S
Shengliang Guan 已提交
299
      break;
S
Shengliang Guan 已提交
300 301
    case SDB_STATUS_CREATING:
      terrno = TSDB_CODE_SDB_OBJ_CREATING;
S
Shengliang Guan 已提交
302
      break;
S
Shengliang Guan 已提交
303
    case SDB_STATUS_DROPPING:
S
Shengliang Guan 已提交
304
      terrno = TSDB_CODE_SDB_OBJ_DROPPING;
S
Shengliang Guan 已提交
305 306
      break;
    default:
S
Shengliang Guan 已提交
307 308
      terrno = TSDB_CODE_SDB_APP_ERROR;
      break;
S
Shengliang Guan 已提交
309 310
  }

311
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
312
  return pRet;
S
Shengliang Guan 已提交
313 314
}

S
Shengliang Guan 已提交
315
static void sdbCheckRow(SSdb *pSdb, SSdbRow *pRow) {
316 317
  TdThreadRwlock *pLock = &pSdb->locks[pRow->type];
  taosThreadRwlockWrlock(pLock);
318 319

  int32_t ref = atomic_load_32(&pRow->refCount);
320
  sdbPrintOper(pSdb, pRow, "check");
321
  if (ref <= 0 && pRow->status == SDB_STATUS_DROPPED) {
322
    sdbFreeRow(pSdb, pRow, true);
323 324
  }

325
  taosThreadRwlockUnlock(pLock);
326 327
}

S
Shengliang Guan 已提交
328
void sdbRelease(SSdb *pSdb, void *pObj) {
S
Shengliang Guan 已提交
329 330
  if (pObj == NULL) return;

S
Shengliang Guan 已提交
331
  SSdbRow *pRow = (SSdbRow *)((char *)pObj - sizeof(SSdbRow));
332
  if (pRow->type >= SDB_MAX) return;
S
Shengliang Guan 已提交
333

334 335
  TdThreadRwlock *pLock = &pSdb->locks[pRow->type];
  taosThreadRwlockWrlock(pLock);
S
Shengliang Guan 已提交
336 337

  int32_t ref = atomic_sub_fetch_32(&pRow->refCount, 1);
338
  sdbPrintOper(pSdb, pRow, "release");
S
Shengliang Guan 已提交
339
  if (ref <= 0 && pRow->status == SDB_STATUS_DROPPED) {
340
    sdbFreeRow(pSdb, pRow, true);
S
Shengliang Guan 已提交
341 342
  }

343
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
344 345
}

S
Shengliang Guan 已提交
346
void *sdbFetch(SSdb *pSdb, ESdbType type, void *pIter, void **ppObj) {
347 348
  *ppObj = NULL;

S
Shengliang Guan 已提交
349
  SHashObj *hash = sdbGetHash(pSdb, type);
S
Shengliang Guan 已提交
350
  if (hash == NULL) return NULL;
S
Shengliang Guan 已提交
351

352 353
  TdThreadRwlock *pLock = &pSdb->locks[type];
  taosThreadRwlockRdlock(pLock);
S
Shengliang Guan 已提交
354

S
Shengliang Guan 已提交
355
  SSdbRow **ppRow = taosHashIterate(hash, pIter);
S
Shengliang Guan 已提交
356 357 358 359 360 361 362 363
  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);
364
    sdbPrintOper(pSdb, pRow, "fetch");
S
Shengliang Guan 已提交
365 366 367
    *ppObj = pRow->pObj;
    break;
  }
368
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
369

S
Shengliang Guan 已提交
370
  return ppRow;
S
Shengliang Guan 已提交
371 372
}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
void *sdbFetchAll(SSdb *pSdb, ESdbType type, void *pIter, void **ppObj, ESdbStatus *status) {
  *ppObj = NULL;

  SHashObj *hash = sdbGetHash(pSdb, type);
  if (hash == NULL) return NULL;

  TdThreadRwlock *pLock = &pSdb->locks[type];
  taosThreadRwlockRdlock(pLock);

  SSdbRow **ppRow = taosHashIterate(hash, pIter);
  while (ppRow != NULL) {
    SSdbRow *pRow = *ppRow;
    if (pRow == NULL) {
      ppRow = taosHashIterate(hash, ppRow);
      continue;
    }

    atomic_add_fetch_32(&pRow->refCount, 1);
    sdbPrintOper(pSdb, pRow, "fetch");
    *ppObj = pRow->pObj;
    *status = pRow->status;
    break;
  }
  taosThreadRwlockUnlock(pLock);

  return ppRow;
}

S
Shengliang Guan 已提交
401
void sdbCancelFetch(SSdb *pSdb, void *pIter) {
S
Shengliang Guan 已提交
402 403
  if (pIter == NULL) return;
  SSdbRow  *pRow = *(SSdbRow **)pIter;
S
Shengliang Guan 已提交
404
  SHashObj *hash = sdbGetHash(pSdb, pRow->type);
S
Shengliang Guan 已提交
405
  if (hash == NULL) return;
S
Shengliang Guan 已提交
406

407 408
  TdThreadRwlock *pLock = &pSdb->locks[pRow->type];
  taosThreadRwlockRdlock(pLock);
S
Shengliang Guan 已提交
409
  taosHashCancelIterate(hash, pIter);
410
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
411 412
}

S
Shengliang Guan 已提交
413 414 415 416
void sdbTraverse(SSdb *pSdb, ESdbType type, sdbTraverseFp fp, void *p1, void *p2, void *p3) {
  SHashObj *hash = sdbGetHash(pSdb, type);
  if (hash == NULL) return;

417 418
  TdThreadRwlock *pLock = &pSdb->locks[type];
  taosThreadRwlockRdlock(pLock);
S
Shengliang Guan 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

  SSdbRow **ppRow = taosHashIterate(hash, NULL);
  while (ppRow != NULL) {
    SSdbRow *pRow = *ppRow;
    if (pRow->status == SDB_STATUS_READY) {
      bool isContinue = (*fp)(pSdb->pMnode, pRow->pObj, p1, p2, p3);
      if (!isContinue) {
        taosHashCancelIterate(hash, ppRow);
        break;
      }
    }

    ppRow = taosHashIterate(hash, ppRow);
  }

434
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
435 436
}

S
Shengliang Guan 已提交
437 438
int32_t sdbGetSize(SSdb *pSdb, ESdbType type) {
  SHashObj *hash = sdbGetHash(pSdb, type);
S
Shengliang Guan 已提交
439
  if (hash == NULL) return 0;
S
Shengliang Guan 已提交
440

441 442
  TdThreadRwlock *pLock = &pSdb->locks[type];
  taosThreadRwlockRdlock(pLock);
S
Shengliang Guan 已提交
443
  int32_t size = taosHashGetSize(hash);
444
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
445 446

  return size;
S
Shengliang Guan 已提交
447
}
S
Shengliang Guan 已提交
448 449 450 451 452 453 454 455 456

int32_t sdbGetMaxId(SSdb *pSdb, ESdbType type) {
  SHashObj *hash = sdbGetHash(pSdb, type);
  if (hash == NULL) return -1;

  if (pSdb->keyTypes[type] != SDB_KEY_INT32) return -1;

  int32_t maxId = 0;

457 458
  TdThreadRwlock *pLock = &pSdb->locks[type];
  taosThreadRwlockRdlock(pLock);
S
Shengliang Guan 已提交
459 460 461 462 463

  SSdbRow **ppRow = taosHashIterate(hash, NULL);
  while (ppRow != NULL) {
    SSdbRow *pRow = *ppRow;
    int32_t  id = *(int32_t *)pRow->pObj;
dengyihao's avatar
dengyihao 已提交
464
    maxId = TMAX(id, maxId);
S
Shengliang Guan 已提交
465 466 467
    ppRow = taosHashIterate(hash, ppRow);
  }

468
  taosThreadRwlockUnlock(pLock);
S
Shengliang Guan 已提交
469

dengyihao's avatar
dengyihao 已提交
470
  maxId = TMAX(maxId, pSdb->maxId[type]);
S
Shengliang Guan 已提交
471 472
  return maxId + 1;
}
473 474 475 476 477 478 479 480 481

int64_t sdbGetTableVer(SSdb *pSdb, ESdbType type) {
  if (type >= SDB_MAX || type < 0) {
    terrno = TSDB_CODE_SDB_INVALID_TABLE_TYPE;
    return -1;
  }

  return pSdb->tableVer[type];
}