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

taosdata / TDengine / #3544

30 Nov 2024 03:06AM UTC coverage: 60.88% (+0.04%) from 60.842%
#3544

push

travis-ci

web-flow
Merge pull request #28988 from taosdata/main

merge: from main to 3.0 branch

120724 of 253479 branches covered (47.63%)

Branch coverage included in aggregate %.

407 of 489 new or added lines in 21 files covered. (83.23%)

1148 existing lines in 113 files now uncovered.

201919 of 276488 relevant lines covered (73.03%)

18898587.44 hits per line

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

57.27
/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,328,995✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
10,328,995✔
22
  metaReaderDoInit(pReader, pMeta, flags);
10,328,995✔
23
  pReader->pAPI = pAPI;
10,333,260✔
24
}
10,333,260✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
14,120,263✔
27
  memset(pReader, 0, sizeof(*pReader));
14,120,263✔
28
  pReader->pMeta = pMeta;
14,120,263✔
29
  pReader->flags = flags;
14,120,263✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
14,120,263!
31
    metaRLock(pMeta);
10,563,987✔
32
  }
33
}
14,120,984✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,239,117✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,239,117!
37
    metaULock(pReader->pMeta);
2,239,382✔
38
    pReader->flags |= META_READER_NOLOCK;
2,240,043✔
39
  }
40
}
2,239,778✔
41

42
void metaReaderClear(SMetaReader *pReader) {
14,829,815✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
14,829,815✔
44
    metaULock(pReader->pMeta);
8,323,521✔
45
  }
46
  tDecoderClear(&pReader->coder);
14,828,495✔
47
  tdbFree(pReader->pBuf);
14,840,726✔
48
  pReader->pBuf = NULL;
14,839,832✔
49
}
14,839,832✔
50

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

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

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

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

71
  return 0;
21,018,406✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
783,999✔
84
  return true;
784,002✔
85
}
86

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

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

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

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

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
7,893,415✔
105
  if (TSDB_CODE_SUCCESS != code) {
7,896,552✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
36!
107
  }
108

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

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

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

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

130
  metaRLock(pMeta);
387,676✔
131

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

137
  metaULock(pMeta);
387,687✔
138

139
  return uid;
387,721✔
140
}
141

142
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
18,842✔
143
  int         code = 0;
18,842✔
144
  SMetaReader mr = {0};
18,842✔
145
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
18,842✔
146
  code = metaReaderGetTableEntryByUid(&mr, uid);
18,810✔
147
  if (code < 0) {
18,735✔
148
    metaReaderClear(&mr);
132✔
149
    return code;
132✔
150
  }
151

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

155
  return 0;
18,710✔
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
  strncpy(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) {
426,861✔
174
  int         code = 0;
426,861✔
175
  SMetaReader mr = {0};
426,861✔
176
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
426,861✔
177

178
  SMetaReader *pReader = &mr;
426,868✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
424,253✔
187

188
  metaReaderClear(&mr);
424,253✔
189

190
  return 0;
424,253✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
424,251✔
199
  if (code == 0) *tbType = mr.me.type;
424,242!
200

201
  metaReaderClear(&mr);
424,242✔
202
  return code;
424,250✔
203
}
204

205
int metaReadNext(SMetaReader *pReader) {
×
206
  SMeta *pMeta = pReader->pMeta;
×
207

208
  // TODO
209

210
  return 0;
×
211
}
212

213
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
214
  int         code = -1;
×
215
  SMetaReader mr = {0};
×
216
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
217
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
218
  if (code < 0) {
×
219
    goto _exit;
×
220
  }
221
  if (mr.me.type == TSDB_CHILD_TABLE) {
×
222
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
223
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
×
224
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
225
  } else {
226
    goto _exit;
×
227
  }
228

229
  code = 0;
×
230

231
_exit:
×
232
  metaReaderClear(&mr);
×
233
  return code;
×
234
}
235

236
#if 1  // ===================================================
237
SMTbCursor *metaOpenTbCursor(void *pVnode) {
632,530✔
238
  SMTbCursor *pTbCur = NULL;
632,530✔
239
  int32_t     code;
240

241
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
632,530✔
242
  if (pTbCur == NULL) {
632,838!
243
    return NULL;
×
244
  }
245

246
  SVnode *pVnodeObj = pVnode;
632,838✔
247
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
248
  pTbCur->pMeta = pVnodeObj->pMeta;
632,838✔
249
  pTbCur->paused = 1;
632,838✔
250
  code = metaResumeTbCursor(pTbCur, 1, 0);
632,838✔
251
  if (code) {
632,895✔
252
    terrno = code;
144✔
253
    taosMemoryFree(pTbCur);
×
254
    return NULL;
×
255
  }
256
  return pTbCur;
632,751✔
257
}
258

259
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,266,588✔
260
  if (pTbCur) {
1,266,588✔
261
    tdbFree(pTbCur->pKey);
634,022✔
262
    tdbFree(pTbCur->pVal);
633,984✔
263
    if (!pTbCur->paused) {
634,039✔
264
      metaReaderClear(&pTbCur->mr);
50,473✔
265
      if (pTbCur->pDbc) {
50,471!
266
        tdbTbcClose((TBC *)pTbCur->pDbc);
50,471✔
267
      }
268
    }
269
    taosMemoryFree(pTbCur);
634,038✔
270
  }
271
}
1,266,642✔
272

273
void metaPauseTbCursor(SMTbCursor *pTbCur) {
584,377✔
274
  if (!pTbCur->paused) {
584,377!
275
    metaReaderClear(&pTbCur->mr);
584,421✔
276
    tdbTbcClose((TBC *)pTbCur->pDbc);
584,509✔
277
    pTbCur->paused = 1;
584,450✔
278
  }
279
}
584,406✔
280
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
634,066✔
281
  int32_t code = 0;
634,066✔
282
  int32_t lino;
283
  int8_t  locked = 0;
634,066✔
284
  if (pTbCur->paused) {
634,066!
285
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
634,249✔
286
    locked = 1;
634,658✔
287
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
634,658✔
288
    if (code != 0) {
633,801!
289
      TSDB_CHECK_CODE(code, lino, _exit);
×
290
    }
291

292
    if (first) {
633,801✔
293
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
632,879✔
294
      TSDB_CHECK_CODE(code, lino, _exit);
633,067!
295
    } else {
296
      int c = 1;
922✔
297
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
922✔
298
      TSDB_CHECK_CODE(code, lino, _exit);
922!
299
      if (c == 0) {
922!
300
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
922!
301
      } else if (c < 0) {
×
302
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
303
        TSDB_CHECK_CODE(code, lino, _exit);
×
304
      } else {
305
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
306
        TSDB_CHECK_CODE(code, lino, _exit);
×
307
      }
308
    }
309

310
    pTbCur->paused = 0;
633,989✔
311
  }
312

313
_exit:
×
314
  if (code != 0 && locked) {
633,806!
315
    metaReaderReleaseLock(&pTbCur->mr);
×
316
  }
317
  return code;
633,698✔
318
}
319

320
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
8,317,099✔
321
  int    ret;
322
  void  *pBuf;
323
  STbCfg tbCfg;
324

325
  for (;;) {
326
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
8,317,099✔
327
    if (ret < 0) {
8,329,443✔
328
      return ret;
633,965✔
329
    }
330

331
    tDecoderClear(&pTbCur->mr.coder);
7,695,478✔
332

333
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
7,712,662✔
334
    if (ret) return ret;
7,688,561!
335

336
    if (pTbCur->mr.me.type == jumpTableType) {
7,688,561✔
337
      continue;
1,232,001✔
338
    }
339

340
    break;
6,456,560✔
341
  }
342

343
  return 0;
6,456,560✔
344
}
345

346
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
347
  int    ret;
348
  void  *pBuf;
349
  STbCfg tbCfg;
350

351
  for (;;) {
352
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
353
    if (ret < 0) {
×
354
      return -1;
×
355
    }
356

357
    tDecoderClear(&pTbCur->mr.coder);
×
358

359
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
360
    if (ret < 0) {
×
361
      return ret;
×
362
    }
363

364
    if (pTbCur->mr.me.type == jumpTableType) {
×
365
      continue;
×
366
    }
367

368
    break;
×
369
  }
370

371
  return 0;
×
372
}
373

374
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, int64_t *createTime) {
5,084,270✔
375
  void           *pData = NULL;
5,084,270✔
376
  int             nData = 0;
5,084,270✔
377
  int64_t         version;
378
  SSchemaWrapper  schema = {0};
5,084,270✔
379
  SSchemaWrapper *pSchema = NULL;
5,084,270✔
380
  SDecoder        dc = {0};
5,084,270✔
381
  if (lock) {
5,084,270✔
382
    metaRLock(pMeta);
5,063,522✔
383
  }
384
_query:
5,096,142✔
385
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
5,175,819✔
386
    goto _err;
49✔
387
  }
388

389
  version = ((SUidIdxVal *)pData)[0].version;
5,181,413✔
390

391
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
5,181,413!
392
    goto _err;
×
393
  }
394

395
  SMetaEntry me = {0};
5,183,929✔
396
  tDecoderInit(&dc, pData, nData);
5,183,929✔
397
  int32_t code = metaDecodeEntry(&dc, &me);
5,178,698✔
398
  if (code) {
5,180,778!
399
    tDecoderClear(&dc);
×
400
    goto _err;
×
401
  }
402
  if (me.type == TSDB_SUPER_TABLE) {
5,180,778✔
403
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,757,639!
404
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,760,489✔
405
      tDecoderClear(&dc);
4,760,489✔
406
      goto _exit;
4,758,707✔
407
    }
408
  } else if (me.type == TSDB_CHILD_TABLE) {
423,139✔
409
    uid = me.ctbEntry.suid;
79,630✔
410
    if (createTime != NULL){
79,630✔
411
      *createTime = me.ctbEntry.btime;
77,472✔
412
    }
413
    tDecoderClear(&dc);
79,630✔
414
    goto _query;
79,677✔
415
  } else {
416
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
343,509✔
417
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
343,651✔
418
      tDecoderClear(&dc);
343,651✔
419
      goto _exit;
343,624✔
420
    }
421
  }
422
  tDecoderClear(&dc);
×
423

424
  // query from skm db
425
  if (tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData) < 0) {
130!
426
    goto _err;
×
427
  }
428

429
  tDecoderInit(&dc, pData, nData);
130✔
430
  if (tDecodeSSchemaWrapperEx(&dc, &schema) != 0) {
130!
431
    goto _err;
×
432
  }
433
  pSchema = tCloneSSchemaWrapper(&schema);
130✔
434
  tDecoderClear(&dc);
130✔
435

436
_exit:
5,102,461✔
437
  if (lock) {
5,102,461✔
438
    metaULock(pMeta);
5,068,390✔
439
  }
440
  tdbFree(pData);
5,104,749✔
441
  return pSchema;
5,104,535✔
442

443
_err:
49✔
444
  if (lock) {
49!
445
    metaULock(pMeta);
49✔
446
  }
447
  tdbFree(pData);
49✔
448
  return NULL;
49✔
449
}
450

451
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
3,970,411✔
452
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
3,970,411✔
453
  SMCtbCursor *pCtbCur = NULL;
3,970,411✔
454
  SCtbIdxKey   ctbIdxKey;
455
  int          ret = 0;
3,970,411✔
456
  int          c = 0;
3,970,411✔
457

458
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,970,411✔
459
  if (pCtbCur == NULL) {
3,980,757!
460
    return NULL;
×
461
  }
462

463
  pCtbCur->pMeta = pMeta;
3,980,757✔
464
  pCtbCur->suid = uid;
3,980,757✔
465
  pCtbCur->lock = lock;
3,980,757✔
466
  pCtbCur->paused = 1;
3,980,757✔
467

468
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,980,757✔
469
  if (ret < 0) {
3,976,002!
470
    return NULL;
×
471
  }
472
  return pCtbCur;
3,976,002✔
473
}
474

475
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,980,157✔
476
  if (pCtbCur) {
3,980,157!
477
    if (!pCtbCur->paused) {
3,980,424✔
478
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,937,300!
479
      if (pCtbCur->pCur) {
3,938,361✔
480
        tdbTbcClose(pCtbCur->pCur);
3,938,204✔
481
      }
482
    }
483
    tdbFree(pCtbCur->pKey);
3,980,463✔
484
    tdbFree(pCtbCur->pVal);
3,980,066✔
485
  }
486
  taosMemoryFree(pCtbCur);
3,980,161✔
487
}
3,981,734✔
488

489
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,259✔
490
  if (!pCtbCur->paused) {
43,259!
491
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,273✔
492
    if (pCtbCur->lock) {
43,315!
493
      metaULock(pCtbCur->pMeta);
43,319✔
494
    }
495
    pCtbCur->paused = 1;
43,297✔
496
  }
497
}
43,283✔
498

499
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,973,336✔
500
  if (pCtbCur->paused) {
3,973,336!
501
    pCtbCur->paused = 0;
3,976,605✔
502

503
    if (pCtbCur->lock) {
3,976,605✔
504
      metaRLock(pCtbCur->pMeta);
3,956,436✔
505
    }
506
    int ret = 0;
3,975,838✔
507
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,975,838✔
508
    if (ret < 0) {
3,977,175!
UNCOV
509
      metaCloseCtbCursor(pCtbCur);
×
510
      return -1;
×
511
    }
512

513
    if (first) {
3,977,891!
514
      SCtbIdxKey ctbIdxKey;
515
      // move to the suid
516
      ctbIdxKey.suid = pCtbCur->suid;
3,977,891✔
517
      ctbIdxKey.uid = INT64_MIN;
3,977,891✔
518
      int c = 0;
3,977,891✔
519
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,977,891✔
520
      if (c > 0) {
3,976,964✔
521
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
898,404✔
522
      }
523
    } else {
524
      int c = 0;
×
525
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
526
      if (c < 0) {
×
527
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
528
      } else {
529
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
530
      }
531
    }
532
  }
533
  return 0;
3,973,471✔
534
}
535

536
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
16,810,897✔
537
  int         ret;
538
  SCtbIdxKey *pCtbIdxKey;
539

540
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
16,810,897✔
541
  if (ret < 0) {
16,814,764✔
542
    return 0;
844,786✔
543
  }
544

545
  pCtbIdxKey = pCtbCur->pKey;
15,969,978✔
546
  if (pCtbIdxKey->suid > pCtbCur->suid) {
15,969,978✔
547
    return 0;
3,137,170✔
548
  }
549

550
  return pCtbIdxKey->uid;
12,832,808✔
551
}
552

553
struct SMStbCursor {
554
  SMeta   *pMeta;
555
  TBC     *pCur;
556
  tb_uid_t suid;
557
  void    *pKey;
558
  void    *pVal;
559
  int      kLen;
560
  int      vLen;
561
};
562

563
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
159,458✔
564
  SMStbCursor *pStbCur = NULL;
159,458✔
565
  int          ret = 0;
159,458✔
566
  int          c = 0;
159,458✔
567

568
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
159,458✔
569
  if (pStbCur == NULL) {
159,457!
570
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
571
    return NULL;
×
572
  }
573

574
  pStbCur->pMeta = pMeta;
159,457✔
575
  pStbCur->suid = suid;
159,457✔
576
  metaRLock(pMeta);
159,457✔
577

578
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
159,458✔
579
  if (ret < 0) {
159,459!
580
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
581
    metaULock(pMeta);
×
582
    taosMemoryFree(pStbCur);
×
583
    return NULL;
1✔
584
  }
585

586
  // move to the suid
587
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
159,459✔
588
  if (c > 0) {
159,454!
589
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
590
  }
591

592
  return pStbCur;
159,454✔
593
}
594

595
void metaCloseStbCursor(SMStbCursor *pStbCur) {
159,456✔
596
  if (pStbCur) {
159,456!
597
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
159,456!
598
    if (pStbCur->pCur) {
159,457✔
599
      tdbTbcClose(pStbCur->pCur);
159,456✔
600

601
      tdbFree(pStbCur->pKey);
159,455✔
602
      tdbFree(pStbCur->pVal);
159,455✔
603
    }
604

605
    taosMemoryFree(pStbCur);
159,455✔
606
  }
607
}
159,460✔
608

609
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
297,261✔
610
  int ret;
611

612
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
297,261✔
613
  if (ret < 0) {
297,248✔
614
    return 0;
159,455✔
615
  }
616
  return *(tb_uid_t *)pStbCur->pKey;
137,793✔
617
}
618

619
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
4,938,176✔
620
  STSchema       *pTSchema = NULL;
4,938,176✔
621
  SSchemaWrapper *pSW = NULL;
4,938,176✔
622

623
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
4,938,176✔
624
  if (!pSW) return NULL;
4,955,051✔
625

626
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
4,955,050✔
627

628
  taosMemoryFree(pSW->pSchema);
4,957,614✔
629
  taosMemoryFree(pSW);
4,958,309✔
630
  return pTSchema;
4,957,352✔
631
}
632

633
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
9,743✔
634
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
9,743✔
635
  if (*ppTSchema == NULL) {
9,757✔
636
    return terrno;
2✔
637
  }
638
  return TSDB_CODE_SUCCESS;
9,755✔
639
}
640

641
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,929,849✔
642
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,929,849✔
643
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,947,124!
644
    return terrno;
9,468✔
645
  }
646
  return TSDB_CODE_SUCCESS;
4,937,656✔
647
}
648

649
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
647,287✔
650
  int32_t code = 0;
647,287✔
651
  int32_t lino;
652

653
  void     *pData = NULL;
647,287✔
654
  int       nData = 0;
647,287✔
655
  SSkmDbKey skmDbKey;
656
  if (sver <= 0) {
647,287✔
657
    SMetaInfo info;
658
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
400,129!
659
      sver = info.skmVer;
400,155✔
660
    } else {
661
      TBC *pSkmDbC = NULL;
×
662
      int  c;
663

664
      skmDbKey.uid = suid ? suid : uid;
×
665
      skmDbKey.sver = INT32_MAX;
×
666

667
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
×
668
      TSDB_CHECK_CODE(code, lino, _exit);
×
669
      metaRLock(pMeta);
×
670

671
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
×
672
        metaULock(pMeta);
×
673
        tdbTbcClose(pSkmDbC);
×
674
        code = TSDB_CODE_NOT_FOUND;
×
675
        goto _exit;
×
676
      }
677

678
      if (c == 0) {
×
679
        metaULock(pMeta);
×
680
        tdbTbcClose(pSkmDbC);
×
681
        code = TSDB_CODE_FAILED;
×
682
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
683
        goto _exit;
×
684
      }
685

686
      if (c < 0) {
×
687
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
688
      }
689

690
      const void *pKey = NULL;
×
691
      int32_t     nKey = 0;
×
692
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
×
693

694
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
×
695
        metaULock(pMeta);
×
696
        tdbTbcClose(pSkmDbC);
×
697
        code = TSDB_CODE_NOT_FOUND;
×
698
        goto _exit;
×
699
      }
700

701
      sver = ((SSkmDbKey *)pKey)->sver;
×
702

703
      metaULock(pMeta);
×
704
      tdbTbcClose(pSkmDbC);
×
705
    }
706
  }
707

708
  if (!(sver > 0)) {
647,283!
709
    code = TSDB_CODE_NOT_FOUND;
×
710
    goto _exit;
×
711
  }
712

713
  skmDbKey.uid = suid ? suid : uid;
647,283✔
714
  skmDbKey.sver = sver;
647,283✔
715
  metaRLock(pMeta);
647,283✔
716
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
647,478!
717
    metaULock(pMeta);
×
718
    code = TSDB_CODE_NOT_FOUND;
×
719
    goto _exit;
×
720
  }
721
  metaULock(pMeta);
647,351✔
722

723
  // decode
724
  SDecoder        dc = {0};
647,432✔
725
  SSchemaWrapper  schema;
726
  SSchemaWrapper *pSchemaWrapper = &schema;
647,432✔
727

728
  tDecoderInit(&dc, pData, nData);
647,432✔
729
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
646,666✔
730
  tDecoderClear(&dc);
646,666✔
731
  tdbFree(pData);
647,296✔
732
  if (TSDB_CODE_SUCCESS != code) {
647,329!
733
    taosMemoryFree(pSchemaWrapper->pSchema);
×
734
    goto _exit;
×
735
  }
736

737
  // convert
738
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
647,329✔
739
  if (pTSchema == NULL) {
647,378!
740
    code = TSDB_CODE_OUT_OF_MEMORY;
×
741
  }
742

743
  *ppTSchema = pTSchema;
647,378✔
744
  taosMemoryFree(pSchemaWrapper->pSchema);
647,378✔
745

746
_exit:
647,469✔
747
  return code;
647,469✔
748
}
749

750
// N.B. Called by statusReq per second
751
int64_t metaGetTbNum(SMeta *pMeta) {
1,018,854✔
752
  // num of child tables (excluding normal tables , stables and others)
753

754
  /* int64_t num = 0; */
755
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
756

757
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
1,018,854✔
758
}
759

760
void metaUpdTimeSeriesNum(SMeta *pMeta) {
159,418✔
761
  int64_t nCtbTimeSeries = 0;
159,418✔
762
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
159,418!
763
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
159,418✔
764
  }
765
}
159,421✔
766

767
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
768
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
769
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,204,774✔
770
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,204,797✔
771
    metaUpdTimeSeriesNum(pMeta);
155,960✔
772
  }
773

774
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,204,824✔
775
}
776

777
// type: 1 reported timeseries
778
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,204,774!
779
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,204,824✔
780
  if (type == 1) {
1,204,824✔
781
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
1,018,854✔
782
  }
783
  return nTimeSeries;
1,204,825✔
784
}
785

786
typedef struct {
787
  SMeta   *pMeta;
788
  TBC     *pCur;
789
  tb_uid_t uid;
790
  void    *pKey;
791
  void    *pVal;
792
  int      kLen;
793
  int      vLen;
794
} SMSmaCursor;
795

796
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
797
  SMSmaCursor *pSmaCur = NULL;
×
798
  SSmaIdxKey   smaIdxKey;
799
  int          ret;
800
  int          c;
801

802
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
803
  if (pSmaCur == NULL) {
×
804
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
805
    return NULL;
×
806
  }
807

808
  pSmaCur->pMeta = pMeta;
×
809
  pSmaCur->uid = uid;
×
810
  metaRLock(pMeta);
×
811

812
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
813
  if (ret < 0) {
×
814
    metaULock(pMeta);
×
815
    taosMemoryFree(pSmaCur);
×
816
    return NULL;
×
817
  }
818

819
  // move to the suid
820
  smaIdxKey.uid = uid;
×
821
  smaIdxKey.smaUid = INT64_MIN;
×
822
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
823
  if (c > 0) {
×
824
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
825
  }
826

827
  return pSmaCur;
×
828
}
829

830
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
831
  if (pSmaCur) {
×
832
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
833
    if (pSmaCur->pCur) {
×
834
      tdbTbcClose(pSmaCur->pCur);
×
835
      pSmaCur->pCur = NULL;
×
836

837
      tdbFree(pSmaCur->pKey);
×
838
      tdbFree(pSmaCur->pVal);
×
839
    }
840

841
    taosMemoryFree(pSmaCur);
×
842
  }
843
}
×
844

845
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
846
  int         ret;
847
  SSmaIdxKey *pSmaIdxKey;
848

849
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
850
  if (ret < 0) {
×
851
    return 0;
×
852
  }
853

854
  pSmaIdxKey = pSmaCur->pKey;
×
855
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
856
    return 0;
×
857
  }
858

859
  return pSmaIdxKey->uid;
×
860
}
861

862
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
863
  STSmaWrapper *pSW = NULL;
×
864
  SArray       *pSmaIds = NULL;
×
865

866
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
867
    return NULL;
×
868
  }
869

870
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
871
  if (!pSW) {
×
872
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
873
    goto _err;
×
874
  }
875

876
  pSW->number = taosArrayGetSize(pSmaIds);
×
877
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
878

879
  if (!pSW->tSma) {
×
880
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
881
    goto _err;
×
882
  }
883

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

921
    ++smaIdx;
×
922
  }
923

924
  if (smaIdx <= 0) goto _err;
×
925
  pSW->number = smaIdx;
×
926

927
  metaReaderClear(&mr);
×
928
  taosArrayDestroy(pSmaIds);
×
929
  return pSW;
×
930
_err:
×
931
  metaReaderClear(&mr);
×
932
  taosArrayDestroy(pSmaIds);
×
933
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
934
  return NULL;
×
935
}
936

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

953
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
3✔
954

955
  metaReaderClear(&mr);
3✔
956
  return pTSma;
3✔
957
}
958

959
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
960
  SArray     *pUids = NULL;
×
961
  SSmaIdxKey *pSmaIdxKey = NULL;
×
962

963
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
964
  if (!pCur) {
×
965
    return NULL;
×
966
  }
967

968
  while (1) {
×
969
    tb_uid_t id = metaSmaCursorNext(pCur);
×
970
    if (id == 0) {
×
971
      break;
×
972
    }
973

974
    if (!pUids) {
×
975
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
976
      if (!pUids) {
×
977
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
978
        metaCloseSmaCursor(pCur);
×
979
        return NULL;
×
980
      }
981
    }
982

983
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
984

985
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
986
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
987
      metaCloseSmaCursor(pCur);
×
988
      taosArrayDestroy(pUids);
×
989
      return NULL;
×
990
    }
991
  }
992

993
  metaCloseSmaCursor(pCur);
×
994
  return pUids;
×
995
}
996

997
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
998
  SArray     *pUids = NULL;
×
999
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1000
  tb_uid_t    lastUid = 0;
×
1001

1002
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1003
  if (!pCur) {
×
1004
    return NULL;
×
1005
  }
1006

1007
  while (1) {
×
1008
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1009
    if (uid == 0) {
×
1010
      break;
×
1011
    }
1012

1013
    if (lastUid == uid) {
×
1014
      continue;
×
1015
    }
1016

1017
    lastUid = uid;
×
1018

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

1028
    if (!taosArrayPush(pUids, &uid)) {
×
1029
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1030
      metaCloseSmaCursor(pCur);
×
1031
      taosArrayDestroy(pUids);
×
1032
      return NULL;
×
1033
    }
1034
  }
1035

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

1040
#endif
1041

1042
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
22,095,514✔
1043
  STag *tag = (STag *)pTag;
22,095,514✔
1044
  if (type == TSDB_DATA_TYPE_JSON) {
22,095,514✔
1045
    return tag;
10,215✔
1046
  }
1047
  bool find = tTagGet(tag, val);
22,085,299✔
1048

1049
  if (!find) {
22,115,731✔
1050
    return NULL;
126,511✔
1051
  }
1052

1053
  return val;
21,989,220✔
1054
}
1055

1056
typedef struct {
1057
  SMeta   *pMeta;
1058
  TBC     *pCur;
1059
  tb_uid_t suid;
1060
  int16_t  cid;
1061
  int16_t  type;
1062
  void    *pKey;
1063
  void    *pVal;
1064
  int32_t  kLen;
1065
  int32_t  vLen;
1066
} SIdxCursor;
1067

1068
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1069
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1070
  SMetaFltParam *param = arg;
6✔
1071
  int32_t        ret = 0;
6✔
1072

1073
  SIdxCursor *pCursor = NULL;
6✔
1074
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
6✔
1075
  if (pCursor == NULL) {
6!
1076
    return terrno;
×
1077
  }
1078
  pCursor->pMeta = pMeta;
6✔
1079
  pCursor->suid = param->suid;
6✔
1080
  pCursor->cid = param->cid;
6✔
1081
  pCursor->type = param->type;
6✔
1082

1083
  metaRLock(pMeta);
6✔
1084
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
6✔
1085
  if (ret != 0) {
6!
1086
    goto END;
×
1087
  }
1088
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
6✔
1089

1090
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
6✔
1091
  SBtimeIdxKey *pBtimeKey = &btimeKey;
6✔
1092

1093
  int cmp = 0;
6✔
1094
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
6!
1095
    goto END;
×
1096
  }
1097

1098
  int32_t valid = 0;
6✔
1099
  int32_t count = 0;
6✔
1100

1101
  static const int8_t TRY_ERROR_LIMIT = 1;
1102
  do {
31,801✔
1103
    void   *entryKey = NULL;
31,807✔
1104
    int32_t nEntryKey = -1;
31,807✔
1105
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
31,807✔
1106
    if (valid < 0) break;
31,334✔
1107

1108
    SBtimeIdxKey *p = entryKey;
31,328✔
1109
    if (count > TRY_ERROR_LIMIT) break;
31,328!
1110

1111
    terrno = TSDB_CODE_SUCCESS;
31,328✔
1112
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
31,320✔
1113
    if (terrno != TSDB_CODE_SUCCESS) {
31,228!
1114
      ret = terrno;
×
1115
      break;
×
1116
    }
1117
    if (cmp == 0) {
31,227!
1118
      if (taosArrayPush(pUids, &p->uid) == NULL) {
62,931!
1119
        ret = terrno;
×
1120
        break;
×
1121
      }
1122
    } else {
1123
      if (param->equal == true) {
×
1124
        if (count > TRY_ERROR_LIMIT) break;
×
1125
        count++;
×
1126
      }
1127
    }
1128
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
31,704✔
1129
    if (valid < 0) break;
31,801!
1130
  } while (1);
1131

1132
END:
6✔
1133
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1134
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1135
  taosMemoryFree(pCursor);
6✔
1136
  return ret;
6✔
1137
}
1138

1139
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1140
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1141
  SMetaFltParam *param = arg;
×
1142
  int32_t        ret = 0;
×
1143
  char          *buf = NULL;
×
1144

1145
  STagIdxKey *pKey = NULL;
×
1146
  int32_t     nKey = 0;
×
1147

1148
  SIdxCursor *pCursor = NULL;
×
1149
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1150
  if (pCursor == NULL) {
×
1151
    return terrno;
×
1152
  }
1153
  pCursor->pMeta = pMeta;
×
1154
  pCursor->suid = param->suid;
×
1155
  pCursor->cid = param->cid;
×
1156
  pCursor->type = param->type;
×
1157

1158
  char *pName = param->val;
×
1159

1160
  metaRLock(pMeta);
×
1161
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1162
  if (ret != 0) {
×
1163
    goto END;
×
1164
  }
1165

1166
  int cmp = 0;
×
1167
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1168
    goto END;
×
1169
  }
1170
  int32_t valid = 0;
×
1171
  int32_t count = 0;
×
1172

1173
  int32_t TRY_ERROR_LIMIT = 1;
×
1174
  do {
×
1175
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1176
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1177
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1178
    if (valid < 0) break;
×
1179

1180
    if (count > TRY_ERROR_LIMIT) break;
×
1181

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

1207
END:
×
1208
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1209
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1210
  taosMemoryFree(buf);
×
1211
  taosMemoryFree(pKey);
×
1212

1213
  taosMemoryFree(pCursor);
×
1214

1215
  return ret;
×
1216
}
1217
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1218
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1219
  SMetaFltParam *param = arg;
×
1220
  int32_t        ret = 0;
×
1221
  char          *buf = NULL;
×
1222

1223
  STtlIdxKey *pKey = NULL;
×
1224
  int32_t     nKey = 0;
×
1225

1226
  SIdxCursor *pCursor = NULL;
×
1227
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1228
  if (pCursor == NULL) {
×
1229
    return terrno;
×
1230
  }
1231
  pCursor->pMeta = pMeta;
×
1232
  pCursor->suid = param->suid;
×
1233
  pCursor->cid = param->cid;
×
1234
  pCursor->type = param->type;
×
1235

1236
  metaRLock(pMeta);
×
1237
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1238

1239
END:
×
1240
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1241
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1242
  taosMemoryFree(buf);
×
1243
  taosMemoryFree(pKey);
×
1244

1245
  taosMemoryFree(pCursor);
×
1246

1247
  return ret;
×
1248
  // impl later
1249
  return 0;
1250
}
1251
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
3,235✔
1252
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,235✔
1253
  SMetaFltParam *param = arg;
3,235✔
1254

1255
  SMetaEntry oStbEntry = {0};
3,235✔
1256
  int32_t    code = 0;
3,235✔
1257
  char      *buf = NULL;
3,235✔
1258
  void      *pData = NULL;
3,235✔
1259
  int        nData = 0;
3,235✔
1260

1261
  SDecoder    dc = {0};
3,235✔
1262
  STbDbKey    tbDbKey = {0};
3,235✔
1263
  STagIdxKey *pKey = NULL;
3,235✔
1264
  int32_t     nKey = 0;
3,235✔
1265

1266
  SIdxCursor *pCursor = NULL;
3,235✔
1267
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,235✔
1268
  if (!pCursor) {
3,253!
1269
    return terrno;
×
1270
  }
1271
  pCursor->pMeta = pMeta;
3,253✔
1272
  pCursor->suid = param->suid;
3,253✔
1273
  pCursor->cid = param->cid;
3,253✔
1274
  pCursor->type = param->type;
3,253✔
1275

1276
  metaRLock(pMeta);
3,253✔
1277

1278
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
3,253!
1279

1280
  tbDbKey.uid = param->suid;
3,248✔
1281
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,248✔
1282

1283
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
3,248!
1284

1285
  tDecoderInit(&dc, pData, nData);
3,258✔
1286

1287
  code = metaDecodeEntry(&dc, &oStbEntry);
3,250✔
1288
  if (code) {
3,255!
1289
    tDecoderClear(&dc);
×
1290
    goto END;
×
1291
  }
1292

1293
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
3,255!
1294
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
2!
1295
  }
1296

1297
  code = TSDB_CODE_INVALID_PARA;
3,255✔
1298
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,522✔
1299
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,062✔
1300
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
4,062!
1301
      code = 0;
450✔
1302
    } else {
1303
      TAOS_CHECK_GOTO(code, NULL, END);
3,612✔
1304
    }
1305
  }
1306

1307
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
460✔
1308
  if (code != 0) {
450!
UNCOV
1309
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1310
  }
1311

1312
  int32_t maxSize = 0;
450✔
1313
  int32_t nTagData = 0;
450✔
1314
  void   *tagData = NULL;
450✔
1315

1316
  if (param->val == NULL) {
450!
1317
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1318
    goto END;
×
1319
  } else {
1320
    if (IS_VAR_DATA_TYPE(param->type)) {
450!
1321
      tagData = varDataVal(param->val);
1✔
1322
      nTagData = varDataLen(param->val);
1✔
1323

1324
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
1!
1325
        maxSize = 4 * nTagData + 1;
×
1326
        buf = taosMemoryCalloc(1, maxSize);
×
1327
        if (buf == NULL) {
×
1328
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1329
        }
1330

1331
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize)) {
×
1332
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1333
        }
1334

1335
        tagData = buf;
×
1336
        nTagData = maxSize;
×
1337
      }
1338
    } else {
1339
      tagData = param->val;
449✔
1340
      nTagData = tDataTypes[param->type].bytes;
449✔
1341
    }
1342
  }
1343

1344
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
450!
1345
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1346
                  NULL, END);
1347

1348
  int cmp = 0;
450✔
1349
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
450!
1350

1351
  int     count = 0;
450✔
1352
  int32_t valid = 0;
450✔
1353
  bool    found = false;
450✔
1354

1355
  static const int8_t TRY_ERROR_LIMIT = 1;
1356

1357
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1358
  /// target:                        [suid, cid2, type2]
1359
  int diffCidCount = 0;
450✔
1360
  do {
11,523✔
1361
    void   *entryKey = NULL, *entryVal = NULL;
11,973✔
1362
    int32_t nEntryKey, nEntryVal;
1363

1364
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
11,973✔
1365
    if (valid < 0) {
11,955✔
1366
      code = valid;
350✔
1367
    }
1368
    if (count > TRY_ERROR_LIMIT) {
11,955✔
1369
      break;
450✔
1370
    }
1371

1372
    STagIdxKey *p = entryKey;
11,930✔
1373
    if (p == NULL) break;
11,930✔
1374

1375
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
11,585!
1376
      if (found == true) break;  //
79!
1377
      if (diffCidCount > TRY_ERROR_LIMIT) break;
×
1378
      diffCidCount++;
×
1379
      count++;
×
1380
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1381
      if (valid < 0) {
2!
1382
        code = valid;
×
1383
        break;
×
1384
      } else {
1385
        continue;
2✔
1386
      }
1387
    }
1388

1389
    terrno = TSDB_CODE_SUCCESS;
11,506✔
1390
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
11,506✔
1391
    if (terrno != TSDB_CODE_SUCCESS) {
11,503!
1392
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1393
      break;
×
1394
    }
1395
    if (cmp == 0) {
11,506✔
1396
      // match
1397
      tb_uid_t tuid = 0;
10,689✔
1398
      if (IS_VAR_DATA_TYPE(pKey->type)) {
10,689!
1399
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
×
1400
      } else {
1401
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
10,689✔
1402
      }
1403
      if (taosArrayPush(pUids, &tuid) == NULL) {
10,700!
1404
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1405
      }
1406
      found = true;
10,700✔
1407
    } else {
1408
      if (param->equal == true) {
817✔
1409
        if (count > TRY_ERROR_LIMIT) break;
113!
1410
        count++;
113✔
1411
      }
1412
    }
1413
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
11,517✔
1414
    if (valid < 0) {
11,521!
1415
      code = valid;
×
1416
      break;
×
1417
    }
1418
  } while (1);
1419

1420
END:
3,245✔
1421
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,245✔
1422
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,251✔
1423
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,251!
1424
  tDecoderClear(&dc);
3,251✔
1425
  tdbFree(pData);
3,244✔
1426

1427
  taosMemoryFree(buf);
3,257✔
1428
  taosMemoryFree(pKey);
3,255✔
1429

1430
  taosMemoryFree(pCursor);
3,255✔
1431

1432
  return code;
3,257✔
1433
}
1434

1435
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
250,513✔
1436
  int ret = 0;
250,513✔
1437
  if (lock) {
250,513!
1438
    metaRLock(pMeta);
×
1439
  }
1440

1441
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
250,513✔
1442
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
250,513✔
1443
  if (lock) {
250,516!
1444
    metaULock(pMeta);
×
1445
  }
1446

1447
  return ret;
250,516✔
1448
}
1449

1450
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
249,959✔
1451
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
249,959✔
1452
  const int32_t LIMIT = 128;
249,959✔
1453

1454
  int32_t isLock = false;
249,959✔
1455
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
249,959!
1456
  for (int i = 0; i < sz; i++) {
500,473✔
1457
    STUidTagInfo *p = taosArrayGet(uidList, i);
250,514✔
1458

1459
    if (i % LIMIT == 0) {
250,513✔
1460
      if (isLock) metaULock(pMeta);
249,958!
1461

1462
      metaRLock(pMeta);
249,958✔
1463
      isLock = true;
249,959✔
1464
    }
1465

1466
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1467
    void   *val = NULL;
250,514✔
1468
    int32_t len = 0;
250,514✔
1469
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
250,514!
1470
      p->pTagVal = taosMemoryMalloc(len);
250,516✔
1471
      if (!p->pTagVal) {
250,515!
1472
        if (isLock) metaULock(pMeta);
×
1473

1474
        TAOS_RETURN(terrno);
×
1475
      }
1476
      memcpy(p->pTagVal, val, len);
250,515✔
1477
      tdbFree(val);
250,515✔
1478
    } else {
1479
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64 "", TD_VID(pMeta->pVnode), suid,
×
1480
                p->uid);
1481
    }
1482
  }
1483
  //  }
1484
  if (isLock) metaULock(pMeta);
249,959!
1485
  return 0;
249,959✔
1486
}
1487

1488
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
972,076✔
1489
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
972,076✔
1490
  if (!pCur) {
973,455!
1491
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1492
  }
1493

1494
  // If len > 0 means there already have uids, and we only want the
1495
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1496
  // in the hash map, that may require a lot of memory
1497
  SHashObj *pSepecifiedUidMap = NULL;
973,455✔
1498
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
973,455✔
1499
  if (numOfElems > 0) {
972,680✔
1500
    pSepecifiedUidMap =
1501
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
97,178✔
1502
    for (int i = 0; i < numOfElems; i++) {
394,466✔
1503
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
297,253✔
1504
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
297,093✔
1505
      if (code) {
297,351!
1506
        metaCloseCtbCursor(pCur);
×
1507
        taosHashCleanup(pSepecifiedUidMap);
×
1508
        return code;
×
1509
      }
1510
    }
1511
  }
1512

1513
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
972,715✔
1514
    while (1) {
2,622,852✔
1515
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,498,017✔
1516
      if (uid == 0) {
3,496,382✔
1517
        break;
878,003✔
1518
      }
1519

1520
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,618,379✔
1521
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,618,379✔
1522
      if (!info.pTagVal) {
2,625,026!
1523
        metaCloseCtbCursor(pCur);
×
1524
        taosHashCleanup(pSepecifiedUidMap);
×
1525
        return terrno;
×
1526
      }
1527
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,625,026✔
1528
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,622,852!
1529
        taosMemoryFreeClear(info.pTagVal);
×
1530
        metaCloseCtbCursor(pCur);
×
1531
        taosHashCleanup(pSepecifiedUidMap);
×
1532
        return terrno;
×
1533
      }
1534
    }
1535
  } else {  // only the specified tables need to be added
1536
    while (1) {
402,268✔
1537
      tb_uid_t uid = metaCtbCursorNext(pCur);
499,818✔
1538
      if (uid == 0) {
499,814✔
1539
        break;
97,204✔
1540
      }
1541

1542
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
402,610✔
1543
      if (index == NULL) {
402,498✔
1544
        continue;
105,569✔
1545
      }
1546

1547
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
296,929✔
1548
      if (pTagInfo->pTagVal == NULL) {
296,833✔
1549
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
296,823✔
1550
        if (!pTagInfo->pTagVal) {
296,965✔
1551
          metaCloseCtbCursor(pCur);
276✔
1552
          taosHashCleanup(pSepecifiedUidMap);
×
1553
          return terrno;
×
1554
        }
1555
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
296,689✔
1556
      }
1557
    }
1558
  }
1559

1560
  taosHashCleanup(pSepecifiedUidMap);
975,207✔
1561
  metaCloseCtbCursor(pCur);
974,890✔
1562
  return TSDB_CODE_SUCCESS;
975,229✔
1563
}
1564

1565
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1566

1567
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
36,110,954✔
1568
  int32_t code = 0;
36,110,954✔
1569
  void   *pData = NULL;
36,110,954✔
1570
  int     nData = 0;
36,110,954✔
1571
  int     lock = 0;
36,110,954✔
1572

1573
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
36,110,954!
1574
    lock = 1;
7,897,226✔
1575
  }
1576

1577
  if (!lock) metaRLock(pMeta);
36,110,954✔
1578

1579
  // search cache
1580
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
36,108,682✔
1581
    if (!lock) metaULock(pMeta);
35,937,513✔
1582
    goto _exit;
35,947,547✔
1583
  }
1584

1585
  // search TDB
1586
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
175,065✔
1587
    // not found
1588
    if (!lock) metaULock(pMeta);
31,266✔
1589
    goto _exit;
31,277✔
1590
  }
1591

1592
  if (!lock) metaULock(pMeta);
143,936✔
1593

1594
  pInfo->uid = uid;
143,936✔
1595
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
143,936✔
1596
  pInfo->version = ((SUidIdxVal *)pData)->version;
143,936✔
1597
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
143,936✔
1598

1599
  if (lock) {
143,936✔
1600
    metaULock(pReader->pMeta);
39,637✔
1601
    // metaReaderReleaseLock(pReader);
1602
  }
1603
  // upsert the cache
1604
  metaWLock(pMeta);
143,936✔
1605
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
143,936✔
1606
  if (ret != 0) {
143,935!
1607
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1608
  }
1609
  metaULock(pMeta);
143,935✔
1610

1611
  if (lock) {
143,936✔
1612
    metaRLock(pReader->pMeta);
39,637✔
1613
  }
1614

1615
_exit:
104,299✔
1616
  tdbFree(pData);
36,122,760✔
1617
  return code;
36,121,020✔
1618
}
1619

1620
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
321,951✔
1621
  int32_t code = 0;
321,951✔
1622

1623
  if (!numOfTables && !numOfCols) goto _exit;
321,951!
1624

1625
  SVnode *pVnodeObj = pVnode;
321,951✔
1626
  metaRLock(pVnodeObj->pMeta);
321,951✔
1627

1628
  // fast path: search cache
1629
  SMetaStbStats state = {0};
322,053✔
1630
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
322,053✔
1631
    metaULock(pVnodeObj->pMeta);
297,471✔
1632
    if (numOfTables) *numOfTables = state.ctbNum;
297,505✔
1633
    if (numOfCols) *numOfCols = state.colNum;
297,505✔
1634
    goto _exit;
297,505✔
1635
  }
1636

1637
  // slow path: search TDB
1638
  int64_t ctbNum = 0;
24,568✔
1639
  int32_t colNum = 0;
24,568✔
1640
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
24,568✔
1641
  if (TSDB_CODE_SUCCESS == code) {
24,567!
1642
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
24,567✔
1643
  }
1644
  metaULock(pVnodeObj->pMeta);
24,568✔
1645
  if (TSDB_CODE_SUCCESS != code) {
24,569!
1646
    goto _exit;
×
1647
  }
1648

1649
  if (numOfTables) *numOfTables = ctbNum;
24,569✔
1650
  if (numOfCols) *numOfCols = colNum;
24,569✔
1651

1652
  state.uid = uid;
24,569✔
1653
  state.ctbNum = ctbNum;
24,569✔
1654
  state.colNum = colNum;
24,569✔
1655

1656
  // upsert the cache
1657
  metaWLock(pVnodeObj->pMeta);
24,569✔
1658

1659
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
24,568✔
1660
  if (ret) {
24,570!
1661
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d", uid, ctbNum, colNum);
×
1662
  }
1663

1664
  metaULock(pVnodeObj->pMeta);
24,570✔
1665

1666
_exit:
322,046✔
1667
  return code;
322,046✔
1668
}
1669

1670
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol) {
193,688✔
1671
  SMetaStbStats stats = {0};
193,688✔
1672

1673
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
193,688✔
1674
    stats.ctbNum += deltaCtb;
188,322✔
1675
    stats.colNum += deltaCol;
188,322✔
1676
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
188,322✔
1677
    if (code) {
188,320!
1678
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d",
×
1679
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol);
1680
    }
1681
  }
1682
}
193,694✔
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