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

taosdata / TDengine / #4849

13 Nov 2025 07:17AM UTC coverage: 63.829% (+0.6%) from 63.27%
#4849

push

travis-ci

guanshengliang
test: enable auto upload

148928 of 233322 relevant lines covered (63.83%)

115593255.19 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 "osMemory.h"
18
#include "tencode.h"
19

20
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
514,014,428✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
514,014,428✔
22
  metaReaderDoInit(pReader, pMeta, flags);
514,047,101✔
23
  pReader->pAPI = pAPI;
514,025,885✔
24
}
514,031,913✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
598,059,331✔
27
  memset(pReader, 0, sizeof(*pReader));
598,059,331✔
28
  pReader->pMeta = pMeta;
598,059,331✔
29
  pReader->flags = flags;
598,015,116✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
597,974,030✔
31
    metaRLock(pMeta);
422,670,506✔
32
  }
33
}
598,008,938✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
224,287,772✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
224,287,772✔
37
    metaULock(pReader->pMeta);
224,401,699✔
38
    pReader->flags |= META_READER_NOLOCK;
224,218,088✔
39
  }
40
}
224,319,188✔
41

42
void metaReaderClear(SMetaReader *pReader) {
628,637,361✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
628,637,361✔
44
    metaULock(pReader->pMeta);
198,211,555✔
45
  }
46
  tDecoderClear(&pReader->coder);
628,536,051✔
47
  tdbFree(pReader->pBuf);
628,738,872✔
48
  pReader->pBuf = NULL;
628,615,570✔
49
}
628,620,239✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
948,220,572✔
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);
948,075,533✔
63
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
948,132,708✔
64

65
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
948,133,301✔
66
  if (code) {
947,923,990✔
67
    tDecoderClear(&pReader->coder);
×
68
    return code;
×
69
  }
70
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
71

72
  return 0;
947,923,990✔
73
}
74

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

79
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
56,409,320✔
80
    metaULock(pVnodeObj->pMeta);
1,158,941✔
81
    return false;
1,158,178✔
82
  }
83

84
  metaULock(pVnodeObj->pMeta);
55,258,471✔
85
  return true;
55,260,916✔
86
}
87

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

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

97
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
266,058,468✔
98
  return metaGetTableEntryByVersion(pReader, version1, uid);
266,057,490✔
99
}
100

101
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
340,998,143✔
102
  SMeta *pMeta = pReader->pMeta;
340,998,143✔
103

104
  SMetaInfo info;
340,986,664✔
105
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
341,038,562✔
106
  if (TSDB_CODE_SUCCESS != code) {
340,995,050✔
107
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
1,197✔
108
  }
109

110
  return metaGetTableEntryByVersion(pReader, info.version, uid);
340,993,853✔
111
}
112

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

117
  // query name.idx
118
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
65,095,820✔
119
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
11,259,469✔
120
  }
121

122
  uid = *(tb_uid_t *)pReader->pBuf;
53,833,379✔
123
  return metaReaderGetTableEntryByUid(pReader, uid);
53,834,392✔
124
}
125

126
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
58,915,610✔
127
  void    *pData = NULL;
58,915,610✔
128
  int      nData = 0;
58,926,189✔
129
  tb_uid_t uid = 0;
58,925,636✔
130

131
  metaRLock(pMeta);
58,925,636✔
132

133
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
58,924,054✔
134
    uid = *(tb_uid_t *)pData;
9,357,412✔
135
    tdbFree(pData);
9,357,412✔
136
  }
137

138
  metaULock(pMeta);
58,924,036✔
139

140
  return uid;
58,922,525✔
141
}
142

143
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
7,333,165✔
144
  int         code = 0;
7,333,165✔
145
  SMetaReader mr = {0};
7,333,165✔
146
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
7,336,156✔
147
  code = metaReaderGetTableEntryByUid(&mr, uid);
7,324,608✔
148
  if (code < 0) {
7,299,989✔
149
    metaReaderClear(&mr);
×
150
    return code;
×
151
  }
152

153
  STR_TO_VARSTR(tbName, mr.me.name);
7,299,989✔
154
  metaReaderClear(&mr);
7,317,277✔
155

156
  return 0;
7,339,494✔
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) {
676,211✔
175
  int         code = 0;
676,211✔
176
  SMetaReader mr = {0};
676,211✔
177
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
676,211✔
178

179
  SMetaReader *pReader = &mr;
675,784✔
180

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

187
  *uid = *(tb_uid_t *)pReader->pBuf;
407,809✔
188

189
  metaReaderClear(&mr);
407,809✔
190

191
  return 0;
407,809✔
192
}
193

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

199
  code = metaGetTableEntryByName(&mr, tbName);
412,606✔
200
  if (code == 0) *tbType = mr.me.type;
413,102✔
201
  if (TSDB_CHILD_TABLE == mr.me.type) {
412,606✔
202
    *suid = mr.me.ctbEntry.suid;
406,318✔
203
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
6,288✔
204
    *suid = mr.me.uid;
5,872✔
205
  } else {
206
    *suid = 0;
416✔
207
  }
208

209
  metaReaderClear(&mr);
411,614✔
210
  return code;
412,606✔
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) {
9,967,224✔
246
  SMTbCursor *pTbCur = NULL;
9,967,224✔
247
  int32_t     code;
248

249
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
9,967,224✔
250
  if (pTbCur == NULL) {
9,966,017✔
251
    return NULL;
×
252
  }
253

254
  SVnode *pVnodeObj = pVnode;
9,966,017✔
255
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
256
  pTbCur->pMeta = pVnodeObj->pMeta;
9,966,017✔
257
  pTbCur->paused = 1;
9,966,628✔
258
  code = metaResumeTbCursor(pTbCur, 1, 0);
9,967,402✔
259
  if (code) {
9,965,336✔
260
    terrno = code;
×
261
    taosMemoryFree(pTbCur);
×
262
    return NULL;
×
263
  }
264
  return pTbCur;
9,965,336✔
265
}
266

267
void metaCloseTbCursor(SMTbCursor *pTbCur) {
19,927,232✔
268
  if (pTbCur) {
19,927,232✔
269
    tdbFree(pTbCur->pKey);
9,970,753✔
270
    tdbFree(pTbCur->pVal);
9,969,692✔
271
    if (!pTbCur->paused) {
9,969,076✔
272
      metaReaderClear(&pTbCur->mr);
4,495,424✔
273
      if (pTbCur->pDbc) {
4,494,871✔
274
        tdbTbcClose((TBC *)pTbCur->pDbc);
4,495,424✔
275
      }
276
    }
277
    taosMemoryFree(pTbCur);
9,968,938✔
278
  }
279
}
19,924,801✔
280

281
void metaPauseTbCursor(SMTbCursor *pTbCur) {
5,773,935✔
282
  if (!pTbCur->paused) {
5,773,935✔
283
    metaReaderClear(&pTbCur->mr);
5,773,935✔
284
    tdbTbcClose((TBC *)pTbCur->pDbc);
5,773,286✔
285
    pTbCur->paused = 1;
5,773,935✔
286
  }
287
}
5,774,136✔
288
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
10,268,987✔
289
  int32_t code = 0;
10,268,987✔
290
  int32_t lino;
291
  int8_t  locked = 0;
10,268,987✔
292
  if (pTbCur->paused) {
10,268,987✔
293
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
10,267,637✔
294
    locked = 1;
10,268,297✔
295
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
10,268,297✔
296
    if (code != 0) {
10,265,986✔
297
      TSDB_CHECK_CODE(code, lino, _exit);
×
298
    }
299

300
    if (first) {
10,265,986✔
301
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
9,967,380✔
302
      TSDB_CHECK_CODE(code, lino, _exit);
9,961,673✔
303
    } else {
304
      int c = 1;
298,606✔
305
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
298,606✔
306
      TSDB_CHECK_CODE(code, lino, _exit);
298,606✔
307
      if (c == 0) {
298,606✔
308
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
298,606✔
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;
10,259,726✔
319
  }
320

321
_exit:
×
322
  if (code != 0 && locked) {
10,265,930✔
323
    metaReaderReleaseLock(&pTbCur->mr);
×
324
  }
325
  return code;
10,263,942✔
326
}
327

328
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
351,038,163✔
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);
351,038,163✔
335
    if (ret < 0) {
351,077,299✔
336
      return ret;
9,961,737✔
337
    }
338

339
    tDecoderClear(&pTbCur->mr.coder);
341,115,562✔
340

341
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
341,127,987✔
342
    if (ret) return ret;
341,106,775✔
343

344
    if (pTbCur->mr.me.type == jumpTableType) {
341,106,775✔
345
      continue;
3,892,893✔
346
    }
347

348
    break;
337,224,514✔
349
  }
350

351
  return 0;
337,224,514✔
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
/**
383
 * @param type 0x01 fetchRsmaSchema if table is rsma
384
 */
385
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
160,684,714✔
386
                                   int8_t type) {
387
  int32_t         code = 0;
160,684,714✔
388
  void           *pData = NULL;
160,684,714✔
389
  int             nData = 0;
160,609,171✔
390
  int64_t         version;
391
  SSchemaWrapper  schema = {0};
160,607,243✔
392
  SSchemaWrapper *pSchema = NULL;
160,585,172✔
393
  SDecoder        dc = {0};
160,585,172✔
394
  if (lock) {
160,683,602✔
395
    metaRLock(pMeta);
153,785,224✔
396
  }
397
_query:
173,182,874✔
398
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
173,198,073✔
399
    goto _err;
191,562✔
400
  }
401

402
  version = ((SUidIdxVal *)pData)[0].version;
173,040,402✔
403

404
  if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData)) !=
173,040,972✔
405
      0) {
406
    goto _err;
×
407
  }
408

409
  SMetaEntry me = {0};
173,039,150✔
410
  tDecoderInit(&dc, pData, nData);
173,033,650✔
411
  code = metaDecodeEntry(&dc, &me);
173,044,943✔
412
  if (code) {
172,959,742✔
413
    tDecoderClear(&dc);
×
414
    goto _err;
×
415
  }
416
  if (me.type == TSDB_SUPER_TABLE) {
172,959,742✔
417
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
128,528,765✔
418
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
128,494,944✔
419
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
128,494,944✔
420
      if (TABLE_IS_ROLLUP(me.flags)) {
128,494,064✔
421
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
192,512✔
422
          tDecoderClear(&dc);
×
423
          goto _err;
×
424
        }
425
      }
426
      tDecoderClear(&dc);
128,494,064✔
427
      goto _exit;
128,491,691✔
428
    }
429
  } else if (me.type == TSDB_CHILD_TABLE) {
44,430,977✔
430
    uid = me.ctbEntry.suid;
12,486,839✔
431
    tDecoderClear(&dc);
12,486,839✔
432
    goto _query;
12,491,844✔
433
  } else {
434
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
31,944,138✔
435
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
31,946,283✔
436
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
31,946,283✔
437
      tDecoderClear(&dc);
31,948,839✔
438
      goto _exit;
31,951,097✔
439
    }
440
  }
441
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
33,401✔
442
  tDecoderClear(&dc);
33,401✔
443

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

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

460
_exit:
160,483,416✔
461
  if (lock) {
160,521,888✔
462
    metaULock(pMeta);
153,612,257✔
463
  }
464
  tdbFree(pData);
160,471,293✔
465
  return pSchema;
160,484,958✔
466

467
_err:
179,660✔
468
  if (lock) {
191,562✔
469
    metaULock(pMeta);
191,562✔
470
  }
471
  tdbFree(pData);
191,562✔
472
  tDeleteSchemaWrapper(pSchema);
473
  if (extSchema != NULL) {
191,562✔
474
    taosMemoryFreeClear(*extSchema);
164,060✔
475
  }
476
  terrno = code;
191,562✔
477
  return NULL;
191,562✔
478
}
479

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

490
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
10,865✔
491
    goto _exit;
×
492
  }
493

494
  version = ((SUidIdxVal *)pData)[0].version;
10,865✔
495

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

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

514
_exit:
10,865✔
515
  if (lock) {
10,865✔
516
    metaULock(pMeta);
10,865✔
517
  }
518
  tdbFree(pData);
10,865✔
519
  return createTime;
10,865✔
520
}
521

522
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
85,032,884✔
523
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
85,032,884✔
524
  SMCtbCursor *pCtbCur = NULL;
85,047,485✔
525
  SCtbIdxKey   ctbIdxKey;
526
  int          ret = 0;
85,047,485✔
527
  int          c = 0;
85,047,485✔
528

529
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
85,047,485✔
530
  if (pCtbCur == NULL) {
84,922,555✔
531
    return NULL;
×
532
  }
533

534
  pCtbCur->pMeta = pMeta;
84,922,555✔
535
  pCtbCur->suid = uid;
84,937,299✔
536
  pCtbCur->lock = lock;
84,917,129✔
537
  pCtbCur->paused = 1;
84,967,979✔
538

539
  ret = metaResumeCtbCursor(pCtbCur, 1);
84,942,055✔
540
  if (ret < 0) {
85,054,422✔
541
    return NULL;
×
542
  }
543
  return pCtbCur;
85,054,422✔
544
}
545

546
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
85,074,502✔
547
  if (pCtbCur) {
85,074,502✔
548
    if (!pCtbCur->paused) {
85,081,391✔
549
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
83,262,353✔
550
      if (pCtbCur->pCur) {
83,273,361✔
551
        tdbTbcClose(pCtbCur->pCur);
83,271,244✔
552
      }
553
    }
554
    tdbFree(pCtbCur->pKey);
85,066,001✔
555
    tdbFree(pCtbCur->pVal);
85,029,102✔
556
  }
557
  taosMemoryFree(pCtbCur);
85,031,829✔
558
}
85,034,550✔
559

560
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
1,807,688✔
561
  if (!pCtbCur->paused) {
1,807,688✔
562
    tdbTbcClose((TBC *)pCtbCur->pCur);
1,809,067✔
563
    if (pCtbCur->lock) {
1,808,499✔
564
      metaULock(pCtbCur->pMeta);
1,809,248✔
565
    }
566
    pCtbCur->paused = 1;
1,806,319✔
567
  }
568
}
1,808,524✔
569

570
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
85,035,863✔
571
  if (pCtbCur->paused) {
85,035,863✔
572
    pCtbCur->paused = 0;
85,000,763✔
573

574
    if (pCtbCur->lock) {
84,964,387✔
575
      metaRLock(pCtbCur->pMeta);
78,132,345✔
576
    }
577
    int ret = 0;
84,914,706✔
578
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
84,914,706✔
579
    if (ret < 0) {
85,027,757✔
580
      metaCloseCtbCursor(pCtbCur);
×
581
      return -1;
×
582
    }
583

584
    if (first) {
85,027,757✔
585
      SCtbIdxKey ctbIdxKey;
84,998,040✔
586
      // move to the suid
587
      ctbIdxKey.suid = pCtbCur->suid;
85,032,464✔
588
      ctbIdxKey.uid = INT64_MIN;
84,998,357✔
589
      int c = 0;
84,998,357✔
590
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
85,003,792✔
591
      if (c > 0) {
85,045,974✔
592
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
10,835,098✔
593
      }
594
    } else {
595
      int c = 0;
×
596
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
597
      if (c < 0) {
×
598
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
599
      } else {
600
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
601
      }
602
    }
603
  }
604
  return 0;
85,052,555✔
605
}
606

607
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
480,064,365✔
608
  int         ret;
609
  SCtbIdxKey *pCtbIdxKey;
610

611
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
480,064,365✔
612
  if (ret < 0) {
480,192,784✔
613
    return 0;
64,712,111✔
614
  }
615

616
  pCtbIdxKey = pCtbCur->pKey;
415,480,673✔
617
  if (pCtbIdxKey->suid > pCtbCur->suid) {
415,517,426✔
618
    return 0;
20,418,108✔
619
  }
620

621
  return pCtbIdxKey->uid;
395,050,804✔
622
}
623

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

634
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
54,999,917✔
635
  SMStbCursor *pStbCur = NULL;
54,999,917✔
636
  int          ret = 0;
54,999,917✔
637
  int          c = 0;
54,999,917✔
638

639
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
54,998,625✔
640
  if (pStbCur == NULL) {
54,997,333✔
641
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
642
    return NULL;
×
643
  }
644

645
  pStbCur->pMeta = pMeta;
54,997,333✔
646
  pStbCur->suid = suid;
54,995,395✔
647
  metaRLock(pMeta);
54,999,917✔
648

649
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
54,999,917✔
650
  if (ret < 0) {
54,990,196✔
651
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
652
    metaULock(pMeta);
×
653
    taosMemoryFree(pStbCur);
×
654
    return NULL;
×
655
  }
656

657
  // move to the suid
658
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
54,990,196✔
659
  if (c > 0) {
55,000,563✔
660
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
661
  }
662

663
  return pStbCur;
55,000,224✔
664
}
665

666
void metaCloseStbCursor(SMStbCursor *pStbCur) {
54,998,932✔
667
  if (pStbCur) {
54,998,932✔
668
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
54,999,578✔
669
    if (pStbCur->pCur) {
55,001,209✔
670
      tdbTbcClose(pStbCur->pCur);
54,999,578✔
671

672
      tdbFree(pStbCur->pKey);
55,001,209✔
673
      tdbFree(pStbCur->pVal);
54,997,640✔
674
    }
675

676
    taosMemoryFree(pStbCur);
54,997,979✔
677
  }
678
}
54,994,103✔
679

680
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
167,486,141✔
681
  int ret;
682

683
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
167,486,141✔
684
  if (ret < 0) {
167,485,704✔
685
    return 0;
55,001,209✔
686
  }
687
  return *(tb_uid_t *)pStbCur->pKey;
112,484,495✔
688
}
689

690
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
139,365,924✔
691
  STSchema       *pTSchema = NULL;
139,365,924✔
692
  SSchemaWrapper *pSW = NULL;
139,365,924✔
693

694
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
139,365,924✔
695
  if (!pSW) return NULL;
139,384,993✔
696

697
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
139,366,081✔
698

699
  tDeleteSchemaWrapper(pSW);
700
  return pTSchema;
139,270,024✔
701
}
702

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

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

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

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

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

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

742
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
137,642,010✔
743
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
137,642,010✔
744
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
137,532,270✔
745
    return terrno;
159✔
746
  }
747
  return TSDB_CODE_SUCCESS;
137,497,666✔
748
}
749

750
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
41,366,559✔
751
  int32_t code = 0;
41,366,559✔
752
  int32_t lino;
753

754
  void     *pData = NULL;
41,366,559✔
755
  int       nData = 0;
41,377,442✔
756
  SSkmDbKey skmDbKey;
41,360,776✔
757
  if (sver <= 0) {
41,377,494✔
758
    SMetaInfo info;
21,623,986✔
759
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
21,634,608✔
760
      sver = info.skmVer;
21,624,454✔
761
    } else {
762
      TBC *pSkmDbC = NULL;
12,567✔
763
      int  c;
7,040✔
764

765
      skmDbKey.uid = suid ? suid : uid;
7,040✔
766
      skmDbKey.sver = INT32_MAX;
7,040✔
767

768
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
7,040✔
769
      TSDB_CHECK_CODE(code, lino, _exit);
7,040✔
770
      metaRLock(pMeta);
7,040✔
771

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

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

787
      if (c < 0) {
7,040✔
788
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
789
      }
790

791
      const void *pKey = NULL;
7,040✔
792
      int32_t     nKey = 0;
7,040✔
793
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
7,040✔
794

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

802
      sver = ((SSkmDbKey *)pKey)->sver;
7,040✔
803

804
      metaULock(pMeta);
7,040✔
805
      tdbTbcClose(pSkmDbC);
7,040✔
806
    }
807
  }
808

809
  if (!(sver > 0)) {
41,376,195✔
810
    code = TSDB_CODE_NOT_FOUND;
×
811
    goto _exit;
×
812
  }
813

814
  skmDbKey.uid = suid ? suid : uid;
41,376,195✔
815
  skmDbKey.sver = sver;
41,376,195✔
816
  metaRLock(pMeta);
41,376,195✔
817
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
41,379,957✔
818
    metaULock(pMeta);
2,592✔
819
    code = TSDB_CODE_NOT_FOUND;
2,592✔
820
    goto _exit;
2,592✔
821
  }
822
  metaULock(pMeta);
41,380,950✔
823

824
  // decode
825
  SDecoder        dc = {0};
41,379,582✔
826
  SSchemaWrapper  schema;
41,349,531✔
827
  SSchemaWrapper *pSchemaWrapper = &schema;
41,370,415✔
828

829
  tDecoderInit(&dc, pData, nData);
41,370,415✔
830
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
41,391,290✔
831
  tDecoderClear(&dc);
41,391,290✔
832
  tdbFree(pData);
41,382,911✔
833
  if (TSDB_CODE_SUCCESS != code) {
41,358,011✔
834
    taosMemoryFree(pSchemaWrapper->pSchema);
×
835
    goto _exit;
×
836
  }
837

838
  // convert
839
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
41,358,011✔
840
  if (pTSchema == NULL) {
41,376,526✔
841
    code = TSDB_CODE_OUT_OF_MEMORY;
×
842
  }
843

844
  *ppTSchema = pTSchema;
41,376,526✔
845
  taosMemoryFree(pSchemaWrapper->pSchema);
41,382,993✔
846

847
_exit:
41,363,366✔
848
  return code;
41,369,579✔
849
}
850

851
// N.B. Called by statusReq per second
852
int64_t metaGetTbNum(SMeta *pMeta) {
111,057,583✔
853
  // num of child tables (excluding normal tables , stables and others)
854

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

858
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
111,057,583✔
859
}
860

861
void metaUpdTimeSeriesNum(SMeta *pMeta) {
55,000,253✔
862
  int64_t nCtbTimeSeries = 0;
55,000,253✔
863
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
55,000,253✔
864
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
54,998,831✔
865
  }
866
}
55,000,123✔
867

868
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
869
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
870
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
179,882,466✔
871
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
179,880,032✔
872
    metaUpdTimeSeriesNum(pMeta);
55,001,402✔
873
  }
874

875
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
179,880,814✔
876
}
877

878
// type: 1 reported timeseries
879
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
179,878,375✔
880
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
179,879,585✔
881
  if (type == 1) {
179,879,585✔
882
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
111,057,583✔
883
  }
884
  return nTimeSeries;
179,877,933✔
885
}
886

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

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

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

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

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

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

928
  return pSmaCur;
×
929
}
930

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

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

942
    taosMemoryFree(pSmaCur);
×
943
  }
944
}
×
945

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

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

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

960
  return pSmaIdxKey->uid;
×
961
}
962

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

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

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

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

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

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

1022
    ++smaIdx;
×
1023
  }
1024

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

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

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

1054
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1055

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

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

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

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

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

1084
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1085

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

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

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

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

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

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

1118
    lastUid = uid;
×
1119

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

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

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

1141
#endif
1142

1143
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
629,893,617✔
1144
  STag *tag = (STag *)pTag;
629,893,617✔
1145
  if (type == TSDB_DATA_TYPE_JSON) {
629,893,617✔
1146
    return tag;
1,524,666✔
1147
  }
1148
  bool find = tTagGet(tag, val);
628,368,951✔
1149

1150
  if (!find) {
628,410,598✔
1151
    return NULL;
5,444,299✔
1152
  }
1153

1154
  return val;
622,966,299✔
1155
}
1156

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

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

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

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

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

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

1199
  int32_t valid = 0;
2,022✔
1200
  int32_t count = 0;
2,022✔
1201

1202
  static const int8_t TRY_ERROR_LIMIT = 1;
1203
  do {
13,466,183✔
1204
    void   *entryKey = NULL;
13,468,205✔
1205
    int32_t nEntryKey = -1;
13,467,531✔
1206
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
13,468,205✔
1207
    if (valid < 0) break;
13,479,326✔
1208

1209
    SBtimeIdxKey *p = entryKey;
13,477,304✔
1210
    if (count > TRY_ERROR_LIMIT) break;
13,477,304✔
1211

1212
    terrno = TSDB_CODE_SUCCESS;
13,477,304✔
1213
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
13,472,249✔
1214
    if (terrno != TSDB_CODE_SUCCESS) {
13,472,923✔
1215
      ret = terrno;
×
1216
      break;
×
1217
    }
1218
    if (cmp == 0) {
13,473,260✔
1219
      if (taosArrayPush(pUids, &p->uid) == NULL) {
26,941,802✔
1220
        ret = terrno;
×
1221
        break;
×
1222
      }
1223
    } else {
1224
      if (param->equal == true) {
×
1225
        if (count > TRY_ERROR_LIMIT) break;
×
1226
        count++;
×
1227
      }
1228
    }
1229
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
13,468,542✔
1230
    if (valid < 0) break;
13,469,890✔
1231
  } while (1);
1232

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

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

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

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

1259
  char *pName = param->val;
×
1260

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

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

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

1281
    if (count > TRY_ERROR_LIMIT) break;
×
1282

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

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

1314
  taosMemoryFree(pCursor);
×
1315

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

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

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

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

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

1346
  taosMemoryFree(pCursor);
×
1347

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

1356
  SMetaEntry oStbEntry = {0};
3,441,053✔
1357
  int32_t    code = 0;
3,427,817✔
1358
  char      *buf = NULL;
3,427,817✔
1359
  void      *pData = NULL;
3,427,817✔
1360
  int        nData = 0;
3,438,486✔
1361

1362
  SDecoder    dc = {0};
3,437,789✔
1363
  STbDbKey    tbDbKey = {0};
3,427,960✔
1364
  STagIdxKey *pKey = NULL;
3,428,599✔
1365
  int32_t     nKey = 0;
3,430,265✔
1366

1367
  SIdxCursor *pCursor = NULL;
3,419,567✔
1368
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,419,567✔
1369
  if (!pCursor) {
3,427,602✔
1370
    return terrno;
×
1371
  }
1372
  pCursor->pMeta = pMeta;
3,427,602✔
1373
  pCursor->suid = param->suid;
3,427,602✔
1374
  pCursor->cid = param->cid;
3,439,906✔
1375
  pCursor->type = param->type;
3,425,277✔
1376

1377
  metaRLock(pMeta);
3,438,655✔
1378

1379
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
3,435,536✔
1380

1381
  tbDbKey.uid = param->suid;
3,442,222✔
1382
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,438,490✔
1383

1384
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
3,440,899✔
1385

1386
  tDecoderInit(&dc, pData, nData);
3,444,190✔
1387

1388
  code = metaDecodeEntry(&dc, &oStbEntry);
3,443,749✔
1389
  if (code) {
3,440,699✔
1390
    tDecoderClear(&dc);
×
1391
    goto END;
×
1392
  }
1393

1394
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
3,440,699✔
1395
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
441✔
1396
  }
1397

1398
  code = TSDB_CODE_INVALID_PARA;
3,440,699✔
1399

1400
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,647,491✔
1401
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,650,145✔
1402
    if (IS_IDX_ON(schema)) {
4,649,255✔
1403
      if (schema->colId == param->cid && param->type == schema->type) {
4,642,115✔
1404
        code = 0;
3,439,813✔
1405
        break;
3,439,813✔
1406
      }
1407
    }
1408
  }
1409

1410
  TAOS_CHECK_GOTO(code, NULL, END);
3,437,159✔
1411

1412
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,437,159✔
1413
  if (code != 0) {
3,438,494✔
1414
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1415
  }
1416

1417
  int32_t maxSize = 0;
3,438,494✔
1418
  int32_t nTagData = 0;
3,438,293✔
1419
  void   *tagData = NULL;
3,438,293✔
1420

1421
  if (param->val == NULL) {
3,438,293✔
1422
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1423
    goto END;
×
1424
  } else {
1425
    if (IS_VAR_DATA_TYPE(param->type)) {
3,439,175✔
1426
      tagData = varDataVal(param->val);
429,753✔
1427
      nTagData = varDataLen(param->val);
426,499✔
1428

1429
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
427,342✔
1430
        maxSize = 4 * nTagData + 1;
36,822✔
1431
        buf = taosMemoryCalloc(1, maxSize);
36,822✔
1432
        if (buf == NULL) {
36,822✔
1433
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1434
        }
1435

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

1440
        tagData = buf;
36,822✔
1441
        nTagData = maxSize;
36,822✔
1442
      }
1443
    } else {
1444
      tagData = param->val;
3,007,295✔
1445
      nTagData = tDataTypes[param->type].bytes;
3,013,156✔
1446
    }
1447
  }
1448

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

1453
  int cmp = 0;
3,434,635✔
1454
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,437,689✔
1455

1456
  int     count = 0;
3,443,353✔
1457
  int32_t valid = 0;
3,443,353✔
1458
  bool    found = false;
3,443,353✔
1459

1460
  static const int8_t TRY_ERROR_LIMIT = 1;
1461

1462
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1463
  /// target:                        [suid, cid2, type2]
1464
  int diffCidCount = 0;
3,443,353✔
1465
  do {
35,111,068✔
1466
    void   *entryKey = NULL, *entryVal = NULL;
38,554,421✔
1467
    int32_t nEntryKey, nEntryVal;
38,550,248✔
1468

1469
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
38,553,864✔
1470
    if (valid < 0) {
38,565,534✔
1471
      break;
1,695,190✔
1472
    }
1473
    if (count > TRY_ERROR_LIMIT) {
36,870,344✔
1474
      break;
1,033,136✔
1475
    }
1476

1477
    STagIdxKey *p = entryKey;
35,837,208✔
1478
    if (p == NULL) break;
35,837,208✔
1479

1480
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
35,837,208✔
1481
      if (found == true) break;  //
1,014,206✔
1482
      if (diffCidCount > TRY_ERROR_LIMIT) break;
297,897✔
1483
      diffCidCount++;
297,897✔
1484
      count++;
297,897✔
1485
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
297,897✔
1486
      if (valid < 0) {
297,897✔
1487
        code = valid;
×
1488
        break;
×
1489
      } else {
1490
        continue;
297,897✔
1491
      }
1492
    }
1493

1494
    terrno = TSDB_CODE_SUCCESS;
34,815,861✔
1495
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
34,815,922✔
1496
    if (terrno != TSDB_CODE_SUCCESS) {
34,812,302✔
1497
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1498
      break;
×
1499
    }
1500
    if (cmp == 0) {
34,815,106✔
1501
      // match
1502
      tb_uid_t tuid = 0;
31,183,537✔
1503
      if (IS_VAR_DATA_TYPE(pKey->type)) {
31,183,092✔
1504
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
293,489✔
1505
      } else {
1506
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
30,891,125✔
1507
      }
1508
      if (taosArrayPush(pUids, &tuid) == NULL) {
31,188,199✔
1509
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1510
      }
1511
      found = true;
31,188,640✔
1512
    } else {
1513
      if (param->equal == true) {
3,631,569✔
1514
        if (count > TRY_ERROR_LIMIT) break;
2,546,157✔
1515
        count++;
2,546,157✔
1516
      }
1517
    }
1518
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
34,818,283✔
1519
    if (valid < 0) {
34,809,247✔
1520
      code = valid;
×
1521
      break;
×
1522
    }
1523
  } while (1);
1524

1525
END:
3,437,024✔
1526
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,444,635✔
1527
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,444,489✔
1528
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,443,384✔
1529
  tDecoderClear(&dc);
3,443,384✔
1530
  tdbFree(pData);
3,443,600✔
1531

1532
  taosMemoryFree(buf);
3,442,867✔
1533
  taosMemoryFree(pKey);
3,444,266✔
1534

1535
  taosMemoryFree(pCursor);
3,442,197✔
1536

1537
  return code;
3,440,992✔
1538
}
1539

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

1546
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
38,852,956✔
1547
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
38,853,506✔
1548
  if (lock) {
38,851,212✔
1549
    metaULock(pMeta);
×
1550
  }
1551

1552
  return ret;
38,851,212✔
1553
}
1554

1555
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
4,999,051✔
1556
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
4,999,051✔
1557
  const int32_t LIMIT = 128;
4,999,702✔
1558

1559
  int32_t isLock = false;
4,999,702✔
1560
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
4,999,702✔
1561
  for (int i = 0; i < sz; i++) {
43,850,453✔
1562
    STUidTagInfo *p = taosArrayGet(uidList, i);
38,849,899✔
1563

1564
    if (i % LIMIT == 0) {
38,850,781✔
1565
      if (isLock) metaULock(pMeta);
4,379,291✔
1566

1567
      metaRLock(pMeta);
4,379,291✔
1568
      isLock = true;
4,379,186✔
1569
    }
1570

1571
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1572
    void   *val = NULL;
38,850,676✔
1573
    int32_t len = 0;
38,852,159✔
1574
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
38,851,718✔
1575
      p->pTagVal = taosMemoryMalloc(len);
38,847,525✔
1576
      if (!p->pTagVal) {
38,852,372✔
1577
        if (isLock) metaULock(pMeta);
×
1578

1579
        TAOS_RETURN(terrno);
×
1580
      }
1581
      memcpy(p->pTagVal, val, len);
38,852,516✔
1582
      tdbFree(val);
38,852,625✔
1583
    } else {
1584
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
2,364✔
1585
    }
1586
  }
1587
  //  }
1588
  if (isLock) metaULock(pMeta);
5,000,554✔
1589
  return 0;
4,999,702✔
1590
}
1591

1592
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
19,718,871✔
1593
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
19,718,871✔
1594
  if (!pCur) {
19,718,906✔
1595
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1596
  }
1597

1598
  while (1) {
143,230,050✔
1599
    tb_uid_t uid = metaCtbCursorNext(pCur);
162,948,956✔
1600
    if (uid == 0) {
162,942,184✔
1601
      break;
19,727,056✔
1602
    }
1603

1604
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
143,215,128✔
1605
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
143,214,680✔
1606
    if (!info.pTagVal) {
143,188,466✔
1607
      metaCloseCtbCursor(pCur);
×
1608
      return terrno;
×
1609
    }
1610
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
143,188,466✔
1611
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
143,227,824✔
1612
      taosMemoryFreeClear(info.pTagVal);
×
1613
      metaCloseCtbCursor(pCur);
×
1614
      return terrno;
×
1615
    }
1616
  }
1617
  metaCloseCtbCursor(pCur);
19,726,671✔
1618
  return TSDB_CODE_SUCCESS;
19,721,727✔
1619
}
1620

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

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

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) {
×
1642
      taosArrayDestroy(suids);
×
1643
      return terrno;
×
1644
    }
1645
  }
1646

1647
  metaCloseStbCursor(pCur);
×
1648

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

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

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

1662
      return code;
×
1663
    }
1664

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

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

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

1675
        return code;
×
1676
      }
1677

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

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

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

1691
          return code;
×
1692
        }
1693
      }
1694

1695
      tDestroyTSchema(pTSchema);
×
1696
    }
1697

1698
    taosArrayDestroy(uids);
×
1699
  }
1700

1701
  taosArrayDestroy(suids);
×
1702

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,551,238,135✔
1709
  int32_t code = 0;
1,551,238,135✔
1710
  void   *pData = NULL;
1,551,238,135✔
1711
  int     nData = 0;
1,551,342,423✔
1712
  int     lock = 0;
1,551,388,043✔
1713

1714
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
1,551,388,043✔
1715
    lock = 1;
341,039,533✔
1716
  }
1717

1718
  if (!lock) metaRLock(pMeta);
1,551,396,677✔
1719

1720
  // search cache
1721
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
1,551,416,460✔
1722
    if (!lock) metaULock(pMeta);
1,465,776,721✔
1723
    goto _exit;
1,465,754,769✔
1724
  }
1725

1726
  // search TDB
1727
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
85,658,761✔
1728
    // not found
1729
    if (!lock) metaULock(pMeta);
77,051,130✔
1730
    goto _exit;
77,038,143✔
1731
  }
1732

1733
  if (!lock) metaULock(pMeta);
8,613,918✔
1734

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

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

1752
  if (lock) {
8,613,270✔
1753
    metaRLock(pReader->pMeta);
740,963✔
1754
  }
1755

1756
_exit:
1,551,238,750✔
1757
  tdbFree(pData);
1,551,410,895✔
1758
  return code;
1,551,369,525✔
1759
}
1760

1761
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
178,155,762✔
1762
  int32_t code = 0;
178,155,762✔
1763

1764
  if (!numOfTables && !numOfCols) goto _exit;
178,155,762✔
1765

1766
  SVnode *pVnodeObj = pVnode;
178,155,762✔
1767
  metaRLock(pVnodeObj->pMeta);
178,155,762✔
1768

1769
  // fast path: search cache
1770
  SMetaStbStats state = {0};
178,152,669✔
1771
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
178,153,545✔
1772
    metaULock(pVnodeObj->pMeta);
171,263,665✔
1773
    if (numOfTables) *numOfTables = state.ctbNum;
171,269,478✔
1774
    if (numOfCols) *numOfCols = state.colNum;
171,269,478✔
1775
    if (flags) *flags = state.flags;
171,265,879✔
1776
    goto _exit;
171,265,879✔
1777
  }
1778

1779
  // slow path: search TDB
1780
  int64_t ctbNum = 0;
6,896,180✔
1781
  int32_t colNum = 0;
6,898,425✔
1782
  int64_t keep = 0;
6,898,445✔
1783
  int8_t  flag = 0;
6,898,425✔
1784

1785
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
6,898,567✔
1786
  if (TSDB_CODE_SUCCESS == code) {
6,898,599✔
1787
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
6,898,599✔
1788
  }
1789
  if (TSDB_CODE_SUCCESS == code) {
6,897,920✔
1790
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
6,897,920✔
1791
  }
1792
  metaULock(pVnodeObj->pMeta);
6,898,416✔
1793
  if (TSDB_CODE_SUCCESS != code) {
6,898,478✔
1794
    goto _exit;
×
1795
  }
1796

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

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

1809
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
6,898,519✔
1810
  if (ret) {
6,898,346✔
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,898,346✔
1816

1817
_exit:
178,164,437✔
1818
  return code;
178,165,424✔
1819
}
1820

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

1824
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
68,295,373✔
1825
    stats.ctbNum += deltaCtb;
57,753,505✔
1826
    stats.colNum += deltaCol;
57,753,505✔
1827
    if (deltaKeep > 0) {
57,753,505✔
1828
      stats.keep = deltaKeep;
12,912✔
1829
    }
1830

1831
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
57,753,505✔
1832
    if (code) {
57,740,055✔
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
}
68,291,209✔
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