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

taosdata / TDengine / #4423

04 Jul 2025 08:20AM UTC coverage: 63.643% (+2.6%) from 61.007%
#4423

push

travis-ci

GitHub
Merge pull request #31575 from taosdata/fix/huoh/taos_log

160637 of 321218 branches covered (50.01%)

Branch coverage included in aggregate %.

247567 of 320175 relevant lines covered (77.32%)

16621050.3 hits per line

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

56.92
/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) {
10,583,037✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
10,583,037✔
22
  metaReaderDoInit(pReader, pMeta, flags);
10,583,037✔
23
  pReader->pAPI = pAPI;
10,597,356✔
24
}
10,597,356✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
15,344,563✔
27
  memset(pReader, 0, sizeof(*pReader));
15,344,563✔
28
  pReader->pMeta = pMeta;
15,344,563✔
29
  pReader->flags = flags;
15,344,563✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
15,344,563!
31
    metaRLock(pMeta);
11,022,752✔
32
  }
33
}
15,343,798✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,030,556✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,030,556!
37
    metaULock(pReader->pMeta);
2,031,312✔
38
    pReader->flags |= META_READER_NOLOCK;
2,032,014✔
39
  }
40
}
2,031,258✔
41

42
void metaReaderClear(SMetaReader *pReader) {
16,713,855✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
16,713,855✔
44
    metaULock(pReader->pMeta);
8,989,243✔
45
  }
46
  tDecoderClear(&pReader->coder);
16,712,067✔
47
  tdbFree(pReader->pBuf);
16,720,980✔
48
  pReader->pBuf = NULL;
16,727,099✔
49
}
16,727,099✔
50

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

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

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

71
  return 0;
25,578,674✔
72
}
73

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

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

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

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

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

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

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

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

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

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

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

121
  uid = *(tb_uid_t *)pReader->pBuf;
2,463,561✔
122
  return metaReaderGetTableEntryByUid(pReader, uid);
2,463,561✔
123
}
124

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

130
  metaRLock(pMeta);
215,117✔
131

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

137
  metaULock(pMeta);
215,154✔
138

139
  return uid;
215,199✔
140
}
141

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

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

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

178
  SMetaReader *pReader = &mr;
478,682✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
475,954✔
187

188
  metaReaderClear(&mr);
475,954✔
189

190
  return 0;
475,942✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
475,953✔
199
  if (code == 0) *tbType = mr.me.type;
475,935✔
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
475,935✔
201
    *suid = mr.me.ctbEntry.suid;
475,910✔
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
25✔
203
    *suid = mr.me.uid;
15✔
204
  } else {
205
    *suid = 0;
10✔
206
  }
207

208
  metaReaderClear(&mr);
475,935✔
209
  return code;
475,952✔
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) {
973,603✔
245
  SMTbCursor *pTbCur = NULL;
973,603✔
246
  int32_t     code;
247

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

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,950,827✔
267
  if (pTbCur) {
1,950,827✔
268
    tdbFree(pTbCur->pKey);
975,935✔
269
    tdbFree(pTbCur->pVal);
975,884✔
270
    if (!pTbCur->paused) {
975,927✔
271
      metaReaderClear(&pTbCur->mr);
81,217✔
272
      if (pTbCur->pDbc) {
81,212!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
81,212✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
975,929!
277
  }
278
}
1,950,896✔
279

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

299
    if (first) {
975,656✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
974,734✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
974,842!
302
    } else {
303
      int c = 1;
922✔
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
922✔
305
      TSDB_CHECK_CODE(code, lino, _exit);
922!
306
      if (c == 0) {
922!
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
922!
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;
975,764✔
318
  }
319

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

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
12,346,445✔
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);
12,346,445✔
334
    if (ret < 0) {
12,411,366✔
335
      return ret;
975,840✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
11,435,526✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
11,461,230✔
341
    if (ret) return ret;
11,422,312!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
11,422,312✔
344
      continue;
2,412,220✔
345
    }
346

347
    break;
9,010,092✔
348
  }
349

350
  return 0;
9,010,092✔
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) {
4,761,513✔
382
  void           *pData = NULL;
4,761,513✔
383
  int             nData = 0;
4,761,513✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
4,761,513✔
386
  SSchemaWrapper *pSchema = NULL;
4,761,513✔
387
  SDecoder        dc = {0};
4,761,513✔
388
  if (lock) {
4,761,513✔
389
    metaRLock(pMeta);
4,741,941✔
390
  }
391
_query:
4,769,157✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
4,848,667✔
393
    goto _err;
735✔
394
  }
395

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

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

402
  SMetaEntry me = {0};
4,853,428✔
403
  tDecoderInit(&dc, pData, nData);
4,853,428✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
4,848,532✔
405
  if (code) {
4,851,074!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
4,851,074✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,387,894!
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,384,887✔
412
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
4,384,887✔
413
      tDecoderClear(&dc);
4,384,892✔
414
      goto _exit;
4,388,807✔
415
    }
416
  } else if (me.type == TSDB_CHILD_TABLE) {
463,180✔
417
    uid = me.ctbEntry.suid;
79,410✔
418
    tDecoderClear(&dc);
79,410✔
419
    goto _query;
79,510✔
420
  } else {
421
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
383,770!
422
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
383,885✔
423
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
383,885✔
424
      tDecoderClear(&dc);
383,863✔
425
      goto _exit;
383,923✔
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) {
132!
433
    goto _err;
×
434
  }
435

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

443
_exit:
4,772,862✔
444
  if (lock) {
4,772,862✔
445
    metaULock(pMeta);
4,737,517✔
446
  }
447
  tdbFree(pData);
4,776,347✔
448
  return pSchema;
4,773,798✔
449

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

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

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

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

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

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

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

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

505
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,550,594!
506
  if (pCtbCur == NULL) {
3,560,027!
507
    return NULL;
×
508
  }
509

510
  pCtbCur->pMeta = pMeta;
3,560,027✔
511
  pCtbCur->suid = uid;
3,560,027✔
512
  pCtbCur->lock = lock;
3,560,027✔
513
  pCtbCur->paused = 1;
3,560,027✔
514

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

522
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,559,404✔
523
  if (pCtbCur) {
3,559,404!
524
    if (!pCtbCur->paused) {
3,559,680✔
525
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,516,267!
526
      if (pCtbCur->pCur) {
3,517,371!
527
        tdbTbcClose(pCtbCur->pCur);
3,517,500✔
528
      }
529
    }
530
    tdbFree(pCtbCur->pKey);
3,559,214✔
531
    tdbFree(pCtbCur->pVal);
3,559,129✔
532
  }
533
  taosMemoryFree(pCtbCur);
3,559,733!
534
}
3,560,635✔
535

536
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,550✔
537
  if (!pCtbCur->paused) {
43,550!
538
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,567✔
539
    if (pCtbCur->lock) {
43,606!
540
      metaULock(pCtbCur->pMeta);
43,607✔
541
    }
542
    pCtbCur->paused = 1;
43,614✔
543
  }
544
}
43,597✔
545

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

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

560
    if (first) {
3,557,797!
561
      SCtbIdxKey ctbIdxKey;
562
      // move to the suid
563
      ctbIdxKey.suid = pCtbCur->suid;
3,557,797✔
564
      ctbIdxKey.uid = INT64_MIN;
3,557,797✔
565
      int c = 0;
3,557,797✔
566
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,557,797✔
567
      if (c > 0) {
3,557,641✔
568
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
926,306✔
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;
3,554,574✔
581
}
582

583
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
15,011,170✔
584
  int         ret;
585
  SCtbIdxKey *pCtbIdxKey;
586

587
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
15,011,170✔
588
  if (ret < 0) {
15,019,559✔
589
    return 0;
1,051,795✔
590
  }
591

592
  pCtbIdxKey = pCtbCur->pKey;
13,967,764✔
593
  if (pCtbIdxKey->suid > pCtbCur->suid) {
13,967,764✔
594
    return 0;
2,509,384✔
595
  }
596

597
  return pCtbIdxKey->uid;
11,458,380✔
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) {
217,431✔
611
  SMStbCursor *pStbCur = NULL;
217,431✔
612
  int          ret = 0;
217,431✔
613
  int          c = 0;
217,431✔
614

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

621
  pStbCur->pMeta = pMeta;
217,453✔
622
  pStbCur->suid = suid;
217,453✔
623
  metaRLock(pMeta);
217,453✔
624

625
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
217,456✔
626
  if (ret < 0) {
217,445!
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);
217,445✔
635
  if (c > 0) {
217,443!
636
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
637
  }
638

639
  return pStbCur;
217,453✔
640
}
641

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

648
      tdbFree(pStbCur->pKey);
217,456✔
649
      tdbFree(pStbCur->pVal);
217,456✔
650
    }
651

652
    taosMemoryFree(pStbCur);
217,456!
653
  }
654
}
217,456✔
655

656
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
415,109✔
657
  int ret;
658

659
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
415,109✔
660
  if (ret < 0) {
415,137✔
661
    return 0;
217,456✔
662
  }
663
  return *(tb_uid_t *)pStbCur->pKey;
197,681✔
664
}
665

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

670
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
4,612,071✔
671
  if (!pSW) return NULL;
4,621,622✔
672

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

675
  taosMemoryFree(pSW->pSchema);
4,624,632✔
676
  taosMemoryFree(pSW);
4,623,653!
677
  return pTSchema;
4,623,859✔
678
}
679

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

688
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,602,652✔
689
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,602,652✔
690
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,613,840✔
691
    return terrno;
17✔
692
  }
693
  return TSDB_CODE_SUCCESS;
4,613,823✔
694
}
695

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

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

711
      skmDbKey.uid = suid ? suid : uid;
77!
712
      skmDbKey.sver = INT32_MAX;
77✔
713

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

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

725
      if (c == 0) {
81!
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) {
81✔
734
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
6✔
735
      }
736

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

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

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

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

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

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

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

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

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

790
  *ppTSchema = pTSchema;
1,406,775✔
791
  taosMemoryFree(pSchemaWrapper->pSchema);
1,406,775!
792

793
_exit:
1,406,987✔
794
  return code;
1,406,987✔
795
}
796

797
// N.B. Called by statusReq per second
798
int64_t metaGetTbNum(SMeta *pMeta) {
1,297,521✔
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;
1,297,521✔
805
}
806

807
void metaUpdTimeSeriesNum(SMeta *pMeta) {
217,441✔
808
  int64_t nCtbTimeSeries = 0;
217,441✔
809
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
217,441!
810
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
217,443✔
811
  }
812
}
217,448✔
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;
1,582,842✔
817
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,582,907✔
818
    metaUpdTimeSeriesNum(pMeta);
217,379✔
819
  }
820

821
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,582,949✔
822
}
823

824
// type: 1 reported timeseries
825
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,582,842!
826
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,582,949✔
827
  if (type == 1) {
1,582,949✔
828
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
1,297,521✔
829
  }
830
  return nTimeSeries;
1,582,951✔
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) {
17,651,042✔
1090
  STag *tag = (STag *)pTag;
17,651,042✔
1091
  if (type == TSDB_DATA_TYPE_JSON) {
17,651,042✔
1092
    return tag;
10,289✔
1093
  }
1094
  bool find = tTagGet(tag, val);
17,640,753✔
1095

1096
  if (!find) {
17,691,933✔
1097
    return NULL;
42,092✔
1098
  }
1099

1100
  return val;
17,649,841✔
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) {
12✔
1116
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
12✔
1117
  SMetaFltParam *param = arg;
12✔
1118
  int32_t        ret = 0;
12✔
1119

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

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

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

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

1145
  int32_t valid = 0;
12✔
1146
  int32_t count = 0;
12✔
1147

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

1155
    SBtimeIdxKey *p = entryKey;
70,240✔
1156
    if (count > TRY_ERROR_LIMIT) break;
70,240!
1157

1158
    terrno = TSDB_CODE_SUCCESS;
70,240✔
1159
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
70,226✔
1160
    if (terrno != TSDB_CODE_SUCCESS) {
70,223!
1161
      ret = terrno;
×
1162
      break;
×
1163
    }
1164
    if (cmp == 0) {
70,174!
1165
      if (taosArrayPush(pUids, &p->uid) == NULL) {
140,484!
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);
70,310✔
1176
    if (valid < 0) break;
70,726!
1177
  } while (1);
1178

1179
END:
12✔
1180
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
12!
1181
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
12!
1182
  taosMemoryFree(pCursor);
12!
1183
  return ret;
12✔
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) {
4,436✔
1299
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
4,436✔
1300
  SMetaFltParam *param = arg;
4,436✔
1301

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

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

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

1323
  metaRLock(pMeta);
4,445✔
1324

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

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

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

1332
  tDecoderInit(&dc, pData, nData);
4,449✔
1333

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

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

1344
  code = TSDB_CODE_INVALID_PARA;
4,448✔
1345
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
7,949✔
1346
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
6,686✔
1347
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
6,686!
1348
      code = 0;
1,258✔
1349
    } else {
1350
      TAOS_CHECK_GOTO(code, NULL, END);
5,428✔
1351
    }
1352
  }
1353

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

1359
  int32_t maxSize = 0;
1,257✔
1360
  int32_t nTagData = 0;
1,257✔
1361
  void   *tagData = NULL;
1,257✔
1362

1363
  if (param->val == NULL) {
1,257!
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)) {
1,257!
1368
      tagData = varDataVal(param->val);
282✔
1369
      nTagData = varDataLen(param->val);
282✔
1370

1371
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
282!
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;
975✔
1387
      nTagData = tDataTypes[param->type].bytes;
975✔
1388
    }
1389
  }
1390

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

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

1398
  int     count = 0;
1,257✔
1399
  int32_t valid = 0;
1,257✔
1400
  bool    found = false;
1,257✔
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;
1,257✔
1407
  do {
13,600✔
1408
    void   *entryKey = NULL, *entryVal = NULL;
14,857✔
1409
    int32_t nEntryKey, nEntryVal;
1410

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

1419
    STagIdxKey *p = entryKey;
14,613✔
1420
    if (p == NULL) break;
14,613✔
1421

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

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

1467
END:
4,443✔
1468
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
4,443✔
1469
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
4,445✔
1470
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
4,445!
1471
  tDecoderClear(&dc);
4,445✔
1472
  tdbFree(pData);
4,448✔
1473

1474
  taosMemoryFree(buf);
4,448!
1475
  taosMemoryFree(pKey);
4,448!
1476

1477
  taosMemoryFree(pCursor);
4,447✔
1478

1479
  return code;
4,447✔
1480
}
1481

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

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

1494
  return ret;
285,662✔
1495
}
1496

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

1501
  int32_t isLock = false;
285,288✔
1502
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
285,288!
1503
  for (int i = 0; i < sz; i++) {
570,942✔
1504
    STUidTagInfo *p = taosArrayGet(uidList, i);
285,653✔
1505

1506
    if (i % LIMIT == 0) {
285,646✔
1507
      if (isLock) metaULock(pMeta);
285,232!
1508

1509
      metaRLock(pMeta);
285,232✔
1510
      isLock = true;
285,247✔
1511
    }
1512

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

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

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

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

1560
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
827,244✔
1561
    while (1) {
2,350,104✔
1562
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,097,812✔
1563
      if (uid == 0) {
3,093,627✔
1564
        break;
749,941✔
1565
      }
1566

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

1589
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
367,276✔
1590
      if (index == NULL) {
367,652✔
1591
        continue;
129,428✔
1592
      }
1593

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

1607
  taosHashCleanup(pSepecifiedUidMap);
829,083✔
1608
  metaCloseCtbCursor(pCur);
828,847✔
1609
  return TSDB_CODE_SUCCESS;
829,116✔
1610
}
1611

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

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

1620
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
55,267,492!
1621
    lock = 1;
7,373,871✔
1622
  }
1623

1624
  if (!lock) metaRLock(pMeta);
55,267,492✔
1625

1626
  // search cache
1627
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
55,272,087✔
1628
    if (!lock) metaULock(pMeta);
41,282,322✔
1629
    goto _exit;
41,282,520✔
1630
  }
1631

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

1639
  if (!lock) metaULock(pMeta);
48,912✔
1640

1641
  pInfo->uid = uid;
48,912✔
1642
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
48,912✔
1643
  pInfo->version = ((SUidIdxVal *)pData)->version;
48,912✔
1644
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
48,912✔
1645

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

1658
  if (lock) {
48,923✔
1659
    metaRLock(pReader->pMeta);
3,634✔
1660
  }
1661

1662
_exit:
45,289✔
1663
  tdbFree(pData);
55,278,635✔
1664
  return code;
55,276,800✔
1665
}
1666

1667
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
482,804✔
1668
  int32_t code = 0;
482,804✔
1669

1670
  if (!numOfTables && !numOfCols) goto _exit;
482,804!
1671

1672
  SVnode *pVnodeObj = pVnode;
482,804✔
1673
  metaRLock(pVnodeObj->pMeta);
482,804✔
1674

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

1685
  // slow path: search TDB
1686
  int64_t ctbNum = 0;
27,881✔
1687
  int32_t colNum = 0;
27,881✔
1688
  int64_t keep = 0;
27,881✔
1689
  int8_t  flag = 0;
27,881✔
1690

1691
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
27,881✔
1692
  if (TSDB_CODE_SUCCESS == code) {
27,880!
1693
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
27,881✔
1694
  }
1695
  if (TSDB_CODE_SUCCESS == code) {
27,877!
1696
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
27,878✔
1697
  }
1698
  metaULock(pVnodeObj->pMeta);
27,878✔
1699
  if (TSDB_CODE_SUCCESS != code) {
27,880!
1700
    goto _exit;
×
1701
  }
1702

1703
  if (numOfTables) *numOfTables = ctbNum;
27,880✔
1704
  if (numOfCols) *numOfCols = colNum;
27,880✔
1705
  if (flags) *flags = flag;
27,880✔
1706

1707
  state.uid = uid;
27,880✔
1708
  state.ctbNum = ctbNum;
27,880✔
1709
  state.colNum = colNum;
27,880✔
1710
  state.flags = flag;
27,880✔
1711
  state.keep = keep;
27,880✔
1712
  // upsert the cache
1713
  metaWLock(pVnodeObj->pMeta);
27,880✔
1714

1715
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
27,879✔
1716
  if (ret) {
27,882!
1717
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1718
              uid, ctbNum, colNum, keep, flag);
1719
  }
1720

1721
  metaULock(pVnodeObj->pMeta);
27,882✔
1722

1723
_exit:
482,886✔
1724
  return code;
482,886✔
1725
}
1726

1727
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
300,325✔
1728
  SMetaStbStats stats = {0};
300,325✔
1729

1730
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
300,325✔
1731
    stats.ctbNum += deltaCtb;
278,123✔
1732
    stats.colNum += deltaCol;
278,123✔
1733
    if (deltaKeep > 0) {
278,123!
1734
      stats.keep = deltaKeep;
×
1735
    }
1736

1737
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
278,123✔
1738
    if (code) {
278,124!
1739
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1740
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1741
    }
1742
  }
1743
}
300,455✔
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