sdbHash.c 12.6 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
    case SDB_QNODE:
      return "qnode";
    case SDB_SNODE:
      return "snode";
S
Shengliang Guan 已提交
33 34 35 36 37 38 39 40
    case SDB_DNODE:
      return "dnode";
    case SDB_USER:
      return "user";
    case SDB_AUTH:
      return "auth";
    case SDB_ACCT:
      return "acct";
S
Shengliang Guan 已提交
41 42
    case SDB_STREAM_CK:
      return "stream_ck";
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
    case SDB_STB:
      return "stb";
    case SDB_DB:
      return "db";
    case SDB_FUNC:
      return "func";
dengyihao's avatar
dengyihao 已提交
63 64
    case SDB_IDX:
      return "idx";
S
Shengliang Guan 已提交
65 66 67 68 69
    default:
      return "undefine";
  }
}

S
Shengliang Guan 已提交
70
const char *sdbStatusName(ESdbStatus status) {
S
Shengliang Guan 已提交
71 72 73 74 75 76 77 78 79
  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";
80 81
    case SDB_STATUS_INIT:
      return "init";
S
Shengliang Guan 已提交
82 83 84 85 86
    default:
      return "undefine";
  }
}

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

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

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

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

  return hash;
}

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

  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 已提交
135
static int32_t sdbInsertRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow *pRow, int32_t keySize) {
136 137
  int32_t type = pRow->type;
  sdbWriteLock(pSdb, type);
S
Shengliang Guan 已提交
138

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

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

  if (taosHashPut(hash, pRow->pObj, keySize, &pRow, sizeof(void *)) != 0) {
152
    sdbUnLock(pSdb, type);
153
    sdbFreeRow(pSdb, pRow, false);
S
Shengliang Guan 已提交
154
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
155
    return terrno;
S
Shengliang Guan 已提交
156 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) {
163
      if (terrno == 0) terrno = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
S
Shengliang Guan 已提交
164
      code = terrno;
S
Shengliang Guan 已提交
165
      taosHashRemove(hash, pRow->pObj, keySize);
166
      sdbFreeRow(pSdb, pRow, false);
S
Shengliang Guan 已提交
167
      terrno = code;
168
      sdbUnLock(pSdb, type);
S
Shengliang Guan 已提交
169
      return terrno;
S
Shengliang Guan 已提交
170 171 172
    }
  }

173
  sdbUnLock(pSdb, type);
174

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

S
Shengliang Guan 已提交
183 184 185
  return 0;
}

S
Shengliang Guan 已提交
186
static int32_t sdbUpdateRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow *pNewRow, int32_t keySize) {
187 188
  int32_t type = pNewRow->type;
  sdbWriteLock(pSdb, type);
S
Shengliang Guan 已提交
189

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

S
Shengliang Guan 已提交
196
  SSdbRow *pOldRow = *ppOldRow;
S
Shengliang Guan 已提交
197
  pOldRow->status = pRaw->status;
198
  sdbPrintOper(pSdb, pOldRow, "update");
199
  sdbUnLock(pSdb, type);
S
Shengliang Guan 已提交
200

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

S
Shengliang Guan 已提交
207
  // sdbUnLock(pSdb, type);
208
  sdbFreeRow(pSdb, pNewRow, false);
S
Shengliang Guan 已提交
209 210

  pSdb->tableVer[pOldRow->type]++;
S
Shengliang Guan 已提交
211
  return code;
S
Shengliang Guan 已提交
212 213
}

S
Shengliang Guan 已提交
214
static int32_t sdbDeleteRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow *pRow, int32_t keySize) {
215 216
  int32_t type = pRow->type;
  sdbWriteLock(pSdb, type);
S
Shengliang Guan 已提交
217

S
Shengliang Guan 已提交
218 219
  SSdbRow **ppOldRow = taosHashGet(hash, pRow->pObj, keySize);
  if (ppOldRow == NULL || *ppOldRow == NULL) {
220
    sdbUnLock(pSdb, type);
221
    sdbFreeRow(pSdb, pRow, false);
S
Shengliang Guan 已提交
222 223
    terrno = TSDB_CODE_SDB_OBJ_NOT_THERE;
    return terrno;
S
Shengliang Guan 已提交
224
  }
S
Shengliang Guan 已提交
225 226
  SSdbRow *pOldRow = *ppOldRow;
  pOldRow->status = pRaw->status;
227 228

  atomic_add_fetch_32(&pOldRow->refCount, 1);
229
  sdbPrintOper(pSdb, pOldRow, "delete");
S
Shengliang Guan 已提交
230

S
Shengliang Guan 已提交
231
  taosHashRemove(hash, pOldRow->pObj, keySize);
232
  pSdb->tableVer[pOldRow->type]++;
233
  sdbUnLock(pSdb, type);
S
Shengliang Guan 已提交
234

235
  sdbFreeRow(pSdb, pRow, false);
236

S
Shengliang Guan 已提交
237
  sdbCheckRow(pSdb, pOldRow);
S
Shengliang Guan 已提交
238
  return 0;
S
Shengliang Guan 已提交
239 240
}

241
int32_t sdbWriteWithoutFree(SSdb *pSdb, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
242
  SHashObj *hash = sdbGetHash(pSdb, pRaw->type);
S
Shengliang Guan 已提交
243
  if (hash == NULL) return terrno;
S
Shengliang Guan 已提交
244

S
Shengliang Guan 已提交
245
  SdbDecodeFp decodeFp = pSdb->decodeFps[pRaw->type];
S
Shengliang Guan 已提交
246
  SSdbRow    *pRow = (*decodeFp)(pRaw);
S
Shengliang Guan 已提交
247
  if (pRow == NULL) return terrno;
S
Shengliang Guan 已提交
248

S
Shengliang Guan 已提交
249
  pRow->type = pRaw->type;
S
Shengliang Guan 已提交
250

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

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

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

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

276
void *sdbAcquireAll(SSdb *pSdb, ESdbType type, const void *pKey, bool onlyReady) {
277 278
  terrno = 0;

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

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

285
  sdbReadLock(pSdb, type);
S
Shengliang Guan 已提交
286 287

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

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

312 313 314 315 316 317 318 319 320
  if (pRet == NULL) {
    if (!onlyReady) {
      terrno = 0;
      atomic_add_fetch_32(&pRow->refCount, 1);
      pRet = pRow->pObj;
      sdbPrintOper(pSdb, pRow, "acquire");
    }
  }

321
  sdbUnLock(pSdb, type);
S
Shengliang Guan 已提交
322
  return pRet;
S
Shengliang Guan 已提交
323 324
}

325 326 327 328 329
void *sdbAcquire(SSdb *pSdb, ESdbType type, const void *pKey) { return sdbAcquireAll(pSdb, type, pKey, true); }
void *sdbAcquireNotReadyObj(SSdb *pSdb, ESdbType type, const void *pKey) {
  return sdbAcquireAll(pSdb, type, pKey, false);
}

S
Shengliang Guan 已提交
330
static void sdbCheckRow(SSdb *pSdb, SSdbRow *pRow) {
331 332
  int32_t type = pRow->type;
  sdbWriteLock(pSdb, type);
333

334
  int32_t ref = atomic_sub_fetch_32(&pRow->refCount, 1);
335
  sdbPrintOper(pSdb, pRow, "check");
336
  if (ref <= 0 && pRow->status == SDB_STATUS_DROPPED) {
337
    sdbFreeRow(pSdb, pRow, true);
338 339
  }

340
  sdbUnLock(pSdb, type);
341 342
}

343
void sdbReleaseLock(SSdb *pSdb, void *pObj, bool lock) {
S
Shengliang Guan 已提交
344 345
  if (pObj == NULL) return;

S
Shengliang Guan 已提交
346
  SSdbRow *pRow = (SSdbRow *)((char *)pObj - sizeof(SSdbRow));
347
  if (pRow->type >= SDB_MAX) return;
S
Shengliang Guan 已提交
348

349
  int32_t type = pRow->type;
350
  if (lock) {
351
    sdbWriteLock(pSdb, type);
352
  }
S
Shengliang Guan 已提交
353 354

  int32_t ref = atomic_sub_fetch_32(&pRow->refCount, 1);
355
  sdbPrintOper(pSdb, pRow, "release");
S
Shengliang Guan 已提交
356
  if (ref <= 0 && pRow->status == SDB_STATUS_DROPPED) {
357
    sdbFreeRow(pSdb, pRow, true);
S
Shengliang Guan 已提交
358 359
  }

360
  if (lock) {
361
    sdbUnLock(pSdb, type);
362
  }
S
Shengliang Guan 已提交
363 364
}

365 366
void sdbRelease(SSdb *pSdb, void *pObj) { sdbReleaseLock(pSdb, pObj, true); }

S
Shengliang Guan 已提交
367
void *sdbFetch(SSdb *pSdb, ESdbType type, void *pIter, void **ppObj) {
368 369
  *ppObj = NULL;

S
Shengliang Guan 已提交
370
  SHashObj *hash = sdbGetHash(pSdb, type);
S
Shengliang Guan 已提交
371
  if (hash == NULL) return NULL;
S
Shengliang Guan 已提交
372

373
  sdbReadLock(pSdb, type);
S
Shengliang Guan 已提交
374

S
Shengliang Guan 已提交
375
  SSdbRow **ppRow = taosHashIterate(hash, pIter);
S
Shengliang Guan 已提交
376 377 378 379 380 381 382 383
  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);
384
    sdbPrintOper(pSdb, pRow, "fetch");
S
Shengliang Guan 已提交
385 386 387
    *ppObj = pRow->pObj;
    break;
  }
388
  sdbUnLock(pSdb, type);
S
Shengliang Guan 已提交
389

S
Shengliang Guan 已提交
390
  return ppRow;
S
Shengliang Guan 已提交
391 392
}

393
void *sdbFetchAll(SSdb *pSdb, ESdbType type, void *pIter, void **ppObj, ESdbStatus *status, bool lock) {
394 395 396 397 398
  *ppObj = NULL;

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

399
  if (lock) {
400
    sdbReadLock(pSdb, type);
401
  }
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

  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;
  }
417
  if (lock) {
418
    sdbUnLock(pSdb, type);
419
  }
420 421 422 423

  return ppRow;
}

S
Shengliang Guan 已提交
424
void sdbCancelFetch(SSdb *pSdb, void *pIter) {
S
Shengliang Guan 已提交
425 426
  if (pIter == NULL) return;
  SSdbRow  *pRow = *(SSdbRow **)pIter;
S
Shengliang Guan 已提交
427
  SHashObj *hash = sdbGetHash(pSdb, pRow->type);
S
Shengliang Guan 已提交
428
  if (hash == NULL) return;
S
Shengliang Guan 已提交
429

430 431
  int32_t type = pRow->type;
  sdbReadLock(pSdb, type);
S
Shengliang Guan 已提交
432
  taosHashCancelIterate(hash, pIter);
433
  sdbUnLock(pSdb, type);
S
Shengliang Guan 已提交
434 435
}

S
Shengliang Guan 已提交
436 437 438 439
void sdbTraverse(SSdb *pSdb, ESdbType type, sdbTraverseFp fp, void *p1, void *p2, void *p3) {
  SHashObj *hash = sdbGetHash(pSdb, type);
  if (hash == NULL) return;

440
  sdbReadLock(pSdb, type);
S
Shengliang Guan 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

  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);
  }

456
  sdbUnLock(pSdb, type);
S
Shengliang Guan 已提交
457 458
}

S
Shengliang Guan 已提交
459 460
int32_t sdbGetSize(SSdb *pSdb, ESdbType type) {
  SHashObj *hash = sdbGetHash(pSdb, type);
S
Shengliang Guan 已提交
461
  if (hash == NULL) return 0;
S
Shengliang Guan 已提交
462

463
  sdbReadLock(pSdb, type);
S
Shengliang Guan 已提交
464
  int32_t size = taosHashGetSize(hash);
465
  sdbUnLock(pSdb, type);
S
Shengliang Guan 已提交
466 467

  return size;
S
Shengliang Guan 已提交
468
}
S
Shengliang Guan 已提交
469 470 471 472 473 474 475 476

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;
477
  sdbReadLock(pSdb, type);
S
Shengliang Guan 已提交
478 479 480 481 482

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

487
  sdbUnLock(pSdb, type);
dengyihao's avatar
dengyihao 已提交
488
  maxId = TMAX(maxId, pSdb->maxId[type]);
S
Shengliang Guan 已提交
489 490
  return maxId + 1;
}
491 492 493 494 495 496 497 498 499

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];
}