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

taosdata / TDengine / #3541

26 Nov 2024 03:56AM UTC coverage: 60.776% (-0.07%) from 60.846%
#3541

push

travis-ci

web-flow
Merge pull request #28920 from taosdata/fix/TD-33008-3.0

fix(query)[TD-33008]. fix error handling in tsdbCacheRead

120076 of 252763 branches covered (47.51%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

1395 existing lines in 154 files now uncovered.

200995 of 275526 relevant lines covered (72.95%)

19612328.37 hits per line

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

56.58
/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,422,009✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
10,422,009✔
22
  metaReaderDoInit(pReader, pMeta, flags);
10,422,009✔
23
  pReader->pAPI = pAPI;
10,425,397✔
24
}
10,425,397✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
14,336,284✔
27
  memset(pReader, 0, sizeof(*pReader));
14,336,284✔
28
  pReader->pMeta = pMeta;
14,336,284✔
29
  pReader->flags = flags;
14,336,284✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
14,336,284!
31
    metaRLock(pMeta);
10,817,705✔
32
  }
33
}
14,337,777✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,286,461✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,286,461!
37
    metaULock(pReader->pMeta);
2,286,734✔
38
    pReader->flags |= META_READER_NOLOCK;
2,287,219✔
39
  }
40
}
2,286,946✔
41

42
void metaReaderClear(SMetaReader *pReader) {
15,060,768✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
15,060,768✔
44
    metaULock(pReader->pMeta);
8,530,537✔
45
  }
46
  tDecoderClear(&pReader->coder);
15,060,731✔
47
  tdbFree(pReader->pBuf);
15,071,469✔
48
  pReader->pBuf = NULL;
15,070,648✔
49
}
15,070,648✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
21,128,481✔
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,138,284✔
63

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

71
  return 0;
21,104,948✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
769,601✔
84
  return true;
769,605✔
85
}
86

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

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

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

100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
8,021,163✔
101
  SMeta *pMeta = pReader->pMeta;
8,021,163✔
102

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
8,021,163✔
105
  if (TSDB_CODE_SUCCESS != code) {
8,022,675✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
35!
107
  }
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
8,022,640✔
110
}
111

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

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

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

130
  metaRLock(pMeta);
399,311✔
131

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

137
  metaULock(pMeta);
399,299✔
138

139
  return uid;
399,324✔
140
}
141

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

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

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

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

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
480,321✔
187

188
  metaReaderClear(&mr);
480,321✔
189

190
  return 0;
480,316✔
191
}
192

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

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

201
  metaReaderClear(&mr);
480,315✔
202
  return code;
480,316✔
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) {
633,240✔
238
  SMTbCursor *pTbCur = NULL;
633,240✔
239
  int32_t     code;
240

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

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

259
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,268,118✔
260
  if (pTbCur) {
1,268,118✔
261
    tdbFree(pTbCur->pKey);
634,441✔
262
    tdbFree(pTbCur->pVal);
634,440✔
263
    if (!pTbCur->paused) {
634,471✔
264
      metaReaderClear(&pTbCur->mr);
50,327✔
265
      if (pTbCur->pDbc) {
50,323!
266
        tdbTbcClose((TBC *)pTbCur->pDbc);
50,323✔
267
      }
268
    }
269
    taosMemoryFree(pTbCur);
634,468✔
270
  }
271
}
1,268,136✔
272

273
void metaPauseTbCursor(SMTbCursor *pTbCur) {
584,794✔
274
  if (!pTbCur->paused) {
584,794!
275
    metaReaderClear(&pTbCur->mr);
584,894✔
276
    tdbTbcClose((TBC *)pTbCur->pDbc);
585,081✔
277
    pTbCur->paused = 1;
584,993✔
278
  }
279
}
584,893✔
280
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
634,781✔
281
  int32_t code = 0;
634,781✔
282
  int32_t lino;
283
  int8_t  locked = 0;
634,781✔
284
  if (pTbCur->paused) {
634,781!
285
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
634,825✔
286
    locked = 1;
635,212✔
287
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
635,212✔
288
    if (code != 0) {
634,609!
289
      TSDB_CHECK_CODE(code, lino, _exit);
×
290
    }
291

292
    if (first) {
634,609✔
293
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
633,687✔
294
      TSDB_CHECK_CODE(code, lino, _exit);
633,469!
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;
634,391✔
311
  }
312

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

320
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
8,267,165✔
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,267,165✔
327
    if (ret < 0) {
8,257,594✔
328
      return ret;
634,307✔
329
    }
330

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

333
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
7,651,164✔
334
    if (ret) return ret;
7,631,032!
335

336
    if (pTbCur->mr.me.type == jumpTableType) {
7,631,032✔
337
      continue;
1,236,667✔
338
    }
339

340
    break;
6,394,365✔
341
  }
342

343
  return 0;
6,394,365✔
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,165,420✔
375
  void           *pData = NULL;
5,165,420✔
376
  int             nData = 0;
5,165,420✔
377
  int64_t         version;
378
  SSchemaWrapper  schema = {0};
5,165,420✔
379
  SSchemaWrapper *pSchema = NULL;
5,165,420✔
380
  SDecoder        dc = {0};
5,165,420✔
381
  if (lock) {
5,165,420✔
382
    metaRLock(pMeta);
5,137,365✔
383
  }
384
_query:
5,176,887✔
385
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
5,257,316✔
386
    goto _err;
48✔
387
  }
388

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

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

395
  SMetaEntry me = {0};
5,261,795✔
396
  tDecoderInit(&dc, pData, nData);
5,261,795✔
397
  int32_t code = metaDecodeEntry(&dc, &me);
5,258,643✔
398
  if (code) {
5,257,910!
399
    tDecoderClear(&dc);
×
400
    goto _err;
×
401
  }
402
  if (me.type == TSDB_SUPER_TABLE) {
5,257,910✔
403
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,833,226!
404
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,836,209✔
405
      tDecoderClear(&dc);
4,836,209✔
406
      goto _exit;
4,835,818✔
407
    }
408
  } else if (me.type == TSDB_CHILD_TABLE) {
424,684✔
409
    uid = me.ctbEntry.suid;
80,371✔
410
    if (createTime != NULL){
80,371✔
411
      *createTime = me.ctbEntry.btime;
78,193✔
412
    }
413
    tDecoderClear(&dc);
80,371✔
414
    goto _query;
80,429✔
415
  } else {
416
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
344,313✔
417
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
344,428✔
418
      tDecoderClear(&dc);
344,428✔
419
      goto _exit;
344,412✔
420
    }
421
  }
UNCOV
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,180,360✔
437
  if (lock) {
5,180,360✔
438
    metaULock(pMeta);
5,148,633✔
439
  }
440
  tdbFree(pData);
5,186,636✔
441
  return pSchema;
5,181,273✔
442

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

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

458
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
4,026,337✔
459
  if (pCtbCur == NULL) {
4,032,150!
460
    return NULL;
×
461
  }
462

463
  pCtbCur->pMeta = pMeta;
4,032,150✔
464
  pCtbCur->suid = uid;
4,032,150✔
465
  pCtbCur->lock = lock;
4,032,150✔
466
  pCtbCur->paused = 1;
4,032,150✔
467

468
  ret = metaResumeCtbCursor(pCtbCur, 1);
4,032,150✔
469
  if (ret < 0) {
4,031,573!
470
    return NULL;
×
471
  }
472
  return pCtbCur;
4,031,573✔
473
}
474

475
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
4,032,206✔
476
  if (pCtbCur) {
4,032,206!
477
    if (!pCtbCur->paused) {
4,032,442✔
478
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,989,294!
479
      if (pCtbCur->pCur) {
3,990,517✔
480
        tdbTbcClose(pCtbCur->pCur);
3,990,097✔
481
      }
482
    }
483
    tdbFree(pCtbCur->pKey);
4,032,910✔
484
    tdbFree(pCtbCur->pVal);
4,032,248✔
485
  }
486
  taosMemoryFree(pCtbCur);
4,032,501✔
487
}
4,033,422✔
488

489
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,312✔
490
  if (!pCtbCur->paused) {
43,312!
491
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,335✔
492
    if (pCtbCur->lock) {
43,368!
493
      metaULock(pCtbCur->pMeta);
43,370✔
494
    }
495
    pCtbCur->paused = 1;
43,366✔
496
  }
497
}
43,343✔
498

499
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
4,028,065✔
500
  if (pCtbCur->paused) {
4,028,065!
501
    pCtbCur->paused = 0;
4,029,245✔
502

503
    if (pCtbCur->lock) {
4,029,245✔
504
      metaRLock(pCtbCur->pMeta);
4,006,323✔
505
    }
506
    int ret = 0;
4,030,328✔
507
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
4,030,328✔
508
    if (ret < 0) {
4,030,435!
509
      metaCloseCtbCursor(pCtbCur);
×
510
      return -1;
×
511
    }
512

513
    if (first) {
4,030,571!
514
      SCtbIdxKey ctbIdxKey;
515
      // move to the suid
516
      ctbIdxKey.suid = pCtbCur->suid;
4,030,571✔
517
      ctbIdxKey.uid = INT64_MIN;
4,030,571✔
518
      int c = 0;
4,030,571✔
519
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
4,030,571✔
520
      if (c > 0) {
4,031,965✔
521
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
1,362,295✔
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;
4,030,477✔
534
}
535

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

540
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
16,653,705✔
541
  if (ret < 0) {
16,649,729✔
542
    return 0;
2,812,884✔
543
  }
544

545
  pCtbIdxKey = pCtbCur->pKey;
13,836,845✔
546
  if (pCtbIdxKey->suid > pCtbCur->suid) {
13,836,845✔
547
    return 0;
1,220,820✔
548
  }
549

550
  return pCtbIdxKey->uid;
12,616,025✔
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) {
163,987✔
564
  SMStbCursor *pStbCur = NULL;
163,987✔
565
  int          ret = 0;
163,987✔
566
  int          c = 0;
163,987✔
567

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

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

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

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

592
  return pStbCur;
163,987✔
593
}
594

595
void metaCloseStbCursor(SMStbCursor *pStbCur) {
163,986✔
596
  if (pStbCur) {
163,986!
597
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
163,986!
598
    if (pStbCur->pCur) {
163,988✔
599
      tdbTbcClose(pStbCur->pCur);
163,987✔
600

601
      tdbFree(pStbCur->pKey);
163,987✔
602
      tdbFree(pStbCur->pVal);
163,986✔
603
    }
604

605
    taosMemoryFree(pStbCur);
163,987✔
606
  }
607
}
163,987✔
608

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

612
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
331,340✔
613
  if (ret < 0) {
331,332✔
614
    return 0;
163,986✔
615
  }
616
  return *(tb_uid_t *)pStbCur->pKey;
167,346✔
617
}
618

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

623
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
5,019,615✔
624
  if (!pSW) return NULL;
5,031,756!
625

626
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
5,031,756✔
627

628
  taosMemoryFree(pSW->pSchema);
5,032,255✔
629
  taosMemoryFree(pSW);
5,033,426✔
630
  return pTSchema;
5,032,490✔
631
}
632

633
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
8,311✔
634
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
8,311✔
635
  if (*ppTSchema == NULL) {
8,326!
636
    return terrno;
×
637
  }
638
  return TSDB_CODE_SUCCESS;
8,327✔
639
}
640

641
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
5,012,314✔
642
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
5,012,314✔
643
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
5,023,590!
644
    return terrno;
6,086✔
645
  }
646
  return TSDB_CODE_SUCCESS;
5,017,504✔
647
}
648

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

653
  void     *pData = NULL;
642,967✔
654
  int       nData = 0;
642,967✔
655
  SSkmDbKey skmDbKey;
656
  if (sver <= 0) {
642,967✔
657
    SMetaInfo info;
658
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
396,346!
659
      sver = info.skmVer;
396,333✔
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)) {
642,883!
709
    code = TSDB_CODE_NOT_FOUND;
×
710
    goto _exit;
×
711
  }
712

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

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

728
  tDecoderInit(&dc, pData, nData);
643,241✔
729
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
642,212✔
730
  tDecoderClear(&dc);
642,212✔
731
  tdbFree(pData);
643,081✔
732
  if (TSDB_CODE_SUCCESS != code) {
643,140!
733
    taosMemoryFree(pSchemaWrapper->pSchema);
×
734
    goto _exit;
×
735
  }
736

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

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

746
_exit:
643,237✔
747
  return code;
643,237✔
748
}
749

750
// N.B. Called by statusReq per second
751
int64_t metaGetTbNum(SMeta *pMeta) {
957,395✔
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;
957,395✔
758
}
759

760
void metaUpdTimeSeriesNum(SMeta *pMeta) {
163,950✔
761
  int64_t nCtbTimeSeries = 0;
163,950✔
762
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
163,950!
763
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
163,950✔
764
  }
765
}
163,950✔
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,157,171✔
770
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,157,191✔
771
    metaUpdTimeSeriesNum(pMeta);
160,553✔
772
  }
773

774
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,157,196✔
775
}
776

777
// type: 1 reported timeseries
778
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,157,171!
779
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,157,196✔
780
  if (type == 1) {
1,157,196✔
781
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
957,395✔
782
  }
783
  return nTimeSeries;
1,157,196✔
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) {
2✔
938
  STSma      *pTSma = NULL;
2✔
939
  SMetaReader mr = {0};
2✔
940
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
2✔
941
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
2!
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));
2✔
947
  if (!pTSma) {
2!
948
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
949
    metaReaderClear(&mr);
×
950
    return NULL;
×
951
  }
952

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

955
  metaReaderClear(&mr);
2✔
956
  return pTSma;
2✔
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) {
21,545,535✔
1043
  STag *tag = (STag *)pTag;
21,545,535✔
1044
  if (type == TSDB_DATA_TYPE_JSON) {
21,545,535✔
1045
    return tag;
10,215✔
1046
  }
1047
  bool find = tTagGet(tag, val);
21,535,320✔
1048

1049
  if (!find) {
21,559,544✔
1050
    return NULL;
122,660✔
1051
  }
1052

1053
  return val;
21,436,884✔
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);
5✔
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 {
33,746✔
1103
    void   *entryKey = NULL;
33,752✔
1104
    int32_t nEntryKey = -1;
33,752✔
1105
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
33,752✔
1106
    if (valid < 0) break;
33,275✔
1107

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

1111
    terrno = TSDB_CODE_SUCCESS;
33,269✔
1112
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
33,305✔
1113
    if (terrno != TSDB_CODE_SUCCESS) {
33,161!
1114
      ret = terrno;
×
1115
      break;
×
1116
    }
1117
    if (cmp == 0) {
33,148!
1118
      if (taosArrayPush(pUids, &p->uid) == NULL) {
66,809!
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);
33,661✔
1129
    if (valid < 0) break;
33,746!
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) {
1,988✔
1252
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,988✔
1253
  SMetaFltParam *param = arg;
1,988✔
1254

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

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

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

1276
  metaRLock(pMeta);
1,995✔
1277

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

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

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

1285
  tDecoderInit(&dc, pData, nData);
1,996✔
1286

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

1293
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
1,994!
1294
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1295
  }
1296

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

1307
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
455✔
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);
×
1322
      nTagData = varDataLen(param->val);
×
1323

1324
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
×
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;
450✔
1340
      nTagData = tDataTypes[param->type].bytes;
450✔
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,511✔
1361
    void   *entryKey = NULL, *entryVal = NULL;
11,961✔
1362
    int32_t nEntryKey, nEntryVal;
1363

1364
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
11,961✔
1365
    if (valid < 0) {
11,955✔
1366
      code = valid;
428✔
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,506!
1376
      if (found == true) break;  //
1!
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) {
1!
1382
        code = valid;
×
1383
        break;
×
1384
      } else {
1385
        continue;
1✔
1386
      }
1387
    }
1388

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

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

1427
  taosMemoryFree(buf);
1,996✔
1428
  taosMemoryFree(pKey);
1,996✔
1429

1430
  taosMemoryFree(pCursor);
1,995✔
1431

1432
  return code;
1,996✔
1433
}
1434

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

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

1447
  return ret;
282,894✔
1448
}
1449

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

1454
  int32_t isLock = false;
282,879✔
1455
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
282,879!
1456
  for (int i = 0; i < sz; i++) {
565,772✔
1457
    STUidTagInfo *p = taosArrayGet(uidList, i);
282,891✔
1458

1459
    if (i % LIMIT == 0) {
282,891✔
1460
      if (isLock) metaULock(pMeta);
282,878!
1461

1462
      metaRLock(pMeta);
282,878✔
1463
      isLock = true;
282,880✔
1464
    }
1465

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

1474
        TAOS_RETURN(terrno);
×
1475
      }
1476
      memcpy(p->pTagVal, val, len);
282,894✔
1477
      tdbFree(val);
282,894✔
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);
282,881✔
1485
  return 0;
282,879✔
1486
}
1487

1488
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
1,025,268✔
1489
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
1,025,268✔
1490
  if (!pCur) {
1,027,077!
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;
1,027,077✔
1498
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
1,027,077✔
1499
  if (numOfElems > 0) {
1,026,705✔
1500
    pSepecifiedUidMap =
1501
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
97,884✔
1502
    for (int i = 0; i < numOfElems; i++) {
388,847✔
1503
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
290,945✔
1504
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
290,893✔
1505
      if (code) {
290,998!
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
1,026,723✔
1514
    while (1) {
2,743,653✔
1515
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,672,309✔
1516
      if (uid == 0) {
3,667,365✔
1517
        break;
929,521✔
1518
      }
1519

1520
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,737,844✔
1521
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,737,844✔
1522
      if (!info.pTagVal) {
2,745,498!
1523
        metaCloseCtbCursor(pCur);
×
1524
        taosHashCleanup(pSepecifiedUidMap);
×
1525
        return terrno;
×
1526
      }
1527
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,745,498✔
1528
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,743,653!
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) {
400,525✔
1537
      tb_uid_t uid = metaCtbCursorNext(pCur);
498,592✔
1538
      if (uid == 0) {
498,440✔
1539
        break;
97,889✔
1540
      }
1541

1542
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
400,551✔
1543
      if (index == NULL) {
400,798✔
1544
        continue;
109,980✔
1545
      }
1546

1547
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
290,818✔
1548
      if (pTagInfo->pTagVal == NULL) {
290,771✔
1549
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
290,762✔
1550
        if (!pTagInfo->pTagVal) {
290,796✔
1551
          metaCloseCtbCursor(pCur);
260✔
1552
          taosHashCleanup(pSepecifiedUidMap);
×
1553
          return terrno;
×
1554
        }
1555
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
290,536✔
1556
      }
1557
    }
1558
  }
1559

1560
  taosHashCleanup(pSepecifiedUidMap);
1,027,410✔
1561
  metaCloseCtbCursor(pCur);
1,027,178✔
1562
  return TSDB_CODE_SUCCESS;
1,027,457✔
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) {
39,239,635✔
1568
  int32_t code = 0;
39,239,635✔
1569
  void   *pData = NULL;
39,239,635✔
1570
  int     nData = 0;
39,239,635✔
1571
  int     lock = 0;
39,239,635✔
1572

1573
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
39,239,635!
1574
    lock = 1;
8,023,625✔
1575
  }
1576

1577
  if (!lock) metaRLock(pMeta);
39,239,635✔
1578

1579
  // search cache
1580
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
39,243,018✔
1581
    if (!lock) metaULock(pMeta);
39,136,797✔
1582
    goto _exit;
39,138,699✔
1583
  }
1584

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

1592
  if (!lock) metaULock(pMeta);
76,907✔
1593

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

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

1611
  if (lock) {
76,921✔
1612
    metaRLock(pReader->pMeta);
6,586✔
1613
  }
1614

1615
_exit:
70,335✔
1616
  tdbFree(pData);
39,247,270✔
1617
  return code;
39,245,889✔
1618
}
1619

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

1623
  if (!numOfTables && !numOfCols) goto _exit;
365,514!
1624

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

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

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

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

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

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

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

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

1666
_exit:
365,568✔
1667
  return code;
365,568✔
1668
}
1669

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

1673
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
207,551✔
1674
    stats.ctbNum += deltaCtb;
202,199✔
1675
    stats.colNum += deltaCol;
202,199✔
1676
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
202,199✔
1677
    if (code) {
202,183!
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
}
207,545✔
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