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

taosdata / TDengine / #3910

23 Apr 2025 02:47AM UTC coverage: 62.362% (-0.7%) from 63.063%
#3910

push

travis-ci

web-flow
docs(datain): add missing health status types (#30828)

155061 of 317305 branches covered (48.87%)

Branch coverage included in aggregate %.

240172 of 316469 relevant lines covered (75.89%)

6269478.46 hits per line

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

55.96
/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) {
2,799,550✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,799,550✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,799,550✔
23
  pReader->pAPI = pAPI;
2,801,018✔
24
}
2,801,018✔
25

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

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,012,398✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,012,398!
37
    metaULock(pReader->pMeta);
1,012,775✔
38
    pReader->flags |= META_READER_NOLOCK;
1,013,518✔
39
  }
40
}
1,013,141✔
41

42
void metaReaderClear(SMetaReader *pReader) {
3,156,526✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
3,156,526✔
44
    metaULock(pReader->pMeta);
1,629,179✔
45
  }
46
  tDecoderClear(&pReader->coder);
3,156,798✔
47
  tdbFree(pReader->pBuf);
3,155,311✔
48
  pReader->pBuf = NULL;
3,156,614✔
49
}
3,156,614✔
50

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

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

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

71
  return 0;
4,043,914✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
518,853✔
84
  return true;
518,855✔
85
}
86

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

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

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

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

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

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

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

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

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

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

130
  metaRLock(pMeta);
111,775✔
131

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

137
  metaULock(pMeta);
111,763✔
138

139
  return uid;
111,796✔
140
}
141

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

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

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

178
  SMetaReader *pReader = &mr;
6,174✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
3,411✔
187

188
  metaReaderClear(&mr);
3,411✔
189

190
  return 0;
3,411✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
3,412✔
199
  if (code == 0) *tbType = mr.me.type;
3,410!
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
3,410✔
201
    *suid = mr.me.ctbEntry.suid;
3,389✔
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);
3,410✔
209
  return code;
3,411✔
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) {
22,820✔
245
  SMTbCursor *pTbCur = NULL;
22,820✔
246
  int32_t     code;
247

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

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
45,657✔
267
  if (pTbCur) {
45,657✔
268
    tdbFree(pTbCur->pKey);
22,841✔
269
    tdbFree(pTbCur->pVal);
22,841✔
270
    if (!pTbCur->paused) {
22,841✔
271
      metaReaderClear(&pTbCur->mr);
3,199✔
272
      if (pTbCur->pDbc) {
3,199!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
3,200✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
22,840!
277
  }
278
}
45,657✔
279

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

299
    if (first) {
23,757✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
22,835✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
22,837!
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;
23,759✔
318
  }
319

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

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
732,996✔
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);
732,996✔
334
    if (ret < 0) {
732,367✔
335
      return ret;
22,837✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
709,530✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
710,336✔
341
    if (ret) return ret;
708,544!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
708,544✔
344
      continue;
13,196✔
345
    }
346

347
    break;
695,348✔
348
  }
349

350
  return 0;
695,348✔
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) {
1,418,022✔
382
  void           *pData = NULL;
1,418,022✔
383
  int             nData = 0;
1,418,022✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
1,418,022✔
386
  SSchemaWrapper *pSchema = NULL;
1,418,022✔
387
  SDecoder        dc = {0};
1,418,022✔
388
  if (lock) {
1,418,022✔
389
    metaRLock(pMeta);
1,401,563✔
390
  }
391
_query:
1,420,489✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,501,118✔
393
    goto _err;
741✔
394
  }
395

396
  version = ((SUidIdxVal *)pData)[0].version;
1,500,289✔
397

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

402
  SMetaEntry me = {0};
1,500,639✔
403
  tDecoderInit(&dc, pData, nData);
1,500,639✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
1,499,796✔
405
  if (code) {
1,500,051!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
1,500,051✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
1,147,014✔
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
1,146,319✔
412
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
1,146,319✔
413
      tDecoderClear(&dc);
1,146,325✔
414
      goto _exit;
1,147,137✔
415
    }
416
  } else if (me.type == TSDB_CHILD_TABLE) {
353,037✔
417
    uid = me.ctbEntry.suid;
80,598✔
418
    tDecoderClear(&dc);
80,598✔
419
    goto _query;
80,629✔
420
  } else {
421
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
272,439✔
422
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
272,538✔
423
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
272,538✔
424
      tDecoderClear(&dc);
272,542✔
425
      goto _exit;
272,573✔
426
    }
427
  }
428
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
120!
429
  tDecoderClear(&dc);
120✔
430

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

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

443
_exit:
1,419,855✔
444
  if (lock) {
1,419,855✔
445
    metaULock(pMeta);
1,400,788✔
446
  }
447
  tdbFree(pData);
1,421,536✔
448
  return pSchema;
1,420,046✔
449

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

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

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

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

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

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

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

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

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

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

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

522
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
842,980✔
523
  if (pCtbCur) {
842,980!
524
    if (!pCtbCur->paused) {
843,126✔
525
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
801,302!
526
      if (pCtbCur->pCur) {
801,707!
527
        tdbTbcClose(pCtbCur->pCur);
801,729✔
528
      }
529
    }
530
    tdbFree(pCtbCur->pKey);
843,291✔
531
    tdbFree(pCtbCur->pVal);
843,339✔
532
  }
533
  taosMemoryFree(pCtbCur);
843,443!
534
}
843,656✔
535

536
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
41,888✔
537
  if (!pCtbCur->paused) {
41,888!
538
    tdbTbcClose((TBC *)pCtbCur->pCur);
41,904✔
539
    if (pCtbCur->lock) {
41,950!
540
      metaULock(pCtbCur->pMeta);
41,951✔
541
    }
542
    pCtbCur->paused = 1;
41,955✔
543
  }
544
}
41,939✔
545

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

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

560
    if (first) {
843,153!
561
      SCtbIdxKey ctbIdxKey;
562
      // move to the suid
563
      ctbIdxKey.suid = pCtbCur->suid;
843,153✔
564
      ctbIdxKey.uid = INT64_MIN;
843,153✔
565
      int c = 0;
843,153✔
566
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
843,153✔
567
      if (c > 0) {
842,841✔
568
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
196,375✔
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;
842,156✔
581
}
582

583
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
4,103,336✔
584
  int         ret;
585
  SCtbIdxKey *pCtbIdxKey;
586

587
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
4,103,336✔
588
  if (ret < 0) {
4,103,597✔
589
    return 0;
664,714✔
590
  }
591

592
  pCtbIdxKey = pCtbCur->pKey;
3,438,883✔
593
  if (pCtbIdxKey->suid > pCtbCur->suid) {
3,438,883✔
594
    return 0;
179,289✔
595
  }
596

597
  return pCtbIdxKey->uid;
3,259,594✔
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) {
100,712✔
611
  SMStbCursor *pStbCur = NULL;
100,712✔
612
  int          ret = 0;
100,712✔
613
  int          c = 0;
100,712✔
614

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

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

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

639
  return pStbCur;
100,716✔
640
}
641

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

648
      tdbFree(pStbCur->pKey);
100,716✔
649
      tdbFree(pStbCur->pVal);
100,715✔
650
    }
651

652
    taosMemoryFree(pStbCur);
100,716!
653
  }
654
}
100,714✔
655

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

659
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
164,386✔
660
  if (ret < 0) {
164,381✔
661
    return 0;
100,712✔
662
  }
663
  return *(tb_uid_t *)pStbCur->pKey;
63,669✔
664
}
665

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

670
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
1,276,962✔
671
  if (!pSW) return NULL;
1,278,987✔
672

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

675
  taosMemoryFree(pSW->pSchema);
1,279,359✔
676
  taosMemoryFree(pSW);
1,279,569!
677
  return pTSchema;
1,279,467✔
678
}
679

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

793
_exit:
1,311,859✔
794
  return code;
1,311,859✔
795
}
796

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

807
void metaUpdTimeSeriesNum(SMeta *pMeta) {
100,710✔
808
  int64_t nCtbTimeSeries = 0;
100,710✔
809
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
100,710!
810
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
100,713✔
811
  }
812
}
100,713✔
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;
339,572✔
817
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
339,594✔
818
    metaUpdTimeSeriesNum(pMeta);
99,723✔
819
  }
820

821
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
339,615✔
822
}
823

824
// type: 1 reported timeseries
825
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
339,572!
826
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
339,615✔
827
  if (type == 1) {
339,615✔
828
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
203,703✔
829
  }
830
  return nTimeSeries;
339,615✔
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) {
5,031,498✔
1090
  STag *tag = (STag *)pTag;
5,031,498✔
1091
  if (type == TSDB_DATA_TYPE_JSON) {
5,031,498✔
1092
    return tag;
10,033✔
1093
  }
1094
  bool find = tTagGet(tag, val);
5,021,465✔
1095

1096
  if (!find) {
5,027,999✔
1097
    return NULL;
39,773✔
1098
  }
1099

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

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

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

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

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

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

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

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

1158
    terrno = TSDB_CODE_SUCCESS;
34,994✔
1159
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
35,470✔
1160
    if (terrno != TSDB_CODE_SUCCESS) {
35,279!
1161
      ret = terrno;
×
1162
      break;
×
1163
    }
1164
    if (cmp == 0) {
35,392!
1165
      if (taosArrayPush(pUids, &p->uid) == NULL) {
71,094!
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);
35,702✔
1176
    if (valid < 0) break;
35,948!
1177
  } while (1);
1178

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

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

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

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

1323
  metaRLock(pMeta);
869✔
1324

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1402
  static const int8_t TRY_ERROR_LIMIT = 1;
1403

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

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

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

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

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

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

1474
  taosMemoryFree(buf);
870!
1475
  taosMemoryFree(pKey);
870!
1476

1477
  taosMemoryFree(pCursor);
870!
1478

1479
  return code;
870✔
1480
}
1481

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

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

1494
  return ret;
308✔
1495
}
1496

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

1501
  int32_t isLock = false;
200✔
1502
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
200!
1503
  for (int i = 0; i < sz; i++) {
508✔
1504
    STUidTagInfo *p = taosArrayGet(uidList, i);
308✔
1505

1506
    if (i % LIMIT == 0) {
308✔
1507
      if (isLock) metaULock(pMeta);
171!
1508

1509
      metaRLock(pMeta);
171✔
1510
      isLock = true;
171✔
1511
    }
1512

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

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

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

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

1560
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
84,804✔
1561
    while (1) {
288,024✔
1562
      tb_uid_t uid = metaCtbCursorNext(pCur);
333,459✔
1563
      if (uid == 0) {
330,073✔
1564
        break;
45,555✔
1565
      }
1566

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

1589
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
212,711✔
1590
      if (index == NULL) {
212,716✔
1591
        continue;
28,460✔
1592
      }
1593

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

1607
  taosHashCleanup(pSepecifiedUidMap);
84,862✔
1608
  metaCloseCtbCursor(pCur);
84,868✔
1609
  return TSDB_CODE_SUCCESS;
84,909✔
1610
}
1611

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

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

1620
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
25,941,815!
1621
    lock = 1;
2,728,776✔
1622
  }
1623

1624
  if (!lock) metaRLock(pMeta);
25,941,815✔
1625

1626
  // search cache
1627
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
25,946,768✔
1628
    if (!lock) metaULock(pMeta);
12,189,259✔
1629
    goto _exit;
12,189,876✔
1630
  }
1631

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

1639
  if (!lock) metaULock(pMeta);
12,129✔
1640

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

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

1658
  if (lock) {
12,147✔
1659
    metaRLock(pReader->pMeta);
2,304✔
1660
  }
1661

1662
_exit:
9,843✔
1663
  tdbFree(pData);
25,949,806✔
1664
  return code;
25,948,434✔
1665
}
1666

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

1670
  if (!numOfTables && !numOfCols) goto _exit;
189,565!
1671

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

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

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

1700
  if (numOfTables) *numOfTables = ctbNum;
17,504✔
1701
  if (numOfCols) *numOfCols = colNum;
17,504✔
1702

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

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

1716
  metaULock(pVnodeObj->pMeta);
17,504✔
1717

1718
_exit:
189,584✔
1719
  return code;
189,584✔
1720
}
1721

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

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

1732
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
120,166✔
1733
    if (code) {
120,153!
1734
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1735
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1736
    }
1737
  }
1738
}
129,774✔
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