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

taosdata / TDengine / #4828

29 Oct 2025 11:10AM UTC coverage: 61.071% (-0.3%) from 61.383%
#4828

push

travis-ci

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

3.0

155924 of 324872 branches covered (48.0%)

Branch coverage included in aggregate %.

207404 of 270051 relevant lines covered (76.8%)

243478715.98 hits per line

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

51.22
/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,147,483,647✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,147,483,647✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,147,483,647✔
23
  pReader->pAPI = pAPI;
2,147,483,647✔
24
}
2,147,483,647✔
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,147,483,647✔
32
  }
33
}
2,147,483,647✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,205,660,017✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,205,660,017!
37
    metaULock(pReader->pMeta);
1,205,933,851✔
38
    pReader->flags |= META_READER_NOLOCK;
1,205,499,689✔
39
  }
40
}
1,205,526,044✔
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,188,967,928✔
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) {
303,240,759✔
76
  SVnode *pVnodeObj = pVnode;
303,240,759✔
77
  metaRLock(pVnodeObj->pMeta);  // query uid.idx
303,240,759✔
78

79
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
303,262,189✔
80
    metaULock(pVnodeObj->pMeta);
846,404✔
81
    return false;
846,404✔
82
  }
83

84
  metaULock(pVnodeObj->pMeta);
302,449,353✔
85
  return true;
302,464,566✔
86
}
87

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

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

97
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
627,763,648✔
98
  return metaGetTableEntryByVersion(pReader, version1, uid);
627,702,966✔
99
}
100

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

104
  SMetaInfo info;
2,147,483,647✔
105
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
2,147,483,647✔
106
  if (TSDB_CODE_SUCCESS != code) {
2,147,483,647✔
107
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
6,002!
108
  }
109

110
  return metaGetTableEntryByVersion(pReader, info.version, uid);
2,147,483,647✔
111
}
112

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

117
  // query name.idx
118
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
206,808,958✔
119
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
38,381,343✔
120
  }
121

122
  uid = *(tb_uid_t *)pReader->pBuf;
168,426,292✔
123
  return metaReaderGetTableEntryByUid(pReader, uid);
168,427,809✔
124
}
125

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

131
  metaRLock(pMeta);
192,215,457✔
132

133
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
192,213,794✔
134
    uid = *(tb_uid_t *)pData;
38,996,659✔
135
    tdbFree(pData);
38,996,659✔
136
  }
137

138
  metaULock(pMeta);
192,197,953✔
139

140
  return uid;
192,191,949✔
141
}
142

143
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
31,419,543✔
144
  int         code = 0;
31,419,543✔
145
  SMetaReader mr = {0};
31,419,543✔
146
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
31,420,817✔
147
  code = metaReaderGetTableEntryByUid(&mr, uid);
31,422,482✔
148
  if (code < 0) {
31,418,312!
149
    metaReaderClear(&mr);
×
150
    return code;
×
151
  }
152

153
  STR_TO_VARSTR(tbName, mr.me.name);
31,418,312!
154
  metaReaderClear(&mr);
31,415,445✔
155

156
  return 0;
31,421,803✔
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) {
2,001,405✔
175
  int         code = 0;
2,001,405✔
176
  SMetaReader mr = {0};
2,001,405✔
177
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
2,002,841✔
178

179
  SMetaReader *pReader = &mr;
2,002,841✔
180

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

187
  *uid = *(tb_uid_t *)pReader->pBuf;
1,007,864✔
188

189
  metaReaderClear(&mr);
1,007,864✔
190

191
  return 0;
1,007,864✔
192
}
193

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

199
  code = metaGetTableEntryByName(&mr, tbName);
1,051,184✔
200
  if (code == 0) *tbType = mr.me.type;
1,051,184!
201
  if (TSDB_CHILD_TABLE == mr.me.type) {
1,051,184✔
202
    *suid = mr.me.ctbEntry.suid;
1,027,220✔
203
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
23,964✔
204
    *suid = mr.me.uid;
14,622✔
205
  } else {
206
    *suid = 0;
9,342✔
207
  }
208

209
  metaReaderClear(&mr);
1,051,184✔
210
  return code;
1,051,184✔
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) {
38,420,551✔
246
  SMTbCursor *pTbCur = NULL;
38,420,551✔
247
  int32_t     code;
248

249
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
38,420,551!
250
  if (pTbCur == NULL) {
38,402,800!
251
    return NULL;
×
252
  }
253

254
  SVnode *pVnodeObj = pVnode;
38,402,800✔
255
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
256
  pTbCur->pMeta = pVnodeObj->pMeta;
38,402,800✔
257
  pTbCur->paused = 1;
38,410,970✔
258
  code = metaResumeTbCursor(pTbCur, 1, 0);
38,410,196✔
259
  if (code) {
38,418,482!
260
    terrno = code;
×
261
    taosMemoryFree(pTbCur);
×
262
    return NULL;
×
263
  }
264
  return pTbCur;
38,418,482✔
265
}
266

267
void metaCloseTbCursor(SMTbCursor *pTbCur) {
76,771,271✔
268
  if (pTbCur) {
76,771,271✔
269
    tdbFree(pTbCur->pKey);
38,426,305✔
270
    tdbFree(pTbCur->pVal);
38,420,329✔
271
    if (!pTbCur->paused) {
38,420,701✔
272
      metaReaderClear(&pTbCur->mr);
13,403,779✔
273
      if (pTbCur->pDbc) {
13,405,224!
274
        tdbTbcClose((TBC *)pTbCur->pDbc);
13,405,224✔
275
      }
276
    }
277
    taosMemoryFree(pTbCur);
38,422,308!
278
  }
279
}
76,765,414✔
280

281
void metaPauseTbCursor(SMTbCursor *pTbCur) {
25,670,406✔
282
  if (!pTbCur->paused) {
25,670,406!
283
    metaReaderClear(&pTbCur->mr);
25,670,406✔
284
    tdbTbcClose((TBC *)pTbCur->pDbc);
25,669,175✔
285
    pTbCur->paused = 1;
25,671,475✔
286
  }
287
}
25,671,475✔
288
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
39,075,217✔
289
  int32_t code = 0;
39,075,217✔
290
  int32_t lino;
291
  int8_t  locked = 0;
39,075,217✔
292
  if (pTbCur->paused) {
39,075,217!
293
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
39,064,076✔
294
    locked = 1;
39,065,935✔
295
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
39,065,935✔
296
    if (code != 0) {
39,065,427!
297
      TSDB_CHECK_CODE(code, lino, _exit);
×
298
    }
299

300
    if (first) {
39,065,427✔
301
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
38,415,075✔
302
      TSDB_CHECK_CODE(code, lino, _exit);
38,396,825!
303
    } else {
304
      int c = 1;
650,352✔
305
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
650,352✔
306
      TSDB_CHECK_CODE(code, lino, _exit);
650,352!
307
      if (c == 0) {
650,352!
308
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
650,352!
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;
39,048,272✔
319
  }
320

321
_exit:
×
322
  if (code != 0 && locked) {
39,073,175!
323
    metaReaderReleaseLock(&pTbCur->mr);
×
324
  }
325
  return code;
39,070,289✔
326
}
327

328
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
896,478,817✔
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);
896,478,817✔
335
    if (ret < 0) {
896,605,669✔
336
      return ret;
38,364,423✔
337
    }
338

339
    tDecoderClear(&pTbCur->mr.coder);
858,241,246✔
340

341
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
858,311,007✔
342
    if (ret) return ret;
857,834,351!
343

344
    if (pTbCur->mr.me.type == jumpTableType) {
857,834,351✔
345
      continue;
11,316,157✔
346
    }
347

348
    break;
846,650,654✔
349
  }
350

351
  return 0;
846,650,654✔
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,
1,067,127,272✔
386
                                   int8_t type) {
387
  int32_t         code = 0;
1,067,127,272✔
388
  void           *pData = NULL;
1,067,127,272✔
389
  int             nData = 0;
1,066,604,929✔
390
  int64_t         version;
391
  SSchemaWrapper  schema = {0};
1,066,580,523✔
392
  SSchemaWrapper *pSchema = NULL;
1,066,649,663✔
393
  SDecoder        dc = {0};
1,066,649,663✔
394
  if (lock) {
1,067,133,081✔
395
    metaRLock(pMeta);
1,044,129,229✔
396
  }
397
_query:
1,155,812,008✔
398
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
1,155,915,429✔
399
    goto _err;
1,188,188✔
400
  }
401

402
  version = ((SUidIdxVal *)pData)[0].version;
1,154,977,506✔
403

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

409
  SMetaEntry me = {0};
1,154,982,593✔
410
  tDecoderInit(&dc, pData, nData);
1,154,926,859✔
411
  code = metaDecodeEntry(&dc, &me);
1,155,050,729✔
412
  if (code) {
1,154,524,514!
413
    tDecoderClear(&dc);
×
414
    goto _err;
×
415
  }
416
  if (me.type == TSDB_SUPER_TABLE) {
1,154,524,514✔
417
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
866,088,493✔
418
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
865,872,293✔
419
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
865,872,293✔
420
      if (TABLE_IS_ROLLUP(me.flags)) {
865,878,211✔
421
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
151,280!
422
          tDecoderClear(&dc);
×
423
          goto _err;
×
424
        }
425
      }
426
      tDecoderClear(&dc);
865,878,211✔
427
      goto _exit;
865,849,329✔
428
    }
429
  } else if (me.type == TSDB_CHILD_TABLE) {
288,436,021✔
430
    uid = me.ctbEntry.suid;
88,679,128✔
431
    tDecoderClear(&dc);
88,679,128✔
432
    goto _query;
88,690,678✔
433
  } else {
434
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
199,756,893✔
435
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
199,818,325✔
436
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
199,818,325✔
437
      tDecoderClear(&dc);
199,868,845✔
438
      goto _exit;
199,909,520✔
439
    }
440
  }
441
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
87,723✔
442
  tDecoderClear(&dc);
87,723✔
443

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

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

460
_exit:
1,065,977,929✔
461
  if (lock) {
1,066,157,256✔
462
    metaULock(pMeta);
1,043,055,458✔
463
  }
464
  tdbFree(pData);
1,065,856,651✔
465
  return pSchema;
1,065,789,961✔
466

467
_err:
1,088,045✔
468
  if (lock) {
1,187,336✔
469
    metaULock(pMeta);
1,186,129✔
470
  }
471
  tdbFree(pData);
1,185,616✔
472
  tDeleteSchemaWrapper(pSchema);
473
  if (extSchema != NULL) {
1,185,616✔
474
    taosMemoryFreeClear(*extSchema);
556,086!
475
  }
476
  terrno = code;
1,186,484✔
477
  return NULL;
1,187,320✔
478
}
479

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

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

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

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

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

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

522
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
625,034,211✔
523
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
625,034,211✔
524
  SMCtbCursor *pCtbCur = NULL;
625,206,377✔
525
  SCtbIdxKey   ctbIdxKey;
526
  int          ret = 0;
625,206,377✔
527
  int          c = 0;
625,206,377✔
528

529
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
625,206,377✔
530
  if (pCtbCur == NULL) {
624,753,683!
531
    return NULL;
×
532
  }
533

534
  pCtbCur->pMeta = pMeta;
624,753,683✔
535
  pCtbCur->suid = uid;
624,813,884✔
536
  pCtbCur->lock = lock;
624,807,179✔
537
  pCtbCur->paused = 1;
624,827,545✔
538

539
  ret = metaResumeCtbCursor(pCtbCur, 1);
624,664,034✔
540
  if (ret < 0) {
625,050,674!
541
    return NULL;
×
542
  }
543
  return pCtbCur;
625,050,674✔
544
}
545

546
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
625,335,392✔
547
  if (pCtbCur) {
625,335,392!
548
    if (!pCtbCur->paused) {
625,376,966✔
549
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
595,471,913✔
550
      if (pCtbCur->pCur) {
595,512,025!
551
        tdbTbcClose(pCtbCur->pCur);
595,529,544✔
552
      }
553
    }
554
    tdbFree(pCtbCur->pKey);
625,243,725✔
555
    tdbFree(pCtbCur->pVal);
625,067,544✔
556
  }
557
  taosMemoryFree(pCtbCur);
625,049,710✔
558
}
625,011,916✔
559

560
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
29,858,460✔
561
  if (!pCtbCur->paused) {
29,858,460✔
562
    tdbTbcClose((TBC *)pCtbCur->pCur);
29,858,594✔
563
    if (pCtbCur->lock) {
29,850,687!
564
      metaULock(pCtbCur->pMeta);
29,858,234✔
565
    }
566
    pCtbCur->paused = 1;
29,851,499✔
567
  }
568
}
29,863,001✔
569

570
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
625,052,478✔
571
  if (pCtbCur->paused) {
625,052,478✔
572
    pCtbCur->paused = 0;
624,909,419✔
573

574
    if (pCtbCur->lock) {
624,668,191✔
575
      metaRLock(pCtbCur->pMeta);
602,030,777✔
576
    }
577
    int ret = 0;
624,304,913✔
578
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
624,304,913✔
579
    if (ret < 0) {
624,997,858!
580
      metaCloseCtbCursor(pCtbCur);
×
581
      return -1;
×
582
    }
583

584
    if (first) {
624,997,858!
585
      SCtbIdxKey ctbIdxKey;
624,991,706✔
586
      // move to the suid
587
      ctbIdxKey.suid = pCtbCur->suid;
625,031,259✔
588
      ctbIdxKey.uid = INT64_MIN;
624,970,478✔
589
      int c = 0;
624,970,478✔
590
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
624,848,867✔
591
      if (c > 0) {
625,094,530✔
592
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
64,458,595✔
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;
625,103,241✔
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;
276,372,100✔
614
  }
615

616
  pCtbIdxKey = pCtbCur->pKey;
2,147,483,647✔
617
  if (pCtbIdxKey->suid > pCtbCur->suid) {
2,147,483,647✔
618
    return 0;
349,573,834✔
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) {
199,952,153✔
635
  SMStbCursor *pStbCur = NULL;
199,952,153✔
636
  int          ret = 0;
199,952,153✔
637
  int          c = 0;
199,952,153✔
638

639
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
199,949,107!
640
  if (pStbCur == NULL) {
199,955,199!
641
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
642
    return NULL;
×
643
  }
644

645
  pStbCur->pMeta = pMeta;
199,955,199✔
646
  pStbCur->suid = suid;
199,939,969✔
647
  metaRLock(pMeta);
199,953,676✔
648

649
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
199,956,697✔
650
  if (ret < 0) {
199,923,518!
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);
199,923,518✔
659
  if (c > 0) {
199,961,291!
660
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
661
  }
662

663
  return pStbCur;
199,956,722✔
664
}
665

666
void metaCloseStbCursor(SMStbCursor *pStbCur) {
199,952,153✔
667
  if (pStbCur) {
199,952,153!
668
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
199,953,676!
669
    if (pStbCur->pCur) {
199,952,153✔
670
      tdbTbcClose(pStbCur->pCur);
199,950,630✔
671

672
      tdbFree(pStbCur->pKey);
199,942,737✔
673
      tdbFree(pStbCur->pVal);
199,952,116✔
674
    }
675

676
    taosMemoryFree(pStbCur);
199,953,676!
677
  }
678
}
199,937,527✔
679

680
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
596,906,242✔
681
  int ret;
682

683
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
596,906,242✔
684
  if (ret < 0) {
596,936,024✔
685
    return 0;
199,962,777✔
686
  }
687
  return *(tb_uid_t *)pStbCur->pKey;
396,973,247✔
688
}
689

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

694
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
919,532,575✔
695
  if (!pSW) return NULL;
919,814,681✔
696

697
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
919,299,964✔
698

699
  tDeleteSchemaWrapper(pSW);
700
  return pTSchema;
918,573,912✔
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) {
42,160✔
707
  SRSchema       *pRSchema = NULL;
42,160✔
708
  SSchemaWrapper *pSW = NULL;
42,160✔
709

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

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

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

725
_exit:
42,160!
726
  tDeleteSchemaWrapper(pSW);
727
  return pRSchema;
42,160✔
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) {
3,162,376✔
735
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
3,162,376✔
736
  if (*ppTSchema == NULL) {
3,162,376!
737
    return terrno;
×
738
  }
739
  return TSDB_CODE_SUCCESS;
3,162,376✔
740
}
741

742
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
901,599,828✔
743
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
901,599,828✔
744
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
900,992,537!
745
    return terrno;
×
746
  }
747
  return TSDB_CODE_SUCCESS;
901,109,792✔
748
}
749

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

754
  void     *pData = NULL;
376,741,035✔
755
  int       nData = 0;
376,837,308✔
756
  SSkmDbKey skmDbKey;
376,881,136✔
757
  if (sver <= 0) {
376,898,756✔
758
    SMetaInfo info;
164,091,476✔
759
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
164,110,545✔
760
      sver = info.skmVer;
163,908,605✔
761
    } else {
762
      TBC *pSkmDbC = NULL;
173,193✔
763
      int  c;
94,010✔
764

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

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

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

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

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

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

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

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

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

814
  skmDbKey.uid = suid ? suid : uid;
376,858,467✔
815
  skmDbKey.sver = sver;
376,858,467✔
816
  metaRLock(pMeta);
376,858,467✔
817
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
376,925,025✔
818
    metaULock(pMeta);
6,076✔
819
    code = TSDB_CODE_NOT_FOUND;
6,076✔
820
    goto _exit;
6,076✔
821
  }
822
  metaULock(pMeta);
376,861,590✔
823

824
  // decode
825
  SDecoder        dc = {0};
376,896,746✔
826
  SSchemaWrapper  schema;
376,781,200✔
827
  SSchemaWrapper *pSchemaWrapper = &schema;
376,819,408✔
828

829
  tDecoderInit(&dc, pData, nData);
376,819,408✔
830
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
377,023,843✔
831
  tDecoderClear(&dc);
377,023,843✔
832
  tdbFree(pData);
376,904,024✔
833
  if (TSDB_CODE_SUCCESS != code) {
376,675,026!
834
    taosMemoryFree(pSchemaWrapper->pSchema);
×
835
    goto _exit;
×
836
  }
837

838
  // convert
839
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
376,675,026✔
840
  if (pTSchema == NULL) {
376,873,244!
841
    code = TSDB_CODE_OUT_OF_MEMORY;
×
842
  }
843

844
  *ppTSchema = pTSchema;
376,873,244✔
845
  taosMemoryFree(pSchemaWrapper->pSchema);
376,951,546✔
846

847
_exit:
376,775,820✔
848
  return code;
376,802,908✔
849
}
850

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

861
void metaUpdTimeSeriesNum(SMeta *pMeta) {
199,946,840✔
862
  int64_t nCtbTimeSeries = 0;
199,946,840✔
863
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
199,948,363!
864
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
199,953,536✔
865
  }
866
}
199,957,501✔
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;
657,686,002✔
871
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
657,697,142✔
872
    metaUpdTimeSeriesNum(pMeta);
199,931,654✔
873
  }
874

875
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
657,726,867✔
876
}
877

878
// type: 1 reported timeseries
879
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
657,650,767!
880
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
657,692,834✔
881
  if (type == 1) {
657,692,834✔
882
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
459,857,571✔
883
  }
884
  return nTimeSeries;
657,659,103✔
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;
4,098,612✔
1147
  }
1148
  bool find = tTagGet(tag, val);
2,147,483,647✔
1149

1150
  if (!find) {
2,147,483,647✔
1151
    return NULL;
42,119,401✔
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) {
×
1170
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1171
  SMetaFltParam *param = arg;
×
1172
  int32_t        ret = 0;
×
1173

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

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

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

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

1199
  int32_t valid = 0;
×
1200
  int32_t count = 0;
×
1201

1202
  static const int8_t TRY_ERROR_LIMIT = 1;
1203
  do {
×
1204
    void   *entryKey = NULL;
×
1205
    int32_t nEntryKey = -1;
×
1206
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
×
1207
    if (valid < 0) break;
×
1208

1209
    SBtimeIdxKey *p = entryKey;
×
1210
    if (count > TRY_ERROR_LIMIT) break;
×
1211

1212
    terrno = TSDB_CODE_SUCCESS;
×
1213
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
×
1214
    if (terrno != TSDB_CODE_SUCCESS) {
×
1215
      ret = terrno;
×
1216
      break;
×
1217
    }
1218
    if (cmp == 0) {
×
1219
      if (taosArrayPush(pUids, &p->uid) == NULL) {
×
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);
×
1230
    if (valid < 0) break;
×
1231
  } while (1);
1232

1233
END:
×
1234
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1235
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1236
  taosMemoryFree(pCursor);
×
1237
  return ret;
×
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) {
9,481,497✔
1353
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
9,481,497✔
1354
  SMetaFltParam *param = arg;
9,492,479✔
1355

1356
  SMetaEntry oStbEntry = {0};
9,492,479✔
1357
  int32_t    code = 0;
9,474,078✔
1358
  char      *buf = NULL;
9,474,078✔
1359
  void      *pData = NULL;
9,474,078✔
1360
  int        nData = 0;
9,489,715✔
1361

1362
  SDecoder    dc = {0};
9,491,686✔
1363
  STbDbKey    tbDbKey = {0};
9,476,664✔
1364
  STagIdxKey *pKey = NULL;
9,486,631✔
1365
  int32_t     nKey = 0;
9,470,666✔
1366

1367
  SIdxCursor *pCursor = NULL;
9,469,504✔
1368
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
9,469,504!
1369
  if (!pCursor) {
9,476,798!
1370
    return terrno;
×
1371
  }
1372
  pCursor->pMeta = pMeta;
9,476,798✔
1373
  pCursor->suid = param->suid;
9,476,798✔
1374
  pCursor->cid = param->cid;
9,488,957✔
1375
  pCursor->type = param->type;
9,462,163✔
1376

1377
  metaRLock(pMeta);
9,487,809✔
1378

1379
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
9,477,217!
1380

1381
  tbDbKey.uid = param->suid;
9,492,293✔
1382
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
9,484,547✔
1383

1384
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
9,485,490!
1385

1386
  tDecoderInit(&dc, pData, nData);
9,498,770✔
1387

1388
  code = metaDecodeEntry(&dc, &oStbEntry);
9,495,935✔
1389
  if (code) {
9,482,588!
1390
    tDecoderClear(&dc);
×
1391
    goto END;
×
1392
  }
1393

1394
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
9,482,588!
1395
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
1,259!
1396
  }
1397

1398
  code = TSDB_CODE_INVALID_PARA;
9,482,588✔
1399

1400
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
13,103,422✔
1401
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
13,099,550✔
1402
    if (IS_IDX_ON(schema)) {
13,099,085✔
1403
      if (schema->colId == param->cid && param->type == schema->type) {
13,086,024✔
1404
        code = 0;
9,478,241✔
1405
        break;
9,478,241✔
1406
      }
1407
    }
1408
  }
1409

1410
  TAOS_CHECK_GOTO(code, NULL, END);
9,482,113!
1411

1412
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
9,482,113✔
1413
  if (code != 0) {
9,481,645!
1414
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1415
  }
1416

1417
  int32_t maxSize = 0;
9,481,645✔
1418
  int32_t nTagData = 0;
9,475,470✔
1419
  void   *tagData = NULL;
9,475,470✔
1420

1421
  if (param->val == NULL) {
9,475,470!
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)) {
9,480,259!
1426
      tagData = varDataVal(param->val);
1,517,192✔
1427
      nTagData = varDataLen(param->val);
1,512,897✔
1428

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

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

1440
        tagData = buf;
209,474✔
1441
        nTagData = maxSize;
209,474✔
1442
      }
1443
    } else {
1444
      tagData = param->val;
7,965,497✔
1445
      nTagData = tDataTypes[param->type].bytes;
7,975,140✔
1446
    }
1447
  }
1448

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

1453
  int cmp = 0;
9,479,512✔
1454
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
9,487,837!
1455

1456
  int     count = 0;
9,500,107✔
1457
  int32_t valid = 0;
9,500,107✔
1458
  bool    found = false;
9,500,107✔
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;
9,500,107✔
1465
  do {
103,629,260✔
1466
    void   *entryKey = NULL, *entryVal = NULL;
113,129,367✔
1467
    int32_t nEntryKey, nEntryVal;
113,126,785✔
1468

1469
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
113,124,014✔
1470
    if (valid < 0) {
113,141,285✔
1471
      break;
4,797,727✔
1472
    }
1473
    if (count > TRY_ERROR_LIMIT) {
108,343,558✔
1474
      break;
2,590,550✔
1475
    }
1476

1477
    STagIdxKey *p = entryKey;
105,753,008✔
1478
    if (p == NULL) break;
105,753,008!
1479

1480
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
105,753,008✔
1481
      if (found == true) break;  //
2,904,670✔
1482
      if (diffCidCount > TRY_ERROR_LIMIT) break;
792,587!
1483
      diffCidCount++;
792,587✔
1484
      count++;
792,587✔
1485
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
792,587!
1486
      if (valid < 0) {
792,587!
1487
        code = valid;
×
1488
        break;
×
1489
      } else {
1490
        continue;
792,587✔
1491
      }
1492
    }
1493

1494
    terrno = TSDB_CODE_SUCCESS;
102,837,400✔
1495
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
102,834,943✔
1496
    if (terrno != TSDB_CODE_SUCCESS) {
102,826,052!
1497
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1498
      break;
×
1499
    }
1500
    if (cmp == 0) {
102,833,460✔
1501
      // match
1502
      tb_uid_t tuid = 0;
92,954,280✔
1503
      if (IS_VAR_DATA_TYPE(pKey->type)) {
92,955,603!
1504
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
1,230,483✔
1505
      } else {
1506
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
91,732,306✔
1507
      }
1508
      if (taosArrayPush(pUids, &tuid) == NULL) {
92,964,490!
1509
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1510
      }
1511
      found = true;
92,960,710✔
1512
    } else {
1513
      if (param->equal == true) {
9,879,180!
1514
        if (count > TRY_ERROR_LIMIT) break;
6,386,140!
1515
        count++;
6,386,140✔
1516
      }
1517
    }
1518
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
102,839,890!
1519
    if (valid < 0) {
102,825,303!
1520
      code = valid;
×
1521
      break;
×
1522
    }
1523
  } while (1);
1524

1525
END:
9,487,979✔
1526
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
9,500,296!
1527
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
9,499,980✔
1528
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
9,494,690!
1529
  tDecoderClear(&dc);
9,494,690✔
1530
  tdbFree(pData);
9,491,833✔
1531

1532
  taosMemoryFree(buf);
9,496,013!
1533
  taosMemoryFree(pKey);
9,500,097!
1534

1535
  taosMemoryFree(pCursor);
9,496,040!
1536

1537
  return code;
9,491,604✔
1538
}
1539

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

1546
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
89,395,642✔
1547
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
89,396,965✔
1548
  if (lock) {
89,387,704!
1549
    metaULock(pMeta);
×
1550
  }
1551

1552
  return ret;
89,387,704✔
1553
}
1554

1555
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
8,027,718✔
1556
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
8,027,718✔
1557
  const int32_t LIMIT = 128;
8,030,680✔
1558

1559
  int32_t isLock = false;
8,030,680✔
1560
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
8,030,680!
1561
  for (int i = 0; i < sz; i++) {
97,418,448✔
1562
    STUidTagInfo *p = taosArrayGet(uidList, i);
89,387,704✔
1563

1564
    if (i % LIMIT == 0) {
89,394,315!
1565
      if (isLock) metaULock(pMeta);
6,476,806✔
1566

1567
      metaRLock(pMeta);
6,476,806✔
1568
      isLock = true;
6,475,483✔
1569
    }
1570

1571
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1572
    void   *val = NULL;
89,392,992✔
1573
    int32_t len = 0;
89,391,673✔
1574
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
89,392,996!
1575
      p->pTagVal = taosMemoryMalloc(len);
89,387,704!
1576
      if (!p->pTagVal) {
89,395,642!
1577
        if (isLock) metaULock(pMeta);
×
1578

1579
        TAOS_RETURN(terrno);
×
1580
      }
1581
      memcpy(p->pTagVal, val, len);
89,393,060!
1582
      tdbFree(val);
89,394,383✔
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);
8,030,744✔
1589
  return 0;
8,029,421✔
1590
}
1591

1592
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
80,318,546✔
1593
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
80,318,546✔
1594
  if (!pCur) {
80,297,529!
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;
80,297,529✔
1602
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
80,297,529✔
1603
  if (numOfElems > 0) {
80,305,506✔
1604
    pSepecifiedUidMap =
1605
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
28,693,261!
1606
    for (int i = 0; i < numOfElems; i++) {
190,080,297✔
1607
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
161,379,811✔
1608
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
161,381,810✔
1609
      if (code) {
161,380,810!
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
80,319,205✔
1618
    while (1) {
455,900,714✔
1619
      tb_uid_t uid = metaCtbCursorNext(pCur);
507,519,823✔
1620
      if (uid == 0) {
507,728,789✔
1621
        break;
51,651,935✔
1622
      }
1623

1624
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
456,076,854✔
1625
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
456,056,451!
1626
      if (!info.pTagVal) {
455,768,789!
1627
        metaCloseCtbCursor(pCur);
×
1628
        taosHashCleanup(pSepecifiedUidMap);
×
1629
        return terrno;
×
1630
      }
1631
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
455,768,789!
1632
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
455,894,649!
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) {
215,666,578✔
1641
      tb_uid_t uid = metaCtbCursorNext(pCur);
244,366,674✔
1642
      if (uid == 0) {
244,348,296✔
1643
        break;
28,701,608✔
1644
      }
1645

1646
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
215,646,688✔
1647
      if (index == NULL) {
215,680,884✔
1648
        continue;
54,299,200✔
1649
      }
1650

1651
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
161,381,684✔
1652
      if (pTagInfo->pTagVal == NULL) {
161,379,433!
1653
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
161,380,696!
1654
        if (!pTagInfo->pTagVal) {
161,351,376!
1655
          metaCloseCtbCursor(pCur);
×
1656
          taosHashCleanup(pSepecifiedUidMap);
×
1657
          return terrno;
×
1658
        }
1659
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
161,359,388!
1660
      }
1661
    }
1662
  }
1663

1664
  taosHashCleanup(pSepecifiedUidMap);
80,348,549✔
1665
  metaCloseCtbCursor(pCur);
80,346,595✔
1666
  return TSDB_CODE_SUCCESS;
80,337,210✔
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;
2,147,483,647✔
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) {
281,457,757✔
1776
    // not found
1777
    if (!lock) metaULock(pMeta);
267,578,353✔
1778
    goto _exit;
267,520,991✔
1779
  }
1780

1781
  if (!lock) metaULock(pMeta);
13,925,896✔
1782

1783
  pInfo->uid = uid;
13,925,896✔
1784
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
13,923,996✔
1785
  pInfo->version = ((SUidIdxVal *)pData)->version;
13,925,914✔
1786
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
13,926,886✔
1787

1788
  if (lock) {
13,925,675✔
1789
    metaULock(pReader->pMeta);
2,973,684✔
1790
    // metaReaderReleaseLock(pReader);
1791
  }
1792
  // upsert the cache
1793
  metaWLock(pMeta);
13,926,419✔
1794
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
13,925,657✔
1795
  if (ret != 0) {
13,926,868!
1796
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1797
  }
1798
  metaULock(pMeta);
13,926,868✔
1799

1800
  if (lock) {
13,926,842✔
1801
    metaRLock(pReader->pMeta);
2,977,679✔
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) {
599,227,699✔
1810
  int32_t code = 0;
599,227,699✔
1811

1812
  if (!numOfTables && !numOfCols) goto _exit;
599,227,699!
1813

1814
  SVnode *pVnodeObj = pVnode;
599,227,699✔
1815
  metaRLock(pVnodeObj->pMeta);
599,227,699✔
1816

1817
  // fast path: search cache
1818
  SMetaStbStats state = {0};
599,204,480✔
1819
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
599,237,125✔
1820
    metaULock(pVnodeObj->pMeta);
576,252,927✔
1821
    if (numOfTables) *numOfTables = state.ctbNum;
576,254,394✔
1822
    if (numOfCols) *numOfCols = state.colNum;
576,250,526✔
1823
    if (flags) *flags = state.flags;
576,230,110✔
1824
    goto _exit;
576,228,587✔
1825
  }
1826

1827
  // slow path: search TDB
1828
  int64_t ctbNum = 0;
22,993,318✔
1829
  int32_t colNum = 0;
22,998,056✔
1830
  int64_t keep = 0;
22,999,875✔
1831
  int8_t  flag = 0;
22,996,996✔
1832

1833
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
22,999,941✔
1834
  if (TSDB_CODE_SUCCESS == code) {
23,002,266!
1835
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
23,002,266✔
1836
  }
1837
  if (TSDB_CODE_SUCCESS == code) {
23,004,878!
1838
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
23,004,878✔
1839
  }
1840
  metaULock(pVnodeObj->pMeta);
23,003,984✔
1841
  if (TSDB_CODE_SUCCESS != code) {
23,003,102!
1842
    goto _exit;
×
1843
  }
1844

1845
  if (numOfTables) *numOfTables = ctbNum;
23,003,102✔
1846
  if (numOfCols) *numOfCols = colNum;
23,003,102✔
1847
  if (flags) *flags = flag;
23,004,786✔
1848

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

1857
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
23,001,411✔
1858
  if (ret) {
23,003,102!
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);
23,003,102✔
1864

1865
_exit:
599,232,603✔
1866
  return code;
599,250,141✔
1867
}
1868

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

1872
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
209,439,273✔
1873
    stats.ctbNum += deltaCtb;
190,136,296✔
1874
    stats.colNum += deltaCol;
190,136,296✔
1875
    if (deltaKeep > 0) {
190,136,296✔
1876
      stats.keep = deltaKeep;
24,321✔
1877
    }
1878

1879
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
190,136,296✔
1880
    if (code) {
190,064,415!
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
}
209,408,132✔
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