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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

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

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

59.38
/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) {
627,794,781✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
627,794,781✔
21
  metaReaderDoInit(pReader, pMeta, flags);
627,936,986✔
22
  pReader->pAPI = pAPI;
627,961,318✔
23
}
627,975,056✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
726,230,426✔
26
  memset(pReader, 0, sizeof(*pReader));
726,230,426✔
27
  pReader->pMeta = pMeta;
726,230,426✔
28
  pReader->flags = flags;
726,233,716✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
726,230,452✔
30
    metaRLock(pMeta);
611,616,328✔
31
  }
32
}
726,372,177✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
239,748,985✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
239,748,985✔
36
    metaULock(pReader->pMeta);
239,757,311✔
37
    pReader->flags |= META_READER_NOLOCK;
239,690,111✔
38
  }
39
}
239,868,847✔
40

41
void metaReaderClear(SMetaReader *pReader) {
733,309,007✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
733,309,007✔
43
    metaULock(pReader->pMeta);
371,354,456✔
44
  }
45
  tDecoderClear(&pReader->coder);
733,293,077✔
46
  tdbFree(pReader->pBuf);
733,660,302✔
47
  pReader->pBuf = NULL;
733,293,567✔
48
}
733,286,359✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
981,686,823✔
51
  int32_t  code = 0;
981,686,823✔
52
  SMeta   *pMeta = pReader->pMeta;
981,686,823✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
981,951,458✔
54

55
  // query table.db
56
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
981,928,117✔
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);
981,471,563✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
981,648,279✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
981,630,366✔
65
  if (code) {
981,222,028✔
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
981,222,028✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
140,525,154✔
79
    metaULock(pVnodeObj->pMeta);
45,887✔
80
    return false;
45,887✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
140,548,953✔
84
  return true;
140,555,608✔
85
}
86

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

91
  // query uid.idx
92
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
152,627,377✔
93
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
95,786✔
94
  }
95

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

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

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

173
  SMetaInfo info;
642,253,882✔
174
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
642,227,176✔
175
  if (TSDB_CODE_SUCCESS != code) {
642,110,566✔
176
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
4,184✔
177
  }
178

179
  return metaGetTableEntryByVersion(pReader, info.version, uid);
642,106,382✔
180
}
181

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

186
  // query name.idx
187
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
27,150,087✔
188
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
7,165,172✔
189
  }
190

191
  uid = *(tb_uid_t *)pReader->pBuf;
19,985,530✔
192
  return metaReaderGetTableEntryByUid(pReader, uid);
19,985,530✔
193
}
194

195
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
74,492,769✔
196
  void    *pData = NULL;
74,492,769✔
197
  int      nData = 0;
74,494,452✔
198
  tb_uid_t uid = 0;
74,495,114✔
199

200
  metaRLock(pMeta);
74,495,114✔
201

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

207
  metaULock(pMeta);
74,483,196✔
208

209
  return uid;
74,495,523✔
210
}
211

212
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
6,435,578✔
213
  int         code = 0;
6,435,578✔
214
  SMetaReader mr = {0};
6,435,578✔
215
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
6,437,087✔
216
  code = metaReaderGetTableEntryByUid(&mr, uid);
6,440,101✔
217
  if (code < 0) {
6,430,544✔
218
    metaReaderClear(&mr);
×
219
    return code;
×
220
  }
221

222
  STR_TO_VARSTR(tbName, mr.me.name);
6,430,544✔
223
  metaReaderClear(&mr);
6,412,440✔
224

225
  return 0;
6,437,083✔
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) {
634,810✔
244
  int         code = 0;
634,810✔
245
  SMetaReader mr = {0};
634,810✔
246
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
634,810✔
247

248
  SMetaReader *pReader = &mr;
634,810✔
249

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

256
  *uid = *(tb_uid_t *)pReader->pBuf;
381,330✔
257

258
  metaReaderClear(&mr);
381,330✔
259

260
  return 0;
381,330✔
261
}
262

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

268
  code = metaGetTableEntryByName(&mr, tbName);
384,717✔
269
  if (code == 0) *tbType = mr.me.type;
384,717✔
270
  if (TSDB_CHILD_TABLE == mr.me.type || TSDB_VIRTUAL_CHILD_TABLE == mr.me.type) {
384,717✔
271
    *suid = mr.me.ctbEntry.suid;
376,617✔
272
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
8,100✔
273
    *suid = mr.me.uid;
5,244✔
274
  } else {
275
    *suid = 0;
2,856✔
276
  }
277

278
  metaReaderClear(&mr);
384,717✔
279
  return code;
384,717✔
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) {
5,965,825✔
315
  SMTbCursor *pTbCur = NULL;
5,965,825✔
316
  int32_t     code;
317

318
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
5,965,825✔
319
  if (pTbCur == NULL) {
5,966,081✔
320
    return NULL;
×
321
  }
322

323
  SVnode *pVnodeObj = pVnode;
5,966,081✔
324
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
325
  pTbCur->pMeta = pVnodeObj->pMeta;
5,966,081✔
326
  pTbCur->paused = 1;
5,966,696✔
327
  code = metaResumeTbCursor(pTbCur, 1, 0);
5,966,440✔
328
  if (code) {
5,965,256✔
329
    terrno = code;
×
330
    taosMemoryFree(pTbCur);
×
331
    return NULL;
×
332
  }
333
  return pTbCur;
5,965,256✔
334
}
335

336
void metaCloseTbCursor(SMTbCursor *pTbCur) {
11,927,618✔
337
  if (pTbCur) {
11,927,618✔
338
    tdbFree(pTbCur->pKey);
5,966,210✔
339
    tdbFree(pTbCur->pVal);
5,966,210✔
340
    if (!pTbCur->paused) {
5,966,696✔
341
      metaReaderClear(&pTbCur->mr);
697,092✔
342
      if (pTbCur->pDbc) {
697,092✔
343
        tdbTbcClose((TBC *)pTbCur->pDbc);
697,092✔
344
      }
345
    }
346
    taosMemoryFree(pTbCur);
5,965,724✔
347
  }
348
}
11,927,132✔
349

350
void metaPauseTbCursor(SMTbCursor *pTbCur) {
5,271,231✔
351
  if (!pTbCur->paused) {
5,271,231✔
352
    metaReaderClear(&pTbCur->mr);
5,271,717✔
353
    tdbTbcClose((TBC *)pTbCur->pDbc);
5,271,231✔
354
    pTbCur->paused = 1;
5,271,717✔
355
  }
356
}
5,271,717✔
357
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
5,967,547✔
358
  int32_t code = 0;
5,967,547✔
359
  int32_t lino;
360
  int8_t  locked = 0;
5,967,547✔
361
  if (pTbCur->paused) {
5,967,547✔
362
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
5,967,547✔
363
    locked = 1;
5,968,672✔
364
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
5,968,672✔
365
    if (code != 0) {
5,966,883✔
366
      TSDB_CHECK_CODE(code, lino, _exit);
×
367
    }
368

369
    if (first) {
5,966,883✔
370
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
5,964,770✔
371
      TSDB_CHECK_CODE(code, lino, _exit);
5,959,747✔
372
    } else {
373
      int c = 1;
2,113✔
374
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
2,113✔
375
      TSDB_CHECK_CODE(code, lino, _exit);
2,113✔
376
      if (c == 0) {
2,113✔
377
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
2,113✔
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;
5,964,627✔
388
  }
389

390
_exit:
×
391
  if (code != 0 && locked) {
5,964,718✔
392
    metaReaderReleaseLock(&pTbCur->mr);
×
393
  }
394
  return code;
5,964,095✔
395
}
396

397
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
192,951,899✔
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);
192,951,899✔
404
    if (ret < 0) {
192,943,616✔
405
      return ret;
5,962,232✔
406
    }
407

408
    tDecoderClear(&pTbCur->mr.coder);
186,981,384✔
409

410
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
187,005,759✔
411
    if (ret) return ret;
186,933,786✔
412

413
    if (pTbCur->mr.me.type == jumpTableType) {
186,933,786✔
414
      continue;
3,963,075✔
415
    }
416

417
    break;
183,015,217✔
418
  }
419

420
  return 0;
183,015,217✔
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,
319,539,099✔
455
                                   int8_t type) {
456
  int32_t         code = 0;
319,539,099✔
457
  void           *pData = NULL;
319,539,099✔
458
  int             nData = 0;
319,462,424✔
459
  int64_t         version;
460
  SSchemaWrapper  schema = {0};
319,469,989✔
461
  SSchemaWrapper *pSchema = NULL;
319,284,171✔
462
  SDecoder        dc = {0};
319,284,171✔
463
  if (lock) {
318,994,788✔
464
    metaRLock(pMeta);
314,621,280✔
465
  }
466
_query:
350,680,917✔
467
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
350,731,872✔
468
    goto _err;
267,276✔
469
  }
470

471
  version = ((SUidIdxVal *)pData)[0].version;
350,615,021✔
472

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

478
  SMetaEntry me = {0};
350,686,139✔
479
  tDecoderInit(&dc, pData, nData);
350,593,276✔
480
  code = metaDecodeEntry(&dc, &me);
350,667,939✔
481
  if (code) {
350,501,793✔
482
    tDecoderClear(&dc);
×
483
    goto _err;
×
484
  }
485
  if (me.type == TSDB_SUPER_TABLE) {
350,501,793✔
486
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
248,866,631✔
487
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
248,591,938✔
488
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
248,591,938✔
489
      if (TABLE_IS_ROLLUP(me.flags)) {
248,595,885✔
490
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
93,568✔
491
          tDecoderClear(&dc);
×
492
          goto _err;
×
493
        }
494
      }
495
      tDecoderClear(&dc);
248,595,885✔
496
      goto _exit;
248,591,804✔
497
    }
498
  } else if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
101,635,162✔
499
    uid = me.ctbEntry.suid;
30,804,887✔
500
    tDecoderClear(&dc);
30,804,887✔
501
    goto _query;
30,812,084✔
502
  } else {
503
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
70,830,857✔
504
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
70,836,876✔
505
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
70,836,876✔
506
      tDecoderClear(&dc);
70,856,440✔
507
      goto _exit;
70,870,406✔
508
    }
509
  }
510
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
44,281✔
511
  tDecoderClear(&dc);
44,281✔
512

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

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

529
_exit:
319,505,243✔
530
  if (lock) {
319,660,338✔
531
    metaULock(pMeta);
315,290,237✔
532
  }
533
  tdbFree(pData);
319,474,789✔
534
  return pSchema;
319,517,868✔
535

536
_err:
264,628✔
537
  if (lock) {
267,276✔
538
    metaULock(pMeta);
267,145✔
539
  }
540
  tdbFree(pData);
267,276✔
541
  tDeleteSchemaWrapper(pSchema);
542
  if (extSchema != NULL) {
267,276✔
543
    taosMemoryFreeClear(*extSchema);
193,230✔
544
  }
545
  terrno = code;
267,276✔
546
  return NULL;
267,276✔
547
}
548

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

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

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

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

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

583
_exit:
87,925✔
584
  if (lock) {
87,925✔
585
    metaULock(pMeta);
87,925✔
586
  }
587
  tdbFree(pData);
87,925✔
588
  return createTime;
87,925✔
589
}
590

591
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
138,750,422✔
592
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
138,750,422✔
593
  SMCtbCursor *pCtbCur = NULL;
138,876,369✔
594
  SCtbIdxKey   ctbIdxKey;
595
  int          ret = 0;
138,876,369✔
596
  int          c = 0;
138,876,369✔
597

598
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
138,876,369✔
599
  if (pCtbCur == NULL) {
138,433,470✔
600
    return NULL;
×
601
  }
602

603
  pCtbCur->pMeta = pMeta;
138,433,470✔
604
  pCtbCur->suid = uid;
138,437,632✔
605
  pCtbCur->lock = lock;
138,483,115✔
606
  pCtbCur->paused = 1;
138,528,800✔
607

608
  ret = metaResumeCtbCursor(pCtbCur, 1);
138,634,510✔
609
  if (ret < 0) {
138,680,303✔
610
    return NULL;
×
611
  }
612
  return pCtbCur;
138,680,303✔
613
}
614

615
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
138,961,474✔
616
  if (pCtbCur) {
138,961,474✔
617
    if (!pCtbCur->paused) {
138,970,579✔
618
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
133,854,180✔
619
      if (pCtbCur->pCur) {
133,845,203✔
620
        tdbTbcClose(pCtbCur->pCur);
133,854,851✔
621
      }
622
    }
623
    tdbFree(pCtbCur->pKey);
138,892,397✔
624
    tdbFree(pCtbCur->pVal);
138,837,453✔
625
  }
626
  taosMemoryFree(pCtbCur);
138,823,594✔
627
}
138,793,498✔
628

629
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
5,125,394✔
630
  if (!pCtbCur->paused) {
5,125,394✔
631
    tdbTbcClose((TBC *)pCtbCur->pCur);
5,125,394✔
632
    if (pCtbCur->lock) {
5,125,516✔
633
      metaULock(pCtbCur->pMeta);
5,123,788✔
634
    }
635
    pCtbCur->paused = 1;
5,120,119✔
636
  }
637
}
5,123,959✔
638

639
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
138,754,106✔
640
  if (pCtbCur->paused) {
138,754,106✔
641
    pCtbCur->paused = 0;
138,560,822✔
642

643
    if (pCtbCur->lock) {
138,617,774✔
644
      metaRLock(pCtbCur->pMeta);
134,114,508✔
645
    }
646
    int ret = 0;
138,674,860✔
647
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
138,674,860✔
648
    if (ret < 0) {
138,681,759✔
649
      metaCloseCtbCursor(pCtbCur);
×
650
      return -1;
×
651
    }
652

653
    if (first) {
138,681,759✔
654
      SCtbIdxKey ctbIdxKey;
138,679,655✔
655
      // move to the suid
656
      ctbIdxKey.suid = pCtbCur->suid;
138,690,214✔
657
      ctbIdxKey.uid = INT64_MIN;
138,735,221✔
658
      int c = 0;
138,735,221✔
659
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
138,564,999✔
660
      if (c > 0) {
138,746,972✔
661
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
21,970,359✔
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;
138,754,428✔
674
}
675

676
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
736,916,385✔
677
  int         ret;
678
  SCtbIdxKey *pCtbIdxKey;
679

680
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
736,916,385✔
681
  if (ret < 0) {
737,231,435✔
682
    return 0;
116,076,023✔
683
  }
684

685
  pCtbIdxKey = pCtbCur->pKey;
621,155,412✔
686
  if (pCtbIdxKey->suid > pCtbCur->suid) {
621,168,578✔
687
    return 0;
22,987,927✔
688
  }
689

690
  return pCtbIdxKey->uid;
598,049,683✔
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) {
72,255,306✔
704
  SMStbCursor *pStbCur = NULL;
72,255,306✔
705
  int          ret = 0;
72,255,306✔
706
  int          c = 0;
72,255,306✔
707

708
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
72,256,538✔
709
  if (pStbCur == NULL) {
72,246,682✔
710
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
711
    return NULL;
×
712
  }
713

714
  pStbCur->pMeta = pMeta;
72,246,682✔
715
  pStbCur->suid = suid;
72,250,994✔
716
  metaRLock(pMeta);
72,246,766✔
717

718
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
72,253,458✔
719
  if (ret < 0) {
72,242,522✔
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);
72,242,522✔
728
  if (c > 0) {
72,259,618✔
729
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
730
  }
731

732
  return pStbCur;
72,255,922✔
733
}
734

735
void metaCloseStbCursor(SMStbCursor *pStbCur) {
72,256,230✔
736
  if (pStbCur) {
72,256,230✔
737
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
72,256,846✔
738
    if (pStbCur->pCur) {
72,259,618✔
739
      tdbTbcClose(pStbCur->pCur);
72,259,002✔
740

741
      tdbFree(pStbCur->pKey);
72,252,842✔
742
      tdbFree(pStbCur->pVal);
72,255,306✔
743
    }
744

745
    taosMemoryFree(pStbCur);
72,251,610✔
746
  }
747
}
72,251,078✔
748

749
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
126,668,491✔
750
  int ret;
751

752
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
126,668,491✔
753
  if (ret < 0) {
126,660,791✔
754
    return 0;
72,259,310✔
755
  }
756
  return *(tb_uid_t *)pStbCur->pKey;
54,401,481✔
757
}
758

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

763
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
272,039,190✔
764
  if (!pSW) return NULL;
272,178,912✔
765

766
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
272,145,692✔
767

768
  tDeleteSchemaWrapper(pSW);
769
  return pTSchema;
271,974,021✔
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,316✔
776
  SRSchema       *pRSchema = NULL;
26,316✔
777
  SSchemaWrapper *pSW = NULL;
26,316✔
778

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

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

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

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

811
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
271,611,083✔
812
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
271,611,083✔
813
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
271,321,759✔
814
    return terrno;
×
815
  }
816
  return TSDB_CODE_SUCCESS;
271,261,976✔
817
}
818

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

823
  void     *pData = NULL;
87,076,650✔
824
  int       nData = 0;
87,094,461✔
825
  SSkmDbKey skmDbKey;
87,096,834✔
826
  if (sver <= 0) {
87,094,259✔
827
    SMetaInfo info;
42,234,091✔
828
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
42,237,531✔
829
      sver = info.skmVer;
42,198,563✔
830
    } else {
831
      TBC *pSkmDbC = NULL;
30,425✔
832
      int  c;
33,220✔
833

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

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

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

848
      if (c == 0) {
33,220✔
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,220✔
857
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
3,320✔
858
      }
859

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

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

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

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

878
  if (!(sver > 0)) {
87,085,749✔
879
    code = TSDB_CODE_NOT_FOUND;
×
880
    goto _exit;
×
881
  }
882

883
  skmDbKey.uid = suid ? suid : uid;
87,085,749✔
884
  skmDbKey.sver = sver;
87,085,749✔
885
  metaRLock(pMeta);
87,085,749✔
886
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
87,104,034✔
887
    metaULock(pMeta);
2,468✔
888
    code = TSDB_CODE_NOT_FOUND;
2,468✔
889
    goto _exit;
2,468✔
890
  }
891
  metaULock(pMeta);
87,082,704✔
892

893
  // decode
894
  SDecoder        dc = {0};
87,124,853✔
895
  SSchemaWrapper  schema;
87,122,891✔
896
  SSchemaWrapper *pSchemaWrapper = &schema;
87,121,227✔
897

898
  tDecoderInit(&dc, pData, nData);
87,121,227✔
899
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
87,123,632✔
900
  tDecoderClear(&dc);
87,123,632✔
901
  tdbFree(pData);
87,109,899✔
902
  if (TSDB_CODE_SUCCESS != code) {
87,078,804✔
903
    taosMemoryFree(pSchemaWrapper->pSchema);
×
904
    goto _exit;
×
905
  }
906

907
  // convert
908
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
87,078,804✔
909
  if (pTSchema == NULL) {
87,108,092✔
910
    code = TSDB_CODE_OUT_OF_MEMORY;
×
911
  }
912

913
  *ppTSchema = pTSchema;
87,108,092✔
914
  taosMemoryFree(pSchemaWrapper->pSchema);
87,106,827✔
915

916
_exit:
87,076,307✔
917
  return code;
87,087,422✔
918
}
919

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

930
void metaUpdTimeSeriesNum(SMeta *pMeta) {
72,248,933✔
931
  int64_t nCtbTimeSeries = 0;
72,248,933✔
932
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
72,250,165✔
933
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
72,250,781✔
934
  }
935
}
72,252,629✔
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,792,930✔
940
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
198,783,465✔
941
    metaUpdTimeSeriesNum(pMeta);
72,236,979✔
942
  }
943

944
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
198,797,506✔
945
}
946

947
// type: 1 reported timeseries
948
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
198,785,110✔
949
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
198,776,460✔
950
  if (type == 1) {
198,776,460✔
951
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
113,223,103✔
952
  }
953
  return nTimeSeries;
198,743,913✔
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) {
1,250,525,683✔
1213
  STag *tag = (STag *)pTag;
1,250,525,683✔
1214
  if (type == TSDB_DATA_TYPE_JSON) {
1,250,525,683✔
1215
    return tag;
1,407,215✔
1216
  }
1217
  bool find = tTagGet(tag, val);
1,249,118,468✔
1218

1219
  if (!find) {
1,249,161,353✔
1220
    return NULL;
6,720,410✔
1221
  }
1222

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

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

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

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

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

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

1271
  static const int8_t TRY_ERROR_LIMIT = 1;
1272
  do {
5,818,266✔
1273
    void   *entryKey = NULL;
5,819,806✔
1274
    int32_t nEntryKey = -1;
5,819,368✔
1275
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
5,819,368✔
1276
    if (valid < 0) break;
5,837,034✔
1277

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

1281
    terrno = TSDB_CODE_SUCCESS;
5,835,494✔
1282
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
5,833,158✔
1283
    if (terrno != TSDB_CODE_SUCCESS) {
5,829,800✔
1284
      ret = terrno;
×
1285
      break;
×
1286
    }
1287
    if (cmp == 0) {
5,829,946✔
1288
      if (taosArrayPush(pUids, &p->uid) == NULL) {
11,666,900✔
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,836,954✔
1299
    if (valid < 0) break;
5,826,734✔
1300
  } while (1);
1301

1302
END:
2,416✔
1303
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
1,540✔
1304
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
1,540✔
1305
  taosMemoryFree(pCursor);
1,540✔
1306
  return ret;
1,540✔
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,999,944✔
1422
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
2,999,944✔
1423
  SMetaFltParam *param = arg;
3,001,106✔
1424

1425
  SMetaEntry oStbEntry = {0};
3,001,106✔
1426
  int32_t    code = 0;
2,998,679✔
1427
  char      *buf = NULL;
2,998,679✔
1428
  void      *pData = NULL;
2,998,679✔
1429
  int        nData = 0;
2,997,891✔
1430

1431
  SDecoder    dc = {0};
2,997,813✔
1432
  STbDbKey    tbDbKey = {0};
2,996,095✔
1433
  STagIdxKey *pKey = NULL;
2,996,166✔
1434
  int32_t     nKey = 0;
2,994,308✔
1435

1436
  SIdxCursor *pCursor = NULL;
2,997,136✔
1437
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
2,997,136✔
1438
  if (!pCursor) {
2,996,837✔
1439
    return terrno;
×
1440
  }
1441
  pCursor->pMeta = pMeta;
2,996,837✔
1442
  pCursor->suid = param->suid;
2,999,062✔
1443
  pCursor->cid = param->cid;
2,997,899✔
1444
  pCursor->type = param->type;
2,998,606✔
1445

1446
  metaRLock(pMeta);
2,996,664✔
1447

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

1450
  tbDbKey.uid = param->suid;
3,000,908✔
1451
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,001,625✔
1452

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

1455
  tDecoderInit(&dc, pData, nData);
3,002,673✔
1456

1457
  code = metaDecodeEntry(&dc, &oStbEntry);
3,001,526✔
1458
  if (code) {
3,000,023✔
1459
    tDecoderClear(&dc);
×
1460
    goto END;
×
1461
  }
1462

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

1467
  code = TSDB_CODE_INVALID_PARA;
3,000,023✔
1468

1469
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
3,976,222✔
1470
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
3,978,317✔
1471
    if (IS_IDX_ON(schema)) {
3,978,436✔
1472
      if (schema->colId == param->cid && param->type == schema->type) {
3,970,317✔
1473
        code = 0;
3,000,054✔
1474
        break;
3,000,054✔
1475
      }
1476
    }
1477
  }
1478

1479
  TAOS_CHECK_GOTO(code, NULL, END);
2,997,959✔
1480

1481
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
2,997,959✔
1482
  if (code != 0) {
3,000,163✔
1483
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1484
  }
1485

1486
  int32_t maxSize = 0;
3,000,163✔
1487
  int32_t nTagData = 0;
2,998,648✔
1488
  void   *tagData = NULL;
2,998,648✔
1489

1490
  if (param->val == NULL) {
2,998,648✔
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,996,709✔
1495
      tagData = varDataVal(param->val);
366,844✔
1496
      nTagData = varDataLen(param->val);
365,123✔
1497

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

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

1509
        tagData = buf;
78,184✔
1510
        nTagData = maxSize;
78,184✔
1511
      }
1512
    } else {
1513
      tagData = param->val;
2,632,965✔
1514
      nTagData = tDataTypes[param->type].bytes;
2,635,524✔
1515
    }
1516
  }
1517

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

1522
  int cmp = 0;
2,993,120✔
1523
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
2,995,125✔
1524

1525
  int     count = 0;
3,002,019✔
1526
  int32_t valid = 0;
3,002,019✔
1527
  bool    found = false;
3,002,019✔
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,002,019✔
1534
  do {
29,249,945✔
1535
    void   *entryKey = NULL, *entryVal = NULL;
32,251,964✔
1536
    int32_t nEntryKey, nEntryVal;
32,249,922✔
1537

1538
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
32,251,261✔
1539
    if (valid < 0) {
32,264,469✔
1540
      break;
1,415,959✔
1541
    }
1542
    if (count > TRY_ERROR_LIMIT) {
30,848,510✔
1543
      break;
879,933✔
1544
    }
1545

1546
    STagIdxKey *p = entryKey;
29,968,577✔
1547
    if (p == NULL) break;
29,968,577✔
1548

1549
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
29,968,577✔
1550
      if (found == true) break;  //
970,687✔
1551
      if (diffCidCount > TRY_ERROR_LIMIT) break;
263,529✔
1552
      diffCidCount++;
263,529✔
1553
      count++;
263,529✔
1554
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
263,529✔
1555
      if (valid < 0) {
263,104✔
1556
        code = valid;
×
1557
        break;
×
1558
      } else {
1559
        continue;
263,104✔
1560
      }
1561
    }
1562

1563
    terrno = TSDB_CODE_SUCCESS;
28,994,802✔
1564
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
28,995,660✔
1565
    if (terrno != TSDB_CODE_SUCCESS) {
28,993,692✔
1566
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1567
      break;
×
1568
    }
1569
    if (cmp == 0) {
28,992,404✔
1570
      // match
1571
      tb_uid_t tuid = 0;
25,922,996✔
1572
      if (IS_VAR_DATA_TYPE(pKey->type)) {
25,922,640✔
1573
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
364,551✔
1574
      } else {
1575
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
25,550,305✔
1576
      }
1577
      if (taosArrayPush(pUids, &tuid) == NULL) {
25,926,098✔
1578
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1579
      }
1580
      found = true;
25,926,454✔
1581
    } else {
1582
      if (param->equal == true) {
3,069,408✔
1583
        if (count > TRY_ERROR_LIMIT) break;
2,138,206✔
1584
        count++;
2,138,206✔
1585
      }
1586
    }
1587
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
28,992,706✔
1588
    if (valid < 0) {
28,987,197✔
1589
      code = valid;
×
1590
      break;
×
1591
    }
1592
  } while (1);
1593

1594
END:
3,004,108✔
1595
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,002,634✔
1596
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,003,241✔
1597
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,001,720✔
1598
  tDecoderClear(&dc);
3,001,720✔
1599
  tdbFree(pData);
3,002,625✔
1600

1601
  taosMemoryFree(buf);
3,002,102✔
1602
  taosMemoryFree(pKey);
3,002,634✔
1603

1604
  taosMemoryFree(pCursor);
3,001,903✔
1605

1606
  return code;
3,002,094✔
1607
}
1608

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

1615
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
47,508,364✔
1616
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
47,510,662✔
1617
  if (lock) {
47,504,475✔
1618
    metaULock(pMeta);
×
1619
  }
1620

1621
  return ret;
47,504,475✔
1622
}
1623

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

1628
  int32_t isLock = false;
6,931,973✔
1629
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
6,931,973✔
1630
  for (int i = 0; i < sz; i++) {
54,437,724✔
1631
    STUidTagInfo *p = taosArrayGet(uidList, i);
47,507,026✔
1632

1633
    if (i % LIMIT == 0) {
47,509,579✔
1634
      if (isLock) metaULock(pMeta);
6,357,291✔
1635

1636
      metaRLock(pMeta);
6,357,291✔
1637
      isLock = true;
6,357,898✔
1638
    }
1639

1640
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1641
    void   *val = NULL;
47,510,186✔
1642
    int32_t len = 0;
47,509,234✔
1643
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
47,509,459✔
1644
      p->pTagVal = taosMemoryMalloc(len);
47,498,516✔
1645
      if (!p->pTagVal) {
47,495,845✔
1646
        if (isLock) metaULock(pMeta);
×
1647

1648
        TAOS_RETURN(terrno);
×
1649
      }
1650
      memcpy(p->pTagVal, val, len);
47,498,308✔
1651
      tdbFree(val);
47,497,873✔
1652
    } else {
1653
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
5,247✔
1654
    }
1655
  }
1656
  //  }
1657
  if (isLock) metaULock(pMeta);
6,930,698✔
1658
  return 0;
6,931,849✔
1659
}
1660

1661
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
8,317,822✔
1662
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
8,317,822✔
1663
  if (!pCur) {
8,317,779✔
1664
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1665
  }
1666

1667
  while (1) {
133,896,154✔
1668
    tb_uid_t uid = metaCtbCursorNext(pCur);
142,213,933✔
1669
    if (uid == 0) {
142,129,608✔
1670
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
8,319,157✔
1671
      break;
8,319,157✔
1672
    }
1673

1674
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
133,810,451✔
1675
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
133,809,411✔
1676
    if (!info.pTagVal) {
133,872,633✔
1677
      metaCloseCtbCursor(pCur);
×
1678
      return terrno;
×
1679
    }
1680
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
133,872,633✔
1681
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
133,903,695✔
1682
      taosMemoryFreeClear(info.pTagVal);
×
1683
      metaCloseCtbCursor(pCur);
×
1684
      return terrno;
×
1685
    }
1686
  }
1687
  metaCloseCtbCursor(pCur);
8,319,157✔
1688
  return TSDB_CODE_SUCCESS;
8,316,898✔
1689
}
1690

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

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

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

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

1717
  metaCloseStbCursor(pCur);
×
1718

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

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

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

1732
      return code;
×
1733
    }
1734

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

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

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

1745
        return code;
×
1746
      }
1747

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

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

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

1761
          return code;
×
1762
        }
1763
      }
1764

1765
      tDestroyTSchema(pTSchema);
×
1766
    }
1767

1768
    taosArrayDestroy(uids);
×
1769
  }
1770

1771
  taosArrayDestroy(suids);
×
1772

1773
  return TSDB_CODE_SUCCESS;
×
1774
}
1775

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

1778
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
2,091,727,788✔
1779
  int32_t code = 0;
2,091,727,788✔
1780
  void   *pData = NULL;
2,091,727,788✔
1781
  int     nData = 0;
2,091,901,281✔
1782
  int     lock = 0;
2,091,966,321✔
1783

1784
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
2,091,966,321✔
1785
    lock = 1;
642,418,490✔
1786
  }
1787

1788
  if (!lock) metaRLock(pMeta);
2,091,971,417✔
1789

1790
  // search cache
1791
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
2,091,986,542✔
1792
    if (!lock) metaULock(pMeta);
1,993,051,245✔
1793
    goto _exit;
1,992,924,208✔
1794
  }
1795

1796
  // search TDB
1797
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
99,007,032✔
1798
    // not found
1799
    if (!lock) metaULock(pMeta);
89,994,074✔
1800
    goto _exit;
89,986,727✔
1801
  }
1802

1803
  if (!lock) metaULock(pMeta);
9,064,247✔
1804

1805
  pInfo->uid = uid;
9,064,247✔
1806
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
9,063,631✔
1807
  pInfo->version = ((SUidIdxVal *)pData)->version;
9,063,244✔
1808
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
9,063,631✔
1809

1810
  if (lock) {
9,063,631✔
1811
    metaULock(pReader->pMeta);
1,148,196✔
1812
    // metaReaderReleaseLock(pReader);
1813
  }
1814
  // upsert the cache
1815
  metaWLock(pMeta);
9,064,018✔
1816
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
9,063,631✔
1817
  if (ret != 0) {
9,063,064✔
1818
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1819
  }
1820
  metaULock(pMeta);
9,063,064✔
1821

1822
  if (lock) {
9,063,631✔
1823
    metaRLock(pReader->pMeta);
1,148,583✔
1824
  }
1825

1826
_exit:
2,091,961,238✔
1827
  tdbFree(pData);
2,091,923,344✔
1828
  return code;
2,091,908,313✔
1829
}
1830

1831
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
120,355,419✔
1832
  int32_t code = 0;
120,355,419✔
1833

1834
  if (!numOfTables && !numOfCols) goto _exit;
120,355,419✔
1835

1836
  SVnode *pVnodeObj = pVnode;
120,355,419✔
1837
  metaRLock(pVnodeObj->pMeta);
120,355,419✔
1838

1839
  // fast path: search cache
1840
  SMetaStbStats state = {0};
120,367,518✔
1841
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
120,380,503✔
1842
    metaULock(pVnodeObj->pMeta);
116,020,990✔
1843
    if (numOfTables) *numOfTables = state.ctbNum;
116,029,622✔
1844
    if (numOfCols) *numOfCols = state.colNum;
116,036,706✔
1845
    if (flags) *flags = state.flags;
116,023,145✔
1846
    goto _exit;
116,022,529✔
1847
  }
1848

1849
  // slow path: search TDB
1850
  int64_t ctbNum = 0;
4,372,493✔
1851
  int32_t colNum = 0;
4,373,572✔
1852
  int64_t keep = 0;
4,374,099✔
1853
  int8_t  flag = 0;
4,373,185✔
1854

1855
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
4,372,705✔
1856
  if (TSDB_CODE_SUCCESS == code) {
4,372,915✔
1857
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
4,372,915✔
1858
  }
1859
  if (TSDB_CODE_SUCCESS == code) {
4,374,170✔
1860
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
4,374,170✔
1861
  }
1862
  metaULock(pVnodeObj->pMeta);
4,374,467✔
1863
  if (TSDB_CODE_SUCCESS != code) {
4,374,452✔
1864
    goto _exit;
×
1865
  }
1866

1867
  if (numOfTables) *numOfTables = ctbNum;
4,374,452✔
1868
  if (numOfCols) *numOfCols = colNum;
4,374,452✔
1869
  if (flags) *flags = flag;
4,374,380✔
1870

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

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

1885
  metaULock(pVnodeObj->pMeta);
4,371,825✔
1886

1887
_exit:
120,396,774✔
1888
  return code;
120,403,029✔
1889
}
1890

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

1894
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
68,132,272✔
1895
    stats.ctbNum += deltaCtb;
57,750,544✔
1896
    stats.colNum += deltaCol;
57,750,544✔
1897
    if (deltaKeep > 0) {
57,750,544✔
1898
      stats.keep = deltaKeep;
9,327✔
1899
    }
1900

1901
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
57,750,544✔
1902
    if (code) {
57,703,684✔
1903
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1904
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1905
    }
1906
  }
1907
}
68,107,688✔
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