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

taosdata / TDengine / #4712

06 Sep 2025 04:27PM UTC coverage: 58.144% (-1.0%) from 59.134%
#4712

push

travis-ci

GitHub
test: update case description (#32878)

133123 of 291691 branches covered (45.64%)

Branch coverage included in aggregate %.

201244 of 283375 relevant lines covered (71.02%)

5637899.03 hits per line

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

53.48
/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,743,582✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,743,582✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,743,582✔
23
  pReader->pAPI = pAPI;
2,743,868✔
24
}
2,743,868✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
3,132,735✔
27
  memset(pReader, 0, sizeof(*pReader));
3,132,735✔
28
  pReader->pMeta = pMeta;
3,132,735✔
29
  pReader->flags = flags;
3,132,735✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
3,132,735!
31
    metaRLock(pMeta);
2,704,393✔
32
  }
33
}
3,132,884✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,141,831✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,141,831!
37
    metaULock(pReader->pMeta);
1,142,181✔
38
    pReader->flags |= META_READER_NOLOCK;
1,142,859✔
39
  }
40
}
1,142,509✔
41

42
void metaReaderClear(SMetaReader *pReader) {
3,174,788✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
3,174,788✔
44
    metaULock(pReader->pMeta);
1,560,862✔
45
  }
46
  tDecoderClear(&pReader->coder);
3,175,401✔
47
  tdbFree(pReader->pBuf);
3,176,063✔
48
  pReader->pBuf = NULL;
3,175,824✔
49
}
3,175,824✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
4,011,035!
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);
4,012,096✔
63
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
4,011,488✔
64

65
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
4,009,636✔
66
  if (code) {
4,007,440!
67
    tDecoderClear(&pReader->coder);
×
68
    return code;
×
69
  }
70
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
71

72
  return 0;
4,007,440✔
73
}
74

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

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

84
  metaULock(pVnodeObj->pMeta);
495,977✔
85
  return true;
495,988✔
86
}
87

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

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

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

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

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

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

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

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

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

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

131
  metaRLock(pMeta);
163,960✔
132

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

138
  metaULock(pMeta);
164,061✔
139

140
  return uid;
164,071✔
141
}
142

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

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

156
  return 0;
56,085✔
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,353✔
246
  SMTbCursor *pTbCur = NULL;
20,353✔
247
  int32_t     code;
248

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

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

267
void metaCloseTbCursor(SMTbCursor *pTbCur) {
40,632✔
268
  if (pTbCur) {
40,632✔
269
    tdbFree(pTbCur->pKey);
20,352✔
270
    tdbFree(pTbCur->pVal);
20,352✔
271
    if (!pTbCur->paused) {
20,355✔
272
      metaReaderClear(&pTbCur->mr);
1,077✔
273
      if (pTbCur->pDbc) {
1,077!
274
        tdbTbcClose((TBC *)pTbCur->pDbc);
1,077✔
275
      }
276
    }
277
    taosMemoryFree(pTbCur);
20,354✔
278
  }
279
}
40,629✔
280

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

300
    if (first) {
21,273✔
301
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
20,351✔
302
      TSDB_CHECK_CODE(code, lino, _exit);
20,350!
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;
21,272✔
319
  }
320

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

328
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
652,898✔
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);
652,898✔
335
    if (ret < 0) {
651,338✔
336
      return ret;
20,288✔
337
    }
338

339
    tDecoderClear(&pTbCur->mr.coder);
631,050✔
340

341
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
631,691✔
342
    if (ret) return ret;
628,625!
343

344
    if (pTbCur->mr.me.type == jumpTableType) {
628,625✔
345
      continue;
10,714✔
346
    }
347

348
    break;
617,911✔
349
  }
350

351
  return 0;
617,911✔
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,347,847✔
383
  void           *pData = NULL;
1,347,847✔
384
  int             nData = 0;
1,347,847✔
385
  int64_t         version;
386
  SSchemaWrapper  schema = {0};
1,347,847✔
387
  SSchemaWrapper *pSchema = NULL;
1,347,847✔
388
  SDecoder        dc = {0};
1,347,847✔
389
  if (lock) {
1,347,847✔
390
    metaRLock(pMeta);
1,323,906✔
391
  }
392
_query:
1,348,385✔
393
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,457,616✔
394
    goto _err;
383✔
395
  }
396

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

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

403
  SMetaEntry me = {0};
1,457,164✔
404
  tDecoderInit(&dc, pData, nData);
1,457,164✔
405
  int32_t code = metaDecodeEntry(&dc, &me);
1,456,945✔
406
  if (code) {
1,456,855!
407
    tDecoderClear(&dc);
×
408
    goto _err;
×
409
  }
410
  if (me.type == TSDB_SUPER_TABLE) {
1,456,855✔
411
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
1,101,506✔
412
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
1,101,071✔
413
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
1,101,071✔
414
      tDecoderClear(&dc);
1,101,111✔
415
      goto _exit;
1,101,331✔
416
    }
417
  } else if (me.type == TSDB_CHILD_TABLE) {
355,349✔
418
    uid = me.ctbEntry.suid;
109,120✔
419
    tDecoderClear(&dc);
109,120✔
420
    goto _query;
109,231✔
421
  } else {
422
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
246,229✔
423
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
246,286✔
424
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
246,286✔
425
      tDecoderClear(&dc);
246,243✔
426
      goto _exit;
246,285✔
427
    }
428
  }
429
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
507✔
430
  tDecoderClear(&dc);
507✔
431

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

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

444
_exit:
1,347,848✔
445
  if (lock) {
1,347,848✔
446
    metaULock(pMeta);
1,322,878✔
447
  }
448
  tdbFree(pData);
1,348,744✔
449
  return pSchema;
1,348,067✔
450

451
_err:
383✔
452
  if (lock) {
383✔
453
    metaULock(pMeta);
382✔
454
  }
455
  tdbFree(pData);
383✔
456
  return NULL;
383✔
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) {
741,799✔
500
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
741,799✔
501
  SMCtbCursor *pCtbCur = NULL;
741,799✔
502
  SCtbIdxKey   ctbIdxKey;
503
  int          ret = 0;
741,799✔
504
  int          c = 0;
741,799✔
505

506
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
741,799!
507
  if (pCtbCur == NULL) {
742,724!
508
    return NULL;
×
509
  }
510

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

516
  ret = metaResumeCtbCursor(pCtbCur, 1);
742,724✔
517
  if (ret < 0) {
741,946!
518
    return NULL;
×
519
  }
520
  return pCtbCur;
741,946✔
521
}
522

523
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
742,184✔
524
  if (pCtbCur) {
742,184!
525
    if (!pCtbCur->paused) {
742,277✔
526
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
710,692!
527
      if (pCtbCur->pCur) {
711,059!
528
        tdbTbcClose(pCtbCur->pCur);
711,089✔
529
      }
530
    }
531
    tdbFree(pCtbCur->pKey);
742,411✔
532
    tdbFree(pCtbCur->pVal);
742,481✔
533
  }
534
  taosMemoryFree(pCtbCur);
742,522!
535
}
742,713✔
536

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

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

551
    if (pCtbCur->lock) {
742,315✔
552
      metaRLock(pCtbCur->pMeta);
718,376✔
553
    }
554
    int ret = 0;
742,406✔
555
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
742,406✔
556
    if (ret < 0) {
742,340!
557
      metaCloseCtbCursor(pCtbCur);
×
558
      return -1;
×
559
    }
560

561
    if (first) {
742,455!
562
      SCtbIdxKey ctbIdxKey;
563
      // move to the suid
564
      ctbIdxKey.suid = pCtbCur->suid;
742,455✔
565
      ctbIdxKey.uid = INT64_MIN;
742,455✔
566
      int c = 0;
742,455✔
567
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
742,455✔
568
      if (c > 0) {
742,015✔
569
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
55,656✔
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;
741,807✔
582
}
583

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

588
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
3,930,206✔
589
  if (ret < 0) {
3,931,963✔
590
    return 0;
343,847✔
591
  }
592

593
  pCtbIdxKey = pCtbCur->pKey;
3,588,116✔
594
  if (pCtbIdxKey->suid > pCtbCur->suid) {
3,588,116✔
595
    return 0;
399,165✔
596
  }
597

598
  return pCtbIdxKey->uid;
3,188,951✔
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) {
139,408✔
612
  SMStbCursor *pStbCur = NULL;
139,408✔
613
  int          ret = 0;
139,408✔
614
  int          c = 0;
139,408✔
615

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

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

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

640
  return pStbCur;
139,408✔
641
}
642

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

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

653
    taosMemoryFree(pStbCur);
139,410!
654
  }
655
}
139,410✔
656

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

660
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
587,499✔
661
  if (ret < 0) {
587,502✔
662
    return 0;
139,410✔
663
  }
664
  return *(tb_uid_t *)pStbCur->pKey;
448,092✔
665
}
666

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

671
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
1,190,070✔
672
  if (!pSW) return NULL;
1,190,333✔
673

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

676
  taosMemoryFree(pSW->pSchema);
1,190,421!
677
  taosMemoryFree(pSW);
1,190,496✔
678
  return pTSchema;
1,190,459✔
679
}
680

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

689
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,169,379✔
690
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,169,379✔
691
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
1,169,785!
692
    return terrno;
630✔
693
  }
694
  return TSDB_CODE_SUCCESS;
1,169,155✔
695
}
696

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

701
  void     *pData = NULL;
167,841✔
702
  int       nData = 0;
167,841✔
703
  SSkmDbKey skmDbKey;
704
  if (sver <= 0) {
167,841✔
705
    SMetaInfo info;
706
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
49,787✔
707
      sver = info.skmVer;
49,689✔
708
    } else {
709
      TBC *pSkmDbC = NULL;
108✔
710
      int  c;
711

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

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

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

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

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

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

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

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

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

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

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

776
  tDecoderInit(&dc, pData, nData);
167,866✔
777
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
167,353✔
778
  tDecoderClear(&dc);
167,353✔
779
  tdbFree(pData);
167,733✔
780
  if (TSDB_CODE_SUCCESS != code) {
167,804!
781
    taosMemoryFree(pSchemaWrapper->pSchema);
×
782
    goto _exit;
×
783
  }
784

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

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

794
_exit:
167,885✔
795
  return code;
167,885✔
796
}
797

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

808
void metaUpdTimeSeriesNum(SMeta *pMeta) {
139,418✔
809
  int64_t nCtbTimeSeries = 0;
139,418✔
810
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
139,418!
811
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
139,419✔
812
  }
813
}
139,419✔
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;
442,639✔
818
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
442,661✔
819
    metaUpdTimeSeriesNum(pMeta);
139,396✔
820
  }
821

822
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
442,675✔
823
}
824

825
// type: 1 reported timeseries
826
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
442,639!
827
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
442,675✔
828
  if (type == 1) {
442,675✔
829
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
265,914✔
830
  }
831
  return nTimeSeries;
442,687✔
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,242,590✔
1091
  STag *tag = (STag *)pTag;
4,242,590✔
1092
  if (type == TSDB_DATA_TYPE_JSON) {
4,242,590✔
1093
    return tag;
10,414✔
1094
  }
1095
  bool find = tTagGet(tag, val);
4,232,176✔
1096

1097
  if (!find) {
4,234,423✔
1098
    return NULL;
45,476✔
1099
  }
1100

1101
  return val;
4,188,947✔
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) {
5✔
1117
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
5✔
1118
  SMetaFltParam *param = arg;
5✔
1119
  int32_t        ret = 0;
5✔
1120

1121
  SIdxCursor *pCursor = NULL;
5✔
1122
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
5!
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 {
29,545✔
1151
    void   *entryKey = NULL;
29,551✔
1152
    int32_t nEntryKey = -1;
29,551✔
1153
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
29,551✔
1154
    if (valid < 0) break;
29,365✔
1155

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

1159
    terrno = TSDB_CODE_SUCCESS;
29,359✔
1160
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
29,406✔
1161
    if (terrno != TSDB_CODE_SUCCESS) {
29,122!
1162
      ret = terrno;
×
1163
      break;
×
1164
    }
1165
    if (cmp == 0) {
29,244!
1166
      if (taosArrayPush(pUids, &p->uid) == NULL) {
58,416!
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);
29,172✔
1177
    if (valid < 0) break;
29,545!
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,737✔
1300
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6,737✔
1301
  SMetaFltParam *param = arg;
6,737✔
1302

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

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

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

1324
  metaRLock(pMeta);
6,761✔
1325

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

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

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

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

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

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

1345
  code = TSDB_CODE_INVALID_PARA;
6,763✔
1346

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

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

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

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

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

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

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

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

1403
  int     count = 0;
6,754✔
1404
  int32_t valid = 0;
6,754✔
1405
  bool    found = false;
6,754✔
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,754✔
1412
  do {
77,288✔
1413
    void   *entryKey = NULL, *entryVal = NULL;
84,042✔
1414
    int32_t nEntryKey, nEntryVal;
1415

1416
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
84,042✔
1417
    if (valid < 0) {
83,657✔
1418
      break;
6,767✔
1419
    }
1420
    if (count > TRY_ERROR_LIMIT) {
80,031✔
1421
      break;
1,527✔
1422
    }
1423

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

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

1441
    terrno = TSDB_CODE_SUCCESS;
76,560✔
1442
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
76,574✔
1443
    if (terrno != TSDB_CODE_SUCCESS) {
76,516!
1444
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1445
      break;
×
1446
    }
1447
    if (cmp == 0) {
76,557✔
1448
      // match
1449
      tb_uid_t tuid = 0;
69,549✔
1450
      if (IS_VAR_DATA_TYPE(pKey->type)) {
69,549!
1451
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
592✔
1452
      } else {
1453
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
68,957✔
1454
      }
1455
      if (taosArrayPush(pUids, &tuid) == NULL) {
69,896!
1456
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1457
      }
1458
      found = true;
69,896✔
1459
    } else {
1460
      if (param->equal == true) {
7,008✔
1461
        if (count > TRY_ERROR_LIMIT) break;
4,109!
1462
        count++;
4,109✔
1463
      }
1464
    }
1465
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
76,904✔
1466
    if (valid < 0) {
76,910!
1467
      code = valid;
×
1468
      break;
×
1469
    }
1470
  } while (1);
1471

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

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

1482
  taosMemoryFree(pCursor);
6,763!
1483

1484
  return code;
6,765✔
1485
}
1486

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

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

1499
  return ret;
62,755✔
1500
}
1501

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

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

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

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

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

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

1539
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
119,088✔
1540
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
119,088✔
1541
  if (!pCur) {
119,161!
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;
119,161✔
1549
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
119,161✔
1550
  if (numOfElems > 0) {
119,136✔
1551
    pSepecifiedUidMap =
1552
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
40,433✔
1553
    for (int i = 0; i < numOfElems; i++) {
234,360✔
1554
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
193,911✔
1555
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
193,905✔
1556
      if (code) {
193,940!
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
119,152✔
1565
    while (1) {
410,351✔
1566
      tb_uid_t uid = metaCtbCursorNext(pCur);
489,042✔
1567
      if (uid == 0) {
484,750✔
1568
        break;
78,752✔
1569
      }
1570

1571
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
405,998✔
1572
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
405,998✔
1573
      if (!info.pTagVal) {
412,281!
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
412,281✔
1579
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
410,351!
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) {
242,241✔
1588
      tb_uid_t uid = metaCtbCursorNext(pCur);
282,702✔
1589
      if (uid == 0) {
282,617✔
1590
        break;
40,440✔
1591
      }
1592

1593
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
242,177✔
1594
      if (index == NULL) {
242,100✔
1595
        continue;
48,560✔
1596
      }
1597

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

1611
  taosHashCleanup(pSepecifiedUidMap);
119,192✔
1612
  metaCloseCtbCursor(pCur);
119,188✔
1613
  return TSDB_CODE_SUCCESS;
119,211✔
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,361,561✔
1704
  int32_t code = 0;
7,361,561✔
1705
  void   *pData = NULL;
7,361,561✔
1706
  int     nData = 0;
7,361,561✔
1707
  int     lock = 0;
7,361,561✔
1708

1709
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
7,361,561!
1710
    lock = 1;
2,735,785✔
1711
  }
1712

1713
  if (!lock) metaRLock(pMeta);
7,361,561✔
1714

1715
  // search cache
1716
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
7,362,162✔
1717
    if (!lock) metaULock(pMeta);
7,102,719✔
1718
    goto _exit;
7,102,828✔
1719
  }
1720

1721
  // search TDB
1722
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
260,319✔
1723
    // not found
1724
    if (!lock) metaULock(pMeta);
244,795✔
1725
    goto _exit;
244,797✔
1726
  }
1727

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

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

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

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

1751
_exit:
12,229✔
1752
  tdbFree(pData);
7,363,177✔
1753
  return code;
7,362,928✔
1754
}
1755

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

1759
  if (!numOfTables && !numOfCols) goto _exit;
625,515!
1760

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

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

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

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

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

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

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

1812
_exit:
625,546✔
1813
  return code;
625,546✔
1814
}
1815

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

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

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