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

taosdata / TDengine / #4982

12 Mar 2026 05:32AM UTC coverage: 68.587% (+0.1%) from 68.488%
#4982

push

travis-ci

web-flow
merge: from main to 3.0 branch #34705

merge: from main to 3.0 branch

279 of 443 new or added lines in 34 files covered. (62.98%)

2352 existing lines in 132 files now uncovered.

212163 of 309332 relevant lines covered (68.59%)

135254549.82 hits per line

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

59.58
/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) {
727,109,842✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
727,109,842✔
21
  metaReaderDoInit(pReader, pMeta, flags);
727,651,644✔
22
  pReader->pAPI = pAPI;
727,570,939✔
23
}
727,602,092✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
837,092,049✔
26
  memset(pReader, 0, sizeof(*pReader));
837,092,049✔
27
  pReader->pMeta = pMeta;
837,092,049✔
28
  pReader->flags = flags;
837,047,954✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
836,674,719✔
30
    metaRLock(pMeta);
714,932,186✔
31
  }
32
}
837,170,705✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
349,624,179✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
349,624,179✔
36
    metaULock(pReader->pMeta);
349,985,109✔
37
    pReader->flags |= META_READER_NOLOCK;
349,481,400✔
38
  }
39
}
350,032,194✔
40

41
void metaReaderClear(SMetaReader *pReader) {
1,065,025,324✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,065,025,324✔
43
    metaULock(pReader->pMeta);
364,150,612✔
44
  }
45
  tDecoderClear(&pReader->coder);
1,065,264,381✔
46
  tdbFree(pReader->pBuf);
1,066,627,084✔
47
  pReader->pBuf = NULL;
1,065,968,412✔
48
}
1,066,083,995✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
1,285,028,252✔
51
  int32_t  code = 0;
1,285,028,252✔
52
  SMeta   *pMeta = pReader->pMeta;
1,285,028,252✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
1,285,694,483✔
54

55
  // query table.db
56
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
1,285,762,957✔
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,284,913,454✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
1,285,087,793✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
1,285,281,726✔
65
  if (code) {
1,284,663,841✔
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
1,284,663,841✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
143,270,335✔
79
    metaULock(pVnodeObj->pMeta);
44,181✔
80
    return false;
44,181✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
143,290,588✔
84
  return true;
143,310,987✔
85
}
86

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

91
  // query uid.idx
92
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
215,729,431✔
93
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
85,175✔
94
  }
95

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
215,650,813✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
215,653,219✔
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 not found", __func__, uid);
×
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) {
169,213✔
150
  if (version < 0) {
169,213✔
151
    return metaReaderGetTableEntryByUid(pReader, uid);
×
152
  }
153
  SMeta *pMeta = pReader->pMeta;
169,213✔
154

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

170
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
703,445,832✔
171
  SMeta *pMeta = pReader->pMeta;
703,445,832✔
172

173
  SMetaInfo info;
703,879,148✔
174
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
703,817,792✔
175
  if (TSDB_CODE_SUCCESS != code) {
703,770,646✔
176
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
4,797✔
177
  }
178

179
  return metaGetTableEntryByVersion(pReader, info.version, uid);
703,765,849✔
180
}
181

182
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
67,413,962✔
183
  SMeta   *pMeta = pReader->pMeta;
67,413,962✔
184
  tb_uid_t uid;
185

186
  // query name.idx
187
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
67,424,059✔
188
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
8,766,694✔
189
  }
190

191
  uid = *(tb_uid_t *)pReader->pBuf;
58,627,269✔
192
  return metaReaderGetTableEntryByUid(pReader, uid);
58,615,043✔
193
}
194

195
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
74,320,186✔
196
  void    *pData = NULL;
74,320,186✔
197
  int      nData = 0;
74,322,229✔
198
  tb_uid_t uid = 0;
74,322,896✔
199

200
  metaRLock(pMeta);
74,322,896✔
201

202
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
74,322,715✔
203
    uid = *(tb_uid_t *)pData;
9,390,265✔
204
    tdbFree(pData);
9,390,265✔
205
  }
206

207
  metaULock(pMeta);
74,304,415✔
208

209
  return uid;
74,318,411✔
210
}
211

212
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
942,315✔
213
  int         code = 0;
942,315✔
214
  SMetaReader mr = {0};
942,315✔
215
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
942,527✔
216
  code = metaReaderGetTableEntryByUid(&mr, uid);
942,303✔
217
  if (code < 0) {
941,819✔
218
    metaReaderClear(&mr);
252✔
219
    return code;
252✔
220
  }
221

222
  STR_TO_VARSTR(tbName, mr.me.name);
941,567✔
223
  metaReaderClear(&mr);
941,858✔
224

225
  return 0;
941,574✔
226
}
227

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

240
  return 0;
×
241
}
242

243
int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) {
1,828,963✔
244
  int         code = 0;
1,828,963✔
245
  SMetaReader mr = {0};
1,828,963✔
246
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
1,829,485✔
247

248
  SMetaReader *pReader = &mr;
1,829,561✔
249

250
  // query name.idx
251
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
1,829,561✔
252
    metaReaderClear(&mr);
1,233,124✔
253
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1,234,690✔
254
  }
255

256
  *uid = *(tb_uid_t *)pReader->pBuf;
595,736✔
257

258
  metaReaderClear(&mr);
595,713✔
259

260
  return 0;
595,724✔
261
}
262

263
int metaGetTableTypeSuidByName(void *pVnode, char *tbName, ETableType *tbType, uint64_t *suid) {
597,014✔
264
  int         code = 0;
597,014✔
265
  SMetaReader mr = {0};
597,014✔
266
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
597,014✔
267

268
  code = metaGetTableEntryByName(&mr, tbName);
597,014✔
269
  if (code == 0) *tbType = mr.me.type;
596,579✔
270
  if (TSDB_CHILD_TABLE == mr.me.type || TSDB_VIRTUAL_CHILD_TABLE == mr.me.type) {
596,138✔
271
    *suid = mr.me.ctbEntry.suid;
588,420✔
272
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
7,718✔
273
    *suid = mr.me.uid;
4,767✔
274
  } else {
275
    *suid = 0;
2,951✔
276
  }
277

278
  metaReaderClear(&mr);
595,703✔
279
  return code;
597,014✔
280
}
281

282
int metaReadNext(SMetaReader *pReader) {
×
283
  SMeta *pMeta = pReader->pMeta;
×
284

285
  // TODO
286

287
  return 0;
×
288
}
289

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

306
  code = 0;
×
307

308
_exit:
×
309
  metaReaderClear(&mr);
×
310
  return code;
×
311
}
312

313
#if 1  // ===================================================
314
SMTbCursor *metaOpenTbCursor(void *pVnode) {
16,199,399✔
315
  SMTbCursor *pTbCur = NULL;
16,199,399✔
316
  int32_t     code;
317

318
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
16,199,399✔
319
  if (pTbCur == NULL) {
16,196,859✔
320
    return NULL;
×
321
  }
322

323
  SVnode *pVnodeObj = pVnode;
16,196,859✔
324
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
325
  pTbCur->pMeta = pVnodeObj->pMeta;
16,196,859✔
326
  pTbCur->paused = 1;
16,205,991✔
327
  code = metaResumeTbCursor(pTbCur, 1, 0);
16,204,624✔
328
  if (code) {
16,172,103✔
329
    terrno = code;
×
330
    taosMemoryFree(pTbCur);
×
331
    return NULL;
×
332
  }
333
  return pTbCur;
16,172,103✔
334
}
335

336
void metaCloseTbCursor(SMTbCursor *pTbCur) {
32,402,396✔
337
  if (pTbCur) {
32,402,396✔
338
    tdbFree(pTbCur->pKey);
16,207,492✔
339
    tdbFree(pTbCur->pVal);
16,209,609✔
340
    if (!pTbCur->paused) {
16,206,166✔
341
      metaReaderClear(&pTbCur->mr);
5,368,110✔
342
      if (pTbCur->pDbc) {
5,365,951✔
343
        tdbTbcClose((TBC *)pTbCur->pDbc);
5,364,907✔
344
      }
345
    }
346
    taosMemoryFree(pTbCur);
16,202,159✔
347
  }
348
}
32,398,051✔
349

350
void metaPauseTbCursor(SMTbCursor *pTbCur) {
10,841,252✔
351
  if (!pTbCur->paused) {
10,841,252✔
352
    metaReaderClear(&pTbCur->mr);
10,842,481✔
353
    tdbTbcClose((TBC *)pTbCur->pDbc);
10,843,285✔
354
    pTbCur->paused = 1;
10,842,708✔
355
  }
356
}
10,842,740✔
357
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
16,198,805✔
358
  int32_t code = 0;
16,198,805✔
359
  int32_t lino;
360
  int8_t  locked = 0;
16,198,805✔
361
  if (pTbCur->paused) {
16,198,805✔
362
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
16,203,362✔
363
    locked = 1;
16,201,660✔
364
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
16,201,660✔
365
    if (code != 0) {
16,195,356✔
366
      TSDB_CHECK_CODE(code, lino, _exit);
×
367
    }
368

369
    if (first) {
16,195,356✔
370
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
16,192,907✔
371
      TSDB_CHECK_CODE(code, lino, _exit);
16,171,972✔
372
    } else {
373
      int c = 1;
2,449✔
374
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
2,449✔
375
      TSDB_CHECK_CODE(code, lino, _exit);
2,449✔
376
      if (c == 0) {
2,449✔
377
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
2,449✔
378
      } else if (c < 0) {
×
379
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
380
        TSDB_CHECK_CODE(code, lino, _exit);
×
381
      } else {
382
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
383
        TSDB_CHECK_CODE(code, lino, _exit);
×
384
      }
385
    }
386

387
    pTbCur->paused = 0;
16,176,473✔
388
  }
389

390
_exit:
×
391
  if (code != 0 && locked) {
16,198,921✔
392
    metaReaderReleaseLock(&pTbCur->mr);
×
393
  }
394
  return code;
16,187,063✔
395
}
396

397
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
382,093,419✔
398
  int    ret;
399
  void  *pBuf;
400
  STbCfg tbCfg;
401

402
  for (;;) {
403
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
382,093,419✔
404
    if (ret < 0) {
382,092,045✔
405
      return ret;
16,204,018✔
406
    }
407

408
    tDecoderClear(&pTbCur->mr.coder);
365,888,027✔
409

410
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
365,925,251✔
411
    if (ret) return ret;
365,852,447✔
412

413
    if (pTbCur->mr.me.type == jumpTableType) {
365,852,447✔
414
      continue;
4,334,349✔
415
    }
416

417
    break;
361,530,503✔
418
  }
419

420
  return 0;
361,530,503✔
421
}
422

423
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
424
  int    ret;
425
  void  *pBuf;
426
  STbCfg tbCfg;
427

428
  for (;;) {
429
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
430
    if (ret < 0) {
×
431
      return -1;
×
432
    }
433

434
    tDecoderClear(&pTbCur->mr.coder);
×
435

436
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
437
    if (ret < 0) {
×
438
      return ret;
×
439
    }
440

441
    if (pTbCur->mr.me.type == jumpTableType) {
×
442
      continue;
×
443
    }
444

445
    break;
×
446
  }
447

448
  return 0;
×
449
}
450

451
/**
452
 * @param type 0x01 fetchRsmaSchema if table is rsma
453
 */
454
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
325,314,074✔
455
                                   int8_t type) {
456
  int32_t         code = 0;
325,314,074✔
457
  void           *pData = NULL;
325,314,074✔
458
  int             nData = 0;
324,659,870✔
459
  int64_t         version;
460
  SSchemaWrapper  schema = {0};
324,689,335✔
461
  SSchemaWrapper *pSchema = NULL;
324,350,480✔
462
  SDecoder        dc = {0};
324,350,480✔
463
  if (lock) {
323,932,174✔
464
    metaRLock(pMeta);
319,438,444✔
465
  }
466
_query:
358,063,078✔
467
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
358,237,953✔
468
    goto _err;
348,241✔
469
  }
470

471
  version = ((SUidIdxVal *)pData)[0].version;
358,410,107✔
472

473
  if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData)) !=
358,477,915✔
474
      0) {
475
    goto _err;
×
476
  }
477

478
  SMetaEntry me = {0};
358,434,365✔
479
  tDecoderInit(&dc, pData, nData);
358,407,212✔
480
  code = metaDecodeEntry(&dc, &me);
358,306,697✔
481
  if (code) {
358,141,862✔
482
    tDecoderClear(&dc);
×
483
    goto _err;
×
484
  }
485
  if (me.type == TSDB_SUPER_TABLE) {
358,141,862✔
486
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
253,257,155✔
487
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
253,065,556✔
488
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
253,065,556✔
489
      if (TABLE_IS_ROLLUP(me.flags)) {
253,074,611✔
490
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
104,676✔
491
          tDecoderClear(&dc);
×
492
          goto _err;
×
493
        }
494
      }
495
      tDecoderClear(&dc);
253,074,611✔
496
      goto _exit;
253,055,729✔
497
    }
498
  } else if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
104,884,707✔
499
    uid = me.ctbEntry.suid;
32,754,424✔
500
    tDecoderClear(&dc);
32,754,424✔
501
    goto _query;
32,772,634✔
502
  } else {
503
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
72,133,957✔
504
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
72,160,730✔
505
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
72,160,730✔
506
      tDecoderClear(&dc);
72,184,113✔
507
      goto _exit;
72,211,745✔
508
    }
509
  }
510
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
2,730✔
511
  tDecoderClear(&dc);
2,730✔
512

513
  // query from skm db
514
  if ((code = tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) < 0) {
44,214✔
515
    goto _err;
×
516
  }
517

518
  tDecoderInit(&dc, pData, nData);
44,214✔
519
  if ((code = tDecodeSSchemaWrapperEx(&dc, &schema)) != 0) {
44,214✔
520
    goto _err;
×
521
  }
522
  pSchema = tCloneSSchemaWrapper(&schema);
44,214✔
523
  if (pSchema == NULL) {
44,214✔
524
    code = terrno;
×
525
    goto _err;
×
526
  }
527
  tDecoderClear(&dc);
44,214✔
528

529
_exit:
325,311,688✔
530
  if (lock) {
325,348,431✔
531
    metaULock(pMeta);
320,882,241✔
532
  }
533
  tdbFree(pData);
325,157,139✔
534
  return pSchema;
325,319,628✔
535

536
_err:
251,125✔
537
  if (lock) {
348,464✔
538
    metaULock(pMeta);
348,464✔
539
  }
540
  tdbFree(pData);
348,241✔
541
  tDeleteSchemaWrapper(pSchema);
542
  if (extSchema != NULL) {
348,464✔
543
    taosMemoryFreeClear(*extSchema);
201,835✔
544
  }
545
  terrno = code;
348,464✔
546
  return NULL;
348,464✔
547
}
548

549
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
496,703✔
550
  void    *pData = NULL;
496,703✔
551
  int      nData = 0;
496,703✔
552
  int64_t  version = 0;
496,703✔
553
  SDecoder dc = {0};
496,703✔
554
  int64_t  createTime = INT64_MAX;
496,703✔
555
  if (lock) {
496,703✔
556
    metaRLock(pMeta);
496,703✔
557
  }
558

559
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
496,703✔
560
    goto _exit;
×
561
  }
562

563
  version = ((SUidIdxVal *)pData)[0].version;
495,737✔
564

565
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
496,059✔
566
    goto _exit;
×
567
  }
568

569
  SMetaEntry me = {0};
496,381✔
570
  tDecoderInit(&dc, pData, nData);
496,381✔
571
  int32_t code = metaDecodeEntry(&dc, &me);
496,381✔
572
  if (code) {
495,415✔
573
    tDecoderClear(&dc);
×
574
    goto _exit;
×
575
  }
576
  if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
495,415✔
577
    createTime = me.ctbEntry.btime;
495,086✔
578
  } else if (me.type == TSDB_NORMAL_TABLE || me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
329✔
579
    createTime = me.ntbEntry.btime;
329✔
580
  }
581
  tDecoderClear(&dc);
495,415✔
582

583
_exit:
494,771✔
584
  if (lock) {
495,415✔
585
    metaULock(pMeta);
495,086✔
586
  }
587
  tdbFree(pData);
495,422✔
588
  return createTime;
496,059✔
589
}
590

591
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
123,218,354✔
592
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
123,218,354✔
593
  SMCtbCursor *pCtbCur = NULL;
123,253,469✔
594
  SCtbIdxKey   ctbIdxKey;
595
  int          ret = 0;
123,253,469✔
596
  int          c = 0;
123,253,469✔
597

598
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
123,253,469✔
599
  if (pCtbCur == NULL) {
123,062,633✔
600
    return NULL;
×
601
  }
602

603
  pCtbCur->pMeta = pMeta;
123,062,633✔
604
  pCtbCur->suid = uid;
123,065,348✔
605
  pCtbCur->lock = lock;
123,075,213✔
606
  pCtbCur->paused = 1;
123,100,832✔
607

608
  ret = metaResumeCtbCursor(pCtbCur, 1);
123,136,892✔
609
  if (ret < 0) {
123,193,848✔
610
    return NULL;
×
611
  }
612
  return pCtbCur;
123,193,848✔
613
}
614

615
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
123,211,804✔
616
  if (pCtbCur) {
123,211,804✔
617
    if (!pCtbCur->paused) {
123,233,553✔
618
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
118,675,838✔
619
      if (pCtbCur->pCur) {
118,734,871✔
620
        tdbTbcClose(pCtbCur->pCur);
118,687,893✔
621
      }
622
    }
623
    tdbFree(pCtbCur->pKey);
123,142,146✔
624
    tdbFree(pCtbCur->pVal);
123,067,680✔
625
  }
626
  taosMemoryFree(pCtbCur);
123,084,125✔
627
}
123,058,751✔
628

629
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
4,577,677✔
630
  if (!pCtbCur->paused) {
4,577,677✔
631
    tdbTbcClose((TBC *)pCtbCur->pCur);
4,577,560✔
632
    if (pCtbCur->lock) {
4,578,294✔
633
      metaULock(pCtbCur->pMeta);
4,578,796✔
634
    }
635
    pCtbCur->paused = 1;
4,578,415✔
636
  }
637
}
4,580,505✔
638

639
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
123,211,225✔
640
  if (pCtbCur->paused) {
123,211,225✔
641
    pCtbCur->paused = 0;
123,159,810✔
642

643
    if (pCtbCur->lock) {
123,194,385✔
644
      metaRLock(pCtbCur->pMeta);
118,603,301✔
645
    }
646
    int ret = 0;
123,239,046✔
647
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
123,239,046✔
648
    if (ret < 0) {
123,151,674✔
649
      metaCloseCtbCursor(pCtbCur);
×
650
      return -1;
×
651
    }
652

653
    if (first) {
123,151,674✔
654
      SCtbIdxKey ctbIdxKey;
123,149,670✔
655
      // move to the suid
656
      ctbIdxKey.suid = pCtbCur->suid;
123,157,623✔
657
      ctbIdxKey.uid = INT64_MIN;
123,192,428✔
658
      int c = 0;
123,192,428✔
659
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
123,120,523✔
660
      if (c > 0) {
123,142,310✔
661
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
23,306,145✔
662
      }
663
    } else {
664
      int c = 0;
×
665
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
666
      if (c < 0) {
×
667
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
668
      } else {
669
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
670
      }
671
    }
672
  }
673
  return 0;
123,215,643✔
674
}
675

676
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
730,044,687✔
677
  int         ret;
678
  SCtbIdxKey *pCtbIdxKey;
679

680
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
730,044,687✔
681
  if (ret < 0) {
730,050,382✔
682
    return 0;
88,683,269✔
683
  }
684

685
  pCtbIdxKey = pCtbCur->pKey;
641,367,113✔
686
  if (pCtbIdxKey->suid > pCtbCur->suid) {
641,402,958✔
687
    return 0;
34,728,448✔
688
  }
689

690
  return pCtbIdxKey->uid;
606,712,833✔
691
}
692

693
struct SMStbCursor {
694
  SMeta   *pMeta;
695
  TBC     *pCur;
696
  tb_uid_t suid;
697
  void    *pKey;
698
  void    *pVal;
699
  int      kLen;
700
  int      vLen;
701
};
702

703
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
58,274,849✔
704
  SMStbCursor *pStbCur = NULL;
58,274,849✔
705
  int          ret = 0;
58,274,849✔
706
  int          c = 0;
58,274,849✔
707

708
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
58,277,433✔
709
  if (pStbCur == NULL) {
58,258,628✔
710
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
711
    return NULL;
×
712
  }
713

714
  pStbCur->pMeta = pMeta;
58,258,628✔
715
  pStbCur->suid = suid;
58,276,070✔
716
  metaRLock(pMeta);
58,276,008✔
717

718
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
58,282,530✔
719
  if (ret < 0) {
58,258,566✔
720
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
721
    metaULock(pMeta);
×
722
    taosMemoryFree(pStbCur);
×
723
    return NULL;
×
724
  }
725

726
  // move to the suid
727
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
58,258,566✔
728
  if (c > 0) {
58,281,884✔
729
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
730
  }
731

732
  return pStbCur;
58,281,238✔
733
}
734

735
void metaCloseStbCursor(SMStbCursor *pStbCur) {
58,284,468✔
736
  if (pStbCur) {
58,284,468✔
737
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
58,283,822✔
738
    if (pStbCur->pCur) {
58,288,344✔
739
      tdbTbcClose(pStbCur->pCur);
58,285,114✔
740

741
      tdbFree(pStbCur->pKey);
58,278,748✔
742
      tdbFree(pStbCur->pVal);
58,274,207✔
743
    }
744

745
    taosMemoryFree(pStbCur);
58,276,070✔
746
  }
747
}
58,270,902✔
748

749
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
95,707,961✔
750
  int ret;
751

752
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
95,707,961✔
753
  if (ret < 0) {
95,707,510✔
754
    return 0;
58,285,760✔
755
  }
756
  return *(tb_uid_t *)pStbCur->pKey;
37,421,750✔
757
}
758

759
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
275,076,587✔
760
  STSchema       *pTSchema = NULL;
275,076,587✔
761
  SSchemaWrapper *pSW = NULL;
275,076,587✔
762

763
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
275,076,587✔
764
  if (!pSW) return NULL;
275,466,998✔
765

766
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
275,433,775✔
767

768
  tDeleteSchemaWrapper(pSW);
769
  return pTSchema;
275,011,542✔
770
}
771

772
/**
773
 * Fetch rsma schema if table is rsma
774
 */
775
SRSchema *metaGetTbTSchemaR(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
27,084✔
776
  SRSchema       *pRSchema = NULL;
27,084✔
777
  SSchemaWrapper *pSW = NULL;
27,084✔
778

779
  if (!(pRSchema = (SRSchema *)taosMemoryCalloc(1, sizeof(SRSchema)))) goto _err;
27,084✔
780
  if (!(pSW = metaGetTableSchema(pMeta, uid, sver, lock, (SExtSchema **)&pRSchema->extSchema, 0x01))) goto _err;
27,084✔
781
  if (!(pRSchema->tSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version))) goto _err;
27,084✔
782

783
  if (pSW->pRsma) {
27,084✔
784
    if (!(pRSchema->funcIds = taosMemoryCalloc(pSW->nCols, sizeof(func_id_t)))) goto _err;
27,084✔
785
    memcpy(pRSchema->funcIds, pSW->pRsma->funcIds, pSW->nCols * sizeof(func_id_t));
27,084✔
786

787
    pRSchema->tbType = pSW->pRsma->tbType;
27,084✔
788
    pRSchema->tbUid = uid;
27,084✔
789
    tstrncpy(pRSchema->tbName, pSW->pRsma->tbName, TSDB_TABLE_NAME_LEN);
27,084✔
790
    pRSchema->interval[0] = pSW->pRsma->interval[0];
27,084✔
791
    pRSchema->interval[1] = pSW->pRsma->interval[1];
27,084✔
792
  }
793

794
_exit:
27,084✔
795
  tDeleteSchemaWrapper(pSW);
796
  return pRSchema;
27,084✔
797
_err:
×
798
  tDeleteSchemaWrapper(pSW);
799
  tFreeSRSchema(&pRSchema);
800
  return NULL;
×
801
}
802

803
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
353,458✔
804
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
353,458✔
805
  if (*ppTSchema == NULL) {
353,481✔
806
    return terrno;
×
807
  }
808
  return TSDB_CODE_SUCCESS;
353,458✔
809
}
810

811
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
274,625,366✔
812
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
274,625,366✔
813
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
274,410,928✔
814
    return terrno;
×
815
  }
816
  return TSDB_CODE_SUCCESS;
274,252,526✔
817
}
818

819
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
68,957,438✔
820
  int32_t code = 0;
68,957,438✔
821
  int32_t lino;
822

823
  void     *pData = NULL;
68,957,438✔
824
  int       nData = 0;
68,983,909✔
825
  SSkmDbKey skmDbKey;
68,989,239✔
826
  if (sver <= 0) {
68,968,074✔
827
    SMetaInfo info;
34,582,235✔
828
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
34,585,354✔
829
      sver = info.skmVer;
34,568,267✔
830
    } else {
831
      TBC *pSkmDbC = NULL;
33,653✔
832
      int  c;
33,223✔
833

834
      skmDbKey.uid = suid ? suid : uid;
33,223✔
835
      skmDbKey.sver = INT32_MAX;
33,223✔
836

837
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
33,223✔
838
      TSDB_CHECK_CODE(code, lino, _exit);
33,223✔
839
      metaRLock(pMeta);
33,223✔
840

841
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
33,223✔
842
        metaULock(pMeta);
×
843
        tdbTbcClose(pSkmDbC);
×
844
        code = TSDB_CODE_NOT_FOUND;
×
845
        goto _exit;
×
846
      }
847

848
      if (c == 0) {
33,223✔
849
        metaULock(pMeta);
×
850
        tdbTbcClose(pSkmDbC);
×
851
        code = TSDB_CODE_FAILED;
×
852
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
853
        goto _exit;
×
854
      }
855

856
      if (c < 0) {
33,223✔
857
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
3,123✔
858
      }
859

860
      const void *pKey = NULL;
33,223✔
861
      int32_t     nKey = 0;
33,223✔
862
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
33,223✔
863

864
      if (ret != 0 || ((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
33,223✔
865
        metaULock(pMeta);
×
866
        tdbTbcClose(pSkmDbC);
×
867
        code = TSDB_CODE_NOT_FOUND;
×
868
        goto _exit;
×
869
      }
870

871
      sver = ((SSkmDbKey *)pKey)->sver;
33,223✔
872

873
      metaULock(pMeta);
33,223✔
874
      tdbTbcClose(pSkmDbC);
33,223✔
875
    }
876
  }
877

878
  if (!(sver > 0)) {
68,976,381✔
879
    code = TSDB_CODE_NOT_FOUND;
×
880
    goto _exit;
×
881
  }
882

883
  skmDbKey.uid = suid ? suid : uid;
68,976,381✔
884
  skmDbKey.sver = sver;
68,976,381✔
885
  metaRLock(pMeta);
68,976,381✔
886
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
69,005,136✔
887
    metaULock(pMeta);
2,552✔
888
    code = TSDB_CODE_NOT_FOUND;
2,552✔
889
    goto _exit;
2,552✔
890
  }
891
  metaULock(pMeta);
68,983,844✔
892

893
  // decode
894
  SDecoder        dc = {0};
69,018,029✔
895
  SSchemaWrapper  schema;
69,002,569✔
896
  SSchemaWrapper *pSchemaWrapper = &schema;
69,009,631✔
897

898
  tDecoderInit(&dc, pData, nData);
69,009,631✔
899
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
69,046,061✔
900
  tDecoderClear(&dc);
69,046,061✔
901
  tdbFree(pData);
69,021,400✔
902
  if (TSDB_CODE_SUCCESS != code) {
68,948,018✔
903
    taosMemoryFree(pSchemaWrapper->pSchema);
×
904
    goto _exit;
×
905
  }
906

907
  // convert
908
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
68,948,018✔
909
  if (pTSchema == NULL) {
69,009,073✔
910
    code = TSDB_CODE_OUT_OF_MEMORY;
×
911
  }
912

913
  *ppTSchema = pTSchema;
69,009,073✔
914
  taosMemoryFree(pSchemaWrapper->pSchema);
69,013,711✔
915

916
_exit:
68,963,247✔
917
  return code;
68,989,741✔
918
}
919

920
// N.B. Called by statusReq per second
921
int64_t metaGetTbNum(SMeta *pMeta) {
112,924,011✔
922
  // num of child tables (excluding normal tables , stables and others)
923

924
  /* int64_t num = 0; */
925
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
926

927
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
112,924,011✔
928
}
929

930
void metaUpdTimeSeriesNum(SMeta *pMeta) {
58,271,532✔
931
  int64_t nCtbTimeSeries = 0;
58,271,532✔
932
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
58,276,580✔
933
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
58,270,886✔
934
  }
935
}
58,277,346✔
936

937
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
938
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
939
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
198,654,534✔
940
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
198,672,988✔
941
    metaUpdTimeSeriesNum(pMeta);
58,282,548✔
942
  }
943

944
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
198,676,474✔
945
}
946

947
// type: 1 reported timeseries
948
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
198,637,280✔
949
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
198,638,285✔
950
  if (type == 1) {
198,638,285✔
951
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
112,924,011✔
952
  }
953
  return nTimeSeries;
198,599,936✔
954
}
955

956
typedef struct {
957
  SMeta   *pMeta;
958
  TBC     *pCur;
959
  tb_uid_t uid;
960
  void    *pKey;
961
  void    *pVal;
962
  int      kLen;
963
  int      vLen;
964
} SMSmaCursor;
965

966
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
967
  SMSmaCursor *pSmaCur = NULL;
×
968
  SSmaIdxKey   smaIdxKey;
×
969
  int          ret;
970
  int          c;
×
971

972
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
973
  if (pSmaCur == NULL) {
×
974
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
975
    return NULL;
×
976
  }
977

978
  pSmaCur->pMeta = pMeta;
×
979
  pSmaCur->uid = uid;
×
980
  metaRLock(pMeta);
×
981

982
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
983
  if (ret < 0) {
×
984
    metaULock(pMeta);
×
985
    taosMemoryFree(pSmaCur);
×
986
    return NULL;
×
987
  }
988

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

997
  return pSmaCur;
×
998
}
999

1000
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
1001
  if (pSmaCur) {
×
1002
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
1003
    if (pSmaCur->pCur) {
×
1004
      tdbTbcClose(pSmaCur->pCur);
×
1005
      pSmaCur->pCur = NULL;
×
1006

1007
      tdbFree(pSmaCur->pKey);
×
1008
      tdbFree(pSmaCur->pVal);
×
1009
    }
1010

1011
    taosMemoryFree(pSmaCur);
×
1012
  }
1013
}
×
1014

1015
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
1016
  int         ret;
1017
  SSmaIdxKey *pSmaIdxKey;
1018

1019
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
1020
  if (ret < 0) {
×
1021
    return 0;
×
1022
  }
1023

1024
  pSmaIdxKey = pSmaCur->pKey;
×
1025
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
1026
    return 0;
×
1027
  }
1028

1029
  return pSmaIdxKey->uid;
×
1030
}
1031

1032
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
1033
  STSmaWrapper *pSW = NULL;
×
1034
  SArray       *pSmaIds = NULL;
×
1035

1036
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
1037
    return NULL;
×
1038
  }
1039

1040
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
1041
  if (!pSW) {
×
1042
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1043
    goto _err;
×
1044
  }
1045

1046
  pSW->number = taosArrayGetSize(pSmaIds);
×
1047
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
1048

1049
  if (!pSW->tSma) {
×
1050
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1051
    goto _err;
×
1052
  }
1053

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

1091
    ++smaIdx;
×
1092
  }
1093

1094
  if (smaIdx <= 0) goto _err;
×
1095
  pSW->number = smaIdx;
×
1096

1097
  metaReaderClear(&mr);
×
1098
  taosArrayDestroy(pSmaIds);
×
1099
  return pSW;
×
1100
_err:
×
1101
  metaReaderClear(&mr);
×
1102
  taosArrayDestroy(pSmaIds);
×
1103
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
1104
  return NULL;
×
1105
}
1106

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

1123
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1124

1125
  metaReaderClear(&mr);
×
1126
  return pTSma;
×
1127
}
1128

1129
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1130
  SArray     *pUids = NULL;
×
1131
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1132

1133
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1134
  if (!pCur) {
×
1135
    return NULL;
×
1136
  }
1137

1138
  while (1) {
×
1139
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1140
    if (id == 0) {
×
1141
      break;
×
1142
    }
1143

1144
    if (!pUids) {
×
1145
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1146
      if (!pUids) {
×
1147
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1148
        metaCloseSmaCursor(pCur);
×
1149
        return NULL;
×
1150
      }
1151
    }
1152

1153
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1154

1155
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1156
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1157
      metaCloseSmaCursor(pCur);
×
1158
      taosArrayDestroy(pUids);
×
1159
      return NULL;
×
1160
    }
1161
  }
1162

1163
  metaCloseSmaCursor(pCur);
×
1164
  return pUids;
×
1165
}
1166

1167
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1168
  SArray     *pUids = NULL;
×
1169
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1170
  tb_uid_t    lastUid = 0;
×
1171

1172
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1173
  if (!pCur) {
×
1174
    return NULL;
×
1175
  }
1176

1177
  while (1) {
×
1178
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1179
    if (uid == 0) {
×
1180
      break;
×
1181
    }
1182

1183
    if (lastUid == uid) {
×
1184
      continue;
×
1185
    }
1186

1187
    lastUid = uid;
×
1188

1189
    if (!pUids) {
×
1190
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1191
      if (!pUids) {
×
1192
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1193
        metaCloseSmaCursor(pCur);
×
1194
        return NULL;
×
1195
      }
1196
    }
1197

1198
    if (!taosArrayPush(pUids, &uid)) {
×
1199
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1200
      metaCloseSmaCursor(pCur);
×
1201
      taosArrayDestroy(pUids);
×
1202
      return NULL;
×
1203
    }
1204
  }
1205

1206
  metaCloseSmaCursor(pCur);
×
1207
  return pUids;
×
1208
}
1209

1210
#endif
1211

1212
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
817,564,603✔
1213
  STag *tag = (STag *)pTag;
817,564,603✔
1214
  if (type == TSDB_DATA_TYPE_JSON) {
817,564,603✔
1215
    return tag;
1,439,345✔
1216
  }
1217
  bool find = tTagGet(tag, val);
816,125,258✔
1218

1219
  if (!find) {
816,638,794✔
1220
    return NULL;
6,164,577✔
1221
  }
1222

1223
  return val;
810,474,217✔
1224
}
1225

1226
typedef struct {
1227
  SMeta   *pMeta;
1228
  TBC     *pCur;
1229
  tb_uid_t suid;
1230
  int16_t  cid;
1231
  int16_t  type;
1232
  void    *pKey;
1233
  void    *pVal;
1234
  int32_t  kLen;
1235
  int32_t  vLen;
1236
} SIdxCursor;
1237

1238
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
1,684✔
1239
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,684✔
1240
  SMetaFltParam *param = arg;
1,684✔
1241
  int32_t        ret = 0;
1,684✔
1242

1243
  SIdxCursor *pCursor = NULL;
1,684✔
1244
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
1,684✔
1245
  if (pCursor == NULL) {
1,684✔
1246
    return terrno;
×
1247
  }
1248
  pCursor->pMeta = pMeta;
1,684✔
1249
  pCursor->suid = param->suid;
1,684✔
1250
  pCursor->cid = param->cid;
1,684✔
1251
  pCursor->type = param->type;
1,684✔
1252

1253
  metaRLock(pMeta);
1,684✔
1254
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
1,684✔
1255
  if (ret != 0) {
1,684✔
1256
    goto END;
×
1257
  }
1258
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
1,684✔
1259

1260
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
1,684✔
1261
  SBtimeIdxKey *pBtimeKey = &btimeKey;
1,684✔
1262

1263
  int cmp = 0;
1,684✔
1264
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
1,684✔
1265
    goto END;
×
1266
  }
1267

1268
  int32_t valid = 0;
1,684✔
1269
  int32_t count = 0;
1,684✔
1270

1271
  static const int8_t TRY_ERROR_LIMIT = 1;
1272
  do {
6,286,588✔
1273
    void   *entryKey = NULL;
6,288,272✔
1274
    int32_t nEntryKey = -1;
6,280,658✔
1275
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
6,286,976✔
1276
    if (valid < 0) break;
6,259,582✔
1277

1278
    SBtimeIdxKey *p = entryKey;
6,258,076✔
1279
    if (count > TRY_ERROR_LIMIT) break;
6,258,076✔
1280

1281
    terrno = TSDB_CODE_SUCCESS;
6,258,076✔
1282
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
6,252,406✔
1283
    if (terrno != TSDB_CODE_SUCCESS) {
6,163,144✔
1284
      ret = terrno;
×
1285
      break;
×
1286
    }
1287
    if (cmp == 0) {
6,159,742✔
1288
      if (taosArrayPush(pUids, &p->uid) == NULL) {
12,435,638✔
1289
        ret = terrno;
×
1290
        break;
×
1291
      }
1292
    } else {
1293
      if (param->equal == true) {
×
1294
        if (count > TRY_ERROR_LIMIT) break;
×
1295
        count++;
×
1296
      }
1297
    }
1298
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
6,275,896✔
1299
    if (valid < 0) break;
6,287,074✔
1300
  } while (1);
1301

1302
END:
1,684✔
1303
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
1,684✔
1304
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
1,506✔
1305
  taosMemoryFree(pCursor);
1,684✔
1306
  return ret;
1,506✔
1307
}
1308

1309
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1310
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1311
  SMetaFltParam *param = arg;
×
1312
  int32_t        ret = 0;
×
1313
  char          *buf = NULL;
×
1314

1315
  STagIdxKey *pKey = NULL;
×
1316
  int32_t     nKey = 0;
×
1317

1318
  SIdxCursor *pCursor = NULL;
×
1319
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1320
  if (pCursor == NULL) {
×
1321
    return terrno;
×
1322
  }
1323
  pCursor->pMeta = pMeta;
×
1324
  pCursor->suid = param->suid;
×
1325
  pCursor->cid = param->cid;
×
1326
  pCursor->type = param->type;
×
1327

1328
  char *pName = param->val;
×
1329

1330
  metaRLock(pMeta);
×
1331
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1332
  if (ret != 0) {
×
1333
    goto END;
×
1334
  }
1335

1336
  int cmp = 0;
×
1337
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1338
    goto END;
×
1339
  }
1340
  int32_t valid = 0;
×
1341
  int32_t count = 0;
×
1342

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

1350
    if (count > TRY_ERROR_LIMIT) break;
×
1351

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

1377
END:
×
1378
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1379
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1380
  taosMemoryFree(buf);
×
1381
  taosMemoryFree(pKey);
×
1382

1383
  taosMemoryFree(pCursor);
×
1384

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

1393
  STtlIdxKey *pKey = NULL;
×
1394
  int32_t     nKey = 0;
×
1395

1396
  SIdxCursor *pCursor = NULL;
×
1397
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1398
  if (pCursor == NULL) {
×
1399
    return terrno;
×
1400
  }
1401
  pCursor->pMeta = pMeta;
×
1402
  pCursor->suid = param->suid;
×
1403
  pCursor->cid = param->cid;
×
1404
  pCursor->type = param->type;
×
1405

1406
  metaRLock(pMeta);
×
1407
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1408

1409
END:
×
1410
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1411
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1412
  taosMemoryFree(buf);
×
1413
  taosMemoryFree(pKey);
×
1414

1415
  taosMemoryFree(pCursor);
×
1416

1417
  return ret;
×
1418
  // impl later
1419
  return 0;
1420
}
1421
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
2,922,115✔
1422
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
2,922,115✔
1423
  SMetaFltParam *param = arg;
2,934,104✔
1424

1425
  SMetaEntry oStbEntry = {0};
2,934,104✔
1426
  int32_t    code = 0;
2,911,086✔
1427
  char      *buf = NULL;
2,911,086✔
1428
  void      *pData = NULL;
2,911,086✔
1429
  int        nData = 0;
2,920,496✔
1430

1431
  SDecoder    dc = {0};
2,922,092✔
1432
  STbDbKey    tbDbKey = {0};
2,912,964✔
1433
  STagIdxKey *pKey = NULL;
2,904,734✔
1434
  int32_t     nKey = 0;
2,899,046✔
1435

1436
  SIdxCursor *pCursor = NULL;
2,898,488✔
1437
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
2,898,488✔
1438
  if (!pCursor) {
2,900,781✔
1439
    return terrno;
×
1440
  }
1441
  pCursor->pMeta = pMeta;
2,900,781✔
1442
  pCursor->suid = param->suid;
2,903,241✔
1443
  pCursor->cid = param->cid;
2,911,786✔
1444
  pCursor->type = param->type;
2,900,061✔
1445

1446
  metaRLock(pMeta);
2,894,951✔
1447

1448
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
2,916,441✔
1449

1450
  tbDbKey.uid = param->suid;
2,938,442✔
1451
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
2,929,208✔
1452

1453
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
2,929,285✔
1454

1455
  tDecoderInit(&dc, pData, nData);
2,941,968✔
1456

1457
  code = metaDecodeEntry(&dc, &oStbEntry);
2,941,894✔
1458
  if (code) {
2,931,567✔
1459
    tDecoderClear(&dc);
×
1460
    goto END;
×
1461
  }
1462

1463
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
2,931,567✔
UNCOV
1464
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1465
  }
1466

1467
  code = TSDB_CODE_INVALID_PARA;
2,931,567✔
1468

1469
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
3,959,182✔
1470
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
3,957,802✔
1471
    if (IS_IDX_ON(schema)) {
3,955,246✔
1472
      if (schema->colId == param->cid && param->type == schema->type) {
3,934,423✔
1473
        code = 0;
2,927,277✔
1474
        break;
2,927,277✔
1475
      }
1476
    }
1477
  }
1478

1479
  TAOS_CHECK_GOTO(code, NULL, END);
2,928,657✔
1480

1481
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
2,925,571✔
1482
  if (code != 0) {
2,923,528✔
1483
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1484
  }
1485

1486
  int32_t maxSize = 0;
2,923,528✔
1487
  int32_t nTagData = 0;
2,920,572✔
1488
  void   *tagData = NULL;
2,920,572✔
1489

1490
  if (param->val == NULL) {
2,920,572✔
1491
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1492
    goto END;
×
1493
  } else {
1494
    if (IS_VAR_DATA_TYPE(param->type)) {
2,916,776✔
1495
      tagData = varDataVal(param->val);
256,061✔
1496
      nTagData = varDataLen(param->val);
255,452✔
1497

1498
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
255,452✔
1499
        maxSize = 4 * nTagData + 1;
66,586✔
1500
        buf = taosMemoryCalloc(1, maxSize);
66,586✔
1501
        if (buf == NULL) {
66,586✔
1502
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1503
        }
1504

1505
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
66,586✔
1506
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1507
        }
1508

1509
        tagData = buf;
66,586✔
1510
        nTagData = maxSize;
66,586✔
1511
      }
1512
    } else {
1513
      tagData = param->val;
2,653,177✔
1514
      nTagData = tDataTypes[param->type].bytes;
2,658,976✔
1515
    }
1516
  }
1517

1518
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
2,922,710✔
1519
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1520
                  NULL, END);
1521

1522
  int cmp = 0;
2,906,727✔
1523
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
2,911,283✔
1524

1525
  int     count = 0;
2,941,575✔
1526
  int32_t valid = 0;
2,941,575✔
1527
  bool    found = false;
2,941,575✔
1528

1529
  static const int8_t TRY_ERROR_LIMIT = 1;
1530

1531
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1532
  /// target:                        [suid, cid2, type2]
1533
  int diffCidCount = 0;
2,941,575✔
1534
  do {
29,641,079✔
1535
    void   *entryKey = NULL, *entryVal = NULL;
32,582,654✔
1536
    int32_t nEntryKey, nEntryVal;
32,590,999✔
1537

1538
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
32,591,267✔
1539
    if (valid < 0) {
32,614,440✔
1540
      break;
1,390,842✔
1541
    }
1542
    if (count > TRY_ERROR_LIMIT) {
31,223,598✔
1543
      break;
830,125✔
1544
    }
1545

1546
    STagIdxKey *p = entryKey;
30,393,473✔
1547
    if (p == NULL) break;
30,393,473✔
1548

1549
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
30,393,473✔
1550
      if (found == true) break;  //
884,680✔
1551
      if (diffCidCount > TRY_ERROR_LIMIT) break;
161,440✔
1552
      diffCidCount++;
161,440✔
1553
      count++;
161,440✔
1554
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
161,440✔
1555
      if (valid < 0) {
161,440✔
1556
        code = valid;
×
1557
        break;
×
1558
      } else {
1559
        continue;
161,440✔
1560
      }
1561
    }
1562

1563
    terrno = TSDB_CODE_SUCCESS;
29,498,092✔
1564
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
29,494,458✔
1565
    if (terrno != TSDB_CODE_SUCCESS) {
29,500,809✔
1566
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1567
      break;
×
1568
    }
1569
    if (cmp == 0) {
29,493,886✔
1570
      // match
1571
      tb_uid_t tuid = 0;
26,421,112✔
1572
      if (IS_VAR_DATA_TYPE(pKey->type)) {
26,418,681✔
1573
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
339,881✔
1574
      } else {
1575
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
26,066,521✔
1576
      }
1577
      if (taosArrayPush(pUids, &tuid) == NULL) {
26,438,140✔
1578
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1579
      }
1580
      found = true;
26,435,777✔
1581
    } else {
1582
      if (param->equal == true) {
3,072,774✔
1583
        if (count > TRY_ERROR_LIMIT) break;
2,117,993✔
1584
        count++;
2,117,993✔
1585
      }
1586
    }
1587
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
29,508,815✔
1588
    if (valid < 0) {
29,488,543✔
1589
      code = valid;
×
1590
      break;
×
1591
    }
1592
  } while (1);
1593

1594
END:
2,961,337✔
1595
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
2,947,644✔
1596
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
2,946,829✔
1597
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
2,943,370✔
1598
  tDecoderClear(&dc);
2,943,370✔
1599
  tdbFree(pData);
2,945,141✔
1600

1601
  taosMemoryFree(buf);
2,941,353✔
1602
  taosMemoryFree(pKey);
2,944,061✔
1603

1604
  taosMemoryFree(pCursor);
2,939,848✔
1605

1606
  return code;
2,940,292✔
1607
}
1608

1609
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
48,312,401✔
1610
  int ret = 0;
48,312,401✔
1611
  if (lock) {
48,312,401✔
1612
    metaRLock(pMeta);
×
1613
  }
1614

1615
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
48,312,401✔
1616
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
48,314,384✔
1617
  if (lock) {
48,296,277✔
1618
    metaULock(pMeta);
×
1619
  }
1620

1621
  return ret;
48,296,277✔
1622
}
1623

1624
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
6,913,955✔
1625
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
6,913,955✔
1626
  const int32_t LIMIT = 128;
6,915,229✔
1627

1628
  int32_t isLock = false;
6,915,229✔
1629
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
6,915,229✔
1630
  for (int i = 0; i < sz; i++) {
55,215,389✔
1631
    STUidTagInfo *p = taosArrayGet(uidList, i);
48,297,627✔
1632
    if (p->pTagVal != NULL) continue;
48,312,786✔
1633

1634
    if (i % LIMIT == 0) {
48,313,936✔
1635
      if (isLock) metaULock(pMeta);
6,912,008✔
1636

1637
      metaRLock(pMeta);
6,912,008✔
1638
      isLock = true;
6,913,279✔
1639
    }
1640

1641
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1642
    void   *val = NULL;
48,315,207✔
1643
    int32_t len = 0;
48,313,315✔
1644
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
48,313,011✔
1645
      p->pTagVal = taosMemoryMalloc(len);
48,289,825✔
1646
      if (!p->pTagVal) {
48,294,795✔
1647
        if (isLock) metaULock(pMeta);
×
1648

1649
        TAOS_RETURN(terrno);
×
1650
      }
1651
      memcpy(p->pTagVal, val, len);
48,293,337✔
1652
      tdbFree(val);
48,295,486✔
1653
    } else {
1654
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
5,838✔
1655
    }
1656
  }
1657
  //  }
1658
  if (isLock) metaULock(pMeta);
6,917,762✔
1659
  return 0;
6,915,229✔
1660
}
1661

1662
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
11,086,701✔
1663
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
11,086,701✔
1664
  if (!pCur) {
11,078,610✔
1665
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1666
  }
1667

1668
  while (1) {
145,275,818✔
1669
    tb_uid_t uid = metaCtbCursorNext(pCur);
156,354,428✔
1670
    if (uid == 0) {
156,038,454✔
1671
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
11,095,608✔
1672
      break;
11,094,893✔
1673
    }
1674

1675
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
144,942,846✔
1676
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
145,048,268✔
1677
    if (!info.pTagVal) {
145,094,915✔
1678
      metaCloseCtbCursor(pCur);
×
1679
      return terrno;
×
1680
    }
1681
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
145,094,915✔
1682
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
145,344,596✔
1683
      taosMemoryFreeClear(info.pTagVal);
×
1684
      metaCloseCtbCursor(pCur);
×
1685
      return terrno;
×
1686
    }
1687
  }
1688
  metaCloseCtbCursor(pCur);
11,095,415✔
1689
  return TSDB_CODE_SUCCESS;
11,078,814✔
1690
}
1691

1692
int32_t metaFlagCache(SVnode *pVnode) {
×
1693
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1694
  if (!pCur) {
×
1695
    return terrno;
×
1696
  }
1697

1698
  SArray *suids = NULL;
×
1699
  while (1) {
×
1700
    tb_uid_t id = metaStbCursorNext(pCur);
×
1701
    if (id == 0) {
×
1702
      break;
×
1703
    }
1704

1705
    if (!suids) {
×
1706
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1707
      if (!suids) {
×
1708
        return terrno;
×
1709
      }
1710
    }
1711

1712
    if (taosArrayPush(suids, &id) == NULL) {
×
1713
      taosArrayDestroy(suids);
×
1714
      return terrno;
×
1715
    }
1716
  }
1717

1718
  metaCloseStbCursor(pCur);
×
1719

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

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

1730
      taosArrayDestroy(uids);
×
1731
      taosArrayDestroy(suids);
×
1732

1733
      return code;
×
1734
    }
1735

1736
    if (uids && TARRAY_SIZE(uids) > 0) {
×
1737
      STSchema *pTSchema = NULL;
×
1738

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

1743
        taosArrayDestroy(uids);
×
1744
        taosArrayDestroy(suids);
×
1745

1746
        return code;
×
1747
      }
1748

1749
      int32_t nCol = pTSchema->numOfCols;
×
1750
      for (int32_t i = 0; i < nCol; ++i) {
×
1751
        int16_t cid = pTSchema->columns[i].colId;
×
1752
        int8_t  col_type = pTSchema->columns[i].type;
×
1753

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

1758
          tDestroyTSchema(pTSchema);
×
1759
          taosArrayDestroy(uids);
×
1760
          taosArrayDestroy(suids);
×
1761

1762
          return code;
×
1763
        }
1764
      }
1765

1766
      tDestroyTSchema(pTSchema);
×
1767
    }
1768

1769
    taosArrayDestroy(uids);
×
1770
  }
1771

1772
  taosArrayDestroy(suids);
×
1773

1774
  return TSDB_CODE_SUCCESS;
×
1775
}
1776

1777
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1778

1779
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
2,147,483,647✔
1780
  int32_t code = 0;
2,147,483,647✔
1781
  void   *pData = NULL;
2,147,483,647✔
1782
  int     nData = 0;
2,147,483,647✔
1783
  int     lock = 0;
2,147,483,647✔
1784

1785
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
2,147,483,647✔
1786
    lock = 1;
704,232,591✔
1787
  }
1788

1789
  if (!lock) metaRLock(pMeta);
2,147,483,647✔
1790

1791
  // search cache
1792
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
2,147,483,647✔
1793
    if (!lock) metaULock(pMeta);
2,132,486,896✔
1794
    goto _exit;
2,132,381,865✔
1795
  }
1796

1797
  // search TDB
1798
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
100,526,144✔
1799
    // not found
1800
    if (!lock) metaULock(pMeta);
91,200,787✔
1801
    goto _exit;
91,189,300✔
1802
  }
1803

1804
  if (!lock) metaULock(pMeta);
9,345,441✔
1805

1806
  pInfo->uid = uid;
9,345,441✔
1807
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
9,345,430✔
1808
  pInfo->version = ((SUidIdxVal *)pData)->version;
9,344,595✔
1809
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
9,345,001✔
1810

1811
  if (lock) {
9,344,595✔
1812
    metaULock(pReader->pMeta);
1,172,621✔
1813
    // metaReaderReleaseLock(pReader);
1814
  }
1815
  // upsert the cache
1816
  metaWLock(pMeta);
9,344,862✔
1817
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
9,343,052✔
1818
  if (ret != 0) {
9,344,089✔
1819
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1820
  }
1821
  metaULock(pMeta);
9,344,089✔
1822

1823
  if (lock) {
9,344,305✔
1824
    metaRLock(pReader->pMeta);
1,173,027✔
1825
  }
1826

1827
_exit:
2,147,483,647✔
1828
  tdbFree(pData);
2,147,483,647✔
1829
  return code;
2,147,483,647✔
1830
}
1831

1832
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
115,696,491✔
1833
  int32_t code = 0;
115,696,491✔
1834

1835
  if (!numOfTables && !numOfCols) goto _exit;
115,696,491✔
1836

1837
  SVnode *pVnodeObj = pVnode;
115,696,491✔
1838
  metaRLock(pVnodeObj->pMeta);
115,696,491✔
1839

1840
  // fast path: search cache
1841
  SMetaStbStats state = {0};
115,740,178✔
1842
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
115,748,330✔
1843
    metaULock(pVnodeObj->pMeta);
111,280,593✔
1844
    if (numOfTables) *numOfTables = state.ctbNum;
111,283,282✔
1845
    if (numOfCols) *numOfCols = state.colNum;
111,283,951✔
1846
    if (flags) *flags = state.flags;
111,274,384✔
1847
    goto _exit;
111,273,738✔
1848
  }
1849

1850
  // slow path: search TDB
1851
  int64_t ctbNum = 0;
4,492,406✔
1852
  int32_t colNum = 0;
4,494,670✔
1853
  int64_t keep = 0;
4,495,596✔
1854
  int8_t  flag = 0;
4,494,993✔
1855

1856
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
4,495,641✔
1857
  if (TSDB_CODE_SUCCESS == code) {
4,495,002✔
1858
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
4,495,002✔
1859
  }
1860
  if (TSDB_CODE_SUCCESS == code) {
4,497,184✔
1861
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
4,497,485✔
1862
  }
1863
  metaULock(pVnodeObj->pMeta);
4,496,823✔
1864
  if (TSDB_CODE_SUCCESS != code) {
4,497,152✔
1865
    goto _exit;
×
1866
  }
1867

1868
  if (numOfTables) *numOfTables = ctbNum;
4,497,152✔
1869
  if (numOfCols) *numOfCols = colNum;
4,497,152✔
1870
  if (flags) *flags = flag;
4,495,709✔
1871

1872
  state.uid = uid;
4,495,709✔
1873
  state.ctbNum = ctbNum;
4,495,709✔
1874
  state.colNum = colNum;
4,495,709✔
1875
  state.flags = flag;
4,495,709✔
1876
  state.keep = keep;
4,495,709✔
1877
  // upsert the cache
1878
  metaWLock(pVnodeObj->pMeta);
4,495,709✔
1879

1880
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
4,495,117✔
1881
  if (ret) {
4,493,367✔
1882
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1883
              uid, ctbNum, colNum, keep, flag);
1884
  }
1885

1886
  metaULock(pVnodeObj->pMeta);
4,493,367✔
1887

1888
_exit:
115,771,223✔
1889
  return code;
115,783,935✔
1890
}
1891

1892
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
80,628,729✔
1893
  SMetaStbStats stats = {0};
80,628,729✔
1894

1895
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
80,646,764✔
1896
    stats.ctbNum += deltaCtb;
69,675,478✔
1897
    stats.colNum += deltaCol;
69,675,478✔
1898
    if (deltaKeep > 0) {
69,675,478✔
1899
      stats.keep = deltaKeep;
7,669✔
1900
    }
1901

1902
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
69,675,478✔
1903
    if (code) {
69,576,036✔
1904
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1905
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1906
    }
1907
  }
1908
}
80,597,140✔
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