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

taosdata / TDengine / #3613

14 Feb 2025 09:14AM UTC coverage: 63.499% (-0.01%) from 63.513%
#3613

push

travis-ci

web-flow
Merge pull request #29781 from taosdata/doc/internal

docs: minor changes

141396 of 286269 branches covered (49.39%)

Branch coverage included in aggregate %.

220278 of 283307 relevant lines covered (77.75%)

19138445.45 hits per line

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

55.69
/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) {
11,493,375✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
11,493,375✔
22
  metaReaderDoInit(pReader, pMeta, flags);
11,493,375✔
23
  pReader->pAPI = pAPI;
11,500,783✔
24
}
11,500,783✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
15,447,479✔
27
  memset(pReader, 0, sizeof(*pReader));
15,447,479✔
28
  pReader->pMeta = pMeta;
15,447,479✔
29
  pReader->flags = flags;
15,447,479✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
15,447,479!
31
    metaRLock(pMeta);
10,413,308✔
32
  }
33
}
15,450,583✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,100,011✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,100,011!
37
    metaULock(pReader->pMeta);
2,100,277✔
38
    pReader->flags |= META_READER_NOLOCK;
2,100,874✔
39
  }
40
}
2,100,608✔
41

42
void metaReaderClear(SMetaReader *pReader) {
16,170,608✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
16,170,608✔
44
    metaULock(pReader->pMeta);
8,314,202✔
45
  }
46
  tDecoderClear(&pReader->coder);
16,170,138✔
47
  tdbFree(pReader->pBuf);
16,184,179✔
48
  pReader->pBuf = NULL;
16,184,888✔
49
}
16,184,888✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
25,252,953!
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);
25,262,917✔
63

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

71
  return 0;
25,054,806✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
685,438✔
79
    metaULock(pVnodeObj->pMeta);
1,775✔
80
    return false;
1,775✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
683,666✔
84
  return true;
683,674✔
85
}
86

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

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

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

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

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

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

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

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

121
  uid = *(tb_uid_t *)pReader->pBuf;
1,806,987✔
122
  return metaReaderGetTableEntryByUid(pReader, uid);
1,806,987✔
123
}
124

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

130
  metaRLock(pMeta);
155,636✔
131

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

137
  metaULock(pMeta);
155,668✔
138

139
  return uid;
155,670✔
140
}
141

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

152
  STR_TO_VARSTR(tbName, mr.me.name);
17,667✔
153
  metaReaderClear(&mr);
17,667✔
154

155
  return 0;
17,711✔
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) {
461,929✔
174
  int         code = 0;
461,929✔
175
  SMetaReader mr = {0};
461,929✔
176
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
461,929✔
177

178
  SMetaReader *pReader = &mr;
461,936✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
460,224✔
187

188
  metaReaderClear(&mr);
460,224✔
189

190
  return 0;
460,220✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
460,225✔
199
  if (code == 0) *tbType = mr.me.type;
460,219!
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
460,219✔
201
    *suid = mr.me.ctbEntry.suid;
460,198✔
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
21✔
203
    *suid = mr.me.uid;
11✔
204
  } else {
205
    *suid = 0;
10✔
206
  }
207

208
  metaReaderClear(&mr);
460,219✔
209
  return code;
460,223✔
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) {
928,791✔
245
  SMTbCursor *pTbCur = NULL;
928,791✔
246
  int32_t     code;
247

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

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,861,978✔
267
  if (pTbCur) {
1,861,978✔
268
    tdbFree(pTbCur->pKey);
931,498✔
269
    tdbFree(pTbCur->pVal);
931,425✔
270
    if (!pTbCur->paused) {
931,498✔
271
      metaReaderClear(&pTbCur->mr);
78,009✔
272
      if (pTbCur->pDbc) {
78,003!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
78,006✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
931,498!
277
  }
278
}
1,862,089✔
279

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

299
    if (first) {
930,259✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
929,443✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
930,133!
302
    } else {
303
      int c = 1;
816✔
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
816✔
305
      TSDB_CHECK_CODE(code, lino, _exit);
816!
306
      if (c == 0) {
816!
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
816!
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;
930,949✔
318
  }
319

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

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
11,728,170✔
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);
11,728,170✔
334
    if (ret < 0) {
11,739,597✔
335
      return ret;
931,451✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
10,808,146✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
10,820,889✔
341
    if (ret) return ret;
10,799,495!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
10,799,495✔
344
      continue;
2,439,274✔
345
    }
346

347
    break;
8,360,221✔
348
  }
349

350
  return 0;
8,360,221✔
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, int64_t *createTime) {
4,875,873✔
382
  void           *pData = NULL;
4,875,873✔
383
  int             nData = 0;
4,875,873✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
4,875,873✔
386
  SSchemaWrapper *pSchema = NULL;
4,875,873✔
387
  SDecoder        dc = {0};
4,875,873✔
388
  if (lock) {
4,875,873✔
389
    metaRLock(pMeta);
4,865,736✔
390
  }
391
_query:
4,897,864✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
4,977,637✔
393
    goto _err;
432✔
394
  }
395

396
  version = ((SUidIdxVal *)pData)[0].version;
4,974,998✔
397

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

402
  SMetaEntry me = {0};
4,977,149✔
403
  tDecoderInit(&dc, pData, nData);
4,977,149✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
4,972,025✔
405
  if (code) {
4,971,991!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
4,971,991✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,578,829!
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,577,430✔
412
      tDecoderClear(&dc);
4,577,430✔
413
      goto _exit;
4,582,847✔
414
    }
415
  } else if (me.type == TSDB_CHILD_TABLE) {
393,162✔
416
    uid = me.ctbEntry.suid;
79,755✔
417
    if (createTime != NULL){
79,755✔
418
      *createTime = me.ctbEntry.btime;
78,027✔
419
    }
420
    tDecoderClear(&dc);
79,755✔
421
    goto _query;
79,773✔
422
  } else {
423
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
313,407✔
424
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
313,449✔
425
      tDecoderClear(&dc);
313,449✔
426
      goto _exit;
313,451✔
427
    }
428
  }
429
  tDecoderClear(&dc);
×
430

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

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

443
_exit:
4,896,429✔
444
  if (lock) {
4,896,429✔
445
    metaULock(pMeta);
4,862,172✔
446
  }
447
  tdbFree(pData);
4,909,031✔
448
  return pSchema;
4,897,361✔
449

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

458
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
3,854,843✔
459
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
3,854,843✔
460
  SMCtbCursor *pCtbCur = NULL;
3,854,843✔
461
  SCtbIdxKey   ctbIdxKey;
462
  int          ret = 0;
3,854,843✔
463
  int          c = 0;
3,854,843✔
464

465
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,854,843!
466
  if (pCtbCur == NULL) {
3,868,526!
467
    return NULL;
×
468
  }
469

470
  pCtbCur->pMeta = pMeta;
3,868,526✔
471
  pCtbCur->suid = uid;
3,868,526✔
472
  pCtbCur->lock = lock;
3,868,526✔
473
  pCtbCur->paused = 1;
3,868,526✔
474

475
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,868,526✔
476
  if (ret < 0) {
3,863,741!
477
    return NULL;
×
478
  }
479
  return pCtbCur;
3,863,741✔
480
}
481

482
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,868,181✔
483
  if (pCtbCur) {
3,868,181!
484
    if (!pCtbCur->paused) {
3,868,582✔
485
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,825,655!
486
      if (pCtbCur->pCur) {
3,826,969!
487
        tdbTbcClose(pCtbCur->pCur);
3,827,013✔
488
      }
489
    }
490
    tdbFree(pCtbCur->pKey);
3,867,904✔
491
    tdbFree(pCtbCur->pVal);
3,867,589✔
492
  }
493
  taosMemoryFree(pCtbCur);
3,868,825!
494
}
3,869,702✔
495

496
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,223✔
497
  if (!pCtbCur->paused) {
43,223!
498
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,233✔
499
    if (pCtbCur->lock) {
43,256!
500
      metaULock(pCtbCur->pMeta);
43,259✔
501
    }
502
    pCtbCur->paused = 1;
43,268✔
503
  }
504
}
43,258✔
505

506
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,859,192✔
507
  if (pCtbCur->paused) {
3,859,192!
508
    pCtbCur->paused = 0;
3,863,062✔
509

510
    if (pCtbCur->lock) {
3,863,062✔
511
      metaRLock(pCtbCur->pMeta);
3,846,048✔
512
    }
513
    int ret = 0;
3,863,899✔
514
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,863,899✔
515
    if (ret < 0) {
3,865,389!
516
      metaCloseCtbCursor(pCtbCur);
×
517
      return -1;
×
518
    }
519

520
    if (first) {
3,866,445!
521
      SCtbIdxKey ctbIdxKey;
522
      // move to the suid
523
      ctbIdxKey.suid = pCtbCur->suid;
3,866,445✔
524
      ctbIdxKey.uid = INT64_MIN;
3,866,445✔
525
      int c = 0;
3,866,445✔
526
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,866,445✔
527
      if (c > 0) {
3,863,769✔
528
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
606,171✔
529
      }
530
    } else {
531
      int c = 0;
×
532
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
533
      if (c < 0) {
×
534
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
535
      } else {
536
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
537
      }
538
    }
539
  }
540
  return 0;
3,859,374✔
541
}
542

543
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
16,240,147✔
544
  int         ret;
545
  SCtbIdxKey *pCtbIdxKey;
546

547
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
16,240,147✔
548
  if (ret < 0) {
16,251,170✔
549
    return 0;
578,620✔
550
  }
551

552
  pCtbIdxKey = pCtbCur->pKey;
15,672,550✔
553
  if (pCtbIdxKey->suid > pCtbCur->suid) {
15,672,550✔
554
    return 0;
3,291,731✔
555
  }
556

557
  return pCtbIdxKey->uid;
12,380,819✔
558
}
559

560
struct SMStbCursor {
561
  SMeta   *pMeta;
562
  TBC     *pCur;
563
  tb_uid_t suid;
564
  void    *pKey;
565
  void    *pVal;
566
  int      kLen;
567
  int      vLen;
568
};
569

570
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
149,504✔
571
  SMStbCursor *pStbCur = NULL;
149,504✔
572
  int          ret = 0;
149,504✔
573
  int          c = 0;
149,504✔
574

575
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
149,504!
576
  if (pStbCur == NULL) {
149,505!
577
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
578
    return NULL;
×
579
  }
580

581
  pStbCur->pMeta = pMeta;
149,505✔
582
  pStbCur->suid = suid;
149,505✔
583
  metaRLock(pMeta);
149,505✔
584

585
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
149,505✔
586
  if (ret < 0) {
149,505!
587
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
588
    metaULock(pMeta);
×
589
    taosMemoryFree(pStbCur);
×
590
    return NULL;
×
591
  }
592

593
  // move to the suid
594
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
149,505✔
595
  if (c > 0) {
149,505!
596
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
597
  }
598

599
  return pStbCur;
149,505✔
600
}
601

602
void metaCloseStbCursor(SMStbCursor *pStbCur) {
149,505✔
603
  if (pStbCur) {
149,505!
604
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
149,505!
605
    if (pStbCur->pCur) {
149,505!
606
      tdbTbcClose(pStbCur->pCur);
149,505✔
607

608
      tdbFree(pStbCur->pKey);
149,505✔
609
      tdbFree(pStbCur->pVal);
149,505✔
610
    }
611

612
    taosMemoryFree(pStbCur);
149,505!
613
  }
614
}
149,505✔
615

616
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
244,827✔
617
  int ret;
618

619
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
244,827✔
620
  if (ret < 0) {
244,834✔
621
    return 0;
149,505✔
622
  }
623
  return *(tb_uid_t *)pStbCur->pKey;
95,329✔
624
}
625

626
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
4,733,894✔
627
  STSchema       *pTSchema = NULL;
4,733,894✔
628
  SSchemaWrapper *pSW = NULL;
4,733,894✔
629

630
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
4,733,894✔
631
  if (!pSW) return NULL;
4,749,008✔
632

633
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
4,749,007✔
634

635
  taosMemoryFree(pSW->pSchema);
4,751,480✔
636
  taosMemoryFree(pSW);
4,750,885!
637
  return pTSchema;
4,752,580✔
638
}
639

640
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
2,833✔
641
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
2,833✔
642
  if (*ppTSchema == NULL) {
2,836!
643
    return terrno;
×
644
  }
645
  return TSDB_CODE_SUCCESS;
2,836✔
646
}
647

648
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,732,736✔
649
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,732,736✔
650
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,748,828!
651
    return terrno;
26✔
652
  }
653
  return TSDB_CODE_SUCCESS;
4,748,802✔
654
}
655

656
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
447,359✔
657
  int32_t code = 0;
447,359✔
658
  int32_t lino;
659

660
  void     *pData = NULL;
447,359✔
661
  int       nData = 0;
447,359✔
662
  SSkmDbKey skmDbKey;
663
  if (sver <= 0) {
447,359✔
664
    SMetaInfo info;
665
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
216,560✔
666
      sver = info.skmVer;
216,554✔
667
    } else {
668
      TBC *pSkmDbC = NULL;
1✔
669
      int  c;
670

671
      skmDbKey.uid = suid ? suid : uid;
1!
672
      skmDbKey.sver = INT32_MAX;
1✔
673

674
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
1✔
675
      TSDB_CHECK_CODE(code, lino, _exit);
1!
676
      metaRLock(pMeta);
1✔
677

678
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
1!
679
        metaULock(pMeta);
×
680
        tdbTbcClose(pSkmDbC);
×
681
        code = TSDB_CODE_NOT_FOUND;
×
682
        goto _exit;
×
683
      }
684

685
      if (c == 0) {
1!
686
        metaULock(pMeta);
×
687
        tdbTbcClose(pSkmDbC);
×
688
        code = TSDB_CODE_FAILED;
×
689
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
690
        goto _exit;
×
691
      }
692

693
      if (c < 0) {
1!
694
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
695
      }
696

697
      const void *pKey = NULL;
1✔
698
      int32_t     nKey = 0;
1✔
699
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
1✔
700

701
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
1!
702
        metaULock(pMeta);
×
703
        tdbTbcClose(pSkmDbC);
×
704
        code = TSDB_CODE_NOT_FOUND;
×
705
        goto _exit;
×
706
      }
707

708
      sver = ((SSkmDbKey *)pKey)->sver;
1✔
709

710
      metaULock(pMeta);
1✔
711
      tdbTbcClose(pSkmDbC);
1✔
712
    }
713
  }
714

715
  if (!(sver > 0)) {
447,332!
716
    code = TSDB_CODE_NOT_FOUND;
×
717
    goto _exit;
×
718
  }
719

720
  skmDbKey.uid = suid ? suid : uid;
447,332✔
721
  skmDbKey.sver = sver;
447,332✔
722
  metaRLock(pMeta);
447,332✔
723
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
447,530!
724
    metaULock(pMeta);
×
725
    code = TSDB_CODE_NOT_FOUND;
×
726
    goto _exit;
×
727
  }
728
  metaULock(pMeta);
447,426✔
729

730
  // decode
731
  SDecoder        dc = {0};
447,471✔
732
  SSchemaWrapper  schema;
733
  SSchemaWrapper *pSchemaWrapper = &schema;
447,471✔
734

735
  tDecoderInit(&dc, pData, nData);
447,471✔
736
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
446,852✔
737
  tDecoderClear(&dc);
446,852✔
738
  tdbFree(pData);
447,335✔
739
  if (TSDB_CODE_SUCCESS != code) {
447,399!
740
    taosMemoryFree(pSchemaWrapper->pSchema);
×
741
    goto _exit;
×
742
  }
743

744
  // convert
745
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
447,399✔
746
  if (pTSchema == NULL) {
447,436!
747
    code = TSDB_CODE_OUT_OF_MEMORY;
×
748
  }
749

750
  *ppTSchema = pTSchema;
447,436✔
751
  taosMemoryFree(pSchemaWrapper->pSchema);
447,436✔
752

753
_exit:
447,512✔
754
  return code;
447,512✔
755
}
756

757
// N.B. Called by statusReq per second
758
int64_t metaGetTbNum(SMeta *pMeta) {
952,642✔
759
  // num of child tables (excluding normal tables , stables and others)
760

761
  /* int64_t num = 0; */
762
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
763

764
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
952,642✔
765
}
766

767
void metaUpdTimeSeriesNum(SMeta *pMeta) {
149,488✔
768
  int64_t nCtbTimeSeries = 0;
149,488✔
769
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
149,488!
770
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
149,488✔
771
  }
772
}
149,488✔
773

774
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
775
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
776
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
952,866✔
777
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
952,866✔
778
    metaUpdTimeSeriesNum(pMeta);
147,171✔
779
  }
780

781
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
952,866✔
782
}
783

784
// type: 1 reported timeseries
785
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
952,866!
786
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
952,866✔
787
  if (type == 1) {
952,866✔
788
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
952,642✔
789
  }
790
  return nTimeSeries;
952,866✔
791
}
792

793
typedef struct {
794
  SMeta   *pMeta;
795
  TBC     *pCur;
796
  tb_uid_t uid;
797
  void    *pKey;
798
  void    *pVal;
799
  int      kLen;
800
  int      vLen;
801
} SMSmaCursor;
802

803
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
804
  SMSmaCursor *pSmaCur = NULL;
×
805
  SSmaIdxKey   smaIdxKey;
806
  int          ret;
807
  int          c;
808

809
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
810
  if (pSmaCur == NULL) {
×
811
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
812
    return NULL;
×
813
  }
814

815
  pSmaCur->pMeta = pMeta;
×
816
  pSmaCur->uid = uid;
×
817
  metaRLock(pMeta);
×
818

819
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
820
  if (ret < 0) {
×
821
    metaULock(pMeta);
×
822
    taosMemoryFree(pSmaCur);
×
823
    return NULL;
×
824
  }
825

826
  // move to the suid
827
  smaIdxKey.uid = uid;
×
828
  smaIdxKey.smaUid = INT64_MIN;
×
829
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
830
  if (c > 0) {
×
831
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
832
  }
833

834
  return pSmaCur;
×
835
}
836

837
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
838
  if (pSmaCur) {
×
839
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
840
    if (pSmaCur->pCur) {
×
841
      tdbTbcClose(pSmaCur->pCur);
×
842
      pSmaCur->pCur = NULL;
×
843

844
      tdbFree(pSmaCur->pKey);
×
845
      tdbFree(pSmaCur->pVal);
×
846
    }
847

848
    taosMemoryFree(pSmaCur);
×
849
  }
850
}
×
851

852
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
853
  int         ret;
854
  SSmaIdxKey *pSmaIdxKey;
855

856
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
857
  if (ret < 0) {
×
858
    return 0;
×
859
  }
860

861
  pSmaIdxKey = pSmaCur->pKey;
×
862
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
863
    return 0;
×
864
  }
865

866
  return pSmaIdxKey->uid;
×
867
}
868

869
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
870
  STSmaWrapper *pSW = NULL;
×
871
  SArray       *pSmaIds = NULL;
×
872

873
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
874
    return NULL;
×
875
  }
876

877
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
878
  if (!pSW) {
×
879
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
880
    goto _err;
×
881
  }
882

883
  pSW->number = taosArrayGetSize(pSmaIds);
×
884
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
885

886
  if (!pSW->tSma) {
×
887
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
888
    goto _err;
×
889
  }
890

891
  SMetaReader mr = {0};
×
892
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
893
  int64_t smaId;
894
  int     smaIdx = 0;
×
895
  STSma  *pTSma = NULL;
×
896
  for (int i = 0; i < pSW->number; ++i) {
×
897
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
898
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
899
      tDecoderClear(&mr.coder);
×
900
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
901
      continue;
×
902
    }
903
    tDecoderClear(&mr.coder);
×
904
    pTSma = pSW->tSma + smaIdx;
×
905
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
906
    if (deepCopy) {
×
907
      if (pTSma->exprLen > 0) {
×
908
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
909
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
910
          goto _err;
×
911
        }
912
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
913
      }
914
      if (pTSma->tagsFilterLen > 0) {
×
915
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
916
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
917
          goto _err;
×
918
        }
919
      }
920
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
921
    } else {
922
      pTSma->exprLen = 0;
×
923
      pTSma->expr = NULL;
×
924
      pTSma->tagsFilterLen = 0;
×
925
      pTSma->tagsFilter = NULL;
×
926
    }
927

928
    ++smaIdx;
×
929
  }
930

931
  if (smaIdx <= 0) goto _err;
×
932
  pSW->number = smaIdx;
×
933

934
  metaReaderClear(&mr);
×
935
  taosArrayDestroy(pSmaIds);
×
936
  return pSW;
×
937
_err:
×
938
  metaReaderClear(&mr);
×
939
  taosArrayDestroy(pSmaIds);
×
940
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
941
  return NULL;
×
942
}
943

944
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
1✔
945
  STSma      *pTSma = NULL;
1✔
946
  SMetaReader mr = {0};
1✔
947
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
1✔
948
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
1!
949
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
950
    metaReaderClear(&mr);
×
951
    return NULL;
×
952
  }
953
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
1!
954
  if (!pTSma) {
1!
955
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
956
    metaReaderClear(&mr);
×
957
    return NULL;
×
958
  }
959

960
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
1✔
961

962
  metaReaderClear(&mr);
1✔
963
  return pTSma;
1✔
964
}
965

966
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
967
  SArray     *pUids = NULL;
×
968
  SSmaIdxKey *pSmaIdxKey = NULL;
×
969

970
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
971
  if (!pCur) {
×
972
    return NULL;
×
973
  }
974

975
  while (1) {
×
976
    tb_uid_t id = metaSmaCursorNext(pCur);
×
977
    if (id == 0) {
×
978
      break;
×
979
    }
980

981
    if (!pUids) {
×
982
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
983
      if (!pUids) {
×
984
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
985
        metaCloseSmaCursor(pCur);
×
986
        return NULL;
×
987
      }
988
    }
989

990
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
991

992
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
993
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
994
      metaCloseSmaCursor(pCur);
×
995
      taosArrayDestroy(pUids);
×
996
      return NULL;
×
997
    }
998
  }
999

1000
  metaCloseSmaCursor(pCur);
×
1001
  return pUids;
×
1002
}
1003

1004
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1005
  SArray     *pUids = NULL;
×
1006
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1007
  tb_uid_t    lastUid = 0;
×
1008

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

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

1020
    if (lastUid == uid) {
×
1021
      continue;
×
1022
    }
1023

1024
    lastUid = uid;
×
1025

1026
    if (!pUids) {
×
1027
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1028
      if (!pUids) {
×
1029
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1030
        metaCloseSmaCursor(pCur);
×
1031
        return NULL;
×
1032
      }
1033
    }
1034

1035
    if (!taosArrayPush(pUids, &uid)) {
×
1036
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1037
      metaCloseSmaCursor(pCur);
×
1038
      taosArrayDestroy(pUids);
×
1039
      return NULL;
×
1040
    }
1041
  }
1042

1043
  metaCloseSmaCursor(pCur);
×
1044
  return pUids;
×
1045
}
1046

1047
#endif
1048

1049
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
22,024,933✔
1050
  STag *tag = (STag *)pTag;
22,024,933✔
1051
  if (type == TSDB_DATA_TYPE_JSON) {
22,024,933✔
1052
    return tag;
10,027✔
1053
  }
1054
  bool find = tTagGet(tag, val);
22,014,906✔
1055

1056
  if (!find) {
22,050,157✔
1057
    return NULL;
128,672✔
1058
  }
1059

1060
  return val;
21,921,485✔
1061
}
1062

1063
typedef struct {
1064
  SMeta   *pMeta;
1065
  TBC     *pCur;
1066
  tb_uid_t suid;
1067
  int16_t  cid;
1068
  int16_t  type;
1069
  void    *pKey;
1070
  void    *pVal;
1071
  int32_t  kLen;
1072
  int32_t  vLen;
1073
} SIdxCursor;
1074

1075
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1076
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1077
  SMetaFltParam *param = arg;
6✔
1078
  int32_t        ret = 0;
6✔
1079

1080
  SIdxCursor *pCursor = NULL;
6✔
1081
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
6!
1082
  if (pCursor == NULL) {
6!
1083
    return terrno;
×
1084
  }
1085
  pCursor->pMeta = pMeta;
6✔
1086
  pCursor->suid = param->suid;
6✔
1087
  pCursor->cid = param->cid;
6✔
1088
  pCursor->type = param->type;
6✔
1089

1090
  metaRLock(pMeta);
6✔
1091
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
6✔
1092
  if (ret != 0) {
6!
1093
    goto END;
×
1094
  }
1095
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
6✔
1096

1097
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
6✔
1098
  SBtimeIdxKey *pBtimeKey = &btimeKey;
6✔
1099

1100
  int cmp = 0;
6✔
1101
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
6!
1102
    goto END;
×
1103
  }
1104

1105
  int32_t valid = 0;
6✔
1106
  int32_t count = 0;
6✔
1107

1108
  static const int8_t TRY_ERROR_LIMIT = 1;
1109
  do {
36,865✔
1110
    void   *entryKey = NULL;
36,871✔
1111
    int32_t nEntryKey = -1;
36,871✔
1112
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
36,871✔
1113
    if (valid < 0) break;
35,423✔
1114

1115
    SBtimeIdxKey *p = entryKey;
35,417✔
1116
    if (count > TRY_ERROR_LIMIT) break;
35,417!
1117

1118
    terrno = TSDB_CODE_SUCCESS;
35,417✔
1119
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
35,406✔
1120
    if (terrno != TSDB_CODE_SUCCESS) {
35,409!
1121
      ret = terrno;
×
1122
      break;
×
1123
    }
1124
    if (cmp == 0) {
35,398!
1125
      if (taosArrayPush(pUids, &p->uid) == NULL) {
72,276!
1126
        ret = terrno;
×
1127
        break;
×
1128
      }
1129
    } else {
1130
      if (param->equal == true) {
×
1131
        if (count > TRY_ERROR_LIMIT) break;
×
1132
        count++;
×
1133
      }
1134
    }
1135
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
36,878✔
1136
    if (valid < 0) break;
36,865!
1137
  } while (1);
1138

1139
END:
6✔
1140
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1141
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1142
  taosMemoryFree(pCursor);
6!
1143
  return ret;
6✔
1144
}
1145

1146
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1147
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1148
  SMetaFltParam *param = arg;
×
1149
  int32_t        ret = 0;
×
1150
  char          *buf = NULL;
×
1151

1152
  STagIdxKey *pKey = NULL;
×
1153
  int32_t     nKey = 0;
×
1154

1155
  SIdxCursor *pCursor = NULL;
×
1156
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1157
  if (pCursor == NULL) {
×
1158
    return terrno;
×
1159
  }
1160
  pCursor->pMeta = pMeta;
×
1161
  pCursor->suid = param->suid;
×
1162
  pCursor->cid = param->cid;
×
1163
  pCursor->type = param->type;
×
1164

1165
  char *pName = param->val;
×
1166

1167
  metaRLock(pMeta);
×
1168
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1169
  if (ret != 0) {
×
1170
    goto END;
×
1171
  }
1172

1173
  int cmp = 0;
×
1174
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1175
    goto END;
×
1176
  }
1177
  int32_t valid = 0;
×
1178
  int32_t count = 0;
×
1179

1180
  int32_t TRY_ERROR_LIMIT = 1;
×
1181
  do {
×
1182
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1183
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1184
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1185
    if (valid < 0) break;
×
1186

1187
    if (count > TRY_ERROR_LIMIT) break;
×
1188

1189
    char *pTableKey = (char *)pEntryKey;
×
1190
    terrno = TSDB_CODE_SUCCESS;
×
1191
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1192
    if (terrno != TSDB_CODE_SUCCESS) {
×
1193
      ret = terrno;
×
1194
      goto END;
×
1195
    }
1196
    if (cmp == 0) {
×
1197
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1198
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1199
        ret = terrno;
×
1200
        goto END;
×
1201
      }
1202
    } else {
1203
      if (param->equal == true) {
×
1204
        if (count > TRY_ERROR_LIMIT) break;
×
1205
        count++;
×
1206
      }
1207
    }
1208
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1209
    if (valid < 0) {
×
1210
      break;
×
1211
    }
1212
  } while (1);
1213

1214
END:
×
1215
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1216
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1217
  taosMemoryFree(buf);
×
1218
  taosMemoryFree(pKey);
×
1219

1220
  taosMemoryFree(pCursor);
×
1221

1222
  return ret;
×
1223
}
1224
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1225
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1226
  SMetaFltParam *param = arg;
×
1227
  int32_t        ret = 0;
×
1228
  char          *buf = NULL;
×
1229

1230
  STtlIdxKey *pKey = NULL;
×
1231
  int32_t     nKey = 0;
×
1232

1233
  SIdxCursor *pCursor = NULL;
×
1234
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1235
  if (pCursor == NULL) {
×
1236
    return terrno;
×
1237
  }
1238
  pCursor->pMeta = pMeta;
×
1239
  pCursor->suid = param->suid;
×
1240
  pCursor->cid = param->cid;
×
1241
  pCursor->type = param->type;
×
1242

1243
  metaRLock(pMeta);
×
1244
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1245

1246
END:
×
1247
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1248
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1249
  taosMemoryFree(buf);
×
1250
  taosMemoryFree(pKey);
×
1251

1252
  taosMemoryFree(pCursor);
×
1253

1254
  return ret;
×
1255
  // impl later
1256
  return 0;
1257
}
1258
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6,031✔
1259
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6,031✔
1260
  SMetaFltParam *param = arg;
6,031✔
1261

1262
  SMetaEntry oStbEntry = {0};
6,031✔
1263
  int32_t    code = 0;
6,031✔
1264
  char      *buf = NULL;
6,031✔
1265
  void      *pData = NULL;
6,031✔
1266
  int        nData = 0;
6,031✔
1267

1268
  SDecoder    dc = {0};
6,031✔
1269
  STbDbKey    tbDbKey = {0};
6,031✔
1270
  STagIdxKey *pKey = NULL;
6,031✔
1271
  int32_t     nKey = 0;
6,031✔
1272

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

1283
  metaRLock(pMeta);
6,048✔
1284

1285
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
6,061!
1286

1287
  tbDbKey.uid = param->suid;
6,049✔
1288
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
6,049✔
1289

1290
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
6,049!
1291

1292
  tDecoderInit(&dc, pData, nData);
6,059✔
1293

1294
  code = metaDecodeEntry(&dc, &oStbEntry);
6,040✔
1295
  if (code) {
6,052!
1296
    tDecoderClear(&dc);
×
1297
    goto END;
×
1298
  }
1299

1300
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
6,052!
1301
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1302
  }
1303

1304
  code = TSDB_CODE_INVALID_PARA;
6,052✔
1305
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
7,274✔
1306
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
6,830✔
1307
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
6,830!
1308
      code = 0;
422✔
1309
    } else {
1310
      TAOS_CHECK_GOTO(code, NULL, END);
6,408✔
1311
    }
1312
  }
1313

1314
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
444✔
1315
  if (code != 0) {
422!
1316
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1317
  }
1318

1319
  int32_t maxSize = 0;
422✔
1320
  int32_t nTagData = 0;
422✔
1321
  void   *tagData = NULL;
422✔
1322

1323
  if (param->val == NULL) {
422!
1324
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1325
    goto END;
×
1326
  } else {
1327
    if (IS_VAR_DATA_TYPE(param->type)) {
422!
1328
      tagData = varDataVal(param->val);
×
1329
      nTagData = varDataLen(param->val);
×
1330

1331
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
×
1332
        maxSize = 4 * nTagData + 1;
×
1333
        buf = taosMemoryCalloc(1, maxSize);
×
1334
        if (buf == NULL) {
×
1335
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1336
        }
1337

1338
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
×
1339
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1340
        }
1341

1342
        tagData = buf;
×
1343
        nTagData = maxSize;
×
1344
      }
1345
    } else {
1346
      tagData = param->val;
422✔
1347
      nTagData = tDataTypes[param->type].bytes;
422✔
1348
    }
1349
  }
1350

1351
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
422!
1352
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1353
                  NULL, END);
1354

1355
  int cmp = 0;
422✔
1356
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
422!
1357

1358
  int     count = 0;
421✔
1359
  int32_t valid = 0;
421✔
1360
  bool    found = false;
421✔
1361

1362
  static const int8_t TRY_ERROR_LIMIT = 1;
1363

1364
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1365
  /// target:                        [suid, cid2, type2]
1366
  int diffCidCount = 0;
421✔
1367
  do {
10,346✔
1368
    void   *entryKey = NULL, *entryVal = NULL;
10,767✔
1369
    int32_t nEntryKey, nEntryVal;
1370

1371
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
10,767✔
1372
    if (valid < 0) {
10,734✔
1373
      code = valid;
350✔
1374
    }
1375
    if (count > TRY_ERROR_LIMIT) {
10,734✔
1376
      break;
422✔
1377
    }
1378

1379
    STagIdxKey *p = entryKey;
10,709✔
1380
    if (p == NULL) break;
10,709✔
1381

1382
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
10,364!
1383
      if (found == true) break;  //
62✔
1384
      if (diffCidCount > TRY_ERROR_LIMIT) break;
10!
1385
      diffCidCount++;
10✔
1386
      count++;
10✔
1387
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
10!
1388
      if (valid < 0) {
10!
1389
        code = valid;
×
1390
        break;
×
1391
      } else {
1392
        continue;
10✔
1393
      }
1394
    }
1395

1396
    terrno = TSDB_CODE_SUCCESS;
10,302✔
1397
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
10,301✔
1398
    if (terrno != TSDB_CODE_SUCCESS) {
10,296!
1399
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1400
      break;
×
1401
    }
1402
    if (cmp == 0) {
10,304✔
1403
      // match
1404
      tb_uid_t tuid = 0;
9,716✔
1405
      if (IS_VAR_DATA_TYPE(pKey->type)) {
9,716!
1406
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
×
1407
      } else {
1408
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
9,716✔
1409
      }
1410
      if (taosArrayPush(pUids, &tuid) == NULL) {
9,743!
1411
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1412
      }
1413
      found = true;
9,743✔
1414
    } else {
1415
      if (param->equal == true) {
588✔
1416
        if (count > TRY_ERROR_LIMIT) break;
105!
1417
        count++;
105✔
1418
      }
1419
    }
1420
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
10,331✔
1421
    if (valid < 0) {
10,336!
1422
      code = valid;
×
1423
      break;
×
1424
    }
1425
  } while (1);
1426

1427
END:
6,030✔
1428
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6,030!
1429
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6,058✔
1430
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
6,058!
1431
  tDecoderClear(&dc);
6,058✔
1432
  tdbFree(pData);
6,058✔
1433

1434
  taosMemoryFree(buf);
6,063!
1435
  taosMemoryFree(pKey);
6,054!
1436

1437
  taosMemoryFree(pCursor);
6,058✔
1438

1439
  return code;
6,064✔
1440
}
1441

1442
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
272,407✔
1443
  int ret = 0;
272,407✔
1444
  if (lock) {
272,407!
1445
    metaRLock(pMeta);
×
1446
  }
1447

1448
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
272,407✔
1449
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
272,407✔
1450
  if (lock) {
272,408!
1451
    metaULock(pMeta);
×
1452
  }
1453

1454
  return ret;
272,408✔
1455
}
1456

1457
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
272,088✔
1458
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
272,088✔
1459
  const int32_t LIMIT = 128;
272,088✔
1460

1461
  int32_t isLock = false;
272,088✔
1462
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
272,088!
1463
  for (int i = 0; i < sz; i++) {
544,492✔
1464
    STUidTagInfo *p = taosArrayGet(uidList, i);
272,408✔
1465

1466
    if (i % LIMIT == 0) {
272,408✔
1467
      if (isLock) metaULock(pMeta);
272,074!
1468

1469
      metaRLock(pMeta);
272,074✔
1470
      isLock = true;
272,075✔
1471
    }
1472

1473
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1474
    void   *val = NULL;
272,409✔
1475
    int32_t len = 0;
272,409✔
1476
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
272,409!
1477
      p->pTagVal = taosMemoryMalloc(len);
272,408!
1478
      if (!p->pTagVal) {
272,409!
1479
        if (isLock) metaULock(pMeta);
×
1480

1481
        TAOS_RETURN(terrno);
×
1482
      }
1483
      memcpy(p->pTagVal, val, len);
272,409✔
1484
      tdbFree(val);
272,409✔
1485
    } else {
1486
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64 "", TD_VID(pMeta->pVnode), suid,
×
1487
                p->uid);
1488
    }
1489
  }
1490
  //  }
1491
  if (isLock) metaULock(pMeta);
272,084✔
1492
  return 0;
272,087✔
1493
}
1494

1495
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
1,024,256✔
1496
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
1,024,256✔
1497
  if (!pCur) {
1,027,266!
1498
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1499
  }
1500

1501
  // If len > 0 means there already have uids, and we only want the
1502
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1503
  // in the hash map, that may require a lot of memory
1504
  SHashObj *pSepecifiedUidMap = NULL;
1,027,266✔
1505
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
1,027,266✔
1506
  if (numOfElems > 0) {
1,025,785✔
1507
    pSepecifiedUidMap =
1508
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
94,364✔
1509
    for (int i = 0; i < numOfElems; i++) {
381,102✔
1510
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
286,650✔
1511
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
286,558✔
1512
      if (code) {
286,703!
1513
        metaCloseCtbCursor(pCur);
×
1514
        taosHashCleanup(pSepecifiedUidMap);
×
1515
        return code;
×
1516
      }
1517
    }
1518
  }
1519

1520
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
1,025,873✔
1521
    while (1) {
2,720,397✔
1522
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,651,556✔
1523
      if (uid == 0) {
3,649,951✔
1524
        break;
933,893✔
1525
      }
1526

1527
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,716,058✔
1528
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,716,058!
1529
      if (!info.pTagVal) {
2,723,294!
1530
        metaCloseCtbCursor(pCur);
×
1531
        taosHashCleanup(pSepecifiedUidMap);
×
1532
        return terrno;
×
1533
      }
1534
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,723,294✔
1535
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,720,397!
1536
        taosMemoryFreeClear(info.pTagVal);
×
1537
        metaCloseCtbCursor(pCur);
×
1538
        taosHashCleanup(pSepecifiedUidMap);
×
1539
        return terrno;
×
1540
      }
1541
    }
1542
  } else {  // only the specified tables need to be added
1543
    while (1) {
389,451✔
1544
      tb_uid_t uid = metaCtbCursorNext(pCur);
484,165✔
1545
      if (uid == 0) {
484,142✔
1546
        break;
94,443✔
1547
      }
1548

1549
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
389,699✔
1550
      if (index == NULL) {
389,810✔
1551
        continue;
103,424✔
1552
      }
1553

1554
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
286,386✔
1555
      if (pTagInfo->pTagVal == NULL) {
286,341!
1556
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
286,708✔
1557
        if (!pTagInfo->pTagVal) {
286,394!
1558
          metaCloseCtbCursor(pCur);
×
1559
          taosHashCleanup(pSepecifiedUidMap);
×
1560
          return terrno;
×
1561
        }
1562
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
286,394✔
1563
      }
1564
    }
1565
  }
1566

1567
  taosHashCleanup(pSepecifiedUidMap);
1,028,336✔
1568
  metaCloseCtbCursor(pCur);
1,027,895✔
1569
  return TSDB_CODE_SUCCESS;
1,028,322✔
1570
}
1571

1572
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1573

1574
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
35,441,382✔
1575
  int32_t code = 0;
35,441,382✔
1576
  void   *pData = NULL;
35,441,382✔
1577
  int     nData = 0;
35,441,382✔
1578
  int     lock = 0;
35,441,382✔
1579

1580
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
35,441,382!
1581
    lock = 1;
7,517,254✔
1582
  }
1583

1584
  if (!lock) metaRLock(pMeta);
35,441,382✔
1585

1586
  // search cache
1587
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
35,443,130✔
1588
    if (!lock) metaULock(pMeta);
35,078,207✔
1589
    goto _exit;
35,083,771✔
1590
  }
1591

1592
  // search TDB
1593
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
369,757✔
1594
    // not found
1595
    if (!lock) metaULock(pMeta);
289,403✔
1596
    goto _exit;
289,407✔
1597
  }
1598

1599
  if (!lock) metaULock(pMeta);
80,637✔
1600

1601
  pInfo->uid = uid;
80,637✔
1602
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
80,637✔
1603
  pInfo->version = ((SUidIdxVal *)pData)->version;
80,637✔
1604
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
80,637✔
1605

1606
  if (lock) {
80,637✔
1607
    metaULock(pReader->pMeta);
23,468✔
1608
    // metaReaderReleaseLock(pReader);
1609
  }
1610
  // upsert the cache
1611
  metaWLock(pMeta);
80,639✔
1612
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
80,639✔
1613
  if (ret != 0) {
80,638!
1614
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1615
  }
1616
  metaULock(pMeta);
80,638✔
1617

1618
  if (lock) {
80,638✔
1619
    metaRLock(pReader->pMeta);
23,469✔
1620
  }
1621

1622
_exit:
57,169✔
1623
  tdbFree(pData);
35,453,817✔
1624
  return code;
35,451,942✔
1625
}
1626

1627
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
316,636✔
1628
  int32_t code = 0;
316,636✔
1629

1630
  if (!numOfTables && !numOfCols) goto _exit;
316,636!
1631

1632
  SVnode *pVnodeObj = pVnode;
316,636✔
1633
  metaRLock(pVnodeObj->pMeta);
316,636✔
1634

1635
  // fast path: search cache
1636
  SMetaStbStats state = {0};
316,669✔
1637
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
316,669✔
1638
    metaULock(pVnodeObj->pMeta);
293,822✔
1639
    if (numOfTables) *numOfTables = state.ctbNum;
293,837✔
1640
    if (numOfCols) *numOfCols = state.colNum;
293,837!
1641
    goto _exit;
293,837✔
1642
  }
1643

1644
  // slow path: search TDB
1645
  int64_t ctbNum = 0;
22,841✔
1646
  int32_t colNum = 0;
22,841✔
1647
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
22,841✔
1648
  if (TSDB_CODE_SUCCESS == code) {
22,843✔
1649
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
22,842✔
1650
  }
1651
  metaULock(pVnodeObj->pMeta);
22,843✔
1652
  if (TSDB_CODE_SUCCESS != code) {
22,843!
1653
    goto _exit;
×
1654
  }
1655

1656
  if (numOfTables) *numOfTables = ctbNum;
22,843✔
1657
  if (numOfCols) *numOfCols = colNum;
22,843!
1658

1659
  state.uid = uid;
22,843✔
1660
  state.ctbNum = ctbNum;
22,843✔
1661
  state.colNum = colNum;
22,843✔
1662

1663
  // upsert the cache
1664
  metaWLock(pVnodeObj->pMeta);
22,843✔
1665

1666
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
22,843✔
1667
  if (ret) {
22,842!
1668
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d", uid, ctbNum, colNum);
×
1669
  }
1670

1671
  metaULock(pVnodeObj->pMeta);
22,842✔
1672

1673
_exit:
316,672✔
1674
  return code;
316,672✔
1675
}
1676

1677
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol) {
226,469✔
1678
  SMetaStbStats stats = {0};
226,469✔
1679

1680
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
226,469✔
1681
    stats.ctbNum += deltaCtb;
210,464✔
1682
    stats.colNum += deltaCol;
210,464✔
1683
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
210,464✔
1684
    if (code) {
210,459!
1685
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d",
×
1686
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol);
1687
    }
1688
  }
1689
}
226,500✔
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