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

taosdata / TDengine / #4850

14 Nov 2025 08:06AM UTC coverage: 63.728% (-0.1%) from 63.829%
#4850

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

355 of 675 new or added lines in 18 files covered. (52.59%)

634 existing lines in 110 files now uncovered.

149066 of 233910 relevant lines covered (63.73%)

115676883.39 hits per line

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

61.25
/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) {
519,104,595✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
519,104,595✔
21
  metaReaderDoInit(pReader, pMeta, flags);
519,165,716✔
22
  pReader->pAPI = pAPI;
519,147,669✔
23
}
519,144,237✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
602,356,458✔
26
  memset(pReader, 0, sizeof(*pReader));
602,356,458✔
27
  pReader->pMeta = pMeta;
602,356,458✔
28
  pReader->flags = flags;
602,345,560✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
602,291,061✔
30
    metaRLock(pMeta);
426,010,579✔
31
  }
32
}
602,291,008✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
223,953,682✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
223,953,682✔
36
    metaULock(pReader->pMeta);
224,122,580✔
37
    pReader->flags |= META_READER_NOLOCK;
223,898,875✔
38
  }
39
}
224,050,983✔
40

41
void metaReaderClear(SMetaReader *pReader) {
632,716,662✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
632,716,662✔
43
    metaULock(pReader->pMeta);
201,811,839✔
44
  }
45
  tDecoderClear(&pReader->coder);
632,636,286✔
46
  tdbFree(pReader->pBuf);
632,938,831✔
47
  pReader->pBuf = NULL;
632,836,825✔
48
}
632,874,027✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
954,829,335✔
51
  int32_t  code = 0;
954,829,335✔
52
  SMeta   *pMeta = pReader->pMeta;
954,829,335✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
954,980,319✔
54

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

60
  // decode the entry
61
  tDecoderClear(&pReader->coder);
954,875,982✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
954,929,521✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
954,947,846✔
65
  if (code) {
954,895,154✔
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
954,895,154✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
57,989,964✔
79
    metaULock(pVnodeObj->pMeta);
733,229✔
80
    return false;
732,873✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
57,261,781✔
84
  return true;
57,267,760✔
85
}
86

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

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

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
266,234,612✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
266,243,840✔
98
}
99

100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
345,852,776✔
101
  SMeta *pMeta = pReader->pMeta;
345,852,776✔
102

103
  SMetaInfo info;
345,896,293✔
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
345,967,247✔
105
  if (TSDB_CODE_SUCCESS != code) {
345,883,978✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
128✔
107
  }
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
345,883,850✔
110
}
111

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

116
  // query name.idx
117
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
64,736,172✔
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
11,215,582✔
119
  }
120

121
  uid = *(tb_uid_t *)pReader->pBuf;
53,521,580✔
122
  return metaReaderGetTableEntryByUid(pReader, uid);
53,520,568✔
123
}
124

125
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
58,741,527✔
126
  void    *pData = NULL;
58,741,527✔
127
  int      nData = 0;
58,742,355✔
128
  tb_uid_t uid = 0;
58,743,088✔
129

130
  metaRLock(pMeta);
58,743,088✔
131

132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
58,743,310✔
133
    uid = *(tb_uid_t *)pData;
9,488,566✔
134
    tdbFree(pData);
9,488,566✔
135
  }
136

137
  metaULock(pMeta);
58,739,783✔
138

139
  return uid;
58,734,481✔
140
}
141

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

152
  STR_TO_VARSTR(tbName, mr.me.name);
7,157,439✔
153
  metaReaderClear(&mr);
7,106,995✔
154

155
  return 0;
7,182,206✔
156
}
157

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

170
  return 0;
×
171
}
172

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

178
  SMetaReader *pReader = &mr;
673,940✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
406,250✔
187

188
  metaReaderClear(&mr);
406,250✔
189

190
  return 0;
406,250✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
410,904✔
199
  if (code == 0) *tbType = mr.me.type;
410,904✔
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
410,904✔
201
    *suid = mr.me.ctbEntry.suid;
404,714✔
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
6,190✔
203
    *suid = mr.me.uid;
5,776✔
204
  } else {
205
    *suid = 0;
414✔
206
  }
207

208
  metaReaderClear(&mr);
410,904✔
209
  return code;
410,415✔
210
}
211

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

215
  // TODO
216

217
  return 0;
×
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;
×
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;
×
232
  } else {
233
    goto _exit;
×
234
  }
235

236
  code = 0;
×
237

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

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

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
9,960,258✔
249
  if (pTbCur == NULL) {
9,961,521✔
250
    return NULL;
×
251
  }
252

253
  SVnode *pVnodeObj = pVnode;
9,961,521✔
254
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
255
  pTbCur->pMeta = pVnodeObj->pMeta;
9,961,521✔
256
  pTbCur->paused = 1;
9,964,170✔
257
  code = metaResumeTbCursor(pTbCur, 1, 0);
9,964,365✔
258
  if (code) {
9,960,256✔
259
    terrno = code;
×
260
    taosMemoryFree(pTbCur);
×
261
    return NULL;
×
262
  }
263
  return pTbCur;
9,960,256✔
264
}
265

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
19,914,303✔
267
  if (pTbCur) {
19,914,303✔
268
    tdbFree(pTbCur->pKey);
9,962,044✔
269
    tdbFree(pTbCur->pVal);
9,963,977✔
270
    if (!pTbCur->paused) {
9,963,977✔
271
      metaReaderClear(&pTbCur->mr);
4,498,182✔
272
      if (pTbCur->pDbc) {
4,497,588✔
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
4,496,429✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
9,961,269✔
277
  }
278
}
19,915,510✔
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
5,763,640✔
281
  if (!pTbCur->paused) {
5,763,640✔
282
    metaReaderClear(&pTbCur->mr);
5,764,030✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
5,764,030✔
284
    pTbCur->paused = 1;
5,764,680✔
285
  }
286
}
5,764,680✔
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
10,258,169✔
288
  int32_t code = 0;
10,258,169✔
289
  int32_t lino;
290
  int8_t  locked = 0;
10,258,169✔
291
  if (pTbCur->paused) {
10,258,169✔
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
10,257,679✔
293
    locked = 1;
10,259,598✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
10,259,598✔
295
    if (code != 0) {
10,255,418✔
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
10,255,418✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
9,958,264✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
9,957,874✔
302
    } else {
303
      int c = 1;
297,154✔
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
297,154✔
305
      TSDB_CHECK_CODE(code, lino, _exit);
297,154✔
306
      if (c == 0) {
297,154✔
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
297,154✔
308
      } else if (c < 0) {
×
309
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
310
        TSDB_CHECK_CODE(code, lino, _exit);
×
311
      } else {
312
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
313
        TSDB_CHECK_CODE(code, lino, _exit);
×
314
      }
315
    }
316

317
    pTbCur->paused = 0;
10,257,615✔
318
  }
319

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

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

332
  for (;;) {
333
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
352,748,216✔
334
    if (ret < 0) {
352,735,309✔
335
      return ret;
9,955,218✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
342,780,091✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
342,795,303✔
341
    if (ret) return ret;
342,777,490✔
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
342,777,490✔
344
      continue;
3,827,101✔
345
    }
346

347
    break;
338,974,712✔
348
  }
349

350
  return 0;
338,974,712✔
351
}
352

353
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
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;
×
362
    }
363

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

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

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

375
    break;
×
376
  }
377

378
  return 0;
×
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,
164,464,146✔
385
                                   int8_t type) {
386
  int32_t         code = 0;
164,464,146✔
387
  void           *pData = NULL;
164,464,146✔
388
  int             nData = 0;
164,484,306✔
389
  int64_t         version;
390
  SSchemaWrapper  schema = {0};
164,483,331✔
391
  SSchemaWrapper *pSchema = NULL;
164,418,159✔
392
  SDecoder        dc = {0};
164,418,159✔
393
  if (lock) {
164,461,386✔
394
    metaRLock(pMeta);
157,641,275✔
395
  }
396
_query:
176,700,723✔
397
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
176,718,571✔
398
    goto _err;
64,809✔
399
  }
400

401
  version = ((SUidIdxVal *)pData)[0].version;
176,734,905✔
402

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

408
  SMetaEntry me = {0};
176,728,715✔
409
  tDecoderInit(&dc, pData, nData);
176,712,319✔
410
  code = metaDecodeEntry(&dc, &me);
176,737,797✔
411
  if (code) {
176,567,729✔
412
    tDecoderClear(&dc);
×
413
    goto _err;
×
414
  }
415
  if (me.type == TSDB_SUPER_TABLE) {
176,567,729✔
416
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
131,280,724✔
417
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
131,242,907✔
418
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
131,242,907✔
419
      if (TABLE_IS_ROLLUP(me.flags)) {
131,228,527✔
420
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
193,521✔
421
          tDecoderClear(&dc);
×
422
          goto _err;
×
423
        }
424
      }
425
      tDecoderClear(&dc);
131,228,527✔
426
      goto _exit;
131,252,951✔
427
    }
428
  } else if (me.type == TSDB_CHILD_TABLE) {
45,287,005✔
429
    uid = me.ctbEntry.suid;
12,175,163✔
430
    tDecoderClear(&dc);
12,175,163✔
431
    goto _query;
12,220,495✔
432
  } else {
433
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
33,111,842✔
434
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
33,117,822✔
435
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
33,117,822✔
436
      tDecoderClear(&dc);
33,117,934✔
437
      goto _exit;
33,123,373✔
438
    }
439
  }
440
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
31,691✔
441
  tDecoderClear(&dc);
31,691✔
442

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

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

459
_exit:
164,411,927✔
460
  if (lock) {
164,455,319✔
461
    metaULock(pMeta);
157,631,159✔
462
  }
463
  tdbFree(pData);
164,382,042✔
464
  return pSchema;
164,367,208✔
465

466
_err:
49,489✔
467
  if (lock) {
64,809✔
468
    metaULock(pMeta);
64,809✔
469
  }
470
  tdbFree(pData);
64,809✔
471
  tDeleteSchemaWrapper(pSchema);
472
  if (extSchema != NULL) {
64,809✔
473
    taosMemoryFreeClear(*extSchema);
39,157✔
474
  }
475
  terrno = code;
64,809✔
476
  return NULL;
64,809✔
477
}
478

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

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

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

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

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

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

521
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
87,681,203✔
522
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
87,681,203✔
523
  SMCtbCursor *pCtbCur = NULL;
87,730,184✔
524
  SCtbIdxKey   ctbIdxKey;
525
  int          ret = 0;
87,730,184✔
526
  int          c = 0;
87,730,184✔
527

528
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
87,730,184✔
529
  if (pCtbCur == NULL) {
87,547,241✔
530
    return NULL;
×
531
  }
532

533
  pCtbCur->pMeta = pMeta;
87,547,241✔
534
  pCtbCur->suid = uid;
87,566,053✔
535
  pCtbCur->lock = lock;
87,587,517✔
536
  pCtbCur->paused = 1;
87,607,224✔
537

538
  ret = metaResumeCtbCursor(pCtbCur, 1);
87,596,250✔
539
  if (ret < 0) {
87,724,010✔
540
    return NULL;
×
541
  }
542
  return pCtbCur;
87,724,010✔
543
}
544

545
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
87,736,650✔
546
  if (pCtbCur) {
87,736,650✔
547
    if (!pCtbCur->paused) {
87,742,032✔
548
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
85,941,895✔
549
      if (pCtbCur->pCur) {
85,965,725✔
550
        tdbTbcClose(pCtbCur->pCur);
85,951,012✔
551
      }
552
    }
553
    tdbFree(pCtbCur->pKey);
87,706,551✔
554
    tdbFree(pCtbCur->pVal);
87,670,047✔
555
  }
556
  taosMemoryFree(pCtbCur);
87,683,417✔
557
}
87,665,837✔
558

559
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
1,793,422✔
560
  if (!pCtbCur->paused) {
1,793,422✔
561
    tdbTbcClose((TBC *)pCtbCur->pCur);
1,792,707✔
562
    if (pCtbCur->lock) {
1,792,838✔
563
      metaULock(pCtbCur->pMeta);
1,793,314✔
564
    }
565
    pCtbCur->paused = 1;
1,792,473✔
566
  }
567
}
1,794,334✔
568

569
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
87,684,758✔
570
  if (pCtbCur->paused) {
87,684,758✔
571
    pCtbCur->paused = 0;
87,643,657✔
572

573
    if (pCtbCur->lock) {
87,691,316✔
574
      metaRLock(pCtbCur->pMeta);
80,856,178✔
575
    }
576
    int ret = 0;
87,558,852✔
577
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
87,558,852✔
578
    if (ret < 0) {
87,678,823✔
579
      metaCloseCtbCursor(pCtbCur);
×
580
      return -1;
×
581
    }
582

583
    if (first) {
87,678,823✔
584
      SCtbIdxKey ctbIdxKey;
87,648,512✔
585
      // move to the suid
586
      ctbIdxKey.suid = pCtbCur->suid;
87,682,623✔
587
      ctbIdxKey.uid = INT64_MIN;
87,713,956✔
588
      int c = 0;
87,713,956✔
589
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
87,604,593✔
590
      if (c > 0) {
87,717,579✔
591
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
9,179,652✔
592
      }
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);
×
598
      } else {
599
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
600
      }
601
    }
602
  }
603
  return 0;
87,725,724✔
604
}
605

606
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
475,835,170✔
607
  int         ret;
608
  SCtbIdxKey *pCtbIdxKey;
609

610
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
475,835,170✔
611
  if (ret < 0) {
475,960,256✔
612
    return 0;
70,784,221✔
613
  }
614

615
  pCtbIdxKey = pCtbCur->pKey;
405,176,035✔
616
  if (pCtbIdxKey->suid > pCtbCur->suid) {
405,176,945✔
617
    return 0;
17,003,783✔
618
  }
619

620
  return pCtbIdxKey->uid;
388,174,440✔
621
}
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) {
61,269,208✔
634
  SMStbCursor *pStbCur = NULL;
61,269,208✔
635
  int          ret = 0;
61,269,208✔
636
  int          c = 0;
61,269,208✔
637

638
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
61,269,855✔
639
  if (pStbCur == NULL) {
61,267,267✔
640
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
641
    return NULL;
×
642
  }
643

644
  pStbCur->pMeta = pMeta;
61,267,267✔
645
  pStbCur->suid = suid;
61,269,423✔
646
  metaRLock(pMeta);
61,269,855✔
647

648
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
61,271,886✔
649
  if (ret < 0) {
61,268,561✔
650
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
651
    metaULock(pMeta);
×
652
    taosMemoryFree(pStbCur);
×
653
    return NULL;
×
654
  }
655

656
  // move to the suid
657
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
61,268,561✔
658
  if (c > 0) {
61,271,921✔
659
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
660
  }
661

662
  return pStbCur;
61,269,980✔
663
}
664

665
void metaCloseStbCursor(SMStbCursor *pStbCur) {
61,268,519✔
666
  if (pStbCur) {
61,268,519✔
667
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
61,270,502✔
668
    if (pStbCur->pCur) {
61,273,305✔
669
      tdbTbcClose(pStbCur->pCur);
61,269,208✔
670

671
      tdbFree(pStbCur->pKey);
61,271,364✔
672
      tdbFree(pStbCur->pVal);
61,270,502✔
673
    }
674

675
    taosMemoryFree(pStbCur);
61,272,568✔
676
  }
677
}
61,267,225✔
678

679
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
169,549,743✔
680
  int ret;
681

682
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
169,549,743✔
683
  if (ret < 0) {
169,554,919✔
684
    return 0;
61,272,658✔
685
  }
686
  return *(tb_uid_t *)pStbCur->pKey;
108,282,261✔
687
}
688

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

693
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
143,728,942✔
694
  if (!pSW) return NULL;
143,758,396✔
695

696
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
143,740,574✔
697

698
  tDeleteSchemaWrapper(pSW);
699
  return pTSchema;
143,647,017✔
700
}
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) {
54,216✔
706
  SRSchema       *pRSchema = NULL;
54,216✔
707
  SSchemaWrapper *pSW = NULL;
54,216✔
708

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

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

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

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

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

741
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
142,083,330✔
742
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
142,083,330✔
743
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
142,003,177✔
UNCOV
744
    return terrno;
×
745
  }
746
  return TSDB_CODE_SUCCESS;
142,046,155✔
747
}
748

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

753
  void     *pData = NULL;
41,437,346✔
754
  int       nData = 0;
41,446,257✔
755
  SSkmDbKey skmDbKey;
41,431,090✔
756
  if (sver <= 0) {
41,442,810✔
757
    SMetaInfo info;
21,776,184✔
758
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
21,788,464✔
759
      sver = info.skmVer;
21,781,175✔
760
    } else {
761
      TBC *pSkmDbC = NULL;
10,385✔
762
      int  c;
7,032✔
763

764
      skmDbKey.uid = suid ? suid : uid;
7,032✔
765
      skmDbKey.sver = INT32_MAX;
7,032✔
766

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

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

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

786
      if (c < 0) {
7,032✔
787
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
1,432✔
788
      }
789

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

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

801
      sver = ((SSkmDbKey *)pKey)->sver;
7,032✔
802

803
      metaULock(pMeta);
7,032✔
804
      tdbTbcClose(pSkmDbC);
7,032✔
805
    }
806
  }
807

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

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

823
  // decode
824
  SDecoder        dc = {0};
41,445,112✔
825
  SSchemaWrapper  schema;
41,417,536✔
826
  SSchemaWrapper *pSchemaWrapper = &schema;
41,437,747✔
827

828
  tDecoderInit(&dc, pData, nData);
41,437,747✔
829
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
41,457,825✔
830
  tDecoderClear(&dc);
41,457,825✔
831
  tdbFree(pData);
41,449,751✔
832
  if (TSDB_CODE_SUCCESS != code) {
41,424,853✔
833
    taosMemoryFree(pSchemaWrapper->pSchema);
×
834
    goto _exit;
×
835
  }
836

837
  // convert
838
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
41,424,853✔
839
  if (pTSchema == NULL) {
41,437,824✔
840
    code = TSDB_CODE_OUT_OF_MEMORY;
×
841
  }
842

843
  *ppTSchema = pTSchema;
41,437,824✔
844
  taosMemoryFree(pSchemaWrapper->pSchema);
41,449,880✔
845

846
_exit:
41,431,225✔
847
  return code;
41,432,720✔
848
}
849

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

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

857
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
123,828,554✔
858
}
859

860
void metaUpdTimeSeriesNum(SMeta *pMeta) {
61,268,823✔
861
  int64_t nCtbTimeSeries = 0;
61,268,823✔
862
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
61,270,764✔
863
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
61,269,470✔
864
  }
865
}
61,272,273✔
866

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;
192,282,003✔
870
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
192,286,207✔
871
    metaUpdTimeSeriesNum(pMeta);
61,265,123✔
872
  }
873

874
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
192,294,238✔
875
}
876

877
// type: 1 reported timeseries
878
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
192,275,833✔
879
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
192,282,631✔
880
  if (type == 1) {
192,282,631✔
881
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
123,828,554✔
882
  }
883
  return nTimeSeries;
192,276,621✔
884
}
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;
×
899
  int          ret;
900
  int          c;
×
901

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

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

912
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
913
  if (ret < 0) {
×
914
    metaULock(pMeta);
×
915
    taosMemoryFree(pSmaCur);
×
916
    return NULL;
×
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);
×
925
  }
926

927
  return pSmaCur;
×
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;
×
936

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

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

945
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
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;
×
952
  }
953

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

959
  return pSmaIdxKey->uid;
×
960
}
961

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

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

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

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

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

984
  SMetaReader mr = {0};
×
985
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
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;
×
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;
×
1004
        }
1005
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
1006
      }
1007
      if (pTSma->tagsFilterLen > 0) {
×
1008
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
1009
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1010
          goto _err;
×
1011
        }
1012
      }
1013
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
1014
    } else {
1015
      pTSma->exprLen = 0;
×
1016
      pTSma->expr = NULL;
×
1017
      pTSma->tagsFilterLen = 0;
×
1018
      pTSma->tagsFilter = NULL;
×
1019
    }
1020

1021
    ++smaIdx;
×
1022
  }
1023

1024
  if (smaIdx <= 0) goto _err;
×
1025
  pSW->number = smaIdx;
×
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;
×
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;
×
1045
  }
1046
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
×
1047
  if (!pTSma) {
×
1048
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1049
    metaReaderClear(&mr);
×
1050
    return NULL;
×
1051
  }
1052

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

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

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

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

1068
  while (1) {
×
1069
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1070
    if (id == 0) {
×
1071
      break;
×
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;
×
1080
      }
1081
    }
1082

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

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

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

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

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

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

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

1117
    lastUid = uid;
×
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;
×
1125
      }
1126
    }
1127

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

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

1140
#endif
1141

1142
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
631,324,313✔
1143
  STag *tag = (STag *)pTag;
631,324,313✔
1144
  if (type == TSDB_DATA_TYPE_JSON) {
631,324,313✔
1145
    return tag;
1,507,001✔
1146
  }
1147
  bool find = tTagGet(tag, val);
629,817,312✔
1148

1149
  if (!find) {
629,879,746✔
1150
    return NULL;
5,616,817✔
1151
  }
1152

1153
  return val;
624,262,929✔
1154
}
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) {
1,980✔
1169
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,980✔
1170
  SMetaFltParam *param = arg;
1,980✔
1171
  int32_t        ret = 0;
1,980✔
1172

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

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

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

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

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

1201
  static const int8_t TRY_ERROR_LIMIT = 1;
1202
  do {
13,199,670✔
1203
    void   *entryKey = NULL;
13,201,650✔
1204
    int32_t nEntryKey = -1;
13,201,650✔
1205
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
13,201,650✔
1206
    if (valid < 0) break;
13,201,320✔
1207

1208
    SBtimeIdxKey *p = entryKey;
13,199,340✔
1209
    if (count > TRY_ERROR_LIMIT) break;
13,199,340✔
1210

1211
    terrno = TSDB_CODE_SUCCESS;
13,199,340✔
1212
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
13,197,360✔
1213
    if (terrno != TSDB_CODE_SUCCESS) {
13,194,390✔
1214
      ret = terrno;
×
1215
      break;
×
1216
    }
1217
    if (cmp == 0) {
13,194,390✔
1218
      if (taosArrayPush(pUids, &p->uid) == NULL) {
26,385,810✔
1219
        ret = terrno;
×
1220
        break;
×
1221
      }
1222
    } else {
1223
      if (param->equal == true) {
×
1224
        if (count > TRY_ERROR_LIMIT) break;
×
1225
        count++;
×
1226
      }
1227
    }
1228
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
13,191,420✔
1229
    if (valid < 0) break;
13,199,670✔
1230
  } while (1);
1231

1232
END:
1,980✔
1233
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
1,980✔
1234
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
1,980✔
1235
  taosMemoryFree(pCursor);
1,980✔
1236
  return ret;
1,980✔
1237
}
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;
×
1244

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

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

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

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

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

1280
    if (count > TRY_ERROR_LIMIT) break;
×
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;
×
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;
×
1294
      }
1295
    } else {
1296
      if (param->equal == true) {
×
1297
        if (count > TRY_ERROR_LIMIT) break;
×
1298
        count++;
×
1299
      }
1300
    }
1301
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1302
    if (valid < 0) {
×
1303
      break;
×
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);
×
1312

1313
  taosMemoryFree(pCursor);
×
1314

1315
  return ret;
×
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;
×
1322

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

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

1336
  metaRLock(pMeta);
×
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);
×
1344

1345
  taosMemoryFree(pCursor);
×
1346

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

1355
  SMetaEntry oStbEntry = {0};
3,536,810✔
1356
  int32_t    code = 0;
3,531,138✔
1357
  char      *buf = NULL;
3,531,138✔
1358
  void      *pData = NULL;
3,531,138✔
1359
  int        nData = 0;
3,532,631✔
1360

1361
  SDecoder    dc = {0};
3,534,684✔
1362
  STbDbKey    tbDbKey = {0};
3,531,671✔
1363
  STagIdxKey *pKey = NULL;
3,523,307✔
1364
  int32_t     nKey = 0;
3,529,347✔
1365

1366
  SIdxCursor *pCursor = NULL;
3,529,367✔
1367
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,529,367✔
1368
  if (!pCursor) {
3,524,902✔
1369
    return terrno;
×
1370
  }
1371
  pCursor->pMeta = pMeta;
3,524,902✔
1372
  pCursor->suid = param->suid;
3,525,416✔
1373
  pCursor->cid = param->cid;
3,533,074✔
1374
  pCursor->type = param->type;
3,528,826✔
1375

1376
  metaRLock(pMeta);
3,535,049✔
1377

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

1380
  tbDbKey.uid = param->suid;
3,536,004✔
1381
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,533,499✔
1382

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

1385
  tDecoderInit(&dc, pData, nData);
3,538,169✔
1386

1387
  code = metaDecodeEntry(&dc, &oStbEntry);
3,539,049✔
1388
  if (code) {
3,536,157✔
1389
    tDecoderClear(&dc);
×
1390
    goto END;
×
1391
  }
1392

1393
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
3,536,157✔
1394
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
798✔
1395
  }
1396

1397
  code = TSDB_CODE_INVALID_PARA;
3,536,157✔
1398

1399
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,741,989✔
1400
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,742,071✔
1401
    if (IS_IDX_ON(schema)) {
4,743,309✔
1402
      if (schema->colId == param->cid && param->type == schema->type) {
4,734,502✔
1403
        code = 0;
3,536,363✔
1404
        break;
3,536,363✔
1405
      }
1406
    }
1407
  }
1408

1409
  TAOS_CHECK_GOTO(code, NULL, END);
3,536,281✔
1410

1411
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,536,281✔
1412
  if (code != 0) {
3,534,887✔
1413
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1414
  }
1415

1416
  int32_t maxSize = 0;
3,534,887✔
1417
  int32_t nTagData = 0;
3,534,529✔
1418
  void   *tagData = NULL;
3,534,529✔
1419

1420
  if (param->val == NULL) {
3,534,529✔
1421
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1422
    goto END;
×
1423
  } else {
1424
    if (IS_VAR_DATA_TYPE(param->type)) {
3,533,295✔
1425
      tagData = varDataVal(param->val);
449,738✔
1426
      nTagData = varDataLen(param->val);
444,842✔
1427

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

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

1439
        tagData = buf;
36,663✔
1440
        nTagData = maxSize;
36,663✔
1441
      }
1442
    } else {
1443
      tagData = param->val;
3,088,285✔
1444
      nTagData = tDataTypes[param->type].bytes;
3,090,632✔
1445
    }
1446
  }
1447

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

1452
  int cmp = 0;
3,530,403✔
1453
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,531,875✔
1454

1455
  int     count = 0;
3,539,489✔
1456
  int32_t valid = 0;
3,539,489✔
1457
  bool    found = false;
3,539,489✔
1458

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;
3,539,489✔
1464
  do {
35,045,336✔
1465
    void   *entryKey = NULL, *entryVal = NULL;
38,584,825✔
1466
    int32_t nEntryKey, nEntryVal;
38,580,248✔
1467

1468
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
38,575,014✔
1469
    if (valid < 0) {
38,579,670✔
1470
      break;
1,691,029✔
1471
    }
1472
    if (count > TRY_ERROR_LIMIT) {
36,888,641✔
1473
      break;
1,073,363✔
1474
    }
1475

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

1479
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
35,815,278✔
1480
      if (found == true) break;  //
1,320,138✔
1481
      if (diffCidCount > TRY_ERROR_LIMIT) break;
545,041✔
1482
      diffCidCount++;
545,041✔
1483
      count++;
545,041✔
1484
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
545,041✔
1485
      if (valid < 0) {
544,325✔
1486
        code = valid;
×
1487
        break;
×
1488
      } else {
1489
        continue;
544,325✔
1490
      }
1491
    }
1492

1493
    terrno = TSDB_CODE_SUCCESS;
34,494,137✔
1494
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
34,495,219✔
1495
    if (terrno != TSDB_CODE_SUCCESS) {
34,490,856✔
1496
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1497
      break;
×
1498
    }
1499
    if (cmp == 0) {
34,489,670✔
1500
      // match
1501
      tb_uid_t tuid = 0;
30,998,571✔
1502
      if (IS_VAR_DATA_TYPE(pKey->type)) {
30,998,131✔
1503
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
243,424✔
1504
      } else {
1505
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
30,754,991✔
1506
      }
1507
      if (taosArrayPush(pUids, &tuid) == NULL) {
31,014,289✔
1508
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1509
      }
1510
      found = true;
31,013,849✔
1511
    } else {
1512
      if (param->equal == true) {
3,491,099✔
1513
        if (count > TRY_ERROR_LIMIT) break;
2,410,984✔
1514
        count++;
2,410,984✔
1515
      }
1516
    }
1517
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
34,505,418✔
1518
    if (valid < 0) {
34,502,552✔
1519
      code = valid;
×
1520
      break;
×
1521
    }
1522
  } while (1);
1523

1524
END:
3,530,781✔
1525
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,539,131✔
1526
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,538,415✔
1527
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,536,119✔
1528
  tDecoderClear(&dc);
3,536,119✔
1529
  tdbFree(pData);
3,535,317✔
1530

1531
  taosMemoryFree(buf);
3,536,886✔
1532
  taosMemoryFree(pKey);
3,538,552✔
1533

1534
  taosMemoryFree(pCursor);
3,535,595✔
1535

1536
  return code;
3,535,717✔
1537
}
1538

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

1545
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
38,579,664✔
1546
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
38,582,862✔
1547
  if (lock) {
38,579,055✔
1548
    metaULock(pMeta);
×
1549
  }
1550

1551
  return ret;
38,579,055✔
1552
}
1553

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

1558
  int32_t isLock = false;
5,100,928✔
1559
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
5,100,928✔
1560
  for (int i = 0; i < sz; i++) {
43,680,478✔
1561
    STUidTagInfo *p = taosArrayGet(uidList, i);
38,580,739✔
1562

1563
    if (i % LIMIT == 0) {
38,581,179✔
1564
      if (isLock) metaULock(pMeta);
4,407,231✔
1565

1566
      metaRLock(pMeta);
4,407,231✔
1567
      isLock = true;
4,407,231✔
1568
    }
1569

1570
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1571
    void   *val = NULL;
38,581,179✔
1572
    int32_t len = 0;
38,581,548✔
1573
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
38,581,548✔
1574
      p->pTagVal = taosMemoryMalloc(len);
38,573,821✔
1575
      if (!p->pTagVal) {
38,576,735✔
1576
        if (isLock) metaULock(pMeta);
×
1577

1578
        TAOS_RETURN(terrno);
×
1579
      }
1580
      memcpy(p->pTagVal, val, len);
38,576,091✔
1581
      tdbFree(val);
38,576,072✔
1582
    } else {
1583
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
2,358✔
1584
    }
1585
  }
1586
  //  }
1587
  if (isLock) metaULock(pMeta);
5,099,739✔
1588
  return 0;
5,100,537✔
1589
}
1590

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

1597
  while (1) {
139,059,844✔
1598
    tb_uid_t uid = metaCtbCursorNext(pCur);
158,675,686✔
1599
    if (uid == 0) {
158,666,935✔
1600
      break;
19,616,598✔
1601
    }
1602

1603
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
139,050,337✔
1604
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
139,051,104✔
1605
    if (!info.pTagVal) {
139,011,160✔
1606
      metaCloseCtbCursor(pCur);
×
1607
      return terrno;
×
1608
    }
1609
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
139,011,160✔
1610
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
139,058,983✔
1611
      taosMemoryFreeClear(info.pTagVal);
×
1612
      metaCloseCtbCursor(pCur);
×
1613
      return terrno;
×
1614
    }
1615
  }
1616
  metaCloseCtbCursor(pCur);
19,616,598✔
1617
  return TSDB_CODE_SUCCESS;
19,614,754✔
1618
}
1619

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

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

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

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

1646
  metaCloseStbCursor(pCur);
×
1647

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

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

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

1661
      return code;
×
1662
    }
1663

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

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

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

1674
        return code;
×
1675
      }
1676

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

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

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

1690
          return code;
×
1691
        }
1692
      }
1693

1694
      tDestroyTSchema(pTSchema);
×
1695
    }
1696

1697
    taosArrayDestroy(uids);
×
1698
  }
1699

1700
  taosArrayDestroy(suids);
×
1701

1702
  return TSDB_CODE_SUCCESS;
×
1703
}
1704

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

1707
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
1,540,643,283✔
1708
  int32_t code = 0;
1,540,643,283✔
1709
  void   *pData = NULL;
1,540,643,283✔
1710
  int     nData = 0;
1,540,932,589✔
1711
  int     lock = 0;
1,541,027,191✔
1712

1713
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
1,541,027,191✔
1714
    lock = 1;
345,986,038✔
1715
  }
1716

1717
  if (!lock) metaRLock(pMeta);
1,541,033,042✔
1718

1719
  // search cache
1720
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
1,541,049,643✔
1721
    if (!lock) metaULock(pMeta);
1,455,099,407✔
1722
    goto _exit;
1,455,082,349✔
1723
  }
1724

1725
  // search TDB
1726
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
85,908,410✔
1727
    // not found
1728
    if (!lock) metaULock(pMeta);
77,335,848✔
1729
    goto _exit;
77,326,704✔
1730
  }
1731

1732
  if (!lock) metaULock(pMeta);
8,620,083✔
1733

1734
  pInfo->uid = uid;
8,620,083✔
1735
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
8,619,741✔
1736
  pInfo->version = ((SUidIdxVal *)pData)->version;
8,620,539✔
1737
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
8,620,539✔
1738

1739
  if (lock) {
8,620,539✔
1740
    metaULock(pReader->pMeta);
709,393✔
1741
    // metaReaderReleaseLock(pReader);
1742
  }
1743
  // upsert the cache
1744
  metaWLock(pMeta);
8,620,767✔
1745
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
8,619,251✔
1746
  if (ret != 0) {
8,620,049✔
1747
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1748
  }
1749
  metaULock(pMeta);
8,620,049✔
1750

1751
  if (lock) {
8,620,539✔
1752
    metaRLock(pReader->pMeta);
710,305✔
1753
  }
1754

1755
_exit:
1,538,683,905✔
1756
  tdbFree(pData);
1,541,019,988✔
1757
  return code;
1,540,970,487✔
1758
}
1759

1760
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
173,937,002✔
1761
  int32_t code = 0;
173,937,002✔
1762

1763
  if (!numOfTables && !numOfCols) goto _exit;
173,937,002✔
1764

1765
  SVnode *pVnodeObj = pVnode;
173,937,002✔
1766
  metaRLock(pVnodeObj->pMeta);
173,937,002✔
1767

1768
  // fast path: search cache
1769
  SMetaStbStats state = {0};
173,931,279✔
1770
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
173,933,590✔
1771
    metaULock(pVnodeObj->pMeta);
167,133,184✔
1772
    if (numOfTables) *numOfTables = state.ctbNum;
167,124,963✔
1773
    if (numOfCols) *numOfCols = state.colNum;
167,125,202✔
1774
    if (flags) *flags = state.flags;
167,121,772✔
1775
    goto _exit;
167,121,000✔
1776
  }
1777

1778
  // slow path: search TDB
1779
  int64_t ctbNum = 0;
6,818,054✔
1780
  int32_t colNum = 0;
6,819,380✔
1781
  int64_t keep = 0;
6,819,859✔
1782
  int8_t  flag = 0;
6,819,859✔
1783

1784
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
6,819,915✔
1785
  if (TSDB_CODE_SUCCESS == code) {
6,819,943✔
1786
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
6,819,943✔
1787
  }
1788
  if (TSDB_CODE_SUCCESS == code) {
6,819,687✔
1789
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
6,819,687✔
1790
  }
1791
  metaULock(pVnodeObj->pMeta);
6,819,777✔
1792
  if (TSDB_CODE_SUCCESS != code) {
6,819,777✔
1793
    goto _exit;
×
1794
  }
1795

1796
  if (numOfTables) *numOfTables = ctbNum;
6,819,777✔
1797
  if (numOfCols) *numOfCols = colNum;
6,819,777✔
1798
  if (flags) *flags = flag;
6,819,555✔
1799

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

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

1814
  metaULock(pVnodeObj->pMeta);
6,819,707✔
1815

1816
_exit:
173,940,999✔
1817
  return code;
173,947,693✔
1818
}
1819

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

1823
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
68,240,970✔
1824
    stats.ctbNum += deltaCtb;
57,710,906✔
1825
    stats.colNum += deltaCol;
57,710,906✔
1826
    if (deltaKeep > 0) {
57,710,906✔
1827
      stats.keep = deltaKeep;
10,332✔
1828
    }
1829

1830
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
57,710,906✔
1831
    if (code) {
57,690,628✔
1832
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1833
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1834
    }
1835
  }
1836
}
68,231,409✔
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