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

taosdata / TDengine / #5081

17 May 2026 01:15AM UTC coverage: 73.368% (-0.04%) from 73.403%
#5081

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281582 of 383795 relevant lines covered (73.37%)

137177705.18 hits per line

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

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

16
#include "meta.h"
17
#include "tencode.h"
18
#include "thash.h"
19
#include "types.h"
20

21
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
906,704,876✔
22
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
906,704,876✔
23
  metaReaderDoInit(pReader, pMeta, flags);
906,876,813✔
24
  pReader->pAPI = pAPI;
906,851,785✔
25
}
906,855,091✔
26

27
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
1,048,686,495✔
28
  memset(pReader, 0, sizeof(*pReader));
1,048,686,495✔
29
  pReader->pMeta = pMeta;
1,048,686,495✔
30
  pReader->flags = flags;
1,048,745,217✔
31
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
1,048,787,823✔
32
    metaRLock(pMeta);
898,673,430✔
33
  }
34
}
1,048,844,515✔
35

36
void metaReaderReleaseLock(SMetaReader *pReader) {
74,240,561✔
37
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
74,240,561✔
38
    metaULock(pReader->pMeta);
74,226,909✔
39
    pReader->flags |= META_READER_NOLOCK;
74,214,048✔
40
  }
41
}
74,287,161✔
42

43
void metaReaderClear(SMetaReader *pReader) {
1,298,239,790✔
44
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,298,239,790✔
45
    metaULock(pReader->pMeta);
824,390,446✔
46
  }
47
  tDecoderClear(&pReader->coder);
1,298,208,521✔
48
  tdbFree(pReader->pBuf);
1,298,586,009✔
49
  pReader->pBuf = NULL;
1,298,358,543✔
50
}
1,298,290,989✔
51

52
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
1,641,847,872✔
53
  int32_t  code = 0;
1,641,847,872✔
54
  SMeta   *pMeta = pReader->pMeta;
1,641,847,872✔
55
  STbDbKey tbDbKey = {.version = version, .uid = uid};
1,642,068,406✔
56

57
  // query table.db
58
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
1,642,070,078✔
59
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
60
  }
61

62
  // decode the entry
63
  tDecoderClear(&pReader->coder);
1,641,831,503✔
64
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
1,641,848,746✔
65

66
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
1,641,942,072✔
67
  if (code) {
1,641,610,016✔
68
    tDecoderClear(&pReader->coder);
×
69
    return code;
×
70
  }
71
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
72

73
  return 0;
1,641,610,016✔
74
}
75

76
bool metaIsTableExist(void *pVnode, tb_uid_t uid) {
164,168,570✔
77
  SVnode *pVnodeObj = pVnode;
164,168,570✔
78
  metaRLock(pVnodeObj->pMeta);  // query uid.idx
164,168,570✔
79

80
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
164,167,475✔
81
    metaULock(pVnodeObj->pMeta);
53,981✔
82
    return false;
53,981✔
83
  }
84

85
  metaULock(pVnodeObj->pMeta);
164,137,732✔
86
  return true;
164,140,860✔
87
}
88

89
int metaReaderGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) {
274,650,415✔
90
  SMeta  *pMeta = pReader->pMeta;
274,650,415✔
91
  int64_t version1;
92

93
  // query uid.idx
94
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
274,660,589✔
95
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
110,435✔
96
  }
97

98
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
274,548,057✔
99
  return metaGetTableEntryByVersion(pReader, version1, uid);
274,554,081✔
100
}
101

102
static int32_t getUidVersion(SMetaReader *pReader, int64_t *version, tb_uid_t uid) {
33,498✔
103
  int32_t code = 0;
33,498✔
104
  SMeta *pMeta = pReader->pMeta;
33,498✔
105
  void* pKey = NULL;
33,887✔
106
  int   kLen = 0;
33,887✔
107

108
  TBC* pCur = NULL;
33,887✔
109
  code = tdbTbcOpen(pMeta->pTbDb, (TBC**)&pCur, NULL);
33,887✔
110
  if (code != 0) {
33,498✔
111
    return TAOS_GET_TERRNO(code);
×
112
  }
113
  STbDbKey key = {.version = *version, .uid = INT64_MAX};
33,498✔
114
  int      c = 0;
33,498✔
115
  code = tdbTbcMoveTo(pCur, &key, sizeof(key), &c);
33,887✔
116
  if (code != 0) {
33,498✔
117
    goto END;
×
118
  }
119

120
  void   *tKey = NULL;
33,498✔
121
  code = tdbTbcGet(pCur, (const void**)&tKey, &kLen, NULL, NULL);
33,498✔
122
  if (code != 0) {
33,887✔
123
    goto END;
×
124
  }
125
  STbDbKey* tmp = (STbDbKey*)tKey;
33,887✔
126
  if (tmp->uid == uid && tmp->version <= *version) {
33,887✔
127
    *version = tmp->version;
14,982✔
128
    goto END;
14,982✔
129
  }
130
  
131
  while (1) {
32,757✔
132
    int32_t ret = tdbTbcPrev(pCur, &pKey, &kLen, NULL, NULL);
51,273✔
133
    if (ret < 0) break;
51,662✔
134

135
    STbDbKey* tmp = (STbDbKey*)pKey;
51,662✔
136
    if (tmp->uid == uid && tmp->version <= *version) {
51,662✔
137
      *version = tmp->version;
18,905✔
138
      goto END;
18,516✔
139
    }
140
  }
141
  code = TSDB_CODE_NOT_FOUND;
×
142
  metaError("%s uid:%" PRId64 " version:%" PRId64 " not found", __func__, uid, *version);
×
143
END:
33,498✔
144
  tdbFree(pKey);
33,887✔
145
  tdbTbcClose(pCur);
33,887✔
146
  return code;
33,887✔
147
} 
148

149
// get table entry according to the latest version number that is less than or equal to version and uid, if version < 0, get latest version
150
int metaReaderGetTableEntryByVersionUid(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
78,805,477✔
151
  if (version < 0) {
78,805,477✔
152
    return metaReaderGetTableEntryByUid(pReader, uid);
78,807,110✔
153
  }
154
  SMeta *pMeta = pReader->pMeta;
×
155

156
  SMetaInfo info;
×
157
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
×
158
  if (TSDB_CODE_SUCCESS != code) {
×
159
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
160
  }
161
  if (info.version <= version) {
×
162
    version = info.version;
×
163
  } else {
164
    if (getUidVersion(pReader, &version, uid) != 0) {
×
165
      version = -1;
×
166
    }
167
  }
168
  return metaGetTableEntryByVersion(pReader, version, uid);
×
169
}
170

171
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
858,408,681✔
172
  SMeta *pMeta = pReader->pMeta;
858,408,681✔
173

174
  SMetaInfo info;
858,554,110✔
175
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
858,534,692✔
176
  if (TSDB_CODE_SUCCESS != code) {
858,479,799✔
177
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
249,583✔
178
  }
179

180
  return metaGetTableEntryByVersion(pReader, info.version, uid);
858,230,216✔
181
}
182

183
int metaGetTableEntryByVersionName(SMetaReader *pReader, int64_t version, const char *name) {
87,174,582✔
184
  SMeta   *pMeta = pReader->pMeta;
87,174,582✔
185
  tb_uid_t uid;
186

187
  // query name.idx
188
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
87,177,515✔
189
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
11,155,347✔
190
  }
191

192
  uid = *(tb_uid_t *)pReader->pBuf;
76,006,876✔
193
  return metaReaderGetTableEntryByVersionUid(pReader, version, uid);
75,998,685✔
194
}
195

196
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
86,784,752✔
197
  return metaGetTableEntryByVersionName(pReader, -1, name);
86,784,752✔
198
}
199

200
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
84,473,829✔
201
  void    *pData = NULL;
84,473,829✔
202
  int      nData = 0;
84,477,889✔
203
  tb_uid_t uid = 0;
84,481,277✔
204

205
  metaRLock(pMeta);
84,481,277✔
206

207
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
84,473,048✔
208
    uid = *(tb_uid_t *)pData;
11,022,236✔
209
    tdbFree(pData);
11,022,236✔
210
  }
211

212
  metaULock(pMeta);
84,463,366✔
213

214
  return uid;
84,470,799✔
215
}
216

217
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
8,295,165✔
218
  int         code = 0;
8,295,165✔
219
  SMetaReader mr = {0};
8,295,165✔
220
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
8,295,165✔
221
  code = metaReaderGetTableEntryByUid(&mr, uid);
8,294,559✔
222
  if (code < 0) {
8,291,091✔
223
    metaReaderClear(&mr);
311✔
224
    return code;
311✔
225
  }
226

227
  STR_TO_VARSTR(tbName, mr.me.name);
8,290,780✔
228
  metaReaderClear(&mr);
8,294,010✔
229

230
  return 0;
8,292,105✔
231
}
232

233
int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
×
234
  int         code = 0;
×
235
  SMetaReader mr = {0};
×
236
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
237
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
238
  if (code < 0) {
×
239
    metaReaderClear(&mr);
×
240
    return code;
×
241
  }
242
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
243
  metaReaderClear(&mr);
×
244

245
  return 0;
×
246
}
247

248
int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) {
1,608,117✔
249
  int         code = 0;
1,608,117✔
250
  SMetaReader mr = {0};
1,608,117✔
251
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
1,608,559✔
252

253
  SMetaReader *pReader = &mr;
1,608,129✔
254

255
  // query name.idx
256
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
1,608,129✔
257
    metaReaderClear(&mr);
1,055,418✔
258
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1,053,208✔
259
  }
260

261
  *uid = *(tb_uid_t *)pReader->pBuf;
553,583✔
262

263
  metaReaderClear(&mr);
553,583✔
264

265
  return 0;
553,348✔
266
}
267

268
int metaGetTableTypeSuidByName(void *pVnode, char *tbName, ETableType *tbType, uint64_t *suid) {
550,045✔
269
  int         code = 0;
550,045✔
270
  SMetaReader mr = {0};
550,045✔
271
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
550,045✔
272

273
  code = metaGetTableEntryByName(&mr, tbName);
550,045✔
274
  if (code == 0) *tbType = mr.me.type;
550,045✔
275
  if (TSDB_CHILD_TABLE == mr.me.type || TSDB_VIRTUAL_CHILD_TABLE == mr.me.type) {
550,045✔
276
    *suid = mr.me.ctbEntry.suid;
539,943✔
277
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
10,102✔
278
    *suid = mr.me.uid;
7,919✔
279
  } else {
280
    *suid = 0;
2,183✔
281
  }
282

283
  metaReaderClear(&mr);
550,045✔
284
  return code;
550,045✔
285
}
286

287
int metaReadNext(SMetaReader *pReader) {
×
288
  SMeta *pMeta = pReader->pMeta;
×
289

290
  // TODO
291

292
  return 0;
×
293
}
294

295
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
296
  int         code = -1;
×
297
  SMetaReader mr = {0};
×
298
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
299
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
300
  if (code < 0) {
×
301
    goto _exit;
×
302
  }
303
  if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
×
304
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
305
  } else if (mr.me.type == TSDB_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
306
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
307
  } else {
308
    goto _exit;
×
309
  }
310

311
  code = 0;
×
312

313
_exit:
×
314
  metaReaderClear(&mr);
×
315
  return code;
×
316
}
317

318
#if 1  // ===================================================
319
SMTbCursor *metaOpenTbCursor(void *pVnode) {
24,146,922✔
320
  SMTbCursor *pTbCur = NULL;
24,146,922✔
321
  int32_t     code;
322

323
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
24,146,922✔
324
  if (pTbCur == NULL) {
24,146,588✔
325
    return NULL;
×
326
  }
327

328
  SVnode *pVnodeObj = pVnode;
24,146,588✔
329
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
330
  pTbCur->pMeta = pVnodeObj->pMeta;
24,146,588✔
331
  pTbCur->paused = 1;
24,148,808✔
332
  code = metaResumeTbCursor(pTbCur, 1, 0);
24,149,444✔
333
  if (code) {
24,141,248✔
334
    terrno = code;
×
335
    taosMemoryFree(pTbCur);
×
336
    return NULL;
×
337
  }
338
  return pTbCur;
24,141,248✔
339
}
340

341
void metaCloseTbCursor(SMTbCursor *pTbCur) {
48,295,960✔
342
  if (pTbCur) {
48,295,960✔
343
    tdbFree(pTbCur->pKey);
24,152,486✔
344
    tdbFree(pTbCur->pVal);
24,151,819✔
345
    if (!pTbCur->paused) {
24,151,836✔
346
      metaReaderClear(&pTbCur->mr);
12,604,965✔
347
      if (pTbCur->pDbc) {
12,604,086✔
348
        tdbTbcClose((TBC *)pTbCur->pDbc);
12,604,086✔
349
      }
350
    }
351
    taosMemoryFree(pTbCur);
24,151,051✔
352
  }
353
}
48,295,898✔
354

355
void metaPauseTbCursor(SMTbCursor *pTbCur) {
11,550,365✔
356
  if (!pTbCur->paused) {
11,550,365✔
357
    metaReaderClear(&pTbCur->mr);
11,550,365✔
358
    tdbTbcClose((TBC *)pTbCur->pDbc);
11,550,365✔
359
    pTbCur->paused = 1;
11,549,923✔
360
  }
361
}
11,549,923✔
362
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
24,153,165✔
363
  int32_t code = 0;
24,153,165✔
364
  int32_t lino;
365
  int8_t  locked = 0;
24,153,165✔
366
  if (pTbCur->paused) {
24,153,165✔
367
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
24,149,329✔
368
    locked = 1;
24,153,285✔
369
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
24,153,285✔
370
    if (code != 0) {
24,148,560✔
371
      TSDB_CHECK_CODE(code, lino, _exit);
×
372
    }
373

374
    if (first) {
24,148,560✔
375
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
24,145,642✔
376
      TSDB_CHECK_CODE(code, lino, _exit);
24,139,702✔
377
    } else {
378
      int c = 1;
2,918✔
379
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
2,918✔
380
      TSDB_CHECK_CODE(code, lino, _exit);
2,918✔
381
      if (c == 0) {
2,918✔
382
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
2,918✔
383
      } else if (c < 0) {
×
384
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
385
        TSDB_CHECK_CODE(code, lino, _exit);
×
386
      } else {
387
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
388
        TSDB_CHECK_CODE(code, lino, _exit);
×
389
      }
390
    }
391

392
    pTbCur->paused = 0;
24,142,873✔
393
  }
394

395
_exit:
×
396
  if (code != 0 && locked) {
24,146,488✔
397
    metaReaderReleaseLock(&pTbCur->mr);
×
398
  }
399
  return code;
24,142,938✔
400
}
401

402
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
533,320,556✔
403
  int    ret;
404
  void  *pBuf;
405
  STbCfg tbCfg;
406

407
  for (;;) {
408
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
533,320,556✔
409
    if (ret < 0) {
533,290,253✔
410
      return ret;
24,146,470✔
411
    }
412

413
    tDecoderClear(&pTbCur->mr.coder);
509,143,783✔
414

415
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
509,189,307✔
416
    if (ret) return ret;
509,162,296✔
417

418
    if (pTbCur->mr.me.type == jumpTableType) {
509,162,296✔
419
      continue;
5,049,324✔
420
    }
421

422
    break;
504,134,679✔
423
  }
424

425
  return 0;
504,134,679✔
426
}
427

428
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
429
  int    ret;
430
  void  *pBuf;
431
  STbCfg tbCfg;
432

433
  for (;;) {
434
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
435
    if (ret < 0) {
×
436
      return -1;
×
437
    }
438

439
    tDecoderClear(&pTbCur->mr.coder);
×
440

441
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
442
    if (ret < 0) {
×
443
      return ret;
×
444
    }
445

446
    if (pTbCur->mr.me.type == jumpTableType) {
×
447
      continue;
×
448
    }
449

450
    break;
×
451
  }
452

453
  return 0;
×
454
}
455

456
static SSchemaWrapper * getSchemaWithVer(SMeta *pMeta, tb_uid_t uid, int32_t sver, SExtSchema **extSchema) {
279,635✔
457
  SDecoder        dc = {0};
279,635✔
458
  int32_t         code = 0;
279,635✔
459
  void           *pData = NULL;
279,635✔
460
  int             nData = 0;
279,253✔
461
  SSchemaWrapper  schema = {0};
279,253✔
462
  SSchemaWrapper *pSchema = NULL;
279,635✔
463
  
464
  // query from skm ext db
465
  if (extSchema != NULL) {
279,635✔
466
    if ((tdbTbGet(pMeta->pSkmExtDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) == 0) {
275,732✔
467
      *extSchema = taosMemoryMalloc(nData);
5,549✔
468
      if (*extSchema) {
5,549✔
469
        (void)memcpy(*extSchema, pData, nData);
5,549✔
470
      }
471
    }
472
    tdbFree(pData);
275,350✔
473
    pData = NULL;
275,732✔
474
  }
475
  
476
  // query from skm db
477
  if ((code = tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) < 0) {
279,635✔
478
    goto _err;
327✔
479
  }
480

481
  tDecoderInit(&dc, pData, nData);
279,308✔
482
  if ((code = tDecodeSSchemaWrapperEx(&dc, &schema)) != 0) {
279,308✔
483
    tDecoderClear(&dc);
×
484
    goto _err;
×
485
  }
486
  pSchema = tCloneSSchemaWrapper(&schema);
279,308✔
487
  if (pSchema == NULL) {
279,308✔
488
    tDecoderClear(&dc);
×
489
    code = terrno;
×
490
    goto _err;
×
491
  }
492
  tDecoderClear(&dc);
279,308✔
493

494
  tdbFree(pData);
278,926✔
495
  return pSchema;
279,308✔
496

497
_err:
327✔
498
  tdbFree(pData);
327✔
499
  tDeleteSchemaWrapper(pSchema);
500
  if (extSchema != NULL) {
327✔
501
    taosMemoryFreeClear(*extSchema);
327✔
502
  }
503
  terrno = code;
327✔
504
  return NULL;
327✔
505
}
506

507
static int32_t metaGetSuidByUidIfTableNotExist(SMeta *pMeta, int64_t uid, int64_t* id){
243,573✔
508
  int32_t code = 0;
243,573✔
509
  void   *pKey = NULL;
243,573✔
510
  void   *pVal = NULL;
243,573✔
511
  int     kLen = 0;
243,191✔
512
  int     vLen = 0;
243,191✔
513

514
  int64_t* suid = (int64_t*)taosHashGet(pMeta->uidSuidHash, &uid, sizeof(uid));
243,191✔
515
  if (suid != NULL) {
243,573✔
516
    *id = *suid;
159,258✔
517
    return code;
159,258✔
518
  }
519

520
  TBC *pCur = NULL;
84,315✔
521
  code = tdbTbcOpen(pMeta->pTbDb, (TBC**)&pCur, NULL);
84,315✔
522
  if (code != 0) {
84,315✔
523
    goto END;
×
524
  }
525
  code = tdbTbcMoveToFirst(pCur);
84,315✔
526
  if (code != 0) {
84,315✔
527
    goto END;
×
528
  }
529

530
  void       *tKey = NULL;
84,315✔
531
  void       *tVal = NULL;
84,315✔
532
  int         tKLen = 0;
84,315✔
533
  int         tVLen = 0;
84,315✔
534
  code = tdbTbcGet(pCur, (const void**)&tKey, &tKLen, (const void**)&tVal, &tVLen);
84,315✔
535
  if (code != 0) {
84,315✔
536
    goto END;
×
537
  }
538
  STbDbKey *tmp = (STbDbKey*)tKey;
84,315✔
539
  if (tmp->uid == uid) {
84,315✔
540
    SMetaEntry  me = {0};
×
541
    SDecoder    dc = {0};
×
542
    tDecoderInit(&dc, tVal, tVLen);
×
543
    if (metaDecodeEntry(&dc, &me) == 0 && (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE)) {
×
544
      *id = me.ctbEntry.suid;
×
545
    }
546
    tDecoderClear(&dc);
×
547
    goto END;
×
548
  }
549

550
  while (1) {
2,496,442✔
551
    int32_t ret = tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen);
2,580,757✔
552
    if (ret < 0) break;
2,580,757✔
553

554
    tmp = (STbDbKey*)pKey;
2,580,757✔
555
    if (tmp->uid == uid) {
2,580,757✔
556
      SMetaEntry  me = {0};
84,315✔
557
      SDecoder    dc = {0};
84,315✔
558
      tDecoderInit(&dc, pVal, vLen);
84,315✔
559
      if (metaDecodeEntry(&dc, &me) == 0 && (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE)) {
84,315✔
560
        *id = me.ctbEntry.suid;
83,988✔
561
      }
562
      tDecoderClear(&dc);
84,315✔
563
      goto END;
84,315✔
564
    }
565
  }
566

567
END:
84,315✔
568
  tdbFree(pKey);
84,315✔
569
  tdbFree(pVal);
84,315✔
570
  tdbTbcClose(pCur);
84,315✔
571
  if (code != 0) {
84,315✔
572
    metaError("%s failed to get suid reason:%s", __func__, tstrerror(terrno));
×
573
  } else {
574
    code = taosHashPut(pMeta->uidSuidHash, &uid, sizeof(uid), id, sizeof(*id));
84,315✔
575
    if (code != 0) {
84,315✔
576
      metaError("%s failed to put suid into uidSuidHash, reason:%s", __func__, tstrerror(terrno));
×
577
    }
578
  }
579
  return code;
84,315✔
580
}
581

582
int32_t metaGetTbnameByIdIfTableNotExist(SMeta *pMeta, int64_t uid, char *tbname){
241,938✔
583
  int32_t code = 0;
241,938✔
584
  void   *pKey = NULL;
241,938✔
585
  void   *pVal = NULL;
241,938✔
586
  int     kLen = 0;
241,938✔
587
  int     vLen = 0;
241,938✔
588

589
  char* name = (char*)taosHashGet(pMeta->uidNameHash, &uid, sizeof(uid));
241,938✔
590
  if (name != NULL) {
241,938✔
591
    tstrncpy(tbname, name, TSDB_TABLE_NAME_LEN);
158,931✔
592
    return code;
158,931✔
593
  }
594

595
  TBC *pCur = NULL;
83,007✔
596
  code = tdbTbcOpen(pMeta->pTbDb, (TBC**)&pCur, NULL);
83,007✔
597
  if (code != 0) {
83,007✔
598
    goto END;
×
599
  }
600
  code = tdbTbcMoveToFirst(pCur);
83,007✔
601
  if (code != 0) {
83,007✔
602
    goto END;
×
603
  }
604

605
  void       *tKey = NULL;
83,007✔
606
  void       *tVal = NULL;
83,007✔
607
  int         tKLen = 0;
83,007✔
608
  int         tVLen = 0;
83,007✔
609
  code = tdbTbcGet(pCur, (const void**)&tKey, &tKLen, (const void**)&tVal, &tVLen);
83,007✔
610
  if (code != 0) {
83,007✔
611
    goto END;
×
612
  }
613
  STbDbKey *tmp = (STbDbKey*)tKey;
83,007✔
614
  if (tmp->uid == uid) {
83,007✔
615
    SMetaEntry  me = {0};
×
616
    SDecoder    dc = {0};
×
617
    tDecoderInit(&dc, tVal, tVLen);
×
618
    code = metaDecodeEntry(&dc, &me);
×
619
    if (code == 0){
×
620
      tstrncpy(tbname, me.name, TSDB_TABLE_NAME_LEN);
×
621
    }
622
    tDecoderClear(&dc);
×
623
    goto END;
×
624
  }
625

626
  while (1) {
2,489,630✔
627
    int32_t ret = tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen);
2,572,637✔
628
    if (ret < 0) break;
2,572,637✔
629

630
    tmp = (STbDbKey*)pKey;
2,572,637✔
631
    if (tmp->uid == uid) {
2,572,637✔
632
      SMetaEntry  me = {0};
83,007✔
633
      SDecoder    dc = {0};
83,007✔
634
      tDecoderInit(&dc, pVal, vLen);
83,007✔
635
      code = metaDecodeEntry(&dc, &me);
83,007✔
636
      if (code == 0){
83,007✔
637
        tstrncpy(tbname, me.name, TSDB_TABLE_NAME_LEN);
83,007✔
638
      }
639
      tDecoderClear(&dc);
83,007✔
640
      goto END;
83,007✔
641
    }
642
  }
643

644
END:
83,007✔
645
  tdbFree(pKey);
83,007✔
646
  tdbFree(pVal);
83,007✔
647
  tdbTbcClose(pCur);
83,007✔
648
  if (code != 0) {
83,007✔
649
    metaError("%s failed to get suid reason:%s", __func__, tstrerror(terrno));
×
650
  } else {
651
    code = taosHashPut(pMeta->uidNameHash, &uid, sizeof(uid), tbname, strlen(tbname) + 1);
83,007✔
652
    if (code != 0) {
83,007✔
653
      metaError("%s failed to put suid into uidSuidHash, reason:%s", __func__, tstrerror(terrno));
×
654
    }
655
  }
656
  return code;
83,007✔
657
}
658

659
/**
660
 * @param type 0x01 fetchRsmaSchema if table is rsma
661
 */
662
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
377,763,534✔
663
                                   int8_t type, bool ignoreExist) {
664
  int32_t         code = 0;
377,763,534✔
665
  void           *pData = NULL;
377,763,534✔
666
  int             nData = 0;
377,657,829✔
667
  int64_t         version;
668
  SSchemaWrapper  schema = {0};
377,609,917✔
669
  SSchemaWrapper *pSchema = NULL;
377,609,917✔
670
  SDecoder        dc = {0};
377,609,917✔
671
  if (lock) {
377,449,198✔
672
    metaRLock(pMeta);
371,793,961✔
673
  }
674
_query:
415,703,747✔
675
  code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
415,747,930✔
676
  if (code == TSDB_CODE_NOT_FOUND && ignoreExist){
415,751,875✔
677
    int64_t id = 0;
243,573✔
678
    code = metaGetSuidByUidIfTableNotExist(pMeta, uid, &id);
243,573✔
679
    if (code != 0) {
243,573✔
680
      goto _err;
×
681
    }
682
    pSchema = getSchemaWithVer(pMeta, id, sver, extSchema);
243,573✔
683
    goto _exit;
243,573✔
684
  }
685

686
  if (code < 0) {
415,508,302✔
687
    goto _err;
191,515✔
688
  }
689

690
  version = ((SUidIdxVal *)pData)[0].version;
415,316,787✔
691

692
  if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData)) !=
415,489,414✔
693
      0) {
694
    goto _err;
×
695
  }
696

697
  SMetaEntry me = {0};
415,479,999✔
698
  tDecoderInit(&dc, pData, nData);
415,480,894✔
699
  code = metaDecodeEntry(&dc, &me);
415,481,694✔
700
  if (code) {
415,363,448✔
701
    tDecoderClear(&dc);
×
702
    goto _err;
×
703
  }
704
  if (me.type == TSDB_SUPER_TABLE) {
415,363,448✔
705
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
296,997,647✔
706
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
296,909,337✔
707
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
296,909,337✔
708
      if (TABLE_IS_ROLLUP(me.flags)) {
296,902,763✔
709
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
118,179✔
710
          tDecoderClear(&dc);
×
711
          goto _err;
×
712
        }
713
      }
714
      tDecoderClear(&dc);
296,902,763✔
715
      goto _exit;
296,909,672✔
716
    }
717
  } else if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
118,365,801✔
718
    uid = me.ctbEntry.suid;
37,903,454✔
719
    tDecoderClear(&dc);
37,903,454✔
720
    goto _query;
37,923,568✔
721
  } else {
722
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
80,462,347✔
723
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
80,470,339✔
724
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
80,470,339✔
725
      tDecoderClear(&dc);
80,483,119✔
726
      goto _exit;
80,497,424✔
727
    }
728
  }
729
  tDecoderClear(&dc);
31,407✔
730

731
  pSchema = getSchemaWithVer(pMeta, uid, sver, extSchema);
36,062✔
732

733
_exit:
377,686,731✔
734
  if (lock) {
377,673,041✔
735
    metaULock(pMeta);
372,063,007✔
736
  }
737
  tdbFree(pData);
377,547,711✔
738
  return pSchema;
377,616,931✔
739

740
_err:
192,836✔
741
  if (lock) {
191,515✔
742
    metaULock(pMeta);
191,365✔
743
  }
744
  tdbFree(pData);
191,515✔
745
  tDeleteSchemaWrapper(pSchema);
746
  if (extSchema != NULL) {
191,515✔
747
    taosMemoryFreeClear(*extSchema);
×
748
  }
749
  terrno = code;
191,515✔
750
  return NULL;
191,515✔
751
}
752

753
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
1,022,790✔
754
  void    *pData = NULL;
1,022,790✔
755
  int      nData = 0;
1,022,790✔
756
  int64_t  version = 0;
1,023,165✔
757
  SDecoder dc = {0};
1,023,165✔
758
  int64_t  createTime = INT64_MAX;
1,023,166✔
759
  if (lock) {
1,023,166✔
760
    metaRLock(pMeta);
1,022,791✔
761
  }
762

763
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,023,916✔
764
    goto _exit;
824✔
765
  }
766

767
  version = ((SUidIdxVal *)pData)[0].version;
1,022,342✔
768

769
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
1,022,342✔
770
    goto _exit;
×
771
  }
772

773
  SMetaEntry me = {0};
1,022,717✔
774
  tDecoderInit(&dc, pData, nData);
1,022,717✔
775
  int32_t code = metaDecodeEntry(&dc, &me);
1,022,717✔
776
  if (code) {
1,021,966✔
777
    tDecoderClear(&dc);
×
778
    goto _exit;
×
779
  }
780
  if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
1,021,966✔
781
    createTime = me.ctbEntry.btime;
1,021,582✔
782
  } else if (me.type == TSDB_NORMAL_TABLE || me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
384✔
783
    createTime = me.ntbEntry.btime;
384✔
784
  }
785
  tDecoderClear(&dc);
1,021,966✔
786

787
_exit:
1,023,166✔
788
  if (lock) {
1,022,037✔
789
    metaULock(pMeta);
1,022,037✔
790
  }
791
  tdbFree(pData);
1,021,286✔
792
  return createTime;
1,022,791✔
793
}
794

795
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
147,708,777✔
796
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
147,708,777✔
797
  SMCtbCursor *pCtbCur = NULL;
147,703,250✔
798
  SCtbIdxKey   ctbIdxKey;
799
  int          ret = 0;
147,703,250✔
800
  int          c = 0;
147,703,250✔
801

802
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
147,703,250✔
803
  if (pCtbCur == NULL) {
147,594,302✔
804
    return NULL;
×
805
  }
806

807
  pCtbCur->pMeta = pMeta;
147,594,302✔
808
  pCtbCur->suid = uid;
147,596,675✔
809
  pCtbCur->lock = lock;
147,599,406✔
810
  pCtbCur->paused = 1;
147,636,764✔
811

812
  ret = metaResumeCtbCursor(pCtbCur, 1);
147,654,063✔
813
  if (ret < 0) {
147,696,445✔
814
    return NULL;
×
815
  }
816
  return pCtbCur;
147,696,445✔
817
}
818

819
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
147,728,842✔
820
  if (pCtbCur) {
147,728,842✔
821
    if (!pCtbCur->paused) {
147,730,844✔
822
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
142,497,404✔
823
      if (pCtbCur->pCur) {
142,505,399✔
824
        tdbTbcClose(pCtbCur->pCur);
142,497,022✔
825
      }
826
    }
827
    tdbFree(pCtbCur->pKey);
147,686,084✔
828
    tdbFree(pCtbCur->pVal);
147,669,760✔
829
  }
830
  taosMemoryFree(pCtbCur);
147,663,887✔
831
}
147,651,153✔
832

833
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
5,238,460✔
834
  if (!pCtbCur->paused) {
5,238,460✔
835
    tdbTbcClose((TBC *)pCtbCur->pCur);
5,235,892✔
836
    if (pCtbCur->lock) {
5,236,598✔
837
      metaULock(pCtbCur->pMeta);
5,236,598✔
838
    }
839
    pCtbCur->paused = 1;
5,236,329✔
840
  }
841
}
5,240,427✔
842

843
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
147,708,977✔
844
  if (pCtbCur->paused) {
147,708,977✔
845
    pCtbCur->paused = 0;
147,643,480✔
846

847
    if (pCtbCur->lock) {
147,682,896✔
848
      metaRLock(pCtbCur->pMeta);
141,887,804✔
849
    }
850
    int ret = 0;
147,742,899✔
851
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
147,742,899✔
852
    if (ret < 0) {
147,654,845✔
853
      metaCloseCtbCursor(pCtbCur);
×
854
      return -1;
×
855
    }
856

857
    if (first) {
147,654,845✔
858
      SCtbIdxKey ctbIdxKey;
147,650,224✔
859
      // move to the suid
860
      ctbIdxKey.suid = pCtbCur->suid;
147,660,627✔
861
      ctbIdxKey.uid = INT64_MIN;
147,686,453✔
862
      int c = 0;
147,686,453✔
863
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
147,606,918✔
864
      if (c > 0) {
147,671,831✔
865
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
27,673,843✔
866
      }
867
    } else {
868
      int c = 0;
×
869
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
870
      if (c < 0) {
×
871
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
872
      } else {
873
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
874
      }
875
    }
876
  }
877
  return 0;
147,709,083✔
878
}
879

880
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
847,830,040✔
881
  int         ret;
882
  SCtbIdxKey *pCtbIdxKey;
883

884
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
847,830,040✔
885
  if (ret < 0) {
847,843,344✔
886
    return 0;
106,488,509✔
887
  }
888

889
  pCtbIdxKey = pCtbCur->pKey;
741,354,835✔
890
  if (pCtbIdxKey->suid > pCtbCur->suid) {
741,356,191✔
891
    return 0;
41,368,878✔
892
  }
893

894
  return pCtbIdxKey->uid;
699,995,926✔
895
}
896

897
struct SMStbCursor {
898
  SMeta   *pMeta;
899
  TBC     *pCur;
900
  tb_uid_t suid;
901
  void    *pKey;
902
  void    *pVal;
903
  int      kLen;
904
  int      vLen;
905
};
906

907
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
78,258,309✔
908
  SMStbCursor *pStbCur = NULL;
78,258,309✔
909
  int          ret = 0;
78,258,309✔
910
  int          c = 0;
78,258,309✔
911

912
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
78,259,040✔
913
  if (pStbCur == NULL) {
78,257,578✔
914
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
915
    return NULL;
×
916
  }
917

918
  pStbCur->pMeta = pMeta;
78,257,578✔
919
  pStbCur->suid = suid;
78,257,578✔
920
  metaRLock(pMeta);
78,261,233✔
921

922
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
78,260,502✔
923
  if (ret < 0) {
78,255,385✔
924
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
925
    metaULock(pMeta);
×
926
    taosMemoryFree(pStbCur);
×
927
    return NULL;
×
928
  }
929

930
  // move to the suid
931
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
78,255,385✔
932
  if (c > 0) {
78,258,309✔
933
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
934
  }
935

936
  return pStbCur;
78,257,578✔
937
}
938

939
void metaCloseStbCursor(SMStbCursor *pStbCur) {
78,258,309✔
940
  if (pStbCur) {
78,258,309✔
941
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
78,259,771✔
942
    if (pStbCur->pCur) {
78,261,964✔
943
      tdbTbcClose(pStbCur->pCur);
78,261,233✔
944

945
      tdbFree(pStbCur->pKey);
78,259,771✔
946
      tdbFree(pStbCur->pVal);
78,260,502✔
947
    }
948

949
    taosMemoryFree(pStbCur);
78,258,309✔
950
  }
951
}
78,256,847✔
952

953
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
137,725,612✔
954
  int ret;
955

956
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
137,725,612✔
957
  if (ret < 0) {
137,720,112✔
958
    return 0;
78,261,233✔
959
  }
960
  return *(tb_uid_t *)pStbCur->pKey;
59,458,879✔
961
}
962

963
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
318,487,092✔
964
  STSchema       *pTSchema = NULL;
318,487,092✔
965
  SSchemaWrapper *pSW = NULL;
318,487,092✔
966

967
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0, false);
318,487,092✔
968
  if (!pSW) return NULL;
318,525,816✔
969

970
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
318,484,576✔
971

972
  tDeleteSchemaWrapper(pSW);
973
  return pTSchema;
318,398,932✔
974
}
975

976
/**
977
 * Fetch rsma schema if table is rsma
978
 */
979
SRSchema *metaGetTbTSchemaR(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
30,488✔
980
  SRSchema       *pRSchema = NULL;
30,488✔
981
  SSchemaWrapper *pSW = NULL;
30,488✔
982

983
  if (!(pRSchema = (SRSchema *)taosMemoryCalloc(1, sizeof(SRSchema)))) goto _err;
30,488✔
984
  if (!(pSW = metaGetTableSchema(pMeta, uid, sver, lock, (SExtSchema **)&pRSchema->extSchema, 0x01, false))) goto _err;
30,488✔
985
  if (!(pRSchema->tSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version))) goto _err;
30,488✔
986

987
  if (pSW->pRsma) {
30,488✔
988
    if (!(pRSchema->funcIds = taosMemoryCalloc(pSW->nCols, sizeof(func_id_t)))) goto _err;
30,488✔
989
    memcpy(pRSchema->funcIds, pSW->pRsma->funcIds, pSW->nCols * sizeof(func_id_t));
30,488✔
990

991
    pRSchema->tbType = pSW->pRsma->tbType;
30,488✔
992
    pRSchema->tbUid = uid;
30,488✔
993
    tstrncpy(pRSchema->tbName, pSW->pRsma->tbName, TSDB_TABLE_NAME_LEN);
30,488✔
994
    pRSchema->interval[0] = pSW->pRsma->interval[0];
30,488✔
995
    pRSchema->interval[1] = pSW->pRsma->interval[1];
30,488✔
996
  }
997

998
_exit:
30,488✔
999
  tDeleteSchemaWrapper(pSW);
1000
  return pRSchema;
30,488✔
1001
_err:
×
1002
  tDeleteSchemaWrapper(pSW);
1003
  tFreeSRSchema(&pRSchema);
1004
  return NULL;
×
1005
}
1006

1007
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,114,443✔
1008
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,114,443✔
1009
  if (*ppTSchema == NULL) {
1,114,443✔
1010
    return terrno;
×
1011
  }
1012
  return TSDB_CODE_SUCCESS;
1,114,443✔
1013
}
1014

1015
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
317,195,319✔
1016
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
317,195,319✔
1017
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
317,090,803✔
1018
    return terrno;
13✔
1019
  }
1020
  return TSDB_CODE_SUCCESS;
316,900,793✔
1021
}
1022

1023
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
81,544,524✔
1024
  int32_t code = 0;
81,544,524✔
1025
  int32_t lino;
1026

1027
  void     *pData = NULL;
81,544,524✔
1028
  int       nData = 0;
81,553,549✔
1029
  SSkmDbKey skmDbKey;
81,555,088✔
1030
  if (sver <= 0) {
81,551,398✔
1031
    SMetaInfo info;
41,213,973✔
1032
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
41,217,759✔
1033
      sver = info.skmVer;
41,183,320✔
1034
    } else {
1035
      TBC *pSkmDbC = NULL;
39,897✔
1036
      int  c;
40,868✔
1037

1038
      skmDbKey.uid = suid ? suid : uid;
40,868✔
1039
      skmDbKey.sver = INT32_MAX;
40,868✔
1040

1041
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
40,868✔
1042
      TSDB_CHECK_CODE(code, lino, _exit);
41,240✔
1043
      metaRLock(pMeta);
41,240✔
1044

1045
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
41,240✔
1046
        metaULock(pMeta);
×
1047
        tdbTbcClose(pSkmDbC);
×
1048
        code = TSDB_CODE_NOT_FOUND;
×
1049
        goto _exit;
×
1050
      }
1051

1052
      if (c == 0) {
40,868✔
1053
        metaULock(pMeta);
×
1054
        tdbTbcClose(pSkmDbC);
×
1055
        code = TSDB_CODE_FAILED;
×
1056
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
1057
        goto _exit;
×
1058
      }
1059

1060
      if (c < 0) {
40,868✔
1061
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
4,040✔
1062
      }
1063

1064
      const void *pKey = NULL;
40,868✔
1065
      int32_t     nKey = 0;
40,868✔
1066
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
41,240✔
1067

1068
      if (ret != 0 || ((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
41,240✔
1069
        metaULock(pMeta);
×
1070
        tdbTbcClose(pSkmDbC);
×
1071
        code = TSDB_CODE_NOT_FOUND;
×
1072
        goto _exit;
×
1073
      }
1074

1075
      sver = ((SSkmDbKey *)pKey)->sver;
41,240✔
1076

1077
      metaULock(pMeta);
41,240✔
1078
      tdbTbcClose(pSkmDbC);
41,240✔
1079
    }
1080
  }
1081

1082
  if (!(sver > 0)) {
81,536,845✔
1083
    code = TSDB_CODE_NOT_FOUND;
×
1084
    goto _exit;
×
1085
  }
1086

1087
  skmDbKey.uid = suid ? suid : uid;
81,536,845✔
1088
  skmDbKey.sver = sver;
81,536,845✔
1089
  metaRLock(pMeta);
81,536,845✔
1090
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
81,556,862✔
1091
    metaULock(pMeta);
3,038✔
1092
    code = TSDB_CODE_NOT_FOUND;
3,038✔
1093
    goto _exit;
3,038✔
1094
  }
1095
  metaULock(pMeta);
81,547,749✔
1096

1097
  // decode
1098
  SDecoder        dc = {0};
81,564,372✔
1099
  SSchemaWrapper  schema;
81,567,261✔
1100
  SSchemaWrapper *pSchemaWrapper = &schema;
81,562,376✔
1101

1102
  tDecoderInit(&dc, pData, nData);
81,562,376✔
1103
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
81,575,170✔
1104
  tDecoderClear(&dc);
81,575,170✔
1105
  tdbFree(pData);
81,558,793✔
1106
  if (TSDB_CODE_SUCCESS != code) {
81,532,383✔
1107
    taosMemoryFree(pSchemaWrapper->pSchema);
×
1108
    goto _exit;
×
1109
  }
1110

1111
  // convert
1112
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
81,532,383✔
1113
  if (pTSchema == NULL) {
81,558,903✔
1114
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1115
  }
1116

1117
  *ppTSchema = pTSchema;
81,558,903✔
1118
  taosMemoryFree(pSchemaWrapper->pSchema);
81,560,741✔
1119

1120
_exit:
81,552,448✔
1121
  return code;
81,532,851✔
1122
}
1123

1124
// N.B. Called by statusReq per second
1125
int64_t metaGetTbNum(SMeta *pMeta) {
172,200,951✔
1126
  // num of child tables (excluding normal tables , stables and others)
1127

1128
  /* int64_t num = 0; */
1129
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
1130

1131
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
172,200,951✔
1132
}
1133

1134
void metaUpdTimeSeriesNum(SMeta *pMeta) {
78,250,911✔
1135
  int64_t nCtbTimeSeries = 0;
78,250,911✔
1136
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
78,253,104✔
1137
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
78,253,104✔
1138
  }
1139
}
78,253,835✔
1140

1141
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
1142
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
1143
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
268,298,467✔
1144
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
268,305,857✔
1145
    metaUpdTimeSeriesNum(pMeta);
78,245,184✔
1146
  }
1147

1148
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
268,304,018✔
1149
}
1150

1151
// type: 1 reported timeseries
1152
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
268,287,510✔
1153
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
268,295,702✔
1154
  if (type == 1) {
268,295,702✔
1155
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
172,200,951✔
1156
  }
1157
  return nTimeSeries;
268,277,119✔
1158
}
1159

1160
typedef struct {
1161
  SMeta   *pMeta;
1162
  TBC     *pCur;
1163
  tb_uid_t uid;
1164
  void    *pKey;
1165
  void    *pVal;
1166
  int      kLen;
1167
  int      vLen;
1168
} SMSmaCursor;
1169

1170
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
1171
  SMSmaCursor *pSmaCur = NULL;
×
1172
  SSmaIdxKey   smaIdxKey;
×
1173
  int          ret;
1174
  int          c;
×
1175

1176
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
1177
  if (pSmaCur == NULL) {
×
1178
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1179
    return NULL;
×
1180
  }
1181

1182
  pSmaCur->pMeta = pMeta;
×
1183
  pSmaCur->uid = uid;
×
1184
  metaRLock(pMeta);
×
1185

1186
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
1187
  if (ret < 0) {
×
1188
    metaULock(pMeta);
×
1189
    taosMemoryFree(pSmaCur);
×
1190
    return NULL;
×
1191
  }
1192

1193
  // move to the suid
1194
  smaIdxKey.uid = uid;
×
1195
  smaIdxKey.smaUid = INT64_MIN;
×
1196
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
1197
  if (c > 0) {
×
1198
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
1199
  }
1200

1201
  return pSmaCur;
×
1202
}
1203

1204
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
1205
  if (pSmaCur) {
×
1206
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
1207
    if (pSmaCur->pCur) {
×
1208
      tdbTbcClose(pSmaCur->pCur);
×
1209
      pSmaCur->pCur = NULL;
×
1210

1211
      tdbFree(pSmaCur->pKey);
×
1212
      tdbFree(pSmaCur->pVal);
×
1213
    }
1214

1215
    taosMemoryFree(pSmaCur);
×
1216
  }
1217
}
×
1218

1219
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
1220
  int         ret;
1221
  SSmaIdxKey *pSmaIdxKey;
1222

1223
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
1224
  if (ret < 0) {
×
1225
    return 0;
×
1226
  }
1227

1228
  pSmaIdxKey = pSmaCur->pKey;
×
1229
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
1230
    return 0;
×
1231
  }
1232

1233
  return pSmaIdxKey->uid;
×
1234
}
1235

1236
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
1237
  STSmaWrapper *pSW = NULL;
×
1238
  SArray       *pSmaIds = NULL;
×
1239

1240
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
1241
    return NULL;
×
1242
  }
1243

1244
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
1245
  if (!pSW) {
×
1246
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1247
    goto _err;
×
1248
  }
1249

1250
  pSW->number = taosArrayGetSize(pSmaIds);
×
1251
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
1252

1253
  if (!pSW->tSma) {
×
1254
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1255
    goto _err;
×
1256
  }
1257

1258
  SMetaReader mr = {0};
×
1259
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
1260
  int64_t smaId;
1261
  int     smaIdx = 0;
×
1262
  STSma  *pTSma = NULL;
×
1263
  for (int i = 0; i < pSW->number; ++i) {
×
1264
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
1265
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
1266
      tDecoderClear(&mr.coder);
×
1267
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
1268
      continue;
×
1269
    }
1270
    tDecoderClear(&mr.coder);
×
1271
    pTSma = pSW->tSma + smaIdx;
×
1272
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1273
    if (deepCopy) {
×
1274
      if (pTSma->exprLen > 0) {
×
1275
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
1276
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1277
          goto _err;
×
1278
        }
1279
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
1280
      }
1281
      if (pTSma->tagsFilterLen > 0) {
×
1282
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
1283
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1284
          goto _err;
×
1285
        }
1286
      }
1287
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
1288
    } else {
1289
      pTSma->exprLen = 0;
×
1290
      pTSma->expr = NULL;
×
1291
      pTSma->tagsFilterLen = 0;
×
1292
      pTSma->tagsFilter = NULL;
×
1293
    }
1294

1295
    ++smaIdx;
×
1296
  }
1297

1298
  if (smaIdx <= 0) goto _err;
×
1299
  pSW->number = smaIdx;
×
1300

1301
  metaReaderClear(&mr);
×
1302
  taosArrayDestroy(pSmaIds);
×
1303
  return pSW;
×
1304
_err:
×
1305
  metaReaderClear(&mr);
×
1306
  taosArrayDestroy(pSmaIds);
×
1307
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
1308
  return NULL;
×
1309
}
1310

1311
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
×
1312
  STSma      *pTSma = NULL;
×
1313
  SMetaReader mr = {0};
×
1314
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
1315
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
×
1316
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
1317
    metaReaderClear(&mr);
×
1318
    return NULL;
×
1319
  }
1320
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
×
1321
  if (!pTSma) {
×
1322
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1323
    metaReaderClear(&mr);
×
1324
    return NULL;
×
1325
  }
1326

1327
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1328

1329
  metaReaderClear(&mr);
×
1330
  return pTSma;
×
1331
}
1332

1333
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1334
  SArray     *pUids = NULL;
×
1335
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1336

1337
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1338
  if (!pCur) {
×
1339
    return NULL;
×
1340
  }
1341

1342
  while (1) {
×
1343
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1344
    if (id == 0) {
×
1345
      break;
×
1346
    }
1347

1348
    if (!pUids) {
×
1349
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1350
      if (!pUids) {
×
1351
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1352
        metaCloseSmaCursor(pCur);
×
1353
        return NULL;
×
1354
      }
1355
    }
1356

1357
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1358

1359
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1360
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1361
      metaCloseSmaCursor(pCur);
×
1362
      taosArrayDestroy(pUids);
×
1363
      return NULL;
×
1364
    }
1365
  }
1366

1367
  metaCloseSmaCursor(pCur);
×
1368
  return pUids;
×
1369
}
1370

1371
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1372
  SArray     *pUids = NULL;
×
1373
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1374
  tb_uid_t    lastUid = 0;
×
1375

1376
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1377
  if (!pCur) {
×
1378
    return NULL;
×
1379
  }
1380

1381
  while (1) {
×
1382
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1383
    if (uid == 0) {
×
1384
      break;
×
1385
    }
1386

1387
    if (lastUid == uid) {
×
1388
      continue;
×
1389
    }
1390

1391
    lastUid = uid;
×
1392

1393
    if (!pUids) {
×
1394
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1395
      if (!pUids) {
×
1396
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1397
        metaCloseSmaCursor(pCur);
×
1398
        return NULL;
×
1399
      }
1400
    }
1401

1402
    if (!taosArrayPush(pUids, &uid)) {
×
1403
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1404
      metaCloseSmaCursor(pCur);
×
1405
      taosArrayDestroy(pUids);
×
1406
      return NULL;
×
1407
    }
1408
  }
1409

1410
  metaCloseSmaCursor(pCur);
×
1411
  return pUids;
×
1412
}
1413

1414
#endif
1415

1416
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
1,286,810,447✔
1417
  STag *tag = (STag *)pTag;
1,286,810,447✔
1418
  if (type == TSDB_DATA_TYPE_JSON) {
1,286,810,447✔
1419
    return tag;
1,693,773✔
1420
  }
1421
  bool find = tTagGet(tag, val);
1,285,116,674✔
1422

1423
  if (!find) {
1,285,228,705✔
1424
    return NULL;
8,255,056✔
1425
  }
1426

1427
  return val;
1,276,973,649✔
1428
}
1429

1430
typedef struct {
1431
  SMeta   *pMeta;
1432
  TBC     *pCur;
1433
  tb_uid_t suid;
1434
  int16_t  cid;
1435
  int16_t  type;
1436
  void    *pKey;
1437
  void    *pVal;
1438
  int32_t  kLen;
1439
  int32_t  vLen;
1440
} SIdxCursor;
1441

1442
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
2,098✔
1443
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
2,098✔
1444
  SMetaFltParam *param = arg;
2,098✔
1445
  int32_t        ret = 0;
2,098✔
1446

1447
  SIdxCursor *pCursor = NULL;
2,098✔
1448
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
2,098✔
1449
  if (pCursor == NULL) {
2,098✔
1450
    return terrno;
×
1451
  }
1452
  pCursor->pMeta = pMeta;
2,098✔
1453
  pCursor->suid = param->suid;
2,098✔
1454
  pCursor->cid = param->cid;
2,098✔
1455
  pCursor->type = param->type;
2,098✔
1456

1457
  metaRLock(pMeta);
2,098✔
1458
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
2,098✔
1459
  if (ret != 0) {
2,098✔
1460
    goto END;
×
1461
  }
1462
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
2,098✔
1463

1464
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
2,098✔
1465
  SBtimeIdxKey *pBtimeKey = &btimeKey;
2,098✔
1466

1467
  int cmp = 0;
2,098✔
1468
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
2,098✔
1469
    goto END;
×
1470
  }
1471

1472
  int32_t valid = 0;
2,098✔
1473
  int32_t count = 0;
2,098✔
1474

1475
  static const int8_t TRY_ERROR_LIMIT = 1;
1476
  do {
7,944,949✔
1477
    void   *entryKey = NULL;
7,947,047✔
1478
    int32_t nEntryKey = -1;
7,946,042✔
1479
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
7,948,052✔
1480
    if (valid < 0) break;
7,808,357✔
1481

1482
    SBtimeIdxKey *p = entryKey;
7,806,259✔
1483
    if (count > TRY_ERROR_LIMIT) break;
7,806,259✔
1484

1485
    terrno = TSDB_CODE_SUCCESS;
7,806,259✔
1486
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
7,800,631✔
1487
    if (terrno != TSDB_CODE_SUCCESS) {
7,788,973✔
1488
      ret = terrno;
×
1489
      break;
×
1490
    }
1491
    if (cmp == 0) {
7,854,700✔
1492
      if (taosArrayPush(pUids, &p->uid) == NULL) {
15,814,121✔
1493
        ret = terrno;
×
1494
        break;
×
1495
      }
1496
    } else {
1497
      if (param->equal == true) {
×
1498
        if (count > TRY_ERROR_LIMIT) break;
×
1499
        count++;
×
1500
      }
1501
    }
1502
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
7,959,421✔
1503
    if (valid < 0) break;
7,952,185✔
1504
  } while (1);
1505

1506
END:
4,510✔
1507
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
2,098✔
1508
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
2,098✔
1509
  taosMemoryFree(pCursor);
2,098✔
1510
  return ret;
2,098✔
1511
}
1512

1513
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1514
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1515
  SMetaFltParam *param = arg;
×
1516
  int32_t        ret = 0;
×
1517
  char          *buf = NULL;
×
1518

1519
  STagIdxKey *pKey = NULL;
×
1520
  int32_t     nKey = 0;
×
1521

1522
  SIdxCursor *pCursor = NULL;
×
1523
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1524
  if (pCursor == NULL) {
×
1525
    return terrno;
×
1526
  }
1527
  pCursor->pMeta = pMeta;
×
1528
  pCursor->suid = param->suid;
×
1529
  pCursor->cid = param->cid;
×
1530
  pCursor->type = param->type;
×
1531

1532
  char *pName = param->val;
×
1533

1534
  metaRLock(pMeta);
×
1535
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1536
  if (ret != 0) {
×
1537
    goto END;
×
1538
  }
1539

1540
  int cmp = 0;
×
1541
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1542
    goto END;
×
1543
  }
1544
  int32_t valid = 0;
×
1545
  int32_t count = 0;
×
1546

1547
  int32_t TRY_ERROR_LIMIT = 1;
×
1548
  do {
×
1549
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1550
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1551
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1552
    if (valid < 0) break;
×
1553

1554
    if (count > TRY_ERROR_LIMIT) break;
×
1555

1556
    char *pTableKey = (char *)pEntryKey;
×
1557
    terrno = TSDB_CODE_SUCCESS;
×
1558
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1559
    if (terrno != TSDB_CODE_SUCCESS) {
×
1560
      ret = terrno;
×
1561
      goto END;
×
1562
    }
1563
    if (cmp == 0) {
×
1564
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1565
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1566
        ret = terrno;
×
1567
        goto END;
×
1568
      }
1569
    } else {
1570
      if (param->equal == true) {
×
1571
        if (count > TRY_ERROR_LIMIT) break;
×
1572
        count++;
×
1573
      }
1574
    }
1575
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1576
    if (valid < 0) {
×
1577
      break;
×
1578
    }
1579
  } while (1);
1580

1581
END:
×
1582
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1583
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1584
  taosMemoryFree(buf);
×
1585
  taosMemoryFree(pKey);
×
1586

1587
  taosMemoryFree(pCursor);
×
1588

1589
  return ret;
×
1590
}
1591
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1592
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1593
  SMetaFltParam *param = arg;
×
1594
  int32_t        ret = 0;
×
1595
  char          *buf = NULL;
×
1596

1597
  STtlIdxKey *pKey = NULL;
×
1598
  int32_t     nKey = 0;
×
1599

1600
  SIdxCursor *pCursor = NULL;
×
1601
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1602
  if (pCursor == NULL) {
×
1603
    return terrno;
×
1604
  }
1605
  pCursor->pMeta = pMeta;
×
1606
  pCursor->suid = param->suid;
×
1607
  pCursor->cid = param->cid;
×
1608
  pCursor->type = param->type;
×
1609

1610
  metaRLock(pMeta);
×
1611
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1612

1613
END:
×
1614
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1615
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1616
  taosMemoryFree(buf);
×
1617
  taosMemoryFree(pKey);
×
1618

1619
  taosMemoryFree(pCursor);
×
1620

1621
  return ret;
×
1622
  // impl later
1623
  return 0;
1624
}
1625
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
3,542,349✔
1626
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,542,349✔
1627
  SMetaFltParam *param = arg;
3,543,001✔
1628

1629
  SMetaEntry oStbEntry = {0};
3,543,001✔
1630
  int32_t    code = 0;
3,538,043✔
1631
  char      *buf = NULL;
3,538,043✔
1632
  void      *pData = NULL;
3,538,043✔
1633
  int        nData = 0;
3,540,731✔
1634

1635
  SDecoder    dc = {0};
3,539,576✔
1636
  STbDbKey    tbDbKey = {0};
3,540,336✔
1637
  STagIdxKey *pKey = NULL;
3,537,634✔
1638
  int32_t     nKey = 0;
3,532,907✔
1639

1640
  SIdxCursor *pCursor = NULL;
3,539,895✔
1641
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,539,895✔
1642
  if (!pCursor) {
3,536,560✔
1643
    return terrno;
×
1644
  }
1645
  pCursor->pMeta = pMeta;
3,536,560✔
1646
  pCursor->suid = param->suid;
3,540,312✔
1647
  pCursor->cid = param->cid;
3,537,628✔
1648
  pCursor->type = param->type;
3,538,753✔
1649

1650
  metaRLock(pMeta);
3,532,965✔
1651

1652
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
3,543,698✔
1653

1654
  tbDbKey.uid = param->suid;
3,544,399✔
1655
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,544,399✔
1656

1657
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
3,542,206✔
1658

1659
  tDecoderInit(&dc, pData, nData);
3,545,662✔
1660

1661
  code = metaDecodeEntry(&dc, &oStbEntry);
3,545,662✔
1662
  if (code) {
3,542,781✔
1663
    tDecoderClear(&dc);
×
1664
    goto END;
×
1665
  }
1666

1667
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
3,542,781✔
1668
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1669
  }
1670

1671
  code = TSDB_CODE_INVALID_PARA;
3,542,781✔
1672

1673
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,770,285✔
1674
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,763,301✔
1675
    if (IS_IDX_ON(schema)) {
4,764,148✔
1676
      if (schema->colId == param->cid && param->type == schema->type) {
4,722,485✔
1677
        code = 0;
3,534,098✔
1678
        break;
3,534,098✔
1679
      }
1680
    }
1681
  }
1682

1683
  TAOS_CHECK_GOTO(code, NULL, END);
3,541,082✔
1684

1685
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,534,581✔
1686
  if (code != 0) {
3,537,118✔
1687
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1688
  }
1689

1690
  int32_t maxSize = 0;
3,537,118✔
1691
  int32_t nTagData = 0;
3,537,118✔
1692
  void   *tagData = NULL;
3,537,118✔
1693

1694
  if (param->val == NULL) {
3,537,118✔
1695
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1696
    goto END;
×
1697
  } else {
1698
    if (IS_VAR_DATA_TYPE(param->type)) {
3,534,624✔
1699
      tagData = varDataVal(param->val);
424,870✔
1700
      nTagData = varDataLen(param->val);
427,919✔
1701

1702
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
427,436✔
1703
        maxSize = 4 * nTagData + 1;
74,536✔
1704
        buf = taosMemoryCalloc(1, maxSize);
74,536✔
1705
        if (buf == NULL) {
74,536✔
1706
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1707
        }
1708

1709
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
74,536✔
1710
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1711
        }
1712

1713
        tagData = buf;
74,536✔
1714
        nTagData = maxSize;
74,536✔
1715
      }
1716
    } else {
1717
      tagData = param->val;
3,107,746✔
1718
      nTagData = tDataTypes[param->type].bytes;
3,109,088✔
1719
    }
1720
  }
1721

1722
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
3,535,370✔
1723
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1724
                  NULL, END);
1725

1726
  int cmp = 0;
3,534,697✔
1727
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,536,273✔
1728

1729
  int     count = 0;
3,539,161✔
1730
  int32_t valid = 0;
3,539,161✔
1731
  bool    found = false;
3,539,161✔
1732

1733
  static const int8_t TRY_ERROR_LIMIT = 1;
1734

1735
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1736
  /// target:                        [suid, cid2, type2]
1737
  int diffCidCount = 0;
3,539,161✔
1738
  do {
34,845,468✔
1739
    void   *entryKey = NULL, *entryVal = NULL;
38,384,629✔
1740
    int32_t nEntryKey, nEntryVal;
38,380,453✔
1741

1742
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
38,378,521✔
1743
    if (valid < 0) {
38,373,893✔
1744
      break;
1,630,226✔
1745
    }
1746
    if (count > TRY_ERROR_LIMIT) {
36,743,667✔
1747
      break;
1,065,466✔
1748
    }
1749

1750
    STagIdxKey *p = entryKey;
35,678,201✔
1751
    if (p == NULL) break;
35,678,201✔
1752

1753
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
35,678,201✔
1754
      if (found == true) break;  //
1,144,783✔
1755
      if (diffCidCount > TRY_ERROR_LIMIT) break;
301,314✔
1756
      diffCidCount++;
301,314✔
1757
      count++;
301,314✔
1758
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
301,314✔
1759
      if (valid < 0) {
301,314✔
1760
        code = valid;
×
1761
        break;
×
1762
      } else {
1763
        continue;
301,314✔
1764
      }
1765
    }
1766

1767
    terrno = TSDB_CODE_SUCCESS;
34,537,081✔
1768
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
34,541,851✔
1769
    if (terrno != TSDB_CODE_SUCCESS) {
34,541,658✔
1770
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1771
      break;
×
1772
    }
1773
    if (cmp == 0) {
34,543,205✔
1774
      // match
1775
      tb_uid_t tuid = 0;
30,854,314✔
1776
      if (IS_VAR_DATA_TYPE(pKey->type)) {
30,852,714✔
1777
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
407,852✔
1778
      } else {
1779
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
30,433,948✔
1780
      }
1781
      if (taosArrayPush(pUids, &tuid) == NULL) {
30,863,368✔
1782
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1783
      }
1784
      found = true;
30,861,674✔
1785
    } else {
1786
      if (param->equal == true) {
3,688,891✔
1787
        if (count > TRY_ERROR_LIMIT) break;
2,562,928✔
1788
        count++;
2,562,928✔
1789
      }
1790
    }
1791
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
34,551,139✔
1792
    if (valid < 0) {
34,545,476✔
1793
      code = valid;
×
1794
      break;
×
1795
    }
1796
  } while (1);
1797

1798
END:
3,545,637✔
1799
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,545,662✔
1800
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,545,231✔
1801
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,543,001✔
1802
  tDecoderClear(&dc);
3,543,001✔
1803
  tdbFree(pData);
3,545,662✔
1804

1805
  taosMemoryFree(buf);
3,545,155✔
1806
  taosMemoryFree(pKey);
3,545,246✔
1807

1808
  taosMemoryFree(pCursor);
3,543,393✔
1809

1810
  return code;
3,542,913✔
1811
}
1812

1813
static int32_t metaGetTableTagByUidVersion(SMeta *pMeta, int64_t suid, int64_t uid, int64_t version, void** tag) {
56,010,935✔
1814
  void   *val = NULL;
56,010,935✔
1815
  int32_t len = 0;
56,011,351✔
1816
  int ret = 0;
56,012,083✔
1817

1818
  if (version != -1) {
56,012,083✔
1819
    SMetaReader mr = {0};
33,887✔
1820
    metaReaderDoInit(&mr, pMeta, META_READER_NOLOCK);
33,887✔
1821
    if (getUidVersion(&mr, &version, uid) != 0) {
33,887✔
1822
      version = -1;
×
1823
    }
1824

1825
    ret = metaGetTableEntryByVersion(&mr, version, uid);
33,498✔
1826
    if (ret == 0) {
33,498✔
1827
      val = mr.me.ctbEntry.pTags;
33,498✔
1828
      len = ((STag *)(mr.me.ctbEntry.pTags))->len;
33,498✔
1829
      *tag = taosMemoryMalloc(len);
33,498✔
1830
      if (*tag) {
33,109✔
1831
        memcpy(*tag, val, len);
33,498✔
1832
      } else {
1833
        ret = terrno;
×
1834
      }
1835
    }
1836
    metaReaderClear(&mr);
33,498✔
1837
  } else {
1838
    SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
55,978,196✔
1839
    ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), &val, &len);
55,978,196✔
1840
    if (ret == 0) {
55,976,489✔
1841
      *tag = taosMemoryMalloc(len);
55,969,091✔
1842
      if (*tag) {
55,967,795✔
1843
        memcpy(*tag, val, len);
55,967,795✔
1844
      } else {
1845
        ret = terrno;
×
1846
      }
1847
      tdbFree(val);
55,966,647✔
1848
    }
1849
  }
1850

1851
  return ret;
56,007,606✔
1852
}
1853

1854
int32_t metaGetTableTagsByUidsVersion(void *pVnode, int64_t suid, SArray *uidList, int64_t version) {
8,124,043✔
1855
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
8,124,043✔
1856
  const int32_t LIMIT = 128;
8,124,043✔
1857

1858
  int32_t isLock = false;
8,124,043✔
1859
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
8,124,043✔
1860
  for (int i = 0; i < sz; i++) {
64,129,540✔
1861
    STUidTagInfo *p = taosArrayGet(uidList, i);
56,005,333✔
1862
    if (p->pTagVal != NULL) continue;
56,007,440✔
1863

1864
    if (i % LIMIT == 0) {
56,007,440✔
1865
      if (isLock) metaULock(pMeta);
8,123,667✔
1866

1867
      metaRLock(pMeta);
8,123,667✔
1868
      isLock = true;
8,123,072✔
1869
    }
1870
    int32_t code = metaGetTableTagByUidVersion(pMeta, suid, p->uid, version, &p->pTagVal);
56,006,845✔
1871
    if (code != 0) {
56,006,229✔
1872
      metaError("vgId:%d, failed to table tags, code:%d, suid: %" PRId64 ", uid: %" PRId64 " version: %" PRId64, TD_VID(pMeta->pVnode), code, suid, p->uid, version);
7,398✔
1873
    }
1874
  }
1875
  if (isLock) metaULock(pMeta);
8,124,207✔
1876
  return 0;
8,123,654✔
1877
}
1878

1879
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
14,549,781✔
1880
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
14,549,781✔
1881
  if (!pCur) {
14,546,388✔
1882
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1883
  }
1884

1885
  while (1) {
172,826,090✔
1886
    tb_uid_t uid = metaCtbCursorNext(pCur);
187,372,478✔
1887
    if (uid == 0) {
187,300,378✔
1888
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
14,551,923✔
1889
      break;
14,551,207✔
1890
    }
1891

1892
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
172,748,455✔
1893
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
172,749,969✔
1894
    if (!info.pTagVal) {
172,759,398✔
1895
      metaCloseCtbCursor(pCur);
×
1896
      return terrno;
×
1897
    }
1898
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
172,759,398✔
1899
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
172,824,413✔
1900
      taosMemoryFreeClear(info.pTagVal);
×
1901
      metaCloseCtbCursor(pCur);
×
1902
      return terrno;
×
1903
    }
1904
  }
1905
  metaCloseCtbCursor(pCur);
14,552,600✔
1906
  return TSDB_CODE_SUCCESS;
14,547,561✔
1907
}
1908

1909
int32_t metaFlagCache(SVnode *pVnode) {
×
1910
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1911
  if (!pCur) {
×
1912
    return terrno;
×
1913
  }
1914

1915
  SArray *suids = NULL;
×
1916
  while (1) {
×
1917
    tb_uid_t id = metaStbCursorNext(pCur);
×
1918
    if (id == 0) {
×
1919
      break;
×
1920
    }
1921

1922
    if (!suids) {
×
1923
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1924
      if (!suids) {
×
1925
        return terrno;
×
1926
      }
1927
    }
1928

1929
    if (taosArrayPush(suids, &id) == NULL) {
×
1930
      taosArrayDestroy(suids);
×
1931
      return terrno;
×
1932
    }
1933
  }
1934

1935
  metaCloseStbCursor(pCur);
×
1936

1937
  for (int idx = 0; suids && idx < TARRAY_SIZE(suids); ++idx) {
×
1938
    tb_uid_t id = ((tb_uid_t *)TARRAY_DATA(suids))[idx];
×
1939
    STsdb   *pTsdb = pVnode->pTsdb;
×
1940
    SMeta   *pMeta = pVnode->pMeta;
×
1941
    SArray  *uids = NULL;
×
1942

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

1947
      taosArrayDestroy(uids);
×
1948
      taosArrayDestroy(suids);
×
1949

1950
      return code;
×
1951
    }
1952

1953
    if (uids && TARRAY_SIZE(uids) > 0) {
×
1954
      STSchema *pTSchema = NULL;
×
1955

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

1960
        taosArrayDestroy(uids);
×
1961
        taosArrayDestroy(suids);
×
1962

1963
        return code;
×
1964
      }
1965

1966
      int32_t nCol = pTSchema->numOfCols;
×
1967
      for (int32_t i = 0; i < nCol; ++i) {
×
1968
        int16_t cid = pTSchema->columns[i].colId;
×
1969
        int8_t  col_type = pTSchema->columns[i].type;
×
1970

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

1975
          tDestroyTSchema(pTSchema);
×
1976
          taosArrayDestroy(uids);
×
1977
          taosArrayDestroy(suids);
×
1978

1979
          return code;
×
1980
        }
1981
      }
1982

1983
      tDestroyTSchema(pTSchema);
×
1984
    }
1985

1986
    taosArrayDestroy(uids);
×
1987
  }
1988

1989
  taosArrayDestroy(suids);
×
1990

1991
  return TSDB_CODE_SUCCESS;
×
1992
}
1993

1994
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1995

1996
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
2,147,483,647✔
1997
  int32_t code = 0;
2,147,483,647✔
1998
  void   *pData = NULL;
2,147,483,647✔
1999
  int     nData = 0;
2,147,483,647✔
2000
  int     lock = 0;
2,147,483,647✔
2001

2002
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
2,147,483,647✔
2003
    lock = 1;
858,547,163✔
2004
  }
2005

2006
  if (!lock) metaRLock(pMeta);
2,147,483,647✔
2007

2008
  // search cache
2009
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
2,147,483,647✔
2010
    if (!lock) metaULock(pMeta);
2,147,483,647✔
2011
    goto _exit;
2,147,483,647✔
2012
  }
2013

2014
  // search TDB
2015
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
114,503,684✔
2016
    // not found
2017
    if (!lock) metaULock(pMeta);
103,754,380✔
2018
    goto _exit;
103,742,619✔
2019
  }
2020

2021
  if (!lock) metaULock(pMeta);
10,753,619✔
2022

2023
  pInfo->uid = uid;
10,753,619✔
2024
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
10,753,619✔
2025
  pInfo->version = ((SUidIdxVal *)pData)->version;
10,753,005✔
2026
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
10,753,005✔
2027

2028
  if (lock) {
10,752,231✔
2029
    metaULock(pReader->pMeta);
1,381,781✔
2030
    // metaReaderReleaseLock(pReader);
2031
  }
2032
  // upsert the cache
2033
  metaWLock(pMeta);
10,753,005✔
2034
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
10,753,005✔
2035
  if (ret != 0) {
10,751,029✔
2036
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
2037
  }
2038
  metaULock(pMeta);
10,751,029✔
2039

2040
  if (lock) {
10,751,803✔
2041
    metaRLock(pReader->pMeta);
1,381,967✔
2042
  }
2043

2044
_exit:
2,147,483,647✔
2045
  tdbFree(pData);
2,147,483,647✔
2046
  return code;
2,147,483,647✔
2047
}
2048

2049
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
147,001,201✔
2050
  int32_t code = 0;
147,001,201✔
2051

2052
  if (!numOfTables && !numOfCols) goto _exit;
147,001,201✔
2053

2054
  SVnode *pVnodeObj = pVnode;
147,001,201✔
2055
  metaRLock(pVnodeObj->pMeta);
147,001,201✔
2056

2057
  // fast path: search cache
2058
  SMetaStbStats state = {0};
146,996,992✔
2059
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
147,003,169✔
2060
    metaULock(pVnodeObj->pMeta);
141,371,679✔
2061
    if (numOfTables) *numOfTables = state.ctbNum;
141,384,828✔
2062
    if (numOfCols) *numOfCols = state.colNum;
141,385,547✔
2063
    if (flags) *flags = state.flags;
141,359,654✔
2064
    goto _exit;
141,359,327✔
2065
  }
2066

2067
  // slow path: search TDB
2068
  int64_t ctbNum = 0;
5,653,089✔
2069
  int32_t colNum = 0;
5,653,552✔
2070
  int64_t keep = 0;
5,653,926✔
2071
  int8_t  flag = 0;
5,654,518✔
2072

2073
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
5,655,193✔
2074
  if (TSDB_CODE_SUCCESS == code) {
5,655,854✔
2075
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
5,655,854✔
2076
  }
2077
  if (TSDB_CODE_SUCCESS == code) {
5,656,146✔
2078
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
5,656,146✔
2079
  }
2080
  metaULock(pVnodeObj->pMeta);
5,655,995✔
2081
  if (TSDB_CODE_SUCCESS != code) {
5,656,872✔
2082
    goto _exit;
×
2083
  }
2084

2085
  if (numOfTables) *numOfTables = ctbNum;
5,656,872✔
2086
  if (numOfCols) *numOfCols = colNum;
5,656,872✔
2087
  if (flags) *flags = flag;
5,656,890✔
2088

2089
  state.uid = uid;
5,656,890✔
2090
  state.ctbNum = ctbNum;
5,656,890✔
2091
  state.colNum = colNum;
5,656,890✔
2092
  state.flags = flag;
5,656,890✔
2093
  state.keep = keep;
5,656,890✔
2094
  // upsert the cache
2095
  metaWLock(pVnodeObj->pMeta);
5,656,890✔
2096

2097
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
5,656,090✔
2098
  if (ret) {
5,655,562✔
2099
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
2100
              uid, ctbNum, colNum, keep, flag);
2101
  }
2102

2103
  metaULock(pVnodeObj->pMeta);
5,655,562✔
2104

2105
_exit:
147,015,974✔
2106
  return code;
147,021,964✔
2107
}
2108

2109
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
90,403,953✔
2110
  SMetaStbStats stats = {0};
90,403,953✔
2111

2112
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
90,412,659✔
2113
    stats.ctbNum += deltaCtb;
77,926,532✔
2114
    stats.colNum += deltaCol;
77,926,532✔
2115
    if (deltaKeep > 0) {
77,926,532✔
2116
      stats.keep = deltaKeep;
7,905✔
2117
    }
2118

2119
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
77,926,532✔
2120
    if (code) {
77,898,893✔
2121
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
2122
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
2123
    }
2124
  }
2125
}
90,398,766✔
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