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

taosdata / TDengine / #4523

17 Jul 2025 02:02AM UTC coverage: 56.768% (+0.3%) from 56.447%
#4523

push

travis-ci

web-flow
Merge pull request #31914 from taosdata/fix/3.0/compare-ans-failed

fix:Convert line endings from LF to CRLF for ans file

140094 of 313745 branches covered (44.65%)

Branch coverage included in aggregate %.

212455 of 307292 relevant lines covered (69.14%)

18276193.53 hits per line

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

55.43
/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,069,508✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
11,069,508✔
22
  metaReaderDoInit(pReader, pMeta, flags);
11,069,508✔
23
  pReader->pAPI = pAPI;
11,080,008✔
24
}
11,080,008✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
16,434,048✔
27
  memset(pReader, 0, sizeof(*pReader));
16,434,048✔
28
  pReader->pMeta = pMeta;
16,434,048✔
29
  pReader->flags = flags;
16,434,048✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
16,434,048!
31
    metaRLock(pMeta);
12,000,455✔
32
  }
33
}
16,433,687✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,101,514✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,101,514!
37
    metaULock(pReader->pMeta);
2,102,186✔
38
    pReader->flags |= META_READER_NOLOCK;
2,103,301✔
39
  }
40
}
2,102,629✔
41

42
void metaReaderClear(SMetaReader *pReader) {
17,836,027✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
17,836,027✔
44
    metaULock(pReader->pMeta);
9,895,649✔
45
  }
46
  tDecoderClear(&pReader->coder);
17,833,937✔
47
  tdbFree(pReader->pBuf);
17,840,425✔
48
  pReader->pBuf = NULL;
17,848,122✔
49
}
17,848,122✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
27,683,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);
27,684,700✔
63

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

71
  return 0;
27,454,724✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
815,612✔
84
  return true;
815,625✔
85
}
86

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

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

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

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

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

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

112
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
2,087,778✔
113
  SMeta   *pMeta = pReader->pMeta;
2,087,778✔
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,087,778✔
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
72,321✔
119
  }
120

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

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

130
  metaRLock(pMeta);
203,551✔
131

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

137
  metaULock(pMeta);
203,806✔
138

139
  return uid;
203,845✔
140
}
141

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

152
  STR_TO_VARSTR(tbName, mr.me.name);
1,506,505✔
153
  metaReaderClear(&mr);
1,506,505✔
154

155
  return 0;
1,507,539✔
156
}
157

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

170
  return 0;
×
171
}
172

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

178
  SMetaReader *pReader = &mr;
×
179

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

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

188
  metaReaderClear(&mr);
×
189

190
  return 0;
×
191
}
192

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

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

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

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

215
  // TODO
216

217
  return 0;
×
218
}
219

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

236
  code = 0;
×
237

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

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

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

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
2,046,719✔
267
  if (pTbCur) {
2,046,719✔
268
    tdbFree(pTbCur->pKey);
1,023,982✔
269
    tdbFree(pTbCur->pVal);
1,023,884✔
270
    if (!pTbCur->paused) {
1,023,948✔
271
      metaReaderClear(&pTbCur->mr);
117,175✔
272
      if (pTbCur->pDbc) {
117,168!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
117,171✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
1,023,941!
277
  }
278
}
2,046,826✔
279

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

299
    if (first) {
1,023,395✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
1,022,473✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
1,022,945!
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;
1,023,867✔
318
  }
319

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

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
12,839,083✔
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,839,083✔
334
    if (ret < 0) {
12,888,963✔
335
      return ret;
1,023,911✔
336
    }
337

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

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
11,892,398✔
341
    if (ret) return ret;
11,839,872!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
11,839,872✔
344
      continue;
2,509,725✔
345
    }
346

347
    break;
9,330,147✔
348
  }
349

350
  return 0;
9,330,147✔
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) {
5,012,304✔
382
  void           *pData = NULL;
5,012,304✔
383
  int             nData = 0;
5,012,304✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
5,012,304✔
386
  SSchemaWrapper *pSchema = NULL;
5,012,304✔
387
  SDecoder        dc = {0};
5,012,304✔
388
  if (lock) {
5,012,304✔
389
    metaRLock(pMeta);
4,998,995✔
390
  }
391
_query:
5,020,800✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
5,099,580✔
393
    goto _err;
775✔
394
  }
395

396
  version = ((SUidIdxVal *)pData)[0].version;
5,104,381✔
397

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

402
  SMetaEntry me = {0};
5,106,302✔
403
  tDecoderInit(&dc, pData, nData);
5,106,302✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
5,100,170✔
405
  if (code) {
5,102,734!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
5,102,734✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,656,343!
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,652,779✔
412
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
4,652,779✔
413
      tDecoderClear(&dc);
4,652,763✔
414
      goto _exit;
4,658,176✔
415
    }
416
  } else if (me.type == TSDB_CHILD_TABLE) {
446,391✔
417
    uid = me.ctbEntry.suid;
78,692✔
418
    tDecoderClear(&dc);
78,692✔
419
    goto _query;
78,780✔
420
  } else {
421
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
367,699!
422
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
367,884✔
423
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
367,884✔
424
      tDecoderClear(&dc);
367,878✔
425
      goto _exit;
367,947✔
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:
5,026,255✔
444
  if (lock) {
5,026,255✔
445
    metaULock(pMeta);
4,994,238✔
446
  }
447
  tdbFree(pData);
5,030,500✔
448
  return pSchema;
5,027,204✔
449

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

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

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

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

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

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

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

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

505
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
4,305,616!
506
  if (pCtbCur == NULL) {
4,315,328!
507
    return NULL;
×
508
  }
509

510
  pCtbCur->pMeta = pMeta;
4,315,328✔
511
  pCtbCur->suid = uid;
4,315,328✔
512
  pCtbCur->lock = lock;
4,315,328✔
513
  pCtbCur->paused = 1;
4,315,328✔
514

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

522
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
4,315,850✔
523
  if (pCtbCur) {
4,315,850!
524
    if (!pCtbCur->paused) {
4,316,138✔
525
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
4,272,528!
526
      if (pCtbCur->pCur) {
4,273,349!
527
        tdbTbcClose(pCtbCur->pCur);
4,273,507✔
528
      }
529
    }
530
    tdbFree(pCtbCur->pKey);
4,314,915✔
531
    tdbFree(pCtbCur->pVal);
4,314,347✔
532
  }
533
  taosMemoryFree(pCtbCur);
4,315,911!
534
}
4,316,973✔
535

536
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,802✔
537
  if (!pCtbCur->paused) {
43,802!
538
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,812✔
539
    if (pCtbCur->lock) {
43,849!
540
      metaULock(pCtbCur->pMeta);
43,849✔
541
    }
542
    pCtbCur->paused = 1;
43,837✔
543
  }
544
}
43,827✔
545

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

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

560
    if (first) {
4,312,222!
561
      SCtbIdxKey ctbIdxKey;
562
      // move to the suid
563
      ctbIdxKey.suid = pCtbCur->suid;
4,312,222✔
564
      ctbIdxKey.uid = INT64_MIN;
4,312,222✔
565
      int c = 0;
4,312,222✔
566
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
4,312,222✔
567
      if (c > 0) {
4,312,985✔
568
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
770,574✔
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;
4,309,128✔
581
}
582

583
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
18,024,500✔
584
  int         ret;
585
  SCtbIdxKey *pCtbIdxKey;
586

587
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
18,024,500✔
588
  if (ret < 0) {
18,028,666✔
589
    return 0;
825,073✔
590
  }
591

592
  pCtbIdxKey = pCtbCur->pKey;
17,203,593✔
593
  if (pCtbIdxKey->suid > pCtbCur->suid) {
17,203,593✔
594
    return 0;
3,492,784✔
595
  }
596

597
  return pCtbIdxKey->uid;
13,710,809✔
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) {
195,490✔
611
  SMStbCursor *pStbCur = NULL;
195,490✔
612
  int          ret = 0;
195,490✔
613
  int          c = 0;
195,490✔
614

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

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

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

639
  return pStbCur;
195,498✔
640
}
641

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

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

652
    taosMemoryFree(pStbCur);
195,501!
653
  }
654
}
195,503✔
655

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

659
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
342,279✔
660
  if (ret < 0) {
342,294✔
661
    return 0;
195,501✔
662
  }
663
  return *(tb_uid_t *)pStbCur->pKey;
146,793✔
664
}
665

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

670
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
4,869,734✔
671
  if (!pSW) return NULL;
4,880,523✔
672

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

675
  taosMemoryFree(pSW->pSchema);
4,884,651✔
676
  taosMemoryFree(pSW);
4,883,837!
677
  return pTSchema;
4,883,113✔
678
}
679

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

688
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,860,013✔
689
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,860,013✔
690
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,870,563!
691
    return terrno;
1,325✔
692
  }
693
  return TSDB_CODE_SUCCESS;
4,869,238✔
694
}
695

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

793
_exit:
486,108✔
794
  return code;
486,108✔
795
}
796

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

807
void metaUpdTimeSeriesNum(SMeta *pMeta) {
195,499✔
808
  int64_t nCtbTimeSeries = 0;
195,499✔
809
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
195,499!
810
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
195,500✔
811
  }
812
}
195,500✔
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;
945,681✔
817
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
945,717✔
818
    metaUpdTimeSeriesNum(pMeta);
195,465✔
819
  }
820

821
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
945,745✔
822
}
823

824
// type: 1 reported timeseries
825
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
945,681!
826
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
945,745✔
827
  if (type == 1) {
945,745✔
828
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
686,965✔
829
  }
830
  return nTimeSeries;
945,751✔
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) {
21,764,263✔
1090
  STag *tag = (STag *)pTag;
21,764,263✔
1091
  if (type == TSDB_DATA_TYPE_JSON) {
21,764,263✔
1092
    return tag;
10,451✔
1093
  }
1094
  bool find = tTagGet(tag, val);
21,753,812✔
1095

1096
  if (!find) {
21,818,171✔
1097
    return NULL;
31,772✔
1098
  }
1099

1100
  return val;
21,786,399✔
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 {
69,143✔
1150
    void   *entryKey = NULL;
69,155✔
1151
    int32_t nEntryKey = -1;
69,155✔
1152
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
69,155✔
1153
    if (valid < 0) break;
69,425✔
1154

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

1158
    terrno = TSDB_CODE_SUCCESS;
69,413✔
1159
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
69,311✔
1160
    if (terrno != TSDB_CODE_SUCCESS) {
69,178!
1161
      ret = terrno;
×
1162
      break;
×
1163
    }
1164
    if (cmp == 0) {
69,245!
1165
      if (taosArrayPush(pUids, &p->uid) == NULL) {
138,432!
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);
69,187✔
1176
    if (valid < 0) break;
69,143!
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) {
17,945✔
1299
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
17,945✔
1300
  SMetaFltParam *param = arg;
17,945✔
1301

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

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

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

1323
  metaRLock(pMeta);
17,972✔
1324

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

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

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

1332
  tDecoderInit(&dc, pData, nData);
17,992✔
1333

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

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

1344
  code = TSDB_CODE_INVALID_PARA;
17,975✔
1345

1346
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
66,175!
1347
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
66,180✔
1348
    if (IS_IDX_ON(schema)) {
66,180✔
1349
      if (schema->colId == param->cid && param->type == schema->type) {
21,687!
1350
        code = 0;
17,980✔
1351
        break;
17,980✔
1352
      }
1353
    }
1354
  }
1355

1356
  TAOS_CHECK_GOTO(code, NULL, END);
17,975!
1357

1358
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
17,975✔
1359
  if (code != 0) {
17,987✔
1360
    TAOS_CHECK_GOTO(terrno, NULL, END);
8!
1361
  }
1362

1363
  int32_t maxSize = 0;
17,979✔
1364
  int32_t nTagData = 0;
17,979✔
1365
  void   *tagData = NULL;
17,979✔
1366

1367
  if (param->val == NULL) {
17,979!
1368
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1369
    goto END;
×
1370
  } else {
1371
    if (IS_VAR_DATA_TYPE(param->type)) {
17,979!
1372
      tagData = varDataVal(param->val);
423✔
1373
      nTagData = varDataLen(param->val);
423✔
1374

1375
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
423✔
1376
        maxSize = 4 * nTagData + 1;
114✔
1377
        buf = taosMemoryCalloc(1, maxSize);
114!
1378
        if (buf == NULL) {
114!
1379
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1380
        }
1381

1382
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
114!
1383
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1384
        }
1385

1386
        tagData = buf;
114✔
1387
        nTagData = maxSize;
114✔
1388
      }
1389
    } else {
1390
      tagData = param->val;
17,556✔
1391
      nTagData = tDataTypes[param->type].bytes;
17,556✔
1392
    }
1393
  }
1394

1395
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
17,979!
1396
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1397
                  NULL, END);
1398

1399
  int cmp = 0;
17,977✔
1400
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
17,977!
1401

1402
  int     count = 0;
17,985✔
1403
  int32_t valid = 0;
17,985✔
1404
  bool    found = false;
17,985✔
1405

1406
  static const int8_t TRY_ERROR_LIMIT = 1;
1407

1408
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1409
  /// target:                        [suid, cid2, type2]
1410
  int diffCidCount = 0;
17,985✔
1411
  do {
138,184✔
1412
    void   *entryKey = NULL, *entryVal = NULL;
156,169✔
1413
    int32_t nEntryKey, nEntryVal;
1414

1415
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
156,169✔
1416
    if (valid < 0) {
156,115✔
1417
      break;
17,993✔
1418
    }
1419
    if (count > TRY_ERROR_LIMIT) {
142,265✔
1420
      break;
1,990✔
1421
    }
1422

1423
    STagIdxKey *p = entryKey;
140,275✔
1424
    if (p == NULL) break;
140,275!
1425

1426
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
140,275✔
1427
      if (found == true) break;  //
2,504✔
1428
      if (diffCidCount > TRY_ERROR_LIMIT) break;
351!
1429
      diffCidCount++;
351✔
1430
      count++;
351✔
1431
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
351✔
1432
      if (valid < 0) {
390!
1433
        code = valid;
×
1434
        break;
×
1435
      } else {
1436
        continue;
390✔
1437
      }
1438
    }
1439

1440
    terrno = TSDB_CODE_SUCCESS;
137,771✔
1441
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
137,772✔
1442
    if (terrno != TSDB_CODE_SUCCESS) {
137,745!
1443
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1444
      break;
×
1445
    }
1446
    if (cmp == 0) {
137,777✔
1447
      // match
1448
      tb_uid_t tuid = 0;
125,719✔
1449
      if (IS_VAR_DATA_TYPE(pKey->type)) {
125,719!
1450
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
722✔
1451
      } else {
1452
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
124,997✔
1453
      }
1454
      if (taosArrayPush(pUids, &tuid) == NULL) {
125,762!
1455
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1456
      }
1457
      found = true;
125,762✔
1458
    } else {
1459
      if (param->equal == true) {
12,058✔
1460
        if (count > TRY_ERROR_LIMIT) break;
7,974!
1461
        count++;
7,974✔
1462
      }
1463
    }
1464
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
137,820✔
1465
    if (valid < 0) {
137,794!
1466
      code = valid;
×
1467
      break;
×
1468
    }
1469
  } while (1);
1470

1471
END:
17,993✔
1472
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
17,993✔
1473
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
17,995✔
1474
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
17,987!
1475
  tDecoderClear(&dc);
17,987✔
1476
  tdbFree(pData);
17,988✔
1477

1478
  taosMemoryFree(buf);
17,993!
1479
  taosMemoryFree(pKey);
17,991!
1480

1481
  taosMemoryFree(pCursor);
17,990!
1482

1483
  return code;
17,989✔
1484
}
1485

1486
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
84,693✔
1487
  int ret = 0;
84,693✔
1488
  if (lock) {
84,693!
1489
    metaRLock(pMeta);
×
1490
  }
1491

1492
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
84,693✔
1493
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
84,693✔
1494
  if (lock) {
85,089!
1495
    metaULock(pMeta);
×
1496
  }
1497

1498
  return ret;
85,090✔
1499
}
1500

1501
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
8,446✔
1502
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
8,446✔
1503
  const int32_t LIMIT = 128;
8,446✔
1504

1505
  int32_t isLock = false;
8,446✔
1506
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
8,446!
1507
  for (int i = 0; i < sz; i++) {
93,211✔
1508
    STUidTagInfo *p = taosArrayGet(uidList, i);
84,773✔
1509

1510
    if (i % LIMIT == 0) {
84,749✔
1511
      if (isLock) metaULock(pMeta);
6,613✔
1512

1513
      metaRLock(pMeta);
6,613✔
1514
      isLock = true;
6,615✔
1515
    }
1516

1517
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1518
    void   *val = NULL;
84,751✔
1519
    int32_t len = 0;
84,751✔
1520
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
84,751!
1521
      p->pTagVal = taosMemoryMalloc(len);
85,084!
1522
      if (!p->pTagVal) {
84,904!
1523
        if (isLock) metaULock(pMeta);
×
1524

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

1539
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
1,431,734✔
1540
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
1,431,734✔
1541
  if (!pCur) {
1,433,675!
1542
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1543
  }
1544

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

1564
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
1,432,684✔
1565
    while (1) {
4,163,231✔
1566
      tb_uid_t uid = metaCtbCursorNext(pCur);
5,500,454✔
1567
      if (uid == 0) {
5,495,819✔
1568
        break;
1,339,580✔
1569
      }
1570

1571
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
4,156,239✔
1572
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
4,156,239!
1573
      if (!info.pTagVal) {
4,167,396!
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
4,167,396✔
1579
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
4,163,231!
1580
        taosMemoryFreeClear(info.pTagVal);
×
1581
        metaCloseCtbCursor(pCur);
×
1582
        taosHashCleanup(pSepecifiedUidMap);
×
1583
        return terrno;
×
1584
      }
1585
    }
1586
  } else {  // only the specified tables need to be added
1587
    while (1) {
427,897✔
1588
      tb_uid_t uid = metaCtbCursorNext(pCur);
523,358✔
1589
      if (uid == 0) {
523,220✔
1590
        break;
95,205✔
1591
      }
1592

1593
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
428,015✔
1594
      if (index == NULL) {
427,954✔
1595
        continue;
118,409✔
1596
      }
1597

1598
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
309,545✔
1599
      if (pTagInfo->pTagVal == NULL) {
309,261!
1600
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
309,672!
1601
        if (!pTagInfo->pTagVal) {
309,899!
1602
          metaCloseCtbCursor(pCur);
×
1603
          taosHashCleanup(pSepecifiedUidMap);
×
1604
          return terrno;
×
1605
        }
1606
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
309,899✔
1607
      }
1608
    }
1609
  }
1610

1611
  taosHashCleanup(pSepecifiedUidMap);
1,434,785✔
1612
  metaCloseCtbCursor(pCur);
1,434,420✔
1613
  return TSDB_CODE_SUCCESS;
1,434,729✔
1614
}
1615

1616
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1617

1618
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
38,335,110✔
1619
  int32_t code = 0;
38,335,110✔
1620
  void   *pData = NULL;
38,335,110✔
1621
  int     nData = 0;
38,335,110✔
1622
  int     lock = 0;
38,335,110✔
1623

1624
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
38,335,110!
1625
    lock = 1;
7,781,448✔
1626
  }
1627

1628
  if (!lock) metaRLock(pMeta);
38,335,110✔
1629

1630
  // search cache
1631
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
38,338,122✔
1632
    if (!lock) metaULock(pMeta);
37,970,011✔
1633
    goto _exit;
37,970,194✔
1634
  }
1635

1636
  // search TDB
1637
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
373,714✔
1638
    // not found
1639
    if (!lock) metaULock(pMeta);
335,350!
1640
    goto _exit;
335,343✔
1641
  }
1642

1643
  if (!lock) metaULock(pMeta);
38,456✔
1644

1645
  pInfo->uid = uid;
38,456✔
1646
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
38,456✔
1647
  pInfo->version = ((SUidIdxVal *)pData)->version;
38,456✔
1648
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
38,456✔
1649

1650
  if (lock) {
38,456✔
1651
    metaULock(pReader->pMeta);
3,819✔
1652
    // metaReaderReleaseLock(pReader);
1653
  }
1654
  // upsert the cache
1655
  metaWLock(pMeta);
38,457✔
1656
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
38,466✔
1657
  if (ret != 0) {
38,466!
1658
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1659
  }
1660
  metaULock(pMeta);
38,466✔
1661

1662
  if (lock) {
38,465✔
1663
    metaRLock(pReader->pMeta);
3,820✔
1664
  }
1665

1666
_exit:
34,645✔
1667
  tdbFree(pData);
38,344,003✔
1668
  return code;
38,342,081✔
1669
}
1670

1671
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
401,058✔
1672
  int32_t code = 0;
401,058✔
1673

1674
  if (!numOfTables && !numOfCols) goto _exit;
401,058!
1675

1676
  SVnode *pVnodeObj = pVnode;
401,058✔
1677
  metaRLock(pVnodeObj->pMeta);
401,058✔
1678

1679
  // fast path: search cache
1680
  SMetaStbStats state = {0};
401,169✔
1681
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
401,169✔
1682
    metaULock(pVnodeObj->pMeta);
378,300✔
1683
    if (numOfTables) *numOfTables = state.ctbNum;
378,304✔
1684
    if (numOfCols) *numOfCols = state.colNum;
378,304✔
1685
    if (flags) *flags = state.flags;
378,304✔
1686
    goto _exit;
378,304✔
1687
  }
1688

1689
  // slow path: search TDB
1690
  int64_t ctbNum = 0;
22,845✔
1691
  int32_t colNum = 0;
22,845✔
1692
  int64_t keep = 0;
22,845✔
1693
  int8_t  flag = 0;
22,845✔
1694

1695
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
22,845✔
1696
  if (TSDB_CODE_SUCCESS == code) {
22,844!
1697
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
22,847✔
1698
  }
1699
  if (TSDB_CODE_SUCCESS == code) {
22,840!
1700
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
22,847✔
1701
  }
1702
  metaULock(pVnodeObj->pMeta);
22,837✔
1703
  if (TSDB_CODE_SUCCESS != code) {
22,848!
1704
    goto _exit;
×
1705
  }
1706

1707
  if (numOfTables) *numOfTables = ctbNum;
22,848✔
1708
  if (numOfCols) *numOfCols = colNum;
22,848✔
1709
  if (flags) *flags = flag;
22,848✔
1710

1711
  state.uid = uid;
22,848✔
1712
  state.ctbNum = ctbNum;
22,848✔
1713
  state.colNum = colNum;
22,848✔
1714
  state.flags = flag;
22,848✔
1715
  state.keep = keep;
22,848✔
1716
  // upsert the cache
1717
  metaWLock(pVnodeObj->pMeta);
22,848✔
1718

1719
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
22,850✔
1720
  if (ret) {
22,845!
1721
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1722
              uid, ctbNum, colNum, keep, flag);
1723
  }
1724

1725
  metaULock(pVnodeObj->pMeta);
22,845✔
1726

1727
_exit:
401,114✔
1728
  return code;
401,114✔
1729
}
1730

1731
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
268,345✔
1732
  SMetaStbStats stats = {0};
268,345✔
1733

1734
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
268,345✔
1735
    stats.ctbNum += deltaCtb;
249,415✔
1736
    stats.colNum += deltaCol;
249,415✔
1737
    if (deltaKeep > 0) {
249,415!
1738
      stats.keep = deltaKeep;
×
1739
    }
1740

1741
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
249,415✔
1742
    if (code) {
249,418!
1743
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1744
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1745
    }
1746
  }
1747
}
268,425✔
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