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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

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

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

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

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 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) {
12,376,514✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
12,376,514✔
22
  metaReaderDoInit(pReader, pMeta, flags);
12,376,514✔
23
  pReader->pAPI = pAPI;
12,389,844✔
24
}
12,389,844✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
17,186,030✔
27
  memset(pReader, 0, sizeof(*pReader));
17,186,030✔
28
  pReader->pMeta = pMeta;
17,186,030✔
29
  pReader->flags = flags;
17,186,030✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
17,186,030!
31
    metaRLock(pMeta);
11,361,985✔
32
  }
33
}
17,185,914✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,036,696✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,036,696!
37
    metaULock(pReader->pMeta);
2,037,244✔
38
    pReader->flags |= META_READER_NOLOCK;
2,038,330✔
39
  }
40
}
2,037,782✔
41

42
void metaReaderClear(SMetaReader *pReader) {
18,591,833✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
18,591,833✔
44
    metaULock(pReader->pMeta);
9,322,796✔
45
  }
46
  tDecoderClear(&pReader->coder);
18,589,842✔
47
  tdbFree(pReader->pBuf);
18,597,289✔
48
  pReader->pBuf = NULL;
18,605,292✔
49
}
18,605,292✔
50

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

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

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

71
  return 0;
28,498,245✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
803,326✔
84
  return true;
803,336✔
85
}
86

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

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

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

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

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
7,671,658✔
105
  if (TSDB_CODE_SUCCESS != code) {
7,673,766✔
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);
7,673,652✔
110
}
111

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

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

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

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

130
  metaRLock(pMeta);
207,304✔
131

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

137
  metaULock(pMeta);
207,326✔
138

139
  return uid;
207,334✔
140
}
141

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

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

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

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

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
436,269✔
187

188
  metaReaderClear(&mr);
436,269✔
189

190
  return 0;
436,266✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
436,269✔
199
  if (code == 0) *tbType = mr.me.type;
436,261✔
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
436,261✔
201
    *suid = mr.me.ctbEntry.suid;
436,232✔
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);
436,261✔
209
  return code;
436,270✔
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) {
1,052,239✔
245
  SMTbCursor *pTbCur = NULL;
1,052,239✔
246
  int32_t     code;
247

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
1,052,239!
249
  if (pTbCur == NULL) {
1,052,601!
250
    return NULL;
×
251
  }
252

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
2,107,766✔
267
  if (pTbCur) {
2,107,766✔
268
    tdbFree(pTbCur->pKey);
1,054,322✔
269
    tdbFree(pTbCur->pVal);
1,054,210✔
270
    if (!pTbCur->paused) {
1,054,269✔
271
      metaReaderClear(&pTbCur->mr);
82,941✔
272
      if (pTbCur->pDbc) {
82,954!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
82,955✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
1,054,273!
277
  }
278
}
2,107,816✔
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
971,969✔
281
  if (!pTbCur->paused) {
971,969!
282
    metaReaderClear(&pTbCur->mr);
972,164✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
972,269✔
284
    pTbCur->paused = 1;
972,074✔
285
  }
286
}
971,879✔
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
1,054,093✔
288
  int32_t code = 0;
1,054,093✔
289
  int32_t lino;
290
  int8_t  locked = 0;
1,054,093✔
291
  if (pTbCur->paused) {
1,054,093!
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
1,054,192✔
293
    locked = 1;
1,055,061✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
1,055,061✔
295
    if (code != 0) {
1,053,710!
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
1,053,710✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
1,052,788✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
1,053,305!
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;
1,054,227✔
318
  }
319

320
_exit:
×
321
  if (code != 0 && locked) {
1,054,128!
322
    metaReaderReleaseLock(&pTbCur->mr);
×
323
  }
324
  return code;
1,053,874✔
325
}
326

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
13,636,465✔
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);
13,636,465✔
334
    if (ret < 0) {
13,706,242✔
335
      return ret;
1,054,235✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
12,652,007✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
12,668,311✔
341
    if (ret) return ret;
12,624,613!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
12,624,613✔
344
      continue;
2,671,844✔
345
    }
346

347
    break;
9,952,769✔
348
  }
349

350
  return 0;
9,952,769✔
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) {
5,068,941✔
382
  void           *pData = NULL;
5,068,941✔
383
  int             nData = 0;
5,068,941✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
5,068,941✔
386
  SSchemaWrapper *pSchema = NULL;
5,068,941✔
387
  SDecoder        dc = {0};
5,068,941✔
388
  if (lock) {
5,068,941✔
389
    metaRLock(pMeta);
5,051,707✔
390
  }
391
_query:
5,078,894✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
5,158,687✔
393
    goto _err;
2,494✔
394
  }
395

396
  version = ((SUidIdxVal *)pData)[0].version;
5,155,711✔
397

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

402
  SMetaEntry me = {0};
5,158,145✔
403
  tDecoderInit(&dc, pData, nData);
5,158,145✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
5,153,806✔
405
  if (code) {
5,155,648!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
5,155,648✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,712,925!
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,711,386✔
412
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
4,711,386✔
413
      tDecoderClear(&dc);
4,711,382✔
414
      goto _exit;
4,715,329✔
415
    }
416
  } else if (me.type == TSDB_CHILD_TABLE) {
442,723✔
417
    uid = me.ctbEntry.suid;
79,703✔
418
    tDecoderClear(&dc);
79,703✔
419
    goto _query;
79,793✔
420
  } else {
421
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
363,020!
422
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
363,146✔
423
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
363,146✔
424
      tDecoderClear(&dc);
363,153✔
425
      goto _exit;
363,160✔
426
    }
427
  }
UNCOV
428
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
×
UNCOV
429
  tDecoderClear(&dc);
×
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:
5,078,621✔
444
  if (lock) {
5,078,621✔
445
    metaULock(pMeta);
5,043,894✔
446
  }
447
  tdbFree(pData);
5,081,278✔
448
  return pSchema;
5,078,310✔
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) {
3,883,008✔
499
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
3,883,008✔
500
  SMCtbCursor *pCtbCur = NULL;
3,883,008✔
501
  SCtbIdxKey   ctbIdxKey;
502
  int          ret = 0;
3,883,008✔
503
  int          c = 0;
3,883,008✔
504

505
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,883,008!
506
  if (pCtbCur == NULL) {
3,892,816!
507
    return NULL;
×
508
  }
509

510
  pCtbCur->pMeta = pMeta;
3,892,816✔
511
  pCtbCur->suid = uid;
3,892,816✔
512
  pCtbCur->lock = lock;
3,892,816✔
513
  pCtbCur->paused = 1;
3,892,816✔
514

515
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,892,816✔
516
  if (ret < 0) {
3,891,848!
517
    return NULL;
×
518
  }
519
  return pCtbCur;
3,891,848✔
520
}
521

522
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,892,743✔
523
  if (pCtbCur) {
3,892,743!
524
    if (!pCtbCur->paused) {
3,893,014✔
525
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,849,728!
526
      if (pCtbCur->pCur) {
3,850,804!
527
        tdbTbcClose(pCtbCur->pCur);
3,850,892✔
528
      }
529
    }
530
    tdbFree(pCtbCur->pKey);
3,892,575✔
531
    tdbFree(pCtbCur->pVal);
3,892,308✔
532
  }
533
  taosMemoryFree(pCtbCur);
3,893,186!
534
}
3,894,197✔
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) {
3,887,271✔
547
  if (pCtbCur->paused) {
3,887,271!
548
    pCtbCur->paused = 0;
3,889,614✔
549

550
    if (pCtbCur->lock) {
3,889,614✔
551
      metaRLock(pCtbCur->pMeta);
3,866,515✔
552
    }
553
    int ret = 0;
3,889,325✔
554
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,889,325✔
555
    if (ret < 0) {
3,888,937!
556
      metaCloseCtbCursor(pCtbCur);
×
557
      return -1;
×
558
    }
559

560
    if (first) {
3,890,188!
561
      SCtbIdxKey ctbIdxKey;
562
      // move to the suid
563
      ctbIdxKey.suid = pCtbCur->suid;
3,890,188✔
564
      ctbIdxKey.uid = INT64_MIN;
3,890,188✔
565
      int c = 0;
3,890,188✔
566
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,890,188✔
567
      if (c > 0) {
3,892,365✔
568
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
775,045✔
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;
3,889,584✔
581
}
582

583
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
16,152,287✔
584
  int         ret;
585
  SCtbIdxKey *pCtbIdxKey;
586

587
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
16,152,287✔
588
  if (ret < 0) {
16,158,677✔
589
    return 0;
2,160,356✔
590
  }
591

592
  pCtbIdxKey = pCtbCur->pKey;
13,998,321✔
593
  if (pCtbIdxKey->suid > pCtbCur->suid) {
13,998,321✔
594
    return 0;
1,734,057✔
595
  }
596

597
  return pCtbIdxKey->uid;
12,264,264✔
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) {
184,607✔
611
  SMStbCursor *pStbCur = NULL;
184,607✔
612
  int          ret = 0;
184,607✔
613
  int          c = 0;
184,607✔
614

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

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

625
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
184,607✔
626
  if (ret < 0) {
184,609!
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);
184,609✔
635
  if (c > 0) {
184,599!
636
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
637
  }
638

639
  return pStbCur;
184,599✔
640
}
641

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

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

652
    taosMemoryFree(pStbCur);
184,605!
653
  }
654
}
184,609✔
655

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

659
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
370,759✔
660
  if (ret < 0) {
370,731✔
661
    return 0;
184,601✔
662
  }
663
  return *(tb_uid_t *)pStbCur->pKey;
186,130✔
664
}
665

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

670
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
4,919,116✔
671
  if (!pSW) return NULL;
4,927,844✔
672

673
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
4,927,778✔
674

675
  taosMemoryFree(pSW->pSchema);
4,931,669✔
676
  taosMemoryFree(pSW);
4,928,015!
677
  return pTSchema;
4,930,122✔
678
}
679

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

688
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,906,792✔
689
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,906,792✔
690
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,916,294!
UNCOV
691
    return terrno;
×
692
  }
693
  return TSDB_CODE_SUCCESS;
4,916,528✔
694
}
695

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

700
  void     *pData = NULL;
1,382,634✔
701
  int       nData = 0;
1,382,634✔
702
  SSkmDbKey skmDbKey;
703
  if (sver <= 0) {
1,382,634✔
704
    SMetaInfo info;
705
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
689,607✔
706
      sver = info.skmVer;
689,531✔
707
    } else {
708
      TBC *pSkmDbC = NULL;
64✔
709
      int  c;
710

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

714
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
64✔
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,382,591!
756
    code = TSDB_CODE_NOT_FOUND;
×
757
    goto _exit;
×
758
  }
759

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

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

775
  tDecoderInit(&dc, pData, nData);
1,382,736✔
776
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
1,382,211✔
777
  tDecoderClear(&dc);
1,382,211✔
778
  tdbFree(pData);
1,382,540✔
779
  if (TSDB_CODE_SUCCESS != code) {
1,382,606!
780
    taosMemoryFree(pSchemaWrapper->pSchema);
×
781
    goto _exit;
×
782
  }
783

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

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

793
_exit:
1,382,792✔
794
  return code;
1,382,792✔
795
}
796

797
// N.B. Called by statusReq per second
798
int64_t metaGetTbNum(SMeta *pMeta) {
732,280✔
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;
732,280✔
805
}
806

807
void metaUpdTimeSeriesNum(SMeta *pMeta) {
184,599✔
808
  int64_t nCtbTimeSeries = 0;
184,599✔
809
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
184,599!
810
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
184,596✔
811
  }
812
}
184,598✔
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;
1,013,394✔
817
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,013,450✔
818
    metaUpdTimeSeriesNum(pMeta);
184,543✔
819
  }
820

821
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,013,463✔
822
}
823

824
// type: 1 reported timeseries
825
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,013,394!
826
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,013,463✔
827
  if (type == 1) {
1,013,463✔
828
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
732,280✔
829
  }
830
  return nTimeSeries;
1,013,460✔
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) {
19,526,486✔
1090
  STag *tag = (STag *)pTag;
19,526,486✔
1091
  if (type == TSDB_DATA_TYPE_JSON) {
19,526,486✔
1092
    return tag;
10,289✔
1093
  }
1094
  bool find = tTagGet(tag, val);
19,516,197✔
1095

1096
  if (!find) {
19,589,293✔
1097
    return NULL;
59,184✔
1098
  }
1099

1100
  return val;
19,530,109✔
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) {
15,922✔
1299
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
15,922✔
1300
  SMetaFltParam *param = arg;
15,922✔
1301

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

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

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

1323
  metaRLock(pMeta);
15,944✔
1324

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

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

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

1332
  tDecoderInit(&dc, pData, nData);
15,960✔
1333

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

1340
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
15,966!
1341
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
1!
1342
  }
1343

1344
  code = TSDB_CODE_INVALID_PARA;
15,966✔
1345

1346
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
84,264✔
1347
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
84,235✔
1348
    if (IS_IDX_ON(schema)) {
84,235✔
1349
      if (schema->colId == param->cid && param->type == schema->type) {
18,669!
1350
        code = 0;
15,937✔
1351
        break;
15,937✔
1352
      }
1353
    }
1354
  }
1355

1356
  TAOS_CHECK_GOTO(code, NULL, END);
15,966✔
1357

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

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

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

1375
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
392✔
1376
        maxSize = 4 * nTagData + 1;
105✔
1377
        buf = taosMemoryCalloc(1, maxSize);
105!
1378
        if (buf == NULL) {
105!
UNCOV
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;
15,532✔
1391
      nTagData = tDataTypes[param->type].bytes;
15,532✔
1392
    }
1393
  }
1394

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

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

1402
  int     count = 0;
15,916✔
1403
  int32_t valid = 0;
15,916✔
1404
  bool    found = false;
15,916✔
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;
15,916✔
1411
  do {
104,800✔
1412
    void   *entryKey = NULL, *entryVal = NULL;
120,716✔
1413
    int32_t nEntryKey, nEntryVal;
1414

1415
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
120,716✔
1416
    if (valid < 0) {
120,474✔
1417
      break;
15,938✔
1418
    }
1419
    if (count > TRY_ERROR_LIMIT) {
108,043✔
1420
      break;
1,780✔
1421
    }
1422

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

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

1440
    terrno = TSDB_CODE_SUCCESS;
104,216✔
1441
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
104,230✔
1442
    if (terrno != TSDB_CODE_SUCCESS) {
104,133!
UNCOV
1443
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
UNCOV
1444
      break;
×
1445
    }
1446
    if (cmp == 0) {
104,196✔
1447
      // match
1448
      tb_uid_t tuid = 0;
88,660✔
1449
      if (IS_VAR_DATA_TYPE(pKey->type)) {
88,660!
1450
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
698✔
1451
      } else {
1452
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
87,962✔
1453
      }
1454
      if (taosArrayPush(pUids, &tuid) == NULL) {
88,964!
UNCOV
1455
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1456
      }
1457
      found = true;
88,964✔
1458
    } else {
1459
      if (param->equal == true) {
15,536✔
1460
        if (count > TRY_ERROR_LIMIT) break;
12,332!
1461
        count++;
12,332✔
1462
      }
1463
    }
1464
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
104,500✔
1465
    if (valid < 0) {
104,455!
UNCOV
1466
      code = valid;
×
UNCOV
1467
      break;
×
1468
    }
1469
  } while (1);
1470

1471
END:
15,967✔
1472
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
15,967!
1473
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
15,964✔
1474
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
15,957!
1475
  tDecoderClear(&dc);
15,957✔
1476
  tdbFree(pData);
15,963✔
1477

1478
  taosMemoryFree(buf);
15,971!
1479
  taosMemoryFree(pKey);
15,962!
1480

1481
  taosMemoryFree(pCursor);
15,961✔
1482

1483
  return code;
15,973✔
1484
}
1485

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

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

1498
  return ret;
332,394✔
1499
}
1500

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

1505
  int32_t isLock = false;
266,248✔
1506
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
266,248!
1507
  for (int i = 0; i < sz; i++) {
598,292✔
1508
    STUidTagInfo *p = taosArrayGet(uidList, i);
332,060✔
1509

1510
    if (i % LIMIT == 0) {
332,037✔
1511
      if (isLock) metaULock(pMeta);
264,681✔
1512

1513
      metaRLock(pMeta);
264,681✔
1514
      isLock = true;
264,688✔
1515
    }
1516

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

UNCOV
1525
        TAOS_RETURN(terrno);
×
1526
      }
1527
      memcpy(p->pTagVal, val, len);
332,221✔
1528
      tdbFree(val);
332,221✔
1529
    } else {
UNCOV
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);
266,232✔
1536
  return 0;
266,259✔
1537
}
1538

1539
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
907,366✔
1540
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
907,366✔
1541
  if (!pCur) {
909,901!
UNCOV
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;
909,901✔
1549
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
909,901✔
1550
  if (numOfElems > 0) {
908,315✔
1551
    pSepecifiedUidMap =
1552
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
72,492✔
1553
    for (int i = 0; i < numOfElems; i++) {
304,324✔
1554
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
231,796✔
1555
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
231,694✔
1556
      if (code) {
231,871!
UNCOV
1557
        metaCloseCtbCursor(pCur);
×
UNCOV
1558
        taosHashCleanup(pSepecifiedUidMap);
×
UNCOV
1559
        return code;
×
1560
      }
1561
    }
1562
  }
1563

1564
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
908,351✔
1565
    while (1) {
2,461,363✔
1566
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,297,085✔
1567
      if (uid == 0) {
3,294,889✔
1568
        break;
837,803✔
1569
      }
1570

1571
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,457,086✔
1572
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,457,086!
1573
      if (!info.pTagVal) {
2,463,214!
UNCOV
1574
        metaCloseCtbCursor(pCur);
×
UNCOV
1575
        taosHashCleanup(pSepecifiedUidMap);
×
UNCOV
1576
        return terrno;
×
1577
      }
1578
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,463,214✔
1579
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,461,363!
1580
        taosMemoryFreeClear(info.pTagVal);
×
UNCOV
1581
        metaCloseCtbCursor(pCur);
×
UNCOV
1582
        taosHashCleanup(pSepecifiedUidMap);
×
UNCOV
1583
        return terrno;
×
1584
      }
1585
    }
1586
  } else {  // only the specified tables need to be added
1587
    while (1) {
341,314✔
1588
      tb_uid_t uid = metaCtbCursorNext(pCur);
413,943✔
1589
      if (uid == 0) {
414,898✔
1590
        break;
72,511✔
1591
      }
1592

1593
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
342,387✔
1594
      if (index == NULL) {
341,704✔
1595
        continue;
110,205✔
1596
      }
1597

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

1611
  taosHashCleanup(pSepecifiedUidMap);
910,314✔
1612
  metaCloseCtbCursor(pCur);
910,067✔
1613
  return TSDB_CODE_SUCCESS;
910,406✔
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) {
54,213,193✔
1619
  int32_t code = 0;
54,213,193✔
1620
  void   *pData = NULL;
54,213,193✔
1621
  int     nData = 0;
54,213,193✔
1622
  int     lock = 0;
54,213,193✔
1623

1624
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
54,213,193!
1625
    lock = 1;
7,674,161✔
1626
  }
1627

1628
  if (!lock) metaRLock(pMeta);
54,213,193✔
1629

1630
  // search cache
1631
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
54,217,139✔
1632
    if (!lock) metaULock(pMeta);
40,247,783✔
1633
    goto _exit;
40,247,841✔
1634
  }
1635

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

1643
  if (!lock) metaULock(pMeta);
40,306✔
1644

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

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

1662
  if (lock) {
40,374✔
1663
    metaRLock(pReader->pMeta);
2,579✔
1664
  }
1665

1666
_exit:
37,795✔
1667
  tdbFree(pData);
54,222,593✔
1668
  return code;
54,220,802✔
1669
}
1670

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

1674
  if (!numOfTables && !numOfCols) goto _exit;
467,389!
1675

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

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

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

1695
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
26,225✔
1696
  if (TSDB_CODE_SUCCESS == code) {
26,224!
1697
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
26,226✔
1698
  }
1699
  if (TSDB_CODE_SUCCESS == code) {
26,226!
1700
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
26,228✔
1701
  }
1702
  metaULock(pVnodeObj->pMeta);
26,223✔
1703
  if (TSDB_CODE_SUCCESS != code) {
26,227!
UNCOV
1704
    goto _exit;
×
1705
  }
1706

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

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

1719
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
26,227✔
1720
  if (ret) {
26,227!
UNCOV
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);
26,227✔
1726

1727
_exit:
467,443✔
1728
  return code;
467,443✔
1729
}
1730

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

1734
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
295,167✔
1735
    stats.ctbNum += deltaCtb;
274,151✔
1736
    stats.colNum += deltaCol;
274,151✔
1737
    if (deltaKeep > 0) {
274,151!
UNCOV
1738
      stats.keep = deltaKeep;
×
1739
    }
1740

1741
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
274,151✔
1742
    if (code) {
274,140!
UNCOV
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
}
295,246✔
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

© 2025 Coveralls, Inc