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

taosdata / TDengine / #4852

14 Nov 2025 08:06AM UTC coverage: 63.812% (+0.06%) from 63.754%
#4852

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

355 of 675 new or added lines in 18 files covered. (52.59%)

3142 existing lines in 128 files now uncovered.

149263 of 233910 relevant lines covered (63.81%)

118719500.98 hits per line

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

61.25
/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) {
552,331,041✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
552,331,041✔
21
  metaReaderDoInit(pReader, pMeta, flags);
552,386,741✔
22
  pReader->pAPI = pAPI;
552,363,207✔
23
}
552,378,486✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
640,868,519✔
26
  memset(pReader, 0, sizeof(*pReader));
640,868,519✔
27
  pReader->pMeta = pMeta;
640,868,519✔
28
  pReader->flags = flags;
640,858,571✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
640,801,543✔
30
    metaRLock(pMeta);
458,181,170✔
31
  }
32
}
640,833,637✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
237,782,052✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
237,782,052✔
36
    metaULock(pReader->pMeta);
237,847,429✔
37
    pReader->flags |= META_READER_NOLOCK;
237,705,162✔
38
  }
39
}
237,907,850✔
40

41
void metaReaderClear(SMetaReader *pReader) {
673,814,563✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
673,814,563✔
43
    metaULock(pReader->pMeta);
220,156,904✔
44
  }
45
  tDecoderClear(&pReader->coder);
673,806,471✔
46
  tdbFree(pReader->pBuf);
674,008,821✔
47
  pReader->pBuf = NULL;
673,924,088✔
48
}
673,964,163✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
1,012,737,691✔
51
  int32_t  code = 0;
1,012,737,691✔
52
  SMeta   *pMeta = pReader->pMeta;
1,012,737,691✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
1,012,845,511✔
54

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

60
  // decode the entry
61
  tDecoderClear(&pReader->coder);
1,012,689,124✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
1,012,742,972✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
1,012,752,090✔
65
  if (code) {
1,012,709,280✔
66
    tDecoderClear(&pReader->coder);
×
UNCOV
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
1,012,709,280✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
70,268,245✔
79
    metaULock(pVnodeObj->pMeta);
4,603,789✔
80
    return false;
4,603,789✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
65,684,279✔
84
  return true;
65,684,120✔
85
}
86

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

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

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
279,804,926✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
279,812,097✔
98
}
99

100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
372,535,434✔
101
  SMeta *pMeta = pReader->pMeta;
372,535,434✔
102

103
  SMetaInfo info;
372,555,398✔
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
372,619,159✔
105
  if (TSDB_CODE_SUCCESS != code) {
372,573,075✔
UNCOV
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
107
  }
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
372,573,075✔
110
}
111

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

116
  // query name.idx
117
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
67,732,664✔
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
11,672,600✔
119
  }
120

121
  uid = *(tb_uid_t *)pReader->pBuf;
56,060,059✔
122
  return metaReaderGetTableEntryByUid(pReader, uid);
56,058,954✔
123
}
124

125
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
61,051,754✔
126
  void    *pData = NULL;
61,051,754✔
127
  int      nData = 0;
61,053,539✔
128
  tb_uid_t uid = 0;
61,053,467✔
129

130
  metaRLock(pMeta);
61,053,467✔
131

132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
61,053,830✔
133
    uid = *(tb_uid_t *)pData;
9,707,370✔
134
    tdbFree(pData);
9,707,633✔
135
  }
136

137
  metaULock(pMeta);
61,049,640✔
138

139
  return uid;
61,050,653✔
140
}
141

142
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
7,490,779✔
143
  int         code = 0;
7,490,779✔
144
  SMetaReader mr = {0};
7,490,779✔
145
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
7,491,353✔
146
  code = metaReaderGetTableEntryByUid(&mr, uid);
7,490,918✔
147
  if (code < 0) {
7,492,641✔
148
    metaReaderClear(&mr);
×
UNCOV
149
    return code;
×
150
  }
151

152
  STR_TO_VARSTR(tbName, mr.me.name);
7,492,641✔
153
  metaReaderClear(&mr);
7,492,067✔
154

155
  return 0;
7,490,919✔
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);
×
UNCOV
165
    return code;
×
166
  }
167
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
UNCOV
168
  metaReaderClear(&mr);
×
169

UNCOV
170
  return 0;
×
171
}
172

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

178
  SMetaReader *pReader = &mr;
695,933✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
420,203✔
187

188
  metaReaderClear(&mr);
420,203✔
189

190
  return 0;
420,203✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
425,257✔
199
  if (code == 0) *tbType = mr.me.type;
425,257✔
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
425,257✔
201
    *suid = mr.me.ctbEntry.suid;
418,911✔
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
6,346✔
203
    *suid = mr.me.uid;
5,904✔
204
  } else {
205
    *suid = 0;
442✔
206
  }
207

208
  metaReaderClear(&mr);
425,257✔
209
  return code;
425,257✔
210
}
211

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

215
  // TODO
216

UNCOV
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) {
×
UNCOV
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) {
×
UNCOV
231
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
232
  } else {
UNCOV
233
    goto _exit;
×
234
  }
235

UNCOV
236
  code = 0;
×
237

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

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

248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
10,697,018✔
249
  if (pTbCur == NULL) {
10,696,935✔
UNCOV
250
    return NULL;
×
251
  }
252

253
  SVnode *pVnodeObj = pVnode;
10,696,935✔
254
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
255
  pTbCur->pMeta = pVnodeObj->pMeta;
10,696,935✔
256
  pTbCur->paused = 1;
10,696,230✔
257
  code = metaResumeTbCursor(pTbCur, 1, 0);
10,696,230✔
258
  if (code) {
10,695,345✔
259
    terrno = code;
×
260
    taosMemoryFree(pTbCur);
×
UNCOV
261
    return NULL;
×
262
  }
263
  return pTbCur;
10,695,345✔
264
}
265

266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
21,386,244✔
267
  if (pTbCur) {
21,386,244✔
268
    tdbFree(pTbCur->pKey);
10,698,214✔
269
    tdbFree(pTbCur->pVal);
10,698,760✔
270
    if (!pTbCur->paused) {
10,696,935✔
271
      metaReaderClear(&pTbCur->mr);
4,936,268✔
272
      if (pTbCur->pDbc) {
4,934,878✔
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
4,934,305✔
274
      }
275
    }
276
    taosMemoryFree(pTbCur);
10,696,387✔
277
  }
278
}
21,386,061✔
279

280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
6,068,075✔
281
  if (!pTbCur->paused) {
6,068,075✔
282
    metaReaderClear(&pTbCur->mr);
6,068,667✔
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
6,068,075✔
284
    pTbCur->paused = 1;
6,068,075✔
285
  }
286
}
6,068,667✔
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
11,002,530✔
288
  int32_t code = 0;
11,002,530✔
289
  int32_t lino;
290
  int8_t  locked = 0;
11,002,530✔
291
  if (pTbCur->paused) {
11,002,530✔
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
11,001,222✔
293
    locked = 1;
11,004,087✔
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
11,004,087✔
295
    if (code != 0) {
11,002,733✔
UNCOV
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

299
    if (first) {
11,002,733✔
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
10,697,461✔
301
      TSDB_CHECK_CODE(code, lino, _exit);
10,692,706✔
302
    } else {
303
      int c = 1;
305,272✔
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
305,272✔
305
      TSDB_CHECK_CODE(code, lino, _exit);
305,272✔
306
      if (c == 0) {
305,272✔
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
305,272✔
308
      } else if (c < 0) {
×
309
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
UNCOV
310
        TSDB_CHECK_CODE(code, lino, _exit);
×
311
      } else {
312
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
UNCOV
313
        TSDB_CHECK_CODE(code, lino, _exit);
×
314
      }
315
    }
316

317
    pTbCur->paused = 0;
10,998,949✔
318
  }
319

UNCOV
320
_exit:
×
321
  if (code != 0 && locked) {
10,996,985✔
UNCOV
322
    metaReaderReleaseLock(&pTbCur->mr);
×
323
  }
324
  return code;
11,000,983✔
325
}
326

327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
371,101,096✔
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);
371,101,096✔
334
    if (ret < 0) {
371,092,103✔
335
      return ret;
10,689,107✔
336
    }
337

338
    tDecoderClear(&pTbCur->mr.coder);
360,402,996✔
339

340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
360,424,756✔
341
    if (ret) return ret;
360,421,135✔
342

343
    if (pTbCur->mr.me.type == jumpTableType) {
360,421,135✔
344
      continue;
3,911,642✔
345
    }
346

347
    break;
356,510,720✔
348
  }
349

350
  return 0;
356,510,720✔
351
}
352

UNCOV
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) {
×
UNCOV
361
      return -1;
×
362
    }
363

UNCOV
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) {
×
UNCOV
368
      return ret;
×
369
    }
370

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

UNCOV
375
    break;
×
376
  }
377

UNCOV
378
  return 0;
×
379
}
380

381
/**
382
 * @param type 0x01 fetchRsmaSchema if table is rsma
383
 */
384
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
184,075,562✔
385
                                   int8_t type) {
386
  int32_t         code = 0;
184,075,562✔
387
  void           *pData = NULL;
184,075,562✔
388
  int             nData = 0;
184,088,409✔
389
  int64_t         version;
390
  SSchemaWrapper  schema = {0};
184,089,516✔
391
  SSchemaWrapper *pSchema = NULL;
184,040,109✔
392
  SDecoder        dc = {0};
184,040,109✔
393
  if (lock) {
183,948,050✔
394
    metaRLock(pMeta);
176,912,050✔
395
  }
396
_query:
196,932,093✔
397
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
196,949,202✔
398
    goto _err;
301,186✔
399
  }
400

401
  version = ((SUidIdxVal *)pData)[0].version;
196,716,521✔
402

403
  if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData)) !=
196,718,025✔
404
      0) {
UNCOV
405
    goto _err;
×
406
  }
407

408
  SMetaEntry me = {0};
196,717,223✔
409
  tDecoderInit(&dc, pData, nData);
196,709,890✔
410
  code = metaDecodeEntry(&dc, &me);
196,721,181✔
411
  if (code) {
196,635,473✔
412
    tDecoderClear(&dc);
×
UNCOV
413
    goto _err;
×
414
  }
415
  if (me.type == TSDB_SUPER_TABLE) {
196,635,473✔
416
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
142,813,344✔
417
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
142,742,595✔
418
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
142,742,595✔
419
      if (TABLE_IS_ROLLUP(me.flags)) {
142,742,391✔
420
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
199,176✔
421
          tDecoderClear(&dc);
×
UNCOV
422
          goto _err;
×
423
        }
424
      }
425
      tDecoderClear(&dc);
142,742,391✔
426
      goto _exit;
142,753,293✔
427
    }
428
  } else if (me.type == TSDB_CHILD_TABLE) {
53,822,129✔
429
    uid = me.ctbEntry.suid;
12,800,549✔
430
    tDecoderClear(&dc);
12,800,549✔
431
    goto _query;
12,818,397✔
432
  } else {
433
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
41,021,580✔
434
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
41,018,410✔
435
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
41,018,410✔
436
      tDecoderClear(&dc);
41,020,883✔
437
      goto _exit;
41,021,908✔
438
    }
439
  }
440
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
28,801✔
441
  tDecoderClear(&dc);
28,801✔
442

443
  // query from skm db
444
  if ((code = tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) < 0) {
38,421✔
UNCOV
445
    goto _err;
×
446
  }
447

448
  tDecoderInit(&dc, pData, nData);
38,421✔
449
  if ((code = tDecodeSSchemaWrapperEx(&dc, &schema)) != 0) {
38,421✔
UNCOV
450
    goto _err;
×
451
  }
452
  pSchema = tCloneSSchemaWrapper(&schema);
38,421✔
453
  if (pSchema == NULL) {
38,421✔
454
    code = terrno;
×
UNCOV
455
    goto _err;
×
456
  }
457
  tDecoderClear(&dc);
38,421✔
458

459
_exit:
183,813,622✔
460
  if (lock) {
183,844,665✔
461
    metaULock(pMeta);
176,802,165✔
462
  }
463
  tdbFree(pData);
183,788,036✔
464
  return pSchema;
183,831,076✔
465

466
_err:
290,072✔
467
  if (lock) {
301,186✔
468
    metaULock(pMeta);
301,186✔
469
  }
470
  tdbFree(pData);
301,118✔
471
  tDeleteSchemaWrapper(pSchema);
472
  if (extSchema != NULL) {
301,118✔
473
    taosMemoryFreeClear(*extSchema);
44,686✔
474
  }
475
  terrno = code;
301,118✔
476
  return NULL;
301,118✔
477
}
478

479
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
16,588✔
480
  void    *pData = NULL;
16,588✔
481
  int      nData = 0;
16,588✔
482
  int64_t  version = 0;
16,588✔
483
  SDecoder dc = {0};
16,588✔
484
  int64_t  createTime = INT64_MAX;
16,588✔
485
  if (lock) {
16,588✔
486
    metaRLock(pMeta);
16,588✔
487
  }
488

489
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
16,588✔
UNCOV
490
    goto _exit;
×
491
  }
492

493
  version = ((SUidIdxVal *)pData)[0].version;
16,588✔
494

495
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
16,588✔
UNCOV
496
    goto _exit;
×
497
  }
498

499
  SMetaEntry me = {0};
16,588✔
500
  tDecoderInit(&dc, pData, nData);
16,588✔
501
  int32_t code = metaDecodeEntry(&dc, &me);
16,588✔
502
  if (code) {
16,588✔
503
    tDecoderClear(&dc);
×
UNCOV
504
    goto _exit;
×
505
  }
506
  if (me.type == TSDB_CHILD_TABLE) {
16,588✔
507
    createTime = me.ctbEntry.btime;
16,520✔
508
  } else if (me.type == TSDB_NORMAL_TABLE) {
68✔
509
    createTime = me.ntbEntry.btime;
68✔
510
  }
511
  tDecoderClear(&dc);
16,588✔
512

513
_exit:
16,588✔
514
  if (lock) {
16,588✔
515
    metaULock(pMeta);
16,588✔
516
  }
517
  tdbFree(pData);
16,588✔
518
  return createTime;
16,588✔
519
}
520

521
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
93,879,878✔
522
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
93,879,878✔
523
  SMCtbCursor *pCtbCur = NULL;
93,892,175✔
524
  SCtbIdxKey   ctbIdxKey;
525
  int          ret = 0;
93,892,175✔
526
  int          c = 0;
93,892,175✔
527

528
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
93,892,175✔
529
  if (pCtbCur == NULL) {
93,735,428✔
UNCOV
530
    return NULL;
×
531
  }
532

533
  pCtbCur->pMeta = pMeta;
93,735,428✔
534
  pCtbCur->suid = uid;
93,736,824✔
535
  pCtbCur->lock = lock;
93,735,782✔
536
  pCtbCur->paused = 1;
93,778,219✔
537

538
  ret = metaResumeCtbCursor(pCtbCur, 1);
93,780,949✔
539
  if (ret < 0) {
93,884,670✔
UNCOV
540
    return NULL;
×
541
  }
542
  return pCtbCur;
93,884,670✔
543
}
544

545
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
93,912,513✔
546
  if (pCtbCur) {
93,912,513✔
547
    if (!pCtbCur->paused) {
93,919,054✔
548
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
92,007,577✔
549
      if (pCtbCur->pCur) {
92,015,959✔
550
        tdbTbcClose(pCtbCur->pCur);
91,996,736✔
551
      }
552
    }
553
    tdbFree(pCtbCur->pKey);
93,889,005✔
554
    tdbFree(pCtbCur->pVal);
93,875,842✔
555
  }
556
  taosMemoryFree(pCtbCur);
93,874,360✔
557
}
93,878,172✔
558

559
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
1,913,121✔
560
  if (!pCtbCur->paused) {
1,913,121✔
561
    tdbTbcClose((TBC *)pCtbCur->pCur);
1,912,301✔
562
    if (pCtbCur->lock) {
1,912,537✔
563
      metaULock(pCtbCur->pMeta);
1,912,537✔
564
    }
565
    pCtbCur->paused = 1;
1,912,490✔
566
  }
567
}
1,913,501✔
568

569
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
93,877,254✔
570
  if (pCtbCur->paused) {
93,877,254✔
571
    pCtbCur->paused = 0;
93,800,545✔
572

573
    if (pCtbCur->lock) {
93,853,100✔
574
      metaRLock(pCtbCur->pMeta);
86,654,522✔
575
    }
576
    int ret = 0;
93,890,504✔
577
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
93,890,504✔
578
    if (ret < 0) {
93,831,296✔
579
      metaCloseCtbCursor(pCtbCur);
×
UNCOV
580
      return -1;
×
581
    }
582

583
    if (first) {
93,831,391✔
584
      SCtbIdxKey ctbIdxKey;
93,800,721✔
585
      // move to the suid
586
      ctbIdxKey.suid = pCtbCur->suid;
93,837,669✔
587
      ctbIdxKey.uid = INT64_MIN;
93,864,029✔
588
      int c = 0;
93,864,029✔
589
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
93,783,996✔
590
      if (c > 0) {
93,882,470✔
591
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
10,323,362✔
592
      }
593
    } else {
594
      int c = 0;
×
595
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
596
      if (c < 0) {
×
UNCOV
597
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
598
      } else {
UNCOV
599
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
600
      }
601
    }
602
  }
603
  return 0;
93,898,231✔
604
}
605

606
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
508,727,067✔
607
  int         ret;
608
  SCtbIdxKey *pCtbIdxKey;
609

610
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
508,727,067✔
611
  if (ret < 0) {
508,763,665✔
612
    return 0;
73,893,206✔
613
  }
614

615
  pCtbIdxKey = pCtbCur->pKey;
434,870,459✔
616
  if (pCtbIdxKey->suid > pCtbCur->suid) {
434,891,944✔
617
    return 0;
20,072,105✔
618
  }
619

620
  return pCtbIdxKey->uid;
414,829,611✔
621
}
622

623
struct SMStbCursor {
624
  SMeta   *pMeta;
625
  TBC     *pCur;
626
  tb_uid_t suid;
627
  void    *pKey;
628
  void    *pVal;
629
  int      kLen;
630
  int      vLen;
631
};
632

633
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
58,544,712✔
634
  SMStbCursor *pStbCur = NULL;
58,544,712✔
635
  int          ret = 0;
58,544,712✔
636
  int          c = 0;
58,544,712✔
637

638
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
58,546,674✔
639
  if (pStbCur == NULL) {
58,531,628✔
640
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
641
    return NULL;
×
642
  }
643

644
  pStbCur->pMeta = pMeta;
58,531,628✔
645
  pStbCur->suid = suid;
58,543,509✔
646
  metaRLock(pMeta);
58,544,712✔
647

648
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
58,549,944✔
649
  if (ret < 0) {
58,540,862✔
650
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
651
    metaULock(pMeta);
×
652
    taosMemoryFree(pStbCur);
×
UNCOV
653
    return NULL;
×
654
  }
655

656
  // move to the suid
657
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
58,540,862✔
658
  if (c > 0) {
58,551,906✔
UNCOV
659
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
660
  }
661

662
  return pStbCur;
58,545,366✔
663
}
664

665
void metaCloseStbCursor(SMStbCursor *pStbCur) {
58,547,433✔
666
  if (pStbCur) {
58,547,433✔
667
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
58,548,636✔
668
    if (pStbCur->pCur) {
58,550,493✔
669
      tdbTbcClose(pStbCur->pCur);
58,549,944✔
670

671
      tdbFree(pStbCur->pKey);
58,543,973✔
672
      tdbFree(pStbCur->pVal);
58,547,328✔
673
    }
674

675
    taosMemoryFree(pStbCur);
58,547,328✔
676
  }
677
}
58,540,259✔
678

679
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
184,814,323✔
680
  int ret;
681

682
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
184,814,323✔
683
  if (ret < 0) {
184,813,096✔
684
    return 0;
58,552,011✔
685
  }
686
  return *(tb_uid_t *)pStbCur->pKey;
126,261,085✔
687
}
688

689
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
161,696,096✔
690
  STSchema       *pTSchema = NULL;
161,696,096✔
691
  SSchemaWrapper *pSW = NULL;
161,696,096✔
692

693
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
161,696,096✔
694
  if (!pSW) return NULL;
161,701,465✔
695

696
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
161,454,405✔
697

698
  tDeleteSchemaWrapper(pSW);
699
  return pTSchema;
161,428,811✔
700
}
701

702
/**
703
 * Fetch rsma schema if table is rsma
704
 */
705
SRSchema *metaGetTbTSchemaR(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
55,584✔
706
  SRSchema       *pRSchema = NULL;
55,584✔
707
  SSchemaWrapper *pSW = NULL;
55,584✔
708

709
  if (!(pRSchema = (SRSchema *)taosMemoryCalloc(1, sizeof(SRSchema)))) goto _err;
55,584✔
710
  if (!(pSW = metaGetTableSchema(pMeta, uid, sver, lock, (SExtSchema **)&pRSchema->extSchema, 0x01))) goto _err;
55,584✔
711
  if (!(pRSchema->tSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version))) goto _err;
55,584✔
712

713
  if (pSW->pRsma) {
55,584✔
714
    if (!(pRSchema->funcIds = taosMemoryCalloc(pSW->nCols, sizeof(func_id_t)))) goto _err;
55,584✔
715
    memcpy(pRSchema->funcIds, pSW->pRsma->funcIds, pSW->nCols * sizeof(func_id_t));
55,584✔
716

717
    pRSchema->tbType = pSW->pRsma->tbType;
55,584✔
718
    pRSchema->tbUid = uid;
55,584✔
719
    tstrncpy(pRSchema->tbName, pSW->pRsma->tbName, TSDB_TABLE_NAME_LEN);
55,584✔
720
    pRSchema->interval[0] = pSW->pRsma->interval[0];
55,584✔
721
    pRSchema->interval[1] = pSW->pRsma->interval[1];
55,584✔
722
  }
723

724
_exit:
55,584✔
725
  tDeleteSchemaWrapper(pSW);
726
  return pRSchema;
55,584✔
UNCOV
727
_err:
×
728
  tDeleteSchemaWrapper(pSW);
729
  tFreeSRSchema(&pRSchema);
UNCOV
730
  return NULL;
×
731
}
732

733
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
440,433✔
734
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
440,433✔
735
  if (*ppTSchema == NULL) {
440,433✔
UNCOV
736
    return terrno;
×
737
  }
738
  return TSDB_CODE_SUCCESS;
440,433✔
739
}
740

741
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
154,373,602✔
742
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
154,373,602✔
743
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
154,340,432✔
744
    return terrno;
95✔
745
  }
746
  return TSDB_CODE_SUCCESS;
154,328,246✔
747
}
748

749
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
43,854,781✔
750
  int32_t code = 0;
43,854,781✔
751
  int32_t lino;
752

753
  void     *pData = NULL;
43,854,781✔
754
  int       nData = 0;
43,864,307✔
755
  SSkmDbKey skmDbKey;
43,857,177✔
756
  if (sver <= 0) {
43,861,497✔
757
    SMetaInfo info;
23,014,325✔
758
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
23,026,322✔
759
      sver = info.skmVer;
23,027,171✔
760
    } else {
761
      TBC *pSkmDbC = NULL;
7,646✔
762
      int  c;
7,740✔
763

764
      skmDbKey.uid = suid ? suid : uid;
7,740✔
765
      skmDbKey.sver = INT32_MAX;
7,740✔
766

767
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
7,740✔
768
      TSDB_CHECK_CODE(code, lino, _exit);
7,740✔
769
      metaRLock(pMeta);
7,740✔
770

771
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
7,740✔
772
        metaULock(pMeta);
×
773
        tdbTbcClose(pSkmDbC);
×
774
        code = TSDB_CODE_NOT_FOUND;
×
UNCOV
775
        goto _exit;
×
776
      }
777

778
      if (c == 0) {
7,740✔
779
        metaULock(pMeta);
×
780
        tdbTbcClose(pSkmDbC);
×
781
        code = TSDB_CODE_FAILED;
×
782
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
UNCOV
783
        goto _exit;
×
784
      }
785

786
      if (c < 0) {
7,740✔
787
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
940✔
788
      }
789

790
      const void *pKey = NULL;
7,740✔
791
      int32_t     nKey = 0;
7,740✔
792
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
7,740✔
793

794
      if (ret != 0 || ((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
7,740✔
795
        metaULock(pMeta);
×
796
        tdbTbcClose(pSkmDbC);
×
797
        code = TSDB_CODE_NOT_FOUND;
×
UNCOV
798
        goto _exit;
×
799
      }
800

801
      sver = ((SSkmDbKey *)pKey)->sver;
7,740✔
802

803
      metaULock(pMeta);
7,740✔
804
      tdbTbcClose(pSkmDbC);
7,740✔
805
    }
806
  }
807

808
  if (!(sver > 0)) {
43,858,960✔
809
    code = TSDB_CODE_NOT_FOUND;
×
UNCOV
810
    goto _exit;
×
811
  }
812

813
  skmDbKey.uid = suid ? suid : uid;
43,858,960✔
814
  skmDbKey.sver = sver;
43,858,960✔
815
  metaRLock(pMeta);
43,858,960✔
816
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
43,872,001✔
817
    metaULock(pMeta);
2,612✔
818
    code = TSDB_CODE_NOT_FOUND;
2,612✔
819
    goto _exit;
2,612✔
820
  }
821
  metaULock(pMeta);
43,862,714✔
822

823
  // decode
824
  SDecoder        dc = {0};
43,869,404✔
825
  SSchemaWrapper  schema;
43,844,573✔
826
  SSchemaWrapper *pSchemaWrapper = &schema;
43,867,474✔
827

828
  tDecoderInit(&dc, pData, nData);
43,867,474✔
829
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
43,882,801✔
830
  tDecoderClear(&dc);
43,882,801✔
831
  tdbFree(pData);
43,874,698✔
832
  if (TSDB_CODE_SUCCESS != code) {
43,854,601✔
833
    taosMemoryFree(pSchemaWrapper->pSchema);
×
UNCOV
834
    goto _exit;
×
835
  }
836

837
  // convert
838
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
43,854,601✔
839
  if (pTSchema == NULL) {
43,865,731✔
UNCOV
840
    code = TSDB_CODE_OUT_OF_MEMORY;
×
841
  }
842

843
  *ppTSchema = pTSchema;
43,865,731✔
844
  taosMemoryFree(pSchemaWrapper->pSchema);
43,867,993✔
845

846
_exit:
43,854,036✔
847
  return code;
43,859,163✔
848
}
849

850
// N.B. Called by statusReq per second
851
int64_t metaGetTbNum(SMeta *pMeta) {
120,313,700✔
852
  // num of child tables (excluding normal tables , stables and others)
853

854
  /* int64_t num = 0; */
855
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
856

857
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
120,313,700✔
858
}
859

860
void metaUpdTimeSeriesNum(SMeta *pMeta) {
58,546,426✔
861
  int64_t nCtbTimeSeries = 0;
58,546,426✔
862
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
58,547,839✔
863
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
58,545,199✔
864
  }
865
}
58,550,455✔
866

867
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
868
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
869
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
191,070,937✔
870
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
191,071,009✔
871
    metaUpdTimeSeriesNum(pMeta);
58,537,491✔
872
  }
873

874
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
191,084,467✔
875
}
876

877
// type: 1 reported timeseries
878
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
191,067,122✔
879
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
191,060,503✔
880
  if (type == 1) {
191,060,503✔
881
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
120,313,700✔
882
  }
883
  return nTimeSeries;
191,045,174✔
884
}
885

886
typedef struct {
887
  SMeta   *pMeta;
888
  TBC     *pCur;
889
  tb_uid_t uid;
890
  void    *pKey;
891
  void    *pVal;
892
  int      kLen;
893
  int      vLen;
894
} SMSmaCursor;
895

896
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
897
  SMSmaCursor *pSmaCur = NULL;
×
UNCOV
898
  SSmaIdxKey   smaIdxKey;
×
899
  int          ret;
UNCOV
900
  int          c;
×
901

902
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
903
  if (pSmaCur == NULL) {
×
904
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
905
    return NULL;
×
906
  }
907

908
  pSmaCur->pMeta = pMeta;
×
909
  pSmaCur->uid = uid;
×
UNCOV
910
  metaRLock(pMeta);
×
911

912
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
913
  if (ret < 0) {
×
914
    metaULock(pMeta);
×
915
    taosMemoryFree(pSmaCur);
×
UNCOV
916
    return NULL;
×
917
  }
918

919
  // move to the suid
920
  smaIdxKey.uid = uid;
×
921
  smaIdxKey.smaUid = INT64_MIN;
×
922
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
923
  if (c > 0) {
×
UNCOV
924
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
925
  }
926

UNCOV
927
  return pSmaCur;
×
928
}
929

930
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
931
  if (pSmaCur) {
×
932
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
933
    if (pSmaCur->pCur) {
×
934
      tdbTbcClose(pSmaCur->pCur);
×
UNCOV
935
      pSmaCur->pCur = NULL;
×
936

937
      tdbFree(pSmaCur->pKey);
×
UNCOV
938
      tdbFree(pSmaCur->pVal);
×
939
    }
940

UNCOV
941
    taosMemoryFree(pSmaCur);
×
942
  }
UNCOV
943
}
×
944

UNCOV
945
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
946
  int         ret;
947
  SSmaIdxKey *pSmaIdxKey;
948

949
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
950
  if (ret < 0) {
×
UNCOV
951
    return 0;
×
952
  }
953

954
  pSmaIdxKey = pSmaCur->pKey;
×
955
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
UNCOV
956
    return 0;
×
957
  }
958

UNCOV
959
  return pSmaIdxKey->uid;
×
960
}
961

962
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
963
  STSmaWrapper *pSW = NULL;
×
UNCOV
964
  SArray       *pSmaIds = NULL;
×
965

966
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
UNCOV
967
    return NULL;
×
968
  }
969

970
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
971
  if (!pSW) {
×
972
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
973
    goto _err;
×
974
  }
975

976
  pSW->number = taosArrayGetSize(pSmaIds);
×
UNCOV
977
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
978

979
  if (!pSW->tSma) {
×
980
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
981
    goto _err;
×
982
  }
983

984
  SMetaReader mr = {0};
×
UNCOV
985
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
986
  int64_t smaId;
987
  int     smaIdx = 0;
×
988
  STSma  *pTSma = NULL;
×
989
  for (int i = 0; i < pSW->number; ++i) {
×
990
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
991
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
992
      tDecoderClear(&mr.coder);
×
993
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
UNCOV
994
      continue;
×
995
    }
996
    tDecoderClear(&mr.coder);
×
997
    pTSma = pSW->tSma + smaIdx;
×
998
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
999
    if (deepCopy) {
×
1000
      if (pTSma->exprLen > 0) {
×
1001
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
1002
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1003
          goto _err;
×
1004
        }
UNCOV
1005
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
1006
      }
1007
      if (pTSma->tagsFilterLen > 0) {
×
1008
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
1009
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1010
          goto _err;
×
1011
        }
1012
      }
UNCOV
1013
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
1014
    } else {
1015
      pTSma->exprLen = 0;
×
1016
      pTSma->expr = NULL;
×
1017
      pTSma->tagsFilterLen = 0;
×
UNCOV
1018
      pTSma->tagsFilter = NULL;
×
1019
    }
1020

UNCOV
1021
    ++smaIdx;
×
1022
  }
1023

1024
  if (smaIdx <= 0) goto _err;
×
UNCOV
1025
  pSW->number = smaIdx;
×
1026

1027
  metaReaderClear(&mr);
×
1028
  taosArrayDestroy(pSmaIds);
×
1029
  return pSW;
×
1030
_err:
×
1031
  metaReaderClear(&mr);
×
1032
  taosArrayDestroy(pSmaIds);
×
1033
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
UNCOV
1034
  return NULL;
×
1035
}
1036

1037
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
×
1038
  STSma      *pTSma = NULL;
×
1039
  SMetaReader mr = {0};
×
1040
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
1041
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
×
1042
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
1043
    metaReaderClear(&mr);
×
UNCOV
1044
    return NULL;
×
1045
  }
1046
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
×
1047
  if (!pTSma) {
×
1048
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1049
    metaReaderClear(&mr);
×
UNCOV
1050
    return NULL;
×
1051
  }
1052

UNCOV
1053
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1054

1055
  metaReaderClear(&mr);
×
UNCOV
1056
  return pTSma;
×
1057
}
1058

1059
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1060
  SArray     *pUids = NULL;
×
UNCOV
1061
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1062

1063
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1064
  if (!pCur) {
×
UNCOV
1065
    return NULL;
×
1066
  }
1067

1068
  while (1) {
×
1069
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1070
    if (id == 0) {
×
UNCOV
1071
      break;
×
1072
    }
1073

1074
    if (!pUids) {
×
1075
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1076
      if (!pUids) {
×
1077
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1078
        metaCloseSmaCursor(pCur);
×
UNCOV
1079
        return NULL;
×
1080
      }
1081
    }
1082

UNCOV
1083
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1084

1085
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1086
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1087
      metaCloseSmaCursor(pCur);
×
1088
      taosArrayDestroy(pUids);
×
UNCOV
1089
      return NULL;
×
1090
    }
1091
  }
1092

1093
  metaCloseSmaCursor(pCur);
×
UNCOV
1094
  return pUids;
×
1095
}
1096

1097
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1098
  SArray     *pUids = NULL;
×
1099
  SSmaIdxKey *pSmaIdxKey = NULL;
×
UNCOV
1100
  tb_uid_t    lastUid = 0;
×
1101

1102
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1103
  if (!pCur) {
×
UNCOV
1104
    return NULL;
×
1105
  }
1106

1107
  while (1) {
×
1108
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1109
    if (uid == 0) {
×
UNCOV
1110
      break;
×
1111
    }
1112

1113
    if (lastUid == uid) {
×
UNCOV
1114
      continue;
×
1115
    }
1116

UNCOV
1117
    lastUid = uid;
×
1118

1119
    if (!pUids) {
×
1120
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1121
      if (!pUids) {
×
1122
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1123
        metaCloseSmaCursor(pCur);
×
UNCOV
1124
        return NULL;
×
1125
      }
1126
    }
1127

1128
    if (!taosArrayPush(pUids, &uid)) {
×
1129
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1130
      metaCloseSmaCursor(pCur);
×
1131
      taosArrayDestroy(pUids);
×
UNCOV
1132
      return NULL;
×
1133
    }
1134
  }
1135

1136
  metaCloseSmaCursor(pCur);
×
UNCOV
1137
  return pUids;
×
1138
}
1139

1140
#endif
1141

1142
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
716,740,946✔
1143
  STag *tag = (STag *)pTag;
716,740,946✔
1144
  if (type == TSDB_DATA_TYPE_JSON) {
716,740,946✔
1145
    return tag;
1,553,299✔
1146
  }
1147
  bool find = tTagGet(tag, val);
715,187,647✔
1148

1149
  if (!find) {
715,256,407✔
1150
    return NULL;
5,788,458✔
1151
  }
1152

1153
  return val;
709,467,949✔
1154
}
1155

1156
typedef struct {
1157
  SMeta   *pMeta;
1158
  TBC     *pCur;
1159
  tb_uid_t suid;
1160
  int16_t  cid;
1161
  int16_t  type;
1162
  void    *pKey;
1163
  void    *pVal;
1164
  int32_t  kLen;
1165
  int32_t  vLen;
1166
} SIdxCursor;
1167

1168
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
2,058✔
1169
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
2,058✔
1170
  SMetaFltParam *param = arg;
2,058✔
1171
  int32_t        ret = 0;
2,058✔
1172

1173
  SIdxCursor *pCursor = NULL;
2,058✔
1174
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
2,058✔
1175
  if (pCursor == NULL) {
2,058✔
UNCOV
1176
    return terrno;
×
1177
  }
1178
  pCursor->pMeta = pMeta;
2,058✔
1179
  pCursor->suid = param->suid;
2,058✔
1180
  pCursor->cid = param->cid;
2,058✔
1181
  pCursor->type = param->type;
2,058✔
1182

1183
  metaRLock(pMeta);
2,058✔
1184
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
2,058✔
1185
  if (ret != 0) {
2,058✔
UNCOV
1186
    goto END;
×
1187
  }
1188
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
2,058✔
1189

1190
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
2,058✔
1191
  SBtimeIdxKey *pBtimeKey = &btimeKey;
2,058✔
1192

1193
  int cmp = 0;
2,058✔
1194
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
2,058✔
UNCOV
1195
    goto END;
×
1196
  }
1197

1198
  int32_t valid = 0;
2,058✔
1199
  int32_t count = 0;
2,058✔
1200

1201
  static const int8_t TRY_ERROR_LIMIT = 1;
1202
  do {
13,423,991✔
1203
    void   *entryKey = NULL;
13,426,049✔
1204
    int32_t nEntryKey = -1;
13,314,917✔
1205
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
13,427,078✔
1206
    if (valid < 0) break;
13,283,704✔
1207

1208
    SBtimeIdxKey *p = entryKey;
13,281,646✔
1209
    if (count > TRY_ERROR_LIMIT) break;
13,281,646✔
1210

1211
    terrno = TSDB_CODE_SUCCESS;
13,281,646✔
1212
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
13,278,216✔
1213
    if (terrno != TSDB_CODE_SUCCESS) {
12,890,283✔
1214
      ret = terrno;
×
UNCOV
1215
      break;
×
1216
    }
1217
    if (cmp == 0) {
13,222,993✔
1218
      if (taosArrayPush(pUids, &p->uid) == NULL) {
26,717,985✔
1219
        ret = terrno;
×
UNCOV
1220
        break;
×
1221
      }
1222
    } else {
1223
      if (param->equal == true) {
×
1224
        if (count > TRY_ERROR_LIMIT) break;
×
UNCOV
1225
        count++;
×
1226
      }
1227
    }
1228
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
13,494,992✔
1229
    if (valid < 0) break;
13,477,842✔
1230
  } while (1);
1231

1232
END:
18,865✔
1233
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
2,058✔
1234
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
2,058✔
1235
  taosMemoryFree(pCursor);
2,058✔
1236
  return ret;
2,058✔
1237
}
1238

1239
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1240
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1241
  SMetaFltParam *param = arg;
×
1242
  int32_t        ret = 0;
×
UNCOV
1243
  char          *buf = NULL;
×
1244

1245
  STagIdxKey *pKey = NULL;
×
UNCOV
1246
  int32_t     nKey = 0;
×
1247

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

UNCOV
1258
  char *pName = param->val;
×
1259

1260
  metaRLock(pMeta);
×
1261
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1262
  if (ret != 0) {
×
UNCOV
1263
    goto END;
×
1264
  }
1265

1266
  int cmp = 0;
×
1267
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
UNCOV
1268
    goto END;
×
1269
  }
1270
  int32_t valid = 0;
×
UNCOV
1271
  int32_t count = 0;
×
1272

1273
  int32_t TRY_ERROR_LIMIT = 1;
×
1274
  do {
×
1275
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1276
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1277
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
UNCOV
1278
    if (valid < 0) break;
×
1279

UNCOV
1280
    if (count > TRY_ERROR_LIMIT) break;
×
1281

1282
    char *pTableKey = (char *)pEntryKey;
×
1283
    terrno = TSDB_CODE_SUCCESS;
×
1284
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1285
    if (terrno != TSDB_CODE_SUCCESS) {
×
1286
      ret = terrno;
×
UNCOV
1287
      goto END;
×
1288
    }
1289
    if (cmp == 0) {
×
1290
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1291
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1292
        ret = terrno;
×
UNCOV
1293
        goto END;
×
1294
      }
1295
    } else {
1296
      if (param->equal == true) {
×
1297
        if (count > TRY_ERROR_LIMIT) break;
×
UNCOV
1298
        count++;
×
1299
      }
1300
    }
1301
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1302
    if (valid < 0) {
×
UNCOV
1303
      break;
×
1304
    }
1305
  } while (1);
1306

1307
END:
×
1308
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1309
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1310
  taosMemoryFree(buf);
×
UNCOV
1311
  taosMemoryFree(pKey);
×
1312

UNCOV
1313
  taosMemoryFree(pCursor);
×
1314

UNCOV
1315
  return ret;
×
1316
}
1317
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1318
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1319
  SMetaFltParam *param = arg;
×
1320
  int32_t        ret = 0;
×
UNCOV
1321
  char          *buf = NULL;
×
1322

1323
  STtlIdxKey *pKey = NULL;
×
UNCOV
1324
  int32_t     nKey = 0;
×
1325

1326
  SIdxCursor *pCursor = NULL;
×
1327
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1328
  if (pCursor == NULL) {
×
UNCOV
1329
    return terrno;
×
1330
  }
1331
  pCursor->pMeta = pMeta;
×
1332
  pCursor->suid = param->suid;
×
1333
  pCursor->cid = param->cid;
×
UNCOV
1334
  pCursor->type = param->type;
×
1335

UNCOV
1336
  metaRLock(pMeta);
×
1337
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1338

1339
END:
×
1340
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1341
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1342
  taosMemoryFree(buf);
×
UNCOV
1343
  taosMemoryFree(pKey);
×
1344

UNCOV
1345
  taosMemoryFree(pCursor);
×
1346

UNCOV
1347
  return ret;
×
1348
  // impl later
1349
  return 0;
1350
}
1351
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
3,737,269✔
1352
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,737,269✔
1353
  SMetaFltParam *param = arg;
3,737,325✔
1354

1355
  SMetaEntry oStbEntry = {0};
3,737,325✔
1356
  int32_t    code = 0;
3,729,251✔
1357
  char      *buf = NULL;
3,729,251✔
1358
  void      *pData = NULL;
3,729,251✔
1359
  int        nData = 0;
3,733,451✔
1360

1361
  SDecoder    dc = {0};
3,734,113✔
1362
  STbDbKey    tbDbKey = {0};
3,729,444✔
1363
  STagIdxKey *pKey = NULL;
3,720,402✔
1364
  int32_t     nKey = 0;
3,725,747✔
1365

1366
  SIdxCursor *pCursor = NULL;
3,728,318✔
1367
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,728,318✔
1368
  if (!pCursor) {
3,723,535✔
UNCOV
1369
    return terrno;
×
1370
  }
1371
  pCursor->pMeta = pMeta;
3,723,535✔
1372
  pCursor->suid = param->suid;
3,724,779✔
1373
  pCursor->cid = param->cid;
3,730,592✔
1374
  pCursor->type = param->type;
3,727,396✔
1375

1376
  metaRLock(pMeta);
3,737,079✔
1377

1378
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
3,733,420✔
1379

1380
  tbDbKey.uid = param->suid;
3,739,126✔
1381
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,738,529✔
1382

1383
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
3,736,912✔
1384

1385
  tDecoderInit(&dc, pData, nData);
3,739,027✔
1386

1387
  code = metaDecodeEntry(&dc, &oStbEntry);
3,737,729✔
1388
  if (code) {
3,739,777✔
1389
    tDecoderClear(&dc);
×
UNCOV
1390
    goto END;
×
1391
  }
1392

1393
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
3,739,777✔
1394
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
457✔
1395
  }
1396

1397
  code = TSDB_CODE_INVALID_PARA;
3,739,777✔
1398

1399
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,992,049✔
1400
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,990,347✔
1401
    if (IS_IDX_ON(schema)) {
4,989,691✔
1402
      if (schema->colId == param->cid && param->type == schema->type) {
4,981,904✔
1403
        code = 0;
3,736,121✔
1404
        break;
3,736,121✔
1405
      }
1406
    }
1407
  }
1408

1409
  TAOS_CHECK_GOTO(code, NULL, END);
3,737,823✔
1410

1411
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,737,823✔
1412
  if (code != 0) {
3,734,702✔
UNCOV
1413
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1414
  }
1415

1416
  int32_t maxSize = 0;
3,734,702✔
1417
  int32_t nTagData = 0;
3,732,526✔
1418
  void   *tagData = NULL;
3,732,526✔
1419

1420
  if (param->val == NULL) {
3,732,526✔
1421
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
UNCOV
1422
    goto END;
×
1423
  } else {
1424
    if (IS_VAR_DATA_TYPE(param->type)) {
3,732,715✔
1425
      tagData = varDataVal(param->val);
564,882✔
1426
      nTagData = varDataLen(param->val);
563,514✔
1427

1428
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
563,514✔
1429
        maxSize = 4 * nTagData + 1;
38,267✔
1430
        buf = taosMemoryCalloc(1, maxSize);
38,267✔
1431
        if (buf == NULL) {
38,267✔
UNCOV
1432
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1433
        }
1434

1435
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
38,267✔
UNCOV
1436
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1437
        }
1438

1439
        tagData = buf;
37,495✔
1440
        nTagData = maxSize;
37,495✔
1441
      }
1442
    } else {
1443
      tagData = param->val;
3,165,242✔
1444
      nTagData = tDataTypes[param->type].bytes;
3,170,673✔
1445
    }
1446
  }
1447

1448
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
3,736,884✔
1449
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1450
                  NULL, END);
1451

1452
  int cmp = 0;
3,728,869✔
1453
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,729,466✔
1454

1455
  int     count = 0;
3,741,459✔
1456
  int32_t valid = 0;
3,741,459✔
1457
  bool    found = false;
3,741,459✔
1458

1459
  static const int8_t TRY_ERROR_LIMIT = 1;
1460

1461
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1462
  /// target:                        [suid, cid2, type2]
1463
  int diffCidCount = 0;
3,741,459✔
1464
  do {
36,601,844✔
1465
    void   *entryKey = NULL, *entryVal = NULL;
40,343,303✔
1466
    int32_t nEntryKey, nEntryVal;
40,339,963✔
1467

1468
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
40,342,819✔
1469
    if (valid < 0) {
40,357,073✔
1470
      break;
1,737,756✔
1471
    }
1472
    if (count > TRY_ERROR_LIMIT) {
38,619,317✔
1473
      break;
1,167,685✔
1474
    }
1475

1476
    STagIdxKey *p = entryKey;
37,451,632✔
1477
    if (p == NULL) break;
37,451,632✔
1478

1479
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
37,451,632✔
1480
      if (found == true) break;  //
1,073,031✔
1481
      if (diffCidCount > TRY_ERROR_LIMIT) break;
237,179✔
1482
      diffCidCount++;
237,179✔
1483
      count++;
237,179✔
1484
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
237,179✔
1485
      if (valid < 0) {
237,084✔
1486
        code = valid;
×
UNCOV
1487
        break;
×
1488
      } else {
1489
        continue;
237,084✔
1490
      }
1491
    }
1492

1493
    terrno = TSDB_CODE_SUCCESS;
36,373,865✔
1494
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
36,369,828✔
1495
    if (terrno != TSDB_CODE_SUCCESS) {
36,361,786✔
1496
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
UNCOV
1497
      break;
×
1498
    }
1499
    if (cmp == 0) {
36,363,608✔
1500
      // match
1501
      tb_uid_t tuid = 0;
32,280,998✔
1502
      if (IS_VAR_DATA_TYPE(pKey->type)) {
32,279,891✔
1503
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
331,340✔
1504
      } else {
1505
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
31,949,228✔
1506
      }
1507
      if (taosArrayPush(pUids, &tuid) == NULL) {
32,292,300✔
UNCOV
1508
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1509
      }
1510
      found = true;
32,292,958✔
1511
    } else {
1512
      if (param->equal == true) {
4,082,610✔
1513
        if (count > TRY_ERROR_LIMIT) break;
2,960,208✔
1514
        count++;
2,960,208✔
1515
      }
1516
    }
1517
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
36,374,003✔
1518
    if (valid < 0) {
36,366,303✔
1519
      code = valid;
×
UNCOV
1520
      break;
×
1521
    }
1522
  } while (1);
1523

1524
END:
3,742,712✔
1525
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,740,336✔
1526
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,742,707✔
1527
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,741,653✔
1528
  tDecoderClear(&dc);
3,741,653✔
1529
  tdbFree(pData);
3,742,110✔
1530

1531
  taosMemoryFree(buf);
3,742,110✔
1532
  taosMemoryFree(pKey);
3,740,822✔
1533

1534
  taosMemoryFree(pCursor);
3,740,365✔
1535

1536
  return code;
3,741,292✔
1537
}
1538

1539
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
40,548,199✔
1540
  int ret = 0;
40,548,199✔
1541
  if (lock) {
40,548,199✔
UNCOV
1542
    metaRLock(pMeta);
×
1543
  }
1544

1545
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
40,548,199✔
1546
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
40,548,199✔
1547
  if (lock) {
40,547,573✔
UNCOV
1548
    metaULock(pMeta);
×
1549
  }
1550

1551
  return ret;
40,547,573✔
1552
}
1553

1554
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
5,397,755✔
1555
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
5,397,755✔
1556
  const int32_t LIMIT = 128;
5,397,755✔
1557

1558
  int32_t isLock = false;
5,397,755✔
1559
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
5,397,755✔
1560
  for (int i = 0; i < sz; i++) {
45,943,013✔
1561
    STUidTagInfo *p = taosArrayGet(uidList, i);
40,546,250✔
1562

1563
    if (i % LIMIT == 0) {
40,549,437✔
1564
      if (isLock) metaULock(pMeta);
4,702,236✔
1565

1566
      metaRLock(pMeta);
4,702,236✔
1567
      isLock = true;
4,702,236✔
1568
    }
1569

1570
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1571
    void   *val = NULL;
40,549,437✔
1572
    int32_t len = 0;
40,548,329✔
1573
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
40,548,329✔
1574
      p->pTagVal = taosMemoryMalloc(len);
40,544,564✔
1575
      if (!p->pTagVal) {
40,546,939✔
UNCOV
1576
        if (isLock) metaULock(pMeta);
×
1577

UNCOV
1578
        TAOS_RETURN(terrno);
×
1579
      }
1580
      memcpy(p->pTagVal, val, len);
40,545,962✔
1581
      tdbFree(val);
40,546,439✔
1582
    } else {
1583
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
2,436✔
1584
    }
1585
  }
1586
  //  }
1587
  if (isLock) metaULock(pMeta);
5,396,763✔
1588
  return 0;
5,396,965✔
1589
}
1590

1591
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
20,314,416✔
1592
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
20,314,416✔
1593
  if (!pCur) {
20,313,103✔
UNCOV
1594
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1595
  }
1596

1597
  while (1) {
148,346,580✔
1598
    tb_uid_t uid = metaCtbCursorNext(pCur);
168,659,683✔
1599
    if (uid == 0) {
168,649,707✔
1600
      break;
20,315,569✔
1601
    }
1602

1603
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
148,334,138✔
1604
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
148,334,398✔
1605
    if (!info.pTagVal) {
148,324,779✔
1606
      metaCloseCtbCursor(pCur);
×
UNCOV
1607
      return terrno;
×
1608
    }
1609
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
148,324,779✔
1610
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
148,347,581✔
1611
      taosMemoryFreeClear(info.pTagVal);
×
1612
      metaCloseCtbCursor(pCur);
×
UNCOV
1613
      return terrno;
×
1614
    }
1615
  }
1616
  metaCloseCtbCursor(pCur);
20,315,569✔
1617
  return TSDB_CODE_SUCCESS;
20,311,112✔
1618
}
1619

1620
int32_t metaFlagCache(SVnode *pVnode) {
×
1621
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1622
  if (!pCur) {
×
UNCOV
1623
    return terrno;
×
1624
  }
1625

1626
  SArray *suids = NULL;
×
1627
  while (1) {
×
1628
    tb_uid_t id = metaStbCursorNext(pCur);
×
1629
    if (id == 0) {
×
UNCOV
1630
      break;
×
1631
    }
1632

1633
    if (!suids) {
×
1634
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1635
      if (!suids) {
×
UNCOV
1636
        return terrno;
×
1637
      }
1638
    }
1639

1640
    if (taosArrayPush(suids, &id) == NULL) {
×
1641
      taosArrayDestroy(suids);
×
UNCOV
1642
      return terrno;
×
1643
    }
1644
  }
1645

UNCOV
1646
  metaCloseStbCursor(pCur);
×
1647

1648
  for (int idx = 0; suids && idx < TARRAY_SIZE(suids); ++idx) {
×
1649
    tb_uid_t id = ((tb_uid_t *)TARRAY_DATA(suids))[idx];
×
1650
    STsdb   *pTsdb = pVnode->pTsdb;
×
1651
    SMeta   *pMeta = pVnode->pMeta;
×
UNCOV
1652
    SArray  *uids = NULL;
×
1653

1654
    int32_t code = metaGetChildUidsOfSuperTable(pMeta, id, &uids);
×
1655
    if (code) {
×
UNCOV
1656
      metaError("vgId:%d, failed to get subtables, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1657

1658
      taosArrayDestroy(uids);
×
UNCOV
1659
      taosArrayDestroy(suids);
×
1660

UNCOV
1661
      return code;
×
1662
    }
1663

1664
    if (uids && TARRAY_SIZE(uids) > 0) {
×
UNCOV
1665
      STSchema *pTSchema = NULL;
×
1666

1667
      code = metaGetTbTSchemaEx(pMeta, id, id, -1, &pTSchema);
×
1668
      if (code) {
×
UNCOV
1669
        metaError("vgId:%d, failed to get schema, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1670

1671
        taosArrayDestroy(uids);
×
UNCOV
1672
        taosArrayDestroy(suids);
×
1673

UNCOV
1674
        return code;
×
1675
      }
1676

1677
      int32_t nCol = pTSchema->numOfCols;
×
1678
      for (int32_t i = 0; i < nCol; ++i) {
×
1679
        int16_t cid = pTSchema->columns[i].colId;
×
UNCOV
1680
        int8_t  col_type = pTSchema->columns[i].type;
×
1681

1682
        code = tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type);
×
1683
        if (code) {
×
UNCOV
1684
          metaError("vgId:%d, failed to flag cache, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
1685

1686
          tDestroyTSchema(pTSchema);
×
1687
          taosArrayDestroy(uids);
×
UNCOV
1688
          taosArrayDestroy(suids);
×
1689

UNCOV
1690
          return code;
×
1691
        }
1692
      }
1693

UNCOV
1694
      tDestroyTSchema(pTSchema);
×
1695
    }
1696

UNCOV
1697
    taosArrayDestroy(uids);
×
1698
  }
1699

UNCOV
1700
  taosArrayDestroy(suids);
×
1701

UNCOV
1702
  return TSDB_CODE_SUCCESS;
×
1703
}
1704

1705
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1706

1707
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
1,641,888,647✔
1708
  int32_t code = 0;
1,641,888,647✔
1709
  void   *pData = NULL;
1,641,888,647✔
1710
  int     nData = 0;
1,641,995,380✔
1711
  int     lock = 0;
1,642,093,784✔
1712

1713
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
1,642,093,784✔
1714
    lock = 1;
372,634,116✔
1715
  }
1716

1717
  if (!lock) metaRLock(pMeta);
1,642,093,751✔
1718

1719
  // search cache
1720
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
1,642,120,346✔
1721
    if (!lock) metaULock(pMeta);
1,554,115,725✔
1722
    goto _exit;
1,554,066,554✔
1723
  }
1724

1725
  // search TDB
1726
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
88,021,113✔
1727
    // not found
1728
    if (!lock) metaULock(pMeta);
79,185,393✔
1729
    goto _exit;
79,179,884✔
1730
  }
1731

1732
  if (!lock) metaULock(pMeta);
8,852,263✔
1733

1734
  pInfo->uid = uid;
8,852,263✔
1735
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
8,852,108✔
1736
  pInfo->version = ((SUidIdxVal *)pData)->version;
8,851,679✔
1737
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
8,852,150✔
1738

1739
  if (lock) {
8,851,290✔
1740
    metaULock(pReader->pMeta);
743,155✔
1741
    // metaReaderReleaseLock(pReader);
1742
  }
1743
  // upsert the cache
1744
  metaWLock(pMeta);
8,852,759✔
1745
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
8,851,563✔
1746
  if (ret != 0) {
8,851,669✔
UNCOV
1747
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1748
  }
1749
  metaULock(pMeta);
8,851,669✔
1750

1751
  if (lock) {
8,851,676✔
1752
    metaRLock(pReader->pMeta);
744,251✔
1753
  }
1754

1755
_exit:
1,641,928,992✔
1756
  tdbFree(pData);
1,642,074,968✔
1757
  return code;
1,642,048,461✔
1758
}
1759

1760
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
194,289,307✔
1761
  int32_t code = 0;
194,289,307✔
1762

1763
  if (!numOfTables && !numOfCols) goto _exit;
194,289,307✔
1764

1765
  SVnode *pVnodeObj = pVnode;
194,289,307✔
1766
  metaRLock(pVnodeObj->pMeta);
194,289,307✔
1767

1768
  // fast path: search cache
1769
  SMetaStbStats state = {0};
194,289,144✔
1770
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
194,292,853✔
1771
    metaULock(pVnodeObj->pMeta);
187,274,398✔
1772
    if (numOfTables) *numOfTables = state.ctbNum;
187,257,516✔
1773
    if (numOfCols) *numOfCols = state.colNum;
187,258,824✔
1774
    if (flags) *flags = state.flags;
187,259,051✔
1775
    goto _exit;
187,259,705✔
1776
  }
1777

1778
  // slow path: search TDB
1779
  int64_t ctbNum = 0;
7,036,057✔
1780
  int32_t colNum = 0;
7,037,077✔
1781
  int64_t keep = 0;
7,037,257✔
1782
  int8_t  flag = 0;
7,037,152✔
1783

1784
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
7,037,152✔
1785
  if (TSDB_CODE_SUCCESS == code) {
7,037,155✔
1786
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
7,037,155✔
1787
  }
1788
  if (TSDB_CODE_SUCCESS == code) {
7,037,196✔
1789
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
7,037,196✔
1790
  }
1791
  metaULock(pVnodeObj->pMeta);
7,037,325✔
1792
  if (TSDB_CODE_SUCCESS != code) {
7,037,325✔
UNCOV
1793
    goto _exit;
×
1794
  }
1795

1796
  if (numOfTables) *numOfTables = ctbNum;
7,037,325✔
1797
  if (numOfCols) *numOfCols = colNum;
7,037,325✔
1798
  if (flags) *flags = flag;
7,037,043✔
1799

1800
  state.uid = uid;
7,037,043✔
1801
  state.ctbNum = ctbNum;
7,037,043✔
1802
  state.colNum = colNum;
7,037,043✔
1803
  state.flags = flag;
7,037,043✔
1804
  state.keep = keep;
7,037,043✔
1805
  // upsert the cache
1806
  metaWLock(pVnodeObj->pMeta);
7,037,043✔
1807

1808
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
7,037,118✔
1809
  if (ret) {
7,036,502✔
UNCOV
1810
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1811
              uid, ctbNum, colNum, keep, flag);
1812
  }
1813

1814
  metaULock(pVnodeObj->pMeta);
7,036,502✔
1815

1816
_exit:
194,297,105✔
1817
  return code;
194,308,479✔
1818
}
1819

1820
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
70,727,343✔
1821
  SMetaStbStats stats = {0};
70,727,343✔
1822

1823
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
70,732,224✔
1824
    stats.ctbNum += deltaCtb;
60,063,215✔
1825
    stats.colNum += deltaCol;
60,063,215✔
1826
    if (deltaKeep > 0) {
60,063,215✔
1827
      stats.keep = deltaKeep;
5,826✔
1828
    }
1829

1830
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
60,063,215✔
1831
    if (code) {
60,011,552✔
UNCOV
1832
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1833
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1834
    }
1835
  }
1836
}
70,700,762✔
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