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

taosdata / TDengine / #3627

02 Mar 2025 11:16PM UTC coverage: 63.596% (-0.2%) from 63.764%
#3627

push

travis-ci

GitHub
Merge pull request #29973 from taosdata/doc/internal

148665 of 299855 branches covered (49.58%)

Branch coverage included in aggregate %.

233076 of 300407 relevant lines covered (77.59%)

17543856.65 hits per line

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

56.0
/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,213,918✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
11,213,918✔
22
  metaReaderDoInit(pReader, pMeta, flags);
11,213,918✔
23
  pReader->pAPI = pAPI;
11,221,649✔
24
}
11,221,649✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
15,047,853✔
27
  memset(pReader, 0, sizeof(*pReader));
15,047,853✔
28
  pReader->pMeta = pMeta;
15,047,853✔
29
  pReader->flags = flags;
15,047,853✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
15,047,853!
31
    metaRLock(pMeta);
9,913,829✔
32
  }
33
}
15,050,857✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,883,686✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,883,686!
37
    metaULock(pReader->pMeta);
1,883,962✔
38
    pReader->flags |= META_READER_NOLOCK;
1,884,494✔
39
  }
40
}
1,884,218✔
41

42
void metaReaderClear(SMetaReader *pReader) {
15,721,977✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
15,721,977✔
44
    metaULock(pReader->pMeta);
8,031,312✔
45
  }
46
  tDecoderClear(&pReader->coder);
15,720,132✔
47
  tdbFree(pReader->pBuf);
15,735,175✔
48
  pReader->pBuf = NULL;
15,733,893✔
49
}
15,733,893✔
50

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

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

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

71
  return 0;
24,988,751✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
710,646✔
84
  return true;
710,653✔
85
}
86

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

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

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

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

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
7,132,450✔
105
  if (TSDB_CODE_SUCCESS != code) {
7,136,392✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
149!
107
  }
108

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

112
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
1,733,436✔
113
  SMeta   *pMeta = pReader->pMeta;
1,733,436✔
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,733,436✔
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
27,856✔
119
  }
120

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

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

130
  metaRLock(pMeta);
155,622✔
131

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

137
  metaULock(pMeta);
155,632✔
138

139
  return uid;
155,645✔
140
}
141

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

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

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

178
  SMetaReader *pReader = &mr;
449,208✔
179

180
  // query name.idx
181
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
449,208✔
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;
447,495✔
187

188
  metaReaderClear(&mr);
447,495✔
189

190
  return 0;
447,490✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
447,498✔
199
  if (code == 0) *tbType = mr.me.type;
447,492!
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
447,492✔
201
    *suid = mr.me.ctbEntry.suid;
447,471✔
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);
447,492✔
209
  return code;
447,497✔
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) {
949,733✔
245
  SMTbCursor *pTbCur = NULL;
949,733✔
246
  int32_t     code;
247

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
949,733!
249
  if (pTbCur == NULL) {
950,368!
250
    return NULL;
×
251
  }
252

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,904,050✔
267
  if (pTbCur) {
1,904,050✔
268
    tdbFree(pTbCur->pKey);
952,508✔
269
    tdbFree(pTbCur->pVal);
952,423✔
270
    if (!pTbCur->paused) {
952,493✔
271
      metaReaderClear(&pTbCur->mr);
76,583✔
272
      if (pTbCur->pDbc) {
76,581!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
76,583✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
952,483!
277
  }
278
}
1,904,123✔
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
876,599✔
281
  if (!pTbCur->paused) {
876,599!
282
    metaReaderClear(&pTbCur->mr);
876,707✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
876,772✔
284
    pTbCur->paused = 1;
876,597✔
285
  }
286
}
876,489✔
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
951,581✔
288
  int32_t code = 0;
951,581✔
289
  int32_t lino;
290
  int8_t  locked = 0;
951,581✔
291
  if (pTbCur->paused) {
951,581!
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
951,888✔
293
    locked = 1;
952,995✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
952,995✔
295
    if (code != 0) {
951,499!
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
951,499✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
950,683✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
951,162!
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;
951,978✔
318
  }
319

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

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

332
  for (;;) {
333
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
12,093,869✔
334
    if (ret < 0) {
12,089,990✔
335
      return ret;
952,464✔
336
    }
337

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

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
11,165,799✔
341
    if (ret) return ret;
11,138,905!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
11,138,905✔
344
      continue;
2,501,613✔
345
    }
346

347
    break;
8,637,292✔
348
  }
349

350
  return 0;
8,637,292✔
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,710,210✔
382
  void           *pData = NULL;
4,710,210✔
383
  int             nData = 0;
4,710,210✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
4,710,210✔
386
  SSchemaWrapper *pSchema = NULL;
4,710,210✔
387
  SDecoder        dc = {0};
4,710,210✔
388
  if (lock) {
4,710,210✔
389
    metaRLock(pMeta);
4,701,555✔
390
  }
391
_query:
4,723,681✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
4,803,576✔
393
    goto _err;
416✔
394
  }
395

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

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

402
  SMetaEntry me = {0};
4,806,316✔
403
  tDecoderInit(&dc, pData, nData);
4,806,316✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
4,801,315✔
405
  if (code) {
4,805,513!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
4,805,513✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,411,585✔
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,407,555✔
412
      tDecoderClear(&dc);
4,407,555✔
413
      goto _exit;
4,404,936✔
414
    }
415
  } else if (me.type == TSDB_CHILD_TABLE) {
393,928✔
416
    uid = me.ctbEntry.suid;
79,831✔
417
    tDecoderClear(&dc);
79,831✔
418
    goto _query;
79,895✔
419
  } else {
420
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
314,097✔
421
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
314,199✔
422
      tDecoderClear(&dc);
314,199✔
423
      goto _exit;
314,227✔
424
    }
425
  }
426
  tDecoderClear(&dc);
4,080✔
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:
4,719,308✔
441
  if (lock) {
4,719,308✔
442
    metaULock(pMeta);
4,694,681✔
443
  }
444
  tdbFree(pData);
4,724,435✔
445
  return pSchema;
4,719,202✔
446

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

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

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

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

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

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

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

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

502
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,648,064!
503
  if (pCtbCur == NULL) {
3,660,087!
504
    return NULL;
×
505
  }
506

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

512
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,660,087✔
513
  if (ret < 0) {
3,655,334!
514
    return NULL;
×
515
  }
516
  return pCtbCur;
3,655,334✔
517
}
518

519
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,660,156✔
520
  if (pCtbCur) {
3,660,156!
521
    if (!pCtbCur->paused) {
3,660,484✔
522
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,617,360!
523
      if (pCtbCur->pCur) {
3,618,596✔
524
        tdbTbcClose(pCtbCur->pCur);
3,618,527✔
525
      }
526
    }
527
    tdbFree(pCtbCur->pKey);
3,660,265✔
528
    tdbFree(pCtbCur->pVal);
3,660,458✔
529
  }
530
  taosMemoryFree(pCtbCur);
3,660,779!
531
}
3,661,461✔
532

533
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,272✔
534
  if (!pCtbCur->paused) {
43,272!
535
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,285✔
536
    if (pCtbCur->lock) {
43,308!
537
      metaULock(pCtbCur->pMeta);
43,309✔
538
    }
539
    pCtbCur->paused = 1;
43,304✔
540
  }
541
}
43,291✔
542

543
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,651,759✔
544
  if (pCtbCur->paused) {
3,651,759!
545
    pCtbCur->paused = 0;
3,655,070✔
546

547
    if (pCtbCur->lock) {
3,655,070✔
548
      metaRLock(pCtbCur->pMeta);
3,638,526✔
549
    }
550
    int ret = 0;
3,655,314✔
551
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,655,314✔
552
    if (ret < 0) {
3,658,316!
553
      metaCloseCtbCursor(pCtbCur);
×
554
      return -1;
×
555
    }
556

557
    if (first) {
3,658,572!
558
      SCtbIdxKey ctbIdxKey;
559
      // move to the suid
560
      ctbIdxKey.suid = pCtbCur->suid;
3,658,572✔
561
      ctbIdxKey.uid = INT64_MIN;
3,658,572✔
562
      int c = 0;
3,658,572✔
563
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,658,572✔
564
      if (c > 0) {
3,655,051✔
565
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
829,406✔
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,652,103✔
578
}
579

580
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
15,266,301✔
581
  int         ret;
582
  SCtbIdxKey *pCtbIdxKey;
583

584
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
15,266,301✔
585
  if (ret < 0) {
15,266,340✔
586
    return 0;
673,904✔
587
  }
588

589
  pCtbIdxKey = pCtbCur->pKey;
14,592,436✔
590
  if (pCtbIdxKey->suid > pCtbCur->suid) {
14,592,436✔
591
    return 0;
2,988,125✔
592
  }
593

594
  return pCtbIdxKey->uid;
11,604,311✔
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) {
152,320✔
608
  SMStbCursor *pStbCur = NULL;
152,320✔
609
  int          ret = 0;
152,320✔
610
  int          c = 0;
152,320✔
611

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

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

622
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
152,323✔
623
  if (ret < 0) {
152,320!
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);
152,320✔
632
  if (c > 0) {
152,321!
633
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
634
  }
635

636
  return pStbCur;
152,321✔
637
}
638

639
void metaCloseStbCursor(SMStbCursor *pStbCur) {
152,320✔
640
  if (pStbCur) {
152,320!
641
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
152,321!
642
    if (pStbCur->pCur) {
152,320!
643
      tdbTbcClose(pStbCur->pCur);
152,321✔
644

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

649
    taosMemoryFree(pStbCur);
152,322!
650
  }
651
}
152,322✔
652

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

656
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
273,235✔
657
  if (ret < 0) {
273,244✔
658
    return 0;
152,322✔
659
  }
660
  return *(tb_uid_t *)pStbCur->pKey;
120,922✔
661
}
662

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

667
  pSW = metaGetTableSchema(pMeta, uid, sver, lock);
4,567,673✔
668
  if (!pSW) return NULL;
4,578,160✔
669

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

672
  taosMemoryFree(pSW->pSchema);
4,584,573!
673
  taosMemoryFree(pSW);
4,582,806✔
674
  return pTSchema;
4,583,732✔
675
}
676

677
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
6,562✔
678
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
6,562✔
679
  if (*ppTSchema == NULL) {
6,593✔
680
    return terrno;
1✔
681
  }
682
  return TSDB_CODE_SUCCESS;
6,592✔
683
}
684

685
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,561,981✔
686
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,561,981✔
687
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,575,965!
688
    return terrno;
9,026✔
689
  }
690
  return TSDB_CODE_SUCCESS;
4,566,939✔
691
}
692

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

697
  void     *pData = NULL;
453,765✔
698
  int       nData = 0;
453,765✔
699
  SSkmDbKey skmDbKey;
700
  if (sver <= 0) {
453,765✔
701
    SMetaInfo info;
702
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
223,039!
703
      sver = info.skmVer;
223,040✔
704
    } else {
705
      TBC *pSkmDbC = NULL;
×
706
      int  c;
707

708
      skmDbKey.uid = suid ? suid : uid;
×
709
      skmDbKey.sver = INT32_MAX;
×
710

711
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
×
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);
×
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)) {
453,730!
753
    code = TSDB_CODE_NOT_FOUND;
×
754
    goto _exit;
×
755
  }
756

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

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

772
  tDecoderInit(&dc, pData, nData);
453,834✔
773
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
452,283✔
774
  tDecoderClear(&dc);
452,283✔
775
  tdbFree(pData);
453,458✔
776
  if (TSDB_CODE_SUCCESS != code) {
453,539!
777
    taosMemoryFree(pSchemaWrapper->pSchema);
×
778
    goto _exit;
×
779
  }
780

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

787
  *ppTSchema = pTSchema;
453,741✔
788
  taosMemoryFree(pSchemaWrapper->pSchema);
453,741!
789

790
_exit:
453,825✔
791
  return code;
453,825✔
792
}
793

794
// N.B. Called by statusReq per second
795
int64_t metaGetTbNum(SMeta *pMeta) {
1,047,811✔
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,047,811✔
802
}
803

804
void metaUpdTimeSeriesNum(SMeta *pMeta) {
152,304✔
805
  int64_t nCtbTimeSeries = 0;
152,304✔
806
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
152,304!
807
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
152,306✔
808
  }
809
}
152,306✔
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,206,502✔
814
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,206,510✔
815
    metaUpdTimeSeriesNum(pMeta);
149,980✔
816
  }
817

818
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,206,517✔
819
}
820

821
// type: 1 reported timeseries
822
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,206,502!
823
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,206,517✔
824
  if (type == 1) {
1,206,517✔
825
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
1,047,811✔
826
  }
827
  return nTimeSeries;
1,206,520✔
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) {
18,604,875✔
1087
  STag *tag = (STag *)pTag;
18,604,875✔
1088
  if (type == TSDB_DATA_TYPE_JSON) {
18,604,875✔
1089
    return tag;
10,033✔
1090
  }
1091
  bool find = tTagGet(tag, val);
18,594,842✔
1092

1093
  if (!find) {
18,626,421✔
1094
    return NULL;
122,894✔
1095
  }
1096

1097
  return val;
18,503,527✔
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 {
30,856✔
1147
    void   *entryKey = NULL;
30,862✔
1148
    int32_t nEntryKey = -1;
30,862✔
1149
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
30,862✔
1150
    if (valid < 0) break;
31,146✔
1151

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

1155
    terrno = TSDB_CODE_SUCCESS;
31,140✔
1156
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
31,257✔
1157
    if (terrno != TSDB_CODE_SUCCESS) {
31,085!
1158
      ret = terrno;
×
1159
      break;
×
1160
    }
1161
    if (cmp == 0) {
30,987!
1162
      if (taosArrayPush(pUids, &p->uid) == NULL) {
61,813!
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);
30,826✔
1173
    if (valid < 0) break;
30,856!
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) {
1,738✔
1296
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,738✔
1297
  SMetaFltParam *param = arg;
1,738✔
1298

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

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

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

1320
  metaRLock(pMeta);
1,743✔
1321

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

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

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

1329
  tDecoderInit(&dc, pData, nData);
1,743✔
1330

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

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

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

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

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

1360
  if (param->val == NULL) {
422!
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)) {
422!
1365
      tagData = varDataVal(param->val);
×
1366
      nTagData = varDataLen(param->val);
×
1367

1368
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
×
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;
422✔
1384
      nTagData = tDataTypes[param->type].bytes;
422✔
1385
    }
1386
  }
1387

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

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

1395
  int     count = 0;
422✔
1396
  int32_t valid = 0;
422✔
1397
  bool    found = false;
422✔
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;
422✔
1404
  do {
10,390✔
1405
    void   *entryKey = NULL, *entryVal = NULL;
10,812✔
1406
    int32_t nEntryKey, nEntryVal;
1407

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

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

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

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

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

1471
  taosMemoryFree(buf);
1,743!
1472
  taosMemoryFree(pKey);
1,744!
1473

1474
  taosMemoryFree(pCursor);
1,743!
1475

1476
  return code;
1,743✔
1477
}
1478

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

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

1491
  return ret;
265,299✔
1492
}
1493

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

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

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

1506
      metaRLock(pMeta);
264,962✔
1507
      isLock = true;
264,962✔
1508
    }
1509

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

1518
        TAOS_RETURN(terrno);
×
1519
      }
1520
      memcpy(p->pTagVal, val, len);
265,299✔
1521
      tdbFree(val);
265,299✔
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);
264,975✔
1529
  return 0;
264,976✔
1530
}
1531

1532
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
857,661✔
1533
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
857,661✔
1534
  if (!pCur) {
859,708!
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;
859,708✔
1542
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
859,708✔
1543
  if (numOfElems > 0) {
858,880✔
1544
    pSepecifiedUidMap =
1545
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
91,881✔
1546
    for (int i = 0; i < numOfElems; i++) {
371,580✔
1547
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
279,641✔
1548
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
279,532✔
1549
      if (code) {
279,696!
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
858,938✔
1558
    while (1) {
2,255,597✔
1559
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,021,941✔
1560
      if (uid == 0) {
3,018,796✔
1561
        break;
769,301✔
1562
      }
1563

1564
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,249,495✔
1565
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,249,495!
1566
      if (!info.pTagVal) {
2,256,912!
1567
        metaCloseCtbCursor(pCur);
×
1568
        taosHashCleanup(pSepecifiedUidMap);
×
1569
        return terrno;
×
1570
      }
1571
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,256,912✔
1572
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,255,597!
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) {
382,780✔
1581
      tb_uid_t uid = metaCtbCursorNext(pCur);
475,374✔
1582
      if (uid == 0) {
474,987✔
1583
        break;
91,923✔
1584
      }
1585

1586
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
383,064✔
1587
      if (index == NULL) {
383,213✔
1588
        continue;
103,822✔
1589
      }
1590

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

1604
  taosHashCleanup(pSepecifiedUidMap);
861,224✔
1605
  metaCloseCtbCursor(pCur);
860,965✔
1606
  return TSDB_CODE_SUCCESS;
861,311✔
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) {
31,630,400✔
1612
  int32_t code = 0;
31,630,400✔
1613
  void   *pData = NULL;
31,630,400✔
1614
  int     nData = 0;
31,630,400✔
1615
  int     lock = 0;
31,630,400✔
1616

1617
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
31,630,400!
1618
    lock = 1;
7,136,851✔
1619
  }
1620

1621
  if (!lock) metaRLock(pMeta);
31,630,400✔
1622

1623
  // search cache
1624
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
31,633,345✔
1625
    if (!lock) metaULock(pMeta);
31,399,371✔
1626
    goto _exit;
31,402,480✔
1627
  }
1628

1629
  // search TDB
1630
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
239,077✔
1631
    // not found
1632
    if (!lock) metaULock(pMeta);
214,435✔
1633
    goto _exit;
214,413✔
1634
  }
1635

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

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

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

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

1659
_exit:
22,665✔
1660
  tdbFree(pData);
31,641,726✔
1661
  return code;
31,639,734✔
1662
}
1663

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

1667
  if (!numOfTables && !numOfCols) goto _exit;
271,144!
1668

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

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

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

1693
  if (numOfTables) *numOfTables = ctbNum;
21,424✔
1694
  if (numOfCols) *numOfCols = colNum;
21,424✔
1695

1696
  state.uid = uid;
21,424✔
1697
  state.ctbNum = ctbNum;
21,424✔
1698
  state.colNum = colNum;
21,424✔
1699

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

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

1708
  metaULock(pVnodeObj->pMeta);
21,424✔
1709

1710
_exit:
271,163✔
1711
  return code;
271,163✔
1712
}
1713

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

1717
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
158,246✔
1718
    stats.ctbNum += deltaCtb;
144,400✔
1719
    stats.colNum += deltaCol;
144,400✔
1720
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
144,400✔
1721
    if (code) {
144,398!
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
}
158,285✔
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