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

taosdata / TDengine / #4951

06 Feb 2026 07:29AM UTC coverage: 66.887% (+0.04%) from 66.849%
#4951

push

travis-ci

web-flow
merge: from main to 3.0 #34521

765 of 1081 new or added lines in 28 files covered. (70.77%)

6419 existing lines in 131 files now uncovered.

205810 of 307696 relevant lines covered (66.89%)

127232277.37 hits per line

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

59.32
/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) {
697,355,962✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
697,355,962✔
21
  metaReaderDoInit(pReader, pMeta, flags);
697,512,703✔
22
  pReader->pAPI = pAPI;
697,499,565✔
23
}
697,504,783✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
800,987,858✔
26
  memset(pReader, 0, sizeof(*pReader));
800,987,858✔
27
  pReader->pMeta = pMeta;
800,987,858✔
28
  pReader->flags = flags;
800,990,462✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
800,976,576✔
30
    metaRLock(pMeta);
670,598,477✔
31
  }
32
}
801,096,274✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
381,292,627✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
381,292,627✔
36
    metaULock(pReader->pMeta);
381,340,009✔
37
    pReader->flags |= META_READER_NOLOCK;
381,235,248✔
38
  }
39
}
381,470,325✔
40

41
void metaReaderClear(SMetaReader *pReader) {
853,266,282✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
853,266,282✔
43
    metaULock(pReader->pMeta);
289,109,775✔
44
  }
45
  tDecoderClear(&pReader->coder);
853,464,835✔
46
  tdbFree(pReader->pBuf);
853,474,059✔
47
  pReader->pBuf = NULL;
853,189,164✔
48
}
853,076,393✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
1,133,975,678✔
51
  int32_t  code = 0;
1,133,975,678✔
52
  SMeta   *pMeta = pReader->pMeta;
1,133,975,678✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
1,134,171,647✔
54

55
  // query table.db
56
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
1,134,164,533✔
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,133,879,147✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
1,133,969,789✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
1,134,069,340✔
65
  if (code) {
1,133,643,848✔
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
1,133,643,848✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
77,319,405✔
79
    metaULock(pVnodeObj->pMeta);
45,995✔
80
    return false;
45,995✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
77,301,267✔
84
  return true;
77,301,013✔
85
}
86

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

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

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
206,715,140✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
206,733,536✔
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) {
175,154✔
150
  if (version < 0) {
175,154✔
151
    return metaReaderGetTableEntryByUid(pReader, uid);
×
152
  }
153
  SMeta *pMeta = pReader->pMeta;
175,154✔
154

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

170
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
614,076,499✔
171
  SMeta *pMeta = pReader->pMeta;
614,076,499✔
172

173
  SMetaInfo info;
614,211,116✔
174
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
614,203,680✔
175
  if (TSDB_CODE_SUCCESS != code) {
614,169,582✔
176
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
4,398✔
177
  }
178

179
  return metaGetTableEntryByVersion(pReader, info.version, uid);
614,165,184✔
180
}
181

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

186
  // query name.idx
187
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
58,998,509✔
188
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
8,172,248✔
189
  }
190

191
  uid = *(tb_uid_t *)pReader->pBuf;
50,808,747✔
192
  return metaReaderGetTableEntryByUid(pReader, uid);
50,815,889✔
193
}
194

195
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
66,584,329✔
196
  void    *pData = NULL;
66,584,329✔
197
  int      nData = 0;
66,585,599✔
198
  tb_uid_t uid = 0;
66,586,753✔
199

200
  metaRLock(pMeta);
66,586,753✔
201

202
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
66,586,435✔
203
    uid = *(tb_uid_t *)pData;
9,183,237✔
204
    tdbFree(pData);
9,183,237✔
205
  }
206

207
  metaULock(pMeta);
66,580,245✔
208

209
  return uid;
66,581,141✔
210
}
211

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

222
  STR_TO_VARSTR(tbName, mr.me.name);
594,991✔
223
  metaReaderClear(&mr);
594,638✔
224

225
  return 0;
594,638✔
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,435,294✔
244
  int         code = 0;
1,435,294✔
245
  SMetaReader mr = {0};
1,435,294✔
246
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
1,435,294✔
247

248
  SMetaReader *pReader = &mr;
1,435,294✔
249

250
  // query name.idx
251
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
1,435,294✔
252
    metaReaderClear(&mr);
885,650✔
253
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
886,284✔
254
  }
255

256
  *uid = *(tb_uid_t *)pReader->pBuf;
549,327✔
257

258
  metaReaderClear(&mr);
549,327✔
259

260
  return 0;
549,327✔
261
}
262

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

268
  code = metaGetTableEntryByName(&mr, tbName);
550,106✔
269
  if (code == 0) *tbType = mr.me.type;
549,756✔
270
  if (TSDB_CHILD_TABLE == mr.me.type || TSDB_VIRTUAL_CHILD_TABLE == mr.me.type) {
549,756✔
271
    *suid = mr.me.ctbEntry.suid;
541,511✔
272
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
8,245✔
273
    *suid = mr.me.uid;
5,304✔
274
  } else {
275
    *suid = 0;
2,941✔
276
  }
277

278
  metaReaderClear(&mr);
550,106✔
279
  return code;
549,756✔
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) {
12,444,700✔
315
  SMTbCursor *pTbCur = NULL;
12,444,700✔
316
  int32_t     code;
317

318
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
12,444,700✔
319
  if (pTbCur == NULL) {
12,446,038✔
320
    return NULL;
×
321
  }
322

323
  SVnode *pVnodeObj = pVnode;
12,446,038✔
324
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
325
  pTbCur->pMeta = pVnodeObj->pMeta;
12,446,038✔
326
  pTbCur->paused = 1;
12,448,724✔
327
  code = metaResumeTbCursor(pTbCur, 1, 0);
12,448,947✔
328
  if (code) {
12,441,476✔
329
    terrno = code;
×
330
    taosMemoryFree(pTbCur);
×
331
    return NULL;
×
332
  }
333
  return pTbCur;
12,441,476✔
334
}
335

336
void metaCloseTbCursor(SMTbCursor *pTbCur) {
24,895,070✔
337
  if (pTbCur) {
24,895,070✔
338
    tdbFree(pTbCur->pKey);
12,449,783✔
339
    tdbFree(pTbCur->pVal);
12,449,042✔
340
    if (!pTbCur->paused) {
12,448,859✔
341
      metaReaderClear(&pTbCur->mr);
3,103,497✔
342
      if (pTbCur->pDbc) {
3,104,539✔
343
        tdbTbcClose((TBC *)pTbCur->pDbc);
3,104,856✔
344
      }
345
    }
346
    taosMemoryFree(pTbCur);
12,449,359✔
347
  }
348
}
24,893,209✔
349

350
void metaPauseTbCursor(SMTbCursor *pTbCur) {
9,346,637✔
351
  if (!pTbCur->paused) {
9,346,637✔
352
    metaReaderClear(&pTbCur->mr);
9,346,063✔
353
    tdbTbcClose((TBC *)pTbCur->pDbc);
9,346,637✔
354
    pTbCur->paused = 1;
9,347,761✔
355
  }
356
}
9,348,335✔
357
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
12,447,797✔
358
  int32_t code = 0;
12,447,797✔
359
  int32_t lino;
360
  int8_t  locked = 0;
12,447,797✔
361
  if (pTbCur->paused) {
12,447,797✔
362
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
12,446,063✔
363
    locked = 1;
12,450,168✔
364
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
12,450,168✔
365
    if (code != 0) {
12,445,617✔
366
      TSDB_CHECK_CODE(code, lino, _exit);
×
367
    }
368

369
    if (first) {
12,445,617✔
370
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
12,443,412✔
371
      TSDB_CHECK_CODE(code, lino, _exit);
12,438,607✔
372
    } else {
373
      int c = 1;
2,205✔
374
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
2,205✔
375
      TSDB_CHECK_CODE(code, lino, _exit);
2,205✔
376
      if (c == 0) {
2,205✔
377
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
2,205✔
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;
12,445,051✔
388
  }
389

390
_exit:
×
391
  if (code != 0 && locked) {
12,447,410✔
392
    metaReaderReleaseLock(&pTbCur->mr);
×
393
  }
394
  return code;
12,446,069✔
395
}
396

397
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
325,505,270✔
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);
325,505,270✔
404
    if (ret < 0) {
325,506,167✔
405
      return ret;
12,445,469✔
406
    }
407

408
    tDecoderClear(&pTbCur->mr.coder);
313,060,698✔
409

410
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
313,060,704✔
411
    if (ret) return ret;
313,027,497✔
412

413
    if (pTbCur->mr.me.type == jumpTableType) {
313,027,497✔
414
      continue;
4,054,315✔
415
    }
416

417
    break;
308,982,843✔
418
  }
419

420
  return 0;
308,982,843✔
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,
252,968,799✔
455
                                   int8_t type) {
456
  int32_t         code = 0;
252,968,799✔
457
  void           *pData = NULL;
252,968,799✔
458
  int             nData = 0;
252,808,460✔
459
  int64_t         version;
460
  SSchemaWrapper  schema = {0};
252,780,522✔
461
  SSchemaWrapper *pSchema = NULL;
252,638,918✔
462
  SDecoder        dc = {0};
252,638,918✔
463
  if (lock) {
252,468,023✔
464
    metaRLock(pMeta);
248,038,121✔
465
  }
466
_query:
285,028,136✔
467
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
285,138,656✔
468
    goto _err;
344,224✔
469
  }
470

471
  version = ((SUidIdxVal *)pData)[0].version;
284,894,798✔
472

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

478
  SMetaEntry me = {0};
284,920,662✔
479
  tDecoderInit(&dc, pData, nData);
284,802,293✔
480
  code = metaDecodeEntry(&dc, &me);
284,912,472✔
481
  if (code) {
284,794,966✔
482
    tDecoderClear(&dc);
×
483
    goto _err;
×
484
  }
485
  if (me.type == TSDB_SUPER_TABLE) {
284,794,966✔
486
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
187,393,624✔
487
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
187,360,006✔
488
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
187,360,006✔
489
      if (TABLE_IS_ROLLUP(me.flags)) {
187,361,541✔
490
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
93,525✔
491
          tDecoderClear(&dc);
×
492
          goto _err;
×
493
        }
494
      }
495
      tDecoderClear(&dc);
187,361,541✔
496
      goto _exit;
187,318,175✔
497
    }
498
  } else if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
97,401,342✔
499
    uid = me.ctbEntry.suid;
32,095,265✔
500
    tDecoderClear(&dc);
32,095,265✔
501
    goto _query;
32,111,011✔
502
  } else {
503
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
65,306,077✔
504
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
65,315,505✔
505
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
65,315,505✔
506
      tDecoderClear(&dc);
65,321,072✔
507
      goto _exit;
65,317,852✔
508
    }
509
  }
510
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
44,407✔
511
  tDecoderClear(&dc);
44,407✔
512

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

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

529
_exit:
252,680,231✔
530
  if (lock) {
252,714,366✔
531
    metaULock(pMeta);
248,270,715✔
532
  }
533
  tdbFree(pData);
252,684,664✔
534
  return pSchema;
252,654,166✔
535

536
_err:
336,318✔
537
  if (lock) {
344,224✔
538
    metaULock(pMeta);
343,988✔
539
  }
540
  tdbFree(pData);
344,224✔
541
  tDeleteSchemaWrapper(pSchema);
542
  if (extSchema != NULL) {
344,224✔
543
    taosMemoryFreeClear(*extSchema);
198,503✔
544
  }
545
  terrno = code;
344,224✔
546
  return NULL;
344,224✔
547
}
548

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

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

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

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

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

583
_exit:
609,909✔
584
  if (lock) {
610,537✔
585
    metaULock(pMeta);
610,537✔
586
  }
587
  tdbFree(pData);
609,909✔
588
  return createTime;
610,222✔
589
}
590

591
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
118,509,855✔
592
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
118,509,855✔
593
  SMCtbCursor *pCtbCur = NULL;
118,530,495✔
594
  SCtbIdxKey   ctbIdxKey;
595
  int          ret = 0;
118,530,495✔
596
  int          c = 0;
118,530,495✔
597

598
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
118,530,495✔
599
  if (pCtbCur == NULL) {
118,440,814✔
600
    return NULL;
×
601
  }
602

603
  pCtbCur->pMeta = pMeta;
118,440,814✔
604
  pCtbCur->suid = uid;
118,440,461✔
605
  pCtbCur->lock = lock;
118,450,397✔
606
  pCtbCur->paused = 1;
118,507,284✔
607

608
  ret = metaResumeCtbCursor(pCtbCur, 1);
118,508,252✔
609
  if (ret < 0) {
118,493,129✔
610
    return NULL;
×
611
  }
612
  return pCtbCur;
118,493,129✔
613
}
614

615
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
118,536,596✔
616
  if (pCtbCur) {
118,536,596✔
617
    if (!pCtbCur->paused) {
118,541,484✔
618
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
114,114,904✔
619
      if (pCtbCur->pCur) {
114,104,162✔
620
        tdbTbcClose(pCtbCur->pCur);
114,112,884✔
621
      }
622
    }
623
    tdbFree(pCtbCur->pKey);
118,500,914✔
624
    tdbFree(pCtbCur->pVal);
118,488,831✔
625
  }
626
  taosMemoryFree(pCtbCur);
118,476,999✔
627
}
118,472,932✔
628

629
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
4,429,358✔
630
  if (!pCtbCur->paused) {
4,429,358✔
631
    tdbTbcClose((TBC *)pCtbCur->pCur);
4,430,067✔
632
    if (pCtbCur->lock) {
4,428,781✔
633
      metaULock(pCtbCur->pMeta);
4,430,913✔
634
    }
635
    pCtbCur->paused = 1;
4,427,399✔
636
  }
637
}
4,431,611✔
638

639
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
118,513,822✔
640
  if (pCtbCur->paused) {
118,513,822✔
641
    pCtbCur->paused = 0;
118,484,327✔
642

643
    if (pCtbCur->lock) {
118,523,614✔
644
      metaRLock(pCtbCur->pMeta);
114,055,487✔
645
    }
646
    int ret = 0;
118,536,616✔
647
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
118,536,616✔
648
    if (ret < 0) {
118,497,044✔
649
      metaCloseCtbCursor(pCtbCur);
×
650
      return -1;
×
651
    }
652

653
    if (first) {
118,497,044✔
654
      SCtbIdxKey ctbIdxKey;
118,495,188✔
655
      // move to the suid
656
      ctbIdxKey.suid = pCtbCur->suid;
118,497,728✔
657
      ctbIdxKey.uid = INT64_MIN;
118,520,426✔
658
      int c = 0;
118,520,426✔
659
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
118,463,120✔
660
      if (c > 0) {
118,518,969✔
661
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
17,544,486✔
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;
118,500,952✔
674
}
675

676
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
701,776,041✔
677
  int         ret;
678
  SCtbIdxKey *pCtbIdxKey;
679

680
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
701,776,041✔
681
  if (ret < 0) {
701,886,339✔
682
    return 0;
63,456,107✔
683
  }
684

685
  pCtbIdxKey = pCtbCur->pKey;
638,430,232✔
686
  if (pCtbIdxKey->suid > pCtbCur->suid) {
638,435,144✔
687
    return 0;
55,184,091✔
688
  }
689

690
  return pCtbIdxKey->uid;
583,330,155✔
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) {
61,910,720✔
704
  SMStbCursor *pStbCur = NULL;
61,910,720✔
705
  int          ret = 0;
61,910,720✔
706
  int          c = 0;
61,910,720✔
707

708
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
61,911,972✔
709
  if (pStbCur == NULL) {
61,893,258✔
710
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
711
    return NULL;
×
712
  }
713

714
  pStbCur->pMeta = pMeta;
61,893,258✔
715
  pStbCur->suid = suid;
61,910,094✔
716
  metaRLock(pMeta);
61,903,208✔
717

718
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
61,909,468✔
719
  if (ret < 0) {
61,901,186✔
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);
61,901,186✔
728
  if (c > 0) {
61,917,282✔
729
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
730
  }
731

732
  return pStbCur;
61,912,274✔
733
}
734

735
void metaCloseStbCursor(SMStbCursor *pStbCur) {
61,910,720✔
736
  if (pStbCur) {
61,910,720✔
737
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
61,913,850✔
738
    if (pStbCur->pCur) {
61,918,232✔
739
      tdbTbcClose(pStbCur->pCur);
61,917,606✔
740

741
      tdbFree(pStbCur->pKey);
61,910,094✔
742
      tdbFree(pStbCur->pVal);
61,912,598✔
743
    }
744

745
    taosMemoryFree(pStbCur);
61,908,842✔
746
  }
747
}
61,908,216✔
748

749
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
107,139,241✔
750
  int ret;
751

752
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
107,139,241✔
753
  if (ret < 0) {
107,125,167✔
754
    return 0;
61,916,980✔
755
  }
756
  return *(tb_uid_t *)pStbCur->pKey;
45,208,187✔
757
}
758

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

763
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
203,593,510✔
764
  if (!pSW) return NULL;
203,627,182✔
765

766
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
203,593,712✔
767

768
  tDeleteSchemaWrapper(pSW);
769
  return pTSchema;
203,433,285✔
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) {
26,100✔
776
  SRSchema       *pRSchema = NULL;
26,100✔
777
  SSchemaWrapper *pSW = NULL;
26,100✔
778

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

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

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

794
_exit:
26,100✔
795
  tDeleteSchemaWrapper(pSW);
796
  return pRSchema;
26,100✔
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) {
594,646✔
804
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
594,646✔
805
  if (*ppTSchema == NULL) {
594,646✔
806
    return terrno;
×
807
  }
808
  return TSDB_CODE_SUCCESS;
594,238✔
809
}
810

811
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
202,867,396✔
812
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
202,867,396✔
813
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
202,677,516✔
814
    return terrno;
×
815
  }
816
  return TSDB_CODE_SUCCESS;
202,855,047✔
817
}
818

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

823
  void     *pData = NULL;
66,840,901✔
824
  int       nData = 0;
66,861,704✔
825
  SSkmDbKey skmDbKey;
66,865,333✔
826
  if (sver <= 0) {
66,859,416✔
827
    SMetaInfo info;
33,546,796✔
828
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
33,549,911✔
829
      sver = info.skmVer;
33,522,875✔
830
    } else {
831
      TBC *pSkmDbC = NULL;
32,208✔
832
      int  c;
34,047✔
833

834
      skmDbKey.uid = suid ? suid : uid;
34,047✔
835
      skmDbKey.sver = INT32_MAX;
34,047✔
836

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

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

848
      if (c == 0) {
34,047✔
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) {
34,047✔
857
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
858
      }
859

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

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

871
      sver = ((SSkmDbKey *)pKey)->sver;
34,047✔
872

873
      metaULock(pMeta);
34,047✔
874
      tdbTbcClose(pSkmDbC);
34,047✔
875
    }
876
  }
877

878
  if (!(sver > 0)) {
66,865,067✔
879
    code = TSDB_CODE_NOT_FOUND;
×
880
    goto _exit;
×
881
  }
882

883
  skmDbKey.uid = suid ? suid : uid;
66,865,067✔
884
  skmDbKey.sver = sver;
66,865,067✔
885
  metaRLock(pMeta);
66,865,067✔
886
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
66,865,767✔
887
    metaULock(pMeta);
2,504✔
888
    code = TSDB_CODE_NOT_FOUND;
2,504✔
889
    goto _exit;
2,504✔
890
  }
891
  metaULock(pMeta);
66,844,552✔
892

893
  // decode
894
  SDecoder        dc = {0};
66,874,305✔
895
  SSchemaWrapper  schema;
66,876,439✔
896
  SSchemaWrapper *pSchemaWrapper = &schema;
66,874,934✔
897

898
  tDecoderInit(&dc, pData, nData);
66,874,934✔
899
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
66,885,290✔
900
  tDecoderClear(&dc);
66,885,290✔
901
  tdbFree(pData);
66,875,134✔
902
  if (TSDB_CODE_SUCCESS != code) {
66,839,946✔
903
    taosMemoryFree(pSchemaWrapper->pSchema);
×
904
    goto _exit;
×
905
  }
906

907
  // convert
908
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
66,839,946✔
909
  if (pTSchema == NULL) {
66,875,353✔
910
    code = TSDB_CODE_OUT_OF_MEMORY;
×
911
  }
912

913
  *ppTSchema = pTSchema;
66,875,353✔
914
  taosMemoryFree(pSchemaWrapper->pSchema);
66,877,897✔
915

916
_exit:
66,846,341✔
917
  return code;
66,854,794✔
918
}
919

920
// N.B. Called by statusReq per second
921
int64_t metaGetTbNum(SMeta *pMeta) {
126,101,855✔
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;
126,101,855✔
928
}
929

930
void metaUpdTimeSeriesNum(SMeta *pMeta) {
61,903,934✔
931
  int64_t nCtbTimeSeries = 0;
61,903,934✔
932
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
61,905,812✔
933
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
61,908,942✔
934
  }
935
}
61,910,194✔
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;
203,311,602✔
940
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
203,311,770✔
941
    metaUpdTimeSeriesNum(pMeta);
61,898,811✔
942
  }
943

944
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
203,332,201✔
945
}
946

947
// type: 1 reported timeseries
948
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
203,296,961✔
949
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
203,315,647✔
950
  if (type == 1) {
203,315,647✔
951
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
126,101,855✔
952
  }
953
  return nTimeSeries;
203,265,181✔
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) {
949,203,259✔
1213
  STag *tag = (STag *)pTag;
949,203,259✔
1214
  if (type == TSDB_DATA_TYPE_JSON) {
949,203,259✔
1215
    return tag;
1,440,908✔
1216
  }
1217
  bool find = tTagGet(tag, val);
947,762,351✔
1218

1219
  if (!find) {
947,951,220✔
1220
    return NULL;
7,475,292✔
1221
  }
1222

1223
  return val;
940,475,928✔
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,576✔
1239
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,576✔
1240
  SMetaFltParam *param = arg;
1,576✔
1241
  int32_t        ret = 0;
1,576✔
1242

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

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

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

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

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

1271
  static const int8_t TRY_ERROR_LIMIT = 1;
1272
  do {
5,837,255✔
1273
    void   *entryKey = NULL;
5,838,831✔
1274
    int32_t nEntryKey = -1;
5,838,685✔
1275
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
5,838,831✔
1276
    if (valid < 0) break;
5,841,751✔
1277

1278
    SBtimeIdxKey *p = entryKey;
5,840,175✔
1279
    if (count > TRY_ERROR_LIMIT) break;
5,840,175✔
1280

1281
    terrno = TSDB_CODE_SUCCESS;
5,840,175✔
1282
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
5,838,715✔
1283
    if (terrno != TSDB_CODE_SUCCESS) {
5,838,277✔
1284
      ret = terrno;
×
1285
      break;
×
1286
    }
1287
    if (cmp == 0) {
5,837,693✔
1288
      if (taosArrayPush(pUids, &p->uid) == NULL) {
11,676,846✔
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);
5,839,153✔
1299
    if (valid < 0) break;
5,837,547✔
1300
  } while (1);
1301

1302
END:
1,868✔
1303
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
1,576✔
1304
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
1,576✔
1305
  taosMemoryFree(pCursor);
1,576✔
1306
  return ret;
1,576✔
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) {
3,118,823✔
1422
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,118,823✔
1423
  SMetaFltParam *param = arg;
3,120,544✔
1424

1425
  SMetaEntry oStbEntry = {0};
3,120,544✔
1426
  int32_t    code = 0;
3,119,658✔
1427
  char      *buf = NULL;
3,119,658✔
1428
  void      *pData = NULL;
3,119,658✔
1429
  int        nData = 0;
3,117,423✔
1430

1431
  SDecoder    dc = {0};
3,116,290✔
1432
  STbDbKey    tbDbKey = {0};
3,116,333✔
1433
  STagIdxKey *pKey = NULL;
3,115,362✔
1434
  int32_t     nKey = 0;
3,118,929✔
1435

1436
  SIdxCursor *pCursor = NULL;
3,121,385✔
1437
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,121,385✔
1438
  if (!pCursor) {
3,114,058✔
1439
    return terrno;
×
1440
  }
1441
  pCursor->pMeta = pMeta;
3,114,058✔
1442
  pCursor->suid = param->suid;
3,118,361✔
1443
  pCursor->cid = param->cid;
3,116,281✔
1444
  pCursor->type = param->type;
3,121,207✔
1445

1446
  metaRLock(pMeta);
3,121,069✔
1447

1448
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
3,120,187✔
1449

1450
  tbDbKey.uid = param->suid;
3,123,353✔
1451
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,123,717✔
1452

1453
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
3,120,383✔
1454

1455
  tDecoderInit(&dc, pData, nData);
3,123,344✔
1456

1457
  code = metaDecodeEntry(&dc, &oStbEntry);
3,121,601✔
1458
  if (code) {
3,119,740✔
1459
    tDecoderClear(&dc);
×
1460
    goto END;
×
1461
  }
1462

1463
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
3,119,740✔
1464
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1465
  }
1466

1467
  code = TSDB_CODE_INVALID_PARA;
3,119,740✔
1468

1469
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,120,544✔
1470
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,120,538✔
1471
    if (IS_IDX_ON(schema)) {
4,119,446✔
1472
      if (schema->colId == param->cid && param->type == schema->type) {
4,114,347✔
1473
        code = 0;
3,118,532✔
1474
        break;
3,118,532✔
1475
      }
1476
    }
1477
  }
1478

1479
  TAOS_CHECK_GOTO(code, NULL, END);
3,118,538✔
1480

1481
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,118,538✔
1482
  if (code != 0) {
3,119,432✔
1483
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1484
  }
1485

1486
  int32_t maxSize = 0;
3,119,432✔
1487
  int32_t nTagData = 0;
3,119,912✔
1488
  void   *tagData = NULL;
3,119,912✔
1489

1490
  if (param->val == NULL) {
3,119,912✔
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)) {
3,116,384✔
1495
      tagData = varDataVal(param->val);
434,963✔
1496
      nTagData = varDataLen(param->val);
438,617✔
1497

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

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

1509
        tagData = buf;
78,710✔
1510
        nTagData = maxSize;
78,710✔
1511
      }
1512
    } else {
1513
      tagData = param->val;
2,681,399✔
1514
      nTagData = tDataTypes[param->type].bytes;
2,683,043✔
1515
    }
1516
  }
1517

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

1522
  int cmp = 0;
3,119,217✔
1523
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,121,657✔
1524

1525
  int     count = 0;
3,123,387✔
1526
  int32_t valid = 0;
3,123,387✔
1527
  bool    found = false;
3,123,387✔
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;
3,123,387✔
1534
  do {
30,033,608✔
1535
    void   *entryKey = NULL, *entryVal = NULL;
33,156,995✔
1536
    int32_t nEntryKey, nEntryVal;
33,155,713✔
1537

1538
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
33,156,682✔
1539
    if (valid < 0) {
33,167,944✔
1540
      break;
1,646,546✔
1541
    }
1542
    if (count > TRY_ERROR_LIMIT) {
31,521,398✔
1543
      break;
918,285✔
1544
    }
1545

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

1549
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
30,603,113✔
1550
      if (found == true) break;  //
940,059✔
1551
      if (diffCidCount > TRY_ERROR_LIMIT) break;
380,808✔
1552
      diffCidCount++;
380,808✔
1553
      count++;
380,808✔
1554
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
380,808✔
1555
      if (valid < 0) {
379,666✔
1556
        code = valid;
×
1557
        break;
×
1558
      } else {
1559
        continue;
379,666✔
1560
      }
1561
    }
1562

1563
    terrno = TSDB_CODE_SUCCESS;
29,658,558✔
1564
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
29,659,465✔
1565
    if (terrno != TSDB_CODE_SUCCESS) {
29,655,589✔
1566
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1567
      break;
×
1568
    }
1569
    if (cmp == 0) {
29,655,032✔
1570
      // match
1571
      tb_uid_t tuid = 0;
26,502,814✔
1572
      if (IS_VAR_DATA_TYPE(pKey->type)) {
26,502,814✔
1573
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
368,246✔
1574
      } else {
1575
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
26,128,129✔
1576
      }
1577
      if (taosArrayPush(pUids, &tuid) == NULL) {
26,503,794✔
1578
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1579
      }
1580
      found = true;
26,502,826✔
1581
    } else {
1582
      if (param->equal == true) {
3,152,218✔
1583
        if (count > TRY_ERROR_LIMIT) break;
2,191,546✔
1584
        count++;
2,191,546✔
1585
      }
1586
    }
1587
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
29,656,677✔
1588
    if (valid < 0) {
29,652,665✔
1589
      code = valid;
×
1590
      break;
×
1591
    }
1592
  } while (1);
1593

1594
END:
3,124,447✔
1595
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,124,082✔
1596
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,124,082✔
1597
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,121,771✔
1598
  tDecoderClear(&dc);
3,121,771✔
1599
  tdbFree(pData);
3,122,960✔
1600

1601
  taosMemoryFree(buf);
3,121,738✔
1602
  taosMemoryFree(pKey);
3,124,082✔
1603

1604
  taosMemoryFree(pCursor);
3,119,755✔
1605

1606
  return code;
3,120,578✔
1607
}
1608

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

1615
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
47,191,060✔
1616
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
47,191,060✔
1617
  if (lock) {
47,188,744✔
1618
    metaULock(pMeta);
×
1619
  }
1620

1621
  return ret;
47,188,744✔
1622
}
1623

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

1628
  int32_t isLock = false;
6,593,145✔
1629
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
6,593,145✔
1630
  for (int i = 0; i < sz; i++) {
53,781,408✔
1631
    STUidTagInfo *p = taosArrayGet(uidList, i);
47,190,338✔
1632
    if (p->pTagVal != NULL) continue;
47,190,197✔
1633

1634
    if (i % LIMIT == 0) {
47,190,197✔
1635
      if (isLock) metaULock(pMeta);
6,590,619✔
1636

1637
      metaRLock(pMeta);
6,590,619✔
1638
      isLock = true;
6,589,993✔
1639
    }
1640

1641
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1642
    void   *val = NULL;
47,189,571✔
1643
    int32_t len = 0;
47,189,455✔
1644
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
47,189,455✔
1645
      p->pTagVal = taosMemoryMalloc(len);
47,183,014✔
1646
      if (!p->pTagVal) {
47,181,872✔
UNCOV
1647
        if (isLock) metaULock(pMeta);
×
1648

UNCOV
1649
        TAOS_RETURN(terrno);
×
1650
      }
1651
      memcpy(p->pTagVal, val, len);
47,182,493✔
1652
      tdbFree(val);
47,182,863✔
1653
    } else {
1654
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
5,730✔
1655
    }
1656
  }
1657
  //  }
1658
  if (isLock) metaULock(pMeta);
6,591,070✔
1659
  return 0;
6,593,145✔
1660
}
1661

1662
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
10,602,206✔
1663
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
10,602,206✔
1664
  if (!pCur) {
10,597,347✔
UNCOV
1665
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1666
  }
1667

1668
  while (1) {
139,758,545✔
1669
    tb_uid_t uid = metaCtbCursorNext(pCur);
150,355,892✔
1670
    if (uid == 0) {
150,352,226✔
1671
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
10,606,355✔
1672
      break;
10,605,734✔
1673
    }
1674

1675
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
139,745,871✔
1676
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
139,746,681✔
1677
    if (!info.pTagVal) {
139,737,343✔
UNCOV
1678
      metaCloseCtbCursor(pCur);
×
1679
      return terrno;
×
1680
    }
1681
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
139,737,343✔
1682
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
139,757,536✔
UNCOV
1683
      taosMemoryFreeClear(info.pTagVal);
×
1684
      metaCloseCtbCursor(pCur);
×
1685
      return terrno;
×
1686
    }
1687
  }
1688
  metaCloseCtbCursor(pCur);
10,606,685✔
1689
  return TSDB_CODE_SUCCESS;
10,603,091✔
1690
}
1691

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

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

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

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

UNCOV
1718
  metaCloseStbCursor(pCur);
×
1719

UNCOV
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

UNCOV
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

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

UNCOV
1733
      return code;
×
1734
    }
1735

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

UNCOV
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

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

UNCOV
1746
        return code;
×
1747
      }
1748

UNCOV
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

UNCOV
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

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

UNCOV
1762
          return code;
×
1763
        }
1764
      }
1765

UNCOV
1766
      tDestroyTSchema(pTSchema);
×
1767
    }
1768

UNCOV
1769
    taosArrayDestroy(uids);
×
1770
  }
1771

UNCOV
1772
  taosArrayDestroy(suids);
×
1773

UNCOV
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,100,552,531✔
1780
  int32_t code = 0;
2,100,552,531✔
1781
  void   *pData = NULL;
2,100,552,531✔
1782
  int     nData = 0;
2,100,724,307✔
1783
  int     lock = 0;
2,100,857,404✔
1784

1785
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
2,100,857,404✔
1786
    lock = 1;
614,447,223✔
1787
  }
1788

1789
  if (!lock) metaRLock(pMeta);
2,100,811,036✔
1790

1791
  // search cache
1792
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
2,100,869,921✔
1793
    if (!lock) metaULock(pMeta);
2,009,356,539✔
1794
    goto _exit;
2,009,329,805✔
1795
  }
1796

1797
  // search TDB
1798
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
91,561,247✔
1799
    // not found
1800
    if (!lock) metaULock(pMeta);
82,296,065✔
1801
    goto _exit;
82,294,028✔
1802
  }
1803

1804
  if (!lock) metaULock(pMeta);
9,260,517✔
1805

1806
  pInfo->uid = uid;
9,260,517✔
1807
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
9,257,967✔
1808
  pInfo->version = ((SUidIdxVal *)pData)->version;
9,258,501✔
1809
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
9,254,469✔
1810

1811
  if (lock) {
9,259,845✔
1812
    metaULock(pReader->pMeta);
1,146,482✔
1813
    // metaReaderReleaseLock(pReader);
1814
  }
1815
  // upsert the cache
1816
  metaWLock(pMeta);
9,252,958✔
1817
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
9,250,437✔
1818
  if (ret != 0) {
9,265,163✔
UNCOV
1819
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1820
  }
1821
  metaULock(pMeta);
9,265,163✔
1822

1823
  if (lock) {
9,261,131✔
1824
    metaRLock(pReader->pMeta);
1,147,768✔
1825
  }
1826

1827
_exit:
2,100,877,291✔
1828
  tdbFree(pData);
2,100,847,246✔
1829
  return code;
2,100,742,956✔
1830
}
1831

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

1835
  if (!numOfTables && !numOfCols) goto _exit;
115,172,323✔
1836

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

1840
  // fast path: search cache
1841
  SMetaStbStats state = {0};
115,166,851✔
1842
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
115,187,159✔
1843
    metaULock(pVnodeObj->pMeta);
110,778,227✔
1844
    if (numOfTables) *numOfTables = state.ctbNum;
110,789,276✔
1845
    if (numOfCols) *numOfCols = state.colNum;
110,791,780✔
1846
    if (flags) *flags = state.flags;
110,777,354✔
1847
    goto _exit;
110,776,728✔
1848
  }
1849

1850
  // slow path: search TDB
1851
  int64_t ctbNum = 0;
4,430,077✔
1852
  int32_t colNum = 0;
4,430,836✔
1853
  int64_t keep = 0;
4,430,880✔
1854
  int8_t  flag = 0;
4,430,876✔
1855

1856
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
4,431,281✔
1857
  if (TSDB_CODE_SUCCESS == code) {
4,429,942✔
1858
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
4,429,942✔
1859
  }
1860
  if (TSDB_CODE_SUCCESS == code) {
4,430,133✔
1861
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
4,430,133✔
1862
  }
1863
  metaULock(pVnodeObj->pMeta);
4,430,717✔
1864
  if (TSDB_CODE_SUCCESS != code) {
4,430,445✔
UNCOV
1865
    goto _exit;
×
1866
  }
1867

1868
  if (numOfTables) *numOfTables = ctbNum;
4,430,445✔
1869
  if (numOfCols) *numOfCols = colNum;
4,430,445✔
1870
  if (flags) *flags = flag;
4,431,049✔
1871

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

1880
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
4,430,042✔
1881
  if (ret) {
4,429,393✔
UNCOV
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,429,393✔
1887

1888
_exit:
115,207,154✔
1889
  return code;
115,209,271✔
1890
}
1891

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

1895
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
72,179,885✔
1896
    stats.ctbNum += deltaCtb;
61,679,365✔
1897
    stats.colNum += deltaCol;
61,679,365✔
1898
    if (deltaKeep > 0) {
61,679,365✔
1899
      stats.keep = deltaKeep;
13,213✔
1900
    }
1901

1902
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
61,679,365✔
1903
    if (code) {
61,648,233✔
UNCOV
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
}
72,161,846✔
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