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

taosdata / TDengine / #3657

14 Mar 2025 08:10AM UTC coverage: 62.877% (+0.04%) from 62.841%
#3657

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

147792 of 302527 branches covered (48.85%)

Branch coverage included in aggregate %.

88 of 99 new or added lines in 12 files covered. (88.89%)

3160 existing lines in 17 files now uncovered.

232856 of 302857 relevant lines covered (76.89%)

6602224.33 hits per line

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

56.46
/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 "osMemory.h"
18
#include "tencode.h"
19

20
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
2,771,923✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,771,923✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,771,923✔
23
  pReader->pAPI = pAPI;
2,772,949✔
24
}
2,772,949✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
3,055,393✔
27
  memset(pReader, 0, sizeof(*pReader));
3,055,393✔
28
  pReader->pMeta = pMeta;
3,055,393✔
29
  pReader->flags = flags;
3,055,393✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
3,055,393!
31
    metaRLock(pMeta);
2,426,895✔
32
  }
33
}
3,055,792✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
944,033✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
944,033!
37
    metaULock(pReader->pMeta);
944,342✔
38
    pReader->flags |= META_READER_NOLOCK;
944,812✔
39
  }
40
}
944,503✔
41

42
void metaReaderClear(SMetaReader *pReader) {
3,066,152✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
3,066,152✔
44
    metaULock(pReader->pMeta);
1,481,963✔
45
  }
46
  tDecoderClear(&pReader->coder);
3,066,012✔
47
  tdbFree(pReader->pBuf);
3,068,484✔
48
  pReader->pBuf = NULL;
3,068,769✔
49
}
3,068,769✔
50

51
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
4,144,492✔
52
  int32_t  code = 0;
4,144,492✔
53
  SMeta   *pMeta = pReader->pMeta;
4,144,492✔
54
  STbDbKey tbDbKey = {.version = version, .uid = uid};
4,144,492✔
55

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
4,144,492!
58
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
59
  }
60

61
  // decode the entry
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
4,145,448✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
4,143,310✔
65
  if (code) {
4,136,272!
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
4,136,272✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
458,076✔
79
    metaULock(pVnodeObj->pMeta);
1,789✔
80
    return false;
1,789✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
456,294✔
84
  return true;
456,305✔
85
}
86

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

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

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
751,778✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
751,778✔
98
}
99

100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
2,487,193✔
101
  SMeta *pMeta = pReader->pMeta;
2,487,193✔
102

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
2,487,193✔
105
  if (TSDB_CODE_SUCCESS != code) {
2,487,865✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
147!
107
  }
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
2,487,718✔
110
}
111

112
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
76,422✔
113
  SMeta   *pMeta = pReader->pMeta;
76,422✔
114
  tb_uid_t uid;
115

116
  // query name.idx
117
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
76,422✔
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
43,003✔
119
  }
120

121
  uid = *(tb_uid_t *)pReader->pBuf;
33,424✔
122
  return metaReaderGetTableEntryByUid(pReader, uid);
33,424✔
123
}
124

125
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
104,740✔
126
  void    *pData = NULL;
104,740✔
127
  int      nData = 0;
104,740✔
128
  tb_uid_t uid = 0;
104,740✔
129

130
  metaRLock(pMeta);
104,740✔
131

132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
104,787✔
133
    uid = *(tb_uid_t *)pData;
5,400✔
134
    tdbFree(pData);
5,400✔
135
  }
136

137
  metaULock(pMeta);
104,742✔
138

139
  return uid;
104,742✔
140
}
141

142
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
17,678✔
143
  int         code = 0;
17,678✔
144
  SMetaReader mr = {0};
17,678✔
145
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
17,678✔
146
  code = metaReaderGetTableEntryByUid(&mr, uid);
17,684✔
147
  if (code < 0) {
17,599!
148
    metaReaderClear(&mr);
×
149
    return code;
×
150
  }
151

152
  STR_TO_VARSTR(tbName, mr.me.name);
17,599✔
153
  metaReaderClear(&mr);
17,599✔
154

155
  return 0;
17,666✔
156
}
157

158
int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
×
159
  int         code = 0;
×
160
  SMetaReader mr = {0};
×
161
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
162
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
163
  if (code < 0) {
×
164
    metaReaderClear(&mr);
×
165
    return code;
×
166
  }
167
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
168
  metaReaderClear(&mr);
×
169

170
  return 0;
×
171
}
172

173
int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) {
4,579✔
174
  int         code = 0;
4,579✔
175
  SMetaReader mr = {0};
4,579✔
176
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
4,579✔
177

178
  SMetaReader *pReader = &mr;
4,583✔
179

180
  // query name.idx
181
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
4,583✔
182
    metaReaderClear(&mr);
1,712✔
183
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
1,712✔
184
  }
185

186
  *uid = *(tb_uid_t *)pReader->pBuf;
2,871✔
187

188
  metaReaderClear(&mr);
2,871✔
189

190
  return 0;
2,871✔
191
}
192

193
int metaGetTableTypeSuidByName(void *pVnode, char *tbName, ETableType *tbType, uint64_t* suid) {
2,871✔
194
  int         code = 0;
2,871✔
195
  SMetaReader mr = {0};
2,871✔
196
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
2,871✔
197

198
  code = metaGetTableEntryByName(&mr, tbName);
2,872✔
199
  if (code == 0) *tbType = mr.me.type;
2,870!
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
2,870✔
201
    *suid = mr.me.ctbEntry.suid;
2,849✔
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
21✔
203
    *suid = mr.me.uid;
11✔
204
  } else {
205
    *suid = 0;
10✔
206
  }
207

208
  metaReaderClear(&mr);
2,870✔
209
  return code;
2,872✔
210
}
211

212
int metaReadNext(SMetaReader *pReader) {
×
213
  SMeta *pMeta = pReader->pMeta;
×
214

215
  // TODO
216

217
  return 0;
×
218
}
219

220
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
221
  int         code = -1;
×
222
  SMetaReader mr = {0};
×
223
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
224
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
225
  if (code < 0) {
×
226
    goto _exit;
×
227
  }
228
  if (mr.me.type == TSDB_CHILD_TABLE) {
×
229
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
230
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
×
231
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
232
  } else {
233
    goto _exit;
×
234
  }
235

236
  code = 0;
×
237

238
_exit:
×
239
  metaReaderClear(&mr);
×
240
  return code;
×
241
}
242

243
#if 1  // ===================================================
244
SMTbCursor *metaOpenTbCursor(void *pVnode) {
32,925✔
245
  SMTbCursor *pTbCur = NULL;
32,925✔
246
  int32_t     code;
247

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
32,925!
249
  if (pTbCur == NULL) {
32,986!
250
    return NULL;
×
251
  }
252

253
  SVnode *pVnodeObj = pVnode;
32,986✔
254
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
255
  pTbCur->pMeta = pVnodeObj->pMeta;
32,986✔
256
  pTbCur->paused = 1;
32,986✔
257
  code = metaResumeTbCursor(pTbCur, 1, 0);
32,986✔
258
  if (code) {
32,967✔
259
    terrno = code;
33✔
260
    taosMemoryFree(pTbCur);
×
261
    return NULL;
×
262
  }
263
  return pTbCur;
32,934✔
264
}
265

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
66,080✔
267
  if (pTbCur) {
66,080✔
268
    tdbFree(pTbCur->pKey);
33,047✔
269
    tdbFree(pTbCur->pVal);
33,053✔
270
    if (!pTbCur->paused) {
33,052✔
271
      metaReaderClear(&pTbCur->mr);
3,731✔
272
      if (pTbCur->pDbc) {
3,731!
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
3,731✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
33,052!
277
  }
278
}
66,084✔
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
30,129✔
281
  if (!pTbCur->paused) {
30,129!
282
    metaReaderClear(&pTbCur->mr);
30,134✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
30,138✔
284
    pTbCur->paused = 1;
30,134✔
285
  }
286
}
30,129✔
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
33,810✔
288
  int32_t code = 0;
33,810✔
289
  int32_t lino;
290
  int8_t  locked = 0;
33,810✔
291
  if (pTbCur->paused) {
33,810!
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
33,819✔
293
    locked = 1;
33,850✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
33,850✔
295
    if (code != 0) {
33,783!
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
33,783✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
32,967✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
32,978!
302
    } else {
303
      int c = 1;
816✔
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
816✔
305
      TSDB_CHECK_CODE(code, lino, _exit);
816!
306
      if (c == 0) {
816!
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
816!
308
      } else if (c < 0) {
×
309
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
310
        TSDB_CHECK_CODE(code, lino, _exit);
×
311
      } else {
312
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
313
        TSDB_CHECK_CODE(code, lino, _exit);
×
314
      }
315
    }
316

317
    pTbCur->paused = 0;
33,794✔
318
  }
319

320
_exit:
×
321
  if (code != 0 && locked) {
33,785!
322
    metaReaderReleaseLock(&pTbCur->mr);
×
323
  }
324
  return code;
33,763✔
325
}
326

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
939,133✔
328
  int    ret;
329
  void  *pBuf;
330
  STbCfg tbCfg;
331

332
  for (;;) {
333
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
939,133✔
334
    if (ret < 0) {
936,904✔
335
      return ret;
33,048✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
903,856✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
907,214✔
341
    if (ret) return ret;
902,051!
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
902,051✔
344
      continue;
49,887✔
345
    }
346

347
    break;
852,164✔
348
  }
349

350
  return 0;
852,164✔
351
}
352

353
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
354
  int    ret;
355
  void  *pBuf;
356
  STbCfg tbCfg;
357

358
  for (;;) {
359
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
360
    if (ret < 0) {
×
361
      return -1;
×
362
    }
363

364
    tDecoderClear(&pTbCur->mr.coder);
×
365

366
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
367
    if (ret < 0) {
×
368
      return ret;
×
369
    }
370

371
    if (pTbCur->mr.me.type == jumpTableType) {
×
372
      continue;
×
373
    }
374

375
    break;
×
376
  }
377

378
  return 0;
×
379
}
380

381
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
1,287,578✔
382
  void           *pData = NULL;
1,287,578✔
383
  int             nData = 0;
1,287,578✔
384
  int64_t         version;
385
  SSchemaWrapper  schema = {0};
1,287,578✔
386
  SSchemaWrapper *pSchema = NULL;
1,287,578✔
387
  SDecoder        dc = {0};
1,287,578✔
388
  if (lock) {
1,287,578✔
389
    metaRLock(pMeta);
1,269,473✔
390
  }
391
_query:
1,289,372✔
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,369,014✔
393
    goto _err;
757✔
394
  }
395

396
  version = ((SUidIdxVal *)pData)[0].version;
1,368,973✔
397

398
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
1,368,973!
399
    goto _err;
×
400
  }
401

402
  SMetaEntry me = {0};
1,369,034✔
403
  tDecoderInit(&dc, pData, nData);
1,369,034✔
404
  int32_t code = metaDecodeEntry(&dc, &me);
1,368,474✔
405
  if (code) {
1,368,552!
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
409
  if (me.type == TSDB_SUPER_TABLE) {
1,368,552✔
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
1,040,663✔
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
1,040,173✔
412
      tDecoderClear(&dc);
1,040,173✔
413
      goto _exit;
1,039,916✔
414
    }
415
  } else if (me.type == TSDB_CHILD_TABLE) {
327,889✔
416
    uid = me.ctbEntry.suid;
79,628✔
417
    tDecoderClear(&dc);
79,628✔
418
    goto _query;
79,642✔
419
  } else {
420
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
248,261✔
421
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
248,340✔
422
      tDecoderClear(&dc);
248,340✔
423
      goto _exit;
248,373✔
424
    }
425
  }
426
  tDecoderClear(&dc);
546✔
427

428
  // query from skm db
429
  if (tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData) < 0) {
145!
430
    goto _err;
×
431
  }
432

433
  tDecoderInit(&dc, pData, nData);
145✔
434
  if (tDecodeSSchemaWrapperEx(&dc, &schema) != 0) {
145!
435
    goto _err;
×
436
  }
437
  pSchema = tCloneSSchemaWrapper(&schema);
145✔
438
  tDecoderClear(&dc);
145✔
439

440
_exit:
1,288,434✔
441
  if (lock) {
1,288,434✔
442
    metaULock(pMeta);
1,268,434✔
443
  }
444
  tdbFree(pData);
1,289,463✔
445
  return pSchema;
1,288,420✔
446

447
_err:
757✔
448
  if (lock) {
757✔
449
    metaULock(pMeta);
755✔
450
  }
451
  tdbFree(pData);
759✔
452
  return NULL;
755✔
453
}
454

455
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
153✔
456
  void           *pData = NULL;
153✔
457
  int             nData = 0;
153✔
458
  int64_t         version = 0;
153✔
459
  SDecoder        dc = {0};
153✔
460
  int64_t         createTime = INT64_MAX;
153✔
461
  if (lock) {
153!
462
    metaRLock(pMeta);
153✔
463
  }
464

465
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
153!
466
    goto _exit;
×
467
  }
468

469
  version = ((SUidIdxVal *)pData)[0].version;
153✔
470

471
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
153!
472
    goto _exit;
×
473
  }
474

475
  SMetaEntry me = {0};
153✔
476
  tDecoderInit(&dc, pData, nData);
153✔
477
  int32_t code = metaDecodeEntry(&dc, &me);
153✔
478
  if (code) {
153!
479
    tDecoderClear(&dc);
×
480
    goto _exit;
×
481
  }
482
  if (me.type == TSDB_CHILD_TABLE) {
153!
483
    createTime = me.ctbEntry.btime;
153✔
484
  }
485
  tDecoderClear(&dc);
153✔
486

487
  _exit:
153✔
488
  if (lock) {
153!
489
    metaULock(pMeta);
153✔
490
  }
491
  tdbFree(pData);
153✔
492
  return createTime;
153✔
493
}
494

495
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
780,187✔
496
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
780,187✔
497
  SMCtbCursor *pCtbCur = NULL;
780,187✔
498
  SCtbIdxKey   ctbIdxKey;
499
  int          ret = 0;
780,187✔
500
  int          c = 0;
780,187✔
501

502
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
780,187!
503
  if (pCtbCur == NULL) {
782,339!
504
    return NULL;
×
505
  }
506

507
  pCtbCur->pMeta = pMeta;
782,339✔
508
  pCtbCur->suid = uid;
782,339✔
509
  pCtbCur->lock = lock;
782,339✔
510
  pCtbCur->paused = 1;
782,339✔
511

512
  ret = metaResumeCtbCursor(pCtbCur, 1);
782,339✔
513
  if (ret < 0) {
781,248!
514
    return NULL;
×
515
  }
516
  return pCtbCur;
781,248✔
517
}
518

519
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
782,006✔
520
  if (pCtbCur) {
782,006!
521
    if (!pCtbCur->paused) {
782,101✔
522
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
738,882!
523
      if (pCtbCur->pCur) {
739,126!
524
        tdbTbcClose(pCtbCur->pCur);
739,176✔
525
      }
526
    }
527
    tdbFree(pCtbCur->pKey);
782,198✔
528
    tdbFree(pCtbCur->pVal);
782,300✔
529
  }
530
  taosMemoryFree(pCtbCur);
782,374!
531
}
782,548✔
532

533
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,303✔
534
  if (!pCtbCur->paused) {
43,303!
535
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,314✔
536
    if (pCtbCur->lock) {
43,341!
537
      metaULock(pCtbCur->pMeta);
43,341✔
538
    }
539
    pCtbCur->paused = 1;
43,341✔
540
  }
541
}
43,330✔
542

543
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
780,905✔
544
  if (pCtbCur->paused) {
780,905!
545
    pCtbCur->paused = 0;
781,450✔
546

547
    if (pCtbCur->lock) {
781,450✔
548
      metaRLock(pCtbCur->pMeta);
762,858✔
549
    }
550
    int ret = 0;
781,465✔
551
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
781,465✔
552
    if (ret < 0) {
781,874!
553
      metaCloseCtbCursor(pCtbCur);
×
554
      return -1;
×
555
    }
556

557
    if (first) {
781,934!
558
      SCtbIdxKey ctbIdxKey;
559
      // move to the suid
560
      ctbIdxKey.suid = pCtbCur->suid;
781,934✔
561
      ctbIdxKey.uid = INT64_MIN;
781,934✔
562
      int c = 0;
781,934✔
563
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
781,934✔
564
      if (c > 0) {
781,521✔
565
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
67,277✔
566
      }
567
    } else {
568
      int c = 0;
×
569
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
570
      if (c < 0) {
×
571
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
572
      } else {
573
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
574
      }
575
    }
576
  }
577
  return 0;
780,926✔
578
}
579

580
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
3,863,500✔
581
  int         ret;
582
  SCtbIdxKey *pCtbIdxKey;
583

584
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
3,863,500✔
585
  if (ret < 0) {
3,861,272✔
586
    return 0;
332,822✔
587
  }
588

589
  pCtbIdxKey = pCtbCur->pKey;
3,528,450✔
590
  if (pCtbIdxKey->suid > pCtbCur->suid) {
3,528,450✔
591
    return 0;
450,201✔
592
  }
593

594
  return pCtbIdxKey->uid;
3,078,249✔
595
}
596

597
struct SMStbCursor {
598
  SMeta   *pMeta;
599
  TBC     *pCur;
600
  tb_uid_t suid;
601
  void    *pKey;
602
  void    *pVal;
603
  int      kLen;
604
  int      vLen;
605
};
606

607
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
97,908✔
608
  SMStbCursor *pStbCur = NULL;
97,908✔
609
  int          ret = 0;
97,908✔
610
  int          c = 0;
97,908✔
611

612
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
97,908!
613
  if (pStbCur == NULL) {
97,911!
614
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
615
    return NULL;
×
616
  }
617

618
  pStbCur->pMeta = pMeta;
97,911✔
619
  pStbCur->suid = suid;
97,911✔
620
  metaRLock(pMeta);
97,911✔
621

622
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
97,911✔
623
  if (ret < 0) {
97,910!
624
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
625
    metaULock(pMeta);
×
626
    taosMemoryFree(pStbCur);
×
627
    return NULL;
×
628
  }
629

630
  // move to the suid
631
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
97,910✔
632
  if (c > 0) {
97,908!
633
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
634
  }
635

636
  return pStbCur;
97,908✔
637
}
638

639
void metaCloseStbCursor(SMStbCursor *pStbCur) {
97,907✔
640
  if (pStbCur) {
97,907!
641
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
97,907!
642
    if (pStbCur->pCur) {
97,911!
643
      tdbTbcClose(pStbCur->pCur);
97,911✔
644

645
      tdbFree(pStbCur->pKey);
97,910✔
646
      tdbFree(pStbCur->pVal);
97,909✔
647
    }
648

649
    taosMemoryFree(pStbCur);
97,908!
650
  }
651
}
97,909✔
652

653
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
171,269✔
654
  int ret;
655

656
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
171,269✔
657
  if (ret < 0) {
171,268✔
658
    return 0;
97,907✔
659
  }
660
  return *(tb_uid_t *)pStbCur->pKey;
73,361✔
661
}
662

663
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
1,145,535✔
664
  STSchema       *pTSchema = NULL;
1,145,535✔
665
  SSchemaWrapper *pSW = NULL;
1,145,535✔
666

667
  pSW = metaGetTableSchema(pMeta, uid, sver, lock);
1,145,535✔
668
  if (!pSW) return NULL;
1,147,193✔
669

670
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
1,147,112✔
671

672
  taosMemoryFree(pSW->pSchema);
1,147,894!
673
  taosMemoryFree(pSW);
1,148,106✔
674
  return pTSchema;
1,147,940✔
675
}
676

677
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
2,232✔
678
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
2,232✔
679
  if (*ppTSchema == NULL) {
2,234!
680
    return terrno;
×
681
  }
682
  return TSDB_CODE_SUCCESS;
2,234✔
683
}
684

685
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,143,339✔
686
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,143,339✔
687
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
1,145,641!
688
    return terrno;
1,457✔
689
  }
690
  return TSDB_CODE_SUCCESS;
1,144,184✔
691
}
692

693
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
1,288,432✔
694
  int32_t code = 0;
1,288,432✔
695
  int32_t lino;
696

697
  void     *pData = NULL;
1,288,432✔
698
  int       nData = 0;
1,288,432✔
699
  SSkmDbKey skmDbKey;
700
  if (sver <= 0) {
1,288,432✔
701
    SMetaInfo info;
702
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
610,306✔
703
      sver = info.skmVer;
610,240✔
704
    } else {
705
      TBC *pSkmDbC = NULL;
71✔
706
      int  c;
707

708
      skmDbKey.uid = suid ? suid : uid;
71!
709
      skmDbKey.sver = INT32_MAX;
71✔
710

711
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
71✔
712
      TSDB_CHECK_CODE(code, lino, _exit);
81!
713
      metaRLock(pMeta);
81✔
714

715
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
81!
716
        metaULock(pMeta);
×
717
        tdbTbcClose(pSkmDbC);
×
718
        code = TSDB_CODE_NOT_FOUND;
×
719
        goto _exit;
×
720
      }
721

722
      if (c == 0) {
81!
723
        metaULock(pMeta);
×
724
        tdbTbcClose(pSkmDbC);
×
725
        code = TSDB_CODE_FAILED;
×
726
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
727
        goto _exit;
×
728
      }
729

730
      if (c < 0) {
81!
731
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
732
      }
733

734
      const void *pKey = NULL;
81✔
735
      int32_t     nKey = 0;
81✔
736
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
81✔
737

738
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
81!
739
        metaULock(pMeta);
×
740
        tdbTbcClose(pSkmDbC);
×
741
        code = TSDB_CODE_NOT_FOUND;
×
742
        goto _exit;
×
743
      }
744

745
      sver = ((SSkmDbKey *)pKey)->sver;
81✔
746

747
      metaULock(pMeta);
81✔
748
      tdbTbcClose(pSkmDbC);
81✔
749
    }
750
  }
751

752
  if (!(sver > 0)) {
1,288,438!
753
    code = TSDB_CODE_NOT_FOUND;
×
754
    goto _exit;
×
755
  }
756

757
  skmDbKey.uid = suid ? suid : uid;
1,288,438✔
758
  skmDbKey.sver = sver;
1,288,438✔
759
  metaRLock(pMeta);
1,288,438✔
760
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
1,288,623!
761
    metaULock(pMeta);
×
762
    code = TSDB_CODE_NOT_FOUND;
×
763
    goto _exit;
×
764
  }
765
  metaULock(pMeta);
1,288,525✔
766

767
  // decode
768
  SDecoder        dc = {0};
1,288,550✔
769
  SSchemaWrapper  schema;
770
  SSchemaWrapper *pSchemaWrapper = &schema;
1,288,550✔
771

772
  tDecoderInit(&dc, pData, nData);
1,288,550✔
773
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
1,287,053✔
774
  tDecoderClear(&dc);
1,287,053✔
775
  tdbFree(pData);
1,288,376✔
776
  if (TSDB_CODE_SUCCESS != code) {
1,288,442!
777
    taosMemoryFree(pSchemaWrapper->pSchema);
×
778
    goto _exit;
×
779
  }
780

781
  // convert
782
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
1,288,442✔
783
  if (pTSchema == NULL) {
1,288,500!
784
    code = TSDB_CODE_OUT_OF_MEMORY;
×
785
  }
786

787
  *ppTSchema = pTSchema;
1,288,500✔
788
  taosMemoryFree(pSchemaWrapper->pSchema);
1,288,500!
789

790
_exit:
1,288,582✔
791
  return code;
1,288,582✔
792
}
793

794
// N.B. Called by statusReq per second
795
int64_t metaGetTbNum(SMeta *pMeta) {
204,531✔
796
  // num of child tables (excluding normal tables , stables and others)
797

798
  /* int64_t num = 0; */
799
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
800

801
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
204,531✔
802
}
803

804
void metaUpdTimeSeriesNum(SMeta *pMeta) {
97,893✔
805
  int64_t nCtbTimeSeries = 0;
97,893✔
806
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
97,893!
807
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
97,892✔
808
  }
809
}
97,894✔
810

811
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
812
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
813
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
331,843✔
814
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
331,858✔
815
    metaUpdTimeSeriesNum(pMeta);
95,498✔
816
  }
817

818
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
331,871✔
819
}
820

821
// type: 1 reported timeseries
822
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
331,843!
823
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
331,871✔
824
  if (type == 1) {
331,871✔
825
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
204,531✔
826
  }
827
  return nTimeSeries;
331,875✔
828
}
829

830
typedef struct {
831
  SMeta   *pMeta;
832
  TBC     *pCur;
833
  tb_uid_t uid;
834
  void    *pKey;
835
  void    *pVal;
836
  int      kLen;
837
  int      vLen;
838
} SMSmaCursor;
839

840
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
841
  SMSmaCursor *pSmaCur = NULL;
×
842
  SSmaIdxKey   smaIdxKey;
843
  int          ret;
844
  int          c;
845

846
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
847
  if (pSmaCur == NULL) {
×
848
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
849
    return NULL;
×
850
  }
851

852
  pSmaCur->pMeta = pMeta;
×
853
  pSmaCur->uid = uid;
×
854
  metaRLock(pMeta);
×
855

856
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
857
  if (ret < 0) {
×
858
    metaULock(pMeta);
×
859
    taosMemoryFree(pSmaCur);
×
860
    return NULL;
×
861
  }
862

863
  // move to the suid
864
  smaIdxKey.uid = uid;
×
865
  smaIdxKey.smaUid = INT64_MIN;
×
866
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
867
  if (c > 0) {
×
868
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
869
  }
870

871
  return pSmaCur;
×
872
}
873

874
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
875
  if (pSmaCur) {
×
876
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
877
    if (pSmaCur->pCur) {
×
878
      tdbTbcClose(pSmaCur->pCur);
×
879
      pSmaCur->pCur = NULL;
×
880

881
      tdbFree(pSmaCur->pKey);
×
882
      tdbFree(pSmaCur->pVal);
×
883
    }
884

885
    taosMemoryFree(pSmaCur);
×
886
  }
887
}
×
888

889
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
890
  int         ret;
891
  SSmaIdxKey *pSmaIdxKey;
892

893
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
894
  if (ret < 0) {
×
895
    return 0;
×
896
  }
897

898
  pSmaIdxKey = pSmaCur->pKey;
×
899
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
900
    return 0;
×
901
  }
902

903
  return pSmaIdxKey->uid;
×
904
}
905

906
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
907
  STSmaWrapper *pSW = NULL;
×
908
  SArray       *pSmaIds = NULL;
×
909

910
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
911
    return NULL;
×
912
  }
913

914
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
915
  if (!pSW) {
×
916
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
917
    goto _err;
×
918
  }
919

920
  pSW->number = taosArrayGetSize(pSmaIds);
×
921
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
922

923
  if (!pSW->tSma) {
×
924
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
925
    goto _err;
×
926
  }
927

928
  SMetaReader mr = {0};
×
929
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
930
  int64_t smaId;
931
  int     smaIdx = 0;
×
932
  STSma  *pTSma = NULL;
×
933
  for (int i = 0; i < pSW->number; ++i) {
×
934
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
935
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
936
      tDecoderClear(&mr.coder);
×
937
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
938
      continue;
×
939
    }
940
    tDecoderClear(&mr.coder);
×
941
    pTSma = pSW->tSma + smaIdx;
×
942
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
943
    if (deepCopy) {
×
944
      if (pTSma->exprLen > 0) {
×
945
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
946
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
947
          goto _err;
×
948
        }
949
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
950
      }
951
      if (pTSma->tagsFilterLen > 0) {
×
952
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
953
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
954
          goto _err;
×
955
        }
956
      }
957
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
958
    } else {
959
      pTSma->exprLen = 0;
×
960
      pTSma->expr = NULL;
×
961
      pTSma->tagsFilterLen = 0;
×
962
      pTSma->tagsFilter = NULL;
×
963
    }
964

965
    ++smaIdx;
×
966
  }
967

968
  if (smaIdx <= 0) goto _err;
×
969
  pSW->number = smaIdx;
×
970

971
  metaReaderClear(&mr);
×
972
  taosArrayDestroy(pSmaIds);
×
973
  return pSW;
×
974
_err:
×
975
  metaReaderClear(&mr);
×
976
  taosArrayDestroy(pSmaIds);
×
977
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
978
  return NULL;
×
979
}
980

981
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
2✔
982
  STSma      *pTSma = NULL;
2✔
983
  SMetaReader mr = {0};
2✔
984
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
2✔
985
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
2!
986
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
987
    metaReaderClear(&mr);
×
988
    return NULL;
×
989
  }
990
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
2!
991
  if (!pTSma) {
2!
992
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
993
    metaReaderClear(&mr);
×
994
    return NULL;
×
995
  }
996

997
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
2✔
998

999
  metaReaderClear(&mr);
2✔
1000
  return pTSma;
2✔
1001
}
1002

1003
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1004
  SArray     *pUids = NULL;
×
1005
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1006

1007
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1008
  if (!pCur) {
×
1009
    return NULL;
×
1010
  }
1011

1012
  while (1) {
×
1013
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1014
    if (id == 0) {
×
1015
      break;
×
1016
    }
1017

1018
    if (!pUids) {
×
1019
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1020
      if (!pUids) {
×
1021
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1022
        metaCloseSmaCursor(pCur);
×
1023
        return NULL;
×
1024
      }
1025
    }
1026

1027
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1028

1029
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1030
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1031
      metaCloseSmaCursor(pCur);
×
1032
      taosArrayDestroy(pUids);
×
1033
      return NULL;
×
1034
    }
1035
  }
1036

1037
  metaCloseSmaCursor(pCur);
×
1038
  return pUids;
×
1039
}
1040

1041
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1042
  SArray     *pUids = NULL;
×
1043
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1044
  tb_uid_t    lastUid = 0;
×
1045

1046
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1047
  if (!pCur) {
×
1048
    return NULL;
×
1049
  }
1050

1051
  while (1) {
×
1052
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1053
    if (uid == 0) {
×
1054
      break;
×
1055
    }
1056

1057
    if (lastUid == uid) {
×
1058
      continue;
×
1059
    }
1060

1061
    lastUid = uid;
×
1062

1063
    if (!pUids) {
×
1064
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1065
      if (!pUids) {
×
1066
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1067
        metaCloseSmaCursor(pCur);
×
1068
        return NULL;
×
1069
      }
1070
    }
1071

1072
    if (!taosArrayPush(pUids, &uid)) {
×
1073
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1074
      metaCloseSmaCursor(pCur);
×
1075
      taosArrayDestroy(pUids);
×
1076
      return NULL;
×
1077
    }
1078
  }
1079

1080
  metaCloseSmaCursor(pCur);
×
1081
  return pUids;
×
1082
}
1083

1084
#endif
1085

1086
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
4,030,210✔
1087
  STag *tag = (STag *)pTag;
4,030,210✔
1088
  if (type == TSDB_DATA_TYPE_JSON) {
4,030,210✔
1089
    return tag;
10,033✔
1090
  }
1091
  bool find = tTagGet(tag, val);
4,020,177✔
1092

1093
  if (!find) {
4,025,043✔
1094
    return NULL;
127,484✔
1095
  }
1096

1097
  return val;
3,897,559✔
1098
}
1099

1100
typedef struct {
1101
  SMeta   *pMeta;
1102
  TBC     *pCur;
1103
  tb_uid_t suid;
1104
  int16_t  cid;
1105
  int16_t  type;
1106
  void    *pKey;
1107
  void    *pVal;
1108
  int32_t  kLen;
1109
  int32_t  vLen;
1110
} SIdxCursor;
1111

1112
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1113
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1114
  SMetaFltParam *param = arg;
6✔
1115
  int32_t        ret = 0;
6✔
1116

1117
  SIdxCursor *pCursor = NULL;
6✔
1118
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
6!
1119
  if (pCursor == NULL) {
6!
1120
    return terrno;
×
1121
  }
1122
  pCursor->pMeta = pMeta;
6✔
1123
  pCursor->suid = param->suid;
6✔
1124
  pCursor->cid = param->cid;
6✔
1125
  pCursor->type = param->type;
6✔
1126

1127
  metaRLock(pMeta);
6✔
1128
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
6✔
1129
  if (ret != 0) {
6!
1130
    goto END;
×
1131
  }
1132
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
6✔
1133

1134
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
6✔
1135
  SBtimeIdxKey *pBtimeKey = &btimeKey;
6✔
1136

1137
  int cmp = 0;
6✔
1138
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
6!
1139
    goto END;
×
1140
  }
1141

1142
  int32_t valid = 0;
6✔
1143
  int32_t count = 0;
6✔
1144

1145
  static const int8_t TRY_ERROR_LIMIT = 1;
1146
  do {
37,581✔
1147
    void   *entryKey = NULL;
37,587✔
1148
    int32_t nEntryKey = -1;
37,587✔
1149
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
37,587✔
1150
    if (valid < 0) break;
38,438✔
1151

1152
    SBtimeIdxKey *p = entryKey;
38,432✔
1153
    if (count > TRY_ERROR_LIMIT) break;
38,432!
1154

1155
    terrno = TSDB_CODE_SUCCESS;
38,432✔
1156
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
38,444✔
1157
    if (terrno != TSDB_CODE_SUCCESS) {
37,786!
1158
      ret = terrno;
×
1159
      break;
×
1160
    }
1161
    if (cmp == 0) {
37,682!
1162
      if (taosArrayPush(pUids, &p->uid) == NULL) {
75,356!
1163
        ret = terrno;
×
1164
        break;
×
1165
      }
1166
    } else {
1167
      if (param->equal == true) {
×
1168
        if (count > TRY_ERROR_LIMIT) break;
×
1169
        count++;
×
1170
      }
1171
    }
1172
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
37,674✔
1173
    if (valid < 0) break;
37,581!
1174
  } while (1);
1175

1176
END:
6✔
1177
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1178
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1179
  taosMemoryFree(pCursor);
6!
1180
  return ret;
6✔
1181
}
1182

1183
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1184
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1185
  SMetaFltParam *param = arg;
×
1186
  int32_t        ret = 0;
×
1187
  char          *buf = NULL;
×
1188

1189
  STagIdxKey *pKey = NULL;
×
1190
  int32_t     nKey = 0;
×
1191

1192
  SIdxCursor *pCursor = NULL;
×
1193
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1194
  if (pCursor == NULL) {
×
1195
    return terrno;
×
1196
  }
1197
  pCursor->pMeta = pMeta;
×
1198
  pCursor->suid = param->suid;
×
1199
  pCursor->cid = param->cid;
×
1200
  pCursor->type = param->type;
×
1201

1202
  char *pName = param->val;
×
1203

1204
  metaRLock(pMeta);
×
1205
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1206
  if (ret != 0) {
×
1207
    goto END;
×
1208
  }
1209

1210
  int cmp = 0;
×
1211
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1212
    goto END;
×
1213
  }
1214
  int32_t valid = 0;
×
1215
  int32_t count = 0;
×
1216

1217
  int32_t TRY_ERROR_LIMIT = 1;
×
1218
  do {
×
1219
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1220
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1221
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1222
    if (valid < 0) break;
×
1223

1224
    if (count > TRY_ERROR_LIMIT) break;
×
1225

1226
    char *pTableKey = (char *)pEntryKey;
×
1227
    terrno = TSDB_CODE_SUCCESS;
×
1228
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1229
    if (terrno != TSDB_CODE_SUCCESS) {
×
1230
      ret = terrno;
×
1231
      goto END;
×
1232
    }
1233
    if (cmp == 0) {
×
1234
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1235
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1236
        ret = terrno;
×
1237
        goto END;
×
1238
      }
1239
    } else {
1240
      if (param->equal == true) {
×
1241
        if (count > TRY_ERROR_LIMIT) break;
×
1242
        count++;
×
1243
      }
1244
    }
1245
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1246
    if (valid < 0) {
×
1247
      break;
×
1248
    }
1249
  } while (1);
1250

1251
END:
×
1252
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1253
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1254
  taosMemoryFree(buf);
×
1255
  taosMemoryFree(pKey);
×
1256

1257
  taosMemoryFree(pCursor);
×
1258

1259
  return ret;
×
1260
}
1261
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1262
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1263
  SMetaFltParam *param = arg;
×
1264
  int32_t        ret = 0;
×
1265
  char          *buf = NULL;
×
1266

1267
  STtlIdxKey *pKey = NULL;
×
1268
  int32_t     nKey = 0;
×
1269

1270
  SIdxCursor *pCursor = NULL;
×
1271
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1272
  if (pCursor == NULL) {
×
1273
    return terrno;
×
1274
  }
1275
  pCursor->pMeta = pMeta;
×
1276
  pCursor->suid = param->suid;
×
1277
  pCursor->cid = param->cid;
×
1278
  pCursor->type = param->type;
×
1279

1280
  metaRLock(pMeta);
×
1281
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1282

1283
END:
×
1284
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1285
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1286
  taosMemoryFree(buf);
×
1287
  taosMemoryFree(pKey);
×
1288

1289
  taosMemoryFree(pCursor);
×
1290

1291
  return ret;
×
1292
  // impl later
1293
  return 0;
1294
}
1295
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
826✔
1296
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
826✔
1297
  SMetaFltParam *param = arg;
826✔
1298

1299
  SMetaEntry oStbEntry = {0};
826✔
1300
  int32_t    code = 0;
826✔
1301
  char      *buf = NULL;
826✔
1302
  void      *pData = NULL;
826✔
1303
  int        nData = 0;
826✔
1304

1305
  SDecoder    dc = {0};
826✔
1306
  STbDbKey    tbDbKey = {0};
826✔
1307
  STagIdxKey *pKey = NULL;
826✔
1308
  int32_t     nKey = 0;
826✔
1309

1310
  SIdxCursor *pCursor = NULL;
826✔
1311
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
826!
1312
  if (!pCursor) {
833!
1313
    return terrno;
×
1314
  }
1315
  pCursor->pMeta = pMeta;
833✔
1316
  pCursor->suid = param->suid;
833✔
1317
  pCursor->cid = param->cid;
833✔
1318
  pCursor->type = param->type;
833✔
1319

1320
  metaRLock(pMeta);
833✔
1321

1322
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
832!
1323

1324
  tbDbKey.uid = param->suid;
828✔
1325
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
828✔
1326

1327
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
828!
1328

1329
  tDecoderInit(&dc, pData, nData);
833✔
1330

1331
  code = metaDecodeEntry(&dc, &oStbEntry);
833✔
1332
  if (code) {
832!
1333
    tDecoderClear(&dc);
×
1334
    goto END;
×
1335
  }
1336

1337
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
832!
1338
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1339
  }
1340

1341
  code = TSDB_CODE_INVALID_PARA;
832✔
1342
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
3,004✔
1343
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
2,172✔
1344
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
2,172!
1345
      code = 0;
833✔
1346
    } else {
1347
      TAOS_CHECK_GOTO(code, NULL, END);
1,339!
1348
    }
1349
  }
1350

1351
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
832✔
1352
  if (code != 0) {
832!
1353
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1354
  }
1355

1356
  int32_t maxSize = 0;
832✔
1357
  int32_t nTagData = 0;
832✔
1358
  void   *tagData = NULL;
832✔
1359

1360
  if (param->val == NULL) {
832!
1361
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1362
    goto END;
×
1363
  } else {
1364
    if (IS_VAR_DATA_TYPE(param->type)) {
832!
1365
      tagData = varDataVal(param->val);
186✔
1366
      nTagData = varDataLen(param->val);
186✔
1367

1368
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
186!
1369
        maxSize = 4 * nTagData + 1;
×
1370
        buf = taosMemoryCalloc(1, maxSize);
×
1371
        if (buf == NULL) {
×
1372
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1373
        }
1374

1375
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
×
1376
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1377
        }
1378

1379
        tagData = buf;
×
1380
        nTagData = maxSize;
×
1381
      }
1382
    } else {
1383
      tagData = param->val;
646✔
1384
      nTagData = tDataTypes[param->type].bytes;
646✔
1385
    }
1386
  }
1387

1388
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
832!
1389
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1390
                  NULL, END);
1391

1392
  int cmp = 0;
833✔
1393
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
833!
1394

1395
  int     count = 0;
832✔
1396
  int32_t valid = 0;
832✔
1397
  bool    found = false;
832✔
1398

1399
  static const int8_t TRY_ERROR_LIMIT = 1;
1400

1401
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1402
  /// target:                        [suid, cid2, type2]
1403
  int diffCidCount = 0;
832✔
1404
  do {
11,350✔
1405
    void   *entryKey = NULL, *entryVal = NULL;
12,182✔
1406
    int32_t nEntryKey, nEntryVal;
1407

1408
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
12,182✔
1409
    if (valid < 0) {
12,186✔
1410
      code = valid;
713✔
1411
    }
1412
    if (count > TRY_ERROR_LIMIT) {
12,186✔
1413
      break;
832✔
1414
    }
1415

1416
    STagIdxKey *p = entryKey;
12,097✔
1417
    if (p == NULL) break;
12,097✔
1418

1419
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
11,410!
1420
      if (found == true) break;  //
116✔
1421
      if (diffCidCount > TRY_ERROR_LIMIT) break;
60!
1422
      diffCidCount++;
60✔
1423
      count++;
60✔
1424
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
60✔
1425
      if (valid < 0) {
62!
1426
        code = valid;
×
1427
        break;
×
1428
      } else {
1429
        continue;
62✔
1430
      }
1431
    }
1432

1433
    terrno = TSDB_CODE_SUCCESS;
11,294✔
1434
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
11,293✔
1435
    if (terrno != TSDB_CODE_SUCCESS) {
11,288!
1436
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1437
      break;
×
1438
    }
1439
    if (cmp == 0) {
11,291✔
1440
      // match
1441
      tb_uid_t tuid = 0;
10,528✔
1442
      if (IS_VAR_DATA_TYPE(pKey->type)) {
10,528!
1443
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
326✔
1444
      } else {
1445
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
10,202✔
1446
      }
1447
      if (taosArrayPush(pUids, &tuid) == NULL) {
10,521!
1448
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1449
      }
1450
      found = true;
10,521✔
1451
    } else {
1452
      if (param->equal == true) {
763✔
1453
        if (count > TRY_ERROR_LIMIT) break;
275!
1454
        count++;
275✔
1455
      }
1456
    }
1457
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
11,284✔
1458
    if (valid < 0) {
11,288!
1459
      code = valid;
×
1460
      break;
×
1461
    }
1462
  } while (1);
1463

1464
END:
832✔
1465
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
832!
1466
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
833!
1467
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
833!
1468
  tDecoderClear(&dc);
833✔
1469
  tdbFree(pData);
833✔
1470

1471
  taosMemoryFree(buf);
833!
1472
  taosMemoryFree(pKey);
833!
1473

1474
  taosMemoryFree(pCursor);
833!
1475

1476
  return code;
833✔
1477
}
1478

1479
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
447✔
1480
  int ret = 0;
447✔
1481
  if (lock) {
447!
1482
    metaRLock(pMeta);
×
1483
  }
1484

1485
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
447✔
1486
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
447✔
1487
  if (lock) {
447!
1488
    metaULock(pMeta);
×
1489
  }
1490

1491
  return ret;
447✔
1492
}
1493

1494
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
98✔
1495
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
98✔
1496
  const int32_t LIMIT = 128;
98✔
1497

1498
  int32_t isLock = false;
98✔
1499
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
98!
1500
  for (int i = 0; i < sz; i++) {
545✔
1501
    STUidTagInfo *p = taosArrayGet(uidList, i);
447✔
1502

1503
    if (i % LIMIT == 0) {
447✔
1504
      if (isLock) metaULock(pMeta);
78!
1505

1506
      metaRLock(pMeta);
78✔
1507
      isLock = true;
78✔
1508
    }
1509

1510
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1511
    void   *val = NULL;
447✔
1512
    int32_t len = 0;
447✔
1513
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
447!
1514
      p->pTagVal = taosMemoryMalloc(len);
447!
1515
      if (!p->pTagVal) {
447!
1516
        if (isLock) metaULock(pMeta);
×
1517

1518
        TAOS_RETURN(terrno);
×
1519
      }
1520
      memcpy(p->pTagVal, val, len);
447✔
1521
      tdbFree(val);
447✔
1522
    } else {
1523
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64 "", TD_VID(pMeta->pVnode), suid,
×
1524
                p->uid);
1525
    }
1526
  }
1527
  //  }
1528
  if (isLock) metaULock(pMeta);
98✔
1529
  return 0;
98✔
1530
}
1531

1532
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
97,491✔
1533
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
97,491✔
1534
  if (!pCur) {
97,649!
1535
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1536
  }
1537

1538
  // If len > 0 means there already have uids, and we only want the
1539
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1540
  // in the hash map, that may require a lot of memory
1541
  SHashObj *pSepecifiedUidMap = NULL;
97,649✔
1542
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
97,649✔
1543
  if (numOfElems > 0) {
97,657✔
1544
    pSepecifiedUidMap =
1545
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
43,417✔
1546
    for (int i = 0; i < numOfElems; i++) {
235,250✔
1547
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
191,814✔
1548
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
191,813✔
1549
      if (code) {
191,852!
1550
        metaCloseCtbCursor(pCur);
×
1551
        taosHashCleanup(pSepecifiedUidMap);
×
1552
        return code;
×
1553
      }
1554
    }
1555
  }
1556

1557
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
97,676✔
1558
    while (1) {
338,045✔
1559
      tb_uid_t uid = metaCtbCursorNext(pCur);
392,197✔
1560
      if (uid == 0) {
387,536✔
1561
        break;
54,342✔
1562
      }
1563

1564
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
333,194✔
1565
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
333,194!
1566
      if (!info.pTagVal) {
338,997!
1567
        metaCloseCtbCursor(pCur);
×
1568
        taosHashCleanup(pSepecifiedUidMap);
×
1569
        return terrno;
×
1570
      }
1571
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
338,997✔
1572
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
338,045!
1573
        taosMemoryFreeClear(info.pTagVal);
×
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
    }
1579
  } else {  // only the specified tables need to be added
1580
    while (1) {
227,903✔
1581
      tb_uid_t uid = metaCtbCursorNext(pCur);
271,427✔
1582
      if (uid == 0) {
271,651✔
1583
        break;
43,423✔
1584
      }
1585

1586
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
228,228✔
1587
      if (index == NULL) {
228,310✔
1588
        continue;
36,697✔
1589
      }
1590

1591
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
191,613✔
1592
      if (pTagInfo->pTagVal == NULL) {
191,611!
1593
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
192,015✔
1594
        if (!pTagInfo->pTagVal) {
191,610!
1595
          metaCloseCtbCursor(pCur);
×
1596
          taosHashCleanup(pSepecifiedUidMap);
×
1597
          return terrno;
×
1598
        }
1599
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
191,610✔
1600
      }
1601
    }
1602
  }
1603

1604
  taosHashCleanup(pSepecifiedUidMap);
97,765✔
1605
  metaCloseCtbCursor(pCur);
97,759✔
1606
  return TSDB_CODE_SUCCESS;
97,797✔
1607
}
1608

1609
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1610

1611
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
25,519,895✔
1612
  int32_t code = 0;
25,519,895✔
1613
  void   *pData = NULL;
25,519,895✔
1614
  int     nData = 0;
25,519,895✔
1615
  int     lock = 0;
25,519,895✔
1616

1617
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
25,519,895!
1618
    lock = 1;
2,488,184✔
1619
  }
1620

1621
  if (!lock) metaRLock(pMeta);
25,519,895✔
1622

1623
  // search cache
1624
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
25,526,783✔
1625
    if (!lock) metaULock(pMeta);
11,769,031✔
1626
    goto _exit;
11,769,070✔
1627
  }
1628

1629
  // search TDB
1630
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
13,757,630✔
1631
    // not found
1632
    if (!lock) metaULock(pMeta);
13,746,187✔
1633
    goto _exit;
13,746,154✔
1634
  }
1635

1636
  if (!lock) metaULock(pMeta);
12,207✔
1637

1638
  pInfo->uid = uid;
12,207✔
1639
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
12,207✔
1640
  pInfo->version = ((SUidIdxVal *)pData)->version;
12,207✔
1641
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
12,207✔
1642

1643
  if (lock) {
12,207✔
1644
    metaULock(pReader->pMeta);
2,237✔
1645
    // metaReaderReleaseLock(pReader);
1646
  }
1647
  // upsert the cache
1648
  metaWLock(pMeta);
12,211✔
1649
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
12,217✔
1650
  if (ret != 0) {
12,215!
1651
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1652
  }
1653
  metaULock(pMeta);
12,215✔
1654

1655
  if (lock) {
12,217✔
1656
    metaRLock(pReader->pMeta);
2,243✔
1657
  }
1658

1659
_exit:
9,974✔
1660
  tdbFree(pData);
25,527,443✔
1661
  return code;
25,526,384✔
1662
}
1663

1664
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
191,826✔
1665
  int32_t code = 0;
191,826✔
1666

1667
  if (!numOfTables && !numOfCols) goto _exit;
191,826!
1668

1669
  SVnode *pVnodeObj = pVnode;
191,826✔
1670
  metaRLock(pVnodeObj->pMeta);
191,826✔
1671

1672
  // fast path: search cache
1673
  SMetaStbStats state = {0};
191,865✔
1674
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
191,865✔
1675
    metaULock(pVnodeObj->pMeta);
172,491✔
1676
    if (numOfTables) *numOfTables = state.ctbNum;
172,485✔
1677
    if (numOfCols) *numOfCols = state.colNum;
172,485✔
1678
    goto _exit;
172,485✔
1679
  }
1680

1681
  // slow path: search TDB
1682
  int64_t ctbNum = 0;
19,377✔
1683
  int32_t colNum = 0;
19,377✔
1684
  int64_t keep = 0;
19,377✔
1685
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
19,377✔
1686
  if (TSDB_CODE_SUCCESS == code) {
19,375!
1687
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
19,375✔
1688
  }
1689
  if (TSDB_CODE_SUCCESS == code) {
19,377✔
1690
    code = vnodeGetStbKeep(pVnode, uid, &keep);
19,375✔
1691
  }
1692
  metaULock(pVnodeObj->pMeta);
19,376✔
1693
  if (TSDB_CODE_SUCCESS != code) {
19,376!
UNCOV
1694
    goto _exit;
×
1695
  }
1696

1697
  if (numOfTables) *numOfTables = ctbNum;
19,376✔
1698
  if (numOfCols) *numOfCols = colNum;
19,376✔
1699

1700
  state.uid = uid;
19,376✔
1701
  state.ctbNum = ctbNum;
19,376✔
1702
  state.colNum = colNum;
19,376✔
1703
  state.keep = keep;
19,376✔
1704
  // upsert the cache
1705
  metaWLock(pVnodeObj->pMeta);
19,376✔
1706

1707
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
19,378✔
1708
  if (ret) {
19,377!
NEW
1709
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64, uid, ctbNum,
×
1710
              colNum, keep);
1711
  }
1712

1713
  metaULock(pVnodeObj->pMeta);
19,377✔
1714

1715
_exit:
191,850✔
1716
  return code;
191,850✔
1717
}
1718

1719
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
122,282✔
1720
  SMetaStbStats stats = {0};
122,282✔
1721

1722
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
122,282✔
1723
    stats.ctbNum += deltaCtb;
111,264✔
1724
    stats.colNum += deltaCol;
111,264✔
1725
    if (deltaKeep > 0) {
111,264!
NEW
1726
      stats.keep = deltaKeep;
×
1727
    }
1728

1729
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
111,264✔
1730
    if (code) {
111,268!
NEW
1731
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1732
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1733
    }
1734
  }
1735
}
122,339✔
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