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

taosdata / TDengine / #3611

12 Feb 2025 09:54AM UTC coverage: 63.456% (+8.7%) from 54.713%
#3611

push

travis-ci

web-flow
Merge pull request #29745 from taosdata/fix/TD33664-3.0

fix: --version show information check for 3.0

141323 of 286257 branches covered (49.37%)

Branch coverage included in aggregate %.

220096 of 283298 relevant lines covered (77.69%)

19188455.82 hits per line

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

55.41
/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) {
10,707,011✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
10,707,011✔
22
  metaReaderDoInit(pReader, pMeta, flags);
10,707,011✔
23
  pReader->pAPI = pAPI;
10,714,313✔
24
}
10,714,313✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
14,647,166✔
27
  memset(pReader, 0, sizeof(*pReader));
14,647,166✔
28
  pReader->pMeta = pMeta;
14,647,166✔
29
  pReader->flags = flags;
14,647,166✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
14,647,166!
31
    metaRLock(pMeta);
10,426,865✔
32
  }
33
}
14,649,240✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,149,745✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,149,745!
37
    metaULock(pReader->pMeta);
2,150,012✔
38
    pReader->flags |= META_READER_NOLOCK;
2,150,451✔
39
  }
40
}
2,150,184✔
41

42
void metaReaderClear(SMetaReader *pReader) {
15,354,953✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
15,354,953✔
44
    metaULock(pReader->pMeta);
8,277,697✔
45
  }
46
  tDecoderClear(&pReader->coder);
15,352,791✔
47
  tdbFree(pReader->pBuf);
15,365,870✔
48
  pReader->pBuf = NULL;
15,366,480✔
49
}
15,366,480✔
50

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

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

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

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
24,342,141✔
65
  if (code) {
24,143,372!
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
24,143,372✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
684,230✔
84
  return true;
684,242✔
85
}
86

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

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

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

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

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
7,541,249✔
105
  if (TSDB_CODE_SUCCESS != code) {
7,544,055✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
145!
107
  }
108

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

112
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
1,835,002✔
113
  SMeta   *pMeta = pReader->pMeta;
1,835,002✔
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,835,002✔
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
27,531✔
119
  }
120

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

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

130
  metaRLock(pMeta);
155,676✔
131

132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
155,756✔
133
    uid = *(tb_uid_t *)pData;
29,694✔
134
    tdbFree(pData);
29,694✔
135
  }
136

137
  metaULock(pMeta);
155,742✔
138

139
  return uid;
155,743✔
140
}
141

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

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

155
  return 0;
17,123✔
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
  tstrncpy(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) {
457,663✔
174
  int         code = 0;
457,663✔
175
  SMetaReader mr = {0};
457,663✔
176
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
457,663✔
177

178
  SMetaReader *pReader = &mr;
457,673✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
455,961✔
187

188
  metaReaderClear(&mr);
455,961✔
189

190
  return 0;
455,960✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
455,959✔
199
  if (code == 0) *tbType = mr.me.type;
455,958!
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
455,958✔
201
    *suid = mr.me.ctbEntry.suid;
455,937✔
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
21✔
203
    *suid = mr.me.uid;
11✔
204
  } else {
205
    *suid = 0;
10✔
206
  }
207

208
  metaReaderClear(&mr);
455,958✔
209
  return code;
455,961✔
210
}
211

212
int metaReadNext(SMetaReader *pReader) {
×
213
  SMeta *pMeta = pReader->pMeta;
×
214

215
  // TODO
216

217
  return 0;
×
218
}
219

220
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
221
  int         code = -1;
×
222
  SMetaReader mr = {0};
×
223
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
224
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
225
  if (code < 0) {
×
226
    goto _exit;
×
227
  }
228
  if (mr.me.type == TSDB_CHILD_TABLE) {
×
229
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
230
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
×
231
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
232
  } else {
233
    goto _exit;
×
234
  }
235

236
  code = 0;
×
237

238
_exit:
×
239
  metaReaderClear(&mr);
×
240
  return code;
×
241
}
242

243
#if 1  // ===================================================
244
SMTbCursor *metaOpenTbCursor(void *pVnode) {
917,594✔
245
  SMTbCursor *pTbCur = NULL;
917,594✔
246
  int32_t     code;
247

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
917,594!
249
  if (pTbCur == NULL) {
918,312!
250
    return NULL;
×
251
  }
252

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,840,167✔
267
  if (pTbCur) {
1,840,167✔
268
    tdbFree(pTbCur->pKey);
920,580✔
269
    tdbFree(pTbCur->pVal);
920,491✔
270
    if (!pTbCur->paused) {
920,555✔
271
      metaReaderClear(&pTbCur->mr);
76,160✔
272
      if (pTbCur->pDbc) {
76,152!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
76,155✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
920,548!
277
  }
278
}
1,840,237✔
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
844,966✔
281
  if (!pTbCur->paused) {
844,966!
282
    metaReaderClear(&pTbCur->mr);
845,218✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
845,330✔
284
    pTbCur->paused = 1;
845,115✔
285
  }
286
}
844,863✔
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
919,339✔
288
  int32_t code = 0;
919,339✔
289
  int32_t lino;
290
  int8_t  locked = 0;
919,339✔
291
  if (pTbCur->paused) {
919,339!
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
919,612✔
293
    locked = 1;
921,155✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
921,155✔
295
    if (code != 0) {
919,174!
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
919,174✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
918,358✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
919,262!
302
    } else {
303
      int c = 1;
816✔
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
816✔
305
      TSDB_CHECK_CODE(code, lino, _exit);
816!
306
      if (c == 0) {
816!
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
816!
308
      } else if (c < 0) {
×
309
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
310
        TSDB_CHECK_CODE(code, lino, _exit);
×
311
      } else {
312
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
313
        TSDB_CHECK_CODE(code, lino, _exit);
×
314
      }
315
    }
316

317
    pTbCur->paused = 0;
920,078✔
318
  }
319

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

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
11,614,928✔
328
  int    ret;
329
  void  *pBuf;
330
  STbCfg tbCfg;
331

332
  for (;;) {
333
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
11,614,928✔
334
    if (ret < 0) {
11,624,624✔
335
      return ret;
920,545✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
10,704,079✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
10,722,009✔
341
    if (ret) return ret;
10,690,958!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
10,690,958✔
344
      continue;
2,408,896✔
345
    }
346

347
    break;
8,282,062✔
348
  }
349

350
  return 0;
8,282,062✔
351
}
352

353
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
354
  int    ret;
355
  void  *pBuf;
356
  STbCfg tbCfg;
357

358
  for (;;) {
359
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
360
    if (ret < 0) {
×
361
      return -1;
×
362
    }
363

364
    tDecoderClear(&pTbCur->mr.coder);
×
365

366
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
367
    if (ret < 0) {
×
368
      return ret;
×
369
    }
370

371
    if (pTbCur->mr.me.type == jumpTableType) {
×
372
      continue;
×
373
    }
374

375
    break;
×
376
  }
377

378
  return 0;
×
379
}
380

381
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, int64_t *createTime) {
4,894,315✔
382
  void           *pData = NULL;
4,894,315✔
383
  int             nData = 0;
4,894,315✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
4,894,315✔
386
  SSchemaWrapper *pSchema = NULL;
4,894,315✔
387
  SDecoder        dc = {0};
4,894,315✔
388
  if (lock) {
4,894,315✔
389
    metaRLock(pMeta);
4,880,116✔
390
  }
391
_query:
4,908,201✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
4,988,218✔
393
    goto _err;
442✔
394
  }
395

396
  version = ((SUidIdxVal *)pData)[0].version;
4,991,635✔
397

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

402
  SMetaEntry me = {0};
4,993,794✔
403
  tDecoderInit(&dc, pData, nData);
4,993,794✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
4,988,246✔
405
  if (code) {
4,988,142!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
4,988,142✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,593,776!
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,592,188✔
412
      tDecoderClear(&dc);
4,592,188✔
413
      goto _exit;
4,597,113✔
414
    }
415
  } else if (me.type == TSDB_CHILD_TABLE) {
394,366✔
416
    uid = me.ctbEntry.suid;
79,997✔
417
    if (createTime != NULL){
79,997✔
418
      *createTime = me.ctbEntry.btime;
78,258✔
419
    }
420
    tDecoderClear(&dc);
79,997✔
421
    goto _query;
80,017✔
422
  } else {
423
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
314,369✔
424
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
314,403✔
425
      tDecoderClear(&dc);
314,403✔
426
      goto _exit;
314,412✔
427
    }
428
  }
429
  tDecoderClear(&dc);
×
430

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

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

443
_exit:
4,911,656✔
444
  if (lock) {
4,911,656✔
445
    metaULock(pMeta);
4,876,746✔
446
  }
447
  tdbFree(pData);
4,916,731✔
448
  return pSchema;
4,913,687✔
449

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

458
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
3,840,881✔
459
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
3,840,881✔
460
  SMCtbCursor *pCtbCur = NULL;
3,840,881✔
461
  SCtbIdxKey   ctbIdxKey;
462
  int          ret = 0;
3,840,881✔
463
  int          c = 0;
3,840,881✔
464

465
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,840,881!
466
  if (pCtbCur == NULL) {
3,852,865!
467
    return NULL;
×
468
  }
469

470
  pCtbCur->pMeta = pMeta;
3,852,865✔
471
  pCtbCur->suid = uid;
3,852,865✔
472
  pCtbCur->lock = lock;
3,852,865✔
473
  pCtbCur->paused = 1;
3,852,865✔
474

475
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,852,865✔
476
  if (ret < 0) {
3,848,398!
477
    return NULL;
×
478
  }
479
  return pCtbCur;
3,848,398✔
480
}
481

482
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,852,933✔
483
  if (pCtbCur) {
3,852,933!
484
    if (!pCtbCur->paused) {
3,853,193✔
485
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,810,090!
486
      if (pCtbCur->pCur) {
3,810,949!
487
        tdbTbcClose(pCtbCur->pCur);
3,810,960✔
488
      }
489
    }
490
    tdbFree(pCtbCur->pKey);
3,852,443✔
491
    tdbFree(pCtbCur->pVal);
3,852,038✔
492
  }
493
  taosMemoryFree(pCtbCur);
3,853,146!
494
}
3,854,073✔
495

496
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,287✔
497
  if (!pCtbCur->paused) {
43,287!
498
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,295✔
499
    if (pCtbCur->lock) {
43,316!
500
      metaULock(pCtbCur->pMeta);
43,318✔
501
    }
502
    pCtbCur->paused = 1;
43,323✔
503
  }
504
}
43,315✔
505

506
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,845,097✔
507
  if (pCtbCur->paused) {
3,845,097!
508
    pCtbCur->paused = 0;
3,848,032✔
509

510
    if (pCtbCur->lock) {
3,848,032✔
511
      metaRLock(pCtbCur->pMeta);
3,827,879✔
512
    }
513
    int ret = 0;
3,847,874✔
514
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,847,874✔
515
    if (ret < 0) {
3,849,045!
516
      metaCloseCtbCursor(pCtbCur);
×
517
      return -1;
×
518
    }
519

520
    if (first) {
3,850,110!
521
      SCtbIdxKey ctbIdxKey;
522
      // move to the suid
523
      ctbIdxKey.suid = pCtbCur->suid;
3,850,110✔
524
      ctbIdxKey.uid = INT64_MIN;
3,850,110✔
525
      int c = 0;
3,850,110✔
526
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,850,110✔
527
      if (c > 0) {
3,848,117✔
528
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
450,825✔
529
      }
530
    } else {
531
      int c = 0;
×
532
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
533
      if (c < 0) {
×
534
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
535
      } else {
536
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
537
      }
538
    }
539
  }
540
  return 0;
3,844,882✔
541
}
542

543
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
16,444,547✔
544
  int         ret;
545
  SCtbIdxKey *pCtbIdxKey;
546

547
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
16,444,547✔
548
  if (ret < 0) {
16,454,971✔
549
    return 0;
567,304✔
550
  }
551

552
  pCtbIdxKey = pCtbCur->pKey;
15,887,667✔
553
  if (pCtbIdxKey->suid > pCtbCur->suid) {
15,887,667✔
554
    return 0;
3,287,230✔
555
  }
556

557
  return pCtbIdxKey->uid;
12,600,437✔
558
}
559

560
struct SMStbCursor {
561
  SMeta   *pMeta;
562
  TBC     *pCur;
563
  tb_uid_t suid;
564
  void    *pKey;
565
  void    *pVal;
566
  int      kLen;
567
  int      vLen;
568
};
569

570
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
155,454✔
571
  SMStbCursor *pStbCur = NULL;
155,454✔
572
  int          ret = 0;
155,454✔
573
  int          c = 0;
155,454✔
574

575
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
155,454!
576
  if (pStbCur == NULL) {
155,454!
577
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
578
    return NULL;
×
579
  }
580

581
  pStbCur->pMeta = pMeta;
155,454✔
582
  pStbCur->suid = suid;
155,454✔
583
  metaRLock(pMeta);
155,454✔
584

585
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
155,454✔
586
  if (ret < 0) {
155,454!
587
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
588
    metaULock(pMeta);
×
589
    taosMemoryFree(pStbCur);
×
590
    return NULL;
×
591
  }
592

593
  // move to the suid
594
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
155,454✔
595
  if (c > 0) {
155,454!
596
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
597
  }
598

599
  return pStbCur;
155,454✔
600
}
601

602
void metaCloseStbCursor(SMStbCursor *pStbCur) {
155,454✔
603
  if (pStbCur) {
155,454!
604
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
155,454!
605
    if (pStbCur->pCur) {
155,454!
606
      tdbTbcClose(pStbCur->pCur);
155,454✔
607

608
      tdbFree(pStbCur->pKey);
155,454✔
609
      tdbFree(pStbCur->pVal);
155,454✔
610
    }
611

612
    taosMemoryFree(pStbCur);
155,454!
613
  }
614
}
155,454✔
615

616
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
276,125✔
617
  int ret;
618

619
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
276,125✔
620
  if (ret < 0) {
276,132✔
621
    return 0;
155,454✔
622
  }
623
  return *(tb_uid_t *)pStbCur->pKey;
120,678✔
624
}
625

626
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
4,749,344✔
627
  STSchema       *pTSchema = NULL;
4,749,344✔
628
  SSchemaWrapper *pSW = NULL;
4,749,344✔
629

630
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
4,749,344✔
631
  if (!pSW) return NULL;
4,763,440✔
632

633
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
4,763,438✔
634

635
  taosMemoryFree(pSW->pSchema);
4,766,420✔
636
  taosMemoryFree(pSW);
4,766,617!
637
  return pTSchema;
4,765,844✔
638
}
639

640
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
5,987✔
641
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
5,987✔
642
  if (*ppTSchema == NULL) {
6,014!
643
    return terrno;
×
644
  }
645
  return TSDB_CODE_SUCCESS;
6,015✔
646
}
647

648
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,744,193✔
649
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,744,193✔
650
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,758,978!
651
    return terrno;
7✔
652
  }
653
  return TSDB_CODE_SUCCESS;
4,758,971✔
654
}
655

656
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
446,699✔
657
  int32_t code = 0;
446,699✔
658
  int32_t lino;
659

660
  void     *pData = NULL;
446,699✔
661
  int       nData = 0;
446,699✔
662
  SSkmDbKey skmDbKey;
663
  if (sver <= 0) {
446,699✔
664
    SMetaInfo info;
665
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
215,901!
666
      sver = info.skmVer;
215,905✔
667
    } else {
668
      TBC *pSkmDbC = NULL;
×
669
      int  c;
670

671
      skmDbKey.uid = suid ? suid : uid;
×
672
      skmDbKey.sver = INT32_MAX;
×
673

674
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
×
675
      TSDB_CHECK_CODE(code, lino, _exit);
1!
676
      metaRLock(pMeta);
1✔
677

678
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
1!
679
        metaULock(pMeta);
×
680
        tdbTbcClose(pSkmDbC);
×
681
        code = TSDB_CODE_NOT_FOUND;
×
682
        goto _exit;
×
683
      }
684

685
      if (c == 0) {
1!
686
        metaULock(pMeta);
×
687
        tdbTbcClose(pSkmDbC);
×
688
        code = TSDB_CODE_FAILED;
×
689
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
690
        goto _exit;
×
691
      }
692

693
      if (c < 0) {
1!
694
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
695
      }
696

697
      const void *pKey = NULL;
1✔
698
      int32_t     nKey = 0;
1✔
699
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
1✔
700

701
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
1!
702
        metaULock(pMeta);
×
703
        tdbTbcClose(pSkmDbC);
×
704
        code = TSDB_CODE_NOT_FOUND;
×
705
        goto _exit;
×
706
      }
707

708
      sver = ((SSkmDbKey *)pKey)->sver;
1✔
709

710
      metaULock(pMeta);
1✔
711
      tdbTbcClose(pSkmDbC);
1✔
712
    }
713
  }
714

715
  if (!(sver > 0)) {
446,681!
716
    code = TSDB_CODE_NOT_FOUND;
×
717
    goto _exit;
×
718
  }
719

720
  skmDbKey.uid = suid ? suid : uid;
446,681✔
721
  skmDbKey.sver = sver;
446,681✔
722
  metaRLock(pMeta);
446,681✔
723
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
446,841!
724
    metaULock(pMeta);
×
725
    code = TSDB_CODE_NOT_FOUND;
×
726
    goto _exit;
×
727
  }
728
  metaULock(pMeta);
446,727✔
729

730
  // decode
731
  SDecoder        dc = {0};
446,790✔
732
  SSchemaWrapper  schema;
733
  SSchemaWrapper *pSchemaWrapper = &schema;
446,790✔
734

735
  tDecoderInit(&dc, pData, nData);
446,790✔
736
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
446,122✔
737
  tDecoderClear(&dc);
446,122✔
738
  tdbFree(pData);
446,637✔
739
  if (TSDB_CODE_SUCCESS != code) {
446,704!
740
    taosMemoryFree(pSchemaWrapper->pSchema);
×
741
    goto _exit;
×
742
  }
743

744
  // convert
745
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
446,704✔
746
  if (pTSchema == NULL) {
446,760!
747
    code = TSDB_CODE_OUT_OF_MEMORY;
×
748
  }
749

750
  *ppTSchema = pTSchema;
446,760✔
751
  taosMemoryFree(pSchemaWrapper->pSchema);
446,760✔
752

753
_exit:
446,822✔
754
  return code;
446,822✔
755
}
756

757
// N.B. Called by statusReq per second
758
int64_t metaGetTbNum(SMeta *pMeta) {
1,032,579✔
759
  // num of child tables (excluding normal tables , stables and others)
760

761
  /* int64_t num = 0; */
762
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
763

764
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
1,032,579✔
765
}
766

767
void metaUpdTimeSeriesNum(SMeta *pMeta) {
155,437✔
768
  int64_t nCtbTimeSeries = 0;
155,437✔
769
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
155,437!
770
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
155,437✔
771
  }
772
}
155,437✔
773

774
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
775
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
776
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,032,803✔
777
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,032,803✔
778
    metaUpdTimeSeriesNum(pMeta);
153,142✔
779
  }
780

781
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,032,803✔
782
}
783

784
// type: 1 reported timeseries
785
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,032,803!
786
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,032,803✔
787
  if (type == 1) {
1,032,803✔
788
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
1,032,579✔
789
  }
790
  return nTimeSeries;
1,032,803✔
791
}
792

793
typedef struct {
794
  SMeta   *pMeta;
795
  TBC     *pCur;
796
  tb_uid_t uid;
797
  void    *pKey;
798
  void    *pVal;
799
  int      kLen;
800
  int      vLen;
801
} SMSmaCursor;
802

803
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
804
  SMSmaCursor *pSmaCur = NULL;
×
805
  SSmaIdxKey   smaIdxKey;
806
  int          ret;
807
  int          c;
808

809
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
810
  if (pSmaCur == NULL) {
×
811
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
812
    return NULL;
×
813
  }
814

815
  pSmaCur->pMeta = pMeta;
×
816
  pSmaCur->uid = uid;
×
817
  metaRLock(pMeta);
×
818

819
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
820
  if (ret < 0) {
×
821
    metaULock(pMeta);
×
822
    taosMemoryFree(pSmaCur);
×
823
    return NULL;
×
824
  }
825

826
  // move to the suid
827
  smaIdxKey.uid = uid;
×
828
  smaIdxKey.smaUid = INT64_MIN;
×
829
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
830
  if (c > 0) {
×
831
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
832
  }
833

834
  return pSmaCur;
×
835
}
836

837
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
838
  if (pSmaCur) {
×
839
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
840
    if (pSmaCur->pCur) {
×
841
      tdbTbcClose(pSmaCur->pCur);
×
842
      pSmaCur->pCur = NULL;
×
843

844
      tdbFree(pSmaCur->pKey);
×
845
      tdbFree(pSmaCur->pVal);
×
846
    }
847

848
    taosMemoryFree(pSmaCur);
×
849
  }
850
}
×
851

852
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
853
  int         ret;
854
  SSmaIdxKey *pSmaIdxKey;
855

856
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
857
  if (ret < 0) {
×
858
    return 0;
×
859
  }
860

861
  pSmaIdxKey = pSmaCur->pKey;
×
862
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
863
    return 0;
×
864
  }
865

866
  return pSmaIdxKey->uid;
×
867
}
868

869
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
870
  STSmaWrapper *pSW = NULL;
×
871
  SArray       *pSmaIds = NULL;
×
872

873
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
874
    return NULL;
×
875
  }
876

877
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
878
  if (!pSW) {
×
879
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
880
    goto _err;
×
881
  }
882

883
  pSW->number = taosArrayGetSize(pSmaIds);
×
884
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
885

886
  if (!pSW->tSma) {
×
887
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
888
    goto _err;
×
889
  }
890

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

928
    ++smaIdx;
×
929
  }
930

931
  if (smaIdx <= 0) goto _err;
×
932
  pSW->number = smaIdx;
×
933

934
  metaReaderClear(&mr);
×
935
  taosArrayDestroy(pSmaIds);
×
936
  return pSW;
×
937
_err:
×
938
  metaReaderClear(&mr);
×
939
  taosArrayDestroy(pSmaIds);
×
940
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
941
  return NULL;
×
942
}
943

944
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
3✔
945
  STSma      *pTSma = NULL;
3✔
946
  SMetaReader mr = {0};
3✔
947
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
3✔
948
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
3!
949
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
950
    metaReaderClear(&mr);
×
951
    return NULL;
×
952
  }
953
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
3!
954
  if (!pTSma) {
3!
955
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
956
    metaReaderClear(&mr);
×
957
    return NULL;
×
958
  }
959

960
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
3✔
961

962
  metaReaderClear(&mr);
3✔
963
  return pTSma;
3✔
964
}
965

966
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
967
  SArray     *pUids = NULL;
×
968
  SSmaIdxKey *pSmaIdxKey = NULL;
×
969

970
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
971
  if (!pCur) {
×
972
    return NULL;
×
973
  }
974

975
  while (1) {
×
976
    tb_uid_t id = metaSmaCursorNext(pCur);
×
977
    if (id == 0) {
×
978
      break;
×
979
    }
980

981
    if (!pUids) {
×
982
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
983
      if (!pUids) {
×
984
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
985
        metaCloseSmaCursor(pCur);
×
986
        return NULL;
×
987
      }
988
    }
989

990
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
991

992
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
993
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
994
      metaCloseSmaCursor(pCur);
×
995
      taosArrayDestroy(pUids);
×
996
      return NULL;
×
997
    }
998
  }
999

1000
  metaCloseSmaCursor(pCur);
×
1001
  return pUids;
×
1002
}
1003

1004
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1005
  SArray     *pUids = NULL;
×
1006
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1007
  tb_uid_t    lastUid = 0;
×
1008

1009
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1010
  if (!pCur) {
×
1011
    return NULL;
×
1012
  }
1013

1014
  while (1) {
×
1015
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1016
    if (uid == 0) {
×
1017
      break;
×
1018
    }
1019

1020
    if (lastUid == uid) {
×
1021
      continue;
×
1022
    }
1023

1024
    lastUid = uid;
×
1025

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

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

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

1047
#endif
1048

1049
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
20,150,677✔
1050
  STag *tag = (STag *)pTag;
20,150,677✔
1051
  if (type == TSDB_DATA_TYPE_JSON) {
20,150,677✔
1052
    return tag;
10,027✔
1053
  }
1054
  bool find = tTagGet(tag, val);
20,140,650✔
1055

1056
  if (!find) {
20,184,508✔
1057
    return NULL;
119,674✔
1058
  }
1059

1060
  return val;
20,064,834✔
1061
}
1062

1063
typedef struct {
1064
  SMeta   *pMeta;
1065
  TBC     *pCur;
1066
  tb_uid_t suid;
1067
  int16_t  cid;
1068
  int16_t  type;
1069
  void    *pKey;
1070
  void    *pVal;
1071
  int32_t  kLen;
1072
  int32_t  vLen;
1073
} SIdxCursor;
1074

1075
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1076
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1077
  SMetaFltParam *param = arg;
6✔
1078
  int32_t        ret = 0;
6✔
1079

1080
  SIdxCursor *pCursor = NULL;
6✔
1081
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
6!
1082
  if (pCursor == NULL) {
6!
1083
    return terrno;
×
1084
  }
1085
  pCursor->pMeta = pMeta;
6✔
1086
  pCursor->suid = param->suid;
6✔
1087
  pCursor->cid = param->cid;
6✔
1088
  pCursor->type = param->type;
6✔
1089

1090
  metaRLock(pMeta);
6✔
1091
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
6✔
1092
  if (ret != 0) {
6!
1093
    goto END;
×
1094
  }
1095
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
6✔
1096

1097
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
6✔
1098
  SBtimeIdxKey *pBtimeKey = &btimeKey;
6✔
1099

1100
  int cmp = 0;
6✔
1101
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
6!
1102
    goto END;
×
1103
  }
1104

1105
  int32_t valid = 0;
6✔
1106
  int32_t count = 0;
6✔
1107

1108
  static const int8_t TRY_ERROR_LIMIT = 1;
1109
  do {
32,362✔
1110
    void   *entryKey = NULL;
32,368✔
1111
    int32_t nEntryKey = -1;
32,368✔
1112
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
32,368✔
1113
    if (valid < 0) break;
32,121✔
1114

1115
    SBtimeIdxKey *p = entryKey;
32,115✔
1116
    if (count > TRY_ERROR_LIMIT) break;
32,115!
1117

1118
    terrno = TSDB_CODE_SUCCESS;
32,115✔
1119
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
32,172✔
1120
    if (terrno != TSDB_CODE_SUCCESS) {
32,185!
1121
      ret = terrno;
×
1122
      break;
×
1123
    }
1124
    if (cmp == 0) {
32,181!
1125
      if (taosArrayPush(pUids, &p->uid) == NULL) {
64,487!
1126
        ret = terrno;
×
1127
        break;
×
1128
      }
1129
    } else {
1130
      if (param->equal == true) {
×
1131
        if (count > TRY_ERROR_LIMIT) break;
×
1132
        count++;
×
1133
      }
1134
    }
1135
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
32,306✔
1136
    if (valid < 0) break;
32,362!
1137
  } while (1);
1138

1139
END:
6✔
1140
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1141
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1142
  taosMemoryFree(pCursor);
6!
1143
  return ret;
6✔
1144
}
1145

1146
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1147
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1148
  SMetaFltParam *param = arg;
×
1149
  int32_t        ret = 0;
×
1150
  char          *buf = NULL;
×
1151

1152
  STagIdxKey *pKey = NULL;
×
1153
  int32_t     nKey = 0;
×
1154

1155
  SIdxCursor *pCursor = NULL;
×
1156
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1157
  if (pCursor == NULL) {
×
1158
    return terrno;
×
1159
  }
1160
  pCursor->pMeta = pMeta;
×
1161
  pCursor->suid = param->suid;
×
1162
  pCursor->cid = param->cid;
×
1163
  pCursor->type = param->type;
×
1164

1165
  char *pName = param->val;
×
1166

1167
  metaRLock(pMeta);
×
1168
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1169
  if (ret != 0) {
×
1170
    goto END;
×
1171
  }
1172

1173
  int cmp = 0;
×
1174
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1175
    goto END;
×
1176
  }
1177
  int32_t valid = 0;
×
1178
  int32_t count = 0;
×
1179

1180
  int32_t TRY_ERROR_LIMIT = 1;
×
1181
  do {
×
1182
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1183
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1184
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1185
    if (valid < 0) break;
×
1186

1187
    if (count > TRY_ERROR_LIMIT) break;
×
1188

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

1214
END:
×
1215
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1216
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1217
  taosMemoryFree(buf);
×
1218
  taosMemoryFree(pKey);
×
1219

1220
  taosMemoryFree(pCursor);
×
1221

1222
  return ret;
×
1223
}
1224
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1225
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1226
  SMetaFltParam *param = arg;
×
1227
  int32_t        ret = 0;
×
1228
  char          *buf = NULL;
×
1229

1230
  STtlIdxKey *pKey = NULL;
×
1231
  int32_t     nKey = 0;
×
1232

1233
  SIdxCursor *pCursor = NULL;
×
1234
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1235
  if (pCursor == NULL) {
×
1236
    return terrno;
×
1237
  }
1238
  pCursor->pMeta = pMeta;
×
1239
  pCursor->suid = param->suid;
×
1240
  pCursor->cid = param->cid;
×
1241
  pCursor->type = param->type;
×
1242

1243
  metaRLock(pMeta);
×
1244
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1245

1246
END:
×
1247
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1248
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1249
  taosMemoryFree(buf);
×
1250
  taosMemoryFree(pKey);
×
1251

1252
  taosMemoryFree(pCursor);
×
1253

1254
  return ret;
×
1255
  // impl later
1256
  return 0;
1257
}
1258
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
12,274✔
1259
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
12,274✔
1260
  SMetaFltParam *param = arg;
12,274✔
1261

1262
  SMetaEntry oStbEntry = {0};
12,274✔
1263
  int32_t    code = 0;
12,274✔
1264
  char      *buf = NULL;
12,274✔
1265
  void      *pData = NULL;
12,274✔
1266
  int        nData = 0;
12,274✔
1267

1268
  SDecoder    dc = {0};
12,274✔
1269
  STbDbKey    tbDbKey = {0};
12,274✔
1270
  STagIdxKey *pKey = NULL;
12,274✔
1271
  int32_t     nKey = 0;
12,274✔
1272

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

1283
  metaRLock(pMeta);
12,275✔
1284

1285
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
12,280!
1286

1287
  tbDbKey.uid = param->suid;
12,277✔
1288
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
12,277✔
1289

1290
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
12,277!
1291

1292
  tDecoderInit(&dc, pData, nData);
12,279✔
1293

1294
  code = metaDecodeEntry(&dc, &oStbEntry);
12,277✔
1295
  if (code) {
12,279!
1296
    tDecoderClear(&dc);
×
1297
    goto END;
×
1298
  }
1299

1300
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
12,279!
1301
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1302
  }
1303

1304
  code = TSDB_CODE_INVALID_PARA;
12,279✔
1305
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
13,496✔
1306
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
13,074✔
1307
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
13,074!
1308
      code = 0;
421✔
1309
    } else {
1310
      TAOS_CHECK_GOTO(code, NULL, END);
12,653✔
1311
    }
1312
  }
1313

1314
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
422✔
1315
  if (code != 0) {
421!
1316
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1317
  }
1318

1319
  int32_t maxSize = 0;
421✔
1320
  int32_t nTagData = 0;
421✔
1321
  void   *tagData = NULL;
421✔
1322

1323
  if (param->val == NULL) {
421!
1324
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1325
    goto END;
×
1326
  } else {
1327
    if (IS_VAR_DATA_TYPE(param->type)) {
421!
1328
      tagData = varDataVal(param->val);
×
1329
      nTagData = varDataLen(param->val);
×
1330

1331
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
×
1332
        maxSize = 4 * nTagData + 1;
×
1333
        buf = taosMemoryCalloc(1, maxSize);
×
1334
        if (buf == NULL) {
×
1335
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1336
        }
1337

1338
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
×
1339
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1340
        }
1341

1342
        tagData = buf;
×
1343
        nTagData = maxSize;
×
1344
      }
1345
    } else {
1346
      tagData = param->val;
421✔
1347
      nTagData = tDataTypes[param->type].bytes;
421✔
1348
    }
1349
  }
1350

1351
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
421!
1352
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1353
                  NULL, END);
1354

1355
  int cmp = 0;
421✔
1356
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
421!
1357

1358
  int     count = 0;
422✔
1359
  int32_t valid = 0;
422✔
1360
  bool    found = false;
422✔
1361

1362
  static const int8_t TRY_ERROR_LIMIT = 1;
1363

1364
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1365
  /// target:                        [suid, cid2, type2]
1366
  int diffCidCount = 0;
422✔
1367
  do {
10,433✔
1368
    void   *entryKey = NULL, *entryVal = NULL;
10,855✔
1369
    int32_t nEntryKey, nEntryVal;
1370

1371
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
10,855✔
1372
    if (valid < 0) {
10,853✔
1373
      code = valid;
350✔
1374
    }
1375
    if (count > TRY_ERROR_LIMIT) {
10,853✔
1376
      break;
422✔
1377
    }
1378

1379
    STagIdxKey *p = entryKey;
10,828✔
1380
    if (p == NULL) break;
10,828✔
1381

1382
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
10,483!
1383
      if (found == true) break;  //
61✔
1384
      if (diffCidCount > TRY_ERROR_LIMIT) break;
9!
1385
      diffCidCount++;
9✔
1386
      count++;
9✔
1387
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
9!
1388
      if (valid < 0) {
10!
1389
        code = valid;
×
1390
        break;
×
1391
      } else {
1392
        continue;
10✔
1393
      }
1394
    }
1395

1396
    terrno = TSDB_CODE_SUCCESS;
10,422✔
1397
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
10,423✔
1398
    if (terrno != TSDB_CODE_SUCCESS) {
10,421!
1399
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1400
      break;
×
1401
    }
1402
    if (cmp == 0) {
10,425✔
1403
      // match
1404
      tb_uid_t tuid = 0;
9,837✔
1405
      if (IS_VAR_DATA_TYPE(pKey->type)) {
9,837!
1406
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
×
1407
      } else {
1408
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
9,837✔
1409
      }
1410
      if (taosArrayPush(pUids, &tuid) == NULL) {
9,842!
1411
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1412
      }
1413
      found = true;
9,842✔
1414
    } else {
1415
      if (param->equal == true) {
588✔
1416
        if (count > TRY_ERROR_LIMIT) break;
105!
1417
        count++;
105✔
1418
      }
1419
    }
1420
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
10,430✔
1421
    if (valid < 0) {
10,423!
1422
      code = valid;
×
1423
      break;
×
1424
    }
1425
  } while (1);
1426

1427
END:
12,279✔
1428
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
12,279!
1429
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
12,280✔
1430
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
12,280!
1431
  tDecoderClear(&dc);
12,280✔
1432
  tdbFree(pData);
12,280✔
1433

1434
  taosMemoryFree(buf);
12,280!
1435
  taosMemoryFree(pKey);
12,280!
1436

1437
  taosMemoryFree(pCursor);
12,280!
1438

1439
  return code;
12,280✔
1440
}
1441

1442
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
268,306✔
1443
  int ret = 0;
268,306✔
1444
  if (lock) {
268,306!
1445
    metaRLock(pMeta);
×
1446
  }
1447

1448
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
268,306✔
1449
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
268,306✔
1450
  if (lock) {
268,307!
1451
    metaULock(pMeta);
×
1452
  }
1453

1454
  return ret;
268,307✔
1455
}
1456

1457
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
267,984✔
1458
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
267,984✔
1459
  const int32_t LIMIT = 128;
267,984✔
1460

1461
  int32_t isLock = false;
267,984✔
1462
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
267,984!
1463
  for (int i = 0; i < sz; i++) {
536,289✔
1464
    STUidTagInfo *p = taosArrayGet(uidList, i);
268,306✔
1465

1466
    if (i % LIMIT == 0) {
268,306✔
1467
      if (isLock) metaULock(pMeta);
267,970!
1468

1469
      metaRLock(pMeta);
267,970✔
1470
      isLock = true;
267,970✔
1471
    }
1472

1473
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1474
    void   *val = NULL;
268,306✔
1475
    int32_t len = 0;
268,306✔
1476
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
268,306!
1477
      p->pTagVal = taosMemoryMalloc(len);
268,307!
1478
      if (!p->pTagVal) {
268,306!
1479
        if (isLock) metaULock(pMeta);
×
1480

1481
        TAOS_RETURN(terrno);
×
1482
      }
1483
      memcpy(p->pTagVal, val, len);
268,306✔
1484
      tdbFree(val);
268,306✔
1485
    } else {
1486
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64 "", TD_VID(pMeta->pVnode), suid,
×
1487
                p->uid);
1488
    }
1489
  }
1490
  //  }
1491
  if (isLock) metaULock(pMeta);
267,983✔
1492
  return 0;
267,984✔
1493
}
1494

1495
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
1,013,906✔
1496
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
1,013,906✔
1497
  if (!pCur) {
1,016,411!
1498
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1499
  }
1500

1501
  // If len > 0 means there already have uids, and we only want the
1502
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1503
  // in the hash map, that may require a lot of memory
1504
  SHashObj *pSepecifiedUidMap = NULL;
1,016,411✔
1505
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
1,016,411✔
1506
  if (numOfElems > 0) {
1,014,190✔
1507
    pSepecifiedUidMap =
1508
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
94,505✔
1509
    for (int i = 0; i < numOfElems; i++) {
376,357✔
1510
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
281,769✔
1511
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
281,699✔
1512
      if (code) {
281,846!
1513
        metaCloseCtbCursor(pCur);
×
1514
        taosHashCleanup(pSepecifiedUidMap);
×
1515
        return code;
×
1516
      }
1517
    }
1518
  }
1519

1520
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
1,014,273✔
1521
    while (1) {
2,749,432✔
1522
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,668,926✔
1523
      if (uid == 0) {
3,668,991✔
1524
        break;
923,280✔
1525
      }
1526

1527
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,745,711✔
1528
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,745,711!
1529
      if (!info.pTagVal) {
2,751,924!
1530
        metaCloseCtbCursor(pCur);
×
1531
        taosHashCleanup(pSepecifiedUidMap);
×
1532
        return terrno;
×
1533
      }
1534
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,751,924✔
1535
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,749,432!
1536
        taosMemoryFreeClear(info.pTagVal);
×
1537
        metaCloseCtbCursor(pCur);
×
1538
        taosHashCleanup(pSepecifiedUidMap);
×
1539
        return terrno;
×
1540
      }
1541
    }
1542
  } else {  // only the specified tables need to be added
1543
    while (1) {
390,273✔
1544
      tb_uid_t uid = metaCtbCursorNext(pCur);
485,052✔
1545
      if (uid == 0) {
485,294✔
1546
        break;
94,580✔
1547
      }
1548

1549
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
390,714✔
1550
      if (index == NULL) {
390,760✔
1551
        continue;
109,302✔
1552
      }
1553

1554
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
281,458✔
1555
      if (pTagInfo->pTagVal == NULL) {
281,437!
1556
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
281,930✔
1557
        if (!pTagInfo->pTagVal) {
281,464!
1558
          metaCloseCtbCursor(pCur);
×
1559
          taosHashCleanup(pSepecifiedUidMap);
×
1560
          return terrno;
×
1561
        }
1562
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
281,464✔
1563
      }
1564
    }
1565
  }
1566

1567
  taosHashCleanup(pSepecifiedUidMap);
1,017,860✔
1568
  metaCloseCtbCursor(pCur);
1,017,630✔
1569
  return TSDB_CODE_SUCCESS;
1,017,821✔
1570
}
1571

1572
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1573

1574
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
35,342,779✔
1575
  int32_t code = 0;
35,342,779✔
1576
  void   *pData = NULL;
35,342,779✔
1577
  int     nData = 0;
35,342,779✔
1578
  int     lock = 0;
35,342,779✔
1579

1580
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
35,342,779!
1581
    lock = 1;
7,544,567✔
1582
  }
1583

1584
  if (!lock) metaRLock(pMeta);
35,342,779✔
1585

1586
  // search cache
1587
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
35,341,768✔
1588
    if (!lock) metaULock(pMeta);
34,957,598✔
1589
    goto _exit;
34,964,880✔
1590
  }
1591

1592
  // search TDB
1593
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
387,245✔
1594
    // not found
1595
    if (!lock) metaULock(pMeta);
292,295✔
1596
    goto _exit;
292,282✔
1597
  }
1598

1599
  if (!lock) metaULock(pMeta);
95,135✔
1600

1601
  pInfo->uid = uid;
95,135✔
1602
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
95,135✔
1603
  pInfo->version = ((SUidIdxVal *)pData)->version;
95,135✔
1604
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
95,135✔
1605

1606
  if (lock) {
95,135✔
1607
    metaULock(pReader->pMeta);
38,846✔
1608
    // metaReaderReleaseLock(pReader);
1609
  }
1610
  // upsert the cache
1611
  metaWLock(pMeta);
95,135✔
1612
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
95,133✔
1613
  if (ret != 0) {
95,129!
1614
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1615
  }
1616
  metaULock(pMeta);
95,129✔
1617

1618
  if (lock) {
95,133✔
1619
    metaRLock(pReader->pMeta);
38,846✔
1620
  }
1621

1622
_exit:
56,287✔
1623
  tdbFree(pData);
35,352,295✔
1624
  return code;
35,350,519✔
1625
}
1626

1627
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
343,273✔
1628
  int32_t code = 0;
343,273✔
1629

1630
  if (!numOfTables && !numOfCols) goto _exit;
343,273!
1631

1632
  SVnode *pVnodeObj = pVnode;
343,273✔
1633
  metaRLock(pVnodeObj->pMeta);
343,273✔
1634

1635
  // fast path: search cache
1636
  SMetaStbStats state = {0};
343,301✔
1637
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
343,301✔
1638
    metaULock(pVnodeObj->pMeta);
318,376✔
1639
    if (numOfTables) *numOfTables = state.ctbNum;
318,394✔
1640
    if (numOfCols) *numOfCols = state.colNum;
318,394!
1641
    goto _exit;
318,394✔
1642
  }
1643

1644
  // slow path: search TDB
1645
  int64_t ctbNum = 0;
24,906✔
1646
  int32_t colNum = 0;
24,906✔
1647
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
24,906✔
1648
  if (TSDB_CODE_SUCCESS == code) {
24,900!
1649
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
24,901✔
1650
  }
1651
  metaULock(pVnodeObj->pMeta);
24,904✔
1652
  if (TSDB_CODE_SUCCESS != code) {
24,907!
1653
    goto _exit;
×
1654
  }
1655

1656
  if (numOfTables) *numOfTables = ctbNum;
24,907✔
1657
  if (numOfCols) *numOfCols = colNum;
24,907!
1658

1659
  state.uid = uid;
24,907✔
1660
  state.ctbNum = ctbNum;
24,907✔
1661
  state.colNum = colNum;
24,907✔
1662

1663
  // upsert the cache
1664
  metaWLock(pVnodeObj->pMeta);
24,907✔
1665

1666
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
24,907✔
1667
  if (ret) {
24,905!
1668
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d", uid, ctbNum, colNum);
×
1669
  }
1670

1671
  metaULock(pVnodeObj->pMeta);
24,905✔
1672

1673
_exit:
343,295✔
1674
  return code;
343,295✔
1675
}
1676

1677
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol) {
227,737✔
1678
  SMetaStbStats stats = {0};
227,737✔
1679

1680
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
227,737✔
1681
    stats.ctbNum += deltaCtb;
211,670✔
1682
    stats.colNum += deltaCol;
211,670✔
1683
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
211,670✔
1684
    if (code) {
211,672!
1685
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d",
×
1686
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol);
1687
    }
1688
  }
1689
}
227,772✔
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