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

taosdata / TDengine / #3552

11 Dec 2024 06:08AM UTC coverage: 62.526% (+0.7%) from 61.798%
#3552

push

travis-ci

web-flow
Merge pull request #29092 from taosdata/fix/3.0/TD-33146

fix:[TD-33146] stmt_get_tag_fields return error code

124833 of 255773 branches covered (48.81%)

Branch coverage included in aggregate %.

209830 of 279467 relevant lines covered (75.08%)

19111707.6 hits per line

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

56.7
/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) {
12,771,456✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
12,771,456✔
22
  metaReaderDoInit(pReader, pMeta, flags);
12,771,456✔
23
  pReader->pAPI = pAPI;
12,787,850✔
24
}
12,787,850✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
16,985,217✔
27
  memset(pReader, 0, sizeof(*pReader));
16,985,217✔
28
  pReader->pMeta = pMeta;
16,985,217✔
29
  pReader->flags = flags;
16,985,217✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
16,985,217!
31
    metaRLock(pMeta);
10,856,759✔
32
  }
33
}
16,985,794✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,153,196✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,153,196!
37
    metaULock(pReader->pMeta);
2,153,621✔
38
    pReader->flags |= META_READER_NOLOCK;
2,154,136✔
39
  }
40
}
2,153,711✔
41

42
void metaReaderClear(SMetaReader *pReader) {
17,667,065✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
17,667,065✔
44
    metaULock(pReader->pMeta);
8,702,876✔
45
  }
46
  tDecoderClear(&pReader->coder);
17,665,462✔
47
  tdbFree(pReader->pBuf);
17,672,903✔
48
  pReader->pBuf = NULL;
17,683,288✔
49
}
17,683,288✔
50

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

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

61
  // decode the entry
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
28,831,875✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
28,812,272✔
65
  if (code) {
28,758,709!
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
28,758,709✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
801,843✔
79
    metaULock(pVnodeObj->pMeta);
1,727✔
80
    return false;
1,727✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
800,120✔
84
  return true;
800,125✔
85
}
86

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

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

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

100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
7,766,847✔
101
  SMeta *pMeta = pReader->pMeta;
7,766,847✔
102

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
7,766,847✔
105
  if (TSDB_CODE_SUCCESS != code) {
7,769,195✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
34!
107
  }
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
7,769,161✔
110
}
111

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

116
  // query name.idx
117
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
1,941,277✔
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
207,319✔
119
  }
120

121
  uid = *(tb_uid_t *)pReader->pBuf;
1,734,782✔
122
  return metaReaderGetTableEntryByUid(pReader, uid);
1,734,782✔
123
}
124

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

130
  metaRLock(pMeta);
379,953✔
131

132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
380,019✔
133
    uid = *(tb_uid_t *)pData;
231,197✔
134
    tdbFree(pData);
231,197✔
135
  }
136

137
  metaULock(pMeta);
379,914✔
138

139
  return uid;
379,965✔
140
}
141

142
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
18,190✔
143
  int         code = 0;
18,190✔
144
  SMetaReader mr = {0};
18,190✔
145
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
18,190✔
146
  code = metaReaderGetTableEntryByUid(&mr, uid);
18,104✔
147
  if (code < 0) {
18,032✔
148
    metaReaderClear(&mr);
133✔
149
    return code;
133✔
150
  }
151

152
  STR_TO_VARSTR(tbName, mr.me.name);
17,899✔
153
  metaReaderClear(&mr);
17,899✔
154

155
  return 0;
18,041✔
156
}
157

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

170
  return 0;
×
171
}
172

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

178
  SMetaReader *pReader = &mr;
414,510✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
411,894✔
187

188
  metaReaderClear(&mr);
411,894✔
189

190
  return 0;
411,894✔
191
}
192

193
int metaGetTableTypeByName(void *pVnode, char *tbName, ETableType *tbType) {
411,893✔
194
  int         code = 0;
411,893✔
195
  SMetaReader mr = {0};
411,893✔
196
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
411,893✔
197

198
  code = metaGetTableEntryByName(&mr, tbName);
411,894✔
199
  if (code == 0) *tbType = mr.me.type;
411,889!
200

201
  metaReaderClear(&mr);
411,889✔
202
  return code;
411,895✔
203
}
204

205
int metaReadNext(SMetaReader *pReader) {
×
206
  SMeta *pMeta = pReader->pMeta;
×
207

208
  // TODO
209

210
  return 0;
×
211
}
212

213
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
214
  int         code = -1;
×
215
  SMetaReader mr = {0};
×
216
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
217
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
218
  if (code < 0) {
×
219
    goto _exit;
×
220
  }
221
  if (mr.me.type == TSDB_CHILD_TABLE) {
×
222
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
223
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
×
224
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
225
  } else {
226
    goto _exit;
×
227
  }
228

229
  code = 0;
×
230

231
_exit:
×
232
  metaReaderClear(&mr);
×
233
  return code;
×
234
}
235

236
#if 1  // ===================================================
237
SMTbCursor *metaOpenTbCursor(void *pVnode) {
1,133,499✔
238
  SMTbCursor *pTbCur = NULL;
1,133,499✔
239
  int32_t     code;
240

241
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
1,133,499✔
242
  if (pTbCur == NULL) {
1,135,232!
243
    return NULL;
×
244
  }
245

246
  SVnode *pVnodeObj = pVnode;
1,135,232✔
247
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
248
  pTbCur->pMeta = pVnodeObj->pMeta;
1,135,232✔
249
  pTbCur->paused = 1;
1,135,232✔
250
  code = metaResumeTbCursor(pTbCur, 1, 0);
1,135,232✔
251
  if (code) {
1,135,165!
252
    terrno = code;
×
253
    taosMemoryFree(pTbCur);
×
254
    return NULL;
×
255
  }
256
  return pTbCur;
1,135,465✔
257
}
258

259
void metaCloseTbCursor(SMTbCursor *pTbCur) {
2,274,939✔
260
  if (pTbCur) {
2,274,939✔
261
    tdbFree(pTbCur->pKey);
1,139,357✔
262
    tdbFree(pTbCur->pVal);
1,139,302✔
263
    if (!pTbCur->paused) {
1,139,358✔
264
      metaReaderClear(&pTbCur->mr);
100,022✔
265
      if (pTbCur->pDbc) {
100,012!
266
        tdbTbcClose((TBC *)pTbCur->pDbc);
100,014✔
267
      }
268
    }
269
    taosMemoryFree(pTbCur);
1,139,350✔
270
  }
271
}
2,274,997✔
272

273
void metaPauseTbCursor(SMTbCursor *pTbCur) {
1,039,874✔
274
  if (!pTbCur->paused) {
1,039,874!
275
    metaReaderClear(&pTbCur->mr);
1,039,945✔
276
    tdbTbcClose((TBC *)pTbCur->pDbc);
1,040,285✔
277
    pTbCur->paused = 1;
1,040,057✔
278
  }
279
}
1,039,986✔
280
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
1,137,228✔
281
  int32_t code = 0;
1,137,228✔
282
  int32_t lino;
283
  int8_t  locked = 0;
1,137,228✔
284
  if (pTbCur->paused) {
1,137,228!
285
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
1,137,585✔
286
    locked = 1;
1,139,325✔
287
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
1,139,325✔
288
    if (code != 0) {
1,137,766!
289
      TSDB_CHECK_CODE(code, lino, _exit);
×
290
    }
291

292
    if (first) {
1,137,766✔
293
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
1,136,844✔
294
      TSDB_CHECK_CODE(code, lino, _exit);
1,135,403!
295
    } else {
296
      int c = 1;
922✔
297
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
922✔
298
      TSDB_CHECK_CODE(code, lino, _exit);
922!
299
      if (c == 0) {
922!
300
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
922!
301
      } else if (c < 0) {
×
302
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
303
        TSDB_CHECK_CODE(code, lino, _exit);
×
304
      } else {
305
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
306
        TSDB_CHECK_CODE(code, lino, _exit);
×
307
      }
308
    }
309

310
    pTbCur->paused = 0;
1,136,325✔
311
  }
312

313
_exit:
×
314
  if (code != 0 && locked) {
1,135,968!
315
    metaReaderReleaseLock(&pTbCur->mr);
×
316
  }
317
  return code;
1,135,675✔
318
}
319

320
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
14,241,482✔
321
  int    ret;
322
  void  *pBuf;
323
  STbCfg tbCfg;
324

325
  for (;;) {
326
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
14,241,482✔
327
    if (ret < 0) {
14,221,838✔
328
      return ret;
1,139,036✔
329
    }
330

331
    tDecoderClear(&pTbCur->mr.coder);
13,082,802✔
332

333
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
13,135,584✔
334
    if (ret) return ret;
13,099,732!
335

336
    if (pTbCur->mr.me.type == jumpTableType) {
13,099,732✔
337
      continue;
3,740,264✔
338
    }
339

340
    break;
9,359,468✔
341
  }
342

343
  return 0;
9,359,468✔
344
}
345

346
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
347
  int    ret;
348
  void  *pBuf;
349
  STbCfg tbCfg;
350

351
  for (;;) {
352
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
353
    if (ret < 0) {
×
354
      return -1;
×
355
    }
356

357
    tDecoderClear(&pTbCur->mr.coder);
×
358

359
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
360
    if (ret < 0) {
×
361
      return ret;
×
362
    }
363

364
    if (pTbCur->mr.me.type == jumpTableType) {
×
365
      continue;
×
366
    }
367

368
    break;
×
369
  }
370

371
  return 0;
×
372
}
373

374
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, int64_t *createTime) {
4,983,820✔
375
  void           *pData = NULL;
4,983,820✔
376
  int             nData = 0;
4,983,820✔
377
  int64_t         version;
378
  SSchemaWrapper  schema = {0};
4,983,820✔
379
  SSchemaWrapper *pSchema = NULL;
4,983,820✔
380
  SDecoder        dc = {0};
4,983,820✔
381
  if (lock) {
4,983,820✔
382
    metaRLock(pMeta);
4,964,835✔
383
  }
384
_query:
4,997,074✔
385
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
5,076,700✔
386
    goto _err;
23✔
387
  }
388

389
  version = ((SUidIdxVal *)pData)[0].version;
5,081,597✔
390

391
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
5,081,597!
392
    goto _err;
×
393
  }
394

395
  SMetaEntry me = {0};
5,083,105✔
396
  tDecoderInit(&dc, pData, nData);
5,083,105✔
397
  int32_t code = metaDecodeEntry(&dc, &me);
5,079,101✔
398
  if (code) {
5,080,851!
399
    tDecoderClear(&dc);
×
400
    goto _err;
×
401
  }
402
  if (me.type == TSDB_SUPER_TABLE) {
5,080,851✔
403
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,648,856✔
404
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,651,666✔
405
      tDecoderClear(&dc);
4,651,666✔
406
      goto _exit;
4,646,121✔
407
    }
408
  } else if (me.type == TSDB_CHILD_TABLE) {
431,995✔
409
    uid = me.ctbEntry.suid;
79,592✔
410
    if (createTime != NULL){
79,592✔
411
      *createTime = me.ctbEntry.btime;
77,901✔
412
    }
413
    tDecoderClear(&dc);
79,592✔
414
    goto _query;
79,626✔
415
  } else {
416
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
352,403✔
417
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
352,571✔
418
      tDecoderClear(&dc);
352,571✔
419
      goto _exit;
352,566✔
420
    }
421
  }
422
  tDecoderClear(&dc);
3,409✔
423

424
  // query from skm db
425
  if (tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData) < 0) {
130!
426
    goto _err;
×
427
  }
428

429
  tDecoderInit(&dc, pData, nData);
130✔
430
  if (tDecodeSSchemaWrapperEx(&dc, &schema) != 0) {
130!
431
    goto _err;
×
432
  }
433
  pSchema = tCloneSSchemaWrapper(&schema);
130✔
434
  tDecoderClear(&dc);
130✔
435

436
_exit:
4,998,817✔
437
  if (lock) {
4,998,817✔
438
    metaULock(pMeta);
4,970,105✔
439
  }
440
  tdbFree(pData);
5,001,662✔
441
  return pSchema;
5,004,847✔
442

443
_err:
23✔
444
  if (lock) {
23!
445
    metaULock(pMeta);
23✔
446
  }
447
  tdbFree(pData);
23✔
448
  return NULL;
23✔
449
}
450

451
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
3,919,660✔
452
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
3,919,660✔
453
  SMCtbCursor *pCtbCur = NULL;
3,919,660✔
454
  SCtbIdxKey   ctbIdxKey;
455
  int          ret = 0;
3,919,660✔
456
  int          c = 0;
3,919,660✔
457

458
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,919,660✔
459
  if (pCtbCur == NULL) {
3,929,568!
460
    return NULL;
×
461
  }
462

463
  pCtbCur->pMeta = pMeta;
3,929,568✔
464
  pCtbCur->suid = uid;
3,929,568✔
465
  pCtbCur->lock = lock;
3,929,568✔
466
  pCtbCur->paused = 1;
3,929,568✔
467

468
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,929,568✔
469
  if (ret < 0) {
3,924,977!
470
    return NULL;
×
471
  }
472
  return pCtbCur;
3,924,977✔
473
}
474

475
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,928,669✔
476
  if (pCtbCur) {
3,928,669!
477
    if (!pCtbCur->paused) {
3,928,970✔
478
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,885,759!
479
      if (pCtbCur->pCur) {
3,886,737!
480
        tdbTbcClose(pCtbCur->pCur);
3,886,919✔
481
      }
482
    }
483
    tdbFree(pCtbCur->pKey);
3,929,313✔
484
    tdbFree(pCtbCur->pVal);
3,929,178✔
485
  }
486
  taosMemoryFree(pCtbCur);
3,929,218✔
487
}
3,930,719✔
488

489
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,344✔
490
  if (!pCtbCur->paused) {
43,344!
491
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,365✔
492
    if (pCtbCur->lock) {
43,433!
493
      metaULock(pCtbCur->pMeta);
43,433✔
494
    }
495
    pCtbCur->paused = 1;
43,413✔
496
  }
497
}
43,392✔
498

499
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,922,918✔
500
  if (pCtbCur->paused) {
3,922,918!
501
    pCtbCur->paused = 0;
3,925,664✔
502

503
    if (pCtbCur->lock) {
3,925,664✔
504
      metaRLock(pCtbCur->pMeta);
3,905,241✔
505
    }
506
    int ret = 0;
3,925,049✔
507
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,925,049✔
508
    if (ret < 0) {
3,928,216!
509
      metaCloseCtbCursor(pCtbCur);
×
510
      return -1;
×
511
    }
512

513
    if (first) {
3,928,342!
514
      SCtbIdxKey ctbIdxKey;
515
      // move to the suid
516
      ctbIdxKey.suid = pCtbCur->suid;
3,928,342✔
517
      ctbIdxKey.uid = INT64_MIN;
3,928,342✔
518
      int c = 0;
3,928,342✔
519
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,928,342✔
520
      if (c > 0) {
3,925,641✔
521
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
800,269✔
522
      }
523
    } else {
524
      int c = 0;
×
525
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
526
      if (c < 0) {
×
527
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
528
      } else {
529
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
530
      }
531
    }
532
  }
533
  return 0;
3,922,326✔
534
}
535

536
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
16,410,492✔
537
  int         ret;
538
  SCtbIdxKey *pCtbIdxKey;
539

540
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
16,410,492✔
541
  if (ret < 0) {
16,411,977✔
542
    return 0;
838,806✔
543
  }
544

545
  pCtbIdxKey = pCtbCur->pKey;
15,573,171✔
546
  if (pCtbIdxKey->suid > pCtbCur->suid) {
15,573,171✔
547
    return 0;
3,091,621✔
548
  }
549

550
  return pCtbIdxKey->uid;
12,481,550✔
551
}
552

553
struct SMStbCursor {
554
  SMeta   *pMeta;
555
  TBC     *pCur;
556
  tb_uid_t suid;
557
  void    *pKey;
558
  void    *pVal;
559
  int      kLen;
560
  int      vLen;
561
};
562

563
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
161,463✔
564
  SMStbCursor *pStbCur = NULL;
161,463✔
565
  int          ret = 0;
161,463✔
566
  int          c = 0;
161,463✔
567

568
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
161,463✔
569
  if (pStbCur == NULL) {
161,464!
570
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
571
    return NULL;
×
572
  }
573

574
  pStbCur->pMeta = pMeta;
161,464✔
575
  pStbCur->suid = suid;
161,464✔
576
  metaRLock(pMeta);
161,464✔
577

578
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
161,464✔
579
  if (ret < 0) {
161,462!
580
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
581
    metaULock(pMeta);
×
582
    taosMemoryFree(pStbCur);
×
583
    return NULL;
×
584
  }
585

586
  // move to the suid
587
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
161,462✔
588
  if (c > 0) {
161,462!
589
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
590
  }
591

592
  return pStbCur;
161,462✔
593
}
594

595
void metaCloseStbCursor(SMStbCursor *pStbCur) {
161,461✔
596
  if (pStbCur) {
161,461!
597
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
161,461!
598
    if (pStbCur->pCur) {
161,463!
599
      tdbTbcClose(pStbCur->pCur);
161,463✔
600

601
      tdbFree(pStbCur->pKey);
161,462✔
602
      tdbFree(pStbCur->pVal);
161,462✔
603
    }
604

605
    taosMemoryFree(pStbCur);
161,462✔
606
  }
607
}
161,462✔
608

609
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
294,732✔
610
  int ret;
611

612
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
294,732✔
613
  if (ret < 0) {
294,727✔
614
    return 0;
161,461✔
615
  }
616
  return *(tb_uid_t *)pStbCur->pKey;
133,266✔
617
}
618

619
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
4,840,950✔
620
  STSchema       *pTSchema = NULL;
4,840,950✔
621
  SSchemaWrapper *pSW = NULL;
4,840,950✔
622

623
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
4,840,950✔
624
  if (!pSW) return NULL;
4,854,959✔
625

626
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
4,854,958✔
627

628
  taosMemoryFree(pSW->pSchema);
4,856,972✔
629
  taosMemoryFree(pSW);
4,851,685✔
630
  return pTSchema;
4,855,270✔
631
}
632

633
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
12,056✔
634
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
12,056✔
635
  if (*ppTSchema == NULL) {
12,077!
636
    return terrno;
×
637
  }
638
  return TSDB_CODE_SUCCESS;
12,078✔
639
}
640

641
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,829,162✔
642
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,829,162✔
643
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,842,856!
644
    return terrno;
×
645
  }
646
  return TSDB_CODE_SUCCESS;
4,843,417✔
647
}
648

649
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
652,612✔
650
  int32_t code = 0;
652,612✔
651
  int32_t lino;
652

653
  void     *pData = NULL;
652,612✔
654
  int       nData = 0;
652,612✔
655
  SSkmDbKey skmDbKey;
656
  if (sver <= 0) {
652,612✔
657
    SMetaInfo info;
658
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
405,685!
659
      sver = info.skmVer;
405,701✔
660
    } else {
661
      TBC *pSkmDbC = NULL;
×
662
      int  c;
663

664
      skmDbKey.uid = suid ? suid : uid;
×
665
      skmDbKey.sver = INT32_MAX;
×
666

667
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
×
668
      TSDB_CHECK_CODE(code, lino, _exit);
×
669
      metaRLock(pMeta);
×
670

671
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
×
672
        metaULock(pMeta);
×
673
        tdbTbcClose(pSkmDbC);
×
674
        code = TSDB_CODE_NOT_FOUND;
×
675
        goto _exit;
×
676
      }
677

678
      if (c == 0) {
×
679
        metaULock(pMeta);
×
680
        tdbTbcClose(pSkmDbC);
×
681
        code = TSDB_CODE_FAILED;
×
682
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
683
        goto _exit;
×
684
      }
685

686
      if (c < 0) {
×
687
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
688
      }
689

690
      const void *pKey = NULL;
×
691
      int32_t     nKey = 0;
×
692
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
×
693

694
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
×
695
        metaULock(pMeta);
×
696
        tdbTbcClose(pSkmDbC);
×
697
        code = TSDB_CODE_NOT_FOUND;
×
698
        goto _exit;
×
699
      }
700

701
      sver = ((SSkmDbKey *)pKey)->sver;
×
702

703
      metaULock(pMeta);
×
704
      tdbTbcClose(pSkmDbC);
×
705
    }
706
  }
707

708
  if (!(sver > 0)) {
652,577!
709
    code = TSDB_CODE_NOT_FOUND;
×
710
    goto _exit;
×
711
  }
712

713
  skmDbKey.uid = suid ? suid : uid;
652,577✔
714
  skmDbKey.sver = sver;
652,577✔
715
  metaRLock(pMeta);
652,577✔
716
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
652,737!
717
    metaULock(pMeta);
×
718
    code = TSDB_CODE_NOT_FOUND;
×
719
    goto _exit;
×
720
  }
721
  metaULock(pMeta);
652,627✔
722

723
  // decode
724
  SDecoder        dc = {0};
652,664✔
725
  SSchemaWrapper  schema;
726
  SSchemaWrapper *pSchemaWrapper = &schema;
652,664✔
727

728
  tDecoderInit(&dc, pData, nData);
652,664✔
729
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
652,882✔
730
  tDecoderClear(&dc);
652,882✔
731
  tdbFree(pData);
652,574✔
732
  if (TSDB_CODE_SUCCESS != code) {
652,599!
733
    taosMemoryFree(pSchemaWrapper->pSchema);
×
734
    goto _exit;
×
735
  }
736

737
  // convert
738
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
652,599✔
739
  if (pTSchema == NULL) {
652,584!
740
    code = TSDB_CODE_OUT_OF_MEMORY;
×
741
  }
742

743
  *ppTSchema = pTSchema;
652,584✔
744
  taosMemoryFree(pSchemaWrapper->pSchema);
652,584✔
745

746
_exit:
652,704✔
747
  return code;
652,704✔
748
}
749

750
// N.B. Called by statusReq per second
751
int64_t metaGetTbNum(SMeta *pMeta) {
957,499✔
752
  // num of child tables (excluding normal tables , stables and others)
753

754
  /* int64_t num = 0; */
755
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
756

757
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
957,499✔
758
}
759

760
void metaUpdTimeSeriesNum(SMeta *pMeta) {
161,458✔
761
  int64_t nCtbTimeSeries = 0;
161,458✔
762
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
161,458!
763
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
161,458✔
764
  }
765
}
161,459✔
766

767
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
768
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
769
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,138,378✔
770
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,138,394✔
771
    metaUpdTimeSeriesNum(pMeta);
158,009✔
772
  }
773

774
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,138,410✔
775
}
776

777
// type: 1 reported timeseries
778
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,138,378!
779
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,138,410✔
780
  if (type == 1) {
1,138,410✔
781
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
957,499✔
782
  }
783
  return nTimeSeries;
1,138,407✔
784
}
785

786
typedef struct {
787
  SMeta   *pMeta;
788
  TBC     *pCur;
789
  tb_uid_t uid;
790
  void    *pKey;
791
  void    *pVal;
792
  int      kLen;
793
  int      vLen;
794
} SMSmaCursor;
795

796
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
797
  SMSmaCursor *pSmaCur = NULL;
×
798
  SSmaIdxKey   smaIdxKey;
799
  int          ret;
800
  int          c;
801

802
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
803
  if (pSmaCur == NULL) {
×
804
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
805
    return NULL;
×
806
  }
807

808
  pSmaCur->pMeta = pMeta;
×
809
  pSmaCur->uid = uid;
×
810
  metaRLock(pMeta);
×
811

812
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
813
  if (ret < 0) {
×
814
    metaULock(pMeta);
×
815
    taosMemoryFree(pSmaCur);
×
816
    return NULL;
×
817
  }
818

819
  // move to the suid
820
  smaIdxKey.uid = uid;
×
821
  smaIdxKey.smaUid = INT64_MIN;
×
822
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
823
  if (c > 0) {
×
824
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
825
  }
826

827
  return pSmaCur;
×
828
}
829

830
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
831
  if (pSmaCur) {
×
832
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
833
    if (pSmaCur->pCur) {
×
834
      tdbTbcClose(pSmaCur->pCur);
×
835
      pSmaCur->pCur = NULL;
×
836

837
      tdbFree(pSmaCur->pKey);
×
838
      tdbFree(pSmaCur->pVal);
×
839
    }
840

841
    taosMemoryFree(pSmaCur);
×
842
  }
843
}
×
844

845
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
846
  int         ret;
847
  SSmaIdxKey *pSmaIdxKey;
848

849
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
850
  if (ret < 0) {
×
851
    return 0;
×
852
  }
853

854
  pSmaIdxKey = pSmaCur->pKey;
×
855
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
856
    return 0;
×
857
  }
858

859
  return pSmaIdxKey->uid;
×
860
}
861

862
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
863
  STSmaWrapper *pSW = NULL;
×
864
  SArray       *pSmaIds = NULL;
×
865

866
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
867
    return NULL;
×
868
  }
869

870
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
871
  if (!pSW) {
×
872
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
873
    goto _err;
×
874
  }
875

876
  pSW->number = taosArrayGetSize(pSmaIds);
×
877
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
878

879
  if (!pSW->tSma) {
×
880
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
881
    goto _err;
×
882
  }
883

884
  SMetaReader mr = {0};
×
885
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
886
  int64_t smaId;
887
  int     smaIdx = 0;
×
888
  STSma  *pTSma = NULL;
×
889
  for (int i = 0; i < pSW->number; ++i) {
×
890
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
891
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
892
      tDecoderClear(&mr.coder);
×
893
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
894
      continue;
×
895
    }
896
    tDecoderClear(&mr.coder);
×
897
    pTSma = pSW->tSma + smaIdx;
×
898
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
899
    if (deepCopy) {
×
900
      if (pTSma->exprLen > 0) {
×
901
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
902
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
903
          goto _err;
×
904
        }
905
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
906
      }
907
      if (pTSma->tagsFilterLen > 0) {
×
908
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
909
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
910
          goto _err;
×
911
        }
912
      }
913
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
914
    } else {
915
      pTSma->exprLen = 0;
×
916
      pTSma->expr = NULL;
×
917
      pTSma->tagsFilterLen = 0;
×
918
      pTSma->tagsFilter = NULL;
×
919
    }
920

921
    ++smaIdx;
×
922
  }
923

924
  if (smaIdx <= 0) goto _err;
×
925
  pSW->number = smaIdx;
×
926

927
  metaReaderClear(&mr);
×
928
  taosArrayDestroy(pSmaIds);
×
929
  return pSW;
×
930
_err:
×
931
  metaReaderClear(&mr);
×
932
  taosArrayDestroy(pSmaIds);
×
933
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
934
  return NULL;
×
935
}
936

937
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
4✔
938
  STSma      *pTSma = NULL;
4✔
939
  SMetaReader mr = {0};
4✔
940
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
4✔
941
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
4!
942
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
943
    metaReaderClear(&mr);
×
944
    return NULL;
×
945
  }
946
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
4✔
947
  if (!pTSma) {
4!
948
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
949
    metaReaderClear(&mr);
×
950
    return NULL;
×
951
  }
952

953
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
4✔
954

955
  metaReaderClear(&mr);
4✔
956
  return pTSma;
4✔
957
}
958

959
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
960
  SArray     *pUids = NULL;
×
961
  SSmaIdxKey *pSmaIdxKey = NULL;
×
962

963
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
964
  if (!pCur) {
×
965
    return NULL;
×
966
  }
967

968
  while (1) {
×
969
    tb_uid_t id = metaSmaCursorNext(pCur);
×
970
    if (id == 0) {
×
971
      break;
×
972
    }
973

974
    if (!pUids) {
×
975
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
976
      if (!pUids) {
×
977
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
978
        metaCloseSmaCursor(pCur);
×
979
        return NULL;
×
980
      }
981
    }
982

983
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
984

985
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
986
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
987
      metaCloseSmaCursor(pCur);
×
988
      taosArrayDestroy(pUids);
×
989
      return NULL;
×
990
    }
991
  }
992

993
  metaCloseSmaCursor(pCur);
×
994
  return pUids;
×
995
}
996

997
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
998
  SArray     *pUids = NULL;
×
999
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1000
  tb_uid_t    lastUid = 0;
×
1001

1002
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1003
  if (!pCur) {
×
1004
    return NULL;
×
1005
  }
1006

1007
  while (1) {
×
1008
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1009
    if (uid == 0) {
×
1010
      break;
×
1011
    }
1012

1013
    if (lastUid == uid) {
×
1014
      continue;
×
1015
    }
1016

1017
    lastUid = uid;
×
1018

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

1028
    if (!taosArrayPush(pUids, &uid)) {
×
1029
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1030
      metaCloseSmaCursor(pCur);
×
1031
      taosArrayDestroy(pUids);
×
1032
      return NULL;
×
1033
    }
1034
  }
1035

1036
  metaCloseSmaCursor(pCur);
×
1037
  return pUids;
×
1038
}
1039

1040
#endif
1041

1042
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
20,084,693✔
1043
  STag *tag = (STag *)pTag;
20,084,693✔
1044
  if (type == TSDB_DATA_TYPE_JSON) {
20,084,693✔
1045
    return tag;
10,215✔
1046
  }
1047
  bool find = tTagGet(tag, val);
20,074,478✔
1048

1049
  if (!find) {
20,104,907✔
1050
    return NULL;
128,172✔
1051
  }
1052

1053
  return val;
19,976,735✔
1054
}
1055

1056
typedef struct {
1057
  SMeta   *pMeta;
1058
  TBC     *pCur;
1059
  tb_uid_t suid;
1060
  int16_t  cid;
1061
  int16_t  type;
1062
  void    *pKey;
1063
  void    *pVal;
1064
  int32_t  kLen;
1065
  int32_t  vLen;
1066
} SIdxCursor;
1067

1068
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1069
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1070
  SMetaFltParam *param = arg;
6✔
1071
  int32_t        ret = 0;
6✔
1072

1073
  SIdxCursor *pCursor = NULL;
6✔
1074
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
6✔
1075
  if (pCursor == NULL) {
6!
1076
    return terrno;
×
1077
  }
1078
  pCursor->pMeta = pMeta;
6✔
1079
  pCursor->suid = param->suid;
6✔
1080
  pCursor->cid = param->cid;
6✔
1081
  pCursor->type = param->type;
6✔
1082

1083
  metaRLock(pMeta);
6✔
1084
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
6✔
1085
  if (ret != 0) {
6!
1086
    goto END;
×
1087
  }
1088
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
6✔
1089

1090
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
6✔
1091
  SBtimeIdxKey *pBtimeKey = &btimeKey;
6✔
1092

1093
  int cmp = 0;
6✔
1094
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
6!
1095
    goto END;
×
1096
  }
1097

1098
  int32_t valid = 0;
6✔
1099
  int32_t count = 0;
6✔
1100

1101
  static const int8_t TRY_ERROR_LIMIT = 1;
1102
  do {
35,565✔
1103
    void   *entryKey = NULL;
35,571✔
1104
    int32_t nEntryKey = -1;
35,571✔
1105
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
35,571✔
1106
    if (valid < 0) break;
36,096✔
1107

1108
    SBtimeIdxKey *p = entryKey;
36,090✔
1109
    if (count > TRY_ERROR_LIMIT) break;
36,090!
1110

1111
    terrno = TSDB_CODE_SUCCESS;
36,090✔
1112
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
36,093✔
1113
    if (terrno != TSDB_CODE_SUCCESS) {
35,818!
1114
      ret = terrno;
×
1115
      break;
×
1116
    }
1117
    if (cmp == 0) {
35,709!
1118
      if (taosArrayPush(pUids, &p->uid) == NULL) {
71,349!
1119
        ret = terrno;
×
1120
        break;
×
1121
      }
1122
    } else {
1123
      if (param->equal == true) {
×
1124
        if (count > TRY_ERROR_LIMIT) break;
×
1125
        count++;
×
1126
      }
1127
    }
1128
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
35,640✔
1129
    if (valid < 0) break;
35,565!
1130
  } while (1);
1131

1132
END:
6✔
1133
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1134
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1135
  taosMemoryFree(pCursor);
6✔
1136
  return ret;
6✔
1137
}
1138

1139
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1140
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1141
  SMetaFltParam *param = arg;
×
1142
  int32_t        ret = 0;
×
1143
  char          *buf = NULL;
×
1144

1145
  STagIdxKey *pKey = NULL;
×
1146
  int32_t     nKey = 0;
×
1147

1148
  SIdxCursor *pCursor = NULL;
×
1149
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1150
  if (pCursor == NULL) {
×
1151
    return terrno;
×
1152
  }
1153
  pCursor->pMeta = pMeta;
×
1154
  pCursor->suid = param->suid;
×
1155
  pCursor->cid = param->cid;
×
1156
  pCursor->type = param->type;
×
1157

1158
  char *pName = param->val;
×
1159

1160
  metaRLock(pMeta);
×
1161
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1162
  if (ret != 0) {
×
1163
    goto END;
×
1164
  }
1165

1166
  int cmp = 0;
×
1167
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1168
    goto END;
×
1169
  }
1170
  int32_t valid = 0;
×
1171
  int32_t count = 0;
×
1172

1173
  int32_t TRY_ERROR_LIMIT = 1;
×
1174
  do {
×
1175
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1176
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1177
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1178
    if (valid < 0) break;
×
1179

1180
    if (count > TRY_ERROR_LIMIT) break;
×
1181

1182
    char *pTableKey = (char *)pEntryKey;
×
1183
    terrno = TSDB_CODE_SUCCESS;
×
1184
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1185
    if (terrno != TSDB_CODE_SUCCESS) {
×
1186
      ret = terrno;
×
1187
      goto END;
×
1188
    }
1189
    if (cmp == 0) {
×
1190
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1191
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1192
        ret = terrno;
×
1193
        goto END;
×
1194
      }
1195
    } else {
1196
      if (param->equal == true) {
×
1197
        if (count > TRY_ERROR_LIMIT) break;
×
1198
        count++;
×
1199
      }
1200
    }
1201
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1202
    if (valid < 0) {
×
1203
      break;
×
1204
    }
1205
  } while (1);
1206

1207
END:
×
1208
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1209
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1210
  taosMemoryFree(buf);
×
1211
  taosMemoryFree(pKey);
×
1212

1213
  taosMemoryFree(pCursor);
×
1214

1215
  return ret;
×
1216
}
1217
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1218
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1219
  SMetaFltParam *param = arg;
×
1220
  int32_t        ret = 0;
×
1221
  char          *buf = NULL;
×
1222

1223
  STtlIdxKey *pKey = NULL;
×
1224
  int32_t     nKey = 0;
×
1225

1226
  SIdxCursor *pCursor = NULL;
×
1227
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1228
  if (pCursor == NULL) {
×
1229
    return terrno;
×
1230
  }
1231
  pCursor->pMeta = pMeta;
×
1232
  pCursor->suid = param->suid;
×
1233
  pCursor->cid = param->cid;
×
1234
  pCursor->type = param->type;
×
1235

1236
  metaRLock(pMeta);
×
1237
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1238

1239
END:
×
1240
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1241
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1242
  taosMemoryFree(buf);
×
1243
  taosMemoryFree(pKey);
×
1244

1245
  taosMemoryFree(pCursor);
×
1246

1247
  return ret;
×
1248
  // impl later
1249
  return 0;
1250
}
1251
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
5,052✔
1252
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
5,052✔
1253
  SMetaFltParam *param = arg;
5,052✔
1254

1255
  SMetaEntry oStbEntry = {0};
5,052✔
1256
  int32_t    code = 0;
5,052✔
1257
  char      *buf = NULL;
5,052✔
1258
  void      *pData = NULL;
5,052✔
1259
  int        nData = 0;
5,052✔
1260

1261
  SDecoder    dc = {0};
5,052✔
1262
  STbDbKey    tbDbKey = {0};
5,052✔
1263
  STagIdxKey *pKey = NULL;
5,052✔
1264
  int32_t     nKey = 0;
5,052✔
1265

1266
  SIdxCursor *pCursor = NULL;
5,052✔
1267
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
5,052✔
1268
  if (!pCursor) {
5,069!
1269
    return terrno;
×
1270
  }
1271
  pCursor->pMeta = pMeta;
5,069✔
1272
  pCursor->suid = param->suid;
5,069✔
1273
  pCursor->cid = param->cid;
5,069✔
1274
  pCursor->type = param->type;
5,069✔
1275

1276
  metaRLock(pMeta);
5,069✔
1277

1278
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
5,057!
1279

1280
  tbDbKey.uid = param->suid;
5,079✔
1281
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
5,079✔
1282

1283
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
5,079!
1284

1285
  tDecoderInit(&dc, pData, nData);
5,082✔
1286

1287
  code = metaDecodeEntry(&dc, &oStbEntry);
5,069✔
1288
  if (code) {
5,074!
1289
    tDecoderClear(&dc);
×
1290
    goto END;
×
1291
  }
1292

1293
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
5,074!
1294
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
3!
1295
  }
1296

1297
  code = TSDB_CODE_INVALID_PARA;
5,074✔
1298
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
6,344✔
1299
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
5,888✔
1300
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
5,888!
1301
      code = 0;
450✔
1302
    } else {
1303
      TAOS_CHECK_GOTO(code, NULL, END);
5,438✔
1304
    }
1305
  }
1306

1307
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
456✔
1308
  if (code != 0) {
450!
1309
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1310
  }
1311

1312
  int32_t maxSize = 0;
450✔
1313
  int32_t nTagData = 0;
450✔
1314
  void   *tagData = NULL;
450✔
1315

1316
  if (param->val == NULL) {
450!
1317
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1318
    goto END;
×
1319
  } else {
1320
    if (IS_VAR_DATA_TYPE(param->type)) {
450!
1321
      tagData = varDataVal(param->val);
×
1322
      nTagData = varDataLen(param->val);
×
1323

1324
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
×
1325
        maxSize = 4 * nTagData + 1;
×
1326
        buf = taosMemoryCalloc(1, maxSize);
×
1327
        if (buf == NULL) {
×
1328
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1329
        }
1330

1331
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
×
1332
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1333
        }
1334

1335
        tagData = buf;
×
1336
        nTagData = maxSize;
×
1337
      }
1338
    } else {
1339
      tagData = param->val;
450✔
1340
      nTagData = tDataTypes[param->type].bytes;
450✔
1341
    }
1342
  }
1343

1344
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
450!
1345
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1346
                  NULL, END);
1347

1348
  int cmp = 0;
450✔
1349
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
450!
1350

1351
  int     count = 0;
450✔
1352
  int32_t valid = 0;
450✔
1353
  bool    found = false;
450✔
1354

1355
  static const int8_t TRY_ERROR_LIMIT = 1;
1356

1357
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1358
  /// target:                        [suid, cid2, type2]
1359
  int diffCidCount = 0;
450✔
1360
  do {
11,472✔
1361
    void   *entryKey = NULL, *entryVal = NULL;
11,922✔
1362
    int32_t nEntryKey, nEntryVal;
1363

1364
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
11,922✔
1365
    if (valid < 0) {
11,915✔
1366
      code = valid;
348✔
1367
    }
1368
    if (count > TRY_ERROR_LIMIT) {
11,915✔
1369
      break;
450✔
1370
    }
1371

1372
    STagIdxKey *p = entryKey;
11,890✔
1373
    if (p == NULL) break;
11,890✔
1374

1375
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
11,546!
1376
      if (found == true) break;  //
81!
1377
      if (diffCidCount > TRY_ERROR_LIMIT) break;
×
1378
      diffCidCount++;
×
1379
      count++;
×
1380
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1381
      if (valid < 0) {
1!
1382
        code = valid;
×
1383
        break;
×
1384
      } else {
1385
        continue;
1✔
1386
      }
1387
    }
1388

1389
    terrno = TSDB_CODE_SUCCESS;
11,465✔
1390
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
11,465✔
1391
    if (terrno != TSDB_CODE_SUCCESS) {
11,461!
1392
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1393
      break;
×
1394
    }
1395
    if (cmp == 0) {
11,468✔
1396
      // match
1397
      tb_uid_t tuid = 0;
10,650✔
1398
      if (IS_VAR_DATA_TYPE(pKey->type)) {
10,650!
1399
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
×
1400
      } else {
1401
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
10,650✔
1402
      }
1403
      if (taosArrayPush(pUids, &tuid) == NULL) {
10,660!
1404
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1405
      }
1406
      found = true;
10,660✔
1407
    } else {
1408
      if (param->equal == true) {
818✔
1409
        if (count > TRY_ERROR_LIMIT) break;
113!
1410
        count++;
113✔
1411
      }
1412
    }
1413
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
11,478✔
1414
    if (valid < 0) {
11,471!
1415
      code = valid;
×
1416
      break;
×
1417
    }
1418
  } while (1);
1419

1420
END:
5,068✔
1421
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
5,068✔
1422
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
5,083✔
1423
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
5,083!
1424
  tDecoderClear(&dc);
5,083✔
1425
  tdbFree(pData);
5,079✔
1426

1427
  taosMemoryFree(buf);
5,080✔
1428
  taosMemoryFree(pKey);
5,079✔
1429

1430
  taosMemoryFree(pCursor);
5,078✔
1431

1432
  return code;
5,071✔
1433
}
1434

1435
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
243,670✔
1436
  int ret = 0;
243,670✔
1437
  if (lock) {
243,670!
1438
    metaRLock(pMeta);
×
1439
  }
1440

1441
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
243,670✔
1442
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
243,670✔
1443
  if (lock) {
243,673!
1444
    metaULock(pMeta);
×
1445
  }
1446

1447
  return ret;
243,673✔
1448
}
1449

1450
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
243,114✔
1451
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
243,114✔
1452
  const int32_t LIMIT = 128;
243,114✔
1453

1454
  int32_t isLock = false;
243,114✔
1455
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
243,114!
1456
  for (int i = 0; i < sz; i++) {
486,783✔
1457
    STUidTagInfo *p = taosArrayGet(uidList, i);
243,670✔
1458

1459
    if (i % LIMIT == 0) {
243,670✔
1460
      if (isLock) metaULock(pMeta);
243,113!
1461

1462
      metaRLock(pMeta);
243,113✔
1463
      isLock = true;
243,114✔
1464
    }
1465

1466
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1467
    void   *val = NULL;
243,671✔
1468
    int32_t len = 0;
243,671✔
1469
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
243,671!
1470
      p->pTagVal = taosMemoryMalloc(len);
243,673✔
1471
      if (!p->pTagVal) {
243,673!
1472
        if (isLock) metaULock(pMeta);
×
1473

1474
        TAOS_RETURN(terrno);
×
1475
      }
1476
      memcpy(p->pTagVal, val, len);
243,673✔
1477
      tdbFree(val);
243,673✔
1478
    } else {
1479
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64 "", TD_VID(pMeta->pVnode), suid,
×
1480
                p->uid);
1481
    }
1482
  }
1483
  //  }
1484
  if (isLock) metaULock(pMeta);
243,113✔
1485
  return 0;
243,118✔
1486
}
1487

1488
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
933,265✔
1489
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
933,265✔
1490
  if (!pCur) {
934,870!
1491
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1492
  }
1493

1494
  // If len > 0 means there already have uids, and we only want the
1495
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1496
  // in the hash map, that may require a lot of memory
1497
  SHashObj *pSepecifiedUidMap = NULL;
934,870✔
1498
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
934,870✔
1499
  if (numOfElems > 0) {
934,458✔
1500
    pSepecifiedUidMap =
1501
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
98,679✔
1502
    for (int i = 0; i < numOfElems; i++) {
396,869✔
1503
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
298,137✔
1504
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
298,062✔
1505
      if (code) {
298,202!
1506
        metaCloseCtbCursor(pCur);
×
1507
        taosHashCleanup(pSepecifiedUidMap);
×
1508
        return code;
×
1509
      }
1510
    }
1511
  }
1512

1513
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
934,511✔
1514
    while (1) {
2,438,844✔
1515
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,274,083✔
1516
      if (uid == 0) {
3,268,819✔
1517
        break;
837,440✔
1518
      }
1519

1520
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,431,379✔
1521
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,431,379✔
1522
      if (!info.pTagVal) {
2,440,769!
1523
        metaCloseCtbCursor(pCur);
×
1524
        taosHashCleanup(pSepecifiedUidMap);
×
1525
        return terrno;
×
1526
      }
1527
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,440,769✔
1528
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,438,844!
1529
        taosMemoryFreeClear(info.pTagVal);
×
1530
        metaCloseCtbCursor(pCur);
×
1531
        taosHashCleanup(pSepecifiedUidMap);
×
1532
        return terrno;
×
1533
      }
1534
    }
1535
  } else {  // only the specified tables need to be added
1536
    while (1) {
409,256✔
1537
      tb_uid_t uid = metaCtbCursorNext(pCur);
508,528✔
1538
      if (uid == 0) {
507,479✔
1539
        break;
98,719✔
1540
      }
1541

1542
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
408,760✔
1543
      if (index == NULL) {
409,427✔
1544
        continue;
111,485✔
1545
      }
1546

1547
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
297,942✔
1548
      if (pTagInfo->pTagVal == NULL) {
297,898✔
1549
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
297,894✔
1550
        if (!pTagInfo->pTagVal) {
297,975✔
1551
          metaCloseCtbCursor(pCur);
208✔
1552
          taosHashCleanup(pSepecifiedUidMap);
×
1553
          return terrno;
×
1554
        }
1555
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
297,767✔
1556
      }
1557
    }
1558
  }
1559

1560
  taosHashCleanup(pSepecifiedUidMap);
936,159✔
1561
  metaCloseCtbCursor(pCur);
935,868✔
1562
  return TSDB_CODE_SUCCESS;
936,296✔
1563
}
1564

1565
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1566

1567
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
36,078,036✔
1568
  int32_t code = 0;
36,078,036✔
1569
  void   *pData = NULL;
36,078,036✔
1570
  int     nData = 0;
36,078,036✔
1571
  int     lock = 0;
36,078,036✔
1572

1573
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
36,078,036!
1574
    lock = 1;
7,769,958✔
1575
  }
1576

1577
  if (!lock) metaRLock(pMeta);
36,078,036✔
1578

1579
  // search cache
1580
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
36,082,334✔
1581
    if (!lock) metaULock(pMeta);
35,913,948✔
1582
    goto _exit;
35,918,814✔
1583
  }
1584

1585
  // search TDB
1586
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
170,980✔
1587
    // not found
1588
    if (!lock) metaULock(pMeta);
30,842✔
1589
    goto _exit;
30,843✔
1590
  }
1591

1592
  if (!lock) metaULock(pMeta);
140,336✔
1593

1594
  pInfo->uid = uid;
140,336✔
1595
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
140,336✔
1596
  pInfo->version = ((SUidIdxVal *)pData)->version;
140,336✔
1597
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
140,336✔
1598

1599
  if (lock) {
140,336✔
1600
    metaULock(pReader->pMeta);
23,649✔
1601
    // metaReaderReleaseLock(pReader);
1602
  }
1603
  // upsert the cache
1604
  metaWLock(pMeta);
140,338✔
1605
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
140,338✔
1606
  if (ret != 0) {
140,336!
1607
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1608
  }
1609
  metaULock(pMeta);
140,336✔
1610

1611
  if (lock) {
140,338✔
1612
    metaRLock(pReader->pMeta);
23,651✔
1613
  }
1614

1615
_exit:
116,687✔
1616
  tdbFree(pData);
36,089,995✔
1617
  return code;
36,088,128✔
1618
}
1619

1620
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
312,163✔
1621
  int32_t code = 0;
312,163✔
1622

1623
  if (!numOfTables && !numOfCols) goto _exit;
312,163!
1624

1625
  SVnode *pVnodeObj = pVnode;
312,163✔
1626
  metaRLock(pVnodeObj->pMeta);
312,163✔
1627

1628
  // fast path: search cache
1629
  SMetaStbStats state = {0};
312,226✔
1630
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
312,226✔
1631
    metaULock(pVnodeObj->pMeta);
287,691✔
1632
    if (numOfTables) *numOfTables = state.ctbNum;
287,719✔
1633
    if (numOfCols) *numOfCols = state.colNum;
287,719✔
1634
    goto _exit;
287,719✔
1635
  }
1636

1637
  // slow path: search TDB
1638
  int64_t ctbNum = 0;
24,536✔
1639
  int32_t colNum = 0;
24,536✔
1640
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
24,536✔
1641
  if (TSDB_CODE_SUCCESS == code) {
24,537!
1642
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
24,537✔
1643
  }
1644
  metaULock(pVnodeObj->pMeta);
24,537✔
1645
  if (TSDB_CODE_SUCCESS != code) {
24,537!
1646
    goto _exit;
×
1647
  }
1648

1649
  if (numOfTables) *numOfTables = ctbNum;
24,537✔
1650
  if (numOfCols) *numOfCols = colNum;
24,537✔
1651

1652
  state.uid = uid;
24,537✔
1653
  state.ctbNum = ctbNum;
24,537✔
1654
  state.colNum = colNum;
24,537✔
1655

1656
  // upsert the cache
1657
  metaWLock(pVnodeObj->pMeta);
24,537✔
1658

1659
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
24,536✔
1660
  if (ret) {
24,535!
1661
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d", uid, ctbNum, colNum);
×
1662
  }
1663

1664
  metaULock(pVnodeObj->pMeta);
24,535✔
1665

1666
_exit:
312,238✔
1667
  return code;
312,238✔
1668
}
1669

1670
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol) {
188,200✔
1671
  SMetaStbStats stats = {0};
188,200✔
1672

1673
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
188,200✔
1674
    stats.ctbNum += deltaCtb;
182,820✔
1675
    stats.colNum += deltaCol;
182,820✔
1676
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
182,820✔
1677
    if (code) {
182,815!
1678
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d",
×
1679
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol);
1680
    }
1681
  }
1682
}
188,198✔
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