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

taosdata / TDengine / #4687

25 Aug 2025 07:22AM UTC coverage: 57.894% (-2.2%) from 60.092%
#4687

push

travis-ci

web-flow
fix: add taosBenchmark windows support params (#32708)

132643 of 292257 branches covered (45.39%)

Branch coverage included in aggregate %.

201266 of 284501 relevant lines covered (70.74%)

4743408.21 hits per line

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

52.97
/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,716,814✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,716,814✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,716,814✔
23
  pReader->pAPI = pAPI;
2,717,800✔
24
}
2,717,800✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
3,100,987✔
27
  memset(pReader, 0, sizeof(*pReader));
3,100,987✔
28
  pReader->pMeta = pMeta;
3,100,987✔
29
  pReader->flags = flags;
3,100,987✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
3,100,987!
31
    metaRLock(pMeta);
2,674,694✔
32
  }
33
}
3,101,104✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,135,741✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,135,741!
37
    metaULock(pReader->pMeta);
1,136,333✔
38
    pReader->flags |= META_READER_NOLOCK;
1,137,058✔
39
  }
40
}
1,136,466✔
41

42
void metaReaderClear(SMetaReader *pReader) {
3,143,495✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
3,143,495✔
44
    metaULock(pReader->pMeta);
1,537,149✔
45
  }
46
  tDecoderClear(&pReader->coder);
3,143,973✔
47
  tdbFree(pReader->pBuf);
3,143,763✔
48
  pReader->pBuf = NULL;
3,144,521✔
49
}
3,144,521✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
3,960,101!
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,959,606✔
63
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
3,958,638✔
64

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

72
  return 0;
3,954,979✔
73
}
74

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

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

84
  metaULock(pVnodeObj->pMeta);
487,920✔
85
  return true;
487,933✔
86
}
87

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

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

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

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

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

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

113
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
123,294✔
114
  SMeta   *pMeta = pReader->pMeta;
123,294✔
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,294✔
119
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
48,056✔
120
  }
121

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

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

131
  metaRLock(pMeta);
162,488✔
132

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

138
  metaULock(pMeta);
162,737✔
139

140
  return uid;
162,736✔
141
}
142

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

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

156
  return 0;
54,076✔
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) {
20,028✔
246
  SMTbCursor *pTbCur = NULL;
20,028✔
247
  int32_t     code;
248

249
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
20,028!
250
  if (pTbCur == NULL) {
20,033!
251
    return NULL;
×
252
  }
253

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

267
void metaCloseTbCursor(SMTbCursor *pTbCur) {
39,989✔
268
  if (pTbCur) {
39,989✔
269
    tdbFree(pTbCur->pKey);
20,031✔
270
    tdbFree(pTbCur->pVal);
20,034✔
271
    if (!pTbCur->paused) {
20,033✔
272
      metaReaderClear(&pTbCur->mr);
900✔
273
      if (pTbCur->pDbc) {
899!
274
        tdbTbcClose((TBC *)pTbCur->pDbc);
900✔
275
      }
276
    }
277
    taosMemoryFree(pTbCur);
20,032!
278
  }
279
}
39,990✔
280

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

300
    if (first) {
20,949✔
301
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
20,027✔
302
      TSDB_CHECK_CODE(code, lino, _exit);
20,034!
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,956✔
319
  }
320

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

328
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
649,442✔
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);
649,442✔
335
    if (ret < 0) {
647,025✔
336
      return ret;
19,969✔
337
    }
338

339
    tDecoderClear(&pTbCur->mr.coder);
627,056✔
340

341
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
629,208✔
342
    if (ret) return ret;
627,117!
343

344
    if (pTbCur->mr.me.type == jumpTableType) {
627,117✔
345
      continue;
10,359✔
346
    }
347

348
    break;
616,758✔
349
  }
350

351
  return 0;
616,758✔
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,328,992✔
383
  void           *pData = NULL;
1,328,992✔
384
  int             nData = 0;
1,328,992✔
385
  int64_t         version;
386
  SSchemaWrapper  schema = {0};
1,328,992✔
387
  SSchemaWrapper *pSchema = NULL;
1,328,992✔
388
  SDecoder        dc = {0};
1,328,992✔
389
  if (lock) {
1,328,992✔
390
    metaRLock(pMeta);
1,305,548✔
391
  }
392
_query:
1,329,538✔
393
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,426,397✔
394
    goto _err;
385✔
395
  }
396

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

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

403
  SMetaEntry me = {0};
1,426,106✔
404
  tDecoderInit(&dc, pData, nData);
1,426,106✔
405
  int32_t code = metaDecodeEntry(&dc, &me);
1,425,807✔
406
  if (code) {
1,425,470!
407
    tDecoderClear(&dc);
×
408
    goto _err;
×
409
  }
410
  if (me.type == TSDB_SUPER_TABLE) {
1,425,470✔
411
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
1,082,484!
412
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
1,082,386✔
413
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
1,082,386✔
414
      tDecoderClear(&dc);
1,082,362✔
415
      goto _exit;
1,082,747✔
416
    }
417
  } else if (me.type == TSDB_CHILD_TABLE) {
342,986✔
418
    uid = me.ctbEntry.suid;
96,776✔
419
    tDecoderClear(&dc);
96,776✔
420
    goto _query;
96,859✔
421
  } else {
422
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
246,210!
423
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
246,290✔
424
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
246,290✔
425
      tDecoderClear(&dc);
246,271✔
426
      goto _exit;
246,300✔
427
    }
428
  }
429
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
×
430
  tDecoderClear(&dc);
×
431

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

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

444
_exit:
1,329,179✔
445
  if (lock) {
1,329,179✔
446
    metaULock(pMeta);
1,304,609✔
447
  }
448
  tdbFree(pData);
1,329,999✔
449
  return pSchema;
1,329,289✔
450

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

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

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

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

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

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

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

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

506
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
737,854!
507
  if (pCtbCur == NULL) {
738,865!
508
    return NULL;
×
509
  }
510

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

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

523
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
738,390✔
524
  if (pCtbCur) {
738,390!
525
    if (!pCtbCur->paused) {
738,482✔
526
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
707,897!
527
      if (pCtbCur->pCur) {
708,231!
528
        tdbTbcClose(pCtbCur->pCur);
708,264✔
529
      }
530
    }
531
    tdbFree(pCtbCur->pKey);
738,665✔
532
    tdbFree(pCtbCur->pVal);
738,686✔
533
  }
534
  taosMemoryFree(pCtbCur);
738,697!
535
}
738,948✔
536

537
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
30,646✔
538
  if (!pCtbCur->paused) {
30,646!
539
    tdbTbcClose((TBC *)pCtbCur->pCur);
30,648✔
540
    if (pCtbCur->lock) {
30,656!
541
      metaULock(pCtbCur->pMeta);
30,656✔
542
    }
543
    pCtbCur->paused = 1;
30,658✔
544
  }
545
}
30,656✔
546

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

551
    if (pCtbCur->lock) {
738,430✔
552
      metaRLock(pCtbCur->pMeta);
715,038✔
553
    }
554
    int ret = 0;
738,411✔
555
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
738,411✔
556
    if (ret < 0) {
738,606!
557
      metaCloseCtbCursor(pCtbCur);
×
558
      return -1;
×
559
    }
560

561
    if (first) {
738,642!
562
      SCtbIdxKey ctbIdxKey;
563
      // move to the suid
564
      ctbIdxKey.suid = pCtbCur->suid;
738,642✔
565
      ctbIdxKey.uid = INT64_MIN;
738,642✔
566
      int c = 0;
738,642✔
567
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
738,642✔
568
      if (c > 0) {
738,233✔
569
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
54,429✔
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;
738,084✔
582
}
583

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

588
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
3,983,417✔
589
  if (ret < 0) {
3,987,713✔
590
    return 0;
341,215✔
591
  }
592

593
  pCtbIdxKey = pCtbCur->pKey;
3,646,498✔
594
  if (pCtbIdxKey->suid > pCtbCur->suid) {
3,646,498✔
595
    return 0;
398,078✔
596
  }
597

598
  return pCtbIdxKey->uid;
3,248,420✔
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) {
133,456✔
612
  SMStbCursor *pStbCur = NULL;
133,456✔
613
  int          ret = 0;
133,456✔
614
  int          c = 0;
133,456✔
615

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

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

626
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
133,457✔
627
  if (ret < 0) {
133,457!
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);
133,457✔
636
  if (c > 0) {
133,456!
637
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
638
  }
639

640
  return pStbCur;
133,456✔
641
}
642

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

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

653
    taosMemoryFree(pStbCur);
133,457!
654
  }
655
}
133,457✔
656

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

660
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
522,380✔
661
  if (ret < 0) {
522,375✔
662
    return 0;
133,455✔
663
  }
664
  return *(tb_uid_t *)pStbCur->pKey;
388,920✔
665
}
666

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

671
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
1,174,025✔
672
  if (!pSW) return NULL;
1,174,415✔
673

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

676
  taosMemoryFree(pSW->pSchema);
1,174,508✔
677
  taosMemoryFree(pSW);
1,174,572!
678
  return pTSchema;
1,174,573✔
679
}
680

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

689
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,164,215✔
690
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,164,215✔
691
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
1,164,780✔
692
    return terrno;
11✔
693
  }
694
  return TSDB_CODE_SUCCESS;
1,164,769✔
695
}
696

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

701
  void     *pData = NULL;
163,367✔
702
  int       nData = 0;
163,367✔
703
  SSkmDbKey skmDbKey;
704
  if (sver <= 0) {
163,367✔
705
    SMetaInfo info;
706
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
47,553✔
707
      sver = info.skmVer;
47,448✔
708
    } else {
709
      TBC *pSkmDbC = NULL;
107✔
710
      int  c;
711

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

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

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

726
      if (c == 0) {
106!
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) {
106!
735
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
736
      }
737

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

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

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

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

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

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

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

776
  tDecoderInit(&dc, pData, nData);
163,360✔
777
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
163,510✔
778
  tDecoderClear(&dc);
163,510✔
779
  tdbFree(pData);
163,308✔
780
  if (TSDB_CODE_SUCCESS != code) {
163,367!
781
    taosMemoryFree(pSchemaWrapper->pSchema);
×
782
    goto _exit;
×
783
  }
784

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

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

794
_exit:
163,365✔
795
  return code;
163,365✔
796
}
797

798
// N.B. Called by statusReq per second
799
int64_t metaGetTbNum(SMeta *pMeta) {
252,899✔
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;
252,899✔
806
}
807

808
void metaUpdTimeSeriesNum(SMeta *pMeta) {
133,463✔
809
  int64_t nCtbTimeSeries = 0;
133,463✔
810
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
133,463!
811
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
133,463✔
812
  }
813
}
133,463✔
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;
428,600✔
818
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
428,630✔
819
    metaUpdTimeSeriesNum(pMeta);
133,433✔
820
  }
821

822
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
428,639✔
823
}
824

825
// type: 1 reported timeseries
826
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
428,600!
827
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
428,639✔
828
  if (type == 1) {
428,639✔
829
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
252,899✔
830
  }
831
  return nTimeSeries;
428,634✔
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,490,247✔
1091
  STag *tag = (STag *)pTag;
4,490,247✔
1092
  if (type == TSDB_DATA_TYPE_JSON) {
4,490,247✔
1093
    return tag;
10,413✔
1094
  }
1095
  bool find = tTagGet(tag, val);
4,479,834✔
1096

1097
  if (!find) {
4,482,814✔
1098
    return NULL;
48,016✔
1099
  }
1100

1101
  return val;
4,434,798✔
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);
5✔
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 {
35,341✔
1151
    void   *entryKey = NULL;
35,347✔
1152
    int32_t nEntryKey = -1;
35,347✔
1153
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
35,347✔
1154
    if (valid < 0) break;
35,283✔
1155

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

1159
    terrno = TSDB_CODE_SUCCESS;
35,277✔
1160
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
35,209✔
1161
    if (terrno != TSDB_CODE_SUCCESS) {
34,474!
1162
      ret = terrno;
×
1163
      break;
×
1164
    }
1165
    if (cmp == 0) {
34,459!
1166
      if (taosArrayPush(pUids, &p->uid) == NULL) {
69,782!
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);
35,323✔
1177
    if (valid < 0) break;
35,341!
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,660✔
1300
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6,660✔
1301
  SMetaFltParam *param = arg;
6,660✔
1302

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

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

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

1324
  metaRLock(pMeta);
6,678✔
1325

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

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

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

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

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

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

1345
  code = TSDB_CODE_INVALID_PARA;
6,669✔
1346

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

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

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

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

1368
  if (param->val == NULL) {
6,678!
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,678!
1373
      tagData = varDataVal(param->val);
360✔
1374
      nTagData = varDataLen(param->val);
360✔
1375

1376
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
360✔
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,318✔
1392
      nTagData = tDataTypes[param->type].bytes;
6,318✔
1393
    }
1394
  }
1395

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

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

1403
  int     count = 0;
6,679✔
1404
  int32_t valid = 0;
6,679✔
1405
  bool    found = false;
6,679✔
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,679✔
1412
  do {
76,909✔
1413
    void   *entryKey = NULL, *entryVal = NULL;
83,588✔
1414
    int32_t nEntryKey, nEntryVal;
1415

1416
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
83,588✔
1417
    if (valid < 0) {
82,966✔
1418
      break;
6,679✔
1419
    }
1420
    if (count > TRY_ERROR_LIMIT) {
79,386✔
1421
      break;
1,501✔
1422
    }
1423

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

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

1441
    terrno = TSDB_CODE_SUCCESS;
76,022✔
1442
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
76,020✔
1443
    if (terrno != TSDB_CODE_SUCCESS) {
75,961!
1444
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1445
      break;
×
1446
    }
1447
    if (cmp == 0) {
76,006✔
1448
      // match
1449
      tb_uid_t tuid = 0;
69,056✔
1450
      if (IS_VAR_DATA_TYPE(pKey->type)) {
69,056!
1451
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
598✔
1452
      } else {
1453
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
68,458✔
1454
      }
1455
      if (taosArrayPush(pUids, &tuid) == NULL) {
69,638!
1456
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1457
      }
1458
      found = true;
69,638✔
1459
    } else {
1460
      if (param->equal == true) {
6,950✔
1461
        if (count > TRY_ERROR_LIMIT) break;
4,043!
1462
        count++;
4,043✔
1463
      }
1464
    }
1465
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
76,588✔
1466
    if (valid < 0) {
76,589!
1467
      code = valid;
×
1468
      break;
×
1469
    }
1470
  } while (1);
1471

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

1479
  taosMemoryFree(buf);
6,682!
1480
  taosMemoryFree(pKey);
6,682!
1481

1482
  taosMemoryFree(pCursor);
6,681!
1483

1484
  return code;
6,684✔
1485
}
1486

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

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

1499
  return ret;
62,926✔
1500
}
1501

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

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

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

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

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

1526
        TAOS_RETURN(terrno);
×
1527
      }
1528
      memcpy(p->pTagVal, val, len);
62,805✔
1529
      tdbFree(val);
62,805✔
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,994✔
1536
  return 0;
5,996✔
1537
}
1538

1539
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
117,878✔
1540
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
117,878✔
1541
  if (!pCur) {
117,884!
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;
117,884✔
1549
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
117,884✔
1550
  if (numOfElems > 0) {
117,885✔
1551
    pSepecifiedUidMap =
1552
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
39,206✔
1553
    for (int i = 0; i < numOfElems; i++) {
230,216✔
1554
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
191,002✔
1555
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
191,000✔
1556
      if (code) {
191,032!
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
117,893✔
1565
    while (1) {
412,790✔
1566
      tb_uid_t uid = metaCtbCursorNext(pCur);
491,450✔
1567
      if (uid == 0) {
485,163✔
1568
        break;
78,730✔
1569
      }
1570

1571
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
406,433✔
1572
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
406,433✔
1573
      if (!info.pTagVal) {
413,002!
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
413,002✔
1579
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
412,790!
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,240✔
1588
      tb_uid_t uid = metaCtbCursorNext(pCur);
277,473✔
1589
      if (uid == 0) {
277,287✔
1590
        break;
39,209✔
1591
      }
1592

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

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

1611
  taosHashCleanup(pSepecifiedUidMap);
117,939✔
1612
  metaCloseCtbCursor(pCur);
117,931✔
1613
  return TSDB_CODE_SUCCESS;
117,964✔
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,328,634✔
1704
  int32_t code = 0;
7,328,634✔
1705
  void   *pData = NULL;
7,328,634✔
1706
  int     nData = 0;
7,328,634✔
1707
  int     lock = 0;
7,328,634✔
1708

1709
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
7,328,634!
1710
    lock = 1;
2,712,659✔
1711
  }
1712

1713
  if (!lock) metaRLock(pMeta);
7,328,634✔
1714

1715
  // search cache
1716
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
7,332,849✔
1717
    if (!lock) metaULock(pMeta);
7,075,283✔
1718
    goto _exit;
7,075,549✔
1719
  }
1720

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

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

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

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

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

1751
_exit:
11,888✔
1752
  tdbFree(pData);
7,333,707✔
1753
  return code;
7,332,971✔
1754
}
1755

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

1759
  if (!numOfTables && !numOfCols) goto _exit;
565,398!
1760

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

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

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

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

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

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

1804
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
23,800✔
1805
  if (ret) {
23,800!
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,800✔
1811

1812
_exit:
565,423✔
1813
  return code;
565,423✔
1814
}
1815

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

1819
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
182,521✔
1820
    stats.ctbNum += deltaCtb;
161,679✔
1821
    stats.colNum += deltaCol;
161,679✔
1822
    if (deltaKeep > 0) {
161,679✔
1823
      stats.keep = deltaKeep;
9✔
1824
    }
1825

1826
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
161,679✔
1827
    if (code) {
161,659!
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
}
182,532✔
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