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

taosdata / TDengine / #3858

17 Apr 2025 01:40PM UTC coverage: 62.968% (+0.5%) from 62.513%
#3858

push

travis-ci

web-flow
docs(opc): add perssit data support (#30783)

156194 of 316378 branches covered (49.37%)

Branch coverage included in aggregate %.

242021 of 316027 relevant lines covered (76.58%)

19473613.85 hits per line

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

56.23
/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,437,444✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
10,437,444✔
22
  metaReaderDoInit(pReader, pMeta, flags);
10,437,444✔
23
  pReader->pAPI = pAPI;
10,444,290✔
24
}
10,444,290✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
14,733,520✔
27
  memset(pReader, 0, sizeof(*pReader));
14,733,520✔
28
  pReader->pMeta = pMeta;
14,733,520✔
29
  pReader->flags = flags;
14,733,520✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
14,733,520!
31
    metaRLock(pMeta);
10,492,963✔
32
  }
33
}
14,734,310✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,926,410✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,926,410!
37
    metaULock(pReader->pMeta);
1,926,838✔
38
    pReader->flags |= META_READER_NOLOCK;
1,927,561✔
39
  }
40
}
1,927,133✔
41

42
void metaReaderClear(SMetaReader *pReader) {
15,924,348✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
15,924,348✔
44
    metaULock(pReader->pMeta);
8,565,363✔
45
  }
46
  tDecoderClear(&pReader->coder);
15,922,521✔
47
  tdbFree(pReader->pBuf);
15,936,554✔
48
  pReader->pBuf = NULL;
15,935,877✔
49
}
15,935,877✔
50

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

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

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
25,101,654✔
65
  if (code) {
24,924,605!
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
24,924,605✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
969,448✔
84
  return true;
969,456✔
85
}
86

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

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

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

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

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
7,471,588✔
105
  if (TSDB_CODE_SUCCESS != code) {
7,475,086✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
122!
107
  }
108

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

112
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
2,225,892✔
113
  SMeta   *pMeta = pReader->pMeta;
2,225,892✔
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,225,892✔
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
60,440✔
119
  }
120

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

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

130
  metaRLock(pMeta);
148,900✔
131

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

137
  metaULock(pMeta);
148,900✔
138

139
  return uid;
148,914✔
140
}
141

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

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

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

178
  SMetaReader *pReader = &mr;
395,955✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
393,195✔
187

188
  metaReaderClear(&mr);
393,195✔
189

190
  return 0;
393,192✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
393,194✔
199
  if (code == 0) *tbType = mr.me.type;
393,187!
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
393,187✔
201
    *suid = mr.me.ctbEntry.suid;
393,166✔
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);
393,187✔
209
  return code;
393,197✔
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) {
955,308✔
245
  SMTbCursor *pTbCur = NULL;
955,308✔
246
  int32_t     code;
247

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
955,308!
249
  if (pTbCur == NULL) {
956,395!
250
    return NULL;
×
251
  }
252

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

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,915,789✔
267
  if (pTbCur) {
1,915,789✔
268
    tdbFree(pTbCur->pKey);
958,385✔
269
    tdbFree(pTbCur->pVal);
958,299✔
270
    if (!pTbCur->paused) {
958,389✔
271
      metaReaderClear(&pTbCur->mr);
79,411✔
272
      if (pTbCur->pDbc) {
79,398!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
79,399✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
958,378!
277
  }
278
}
1,915,872✔
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
879,756✔
281
  if (!pTbCur->paused) {
879,756!
282
    metaReaderClear(&pTbCur->mr);
879,847✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
879,996✔
284
    pTbCur->paused = 1;
879,746✔
285
  }
286
}
879,655✔
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
957,562✔
288
  int32_t code = 0;
957,562✔
289
  int32_t lino;
290
  int8_t  locked = 0;
957,562✔
291
  if (pTbCur->paused) {
957,562!
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
957,934✔
293
    locked = 1;
958,985✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
958,985✔
295
    if (code != 0) {
957,340!
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
957,340✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
956,418✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
957,058!
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;
957,980✔
318
  }
319

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

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
12,034,347✔
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,034,347✔
334
    if (ret < 0) {
12,086,685✔
335
      return ret;
958,339✔
336
    }
337

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

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
11,163,277✔
341
    if (ret) return ret;
11,097,714!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
11,097,714✔
344
      continue;
2,395,848✔
345
    }
346

347
    break;
8,701,866✔
348
  }
349

350
  return 0;
8,701,866✔
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) {
4,774,494✔
382
  void           *pData = NULL;
4,774,494✔
383
  int             nData = 0;
4,774,494✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
4,774,494✔
386
  SSchemaWrapper *pSchema = NULL;
4,774,494✔
387
  SDecoder        dc = {0};
4,774,494✔
388
  if (lock) {
4,774,494✔
389
    metaRLock(pMeta);
4,762,854✔
390
  }
391
_query:
4,785,553✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
4,865,438✔
393
    goto _err;
746✔
394
  }
395

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

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

402
  SMetaEntry me = {0};
4,869,717✔
403
  tDecoderInit(&dc, pData, nData);
4,869,717✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
4,865,693✔
405
  if (code) {
4,868,619!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
4,868,619✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,425,620✔
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,421,643✔
412
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
4,421,643✔
413
      tDecoderClear(&dc);
4,421,644✔
414
      goto _exit;
4,424,110✔
415
    }
416
  } else if (me.type == TSDB_CHILD_TABLE) {
442,999✔
417
    uid = me.ctbEntry.suid;
79,850✔
418
    tDecoderClear(&dc);
79,850✔
419
    goto _query;
79,885✔
420
  } else {
421
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
363,149✔
422
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
363,213✔
423
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
363,213✔
424
      tDecoderClear(&dc);
363,193✔
425
      goto _exit;
363,227✔
426
    }
427
  }
428
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
4,369✔
429
  tDecoderClear(&dc);
4,369✔
430

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

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

443
_exit:
4,787,482✔
444
  if (lock) {
4,787,482✔
445
    metaULock(pMeta);
4,757,137✔
446
  }
447
  tdbFree(pData);
4,791,886✔
448
  return pSchema;
4,790,694✔
449

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

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

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

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

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

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

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

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

505
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,537,337!
506
  if (pCtbCur == NULL) {
3,548,041!
507
    return NULL;
×
508
  }
509

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

515
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,548,041✔
516
  if (ret < 0) {
3,542,265!
517
    return NULL;
×
518
  }
519
  return pCtbCur;
3,542,265✔
520
}
521

522
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,547,708✔
523
  if (pCtbCur) {
3,547,708!
524
    if (!pCtbCur->paused) {
3,548,146✔
525
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,505,075!
526
      if (pCtbCur->pCur) {
3,506,203!
527
        tdbTbcClose(pCtbCur->pCur);
3,506,225✔
528
      }
529
    }
530
    tdbFree(pCtbCur->pKey);
3,548,108✔
531
    tdbFree(pCtbCur->pVal);
3,548,293✔
532
  }
533
  taosMemoryFree(pCtbCur);
3,548,779!
534
}
3,549,420✔
535

536
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,342✔
537
  if (!pCtbCur->paused) {
43,342!
538
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,357✔
539
    if (pCtbCur->lock) {
43,387!
540
      metaULock(pCtbCur->pMeta);
43,388✔
541
    }
542
    pCtbCur->paused = 1;
43,389✔
543
  }
544
}
43,374✔
545

546
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,540,825✔
547
  if (pCtbCur->paused) {
3,540,825!
548
    pCtbCur->paused = 0;
3,543,564✔
549

550
    if (pCtbCur->lock) {
3,543,564✔
551
      metaRLock(pCtbCur->pMeta);
3,524,587✔
552
    }
553
    int ret = 0;
3,543,614✔
554
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,543,614✔
555
    if (ret < 0) {
3,544,489!
556
      metaCloseCtbCursor(pCtbCur);
×
557
      return -1;
×
558
    }
559

560
    if (first) {
3,545,439!
561
      SCtbIdxKey ctbIdxKey;
562
      // move to the suid
563
      ctbIdxKey.suid = pCtbCur->suid;
3,545,439✔
564
      ctbIdxKey.uid = INT64_MIN;
3,545,439✔
565
      int c = 0;
3,545,439✔
566
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,545,439✔
567
      if (c > 0) {
3,544,763✔
568
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
619,704✔
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,541,536✔
581
}
582

583
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
14,661,513✔
584
  int         ret;
585
  SCtbIdxKey *pCtbIdxKey;
586

587
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
14,661,513✔
588
  if (ret < 0) {
14,658,082✔
589
    return 0;
679,410✔
590
  }
591

592
  pCtbIdxKey = pCtbCur->pKey;
13,978,672✔
593
  if (pCtbIdxKey->suid > pCtbCur->suid) {
13,978,672✔
594
    return 0;
2,870,275✔
595
  }
596

597
  return pCtbIdxKey->uid;
11,108,397✔
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) {
162,807✔
611
  SMStbCursor *pStbCur = NULL;
162,807✔
612
  int          ret = 0;
162,807✔
613
  int          c = 0;
162,807✔
614

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

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

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

639
  return pStbCur;
162,812✔
640
}
641

642
void metaCloseStbCursor(SMStbCursor *pStbCur) {
162,812✔
643
  if (pStbCur) {
162,812!
644
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
162,813!
645
    if (pStbCur->pCur) {
162,813!
646
      tdbTbcClose(pStbCur->pCur);
162,814✔
647

648
      tdbFree(pStbCur->pKey);
162,813✔
649
      tdbFree(pStbCur->pVal);
162,810✔
650
    }
651

652
    taosMemoryFree(pStbCur);
162,811!
653
  }
654
}
162,813✔
655

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

659
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
301,722✔
660
  if (ret < 0) {
301,706✔
661
    return 0;
162,813✔
662
  }
663
  return *(tb_uid_t *)pStbCur->pKey;
138,893✔
664
}
665

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

670
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
4,630,275✔
671
  if (!pSW) return NULL;
4,642,764✔
672

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

675
  taosMemoryFree(pSW->pSchema);
4,645,507!
676
  taosMemoryFree(pSW);
4,645,608✔
677
  return pTSchema;
4,644,651✔
678
}
679

680
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
9,440✔
681
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
9,440✔
682
  if (*ppTSchema == NULL) {
9,472✔
683
    return terrno;
1✔
684
  }
685
  return TSDB_CODE_SUCCESS;
9,471✔
686
}
687

688
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,622,402✔
689
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,622,402✔
690
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,634,701!
691
    return terrno;
8,064✔
692
  }
693
  return TSDB_CODE_SUCCESS;
4,626,638✔
694
}
695

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

700
  void     *pData = NULL;
1,434,986✔
701
  int       nData = 0;
1,434,986✔
702
  SSkmDbKey skmDbKey;
703
  if (sver <= 0) {
1,434,986✔
704
    SMetaInfo info;
705
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
742,891✔
706
      sver = info.skmVer;
742,820✔
707
    } else {
708
      TBC *pSkmDbC = NULL;
70✔
709
      int  c;
710

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

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

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

725
      if (c == 0) {
80!
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) {
80!
734
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
735
      }
736

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

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

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

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

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

760
  skmDbKey.uid = suid ? suid : uid;
1,434,971✔
761
  skmDbKey.sver = sver;
1,434,971✔
762
  metaRLock(pMeta);
1,434,971✔
763
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
1,435,188!
764
    metaULock(pMeta);
×
765
    code = TSDB_CODE_NOT_FOUND;
×
766
    goto _exit;
×
767
  }
768
  metaULock(pMeta);
1,435,040✔
769

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

775
  tDecoderInit(&dc, pData, nData);
1,435,125✔
776
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
1,434,044✔
777
  tDecoderClear(&dc);
1,434,044✔
778
  tdbFree(pData);
1,434,899✔
779
  if (TSDB_CODE_SUCCESS != code) {
1,434,935!
780
    taosMemoryFree(pSchemaWrapper->pSchema);
×
781
    goto _exit;
×
782
  }
783

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

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

793
_exit:
1,435,099✔
794
  return code;
1,435,099✔
795
}
796

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

807
void metaUpdTimeSeriesNum(SMeta *pMeta) {
162,806✔
808
  int64_t nCtbTimeSeries = 0;
162,806✔
809
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
162,806!
810
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
162,810✔
811
  }
812
}
162,811✔
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,227,449✔
817
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,227,473✔
818
    metaUpdTimeSeriesNum(pMeta);
160,292✔
819
  }
820

821
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,227,487✔
822
}
823

824
// type: 1 reported timeseries
825
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,227,449!
826
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,227,487✔
827
  if (type == 1) {
1,227,487✔
828
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
1,049,653✔
829
  }
830
  return nTimeSeries;
1,227,496✔
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) {
17,284,386✔
1090
  STag *tag = (STag *)pTag;
17,284,386✔
1091
  if (type == TSDB_DATA_TYPE_JSON) {
17,284,386✔
1092
    return tag;
10,033✔
1093
  }
1094
  bool find = tTagGet(tag, val);
17,274,353✔
1095

1096
  if (!find) {
17,299,606✔
1097
    return NULL;
147,473✔
1098
  }
1099

1100
  return val;
17,152,133✔
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) {
6✔
1116
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1117
  SMetaFltParam *param = arg;
6✔
1118
  int32_t        ret = 0;
6✔
1119

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

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

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

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

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

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

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

1158
    terrno = TSDB_CODE_SUCCESS;
35,864✔
1159
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
36,320✔
1160
    if (terrno != TSDB_CODE_SUCCESS) {
36,137!
1161
      ret = terrno;
×
1162
      break;
×
1163
    }
1164
    if (cmp == 0) {
36,340!
1165
      if (taosArrayPush(pUids, &p->uid) == NULL) {
73,722!
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);
37,382✔
1176
    if (valid < 0) break;
37,490!
1177
  } while (1);
1178

1179
END:
6✔
1180
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1181
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1182
  taosMemoryFree(pCursor);
6!
1183
  return ret;
6✔
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) {
4,776✔
1299
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
4,776✔
1300
  SMetaFltParam *param = arg;
4,776✔
1301

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

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

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

1323
  metaRLock(pMeta);
4,782✔
1324

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

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

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

1332
  tDecoderInit(&dc, pData, nData);
4,783✔
1333

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

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

1344
  code = TSDB_CODE_INVALID_PARA;
4,781✔
1345
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
7,064✔
1346
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
6,192✔
1347
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
6,192!
1348
      code = 0;
868✔
1349
    } else {
1350
      TAOS_CHECK_GOTO(code, NULL, END);
5,324✔
1351
    }
1352
  }
1353

1354
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
872✔
1355
  if (code != 0) {
869✔
1356
    TAOS_CHECK_GOTO(terrno, NULL, END);
1!
1357
  }
1358

1359
  int32_t maxSize = 0;
868✔
1360
  int32_t nTagData = 0;
868✔
1361
  void   *tagData = NULL;
868✔
1362

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

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

1378
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
×
1379
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1380
        }
1381

1382
        tagData = buf;
×
1383
        nTagData = maxSize;
×
1384
      }
1385
    } else {
1386
      tagData = param->val;
680✔
1387
      nTagData = tDataTypes[param->type].bytes;
680✔
1388
    }
1389
  }
1390

1391
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
868!
1392
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1393
                  NULL, END);
1394

1395
  int cmp = 0;
868✔
1396
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
868!
1397

1398
  int     count = 0;
869✔
1399
  int32_t valid = 0;
869✔
1400
  bool    found = false;
869✔
1401

1402
  static const int8_t TRY_ERROR_LIMIT = 1;
1403

1404
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1405
  /// target:                        [suid, cid2, type2]
1406
  int diffCidCount = 0;
869✔
1407
  do {
12,444✔
1408
    void   *entryKey = NULL, *entryVal = NULL;
13,313✔
1409
    int32_t nEntryKey, nEntryVal;
1410

1411
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
13,313✔
1412
    if (valid < 0) {
13,280✔
1413
      code = valid;
718✔
1414
    }
1415
    if (count > TRY_ERROR_LIMIT) {
13,280✔
1416
      break;
869✔
1417
    }
1418

1419
    STagIdxKey *p = entryKey;
13,191✔
1420
    if (p == NULL) break;
13,191✔
1421

1422
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
12,499!
1423
      if (found == true) break;  //
150✔
1424
      if (diffCidCount > TRY_ERROR_LIMIT) break;
62!
1425
      diffCidCount++;
62✔
1426
      count++;
62✔
1427
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
62✔
1428
      if (valid < 0) {
62!
1429
        code = valid;
×
1430
        break;
×
1431
      } else {
1432
        continue;
62✔
1433
      }
1434
    }
1435

1436
    terrno = TSDB_CODE_SUCCESS;
12,349✔
1437
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
12,351✔
1438
    if (terrno != TSDB_CODE_SUCCESS) {
12,348!
1439
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1440
      break;
×
1441
    }
1442
    if (cmp == 0) {
12,358✔
1443
      // match
1444
      tb_uid_t tuid = 0;
11,369✔
1445
      if (IS_VAR_DATA_TYPE(pKey->type)) {
11,369!
1446
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
332✔
1447
      } else {
1448
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
11,037✔
1449
      }
1450
      if (taosArrayPush(pUids, &tuid) == NULL) {
11,396!
1451
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1452
      }
1453
      found = true;
11,396✔
1454
    } else {
1455
      if (param->equal == true) {
989✔
1456
        if (count > TRY_ERROR_LIMIT) break;
275!
1457
        count++;
275✔
1458
      }
1459
    }
1460
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
12,385✔
1461
    if (valid < 0) {
12,382!
1462
      code = valid;
×
1463
      break;
×
1464
    }
1465
  } while (1);
1466

1467
END:
4,778✔
1468
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
4,778!
1469
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
4,782✔
1470
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
4,782!
1471
  tDecoderClear(&dc);
4,782✔
1472
  tdbFree(pData);
4,781✔
1473

1474
  taosMemoryFree(buf);
4,784!
1475
  taosMemoryFree(pKey);
4,785!
1476

1477
  taosMemoryFree(pCursor);
4,783!
1478

1479
  return code;
4,783✔
1480
}
1481

1482
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
232,404✔
1483
  int ret = 0;
232,404✔
1484
  if (lock) {
232,404!
1485
    metaRLock(pMeta);
×
1486
  }
1487

1488
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
232,404✔
1489
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
232,404✔
1490
  if (lock) {
232,408!
1491
    metaULock(pMeta);
×
1492
  }
1493

1494
  return ret;
232,408✔
1495
}
1496

1497
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
231,844✔
1498
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
231,844✔
1499
  const int32_t LIMIT = 128;
231,844✔
1500

1501
  int32_t isLock = false;
231,844✔
1502
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
231,844!
1503
  for (int i = 0; i < sz; i++) {
464,247✔
1504
    STUidTagInfo *p = taosArrayGet(uidList, i);
232,405✔
1505

1506
    if (i % LIMIT == 0) {
232,402✔
1507
      if (isLock) metaULock(pMeta);
231,820!
1508

1509
      metaRLock(pMeta);
231,820✔
1510
      isLock = true;
231,825✔
1511
    }
1512

1513
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1514
    void   *val = NULL;
232,407✔
1515
    int32_t len = 0;
232,407✔
1516
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
232,407!
1517
      p->pTagVal = taosMemoryMalloc(len);
232,408!
1518
      if (!p->pTagVal) {
232,405!
1519
        if (isLock) metaULock(pMeta);
×
1520

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

1535
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
801,188✔
1536
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
801,188✔
1537
  if (!pCur) {
802,963!
1538
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1539
  }
1540

1541
  // If len > 0 means there already have uids, and we only want the
1542
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1543
  // in the hash map, that may require a lot of memory
1544
  SHashObj *pSepecifiedUidMap = NULL;
802,963✔
1545
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
802,963✔
1546
  if (numOfElems > 0) {
802,068✔
1547
    pSepecifiedUidMap =
1548
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
96,259✔
1549
    for (int i = 0; i < numOfElems; i++) {
390,334✔
1550
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
294,041✔
1551
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
293,890✔
1552
      if (code) {
294,170!
1553
        metaCloseCtbCursor(pCur);
×
1554
        taosHashCleanup(pSepecifiedUidMap);
×
1555
        return code;
×
1556
      }
1557
    }
1558
  }
1559

1560
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
802,102✔
1561
    while (1) {
2,058,692✔
1562
      tb_uid_t uid = metaCtbCursorNext(pCur);
2,764,109✔
1563
      if (uid == 0) {
2,759,221✔
1564
        break;
708,368✔
1565
      }
1566

1567
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,050,853✔
1568
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,050,853!
1569
      if (!info.pTagVal) {
2,059,095!
1570
        metaCloseCtbCursor(pCur);
×
1571
        taosHashCleanup(pSepecifiedUidMap);
×
1572
        return terrno;
×
1573
      }
1574
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,059,095✔
1575
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,058,692!
1576
        taosMemoryFreeClear(info.pTagVal);
×
1577
        metaCloseCtbCursor(pCur);
×
1578
        taosHashCleanup(pSepecifiedUidMap);
×
1579
        return terrno;
×
1580
      }
1581
    }
1582
  } else {  // only the specified tables need to be added
1583
    while (1) {
397,719✔
1584
      tb_uid_t uid = metaCtbCursorNext(pCur);
494,404✔
1585
      if (uid == 0) {
494,064✔
1586
        break;
96,274✔
1587
      }
1588

1589
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
397,790✔
1590
      if (index == NULL) {
398,183✔
1591
        continue;
104,409✔
1592
      }
1593

1594
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
293,774✔
1595
      if (pTagInfo->pTagVal == NULL) {
293,691!
1596
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
294,105✔
1597
        if (!pTagInfo->pTagVal) {
293,724!
1598
          metaCloseCtbCursor(pCur);
×
1599
          taosHashCleanup(pSepecifiedUidMap);
×
1600
          return terrno;
×
1601
        }
1602
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
293,724✔
1603
      }
1604
    }
1605
  }
1606

1607
  taosHashCleanup(pSepecifiedUidMap);
804,642✔
1608
  metaCloseCtbCursor(pCur);
804,493✔
1609
  return TSDB_CODE_SUCCESS;
804,689✔
1610
}
1611

1612
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1613

1614
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
49,712,721✔
1615
  int32_t code = 0;
49,712,721✔
1616
  void   *pData = NULL;
49,712,721✔
1617
  int     nData = 0;
49,712,721✔
1618
  int     lock = 0;
49,712,721✔
1619

1620
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
49,712,721!
1621
    lock = 1;
7,475,478✔
1622
  }
1623

1624
  if (!lock) metaRLock(pMeta);
49,712,721✔
1625

1626
  // search cache
1627
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
49,722,550✔
1628
    if (!lock) metaULock(pMeta);
35,892,404✔
1629
    goto _exit;
35,891,762✔
1630
  }
1631

1632
  // search TDB
1633
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
13,833,777✔
1634
    // not found
1635
    if (!lock) metaULock(pMeta);
13,802,137✔
1636
    goto _exit;
13,802,172✔
1637
  }
1638

1639
  if (!lock) metaULock(pMeta);
32,387✔
1640

1641
  pInfo->uid = uid;
32,387✔
1642
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
32,387✔
1643
  pInfo->version = ((SUidIdxVal *)pData)->version;
32,387✔
1644
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
32,387✔
1645

1646
  if (lock) {
32,387✔
1647
    metaULock(pReader->pMeta);
2,320✔
1648
    // metaReaderReleaseLock(pReader);
1649
  }
1650
  // upsert the cache
1651
  metaWLock(pMeta);
32,388✔
1652
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
32,402✔
1653
  if (ret != 0) {
32,401!
1654
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1655
  }
1656
  metaULock(pMeta);
32,401✔
1657

1658
  if (lock) {
32,401✔
1659
    metaRLock(pReader->pMeta);
2,321✔
1660
  }
1661

1662
_exit:
30,080✔
1663
  tdbFree(pData);
49,726,336✔
1664
  return code;
49,724,181✔
1665
}
1666

1667
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
308,671✔
1668
  int32_t code = 0;
308,671✔
1669

1670
  if (!numOfTables && !numOfCols) goto _exit;
308,671!
1671

1672
  SVnode *pVnodeObj = pVnode;
308,671✔
1673
  metaRLock(pVnodeObj->pMeta);
308,671✔
1674

1675
  // fast path: search cache
1676
  SMetaStbStats state = {0};
308,735✔
1677
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
308,735✔
1678
    metaULock(pVnodeObj->pMeta);
285,608✔
1679
    if (numOfTables) *numOfTables = state.ctbNum;
285,602✔
1680
    if (numOfCols) *numOfCols = state.colNum;
285,602✔
1681
    goto _exit;
285,602✔
1682
  }
1683

1684
  // slow path: search TDB
1685
  int64_t ctbNum = 0;
23,128✔
1686
  int32_t colNum = 0;
23,128✔
1687
  int64_t keep = 0;
23,128✔
1688
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
23,128✔
1689
  if (TSDB_CODE_SUCCESS == code) {
23,122!
1690
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
23,122✔
1691
  }
1692
  if (TSDB_CODE_SUCCESS == code) {
23,125✔
1693
    code = vnodeGetStbKeep(pVnode, uid, &keep);
23,123✔
1694
  }
1695
  metaULock(pVnodeObj->pMeta);
23,129✔
1696
  if (TSDB_CODE_SUCCESS != code) {
23,127!
1697
    goto _exit;
×
1698
  }
1699

1700
  if (numOfTables) *numOfTables = ctbNum;
23,127✔
1701
  if (numOfCols) *numOfCols = colNum;
23,127✔
1702

1703
  state.uid = uid;
23,127✔
1704
  state.ctbNum = ctbNum;
23,127✔
1705
  state.colNum = colNum;
23,127✔
1706
  state.keep = keep;
23,127✔
1707
  // upsert the cache
1708
  metaWLock(pVnodeObj->pMeta);
23,127✔
1709

1710
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
23,127✔
1711
  if (ret) {
23,123!
1712
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64, uid, ctbNum,
×
1713
              colNum, keep);
1714
  }
1715

1716
  metaULock(pVnodeObj->pMeta);
23,123✔
1717

1718
_exit:
308,707✔
1719
  return code;
308,707✔
1720
}
1721

1722
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
177,869✔
1723
  SMetaStbStats stats = {0};
177,869✔
1724

1725
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
177,869✔
1726
    stats.ctbNum += deltaCtb;
162,891✔
1727
    stats.colNum += deltaCol;
162,891✔
1728
    if (deltaKeep > 0) {
162,891!
1729
      stats.keep = deltaKeep;
×
1730
    }
1731

1732
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
162,891✔
1733
    if (code) {
162,891!
1734
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1735
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1736
    }
1737
  }
1738
}
177,912✔
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