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

taosdata / TDengine / #4847

11 Nov 2025 05:50AM UTC coverage: 62.651% (+0.3%) from 62.306%
#4847

push

travis-ci

web-flow
Merge e78cd6509 into 47a2ea7a0

542 of 650 new or added lines in 16 files covered. (83.38%)

1515 existing lines in 91 files now uncovered.

113826 of 181682 relevant lines covered (62.65%)

113230552.12 hits per line

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

61.08
/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 "tencode.h"
18

19
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
514,257,903✔
21
  metaReaderDoInit(pReader, pMeta, flags);
514,257,903✔
22
  pReader->pAPI = pAPI;
514,307,729✔
23
}
514,298,676✔
24

514,293,872✔
25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
26
  memset(pReader, 0, sizeof(*pReader));
597,402,859✔
27
  pReader->pMeta = pMeta;
597,402,859✔
28
  pReader->flags = flags;
597,402,859✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
597,405,751✔
30
    metaRLock(pMeta);
597,346,206✔
31
  }
418,313,253✔
32
}
33

597,415,323✔
34
void metaReaderReleaseLock(SMetaReader *pReader) {
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
222,787,113✔
36
    metaULock(pReader->pMeta);
222,787,113✔
37
    pReader->flags |= META_READER_NOLOCK;
222,908,239✔
38
  }
222,722,025✔
39
}
40

222,787,193✔
41
void metaReaderClear(SMetaReader *pReader) {
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
628,780,772✔
43
    metaULock(pReader->pMeta);
628,780,772✔
44
  }
195,347,116✔
45
  tDecoderClear(&pReader->coder);
46
  tdbFree(pReader->pBuf);
628,715,813✔
47
  pReader->pBuf = NULL;
628,850,277✔
48
}
628,756,429✔
49

628,764,453✔
50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
51
  int32_t  code = 0;
951,220,377✔
52
  SMeta   *pMeta = pReader->pMeta;
951,220,377✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
951,220,377✔
54

951,291,057✔
55
  // query table.db
56
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
57
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
951,298,460✔
UNCOV
58
  }
×
59

60
  // decode the entry
61
  tDecoderClear(&pReader->coder);
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
951,214,283✔
63

951,175,846✔
64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
65
  if (code) {
951,254,790✔
66
    tDecoderClear(&pReader->coder);
951,201,387✔
67
    return code;
×
UNCOV
68
  }
×
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
72
}
951,201,387✔
73

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

54,613,677✔
78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
79
    metaULock(pVnodeObj->pMeta);
54,615,685✔
80
    return false;
193,236✔
81
  }
193,236✔
82

83
  metaULock(pVnodeObj->pMeta);
84
  return true;
54,428,777✔
85
}
54,431,781✔
86

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

91
  // query uid.idx
92
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
93
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
270,595,671✔
94
  }
224,725✔
95

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
270,349,371✔
98
}
270,359,007✔
99

100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
101
  SMeta *pMeta = pReader->pMeta;
335,429,208✔
102

335,429,208✔
103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
335,398,976✔
105
  if (TSDB_CODE_SUCCESS != code) {
335,457,652✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
335,421,650✔
107
  }
896✔
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
110
}
335,420,754✔
111

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

116
  // query name.idx
117
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
65,429,170✔
119
  }
10,951,209✔
120

121
  uid = *(tb_uid_t *)pReader->pBuf;
122
  return metaReaderGetTableEntryByUid(pReader, uid);
54,477,640✔
123
}
54,477,392✔
124

125
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
126
  void    *pData = NULL;
58,141,990✔
127
  int      nData = 0;
58,141,990✔
128
  tb_uid_t uid = 0;
58,142,172✔
129

58,142,293✔
130
  metaRLock(pMeta);
131

58,142,293✔
132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
133
    uid = *(tb_uid_t *)pData;
58,141,007✔
134
    tdbFree(pData);
9,347,897✔
135
  }
9,347,897✔
136

137
  metaULock(pMeta);
138

58,136,439✔
139
  return uid;
140
}
58,138,673✔
141

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

152
  STR_TO_VARSTR(tbName, mr.me.name);
153
  metaReaderClear(&mr);
7,560,266✔
154

7,558,728✔
155
  return 0;
156
}
7,559,889✔
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;
×
UNCOV
166
  }
×
167
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
168
  metaReaderClear(&mr);
×
UNCOV
169

×
170
  return 0;
UNCOV
171
}
×
172

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

665,397✔
178
  SMetaReader *pReader = &mr;
179

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

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

398,474✔
188
  metaReaderClear(&mr);
189

398,474✔
190
  return 0;
191
}
398,474✔
192

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

411,542✔
198
  code = metaGetTableEntryByName(&mr, tbName);
199
  if (code == 0) *tbType = mr.me.type;
411,542✔
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
411,117✔
201
    *suid = mr.me.ctbEntry.suid;
411,542✔
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
405,258✔
203
    *suid = mr.me.uid;
6,284✔
204
  } else {
5,866✔
205
    *suid = 0;
206
  }
418✔
207

208
  metaReaderClear(&mr);
209
  return code;
411,542✔
210
}
411,117✔
211

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

×
215
  // TODO
216

217
  return 0;
UNCOV
218
}
×
219

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

236
  code = 0;
UNCOV
237

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

243
#if 1  // ===================================================
244
SMTbCursor *metaOpenTbCursor(void *pVnode) {
245
  SMTbCursor *pTbCur = NULL;
9,892,801✔
246
  int32_t     code;
9,892,801✔
247

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
249
  if (pTbCur == NULL) {
9,892,801✔
250
    return NULL;
9,892,202✔
UNCOV
251
  }
×
252

253
  SVnode *pVnodeObj = pVnode;
254
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
9,892,202✔
255
  pTbCur->pMeta = pVnodeObj->pMeta;
256
  pTbCur->paused = 1;
9,892,202✔
257
  code = metaResumeTbCursor(pTbCur, 1, 0);
9,893,398✔
258
  if (code) {
9,893,398✔
259
    terrno = code;
9,890,030✔
260
    taosMemoryFree(pTbCur);
×
261
    return NULL;
×
UNCOV
262
  }
×
263
  return pTbCur;
264
}
9,890,030✔
265

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
267
  if (pTbCur) {
19,772,799✔
268
    tdbFree(pTbCur->pKey);
19,772,799✔
269
    tdbFree(pTbCur->pVal);
9,891,487✔
270
    if (!pTbCur->paused) {
9,892,218✔
271
      metaReaderClear(&pTbCur->mr);
9,893,157✔
272
      if (pTbCur->pDbc) {
4,446,826✔
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
4,447,380✔
274
      }
4,445,717✔
275
    }
276
    taosMemoryFree(pTbCur);
277
  }
9,890,225✔
278
}
279

19,774,470✔
280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
281
  if (!pTbCur->paused) {
5,745,359✔
282
    metaReaderClear(&pTbCur->mr);
5,745,359✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
5,745,359✔
284
    pTbCur->paused = 1;
5,745,828✔
285
  }
5,745,828✔
286
}
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
5,745,828✔
288
  int32_t code = 0;
10,190,772✔
289
  int32_t lino;
10,190,772✔
290
  int8_t  locked = 0;
291
  if (pTbCur->paused) {
10,190,772✔
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
10,190,772✔
293
    locked = 1;
10,193,208✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
10,192,570✔
295
    if (code != 0) {
10,192,570✔
296
      TSDB_CHECK_CODE(code, lino, _exit);
10,189,770✔
UNCOV
297
    }
×
298

299
    if (first) {
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
10,189,770✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
9,890,274✔
302
    } else {
9,886,540✔
303
      int c = 1;
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
299,496✔
305
      TSDB_CHECK_CODE(code, lino, _exit);
299,496✔
306
      if (c == 0) {
299,496✔
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
299,496✔
308
      } else if (c < 0) {
299,496✔
309
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
310
        TSDB_CHECK_CODE(code, lino, _exit);
×
UNCOV
311
      } else {
×
312
        code = tdbTbcMoveToNext(pTbCur->pDbc);
313
        TSDB_CHECK_CODE(code, lino, _exit);
×
UNCOV
314
      }
×
315
    }
316

317
    pTbCur->paused = 0;
318
  }
10,185,680✔
319

320
_exit:
UNCOV
321
  if (code != 0 && locked) {
×
322
    metaReaderReleaseLock(&pTbCur->mr);
10,188,128✔
UNCOV
323
  }
×
324
  return code;
325
}
10,188,888✔
326

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
328
  int    ret;
355,362,750✔
329
  void  *pBuf;
330
  STbCfg tbCfg;
331

332
  for (;;) {
333
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
334
    if (ret < 0) {
355,362,750✔
335
      return ret;
355,368,789✔
336
    }
9,883,129✔
337

338
    tDecoderClear(&pTbCur->mr.coder);
339

345,485,660✔
340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
341
    if (ret) return ret;
345,491,996✔
342

345,483,191✔
343
    if (pTbCur->mr.me.type == jumpTableType) {
344
      continue;
345,483,191✔
345
    }
3,789,039✔
346

347
    break;
348
  }
341,698,182✔
349

350
  return 0;
351
}
341,698,182✔
352

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

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

364
    tDecoderClear(&pTbCur->mr.coder);
UNCOV
365

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

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

375
    break;
UNCOV
376
  }
×
377

378
  return 0;
UNCOV
379
}
×
380

381
/**
382
 * @param type 0x01 fetchRsmaSchema if table is rsma
383
 */
384
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
385
                                   int8_t type) {
157,438,206✔
386
  int32_t         code = 0;
387
  void           *pData = NULL;
157,438,206✔
388
  int             nData = 0;
157,438,206✔
389
  int64_t         version;
157,408,286✔
390
  SSchemaWrapper  schema = {0};
391
  SSchemaWrapper *pSchema = NULL;
157,396,014✔
392
  SDecoder        dc = {0};
157,342,421✔
393
  if (lock) {
157,342,421✔
394
    metaRLock(pMeta);
157,434,631✔
395
  }
150,685,901✔
396
_query:
397
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
168,816,202✔
398
    goto _err;
168,832,323✔
399
  }
180,379✔
400

401
  version = ((SUidIdxVal *)pData)[0].version;
402

168,673,889✔
403
  if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData)) !=
404
      0) {
168,660,854✔
405
    goto _err;
UNCOV
406
  }
×
407

408
  SMetaEntry me = {0};
409
  tDecoderInit(&dc, pData, nData);
168,682,031✔
410
  code = metaDecodeEntry(&dc, &me);
168,677,213✔
411
  if (code) {
168,677,532✔
412
    tDecoderClear(&dc);
168,611,789✔
413
    goto _err;
×
UNCOV
414
  }
×
415
  if (me.type == TSDB_SUPER_TABLE) {
416
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
168,611,789✔
417
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
125,690,894✔
418
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
125,657,537✔
419
      if (TABLE_IS_ROLLUP(me.flags)) {
125,657,537✔
420
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
125,658,449✔
421
          tDecoderClear(&dc);
192,750✔
422
          goto _err;
×
UNCOV
423
        }
×
424
      }
425
      tDecoderClear(&dc);
426
      goto _exit;
125,658,449✔
427
    }
125,659,024✔
428
  } else if (me.type == TSDB_CHILD_TABLE) {
429
    uid = me.ctbEntry.suid;
42,920,895✔
430
    tDecoderClear(&dc);
11,370,995✔
431
    goto _query;
11,370,995✔
432
  } else {
11,372,671✔
433
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
434
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
31,549,900✔
435
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
31,552,609✔
436
      tDecoderClear(&dc);
31,552,609✔
437
      goto _exit;
31,555,365✔
438
    }
31,560,532✔
439
  }
440
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
441
  tDecoderClear(&dc);
27,684✔
442

27,684✔
443
  // query from skm db
444
  if ((code = tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) < 0) {
445
    goto _err;
35,278✔
UNCOV
446
  }
×
447

448
  tDecoderInit(&dc, pData, nData);
449
  if ((code = tDecodeSSchemaWrapperEx(&dc, &schema)) != 0) {
35,278✔
450
    goto _err;
35,278✔
UNCOV
451
  }
×
452
  pSchema = tCloneSSchemaWrapper(&schema);
453
  if (pSchema == NULL) {
35,278✔
454
    code = terrno;
35,278✔
455
    goto _err;
×
UNCOV
456
  }
×
457
  tDecoderClear(&dc);
458

35,278✔
459
_exit:
460
  if (lock) {
157,254,834✔
461
    metaULock(pMeta);
157,291,742✔
462
  }
150,524,277✔
463
  tdbFree(pData);
464
  return pSchema;
157,240,196✔
465

157,248,959✔
466
_err:
467
  if (lock) {
170,164✔
468
    metaULock(pMeta);
180,379✔
469
  }
180,379✔
470
  tdbFree(pData);
471
  tDeleteSchemaWrapper(pSchema);
180,379✔
472
  if (extSchema != NULL) {
473
    taosMemoryFreeClear(*extSchema);
180,379✔
474
  }
155,258✔
475
  terrno = code;
476
  return NULL;
180,379✔
477
}
180,379✔
478

479
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
480
  void    *pData = NULL;
11,364✔
481
  int      nData = 0;
11,364✔
482
  int64_t  version = 0;
11,364✔
483
  SDecoder dc = {0};
11,364✔
484
  int64_t  createTime = INT64_MAX;
11,364✔
485
  if (lock) {
11,364✔
486
    metaRLock(pMeta);
11,364✔
487
  }
11,364✔
488

489
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
490
    goto _exit;
11,338✔
UNCOV
491
  }
×
492

493
  version = ((SUidIdxVal *)pData)[0].version;
494

11,364✔
495
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
496
    goto _exit;
11,364✔
UNCOV
497
  }
×
498

499
  SMetaEntry me = {0};
500
  tDecoderInit(&dc, pData, nData);
11,364✔
501
  int32_t code = metaDecodeEntry(&dc, &me);
11,364✔
502
  if (code) {
11,364✔
503
    tDecoderClear(&dc);
11,338✔
504
    goto _exit;
×
UNCOV
505
  }
×
506
  if (me.type == TSDB_CHILD_TABLE) {
507
    createTime = me.ctbEntry.btime;
11,338✔
508
  } else if (me.type == TSDB_NORMAL_TABLE) {
11,284✔
509
    createTime = me.ntbEntry.btime;
54✔
510
  }
54✔
511
  tDecoderClear(&dc);
512

11,338✔
513
_exit:
514
  if (lock) {
11,364✔
515
    metaULock(pMeta);
11,364✔
516
  }
11,364✔
517
  tdbFree(pData);
518
  return createTime;
11,364✔
519
}
11,364✔
520

521
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
522
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
75,504,077✔
523
  SMCtbCursor *pCtbCur = NULL;
75,504,077✔
524
  SCtbIdxKey   ctbIdxKey;
75,525,048✔
525
  int          ret = 0;
526
  int          c = 0;
75,525,048✔
527

75,525,048✔
528
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
529
  if (pCtbCur == NULL) {
75,525,048✔
530
    return NULL;
75,408,710✔
UNCOV
531
  }
×
532

533
  pCtbCur->pMeta = pMeta;
534
  pCtbCur->suid = uid;
75,408,710✔
535
  pCtbCur->lock = lock;
75,416,181✔
536
  pCtbCur->paused = 1;
75,430,351✔
537

75,437,981✔
538
  ret = metaResumeCtbCursor(pCtbCur, 1);
539
  if (ret < 0) {
75,454,503✔
540
    return NULL;
75,521,122✔
UNCOV
541
  }
×
542
  return pCtbCur;
543
}
75,521,122✔
544

545
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
546
  if (pCtbCur) {
75,531,581✔
547
    if (!pCtbCur->paused) {
75,531,581✔
548
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
75,539,604✔
549
      if (pCtbCur->pCur) {
73,750,623✔
550
        tdbTbcClose(pCtbCur->pCur);
73,747,470✔
551
      }
73,758,197✔
552
    }
553
    tdbFree(pCtbCur->pKey);
554
    tdbFree(pCtbCur->pVal);
75,520,506✔
555
  }
75,503,046✔
556
  taosMemoryFree(pCtbCur);
557
}
75,509,728✔
558

75,510,563✔
559
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
560
  if (!pCtbCur->paused) {
1,784,784✔
561
    tdbTbcClose((TBC *)pCtbCur->pCur);
1,784,784✔
562
    if (pCtbCur->lock) {
1,784,428✔
563
      metaULock(pCtbCur->pMeta);
1,784,116✔
564
    }
1,784,292✔
565
    pCtbCur->paused = 1;
566
  }
1,784,053✔
567
}
568

1,785,111✔
569
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
570
  if (pCtbCur->paused) {
75,504,094✔
571
    pCtbCur->paused = 0;
75,504,094✔
572

75,489,597✔
573
    if (pCtbCur->lock) {
574
      metaRLock(pCtbCur->pMeta);
75,382,288✔
575
    }
68,754,596✔
576
    int ret = 0;
577
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
75,468,024✔
578
    if (ret < 0) {
75,468,024✔
579
      metaCloseCtbCursor(pCtbCur);
75,493,828✔
580
      return -1;
×
UNCOV
581
    }
×
582

583
    if (first) {
584
      SCtbIdxKey ctbIdxKey;
75,493,828✔
585
      // move to the suid
75,463,655✔
586
      ctbIdxKey.suid = pCtbCur->suid;
587
      ctbIdxKey.uid = INT64_MIN;
75,497,487✔
588
      int c = 0;
75,522,865✔
589
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
75,522,865✔
590
      if (c > 0) {
75,472,313✔
591
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
75,531,157✔
592
      }
10,841,121✔
593
    } else {
594
      int c = 0;
595
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
596
      if (c < 0) {
×
597
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
UNCOV
598
      } else {
×
599
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
UNCOV
600
      }
×
601
    }
602
  }
603
  return 0;
604
}
75,523,292✔
605

606
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
607
  int         ret;
466,228,883✔
608
  SCtbIdxKey *pCtbIdxKey;
609

610
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
611
  if (ret < 0) {
466,228,883✔
612
    return 0;
466,267,154✔
613
  }
55,548,438✔
614

615
  pCtbIdxKey = pCtbCur->pKey;
616
  if (pCtbIdxKey->suid > pCtbCur->suid) {
410,718,716✔
617
    return 0;
410,759,238✔
618
  }
20,036,964✔
619

620
  return pCtbIdxKey->uid;
621
}
390,678,087✔
622

623
struct SMStbCursor {
624
  SMeta   *pMeta;
625
  TBC     *pCur;
626
  tb_uid_t suid;
627
  void    *pKey;
628
  void    *pVal;
629
  int      kLen;
630
  int      vLen;
631
};
632

633
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
634
  SMStbCursor *pStbCur = NULL;
56,567,556✔
635
  int          ret = 0;
56,567,556✔
636
  int          c = 0;
56,567,556✔
637

56,567,556✔
638
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
639
  if (pStbCur == NULL) {
56,570,756✔
640
    terrno = TSDB_CODE_OUT_OF_MEMORY;
56,566,096✔
641
    return NULL;
×
UNCOV
642
  }
×
643

644
  pStbCur->pMeta = pMeta;
645
  pStbCur->suid = suid;
56,566,096✔
646
  metaRLock(pMeta);
56,562,256✔
647

56,567,466✔
648
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
649
  if (ret < 0) {
56,571,306✔
650
    terrno = TSDB_CODE_OUT_OF_MEMORY;
56,554,772✔
651
    metaULock(pMeta);
×
652
    taosMemoryFree(pStbCur);
×
653
    return NULL;
×
UNCOV
654
  }
×
655

656
  // move to the suid
657
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
658
  if (c > 0) {
56,554,772✔
659
    ret = tdbTbcMoveToNext(pStbCur->pCur);
56,572,036✔
UNCOV
660
  }
×
661

662
  return pStbCur;
663
}
56,569,476✔
664

665
void metaCloseStbCursor(SMStbCursor *pStbCur) {
666
  if (pStbCur) {
56,568,746✔
667
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
56,568,746✔
668
    if (pStbCur->pCur) {
56,570,026✔
669
      tdbTbcClose(pStbCur->pCur);
56,572,036✔
670

56,568,836✔
671
      tdbFree(pStbCur->pKey);
672
      tdbFree(pStbCur->pVal);
56,567,556✔
673
    }
56,570,116✔
674

675
    taosMemoryFree(pStbCur);
676
  }
56,569,476✔
677
}
678

56,564,356✔
679
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
680
  int ret;
162,027,931✔
681

682
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
683
  if (ret < 0) {
162,027,931✔
684
    return 0;
162,028,661✔
685
  }
56,572,676✔
686
  return *(tb_uid_t *)pStbCur->pKey;
687
}
105,455,985✔
688

689
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
690
  STSchema       *pTSchema = NULL;
137,513,844✔
691
  SSchemaWrapper *pSW = NULL;
137,513,844✔
692

137,513,844✔
693
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
694
  if (!pSW) return NULL;
137,513,844✔
695

137,534,443✔
696
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
697

137,517,257✔
698
  tDeleteSchemaWrapper(pSW);
699
  return pTSchema;
700
}
137,427,641✔
701

702
/**
703
 * Fetch rsma schema if table is rsma
704
 */
705
SRSchema *metaGetTbTSchemaR(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
706
  SRSchema       *pRSchema = NULL;
54,000✔
707
  SSchemaWrapper *pSW = NULL;
54,000✔
708

54,000✔
709
  if (!(pRSchema = (SRSchema *)taosMemoryCalloc(1, sizeof(SRSchema)))) goto _err;
710
  if (!(pSW = metaGetTableSchema(pMeta, uid, sver, lock, (SExtSchema **)&pRSchema->extSchema, 0x01))) goto _err;
54,000✔
711
  if (!(pRSchema->tSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version))) goto _err;
54,000✔
712

54,000✔
713
  if (pSW->pRsma) {
714
    if (!(pRSchema->funcIds = taosMemoryCalloc(pSW->nCols, sizeof(func_id_t)))) goto _err;
54,000✔
715
    memcpy(pRSchema->funcIds, pSW->pRsma->funcIds, pSW->nCols * sizeof(func_id_t));
54,000✔
716

54,000✔
717
    pRSchema->tbType = pSW->pRsma->tbType;
718
    pRSchema->tbUid = uid;
54,000✔
719
    tstrncpy(pRSchema->tbName, pSW->pRsma->tbName, TSDB_TABLE_NAME_LEN);
54,000✔
720
    pRSchema->interval[0] = pSW->pRsma->interval[0];
54,000✔
721
    pRSchema->interval[1] = pSW->pRsma->interval[1];
54,000✔
722
  }
54,000✔
723

724
_exit:
725
  tDeleteSchemaWrapper(pSW);
54,000✔
726
  return pRSchema;
727
_err:
54,000✔
UNCOV
728
  tDeleteSchemaWrapper(pSW);
×
729
  tFreeSRSchema(&pRSchema);
730
  return NULL;
UNCOV
731
}
×
732

733
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
734
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
416,745✔
735
  if (*ppTSchema == NULL) {
416,745✔
736
    return terrno;
416,745✔
UNCOV
737
  }
×
738
  return TSDB_CODE_SUCCESS;
739
}
416,745✔
740

741
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
742
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
135,887,766✔
743
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
135,887,766✔
744
    return terrno;
135,796,019✔
UNCOV
745
  }
×
746
  return TSDB_CODE_SUCCESS;
747
}
135,842,116✔
748

749
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
750
  int32_t code = 0;
40,996,729✔
751
  int32_t lino;
40,996,729✔
752

753
  void     *pData = NULL;
754
  int       nData = 0;
40,996,729✔
755
  SSkmDbKey skmDbKey;
41,006,402✔
756
  if (sver <= 0) {
40,995,268✔
757
    SMetaInfo info;
41,007,423✔
758
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
21,474,038✔
759
      sver = info.skmVer;
21,487,242✔
760
    } else {
21,469,799✔
761
      TBC *pSkmDbC = NULL;
762
      int  c;
16,459✔
763

6,510✔
764
      skmDbKey.uid = suid ? suid : uid;
765
      skmDbKey.sver = INT32_MAX;
6,510✔
766

6,510✔
767
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
768
      TSDB_CHECK_CODE(code, lino, _exit);
6,510✔
769
      metaRLock(pMeta);
6,510✔
770

6,510✔
771
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
772
        metaULock(pMeta);
6,510✔
773
        tdbTbcClose(pSkmDbC);
×
774
        code = TSDB_CODE_NOT_FOUND;
×
775
        goto _exit;
×
UNCOV
776
      }
×
777

778
      if (c == 0) {
779
        metaULock(pMeta);
6,510✔
780
        tdbTbcClose(pSkmDbC);
×
781
        code = TSDB_CODE_FAILED;
×
782
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
783
        goto _exit;
×
UNCOV
784
      }
×
785

786
      if (c < 0) {
787
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
6,510✔
UNCOV
788
      }
×
789

790
      const void *pKey = NULL;
791
      int32_t     nKey = 0;
6,510✔
792
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
6,510✔
793

6,510✔
794
      if (ret != 0 || ((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
795
        metaULock(pMeta);
6,510✔
796
        tdbTbcClose(pSkmDbC);
×
797
        code = TSDB_CODE_NOT_FOUND;
×
798
        goto _exit;
×
UNCOV
799
      }
×
800

801
      sver = ((SSkmDbKey *)pKey)->sver;
802

6,510✔
803
      metaULock(pMeta);
804
      tdbTbcClose(pSkmDbC);
6,510✔
805
    }
6,510✔
806
  }
807

808
  if (!(sver > 0)) {
809
    code = TSDB_CODE_NOT_FOUND;
41,009,199✔
810
    goto _exit;
×
UNCOV
811
  }
×
812

813
  skmDbKey.uid = suid ? suid : uid;
814
  skmDbKey.sver = sver;
41,009,199✔
815
  metaRLock(pMeta);
41,009,199✔
816
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
41,009,199✔
817
    metaULock(pMeta);
41,018,037✔
818
    code = TSDB_CODE_NOT_FOUND;
2,576✔
819
    goto _exit;
2,576✔
820
  }
2,576✔
821
  metaULock(pMeta);
822

41,010,018✔
823
  // decode
824
  SDecoder        dc = {0};
825
  SSchemaWrapper  schema;
41,017,292✔
826
  SSchemaWrapper *pSchemaWrapper = &schema;
40,984,385✔
827

41,014,233✔
828
  tDecoderInit(&dc, pData, nData);
829
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
41,014,233✔
830
  tDecoderClear(&dc);
41,027,393✔
831
  tdbFree(pData);
41,027,393✔
832
  if (TSDB_CODE_SUCCESS != code) {
41,018,832✔
833
    taosMemoryFree(pSchemaWrapper->pSchema);
40,988,747✔
834
    goto _exit;
×
UNCOV
835
  }
×
836

837
  // convert
838
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
839
  if (pTSchema == NULL) {
40,988,747✔
840
    code = TSDB_CODE_OUT_OF_MEMORY;
41,005,769✔
UNCOV
841
  }
×
842

843
  *ppTSchema = pTSchema;
844
  taosMemoryFree(pSchemaWrapper->pSchema);
41,005,769✔
845

41,014,074✔
846
_exit:
847
  return code;
40,996,026✔
848
}
40,999,536✔
849

850
// N.B. Called by statusReq per second
851
int64_t metaGetTbNum(SMeta *pMeta) {
852
  // num of child tables (excluding normal tables , stables and others)
115,369,825✔
853

854
  /* int64_t num = 0; */
855
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
856

857
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
858
}
115,369,825✔
859

860
void metaUpdTimeSeriesNum(SMeta *pMeta) {
861
  int64_t nCtbTimeSeries = 0;
56,567,582✔
862
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
56,567,582✔
863
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
56,569,502✔
864
  }
56,567,492✔
865
}
866

56,570,782✔
867
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
868
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
869
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
870
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
183,192,813✔
871
    metaUpdTimeSeriesNum(pMeta);
183,194,092✔
872
  }
56,563,974✔
873

874
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
875
}
183,200,924✔
876

877
// type: 1 reported timeseries
878
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
879
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
183,184,248✔
880
  if (type == 1) {
183,193,831✔
881
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
183,193,831✔
882
  }
115,369,825✔
883
  return nTimeSeries;
884
}
183,184,240✔
885

886
typedef struct {
887
  SMeta   *pMeta;
888
  TBC     *pCur;
889
  tb_uid_t uid;
890
  void    *pKey;
891
  void    *pVal;
892
  int      kLen;
893
  int      vLen;
894
} SMSmaCursor;
895

896
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
897
  SMSmaCursor *pSmaCur = NULL;
×
898
  SSmaIdxKey   smaIdxKey;
×
UNCOV
899
  int          ret;
×
900
  int          c;
UNCOV
901

×
902
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
903
  if (pSmaCur == NULL) {
×
904
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
905
    return NULL;
×
UNCOV
906
  }
×
907

908
  pSmaCur->pMeta = pMeta;
909
  pSmaCur->uid = uid;
×
910
  metaRLock(pMeta);
×
UNCOV
911

×
912
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
913
  if (ret < 0) {
×
914
    metaULock(pMeta);
×
915
    taosMemoryFree(pSmaCur);
×
916
    return NULL;
×
UNCOV
917
  }
×
918

919
  // move to the suid
920
  smaIdxKey.uid = uid;
921
  smaIdxKey.smaUid = INT64_MIN;
×
922
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
923
  if (c > 0) {
×
924
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
UNCOV
925
  }
×
926

927
  return pSmaCur;
UNCOV
928
}
×
929

930
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
931
  if (pSmaCur) {
×
932
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
933
    if (pSmaCur->pCur) {
×
934
      tdbTbcClose(pSmaCur->pCur);
×
935
      pSmaCur->pCur = NULL;
×
UNCOV
936

×
937
      tdbFree(pSmaCur->pKey);
938
      tdbFree(pSmaCur->pVal);
×
UNCOV
939
    }
×
940

941
    taosMemoryFree(pSmaCur);
UNCOV
942
  }
×
943
}
UNCOV
944

×
945
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
UNCOV
946
  int         ret;
×
947
  SSmaIdxKey *pSmaIdxKey;
948

949
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
950
  if (ret < 0) {
×
951
    return 0;
×
UNCOV
952
  }
×
953

954
  pSmaIdxKey = pSmaCur->pKey;
955
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
956
    return 0;
×
UNCOV
957
  }
×
958

959
  return pSmaIdxKey->uid;
UNCOV
960
}
×
961

962
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
963
  STSmaWrapper *pSW = NULL;
×
964
  SArray       *pSmaIds = NULL;
×
UNCOV
965

×
966
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
967
    return NULL;
×
UNCOV
968
  }
×
969

970
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
971
  if (!pSW) {
×
972
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
973
    goto _err;
×
UNCOV
974
  }
×
975

976
  pSW->number = taosArrayGetSize(pSmaIds);
977
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
UNCOV
978

×
979
  if (!pSW->tSma) {
980
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
981
    goto _err;
×
UNCOV
982
  }
×
983

984
  SMetaReader mr = {0};
985
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
UNCOV
986
  int64_t smaId;
×
987
  int     smaIdx = 0;
988
  STSma  *pTSma = NULL;
×
989
  for (int i = 0; i < pSW->number; ++i) {
×
990
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
991
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
992
      tDecoderClear(&mr.coder);
×
993
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
994
      continue;
×
UNCOV
995
    }
×
996
    tDecoderClear(&mr.coder);
997
    pTSma = pSW->tSma + smaIdx;
×
998
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
999
    if (deepCopy) {
×
1000
      if (pTSma->exprLen > 0) {
×
1001
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
1002
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1003
          goto _err;
×
UNCOV
1004
        }
×
1005
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
UNCOV
1006
      }
×
1007
      if (pTSma->tagsFilterLen > 0) {
1008
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
1009
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1010
          goto _err;
×
UNCOV
1011
        }
×
1012
      }
1013
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
UNCOV
1014
    } else {
×
1015
      pTSma->exprLen = 0;
1016
      pTSma->expr = NULL;
×
1017
      pTSma->tagsFilterLen = 0;
×
1018
      pTSma->tagsFilter = NULL;
×
UNCOV
1019
    }
×
1020

1021
    ++smaIdx;
UNCOV
1022
  }
×
1023

1024
  if (smaIdx <= 0) goto _err;
1025
  pSW->number = smaIdx;
×
UNCOV
1026

×
1027
  metaReaderClear(&mr);
1028
  taosArrayDestroy(pSmaIds);
×
1029
  return pSW;
×
1030
_err:
×
1031
  metaReaderClear(&mr);
×
1032
  taosArrayDestroy(pSmaIds);
×
1033
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
1034
  return NULL;
×
UNCOV
1035
}
×
1036

1037
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
1038
  STSma      *pTSma = NULL;
×
1039
  SMetaReader mr = {0};
×
1040
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
1041
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
×
1042
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
1043
    metaReaderClear(&mr);
×
1044
    return NULL;
×
UNCOV
1045
  }
×
1046
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
1047
  if (!pTSma) {
×
1048
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1049
    metaReaderClear(&mr);
×
1050
    return NULL;
×
UNCOV
1051
  }
×
1052

1053
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
UNCOV
1054

×
1055
  metaReaderClear(&mr);
1056
  return pTSma;
×
UNCOV
1057
}
×
1058

1059
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
1060
  SArray     *pUids = NULL;
×
1061
  SSmaIdxKey *pSmaIdxKey = NULL;
×
UNCOV
1062

×
1063
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
1064
  if (!pCur) {
×
1065
    return NULL;
×
UNCOV
1066
  }
×
1067

1068
  while (1) {
1069
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1070
    if (id == 0) {
×
1071
      break;
×
UNCOV
1072
    }
×
1073

1074
    if (!pUids) {
1075
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1076
      if (!pUids) {
×
1077
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1078
        metaCloseSmaCursor(pCur);
×
1079
        return NULL;
×
UNCOV
1080
      }
×
1081
    }
1082

1083
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
UNCOV
1084

×
1085
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
1086
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1087
      metaCloseSmaCursor(pCur);
×
1088
      taosArrayDestroy(pUids);
×
1089
      return NULL;
×
UNCOV
1090
    }
×
1091
  }
1092

1093
  metaCloseSmaCursor(pCur);
1094
  return pUids;
×
UNCOV
1095
}
×
1096

1097
SArray *metaGetSmaTbUids(SMeta *pMeta) {
1098
  SArray     *pUids = NULL;
×
1099
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1100
  tb_uid_t    lastUid = 0;
×
UNCOV
1101

×
1102
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
1103
  if (!pCur) {
×
1104
    return NULL;
×
UNCOV
1105
  }
×
1106

1107
  while (1) {
1108
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1109
    if (uid == 0) {
×
1110
      break;
×
UNCOV
1111
    }
×
1112

1113
    if (lastUid == uid) {
1114
      continue;
×
UNCOV
1115
    }
×
1116

1117
    lastUid = uid;
UNCOV
1118

×
1119
    if (!pUids) {
1120
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1121
      if (!pUids) {
×
1122
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1123
        metaCloseSmaCursor(pCur);
×
1124
        return NULL;
×
UNCOV
1125
      }
×
1126
    }
1127

1128
    if (!taosArrayPush(pUids, &uid)) {
1129
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1130
      metaCloseSmaCursor(pCur);
×
1131
      taosArrayDestroy(pUids);
×
1132
      return NULL;
×
UNCOV
1133
    }
×
1134
  }
1135

1136
  metaCloseSmaCursor(pCur);
1137
  return pUids;
×
UNCOV
1138
}
×
1139

1140
#endif
1141

1142
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
1143
  STag *tag = (STag *)pTag;
630,031,107✔
1144
  if (type == TSDB_DATA_TYPE_JSON) {
630,031,107✔
1145
    return tag;
630,031,107✔
1146
  }
1,513,539✔
1147
  bool find = tTagGet(tag, val);
1148

628,517,568✔
1149
  if (!find) {
1150
    return NULL;
628,562,009✔
1151
  }
5,467,071✔
1152

1153
  return val;
1154
}
623,094,938✔
1155

1156
typedef struct {
1157
  SMeta   *pMeta;
1158
  TBC     *pCur;
1159
  tb_uid_t suid;
1160
  int16_t  cid;
1161
  int16_t  type;
1162
  void    *pKey;
1163
  void    *pVal;
1164
  int32_t  kLen;
1165
  int32_t  vLen;
1166
} SIdxCursor;
1167

1168
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
1169
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,998✔
1170
  SMetaFltParam *param = arg;
1,998✔
1171
  int32_t        ret = 0;
1,998✔
1172

1,998✔
1173
  SIdxCursor *pCursor = NULL;
1174
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
1,998✔
1175
  if (pCursor == NULL) {
1,998✔
1176
    return terrno;
1,998✔
UNCOV
1177
  }
×
1178
  pCursor->pMeta = pMeta;
1179
  pCursor->suid = param->suid;
1,998✔
1180
  pCursor->cid = param->cid;
1,998✔
1181
  pCursor->type = param->type;
1,998✔
1182

1,998✔
1183
  metaRLock(pMeta);
1184
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
1,998✔
1185
  if (ret != 0) {
1,998✔
1186
    goto END;
1,998✔
UNCOV
1187
  }
×
1188
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
1189

1,998✔
1190
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
1191
  SBtimeIdxKey *pBtimeKey = &btimeKey;
1,998✔
1192

1,998✔
1193
  int cmp = 0;
1194
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
1,998✔
1195
    goto END;
1,998✔
UNCOV
1196
  }
×
1197

1198
  int32_t valid = 0;
1199
  int32_t count = 0;
1,998✔
1200

1,998✔
1201
  static const int8_t TRY_ERROR_LIMIT = 1;
1202
  do {
1203
    void   *entryKey = NULL;
13,249,737✔
1204
    int32_t nEntryKey = -1;
13,251,735✔
1205
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
13,251,402✔
1206
    if (valid < 0) break;
13,251,069✔
1207

13,250,736✔
1208
    SBtimeIdxKey *p = entryKey;
1209
    if (count > TRY_ERROR_LIMIT) break;
13,248,738✔
1210

13,248,738✔
1211
    terrno = TSDB_CODE_SUCCESS;
1212
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
13,248,738✔
1213
    if (terrno != TSDB_CODE_SUCCESS) {
13,245,408✔
1214
      ret = terrno;
12,908,412✔
1215
      break;
×
UNCOV
1216
    }
×
1217
    if (cmp == 0) {
1218
      if (taosArrayPush(pUids, &p->uid) == NULL) {
13,221,765✔
1219
        ret = terrno;
26,477,496✔
1220
        break;
×
UNCOV
1221
      }
×
1222
    } else {
1223
      if (param->equal == true) {
1224
        if (count > TRY_ERROR_LIMIT) break;
×
1225
        count++;
×
UNCOV
1226
      }
×
1227
    }
1228
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
1229
    if (valid < 0) break;
13,255,731✔
1230
  } while (1);
13,250,070✔
1231

1232
END:
1233
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
1,998✔
1234
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
1,998✔
1235
  taosMemoryFree(pCursor);
1,998✔
1236
  return ret;
1,998✔
1237
}
1,998✔
1238

1239
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
1240
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1241
  SMetaFltParam *param = arg;
×
1242
  int32_t        ret = 0;
×
1243
  char          *buf = NULL;
×
UNCOV
1244

×
1245
  STagIdxKey *pKey = NULL;
1246
  int32_t     nKey = 0;
×
UNCOV
1247

×
1248
  SIdxCursor *pCursor = NULL;
1249
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1250
  if (pCursor == NULL) {
×
1251
    return terrno;
×
UNCOV
1252
  }
×
1253
  pCursor->pMeta = pMeta;
1254
  pCursor->suid = param->suid;
×
1255
  pCursor->cid = param->cid;
×
1256
  pCursor->type = param->type;
×
UNCOV
1257

×
1258
  char *pName = param->val;
UNCOV
1259

×
1260
  metaRLock(pMeta);
1261
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1262
  if (ret != 0) {
×
1263
    goto END;
×
UNCOV
1264
  }
×
1265

1266
  int cmp = 0;
1267
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1268
    goto END;
×
UNCOV
1269
  }
×
1270
  int32_t valid = 0;
1271
  int32_t count = 0;
×
UNCOV
1272

×
1273
  int32_t TRY_ERROR_LIMIT = 1;
1274
  do {
×
1275
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1276
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1277
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1278
    if (valid < 0) break;
×
UNCOV
1279

×
1280
    if (count > TRY_ERROR_LIMIT) break;
UNCOV
1281

×
1282
    char *pTableKey = (char *)pEntryKey;
1283
    terrno = TSDB_CODE_SUCCESS;
×
1284
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1285
    if (terrno != TSDB_CODE_SUCCESS) {
×
1286
      ret = terrno;
×
1287
      goto END;
×
UNCOV
1288
    }
×
1289
    if (cmp == 0) {
1290
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1291
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1292
        ret = terrno;
×
1293
        goto END;
×
UNCOV
1294
      }
×
1295
    } else {
1296
      if (param->equal == true) {
1297
        if (count > TRY_ERROR_LIMIT) break;
×
1298
        count++;
×
UNCOV
1299
      }
×
1300
    }
1301
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
1302
    if (valid < 0) {
×
1303
      break;
×
UNCOV
1304
    }
×
1305
  } while (1);
1306

1307
END:
1308
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1309
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1310
  taosMemoryFree(buf);
×
1311
  taosMemoryFree(pKey);
×
UNCOV
1312

×
1313
  taosMemoryFree(pCursor);
UNCOV
1314

×
1315
  return ret;
UNCOV
1316
}
×
1317
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
1318
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1319
  SMetaFltParam *param = arg;
×
1320
  int32_t        ret = 0;
×
1321
  char          *buf = NULL;
×
UNCOV
1322

×
1323
  STtlIdxKey *pKey = NULL;
1324
  int32_t     nKey = 0;
×
UNCOV
1325

×
1326
  SIdxCursor *pCursor = NULL;
1327
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1328
  if (pCursor == NULL) {
×
1329
    return terrno;
×
UNCOV
1330
  }
×
1331
  pCursor->pMeta = pMeta;
1332
  pCursor->suid = param->suid;
×
1333
  pCursor->cid = param->cid;
×
1334
  pCursor->type = param->type;
×
UNCOV
1335

×
1336
  metaRLock(pMeta);
UNCOV
1337
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
×
1338

1339
END:
1340
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1341
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1342
  taosMemoryFree(buf);
×
1343
  taosMemoryFree(pKey);
×
UNCOV
1344

×
1345
  taosMemoryFree(pCursor);
UNCOV
1346

×
1347
  return ret;
UNCOV
1348
  // impl later
×
1349
  return 0;
1350
}
1351
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
1352
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,437,278✔
1353
  SMetaFltParam *param = arg;
3,437,278✔
1354

3,434,773✔
1355
  SMetaEntry oStbEntry = {0};
1356
  int32_t    code = 0;
3,434,773✔
1357
  char      *buf = NULL;
3,434,374✔
1358
  void      *pData = NULL;
3,434,374✔
1359
  int        nData = 0;
3,434,374✔
1360

3,437,866✔
1361
  SDecoder    dc = {0};
1362
  STbDbKey    tbDbKey = {0};
3,440,286✔
1363
  STagIdxKey *pKey = NULL;
3,434,477✔
1364
  int32_t     nKey = 0;
3,432,172✔
1365

3,432,473✔
1366
  SIdxCursor *pCursor = NULL;
1367
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,430,426✔
1368
  if (!pCursor) {
3,430,426✔
1369
    return terrno;
3,426,150✔
UNCOV
1370
  }
×
1371
  pCursor->pMeta = pMeta;
1372
  pCursor->suid = param->suid;
3,426,150✔
1373
  pCursor->cid = param->cid;
3,431,076✔
1374
  pCursor->type = param->type;
3,438,142✔
1375

3,430,974✔
1376
  metaRLock(pMeta);
1377

3,438,704✔
1378
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
1379

3,436,244✔
1380
  tbDbKey.uid = param->suid;
1381
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,442,664✔
1382

3,439,053✔
1383
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
1384

3,438,562✔
1385
  tDecoderInit(&dc, pData, nData);
1386

3,443,545✔
1387
  code = metaDecodeEntry(&dc, &oStbEntry);
1388
  if (code) {
3,443,347✔
1389
    tDecoderClear(&dc);
3,440,245✔
1390
    goto END;
×
UNCOV
1391
  }
×
1392

1393
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
1394
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
3,440,245✔
UNCOV
1395
  }
×
1396

1397
  code = TSDB_CODE_INVALID_PARA;
1398

3,440,245✔
1399
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
1400
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,643,262✔
1401
    if (IS_IDX_ON(schema)) {
4,643,318✔
1402
      if (schema->colId == param->cid && param->type == schema->type) {
4,642,390✔
1403
        code = 0;
4,636,032✔
1404
        break;
3,438,108✔
1405
      }
3,438,108✔
1406
    }
1407
  }
1408

1409
  TAOS_CHECK_GOTO(code, NULL, END);
1410

3,438,052✔
1411
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
1412
  if (code != 0) {
3,438,052✔
1413
    TAOS_CHECK_GOTO(terrno, NULL, END);
3,440,561✔
UNCOV
1414
  }
×
1415

1416
  int32_t maxSize = 0;
1417
  int32_t nTagData = 0;
3,440,561✔
1418
  void   *tagData = NULL;
3,440,513✔
1419

3,440,513✔
1420
  if (param->val == NULL) {
1421
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
3,440,513✔
1422
    goto END;
×
UNCOV
1423
  } else {
×
1424
    if (IS_VAR_DATA_TYPE(param->type)) {
1425
      tagData = varDataVal(param->val);
3,438,783✔
1426
      nTagData = varDataLen(param->val);
443,414✔
1427

440,479✔
1428
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
1429
        maxSize = 4 * nTagData + 1;
440,702✔
1430
        buf = taosMemoryCalloc(1, maxSize);
36,416✔
1431
        if (buf == NULL) {
36,416✔
1432
          TAOS_CHECK_GOTO(terrno, NULL, END);
36,416✔
UNCOV
1433
        }
×
1434

1435
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
1436
          TAOS_CHECK_GOTO(terrno, NULL, END);
36,416✔
UNCOV
1437
        }
×
1438

1439
        tagData = buf;
1440
        nTagData = maxSize;
36,416✔
1441
      }
36,416✔
1442
    } else {
1443
      tagData = param->val;
1444
      nTagData = tDataTypes[param->type].bytes;
2,997,596✔
1445
    }
3,001,727✔
1446
  }
1447

1448
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
1449
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
3,438,062✔
1450
                  NULL, END);
1451

1452
  int cmp = 0;
1453
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,433,182✔
1454

3,435,358✔
1455
  int     count = 0;
1456
  int32_t valid = 0;
3,443,347✔
1457
  bool    found = false;
3,443,347✔
1458

3,443,347✔
1459
  static const int8_t TRY_ERROR_LIMIT = 1;
1460

1461
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1462
  /// target:                        [suid, cid2, type2]
1463
  int diffCidCount = 0;
1464
  do {
3,443,347✔
1465
    void   *entryKey = NULL, *entryVal = NULL;
34,882,518✔
1466
    int32_t nEntryKey, nEntryVal;
38,325,865✔
1467

38,320,446✔
1468
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
1469
    if (valid < 0) {
38,325,300✔
1470
      break;
38,443,378✔
1471
    }
1,774,671✔
1472
    if (count > TRY_ERROR_LIMIT) {
1473
      break;
36,668,707✔
1474
    }
968,747✔
1475

1476
    STagIdxKey *p = entryKey;
1477
    if (p == NULL) break;
35,699,960✔
1478

35,699,960✔
1479
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
1480
      if (found == true) break;  //
35,699,960✔
1481
      if (diffCidCount > TRY_ERROR_LIMIT) break;
1,209,605✔
1482
      diffCidCount++;
509,038✔
1483
      count++;
509,038✔
1484
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
509,038✔
1485
      if (valid < 0) {
509,038✔
1486
        code = valid;
507,940✔
1487
        break;
×
UNCOV
1488
      } else {
×
1489
        continue;
1490
      }
507,940✔
1491
    }
1492

1493
    terrno = TSDB_CODE_SUCCESS;
1494
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
34,485,476✔
1495
    if (terrno != TSDB_CODE_SUCCESS) {
34,487,458✔
1496
      TAOS_CHECK_GOTO(terrno, NULL, END);
34,402,119✔
1497
      break;
×
UNCOV
1498
    }
×
1499
    if (cmp == 0) {
1500
      // match
34,401,988✔
1501
      tb_uid_t tuid = 0;
1502
      if (IS_VAR_DATA_TYPE(pKey->type)) {
30,912,425✔
1503
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
30,911,985✔
1504
      } else {
242,897✔
1505
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
1506
      }
30,671,816✔
1507
      if (taosArrayPush(pUids, &tuid) == NULL) {
1508
        TAOS_CHECK_GOTO(terrno, NULL, END);
30,863,321✔
UNCOV
1509
      }
×
1510
      found = true;
1511
    } else {
30,863,321✔
1512
      if (param->equal == true) {
1513
        if (count > TRY_ERROR_LIMIT) break;
3,489,563✔
1514
        count++;
2,408,298✔
1515
      }
2,408,298✔
1516
    }
1517
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
1518
    if (valid < 0) {
34,353,038✔
1519
      code = valid;
34,371,714✔
1520
      break;
×
UNCOV
1521
    }
×
1522
  } while (1);
1523

1524
END:
1525
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,440,345✔
1526
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,443,619✔
1527
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,443,911✔
1528
  tDecoderClear(&dc);
3,440,043✔
1529
  tdbFree(pData);
3,440,043✔
1530

3,442,174✔
1531
  taosMemoryFree(buf);
1532
  taosMemoryFree(pKey);
3,438,759✔
1533

3,441,978✔
1534
  taosMemoryFree(pCursor);
1535

3,439,481✔
1536
  return code;
1537
}
3,437,936✔
1538

1539
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
1540
  int ret = 0;
38,911,811✔
1541
  if (lock) {
38,911,811✔
1542
    metaRLock(pMeta);
38,911,811✔
UNCOV
1543
  }
×
1544

1545
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
1546
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
38,911,811✔
1547
  if (lock) {
38,911,913✔
1548
    metaULock(pMeta);
38,901,725✔
UNCOV
1549
  }
×
1550

1551
  return ret;
1552
}
38,901,725✔
1553

1554
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
1555
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
5,053,226✔
1556
  const int32_t LIMIT = 128;
5,053,226✔
1557

5,053,226✔
1558
  int32_t isLock = false;
1559
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
5,053,226✔
1560
  for (int i = 0; i < sz; i++) {
5,053,226✔
1561
    STUidTagInfo *p = taosArrayGet(uidList, i);
43,957,180✔
1562

38,902,956✔
1563
    if (i % LIMIT == 0) {
1564
      if (isLock) metaULock(pMeta);
38,904,898✔
1565

4,408,797✔
1566
      metaRLock(pMeta);
1567
      isLock = true;
4,408,797✔
1568
    }
4,408,915✔
1569

1570
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1571
    void   *val = NULL;
1572
    int32_t len = 0;
38,905,016✔
1573
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
38,905,536✔
1574
      p->pTagVal = taosMemoryMalloc(len);
38,905,976✔
1575
      if (!p->pTagVal) {
38,906,107✔
1576
        if (isLock) metaULock(pMeta);
38,906,029✔
UNCOV
1577

×
1578
        TAOS_RETURN(terrno);
UNCOV
1579
      }
×
1580
      memcpy(p->pTagVal, val, len);
1581
      tdbFree(val);
38,905,691✔
1582
    } else {
38,905,353✔
1583
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
1584
    }
2,364✔
1585
  }
1586
  //  }
1587
  if (isLock) metaULock(pMeta);
1588
  return 0;
5,054,224✔
1589
}
5,053,226✔
1590

1591
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
1592
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
10,521,386✔
1593
  if (!pCur) {
10,521,386✔
1594
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
10,520,536✔
UNCOV
1595
  }
×
1596

1597
  while (1) {
1598
    tb_uid_t uid = metaCtbCursorNext(pCur);
139,210,344✔
1599
    if (uid == 0) {
149,730,880✔
1600
      metaInfo("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
149,727,882✔
1601
      break;
10,525,360✔
1602
    }
1603

1604
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
139,202,522✔
1605
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
139,201,051✔
1606
    if (!info.pTagVal) {
139,194,717✔
NEW
1607
      metaCloseCtbCursor(pCur);
×
NEW
1608
      return terrno;
×
1609
    }
1610
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
139,194,717✔
1611
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
139,062,669✔
NEW
1612
      taosMemoryFreeClear(info.pTagVal);
×
NEW
1613
      metaCloseCtbCursor(pCur);
×
NEW
1614
      return terrno;
×
1615
    }
1616
  }
1617
  metaCloseCtbCursor(pCur);
10,524,761✔
1618
  return TSDB_CODE_SUCCESS;
10,523,028✔
1619
}
1620

1621
int32_t metaFlagCache(SVnode *pVnode) {
×
1622
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
UNCOV
1623
  if (!pCur) {
×
1624
    return terrno;
×
1625
  }
1626

1627
  SArray *suids = NULL;
×
UNCOV
1628
  while (1) {
×
UNCOV
1629
    tb_uid_t id = metaStbCursorNext(pCur);
×
1630
    if (id == 0) {
×
1631
      break;
×
1632
    }
1633

UNCOV
1634
    if (!suids) {
×
1635
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1636
      if (!suids) {
×
1637
        return terrno;
×
1638
      }
1639
    }
1640

1641
    if (taosArrayPush(suids, &id) == NULL) {
×
UNCOV
1642
      taosArrayDestroy(suids);
×
1643
      return terrno;
×
1644
    }
1645
  }
1646

1647
  metaCloseStbCursor(pCur);
×
1648

UNCOV
1649
  for (int idx = 0; suids && idx < TARRAY_SIZE(suids); ++idx) {
×
1650
    tb_uid_t id = ((tb_uid_t *)TARRAY_DATA(suids))[idx];
×
UNCOV
1651
    STsdb   *pTsdb = pVnode->pTsdb;
×
UNCOV
1652
    SMeta   *pMeta = pVnode->pMeta;
×
1653
    SArray  *uids = NULL;
×
1654

1655
    int32_t code = metaGetChildUidsOfSuperTable(pMeta, id, &uids);
×
UNCOV
1656
    if (code) {
×
UNCOV
1657
      metaError("vgId:%d, failed to get subtables, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1658

UNCOV
1659
      taosArrayDestroy(uids);
×
UNCOV
1660
      taosArrayDestroy(suids);
×
1661

UNCOV
1662
      return code;
×
1663
    }
1664

UNCOV
1665
    if (uids && TARRAY_SIZE(uids) > 0) {
×
UNCOV
1666
      STSchema *pTSchema = NULL;
×
1667

UNCOV
1668
      code = metaGetTbTSchemaEx(pMeta, id, id, -1, &pTSchema);
×
UNCOV
1669
      if (code) {
×
UNCOV
1670
        metaError("vgId:%d, failed to get schema, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1671

UNCOV
1672
        taosArrayDestroy(uids);
×
UNCOV
1673
        taosArrayDestroy(suids);
×
1674

UNCOV
1675
        return code;
×
1676
      }
1677

UNCOV
1678
      int32_t nCol = pTSchema->numOfCols;
×
UNCOV
1679
      for (int32_t i = 0; i < nCol; ++i) {
×
UNCOV
1680
        int16_t cid = pTSchema->columns[i].colId;
×
UNCOV
1681
        int8_t  col_type = pTSchema->columns[i].type;
×
1682

UNCOV
1683
        code = tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type);
×
UNCOV
1684
        if (code) {
×
UNCOV
1685
          metaError("vgId:%d, failed to flag cache, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1686

UNCOV
1687
          tDestroyTSchema(pTSchema);
×
UNCOV
1688
          taosArrayDestroy(uids);
×
UNCOV
1689
          taosArrayDestroy(suids);
×
1690

UNCOV
1691
          return code;
×
1692
        }
1693
      }
1694

UNCOV
1695
      tDestroyTSchema(pTSchema);
×
1696
    }
1697

UNCOV
1698
    taosArrayDestroy(uids);
×
1699
  }
1700

UNCOV
1701
  taosArrayDestroy(suids);
×
1702

UNCOV
1703
  return TSDB_CODE_SUCCESS;
×
1704
}
1705

1706
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1707

1708
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
1,546,358,527✔
1709
  int32_t code = 0;
1,546,358,527✔
1710
  void   *pData = NULL;
1,546,358,527✔
1711
  int     nData = 0;
1,546,446,837✔
1712
  int     lock = 0;
1,546,496,261✔
1713

1714
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
1,546,496,261✔
1715
    lock = 1;
335,456,655✔
1716
  }
1717

1718
  if (!lock) metaRLock(pMeta);
1,546,497,131✔
1719

1720
  // search cache
1721
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
1,546,503,886✔
1722
    if (!lock) metaULock(pMeta);
1,462,310,845✔
1723
    goto _exit;
1,462,270,412✔
1724
  }
1725

1726
  // search TDB
1727
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
84,208,613✔
1728
    // not found
1729
    if (!lock) metaULock(pMeta);
75,620,721✔
1730
    goto _exit;
75,613,641✔
1731
  }
1732

1733
  if (!lock) metaULock(pMeta);
8,599,743✔
1734

1735
  pInfo->uid = uid;
8,599,743✔
1736
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
8,599,743✔
1737
  pInfo->version = ((SUidIdxVal *)pData)->version;
8,599,743✔
1738
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
8,599,743✔
1739

1740
  if (lock) {
8,599,743✔
1741
    metaULock(pReader->pMeta);
714,421✔
1742
    // metaReaderReleaseLock(pReader);
1743
  }
1744
  // upsert the cache
1745
  metaWLock(pMeta);
8,599,743✔
1746
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
8,599,743✔
1747
  if (ret != 0) {
8,599,743✔
UNCOV
1748
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1749
  }
1750
  metaULock(pMeta);
8,599,743✔
1751

1752
  if (lock) {
8,599,683✔
1753
    metaRLock(pReader->pMeta);
714,361✔
1754
  }
1755

1756
_exit:
1,546,319,396✔
1757
  tdbFree(pData);
1,546,505,763✔
1758
  return code;
1,546,477,763✔
1759
}
1760

1761
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
170,160,981✔
1762
  int32_t code = 0;
170,160,981✔
1763

1764
  if (!numOfTables && !numOfCols) goto _exit;
170,160,981✔
1765

1766
  SVnode *pVnodeObj = pVnode;
170,160,981✔
1767
  metaRLock(pVnodeObj->pMeta);
170,160,981✔
1768

1769
  // fast path: search cache
1770
  SMetaStbStats state = {0};
170,165,805✔
1771
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
170,176,682✔
1772
    metaULock(pVnodeObj->pMeta);
163,437,568✔
1773
    if (numOfTables) *numOfTables = state.ctbNum;
163,423,286✔
1774
    if (numOfCols) *numOfCols = state.colNum;
163,422,006✔
1775
    if (flags) *flags = state.flags;
163,416,811✔
1776
    goto _exit;
163,416,811✔
1777
  }
1778

1779
  // slow path: search TDB
1780
  int64_t ctbNum = 0;
6,748,609✔
1781
  int32_t colNum = 0;
6,748,639✔
1782
  int64_t keep = 0;
6,748,706✔
1783
  int8_t  flag = 0;
6,748,570✔
1784

1785
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
6,748,706✔
1786
  if (TSDB_CODE_SUCCESS == code) {
6,748,797✔
1787
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
6,748,797✔
1788
  }
1789
  if (TSDB_CODE_SUCCESS == code) {
6,748,133✔
1790
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
6,748,133✔
1791
  }
1792
  metaULock(pVnodeObj->pMeta);
6,748,730✔
1793
  if (TSDB_CODE_SUCCESS != code) {
6,748,797✔
UNCOV
1794
    goto _exit;
×
1795
  }
1796

1797
  if (numOfTables) *numOfTables = ctbNum;
6,748,797✔
1798
  if (numOfCols) *numOfCols = colNum;
6,748,797✔
1799
  if (flags) *flags = flag;
6,748,797✔
1800

1801
  state.uid = uid;
6,748,797✔
1802
  state.ctbNum = ctbNum;
6,748,797✔
1803
  state.colNum = colNum;
6,748,797✔
1804
  state.flags = flag;
6,748,797✔
1805
  state.keep = keep;
6,748,797✔
1806
  // upsert the cache
1807
  metaWLock(pVnodeObj->pMeta);
6,748,797✔
1808

1809
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
6,748,730✔
1810
  if (ret) {
6,748,730✔
UNCOV
1811
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1812
              uid, ctbNum, colNum, keep, flag);
1813
  }
1814

1815
  metaULock(pVnodeObj->pMeta);
6,748,730✔
1816

1817
_exit:
170,165,467✔
1818
  return code;
170,177,073✔
1819
}
1820

1821
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
67,246,166✔
1822
  SMetaStbStats stats = {0};
67,246,166✔
1823

1824
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
67,255,351✔
1825
    stats.ctbNum += deltaCtb;
56,845,106✔
1826
    stats.colNum += deltaCol;
56,845,106✔
1827
    if (deltaKeep > 0) {
56,845,106✔
1828
      stats.keep = deltaKeep;
6,967✔
1829
    }
1830

1831
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
56,845,106✔
1832
    if (code) {
56,828,947✔
UNCOV
1833
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1834
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1835
    }
1836
  }
1837
}
67,249,399✔
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