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

taosdata / TDengine / #4824

27 Oct 2025 05:39AM UTC coverage: 61.409% (+0.2%) from 61.242%
#4824

push

travis-ci

web-flow
Merge pull request #33376 from taosdata/3.0

merge 3.0

156919 of 324854 branches covered (48.3%)

Branch coverage included in aggregate %.

371 of 493 new or added lines in 25 files covered. (75.25%)

2822 existing lines in 108 files now uncovered.

208404 of 270043 relevant lines covered (77.17%)

229356519.53 hits per line

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

54.21
/source/dnode/vnode/src/meta/metaQuery.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "meta.h"
17
#include "osMemory.h"
18
#include "tencode.h"
19

20
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
2,120,508,391✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,120,508,391✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,121,355,680✔
23
  pReader->pAPI = pAPI;
2,121,353,647✔
24
}
2,121,274,159✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
2,147,483,647✔
27
  memset(pReader, 0, sizeof(*pReader));
2,147,483,647!
28
  pReader->pMeta = pMeta;
2,147,483,647✔
29
  pReader->flags = flags;
2,147,483,647✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
2,147,483,647✔
31
    metaRLock(pMeta);
2,091,798,525✔
32
  }
33
}
2,147,483,647✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,007,570,115✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,007,570,115✔
37
    metaULock(pReader->pMeta);
1,008,275,550✔
38
    pReader->flags |= META_READER_NOLOCK;
1,007,269,475✔
39
  }
40
}
1,007,663,150✔
41

42
void metaReaderClear(SMetaReader *pReader) {
2,147,483,647✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,147,483,647✔
44
    metaULock(pReader->pMeta);
1,083,176,092✔
45
  }
46
  tDecoderClear(&pReader->coder);
2,147,483,647✔
47
  tdbFree(pReader->pBuf);
2,147,483,647✔
48
  pReader->pBuf = NULL;
2,147,483,647✔
49
}
2,147,483,647✔
50

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

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
2,147,483,647!
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);
2,147,483,647✔
63
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
2,147,483,647✔
64

65
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
2,147,483,647✔
66
  if (code) {
2,147,483,647!
67
    tDecoderClear(&pReader->coder);
×
68
    return code;
×
69
  }
70
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
71

72
  return 0;
2,147,483,647✔
73
}
74

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

79
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
260,940,345✔
80
    metaULock(pVnodeObj->pMeta);
749,426✔
81
    return false;
749,426✔
82
  }
83

84
  metaULock(pVnodeObj->pMeta);
260,270,002✔
85
  return true;
260,289,359✔
86
}
87

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

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

97
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
664,848,631✔
98
  return metaGetTableEntryByVersion(pReader, version1, uid);
664,826,854✔
99
}
100

101
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
1,845,208,050✔
102
  SMeta *pMeta = pReader->pMeta;
1,845,208,050✔
103

104
  SMetaInfo info;
1,845,734,682✔
105
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
1,845,741,864✔
106
  if (TSDB_CODE_SUCCESS != code) {
1,845,305,182✔
107
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
2,336!
108
  }
109

110
  return metaGetTableEntryByVersion(pReader, info.version, uid);
1,845,302,846✔
111
}
112

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

117
  // query name.idx
118
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
216,452,087✔
119
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
58,362,405✔
120
  }
121

122
  uid = *(tb_uid_t *)pReader->pBuf;
158,086,866✔
123
  return metaReaderGetTableEntryByUid(pReader, uid);
158,087,281✔
124
}
125

126
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
196,664,699✔
127
  void    *pData = NULL;
196,664,699✔
128
  int      nData = 0;
196,668,259✔
129
  tb_uid_t uid = 0;
196,670,423✔
130

131
  metaRLock(pMeta);
196,670,423✔
132

133
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
196,652,274✔
134
    uid = *(tb_uid_t *)pData;
35,361,638✔
135
    tdbFree(pData);
35,361,638✔
136
  }
137

138
  metaULock(pMeta);
196,637,275✔
139

140
  return uid;
196,631,058✔
141
}
142

143
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
29,235,750✔
144
  int         code = 0;
29,235,750✔
145
  SMetaReader mr = {0};
29,235,750✔
146
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
29,236,648✔
147
  code = metaReaderGetTableEntryByUid(&mr, uid);
29,226,065✔
148
  if (code < 0) {
29,237,554!
149
    metaReaderClear(&mr);
×
150
    return code;
×
151
  }
152

153
  STR_TO_VARSTR(tbName, mr.me.name);
29,237,554!
154
  metaReaderClear(&mr);
29,222,649✔
155

156
  return 0;
29,238,914✔
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) {
1,717,122✔
175
  int         code = 0;
1,717,122✔
176
  SMetaReader mr = {0};
1,717,122✔
177
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
1,717,122✔
178

179
  SMetaReader *pReader = &mr;
1,717,122✔
180

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

187
  *uid = *(tb_uid_t *)pReader->pBuf;
839,725✔
188

189
  metaReaderClear(&mr);
839,725✔
190

191
  return 0;
839,725✔
192
}
193

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

199
  code = metaGetTableEntryByName(&mr, tbName);
880,412✔
200
  if (code == 0) *tbType = mr.me.type;
879,144!
201
  if (TSDB_CHILD_TABLE == mr.me.type) {
879,144✔
202
    *suid = mr.me.ctbEntry.suid;
858,310✔
203
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
20,834✔
204
    *suid = mr.me.uid;
13,374✔
205
  } else {
206
    *suid = 0;
7,460✔
207
  }
208

209
  metaReaderClear(&mr);
877,877✔
210
  return code;
879,144✔
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) {
34,130,413✔
246
  SMTbCursor *pTbCur = NULL;
34,130,413✔
247
  int32_t     code;
248

249
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
34,130,413!
250
  if (pTbCur == NULL) {
34,116,834!
251
    return NULL;
×
252
  }
253

254
  SVnode *pVnodeObj = pVnode;
34,116,834✔
255
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
256
  pTbCur->pMeta = pVnodeObj->pMeta;
34,116,834✔
257
  pTbCur->paused = 1;
34,130,356✔
258
  code = metaResumeTbCursor(pTbCur, 1, 0);
34,126,698✔
259
  if (code) {
34,114,738!
260
    terrno = code;
×
261
    taosMemoryFree(pTbCur);
×
262
    return NULL;
×
263
  }
264
  return pTbCur;
34,114,738✔
265
}
266

267
void metaCloseTbCursor(SMTbCursor *pTbCur) {
68,204,965✔
268
  if (pTbCur) {
68,204,965✔
269
    tdbFree(pTbCur->pKey);
34,139,115✔
270
    tdbFree(pTbCur->pVal);
34,136,680✔
271
    if (!pTbCur->paused) {
34,134,988✔
272
      metaReaderClear(&pTbCur->mr);
12,691,715✔
273
      if (pTbCur->pDbc) {
12,690,353✔
274
        tdbTbcClose((TBC *)pTbCur->pDbc);
12,688,986✔
275
      }
276
    }
277
    taosMemoryFree(pTbCur);
34,136,632!
278
  }
279
}
68,201,870✔
280

281
void metaPauseTbCursor(SMTbCursor *pTbCur) {
21,971,528✔
282
  if (!pTbCur->paused) {
21,971,528!
283
    metaReaderClear(&pTbCur->mr);
21,971,528✔
284
    tdbTbcClose((TBC *)pTbCur->pDbc);
21,970,615✔
285
    pTbCur->paused = 1;
21,972,045✔
286
  }
287
}
21,971,528✔
288
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
34,647,712✔
289
  int32_t code = 0;
34,647,712✔
290
  int32_t lino;
291
  int8_t  locked = 0;
34,647,712✔
292
  if (pTbCur->paused) {
34,647,712!
293
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
34,647,562✔
294
    locked = 1;
34,649,048✔
295
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
34,649,048✔
296
    if (code != 0) {
34,636,267!
297
      TSDB_CHECK_CODE(code, lino, _exit);
×
298
    }
299

300
    if (first) {
34,636,267✔
301
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
34,114,843✔
302
      TSDB_CHECK_CODE(code, lino, _exit);
34,104,129!
303
    } else {
304
      int c = 1;
521,424✔
305
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
521,424✔
306
      TSDB_CHECK_CODE(code, lino, _exit);
521,424!
307
      if (c == 0) {
521,424!
308
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
521,424!
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;
34,632,884✔
319
  }
320

321
_exit:
×
322
  if (code != 0 && locked) {
34,637,503!
323
    metaReaderReleaseLock(&pTbCur->mr);
×
324
  }
325
  return code;
34,633,064✔
326
}
327

328
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
788,853,379✔
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);
788,853,379✔
335
    if (ret < 0) {
788,938,870✔
336
      return ret;
34,086,431✔
337
    }
338

339
    tDecoderClear(&pTbCur->mr.coder);
754,852,439✔
340

341
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
755,093,455✔
342
    if (ret) return ret;
754,230,078!
343

344
    if (pTbCur->mr.me.type == jumpTableType) {
754,230,078✔
345
      continue;
9,866,746✔
346
    }
347

348
    break;
744,860,055✔
349
  }
350

351
  return 0;
744,860,055✔
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,
911,184,994✔
386
                                   int8_t type) {
387
  int32_t         code = 0;
911,184,994✔
388
  void           *pData = NULL;
911,184,994✔
389
  int             nData = 0;
910,904,071✔
390
  int64_t         version;
391
  SSchemaWrapper  schema = {0};
911,030,620✔
392
  SSchemaWrapper *pSchema = NULL;
910,605,718✔
393
  SDecoder        dc = {0};
910,605,718✔
394
  if (lock) {
911,230,893✔
395
    metaRLock(pMeta);
891,432,893✔
396
  }
397
_query:
985,564,723✔
398
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
985,824,509✔
399
    goto _err;
973,734✔
400
  }
401

402
  version = ((SUidIdxVal *)pData)[0].version;
985,018,590✔
403

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

409
  SMetaEntry me = {0};
985,001,961✔
410
  tDecoderInit(&dc, pData, nData);
984,841,280✔
411
  code = metaDecodeEntry(&dc, &me);
985,022,212✔
412
  if (code) {
983,960,038!
413
    tDecoderClear(&dc);
×
414
    goto _err;
×
415
  }
416
  if (me.type == TSDB_SUPER_TABLE) {
983,960,038✔
417
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
740,248,247✔
418
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
740,070,380✔
419
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
740,070,380✔
420
      if (TABLE_IS_ROLLUP(me.flags)) {
740,080,995✔
421
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
138,498!
422
          tDecoderClear(&dc);
×
423
          goto _err;
×
424
        }
425
      }
426
      tDecoderClear(&dc);
740,080,995✔
427
      goto _exit;
739,947,137✔
428
    }
429
  } else if (me.type == TSDB_CHILD_TABLE) {
243,711,791✔
430
    uid = me.ctbEntry.suid;
73,741,513✔
431
    tDecoderClear(&dc);
73,741,513✔
432
    goto _query;
73,784,012✔
433
  } else {
434
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
169,970,278✔
435
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
170,112,314✔
436
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
170,112,314✔
437
      tDecoderClear(&dc);
170,140,070✔
438
      goto _exit;
170,206,874✔
439
    }
440
  }
441
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
88,408✔
442
  tDecoderClear(&dc);
88,408✔
443

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

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

460
_exit:
910,461,510✔
461
  if (lock) {
910,823,677✔
462
    metaULock(pMeta);
890,982,293✔
463
  }
464
  tdbFree(pData);
910,281,845✔
465
  return pSchema;
910,654,483✔
466

467
_err:
606,029✔
468
  if (lock) {
973,734!
469
    metaULock(pMeta);
973,734✔
470
  }
471
  tdbFree(pData);
973,037✔
472
  tDeleteSchemaWrapper(pSchema);
473
  if (extSchema != NULL) {
973,037✔
474
    taosMemoryFreeClear(*extSchema);
465,270!
475
  }
476
  terrno = code;
973,037✔
477
  return NULL;
973,037✔
478
}
479

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

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

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

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

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

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

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

529
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
524,485,458✔
530
  if (pCtbCur == NULL) {
523,322,577!
531
    return NULL;
×
532
  }
533

534
  pCtbCur->pMeta = pMeta;
523,322,577✔
535
  pCtbCur->suid = uid;
523,533,240✔
536
  pCtbCur->lock = lock;
523,719,907✔
537
  pCtbCur->paused = 1;
523,935,695✔
538

539
  ret = metaResumeCtbCursor(pCtbCur, 1);
523,829,879✔
540
  if (ret < 0) {
524,535,667!
541
    return NULL;
×
542
  }
543
  return pCtbCur;
524,535,667✔
544
}
545

546
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
525,201,301✔
547
  if (pCtbCur) {
525,201,301!
548
    if (!pCtbCur->paused) {
525,254,445✔
549
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
501,064,029✔
550
      if (pCtbCur->pCur) {
501,111,990✔
551
        tdbTbcClose(pCtbCur->pCur);
501,124,919✔
552
      }
553
    }
554
    tdbFree(pCtbCur->pKey);
525,040,390✔
555
    tdbFree(pCtbCur->pVal);
524,704,881✔
556
  }
557
  taosMemoryFree(pCtbCur);
524,749,726✔
558
}
524,689,673✔
559

560
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
24,154,574✔
561
  if (!pCtbCur->paused) {
24,154,574✔
562
    tdbTbcClose((TBC *)pCtbCur->pCur);
24,159,090✔
563
    if (pCtbCur->lock) {
24,153,582!
564
      metaULock(pCtbCur->pMeta);
24,156,673✔
565
    }
566
    pCtbCur->paused = 1;
24,144,227✔
567
  }
568
}
24,167,250✔
569

570
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
524,488,993✔
571
  if (pCtbCur->paused) {
524,488,993✔
572
    pCtbCur->paused = 0;
524,455,319✔
573

574
    if (pCtbCur->lock) {
523,894,249✔
575
      metaRLock(pCtbCur->pMeta);
504,554,090✔
576
    }
577
    int ret = 0;
523,893,516✔
578
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
523,893,516✔
579
    if (ret < 0) {
524,390,011!
580
      metaCloseCtbCursor(pCtbCur);
×
581
      return -1;
×
582
    }
583

584
    if (first) {
524,390,011!
585
      SCtbIdxKey ctbIdxKey;
524,385,451✔
586
      // move to the suid
587
      ctbIdxKey.suid = pCtbCur->suid;
524,480,619✔
588
      ctbIdxKey.uid = INT64_MIN;
524,686,280✔
589
      int c = 0;
524,686,280✔
590
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
524,311,733✔
591
      if (c > 0) {
524,637,728✔
592
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
55,480,801✔
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;
524,612,391✔
605
}
606

607
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
2,147,483,647✔
608
  int         ret;
609
  SCtbIdxKey *pCtbIdxKey;
610

611
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
2,147,483,647✔
612
  if (ret < 0) {
2,147,483,647✔
613
    return 0;
239,046,661✔
614
  }
615

616
  pCtbIdxKey = pCtbCur->pKey;
2,147,483,647✔
617
  if (pCtbIdxKey->suid > pCtbCur->suid) {
2,147,483,647✔
618
    return 0;
286,636,552✔
619
  }
620

621
  return pCtbIdxKey->uid;
2,147,483,647✔
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) {
163,220,095✔
635
  SMStbCursor *pStbCur = NULL;
163,220,095✔
636
  int          ret = 0;
163,220,095✔
637
  int          c = 0;
163,220,095✔
638

639
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
163,220,095!
640
  if (pStbCur == NULL) {
163,218,692!
641
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
642
    return NULL;
×
643
  }
644

645
  pStbCur->pMeta = pMeta;
163,218,692✔
646
  pStbCur->suid = suid;
163,213,080✔
647
  metaRLock(pMeta);
163,221,498✔
648

649
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
163,221,498✔
650
  if (ret < 0) {
163,215,155!
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);
163,215,155✔
659
  if (c > 0) {
163,221,498!
660
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
661
  }
662

663
  return pStbCur;
163,219,364✔
664
}
665

666
void metaCloseStbCursor(SMStbCursor *pStbCur) {
163,220,767✔
667
  if (pStbCur) {
163,220,767!
668
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
163,220,767!
669
    if (pStbCur->pCur) {
163,222,170✔
670
      tdbTbcClose(pStbCur->pCur);
163,220,767✔
671

672
      tdbFree(pStbCur->pKey);
163,221,498✔
673
      tdbFree(pStbCur->pVal);
163,219,364✔
674
    }
675

676
    taosMemoryFree(pStbCur);
163,220,767!
677
  }
678
}
163,213,752✔
679

680
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
435,648,035✔
681
  int ret;
682

683
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
435,648,035✔
684
  if (ret < 0) {
435,651,112✔
685
    return 0;
163,222,170✔
686
  }
687
  return *(tb_uid_t *)pStbCur->pKey;
272,428,942✔
688
}
689

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

694
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
788,161,769✔
695
  if (!pSW) return NULL;
788,630,250✔
696

697
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
788,216,221✔
698

699
  tDeleteSchemaWrapper(pSW);
700
  return pTSchema;
787,625,926✔
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) {
38,284✔
707
  SRSchema       *pRSchema = NULL;
38,284✔
708
  SSchemaWrapper *pSW = NULL;
38,284✔
709

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

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

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

725
_exit:
38,284!
726
  tDeleteSchemaWrapper(pSW);
727
  return pRSchema;
38,284✔
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) {
2,260,314✔
735
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
2,260,314✔
736
  if (*ppTSchema == NULL) {
2,260,314!
737
    return terrno;
×
738
  }
739
  return TSDB_CODE_SUCCESS;
2,259,389✔
740
}
741

742
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
763,579,704✔
743
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
763,579,704✔
744
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
763,052,903!
745
    return terrno;
×
746
  }
747
  return TSDB_CODE_SUCCESS;
763,191,817✔
748
}
749

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

754
  void     *pData = NULL;
313,235,046✔
755
  int       nData = 0;
313,384,332✔
756
  SSkmDbKey skmDbKey;
313,429,011✔
757
  if (sver <= 0) {
313,368,054✔
758
    SMetaInfo info;
136,734,871✔
759
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
136,765,282✔
760
      sver = info.skmVer;
136,581,861✔
761
    } else {
762
      TBC *pSkmDbC = NULL;
176,409✔
763
      int  c;
77,010✔
764

765
      skmDbKey.uid = suid ? suid : uid;
77,010!
766
      skmDbKey.sver = INT32_MAX;
77,010✔
767

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

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

779
      if (c == 0) {
77,010!
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) {
77,010!
UNCOV
788
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
789
      }
790

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

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

802
      sver = ((SSkmDbKey *)pKey)->sver;
77,010✔
803

804
      metaULock(pMeta);
77,010✔
805
      tdbTbcClose(pSkmDbC);
77,010✔
806
    }
807
  }
808

809
  if (!(sver > 0)) {
313,346,003!
810
    code = TSDB_CODE_NOT_FOUND;
×
811
    goto _exit;
×
812
  }
813

814
  skmDbKey.uid = suid ? suid : uid;
313,346,003✔
815
  skmDbKey.sver = sver;
313,346,003✔
816
  metaRLock(pMeta);
313,346,003✔
817
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
313,461,829✔
818
    metaULock(pMeta);
5,576✔
819
    code = TSDB_CODE_NOT_FOUND;
5,576✔
820
    goto _exit;
5,576✔
821
  }
822
  metaULock(pMeta);
313,374,115✔
823

824
  // decode
825
  SDecoder        dc = {0};
313,460,225✔
826
  SSchemaWrapper  schema;
313,269,942✔
827
  SSchemaWrapper *pSchemaWrapper = &schema;
313,342,266✔
828

829
  tDecoderInit(&dc, pData, nData);
313,342,266✔
830
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
313,630,142✔
831
  tDecoderClear(&dc);
313,630,142✔
832
  tdbFree(pData);
313,443,799✔
833
  if (TSDB_CODE_SUCCESS != code) {
313,125,836!
834
    taosMemoryFree(pSchemaWrapper->pSchema);
×
835
    goto _exit;
×
836
  }
837

838
  // convert
839
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
313,125,836✔
840
  if (pTSchema == NULL) {
313,479,689!
841
    code = TSDB_CODE_OUT_OF_MEMORY;
×
842
  }
843

844
  *ppTSchema = pTSchema;
313,479,689✔
845
  taosMemoryFree(pSchemaWrapper->pSchema);
313,553,271✔
846

847
_exit:
313,323,121✔
848
  return code;
313,319,706✔
849
}
850

851
// N.B. Called by statusReq per second
852
int64_t metaGetTbNum(SMeta *pMeta) {
361,563,362✔
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;
361,563,362✔
859
}
860

861
void metaUpdTimeSeriesNum(SMeta *pMeta) {
163,217,985✔
862
  int64_t nCtbTimeSeries = 0;
163,217,985✔
863
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
163,217,985!
864
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
163,214,046✔
865
  }
866
}
163,218,986✔
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;
562,291,581✔
871
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
562,295,104✔
872
    metaUpdTimeSeriesNum(pMeta);
163,199,892✔
873
  }
874

875
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
562,310,181✔
876
}
877

878
// type: 1 reported timeseries
879
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
562,258,780!
880
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
562,279,401✔
881
  if (type == 1) {
562,279,401✔
882
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
361,563,362✔
883
  }
884
  return nTimeSeries;
562,270,429✔
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) {
2,147,483,647✔
1144
  STag *tag = (STag *)pTag;
2,147,483,647✔
1145
  if (type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
1146
    return tag;
3,744,085✔
1147
  }
1148
  bool find = tTagGet(tag, val);
2,147,483,647✔
1149

1150
  if (!find) {
2,147,483,647✔
1151
    return NULL;
35,061,085✔
1152
  }
1153

1154
  return val;
2,147,483,647✔
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) {
6,744✔
1170
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6,744✔
1171
  SMetaFltParam *param = arg;
6,744✔
1172
  int32_t        ret = 0;
6,744✔
1173

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

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

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

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

1199
  int32_t valid = 0;
6,744✔
1200
  int32_t count = 0;
6,744✔
1201

1202
  static const int8_t TRY_ERROR_LIMIT = 1;
1203
  do {
44,583,460✔
1204
    void   *entryKey = NULL;
44,590,204✔
1205
    int32_t nEntryKey = -1;
44,513,772✔
1206
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
44,583,460✔
1207
    if (valid < 0) break;
44,928,528✔
1208

1209
    SBtimeIdxKey *p = entryKey;
44,921,784✔
1210
    if (count > TRY_ERROR_LIMIT) break;
44,921,784!
1211

1212
    terrno = TSDB_CODE_SUCCESS;
44,921,784✔
1213
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
44,909,420✔
1214
    if (terrno != TSDB_CODE_SUCCESS) {
44,659,892!
1215
      ret = terrno;
×
1216
      break;
×
1217
    }
1218
    if (cmp == 0) {
44,484,548!
1219
      if (taosArrayPush(pUids, &p->uid) == NULL) {
89,318,660!
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);
44,834,112!
1230
    if (valid < 0) break;
44,629,544!
1231
  } while (1);
1232

1233
END:
11,240✔
1234
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6,744!
1235
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6,744!
1236
  taosMemoryFree(pCursor);
6,744!
1237
  return ret;
6,744✔
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) {
8,813,456✔
1353
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
8,813,456✔
1354
  SMetaFltParam *param = arg;
8,841,638✔
1355

1356
  SMetaEntry oStbEntry = {0};
8,841,638✔
1357
  int32_t    code = 0;
8,824,299✔
1358
  char      *buf = NULL;
8,824,299✔
1359
  void      *pData = NULL;
8,824,299✔
1360
  int        nData = 0;
8,829,024✔
1361

1362
  SDecoder    dc = {0};
8,832,535✔
1363
  STbDbKey    tbDbKey = {0};
8,805,568✔
1364
  STagIdxKey *pKey = NULL;
8,802,138✔
1365
  int32_t     nKey = 0;
8,801,530✔
1366

1367
  SIdxCursor *pCursor = NULL;
8,790,519✔
1368
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
8,790,519!
1369
  if (!pCursor) {
8,794,727!
1370
    return terrno;
×
1371
  }
1372
  pCursor->pMeta = pMeta;
8,794,727✔
1373
  pCursor->suid = param->suid;
8,788,147✔
1374
  pCursor->cid = param->cid;
8,823,655✔
1375
  pCursor->type = param->type;
8,797,724✔
1376

1377
  metaRLock(pMeta);
8,834,196✔
1378

1379
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
8,819,988!
1380

1381
  tbDbKey.uid = param->suid;
8,847,967✔
1382
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
8,823,307✔
1383

1384
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
8,830,098!
1385

1386
  tDecoderInit(&dc, pData, nData);
8,852,053✔
1387

1388
  code = metaDecodeEntry(&dc, &oStbEntry);
8,844,851✔
1389
  if (code) {
8,832,916!
1390
    tDecoderClear(&dc);
×
1391
    goto END;
×
1392
  }
1393

1394
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
8,832,916!
1395
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1396
  }
1397

1398
  code = TSDB_CODE_INVALID_PARA;
8,832,916✔
1399

1400
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
12,142,715✔
1401
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
12,145,958✔
1402
    if (IS_IDX_ON(schema)) {
12,152,117✔
1403
      if (schema->colId == param->cid && param->type == schema->type) {
12,138,234✔
1404
        code = 0;
8,839,902✔
1405
        break;
8,839,902✔
1406
      }
1407
    }
1408
  }
1409

1410
  TAOS_CHECK_GOTO(code, NULL, END);
8,836,659!
1411

1412
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
8,836,659✔
1413
  if (code != 0) {
8,835,610!
1414
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1415
  }
1416

1417
  int32_t maxSize = 0;
8,835,610✔
1418
  int32_t nTagData = 0;
8,824,421✔
1419
  void   *tagData = NULL;
8,824,421✔
1420

1421
  if (param->val == NULL) {
8,824,421!
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)) {
8,825,784!
1426
      tagData = varDataVal(param->val);
1,384,600✔
1427
      nTagData = varDataLen(param->val);
1,359,390✔
1428

1429
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
1,364,088✔
1430
        maxSize = 4 * nTagData + 1;
169,283✔
1431
        buf = taosMemoryCalloc(1, maxSize);
169,283!
1432
        if (buf == NULL) {
169,283!
1433
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1434
        }
1435

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

1440
        tagData = buf;
169,283✔
1441
        nTagData = maxSize;
169,283✔
1442
      }
1443
    } else {
1444
      tagData = param->val;
7,453,672✔
1445
      nTagData = tDataTypes[param->type].bytes;
7,471,670✔
1446
    }
1447
  }
1448

1449
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
8,831,575!
1450
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1451
                  NULL, END);
1452

1453
  int cmp = 0;
8,810,395✔
1454
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
8,830,999!
1455

1456
  int     count = 0;
8,855,178✔
1457
  int32_t valid = 0;
8,855,178✔
1458
  bool    found = false;
8,855,178✔
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;
8,855,178✔
1465
  do {
94,597,924✔
1466
    void   *entryKey = NULL, *entryVal = NULL;
103,453,102✔
1467
    int32_t nEntryKey, nEntryVal;
103,460,200✔
1468

1469
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
103,460,960✔
1470
    if (valid < 0) {
103,490,808✔
1471
      break;
4,512,141✔
1472
    }
1473
    if (count > TRY_ERROR_LIMIT) {
98,978,667✔
1474
      break;
2,262,295✔
1475
    }
1476

1477
    STagIdxKey *p = entryKey;
96,716,372✔
1478
    if (p == NULL) break;
96,716,372!
1479

1480
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
96,716,372✔
1481
      if (found == true) break;  //
2,948,064✔
1482
      if (diffCidCount > TRY_ERROR_LIMIT) break;
863,143!
1483
      diffCidCount++;
863,143✔
1484
      count++;
863,143✔
1485
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
863,143!
1486
      if (valid < 0) {
861,996!
1487
        code = valid;
×
1488
        break;
×
1489
      } else {
1490
        continue;
861,996✔
1491
      }
1492
    }
1493

1494
    terrno = TSDB_CODE_SUCCESS;
93,743,112✔
1495
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
93,743,139✔
1496
    if (terrno != TSDB_CODE_SUCCESS) {
93,725,717!
1497
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1498
      break;
×
1499
    }
1500
    if (cmp == 0) {
93,725,508✔
1501
      // match
1502
      tb_uid_t tuid = 0;
84,906,478✔
1503
      if (IS_VAR_DATA_TYPE(pKey->type)) {
84,895,930!
1504
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
1,060,364✔
1505
      } else {
1506
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
83,860,684✔
1507
      }
1508
      if (taosArrayPush(pUids, &tuid) == NULL) {
84,929,231!
1509
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1510
      }
1511
      found = true;
84,922,983✔
1512
    } else {
1513
      if (param->equal == true) {
8,819,030!
1514
        if (count > TRY_ERROR_LIMIT) break;
5,699,568!
1515
        count++;
5,699,568✔
1516
      }
1517
    }
1518
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
93,752,208!
1519
    if (valid < 0) {
93,727,863!
1520
      code = valid;
×
1521
      break;
×
1522
    }
1523
  } while (1);
1524

1525
END:
8,861,740✔
1526
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
8,859,172✔
1527
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
8,858,025!
1528
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
8,854,276!
1529
  tDecoderClear(&dc);
8,854,276✔
1530
  tdbFree(pData);
8,850,322✔
1531

1532
  taosMemoryFree(buf);
8,855,724!
1533
  taosMemoryFree(pKey);
8,854,081!
1534

1535
  taosMemoryFree(pCursor);
8,849,366!
1536

1537
  return code;
8,848,262✔
1538
}
1539

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

1546
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
81,927,157✔
1547
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
81,928,369✔
1548
  if (lock) {
81,916,241!
1549
    metaULock(pMeta);
×
1550
  }
1551

1552
  return ret;
81,916,241✔
1553
}
1554

1555
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
7,534,569✔
1556
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
7,534,569✔
1557
  const int32_t LIMIT = 128;
7,537,034✔
1558

1559
  int32_t isLock = false;
7,537,034✔
1560
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
7,537,034!
1561
  for (int i = 0; i < sz; i++) {
89,458,690✔
1562
    STUidTagInfo *p = taosArrayGet(uidList, i);
81,925,478✔
1563

1564
    if (i % LIMIT == 0) {
81,925,940!
1565
      if (isLock) metaULock(pMeta);
5,951,105✔
1566

1567
      metaRLock(pMeta);
5,951,105✔
1568
      isLock = true;
5,951,105✔
1569
    }
1570

1571
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1572
    void   *val = NULL;
81,925,940✔
1573
    int32_t len = 0;
81,924,731✔
1574
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
81,925,943!
1575
      p->pTagVal = taosMemoryMalloc(len);
81,918,665!
1576
      if (!p->pTagVal) {
81,924,444!
1577
        if (isLock) metaULock(pMeta);
×
1578

1579
        TAOS_RETURN(terrno);
×
1580
      }
1581
      memcpy(p->pTagVal, val, len);
81,919,586!
1582
      tdbFree(val);
81,918,369✔
1583
    } else {
1584
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
×
1585
    }
1586
  }
1587
  //  }
1588
  if (isLock) metaULock(pMeta);
7,533,212✔
1589
  return 0;
7,537,034✔
1590
}
1591

1592
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
69,042,472✔
1593
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
69,042,472✔
1594
  if (!pCur) {
68,998,606!
1595
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1596
  }
1597

1598
  // If len > 0 means there already have uids, and we only want the
1599
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1600
  // in the hash map, that may require a lot of memory
1601
  SHashObj *pSepecifiedUidMap = NULL;
68,998,606✔
1602
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
68,998,606✔
1603
  if (numOfElems > 0) {
69,005,260✔
1604
    pSepecifiedUidMap =
1605
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
25,031,862!
1606
    for (int i = 0; i < numOfElems; i++) {
161,727,336✔
1607
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
136,688,558✔
1608
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
136,687,377✔
1609
      if (code) {
136,688,409!
1610
        metaCloseCtbCursor(pCur);
×
1611
        taosHashCleanup(pSepecifiedUidMap);
×
1612
        return code;
×
1613
      }
1614
    }
1615
  }
1616

1617
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
69,027,158✔
1618
    while (1) {
368,352,124✔
1619
      tb_uid_t uid = metaCtbCursorNext(pCur);
412,338,819✔
1620
      if (uid == 0) {
412,281,727✔
1621
        break;
44,079,364✔
1622
      }
1623

1624
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
368,202,363✔
1625
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
368,126,125!
1626
      if (!info.pTagVal) {
367,938,724!
1627
        metaCloseCtbCursor(pCur);
×
1628
        taosHashCleanup(pSepecifiedUidMap);
×
1629
        return terrno;
×
1630
      }
1631
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
367,938,724!
1632
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
368,359,358!
1633
        taosMemoryFreeClear(info.pTagVal);
×
1634
        metaCloseCtbCursor(pCur);
×
1635
        taosHashCleanup(pSepecifiedUidMap);
×
1636
        return terrno;
×
1637
      }
1638
    }
1639
  } else {  // only the specified tables need to be added
1640
    while (1) {
180,845,211✔
1641
      tb_uid_t uid = metaCtbCursorNext(pCur);
205,885,674✔
1642
      if (uid == 0) {
205,952,776✔
1643
        break;
25,041,453✔
1644
      }
1645

1646
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
180,911,323✔
1647
      if (index == NULL) {
180,855,960✔
1648
        continue;
44,176,861✔
1649
      }
1650

1651
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
136,679,099✔
1652
      if (pTagInfo->pTagVal == NULL) {
136,680,455✔
1653
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
136,679,457!
1654
        if (!pTagInfo->pTagVal) {
136,644,091!
1655
          metaCloseCtbCursor(pCur);
×
1656
          taosHashCleanup(pSepecifiedUidMap);
×
1657
          return terrno;
×
1658
        }
1659
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
136,653,156!
1660
      }
1661
    }
1662
  }
1663

1664
  taosHashCleanup(pSepecifiedUidMap);
69,116,440✔
1665
  metaCloseCtbCursor(pCur);
69,115,280✔
1666
  return TSDB_CODE_SUCCESS;
69,078,146✔
1667
}
1668

1669
int32_t metaFlagCache(SVnode *pVnode) {
×
1670
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1671
  if (!pCur) {
×
1672
    return terrno;
×
1673
  }
1674

1675
  SArray *suids = NULL;
×
1676
  while (1) {
×
1677
    tb_uid_t id = metaStbCursorNext(pCur);
×
1678
    if (id == 0) {
×
1679
      break;
×
1680
    }
1681

1682
    if (!suids) {
×
1683
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1684
      if (!suids) {
×
1685
        return terrno;
×
1686
      }
1687
    }
1688

1689
    if (taosArrayPush(suids, &id) == NULL) {
×
1690
      taosArrayDestroy(suids);
×
1691
      return terrno;
×
1692
    }
1693
  }
1694

1695
  metaCloseStbCursor(pCur);
×
1696

1697
  for (int idx = 0; suids && idx < TARRAY_SIZE(suids); ++idx) {
×
1698
    tb_uid_t id = ((tb_uid_t *)TARRAY_DATA(suids))[idx];
×
1699
    STsdb   *pTsdb = pVnode->pTsdb;
×
1700
    SMeta   *pMeta = pVnode->pMeta;
×
1701
    SArray  *uids = NULL;
×
1702

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

1707
      taosArrayDestroy(uids);
×
1708
      taosArrayDestroy(suids);
×
1709

1710
      return code;
×
1711
    }
1712

1713
    if (uids && TARRAY_SIZE(uids) > 0) {
×
1714
      STSchema *pTSchema = NULL;
×
1715

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

1720
        taosArrayDestroy(uids);
×
1721
        taosArrayDestroy(suids);
×
1722

1723
        return code;
×
1724
      }
1725

1726
      int32_t nCol = pTSchema->numOfCols;
×
1727
      for (int32_t i = 0; i < nCol; ++i) {
×
1728
        int16_t cid = pTSchema->columns[i].colId;
×
1729
        int8_t  col_type = pTSchema->columns[i].type;
×
1730

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

1735
          tDestroyTSchema(pTSchema);
×
1736
          taosArrayDestroy(uids);
×
1737
          taosArrayDestroy(suids);
×
1738

1739
          return code;
×
1740
        }
1741
      }
1742

1743
      tDestroyTSchema(pTSchema);
×
1744
    }
1745

1746
    taosArrayDestroy(uids);
×
1747
  }
1748

1749
  taosArrayDestroy(suids);
×
1750

1751
  return TSDB_CODE_SUCCESS;
×
1752
}
1753

1754
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1755

1756
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
2,147,483,647✔
1757
  int32_t code = 0;
2,147,483,647✔
1758
  void   *pData = NULL;
2,147,483,647✔
1759
  int     nData = 0;
2,147,483,647✔
1760
  int     lock = 0;
2,147,483,647✔
1761

1762
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
2,147,483,647!
1763
    lock = 1;
1,845,704,515✔
1764
  }
1765

1766
  if (!lock) metaRLock(pMeta);
2,147,483,647✔
1767

1768
  // search cache
1769
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
2,147,483,647✔
1770
    if (!lock) metaULock(pMeta);
2,147,483,647✔
1771
    goto _exit;
2,147,483,647✔
1772
  }
1773

1774
  // search TDB
1775
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
277,504,406✔
1776
    // not found
1777
    if (!lock) metaULock(pMeta);
266,088,648✔
1778
    goto _exit;
266,050,790✔
1779
  }
1780

1781
  if (!lock) metaULock(pMeta);
11,633,165✔
1782

1783
  pInfo->uid = uid;
11,633,165✔
1784
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
11,630,426✔
1785
  pInfo->version = ((SUidIdxVal *)pData)->version;
11,630,426✔
1786
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
11,629,339✔
1787

1788
  if (lock) {
11,630,426✔
1789
    metaULock(pReader->pMeta);
2,521,703✔
1790
    // metaReaderReleaseLock(pReader);
1791
  }
1792
  // upsert the cache
1793
  metaWLock(pMeta);
11,632,852✔
1794
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
11,631,765✔
1795
  if (ret != 0) {
11,631,030!
1796
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1797
  }
1798
  metaULock(pMeta);
11,631,030✔
1799

1800
  if (lock) {
11,629,630✔
1801
    metaRLock(pReader->pMeta);
2,523,333✔
1802
  }
1803

1804
_exit:
2,147,483,647✔
1805
  tdbFree(pData);
2,147,483,647✔
1806
  return code;
2,147,483,647✔
1807
}
1808

1809
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
477,158,516✔
1810
  int32_t code = 0;
477,158,516✔
1811

1812
  if (!numOfTables && !numOfCols) goto _exit;
477,158,516!
1813

1814
  SVnode *pVnodeObj = pVnode;
477,158,516✔
1815
  metaRLock(pVnodeObj->pMeta);
477,158,516✔
1816

1817
  // fast path: search cache
1818
  SMetaStbStats state = {0};
477,166,205✔
1819
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
477,184,693✔
1820
    metaULock(pVnodeObj->pMeta);
457,392,994✔
1821
    if (numOfTables) *numOfTables = state.ctbNum;
457,371,270✔
1822
    if (numOfCols) *numOfCols = state.colNum;
457,369,932✔
1823
    if (flags) *flags = state.flags;
457,343,016✔
1824
    goto _exit;
457,343,345✔
1825
  }
1826

1827
  // slow path: search TDB
1828
  int64_t ctbNum = 0;
19,793,001✔
1829
  int32_t colNum = 0;
19,796,116✔
1830
  int64_t keep = 0;
19,797,416✔
1831
  int8_t  flag = 0;
19,797,416✔
1832

1833
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
19,795,173✔
1834
  if (TSDB_CODE_SUCCESS == code) {
19,799,117!
1835
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
19,799,117✔
1836
  }
1837
  if (TSDB_CODE_SUCCESS == code) {
19,798,096!
1838
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
19,798,096✔
1839
  }
1840
  metaULock(pVnodeObj->pMeta);
19,800,539✔
1841
  if (TSDB_CODE_SUCCESS != code) {
19,798,774!
1842
    goto _exit;
×
1843
  }
1844

1845
  if (numOfTables) *numOfTables = ctbNum;
19,798,774✔
1846
  if (numOfCols) *numOfCols = colNum;
19,798,774✔
1847
  if (flags) *flags = flag;
19,799,373✔
1848

1849
  state.uid = uid;
19,799,373✔
1850
  state.ctbNum = ctbNum;
19,799,373✔
1851
  state.colNum = colNum;
19,799,373✔
1852
  state.flags = flag;
19,799,373✔
1853
  state.keep = keep;
19,799,373✔
1854
  // upsert the cache
1855
  metaWLock(pVnodeObj->pMeta);
19,799,373✔
1856

1857
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
19,798,167✔
1858
  if (ret) {
19,798,841!
1859
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1860
              uid, ctbNum, colNum, keep, flag);
1861
  }
1862

1863
  metaULock(pVnodeObj->pMeta);
19,798,841✔
1864

1865
_exit:
477,139,755✔
1866
  return code;
477,200,677✔
1867
}
1868

1869
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
211,001,931✔
1870
  SMetaStbStats stats = {0};
211,001,931✔
1871

1872
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
211,024,836✔
1873
    stats.ctbNum += deltaCtb;
194,131,617✔
1874
    stats.colNum += deltaCol;
194,131,617✔
1875
    if (deltaKeep > 0) {
194,131,617✔
1876
      stats.keep = deltaKeep;
9,549✔
1877
    }
1878

1879
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
194,131,617✔
1880
    if (code) {
194,085,268!
1881
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1882
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1883
    }
1884
  }
1885
}
211,000,737✔
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