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

taosdata / TDengine / #4680

21 Aug 2025 12:03PM UTC coverage: 59.87% (-0.2%) from 60.031%
#4680

push

travis-ci

web-flow
jdbc release 3.7.3 (#32682)

* chore(jdbc): update jdbc version

* docs(jdbc): release 3.7.3

137338 of 292193 branches covered (47.0%)

Branch coverage included in aggregate %.

207909 of 284466 relevant lines covered (73.09%)

4731310.74 hits per line

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

52.92
/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) {
2,785,213✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,785,213✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,785,213✔
23
  pReader->pAPI = pAPI;
2,786,281✔
24
}
2,786,281✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
3,203,458✔
27
  memset(pReader, 0, sizeof(*pReader));
3,203,458✔
28
  pReader->pMeta = pMeta;
3,203,458✔
29
  pReader->flags = flags;
3,203,458✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
3,203,458!
31
    metaRLock(pMeta);
2,760,156✔
32
  }
33
}
3,203,592✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,143,694✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,143,694!
37
    metaULock(pReader->pMeta);
1,144,090✔
38
    pReader->flags |= META_READER_NOLOCK;
1,144,794✔
39
  }
40
}
1,144,398✔
41

42
void metaReaderClear(SMetaReader *pReader) {
3,246,178✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
3,246,178✔
44
    metaULock(pReader->pMeta);
1,614,598✔
45
  }
46
  tDecoderClear(&pReader->coder);
3,246,986✔
47
  tdbFree(pReader->pBuf);
3,246,523✔
48
  pReader->pBuf = NULL;
3,246,986✔
49
}
3,246,986✔
50

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

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

61
  // decode the entry
62
  tDecoderClear(&pReader->coder);
4,092,348✔
63
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
4,090,837✔
64

65
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
4,090,156✔
66
  if (code) {
4,086,859!
67
    tDecoderClear(&pReader->coder);
×
68
    return code;
×
69
  }
70
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
71

72
  return 0;
4,086,859✔
73
}
74

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

79
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
499,879✔
80
    metaULock(pVnodeObj->pMeta);
84✔
81
    return false;
84✔
82
  }
83

84
  metaULock(pVnodeObj->pMeta);
499,789✔
85
  return true;
499,796✔
86
}
87

88
int metaReaderGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) {
658,922✔
89
  SMeta  *pMeta = pReader->pMeta;
658,922✔
90
  int64_t version1;
91

92
  // query uid.idx
93
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
658,922✔
94
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
661✔
95
  }
96

97
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
657,592✔
98
  return metaGetTableEntryByVersion(pReader, version1, uid);
657,592✔
99
}
100

101
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
2,780,438✔
102
  SMeta *pMeta = pReader->pMeta;
2,780,438✔
103

104
  SMetaInfo info;
105
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
2,780,438✔
106
  if (TSDB_CODE_SUCCESS != code) {
2,780,628✔
107
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
3!
108
  }
109

110
  return metaGetTableEntryByVersion(pReader, info.version, uid);
2,780,625✔
111
}
112

113
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
124,567✔
114
  SMeta   *pMeta = pReader->pMeta;
124,567✔
115
  tb_uid_t uid;
116

117
  // query name.idx
118
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
124,567✔
119
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
49,338✔
120
  }
121

122
  uid = *(tb_uid_t *)pReader->pBuf;
75,231✔
123
  return metaReaderGetTableEntryByUid(pReader, uid);
75,231✔
124
}
125

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

131
  metaRLock(pMeta);
166,455✔
132

133
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
166,542✔
134
    uid = *(tb_uid_t *)pData;
32,932✔
135
    tdbFree(pData);
32,932✔
136
  }
137

138
  metaULock(pMeta);
166,553✔
139

140
  return uid;
166,564✔
141
}
142

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

153
  STR_TO_VARSTR(tbName, mr.me.name);
81,516✔
154
  metaReaderClear(&mr);
81,516✔
155

156
  return 0;
82,096✔
157
}
158

159
int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
×
160
  int         code = 0;
×
161
  SMetaReader mr = {0};
×
162
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
163
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
164
  if (code < 0) {
×
165
    metaReaderClear(&mr);
×
166
    return code;
×
167
  }
168
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
169
  metaReaderClear(&mr);
×
170

171
  return 0;
×
172
}
173

174
int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) {
×
175
  int         code = 0;
×
176
  SMetaReader mr = {0};
×
177
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
×
178

179
  SMetaReader *pReader = &mr;
×
180

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

187
  *uid = *(tb_uid_t *)pReader->pBuf;
×
188

189
  metaReaderClear(&mr);
×
190

191
  return 0;
×
192
}
193

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

199
  code = metaGetTableEntryByName(&mr, tbName);
×
200
  if (code == 0) *tbType = mr.me.type;
×
201
  if (TSDB_CHILD_TABLE == mr.me.type) {
×
202
    *suid = mr.me.ctbEntry.suid;
×
203
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
×
204
    *suid = mr.me.uid;
×
205
  } else {
206
    *suid = 0;
×
207
  }
208

209
  metaReaderClear(&mr);
×
210
  return code;
×
211
}
212

213
int metaReadNext(SMetaReader *pReader) {
×
214
  SMeta *pMeta = pReader->pMeta;
×
215

216
  // TODO
217

218
  return 0;
×
219
}
220

221
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
222
  int         code = -1;
×
223
  SMetaReader mr = {0};
×
224
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
225
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
226
  if (code < 0) {
×
227
    goto _exit;
×
228
  }
229
  if (mr.me.type == TSDB_CHILD_TABLE) {
×
230
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
231
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
×
232
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
233
  } else {
234
    goto _exit;
×
235
  }
236

237
  code = 0;
×
238

239
_exit:
×
240
  metaReaderClear(&mr);
×
241
  return code;
×
242
}
243

244
#if 1  // ===================================================
245
SMTbCursor *metaOpenTbCursor(void *pVnode) {
20,303✔
246
  SMTbCursor *pTbCur = NULL;
20,303✔
247
  int32_t     code;
248

249
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
20,303!
250
  if (pTbCur == NULL) {
20,309!
251
    return NULL;
×
252
  }
253

254
  SVnode *pVnodeObj = pVnode;
20,309✔
255
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
256
  pTbCur->pMeta = pVnodeObj->pMeta;
20,309✔
257
  pTbCur->paused = 1;
20,309✔
258
  code = metaResumeTbCursor(pTbCur, 1, 0);
20,309✔
259
  if (code) {
20,300✔
260
    terrno = code;
1✔
261
    taosMemoryFree(pTbCur);
×
262
    return NULL;
×
263
  }
264
  return pTbCur;
20,299✔
265
}
266

267
void metaCloseTbCursor(SMTbCursor *pTbCur) {
40,516✔
268
  if (pTbCur) {
40,516✔
269
    tdbFree(pTbCur->pKey);
20,305✔
270
    tdbFree(pTbCur->pVal);
20,309✔
271
    if (!pTbCur->paused) {
20,310✔
272
      metaReaderClear(&pTbCur->mr);
964✔
273
      if (pTbCur->pDbc) {
964!
274
        tdbTbcClose((TBC *)pTbCur->pDbc);
964✔
275
      }
276
    }
277
    taosMemoryFree(pTbCur);
20,310!
278
  }
279
}
40,520✔
280

281
void metaPauseTbCursor(SMTbCursor *pTbCur) {
20,266✔
282
  if (!pTbCur->paused) {
20,266!
283
    metaReaderClear(&pTbCur->mr);
20,266✔
284
    tdbTbcClose((TBC *)pTbCur->pDbc);
20,267✔
285
    pTbCur->paused = 1;
20,266✔
286
  }
287
}
20,266✔
288
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
21,228✔
289
  int32_t code = 0;
21,228✔
290
  int32_t lino;
291
  int8_t  locked = 0;
21,228✔
292
  if (pTbCur->paused) {
21,228!
293
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
21,228✔
294
    locked = 1;
21,230✔
295
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
21,230✔
296
    if (code != 0) {
21,224!
297
      TSDB_CHECK_CODE(code, lino, _exit);
×
298
    }
299

300
    if (first) {
21,224✔
301
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
20,302✔
302
      TSDB_CHECK_CODE(code, lino, _exit);
20,305!
303
    } else {
304
      int c = 1;
922✔
305
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
922✔
306
      TSDB_CHECK_CODE(code, lino, _exit);
922!
307
      if (c == 0) {
922!
308
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
922!
309
      } else if (c < 0) {
×
310
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
311
        TSDB_CHECK_CODE(code, lino, _exit);
×
312
      } else {
313
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
314
        TSDB_CHECK_CODE(code, lino, _exit);
×
315
      }
316
    }
317

318
    pTbCur->paused = 0;
21,227✔
319
  }
320

321
_exit:
×
322
  if (code != 0 && locked) {
21,227!
323
    metaReaderReleaseLock(&pTbCur->mr);
×
324
  }
325
  return code;
21,224✔
326
}
327

328
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
673,971✔
329
  int    ret;
330
  void  *pBuf;
331
  STbCfg tbCfg;
332

333
  for (;;) {
334
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
673,971✔
335
    if (ret < 0) {
672,824✔
336
      return ret;
20,232✔
337
    }
338

339
    tDecoderClear(&pTbCur->mr.coder);
652,592✔
340

341
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
653,519✔
342
    if (ret) return ret;
651,635!
343

344
    if (pTbCur->mr.me.type == jumpTableType) {
651,635✔
345
      continue;
10,510✔
346
    }
347

348
    break;
641,125✔
349
  }
350

351
  return 0;
641,125✔
352
}
353

354
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
355
  int    ret;
356
  void  *pBuf;
357
  STbCfg tbCfg;
358

359
  for (;;) {
360
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
361
    if (ret < 0) {
×
362
      return -1;
×
363
    }
364

365
    tDecoderClear(&pTbCur->mr.coder);
×
366

367
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
368
    if (ret < 0) {
×
369
      return ret;
×
370
    }
371

372
    if (pTbCur->mr.me.type == jumpTableType) {
×
373
      continue;
×
374
    }
375

376
    break;
×
377
  }
378

379
  return 0;
×
380
}
381

382
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema) {
1,371,378✔
383
  void           *pData = NULL;
1,371,378✔
384
  int             nData = 0;
1,371,378✔
385
  int64_t         version;
386
  SSchemaWrapper  schema = {0};
1,371,378✔
387
  SSchemaWrapper *pSchema = NULL;
1,371,378✔
388
  SDecoder        dc = {0};
1,371,378✔
389
  if (lock) {
1,371,378✔
390
    metaRLock(pMeta);
1,347,131✔
391
  }
392
_query:
1,371,880✔
393
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,470,187✔
394
    goto _err;
381✔
395
  }
396

397
  version = ((SUidIdxVal *)pData)[0].version;
1,469,685✔
398

399
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
1,469,685!
400
    goto _err;
×
401
  }
402

403
  SMetaEntry me = {0};
1,469,857✔
404
  tDecoderInit(&dc, pData, nData);
1,469,857✔
405
  int32_t code = metaDecodeEntry(&dc, &me);
1,469,558✔
406
  if (code) {
1,469,239!
407
    tDecoderClear(&dc);
×
408
    goto _err;
×
409
  }
410
  if (me.type == TSDB_SUPER_TABLE) {
1,469,239✔
411
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
1,122,526!
412
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
1,122,451✔
413
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
1,122,451✔
414
      tDecoderClear(&dc);
1,122,451✔
415
      goto _exit;
1,122,775✔
416
    }
417
  } else if (me.type == TSDB_CHILD_TABLE) {
346,713✔
418
    uid = me.ctbEntry.suid;
98,238✔
419
    tDecoderClear(&dc);
98,238✔
420
    goto _query;
98,307✔
421
  } else {
422
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
248,475!
423
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
248,531✔
424
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
248,531✔
425
      tDecoderClear(&dc);
248,524✔
426
      goto _exit;
248,545✔
427
    }
428
  }
429
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
×
430
  tDecoderClear(&dc);
×
431

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

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

444
_exit:
1,371,452✔
445
  if (lock) {
1,371,452✔
446
    metaULock(pMeta);
1,346,224✔
447
  }
448
  tdbFree(pData);
1,372,187✔
449
  return pSchema;
1,371,571✔
450

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

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

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

473
  version = ((SUidIdxVal *)pData)[0].version;
139✔
474

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

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

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

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

506
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
773,713!
507
  if (pCtbCur == NULL) {
774,680!
508
    return NULL;
×
509
  }
510

511
  pCtbCur->pMeta = pMeta;
774,680✔
512
  pCtbCur->suid = uid;
774,680✔
513
  pCtbCur->lock = lock;
774,680✔
514
  pCtbCur->paused = 1;
774,680✔
515

516
  ret = metaResumeCtbCursor(pCtbCur, 1);
774,680✔
517
  if (ret < 0) {
773,920!
518
    return NULL;
×
519
  }
520
  return pCtbCur;
773,920✔
521
}
522

523
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
774,101✔
524
  if (pCtbCur) {
774,101!
525
    if (!pCtbCur->paused) {
774,195✔
526
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
743,165!
527
      if (pCtbCur->pCur) {
743,566!
528
        tdbTbcClose(pCtbCur->pCur);
743,593✔
529
      }
530
    }
531
    tdbFree(pCtbCur->pKey);
774,357✔
532
    tdbFree(pCtbCur->pVal);
774,359✔
533
  }
534
  taosMemoryFree(pCtbCur);
774,492!
535
}
774,670✔
536

537
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
31,097✔
538
  if (!pCtbCur->paused) {
31,097!
539
    tdbTbcClose((TBC *)pCtbCur->pCur);
31,099✔
540
    if (pCtbCur->lock) {
31,112!
541
      metaULock(pCtbCur->pMeta);
31,114✔
542
    }
543
    pCtbCur->paused = 1;
31,115✔
544
  }
545
}
31,113✔
546

547
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
774,049✔
548
  if (pCtbCur->paused) {
774,049!
549
    pCtbCur->paused = 0;
774,284✔
550

551
    if (pCtbCur->lock) {
774,284✔
552
      metaRLock(pCtbCur->pMeta);
750,099✔
553
    }
554
    int ret = 0;
774,296✔
555
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
774,296✔
556
    if (ret < 0) {
774,325!
557
      metaCloseCtbCursor(pCtbCur);
×
558
      return -1;
×
559
    }
560

561
    if (first) {
774,485!
562
      SCtbIdxKey ctbIdxKey;
563
      // move to the suid
564
      ctbIdxKey.suid = pCtbCur->suid;
774,485✔
565
      ctbIdxKey.uid = INT64_MIN;
774,485✔
566
      int c = 0;
774,485✔
567
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
774,485✔
568
      if (c > 0) {
774,078✔
569
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
164,772✔
570
      }
571
    } else {
572
      int c = 0;
×
573
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
574
      if (c < 0) {
×
575
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
576
      } else {
577
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
578
      }
579
    }
580
  }
581
  return 0;
773,730✔
582
}
583

584
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
4,119,422✔
585
  int         ret;
586
  SCtbIdxKey *pCtbIdxKey;
587

588
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
4,119,422✔
589
  if (ret < 0) {
4,120,529✔
590
    return 0;
664,336✔
591
  }
592

593
  pCtbIdxKey = pCtbCur->pKey;
3,456,193✔
594
  if (pCtbIdxKey->suid > pCtbCur->suid) {
3,456,193✔
595
    return 0;
110,736✔
596
  }
597

598
  return pCtbIdxKey->uid;
3,345,457✔
599
}
600

601
struct SMStbCursor {
602
  SMeta   *pMeta;
603
  TBC     *pCur;
604
  tb_uid_t suid;
605
  void    *pKey;
606
  void    *pVal;
607
  int      kLen;
608
  int      vLen;
609
};
610

611
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
133,925✔
612
  SMStbCursor *pStbCur = NULL;
133,925✔
613
  int          ret = 0;
133,925✔
614
  int          c = 0;
133,925✔
615

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

622
  pStbCur->pMeta = pMeta;
133,925✔
623
  pStbCur->suid = suid;
133,925✔
624
  metaRLock(pMeta);
133,925✔
625

626
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
133,925✔
627
  if (ret < 0) {
133,925!
628
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
629
    metaULock(pMeta);
×
630
    taosMemoryFree(pStbCur);
×
631
    return NULL;
×
632
  }
633

634
  // move to the suid
635
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
133,925✔
636
  if (c > 0) {
133,921!
637
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
638
  }
639

640
  return pStbCur;
133,921✔
641
}
642

643
void metaCloseStbCursor(SMStbCursor *pStbCur) {
133,922✔
644
  if (pStbCur) {
133,922!
645
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
133,922!
646
    if (pStbCur->pCur) {
133,924!
647
      tdbTbcClose(pStbCur->pCur);
133,924✔
648

649
      tdbFree(pStbCur->pKey);
133,924✔
650
      tdbFree(pStbCur->pVal);
133,922✔
651
    }
652

653
    taosMemoryFree(pStbCur);
133,922!
654
  }
655
}
133,925✔
656

657
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
513,835✔
658
  int ret;
659

660
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
513,835✔
661
  if (ret < 0) {
513,828✔
662
    return 0;
133,923✔
663
  }
664
  return *(tb_uid_t *)pStbCur->pKey;
379,905✔
665
}
666

667
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
1,212,380✔
668
  STSchema       *pTSchema = NULL;
1,212,380✔
669
  SSchemaWrapper *pSW = NULL;
1,212,380✔
670

671
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
1,212,380✔
672
  if (!pSW) return NULL;
1,212,707✔
673

674
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
1,212,600✔
675

676
  taosMemoryFree(pSW->pSchema);
1,212,742✔
677
  taosMemoryFree(pSW);
1,212,811!
678
  return pTSchema;
1,212,793✔
679
}
680

681
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,312✔
682
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,312✔
683
  if (*ppTSchema == NULL) {
4,311!
684
    return terrno;
×
685
  }
686
  return TSDB_CODE_SUCCESS;
4,311✔
687
}
688

689
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,202,350✔
690
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,202,350✔
691
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
1,202,762✔
692
    return terrno;
4✔
693
  }
694
  return TSDB_CODE_SUCCESS;
1,202,758✔
695
}
696

697
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
165,462✔
698
  int32_t code = 0;
165,462✔
699
  int32_t lino;
700

701
  void     *pData = NULL;
165,462✔
702
  int       nData = 0;
165,462✔
703
  SSkmDbKey skmDbKey;
704
  if (sver <= 0) {
165,462✔
705
    SMetaInfo info;
706
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
48,491✔
707
      sver = info.skmVer;
48,392✔
708
    } else {
709
      TBC *pSkmDbC = NULL;
112✔
710
      int  c;
711

712
      skmDbKey.uid = suid ? suid : uid;
112✔
713
      skmDbKey.sver = INT32_MAX;
112✔
714

715
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
112✔
716
      TSDB_CHECK_CODE(code, lino, _exit);
107!
717
      metaRLock(pMeta);
107✔
718

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

726
      if (c == 0) {
107!
727
        metaULock(pMeta);
×
728
        tdbTbcClose(pSkmDbC);
×
729
        code = TSDB_CODE_FAILED;
×
730
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
731
        goto _exit;
×
732
      }
733

734
      if (c < 0) {
107✔
735
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
7✔
736
      }
737

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

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

749
      sver = ((SSkmDbKey *)pKey)->sver;
107✔
750

751
      metaULock(pMeta);
107✔
752
      tdbTbcClose(pSkmDbC);
107✔
753
    }
754
  }
755

756
  if (!(sver > 0)) {
165,463!
757
    code = TSDB_CODE_NOT_FOUND;
×
758
    goto _exit;
×
759
  }
760

761
  skmDbKey.uid = suid ? suid : uid;
165,463✔
762
  skmDbKey.sver = sver;
165,463✔
763
  metaRLock(pMeta);
165,463✔
764
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
165,503✔
765
    metaULock(pMeta);
4✔
766
    code = TSDB_CODE_NOT_FOUND;
4✔
767
    goto _exit;
4✔
768
  }
769
  metaULock(pMeta);
165,432✔
770

771
  // decode
772
  SDecoder        dc = {0};
165,495✔
773
  SSchemaWrapper  schema;
774
  SSchemaWrapper *pSchemaWrapper = &schema;
165,495✔
775

776
  tDecoderInit(&dc, pData, nData);
165,495✔
777
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
164,541✔
778
  tDecoderClear(&dc);
164,541✔
779
  tdbFree(pData);
165,388✔
780
  if (TSDB_CODE_SUCCESS != code) {
165,483!
781
    taosMemoryFree(pSchemaWrapper->pSchema);
×
782
    goto _exit;
×
783
  }
784

785
  // convert
786
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
165,483✔
787
  if (pTSchema == NULL) {
165,466!
788
    code = TSDB_CODE_OUT_OF_MEMORY;
×
789
  }
790

791
  *ppTSchema = pTSchema;
165,466✔
792
  taosMemoryFree(pSchemaWrapper->pSchema);
165,466!
793

794
_exit:
165,516✔
795
  return code;
165,516✔
796
}
797

798
// N.B. Called by statusReq per second
799
int64_t metaGetTbNum(SMeta *pMeta) {
256,832✔
800
  // num of child tables (excluding normal tables , stables and others)
801

802
  /* int64_t num = 0; */
803
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
804

805
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
256,832✔
806
}
807

808
void metaUpdTimeSeriesNum(SMeta *pMeta) {
133,931✔
809
  int64_t nCtbTimeSeries = 0;
133,931✔
810
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
133,931!
811
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
133,931✔
812
  }
813
}
133,931✔
814

815
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
816
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
817
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
436,421✔
818
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
436,444✔
819
    metaUpdTimeSeriesNum(pMeta);
133,908✔
820
  }
821

822
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
436,457✔
823
}
824

825
// type: 1 reported timeseries
826
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
436,421!
827
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
436,457✔
828
  if (type == 1) {
436,457✔
829
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
256,832✔
830
  }
831
  return nTimeSeries;
436,453✔
832
}
833

834
typedef struct {
835
  SMeta   *pMeta;
836
  TBC     *pCur;
837
  tb_uid_t uid;
838
  void    *pKey;
839
  void    *pVal;
840
  int      kLen;
841
  int      vLen;
842
} SMSmaCursor;
843

844
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
845
  SMSmaCursor *pSmaCur = NULL;
×
846
  SSmaIdxKey   smaIdxKey;
847
  int          ret;
848
  int          c;
849

850
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
851
  if (pSmaCur == NULL) {
×
852
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
853
    return NULL;
×
854
  }
855

856
  pSmaCur->pMeta = pMeta;
×
857
  pSmaCur->uid = uid;
×
858
  metaRLock(pMeta);
×
859

860
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
861
  if (ret < 0) {
×
862
    metaULock(pMeta);
×
863
    taosMemoryFree(pSmaCur);
×
864
    return NULL;
×
865
  }
866

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

875
  return pSmaCur;
×
876
}
877

878
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
879
  if (pSmaCur) {
×
880
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
881
    if (pSmaCur->pCur) {
×
882
      tdbTbcClose(pSmaCur->pCur);
×
883
      pSmaCur->pCur = NULL;
×
884

885
      tdbFree(pSmaCur->pKey);
×
886
      tdbFree(pSmaCur->pVal);
×
887
    }
888

889
    taosMemoryFree(pSmaCur);
×
890
  }
891
}
×
892

893
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
894
  int         ret;
895
  SSmaIdxKey *pSmaIdxKey;
896

897
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
898
  if (ret < 0) {
×
899
    return 0;
×
900
  }
901

902
  pSmaIdxKey = pSmaCur->pKey;
×
903
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
904
    return 0;
×
905
  }
906

907
  return pSmaIdxKey->uid;
×
908
}
909

910
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
911
  STSmaWrapper *pSW = NULL;
×
912
  SArray       *pSmaIds = NULL;
×
913

914
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
915
    return NULL;
×
916
  }
917

918
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
919
  if (!pSW) {
×
920
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
921
    goto _err;
×
922
  }
923

924
  pSW->number = taosArrayGetSize(pSmaIds);
×
925
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
926

927
  if (!pSW->tSma) {
×
928
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
929
    goto _err;
×
930
  }
931

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

969
    ++smaIdx;
×
970
  }
971

972
  if (smaIdx <= 0) goto _err;
×
973
  pSW->number = smaIdx;
×
974

975
  metaReaderClear(&mr);
×
976
  taosArrayDestroy(pSmaIds);
×
977
  return pSW;
×
978
_err:
×
979
  metaReaderClear(&mr);
×
980
  taosArrayDestroy(pSmaIds);
×
981
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
982
  return NULL;
×
983
}
984

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

1001
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1002

1003
  metaReaderClear(&mr);
×
1004
  return pTSma;
×
1005
}
1006

1007
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1008
  SArray     *pUids = NULL;
×
1009
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1010

1011
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1012
  if (!pCur) {
×
1013
    return NULL;
×
1014
  }
1015

1016
  while (1) {
×
1017
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1018
    if (id == 0) {
×
1019
      break;
×
1020
    }
1021

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

1031
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1032

1033
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1034
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1035
      metaCloseSmaCursor(pCur);
×
1036
      taosArrayDestroy(pUids);
×
1037
      return NULL;
×
1038
    }
1039
  }
1040

1041
  metaCloseSmaCursor(pCur);
×
1042
  return pUids;
×
1043
}
1044

1045
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1046
  SArray     *pUids = NULL;
×
1047
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1048
  tb_uid_t    lastUid = 0;
×
1049

1050
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1051
  if (!pCur) {
×
1052
    return NULL;
×
1053
  }
1054

1055
  while (1) {
×
1056
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1057
    if (uid == 0) {
×
1058
      break;
×
1059
    }
1060

1061
    if (lastUid == uid) {
×
1062
      continue;
×
1063
    }
1064

1065
    lastUid = uid;
×
1066

1067
    if (!pUids) {
×
1068
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1069
      if (!pUids) {
×
1070
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1071
        metaCloseSmaCursor(pCur);
×
1072
        return NULL;
×
1073
      }
1074
    }
1075

1076
    if (!taosArrayPush(pUids, &uid)) {
×
1077
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1078
      metaCloseSmaCursor(pCur);
×
1079
      taosArrayDestroy(pUids);
×
1080
      return NULL;
×
1081
    }
1082
  }
1083

1084
  metaCloseSmaCursor(pCur);
×
1085
  return pUids;
×
1086
}
1087

1088
#endif
1089

1090
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
4,385,359✔
1091
  STag *tag = (STag *)pTag;
4,385,359✔
1092
  if (type == TSDB_DATA_TYPE_JSON) {
4,385,359✔
1093
    return tag;
10,557✔
1094
  }
1095
  bool find = tTagGet(tag, val);
4,374,802✔
1096

1097
  if (!find) {
4,376,959✔
1098
    return NULL;
48,542✔
1099
  }
1100

1101
  return val;
4,328,417✔
1102
}
1103

1104
typedef struct {
1105
  SMeta   *pMeta;
1106
  TBC     *pCur;
1107
  tb_uid_t suid;
1108
  int16_t  cid;
1109
  int16_t  type;
1110
  void    *pKey;
1111
  void    *pVal;
1112
  int32_t  kLen;
1113
  int32_t  vLen;
1114
} SIdxCursor;
1115

1116
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1117
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1118
  SMetaFltParam *param = arg;
6✔
1119
  int32_t        ret = 0;
6✔
1120

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

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

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

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

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

1149
  static const int8_t TRY_ERROR_LIMIT = 1;
1150
  do {
29,144✔
1151
    void   *entryKey = NULL;
29,150✔
1152
    int32_t nEntryKey = -1;
29,150✔
1153
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
29,150✔
1154
    if (valid < 0) break;
28,809✔
1155

1156
    SBtimeIdxKey *p = entryKey;
28,803✔
1157
    if (count > TRY_ERROR_LIMIT) break;
28,803!
1158

1159
    terrno = TSDB_CODE_SUCCESS;
28,803✔
1160
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
28,790✔
1161
    if (terrno != TSDB_CODE_SUCCESS) {
28,635!
1162
      ret = terrno;
×
1163
      break;
×
1164
    }
1165
    if (cmp == 0) {
28,635!
1166
      if (taosArrayPush(pUids, &p->uid) == NULL) {
57,668!
1167
        ret = terrno;
×
1168
        break;
×
1169
      }
1170
    } else {
1171
      if (param->equal == true) {
×
1172
        if (count > TRY_ERROR_LIMIT) break;
×
1173
        count++;
×
1174
      }
1175
    }
1176
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
29,033✔
1177
    if (valid < 0) break;
29,144!
1178
  } while (1);
1179

1180
END:
6✔
1181
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1182
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1183
  taosMemoryFree(pCursor);
6!
1184
  return ret;
6✔
1185
}
1186

1187
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1188
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1189
  SMetaFltParam *param = arg;
×
1190
  int32_t        ret = 0;
×
1191
  char          *buf = NULL;
×
1192

1193
  STagIdxKey *pKey = NULL;
×
1194
  int32_t     nKey = 0;
×
1195

1196
  SIdxCursor *pCursor = NULL;
×
1197
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1198
  if (pCursor == NULL) {
×
1199
    return terrno;
×
1200
  }
1201
  pCursor->pMeta = pMeta;
×
1202
  pCursor->suid = param->suid;
×
1203
  pCursor->cid = param->cid;
×
1204
  pCursor->type = param->type;
×
1205

1206
  char *pName = param->val;
×
1207

1208
  metaRLock(pMeta);
×
1209
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1210
  if (ret != 0) {
×
1211
    goto END;
×
1212
  }
1213

1214
  int cmp = 0;
×
1215
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1216
    goto END;
×
1217
  }
1218
  int32_t valid = 0;
×
1219
  int32_t count = 0;
×
1220

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

1228
    if (count > TRY_ERROR_LIMIT) break;
×
1229

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

1255
END:
×
1256
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1257
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1258
  taosMemoryFree(buf);
×
1259
  taosMemoryFree(pKey);
×
1260

1261
  taosMemoryFree(pCursor);
×
1262

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

1271
  STtlIdxKey *pKey = NULL;
×
1272
  int32_t     nKey = 0;
×
1273

1274
  SIdxCursor *pCursor = NULL;
×
1275
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1276
  if (pCursor == NULL) {
×
1277
    return terrno;
×
1278
  }
1279
  pCursor->pMeta = pMeta;
×
1280
  pCursor->suid = param->suid;
×
1281
  pCursor->cid = param->cid;
×
1282
  pCursor->type = param->type;
×
1283

1284
  metaRLock(pMeta);
×
1285
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1286

1287
END:
×
1288
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1289
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1290
  taosMemoryFree(buf);
×
1291
  taosMemoryFree(pKey);
×
1292

1293
  taosMemoryFree(pCursor);
×
1294

1295
  return ret;
×
1296
  // impl later
1297
  return 0;
1298
}
1299
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6,736✔
1300
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6,736✔
1301
  SMetaFltParam *param = arg;
6,736✔
1302

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

1309
  SDecoder    dc = {0};
6,736✔
1310
  STbDbKey    tbDbKey = {0};
6,736✔
1311
  STagIdxKey *pKey = NULL;
6,736✔
1312
  int32_t     nKey = 0;
6,736✔
1313

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

1324
  metaRLock(pMeta);
6,762✔
1325

1326
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
6,760!
1327

1328
  tbDbKey.uid = param->suid;
6,750✔
1329
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
6,750✔
1330

1331
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
6,750!
1332

1333
  tDecoderInit(&dc, pData, nData);
6,756✔
1334

1335
  code = metaDecodeEntry(&dc, &oStbEntry);
6,752✔
1336
  if (code) {
6,743!
1337
    tDecoderClear(&dc);
×
1338
    goto END;
×
1339
  }
1340

1341
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
6,743!
1342
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1343
  }
1344

1345
  code = TSDB_CODE_INVALID_PARA;
6,743✔
1346

1347
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
9,472!
1348
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
9,479✔
1349
    if (IS_IDX_ON(schema)) {
9,479✔
1350
      if (schema->colId == param->cid && param->type == schema->type) {
9,455!
1351
        code = 0;
6,750✔
1352
        break;
6,750✔
1353
      }
1354
    }
1355
  }
1356

1357
  TAOS_CHECK_GOTO(code, NULL, END);
6,743!
1358

1359
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
6,743✔
1360
  if (code != 0) {
6,757!
1361
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1362
  }
1363

1364
  int32_t maxSize = 0;
6,758✔
1365
  int32_t nTagData = 0;
6,758✔
1366
  void   *tagData = NULL;
6,758✔
1367

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

1376
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
363✔
1377
        maxSize = 4 * nTagData + 1;
103✔
1378
        buf = taosMemoryCalloc(1, maxSize);
103!
1379
        if (buf == NULL) {
103!
1380
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1381
        }
1382

1383
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
103!
1384
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1385
        }
1386

1387
        tagData = buf;
103✔
1388
        nTagData = maxSize;
103✔
1389
      }
1390
    } else {
1391
      tagData = param->val;
6,395✔
1392
      nTagData = tDataTypes[param->type].bytes;
6,395✔
1393
    }
1394
  }
1395

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

1400
  int cmp = 0;
6,757✔
1401
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
6,757!
1402

1403
  int     count = 0;
6,753✔
1404
  int32_t valid = 0;
6,753✔
1405
  bool    found = false;
6,753✔
1406

1407
  static const int8_t TRY_ERROR_LIMIT = 1;
1408

1409
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1410
  /// target:                        [suid, cid2, type2]
1411
  int diffCidCount = 0;
6,753✔
1412
  do {
80,649✔
1413
    void   *entryKey = NULL, *entryVal = NULL;
87,402✔
1414
    int32_t nEntryKey, nEntryVal;
1415

1416
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
87,402✔
1417
    if (valid < 0) {
86,905✔
1418
      break;
6,758✔
1419
    }
1420
    if (count > TRY_ERROR_LIMIT) {
83,451✔
1421
      break;
1,521✔
1422
    }
1423

1424
    STagIdxKey *p = entryKey;
81,930✔
1425
    if (p == NULL) break;
81,930!
1426

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

1441
    terrno = TSDB_CODE_SUCCESS;
79,875✔
1442
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
79,877✔
1443
    if (terrno != TSDB_CODE_SUCCESS) {
79,857!
1444
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1445
      break;
×
1446
    }
1447
    if (cmp == 0) {
79,910✔
1448
      // match
1449
      tb_uid_t tuid = 0;
72,939✔
1450
      if (IS_VAR_DATA_TYPE(pKey->type)) {
72,939!
1451
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
600✔
1452
      } else {
1453
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
72,339✔
1454
      }
1455
      if (taosArrayPush(pUids, &tuid) == NULL) {
73,292!
1456
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1457
      }
1458
      found = true;
73,292✔
1459
    } else {
1460
      if (param->equal == true) {
6,971✔
1461
        if (count > TRY_ERROR_LIMIT) break;
4,002!
1462
        count++;
4,002✔
1463
      }
1464
    }
1465
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
80,263✔
1466
    if (valid < 0) {
80,327!
1467
      code = valid;
×
1468
      break;
×
1469
    }
1470
  } while (1);
1471

1472
END:
6,758✔
1473
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6,758✔
1474
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6,765✔
1475
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
6,762!
1476
  tDecoderClear(&dc);
6,762✔
1477
  tdbFree(pData);
6,761✔
1478

1479
  taosMemoryFree(buf);
6,760!
1480
  taosMemoryFree(pKey);
6,759!
1481

1482
  taosMemoryFree(pCursor);
6,762!
1483

1484
  return code;
6,763✔
1485
}
1486

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

1493
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
62,690✔
1494
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
62,690✔
1495
  if (lock) {
62,862!
1496
    metaULock(pMeta);
×
1497
  }
1498

1499
  return ret;
62,862✔
1500
}
1501

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

1506
  int32_t isLock = false;
6,068✔
1507
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
6,068!
1508
  for (int i = 0; i < sz; i++) {
68,793✔
1509
    STUidTagInfo *p = taosArrayGet(uidList, i);
62,737✔
1510

1511
    if (i % LIMIT == 0) {
62,719✔
1512
      if (isLock) metaULock(pMeta);
4,880!
1513

1514
      metaRLock(pMeta);
4,880✔
1515
      isLock = true;
4,880✔
1516
    }
1517

1518
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1519
    void   *val = NULL;
62,719✔
1520
    int32_t len = 0;
62,719✔
1521
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
62,719!
1522
      p->pTagVal = taosMemoryMalloc(len);
62,862!
1523
      if (!p->pTagVal) {
62,887!
1524
        if (isLock) metaULock(pMeta);
×
1525

1526
        TAOS_RETURN(terrno);
×
1527
      }
1528
      memcpy(p->pTagVal, val, len);
62,887✔
1529
      tdbFree(val);
62,887✔
1530
    } else {
1531
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
×
1532
    }
1533
  }
1534
  //  }
1535
  if (isLock) metaULock(pMeta);
6,056✔
1536
  return 0;
6,068✔
1537
}
1538

1539
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
119,280✔
1540
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
119,280✔
1541
  if (!pCur) {
119,299!
1542
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1543
  }
1544

1545
  // If len > 0 means there already have uids, and we only want the
1546
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1547
  // in the hash map, that may require a lot of memory
1548
  SHashObj *pSepecifiedUidMap = NULL;
119,299✔
1549
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
119,299✔
1550
  if (numOfElems > 0) {
119,298✔
1551
    pSepecifiedUidMap =
1552
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
39,641✔
1553
    for (int i = 0; i < numOfElems; i++) {
235,583✔
1554
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
195,929✔
1555
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
195,917✔
1556
      if (code) {
195,959!
1557
        metaCloseCtbCursor(pCur);
×
1558
        taosHashCleanup(pSepecifiedUidMap);
×
1559
        return code;
×
1560
      }
1561
    }
1562
  }
1563

1564
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
119,311✔
1565
    while (1) {
436,924✔
1566
      tb_uid_t uid = metaCtbCursorNext(pCur);
516,570✔
1567
      if (uid == 0) {
510,799✔
1568
        break;
79,711✔
1569
      }
1570

1571
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
431,088✔
1572
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
431,088!
1573
      if (!info.pTagVal) {
438,608!
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
438,608✔
1579
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
436,924!
1580
        taosMemoryFreeClear(info.pTagVal);
×
1581
        metaCloseCtbCursor(pCur);
×
1582
        taosHashCleanup(pSepecifiedUidMap);
×
1583
        return terrno;
×
1584
      }
1585
    }
1586
  } else {  // only the specified tables need to be added
1587
    while (1) {
258,703✔
1588
      tb_uid_t uid = metaCtbCursorNext(pCur);
298,368✔
1589
      if (uid == 0) {
298,535✔
1590
        break;
39,647✔
1591
      }
1592

1593
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
258,888✔
1594
      if (index == NULL) {
258,543✔
1595
        continue;
63,013✔
1596
      }
1597

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

1611
  taosHashCleanup(pSepecifiedUidMap);
119,358✔
1612
  metaCloseCtbCursor(pCur);
119,353✔
1613
  return TSDB_CODE_SUCCESS;
119,379✔
1614
}
1615

1616
int32_t metaFlagCache(SVnode *pVnode) {
×
1617
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1618
  if (!pCur) {
×
1619
    return terrno;
×
1620
  }
1621

1622
  SArray *suids = NULL;
×
1623
  while (1) {
×
1624
    tb_uid_t id = metaStbCursorNext(pCur);
×
1625
    if (id == 0) {
×
1626
      break;
×
1627
    }
1628

1629
    if (!suids) {
×
1630
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1631
      if (!suids) {
×
1632
        return terrno;
×
1633
      }
1634
    }
1635

1636
    if (taosArrayPush(suids, &id) == NULL) {
×
1637
      taosArrayDestroy(suids);
×
1638
      return terrno;
×
1639
    }
1640
  }
1641

1642
  metaCloseStbCursor(pCur);
×
1643

1644
  for (int idx = 0; suids && idx < TARRAY_SIZE(suids); ++idx) {
×
1645
    tb_uid_t id = ((tb_uid_t *)TARRAY_DATA(suids))[idx];
×
1646
    STsdb   *pTsdb = pVnode->pTsdb;
×
1647
    SMeta   *pMeta = pVnode->pMeta;
×
1648
    SArray  *uids = NULL;
×
1649

1650
    int32_t code = metaGetChildUidsOfSuperTable(pMeta, id, &uids);
×
1651
    if (code) {
×
1652
      metaError("vgId:%d, failed to get subtables, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1653

1654
      taosArrayDestroy(uids);
×
1655
      taosArrayDestroy(suids);
×
1656

1657
      return code;
×
1658
    }
1659

1660
    if (uids && TARRAY_SIZE(uids) > 0) {
×
1661
      STSchema *pTSchema = NULL;
×
1662

1663
      code = metaGetTbTSchemaEx(pMeta, id, id, -1, &pTSchema);
×
1664
      if (code) {
×
1665
        metaError("vgId:%d, failed to get schema, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1666

1667
        taosArrayDestroy(uids);
×
1668
        taosArrayDestroy(suids);
×
1669

1670
        return code;
×
1671
      }
1672

1673
      int32_t nCol = pTSchema->numOfCols;
×
1674
      for (int32_t i = 0; i < nCol; ++i) {
×
1675
        int16_t cid = pTSchema->columns[i].colId;
×
1676
        int8_t  col_type = pTSchema->columns[i].type;
×
1677

1678
        code = tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type);
×
1679
        if (code) {
×
1680
          metaError("vgId:%d, failed to flag cache, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1681

1682
          tDestroyTSchema(pTSchema);
×
1683
          taosArrayDestroy(uids);
×
1684
          taosArrayDestroy(suids);
×
1685

1686
          return code;
×
1687
        }
1688
      }
1689

1690
      tDestroyTSchema(pTSchema);
×
1691
    }
1692

1693
    taosArrayDestroy(uids);
×
1694
  }
1695

1696
  taosArrayDestroy(suids);
×
1697

1698
  return TSDB_CODE_SUCCESS;
×
1699
}
1700

1701
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1702

1703
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
7,654,127✔
1704
  int32_t code = 0;
7,654,127✔
1705
  void   *pData = NULL;
7,654,127✔
1706
  int     nData = 0;
7,654,127✔
1707
  int     lock = 0;
7,654,127✔
1708

1709
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
7,654,127!
1710
    lock = 1;
2,780,787✔
1711
  }
1712

1713
  if (!lock) metaRLock(pMeta);
7,654,127✔
1714

1715
  // search cache
1716
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
7,656,137✔
1717
    if (!lock) metaULock(pMeta);
7,393,005✔
1718
    goto _exit;
7,393,105✔
1719
  }
1720

1721
  // search TDB
1722
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
263,859✔
1723
    // not found
1724
    if (!lock) metaULock(pMeta);
248,220✔
1725
    goto _exit;
248,200✔
1726
  }
1727

1728
  if (!lock) metaULock(pMeta);
15,687✔
1729

1730
  pInfo->uid = uid;
15,687✔
1731
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
15,687✔
1732
  pInfo->version = ((SUidIdxVal *)pData)->version;
15,687✔
1733
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
15,687✔
1734

1735
  if (lock) {
15,687✔
1736
    metaULock(pReader->pMeta);
3,550✔
1737
    // metaReaderReleaseLock(pReader);
1738
  }
1739
  // upsert the cache
1740
  metaWLock(pMeta);
15,687✔
1741
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
15,688✔
1742
  if (ret != 0) {
15,686!
1743
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1744
  }
1745
  metaULock(pMeta);
15,686✔
1746

1747
  if (lock) {
15,687✔
1748
    metaRLock(pReader->pMeta);
3,549✔
1749
  }
1750

1751
_exit:
12,138✔
1752
  tdbFree(pData);
7,656,993✔
1753
  return code;
7,656,675✔
1754
}
1755

1756
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
560,870✔
1757
  int32_t code = 0;
560,870✔
1758

1759
  if (!numOfTables && !numOfCols) goto _exit;
560,870!
1760

1761
  SVnode *pVnodeObj = pVnode;
560,870✔
1762
  metaRLock(pVnodeObj->pMeta);
560,870✔
1763

1764
  // fast path: search cache
1765
  SMetaStbStats state = {0};
560,916✔
1766
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
560,916✔
1767
    metaULock(pVnodeObj->pMeta);
536,352✔
1768
    if (numOfTables) *numOfTables = state.ctbNum;
536,367✔
1769
    if (numOfCols) *numOfCols = state.colNum;
536,367✔
1770
    if (flags) *flags = state.flags;
536,367✔
1771
    goto _exit;
536,367✔
1772
  }
1773

1774
  // slow path: search TDB
1775
  int64_t ctbNum = 0;
24,546✔
1776
  int32_t colNum = 0;
24,546✔
1777
  int64_t keep = 0;
24,546✔
1778
  int8_t  flag = 0;
24,546✔
1779

1780
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
24,546✔
1781
  if (TSDB_CODE_SUCCESS == code) {
24,544!
1782
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
24,544✔
1783
  }
1784
  if (TSDB_CODE_SUCCESS == code) {
24,547!
1785
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
24,547✔
1786
  }
1787
  metaULock(pVnodeObj->pMeta);
24,546✔
1788
  if (TSDB_CODE_SUCCESS != code) {
24,547!
1789
    goto _exit;
×
1790
  }
1791

1792
  if (numOfTables) *numOfTables = ctbNum;
24,547✔
1793
  if (numOfCols) *numOfCols = colNum;
24,547✔
1794
  if (flags) *flags = flag;
24,547✔
1795

1796
  state.uid = uid;
24,547✔
1797
  state.ctbNum = ctbNum;
24,547✔
1798
  state.colNum = colNum;
24,547✔
1799
  state.flags = flag;
24,547✔
1800
  state.keep = keep;
24,547✔
1801
  // upsert the cache
1802
  metaWLock(pVnodeObj->pMeta);
24,547✔
1803

1804
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
24,547✔
1805
  if (ret) {
24,546!
1806
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1807
              uid, ctbNum, colNum, keep, flag);
1808
  }
1809

1810
  metaULock(pVnodeObj->pMeta);
24,546✔
1811

1812
_exit:
560,895✔
1813
  return code;
560,895✔
1814
}
1815

1816
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
187,071✔
1817
  SMetaStbStats stats = {0};
187,071✔
1818

1819
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
187,071✔
1820
    stats.ctbNum += deltaCtb;
165,968✔
1821
    stats.colNum += deltaCol;
165,968✔
1822
    if (deltaKeep > 0) {
165,968✔
1823
      stats.keep = deltaKeep;
11✔
1824
    }
1825

1826
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
165,968✔
1827
    if (code) {
165,947!
1828
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1829
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1830
    }
1831
  }
1832
}
187,099✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc