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

taosdata / TDengine / #3620

21 Feb 2025 09:00AM UTC coverage: 63.573% (+0.2%) from 63.423%
#3620

push

travis-ci

web-flow
ci: taosBenchmark add coverage cases branch 3.0 (#29788)

* fix: add unit test for taos-tools

* fix: only .cpp include

* fix: remove no use function

* fix: restore toolsSys.c

* fix: add toolsSys case

* fix: rebuild error fixed

* fix: fix build error

* fix: support get vgroups with core and memory limit

* fix: build error for strcasecmp

* fix: add insertBasic.py case

* fix: add command line set vgroups=3

* fix: change with ns database

* toolscJson read with int replace float and add insertPrecison.py

* fix: add insertBindVGroup.json case

* fix: remove public fun removeQuotation

* fix: vgroups change method

* fix: memory leak for runInsertLimitThread slot

* insertPrecision.py word write wrong

* fix: check isFloat number

* fix: vgroups change logic error

* fix: insertBasic.py real and expect error

* fix: adjust default vgroups

* fix: adjust default vgroups modify comment

148962 of 300203 branches covered (49.62%)

Branch coverage included in aggregate %.

15 of 16 new or added lines in 1 file covered. (93.75%)

2018 existing lines in 133 files now uncovered.

233201 of 300933 relevant lines covered (77.49%)

18174406.98 hits per line

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

56.72
/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,476,105✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
11,476,105✔
22
  metaReaderDoInit(pReader, pMeta, flags);
11,476,105✔
23
  pReader->pAPI = pAPI;
11,491,397✔
24
}
11,491,397✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
15,345,499✔
27
  memset(pReader, 0, sizeof(*pReader));
15,345,499✔
28
  pReader->pMeta = pMeta;
15,345,499✔
29
  pReader->flags = flags;
15,345,499✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
15,345,499!
31
    metaRLock(pMeta);
10,332,746✔
32
  }
33
}
15,346,079✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,000,152✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,000,152!
37
    metaULock(pReader->pMeta);
2,000,714✔
38
    pReader->flags |= META_READER_NOLOCK;
2,001,577✔
39
  }
40
}
2,001,015✔
41

42
void metaReaderClear(SMetaReader *pReader) {
16,033,981✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
16,033,981✔
44
    metaULock(pReader->pMeta);
8,329,834✔
45
  }
46
  tDecoderClear(&pReader->coder);
16,034,028✔
47
  tdbFree(pReader->pBuf);
16,028,143✔
48
  pReader->pBuf = NULL;
16,041,026✔
49
}
16,041,026✔
50

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

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

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

71
  return 0;
24,910,973✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
769,097✔
84
  return true;
769,094✔
85
}
86

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

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

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

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

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

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

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

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

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

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

130
  metaRLock(pMeta);
158,060✔
131

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

137
  metaULock(pMeta);
158,090✔
138

139
  return uid;
158,082✔
140
}
141

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

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

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

178
  SMetaReader *pReader = &mr;
433,592✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
431,881✔
187

188
  metaReaderClear(&mr);
431,881✔
189

190
  return 0;
431,879✔
191
}
192

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

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

208
  metaReaderClear(&mr);
431,872✔
209
  return code;
431,879✔
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) {
917,322✔
245
  SMTbCursor *pTbCur = NULL;
917,322✔
246
  int32_t     code;
247

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

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,839,142✔
267
  if (pTbCur) {
1,839,142✔
268
    tdbFree(pTbCur->pKey);
920,080✔
269
    tdbFree(pTbCur->pVal);
920,053✔
270
    if (!pTbCur->paused) {
920,072✔
271
      metaReaderClear(&pTbCur->mr);
76,403✔
272
      if (pTbCur->pDbc) {
76,404!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
76,406✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
920,065!
277
  }
278
}
1,839,213✔
279

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

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

317
    pTbCur->paused = 0;
919,576✔
318
  }
319

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

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

332
  for (;;) {
333
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
11,620,241✔
334
    if (ret < 0) {
11,631,639✔
335
      return ret;
920,040✔
336
    }
337

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

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
10,724,891✔
341
    if (ret) return ret;
10,695,060!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
10,695,060✔
344
      continue;
2,406,261✔
345
    }
346

347
    break;
8,288,799✔
348
  }
349

350
  return 0;
8,288,799✔
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) {
4,982,715✔
382
  void           *pData = NULL;
4,982,715✔
383
  int             nData = 0;
4,982,715✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
4,982,715✔
386
  SSchemaWrapper *pSchema = NULL;
4,982,715✔
387
  SDecoder        dc = {0};
4,982,715✔
388
  if (lock) {
4,982,715✔
389
    metaRLock(pMeta);
4,973,642✔
390
  }
391
_query:
5,002,590✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
5,082,516✔
393
    goto _err;
444✔
394
  }
395

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

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

402
  SMetaEntry me = {0};
5,082,463✔
403
  tDecoderInit(&dc, pData, nData);
5,082,463✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
5,076,045✔
405
  if (code) {
5,076,818!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
5,076,818✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,657,691!
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,656,234✔
412
      tDecoderClear(&dc);
4,656,234✔
413
      goto _exit;
4,661,795✔
414
    }
415
  } else if (me.type == TSDB_CHILD_TABLE) {
419,127✔
416
    uid = me.ctbEntry.suid;
79,856✔
417
    tDecoderClear(&dc);
79,856✔
418
    goto _query;
79,926✔
419
  } else {
420
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
339,271!
421
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
339,378✔
422
      tDecoderClear(&dc);
339,378✔
423
      goto _exit;
339,353✔
424
    }
425
  }
UNCOV
426
  tDecoderClear(&dc);
×
427

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

433
  tDecoderInit(&dc, pData, nData);
145✔
434
  if (tDecodeSSchemaWrapperEx(&dc, &schema) != 0) {
145!
435
    goto _err;
×
436
  }
437
  pSchema = tCloneSSchemaWrapper(&schema);
145✔
438
  tDecoderClear(&dc);
145✔
439

440
_exit:
5,001,293✔
441
  if (lock) {
5,001,293✔
442
    metaULock(pMeta);
4,967,396✔
443
  }
444
  tdbFree(pData);
5,012,342✔
445
  return pSchema;
5,002,640✔
446

447
_err:
444✔
448
  if (lock) {
444✔
449
    metaULock(pMeta);
443✔
450
  }
451
  tdbFree(pData);
444✔
452
  return NULL;
444✔
453
}
454

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

465
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
8!
466
    goto _exit;
×
467
  }
468

469
  version = ((SUidIdxVal *)pData)[0].version;
8✔
470

471
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
8!
472
    goto _exit;
×
473
  }
474

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

487
  _exit:
8✔
488
  if (lock) {
8!
489
    metaULock(pMeta);
8✔
490
  }
491
  tdbFree(pData);
8✔
492
  return createTime;
8✔
493
}
494

495
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
3,916,728✔
496
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
3,916,728✔
497
  SMCtbCursor *pCtbCur = NULL;
3,916,728✔
498
  SCtbIdxKey   ctbIdxKey;
499
  int          ret = 0;
3,916,728✔
500
  int          c = 0;
3,916,728✔
501

502
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,916,728!
503
  if (pCtbCur == NULL) {
3,929,147!
504
    return NULL;
×
505
  }
506

507
  pCtbCur->pMeta = pMeta;
3,929,147✔
508
  pCtbCur->suid = uid;
3,929,147✔
509
  pCtbCur->lock = lock;
3,929,147✔
510
  pCtbCur->paused = 1;
3,929,147✔
511

512
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,929,147✔
513
  if (ret < 0) {
3,928,412!
514
    return NULL;
×
515
  }
516
  return pCtbCur;
3,928,412✔
517
}
518

519
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,928,281✔
520
  if (pCtbCur) {
3,928,281!
521
    if (!pCtbCur->paused) {
3,928,907✔
522
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,885,911!
523
      if (pCtbCur->pCur) {
3,887,475!
524
        tdbTbcClose(pCtbCur->pCur);
3,887,707✔
525
      }
526
    }
527
    tdbFree(pCtbCur->pKey);
3,928,849✔
528
    tdbFree(pCtbCur->pVal);
3,928,678✔
529
  }
530
  taosMemoryFree(pCtbCur);
3,929,754!
531
}
3,930,740✔
532

533
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,335✔
534
  if (!pCtbCur->paused) {
43,335!
535
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,347✔
536
    if (pCtbCur->lock) {
43,380!
537
      metaULock(pCtbCur->pMeta);
43,380✔
538
    }
539
    pCtbCur->paused = 1;
43,384✔
540
  }
541
}
43,372✔
542

543
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,920,931✔
544
  if (pCtbCur->paused) {
3,920,931!
545
    pCtbCur->paused = 0;
3,924,133✔
546

547
    if (pCtbCur->lock) {
3,924,133✔
548
      metaRLock(pCtbCur->pMeta);
3,906,360✔
549
    }
550
    int ret = 0;
3,925,638✔
551
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,925,638✔
552
    if (ret < 0) {
3,925,605!
553
      metaCloseCtbCursor(pCtbCur);
×
554
      return -1;
×
555
    }
556

557
    if (first) {
3,926,768!
558
      SCtbIdxKey ctbIdxKey;
559
      // move to the suid
560
      ctbIdxKey.suid = pCtbCur->suid;
3,926,768✔
561
      ctbIdxKey.uid = INT64_MIN;
3,926,768✔
562
      int c = 0;
3,926,768✔
563
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,926,768✔
564
      if (c > 0) {
3,928,827✔
565
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
969,493✔
566
      }
567
    } else {
568
      int c = 0;
×
569
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
570
      if (c < 0) {
×
571
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
572
      } else {
573
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
574
      }
575
    }
576
  }
577
  return 0;
3,925,165✔
578
}
579

580
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
16,097,246✔
581
  int         ret;
582
  SCtbIdxKey *pCtbIdxKey;
583

584
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
16,097,246✔
585
  if (ret < 0) {
16,109,168✔
586
    return 0;
2,127,986✔
587
  }
588

589
  pCtbIdxKey = pCtbCur->pKey;
13,981,182✔
590
  if (pCtbIdxKey->suid > pCtbCur->suid) {
13,981,182✔
591
    return 0;
1,802,583✔
592
  }
593

594
  return pCtbIdxKey->uid;
12,178,599✔
595
}
596

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

607
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
158,962✔
608
  SMStbCursor *pStbCur = NULL;
158,962✔
609
  int          ret = 0;
158,962✔
610
  int          c = 0;
158,962✔
611

612
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
158,962!
613
  if (pStbCur == NULL) {
158,964!
614
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
615
    return NULL;
×
616
  }
617

618
  pStbCur->pMeta = pMeta;
158,964✔
619
  pStbCur->suid = suid;
158,964✔
620
  metaRLock(pMeta);
158,964✔
621

622
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
158,968✔
623
  if (ret < 0) {
158,965!
624
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
625
    metaULock(pMeta);
×
626
    taosMemoryFree(pStbCur);
×
627
    return NULL;
×
628
  }
629

630
  // move to the suid
631
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
158,965✔
632
  if (c > 0) {
158,966!
633
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
634
  }
635

636
  return pStbCur;
158,967✔
637
}
638

639
void metaCloseStbCursor(SMStbCursor *pStbCur) {
158,966✔
640
  if (pStbCur) {
158,966!
641
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
158,966!
642
    if (pStbCur->pCur) {
158,967✔
643
      tdbTbcClose(pStbCur->pCur);
158,965✔
644

645
      tdbFree(pStbCur->pKey);
158,965✔
646
      tdbFree(pStbCur->pVal);
158,965✔
647
    }
648

649
    taosMemoryFree(pStbCur);
158,965!
650
  }
651
}
158,968✔
652

653
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
304,059✔
654
  int ret;
655

656
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
304,059✔
657
  if (ret < 0) {
304,062✔
658
    return 0;
158,967✔
659
  }
660
  return *(tb_uid_t *)pStbCur->pKey;
145,095✔
661
}
662

663
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
4,838,037✔
664
  STSchema       *pTSchema = NULL;
4,838,037✔
665
  SSchemaWrapper *pSW = NULL;
4,838,037✔
666

667
  pSW = metaGetTableSchema(pMeta, uid, sver, lock);
4,838,037✔
668
  if (!pSW) return NULL;
4,855,080✔
669

670
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
4,855,079✔
671

672
  taosMemoryFree(pSW->pSchema);
4,857,873✔
673
  taosMemoryFree(pSW);
4,857,823!
674
  return pTSchema;
4,857,177✔
675
}
676

677
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
10,127✔
678
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
10,127✔
679
  if (*ppTSchema == NULL) {
10,144!
680
    return terrno;
×
681
  }
682
  return TSDB_CODE_SUCCESS;
10,145✔
683
}
684

685
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,829,219✔
686
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,829,219✔
687
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,844,541!
UNCOV
688
    return terrno;
×
689
  }
690
  return TSDB_CODE_SUCCESS;
4,844,872✔
691
}
692

693
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
468,619✔
694
  int32_t code = 0;
468,619✔
695
  int32_t lino;
696

697
  void     *pData = NULL;
468,619✔
698
  int       nData = 0;
468,619✔
699
  SSkmDbKey skmDbKey;
700
  if (sver <= 0) {
468,619✔
701
    SMetaInfo info;
702
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
230,288✔
703
      sver = info.skmVer;
230,259✔
704
    } else {
705
      TBC *pSkmDbC = NULL;
1✔
706
      int  c;
707

708
      skmDbKey.uid = suid ? suid : uid;
1!
709
      skmDbKey.sver = INT32_MAX;
1✔
710

711
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
1✔
712
      TSDB_CHECK_CODE(code, lino, _exit);
1!
713
      metaRLock(pMeta);
1✔
714

715
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
1!
716
        metaULock(pMeta);
×
717
        tdbTbcClose(pSkmDbC);
×
718
        code = TSDB_CODE_NOT_FOUND;
×
719
        goto _exit;
×
720
      }
721

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

730
      if (c < 0) {
1!
731
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
1✔
732
      }
733

734
      const void *pKey = NULL;
1✔
735
      int32_t     nKey = 0;
1✔
736
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
1✔
737

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

745
      sver = ((SSkmDbKey *)pKey)->sver;
1✔
746

747
      metaULock(pMeta);
1✔
748
      tdbTbcClose(pSkmDbC);
1✔
749
    }
750
  }
751

752
  if (!(sver > 0)) {
468,583!
753
    code = TSDB_CODE_NOT_FOUND;
×
754
    goto _exit;
×
755
  }
756

757
  skmDbKey.uid = suid ? suid : uid;
468,583✔
758
  skmDbKey.sver = sver;
468,583✔
759
  metaRLock(pMeta);
468,583✔
760
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
468,734!
761
    metaULock(pMeta);
×
762
    code = TSDB_CODE_NOT_FOUND;
×
763
    goto _exit;
×
764
  }
765
  metaULock(pMeta);
468,624✔
766

767
  // decode
768
  SDecoder        dc = {0};
468,683✔
769
  SSchemaWrapper  schema;
770
  SSchemaWrapper *pSchemaWrapper = &schema;
468,683✔
771

772
  tDecoderInit(&dc, pData, nData);
468,683✔
773
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
467,856✔
774
  tDecoderClear(&dc);
467,856✔
775
  tdbFree(pData);
468,475✔
776
  if (TSDB_CODE_SUCCESS != code) {
468,564!
777
    taosMemoryFree(pSchemaWrapper->pSchema);
×
778
    goto _exit;
×
779
  }
780

781
  // convert
782
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
468,564✔
783
  if (pTSchema == NULL) {
468,573!
784
    code = TSDB_CODE_OUT_OF_MEMORY;
×
785
  }
786

787
  *ppTSchema = pTSchema;
468,573✔
788
  taosMemoryFree(pSchemaWrapper->pSchema);
468,573✔
789

790
_exit:
468,701✔
791
  return code;
468,701✔
792
}
793

794
// N.B. Called by statusReq per second
795
int64_t metaGetTbNum(SMeta *pMeta) {
1,044,953✔
796
  // num of child tables (excluding normal tables , stables and others)
797

798
  /* int64_t num = 0; */
799
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
800

801
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
1,044,953✔
802
}
803

804
void metaUpdTimeSeriesNum(SMeta *pMeta) {
158,947✔
805
  int64_t nCtbTimeSeries = 0;
158,947✔
806
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
158,947!
807
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
158,951✔
808
  }
809
}
158,950✔
810

811
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
812
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
813
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,205,078✔
814
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,205,099✔
815
    metaUpdTimeSeriesNum(pMeta);
156,582✔
816
  }
817

818
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,205,114✔
819
}
820

821
// type: 1 reported timeseries
822
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,205,078!
823
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,205,114✔
824
  if (type == 1) {
1,205,114✔
825
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
1,044,953✔
826
  }
827
  return nTimeSeries;
1,205,114✔
828
}
829

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

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

846
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
847
  if (pSmaCur == NULL) {
×
848
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
849
    return NULL;
×
850
  }
851

852
  pSmaCur->pMeta = pMeta;
×
853
  pSmaCur->uid = uid;
×
854
  metaRLock(pMeta);
×
855

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

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

871
  return pSmaCur;
×
872
}
873

874
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
875
  if (pSmaCur) {
×
876
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
877
    if (pSmaCur->pCur) {
×
878
      tdbTbcClose(pSmaCur->pCur);
×
879
      pSmaCur->pCur = NULL;
×
880

881
      tdbFree(pSmaCur->pKey);
×
882
      tdbFree(pSmaCur->pVal);
×
883
    }
884

885
    taosMemoryFree(pSmaCur);
×
886
  }
887
}
×
888

889
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
890
  int         ret;
891
  SSmaIdxKey *pSmaIdxKey;
892

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

898
  pSmaIdxKey = pSmaCur->pKey;
×
899
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
900
    return 0;
×
901
  }
902

903
  return pSmaIdxKey->uid;
×
904
}
905

906
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
907
  STSmaWrapper *pSW = NULL;
×
908
  SArray       *pSmaIds = NULL;
×
909

910
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
911
    return NULL;
×
912
  }
913

914
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
915
  if (!pSW) {
×
916
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
917
    goto _err;
×
918
  }
919

920
  pSW->number = taosArrayGetSize(pSmaIds);
×
921
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
922

923
  if (!pSW->tSma) {
×
924
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
925
    goto _err;
×
926
  }
927

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

965
    ++smaIdx;
×
966
  }
967

968
  if (smaIdx <= 0) goto _err;
×
969
  pSW->number = smaIdx;
×
970

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

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

997
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
3✔
998

999
  metaReaderClear(&mr);
3✔
1000
  return pTSma;
3✔
1001
}
1002

1003
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1004
  SArray     *pUids = NULL;
×
1005
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1006

1007
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1008
  if (!pCur) {
×
1009
    return NULL;
×
1010
  }
1011

1012
  while (1) {
×
1013
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1014
    if (id == 0) {
×
1015
      break;
×
1016
    }
1017

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

1027
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1028

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

1037
  metaCloseSmaCursor(pCur);
×
1038
  return pUids;
×
1039
}
1040

1041
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1042
  SArray     *pUids = NULL;
×
1043
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1044
  tb_uid_t    lastUid = 0;
×
1045

1046
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1047
  if (!pCur) {
×
1048
    return NULL;
×
1049
  }
1050

1051
  while (1) {
×
1052
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1053
    if (uid == 0) {
×
1054
      break;
×
1055
    }
1056

1057
    if (lastUid == uid) {
×
1058
      continue;
×
1059
    }
1060

1061
    lastUid = uid;
×
1062

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

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

1080
  metaCloseSmaCursor(pCur);
×
1081
  return pUids;
×
1082
}
1083

1084
#endif
1085

1086
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
22,111,631✔
1087
  STag *tag = (STag *)pTag;
22,111,631✔
1088
  if (type == TSDB_DATA_TYPE_JSON) {
22,111,631✔
1089
    return tag;
10,033✔
1090
  }
1091
  bool find = tTagGet(tag, val);
22,101,598✔
1092

1093
  if (!find) {
22,139,041✔
1094
    return NULL;
128,861✔
1095
  }
1096

1097
  return val;
22,010,180✔
1098
}
1099

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

1112
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1113
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1114
  SMetaFltParam *param = arg;
6✔
1115
  int32_t        ret = 0;
6✔
1116

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

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

1134
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
6✔
1135
  SBtimeIdxKey *pBtimeKey = &btimeKey;
6✔
1136

1137
  int cmp = 0;
6✔
1138
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
6!
1139
    goto END;
×
1140
  }
1141

1142
  int32_t valid = 0;
6✔
1143
  int32_t count = 0;
6✔
1144

1145
  static const int8_t TRY_ERROR_LIMIT = 1;
1146
  do {
31,474✔
1147
    void   *entryKey = NULL;
31,480✔
1148
    int32_t nEntryKey = -1;
31,480✔
1149
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
31,480✔
1150
    if (valid < 0) break;
31,327✔
1151

1152
    SBtimeIdxKey *p = entryKey;
31,321✔
1153
    if (count > TRY_ERROR_LIMIT) break;
31,321!
1154

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

1176
END:
6✔
1177
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1178
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1179
  taosMemoryFree(pCursor);
6!
1180
  return ret;
6✔
1181
}
1182

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

1189
  STagIdxKey *pKey = NULL;
×
1190
  int32_t     nKey = 0;
×
1191

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

1202
  char *pName = param->val;
×
1203

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

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

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

1224
    if (count > TRY_ERROR_LIMIT) break;
×
1225

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

1251
END:
×
1252
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1253
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1254
  taosMemoryFree(buf);
×
1255
  taosMemoryFree(pKey);
×
1256

1257
  taosMemoryFree(pCursor);
×
1258

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

1267
  STtlIdxKey *pKey = NULL;
×
1268
  int32_t     nKey = 0;
×
1269

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

1280
  metaRLock(pMeta);
×
1281
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1282

1283
END:
×
1284
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1285
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1286
  taosMemoryFree(buf);
×
1287
  taosMemoryFree(pKey);
×
1288

1289
  taosMemoryFree(pCursor);
×
1290

1291
  return ret;
×
1292
  // impl later
1293
  return 0;
1294
}
1295
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
2,582✔
1296
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
2,582✔
1297
  SMetaFltParam *param = arg;
2,582✔
1298

1299
  SMetaEntry oStbEntry = {0};
2,582✔
1300
  int32_t    code = 0;
2,582✔
1301
  char      *buf = NULL;
2,582✔
1302
  void      *pData = NULL;
2,582✔
1303
  int        nData = 0;
2,582✔
1304

1305
  SDecoder    dc = {0};
2,582✔
1306
  STbDbKey    tbDbKey = {0};
2,582✔
1307
  STagIdxKey *pKey = NULL;
2,582✔
1308
  int32_t     nKey = 0;
2,582✔
1309

1310
  SIdxCursor *pCursor = NULL;
2,582✔
1311
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
2,582!
1312
  if (!pCursor) {
2,592!
1313
    return terrno;
×
1314
  }
1315
  pCursor->pMeta = pMeta;
2,592✔
1316
  pCursor->suid = param->suid;
2,592✔
1317
  pCursor->cid = param->cid;
2,592✔
1318
  pCursor->type = param->type;
2,592✔
1319

1320
  metaRLock(pMeta);
2,592✔
1321

1322
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
2,604!
1323

1324
  tbDbKey.uid = param->suid;
2,601✔
1325
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
2,601✔
1326

1327
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
2,601!
1328

1329
  tDecoderInit(&dc, pData, nData);
2,598✔
1330

1331
  code = metaDecodeEntry(&dc, &oStbEntry);
2,589✔
1332
  if (code) {
2,597!
1333
    tDecoderClear(&dc);
×
1334
    goto END;
×
1335
  }
1336

1337
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
2,597!
1338
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1339
  }
1340

1341
  code = TSDB_CODE_INVALID_PARA;
2,597✔
1342
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
3,812✔
1343
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
3,385✔
1344
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
3,385!
1345
      code = 0;
419✔
1346
    } else {
1347
      TAOS_CHECK_GOTO(code, NULL, END);
2,966✔
1348
    }
1349
  }
1350

1351
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
427✔
1352
  if (code != 0) {
422✔
1353
    TAOS_CHECK_GOTO(terrno, NULL, END);
2!
1354
  }
1355

1356
  int32_t maxSize = 0;
420✔
1357
  int32_t nTagData = 0;
420✔
1358
  void   *tagData = NULL;
420✔
1359

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

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

1375
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
×
1376
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1377
        }
1378

1379
        tagData = buf;
×
1380
        nTagData = maxSize;
×
1381
      }
1382
    } else {
1383
      tagData = param->val;
419✔
1384
      nTagData = tDataTypes[param->type].bytes;
419✔
1385
    }
1386
  }
1387

1388
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
420!
1389
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1390
                  NULL, END);
1391

1392
  int cmp = 0;
421✔
1393
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
421!
1394

1395
  int     count = 0;
421✔
1396
  int32_t valid = 0;
421✔
1397
  bool    found = false;
421✔
1398

1399
  static const int8_t TRY_ERROR_LIMIT = 1;
1400

1401
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1402
  /// target:                        [suid, cid2, type2]
1403
  int diffCidCount = 0;
421✔
1404
  do {
10,348✔
1405
    void   *entryKey = NULL, *entryVal = NULL;
10,769✔
1406
    int32_t nEntryKey, nEntryVal;
1407

1408
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
10,769✔
1409
    if (valid < 0) {
10,762✔
1410
      code = valid;
404✔
1411
    }
1412
    if (count > TRY_ERROR_LIMIT) {
10,762✔
1413
      break;
422✔
1414
    }
1415

1416
    STagIdxKey *p = entryKey;
10,741✔
1417
    if (p == NULL) break;
10,741✔
1418

1419
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
10,341!
1420
      if (found == true) break;  //
8✔
1421
      if (diffCidCount > TRY_ERROR_LIMIT) break;
7!
1422
      diffCidCount++;
7✔
1423
      count++;
7✔
1424
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
7!
1425
      if (valid < 0) {
9!
1426
        code = valid;
×
1427
        break;
×
1428
      } else {
1429
        continue;
9✔
1430
      }
1431
    }
1432

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

1464
END:
2,592✔
1465
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
2,592✔
1466
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
2,603✔
1467
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
2,603!
1468
  tDecoderClear(&dc);
2,603✔
1469
  tdbFree(pData);
2,602✔
1470

1471
  taosMemoryFree(buf);
2,604!
1472
  taosMemoryFree(pKey);
2,603!
1473

1474
  taosMemoryFree(pCursor);
2,604!
1475

1476
  return code;
2,602✔
1477
}
1478

1479
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
265,038✔
1480
  int ret = 0;
265,038✔
1481
  if (lock) {
265,038!
1482
    metaRLock(pMeta);
×
1483
  }
1484

1485
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
265,038✔
1486
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
265,038✔
1487
  if (lock) {
265,040!
1488
    metaULock(pMeta);
×
1489
  }
1490

1491
  return ret;
265,040✔
1492
}
1493

1494
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
265,035✔
1495
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
265,035✔
1496
  const int32_t LIMIT = 128;
265,035✔
1497

1498
  int32_t isLock = false;
265,035✔
1499
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
265,035!
1500
  for (int i = 0; i < sz; i++) {
530,074✔
1501
    STUidTagInfo *p = taosArrayGet(uidList, i);
265,038✔
1502

1503
    if (i % LIMIT == 0) {
265,037✔
1504
      if (isLock) metaULock(pMeta);
265,026!
1505

1506
      metaRLock(pMeta);
265,026✔
1507
      isLock = true;
265,028✔
1508
    }
1509

1510
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1511
    void   *val = NULL;
265,039✔
1512
    int32_t len = 0;
265,039✔
1513
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
265,039!
1514
      p->pTagVal = taosMemoryMalloc(len);
265,040!
1515
      if (!p->pTagVal) {
265,040!
1516
        if (isLock) metaULock(pMeta);
×
1517

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

1532
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
960,583✔
1533
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
960,583✔
1534
  if (!pCur) {
963,755!
1535
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1536
  }
1537

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

1557
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
963,117✔
1558
    while (1) {
2,503,210✔
1559
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,371,973✔
1560
      if (uid == 0) {
3,370,917✔
1561
        break;
869,902✔
1562
      }
1563

1564
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,501,015✔
1565
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,501,015!
1566
      if (!info.pTagVal) {
2,507,698!
1567
        metaCloseCtbCursor(pCur);
×
1568
        taosHashCleanup(pSepecifiedUidMap);
×
1569
        return terrno;
×
1570
      }
1571
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,507,698✔
1572
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,503,210!
1573
        taosMemoryFreeClear(info.pTagVal);
×
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
    }
1579
  } else {  // only the specified tables need to be added
1580
    while (1) {
383,536✔
1581
      tb_uid_t uid = metaCtbCursorNext(pCur);
477,890✔
1582
      if (uid == 0) {
478,122✔
1583
        break;
94,128✔
1584
      }
1585

1586
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
383,994✔
1587
      if (index == NULL) {
383,990✔
1588
        continue;
98,637✔
1589
      }
1590

1591
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
285,353✔
1592
      if (pTagInfo->pTagVal == NULL) {
285,293!
1593
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
285,710✔
1594
        if (!pTagInfo->pTagVal) {
285,316!
1595
          metaCloseCtbCursor(pCur);
×
1596
          taosHashCleanup(pSepecifiedUidMap);
×
1597
          return terrno;
×
1598
        }
1599
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
285,316✔
1600
      }
1601
    }
1602
  }
1603

1604
  taosHashCleanup(pSepecifiedUidMap);
964,030✔
1605
  metaCloseCtbCursor(pCur);
963,677✔
1606
  return TSDB_CODE_SUCCESS;
964,197✔
1607
}
1608

1609
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1610

1611
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
32,121,750✔
1612
  int32_t code = 0;
32,121,750✔
1613
  void   *pData = NULL;
32,121,750✔
1614
  int     nData = 0;
32,121,750✔
1615
  int     lock = 0;
32,121,750✔
1616

1617
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
32,121,750!
1618
    lock = 1;
7,595,066✔
1619
  }
1620

1621
  if (!lock) metaRLock(pMeta);
32,121,750✔
1622

1623
  // search cache
1624
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
32,125,761✔
1625
    if (!lock) metaULock(pMeta);
31,886,893✔
1626
    goto _exit;
31,889,928✔
1627
  }
1628

1629
  // search TDB
1630
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
243,870✔
1631
    // not found
1632
    if (!lock) metaULock(pMeta);
219,639✔
1633
    goto _exit;
219,633✔
1634
  }
1635

1636
  if (!lock) metaULock(pMeta);
24,555✔
1637

1638
  pInfo->uid = uid;
24,550✔
1639
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
24,550✔
1640
  pInfo->version = ((SUidIdxVal *)pData)->version;
24,550✔
1641
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
24,550✔
1642

1643
  if (lock) {
24,550✔
1644
    metaULock(pReader->pMeta);
2,260✔
1645
    // metaReaderReleaseLock(pReader);
1646
  }
1647
  // upsert the cache
1648
  metaWLock(pMeta);
24,550✔
1649
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
24,556✔
1650
  if (ret != 0) {
24,555!
1651
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1652
  }
1653
  metaULock(pMeta);
24,555✔
1654

1655
  if (lock) {
24,555✔
1656
    metaRLock(pReader->pMeta);
2,261✔
1657
  }
1658

1659
_exit:
22,294✔
1660
  tdbFree(pData);
32,134,115✔
1661
  return code;
32,131,778✔
1662
}
1663

1664
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
296,311✔
1665
  int32_t code = 0;
296,311✔
1666

1667
  if (!numOfTables && !numOfCols) goto _exit;
296,311!
1668

1669
  SVnode *pVnodeObj = pVnode;
296,311✔
1670
  metaRLock(pVnodeObj->pMeta);
296,311✔
1671

1672
  // fast path: search cache
1673
  SMetaStbStats state = {0};
296,359✔
1674
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
296,359✔
1675
    metaULock(pVnodeObj->pMeta);
273,714✔
1676
    if (numOfTables) *numOfTables = state.ctbNum;
273,728✔
1677
    if (numOfCols) *numOfCols = state.colNum;
273,728✔
1678
    goto _exit;
273,728✔
1679
  }
1680

1681
  // slow path: search TDB
1682
  int64_t ctbNum = 0;
22,630✔
1683
  int32_t colNum = 0;
22,630✔
1684
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
22,630✔
1685
  if (TSDB_CODE_SUCCESS == code) {
22,627!
1686
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
22,631✔
1687
  }
1688
  metaULock(pVnodeObj->pMeta);
22,623✔
1689
  if (TSDB_CODE_SUCCESS != code) {
22,631!
1690
    goto _exit;
×
1691
  }
1692

1693
  if (numOfTables) *numOfTables = ctbNum;
22,631✔
1694
  if (numOfCols) *numOfCols = colNum;
22,631✔
1695

1696
  state.uid = uid;
22,631✔
1697
  state.ctbNum = ctbNum;
22,631✔
1698
  state.colNum = colNum;
22,631✔
1699

1700
  // upsert the cache
1701
  metaWLock(pVnodeObj->pMeta);
22,631✔
1702

1703
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
22,629✔
1704
  if (ret) {
22,628!
1705
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d", uid, ctbNum, colNum);
×
1706
  }
1707

1708
  metaULock(pVnodeObj->pMeta);
22,628✔
1709

1710
_exit:
296,337✔
1711
  return code;
296,337✔
1712
}
1713

1714
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol) {
159,307✔
1715
  SMetaStbStats stats = {0};
159,307✔
1716

1717
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
159,307✔
1718
    stats.ctbNum += deltaCtb;
144,759✔
1719
    stats.colNum += deltaCol;
144,759✔
1720
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
144,759✔
1721
    if (code) {
144,763!
1722
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d",
×
1723
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol);
1724
    }
1725
  }
1726
}
159,358✔
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