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

taosdata / TDengine / #4706

01 Sep 2025 11:36AM UTC coverage: 59.087% (+0.2%) from 58.937%
#4706

push

travis-ci

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

refactor: simplify get started

135606 of 291897 branches covered (46.46%)

Branch coverage included in aggregate %.

204421 of 283568 relevant lines covered (72.09%)

17573328.71 hits per line

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

53.33
/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) {
9,716,014✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
9,716,014✔
22
  metaReaderDoInit(pReader, pMeta, flags);
9,716,014✔
23
  pReader->pAPI = pAPI;
9,721,551✔
24
}
9,721,551✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
14,405,992✔
27
  memset(pReader, 0, sizeof(*pReader));
14,405,992✔
28
  pReader->pMeta = pMeta;
14,405,992✔
29
  pReader->flags = flags;
14,405,992✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
14,405,992!
31
    metaRLock(pMeta);
10,181,964✔
32
  }
33
}
14,407,366✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,889,250✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,889,250!
37
    metaULock(pReader->pMeta);
1,889,649✔
38
    pReader->flags |= META_READER_NOLOCK;
1,890,381✔
39
  }
40
}
1,889,982✔
41

42
void metaReaderClear(SMetaReader *pReader) {
15,539,703✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
15,539,703✔
44
    metaULock(pReader->pMeta);
8,291,392✔
45
  }
46
  tDecoderClear(&pReader->coder);
15,540,746✔
47
  tdbFree(pReader->pBuf);
15,557,242✔
48
  pReader->pBuf = NULL;
15,554,563✔
49
}
15,554,563✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
25,884,336!
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);
25,888,289✔
63
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
25,859,776✔
64

65
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
25,881,746✔
66
  if (code) {
25,684,773!
67
    tDecoderClear(&pReader->coder);
×
68
    return code;
×
69
  }
70
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
71

72
  return 0;
25,684,773✔
73
}
74

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

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

84
  metaULock(pVnodeObj->pMeta);
682,416✔
85
  return true;
682,420✔
86
}
87

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

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

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

101
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
6,447,948✔
102
  SMeta *pMeta = pReader->pMeta;
6,447,948✔
103

104
  SMetaInfo info;
105
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
6,447,948✔
106
  if (TSDB_CODE_SUCCESS != code) {
6,449,769✔
107
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
7!
108
  }
109

110
  return metaGetTableEntryByVersion(pReader, info.version, uid);
6,449,762✔
111
}
112

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

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

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

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

131
  metaRLock(pMeta);
191,206✔
132

133
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
191,491✔
134
    uid = *(tb_uid_t *)pData;
39,223✔
135
    tdbFree(pData);
39,223✔
136
  }
137

138
  metaULock(pMeta);
191,747✔
139

140
  return uid;
191,796✔
141
}
142

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

153
  STR_TO_VARSTR(tbName, mr.me.name);
1,362,804✔
154
  metaReaderClear(&mr);
1,362,804✔
155

156
  return 0;
1,363,107✔
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) {
1,051,714✔
246
  SMTbCursor *pTbCur = NULL;
1,051,714✔
247
  int32_t     code;
248

249
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
1,051,714!
250
  if (pTbCur == NULL) {
1,052,021!
251
    return NULL;
×
252
  }
253

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

267
void metaCloseTbCursor(SMTbCursor *pTbCur) {
2,106,335✔
268
  if (pTbCur) {
2,106,335✔
269
    tdbFree(pTbCur->pKey);
1,053,734✔
270
    tdbFree(pTbCur->pVal);
1,053,643✔
271
    if (!pTbCur->paused) {
1,053,718✔
272
      metaReaderClear(&pTbCur->mr);
118,576✔
273
      if (pTbCur->pDbc) {
118,562!
274
        tdbTbcClose((TBC *)pTbCur->pDbc);
118,566✔
275
      }
276
    }
277
    taosMemoryFree(pTbCur);
1,053,690!
278
  }
279
}
2,106,417✔
280

281
void metaPauseTbCursor(SMTbCursor *pTbCur) {
935,839✔
282
  if (!pTbCur->paused) {
935,839!
283
    metaReaderClear(&pTbCur->mr);
935,958✔
284
    tdbTbcClose((TBC *)pTbCur->pDbc);
936,093✔
285
    pTbCur->paused = 1;
935,981✔
286
  }
287
}
935,862✔
288
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
1,053,410✔
289
  int32_t code = 0;
1,053,410✔
290
  int32_t lino;
291
  int8_t  locked = 0;
1,053,410✔
292
  if (pTbCur->paused) {
1,053,410!
293
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
1,053,534✔
294
    locked = 1;
1,054,618✔
295
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
1,054,618✔
296
    if (code != 0) {
1,053,355!
297
      TSDB_CHECK_CODE(code, lino, _exit);
×
298
    }
299

300
    if (first) {
1,053,355✔
301
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
1,052,433✔
302
      TSDB_CHECK_CODE(code, lino, _exit);
1,052,696!
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;
1,053,618✔
319
  }
320

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

328
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
13,123,967✔
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);
13,123,967✔
335
    if (ret < 0) {
13,238,303✔
336
      return ret;
1,053,501✔
337
    }
338

339
    tDecoderClear(&pTbCur->mr.coder);
12,184,802✔
340

341
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
12,214,420✔
342
    if (ret) return ret;
12,160,655!
343

344
    if (pTbCur->mr.me.type == jumpTableType) {
12,160,655✔
345
      continue;
2,512,033✔
346
    }
347

348
    break;
9,648,622✔
349
  }
350

351
  return 0;
9,648,622✔
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) {
4,069,916✔
383
  void           *pData = NULL;
4,069,916✔
384
  int             nData = 0;
4,069,916✔
385
  int64_t         version;
386
  SSchemaWrapper  schema = {0};
4,069,916✔
387
  SSchemaWrapper *pSchema = NULL;
4,069,916✔
388
  SDecoder        dc = {0};
4,069,916✔
389
  if (lock) {
4,069,916✔
390
    metaRLock(pMeta);
4,051,197✔
391
  }
392
_query:
4,080,166✔
393
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
4,178,729✔
394
    goto _err;
364✔
395
  }
396

397
  version = ((SUidIdxVal *)pData)[0].version;
4,175,339✔
398

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

403
  SMetaEntry me = {0};
4,176,679✔
404
  tDecoderInit(&dc, pData, nData);
4,176,679✔
405
  int32_t code = metaDecodeEntry(&dc, &me);
4,173,150✔
406
  if (code) {
4,175,611!
407
    tDecoderClear(&dc);
×
408
    goto _err;
×
409
  }
410
  if (me.type == TSDB_SUPER_TABLE) {
4,175,611✔
411
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
3,753,644✔
412
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
3,752,267✔
413
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
3,752,267✔
414
      tDecoderClear(&dc);
3,752,263✔
415
      goto _exit;
3,754,626✔
416
    }
417
  } else if (me.type == TSDB_CHILD_TABLE) {
421,967✔
418
    uid = me.ctbEntry.suid;
98,489✔
419
    tDecoderClear(&dc);
98,489✔
420
    goto _query;
98,563✔
421
  } else {
422
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
323,478✔
423
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
323,557✔
424
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
323,557✔
425
      tDecoderClear(&dc);
323,529✔
426
      goto _exit;
323,573✔
427
    }
428
  }
429
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
2,526✔
430
  tDecoderClear(&dc);
2,526✔
431

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

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

444
_exit:
4,078,333✔
445
  if (lock) {
4,078,333✔
446
    metaULock(pMeta);
4,047,656✔
447
  }
448
  tdbFree(pData);
4,085,295✔
449
  return pSchema;
4,079,184✔
450

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

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

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

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

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

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

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

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

506
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,469,336!
507
  if (pCtbCur == NULL) {
3,476,370!
508
    return NULL;
×
509
  }
510

511
  pCtbCur->pMeta = pMeta;
3,476,370✔
512
  pCtbCur->suid = uid;
3,476,370✔
513
  pCtbCur->lock = lock;
3,476,370✔
514
  pCtbCur->paused = 1;
3,476,370✔
515

516
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,476,370✔
517
  if (ret < 0) {
3,476,038!
518
    return NULL;
×
519
  }
520
  return pCtbCur;
3,476,038✔
521
}
522

523
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,476,323✔
524
  if (pCtbCur) {
3,476,323!
525
    if (!pCtbCur->paused) {
3,476,541✔
526
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,446,080!
527
      if (pCtbCur->pCur) {
3,447,435!
528
        tdbTbcClose(pCtbCur->pCur);
3,447,478✔
529
      }
530
    }
531
    tdbFree(pCtbCur->pKey);
3,476,004✔
532
    tdbFree(pCtbCur->pVal);
3,476,162✔
533
  }
534
  taosMemoryFree(pCtbCur);
3,476,610!
535
}
3,477,335✔
536

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

547
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,472,075✔
548
  if (pCtbCur->paused) {
3,472,075!
549
    pCtbCur->paused = 0;
3,473,980✔
550

551
    if (pCtbCur->lock) {
3,473,980✔
552
      metaRLock(pCtbCur->pMeta);
3,450,940✔
553
    }
554
    int ret = 0;
3,474,954✔
555
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,474,954✔
556
    if (ret < 0) {
3,475,145!
557
      metaCloseCtbCursor(pCtbCur);
×
558
      return -1;
×
559
    }
560

561
    if (first) {
3,475,178!
562
      SCtbIdxKey ctbIdxKey;
563
      // move to the suid
564
      ctbIdxKey.suid = pCtbCur->suid;
3,475,178✔
565
      ctbIdxKey.uid = INT64_MIN;
3,475,178✔
566
      int c = 0;
3,475,178✔
567
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,475,178✔
568
      if (c > 0) {
3,476,490✔
569
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
678,024✔
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;
3,474,347✔
582
}
583

584
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
14,836,576✔
585
  int         ret;
586
  SCtbIdxKey *pCtbIdxKey;
587

588
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
14,836,576✔
589
  if (ret < 0) {
14,843,820✔
590
    return 0;
1,578,563✔
591
  }
592

593
  pCtbIdxKey = pCtbCur->pKey;
13,265,257✔
594
  if (pCtbIdxKey->suid > pCtbCur->suid) {
13,265,257✔
595
    return 0;
1,899,264✔
596
  }
597

598
  return pCtbIdxKey->uid;
11,365,993✔
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) {
217,278✔
612
  SMStbCursor *pStbCur = NULL;
217,278✔
613
  int          ret = 0;
217,278✔
614
  int          c = 0;
217,278✔
615

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

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

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

640
  return pStbCur;
217,276✔
641
}
642

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

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

653
    taosMemoryFree(pStbCur);
217,280!
654
  }
655
}
217,280✔
656

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

660
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
685,271✔
661
  if (ret < 0) {
685,264✔
662
    return 0;
217,279✔
663
  }
664
  return *(tb_uid_t *)pStbCur->pKey;
467,985✔
665
}
666

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

671
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
3,913,699✔
672
  if (!pSW) return NULL;
3,921,714✔
673

674
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
3,921,616✔
675

676
  taosMemoryFree(pSW->pSchema);
3,923,905!
677
  taosMemoryFree(pSW);
3,921,511✔
678
  return pTSchema;
3,922,387✔
679
}
680

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

689
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
3,899,131✔
690
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
3,899,131✔
691
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
3,907,336!
692
    return terrno;
5,107✔
693
  }
694
  return TSDB_CODE_SUCCESS;
3,902,229✔
695
}
696

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

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

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

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

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

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

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

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

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

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

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

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

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

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

776
  tDecoderInit(&dc, pData, nData);
206,190✔
777
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
205,727✔
778
  tDecoderClear(&dc);
205,727✔
779
  tdbFree(pData);
206,084✔
780
  if (TSDB_CODE_SUCCESS != code) {
206,160!
781
    taosMemoryFree(pSchemaWrapper->pSchema);
×
782
    goto _exit;
×
783
  }
784

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

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

794
_exit:
206,202✔
795
  return code;
206,202✔
796
}
797

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

808
void metaUpdTimeSeriesNum(SMeta *pMeta) {
217,285✔
809
  int64_t nCtbTimeSeries = 0;
217,285✔
810
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
217,285!
811
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
217,283✔
812
  }
813
}
217,286✔
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;
1,021,045✔
818
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,021,089✔
819
    metaUpdTimeSeriesNum(pMeta);
217,241✔
820
  }
821

822
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,021,108✔
823
}
824

825
// type: 1 reported timeseries
826
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,021,045!
827
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,021,108✔
828
  if (type == 1) {
1,021,108✔
829
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
815,506✔
830
  }
831
  return nTimeSeries;
1,021,118✔
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) {
19,154,097✔
1091
  STag *tag = (STag *)pTag;
19,154,097✔
1092
  if (type == TSDB_DATA_TYPE_JSON) {
19,154,097✔
1093
    return tag;
10,416✔
1094
  }
1095
  bool find = tTagGet(tag, val);
19,143,681✔
1096

1097
  if (!find) {
19,201,862✔
1098
    return NULL;
44,641✔
1099
  }
1100

1101
  return val;
19,157,221✔
1102
}
1103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1228
    if (count > TRY_ERROR_LIMIT) break;
×
1229

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

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

1261
  taosMemoryFree(pCursor);
×
1262

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

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

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

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

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

1293
  taosMemoryFree(pCursor);
×
1294

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

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

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

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

1324
  metaRLock(pMeta);
11,457✔
1325

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

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

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

1333
  tDecoderInit(&dc, pData, nData);
11,460✔
1334

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

1341
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
11,462!
1342
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
2!
1343
  }
1344

1345
  code = TSDB_CODE_INVALID_PARA;
11,462✔
1346

1347
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
57,177✔
1348
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
57,175✔
1349
    if (IS_IDX_ON(schema)) {
57,175✔
1350
      if (schema->colId == param->cid && param->type == schema->type) {
14,161!
1351
        code = 0;
11,460✔
1352
        break;
11,460✔
1353
      }
1354
    }
1355
  }
1356

1357
  TAOS_CHECK_GOTO(code, NULL, END);
11,462!
1358

1359
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
11,462✔
1360
  if (code != 0) {
11,460✔
1361
    TAOS_CHECK_GOTO(terrno, NULL, END);
8!
1362
  }
1363

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

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

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

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

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

1403
  int     count = 0;
11,457✔
1404
  int32_t valid = 0;
11,457✔
1405
  bool    found = false;
11,457✔
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;
11,457✔
1412
  do {
89,176✔
1413
    void   *entryKey = NULL, *entryVal = NULL;
100,633✔
1414
    int32_t nEntryKey, nEntryVal;
1415

1416
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
100,633✔
1417
    if (valid < 0) {
100,323✔
1418
      break;
11,461✔
1419
    }
1420
    if (count > TRY_ERROR_LIMIT) {
92,159✔
1421
      break;
1,517✔
1422
    }
1423

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

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

1441
    terrno = TSDB_CODE_SUCCESS;
88,580✔
1442
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
88,581✔
1443
    if (terrno != TSDB_CODE_SUCCESS) {
88,566!
1444
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1445
      break;
×
1446
    }
1447
    if (cmp == 0) {
88,608✔
1448
      // match
1449
      tb_uid_t tuid = 0;
77,323✔
1450
      if (IS_VAR_DATA_TYPE(pKey->type)) {
77,323!
1451
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
425✔
1452
      } else {
1453
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
76,898✔
1454
      }
1455
      if (taosArrayPush(pUids, &tuid) == NULL) {
77,593!
1456
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1457
      }
1458
      found = true;
77,593✔
1459
    } else {
1460
      if (param->equal == true) {
11,285✔
1461
        if (count > TRY_ERROR_LIMIT) break;
8,396!
1462
        count++;
8,396✔
1463
      }
1464
    }
1465
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
88,878✔
1466
    if (valid < 0) {
88,859!
1467
      code = valid;
×
1468
      break;
×
1469
    }
1470
  } while (1);
1471

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

1479
  taosMemoryFree(buf);
11,468!
1480
  taosMemoryFree(pKey);
11,467✔
1481

1482
  taosMemoryFree(pCursor);
11,460!
1483

1484
  return code;
11,470✔
1485
}
1486

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

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

1499
  return ret;
62,700✔
1500
}
1501

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

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

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

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

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

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

1539
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
1,220,320✔
1540
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
1,220,320✔
1541
  if (!pCur) {
1,222,486!
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;
1,222,486✔
1549
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
1,222,486✔
1550
  if (numOfElems > 0) {
1,221,735✔
1551
    pSepecifiedUidMap =
1552
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
92,447✔
1553
    for (int i = 0; i < numOfElems; i++) {
383,872✔
1554
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
291,407✔
1555
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
291,164✔
1556
      if (code) {
291,502!
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
1,221,753✔
1565
    while (1) {
3,465,263✔
1566
      tb_uid_t uid = metaCtbCursorNext(pCur);
4,594,441✔
1567
      if (uid == 0) {
4,589,728✔
1568
        break;
1,130,349✔
1569
      }
1570

1571
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
3,459,379✔
1572
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
3,459,379!
1573
      if (!info.pTagVal) {
3,469,164!
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
3,469,164✔
1579
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
3,465,263!
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) {
410,857✔
1588
      tb_uid_t uid = metaCtbCursorNext(pCur);
503,432✔
1589
      if (uid == 0) {
503,207✔
1590
        break;
92,450✔
1591
      }
1592

1593
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
410,757✔
1594
      if (index == NULL) {
410,786✔
1595
        continue;
119,747✔
1596
      }
1597

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

1611
  taosHashCleanup(pSepecifiedUidMap);
1,222,799✔
1612
  metaCloseCtbCursor(pCur);
1,222,481✔
1613
  return TSDB_CODE_SUCCESS;
1,222,788✔
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) {
31,958,889✔
1704
  int32_t code = 0;
31,958,889✔
1705
  void   *pData = NULL;
31,958,889✔
1706
  int     nData = 0;
31,958,889✔
1707
  int     lock = 0;
31,958,889✔
1708

1709
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
31,958,889!
1710
    lock = 1;
6,449,917✔
1711
  }
1712

1713
  if (!lock) metaRLock(pMeta);
31,958,889✔
1714

1715
  // search cache
1716
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
31,964,119✔
1717
    if (!lock) metaULock(pMeta);
31,665,469✔
1718
    goto _exit;
31,665,864✔
1719
  }
1720

1721
  // search TDB
1722
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
301,637✔
1723
    // not found
1724
    if (!lock) metaULock(pMeta);
280,199!
1725
    goto _exit;
280,232✔
1726
  }
1727

1728
  if (!lock) metaULock(pMeta);
21,143✔
1729

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

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

1747
  if (lock) {
21,462✔
1748
    metaRLock(pReader->pMeta);
3,358✔
1749
  }
1750

1751
_exit:
18,104✔
1752
  tdbFree(pData);
31,967,556✔
1753
  return code;
31,966,960✔
1754
}
1755

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

1759
  if (!numOfTables && !numOfCols) goto _exit;
674,590!
1760

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

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

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

1780
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
25,501✔
1781
  if (TSDB_CODE_SUCCESS == code) {
25,503!
1782
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
25,504✔
1783
  }
1784
  if (TSDB_CODE_SUCCESS == code) {
25,498!
1785
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
25,499✔
1786
  }
1787
  metaULock(pVnodeObj->pMeta);
25,502✔
1788
  if (TSDB_CODE_SUCCESS != code) {
25,505!
1789
    goto _exit;
×
1790
  }
1791

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

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

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

1812
_exit:
674,615✔
1813
  return code;
674,615✔
1814
}
1815

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

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

1826
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
192,069✔
1827
    if (code) {
192,074!
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
}
220,837✔
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