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

taosdata / TDengine / #4754

25 Sep 2025 05:58AM UTC coverage: 57.946% (-1.0%) from 58.977%
#4754

push

travis-ci

web-flow
enh: taos command line support '-uroot' on windows (#33055)

133189 of 293169 branches covered (45.43%)

Branch coverage included in aggregate %.

201677 of 284720 relevant lines covered (70.83%)

5398749.0 hits per line

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

55.18
/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,693,237✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,693,237✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,693,237✔
23
  pReader->pAPI = pAPI;
2,693,430✔
24
}
2,693,430✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
3,007,143✔
27
  memset(pReader, 0, sizeof(*pReader));
3,007,143✔
28
  pReader->pMeta = pMeta;
3,007,143✔
29
  pReader->flags = flags;
3,007,143✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
3,007,143✔
31
    metaRLock(pMeta);
2,582,048✔
32
  }
33
}
3,007,349✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,117,310✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,117,310!
37
    metaULock(pReader->pMeta);
1,117,588✔
38
    pReader->flags |= META_READER_NOLOCK;
1,118,217✔
39
  }
40
}
1,117,939✔
41

42
void metaReaderClear(SMetaReader *pReader) {
3,050,717✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
3,050,717✔
44
    metaULock(pReader->pMeta);
1,463,311✔
45
  }
46
  tDecoderClear(&pReader->coder);
3,050,934✔
47
  tdbFree(pReader->pBuf);
3,051,615✔
48
  pReader->pBuf = NULL;
3,051,061✔
49
}
3,051,061✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
3,862,718!
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,861,766✔
63
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
3,861,014✔
64

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

72
  return 0;
3,856,391✔
73
}
74

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

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

84
  metaULock(pVnodeObj->pMeta);
497,509✔
85
  return true;
497,513✔
86
}
87

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

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

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

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

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

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

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

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

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

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

131
  metaRLock(pMeta);
162,834✔
132

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

138
  metaULock(pMeta);
163,156✔
139

140
  return uid;
163,173✔
141
}
142

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

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

156
  return 0;
24,232✔
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) {
6,146✔
175
  int         code = 0;
6,146✔
176
  SMetaReader mr = {0};
6,146✔
177
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
6,146✔
178

179
  SMetaReader *pReader = &mr;
6,153✔
180

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

187
  *uid = *(tb_uid_t *)pReader->pBuf;
3,400✔
188

189
  metaReaderClear(&mr);
3,400✔
190

191
  return 0;
3,400✔
192
}
193

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

199
  code = metaGetTableEntryByName(&mr, tbName);
3,400✔
200
  if (code == 0) *tbType = mr.me.type;
3,400!
201
  if (TSDB_CHILD_TABLE == mr.me.type) {
3,400✔
202
    *suid = mr.me.ctbEntry.suid;
3,382✔
203
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
18✔
204
    *suid = mr.me.uid;
10✔
205
  } else {
206
    *suid = 0;
8✔
207
  }
208

209
  metaReaderClear(&mr);
3,400✔
210
  return code;
3,400✔
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,963✔
246
  SMTbCursor *pTbCur = NULL;
19,963✔
247
  int32_t     code;
248

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

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

267
void metaCloseTbCursor(SMTbCursor *pTbCur) {
39,862✔
268
  if (pTbCur) {
39,862✔
269
    tdbFree(pTbCur->pKey);
19,968✔
270
    tdbFree(pTbCur->pVal);
19,969✔
271
    if (!pTbCur->paused) {
19,971✔
272
      metaReaderClear(&pTbCur->mr);
754✔
273
      if (pTbCur->pDbc) {
754!
274
        tdbTbcClose((TBC *)pTbCur->pDbc);
754✔
275
      }
276
    }
277
    taosMemoryFree(pTbCur);
19,971✔
278
  }
279
}
39,863✔
280

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

300
    if (first) {
20,886✔
301
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
19,964✔
302
      TSDB_CHECK_CODE(code, lino, _exit);
19,962!
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,884✔
319
  }
320

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

328
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
646,601✔
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);
646,601✔
335
    if (ret < 0) {
644,689✔
336
      return ret;
19,907✔
337
    }
338

339
    tDecoderClear(&pTbCur->mr.coder);
624,782✔
340

341
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
626,387✔
342
    if (ret) return ret;
621,086!
343

344
    if (pTbCur->mr.me.type == jumpTableType) {
621,086✔
345
      continue;
10,465✔
346
    }
347

348
    break;
610,621✔
349
  }
350

351
  return 0;
610,621✔
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,272,952✔
383
  void           *pData = NULL;
1,272,952✔
384
  int             nData = 0;
1,272,952✔
385
  int64_t         version;
386
  SSchemaWrapper  schema = {0};
1,272,952✔
387
  SSchemaWrapper *pSchema = NULL;
1,272,952✔
388
  SDecoder        dc = {0};
1,272,952✔
389
  if (lock) {
1,272,952✔
390
    metaRLock(pMeta);
1,249,503✔
391
  }
392
_query:
1,273,380✔
393
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,319,958✔
394
    goto _err;
369✔
395
  }
396

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

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

403
  SMetaEntry me = {0};
1,319,710✔
404
  tDecoderInit(&dc, pData, nData);
1,319,710✔
405
  int32_t code = metaDecodeEntry(&dc, &me);
1,319,497✔
406
  if (code) {
1,319,393!
407
    tDecoderClear(&dc);
×
408
    goto _err;
×
409
  }
410
  if (me.type == TSDB_SUPER_TABLE) {
1,319,393✔
411
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
1,025,467✔
412
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
1,024,958✔
413
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
1,024,958✔
414
      tDecoderClear(&dc);
1,024,957✔
415
      goto _exit;
1,025,138✔
416
    }
417
  } else if (me.type == TSDB_CHILD_TABLE) {
293,926✔
418
    uid = me.ctbEntry.suid;
46,540✔
419
    tDecoderClear(&dc);
46,540✔
420
    goto _query;
46,578✔
421
  } else {
422
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
247,386✔
423
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
247,482✔
424
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
247,482✔
425
      tDecoderClear(&dc);
247,455✔
426
      goto _exit;
247,486✔
427
    }
428
  }
429
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
536✔
430
  tDecoderClear(&dc);
536✔
431

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

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

444
_exit:
1,272,922✔
445
  if (lock) {
1,272,922✔
446
    metaULock(pMeta);
1,248,569✔
447
  }
448
  tdbFree(pData);
1,273,560✔
449
  return pSchema;
1,273,215✔
450

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

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

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

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

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

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

493
_exit:
140✔
494
  if (lock) {
140!
495
    metaULock(pMeta);
140✔
496
  }
497
  tdbFree(pData);
140✔
498
  return createTime;
140✔
499
}
500

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

508
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
722,637!
509
  if (pCtbCur == NULL) {
723,545!
510
    return NULL;
×
511
  }
512

513
  pCtbCur->pMeta = pMeta;
723,545✔
514
  pCtbCur->suid = uid;
723,545✔
515
  pCtbCur->lock = lock;
723,545✔
516
  pCtbCur->paused = 1;
723,545✔
517

518
  ret = metaResumeCtbCursor(pCtbCur, 1);
723,545✔
519
  if (ret < 0) {
722,922!
520
    return NULL;
×
521
  }
522
  return pCtbCur;
722,922✔
523
}
524

525
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
723,048✔
526
  if (pCtbCur) {
723,048!
527
    if (!pCtbCur->paused) {
723,158✔
528
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
691,605!
529
      if (pCtbCur->pCur) {
691,945!
530
        tdbTbcClose(pCtbCur->pCur);
691,970✔
531
      }
532
    }
533
    tdbFree(pCtbCur->pKey);
723,331✔
534
    tdbFree(pCtbCur->pVal);
723,349✔
535
  }
536
  taosMemoryFree(pCtbCur);
723,430!
537
}
723,612✔
538

539
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
31,630✔
540
  if (!pCtbCur->paused) {
31,630!
541
    tdbTbcClose((TBC *)pCtbCur->pCur);
31,632✔
542
    if (pCtbCur->lock) {
31,633✔
543
      metaULock(pCtbCur->pMeta);
31,632✔
544
    }
545
    pCtbCur->paused = 1;
31,632✔
546
  }
547
}
31,630✔
548

549
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
722,963✔
550
  if (pCtbCur->paused) {
722,963!
551
    pCtbCur->paused = 0;
723,146✔
552

553
    if (pCtbCur->lock) {
723,146✔
554
      metaRLock(pCtbCur->pMeta);
699,696✔
555
    }
556
    int ret = 0;
723,162✔
557
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
723,162✔
558
    if (ret < 0) {
723,156!
559
      metaCloseCtbCursor(pCtbCur);
×
560
      return -1;
×
561
    }
562

563
    if (first) {
723,250!
564
      SCtbIdxKey ctbIdxKey;
565
      // move to the suid
566
      ctbIdxKey.suid = pCtbCur->suid;
723,250✔
567
      ctbIdxKey.uid = INT64_MIN;
723,250✔
568
      int c = 0;
723,250✔
569
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
723,250✔
570
      if (c > 0) {
723,021✔
571
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
159,660✔
572
      }
573
    } else {
574
      int c = 0;
×
575
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
576
      if (c < 0) {
×
577
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
578
      } else {
579
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
580
      }
581
    }
582
  }
583
  return 0;
722,776✔
584
}
585

586
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
3,850,500✔
587
  int         ret;
588
  SCtbIdxKey *pCtbIdxKey;
589

590
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
3,850,500✔
591
  if (ret < 0) {
3,852,065✔
592
    return 0;
621,788✔
593
  }
594

595
  pCtbIdxKey = pCtbCur->pKey;
3,230,277✔
596
  if (pCtbIdxKey->suid > pCtbCur->suid) {
3,230,277✔
597
    return 0;
102,154✔
598
  }
599

600
  return pCtbIdxKey->uid;
3,128,123✔
601
}
602

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

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

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

624
  pStbCur->pMeta = pMeta;
141,961✔
625
  pStbCur->suid = suid;
141,961✔
626
  metaRLock(pMeta);
141,961✔
627

628
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
141,961✔
629
  if (ret < 0) {
141,961!
630
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
631
    metaULock(pMeta);
×
632
    taosMemoryFree(pStbCur);
×
633
    return NULL;
×
634
  }
635

636
  // move to the suid
637
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
141,961✔
638
  if (c > 0) {
141,959!
639
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
640
  }
641

642
  return pStbCur;
141,959✔
643
}
644

645
void metaCloseStbCursor(SMStbCursor *pStbCur) {
141,958✔
646
  if (pStbCur) {
141,958!
647
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
141,958!
648
    if (pStbCur->pCur) {
141,959!
649
      tdbTbcClose(pStbCur->pCur);
141,959✔
650

651
      tdbFree(pStbCur->pKey);
141,959✔
652
      tdbFree(pStbCur->pVal);
141,959✔
653
    }
654

655
    taosMemoryFree(pStbCur);
141,960!
656
  }
657
}
141,961✔
658

659
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
663,109✔
660
  int ret;
661

662
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
663,109✔
663
  if (ret < 0) {
663,098✔
664
    return 0;
141,958✔
665
  }
666
  return *(tb_uid_t *)pStbCur->pKey;
521,140✔
667
}
668

669
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
1,168,537✔
670
  STSchema       *pTSchema = NULL;
1,168,537✔
671
  SSchemaWrapper *pSW = NULL;
1,168,537✔
672

673
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
1,168,537✔
674
  if (!pSW) return NULL;
1,168,842✔
675

676
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
1,168,743✔
677

678
  taosMemoryFree(pSW->pSchema);
1,168,949✔
679
  taosMemoryFree(pSW);
1,169,012!
680
  return pTSchema;
1,169,009✔
681
}
682

683
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
2,492✔
684
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
2,492✔
685
  if (*ppTSchema == NULL) {
2,492!
686
    return terrno;
×
687
  }
688
  return TSDB_CODE_SUCCESS;
2,492✔
689
}
690

691
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,158,352✔
692
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,158,352✔
693
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
1,158,775!
694
    return terrno;
×
695
  }
696
  return TSDB_CODE_SUCCESS;
1,158,785✔
697
}
698

699
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
165,671✔
700
  int32_t code = 0;
165,671✔
701
  int32_t lino;
702

703
  void     *pData = NULL;
165,671✔
704
  int       nData = 0;
165,671✔
705
  SSkmDbKey skmDbKey;
706
  if (sver <= 0) {
165,671✔
707
    SMetaInfo info;
708
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
48,559✔
709
      sver = info.skmVer;
48,465✔
710
    } else {
711
      TBC *pSkmDbC = NULL;
99✔
712
      int  c;
713

714
      skmDbKey.uid = suid ? suid : uid;
99!
715
      skmDbKey.sver = INT32_MAX;
99✔
716

717
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
99✔
718
      TSDB_CHECK_CODE(code, lino, _exit);
99!
719
      metaRLock(pMeta);
99✔
720

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

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

736
      if (c < 0) {
99✔
737
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
8✔
738
      }
739

740
      const void *pKey = NULL;
99✔
741
      int32_t     nKey = 0;
99✔
742
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
99✔
743

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

751
      sver = ((SSkmDbKey *)pKey)->sver;
99✔
752

753
      metaULock(pMeta);
99✔
754
      tdbTbcClose(pSkmDbC);
99✔
755
    }
756
  }
757

758
  if (!(sver > 0)) {
165,669!
759
    code = TSDB_CODE_NOT_FOUND;
×
760
    goto _exit;
×
761
  }
762

763
  skmDbKey.uid = suid ? suid : uid;
165,669✔
764
  skmDbKey.sver = sver;
165,669✔
765
  metaRLock(pMeta);
165,669✔
766
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
165,703✔
767
    metaULock(pMeta);
4✔
768
    code = TSDB_CODE_NOT_FOUND;
4✔
769
    goto _exit;
4✔
770
  }
771
  metaULock(pMeta);
165,645✔
772

773
  // decode
774
  SDecoder        dc = {0};
165,664✔
775
  SSchemaWrapper  schema;
776
  SSchemaWrapper *pSchemaWrapper = &schema;
165,664✔
777

778
  tDecoderInit(&dc, pData, nData);
165,664✔
779
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
165,892✔
780
  tDecoderClear(&dc);
165,892✔
781
  tdbFree(pData);
165,619✔
782
  if (TSDB_CODE_SUCCESS != code) {
165,672!
783
    taosMemoryFree(pSchemaWrapper->pSchema);
×
784
    goto _exit;
×
785
  }
786

787
  // convert
788
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
165,672✔
789
  if (pTSchema == NULL) {
165,633!
790
    code = TSDB_CODE_OUT_OF_MEMORY;
×
791
  }
792

793
  *ppTSchema = pTSchema;
165,633✔
794
  taosMemoryFree(pSchemaWrapper->pSchema);
165,633!
795

796
_exit:
165,671✔
797
  return code;
165,671✔
798
}
799

800
// N.B. Called by statusReq per second
801
int64_t metaGetTbNum(SMeta *pMeta) {
272,847✔
802
  // num of child tables (excluding normal tables , stables and others)
803

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

807
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
272,847✔
808
}
809

810
void metaUpdTimeSeriesNum(SMeta *pMeta) {
141,966✔
811
  int64_t nCtbTimeSeries = 0;
141,966✔
812
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
141,966!
813
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
141,966✔
814
  }
815
}
141,967✔
816

817
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
818
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
819
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
444,854✔
820
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
444,902✔
821
    metaUpdTimeSeriesNum(pMeta);
141,918✔
822
  }
823

824
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
444,906✔
825
}
826

827
// type: 1 reported timeseries
828
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
444,854!
829
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
444,906✔
830
  if (type == 1) {
444,906✔
831
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
272,847✔
832
  }
833
  return nTimeSeries;
444,908✔
834
}
835

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

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

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

858
  pSmaCur->pMeta = pMeta;
×
859
  pSmaCur->uid = uid;
×
860
  metaRLock(pMeta);
×
861

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

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

877
  return pSmaCur;
×
878
}
879

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

887
      tdbFree(pSmaCur->pKey);
×
888
      tdbFree(pSmaCur->pVal);
×
889
    }
890

891
    taosMemoryFree(pSmaCur);
×
892
  }
893
}
×
894

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

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

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

909
  return pSmaIdxKey->uid;
×
910
}
911

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

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

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

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

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

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

971
    ++smaIdx;
×
972
  }
973

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

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

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

1003
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1004

1005
  metaReaderClear(&mr);
×
1006
  return pTSma;
×
1007
}
1008

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

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

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

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

1033
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1034

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

1043
  metaCloseSmaCursor(pCur);
×
1044
  return pUids;
×
1045
}
1046

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

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

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

1063
    if (lastUid == uid) {
×
1064
      continue;
×
1065
    }
1066

1067
    lastUid = uid;
×
1068

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

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

1086
  metaCloseSmaCursor(pCur);
×
1087
  return pUids;
×
1088
}
1089

1090
#endif
1091

1092
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
4,084,434✔
1093
  STag *tag = (STag *)pTag;
4,084,434✔
1094
  if (type == TSDB_DATA_TYPE_JSON) {
4,084,434✔
1095
    return tag;
10,414✔
1096
  }
1097
  bool find = tTagGet(tag, val);
4,074,020✔
1098

1099
  if (!find) {
4,076,917✔
1100
    return NULL;
48,811✔
1101
  }
1102

1103
  return val;
4,028,106✔
1104
}
1105

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

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

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

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

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

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

1148
  int32_t valid = 0;
6✔
1149
  int32_t count = 0;
6✔
1150

1151
  static const int8_t TRY_ERROR_LIMIT = 1;
1152
  do {
32,228✔
1153
    void   *entryKey = NULL;
32,234✔
1154
    int32_t nEntryKey = -1;
32,234✔
1155
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
32,234✔
1156
    if (valid < 0) break;
30,949✔
1157

1158
    SBtimeIdxKey *p = entryKey;
30,943✔
1159
    if (count > TRY_ERROR_LIMIT) break;
30,943!
1160

1161
    terrno = TSDB_CODE_SUCCESS;
30,943✔
1162
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
31,579✔
1163
    if (terrno != TSDB_CODE_SUCCESS) {
31,566!
1164
      ret = terrno;
×
1165
      break;
×
1166
    }
1167
    if (cmp == 0) {
31,561!
1168
      if (taosArrayPush(pUids, &p->uid) == NULL) {
63,729!
1169
        ret = terrno;
×
1170
        break;
×
1171
      }
1172
    } else {
1173
      if (param->equal == true) {
×
1174
        if (count > TRY_ERROR_LIMIT) break;
×
1175
        count++;
×
1176
      }
1177
    }
1178
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
32,168✔
1179
    if (valid < 0) break;
32,228!
1180
  } while (1);
1181

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

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

1195
  STagIdxKey *pKey = NULL;
×
1196
  int32_t     nKey = 0;
×
1197

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

1208
  char *pName = param->val;
×
1209

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

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

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

1230
    if (count > TRY_ERROR_LIMIT) break;
×
1231

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

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

1263
  taosMemoryFree(pCursor);
×
1264

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

1273
  STtlIdxKey *pKey = NULL;
×
1274
  int32_t     nKey = 0;
×
1275

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

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

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

1295
  taosMemoryFree(pCursor);
×
1296

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

1305
  SMetaEntry oStbEntry = {0};
7,152✔
1306
  int32_t    code = 0;
7,152✔
1307
  char      *buf = NULL;
7,152✔
1308
  void      *pData = NULL;
7,152✔
1309
  int        nData = 0;
7,152✔
1310

1311
  SDecoder    dc = {0};
7,152✔
1312
  STbDbKey    tbDbKey = {0};
7,152✔
1313
  STagIdxKey *pKey = NULL;
7,152✔
1314
  int32_t     nKey = 0;
7,152✔
1315

1316
  SIdxCursor *pCursor = NULL;
7,152✔
1317
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
7,152!
1318
  if (!pCursor) {
7,174!
1319
    return terrno;
×
1320
  }
1321
  pCursor->pMeta = pMeta;
7,174✔
1322
  pCursor->suid = param->suid;
7,174✔
1323
  pCursor->cid = param->cid;
7,174✔
1324
  pCursor->type = param->type;
7,174✔
1325

1326
  metaRLock(pMeta);
7,174✔
1327

1328
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
7,170!
1329

1330
  tbDbKey.uid = param->suid;
7,164✔
1331
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
7,164✔
1332

1333
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
7,164!
1334

1335
  tDecoderInit(&dc, pData, nData);
7,175✔
1336

1337
  code = metaDecodeEntry(&dc, &oStbEntry);
7,172✔
1338
  if (code) {
7,161!
1339
    tDecoderClear(&dc);
×
1340
    goto END;
×
1341
  }
1342

1343
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
7,161!
1344
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1345
  }
1346

1347
  code = TSDB_CODE_INVALID_PARA;
7,161✔
1348

1349
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
9,894!
1350
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
9,904✔
1351
    if (IS_IDX_ON(schema)) {
9,904✔
1352
      if (schema->colId == param->cid && param->type == schema->type) {
9,875!
1353
        code = 0;
7,171✔
1354
        break;
7,171✔
1355
      }
1356
    }
1357
  }
1358

1359
  TAOS_CHECK_GOTO(code, NULL, END);
7,161!
1360

1361
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
7,161✔
1362
  if (code != 0) {
7,164!
1363
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1364
  }
1365

1366
  int32_t maxSize = 0;
7,168✔
1367
  int32_t nTagData = 0;
7,168✔
1368
  void   *tagData = NULL;
7,168✔
1369

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

1378
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
761✔
1379
        maxSize = 4 * nTagData + 1;
175✔
1380
        buf = taosMemoryCalloc(1, maxSize);
175!
1381
        if (buf == NULL) {
177!
1382
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1383
        }
1384

1385
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
177✔
1386
          TAOS_CHECK_GOTO(terrno, NULL, END);
2!
1387
        }
1388

1389
        tagData = buf;
175✔
1390
        nTagData = maxSize;
175✔
1391
      }
1392
    } else {
1393
      tagData = param->val;
6,407✔
1394
      nTagData = tDataTypes[param->type].bytes;
6,407✔
1395
    }
1396
  }
1397

1398
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
7,168!
1399
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1400
                  NULL, END);
1401

1402
  int cmp = 0;
7,167✔
1403
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
7,167!
1404

1405
  int     count = 0;
7,166✔
1406
  int32_t valid = 0;
7,166✔
1407
  bool    found = false;
7,166✔
1408

1409
  static const int8_t TRY_ERROR_LIMIT = 1;
1410

1411
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1412
  /// target:                        [suid, cid2, type2]
1413
  int diffCidCount = 0;
7,166✔
1414
  do {
78,237✔
1415
    void   *entryKey = NULL, *entryVal = NULL;
85,403✔
1416
    int32_t nEntryKey, nEntryVal;
1417

1418
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
85,403✔
1419
    if (valid < 0) {
84,857✔
1420
      break;
7,173✔
1421
    }
1422
    if (count > TRY_ERROR_LIMIT) {
81,295✔
1423
      break;
1,827✔
1424
    }
1425

1426
    STagIdxKey *p = entryKey;
79,468✔
1427
    if (p == NULL) break;
79,468!
1428

1429
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
79,468✔
1430
      if (found == true) break;  //
2,305✔
1431
      if (diffCidCount > TRY_ERROR_LIMIT) break;
521!
1432
      diffCidCount++;
521✔
1433
      count++;
521✔
1434
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
521✔
1435
      if (valid < 0) {
566!
1436
        code = valid;
×
1437
        break;
×
1438
      } else {
1439
        continue;
566✔
1440
      }
1441
    }
1442

1443
    terrno = TSDB_CODE_SUCCESS;
77,163✔
1444
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
77,165✔
1445
    if (terrno != TSDB_CODE_SUCCESS) {
77,141!
1446
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1447
      break;
×
1448
    }
1449
    if (cmp == 0) {
77,184✔
1450
      // match
1451
      tb_uid_t tuid = 0;
69,802✔
1452
      if (IS_VAR_DATA_TYPE(pKey->type)) {
69,802!
1453
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
712✔
1454
      } else {
1455
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
69,090✔
1456
      }
1457
      if (taosArrayPush(pUids, &tuid) == NULL) {
70,313!
1458
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1459
      }
1460
      found = true;
70,313✔
1461
    } else {
1462
      if (param->equal == true) {
7,382✔
1463
        if (count > TRY_ERROR_LIMIT) break;
4,493!
1464
        count++;
4,493✔
1465
      }
1466
    }
1467
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
77,695✔
1468
    if (valid < 0) {
77,671!
1469
      code = valid;
×
1470
      break;
×
1471
    }
1472
  } while (1);
1473

1474
END:
7,173✔
1475
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
7,173!
1476
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
7,174!
1477
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
7,171!
1478
  tDecoderClear(&dc);
7,171✔
1479
  tdbFree(pData);
7,170✔
1480

1481
  taosMemoryFree(buf);
7,173!
1482
  taosMemoryFree(pKey);
7,173!
1483

1484
  taosMemoryFree(pCursor);
7,171✔
1485

1486
  return code;
7,173✔
1487
}
1488

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

1495
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
67,024✔
1496
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
67,024✔
1497
  if (lock) {
67,272!
1498
    metaULock(pMeta);
×
1499
  }
1500

1501
  return ret;
67,272✔
1502
}
1503

1504
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
6,222✔
1505
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
6,222✔
1506
  const int32_t LIMIT = 128;
6,222✔
1507

1508
  int32_t isLock = false;
6,222✔
1509
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
6,222!
1510
  for (int i = 0; i < sz; i++) {
73,262✔
1511
    STUidTagInfo *p = taosArrayGet(uidList, i);
67,057✔
1512

1513
    if (i % LIMIT == 0) {
67,045✔
1514
      if (isLock) metaULock(pMeta);
4,974✔
1515

1516
      metaRLock(pMeta);
4,974✔
1517
      isLock = true;
4,974✔
1518
    }
1519

1520
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1521
    void   *val = NULL;
67,045✔
1522
    int32_t len = 0;
67,045✔
1523
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
67,045!
1524
      p->pTagVal = taosMemoryMalloc(len);
67,269!
1525
      if (!p->pTagVal) {
67,171!
1526
        if (isLock) metaULock(pMeta);
×
1527

1528
        TAOS_RETURN(terrno);
×
1529
      }
1530
      memcpy(p->pTagVal, val, len);
67,171✔
1531
      tdbFree(val);
67,171✔
1532
    } else {
1533
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
×
1534
    }
1535
  }
1536
  //  }
1537
  if (isLock) metaULock(pMeta);
6,205✔
1538
  return 0;
6,221✔
1539
}
1540

1541
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
111,629✔
1542
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
111,629✔
1543
  if (!pCur) {
111,690!
1544
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1545
  }
1546

1547
  // If len > 0 means there already have uids, and we only want the
1548
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1549
  // in the hash map, that may require a lot of memory
1550
  SHashObj *pSepecifiedUidMap = NULL;
111,690✔
1551
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
111,690✔
1552
  if (numOfElems > 0) {
111,683✔
1553
    pSepecifiedUidMap =
1554
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
40,169✔
1555
    for (int i = 0; i < numOfElems; i++) {
229,805✔
1556
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
189,627✔
1557
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
189,628✔
1558
      if (code) {
189,652!
1559
        metaCloseCtbCursor(pCur);
×
1560
        taosHashCleanup(pSepecifiedUidMap);
×
1561
        return code;
×
1562
      }
1563
    }
1564
  }
1565

1566
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
111,692✔
1567
    while (1) {
367,382✔
1568
      tb_uid_t uid = metaCtbCursorNext(pCur);
438,886✔
1569
      if (uid == 0) {
433,941✔
1570
        break;
71,546✔
1571
      }
1572

1573
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
362,395✔
1574
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
362,395✔
1575
      if (!info.pTagVal) {
367,847!
1576
        metaCloseCtbCursor(pCur);
×
1577
        taosHashCleanup(pSepecifiedUidMap);
×
1578
        return terrno;
×
1579
      }
1580
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
367,847✔
1581
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
367,382!
1582
        taosMemoryFreeClear(info.pTagVal);
×
1583
        metaCloseCtbCursor(pCur);
×
1584
        taosHashCleanup(pSepecifiedUidMap);
×
1585
        return terrno;
×
1586
      }
1587
    }
1588
  } else {  // only the specified tables need to be added
1589
    while (1) {
237,341✔
1590
      tb_uid_t uid = metaCtbCursorNext(pCur);
277,529✔
1591
      if (uid == 0) {
277,658✔
1592
        break;
40,170✔
1593
      }
1594

1595
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
237,488✔
1596
      if (index == NULL) {
237,371✔
1597
        continue;
47,878✔
1598
      }
1599

1600
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
189,493✔
1601
      if (pTagInfo->pTagVal == NULL) {
189,501!
1602
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
189,576✔
1603
        if (!pTagInfo->pTagVal) {
189,538!
1604
          metaCloseCtbCursor(pCur);
×
1605
          taosHashCleanup(pSepecifiedUidMap);
×
1606
          return terrno;
×
1607
        }
1608
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
189,538✔
1609
      }
1610
    }
1611
  }
1612

1613
  taosHashCleanup(pSepecifiedUidMap);
111,716✔
1614
  metaCloseCtbCursor(pCur);
111,710✔
1615
  return TSDB_CODE_SUCCESS;
111,741✔
1616
}
1617

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

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

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

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

1644
  metaCloseStbCursor(pCur);
×
1645

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

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

1656
      taosArrayDestroy(uids);
×
1657
      taosArrayDestroy(suids);
×
1658

1659
      return code;
×
1660
    }
1661

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

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

1669
        taosArrayDestroy(uids);
×
1670
        taosArrayDestroy(suids);
×
1671

1672
        return code;
×
1673
      }
1674

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

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

1684
          tDestroyTSchema(pTSchema);
×
1685
          taosArrayDestroy(uids);
×
1686
          taosArrayDestroy(suids);
×
1687

1688
          return code;
×
1689
        }
1690
      }
1691

1692
      tDestroyTSchema(pTSchema);
×
1693
    }
1694

1695
    taosArrayDestroy(uids);
×
1696
  }
1697

1698
  taosArrayDestroy(suids);
×
1699

1700
  return TSDB_CODE_SUCCESS;
×
1701
}
1702

1703
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1704

1705
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
7,149,263✔
1706
  int32_t code = 0;
7,149,263✔
1707
  void   *pData = NULL;
7,149,263✔
1708
  int     nData = 0;
7,149,263✔
1709
  int     lock = 0;
7,149,263✔
1710

1711
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
7,149,263!
1712
    lock = 1;
2,649,361✔
1713
  }
1714

1715
  if (!lock) metaRLock(pMeta);
7,149,263✔
1716

1717
  // search cache
1718
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
7,149,822✔
1719
    if (!lock) metaULock(pMeta);
6,891,764✔
1720
    goto _exit;
6,891,827✔
1721
  }
1722

1723
  // search TDB
1724
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
258,847✔
1725
    // not found
1726
    if (!lock) metaULock(pMeta);
243,300✔
1727
    goto _exit;
243,305✔
1728
  }
1729

1730
  if (!lock) metaULock(pMeta);
15,562✔
1731

1732
  pInfo->uid = uid;
15,562✔
1733
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
15,562✔
1734
  pInfo->version = ((SUidIdxVal *)pData)->version;
15,562✔
1735
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
15,562✔
1736

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

1749
  if (lock) {
15,565✔
1750
    metaRLock(pReader->pMeta);
3,361✔
1751
  }
1752

1753
_exit:
12,204✔
1754
  tdbFree(pData);
7,150,697✔
1755
  return code;
7,150,445✔
1756
}
1757

1758
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
699,026✔
1759
  int32_t code = 0;
699,026✔
1760

1761
  if (!numOfTables && !numOfCols) goto _exit;
699,026!
1762

1763
  SVnode *pVnodeObj = pVnode;
699,026✔
1764
  metaRLock(pVnodeObj->pMeta);
699,026✔
1765

1766
  // fast path: search cache
1767
  SMetaStbStats state = {0};
699,070✔
1768
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
699,070✔
1769
    metaULock(pVnodeObj->pMeta);
675,255✔
1770
    if (numOfTables) *numOfTables = state.ctbNum;
675,269✔
1771
    if (numOfCols) *numOfCols = state.colNum;
675,269✔
1772
    if (flags) *flags = state.flags;
675,269✔
1773
    goto _exit;
675,269✔
1774
  }
1775

1776
  // slow path: search TDB
1777
  int64_t ctbNum = 0;
23,803✔
1778
  int32_t colNum = 0;
23,803✔
1779
  int64_t keep = 0;
23,803✔
1780
  int8_t  flag = 0;
23,803✔
1781

1782
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
23,803✔
1783
  if (TSDB_CODE_SUCCESS == code) {
23,803!
1784
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
23,803✔
1785
  }
1786
  if (TSDB_CODE_SUCCESS == code) {
23,801!
1787
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
23,801✔
1788
  }
1789
  metaULock(pVnodeObj->pMeta);
23,803✔
1790
  if (TSDB_CODE_SUCCESS != code) {
23,802!
1791
    goto _exit;
×
1792
  }
1793

1794
  if (numOfTables) *numOfTables = ctbNum;
23,802✔
1795
  if (numOfCols) *numOfCols = colNum;
23,802✔
1796
  if (flags) *flags = flag;
23,802✔
1797

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

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

1812
  metaULock(pVnodeObj->pMeta);
23,802✔
1813

1814
_exit:
699,048✔
1815
  return code;
699,048✔
1816
}
1817

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

1821
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
183,929✔
1822
    stats.ctbNum += deltaCtb;
163,110✔
1823
    stats.colNum += deltaCol;
163,110✔
1824
    if (deltaKeep > 0) {
163,110✔
1825
      stats.keep = deltaKeep;
11✔
1826
    }
1827

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