sdbHash.c 12.5 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) {
S
Shengliang Guan 已提交
163
      code = terrno;
S
Shengliang Guan 已提交
164
      taosHashRemove(hash, pRow->pObj, keySize);
165
      sdbFreeRow(pSdb, pRow, false);
S
Shengliang Guan 已提交
166
      terrno = code;
167
      sdbUnLock(pSdb, type);
S
Shengliang Guan 已提交
168
      return terrno;
S
Shengliang Guan 已提交
169 170 171
    }
  }

172
  sdbUnLock(pSdb, type);
173

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

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

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

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

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

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

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

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

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

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

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

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

234
  sdbFreeRow(pSdb, pRow, false);
235

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

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

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

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

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

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

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

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

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

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

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

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

  SSdbRow **ppRow = taosHashGet(hash, pKey, keySize);
S
Shengliang Guan 已提交
287
  if (ppRow == NULL || *ppRow == NULL) {
288
    sdbUnLock(pSdb, type);
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
      terrno = TSDB_CODE_APP_ERROR;
S
Shengliang Guan 已提交
308
      break;
S
Shengliang Guan 已提交
309 310
  }

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

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

324 325 326 327 328
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 已提交
329
static void sdbCheckRow(SSdb *pSdb, SSdbRow *pRow) {
330 331
  int32_t type = pRow->type;
  sdbWriteLock(pSdb, type);
332

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

339
  sdbUnLock(pSdb, type);
340 341
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return ppRow;
}

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

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

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

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

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

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

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

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

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

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

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

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

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