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

taosdata / TDengine / #3630

06 Mar 2025 11:35AM UTC coverage: 63.629% (-0.06%) from 63.692%
#3630

push

travis-ci

web-flow
Merge pull request #30042 from taosdata/doc/internal

docs: format

149060 of 300532 branches covered (49.6%)

Branch coverage included in aggregate %.

233739 of 301077 relevant lines covered (77.63%)

17473135.72 hits per line

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

56.39
/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,661,795✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
10,661,795✔
22
  metaReaderDoInit(pReader, pMeta, flags);
10,661,795✔
23
  pReader->pAPI = pAPI;
10,670,417✔
24
}
10,670,417✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
14,500,507✔
27
  memset(pReader, 0, sizeof(*pReader));
14,500,507✔
28
  pReader->pMeta = pMeta;
14,500,507✔
29
  pReader->flags = flags;
14,500,507✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
14,500,507!
31
    metaRLock(pMeta);
10,125,950✔
32
  }
33
}
14,504,012✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,932,960✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,932,960!
37
    metaULock(pReader->pMeta);
1,933,253✔
38
    pReader->flags |= META_READER_NOLOCK;
1,934,011✔
39
  }
40
}
1,933,718✔
41

42
void metaReaderClear(SMetaReader *pReader) {
15,172,375✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
15,172,375✔
44
    metaULock(pReader->pMeta);
8,193,055✔
45
  }
46
  tDecoderClear(&pReader->coder);
15,173,239✔
47
  tdbFree(pReader->pBuf);
15,185,834✔
48
  pReader->pBuf = NULL;
15,186,810✔
49
}
15,186,810✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
24,722,155!
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,724,543✔
63

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

71
  return 0;
24,452,889✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
708,575✔
84
  return true;
708,574✔
85
}
86

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

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

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

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

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
7,321,155✔
105
  if (TSDB_CODE_SUCCESS != code) {
7,325,434✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
150!
107
  }
108

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

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

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

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

130
  metaRLock(pMeta);
136,447✔
131

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

137
  metaULock(pMeta);
136,472✔
138

139
  return uid;
136,481✔
140
}
141

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

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

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

178
  SMetaReader *pReader = &mr;
439,934✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
438,221✔
187

188
  metaReaderClear(&mr);
438,221✔
189

190
  return 0;
438,211✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
438,224✔
199
  if (code == 0) *tbType = mr.me.type;
438,213!
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
438,213✔
201
    *suid = mr.me.ctbEntry.suid;
438,192✔
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);
438,213✔
209
  return code;
438,223✔
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) {
953,007✔
245
  SMTbCursor *pTbCur = NULL;
953,007✔
246
  int32_t     code;
247

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
953,007!
249
  if (pTbCur == NULL) {
953,649!
250
    return NULL;
×
251
  }
252

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,910,819✔
267
  if (pTbCur) {
1,910,819✔
268
    tdbFree(pTbCur->pKey);
956,037✔
269
    tdbFree(pTbCur->pVal);
955,978✔
270
    if (!pTbCur->paused) {
955,981✔
271
      metaReaderClear(&pTbCur->mr);
77,025✔
272
      if (pTbCur->pDbc) {
77,016!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
77,019✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
955,974!
277
  }
278
}
1,910,827✔
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
879,568✔
281
  if (!pTbCur->paused) {
879,568!
282
    metaReaderClear(&pTbCur->mr);
879,721✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
879,788✔
284
    pTbCur->paused = 1;
879,649✔
285
  }
286
}
879,496✔
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
955,013✔
288
  int32_t code = 0;
955,013✔
289
  int32_t lino;
290
  int8_t  locked = 0;
955,013✔
291
  if (pTbCur->paused) {
955,013!
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
955,264✔
293
    locked = 1;
956,588✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
956,588✔
295
    if (code != 0) {
954,835!
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
954,835✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
954,019✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
954,769!
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;
955,585✔
318
  }
319

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

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
12,180,534✔
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);
12,180,534✔
334
    if (ret < 0) {
12,172,793✔
335
      return ret;
955,939✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
11,216,854✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
11,249,521✔
341
    if (ret) return ret;
11,215,533!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
11,215,533✔
344
      continue;
2,519,799✔
345
    }
346

347
    break;
8,695,734✔
348
  }
349

350
  return 0;
8,695,734✔
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) {
4,853,617✔
382
  void           *pData = NULL;
4,853,617✔
383
  int             nData = 0;
4,853,617✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
4,853,617✔
386
  SSchemaWrapper *pSchema = NULL;
4,853,617✔
387
  SDecoder        dc = {0};
4,853,617✔
388
  if (lock) {
4,853,617✔
389
    metaRLock(pMeta);
4,845,522✔
390
  }
391
_query:
4,874,091✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
4,954,069✔
393
    goto _err;
752✔
394
  }
395

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

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

402
  SMetaEntry me = {0};
4,954,885✔
403
  tDecoderInit(&dc, pData, nData);
4,954,885✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
4,948,317✔
405
  if (code) {
4,952,293!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
4,952,293✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,545,401✔
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,541,820✔
412
      tDecoderClear(&dc);
4,541,820✔
413
      goto _exit;
4,538,945✔
414
    }
415
  } else if (me.type == TSDB_CHILD_TABLE) {
406,892✔
416
    uid = me.ctbEntry.suid;
79,955✔
417
    tDecoderClear(&dc);
79,955✔
418
    goto _query;
79,978✔
419
  } else {
420
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
326,937✔
421
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
326,974✔
422
      tDecoderClear(&dc);
326,974✔
423
      goto _exit;
326,999✔
424
    }
425
  }
426
  tDecoderClear(&dc);
3,916✔
427

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

433
  tDecoderInit(&dc, pData, nData);
145✔
434
  if (tDecodeSSchemaWrapperEx(&dc, &schema) != 0) {
145!
435
    goto _err;
×
436
  }
437
  pSchema = tCloneSSchemaWrapper(&schema);
145✔
438
  tDecoderClear(&dc);
145✔
439

440
_exit:
4,866,089✔
441
  if (lock) {
4,866,089✔
442
    metaULock(pMeta);
4,841,051✔
443
  }
444
  tdbFree(pData);
4,879,317✔
445
  return pSchema;
4,866,303✔
446

447
_err:
752✔
448
  if (lock) {
752!
449
    metaULock(pMeta);
752✔
450
  }
451
  tdbFree(pData);
752✔
452
  return NULL;
752✔
453
}
454

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

465
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
153!
466
    goto _exit;
×
467
  }
468

469
  version = ((SUidIdxVal *)pData)[0].version;
153✔
470

471
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
153!
472
    goto _exit;
×
473
  }
474

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

487
  _exit:
153✔
488
  if (lock) {
153!
489
    metaULock(pMeta);
153✔
490
  }
491
  tdbFree(pData);
153✔
492
  return createTime;
153✔
493
}
494

495
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
3,814,082✔
496
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
3,814,082✔
497
  SMCtbCursor *pCtbCur = NULL;
3,814,082✔
498
  SCtbIdxKey   ctbIdxKey;
499
  int          ret = 0;
3,814,082✔
500
  int          c = 0;
3,814,082✔
501

502
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,814,082!
503
  if (pCtbCur == NULL) {
3,827,751!
504
    return NULL;
×
505
  }
506

507
  pCtbCur->pMeta = pMeta;
3,827,751✔
508
  pCtbCur->suid = uid;
3,827,751✔
509
  pCtbCur->lock = lock;
3,827,751✔
510
  pCtbCur->paused = 1;
3,827,751✔
511

512
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,827,751✔
513
  if (ret < 0) {
3,825,328!
514
    return NULL;
×
515
  }
516
  return pCtbCur;
3,825,328✔
517
}
518

519
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,826,644✔
520
  if (pCtbCur) {
3,826,644!
521
    if (!pCtbCur->paused) {
3,827,428✔
522
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,784,576!
523
      if (pCtbCur->pCur) {
3,786,236!
524
        tdbTbcClose(pCtbCur->pCur);
3,786,480✔
525
      }
526
    }
527
    tdbFree(pCtbCur->pKey);
3,826,905✔
528
    tdbFree(pCtbCur->pVal);
3,827,864✔
529
  }
530
  taosMemoryFree(pCtbCur);
3,828,419!
531
}
3,829,095✔
532

533
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,324✔
534
  if (!pCtbCur->paused) {
43,324!
535
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,341✔
536
    if (pCtbCur->lock) {
43,371!
537
      metaULock(pCtbCur->pMeta);
43,372✔
538
    }
539
    pCtbCur->paused = 1;
43,376✔
540
  }
541
}
43,359✔
542

543
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,817,585✔
544
  if (pCtbCur->paused) {
3,817,585!
545
    pCtbCur->paused = 0;
3,821,964✔
546

547
    if (pCtbCur->lock) {
3,821,964✔
548
      metaRLock(pCtbCur->pMeta);
3,806,485✔
549
    }
550
    int ret = 0;
3,823,683✔
551
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,823,683✔
552
    if (ret < 0) {
3,824,302!
553
      metaCloseCtbCursor(pCtbCur);
×
554
      return -1;
×
555
    }
556

557
    if (first) {
3,824,558!
558
      SCtbIdxKey ctbIdxKey;
559
      // move to the suid
560
      ctbIdxKey.suid = pCtbCur->suid;
3,824,558✔
561
      ctbIdxKey.uid = INT64_MIN;
3,824,558✔
562
      int c = 0;
3,824,558✔
563
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,824,558✔
564
      if (c > 0) {
3,827,053✔
565
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
896,498✔
566
      }
567
    } else {
568
      int c = 0;
×
569
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
570
      if (c < 0) {
×
571
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
572
      } else {
573
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
574
      }
575
    }
576
  }
577
  return 0;
3,822,175✔
578
}
579

580
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
15,902,978✔
581
  int         ret;
582
  SCtbIdxKey *pCtbIdxKey;
583

584
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
15,902,978✔
585
  if (ret < 0) {
15,900,704✔
586
    return 0;
2,369,455✔
587
  }
588

589
  pCtbIdxKey = pCtbCur->pKey;
13,531,249✔
590
  if (pCtbIdxKey->suid > pCtbCur->suid) {
13,531,249✔
591
    return 0;
1,459,947✔
592
  }
593

594
  return pCtbIdxKey->uid;
12,071,302✔
595
}
596

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

607
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
153,736✔
608
  SMStbCursor *pStbCur = NULL;
153,736✔
609
  int          ret = 0;
153,736✔
610
  int          c = 0;
153,736✔
611

612
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
153,736!
613
  if (pStbCur == NULL) {
153,736!
614
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
615
    return NULL;
×
616
  }
617

618
  pStbCur->pMeta = pMeta;
153,736✔
619
  pStbCur->suid = suid;
153,736✔
620
  metaRLock(pMeta);
153,736✔
621

622
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
153,736✔
623
  if (ret < 0) {
153,736!
624
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
625
    metaULock(pMeta);
×
626
    taosMemoryFree(pStbCur);
×
627
    return NULL;
×
628
  }
629

630
  // move to the suid
631
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
153,736✔
632
  if (c > 0) {
153,734!
633
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
634
  }
635

636
  return pStbCur;
153,734✔
637
}
638

639
void metaCloseStbCursor(SMStbCursor *pStbCur) {
153,735✔
640
  if (pStbCur) {
153,735!
641
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
153,735!
642
    if (pStbCur->pCur) {
153,734!
643
      tdbTbcClose(pStbCur->pCur);
153,734✔
644

645
      tdbFree(pStbCur->pKey);
153,736✔
646
      tdbFree(pStbCur->pVal);
153,736✔
647
    }
648

649
    taosMemoryFree(pStbCur);
153,736!
650
  }
651
}
153,736✔
652

653
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
256,287✔
654
  int ret;
655

656
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
256,287✔
657
  if (ret < 0) {
256,288✔
658
    return 0;
153,735✔
659
  }
660
  return *(tb_uid_t *)pStbCur->pKey;
102,553✔
661
}
662

663
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
4,711,961✔
664
  STSchema       *pTSchema = NULL;
4,711,961✔
665
  SSchemaWrapper *pSW = NULL;
4,711,961✔
666

667
  pSW = metaGetTableSchema(pMeta, uid, sver, lock);
4,711,961✔
668
  if (!pSW) return NULL;
4,725,579✔
669

670
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
4,725,498✔
671

672
  taosMemoryFree(pSW->pSchema);
4,732,548!
673
  taosMemoryFree(pSW);
4,733,071✔
674
  return pTSchema;
4,732,147✔
675
}
676

677
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,065✔
678
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,065✔
679
  if (*ppTSchema == NULL) {
4,078!
680
    return terrno;
×
681
  }
682
  return TSDB_CODE_SUCCESS;
4,079✔
683
}
684

685
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,708,961✔
686
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,708,961✔
687
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,727,181!
688
    return terrno;
10,244✔
689
  }
690
  return TSDB_CODE_SUCCESS;
4,716,937✔
691
}
692

693
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
1,345,998✔
694
  int32_t code = 0;
1,345,998✔
695
  int32_t lino;
696

697
  void     *pData = NULL;
1,345,998✔
698
  int       nData = 0;
1,345,998✔
699
  SSkmDbKey skmDbKey;
700
  if (sver <= 0) {
1,345,998✔
701
    SMetaInfo info;
702
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
663,111✔
703
      sver = info.skmVer;
663,040✔
704
    } else {
705
      TBC *pSkmDbC = NULL;
73✔
706
      int  c;
707

708
      skmDbKey.uid = suid ? suid : uid;
73!
709
      skmDbKey.sver = INT32_MAX;
73✔
710

711
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
73✔
712
      TSDB_CHECK_CODE(code, lino, _exit);
81!
713
      metaRLock(pMeta);
81✔
714

715
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
81!
716
        metaULock(pMeta);
×
717
        tdbTbcClose(pSkmDbC);
×
718
        code = TSDB_CODE_NOT_FOUND;
×
719
        goto _exit;
×
720
      }
721

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

730
      if (c < 0) {
81✔
731
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
6✔
732
      }
733

734
      const void *pKey = NULL;
81✔
735
      int32_t     nKey = 0;
81✔
736
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
81✔
737

738
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
81!
739
        metaULock(pMeta);
×
740
        tdbTbcClose(pSkmDbC);
×
741
        code = TSDB_CODE_NOT_FOUND;
×
742
        goto _exit;
×
743
      }
744

745
      sver = ((SSkmDbKey *)pKey)->sver;
81✔
746

747
      metaULock(pMeta);
81✔
748
      tdbTbcClose(pSkmDbC);
81✔
749
    }
750
  }
751

752
  if (!(sver > 0)) {
1,345,991!
753
    code = TSDB_CODE_NOT_FOUND;
×
754
    goto _exit;
×
755
  }
756

757
  skmDbKey.uid = suid ? suid : uid;
1,345,991✔
758
  skmDbKey.sver = sver;
1,345,991✔
759
  metaRLock(pMeta);
1,345,991✔
760
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
1,346,143!
761
    metaULock(pMeta);
×
762
    code = TSDB_CODE_NOT_FOUND;
×
763
    goto _exit;
×
764
  }
765
  metaULock(pMeta);
1,346,013✔
766

767
  // decode
768
  SDecoder        dc = {0};
1,346,112✔
769
  SSchemaWrapper  schema;
770
  SSchemaWrapper *pSchemaWrapper = &schema;
1,346,112✔
771

772
  tDecoderInit(&dc, pData, nData);
1,346,112✔
773
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
1,345,008✔
774
  tDecoderClear(&dc);
1,345,008✔
775
  tdbFree(pData);
1,345,911✔
776
  if (TSDB_CODE_SUCCESS != code) {
1,345,957!
777
    taosMemoryFree(pSchemaWrapper->pSchema);
×
778
    goto _exit;
×
779
  }
780

781
  // convert
782
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
1,345,957✔
783
  if (pTSchema == NULL) {
1,346,026!
784
    code = TSDB_CODE_OUT_OF_MEMORY;
×
785
  }
786

787
  *ppTSchema = pTSchema;
1,346,026✔
788
  taosMemoryFree(pSchemaWrapper->pSchema);
1,346,026!
789

790
_exit:
1,346,121✔
791
  return code;
1,346,121✔
792
}
793

794
// N.B. Called by statusReq per second
795
int64_t metaGetTbNum(SMeta *pMeta) {
998,975✔
796
  // num of child tables (excluding normal tables , stables and others)
797

798
  /* int64_t num = 0; */
799
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
800

801
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
998,975✔
802
}
803

804
void metaUpdTimeSeriesNum(SMeta *pMeta) {
153,718✔
805
  int64_t nCtbTimeSeries = 0;
153,718✔
806
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
153,718!
807
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
153,719✔
808
  }
809
}
153,719✔
810

811
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
812
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
813
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,154,872✔
814
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,154,896✔
815
    metaUpdTimeSeriesNum(pMeta);
151,348✔
816
  }
817

818
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,154,901✔
819
}
820

821
// type: 1 reported timeseries
822
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,154,872!
823
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,154,901✔
824
  if (type == 1) {
1,154,901✔
825
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
998,975✔
826
  }
827
  return nTimeSeries;
1,154,907✔
828
}
829

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

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

846
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
847
  if (pSmaCur == NULL) {
×
848
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
849
    return NULL;
×
850
  }
851

852
  pSmaCur->pMeta = pMeta;
×
853
  pSmaCur->uid = uid;
×
854
  metaRLock(pMeta);
×
855

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

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

871
  return pSmaCur;
×
872
}
873

874
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
875
  if (pSmaCur) {
×
876
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
877
    if (pSmaCur->pCur) {
×
878
      tdbTbcClose(pSmaCur->pCur);
×
879
      pSmaCur->pCur = NULL;
×
880

881
      tdbFree(pSmaCur->pKey);
×
882
      tdbFree(pSmaCur->pVal);
×
883
    }
884

885
    taosMemoryFree(pSmaCur);
×
886
  }
887
}
×
888

889
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
890
  int         ret;
891
  SSmaIdxKey *pSmaIdxKey;
892

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

898
  pSmaIdxKey = pSmaCur->pKey;
×
899
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
900
    return 0;
×
901
  }
902

903
  return pSmaIdxKey->uid;
×
904
}
905

906
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
907
  STSmaWrapper *pSW = NULL;
×
908
  SArray       *pSmaIds = NULL;
×
909

910
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
911
    return NULL;
×
912
  }
913

914
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
915
  if (!pSW) {
×
916
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
917
    goto _err;
×
918
  }
919

920
  pSW->number = taosArrayGetSize(pSmaIds);
×
921
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
922

923
  if (!pSW->tSma) {
×
924
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
925
    goto _err;
×
926
  }
927

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

965
    ++smaIdx;
×
966
  }
967

968
  if (smaIdx <= 0) goto _err;
×
969
  pSW->number = smaIdx;
×
970

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

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

997
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
2✔
998

999
  metaReaderClear(&mr);
2✔
1000
  return pTSma;
2✔
1001
}
1002

1003
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1004
  SArray     *pUids = NULL;
×
1005
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1006

1007
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1008
  if (!pCur) {
×
1009
    return NULL;
×
1010
  }
1011

1012
  while (1) {
×
1013
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1014
    if (id == 0) {
×
1015
      break;
×
1016
    }
1017

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

1027
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1028

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

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

1041
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1042
  SArray     *pUids = NULL;
×
1043
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1044
  tb_uid_t    lastUid = 0;
×
1045

1046
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1047
  if (!pCur) {
×
1048
    return NULL;
×
1049
  }
1050

1051
  while (1) {
×
1052
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1053
    if (uid == 0) {
×
1054
      break;
×
1055
    }
1056

1057
    if (lastUid == uid) {
×
1058
      continue;
×
1059
    }
1060

1061
    lastUid = uid;
×
1062

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

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

1080
  metaCloseSmaCursor(pCur);
×
1081
  return pUids;
×
1082
}
1083

1084
#endif
1085

1086
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
20,835,370✔
1087
  STag *tag = (STag *)pTag;
20,835,370✔
1088
  if (type == TSDB_DATA_TYPE_JSON) {
20,835,370✔
1089
    return tag;
10,033✔
1090
  }
1091
  bool find = tTagGet(tag, val);
20,825,337✔
1092

1093
  if (!find) {
20,862,137✔
1094
    return NULL;
138,851✔
1095
  }
1096

1097
  return val;
20,723,286✔
1098
}
1099

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

1112
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1113
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1114
  SMetaFltParam *param = arg;
6✔
1115
  int32_t        ret = 0;
6✔
1116

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

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

1134
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
6✔
1135
  SBtimeIdxKey *pBtimeKey = &btimeKey;
6✔
1136

1137
  int cmp = 0;
6✔
1138
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
6!
1139
    goto END;
×
1140
  }
1141

1142
  int32_t valid = 0;
6✔
1143
  int32_t count = 0;
6✔
1144

1145
  static const int8_t TRY_ERROR_LIMIT = 1;
1146
  do {
35,328✔
1147
    void   *entryKey = NULL;
35,334✔
1148
    int32_t nEntryKey = -1;
35,334✔
1149
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
35,334✔
1150
    if (valid < 0) break;
35,173✔
1151

1152
    SBtimeIdxKey *p = entryKey;
35,167✔
1153
    if (count > TRY_ERROR_LIMIT) break;
35,167!
1154

1155
    terrno = TSDB_CODE_SUCCESS;
35,167✔
1156
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
35,168✔
1157
    if (terrno != TSDB_CODE_SUCCESS) {
35,011!
1158
      ret = terrno;
×
1159
      break;
×
1160
    }
1161
    if (cmp == 0) {
35,034!
1162
      if (taosArrayPush(pUids, &p->uid) == NULL) {
70,182!
1163
        ret = terrno;
×
1164
        break;
×
1165
      }
1166
    } else {
1167
      if (param->equal == true) {
×
1168
        if (count > TRY_ERROR_LIMIT) break;
×
1169
        count++;
×
1170
      }
1171
    }
1172
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
35,148✔
1173
    if (valid < 0) break;
35,328!
1174
  } while (1);
1175

1176
END:
6✔
1177
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1178
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1179
  taosMemoryFree(pCursor);
6!
1180
  return ret;
6✔
1181
}
1182

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

1189
  STagIdxKey *pKey = NULL;
×
1190
  int32_t     nKey = 0;
×
1191

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

1202
  char *pName = param->val;
×
1203

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

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

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

1224
    if (count > TRY_ERROR_LIMIT) break;
×
1225

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

1251
END:
×
1252
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1253
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1254
  taosMemoryFree(buf);
×
1255
  taosMemoryFree(pKey);
×
1256

1257
  taosMemoryFree(pCursor);
×
1258

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

1267
  STtlIdxKey *pKey = NULL;
×
1268
  int32_t     nKey = 0;
×
1269

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

1280
  metaRLock(pMeta);
×
1281
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1282

1283
END:
×
1284
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1285
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1286
  taosMemoryFree(buf);
×
1287
  taosMemoryFree(pKey);
×
1288

1289
  taosMemoryFree(pCursor);
×
1290

1291
  return ret;
×
1292
  // impl later
1293
  return 0;
1294
}
1295
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
13,542✔
1296
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
13,542✔
1297
  SMetaFltParam *param = arg;
13,542✔
1298

1299
  SMetaEntry oStbEntry = {0};
13,542✔
1300
  int32_t    code = 0;
13,542✔
1301
  char      *buf = NULL;
13,542✔
1302
  void      *pData = NULL;
13,542✔
1303
  int        nData = 0;
13,542✔
1304

1305
  SDecoder    dc = {0};
13,542✔
1306
  STbDbKey    tbDbKey = {0};
13,542✔
1307
  STagIdxKey *pKey = NULL;
13,542✔
1308
  int32_t     nKey = 0;
13,542✔
1309

1310
  SIdxCursor *pCursor = NULL;
13,542✔
1311
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
13,542!
1312
  if (!pCursor) {
13,545!
1313
    return terrno;
×
1314
  }
1315
  pCursor->pMeta = pMeta;
13,545✔
1316
  pCursor->suid = param->suid;
13,545✔
1317
  pCursor->cid = param->cid;
13,545✔
1318
  pCursor->type = param->type;
13,545✔
1319

1320
  metaRLock(pMeta);
13,545✔
1321

1322
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
13,546!
1323

1324
  tbDbKey.uid = param->suid;
13,544✔
1325
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
13,544✔
1326

1327
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
13,544!
1328

1329
  tDecoderInit(&dc, pData, nData);
13,547✔
1330

1331
  code = metaDecodeEntry(&dc, &oStbEntry);
13,544✔
1332
  if (code) {
13,545!
1333
    tDecoderClear(&dc);
×
1334
    goto END;
×
1335
  }
1336

1337
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
13,545!
1338
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1339
  }
1340

1341
  code = TSDB_CODE_INVALID_PARA;
13,545✔
1342
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
14,766✔
1343
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
14,346✔
1344
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
14,346!
1345
      code = 0;
422✔
1346
    } else {
1347
      TAOS_CHECK_GOTO(code, NULL, END);
13,924✔
1348
    }
1349
  }
1350

1351
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
420✔
1352
  if (code != 0) {
422!
1353
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1354
  }
1355

1356
  int32_t maxSize = 0;
422✔
1357
  int32_t nTagData = 0;
422✔
1358
  void   *tagData = NULL;
422✔
1359

1360
  if (param->val == NULL) {
422!
1361
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1362
    goto END;
×
1363
  } else {
1364
    if (IS_VAR_DATA_TYPE(param->type)) {
422!
1365
      tagData = varDataVal(param->val);
1✔
1366
      nTagData = varDataLen(param->val);
1✔
1367

1368
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
1!
1369
        maxSize = 4 * nTagData + 1;
×
1370
        buf = taosMemoryCalloc(1, maxSize);
×
1371
        if (buf == NULL) {
×
1372
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1373
        }
1374

1375
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
×
1376
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1377
        }
1378

1379
        tagData = buf;
×
1380
        nTagData = maxSize;
×
1381
      }
1382
    } else {
1383
      tagData = param->val;
421✔
1384
      nTagData = tDataTypes[param->type].bytes;
421✔
1385
    }
1386
  }
1387

1388
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
422!
1389
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1390
                  NULL, END);
1391

1392
  int cmp = 0;
421✔
1393
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
421!
1394

1395
  int     count = 0;
422✔
1396
  int32_t valid = 0;
422✔
1397
  bool    found = false;
422✔
1398

1399
  static const int8_t TRY_ERROR_LIMIT = 1;
1400

1401
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1402
  /// target:                        [suid, cid2, type2]
1403
  int diffCidCount = 0;
422✔
1404
  do {
10,385✔
1405
    void   *entryKey = NULL, *entryVal = NULL;
10,807✔
1406
    int32_t nEntryKey, nEntryVal;
1407

1408
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
10,807✔
1409
    if (valid < 0) {
10,793✔
1410
      code = valid;
404✔
1411
    }
1412
    if (count > TRY_ERROR_LIMIT) {
10,793✔
1413
      break;
422✔
1414
    }
1415

1416
    STagIdxKey *p = entryKey;
10,772✔
1417
    if (p == NULL) break;
10,772✔
1418

1419
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
10,372!
1420
      if (found == true) break;  //
8✔
1421
      if (diffCidCount > TRY_ERROR_LIMIT) break;
7!
1422
      diffCidCount++;
7✔
1423
      count++;
7✔
1424
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
7!
1425
      if (valid < 0) {
9!
1426
        code = valid;
×
1427
        break;
×
1428
      } else {
1429
        continue;
9✔
1430
      }
1431
    }
1432

1433
    terrno = TSDB_CODE_SUCCESS;
10,364✔
1434
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
10,364✔
1435
    if (terrno != TSDB_CODE_SUCCESS) {
10,343!
1436
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1437
      break;
×
1438
    }
1439
    if (cmp == 0) {
10,345✔
1440
      // match
1441
      tb_uid_t tuid = 0;
9,758✔
1442
      if (IS_VAR_DATA_TYPE(pKey->type)) {
9,758!
1443
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
×
1444
      } else {
1445
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
9,770✔
1446
      }
1447
      if (taosArrayPush(pUids, &tuid) == NULL) {
9,796!
1448
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1449
      }
1450
      found = true;
9,796✔
1451
    } else {
1452
      if (param->equal == true) {
587✔
1453
        if (count > TRY_ERROR_LIMIT) break;
105!
1454
        count++;
105✔
1455
      }
1456
    }
1457
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
10,383✔
1458
    if (valid < 0) {
10,376!
1459
      code = valid;
×
1460
      break;
×
1461
    }
1462
  } while (1);
1463

1464
END:
13,547✔
1465
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
13,547✔
1466
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
13,550✔
1467
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
13,550!
1468
  tDecoderClear(&dc);
13,550✔
1469
  tdbFree(pData);
13,546✔
1470

1471
  taosMemoryFree(buf);
13,546!
1472
  taosMemoryFree(pKey);
13,546!
1473

1474
  taosMemoryFree(pCursor);
13,545!
1475

1476
  return code;
13,547✔
1477
}
1478

1479
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
247,998✔
1480
  int ret = 0;
247,998✔
1481
  if (lock) {
247,998!
1482
    metaRLock(pMeta);
×
1483
  }
1484

1485
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
247,998✔
1486
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
247,998✔
1487
  if (lock) {
247,997!
1488
    metaULock(pMeta);
×
1489
  }
1490

1491
  return ret;
247,997✔
1492
}
1493

1494
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
247,994✔
1495
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
247,994✔
1496
  const int32_t LIMIT = 128;
247,994✔
1497

1498
  int32_t isLock = false;
247,994✔
1499
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
247,994!
1500
  for (int i = 0; i < sz; i++) {
495,991✔
1501
    STUidTagInfo *p = taosArrayGet(uidList, i);
247,996✔
1502

1503
    if (i % LIMIT == 0) {
247,996✔
1504
      if (isLock) metaULock(pMeta);
247,983!
1505

1506
      metaRLock(pMeta);
247,983✔
1507
      isLock = true;
247,985✔
1508
    }
1509

1510
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1511
    void   *val = NULL;
247,998✔
1512
    int32_t len = 0;
247,998✔
1513
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
247,998!
1514
      p->pTagVal = taosMemoryMalloc(len);
247,997!
1515
      if (!p->pTagVal) {
247,995!
1516
        if (isLock) metaULock(pMeta);
×
1517

1518
        TAOS_RETURN(terrno);
×
1519
      }
1520
      memcpy(p->pTagVal, val, len);
247,995✔
1521
      tdbFree(val);
247,995✔
1522
    } else {
1523
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64 "", TD_VID(pMeta->pVnode), suid,
×
1524
                p->uid);
1525
    }
1526
  }
1527
  //  }
1528
  if (isLock) metaULock(pMeta);
247,995✔
1529
  return 0;
247,995✔
1530
}
1531

1532
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
987,285✔
1533
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
987,285✔
1534
  if (!pCur) {
990,310!
1535
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1536
  }
1537

1538
  // If len > 0 means there already have uids, and we only want the
1539
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1540
  // in the hash map, that may require a lot of memory
1541
  SHashObj *pSepecifiedUidMap = NULL;
990,310✔
1542
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
990,310✔
1543
  if (numOfElems > 0) {
989,731✔
1544
    pSepecifiedUidMap =
1545
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
94,127✔
1546
    for (int i = 0; i < numOfElems; i++) {
380,821✔
1547
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
286,667✔
1548
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
286,585✔
1549
      if (code) {
286,730!
1550
        metaCloseCtbCursor(pCur);
×
1551
        taosHashCleanup(pSepecifiedUidMap);
×
1552
        return code;
×
1553
      }
1554
    }
1555
  }
1556

1557
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
989,758✔
1558
    while (1) {
2,631,250✔
1559
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,526,419✔
1560
      if (uid == 0) {
3,521,153✔
1561
        break;
896,887✔
1562
      }
1563

1564
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,624,266✔
1565
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,624,266!
1566
      if (!info.pTagVal) {
2,632,319!
1567
        metaCloseCtbCursor(pCur);
×
1568
        taosHashCleanup(pSepecifiedUidMap);
×
1569
        return terrno;
×
1570
      }
1571
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,632,319✔
1572
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,631,250!
1573
        taosMemoryFreeClear(info.pTagVal);
×
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
    }
1579
  } else {  // only the specified tables need to be added
1580
    while (1) {
379,085✔
1581
      tb_uid_t uid = metaCtbCursorNext(pCur);
473,674✔
1582
      if (uid == 0) {
473,557✔
1583
        break;
94,135✔
1584
      }
1585

1586
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
379,422✔
1587
      if (index == NULL) {
379,557✔
1588
        continue;
93,127✔
1589
      }
1590

1591
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
286,430✔
1592
      if (pTagInfo->pTagVal == NULL) {
286,373!
1593
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
286,815✔
1594
        if (!pTagInfo->pTagVal) {
286,400!
1595
          metaCloseCtbCursor(pCur);
×
1596
          taosHashCleanup(pSepecifiedUidMap);
×
1597
          return terrno;
×
1598
        }
1599
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
286,400✔
1600
      }
1601
    }
1602
  }
1603

1604
  taosHashCleanup(pSepecifiedUidMap);
991,022✔
1605
  metaCloseCtbCursor(pCur);
990,682✔
1606
  return TSDB_CODE_SUCCESS;
991,046✔
1607
}
1608

1609
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1610

1611
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
32,268,414✔
1612
  int32_t code = 0;
32,268,414✔
1613
  void   *pData = NULL;
32,268,414✔
1614
  int     nData = 0;
32,268,414✔
1615
  int     lock = 0;
32,268,414✔
1616

1617
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
32,268,414!
1618
    lock = 1;
7,325,966✔
1619
  }
1620

1621
  if (!lock) metaRLock(pMeta);
32,268,414✔
1622

1623
  // search cache
1624
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
32,280,294✔
1625
    if (!lock) metaULock(pMeta);
18,502,172✔
1626
    goto _exit;
18,498,720✔
1627
  }
1628

1629
  // search TDB
1630
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
13,784,720✔
1631
    // not found
1632
    if (!lock) metaULock(pMeta);
13,777,893✔
1633
    goto _exit;
13,777,943✔
1634
  }
1635

1636
  if (!lock) metaULock(pMeta);
7,377✔
1637

1638
  pInfo->uid = uid;
7,377✔
1639
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
7,377✔
1640
  pInfo->version = ((SUidIdxVal *)pData)->version;
7,377✔
1641
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
7,377✔
1642

1643
  if (lock) {
7,377✔
1644
    metaULock(pReader->pMeta);
2,243✔
1645
    // metaReaderReleaseLock(pReader);
1646
  }
1647
  // upsert the cache
1648
  metaWLock(pMeta);
7,383✔
1649
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
7,386✔
1650
  if (ret != 0) {
7,387!
1651
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1652
  }
1653
  metaULock(pMeta);
7,387✔
1654

1655
  if (lock) {
7,387✔
1656
    metaRLock(pReader->pMeta);
2,249✔
1657
  }
1658

1659
_exit:
5,138✔
1660
  tdbFree(pData);
32,284,051✔
1661
  return code;
32,281,670✔
1662
}
1663

1664
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
249,061✔
1665
  int32_t code = 0;
249,061✔
1666

1667
  if (!numOfTables && !numOfCols) goto _exit;
249,061!
1668

1669
  SVnode *pVnodeObj = pVnode;
249,061✔
1670
  metaRLock(pVnodeObj->pMeta);
249,061✔
1671

1672
  // fast path: search cache
1673
  SMetaStbStats state = {0};
249,089✔
1674
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
249,089✔
1675
    metaULock(pVnodeObj->pMeta);
228,463✔
1676
    if (numOfTables) *numOfTables = state.ctbNum;
228,463✔
1677
    if (numOfCols) *numOfCols = state.colNum;
228,463✔
1678
    goto _exit;
228,463✔
1679
  }
1680

1681
  // slow path: search TDB
1682
  int64_t ctbNum = 0;
20,626✔
1683
  int32_t colNum = 0;
20,626✔
1684
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
20,626✔
1685
  if (TSDB_CODE_SUCCESS == code) {
20,625!
1686
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
20,625✔
1687
  }
1688
  metaULock(pVnodeObj->pMeta);
20,625✔
1689
  if (TSDB_CODE_SUCCESS != code) {
20,623!
1690
    goto _exit;
×
1691
  }
1692

1693
  if (numOfTables) *numOfTables = ctbNum;
20,623✔
1694
  if (numOfCols) *numOfCols = colNum;
20,623✔
1695

1696
  state.uid = uid;
20,623✔
1697
  state.ctbNum = ctbNum;
20,623✔
1698
  state.colNum = colNum;
20,623✔
1699

1700
  // upsert the cache
1701
  metaWLock(pVnodeObj->pMeta);
20,623✔
1702

1703
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
20,626✔
1704
  if (ret) {
20,625!
1705
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d", uid, ctbNum, colNum);
×
1706
  }
1707

1708
  metaULock(pVnodeObj->pMeta);
20,625✔
1709

1710
_exit:
249,084✔
1711
  return code;
249,084✔
1712
}
1713

1714
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol) {
153,247✔
1715
  SMetaStbStats stats = {0};
153,247✔
1716

1717
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
153,247✔
1718
    stats.ctbNum += deltaCtb;
140,385✔
1719
    stats.colNum += deltaCol;
140,385✔
1720
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
140,385✔
1721
    if (code) {
140,389!
1722
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d",
×
1723
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol);
1724
    }
1725
  }
1726
}
153,276✔
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