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

taosdata / TDengine / #5019

10 Apr 2026 06:58AM UTC coverage: 72.254% (-0.002%) from 72.256%
#5019

push

travis-ci

web-flow
merge: from main to 3.0 #35103

77 of 116 new or added lines in 3 files covered. (66.38%)

2985 existing lines in 127 files now uncovered.

257415 of 356263 relevant lines covered (72.25%)

134284372.15 hits per line

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

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

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

19
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
806,178,300✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
806,178,300✔
21
  metaReaderDoInit(pReader, pMeta, flags);
806,674,749✔
22
  pReader->pAPI = pAPI;
806,638,415✔
23
}
806,695,325✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
936,980,300✔
26
  memset(pReader, 0, sizeof(*pReader));
936,980,300✔
27
  pReader->pMeta = pMeta;
936,980,300✔
28
  pReader->flags = flags;
937,020,043✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
936,717,742✔
30
    metaRLock(pMeta);
797,686,248✔
31
  }
32
}
937,050,223✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
79,511,802✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
79,511,802✔
36
    metaULock(pReader->pMeta);
79,524,582✔
37
    pReader->flags |= META_READER_NOLOCK;
79,476,261✔
38
  }
39
}
79,541,133✔
40

41
void metaReaderClear(SMetaReader *pReader) {
1,196,208,265✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,196,208,265✔
43
    metaULock(pReader->pMeta);
717,590,389✔
44
  }
45
  tDecoderClear(&pReader->coder);
1,195,677,918✔
46
  tdbFree(pReader->pBuf);
1,197,419,574✔
47
  pReader->pBuf = NULL;
1,196,651,158✔
48
}
1,196,801,203✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
1,520,773,419✔
51
  int32_t  code = 0;
1,520,773,419✔
52
  SMeta   *pMeta = pReader->pMeta;
1,520,773,419✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
1,521,486,684✔
54

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

60
  // decode the entry
61
  tDecoderClear(&pReader->coder);
1,520,660,520✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
1,520,655,444✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
1,520,909,118✔
65
  if (code) {
1,520,289,313✔
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
1,520,289,313✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
151,346,801✔
79
    metaULock(pVnodeObj->pMeta);
49,465✔
80
    return false;
49,465✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
151,385,836✔
84
  return true;
151,402,753✔
85
}
86

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

91
  // query uid.idx
92
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
270,975,899✔
93
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
88,125✔
94
  }
95

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
270,897,656✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
270,879,955✔
98
}
99

100
static int32_t getUidVersion(SMetaReader *pReader, int64_t *version, tb_uid_t uid) {
×
101
  int32_t code = 0;
×
102
  SMeta *pMeta = pReader->pMeta;
×
103
  void* pKey = NULL;
×
104
  void* pVal = NULL;
×
105
  int   vLen = 0, kLen = 0;
×
106

107
  TBC* pCur = NULL;
×
108
  code = tdbTbcOpen(pMeta->pTbDb, (TBC**)&pCur, NULL);
×
109
  if (code != 0) {
×
110
    return TAOS_GET_TERRNO(code);
×
111
  }
112
  STbDbKey key = {.version = *version, .uid = INT64_MAX};
×
113
  int      c = 0;
×
114
  code = tdbTbcMoveTo(pCur, &key, sizeof(key), &c);
×
115
  if (code != 0) {
×
116
    goto END;
×
117
  }
118
  if (c >= 0){
×
119
    metaError("%s move to version:%"PRId64 " max failed", __func__, *version);
×
120
    code = TSDB_CODE_FAILED;
×
121
    goto END;
×
122
  }
123
  code = tdbTbcMoveToPrev(pCur);
×
124
  if (code != 0) {
×
125
    metaError("%s move to prev failed", __func__);
×
126
    goto END;
×
127
  }
128

129
  while (1) {
×
130
    int32_t ret = tdbTbcPrev(pCur, &pKey, &kLen, &pVal, &vLen);
×
131
    if (ret < 0) break;
×
132

133
    STbDbKey* tmp = (STbDbKey*)pKey;
×
134
    if (tmp->uid == uid) {
×
135
      *version = tmp->version;
×
136
      goto END;
×
137
    }
138
  }
139
  code = TSDB_CODE_NOT_FOUND;
×
140
  metaError("%s uid:%" PRId64 " version:%" PRId64 " not found", __func__, uid, *version);
×
141
END:
×
142
  tdbFree(pKey);
×
143
  tdbFree(pVal);
×
144
  tdbTbcClose(pCur);
×
145
  return code;
×
146
} 
147

148
// 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
149
int metaReaderGetTableEntryByVersionUid(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
83,837,453✔
150
  if (version < 0) {
83,837,453✔
151
    return metaReaderGetTableEntryByUid(pReader, uid);
83,839,195✔
152
  }
153
  SMeta *pMeta = pReader->pMeta;
40✔
154

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

170
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
745,959,449✔
171
  SMeta *pMeta = pReader->pMeta;
745,959,449✔
172

173
  SMetaInfo info;
746,486,796✔
174
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
746,383,736✔
175
  if (TSDB_CODE_SUCCESS != code) {
746,169,840✔
176
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
6,284✔
177
  }
178

179
  return metaGetTableEntryByVersion(pReader, info.version, uid);
746,163,556✔
180
}
181

182
int metaGetTableEntryByVersionName(SMetaReader *pReader, int64_t version, const char *name) {
91,543,118✔
183
  SMeta   *pMeta = pReader->pMeta;
91,543,118✔
184
  tb_uid_t uid;
185

186
  // query name.idx
187
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
91,550,306✔
188
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
10,139,685✔
189
  }
190

191
  uid = *(tb_uid_t *)pReader->pBuf;
81,378,257✔
192
  return metaReaderGetTableEntryByVersionUid(pReader, version, uid);
81,378,460✔
193
}
194

195
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
91,179,682✔
196
  return metaGetTableEntryByVersionName(pReader, -1, name);
91,179,682✔
197
}
198

199
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
76,223,324✔
200
  void    *pData = NULL;
76,223,324✔
201
  int      nData = 0;
76,225,557✔
202
  tb_uid_t uid = 0;
76,226,064✔
203

204
  metaRLock(pMeta);
76,226,064✔
205

206
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
76,224,807✔
207
    uid = *(tb_uid_t *)pData;
10,168,742✔
208
    tdbFree(pData);
10,168,742✔
209
  }
210

211
  metaULock(pMeta);
76,202,632✔
212

213
  return uid;
76,214,219✔
214
}
215

216
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
7,555,617✔
217
  int         code = 0;
7,555,617✔
218
  SMetaReader mr = {0};
7,555,617✔
219
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
7,555,567✔
220
  code = metaReaderGetTableEntryByUid(&mr, uid);
7,554,022✔
221
  if (code < 0) {
7,553,695✔
222
    metaReaderClear(&mr);
269✔
223
    return code;
269✔
224
  }
225

226
  STR_TO_VARSTR(tbName, mr.me.name);
7,553,426✔
227
  metaReaderClear(&mr);
7,548,663✔
228

229
  return 0;
7,555,810✔
230
}
231

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

244
  return 0;
×
245
}
246

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

252
  SMetaReader *pReader = &mr;
1,923,941✔
253

254
  // query name.idx
255
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
1,923,941✔
256
    metaReaderClear(&mr);
1,302,044✔
257
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1,302,596✔
258
  }
259

260
  *uid = *(tb_uid_t *)pReader->pBuf;
621,350✔
261

262
  metaReaderClear(&mr);
621,306✔
263

264
  return 0;
621,113✔
265
}
266

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

272
  code = metaGetTableEntryByName(&mr, tbName);
618,484✔
273
  if (code == 0) *tbType = mr.me.type;
618,309✔
274
  if (TSDB_CHILD_TABLE == mr.me.type || TSDB_VIRTUAL_CHILD_TABLE == mr.me.type) {
618,309✔
275
    *suid = mr.me.ctbEntry.suid;
611,802✔
276
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
6,507✔
277
    *suid = mr.me.uid;
4,969✔
278
  } else {
279
    *suid = 0;
1,538✔
280
  }
281

282
  metaReaderClear(&mr);
617,859✔
283
  return code;
618,309✔
284
}
285

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

289
  // TODO
290

291
  return 0;
×
292
}
293

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

310
  code = 0;
×
311

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

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

322
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
24,131,771✔
323
  if (pTbCur == NULL) {
24,129,367✔
324
    return NULL;
×
325
  }
326

327
  SVnode *pVnodeObj = pVnode;
24,129,367✔
328
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
329
  pTbCur->pMeta = pVnodeObj->pMeta;
24,129,367✔
330
  pTbCur->paused = 1;
24,139,274✔
331
  code = metaResumeTbCursor(pTbCur, 1, 0);
24,138,802✔
332
  if (code) {
24,118,622✔
333
    terrno = code;
×
334
    taosMemoryFree(pTbCur);
×
335
    return NULL;
×
336
  }
337
  return pTbCur;
24,118,622✔
338
}
339

340
void metaCloseTbCursor(SMTbCursor *pTbCur) {
48,271,616✔
341
  if (pTbCur) {
48,271,616✔
342
    tdbFree(pTbCur->pKey);
24,140,991✔
343
    tdbFree(pTbCur->pVal);
24,142,607✔
344
    if (!pTbCur->paused) {
24,145,154✔
345
      metaReaderClear(&pTbCur->mr);
12,279,230✔
346
      if (pTbCur->pDbc) {
12,279,782✔
347
        tdbTbcClose((TBC *)pTbCur->pDbc);
12,279,186✔
348
      }
349
    }
350
    taosMemoryFree(pTbCur);
24,139,039✔
351
  }
352
}
48,271,704✔
353

354
void metaPauseTbCursor(SMTbCursor *pTbCur) {
11,866,678✔
355
  if (!pTbCur->paused) {
11,866,678✔
356
    metaReaderClear(&pTbCur->mr);
11,866,880✔
357
    tdbTbcClose((TBC *)pTbCur->pDbc);
11,867,488✔
358
    pTbCur->paused = 1;
11,866,175✔
359
  }
360
}
11,866,581✔
361
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
24,135,315✔
362
  int32_t code = 0;
24,135,315✔
363
  int32_t lino;
364
  int8_t  locked = 0;
24,135,315✔
365
  if (pTbCur->paused) {
24,135,315✔
366
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
24,128,192✔
367
    locked = 1;
24,138,208✔
368
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
24,138,208✔
369
    if (code != 0) {
24,121,918✔
370
      TSDB_CHECK_CODE(code, lino, _exit);
×
371
    }
372

373
    if (first) {
24,121,918✔
374
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
24,119,359✔
375
      TSDB_CHECK_CODE(code, lino, _exit);
24,104,311✔
376
    } else {
377
      int c = 1;
2,559✔
378
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
2,559✔
379
      TSDB_CHECK_CODE(code, lino, _exit);
2,559✔
380
      if (c == 0) {
2,559✔
381
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
2,559✔
382
      } else if (c < 0) {
×
383
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
384
        TSDB_CHECK_CODE(code, lino, _exit);
×
385
      } else {
386
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
387
        TSDB_CHECK_CODE(code, lino, _exit);
×
388
      }
389
    }
390

391
    pTbCur->paused = 0;
24,117,179✔
392
  }
393

394
_exit:
×
395
  if (code != 0 && locked) {
24,127,091✔
396
    metaReaderReleaseLock(&pTbCur->mr);
×
397
  }
398
  return code;
24,118,732✔
399
}
400

401
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
528,322,877✔
402
  int    ret;
403
  void  *pBuf;
404
  STbCfg tbCfg;
405

406
  for (;;) {
407
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
528,322,877✔
408
    if (ret < 0) {
528,298,261✔
409
      return ret;
24,140,608✔
410
    }
411

412
    tDecoderClear(&pTbCur->mr.coder);
504,157,653✔
413

414
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
504,275,020✔
415
    if (ret) return ret;
504,119,148✔
416

417
    if (pTbCur->mr.me.type == jumpTableType) {
504,119,148✔
418
      continue;
4,414,570✔
419
    }
420

421
    break;
499,706,437✔
422
  }
423

424
  return 0;
499,706,437✔
425
}
426

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

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

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

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

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

449
    break;
×
450
  }
451

452
  return 0;
×
453
}
454

455
/**
456
 * @param type 0x01 fetchRsmaSchema if table is rsma
457
 */
458
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
353,784,793✔
459
                                   int8_t type) {
460
  int32_t         code = 0;
353,784,793✔
461
  void           *pData = NULL;
353,784,793✔
462
  int             nData = 0;
353,951,286✔
463
  int64_t         version;
464
  SSchemaWrapper  schema = {0};
353,773,875✔
465
  SSchemaWrapper *pSchema = NULL;
353,086,779✔
466
  SDecoder        dc = {0};
353,086,779✔
467
  if (lock) {
352,313,409✔
468
    metaRLock(pMeta);
347,599,101✔
469
  }
470
_query:
387,942,613✔
471
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
387,555,910✔
472
    goto _err;
142,909✔
473
  }
474

475
  version = ((SUidIdxVal *)pData)[0].version;
388,426,342✔
476

477
  if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData)) !=
387,983,590✔
478
      0) {
479
    goto _err;
×
480
  }
481

482
  SMetaEntry me = {0};
388,530,802✔
483
  tDecoderInit(&dc, pData, nData);
388,255,374✔
484
  code = metaDecodeEntry(&dc, &me);
388,527,534✔
485
  if (code) {
388,137,143✔
486
    tDecoderClear(&dc);
×
487
    goto _err;
×
488
  }
489
  if (me.type == TSDB_SUPER_TABLE) {
388,137,143✔
490
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
273,732,404✔
491
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
273,576,473✔
492
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
273,576,473✔
493
      if (TABLE_IS_ROLLUP(me.flags)) {
273,566,314✔
494
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
110,110✔
495
          tDecoderClear(&dc);
×
496
          goto _err;
×
497
        }
498
      }
499
      tDecoderClear(&dc);
273,566,314✔
500
      goto _exit;
273,531,280✔
501
    }
502
  } else if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
114,404,739✔
503
    uid = me.ctbEntry.suid;
34,143,549✔
504
    tDecoderClear(&dc);
34,143,549✔
505
    goto _query;
34,169,018✔
506
  } else {
507
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
80,264,734✔
508
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
80,314,625✔
509
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
80,314,625✔
510
      tDecoderClear(&dc);
80,304,513✔
511
      goto _exit;
80,345,842✔
512
    }
513
  }
514
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
33,465✔
515
  tDecoderClear(&dc);
33,465✔
516

517
  // query from skm db
518
  if ((code = tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) < 0) {
38,528✔
519
    goto _err;
×
520
  }
521

522
  tDecoderInit(&dc, pData, nData);
38,528✔
523
  if ((code = tDecodeSSchemaWrapperEx(&dc, &schema)) != 0) {
38,528✔
524
    goto _err;
×
525
  }
526
  pSchema = tCloneSSchemaWrapper(&schema);
38,528✔
527
  if (pSchema == NULL) {
38,528✔
528
    code = terrno;
×
529
    goto _err;
×
530
  }
531
  tDecoderClear(&dc);
38,528✔
532

533
_exit:
353,915,650✔
534
  if (lock) {
353,969,666✔
535
    metaULock(pMeta);
349,270,818✔
536
  }
537
  tdbFree(pData);
353,702,265✔
538
  return pSchema;
353,897,797✔
539

540
_err:
153,157✔
541
  if (lock) {
142,909✔
542
    metaULock(pMeta);
142,195✔
543
  }
544
  tdbFree(pData);
142,909✔
545
  tDeleteSchemaWrapper(pSchema);
546
  if (extSchema != NULL) {
142,590✔
547
    taosMemoryFreeClear(*extSchema);
8,665✔
548
  }
549
  terrno = code;
142,590✔
550
  return NULL;
142,590✔
551
}
552

553
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
650,594✔
554
  void    *pData = NULL;
650,594✔
555
  int      nData = 0;
650,933✔
556
  int64_t  version = 0;
650,933✔
557
  SDecoder dc = {0};
650,933✔
558
  int64_t  createTime = INT64_MAX;
650,933✔
559
  if (lock) {
650,933✔
560
    metaRLock(pMeta);
650,933✔
561
  }
562

563
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
650,594✔
564
    goto _exit;
×
565
  }
566

567
  version = ((SUidIdxVal *)pData)[0].version;
650,255✔
568

569
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
650,594✔
570
    goto _exit;
×
571
  }
572

573
  SMetaEntry me = {0};
650,933✔
574
  tDecoderInit(&dc, pData, nData);
650,933✔
575
  int32_t code = metaDecodeEntry(&dc, &me);
650,933✔
576
  if (code) {
650,933✔
577
    tDecoderClear(&dc);
×
578
    goto _exit;
×
579
  }
580
  if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
650,933✔
581
    createTime = me.ctbEntry.btime;
650,587✔
582
  } else if (me.type == TSDB_NORMAL_TABLE || me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
346✔
583
    createTime = me.ntbEntry.btime;
346✔
584
  }
585
  tDecoderClear(&dc);
650,933✔
586

587
_exit:
650,255✔
588
  if (lock) {
650,594✔
589
    metaULock(pMeta);
650,594✔
590
  }
591
  tdbFree(pData);
649,916✔
592
  return createTime;
650,933✔
593
}
594

595
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
140,523,860✔
596
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
140,523,860✔
597
  SMCtbCursor *pCtbCur = NULL;
140,538,513✔
598
  SCtbIdxKey   ctbIdxKey;
599
  int          ret = 0;
140,538,513✔
600
  int          c = 0;
140,538,513✔
601

602
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
140,538,513✔
603
  if (pCtbCur == NULL) {
140,327,594✔
604
    return NULL;
×
605
  }
606

607
  pCtbCur->pMeta = pMeta;
140,327,594✔
608
  pCtbCur->suid = uid;
140,330,714✔
609
  pCtbCur->lock = lock;
140,431,998✔
610
  pCtbCur->paused = 1;
140,427,698✔
611

612
  ret = metaResumeCtbCursor(pCtbCur, 1);
140,445,920✔
613
  if (ret < 0) {
140,375,359✔
614
    return NULL;
×
615
  }
616
  return pCtbCur;
140,375,359✔
617
}
618

619
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
140,539,790✔
620
  if (pCtbCur) {
140,539,790✔
621
    if (!pCtbCur->paused) {
140,567,549✔
622
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
135,794,181✔
623
      if (pCtbCur->pCur) {
135,791,297✔
624
        tdbTbcClose(pCtbCur->pCur);
135,817,089✔
625
      }
626
    }
627
    tdbFree(pCtbCur->pKey);
140,434,951✔
628
    tdbFree(pCtbCur->pVal);
140,446,874✔
629
  }
630
  taosMemoryFree(pCtbCur);
140,408,097✔
631
}
140,374,354✔
632

633
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
4,793,377✔
634
  if (!pCtbCur->paused) {
4,793,377✔
635
    tdbTbcClose((TBC *)pCtbCur->pCur);
4,792,754✔
636
    if (pCtbCur->lock) {
4,792,246✔
637
      metaULock(pCtbCur->pMeta);
4,790,824✔
638
    }
639
    pCtbCur->paused = 1;
4,797,418✔
640
  }
641
}
4,802,514✔
642

643
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
140,530,982✔
644
  if (pCtbCur->paused) {
140,530,982✔
645
    pCtbCur->paused = 0;
140,509,433✔
646

647
    if (pCtbCur->lock) {
140,511,985✔
648
      metaRLock(pCtbCur->pMeta);
135,721,192✔
649
    }
650
    int ret = 0;
140,483,604✔
651
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
140,483,604✔
652
    if (ret < 0) {
140,428,784✔
653
      metaCloseCtbCursor(pCtbCur);
×
654
      return -1;
×
655
    }
656

657
    if (first) {
140,428,784✔
658
      SCtbIdxKey ctbIdxKey;
140,425,113✔
659
      // move to the suid
660
      ctbIdxKey.suid = pCtbCur->suid;
140,436,439✔
661
      ctbIdxKey.uid = INT64_MIN;
140,474,652✔
662
      int c = 0;
140,474,652✔
663
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
140,443,232✔
664
      if (c > 0) {
140,388,536✔
665
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
28,312,473✔
666
      }
667
    } else {
668
      int c = 0;
×
669
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
670
      if (c < 0) {
×
671
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
672
      } else {
673
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
674
      }
675
    }
676
  }
677
  return 0;
140,458,273✔
678
}
679

680
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
784,370,113✔
681
  int         ret;
682
  SCtbIdxKey *pCtbIdxKey;
683

684
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
784,370,113✔
685
  if (ret < 0) {
784,239,824✔
686
    return 0;
79,378,705✔
687
  }
688

689
  pCtbIdxKey = pCtbCur->pKey;
704,861,119✔
690
  if (pCtbIdxKey->suid > pCtbCur->suid) {
704,877,835✔
691
    return 0;
61,317,542✔
692
  }
693

694
  return pCtbIdxKey->uid;
643,761,423✔
695
}
696

697
struct SMStbCursor {
698
  SMeta   *pMeta;
699
  TBC     *pCur;
700
  tb_uid_t suid;
701
  void    *pKey;
702
  void    *pVal;
703
  int      kLen;
704
  int      vLen;
705
};
706

707
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
61,881,067✔
708
  SMStbCursor *pStbCur = NULL;
61,881,067✔
709
  int          ret = 0;
61,881,067✔
710
  int          c = 0;
61,881,067✔
711

712
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
61,883,083✔
713
  if (pStbCur == NULL) {
61,883,510✔
714
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
715
    return NULL;
×
716
  }
717

718
  pStbCur->pMeta = pMeta;
61,883,510✔
719
  pStbCur->suid = suid;
61,880,270✔
720
  metaRLock(pMeta);
61,874,677✔
721

722
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
61,887,787✔
723
  if (ret < 0) {
61,872,770✔
724
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
725
    metaULock(pMeta);
×
726
    taosMemoryFree(pStbCur);
×
727
    return NULL;
×
728
  }
729

730
  // move to the suid
731
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
61,872,770✔
732
  if (c > 0) {
61,892,626✔
733
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
734
  }
735

736
  return pStbCur;
61,885,763✔
737
}
738

739
void metaCloseStbCursor(SMStbCursor *pStbCur) {
61,883,227✔
740
  if (pStbCur) {
61,883,227✔
741
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
61,884,571✔
742
    if (pStbCur->pCur) {
61,894,177✔
743
      tdbTbcClose(pStbCur->pCur);
61,893,505✔
744

745
      tdbFree(pStbCur->pKey);
61,884,585✔
746
      tdbFree(pStbCur->pVal);
61,882,508✔
747
    }
748

749
    taosMemoryFree(pStbCur);
61,887,212✔
750
  }
751
}
61,881,836✔
752

753
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
99,517,795✔
754
  int ret;
755

756
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
99,517,795✔
757
  if (ret < 0) {
99,511,000✔
758
    return 0;
61,892,813✔
759
  }
760
  return *(tb_uid_t *)pStbCur->pKey;
37,618,187✔
761
}
762

763
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
301,107,982✔
764
  STSchema       *pTSchema = NULL;
301,107,982✔
765
  SSchemaWrapper *pSW = NULL;
301,107,982✔
766

767
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
301,107,982✔
768
  if (!pSW) return NULL;
301,235,625✔
769

770
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
301,200,458✔
771

772
  tDeleteSchemaWrapper(pSW);
773
  return pTSchema;
301,045,576✔
774
}
775

776
/**
777
 * Fetch rsma schema if table is rsma
778
 */
779
SRSchema *metaGetTbTSchemaR(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
28,490✔
780
  SRSchema       *pRSchema = NULL;
28,490✔
781
  SSchemaWrapper *pSW = NULL;
28,490✔
782

783
  if (!(pRSchema = (SRSchema *)taosMemoryCalloc(1, sizeof(SRSchema)))) goto _err;
28,490✔
784
  if (!(pSW = metaGetTableSchema(pMeta, uid, sver, lock, (SExtSchema **)&pRSchema->extSchema, 0x01))) goto _err;
28,490✔
785
  if (!(pRSchema->tSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version))) goto _err;
28,490✔
786

787
  if (pSW->pRsma) {
28,490✔
788
    if (!(pRSchema->funcIds = taosMemoryCalloc(pSW->nCols, sizeof(func_id_t)))) goto _err;
28,490✔
789
    memcpy(pRSchema->funcIds, pSW->pRsma->funcIds, pSW->nCols * sizeof(func_id_t));
28,490✔
790

791
    pRSchema->tbType = pSW->pRsma->tbType;
28,490✔
792
    pRSchema->tbUid = uid;
28,490✔
793
    tstrncpy(pRSchema->tbName, pSW->pRsma->tbName, TSDB_TABLE_NAME_LEN);
28,490✔
794
    pRSchema->interval[0] = pSW->pRsma->interval[0];
28,490✔
795
    pRSchema->interval[1] = pSW->pRsma->interval[1];
28,490✔
796
  }
797

798
_exit:
28,490✔
799
  tDeleteSchemaWrapper(pSW);
800
  return pRSchema;
28,490✔
801
_err:
×
802
  tDeleteSchemaWrapper(pSW);
803
  tFreeSRSchema(&pRSchema);
804
  return NULL;
×
805
}
806

807
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
650,928✔
808
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
650,928✔
809
  if (*ppTSchema == NULL) {
650,242✔
810
    return terrno;
×
811
  }
812
  return TSDB_CODE_SUCCESS;
648,441✔
813
}
814

815
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
300,426,454✔
816
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
300,426,454✔
817
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
300,211,560✔
818
    return terrno;
×
819
  }
820
  return TSDB_CODE_SUCCESS;
299,958,842✔
821
}
822

823
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
73,248,106✔
824
  int32_t code = 0;
73,248,106✔
825
  int32_t lino;
826

827
  void     *pData = NULL;
73,248,106✔
828
  int       nData = 0;
73,281,798✔
829
  SSkmDbKey skmDbKey;
73,302,680✔
830
  if (sver <= 0) {
73,298,641✔
831
    SMetaInfo info;
37,056,972✔
832
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
37,059,883✔
833
      sver = info.skmVer;
37,021,419✔
834
    } else {
835
      TBC *pSkmDbC = NULL;
34,457✔
836
      int  c;
35,167✔
837

838
      skmDbKey.uid = suid ? suid : uid;
35,167✔
839
      skmDbKey.sver = INT32_MAX;
35,167✔
840

841
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
35,167✔
842
      TSDB_CHECK_CODE(code, lino, _exit);
35,167✔
843
      metaRLock(pMeta);
35,167✔
844

845
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
35,167✔
846
        metaULock(pMeta);
×
847
        tdbTbcClose(pSkmDbC);
×
848
        code = TSDB_CODE_NOT_FOUND;
×
849
        goto _exit;
×
850
      }
851

852
      if (c == 0) {
35,167✔
853
        metaULock(pMeta);
×
854
        tdbTbcClose(pSkmDbC);
×
855
        code = TSDB_CODE_FAILED;
×
856
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
857
        goto _exit;
×
858
      }
859

860
      if (c < 0) {
35,167✔
861
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
862
      }
863

864
      const void *pKey = NULL;
35,167✔
865
      int32_t     nKey = 0;
35,167✔
866
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
35,167✔
867

868
      if (ret != 0 || ((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
35,167✔
869
        metaULock(pMeta);
×
870
        tdbTbcClose(pSkmDbC);
×
871
        code = TSDB_CODE_NOT_FOUND;
×
872
        goto _exit;
×
873
      }
874

875
      sver = ((SSkmDbKey *)pKey)->sver;
35,167✔
876

877
      metaULock(pMeta);
35,167✔
878
      tdbTbcClose(pSkmDbC);
35,167✔
879
    }
880
  }
881

882
  if (!(sver > 0)) {
73,265,806✔
883
    code = TSDB_CODE_NOT_FOUND;
×
884
    goto _exit;
×
885
  }
886

887
  skmDbKey.uid = suid ? suid : uid;
73,265,806✔
888
  skmDbKey.sver = sver;
73,265,806✔
889
  metaRLock(pMeta);
73,265,806✔
890
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
73,339,201✔
891
    metaULock(pMeta);
2,784✔
892
    code = TSDB_CODE_NOT_FOUND;
2,784✔
893
    goto _exit;
2,784✔
894
  }
895
  metaULock(pMeta);
73,277,733✔
896

897
  // decode
898
  SDecoder        dc = {0};
73,353,020✔
899
  SSchemaWrapper  schema;
73,329,822✔
900
  SSchemaWrapper *pSchemaWrapper = &schema;
73,337,181✔
901

902
  tDecoderInit(&dc, pData, nData);
73,337,181✔
903
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
73,393,304✔
904
  tDecoderClear(&dc);
73,393,304✔
905
  tdbFree(pData);
73,359,147✔
906
  if (TSDB_CODE_SUCCESS != code) {
73,273,698✔
907
    taosMemoryFree(pSchemaWrapper->pSchema);
×
908
    goto _exit;
×
909
  }
910

911
  // convert
912
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
73,273,698✔
913
  if (pTSchema == NULL) {
73,343,113✔
914
    code = TSDB_CODE_OUT_OF_MEMORY;
×
915
  }
916

917
  *ppTSchema = pTSchema;
73,343,113✔
918
  taosMemoryFree(pSchemaWrapper->pSchema);
73,351,935✔
919

920
_exit:
73,284,761✔
921
  return code;
73,306,989✔
922
}
923

924
// N.B. Called by statusReq per second
925
int64_t metaGetTbNum(SMeta *pMeta) {
127,954,634✔
926
  // num of child tables (excluding normal tables , stables and others)
927

928
  /* int64_t num = 0; */
929
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
930

931
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
127,954,634✔
932
}
933

934
void metaUpdTimeSeriesNum(SMeta *pMeta) {
61,875,798✔
935
  int64_t nCtbTimeSeries = 0;
61,875,798✔
936
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
61,881,846✔
937
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
61,884,192✔
938
  }
939
}
61,889,238✔
940

941
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
942
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
943
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
215,068,449✔
944
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
215,079,717✔
945
    metaUpdTimeSeriesNum(pMeta);
61,875,180✔
946
  }
947

948
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
215,089,634✔
949
}
950

951
// type: 1 reported timeseries
952
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
215,053,596✔
953
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
215,056,280✔
954
  if (type == 1) {
215,056,280✔
955
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
127,954,634✔
956
  }
957
  return nTimeSeries;
215,020,885✔
958
}
959

960
typedef struct {
961
  SMeta   *pMeta;
962
  TBC     *pCur;
963
  tb_uid_t uid;
964
  void    *pKey;
965
  void    *pVal;
966
  int      kLen;
967
  int      vLen;
968
} SMSmaCursor;
969

970
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
971
  SMSmaCursor *pSmaCur = NULL;
×
972
  SSmaIdxKey   smaIdxKey;
×
973
  int          ret;
974
  int          c;
×
975

976
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
977
  if (pSmaCur == NULL) {
×
978
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
979
    return NULL;
×
980
  }
981

982
  pSmaCur->pMeta = pMeta;
×
983
  pSmaCur->uid = uid;
×
984
  metaRLock(pMeta);
×
985

986
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
987
  if (ret < 0) {
×
988
    metaULock(pMeta);
×
989
    taosMemoryFree(pSmaCur);
×
990
    return NULL;
×
991
  }
992

993
  // move to the suid
994
  smaIdxKey.uid = uid;
×
995
  smaIdxKey.smaUid = INT64_MIN;
×
996
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
997
  if (c > 0) {
×
998
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
999
  }
1000

1001
  return pSmaCur;
×
1002
}
1003

1004
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
1005
  if (pSmaCur) {
×
1006
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
1007
    if (pSmaCur->pCur) {
×
1008
      tdbTbcClose(pSmaCur->pCur);
×
1009
      pSmaCur->pCur = NULL;
×
1010

1011
      tdbFree(pSmaCur->pKey);
×
1012
      tdbFree(pSmaCur->pVal);
×
1013
    }
1014

1015
    taosMemoryFree(pSmaCur);
×
1016
  }
1017
}
×
1018

1019
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
1020
  int         ret;
1021
  SSmaIdxKey *pSmaIdxKey;
1022

1023
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
1024
  if (ret < 0) {
×
1025
    return 0;
×
1026
  }
1027

1028
  pSmaIdxKey = pSmaCur->pKey;
×
1029
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
1030
    return 0;
×
1031
  }
1032

1033
  return pSmaIdxKey->uid;
×
1034
}
1035

1036
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
1037
  STSmaWrapper *pSW = NULL;
×
1038
  SArray       *pSmaIds = NULL;
×
1039

1040
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
1041
    return NULL;
×
1042
  }
1043

1044
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
1045
  if (!pSW) {
×
1046
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1047
    goto _err;
×
1048
  }
1049

1050
  pSW->number = taosArrayGetSize(pSmaIds);
×
1051
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
1052

1053
  if (!pSW->tSma) {
×
1054
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1055
    goto _err;
×
1056
  }
1057

1058
  SMetaReader mr = {0};
×
1059
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
1060
  int64_t smaId;
1061
  int     smaIdx = 0;
×
1062
  STSma  *pTSma = NULL;
×
1063
  for (int i = 0; i < pSW->number; ++i) {
×
1064
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
1065
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
1066
      tDecoderClear(&mr.coder);
×
1067
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
1068
      continue;
×
1069
    }
1070
    tDecoderClear(&mr.coder);
×
1071
    pTSma = pSW->tSma + smaIdx;
×
1072
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1073
    if (deepCopy) {
×
1074
      if (pTSma->exprLen > 0) {
×
1075
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
1076
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1077
          goto _err;
×
1078
        }
1079
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
1080
      }
1081
      if (pTSma->tagsFilterLen > 0) {
×
1082
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
1083
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1084
          goto _err;
×
1085
        }
1086
      }
1087
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
1088
    } else {
1089
      pTSma->exprLen = 0;
×
1090
      pTSma->expr = NULL;
×
1091
      pTSma->tagsFilterLen = 0;
×
1092
      pTSma->tagsFilter = NULL;
×
1093
    }
1094

1095
    ++smaIdx;
×
1096
  }
1097

1098
  if (smaIdx <= 0) goto _err;
×
1099
  pSW->number = smaIdx;
×
1100

1101
  metaReaderClear(&mr);
×
1102
  taosArrayDestroy(pSmaIds);
×
1103
  return pSW;
×
1104
_err:
×
1105
  metaReaderClear(&mr);
×
1106
  taosArrayDestroy(pSmaIds);
×
1107
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
1108
  return NULL;
×
1109
}
1110

1111
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
×
1112
  STSma      *pTSma = NULL;
×
1113
  SMetaReader mr = {0};
×
1114
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
1115
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
×
1116
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
1117
    metaReaderClear(&mr);
×
1118
    return NULL;
×
1119
  }
1120
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
×
1121
  if (!pTSma) {
×
1122
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1123
    metaReaderClear(&mr);
×
1124
    return NULL;
×
1125
  }
1126

1127
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1128

1129
  metaReaderClear(&mr);
×
1130
  return pTSma;
×
1131
}
1132

1133
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1134
  SArray     *pUids = NULL;
×
1135
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1136

1137
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1138
  if (!pCur) {
×
1139
    return NULL;
×
1140
  }
1141

1142
  while (1) {
×
1143
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1144
    if (id == 0) {
×
1145
      break;
×
1146
    }
1147

1148
    if (!pUids) {
×
1149
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1150
      if (!pUids) {
×
1151
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1152
        metaCloseSmaCursor(pCur);
×
1153
        return NULL;
×
1154
      }
1155
    }
1156

1157
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1158

1159
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1160
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1161
      metaCloseSmaCursor(pCur);
×
1162
      taosArrayDestroy(pUids);
×
1163
      return NULL;
×
1164
    }
1165
  }
1166

1167
  metaCloseSmaCursor(pCur);
×
1168
  return pUids;
×
1169
}
1170

1171
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1172
  SArray     *pUids = NULL;
×
1173
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1174
  tb_uid_t    lastUid = 0;
×
1175

1176
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1177
  if (!pCur) {
×
1178
    return NULL;
×
1179
  }
1180

1181
  while (1) {
×
1182
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1183
    if (uid == 0) {
×
1184
      break;
×
1185
    }
1186

1187
    if (lastUid == uid) {
×
1188
      continue;
×
1189
    }
1190

1191
    lastUid = uid;
×
1192

1193
    if (!pUids) {
×
1194
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1195
      if (!pUids) {
×
1196
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1197
        metaCloseSmaCursor(pCur);
×
1198
        return NULL;
×
1199
      }
1200
    }
1201

1202
    if (!taosArrayPush(pUids, &uid)) {
×
1203
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1204
      metaCloseSmaCursor(pCur);
×
1205
      taosArrayDestroy(pUids);
×
1206
      return NULL;
×
1207
    }
1208
  }
1209

1210
  metaCloseSmaCursor(pCur);
×
1211
  return pUids;
×
1212
}
1213

1214
#endif
1215

1216
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
864,327,118✔
1217
  STag *tag = (STag *)pTag;
864,327,118✔
1218
  if (type == TSDB_DATA_TYPE_JSON) {
864,327,118✔
1219
    return tag;
1,497,738✔
1220
  }
1221
  bool find = tTagGet(tag, val);
862,829,380✔
1222

1223
  if (!find) {
863,038,961✔
1224
    return NULL;
7,519,451✔
1225
  }
1226

1227
  return val;
855,519,510✔
1228
}
1229

1230
typedef struct {
1231
  SMeta   *pMeta;
1232
  TBC     *pCur;
1233
  tb_uid_t suid;
1234
  int16_t  cid;
1235
  int16_t  type;
1236
  void    *pKey;
1237
  void    *pVal;
1238
  int32_t  kLen;
1239
  int32_t  vLen;
1240
} SIdxCursor;
1241

1242
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
1,776✔
1243
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,776✔
1244
  SMetaFltParam *param = arg;
1,776✔
1245
  int32_t        ret = 0;
1,776✔
1246

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

1257
  metaRLock(pMeta);
1,776✔
1258
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
1,776✔
1259
  if (ret != 0) {
1,776✔
1260
    goto END;
×
1261
  }
1262
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
1,776✔
1263

1264
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
1,776✔
1265
  SBtimeIdxKey *pBtimeKey = &btimeKey;
1,776✔
1266

1267
  int cmp = 0;
1,776✔
1268
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
1,776✔
1269
    goto END;
×
1270
  }
1271

1272
  int32_t valid = 0;
1,776✔
1273
  int32_t count = 0;
1,776✔
1274

1275
  static const int8_t TRY_ERROR_LIMIT = 1;
1276
  do {
6,698,210✔
1277
    void   *entryKey = NULL;
6,699,986✔
1278
    int32_t nEntryKey = -1;
6,691,214✔
1279
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
6,692,246✔
1280
    if (valid < 0) break;
6,701,706✔
1281

1282
    SBtimeIdxKey *p = entryKey;
6,699,930✔
1283
    if (count > TRY_ERROR_LIMIT) break;
6,699,930✔
1284

1285
    terrno = TSDB_CODE_SUCCESS;
6,699,930✔
1286
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
6,701,134✔
1287
    if (terrno != TSDB_CODE_SUCCESS) {
6,621,154✔
1288
      ret = terrno;
×
1289
      break;
×
1290
    }
1291
    if (cmp == 0) {
6,618,402✔
1292
      if (taosArrayPush(pUids, &p->uid) == NULL) {
13,347,744✔
1293
        ret = terrno;
×
1294
        break;
×
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);
6,729,342✔
1303
    if (valid < 0) break;
6,700,618✔
1304
  } while (1);
1305

1306
END:
1,088✔
1307
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
1,776✔
1308
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
1,776✔
1309
  taosMemoryFree(pCursor);
1,776✔
1310
  return ret;
1,776✔
1311
}
1312

1313
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1314
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1315
  SMetaFltParam *param = arg;
×
1316
  int32_t        ret = 0;
×
1317
  char          *buf = NULL;
×
1318

1319
  STagIdxKey *pKey = NULL;
×
1320
  int32_t     nKey = 0;
×
1321

1322
  SIdxCursor *pCursor = NULL;
×
1323
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1324
  if (pCursor == NULL) {
×
1325
    return terrno;
×
1326
  }
1327
  pCursor->pMeta = pMeta;
×
1328
  pCursor->suid = param->suid;
×
1329
  pCursor->cid = param->cid;
×
1330
  pCursor->type = param->type;
×
1331

1332
  char *pName = param->val;
×
1333

1334
  metaRLock(pMeta);
×
1335
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1336
  if (ret != 0) {
×
1337
    goto END;
×
1338
  }
1339

1340
  int cmp = 0;
×
1341
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1342
    goto END;
×
1343
  }
1344
  int32_t valid = 0;
×
1345
  int32_t count = 0;
×
1346

1347
  int32_t TRY_ERROR_LIMIT = 1;
×
1348
  do {
×
1349
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1350
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1351
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1352
    if (valid < 0) break;
×
1353

1354
    if (count > TRY_ERROR_LIMIT) break;
×
1355

1356
    char *pTableKey = (char *)pEntryKey;
×
1357
    terrno = TSDB_CODE_SUCCESS;
×
1358
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1359
    if (terrno != TSDB_CODE_SUCCESS) {
×
1360
      ret = terrno;
×
1361
      goto END;
×
1362
    }
1363
    if (cmp == 0) {
×
1364
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1365
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1366
        ret = terrno;
×
1367
        goto END;
×
1368
      }
1369
    } else {
1370
      if (param->equal == true) {
×
1371
        if (count > TRY_ERROR_LIMIT) break;
×
1372
        count++;
×
1373
      }
1374
    }
1375
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1376
    if (valid < 0) {
×
1377
      break;
×
1378
    }
1379
  } while (1);
1380

1381
END:
×
1382
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1383
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1384
  taosMemoryFree(buf);
×
1385
  taosMemoryFree(pKey);
×
1386

1387
  taosMemoryFree(pCursor);
×
1388

1389
  return ret;
×
1390
}
1391
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1392
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1393
  SMetaFltParam *param = arg;
×
1394
  int32_t        ret = 0;
×
1395
  char          *buf = NULL;
×
1396

1397
  STtlIdxKey *pKey = NULL;
×
1398
  int32_t     nKey = 0;
×
1399

1400
  SIdxCursor *pCursor = NULL;
×
1401
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1402
  if (pCursor == NULL) {
×
1403
    return terrno;
×
1404
  }
1405
  pCursor->pMeta = pMeta;
×
1406
  pCursor->suid = param->suid;
×
1407
  pCursor->cid = param->cid;
×
1408
  pCursor->type = param->type;
×
1409

1410
  metaRLock(pMeta);
×
1411
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1412

1413
END:
×
1414
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1415
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1416
  taosMemoryFree(buf);
×
1417
  taosMemoryFree(pKey);
×
1418

1419
  taosMemoryFree(pCursor);
×
1420

1421
  return ret;
×
1422
  // impl later
1423
  return 0;
1424
}
1425
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
3,089,218✔
1426
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,089,218✔
1427
  SMetaFltParam *param = arg;
3,110,747✔
1428

1429
  SMetaEntry oStbEntry = {0};
3,110,747✔
1430
  int32_t    code = 0;
3,090,246✔
1431
  char      *buf = NULL;
3,090,246✔
1432
  void      *pData = NULL;
3,090,246✔
1433
  int        nData = 0;
3,097,601✔
1434

1435
  SDecoder    dc = {0};
3,100,494✔
1436
  STbDbKey    tbDbKey = {0};
3,103,911✔
1437
  STagIdxKey *pKey = NULL;
3,075,943✔
1438
  int32_t     nKey = 0;
3,068,203✔
1439

1440
  SIdxCursor *pCursor = NULL;
3,073,686✔
1441
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,073,686✔
1442
  if (!pCursor) {
3,072,245✔
1443
    return terrno;
×
1444
  }
1445
  pCursor->pMeta = pMeta;
3,072,245✔
1446
  pCursor->suid = param->suid;
3,074,111✔
1447
  pCursor->cid = param->cid;
3,085,043✔
1448
  pCursor->type = param->type;
3,080,256✔
1449

1450
  metaRLock(pMeta);
3,067,747✔
1451

1452
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
3,088,809✔
1453

1454
  tbDbKey.uid = param->suid;
3,115,501✔
1455
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,097,602✔
1456

1457
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
3,110,190✔
1458

1459
  tDecoderInit(&dc, pData, nData);
3,117,989✔
1460

1461
  code = metaDecodeEntry(&dc, &oStbEntry);
3,117,066✔
1462
  if (code) {
3,104,077✔
1463
    tDecoderClear(&dc);
×
1464
    goto END;
×
1465
  }
1466

1467
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
3,104,077✔
UNCOV
1468
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1469
  }
1470

1471
  code = TSDB_CODE_INVALID_PARA;
3,104,077✔
1472

1473
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,187,108✔
1474
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,183,329✔
1475
    if (IS_IDX_ON(schema)) {
4,181,288✔
1476
      if (schema->colId == param->cid && param->type == schema->type) {
4,159,386✔
1477
        code = 0;
3,092,592✔
1478
        break;
3,092,592✔
1479
      }
1480
    }
1481
  }
1482

1483
  TAOS_CHECK_GOTO(code, NULL, END);
3,096,371✔
1484

1485
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,092,498✔
1486
  if (code != 0) {
3,092,319✔
1487
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1488
  }
1489

1490
  int32_t maxSize = 0;
3,092,319✔
1491
  int32_t nTagData = 0;
3,092,319✔
1492
  void   *tagData = NULL;
3,092,319✔
1493

1494
  if (param->val == NULL) {
3,092,319✔
1495
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1496
    goto END;
×
1497
  } else {
1498
    if (IS_VAR_DATA_TYPE(param->type)) {
3,085,061✔
1499
      tagData = varDataVal(param->val);
300,067✔
1500
      nTagData = varDataLen(param->val);
285,998✔
1501

1502
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
285,593✔
1503
        maxSize = 4 * nTagData + 1;
69,060✔
1504
        buf = taosMemoryCalloc(1, maxSize);
69,060✔
1505
        if (buf == NULL) {
69,060✔
1506
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1507
        }
1508

1509
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
69,060✔
1510
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1511
        }
1512

1513
        tagData = buf;
69,447✔
1514
        nTagData = maxSize;
69,447✔
1515
      }
1516
    } else {
1517
      tagData = param->val;
2,784,445✔
1518
      nTagData = tDataTypes[param->type].bytes;
2,796,453✔
1519
    }
1520
  }
1521

1522
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
3,090,791✔
1523
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1524
                  NULL, END);
1525

1526
  int cmp = 0;
3,069,114✔
1527
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,079,998✔
1528

1529
  int     count = 0;
3,113,521✔
1530
  int32_t valid = 0;
3,113,521✔
1531
  bool    found = false;
3,113,521✔
1532

1533
  static const int8_t TRY_ERROR_LIMIT = 1;
1534

1535
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1536
  /// target:                        [suid, cid2, type2]
1537
  int diffCidCount = 0;
3,113,521✔
1538
  do {
31,158,133✔
1539
    void   *entryKey = NULL, *entryVal = NULL;
34,271,654✔
1540
    int32_t nEntryKey, nEntryVal;
34,262,482✔
1541

1542
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
34,262,127✔
1543
    if (valid < 0) {
34,290,119✔
1544
      break;
1,674,746✔
1545
    }
1546
    if (count > TRY_ERROR_LIMIT) {
32,615,373✔
1547
      break;
855,257✔
1548
    }
1549

1550
    STagIdxKey *p = entryKey;
31,760,116✔
1551
    if (p == NULL) break;
31,760,116✔
1552

1553
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
31,760,116✔
1554
      if (found == true) break;  //
746,674✔
1555
      if (diffCidCount > TRY_ERROR_LIMIT) break;
158,779✔
1556
      diffCidCount++;
158,779✔
1557
      count++;
158,779✔
1558
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
158,779✔
1559
      if (valid < 0) {
158,413✔
1560
        code = valid;
×
1561
        break;
×
1562
      } else {
1563
        continue;
158,413✔
1564
      }
1565
    }
1566

1567
    terrno = TSDB_CODE_SUCCESS;
31,005,224✔
1568
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
30,997,472✔
1569
    if (terrno != TSDB_CODE_SUCCESS) {
30,964,424✔
1570
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1571
      break;
×
1572
    }
1573
    if (cmp == 0) {
30,955,181✔
1574
      // match
1575
      tb_uid_t tuid = 0;
27,692,680✔
1576
      if (IS_VAR_DATA_TYPE(pKey->type)) {
27,687,345✔
1577
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
355,373✔
1578
      } else {
1579
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
27,350,530✔
1580
      }
1581
      if (taosArrayPush(pUids, &tuid) == NULL) {
27,771,702✔
1582
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1583
      }
1584
      found = true;
27,754,953✔
1585
    } else {
1586
      if (param->equal == true) {
3,262,501✔
1587
        if (count > TRY_ERROR_LIMIT) break;
2,250,277✔
1588
        count++;
2,250,277✔
1589
      }
1590
    }
1591
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
31,016,680✔
1592
    if (valid < 0) {
31,009,130✔
1593
      code = valid;
×
1594
      break;
×
1595
    }
1596
  } while (1);
1597

1598
END:
3,130,877✔
1599
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,120,963✔
1600
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,117,431✔
1601
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,115,534✔
1602
  tDecoderClear(&dc);
3,115,534✔
1603
  tdbFree(pData);
3,117,877✔
1604

1605
  taosMemoryFree(buf);
3,115,124✔
1606
  taosMemoryFree(pKey);
3,121,922✔
1607

1608
  taosMemoryFree(pCursor);
3,113,057✔
1609

1610
  return code;
3,111,857✔
1611
}
1612

1613
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
50,826,847✔
1614
  int ret = 0;
50,826,847✔
1615
  if (lock) {
50,826,847✔
1616
    metaRLock(pMeta);
×
1617
  }
1618

1619
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
50,826,847✔
1620
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
50,830,007✔
1621
  if (lock) {
50,807,442✔
1622
    metaULock(pMeta);
×
1623
  }
1624

1625
  return ret;
50,807,442✔
1626
}
1627

1628
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
7,346,575✔
1629
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
7,346,575✔
1630
  const int32_t LIMIT = 128;
7,351,843✔
1631

1632
  int32_t isLock = false;
7,351,843✔
1633
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
7,351,843✔
1634
  for (int i = 0; i < sz; i++) {
58,173,330✔
1635
    STUidTagInfo *p = taosArrayGet(uidList, i);
50,818,738✔
1636
    if (p->pTagVal != NULL) continue;
50,825,871✔
1637

1638
    if (i % LIMIT == 0) {
50,828,554✔
1639
      if (isLock) metaULock(pMeta);
7,347,132✔
1640

1641
      metaRLock(pMeta);
7,347,132✔
1642
      isLock = true;
7,349,448✔
1643
    }
1644

1645
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1646
    void   *val = NULL;
50,830,870✔
1647
    int32_t len = 0;
50,829,133✔
1648
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
50,830,221✔
1649
      p->pTagVal = taosMemoryMalloc(len);
50,788,597✔
1650
      if (!p->pTagVal) {
50,796,422✔
1651
        if (isLock) metaULock(pMeta);
×
1652

1653
        TAOS_RETURN(terrno);
×
1654
      }
1655
      memcpy(p->pTagVal, val, len);
50,802,163✔
1656
      tdbFree(val);
50,803,746✔
1657
    } else {
1658
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
6,296✔
1659
    }
1660
  }
1661
  //  }
1662
  if (isLock) metaULock(pMeta);
7,354,592✔
1663
  return 0;
7,351,607✔
1664
}
1665

1666
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
13,632,140✔
1667
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
13,632,140✔
1668
  if (!pCur) {
13,599,869✔
1669
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1670
  }
1671

1672
  while (1) {
157,635,624✔
1673
    tb_uid_t uid = metaCtbCursorNext(pCur);
171,235,493✔
1674
    if (uid == 0) {
171,003,925✔
1675
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
13,651,242✔
1676
      break;
13,648,727✔
1677
    }
1678

1679
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
157,352,683✔
1680
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
157,384,189✔
1681
    if (!info.pTagVal) {
157,502,482✔
1682
      metaCloseCtbCursor(pCur);
×
1683
      return terrno;
×
1684
    }
1685
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
157,502,482✔
1686
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
157,674,502✔
1687
      taosMemoryFreeClear(info.pTagVal);
×
1688
      metaCloseCtbCursor(pCur);
×
1689
      return terrno;
×
1690
    }
1691
  }
1692
  metaCloseCtbCursor(pCur);
13,648,321✔
1693
  return TSDB_CODE_SUCCESS;
13,635,111✔
1694
}
1695

1696
int32_t metaFlagCache(SVnode *pVnode) {
×
1697
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1698
  if (!pCur) {
×
1699
    return terrno;
×
1700
  }
1701

1702
  SArray *suids = NULL;
×
1703
  while (1) {
×
1704
    tb_uid_t id = metaStbCursorNext(pCur);
×
1705
    if (id == 0) {
×
1706
      break;
×
1707
    }
1708

1709
    if (!suids) {
×
1710
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1711
      if (!suids) {
×
1712
        return terrno;
×
1713
      }
1714
    }
1715

1716
    if (taosArrayPush(suids, &id) == NULL) {
×
1717
      taosArrayDestroy(suids);
×
1718
      return terrno;
×
1719
    }
1720
  }
1721

1722
  metaCloseStbCursor(pCur);
×
1723

1724
  for (int idx = 0; suids && idx < TARRAY_SIZE(suids); ++idx) {
×
1725
    tb_uid_t id = ((tb_uid_t *)TARRAY_DATA(suids))[idx];
×
1726
    STsdb   *pTsdb = pVnode->pTsdb;
×
1727
    SMeta   *pMeta = pVnode->pMeta;
×
1728
    SArray  *uids = NULL;
×
1729

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

1734
      taosArrayDestroy(uids);
×
1735
      taosArrayDestroy(suids);
×
1736

1737
      return code;
×
1738
    }
1739

1740
    if (uids && TARRAY_SIZE(uids) > 0) {
×
1741
      STSchema *pTSchema = NULL;
×
1742

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

1747
        taosArrayDestroy(uids);
×
1748
        taosArrayDestroy(suids);
×
1749

1750
        return code;
×
1751
      }
1752

1753
      int32_t nCol = pTSchema->numOfCols;
×
1754
      for (int32_t i = 0; i < nCol; ++i) {
×
1755
        int16_t cid = pTSchema->columns[i].colId;
×
1756
        int8_t  col_type = pTSchema->columns[i].type;
×
1757

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

1762
          tDestroyTSchema(pTSchema);
×
1763
          taosArrayDestroy(uids);
×
1764
          taosArrayDestroy(suids);
×
1765

1766
          return code;
×
1767
        }
1768
      }
1769

1770
      tDestroyTSchema(pTSchema);
×
1771
    }
1772

1773
    taosArrayDestroy(uids);
×
1774
  }
1775

1776
  taosArrayDestroy(suids);
×
1777

1778
  return TSDB_CODE_SUCCESS;
×
1779
}
1780

1781
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1782

1783
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
2,117,468,305✔
1784
  int32_t code = 0;
2,117,468,305✔
1785
  void   *pData = NULL;
2,117,468,305✔
1786
  int     nData = 0;
2,117,981,555✔
1787
  int     lock = 0;
2,118,375,349✔
1788

1789
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
2,118,375,349✔
1790
    lock = 1;
746,458,457✔
1791
  }
1792

1793
  if (!lock) metaRLock(pMeta);
2,118,422,484✔
1794

1795
  // search cache
1796
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
2,118,460,648✔
1797
    if (!lock) metaULock(pMeta);
2,015,368,875✔
1798
    goto _exit;
2,015,195,430✔
1799
  }
1800

1801
  // search TDB
1802
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
103,150,279✔
1803
    // not found
1804
    if (!lock) metaULock(pMeta);
93,229,670✔
1805
    goto _exit;
93,216,259✔
1806
  }
1807

1808
  if (!lock) metaULock(pMeta);
9,992,469✔
1809

1810
  pInfo->uid = uid;
9,992,469✔
1811
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
9,992,469✔
1812
  pInfo->version = ((SUidIdxVal *)pData)->version;
9,991,893✔
1813
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
9,992,026✔
1814

1815
  if (lock) {
9,992,336✔
1816
    metaULock(pReader->pMeta);
1,234,748✔
1817
    // metaReaderReleaseLock(pReader);
1818
  }
1819
  // upsert the cache
1820
  metaWLock(pMeta);
9,993,007✔
1821
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
9,991,045✔
1822
  if (ret != 0) {
9,991,394✔
1823
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1824
  }
1825
  metaULock(pMeta);
9,991,394✔
1826

1827
  if (lock) {
9,992,469✔
1828
    metaRLock(pReader->pMeta);
1,235,475✔
1829
  }
1830

1831
_exit:
2,118,373,810✔
1832
  tdbFree(pData);
2,118,168,349✔
1833
  return code;
2,117,874,351✔
1834
}
1835

1836
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
116,901,809✔
1837
  int32_t code = 0;
116,901,809✔
1838

1839
  if (!numOfTables && !numOfCols) goto _exit;
116,901,809✔
1840

1841
  SVnode *pVnodeObj = pVnode;
116,901,809✔
1842
  metaRLock(pVnodeObj->pMeta);
116,901,809✔
1843

1844
  // fast path: search cache
1845
  SMetaStbStats state = {0};
116,933,421✔
1846
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
116,941,346✔
1847
    metaULock(pVnodeObj->pMeta);
112,240,119✔
1848
    if (numOfTables) *numOfTables = state.ctbNum;
112,220,418✔
1849
    if (numOfCols) *numOfCols = state.colNum;
112,220,418✔
1850
    if (flags) *flags = state.flags;
112,215,114✔
1851
    goto _exit;
112,218,474✔
1852
  }
1853

1854
  // slow path: search TDB
1855
  int64_t ctbNum = 0;
4,711,980✔
1856
  int32_t colNum = 0;
4,714,052✔
1857
  int64_t keep = 0;
4,715,937✔
1858
  int8_t  flag = 0;
4,715,510✔
1859

1860
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
4,713,118✔
1861
  if (TSDB_CODE_SUCCESS == code) {
4,712,319✔
1862
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
4,712,319✔
1863
  }
1864
  if (TSDB_CODE_SUCCESS == code) {
4,716,060✔
1865
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
4,716,438✔
1866
  }
1867
  metaULock(pVnodeObj->pMeta);
4,716,486✔
1868
  if (TSDB_CODE_SUCCESS != code) {
4,716,784✔
1869
    goto _exit;
×
1870
  }
1871

1872
  if (numOfTables) *numOfTables = ctbNum;
4,716,784✔
1873
  if (numOfCols) *numOfCols = colNum;
4,716,784✔
1874
  if (flags) *flags = flag;
4,716,189✔
1875

1876
  state.uid = uid;
4,716,189✔
1877
  state.ctbNum = ctbNum;
4,716,189✔
1878
  state.colNum = colNum;
4,716,189✔
1879
  state.flags = flag;
4,716,189✔
1880
  state.keep = keep;
4,716,189✔
1881
  // upsert the cache
1882
  metaWLock(pVnodeObj->pMeta);
4,716,189✔
1883

1884
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
4,714,920✔
1885
  if (ret) {
4,714,030✔
1886
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1887
              uid, ctbNum, colNum, keep, flag);
1888
  }
1889

1890
  metaULock(pVnodeObj->pMeta);
4,714,030✔
1891

1892
_exit:
116,935,393✔
1893
  return code;
116,952,759✔
1894
}
1895

1896
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
81,847,526✔
1897
  SMetaStbStats stats = {0};
81,847,526✔
1898

1899
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
81,859,677✔
1900
    stats.ctbNum += deltaCtb;
70,087,601✔
1901
    stats.colNum += deltaCol;
70,087,601✔
1902
    if (deltaKeep > 0) {
70,087,601✔
1903
      stats.keep = deltaKeep;
5,946✔
1904
    }
1905

1906
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
70,087,601✔
1907
    if (code) {
69,988,478✔
1908
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1909
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1910
    }
1911
  }
1912
}
81,814,295✔
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