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

taosdata / TDengine / #4469

08 Jul 2025 09:38AM UTC coverage: 62.22% (-1.2%) from 63.381%
#4469

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

153678 of 316510 branches covered (48.55%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

5035 existing lines in 221 files now uncovered.

238955 of 314529 relevant lines covered (75.97%)

6273248.0 hits per line

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

57.62
/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) {
3,049,524✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
3,049,524✔
22
  metaReaderDoInit(pReader, pMeta, flags);
3,049,524✔
23
  pReader->pAPI = pAPI;
3,050,589✔
24
}
3,050,589✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
3,424,043✔
27
  memset(pReader, 0, sizeof(*pReader));
3,424,043✔
28
  pReader->pMeta = pMeta;
3,424,043✔
29
  pReader->flags = flags;
3,424,043✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
3,424,043✔
31
    metaRLock(pMeta);
2,894,568✔
32
  }
33
}
3,424,337✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,095,747✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,095,747!
37
    metaULock(pReader->pMeta);
1,096,264✔
38
    pReader->flags |= META_READER_NOLOCK;
1,097,327✔
39
  }
40
}
1,096,810✔
41

42
void metaReaderClear(SMetaReader *pReader) {
3,472,773✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
3,472,773✔
44
    metaULock(pReader->pMeta);
1,797,007✔
45
  }
46
  tDecoderClear(&pReader->coder);
3,472,488✔
47
  tdbFree(pReader->pBuf);
3,471,707✔
48
  pReader->pBuf = NULL;
3,473,246✔
49
}
3,473,246✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
4,360,662!
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);
4,360,224✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
4,358,700✔
65
  if (code) {
4,354,103!
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
4,354,103✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
528,633✔
84
  return true;
528,643✔
85
}
86

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

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

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

100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
2,882,910✔
101
  SMeta *pMeta = pReader->pMeta;
2,882,910✔
102

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
2,882,910✔
105
  if (TSDB_CODE_SUCCESS != code) {
2,883,514✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
114!
107
  }
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
2,883,400✔
110
}
111

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

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

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

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

130
  metaRLock(pMeta);
168,447✔
131

132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
168,479✔
133
    uid = *(tb_uid_t *)pData;
20,849✔
134
    tdbFree(pData);
20,849✔
135
  }
136

137
  metaULock(pMeta);
168,448✔
138

139
  return uid;
168,453✔
140
}
141

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

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

155
  return 0;
26,513✔
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) {
6,247✔
174
  int         code = 0;
6,247✔
175
  SMetaReader mr = {0};
6,247✔
176
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
6,247✔
177

178
  SMetaReader *pReader = &mr;
6,251✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
3,446✔
187

188
  metaReaderClear(&mr);
3,446✔
189

190
  return 0;
3,446✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
3,446✔
199
  if (code == 0) *tbType = mr.me.type;
3,443✔
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
3,443✔
201
    *suid = mr.me.ctbEntry.suid;
3,414✔
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
29✔
203
    *suid = mr.me.uid;
19✔
204
  } else {
205
    *suid = 0;
10✔
206
  }
207

208
  metaReaderClear(&mr);
3,443✔
209
  return code;
3,445✔
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) {
25,082✔
245
  SMTbCursor *pTbCur = NULL;
25,082✔
246
  int32_t     code;
247

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
25,082!
249
  if (pTbCur == NULL) {
25,093!
250
    return NULL;
×
251
  }
252

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
50,149✔
267
  if (pTbCur) {
50,149✔
268
    tdbFree(pTbCur->pKey);
25,098✔
269
    tdbFree(pTbCur->pVal);
25,097✔
270
    if (!pTbCur->paused) {
25,098✔
271
      metaReaderClear(&pTbCur->mr);
3,936✔
272
      if (pTbCur->pDbc) {
3,936!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
3,937✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
25,099!
277
  }
278
}
50,148✔
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
22,075✔
281
  if (!pTbCur->paused) {
22,075!
282
    metaReaderClear(&pTbCur->mr);
22,076✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
22,082✔
284
    pTbCur->paused = 1;
22,081✔
285
  }
286
}
22,080✔
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
26,012✔
288
  int32_t code = 0;
26,012✔
289
  int32_t lino;
290
  int8_t  locked = 0;
26,012✔
291
  if (pTbCur->paused) {
26,012!
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
26,014✔
293
    locked = 1;
26,019✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
26,019✔
295
    if (code != 0) {
26,008!
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
26,008✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
25,086✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
25,088!
302
    } else {
303
      int c = 1;
922✔
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
922✔
305
      TSDB_CHECK_CODE(code, lino, _exit);
922!
306
      if (c == 0) {
922!
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
922!
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;
26,010✔
318
  }
319

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

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
770,552✔
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);
770,552✔
334
    if (ret < 0) {
769,595✔
335
      return ret;
25,096✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
744,499✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
745,925✔
341
    if (ret) return ret;
743,477!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
743,477✔
344
      continue;
15,349✔
345
    }
346

347
    break;
728,128✔
348
  }
349

350
  return 0;
728,128✔
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, SExtSchema** extSchema) {
1,487,333✔
382
  void           *pData = NULL;
1,487,333✔
383
  int             nData = 0;
1,487,333✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
1,487,333✔
386
  SSchemaWrapper *pSchema = NULL;
1,487,333✔
387
  SDecoder        dc = {0};
1,487,333✔
388
  if (lock) {
1,487,333✔
389
    metaRLock(pMeta);
1,466,017✔
390
  }
391
_query:
1,489,597✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,569,079✔
393
    goto _err;
2,494✔
394
  }
395

396
  version = ((SUidIdxVal *)pData)[0].version;
1,566,687✔
397

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

402
  SMetaEntry me = {0};
1,567,110✔
403
  tDecoderInit(&dc, pData, nData);
1,567,110✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
1,566,326✔
405
  if (code) {
1,566,334!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
1,566,334✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
1,207,643✔
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
1,206,987✔
412
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
1,206,987✔
413
      tDecoderClear(&dc);
1,206,983✔
414
      goto _exit;
1,207,838✔
415
    }
416
  } else if (me.type == TSDB_CHILD_TABLE) {
358,691✔
417
    uid = me.ctbEntry.suid;
79,392✔
418
    tDecoderClear(&dc);
79,392✔
419
    goto _query;
79,482✔
420
  } else {
421
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
279,299!
422
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
279,425✔
423
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
279,425✔
424
      tDecoderClear(&dc);
279,432✔
425
      goto _exit;
279,439✔
426
    }
427
  }
428
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
51!
429
  tDecoderClear(&dc);
51✔
430

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

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

443
_exit:
1,487,409✔
444
  if (lock) {
1,487,409✔
445
    metaULock(pMeta);
1,462,892✔
446
  }
447
  tdbFree(pData);
1,488,708✔
448
  return pSchema;
1,487,734✔
449

450
_err:
2,494✔
451
  if (lock) {
2,494✔
452
    metaULock(pMeta);
2,488✔
453
  }
454
  tdbFree(pData);
2,500✔
455
  return NULL;
2,492✔
456
}
457

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

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

472
  version = ((SUidIdxVal *)pData)[0].version;
243✔
473

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

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

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

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

505
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
900,734!
506
  if (pCtbCur == NULL) {
902,977!
507
    return NULL;
×
508
  }
509

510
  pCtbCur->pMeta = pMeta;
902,977✔
511
  pCtbCur->suid = uid;
902,977✔
512
  pCtbCur->lock = lock;
902,977✔
513
  pCtbCur->paused = 1;
902,977✔
514

515
  ret = metaResumeCtbCursor(pCtbCur, 1);
902,977✔
516
  if (ret < 0) {
902,181!
517
    return NULL;
×
518
  }
519
  return pCtbCur;
902,181✔
520
}
521

522
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
902,529✔
523
  if (pCtbCur) {
902,529!
524
    if (!pCtbCur->paused) {
902,637✔
525
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
859,228!
526
      if (pCtbCur->pCur) {
859,626!
527
        tdbTbcClose(pCtbCur->pCur);
859,653✔
528
      }
529
    }
530
    tdbFree(pCtbCur->pKey);
902,824✔
531
    tdbFree(pCtbCur->pVal);
902,831✔
532
  }
533
  taosMemoryFree(pCtbCur);
902,937!
534
}
903,141✔
535

536
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,440✔
537
  if (!pCtbCur->paused) {
43,440!
538
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,458✔
539
    if (pCtbCur->lock) {
43,501!
540
      metaULock(pCtbCur->pMeta);
43,503✔
541
    }
542
    pCtbCur->paused = 1;
43,496✔
543
  }
544
}
43,478✔
545

546
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
901,638✔
547
  if (pCtbCur->paused) {
901,638!
548
    pCtbCur->paused = 0;
902,159✔
549

550
    if (pCtbCur->lock) {
902,159✔
551
      metaRLock(pCtbCur->pMeta);
879,999✔
552
    }
553
    int ret = 0;
902,188✔
554
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
902,188✔
555
    if (ret < 0) {
902,156!
556
      metaCloseCtbCursor(pCtbCur);
×
557
      return -1;
×
558
    }
559

560
    if (first) {
902,362!
561
      SCtbIdxKey ctbIdxKey;
562
      // move to the suid
563
      ctbIdxKey.suid = pCtbCur->suid;
902,362✔
564
      ctbIdxKey.uid = INT64_MIN;
902,362✔
565
      int c = 0;
902,362✔
566
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
902,362✔
567
      if (c > 0) {
902,362✔
568
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
211,435✔
569
      }
570
    } else {
571
      int c = 0;
×
572
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
573
      if (c < 0) {
×
574
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
575
      } else {
576
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
577
      }
578
    }
579
  }
580
  return 0;
901,723✔
581
}
582

583
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
4,211,776✔
584
  int         ret;
585
  SCtbIdxKey *pCtbIdxKey;
586

587
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
4,211,776✔
588
  if (ret < 0) {
4,211,788✔
589
    return 0;
694,022✔
590
  }
591

592
  pCtbIdxKey = pCtbCur->pKey;
3,517,766✔
593
  if (pCtbIdxKey->suid > pCtbCur->suid) {
3,517,766✔
594
    return 0;
209,543✔
595
  }
596

597
  return pCtbIdxKey->uid;
3,308,223✔
598
}
599

600
struct SMStbCursor {
601
  SMeta   *pMeta;
602
  TBC     *pCur;
603
  tb_uid_t suid;
604
  void    *pKey;
605
  void    *pVal;
606
  int      kLen;
607
  int      vLen;
608
};
609

610
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
136,455✔
611
  SMStbCursor *pStbCur = NULL;
136,455✔
612
  int          ret = 0;
136,455✔
613
  int          c = 0;
136,455✔
614

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

621
  pStbCur->pMeta = pMeta;
136,455✔
622
  pStbCur->suid = suid;
136,455✔
623
  metaRLock(pMeta);
136,455✔
624

625
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
136,455✔
626
  if (ret < 0) {
136,457!
627
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
628
    metaULock(pMeta);
×
629
    taosMemoryFree(pStbCur);
×
630
    return NULL;
×
631
  }
632

633
  // move to the suid
634
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
136,457✔
635
  if (c > 0) {
136,447!
636
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
637
  }
638

639
  return pStbCur;
136,447✔
640
}
641

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

648
      tdbFree(pStbCur->pKey);
136,454✔
649
      tdbFree(pStbCur->pVal);
136,454✔
650
    }
651

652
    taosMemoryFree(pStbCur);
136,453!
653
  }
654
}
136,457✔
655

656
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
293,913✔
657
  int ret;
658

659
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
293,913✔
660
  if (ret < 0) {
293,885✔
661
    return 0;
136,449✔
662
  }
663
  return *(tb_uid_t *)pStbCur->pKey;
157,436✔
664
}
665

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

670
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
1,340,297✔
671
  if (!pSW) return NULL;
1,342,595✔
672

673
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
1,342,529✔
674

675
  taosMemoryFree(pSW->pSchema);
1,342,859✔
676
  taosMemoryFree(pSW);
1,342,916!
677
  return pTSchema;
1,342,951✔
678
}
679

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

688
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,331,834✔
689
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,331,834✔
690
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
1,334,245✔
691
    return terrno;
3✔
692
  }
693
  return TSDB_CODE_SUCCESS;
1,334,242✔
694
}
695

696
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
1,317,164✔
697
  int32_t code = 0;
1,317,164✔
698
  int32_t lino;
699

700
  void     *pData = NULL;
1,317,164✔
701
  int       nData = 0;
1,317,164✔
702
  SSkmDbKey skmDbKey;
703
  if (sver <= 0) {
1,317,164✔
704
    SMetaInfo info;
705
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
628,899✔
706
      sver = info.skmVer;
628,824✔
707
    } else {
708
      TBC *pSkmDbC = NULL;
65✔
709
      int  c;
710

711
      skmDbKey.uid = suid ? suid : uid;
65!
712
      skmDbKey.sver = INT32_MAX;
65✔
713

714
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
65✔
715
      TSDB_CHECK_CODE(code, lino, _exit);
66!
716
      metaRLock(pMeta);
66✔
717

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

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

733
      if (c < 0) {
66✔
734
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
6✔
735
      }
736

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

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

748
      sver = ((SSkmDbKey *)pKey)->sver;
65✔
749

750
      metaULock(pMeta);
65✔
751
      tdbTbcClose(pSkmDbC);
66✔
752
    }
753
  }
754

755
  if (!(sver > 0)) {
1,317,133!
756
    code = TSDB_CODE_NOT_FOUND;
×
757
    goto _exit;
×
758
  }
759

760
  skmDbKey.uid = suid ? suid : uid;
1,317,133✔
761
  skmDbKey.sver = sver;
1,317,133✔
762
  metaRLock(pMeta);
1,317,133✔
763
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
1,317,332✔
764
    metaULock(pMeta);
6✔
765
    code = TSDB_CODE_NOT_FOUND;
6✔
766
    goto _exit;
6✔
767
  }
768
  metaULock(pMeta);
1,317,180✔
769

770
  // decode
771
  SDecoder        dc = {0};
1,317,244✔
772
  SSchemaWrapper  schema;
773
  SSchemaWrapper *pSchemaWrapper = &schema;
1,317,244✔
774

775
  tDecoderInit(&dc, pData, nData);
1,317,244✔
776
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
1,316,720✔
777
  tDecoderClear(&dc);
1,316,720✔
778
  tdbFree(pData);
1,317,050✔
779
  if (TSDB_CODE_SUCCESS != code) {
1,317,120!
780
    taosMemoryFree(pSchemaWrapper->pSchema);
×
781
    goto _exit;
×
782
  }
783

784
  // convert
785
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
1,317,120✔
786
  if (pTSchema == NULL) {
1,317,148!
787
    code = TSDB_CODE_OUT_OF_MEMORY;
×
788
  }
789

790
  *ppTSchema = pTSchema;
1,317,148✔
791
  taosMemoryFree(pSchemaWrapper->pSchema);
1,317,148!
792

793
_exit:
1,317,301✔
794
  return code;
1,317,301✔
795
}
796

797
// N.B. Called by statusReq per second
798
int64_t metaGetTbNum(SMeta *pMeta) {
255,963✔
799
  // num of child tables (excluding normal tables , stables and others)
800

801
  /* int64_t num = 0; */
802
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
803

804
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
255,963✔
805
}
806

807
void metaUpdTimeSeriesNum(SMeta *pMeta) {
136,447✔
808
  int64_t nCtbTimeSeries = 0;
136,447✔
809
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
136,447!
810
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
136,444✔
811
  }
812
}
136,446✔
813

814
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
815
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
816
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
434,983✔
817
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
435,033✔
818
    metaUpdTimeSeriesNum(pMeta);
136,397✔
819
  }
820

821
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
435,046✔
822
}
823

824
// type: 1 reported timeseries
825
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
434,983!
826
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
435,046✔
827
  if (type == 1) {
435,046✔
828
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
255,963✔
829
  }
830
  return nTimeSeries;
435,042✔
831
}
832

833
typedef struct {
834
  SMeta   *pMeta;
835
  TBC     *pCur;
836
  tb_uid_t uid;
837
  void    *pKey;
838
  void    *pVal;
839
  int      kLen;
840
  int      vLen;
841
} SMSmaCursor;
842

843
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
844
  SMSmaCursor *pSmaCur = NULL;
×
845
  SSmaIdxKey   smaIdxKey;
846
  int          ret;
847
  int          c;
848

849
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
850
  if (pSmaCur == NULL) {
×
851
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
852
    return NULL;
×
853
  }
854

855
  pSmaCur->pMeta = pMeta;
×
856
  pSmaCur->uid = uid;
×
857
  metaRLock(pMeta);
×
858

859
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
860
  if (ret < 0) {
×
861
    metaULock(pMeta);
×
862
    taosMemoryFree(pSmaCur);
×
863
    return NULL;
×
864
  }
865

866
  // move to the suid
867
  smaIdxKey.uid = uid;
×
868
  smaIdxKey.smaUid = INT64_MIN;
×
869
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
870
  if (c > 0) {
×
871
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
872
  }
873

874
  return pSmaCur;
×
875
}
876

877
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
878
  if (pSmaCur) {
×
879
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
880
    if (pSmaCur->pCur) {
×
881
      tdbTbcClose(pSmaCur->pCur);
×
882
      pSmaCur->pCur = NULL;
×
883

884
      tdbFree(pSmaCur->pKey);
×
885
      tdbFree(pSmaCur->pVal);
×
886
    }
887

888
    taosMemoryFree(pSmaCur);
×
889
  }
890
}
×
891

892
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
893
  int         ret;
894
  SSmaIdxKey *pSmaIdxKey;
895

896
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
897
  if (ret < 0) {
×
898
    return 0;
×
899
  }
900

901
  pSmaIdxKey = pSmaCur->pKey;
×
902
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
903
    return 0;
×
904
  }
905

906
  return pSmaIdxKey->uid;
×
907
}
908

909
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
910
  STSmaWrapper *pSW = NULL;
×
911
  SArray       *pSmaIds = NULL;
×
912

913
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
914
    return NULL;
×
915
  }
916

917
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
918
  if (!pSW) {
×
919
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
920
    goto _err;
×
921
  }
922

923
  pSW->number = taosArrayGetSize(pSmaIds);
×
924
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
925

926
  if (!pSW->tSma) {
×
927
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
928
    goto _err;
×
929
  }
930

931
  SMetaReader mr = {0};
×
932
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
933
  int64_t smaId;
934
  int     smaIdx = 0;
×
935
  STSma  *pTSma = NULL;
×
936
  for (int i = 0; i < pSW->number; ++i) {
×
937
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
938
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
939
      tDecoderClear(&mr.coder);
×
940
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
941
      continue;
×
942
    }
943
    tDecoderClear(&mr.coder);
×
944
    pTSma = pSW->tSma + smaIdx;
×
945
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
946
    if (deepCopy) {
×
947
      if (pTSma->exprLen > 0) {
×
948
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
949
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
950
          goto _err;
×
951
        }
952
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
953
      }
954
      if (pTSma->tagsFilterLen > 0) {
×
955
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
956
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
957
          goto _err;
×
958
        }
959
      }
960
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
961
    } else {
962
      pTSma->exprLen = 0;
×
963
      pTSma->expr = NULL;
×
964
      pTSma->tagsFilterLen = 0;
×
965
      pTSma->tagsFilter = NULL;
×
966
    }
967

968
    ++smaIdx;
×
969
  }
970

971
  if (smaIdx <= 0) goto _err;
×
972
  pSW->number = smaIdx;
×
973

974
  metaReaderClear(&mr);
×
975
  taosArrayDestroy(pSmaIds);
×
976
  return pSW;
×
977
_err:
×
978
  metaReaderClear(&mr);
×
979
  taosArrayDestroy(pSmaIds);
×
980
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
981
  return NULL;
×
982
}
983

984
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
×
985
  STSma      *pTSma = NULL;
×
986
  SMetaReader mr = {0};
×
987
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
988
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
×
989
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
990
    metaReaderClear(&mr);
×
991
    return NULL;
×
992
  }
993
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
×
994
  if (!pTSma) {
×
995
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
996
    metaReaderClear(&mr);
×
997
    return NULL;
×
998
  }
999

1000
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1001

1002
  metaReaderClear(&mr);
×
1003
  return pTSma;
×
1004
}
1005

1006
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1007
  SArray     *pUids = NULL;
×
1008
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1009

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

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

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

1030
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1031

1032
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1033
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1034
      metaCloseSmaCursor(pCur);
×
1035
      taosArrayDestroy(pUids);
×
1036
      return NULL;
×
1037
    }
1038
  }
1039

1040
  metaCloseSmaCursor(pCur);
×
1041
  return pUids;
×
1042
}
1043

1044
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1045
  SArray     *pUids = NULL;
×
1046
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1047
  tb_uid_t    lastUid = 0;
×
1048

1049
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1050
  if (!pCur) {
×
1051
    return NULL;
×
1052
  }
1053

1054
  while (1) {
×
1055
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1056
    if (uid == 0) {
×
1057
      break;
×
1058
    }
1059

1060
    if (lastUid == uid) {
×
1061
      continue;
×
1062
    }
1063

1064
    lastUid = uid;
×
1065

1066
    if (!pUids) {
×
1067
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1068
      if (!pUids) {
×
1069
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1070
        metaCloseSmaCursor(pCur);
×
1071
        return NULL;
×
1072
      }
1073
    }
1074

1075
    if (!taosArrayPush(pUids, &uid)) {
×
1076
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1077
      metaCloseSmaCursor(pCur);
×
1078
      taosArrayDestroy(pUids);
×
1079
      return NULL;
×
1080
    }
1081
  }
1082

1083
  metaCloseSmaCursor(pCur);
×
1084
  return pUids;
×
1085
}
1086

1087
#endif
1088

1089
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
6,086,874✔
1090
  STag *tag = (STag *)pTag;
6,086,874✔
1091
  if (type == TSDB_DATA_TYPE_JSON) {
6,086,874✔
1092
    return tag;
10,289✔
1093
  }
1094
  bool find = tTagGet(tag, val);
6,076,585✔
1095

1096
  if (!find) {
6,080,109✔
1097
    return NULL;
45,277✔
1098
  }
1099

1100
  return val;
6,034,832✔
1101
}
1102

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

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

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

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

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

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

1145
  int32_t valid = 0;
12✔
1146
  int32_t count = 0;
12✔
1147

1148
  static const int8_t TRY_ERROR_LIMIT = 1;
1149
  do {
75,074✔
1150
    void   *entryKey = NULL;
75,086✔
1151
    int32_t nEntryKey = -1;
75,086✔
1152
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
75,086✔
1153
    if (valid < 0) break;
75,593✔
1154

1155
    SBtimeIdxKey *p = entryKey;
75,581✔
1156
    if (count > TRY_ERROR_LIMIT) break;
75,581!
1157

1158
    terrno = TSDB_CODE_SUCCESS;
75,581✔
1159
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
75,509✔
1160
    if (terrno != TSDB_CODE_SUCCESS) {
75,293!
1161
      ret = terrno;
×
1162
      break;
×
1163
    }
1164
    if (cmp == 0) {
75,319!
1165
      if (taosArrayPush(pUids, &p->uid) == NULL) {
150,538!
1166
        ret = terrno;
×
1167
        break;
×
1168
      }
1169
    } else {
1170
      if (param->equal == true) {
×
1171
        if (count > TRY_ERROR_LIMIT) break;
×
1172
        count++;
×
1173
      }
1174
    }
1175
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
75,219✔
1176
    if (valid < 0) break;
75,074!
1177
  } while (1);
1178

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

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

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

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

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

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

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

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

1227
    if (count > TRY_ERROR_LIMIT) break;
×
1228

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

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

1260
  taosMemoryFree(pCursor);
×
1261

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

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

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

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

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

1292
  taosMemoryFree(pCursor);
×
1293

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

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

1308
  SDecoder    dc = {0};
7,835✔
1309
  STbDbKey    tbDbKey = {0};
7,835✔
1310
  STagIdxKey *pKey = NULL;
7,835✔
1311
  int32_t     nKey = 0;
7,835✔
1312

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

1323
  metaRLock(pMeta);
7,848✔
1324

1325
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
7,853!
1326

1327
  tbDbKey.uid = param->suid;
7,857✔
1328
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
7,857✔
1329

1330
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
7,857!
1331

1332
  tDecoderInit(&dc, pData, nData);
7,856✔
1333

1334
  code = metaDecodeEntry(&dc, &oStbEntry);
7,850✔
1335
  if (code) {
7,855!
1336
    tDecoderClear(&dc);
×
1337
    goto END;
×
1338
  }
1339

1340
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
7,855!
1341
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1342
  }
1343

1344
  code = TSDB_CODE_INVALID_PARA;
7,855✔
1345

1346
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
10,789✔
1347
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
10,759✔
1348
    if (IS_IDX_ON(schema)) {
10,759✔
1349
      if (schema->colId == param->cid && param->type == schema->type) {
10,562!
1350
        code = 0;
7,825✔
1351
        break;
7,825✔
1352
      }
1353
    }
1354
  }
1355

1356
  TAOS_CHECK_GOTO(code, NULL, END);
7,855✔
1357

1358
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
7,826✔
1359
  if (code != 0) {
7,829✔
1360
    TAOS_CHECK_GOTO(terrno, NULL, END);
2!
1361
  }
1362

1363
  int32_t maxSize = 0;
7,827✔
1364
  int32_t nTagData = 0;
7,827✔
1365
  void   *tagData = NULL;
7,827✔
1366

1367
  if (param->val == NULL) {
7,827!
1368
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1369
    goto END;
×
1370
  } else {
1371
    if (IS_VAR_DATA_TYPE(param->type)) {
7,827!
1372
      tagData = varDataVal(param->val);
386✔
1373
      nTagData = varDataLen(param->val);
386✔
1374

1375
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
386✔
1376
        maxSize = 4 * nTagData + 1;
105✔
1377
        buf = taosMemoryCalloc(1, maxSize);
105!
1378
        if (buf == NULL) {
105!
1379
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1380
        }
1381

1382
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
105!
1383
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1384
        }
1385

1386
        tagData = buf;
105✔
1387
        nTagData = maxSize;
105✔
1388
      }
1389
    } else {
1390
      tagData = param->val;
7,441✔
1391
      nTagData = tDataTypes[param->type].bytes;
7,441✔
1392
    }
1393
  }
1394

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

1399
  int cmp = 0;
7,825✔
1400
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
7,825!
1401

1402
  int     count = 0;
7,824✔
1403
  int32_t valid = 0;
7,824✔
1404
  bool    found = false;
7,824✔
1405

1406
  static const int8_t TRY_ERROR_LIMIT = 1;
1407

1408
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1409
  /// target:                        [suid, cid2, type2]
1410
  int diffCidCount = 0;
7,824✔
1411
  do {
84,123✔
1412
    void   *entryKey = NULL, *entryVal = NULL;
91,947✔
1413
    int32_t nEntryKey, nEntryVal;
1414

1415
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
91,947✔
1416
    if (valid < 0) {
91,681✔
1417
      break;
7,829✔
1418
    }
1419
    if (count > TRY_ERROR_LIMIT) {
87,359✔
1420
      break;
1,780✔
1421
    }
1422

1423
    STagIdxKey *p = entryKey;
85,579✔
1424
    if (p == NULL) break;
85,579!
1425

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

1440
    terrno = TSDB_CODE_SUCCESS;
83,522✔
1441
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
83,543✔
1442
    if (terrno != TSDB_CODE_SUCCESS) {
83,450!
1443
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1444
      break;
×
1445
    }
1446
    if (cmp == 0) {
83,490✔
1447
      // match
1448
      tb_uid_t tuid = 0;
75,114✔
1449
      if (IS_VAR_DATA_TYPE(pKey->type)) {
75,114!
1450
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
699✔
1451
      } else {
1452
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
74,415✔
1453
      }
1454
      if (taosArrayPush(pUids, &tuid) == NULL) {
75,422!
1455
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1456
      }
1457
      found = true;
75,422✔
1458
    } else {
1459
      if (param->equal == true) {
8,376✔
1460
        if (count > TRY_ERROR_LIMIT) break;
5,172!
1461
        count++;
5,172✔
1462
      }
1463
    }
1464
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
83,798✔
1465
    if (valid < 0) {
83,778!
1466
      code = valid;
×
1467
      break;
×
1468
    }
1469
  } while (1);
1470

1471
END:
7,858✔
1472
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
7,858✔
1473
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
7,860✔
1474
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
7,859!
1475
  tDecoderClear(&dc);
7,859✔
1476
  tdbFree(pData);
7,858✔
1477

1478
  taosMemoryFree(buf);
7,861!
1479
  taosMemoryFree(pKey);
7,859!
1480

1481
  taosMemoryFree(pCursor);
7,859!
1482

1483
  return code;
7,861✔
1484
}
1485

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

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

1498
  return ret;
71,448✔
1499
}
1500

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

1505
  int32_t isLock = false;
6,721✔
1506
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
6,721!
1507
  for (int i = 0; i < sz; i++) {
77,821✔
1508
    STUidTagInfo *p = taosArrayGet(uidList, i);
71,119✔
1509

1510
    if (i % LIMIT == 0) {
71,098✔
1511
      if (isLock) metaULock(pMeta);
5,157✔
1512

1513
      metaRLock(pMeta);
5,157✔
1514
      isLock = true;
5,158✔
1515
    }
1516

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

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

1539
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
87,232✔
1540
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
87,232✔
1541
  if (!pCur) {
87,446!
1542
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1543
  }
1544

1545
  // If len > 0 means there already have uids, and we only want the
1546
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1547
  // in the hash map, that may require a lot of memory
1548
  SHashObj *pSepecifiedUidMap = NULL;
87,446✔
1549
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
87,446✔
1550
  if (numOfElems > 0) {
87,406✔
1551
    pSepecifiedUidMap =
1552
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
40,511✔
1553
    for (int i = 0; i < numOfElems; i++) {
231,761✔
1554
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
191,227✔
1555
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
191,216✔
1556
      if (code) {
191,291!
1557
        metaCloseCtbCursor(pCur);
×
1558
        taosHashCleanup(pSepecifiedUidMap);
×
1559
        return code;
×
1560
      }
1561
    }
1562
  }
1563

1564
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
87,429✔
1565
    while (1) {
258,140✔
1566
      tb_uid_t uid = metaCtbCursorNext(pCur);
305,005✔
1567
      if (uid == 0) {
301,050✔
1568
        break;
46,971✔
1569
      }
1570

1571
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
254,079✔
1572
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
254,079!
1573
      if (!info.pTagVal) {
259,243!
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
259,243✔
1579
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
258,140!
1580
        taosMemoryFreeClear(info.pTagVal);
×
1581
        metaCloseCtbCursor(pCur);
×
1582
        taosHashCleanup(pSepecifiedUidMap);
×
1583
        return terrno;
×
1584
      }
1585
    }
1586
  } else {  // only the specified tables need to be added
1587
    while (1) {
238,186✔
1588
      tb_uid_t uid = metaCtbCursorNext(pCur);
278,750✔
1589
      if (uid == 0) {
279,734✔
1590
        break;
40,518✔
1591
      }
1592

1593
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
239,216✔
1594
      if (index == NULL) {
238,516✔
1595
        continue;
47,581✔
1596
      }
1597

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

1611
  taosHashCleanup(pSepecifiedUidMap);
87,489✔
1612
  metaCloseCtbCursor(pCur);
87,502✔
1613
  return TSDB_CODE_SUCCESS;
87,528✔
1614
}
1615

1616
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1617

1618
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
27,405,310✔
1619
  int32_t code = 0;
27,405,310✔
1620
  void   *pData = NULL;
27,405,310✔
1621
  int     nData = 0;
27,405,310✔
1622
  int     lock = 0;
27,405,310✔
1623

1624
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
27,405,310!
1625
    lock = 1;
2,884,018✔
1626
  }
1627

1628
  if (!lock) metaRLock(pMeta);
27,405,310✔
1629

1630
  // search cache
1631
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
27,409,185✔
1632
    if (!lock) metaULock(pMeta);
13,579,134✔
1633
    goto _exit;
13,579,220✔
1634
  }
1635

1636
  // search TDB
1637
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
13,832,344✔
1638
    // not found
1639
    if (!lock) metaULock(pMeta);
13,819,807✔
1640
    goto _exit;
13,819,830✔
1641
  }
1642

1643
  if (!lock) metaULock(pMeta);
12,875✔
1644

1645
  pInfo->uid = uid;
12,890✔
1646
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
12,890✔
1647
  pInfo->version = ((SUidIdxVal *)pData)->version;
12,890✔
1648
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
12,890✔
1649

1650
  if (lock) {
12,890✔
1651
    metaULock(pReader->pMeta);
2,572✔
1652
    // metaReaderReleaseLock(pReader);
1653
  }
1654
  // upsert the cache
1655
  metaWLock(pMeta);
12,892✔
1656
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
12,924✔
1657
  if (ret != 0) {
12,942!
1658
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1659
  }
1660
  metaULock(pMeta);
12,942✔
1661

1662
  if (lock) {
12,942✔
1663
    metaRLock(pReader->pMeta);
2,571✔
1664
  }
1665

1666
_exit:
10,371✔
1667
  tdbFree(pData);
27,411,994✔
1668
  return code;
27,410,582✔
1669
}
1670

1671
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
336,551✔
1672
  int32_t code = 0;
336,551✔
1673

1674
  if (!numOfTables && !numOfCols) goto _exit;
336,551!
1675

1676
  SVnode *pVnodeObj = pVnode;
336,551✔
1677
  metaRLock(pVnodeObj->pMeta);
336,551✔
1678

1679
  // fast path: search cache
1680
  SMetaStbStats state = {0};
336,641✔
1681
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
336,641✔
1682
    metaULock(pVnodeObj->pMeta);
313,753✔
1683
    if (numOfTables) *numOfTables = state.ctbNum;
313,760✔
1684
    if (numOfCols) *numOfCols = state.colNum;
313,760✔
1685
    if (flags) *flags = state.flags;
313,760✔
1686
    goto _exit;
313,760✔
1687
  }
1688

1689
  // slow path: search TDB
1690
  int64_t ctbNum = 0;
22,871✔
1691
  int32_t colNum = 0;
22,871✔
1692
  int64_t keep = 0;
22,871✔
1693
  int8_t  flag = 0;
22,871✔
1694

1695
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
22,871✔
1696
  if (TSDB_CODE_SUCCESS == code) {
22,872!
1697
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
22,874✔
1698
  }
1699
  if (TSDB_CODE_SUCCESS == code) {
22,872!
1700
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
22,874✔
1701
  }
1702
  metaULock(pVnodeObj->pMeta);
22,870✔
1703
  if (TSDB_CODE_SUCCESS != code) {
22,873!
1704
    goto _exit;
×
1705
  }
1706

1707
  if (numOfTables) *numOfTables = ctbNum;
22,873✔
1708
  if (numOfCols) *numOfCols = colNum;
22,873✔
1709
  if (flags) *flags = flag;
22,873✔
1710

1711
  state.uid = uid;
22,873✔
1712
  state.ctbNum = ctbNum;
22,873✔
1713
  state.colNum = colNum;
22,873✔
1714
  state.flags = flag;
22,873✔
1715
  state.keep = keep;
22,873✔
1716
  // upsert the cache
1717
  metaWLock(pVnodeObj->pMeta);
22,873✔
1718

1719
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
22,873✔
1720
  if (ret) {
22,873!
1721
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1722
              uid, ctbNum, colNum, keep, flag);
1723
  }
1724

1725
  metaULock(pVnodeObj->pMeta);
22,873✔
1726

1727
_exit:
336,595✔
1728
  return code;
336,595✔
1729
}
1730

1731
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
184,958✔
1732
  SMetaStbStats stats = {0};
184,958✔
1733

1734
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
184,958✔
1735
    stats.ctbNum += deltaCtb;
172,402✔
1736
    stats.colNum += deltaCol;
172,402✔
1737
    if (deltaKeep > 0) {
172,402!
1738
      stats.keep = deltaKeep;
×
1739
    }
1740

1741
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
172,402✔
1742
    if (code) {
172,395!
1743
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1744
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1745
    }
1746
  }
1747
}
185,031✔
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