• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3796

31 Mar 2025 10:39AM UTC coverage: 30.372% (-7.1%) from 37.443%
#3796

push

travis-ci

happyguoxy
test:add test cases

69287 of 309062 branches covered (22.42%)

Branch coverage included in aggregate %.

118044 of 307720 relevant lines covered (38.36%)

278592.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.35
/source/dnode/vnode/src/meta/metaQuery.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "meta.h"
17
#include "osMemory.h"
18
#include "tencode.h"
19

20
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
40✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
40✔
22
  metaReaderDoInit(pReader, pMeta, flags);
40✔
23
  pReader->pAPI = pAPI;
40✔
24
}
40✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
49✔
27
  memset(pReader, 0, sizeof(*pReader));
49✔
28
  pReader->pMeta = pMeta;
49✔
29
  pReader->flags = flags;
49✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
49!
31
    metaRLock(pMeta);
40✔
32
  }
33
}
49✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
×
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
×
37
    metaULock(pReader->pMeta);
×
38
    pReader->flags |= META_READER_NOLOCK;
×
39
  }
40
}
×
41

42
void metaReaderClear(SMetaReader *pReader) {
49✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
49!
44
    metaULock(pReader->pMeta);
40✔
45
  }
46
  tDecoderClear(&pReader->coder);
49✔
47
  tdbFree(pReader->pBuf);
49✔
48
  pReader->pBuf = NULL;
49✔
49
}
49✔
50

51
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
89✔
52
  int32_t  code = 0;
89✔
53
  SMeta   *pMeta = pReader->pMeta;
89✔
54
  STbDbKey tbDbKey = {.version = version, .uid = uid};
89✔
55

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
89!
58
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
59
  }
60

61
  // decode the entry
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
89✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
89✔
65
  if (code) {
89!
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
89✔
72
}
73

74
bool metaIsTableExist(void *pVnode, tb_uid_t uid) {
40✔
75
  SVnode *pVnodeObj = pVnode;
40✔
76
  metaRLock(pVnodeObj->pMeta);  // query uid.idx
40✔
77

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
40!
79
    metaULock(pVnodeObj->pMeta);
×
80
    return false;
×
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
40✔
84
  return true;
40✔
85
}
86

87
int metaReaderGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) {
9✔
88
  SMeta  *pMeta = pReader->pMeta;
9✔
89
  int64_t version1;
90

91
  // query uid.idx
92
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
9!
93
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
94
  }
95

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
9✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
9✔
98
}
99

100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
80✔
101
  SMeta *pMeta = pReader->pMeta;
80✔
102

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
80✔
105
  if (TSDB_CODE_SUCCESS != code) {
80!
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
107
  }
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
80✔
110
}
111

112
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
×
113
  SMeta   *pMeta = pReader->pMeta;
×
114
  tb_uid_t uid;
115

116
  // query name.idx
117
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
×
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
119
  }
120

121
  uid = *(tb_uid_t *)pReader->pBuf;
×
122
  return metaReaderGetTableEntryByUid(pReader, uid);
×
123
}
124

125
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
1✔
126
  void    *pData = NULL;
1✔
127
  int      nData = 0;
1✔
128
  tb_uid_t uid = 0;
1✔
129

130
  metaRLock(pMeta);
1✔
131

132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
1!
133
    uid = *(tb_uid_t *)pData;
×
134
    tdbFree(pData);
×
135
  }
136

137
  metaULock(pMeta);
1✔
138

139
  return uid;
1✔
140
}
141

142
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
×
143
  int         code = 0;
×
144
  SMetaReader mr = {0};
×
145
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
×
146
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
147
  if (code < 0) {
×
148
    metaReaderClear(&mr);
×
149
    return code;
×
150
  }
151

152
  STR_TO_VARSTR(tbName, mr.me.name);
×
153
  metaReaderClear(&mr);
×
154

155
  return 0;
×
156
}
157

158
int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
×
159
  int         code = 0;
×
160
  SMetaReader mr = {0};
×
161
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
162
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
163
  if (code < 0) {
×
164
    metaReaderClear(&mr);
×
165
    return code;
×
166
  }
167
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
168
  metaReaderClear(&mr);
×
169

170
  return 0;
×
171
}
172

173
int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) {
×
174
  int         code = 0;
×
175
  SMetaReader mr = {0};
×
176
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
×
177

178
  SMetaReader *pReader = &mr;
×
179

180
  // query name.idx
181
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
×
182
    metaReaderClear(&mr);
×
183
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
184
  }
185

186
  *uid = *(tb_uid_t *)pReader->pBuf;
×
187

188
  metaReaderClear(&mr);
×
189

190
  return 0;
×
191
}
192

193
int metaGetTableTypeSuidByName(void *pVnode, char *tbName, ETableType *tbType, uint64_t* suid) {
×
194
  int         code = 0;
×
195
  SMetaReader mr = {0};
×
196
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
×
197

198
  code = metaGetTableEntryByName(&mr, tbName);
×
199
  if (code == 0) *tbType = mr.me.type;
×
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
×
201
    *suid = mr.me.ctbEntry.suid;
×
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
×
203
    *suid = mr.me.uid;
×
204
  } else {
205
    *suid = 0;
×
206
  }
207

208
  metaReaderClear(&mr);
×
209
  return code;
×
210
}
211

212
int metaReadNext(SMetaReader *pReader) {
×
213
  SMeta *pMeta = pReader->pMeta;
×
214

215
  // TODO
216

217
  return 0;
×
218
}
219

220
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
221
  int         code = -1;
×
222
  SMetaReader mr = {0};
×
223
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
224
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
225
  if (code < 0) {
×
226
    goto _exit;
×
227
  }
228
  if (mr.me.type == TSDB_CHILD_TABLE) {
×
229
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
230
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
×
231
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
232
  } else {
233
    goto _exit;
×
234
  }
235

236
  code = 0;
×
237

238
_exit:
×
239
  metaReaderClear(&mr);
×
240
  return code;
×
241
}
242

243
#if 1  // ===================================================
244
SMTbCursor *metaOpenTbCursor(void *pVnode) {
×
245
  SMTbCursor *pTbCur = NULL;
×
246
  int32_t     code;
247

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
×
249
  if (pTbCur == NULL) {
×
250
    return NULL;
×
251
  }
252

253
  SVnode *pVnodeObj = pVnode;
×
254
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
255
  pTbCur->pMeta = pVnodeObj->pMeta;
×
256
  pTbCur->paused = 1;
×
257
  code = metaResumeTbCursor(pTbCur, 1, 0);
×
258
  if (code) {
×
259
    terrno = code;
×
260
    taosMemoryFree(pTbCur);
×
261
    return NULL;
×
262
  }
263
  return pTbCur;
×
264
}
265

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
×
267
  if (pTbCur) {
×
268
    tdbFree(pTbCur->pKey);
×
269
    tdbFree(pTbCur->pVal);
×
270
    if (!pTbCur->paused) {
×
271
      metaReaderClear(&pTbCur->mr);
×
272
      if (pTbCur->pDbc) {
×
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
×
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
×
277
  }
278
}
×
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
×
281
  if (!pTbCur->paused) {
×
282
    metaReaderClear(&pTbCur->mr);
×
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
×
284
    pTbCur->paused = 1;
×
285
  }
286
}
×
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
×
288
  int32_t code = 0;
×
289
  int32_t lino;
290
  int8_t  locked = 0;
×
291
  if (pTbCur->paused) {
×
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
×
293
    locked = 1;
×
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
×
295
    if (code != 0) {
×
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
×
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
×
301
      TSDB_CHECK_CODE(code, lino, _exit);
×
302
    } else {
303
      int c = 1;
×
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
×
305
      TSDB_CHECK_CODE(code, lino, _exit);
×
306
      if (c == 0) {
×
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
×
308
      } else if (c < 0) {
×
309
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
310
        TSDB_CHECK_CODE(code, lino, _exit);
×
311
      } else {
312
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
313
        TSDB_CHECK_CODE(code, lino, _exit);
×
314
      }
315
    }
316

317
    pTbCur->paused = 0;
×
318
  }
319

320
_exit:
×
321
  if (code != 0 && locked) {
×
322
    metaReaderReleaseLock(&pTbCur->mr);
×
323
  }
324
  return code;
×
325
}
326

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
328
  int    ret;
329
  void  *pBuf;
330
  STbCfg tbCfg;
331

332
  for (;;) {
333
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
334
    if (ret < 0) {
×
335
      return ret;
×
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
×
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
341
    if (ret) return ret;
×
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
×
344
      continue;
×
345
    }
346

347
    break;
×
348
  }
349

350
  return 0;
×
351
}
352

353
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
354
  int    ret;
355
  void  *pBuf;
356
  STbCfg tbCfg;
357

358
  for (;;) {
359
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
360
    if (ret < 0) {
×
361
      return -1;
×
362
    }
363

364
    tDecoderClear(&pTbCur->mr.coder);
×
365

366
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
367
    if (ret < 0) {
×
368
      return ret;
×
369
    }
370

371
    if (pTbCur->mr.me.type == jumpTableType) {
×
372
      continue;
×
373
    }
374

375
    break;
×
376
  }
377

378
  return 0;
×
379
}
380

381
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema** extSchema) {
46✔
382
  void           *pData = NULL;
46✔
383
  int             nData = 0;
46✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
46✔
386
  SSchemaWrapper *pSchema = NULL;
46✔
387
  SDecoder        dc = {0};
46✔
388
  if (lock) {
46✔
389
    metaRLock(pMeta);
37✔
390
  }
391
_query:
46✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
46!
393
    goto _err;
×
394
  }
395

396
  version = ((SUidIdxVal *)pData)[0].version;
46✔
397

398
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
46!
399
    goto _err;
×
400
  }
401

402
  SMetaEntry me = {0};
46✔
403
  tDecoderInit(&dc, pData, nData);
46✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
46✔
405
  if (code) {
46!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
46!
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
46!
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
46✔
412
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
46!
413
      tDecoderClear(&dc);
46✔
414
      goto _exit;
46✔
415
    }
416
  } else if (me.type == TSDB_CHILD_TABLE) {
×
417
    uid = me.ctbEntry.suid;
×
418
    tDecoderClear(&dc);
×
419
    goto _query;
×
420
  } else {
421
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
×
422
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
×
423
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
×
424
      tDecoderClear(&dc);
×
425
      goto _exit;
×
426
    }
427
  }
428
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
×
429
  tDecoderClear(&dc);
×
430

431
  // query from skm db
432
  if (tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData) < 0) {
×
433
    goto _err;
×
434
  }
435

436
  tDecoderInit(&dc, pData, nData);
×
437
  if (tDecodeSSchemaWrapperEx(&dc, &schema) != 0) {
×
438
    goto _err;
×
439
  }
440
  pSchema = tCloneSSchemaWrapper(&schema);
×
441
  tDecoderClear(&dc);
×
442

443
_exit:
46✔
444
  if (lock) {
46✔
445
    metaULock(pMeta);
37✔
446
  }
447
  tdbFree(pData);
46✔
448
  return pSchema;
46✔
449

450
_err:
×
451
  if (lock) {
×
452
    metaULock(pMeta);
×
453
  }
454
  tdbFree(pData);
×
455
  return NULL;
×
456
}
457

458
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
×
459
  void           *pData = NULL;
×
460
  int             nData = 0;
×
461
  int64_t         version = 0;
×
462
  SDecoder        dc = {0};
×
463
  int64_t         createTime = INT64_MAX;
×
464
  if (lock) {
×
465
    metaRLock(pMeta);
×
466
  }
467

468
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
×
469
    goto _exit;
×
470
  }
471

472
  version = ((SUidIdxVal *)pData)[0].version;
×
473

474
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
×
475
    goto _exit;
×
476
  }
477

478
  SMetaEntry me = {0};
×
479
  tDecoderInit(&dc, pData, nData);
×
480
  int32_t code = metaDecodeEntry(&dc, &me);
×
481
  if (code) {
×
482
    tDecoderClear(&dc);
×
483
    goto _exit;
×
484
  }
485
  if (me.type == TSDB_CHILD_TABLE) {
×
486
    createTime = me.ctbEntry.btime;
×
487
  }
488
  tDecoderClear(&dc);
×
489

490
  _exit:
×
491
  if (lock) {
×
492
    metaULock(pMeta);
×
493
  }
494
  tdbFree(pData);
×
495
  return createTime;
×
496
}
497

498
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
9✔
499
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
9✔
500
  SMCtbCursor *pCtbCur = NULL;
9✔
501
  SCtbIdxKey   ctbIdxKey;
502
  int          ret = 0;
9✔
503
  int          c = 0;
9✔
504

505
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
9!
506
  if (pCtbCur == NULL) {
9!
507
    return NULL;
×
508
  }
509

510
  pCtbCur->pMeta = pMeta;
9✔
511
  pCtbCur->suid = uid;
9✔
512
  pCtbCur->lock = lock;
9✔
513
  pCtbCur->paused = 1;
9✔
514

515
  ret = metaResumeCtbCursor(pCtbCur, 1);
9✔
516
  if (ret < 0) {
9!
517
    return NULL;
×
518
  }
519
  return pCtbCur;
9✔
520
}
521

522
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
9✔
523
  if (pCtbCur) {
9!
524
    if (!pCtbCur->paused) {
9!
525
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
9!
526
      if (pCtbCur->pCur) {
9!
527
        tdbTbcClose(pCtbCur->pCur);
9✔
528
      }
529
    }
530
    tdbFree(pCtbCur->pKey);
9✔
531
    tdbFree(pCtbCur->pVal);
9✔
532
  }
533
  taosMemoryFree(pCtbCur);
9!
534
}
9✔
535

536
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
×
537
  if (!pCtbCur->paused) {
×
538
    tdbTbcClose((TBC *)pCtbCur->pCur);
×
539
    if (pCtbCur->lock) {
×
540
      metaULock(pCtbCur->pMeta);
×
541
    }
542
    pCtbCur->paused = 1;
×
543
  }
544
}
×
545

546
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
9✔
547
  if (pCtbCur->paused) {
9!
548
    pCtbCur->paused = 0;
9✔
549

550
    if (pCtbCur->lock) {
9!
551
      metaRLock(pCtbCur->pMeta);
×
552
    }
553
    int ret = 0;
9✔
554
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
9✔
555
    if (ret < 0) {
9!
556
      metaCloseCtbCursor(pCtbCur);
×
557
      return -1;
×
558
    }
559

560
    if (first) {
9!
561
      SCtbIdxKey ctbIdxKey;
562
      // move to the suid
563
      ctbIdxKey.suid = pCtbCur->suid;
9✔
564
      ctbIdxKey.uid = INT64_MIN;
9✔
565
      int c = 0;
9✔
566
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
9✔
567
      if (c > 0) {
9!
568
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
569
      }
570
    } else {
571
      int c = 0;
×
572
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
573
      if (c < 0) {
×
574
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
575
      } else {
576
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
577
      }
578
    }
579
  }
580
  return 0;
9✔
581
}
582

583
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
10✔
584
  int         ret;
585
  SCtbIdxKey *pCtbIdxKey;
586

587
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
10✔
588
  if (ret < 0) {
10✔
589
    return 0;
9✔
590
  }
591

592
  pCtbIdxKey = pCtbCur->pKey;
1✔
593
  if (pCtbIdxKey->suid > pCtbCur->suid) {
1!
594
    return 0;
×
595
  }
596

597
  return pCtbIdxKey->uid;
1✔
598
}
599

600
struct SMStbCursor {
601
  SMeta   *pMeta;
602
  TBC     *pCur;
603
  tb_uid_t suid;
604
  void    *pKey;
605
  void    *pVal;
606
  int      kLen;
607
  int      vLen;
608
};
609

610
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
14✔
611
  SMStbCursor *pStbCur = NULL;
14✔
612
  int          ret = 0;
14✔
613
  int          c = 0;
14✔
614

615
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
14!
616
  if (pStbCur == NULL) {
14!
617
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
618
    return NULL;
×
619
  }
620

621
  pStbCur->pMeta = pMeta;
14✔
622
  pStbCur->suid = suid;
14✔
623
  metaRLock(pMeta);
14✔
624

625
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
14✔
626
  if (ret < 0) {
14!
627
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
628
    metaULock(pMeta);
×
629
    taosMemoryFree(pStbCur);
×
630
    return NULL;
×
631
  }
632

633
  // move to the suid
634
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
14✔
635
  if (c > 0) {
14!
636
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
637
  }
638

639
  return pStbCur;
14✔
640
}
641

642
void metaCloseStbCursor(SMStbCursor *pStbCur) {
14✔
643
  if (pStbCur) {
14!
644
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
14!
645
    if (pStbCur->pCur) {
14!
646
      tdbTbcClose(pStbCur->pCur);
14✔
647

648
      tdbFree(pStbCur->pKey);
14✔
649
      tdbFree(pStbCur->pVal);
14✔
650
    }
651

652
    taosMemoryFree(pStbCur);
14!
653
  }
654
}
14✔
655

656
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
23✔
657
  int ret;
658

659
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
23✔
660
  if (ret < 0) {
23✔
661
    return 0;
14✔
662
  }
663
  return *(tb_uid_t *)pStbCur->pKey;
9✔
664
}
665

666
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
37✔
667
  STSchema       *pTSchema = NULL;
37✔
668
  SSchemaWrapper *pSW = NULL;
37✔
669

670
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
37✔
671
  if (!pSW) return NULL;
37!
672

673
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
37✔
674

675
  taosMemoryFree(pSW->pSchema);
37!
676
  taosMemoryFree(pSW);
37!
677
  return pTSchema;
37✔
678
}
679

680
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
×
681
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
×
682
  if (*ppTSchema == NULL) {
×
683
    return terrno;
×
684
  }
685
  return TSDB_CODE_SUCCESS;
×
686
}
687

688
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
37✔
689
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
37✔
690
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
37!
691
    return terrno;
×
692
  }
693
  return TSDB_CODE_SUCCESS;
37✔
694
}
695

696
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
6✔
697
  int32_t code = 0;
6✔
698
  int32_t lino;
699

700
  void     *pData = NULL;
6✔
701
  int       nData = 0;
6✔
702
  SSkmDbKey skmDbKey;
703
  if (sver <= 0) {
6✔
704
    SMetaInfo info;
705
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
5!
706
      sver = info.skmVer;
5✔
707
    } else {
708
      TBC *pSkmDbC = NULL;
×
709
      int  c;
710

711
      skmDbKey.uid = suid ? suid : uid;
×
712
      skmDbKey.sver = INT32_MAX;
×
713

714
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
×
715
      TSDB_CHECK_CODE(code, lino, _exit);
×
716
      metaRLock(pMeta);
×
717

718
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
×
719
        metaULock(pMeta);
×
720
        tdbTbcClose(pSkmDbC);
×
721
        code = TSDB_CODE_NOT_FOUND;
×
722
        goto _exit;
×
723
      }
724

725
      if (c == 0) {
×
726
        metaULock(pMeta);
×
727
        tdbTbcClose(pSkmDbC);
×
728
        code = TSDB_CODE_FAILED;
×
729
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
730
        goto _exit;
×
731
      }
732

733
      if (c < 0) {
×
734
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
735
      }
736

737
      const void *pKey = NULL;
×
738
      int32_t     nKey = 0;
×
739
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
×
740

741
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
×
742
        metaULock(pMeta);
×
743
        tdbTbcClose(pSkmDbC);
×
744
        code = TSDB_CODE_NOT_FOUND;
×
745
        goto _exit;
×
746
      }
747

748
      sver = ((SSkmDbKey *)pKey)->sver;
×
749

750
      metaULock(pMeta);
×
751
      tdbTbcClose(pSkmDbC);
×
752
    }
753
  }
754

755
  if (!(sver > 0)) {
6!
756
    code = TSDB_CODE_NOT_FOUND;
×
757
    goto _exit;
×
758
  }
759

760
  skmDbKey.uid = suid ? suid : uid;
6!
761
  skmDbKey.sver = sver;
6✔
762
  metaRLock(pMeta);
6✔
763
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
6!
764
    metaULock(pMeta);
×
765
    code = TSDB_CODE_NOT_FOUND;
×
766
    goto _exit;
×
767
  }
768
  metaULock(pMeta);
6✔
769

770
  // decode
771
  SDecoder        dc = {0};
6✔
772
  SSchemaWrapper  schema;
773
  SSchemaWrapper *pSchemaWrapper = &schema;
6✔
774

775
  tDecoderInit(&dc, pData, nData);
6✔
776
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
6✔
777
  tDecoderClear(&dc);
6✔
778
  tdbFree(pData);
6✔
779
  if (TSDB_CODE_SUCCESS != code) {
6!
780
    taosMemoryFree(pSchemaWrapper->pSchema);
×
781
    goto _exit;
×
782
  }
783

784
  // convert
785
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
6✔
786
  if (pTSchema == NULL) {
6!
787
    code = TSDB_CODE_OUT_OF_MEMORY;
×
788
  }
789

790
  *ppTSchema = pTSchema;
6✔
791
  taosMemoryFree(pSchemaWrapper->pSchema);
6!
792

793
_exit:
6✔
794
  return code;
6✔
795
}
796

797
// N.B. Called by statusReq per second
798
int64_t metaGetTbNum(SMeta *pMeta) {
8✔
799
  // num of child tables (excluding normal tables , stables and others)
800

801
  /* int64_t num = 0; */
802
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
803

804
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
8✔
805
}
806

807
void metaUpdTimeSeriesNum(SMeta *pMeta) {
14✔
808
  int64_t nCtbTimeSeries = 0;
14✔
809
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
14!
810
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
14✔
811
  }
812
}
14✔
813

814
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
815
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
816
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
13✔
817
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
13✔
818
    metaUpdTimeSeriesNum(pMeta);
10✔
819
  }
820

821
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
13✔
822
}
823

824
// type: 1 reported timeseries
825
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
13!
826
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
13✔
827
  if (type == 1) {
13✔
828
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
8✔
829
  }
830
  return nTimeSeries;
13✔
831
}
832

833
typedef struct {
834
  SMeta   *pMeta;
835
  TBC     *pCur;
836
  tb_uid_t uid;
837
  void    *pKey;
838
  void    *pVal;
839
  int      kLen;
840
  int      vLen;
841
} SMSmaCursor;
842

843
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
844
  SMSmaCursor *pSmaCur = NULL;
×
845
  SSmaIdxKey   smaIdxKey;
846
  int          ret;
847
  int          c;
848

849
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
850
  if (pSmaCur == NULL) {
×
851
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
852
    return NULL;
×
853
  }
854

855
  pSmaCur->pMeta = pMeta;
×
856
  pSmaCur->uid = uid;
×
857
  metaRLock(pMeta);
×
858

859
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
860
  if (ret < 0) {
×
861
    metaULock(pMeta);
×
862
    taosMemoryFree(pSmaCur);
×
863
    return NULL;
×
864
  }
865

866
  // move to the suid
867
  smaIdxKey.uid = uid;
×
868
  smaIdxKey.smaUid = INT64_MIN;
×
869
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
870
  if (c > 0) {
×
871
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
872
  }
873

874
  return pSmaCur;
×
875
}
876

877
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
878
  if (pSmaCur) {
×
879
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
880
    if (pSmaCur->pCur) {
×
881
      tdbTbcClose(pSmaCur->pCur);
×
882
      pSmaCur->pCur = NULL;
×
883

884
      tdbFree(pSmaCur->pKey);
×
885
      tdbFree(pSmaCur->pVal);
×
886
    }
887

888
    taosMemoryFree(pSmaCur);
×
889
  }
890
}
×
891

892
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
893
  int         ret;
894
  SSmaIdxKey *pSmaIdxKey;
895

896
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
897
  if (ret < 0) {
×
898
    return 0;
×
899
  }
900

901
  pSmaIdxKey = pSmaCur->pKey;
×
902
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
903
    return 0;
×
904
  }
905

906
  return pSmaIdxKey->uid;
×
907
}
908

909
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
910
  STSmaWrapper *pSW = NULL;
×
911
  SArray       *pSmaIds = NULL;
×
912

913
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
914
    return NULL;
×
915
  }
916

917
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
918
  if (!pSW) {
×
919
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
920
    goto _err;
×
921
  }
922

923
  pSW->number = taosArrayGetSize(pSmaIds);
×
924
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
925

926
  if (!pSW->tSma) {
×
927
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
928
    goto _err;
×
929
  }
930

931
  SMetaReader mr = {0};
×
932
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
933
  int64_t smaId;
934
  int     smaIdx = 0;
×
935
  STSma  *pTSma = NULL;
×
936
  for (int i = 0; i < pSW->number; ++i) {
×
937
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
938
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
939
      tDecoderClear(&mr.coder);
×
940
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
941
      continue;
×
942
    }
943
    tDecoderClear(&mr.coder);
×
944
    pTSma = pSW->tSma + smaIdx;
×
945
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
946
    if (deepCopy) {
×
947
      if (pTSma->exprLen > 0) {
×
948
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
949
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
950
          goto _err;
×
951
        }
952
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
953
      }
954
      if (pTSma->tagsFilterLen > 0) {
×
955
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
956
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
957
          goto _err;
×
958
        }
959
      }
960
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
961
    } else {
962
      pTSma->exprLen = 0;
×
963
      pTSma->expr = NULL;
×
964
      pTSma->tagsFilterLen = 0;
×
965
      pTSma->tagsFilter = NULL;
×
966
    }
967

968
    ++smaIdx;
×
969
  }
970

971
  if (smaIdx <= 0) goto _err;
×
972
  pSW->number = smaIdx;
×
973

974
  metaReaderClear(&mr);
×
975
  taosArrayDestroy(pSmaIds);
×
976
  return pSW;
×
977
_err:
×
978
  metaReaderClear(&mr);
×
979
  taosArrayDestroy(pSmaIds);
×
980
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
981
  return NULL;
×
982
}
983

984
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
×
985
  STSma      *pTSma = NULL;
×
986
  SMetaReader mr = {0};
×
987
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
988
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
×
989
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
990
    metaReaderClear(&mr);
×
991
    return NULL;
×
992
  }
993
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
×
994
  if (!pTSma) {
×
995
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
996
    metaReaderClear(&mr);
×
997
    return NULL;
×
998
  }
999

1000
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1001

1002
  metaReaderClear(&mr);
×
1003
  return pTSma;
×
1004
}
1005

1006
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1007
  SArray     *pUids = NULL;
×
1008
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1009

1010
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1011
  if (!pCur) {
×
1012
    return NULL;
×
1013
  }
1014

1015
  while (1) {
×
1016
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1017
    if (id == 0) {
×
1018
      break;
×
1019
    }
1020

1021
    if (!pUids) {
×
1022
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1023
      if (!pUids) {
×
1024
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1025
        metaCloseSmaCursor(pCur);
×
1026
        return NULL;
×
1027
      }
1028
    }
1029

1030
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1031

1032
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1033
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1034
      metaCloseSmaCursor(pCur);
×
1035
      taosArrayDestroy(pUids);
×
1036
      return NULL;
×
1037
    }
1038
  }
1039

1040
  metaCloseSmaCursor(pCur);
×
1041
  return pUids;
×
1042
}
1043

1044
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1045
  SArray     *pUids = NULL;
×
1046
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1047
  tb_uid_t    lastUid = 0;
×
1048

1049
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1050
  if (!pCur) {
×
1051
    return NULL;
×
1052
  }
1053

1054
  while (1) {
×
1055
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1056
    if (uid == 0) {
×
1057
      break;
×
1058
    }
1059

1060
    if (lastUid == uid) {
×
1061
      continue;
×
1062
    }
1063

1064
    lastUid = uid;
×
1065

1066
    if (!pUids) {
×
1067
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1068
      if (!pUids) {
×
1069
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1070
        metaCloseSmaCursor(pCur);
×
1071
        return NULL;
×
1072
      }
1073
    }
1074

1075
    if (!taosArrayPush(pUids, &uid)) {
×
1076
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1077
      metaCloseSmaCursor(pCur);
×
1078
      taosArrayDestroy(pUids);
×
1079
      return NULL;
×
1080
    }
1081
  }
1082

1083
  metaCloseSmaCursor(pCur);
×
1084
  return pUids;
×
1085
}
1086

1087
#endif
1088

1089
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
×
1090
  STag *tag = (STag *)pTag;
×
1091
  if (type == TSDB_DATA_TYPE_JSON) {
×
1092
    return tag;
×
1093
  }
1094
  bool find = tTagGet(tag, val);
×
1095

1096
  if (!find) {
×
1097
    return NULL;
×
1098
  }
1099

1100
  return val;
×
1101
}
1102

1103
typedef struct {
1104
  SMeta   *pMeta;
1105
  TBC     *pCur;
1106
  tb_uid_t suid;
1107
  int16_t  cid;
1108
  int16_t  type;
1109
  void    *pKey;
1110
  void    *pVal;
1111
  int32_t  kLen;
1112
  int32_t  vLen;
1113
} SIdxCursor;
1114

1115
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1116
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1117
  SMetaFltParam *param = arg;
×
1118
  int32_t        ret = 0;
×
1119

1120
  SIdxCursor *pCursor = NULL;
×
1121
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1122
  if (pCursor == NULL) {
×
1123
    return terrno;
×
1124
  }
1125
  pCursor->pMeta = pMeta;
×
1126
  pCursor->suid = param->suid;
×
1127
  pCursor->cid = param->cid;
×
1128
  pCursor->type = param->type;
×
1129

1130
  metaRLock(pMeta);
×
1131
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
×
1132
  if (ret != 0) {
×
1133
    goto END;
×
1134
  }
1135
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
×
1136

1137
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
×
1138
  SBtimeIdxKey *pBtimeKey = &btimeKey;
×
1139

1140
  int cmp = 0;
×
1141
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
×
1142
    goto END;
×
1143
  }
1144

1145
  int32_t valid = 0;
×
1146
  int32_t count = 0;
×
1147

1148
  static const int8_t TRY_ERROR_LIMIT = 1;
1149
  do {
×
1150
    void   *entryKey = NULL;
×
1151
    int32_t nEntryKey = -1;
×
1152
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
×
1153
    if (valid < 0) break;
×
1154

1155
    SBtimeIdxKey *p = entryKey;
×
1156
    if (count > TRY_ERROR_LIMIT) break;
×
1157

1158
    terrno = TSDB_CODE_SUCCESS;
×
1159
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
×
1160
    if (terrno != TSDB_CODE_SUCCESS) {
×
1161
      ret = terrno;
×
1162
      break;
×
1163
    }
1164
    if (cmp == 0) {
×
1165
      if (taosArrayPush(pUids, &p->uid) == NULL) {
×
1166
        ret = terrno;
×
1167
        break;
×
1168
      }
1169
    } else {
1170
      if (param->equal == true) {
×
1171
        if (count > TRY_ERROR_LIMIT) break;
×
1172
        count++;
×
1173
      }
1174
    }
1175
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1176
    if (valid < 0) break;
×
1177
  } while (1);
1178

1179
END:
×
1180
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1181
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1182
  taosMemoryFree(pCursor);
×
1183
  return ret;
×
1184
}
1185

1186
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1187
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1188
  SMetaFltParam *param = arg;
×
1189
  int32_t        ret = 0;
×
1190
  char          *buf = NULL;
×
1191

1192
  STagIdxKey *pKey = NULL;
×
1193
  int32_t     nKey = 0;
×
1194

1195
  SIdxCursor *pCursor = NULL;
×
1196
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1197
  if (pCursor == NULL) {
×
1198
    return terrno;
×
1199
  }
1200
  pCursor->pMeta = pMeta;
×
1201
  pCursor->suid = param->suid;
×
1202
  pCursor->cid = param->cid;
×
1203
  pCursor->type = param->type;
×
1204

1205
  char *pName = param->val;
×
1206

1207
  metaRLock(pMeta);
×
1208
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1209
  if (ret != 0) {
×
1210
    goto END;
×
1211
  }
1212

1213
  int cmp = 0;
×
1214
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1215
    goto END;
×
1216
  }
1217
  int32_t valid = 0;
×
1218
  int32_t count = 0;
×
1219

1220
  int32_t TRY_ERROR_LIMIT = 1;
×
1221
  do {
×
1222
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1223
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1224
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1225
    if (valid < 0) break;
×
1226

1227
    if (count > TRY_ERROR_LIMIT) break;
×
1228

1229
    char *pTableKey = (char *)pEntryKey;
×
1230
    terrno = TSDB_CODE_SUCCESS;
×
1231
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1232
    if (terrno != TSDB_CODE_SUCCESS) {
×
1233
      ret = terrno;
×
1234
      goto END;
×
1235
    }
1236
    if (cmp == 0) {
×
1237
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1238
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1239
        ret = terrno;
×
1240
        goto END;
×
1241
      }
1242
    } else {
1243
      if (param->equal == true) {
×
1244
        if (count > TRY_ERROR_LIMIT) break;
×
1245
        count++;
×
1246
      }
1247
    }
1248
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1249
    if (valid < 0) {
×
1250
      break;
×
1251
    }
1252
  } while (1);
1253

1254
END:
×
1255
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1256
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1257
  taosMemoryFree(buf);
×
1258
  taosMemoryFree(pKey);
×
1259

1260
  taosMemoryFree(pCursor);
×
1261

1262
  return ret;
×
1263
}
1264
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1265
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1266
  SMetaFltParam *param = arg;
×
1267
  int32_t        ret = 0;
×
1268
  char          *buf = NULL;
×
1269

1270
  STtlIdxKey *pKey = NULL;
×
1271
  int32_t     nKey = 0;
×
1272

1273
  SIdxCursor *pCursor = NULL;
×
1274
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1275
  if (pCursor == NULL) {
×
1276
    return terrno;
×
1277
  }
1278
  pCursor->pMeta = pMeta;
×
1279
  pCursor->suid = param->suid;
×
1280
  pCursor->cid = param->cid;
×
1281
  pCursor->type = param->type;
×
1282

1283
  metaRLock(pMeta);
×
1284
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1285

1286
END:
×
1287
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1288
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1289
  taosMemoryFree(buf);
×
1290
  taosMemoryFree(pKey);
×
1291

1292
  taosMemoryFree(pCursor);
×
1293

1294
  return ret;
×
1295
  // impl later
1296
  return 0;
1297
}
1298
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1299
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1300
  SMetaFltParam *param = arg;
×
1301

1302
  SMetaEntry oStbEntry = {0};
×
1303
  int32_t    code = 0;
×
1304
  char      *buf = NULL;
×
1305
  void      *pData = NULL;
×
1306
  int        nData = 0;
×
1307

1308
  SDecoder    dc = {0};
×
1309
  STbDbKey    tbDbKey = {0};
×
1310
  STagIdxKey *pKey = NULL;
×
1311
  int32_t     nKey = 0;
×
1312

1313
  SIdxCursor *pCursor = NULL;
×
1314
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1315
  if (!pCursor) {
×
1316
    return terrno;
×
1317
  }
1318
  pCursor->pMeta = pMeta;
×
1319
  pCursor->suid = param->suid;
×
1320
  pCursor->cid = param->cid;
×
1321
  pCursor->type = param->type;
×
1322

1323
  metaRLock(pMeta);
×
1324

1325
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
×
1326

1327
  tbDbKey.uid = param->suid;
×
1328
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
×
1329

1330
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
×
1331

1332
  tDecoderInit(&dc, pData, nData);
×
1333

1334
  code = metaDecodeEntry(&dc, &oStbEntry);
×
1335
  if (code) {
×
1336
    tDecoderClear(&dc);
×
1337
    goto END;
×
1338
  }
1339

1340
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
×
1341
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1342
  }
1343

1344
  code = TSDB_CODE_INVALID_PARA;
×
1345
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
×
1346
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
×
1347
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
×
1348
      code = 0;
×
1349
    } else {
1350
      TAOS_CHECK_GOTO(code, NULL, END);
×
1351
    }
1352
  }
1353

1354
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
×
1355
  if (code != 0) {
×
1356
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1357
  }
1358

1359
  int32_t maxSize = 0;
×
1360
  int32_t nTagData = 0;
×
1361
  void   *tagData = NULL;
×
1362

1363
  if (param->val == NULL) {
×
1364
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1365
    goto END;
×
1366
  } else {
1367
    if (IS_VAR_DATA_TYPE(param->type)) {
×
1368
      tagData = varDataVal(param->val);
×
1369
      nTagData = varDataLen(param->val);
×
1370

1371
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
×
1372
        maxSize = 4 * nTagData + 1;
×
1373
        buf = taosMemoryCalloc(1, maxSize);
×
1374
        if (buf == NULL) {
×
1375
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1376
        }
1377

1378
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
×
1379
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1380
        }
1381

1382
        tagData = buf;
×
1383
        nTagData = maxSize;
×
1384
      }
1385
    } else {
1386
      tagData = param->val;
×
1387
      nTagData = tDataTypes[param->type].bytes;
×
1388
    }
1389
  }
1390

1391
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
×
1392
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1393
                  NULL, END);
1394

1395
  int cmp = 0;
×
1396
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
×
1397

1398
  int     count = 0;
×
1399
  int32_t valid = 0;
×
1400
  bool    found = false;
×
1401

1402
  static const int8_t TRY_ERROR_LIMIT = 1;
1403

1404
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1405
  /// target:                        [suid, cid2, type2]
1406
  int diffCidCount = 0;
×
1407
  do {
×
1408
    void   *entryKey = NULL, *entryVal = NULL;
×
1409
    int32_t nEntryKey, nEntryVal;
1410

1411
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
×
1412
    if (valid < 0) {
×
1413
      code = valid;
×
1414
    }
1415
    if (count > TRY_ERROR_LIMIT) {
×
1416
      break;
×
1417
    }
1418

1419
    STagIdxKey *p = entryKey;
×
1420
    if (p == NULL) break;
×
1421

1422
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
×
1423
      if (found == true) break;  //
×
1424
      if (diffCidCount > TRY_ERROR_LIMIT) break;
×
1425
      diffCidCount++;
×
1426
      count++;
×
1427
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1428
      if (valid < 0) {
×
1429
        code = valid;
×
1430
        break;
×
1431
      } else {
1432
        continue;
×
1433
      }
1434
    }
1435

1436
    terrno = TSDB_CODE_SUCCESS;
×
1437
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
×
1438
    if (terrno != TSDB_CODE_SUCCESS) {
×
1439
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1440
      break;
×
1441
    }
1442
    if (cmp == 0) {
×
1443
      // match
1444
      tb_uid_t tuid = 0;
×
1445
      if (IS_VAR_DATA_TYPE(pKey->type)) {
×
1446
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
×
1447
      } else {
1448
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
×
1449
      }
1450
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1451
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1452
      }
1453
      found = true;
×
1454
    } else {
1455
      if (param->equal == true) {
×
1456
        if (count > TRY_ERROR_LIMIT) break;
×
1457
        count++;
×
1458
      }
1459
    }
1460
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1461
    if (valid < 0) {
×
1462
      code = valid;
×
1463
      break;
×
1464
    }
1465
  } while (1);
1466

1467
END:
×
1468
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1469
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1470
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
1471
  taosMemoryFreeClear(oStbEntry.pExtSchemas);
×
1472
  tDecoderClear(&dc);
×
1473
  tdbFree(pData);
×
1474

1475
  taosMemoryFree(buf);
×
1476
  taosMemoryFree(pKey);
×
1477

1478
  taosMemoryFree(pCursor);
×
1479

1480
  return code;
×
1481
}
1482

1483
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
×
1484
  int ret = 0;
×
1485
  if (lock) {
×
1486
    metaRLock(pMeta);
×
1487
  }
1488

1489
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
×
1490
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
×
1491
  if (lock) {
×
1492
    metaULock(pMeta);
×
1493
  }
1494

1495
  return ret;
×
1496
}
1497

1498
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
×
1499
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
×
1500
  const int32_t LIMIT = 128;
×
1501

1502
  int32_t isLock = false;
×
1503
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
×
1504
  for (int i = 0; i < sz; i++) {
×
1505
    STUidTagInfo *p = taosArrayGet(uidList, i);
×
1506

1507
    if (i % LIMIT == 0) {
×
1508
      if (isLock) metaULock(pMeta);
×
1509

1510
      metaRLock(pMeta);
×
1511
      isLock = true;
×
1512
    }
1513

1514
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1515
    void   *val = NULL;
×
1516
    int32_t len = 0;
×
1517
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
×
1518
      p->pTagVal = taosMemoryMalloc(len);
×
1519
      if (!p->pTagVal) {
×
1520
        if (isLock) metaULock(pMeta);
×
1521

1522
        TAOS_RETURN(terrno);
×
1523
      }
1524
      memcpy(p->pTagVal, val, len);
×
1525
      tdbFree(val);
×
1526
    } else {
1527
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid,
×
1528
                p->uid);
1529
    }
1530
  }
1531
  //  }
1532
  if (isLock) metaULock(pMeta);
×
1533
  return 0;
×
1534
}
1535

1536
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
×
1537
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
×
1538
  if (!pCur) {
×
1539
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1540
  }
1541

1542
  // If len > 0 means there already have uids, and we only want the
1543
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1544
  // in the hash map, that may require a lot of memory
1545
  SHashObj *pSepecifiedUidMap = NULL;
×
1546
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
×
1547
  if (numOfElems > 0) {
×
1548
    pSepecifiedUidMap =
1549
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1550
    for (int i = 0; i < numOfElems; i++) {
×
1551
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
×
1552
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
×
1553
      if (code) {
×
1554
        metaCloseCtbCursor(pCur);
×
1555
        taosHashCleanup(pSepecifiedUidMap);
×
1556
        return code;
×
1557
      }
1558
    }
1559
  }
1560

1561
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
×
1562
    while (1) {
×
1563
      tb_uid_t uid = metaCtbCursorNext(pCur);
×
1564
      if (uid == 0) {
×
1565
        break;
×
1566
      }
1567

1568
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
×
1569
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
×
1570
      if (!info.pTagVal) {
×
1571
        metaCloseCtbCursor(pCur);
×
1572
        taosHashCleanup(pSepecifiedUidMap);
×
1573
        return terrno;
×
1574
      }
1575
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
×
1576
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
×
1577
        taosMemoryFreeClear(info.pTagVal);
×
1578
        metaCloseCtbCursor(pCur);
×
1579
        taosHashCleanup(pSepecifiedUidMap);
×
1580
        return terrno;
×
1581
      }
1582
    }
1583
  } else {  // only the specified tables need to be added
1584
    while (1) {
×
1585
      tb_uid_t uid = metaCtbCursorNext(pCur);
×
1586
      if (uid == 0) {
×
1587
        break;
×
1588
      }
1589

1590
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
×
1591
      if (index == NULL) {
×
1592
        continue;
×
1593
      }
1594

1595
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
×
1596
      if (pTagInfo->pTagVal == NULL) {
×
1597
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
×
1598
        if (!pTagInfo->pTagVal) {
×
1599
          metaCloseCtbCursor(pCur);
×
1600
          taosHashCleanup(pSepecifiedUidMap);
×
1601
          return terrno;
×
1602
        }
1603
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
×
1604
      }
1605
    }
1606
  }
1607

1608
  taosHashCleanup(pSepecifiedUidMap);
×
1609
  metaCloseCtbCursor(pCur);
×
1610
  return TSDB_CODE_SUCCESS;
×
1611
}
1612

1613
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1614

1615
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
156✔
1616
  int32_t code = 0;
156✔
1617
  void   *pData = NULL;
156✔
1618
  int     nData = 0;
156✔
1619
  int     lock = 0;
156✔
1620

1621
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
156!
1622
    lock = 1;
80✔
1623
  }
1624

1625
  if (!lock) metaRLock(pMeta);
156✔
1626

1627
  // search cache
1628
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
156✔
1629
    if (!lock) metaULock(pMeta);
117✔
1630
    goto _exit;
117✔
1631
  }
1632

1633
  // search TDB
1634
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
39!
1635
    // not found
1636
    if (!lock) metaULock(pMeta);
39!
1637
    goto _exit;
39✔
1638
  }
1639

1640
  if (!lock) metaULock(pMeta);
×
1641

1642
  pInfo->uid = uid;
×
1643
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
×
1644
  pInfo->version = ((SUidIdxVal *)pData)->version;
×
1645
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
×
1646

1647
  if (lock) {
×
1648
    metaULock(pReader->pMeta);
×
1649
    // metaReaderReleaseLock(pReader);
1650
  }
1651
  // upsert the cache
1652
  metaWLock(pMeta);
×
1653
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
×
1654
  if (ret != 0) {
×
1655
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1656
  }
1657
  metaULock(pMeta);
×
1658

1659
  if (lock) {
×
1660
    metaRLock(pReader->pMeta);
×
1661
  }
1662

1663
_exit:
×
1664
  tdbFree(pData);
156✔
1665
  return code;
156✔
1666
}
1667

1668
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
16✔
1669
  int32_t code = 0;
16✔
1670

1671
  if (!numOfTables && !numOfCols) goto _exit;
16!
1672

1673
  SVnode *pVnodeObj = pVnode;
16✔
1674
  metaRLock(pVnodeObj->pMeta);
16✔
1675

1676
  // fast path: search cache
1677
  SMetaStbStats state = {0};
16✔
1678
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
16✔
1679
    metaULock(pVnodeObj->pMeta);
7✔
1680
    if (numOfTables) *numOfTables = state.ctbNum;
7!
1681
    if (numOfCols) *numOfCols = state.colNum;
7✔
1682
    goto _exit;
7✔
1683
  }
1684

1685
  // slow path: search TDB
1686
  int64_t ctbNum = 0;
9✔
1687
  int32_t colNum = 0;
9✔
1688
  int64_t keep = 0;
9✔
1689
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
9✔
1690
  if (TSDB_CODE_SUCCESS == code) {
9!
1691
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
9✔
1692
  }
1693
  if (TSDB_CODE_SUCCESS == code) {
9!
1694
    code = vnodeGetStbKeep(pVnode, uid, &keep);
9✔
1695
  }
1696
  metaULock(pVnodeObj->pMeta);
9✔
1697
  if (TSDB_CODE_SUCCESS != code) {
9!
1698
    goto _exit;
×
1699
  }
1700

1701
  if (numOfTables) *numOfTables = ctbNum;
9✔
1702
  if (numOfCols) *numOfCols = colNum;
9✔
1703

1704
  state.uid = uid;
9✔
1705
  state.ctbNum = ctbNum;
9✔
1706
  state.colNum = colNum;
9✔
1707
  state.keep = keep;
9✔
1708
  // upsert the cache
1709
  metaWLock(pVnodeObj->pMeta);
9✔
1710

1711
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
9✔
1712
  if (ret) {
9!
1713
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64, uid, ctbNum,
×
1714
              colNum, keep);
1715
  }
1716

1717
  metaULock(pVnodeObj->pMeta);
9✔
1718

1719
_exit:
16✔
1720
  return code;
16✔
1721
}
1722

1723
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
17✔
1724
  SMetaStbStats stats = {0};
17✔
1725

1726
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
17✔
1727
    stats.ctbNum += deltaCtb;
4✔
1728
    stats.colNum += deltaCol;
4✔
1729
    if (deltaKeep > 0) {
4!
1730
      stats.keep = deltaKeep;
×
1731
    }
1732

1733
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
4✔
1734
    if (code) {
4!
1735
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1736
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1737
    }
1738
  }
1739
}
17✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc