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

taosdata / TDengine / #4703

01 Sep 2025 11:36AM UTC coverage: 58.16% (-0.9%) from 59.056%
#4703

push

travis-ci

web-flow
Merge pull request #32834 from taosdata/docs/wangxu/simplify-get-started

refactor: simplify get started

133101 of 291897 branches covered (45.6%)

Branch coverage included in aggregate %.

201587 of 283568 relevant lines covered (71.09%)

5506801.45 hits per line

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

53.43
/source/dnode/vnode/src/meta/metaQuery.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "meta.h"
17
#include "osMemory.h"
18
#include "tencode.h"
19

20
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
2,708,955✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,708,955✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,708,955✔
23
  pReader->pAPI = pAPI;
2,708,449✔
24
}
2,708,449✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
3,092,825✔
27
  memset(pReader, 0, sizeof(*pReader));
3,092,825✔
28
  pReader->pMeta = pMeta;
3,092,825✔
29
  pReader->flags = flags;
3,092,825✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
3,092,825✔
31
    metaRLock(pMeta);
2,661,364✔
32
  }
33
}
3,093,047✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,134,224✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,134,224!
37
    metaULock(pReader->pMeta);
1,134,602✔
38
    pReader->flags |= META_READER_NOLOCK;
1,135,314✔
39
  }
40
}
1,134,936✔
41

42
void metaReaderClear(SMetaReader *pReader) {
3,134,427✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
3,134,427✔
44
    metaULock(pReader->pMeta);
1,525,603✔
45
  }
46
  tDecoderClear(&pReader->coder);
3,134,970✔
47
  tdbFree(pReader->pBuf);
3,135,911✔
48
  pReader->pBuf = NULL;
3,135,594✔
49
}
3,135,594✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
3,956,434!
58
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
59
  }
60

61
  // decode the entry
62
  tDecoderClear(&pReader->coder);
3,956,446✔
63
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
3,955,544✔
64

65
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
3,953,725✔
66
  if (code) {
3,953,942!
67
    tDecoderClear(&pReader->coder);
×
68
    return code;
×
69
  }
70
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
71

72
  return 0;
3,953,942✔
73
}
74

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

79
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
488,066✔
80
    metaULock(pVnodeObj->pMeta);
21✔
81
    return false;
21✔
82
  }
83

84
  metaULock(pVnodeObj->pMeta);
488,041✔
85
  return true;
488,045✔
86
}
87

88
int metaReaderGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) {
625,176✔
89
  SMeta  *pMeta = pReader->pMeta;
625,176✔
90
  int64_t version1;
91

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

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

101
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
2,699,813✔
102
  SMeta *pMeta = pReader->pMeta;
2,699,813✔
103

104
  SMetaInfo info;
105
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
2,699,813✔
106
  if (TSDB_CODE_SUCCESS != code) {
2,699,876✔
107
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
7!
108
  }
109

110
  return metaGetTableEntryByVersion(pReader, info.version, uid);
2,699,869✔
111
}
112

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

117
  // query name.idx
118
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
123,304✔
119
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
47,441✔
120
  }
121

122
  uid = *(tb_uid_t *)pReader->pBuf;
75,865✔
123
  return metaReaderGetTableEntryByUid(pReader, uid);
75,865✔
124
}
125

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

131
  metaRLock(pMeta);
163,110✔
132

133
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
163,316✔
134
    uid = *(tb_uid_t *)pData;
33,078✔
135
    tdbFree(pData);
33,078✔
136
  }
137

138
  metaULock(pMeta);
163,616✔
139

140
  return uid;
163,638✔
141
}
142

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

153
  STR_TO_VARSTR(tbName, mr.me.name);
54,597✔
154
  metaReaderClear(&mr);
54,597✔
155

156
  return 0;
54,840✔
157
}
158

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

171
  return 0;
×
172
}
173

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

179
  SMetaReader *pReader = &mr;
×
180

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

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

189
  metaReaderClear(&mr);
×
190

191
  return 0;
×
192
}
193

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

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

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

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

216
  // TODO
217

218
  return 0;
×
219
}
220

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

237
  code = 0;
×
238

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

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

249
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
19,898!
250
  if (pTbCur == NULL) {
19,897!
251
    return NULL;
×
252
  }
253

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

267
void metaCloseTbCursor(SMTbCursor *pTbCur) {
39,715✔
268
  if (pTbCur) {
39,715✔
269
    tdbFree(pTbCur->pKey);
19,891✔
270
    tdbFree(pTbCur->pVal);
19,896✔
271
    if (!pTbCur->paused) {
19,898✔
272
      metaReaderClear(&pTbCur->mr);
751✔
273
      if (pTbCur->pDbc) {
751!
274
        tdbTbcClose((TBC *)pTbCur->pDbc);
751✔
275
      }
276
    }
277
    taosMemoryFree(pTbCur);
19,898✔
278
  }
279
}
39,720✔
280

281
void metaPauseTbCursor(SMTbCursor *pTbCur) {
20,069✔
282
  if (!pTbCur->paused) {
20,069!
283
    metaReaderClear(&pTbCur->mr);
20,069✔
284
    tdbTbcClose((TBC *)pTbCur->pDbc);
20,070✔
285
    pTbCur->paused = 1;
20,069✔
286
  }
287
}
20,069✔
288
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
20,820✔
289
  int32_t code = 0;
20,820✔
290
  int32_t lino;
291
  int8_t  locked = 0;
20,820✔
292
  if (pTbCur->paused) {
20,820!
293
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
20,820✔
294
    locked = 1;
20,819✔
295
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
20,819✔
296
    if (code != 0) {
20,817!
297
      TSDB_CHECK_CODE(code, lino, _exit);
×
298
    }
299

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

318
    pTbCur->paused = 0;
20,815✔
319
  }
320

321
_exit:
×
322
  if (code != 0 && locked) {
20,815!
323
    metaReaderReleaseLock(&pTbCur->mr);
×
324
  }
325
  return code;
20,812✔
326
}
327

328
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
655,425✔
329
  int    ret;
330
  void  *pBuf;
331
  STbCfg tbCfg;
332

333
  for (;;) {
334
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
655,425✔
335
    if (ret < 0) {
653,110✔
336
      return ret;
19,836✔
337
    }
338

339
    tDecoderClear(&pTbCur->mr.coder);
633,274✔
340

341
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
634,170✔
342
    if (ret) return ret;
630,943!
343

344
    if (pTbCur->mr.me.type == jumpTableType) {
630,943✔
345
      continue;
10,291✔
346
    }
347

348
    break;
620,652✔
349
  }
350

351
  return 0;
620,652✔
352
}
353

354
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
355
  int    ret;
356
  void  *pBuf;
357
  STbCfg tbCfg;
358

359
  for (;;) {
360
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
361
    if (ret < 0) {
×
362
      return -1;
×
363
    }
364

365
    tDecoderClear(&pTbCur->mr.coder);
×
366

367
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
368
    if (ret < 0) {
×
369
      return ret;
×
370
    }
371

372
    if (pTbCur->mr.me.type == jumpTableType) {
×
373
      continue;
×
374
    }
375

376
    break;
×
377
  }
378

379
  return 0;
×
380
}
381

382
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema) {
1,315,604✔
383
  void           *pData = NULL;
1,315,604✔
384
  int             nData = 0;
1,315,604✔
385
  int64_t         version;
386
  SSchemaWrapper  schema = {0};
1,315,604✔
387
  SSchemaWrapper *pSchema = NULL;
1,315,604✔
388
  SDecoder        dc = {0};
1,315,604✔
389
  if (lock) {
1,315,604✔
390
    metaRLock(pMeta);
1,292,273✔
391
  }
392
_query:
1,316,064✔
393
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,413,854✔
394
    goto _err;
364✔
395
  }
396

397
  version = ((SUidIdxVal *)pData)[0].version;
1,413,204✔
398

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

403
  SMetaEntry me = {0};
1,413,377✔
404
  tDecoderInit(&dc, pData, nData);
1,413,377✔
405
  int32_t code = metaDecodeEntry(&dc, &me);
1,413,147✔
406
  if (code) {
1,413,130!
407
    tDecoderClear(&dc);
×
408
    goto _err;
×
409
  }
410
  if (me.type == TSDB_SUPER_TABLE) {
1,413,130✔
411
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
1,071,005✔
412
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
1,070,740✔
413
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
1,070,740✔
414
      tDecoderClear(&dc);
1,070,736✔
415
      goto _exit;
1,070,932✔
416
    }
417
  } else if (me.type == TSDB_CHILD_TABLE) {
342,125✔
418
    uid = me.ctbEntry.suid;
97,716✔
419
    tDecoderClear(&dc);
97,716✔
420
    goto _query;
97,790✔
421
  } else {
422
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
244,409✔
423
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
244,488✔
424
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
244,488✔
425
      tDecoderClear(&dc);
244,460✔
426
      goto _exit;
244,504✔
427
    }
428
  }
429
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
363✔
430
  tDecoderClear(&dc);
363✔
431

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

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

444
_exit:
1,315,570✔
445
  if (lock) {
1,315,570✔
446
    metaULock(pMeta);
1,291,370✔
447
  }
448
  tdbFree(pData);
1,316,374✔
449
  return pSchema;
1,315,727✔
450

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

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

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

473
  version = ((SUidIdxVal *)pData)[0].version;
138✔
474

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

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

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

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

506
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
726,090!
507
  if (pCtbCur == NULL) {
726,875!
508
    return NULL;
×
509
  }
510

511
  pCtbCur->pMeta = pMeta;
726,875✔
512
  pCtbCur->suid = uid;
726,875✔
513
  pCtbCur->lock = lock;
726,875✔
514
  pCtbCur->paused = 1;
726,875✔
515

516
  ret = metaResumeCtbCursor(pCtbCur, 1);
726,875✔
517
  if (ret < 0) {
726,380!
518
    return NULL;
×
519
  }
520
  return pCtbCur;
726,380✔
521
}
522

523
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
726,342✔
524
  if (pCtbCur) {
726,342!
525
    if (!pCtbCur->paused) {
726,434✔
526
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
695,891!
527
      if (pCtbCur->pCur) {
696,258!
528
        tdbTbcClose(pCtbCur->pCur);
696,266✔
529
      }
530
    }
531
    tdbFree(pCtbCur->pKey);
726,648✔
532
    tdbFree(pCtbCur->pVal);
726,672✔
533
  }
534
  taosMemoryFree(pCtbCur);
726,689!
535
}
726,848✔
536

537
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
30,612✔
538
  if (!pCtbCur->paused) {
30,612!
539
    tdbTbcClose((TBC *)pCtbCur->pCur);
30,613✔
540
    if (pCtbCur->lock) {
30,623!
541
      metaULock(pCtbCur->pMeta);
30,624✔
542
    }
543
    pCtbCur->paused = 1;
30,627✔
544
  }
545
}
30,626✔
546

547
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
726,324✔
548
  if (pCtbCur->paused) {
726,324!
549
    pCtbCur->paused = 0;
726,528✔
550

551
    if (pCtbCur->lock) {
726,528✔
552
      metaRLock(pCtbCur->pMeta);
703,290✔
553
    }
554
    int ret = 0;
726,544✔
555
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
726,544✔
556
    if (ret < 0) {
726,733✔
557
      metaCloseCtbCursor(pCtbCur);
3✔
558
      return -1;
×
559
    }
560

561
    if (first) {
726,730!
562
      SCtbIdxKey ctbIdxKey;
563
      // move to the suid
564
      ctbIdxKey.suid = pCtbCur->suid;
726,730✔
565
      ctbIdxKey.uid = INT64_MIN;
726,730✔
566
      int c = 0;
726,730✔
567
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
726,730✔
568
      if (c > 0) {
726,541✔
569
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
161,198✔
570
      }
571
    } else {
572
      int c = 0;
×
573
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
574
      if (c < 0) {
×
575
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
576
      } else {
577
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
578
      }
579
    }
580
  }
581
  return 0;
726,212✔
582
}
583

584
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
3,962,312✔
585
  int         ret;
586
  SCtbIdxKey *pCtbIdxKey;
587

588
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
3,962,312✔
589
  if (ret < 0) {
3,964,020✔
590
    return 0;
621,822✔
591
  }
592

593
  pCtbIdxKey = pCtbCur->pKey;
3,342,198✔
594
  if (pCtbIdxKey->suid > pCtbCur->suid) {
3,342,198✔
595
    return 0;
105,458✔
596
  }
597

598
  return pCtbIdxKey->uid;
3,236,740✔
599
}
600

601
struct SMStbCursor {
602
  SMeta   *pMeta;
603
  TBC     *pCur;
604
  tb_uid_t suid;
605
  void    *pKey;
606
  void    *pVal;
607
  int      kLen;
608
  int      vLen;
609
};
610

611
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
134,141✔
612
  SMStbCursor *pStbCur = NULL;
134,141✔
613
  int          ret = 0;
134,141✔
614
  int          c = 0;
134,141✔
615

616
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
134,141!
617
  if (pStbCur == NULL) {
134,141!
618
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
619
    return NULL;
×
620
  }
621

622
  pStbCur->pMeta = pMeta;
134,141✔
623
  pStbCur->suid = suid;
134,141✔
624
  metaRLock(pMeta);
134,141✔
625

626
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
134,143✔
627
  if (ret < 0) {
134,140!
628
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
629
    metaULock(pMeta);
×
630
    taosMemoryFree(pStbCur);
×
631
    return NULL;
×
632
  }
633

634
  // move to the suid
635
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
134,140✔
636
  if (c > 0) {
134,139!
637
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
638
  }
639

640
  return pStbCur;
134,139✔
641
}
642

643
void metaCloseStbCursor(SMStbCursor *pStbCur) {
134,141✔
644
  if (pStbCur) {
134,141!
645
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
134,141!
646
    if (pStbCur->pCur) {
134,143!
647
      tdbTbcClose(pStbCur->pCur);
134,143✔
648

649
      tdbFree(pStbCur->pKey);
134,143✔
650
      tdbFree(pStbCur->pVal);
134,143✔
651
    }
652

653
    taosMemoryFree(pStbCur);
134,143!
654
  }
655
}
134,143✔
656

657
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
553,486✔
658
  int ret;
659

660
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
553,486✔
661
  if (ret < 0) {
553,479✔
662
    return 0;
134,142✔
663
  }
664
  return *(tb_uid_t *)pStbCur->pKey;
419,337✔
665
}
666

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

671
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
1,160,958✔
672
  if (!pSW) return NULL;
1,161,181✔
673

674
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
1,161,083✔
675

676
  taosMemoryFree(pSW->pSchema);
1,161,281!
677
  taosMemoryFree(pSW);
1,161,354✔
678
  return pTSchema;
1,161,327✔
679
}
680

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

689
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,149,023✔
690
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,149,023✔
691
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
1,149,347!
692
    return terrno;
590✔
693
  }
694
  return TSDB_CODE_SUCCESS;
1,148,757✔
695
}
696

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

701
  void     *pData = NULL;
166,184✔
702
  int       nData = 0;
166,184✔
703
  SSkmDbKey skmDbKey;
704
  if (sver <= 0) {
166,184✔
705
    SMetaInfo info;
706
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
48,829✔
707
      sver = info.skmVer;
48,755✔
708
    } else {
709
      TBC *pSkmDbC = NULL;
96✔
710
      int  c;
711

712
      skmDbKey.uid = suid ? suid : uid;
96!
713
      skmDbKey.sver = INT32_MAX;
96✔
714

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

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

726
      if (c == 0) {
98!
727
        metaULock(pMeta);
×
728
        tdbTbcClose(pSkmDbC);
×
729
        code = TSDB_CODE_FAILED;
×
730
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
731
        goto _exit;
×
732
      }
733

734
      if (c < 0) {
98✔
735
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
7✔
736
      }
737

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

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

749
      sver = ((SSkmDbKey *)pKey)->sver;
98✔
750

751
      metaULock(pMeta);
98✔
752
      tdbTbcClose(pSkmDbC);
98✔
753
    }
754
  }
755

756
  if (!(sver > 0)) {
166,181!
757
    code = TSDB_CODE_NOT_FOUND;
×
758
    goto _exit;
×
759
  }
760

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

771
  // decode
772
  SDecoder        dc = {0};
166,218✔
773
  SSchemaWrapper  schema;
774
  SSchemaWrapper *pSchemaWrapper = &schema;
166,218✔
775

776
  tDecoderInit(&dc, pData, nData);
166,218✔
777
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
165,771✔
778
  tDecoderClear(&dc);
165,771✔
779
  tdbFree(pData);
166,112✔
780
  if (TSDB_CODE_SUCCESS != code) {
166,189!
781
    taosMemoryFree(pSchemaWrapper->pSchema);
×
782
    goto _exit;
×
783
  }
784

785
  // convert
786
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
166,189✔
787
  if (pTSchema == NULL) {
166,205!
788
    code = TSDB_CODE_OUT_OF_MEMORY;
×
789
  }
790

791
  *ppTSchema = pTSchema;
166,205✔
792
  taosMemoryFree(pSchemaWrapper->pSchema);
166,205!
793

794
_exit:
166,228✔
795
  return code;
166,228✔
796
}
797

798
// N.B. Called by statusReq per second
799
int64_t metaGetTbNum(SMeta *pMeta) {
254,657✔
800
  // num of child tables (excluding normal tables , stables and others)
801

802
  /* int64_t num = 0; */
803
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
804

805
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
254,657✔
806
}
807

808
void metaUpdTimeSeriesNum(SMeta *pMeta) {
134,148✔
809
  int64_t nCtbTimeSeries = 0;
134,148✔
810
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
134,148!
811
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
134,146✔
812
  }
813
}
134,149✔
814

815
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
816
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
817
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
431,180✔
818
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
431,219✔
819
    metaUpdTimeSeriesNum(pMeta);
134,109✔
820
  }
821

822
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
431,238✔
823
}
824

825
// type: 1 reported timeseries
826
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
431,180!
827
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
431,238✔
828
  if (type == 1) {
431,238✔
829
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
254,657✔
830
  }
831
  return nTimeSeries;
431,245✔
832
}
833

834
typedef struct {
835
  SMeta   *pMeta;
836
  TBC     *pCur;
837
  tb_uid_t uid;
838
  void    *pKey;
839
  void    *pVal;
840
  int      kLen;
841
  int      vLen;
842
} SMSmaCursor;
843

844
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
845
  SMSmaCursor *pSmaCur = NULL;
×
846
  SSmaIdxKey   smaIdxKey;
847
  int          ret;
848
  int          c;
849

850
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
851
  if (pSmaCur == NULL) {
×
852
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
853
    return NULL;
×
854
  }
855

856
  pSmaCur->pMeta = pMeta;
×
857
  pSmaCur->uid = uid;
×
858
  metaRLock(pMeta);
×
859

860
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
861
  if (ret < 0) {
×
862
    metaULock(pMeta);
×
863
    taosMemoryFree(pSmaCur);
×
864
    return NULL;
×
865
  }
866

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

875
  return pSmaCur;
×
876
}
877

878
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
879
  if (pSmaCur) {
×
880
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
881
    if (pSmaCur->pCur) {
×
882
      tdbTbcClose(pSmaCur->pCur);
×
883
      pSmaCur->pCur = NULL;
×
884

885
      tdbFree(pSmaCur->pKey);
×
886
      tdbFree(pSmaCur->pVal);
×
887
    }
888

889
    taosMemoryFree(pSmaCur);
×
890
  }
891
}
×
892

893
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
894
  int         ret;
895
  SSmaIdxKey *pSmaIdxKey;
896

897
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
898
  if (ret < 0) {
×
899
    return 0;
×
900
  }
901

902
  pSmaIdxKey = pSmaCur->pKey;
×
903
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
904
    return 0;
×
905
  }
906

907
  return pSmaIdxKey->uid;
×
908
}
909

910
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
911
  STSmaWrapper *pSW = NULL;
×
912
  SArray       *pSmaIds = NULL;
×
913

914
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
915
    return NULL;
×
916
  }
917

918
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
919
  if (!pSW) {
×
920
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
921
    goto _err;
×
922
  }
923

924
  pSW->number = taosArrayGetSize(pSmaIds);
×
925
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
926

927
  if (!pSW->tSma) {
×
928
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
929
    goto _err;
×
930
  }
931

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

969
    ++smaIdx;
×
970
  }
971

972
  if (smaIdx <= 0) goto _err;
×
973
  pSW->number = smaIdx;
×
974

975
  metaReaderClear(&mr);
×
976
  taosArrayDestroy(pSmaIds);
×
977
  return pSW;
×
978
_err:
×
979
  metaReaderClear(&mr);
×
980
  taosArrayDestroy(pSmaIds);
×
981
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
982
  return NULL;
×
983
}
984

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

1001
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1002

1003
  metaReaderClear(&mr);
×
1004
  return pTSma;
×
1005
}
1006

1007
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1008
  SArray     *pUids = NULL;
×
1009
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1010

1011
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1012
  if (!pCur) {
×
1013
    return NULL;
×
1014
  }
1015

1016
  while (1) {
×
1017
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1018
    if (id == 0) {
×
1019
      break;
×
1020
    }
1021

1022
    if (!pUids) {
×
1023
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1024
      if (!pUids) {
×
1025
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1026
        metaCloseSmaCursor(pCur);
×
1027
        return NULL;
×
1028
      }
1029
    }
1030

1031
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1032

1033
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1034
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1035
      metaCloseSmaCursor(pCur);
×
1036
      taosArrayDestroy(pUids);
×
1037
      return NULL;
×
1038
    }
1039
  }
1040

1041
  metaCloseSmaCursor(pCur);
×
1042
  return pUids;
×
1043
}
1044

1045
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1046
  SArray     *pUids = NULL;
×
1047
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1048
  tb_uid_t    lastUid = 0;
×
1049

1050
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1051
  if (!pCur) {
×
1052
    return NULL;
×
1053
  }
1054

1055
  while (1) {
×
1056
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1057
    if (uid == 0) {
×
1058
      break;
×
1059
    }
1060

1061
    if (lastUid == uid) {
×
1062
      continue;
×
1063
    }
1064

1065
    lastUid = uid;
×
1066

1067
    if (!pUids) {
×
1068
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1069
      if (!pUids) {
×
1070
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1071
        metaCloseSmaCursor(pCur);
×
1072
        return NULL;
×
1073
      }
1074
    }
1075

1076
    if (!taosArrayPush(pUids, &uid)) {
×
1077
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1078
      metaCloseSmaCursor(pCur);
×
1079
      taosArrayDestroy(pUids);
×
1080
      return NULL;
×
1081
    }
1082
  }
1083

1084
  metaCloseSmaCursor(pCur);
×
1085
  return pUids;
×
1086
}
1087

1088
#endif
1089

1090
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
4,250,649✔
1091
  STag *tag = (STag *)pTag;
4,250,649✔
1092
  if (type == TSDB_DATA_TYPE_JSON) {
4,250,649✔
1093
    return tag;
10,414✔
1094
  }
1095
  bool find = tTagGet(tag, val);
4,240,235✔
1096

1097
  if (!find) {
4,242,835✔
1098
    return NULL;
44,586✔
1099
  }
1100

1101
  return val;
4,198,249✔
1102
}
1103

1104
typedef struct {
1105
  SMeta   *pMeta;
1106
  TBC     *pCur;
1107
  tb_uid_t suid;
1108
  int16_t  cid;
1109
  int16_t  type;
1110
  void    *pKey;
1111
  void    *pVal;
1112
  int32_t  kLen;
1113
  int32_t  vLen;
1114
} SIdxCursor;
1115

1116
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1117
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1118
  SMetaFltParam *param = arg;
6✔
1119
  int32_t        ret = 0;
6✔
1120

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

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

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

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

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

1149
  static const int8_t TRY_ERROR_LIMIT = 1;
1150
  do {
32,824✔
1151
    void   *entryKey = NULL;
32,830✔
1152
    int32_t nEntryKey = -1;
32,830✔
1153
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
32,830✔
1154
    if (valid < 0) break;
33,123✔
1155

1156
    SBtimeIdxKey *p = entryKey;
33,117✔
1157
    if (count > TRY_ERROR_LIMIT) break;
33,117!
1158

1159
    terrno = TSDB_CODE_SUCCESS;
33,117✔
1160
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
33,117✔
1161
    if (terrno != TSDB_CODE_SUCCESS) {
32,929!
1162
      ret = terrno;
×
1163
      break;
×
1164
    }
1165
    if (cmp == 0) {
32,962!
1166
      if (taosArrayPush(pUids, &p->uid) == NULL) {
65,882!
1167
        ret = terrno;
×
1168
        break;
×
1169
      }
1170
    } else {
1171
      if (param->equal == true) {
×
1172
        if (count > TRY_ERROR_LIMIT) break;
×
1173
        count++;
×
1174
      }
1175
    }
1176
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
32,920✔
1177
    if (valid < 0) break;
32,824!
1178
  } while (1);
1179

1180
END:
6✔
1181
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1182
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1183
  taosMemoryFree(pCursor);
6!
1184
  return ret;
6✔
1185
}
1186

1187
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1188
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1189
  SMetaFltParam *param = arg;
×
1190
  int32_t        ret = 0;
×
1191
  char          *buf = NULL;
×
1192

1193
  STagIdxKey *pKey = NULL;
×
1194
  int32_t     nKey = 0;
×
1195

1196
  SIdxCursor *pCursor = NULL;
×
1197
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1198
  if (pCursor == NULL) {
×
1199
    return terrno;
×
1200
  }
1201
  pCursor->pMeta = pMeta;
×
1202
  pCursor->suid = param->suid;
×
1203
  pCursor->cid = param->cid;
×
1204
  pCursor->type = param->type;
×
1205

1206
  char *pName = param->val;
×
1207

1208
  metaRLock(pMeta);
×
1209
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1210
  if (ret != 0) {
×
1211
    goto END;
×
1212
  }
1213

1214
  int cmp = 0;
×
1215
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1216
    goto END;
×
1217
  }
1218
  int32_t valid = 0;
×
1219
  int32_t count = 0;
×
1220

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

1228
    if (count > TRY_ERROR_LIMIT) break;
×
1229

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

1255
END:
×
1256
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1257
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1258
  taosMemoryFree(buf);
×
1259
  taosMemoryFree(pKey);
×
1260

1261
  taosMemoryFree(pCursor);
×
1262

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

1271
  STtlIdxKey *pKey = NULL;
×
1272
  int32_t     nKey = 0;
×
1273

1274
  SIdxCursor *pCursor = NULL;
×
1275
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1276
  if (pCursor == NULL) {
×
1277
    return terrno;
×
1278
  }
1279
  pCursor->pMeta = pMeta;
×
1280
  pCursor->suid = param->suid;
×
1281
  pCursor->cid = param->cid;
×
1282
  pCursor->type = param->type;
×
1283

1284
  metaRLock(pMeta);
×
1285
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1286

1287
END:
×
1288
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1289
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1290
  taosMemoryFree(buf);
×
1291
  taosMemoryFree(pKey);
×
1292

1293
  taosMemoryFree(pCursor);
×
1294

1295
  return ret;
×
1296
  // impl later
1297
  return 0;
1298
}
1299
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6,641✔
1300
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6,641✔
1301
  SMetaFltParam *param = arg;
6,641✔
1302

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

1309
  SDecoder    dc = {0};
6,641✔
1310
  STbDbKey    tbDbKey = {0};
6,641✔
1311
  STagIdxKey *pKey = NULL;
6,641✔
1312
  int32_t     nKey = 0;
6,641✔
1313

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

1324
  metaRLock(pMeta);
6,662✔
1325

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

1328
  tbDbKey.uid = param->suid;
6,666✔
1329
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
6,666✔
1330

1331
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
6,666!
1332

1333
  tDecoderInit(&dc, pData, nData);
6,665✔
1334

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

1341
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
6,667!
1342
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
1!
1343
  }
1344

1345
  code = TSDB_CODE_INVALID_PARA;
6,667✔
1346

1347
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
9,397✔
1348
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
9,395✔
1349
    if (IS_IDX_ON(schema)) {
9,395✔
1350
      if (schema->colId == param->cid && param->type == schema->type) {
9,366!
1351
        code = 0;
6,665✔
1352
        break;
6,665✔
1353
      }
1354
    }
1355
  }
1356

1357
  TAOS_CHECK_GOTO(code, NULL, END);
6,667!
1358

1359
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
6,667✔
1360
  if (code != 0) {
6,668✔
1361
    TAOS_CHECK_GOTO(terrno, NULL, END);
7!
1362
  }
1363

1364
  int32_t maxSize = 0;
6,661✔
1365
  int32_t nTagData = 0;
6,661✔
1366
  void   *tagData = NULL;
6,661✔
1367

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

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

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

1387
        tagData = buf;
103✔
1388
        nTagData = maxSize;
103✔
1389
      }
1390
    } else {
1391
      tagData = param->val;
6,290✔
1392
      nTagData = tDataTypes[param->type].bytes;
6,290✔
1393
    }
1394
  }
1395

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

1400
  int cmp = 0;
6,663✔
1401
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
6,663!
1402

1403
  int     count = 0;
6,666✔
1404
  int32_t valid = 0;
6,666✔
1405
  bool    found = false;
6,666✔
1406

1407
  static const int8_t TRY_ERROR_LIMIT = 1;
1408

1409
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1410
  /// target:                        [suid, cid2, type2]
1411
  int diffCidCount = 0;
6,666✔
1412
  do {
76,852✔
1413
    void   *entryKey = NULL, *entryVal = NULL;
83,518✔
1414
    int32_t nEntryKey, nEntryVal;
1415

1416
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
83,518✔
1417
    if (valid < 0) {
83,202✔
1418
      break;
6,670✔
1419
    }
1420
    if (count > TRY_ERROR_LIMIT) {
79,829✔
1421
      break;
1,517✔
1422
    }
1423

1424
    STagIdxKey *p = entryKey;
78,312✔
1425
    if (p == NULL) break;
78,312!
1426

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

1441
    terrno = TSDB_CODE_SUCCESS;
76,251✔
1442
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
76,254✔
1443
    if (terrno != TSDB_CODE_SUCCESS) {
76,239!
1444
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1445
      break;
×
1446
    }
1447
    if (cmp == 0) {
76,269✔
1448
      // match
1449
      tb_uid_t tuid = 0;
69,385✔
1450
      if (IS_VAR_DATA_TYPE(pKey->type)) {
69,385!
1451
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
422✔
1452
      } else {
1453
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
68,963✔
1454
      }
1455
      if (taosArrayPush(pUids, &tuid) == NULL) {
69,664!
1456
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1457
      }
1458
      found = true;
69,664✔
1459
    } else {
1460
      if (param->equal == true) {
6,884✔
1461
        if (count > TRY_ERROR_LIMIT) break;
3,995!
1462
        count++;
3,995✔
1463
      }
1464
    }
1465
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
76,548✔
1466
    if (valid < 0) {
76,535!
1467
      code = valid;
×
1468
      break;
×
1469
    }
1470
  } while (1);
1471

1472
END:
6,670✔
1473
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6,670!
1474
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6,673!
1475
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
6,674!
1476
  tDecoderClear(&dc);
6,674✔
1477
  tdbFree(pData);
6,668✔
1478

1479
  taosMemoryFree(buf);
6,674!
1480
  taosMemoryFree(pKey);
6,673✔
1481

1482
  taosMemoryFree(pCursor);
6,672!
1483

1484
  return code;
6,675✔
1485
}
1486

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

1493
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
62,464✔
1494
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
62,464✔
1495
  if (lock) {
62,705!
1496
    metaULock(pMeta);
×
1497
  }
1498

1499
  return ret;
62,700✔
1500
}
1501

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

1506
  int32_t isLock = false;
5,980✔
1507
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
5,980!
1508
  for (int i = 0; i < sz; i++) {
68,477✔
1509
    STUidTagInfo *p = taosArrayGet(uidList, i);
62,494✔
1510

1511
    if (i % LIMIT == 0) {
62,478✔
1512
      if (isLock) metaULock(pMeta);
4,817!
1513

1514
      metaRLock(pMeta);
4,817✔
1515
      isLock = true;
4,817✔
1516
    }
1517

1518
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1519
    void   *val = NULL;
62,478✔
1520
    int32_t len = 0;
62,478✔
1521
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
62,478!
1522
      p->pTagVal = taosMemoryMalloc(len);
62,699!
1523
      if (!p->pTagVal) {
62,672!
1524
        if (isLock) metaULock(pMeta);
×
1525

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

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

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

1564
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
116,057✔
1565
    while (1) {
405,760✔
1566
      tb_uid_t uid = metaCtbCursorNext(pCur);
482,342✔
1567
      if (uid == 0) {
476,226✔
1568
        break;
76,627✔
1569
      }
1570

1571
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
399,599✔
1572
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
399,599!
1573
      if (!info.pTagVal) {
407,893!
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
407,893✔
1579
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
405,760!
1580
        taosMemoryFreeClear(info.pTagVal);
×
1581
        metaCloseCtbCursor(pCur);
×
1582
        taosHashCleanup(pSepecifiedUidMap);
×
1583
        return terrno;
×
1584
      }
1585
    }
1586
  } else {  // only the specified tables need to be added
1587
    while (1) {
238,991✔
1588
      tb_uid_t uid = metaCtbCursorNext(pCur);
278,466✔
1589
      if (uid == 0) {
278,276✔
1590
        break;
39,468✔
1591
      }
1592

1593
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
238,808✔
1594
      if (index == NULL) {
238,829✔
1595
        continue;
47,856✔
1596
      }
1597

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

1611
  taosHashCleanup(pSepecifiedUidMap);
116,095✔
1612
  metaCloseCtbCursor(pCur);
116,084✔
1613
  return TSDB_CODE_SUCCESS;
116,106✔
1614
}
1615

1616
int32_t metaFlagCache(SVnode *pVnode) {
×
1617
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1618
  if (!pCur) {
×
1619
    return terrno;
×
1620
  }
1621

1622
  SArray *suids = NULL;
×
1623
  while (1) {
×
1624
    tb_uid_t id = metaStbCursorNext(pCur);
×
1625
    if (id == 0) {
×
1626
      break;
×
1627
    }
1628

1629
    if (!suids) {
×
1630
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1631
      if (!suids) {
×
1632
        return terrno;
×
1633
      }
1634
    }
1635

1636
    if (taosArrayPush(suids, &id) == NULL) {
×
1637
      taosArrayDestroy(suids);
×
1638
      return terrno;
×
1639
    }
1640
  }
1641

1642
  metaCloseStbCursor(pCur);
×
1643

1644
  for (int idx = 0; suids && idx < TARRAY_SIZE(suids); ++idx) {
×
1645
    tb_uid_t id = ((tb_uid_t *)TARRAY_DATA(suids))[idx];
×
1646
    STsdb   *pTsdb = pVnode->pTsdb;
×
1647
    SMeta   *pMeta = pVnode->pMeta;
×
1648
    SArray  *uids = NULL;
×
1649

1650
    int32_t code = metaGetChildUidsOfSuperTable(pMeta, id, &uids);
×
1651
    if (code) {
×
1652
      metaError("vgId:%d, failed to get subtables, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1653

1654
      taosArrayDestroy(uids);
×
1655
      taosArrayDestroy(suids);
×
1656

1657
      return code;
×
1658
    }
1659

1660
    if (uids && TARRAY_SIZE(uids) > 0) {
×
1661
      STSchema *pTSchema = NULL;
×
1662

1663
      code = metaGetTbTSchemaEx(pMeta, id, id, -1, &pTSchema);
×
1664
      if (code) {
×
1665
        metaError("vgId:%d, failed to get schema, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1666

1667
        taosArrayDestroy(uids);
×
1668
        taosArrayDestroy(suids);
×
1669

1670
        return code;
×
1671
      }
1672

1673
      int32_t nCol = pTSchema->numOfCols;
×
1674
      for (int32_t i = 0; i < nCol; ++i) {
×
1675
        int16_t cid = pTSchema->columns[i].colId;
×
1676
        int8_t  col_type = pTSchema->columns[i].type;
×
1677

1678
        code = tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type);
×
1679
        if (code) {
×
1680
          metaError("vgId:%d, failed to flag cache, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1681

1682
          tDestroyTSchema(pTSchema);
×
1683
          taosArrayDestroy(uids);
×
1684
          taosArrayDestroy(suids);
×
1685

1686
          return code;
×
1687
        }
1688
      }
1689

1690
      tDestroyTSchema(pTSchema);
×
1691
    }
1692

1693
    taosArrayDestroy(uids);
×
1694
  }
1695

1696
  taosArrayDestroy(suids);
×
1697

1698
  return TSDB_CODE_SUCCESS;
×
1699
}
1700

1701
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1702

1703
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
7,195,150✔
1704
  int32_t code = 0;
7,195,150✔
1705
  void   *pData = NULL;
7,195,150✔
1706
  int     nData = 0;
7,195,150✔
1707
  int     lock = 0;
7,195,150✔
1708

1709
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
7,195,150!
1710
    lock = 1;
2,700,146✔
1711
  }
1712

1713
  if (!lock) metaRLock(pMeta);
7,195,150✔
1714

1715
  // search cache
1716
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
7,200,268✔
1717
    if (!lock) metaULock(pMeta);
6,941,949✔
1718
    goto _exit;
6,942,370✔
1719
  }
1720

1721
  // search TDB
1722
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
258,490✔
1723
    // not found
1724
    if (!lock) metaULock(pMeta);
243,055!
1725
    goto _exit;
243,081✔
1726
  }
1727

1728
  if (!lock) metaULock(pMeta);
15,131✔
1729

1730
  pInfo->uid = uid;
15,131✔
1731
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
15,131✔
1732
  pInfo->version = ((SUidIdxVal *)pData)->version;
15,131✔
1733
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
15,131✔
1734

1735
  if (lock) {
15,131✔
1736
    metaULock(pReader->pMeta);
3,353✔
1737
    // metaReaderReleaseLock(pReader);
1738
  }
1739
  // upsert the cache
1740
  metaWLock(pMeta);
15,130✔
1741
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
15,442✔
1742
  if (ret != 0) {
15,442!
1743
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1744
  }
1745
  metaULock(pMeta);
15,442✔
1746

1747
  if (lock) {
15,443✔
1748
    metaRLock(pReader->pMeta);
3,353✔
1749
  }
1750

1751
_exit:
12,090✔
1752
  tdbFree(pData);
7,200,892✔
1753
  return code;
7,200,704✔
1754
}
1755

1756
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
596,669✔
1757
  int32_t code = 0;
596,669✔
1758

1759
  if (!numOfTables && !numOfCols) goto _exit;
596,669!
1760

1761
  SVnode *pVnodeObj = pVnode;
596,669✔
1762
  metaRLock(pVnodeObj->pMeta);
596,669✔
1763

1764
  // fast path: search cache
1765
  SMetaStbStats state = {0};
596,726✔
1766
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
596,726✔
1767
    metaULock(pVnodeObj->pMeta);
573,152✔
1768
    if (numOfTables) *numOfTables = state.ctbNum;
573,164✔
1769
    if (numOfCols) *numOfCols = state.colNum;
573,164✔
1770
    if (flags) *flags = state.flags;
573,164✔
1771
    goto _exit;
573,164✔
1772
  }
1773

1774
  // slow path: search TDB
1775
  int64_t ctbNum = 0;
23,562✔
1776
  int32_t colNum = 0;
23,562✔
1777
  int64_t keep = 0;
23,562✔
1778
  int8_t  flag = 0;
23,562✔
1779

1780
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
23,562✔
1781
  if (TSDB_CODE_SUCCESS == code) {
23,563!
1782
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
23,564✔
1783
  }
1784
  if (TSDB_CODE_SUCCESS == code) {
23,559!
1785
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
23,560✔
1786
  }
1787
  metaULock(pVnodeObj->pMeta);
23,562✔
1788
  if (TSDB_CODE_SUCCESS != code) {
23,565!
1789
    goto _exit;
×
1790
  }
1791

1792
  if (numOfTables) *numOfTables = ctbNum;
23,565✔
1793
  if (numOfCols) *numOfCols = colNum;
23,565✔
1794
  if (flags) *flags = flag;
23,565✔
1795

1796
  state.uid = uid;
23,565✔
1797
  state.ctbNum = ctbNum;
23,565✔
1798
  state.colNum = colNum;
23,565✔
1799
  state.flags = flag;
23,565✔
1800
  state.keep = keep;
23,565✔
1801
  // upsert the cache
1802
  metaWLock(pVnodeObj->pMeta);
23,565✔
1803

1804
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
23,565✔
1805
  if (ret) {
23,558!
1806
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1807
              uid, ctbNum, colNum, keep, flag);
1808
  }
1809

1810
  metaULock(pVnodeObj->pMeta);
23,558✔
1811

1812
_exit:
596,689✔
1813
  return code;
596,689✔
1814
}
1815

1816
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
183,445✔
1817
  SMetaStbStats stats = {0};
183,445✔
1818

1819
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
183,445✔
1820
    stats.ctbNum += deltaCtb;
162,496✔
1821
    stats.colNum += deltaCol;
162,496✔
1822
    if (deltaKeep > 0) {
162,496✔
1823
      stats.keep = deltaKeep;
11✔
1824
    }
1825

1826
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
162,496✔
1827
    if (code) {
162,495!
1828
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1829
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1830
    }
1831
  }
1832
}
183,498✔
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