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

taosdata / TDengine / #4856

17 Nov 2025 09:53AM UTC coverage: 64.286% (+0.2%) from 64.039%
#4856

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

4657 existing lines in 112 files now uncovered.

151658 of 235910 relevant lines covered (64.29%)

116320814.6 hits per line

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

60.9
/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) {
526,212,028✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
526,212,028✔
21
  metaReaderDoInit(pReader, pMeta, flags);
526,239,244✔
22
  pReader->pAPI = pAPI;
526,235,646✔
23
}
526,237,448✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
610,211,940✔
26
  memset(pReader, 0, sizeof(*pReader));
610,211,940✔
27
  pReader->pMeta = pMeta;
610,211,940✔
28
  pReader->flags = flags;
610,197,141✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
610,195,719✔
30
    metaRLock(pMeta);
428,092,779✔
31
  }
32
}
610,236,772✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
228,687,283✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
228,687,283✔
36
    metaULock(pReader->pMeta);
228,717,401✔
37
    pReader->flags |= META_READER_NOLOCK;
228,621,464✔
38
  }
39
}
228,726,538✔
40

41
void metaReaderClear(SMetaReader *pReader) {
641,747,843✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
641,747,843✔
43
    metaULock(pReader->pMeta);
199,292,998✔
44
  }
45
  tDecoderClear(&pReader->coder);
641,823,918✔
46
  tdbFree(pReader->pBuf);
641,826,027✔
47
  pReader->pBuf = NULL;
641,748,185✔
48
}
641,700,662✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
980,452,280✔
51
  int32_t  code = 0;
980,452,280✔
52
  SMeta   *pMeta = pReader->pMeta;
980,452,280✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
980,500,262✔
54

55
  // query table.db
56
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
980,500,242✔
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);
980,427,590✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
980,411,305✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
980,480,335✔
65
  if (code) {
980,283,061✔
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
980,283,061✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
60,679,337✔
79
    metaULock(pVnodeObj->pMeta);
4,874,227✔
80
    return false;
4,874,227✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
55,809,329✔
84
  return true;
55,809,851✔
85
}
86

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

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

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
276,125,534✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
276,128,297✔
98
}
99

NEW
100
int metaReaderGetTableEntryByVersionUid(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
×
NEW
101
  if (version < 0) {
×
NEW
102
    return metaReaderGetTableEntryByUid(pReader, uid);
×
103
  }
NEW
104
  return metaGetTableEntryByVersion(pReader, version, uid);
×
105
}
106

107
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
344,113,080✔
108
  SMeta *pMeta = pReader->pMeta;
344,113,080✔
109

110
  SMetaInfo info;
344,056,940✔
111
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
344,113,201✔
112
  if (TSDB_CODE_SUCCESS != code) {
344,119,235✔
UNCOV
113
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
114
  }
115

116
  return metaGetTableEntryByVersion(pReader, info.version, uid);
344,119,235✔
117
}
118

119
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
65,592,656✔
120
  SMeta   *pMeta = pReader->pMeta;
65,592,656✔
121
  tb_uid_t uid;
122

123
  // query name.idx
124
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
65,593,461✔
125
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
11,301,926✔
126
  }
127

128
  uid = *(tb_uid_t *)pReader->pBuf;
54,289,599✔
129
  return metaReaderGetTableEntryByUid(pReader, uid);
54,289,486✔
130
}
131

132
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
58,353,483✔
133
  void    *pData = NULL;
58,353,483✔
134
  int      nData = 0;
58,354,521✔
135
  tb_uid_t uid = 0;
58,354,187✔
136

137
  metaRLock(pMeta);
58,354,187✔
138

139
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
58,348,626✔
140
    uid = *(tb_uid_t *)pData;
9,429,411✔
141
    tdbFree(pData);
9,429,411✔
142
  }
143

144
  metaULock(pMeta);
58,352,534✔
145

146
  return uid;
58,350,500✔
147
}
148

149
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
7,373,177✔
150
  int         code = 0;
7,373,177✔
151
  SMetaReader mr = {0};
7,373,177✔
152
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
7,373,177✔
153
  code = metaReaderGetTableEntryByUid(&mr, uid);
7,372,611✔
154
  if (code < 0) {
7,367,693✔
UNCOV
155
    metaReaderClear(&mr);
×
UNCOV
156
    return code;
×
157
  }
158

159
  STR_TO_VARSTR(tbName, mr.me.name);
7,367,693✔
160
  metaReaderClear(&mr);
7,365,995✔
161

162
  return 0;
7,372,982✔
163
}
164

UNCOV
165
int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
×
UNCOV
166
  int         code = 0;
×
UNCOV
167
  SMetaReader mr = {0};
×
UNCOV
168
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
UNCOV
169
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
UNCOV
170
  if (code < 0) {
×
UNCOV
171
    metaReaderClear(&mr);
×
172
    return code;
×
173
  }
174
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
175
  metaReaderClear(&mr);
×
176

177
  return 0;
×
178
}
179

180
int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) {
707,304✔
181
  int         code = 0;
707,304✔
182
  SMetaReader mr = {0};
707,304✔
183
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
707,304✔
184

185
  SMetaReader *pReader = &mr;
707,304✔
186

187
  // query name.idx
188
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
707,304✔
189
    metaReaderClear(&mr);
267,389✔
190
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
267,389✔
191
  }
192

193
  *uid = *(tb_uid_t *)pReader->pBuf;
439,915✔
194

195
  metaReaderClear(&mr);
439,915✔
196

197
  return 0;
439,915✔
198
}
199

200
int metaGetTableTypeSuidByName(void *pVnode, char *tbName, ETableType *tbType, uint64_t *suid) {
444,621✔
201
  int         code = 0;
444,621✔
202
  SMetaReader mr = {0};
444,621✔
203
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
444,621✔
204

205
  code = metaGetTableEntryByName(&mr, tbName);
444,621✔
206
  if (code == 0) *tbType = mr.me.type;
443,967✔
207
  if (TSDB_CHILD_TABLE == mr.me.type) {
443,967✔
208
    *suid = mr.me.ctbEntry.suid;
437,733✔
209
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
6,234✔
210
    *suid = mr.me.uid;
5,822✔
211
  } else {
212
    *suid = 0;
412✔
213
  }
214

215
  metaReaderClear(&mr);
444,621✔
216
  return code;
444,621✔
217
}
218

UNCOV
219
int metaReadNext(SMetaReader *pReader) {
×
UNCOV
220
  SMeta *pMeta = pReader->pMeta;
×
221

222
  // TODO
223

UNCOV
224
  return 0;
×
225
}
226

227
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
UNCOV
228
  int         code = -1;
×
UNCOV
229
  SMetaReader mr = {0};
×
UNCOV
230
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
231
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
UNCOV
232
  if (code < 0) {
×
UNCOV
233
    goto _exit;
×
234
  }
235
  if (mr.me.type == TSDB_CHILD_TABLE) {
×
236
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
237
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
×
238
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
239
  } else {
240
    goto _exit;
×
241
  }
242

243
  code = 0;
×
244

245
_exit:
×
UNCOV
246
  metaReaderClear(&mr);
×
247
  return code;
×
248
}
249

250
#if 1  // ===================================================
251
SMTbCursor *metaOpenTbCursor(void *pVnode) {
10,471,764✔
252
  SMTbCursor *pTbCur = NULL;
10,471,764✔
253
  int32_t     code;
254

255
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
10,471,764✔
256
  if (pTbCur == NULL) {
10,471,533✔
UNCOV
257
    return NULL;
×
258
  }
259

260
  SVnode *pVnodeObj = pVnode;
10,471,533✔
261
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
262
  pTbCur->pMeta = pVnodeObj->pMeta;
10,471,533✔
263
  pTbCur->paused = 1;
10,470,933✔
264
  code = metaResumeTbCursor(pTbCur, 1, 0);
10,471,531✔
265
  if (code) {
10,471,489✔
UNCOV
266
    terrno = code;
×
UNCOV
267
    taosMemoryFree(pTbCur);
×
UNCOV
268
    return NULL;
×
269
  }
270
  return pTbCur;
10,471,489✔
271
}
272

273
void metaCloseTbCursor(SMTbCursor *pTbCur) {
20,931,844✔
274
  if (pTbCur) {
20,931,844✔
275
    tdbFree(pTbCur->pKey);
10,470,561✔
276
    tdbFree(pTbCur->pVal);
10,470,915✔
277
    if (!pTbCur->paused) {
10,471,567✔
278
      metaReaderClear(&pTbCur->mr);
4,861,102✔
279
      if (pTbCur->pDbc) {
4,861,102✔
280
        tdbTbcClose((TBC *)pTbCur->pDbc);
4,861,102✔
281
      }
282
    }
283
    taosMemoryFree(pTbCur);
10,469,909✔
284
  }
285
}
20,931,876✔
286

287
void metaPauseTbCursor(SMTbCursor *pTbCur) {
5,910,101✔
288
  if (!pTbCur->paused) {
5,910,101✔
289
    metaReaderClear(&pTbCur->mr);
5,910,101✔
290
    tdbTbcClose((TBC *)pTbCur->pDbc);
5,909,869✔
291
    pTbCur->paused = 1;
5,909,869✔
292
  }
293
}
5,909,637✔
294
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
10,770,859✔
295
  int32_t code = 0;
10,770,859✔
296
  int32_t lino;
297
  int8_t  locked = 0;
10,770,859✔
298
  if (pTbCur->paused) {
10,770,859✔
299
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
10,768,012✔
300
    locked = 1;
10,769,873✔
301
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
10,769,873✔
302
    if (code != 0) {
10,770,793✔
UNCOV
303
      TSDB_CHECK_CODE(code, lino, _exit);
×
304
    }
305

306
    if (first) {
10,770,793✔
307
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
10,471,721✔
308
      TSDB_CHECK_CODE(code, lino, _exit);
10,469,205✔
309
    } else {
310
      int c = 1;
299,072✔
311
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
299,072✔
312
      TSDB_CHECK_CODE(code, lino, _exit);
299,072✔
313
      if (c == 0) {
299,072✔
314
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
299,072✔
UNCOV
315
      } else if (c < 0) {
×
UNCOV
316
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
UNCOV
317
        TSDB_CHECK_CODE(code, lino, _exit);
×
318
      } else {
UNCOV
319
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
UNCOV
320
        TSDB_CHECK_CODE(code, lino, _exit);
×
321
      }
322
    }
323

324
    pTbCur->paused = 0;
10,768,007✔
325
  }
326

327
_exit:
×
328
  if (code != 0 && locked) {
10,768,968✔
UNCOV
329
    metaReaderReleaseLock(&pTbCur->mr);
×
330
  }
331
  return code;
10,766,415✔
332
}
333

334
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
370,683,410✔
335
  int    ret;
336
  void  *pBuf;
337
  STbCfg tbCfg;
338

339
  for (;;) {
340
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
370,683,410✔
341
    if (ret < 0) {
370,686,592✔
342
      return ret;
10,462,023✔
343
    }
344

345
    tDecoderClear(&pTbCur->mr.coder);
360,224,569✔
346

347
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
360,231,123✔
348
    if (ret) return ret;
360,214,752✔
349

350
    if (pTbCur->mr.me.type == jumpTableType) {
360,214,752✔
351
      continue;
3,759,577✔
352
    }
353

354
    break;
356,464,889✔
355
  }
356

357
  return 0;
356,464,889✔
358
}
359

UNCOV
360
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
361
  int    ret;
362
  void  *pBuf;
363
  STbCfg tbCfg;
364

365
  for (;;) {
UNCOV
366
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
367
    if (ret < 0) {
×
UNCOV
368
      return -1;
×
369
    }
370

UNCOV
371
    tDecoderClear(&pTbCur->mr.coder);
×
372

373
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
374
    if (ret < 0) {
×
375
      return ret;
×
376
    }
377

378
    if (pTbCur->mr.me.type == jumpTableType) {
×
UNCOV
379
      continue;
×
380
    }
381

382
    break;
×
383
  }
384

385
  return 0;
×
386
}
387

388
/**
389
 * @param type 0x01 fetchRsmaSchema if table is rsma
390
 */
391
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
165,756,939✔
392
                                   int8_t type) {
393
  int32_t         code = 0;
165,756,939✔
394
  void           *pData = NULL;
165,756,939✔
395
  int             nData = 0;
165,760,728✔
396
  int64_t         version;
397
  SSchemaWrapper  schema = {0};
165,752,052✔
398
  SSchemaWrapper *pSchema = NULL;
165,707,792✔
399
  SDecoder        dc = {0};
165,707,792✔
400
  if (lock) {
165,625,518✔
401
    metaRLock(pMeta);
158,951,794✔
402
  }
403
_query:
177,606,908✔
404
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
177,627,890✔
405
    goto _err;
268,117✔
406
  }
407

408
  version = ((SUidIdxVal *)pData)[0].version;
177,381,201✔
409

410
  if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData)) !=
177,346,677✔
411
      0) {
UNCOV
412
    goto _err;
×
413
  }
414

415
  SMetaEntry me = {0};
177,393,844✔
416
  tDecoderInit(&dc, pData, nData);
177,355,003✔
417
  code = metaDecodeEntry(&dc, &me);
177,387,362✔
418
  if (code) {
177,345,395✔
419
    tDecoderClear(&dc);
×
UNCOV
420
    goto _err;
×
421
  }
422
  if (me.type == TSDB_SUPER_TABLE) {
177,345,395✔
423
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
133,322,510✔
424
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
133,283,522✔
425
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
133,283,522✔
426
      if (TABLE_IS_ROLLUP(me.flags)) {
133,283,871✔
427
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
193,792✔
UNCOV
428
          tDecoderClear(&dc);
×
UNCOV
429
          goto _err;
×
430
        }
431
      }
432
      tDecoderClear(&dc);
133,283,871✔
433
      goto _exit;
133,271,365✔
434
    }
435
  } else if (me.type == TSDB_CHILD_TABLE) {
44,022,885✔
436
    uid = me.ctbEntry.suid;
11,855,137✔
437
    tDecoderClear(&dc);
11,855,137✔
438
    goto _query;
11,866,489✔
439
  } else {
440
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
32,167,748✔
441
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
32,164,450✔
442
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
32,164,450✔
443
      tDecoderClear(&dc);
32,164,472✔
444
      goto _exit;
32,162,034✔
445
    }
446
  }
447
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
37,153✔
448
  tDecoderClear(&dc);
37,153✔
449

450
  // query from skm db
451
  if ((code = tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) < 0) {
40,043✔
UNCOV
452
    goto _err;
×
453
  }
454

455
  tDecoderInit(&dc, pData, nData);
40,043✔
456
  if ((code = tDecodeSSchemaWrapperEx(&dc, &schema)) != 0) {
40,043✔
UNCOV
457
    goto _err;
×
458
  }
459
  pSchema = tCloneSSchemaWrapper(&schema);
40,043✔
460
  if (pSchema == NULL) {
40,043✔
UNCOV
461
    code = terrno;
×
UNCOV
462
    goto _err;
×
463
  }
464
  tDecoderClear(&dc);
40,043✔
465

466
_exit:
165,473,442✔
467
  if (lock) {
165,493,919✔
468
    metaULock(pMeta);
158,823,840✔
469
  }
470
  tdbFree(pData);
165,464,482✔
471
  return pSchema;
165,473,371✔
472

473
_err:
269,889✔
474
  if (lock) {
268,117✔
475
    metaULock(pMeta);
266,907✔
476
  }
477
  tdbFree(pData);
268,117✔
478
  tDeleteSchemaWrapper(pSchema);
479
  if (extSchema != NULL) {
268,117✔
480
    taosMemoryFreeClear(*extSchema);
39,383✔
481
  }
482
  terrno = code;
268,117✔
483
  return NULL;
268,117✔
484
}
485

486
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
11,268✔
487
  void    *pData = NULL;
11,268✔
488
  int      nData = 0;
11,268✔
489
  int64_t  version = 0;
11,268✔
490
  SDecoder dc = {0};
11,268✔
491
  int64_t  createTime = INT64_MAX;
11,268✔
492
  if (lock) {
11,268✔
493
    metaRLock(pMeta);
11,268✔
494
  }
495

496
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
11,268✔
UNCOV
497
    goto _exit;
×
498
  }
499

500
  version = ((SUidIdxVal *)pData)[0].version;
11,268✔
501

502
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
11,268✔
UNCOV
503
    goto _exit;
×
504
  }
505

506
  SMetaEntry me = {0};
11,268✔
507
  tDecoderInit(&dc, pData, nData);
11,268✔
508
  int32_t code = metaDecodeEntry(&dc, &me);
11,268✔
509
  if (code) {
11,268✔
510
    tDecoderClear(&dc);
×
UNCOV
511
    goto _exit;
×
512
  }
513
  if (me.type == TSDB_CHILD_TABLE) {
11,268✔
514
    createTime = me.ctbEntry.btime;
11,220✔
515
  } else if (me.type == TSDB_NORMAL_TABLE) {
48✔
516
    createTime = me.ntbEntry.btime;
48✔
517
  }
518
  tDecoderClear(&dc);
11,268✔
519

520
_exit:
11,268✔
521
  if (lock) {
11,268✔
522
    metaULock(pMeta);
11,268✔
523
  }
524
  tdbFree(pData);
11,268✔
525
  return createTime;
11,268✔
526
}
527

528
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
87,316,050✔
529
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
87,316,050✔
530
  SMCtbCursor *pCtbCur = NULL;
87,316,979✔
531
  SCtbIdxKey   ctbIdxKey;
532
  int          ret = 0;
87,316,979✔
533
  int          c = 0;
87,316,979✔
534

535
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
87,316,979✔
536
  if (pCtbCur == NULL) {
87,213,446✔
UNCOV
537
    return NULL;
×
538
  }
539

540
  pCtbCur->pMeta = pMeta;
87,213,446✔
541
  pCtbCur->suid = uid;
87,214,045✔
542
  pCtbCur->lock = lock;
87,230,394✔
543
  pCtbCur->paused = 1;
87,312,812✔
544

545
  ret = metaResumeCtbCursor(pCtbCur, 1);
87,296,062✔
546
  if (ret < 0) {
87,326,599✔
UNCOV
547
    return NULL;
×
548
  }
549
  return pCtbCur;
87,326,599✔
550
}
551

552
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
87,340,300✔
553
  if (pCtbCur) {
87,340,300✔
554
    if (!pCtbCur->paused) {
87,346,027✔
555
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
85,536,533✔
556
      if (pCtbCur->pCur) {
85,519,399✔
557
        tdbTbcClose(pCtbCur->pCur);
85,531,955✔
558
      }
559
    }
560
    tdbFree(pCtbCur->pKey);
87,325,975✔
561
    tdbFree(pCtbCur->pVal);
87,319,993✔
562
  }
563
  taosMemoryFree(pCtbCur);
87,312,109✔
564
}
87,313,312✔
565

566
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
1,812,555✔
567
  if (!pCtbCur->paused) {
1,812,555✔
568
    tdbTbcClose((TBC *)pCtbCur->pCur);
1,812,831✔
569
    if (pCtbCur->lock) {
1,811,528✔
570
      metaULock(pCtbCur->pMeta);
1,812,165✔
571
    }
572
    pCtbCur->paused = 1;
1,811,957✔
573
  }
574
}
1,813,283✔
575

576
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
87,301,715✔
577
  if (pCtbCur->paused) {
87,301,715✔
578
    pCtbCur->paused = 0;
87,280,568✔
579

580
    if (pCtbCur->lock) {
87,313,986✔
581
      metaRLock(pCtbCur->pMeta);
80,585,731✔
582
    }
583
    int ret = 0;
87,286,522✔
584
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
87,286,522✔
585
    if (ret < 0) {
87,298,489✔
UNCOV
586
      metaCloseCtbCursor(pCtbCur);
×
UNCOV
587
      return -1;
×
588
    }
589

590
    if (first) {
87,298,568✔
591
      SCtbIdxKey ctbIdxKey;
87,269,762✔
592
      // move to the suid
593
      ctbIdxKey.suid = pCtbCur->suid;
87,304,510✔
594
      ctbIdxKey.uid = INT64_MIN;
87,321,512✔
595
      int c = 0;
87,321,512✔
596
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
87,276,882✔
597
      if (c > 0) {
87,333,904✔
598
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
9,345,443✔
599
      }
600
    } else {
UNCOV
601
      int c = 0;
×
UNCOV
602
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
UNCOV
603
      if (c < 0) {
×
UNCOV
604
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
605
      } else {
UNCOV
606
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
607
      }
608
    }
609
  }
610
  return 0;
87,334,984✔
611
}
612

613
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
484,706,759✔
614
  int         ret;
615
  SCtbIdxKey *pCtbIdxKey;
616

617
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
484,706,759✔
618
  if (ret < 0) {
484,713,933✔
619
    return 0;
69,677,225✔
620
  }
621

622
  pCtbIdxKey = pCtbCur->pKey;
415,036,708✔
623
  if (pCtbIdxKey->suid > pCtbCur->suid) {
415,036,013✔
624
    return 0;
17,706,127✔
625
  }
626

627
  return pCtbIdxKey->uid;
397,355,445✔
628
}
629

630
struct SMStbCursor {
631
  SMeta   *pMeta;
632
  TBC     *pCur;
633
  tb_uid_t suid;
634
  void    *pKey;
635
  void    *pVal;
636
  int      kLen;
637
  int      vLen;
638
};
639

640
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
56,250,806✔
641
  SMStbCursor *pStbCur = NULL;
56,250,806✔
642
  int          ret = 0;
56,250,806✔
643
  int          c = 0;
56,250,806✔
644

645
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
56,250,157✔
646
  if (pStbCur == NULL) {
56,248,732✔
UNCOV
647
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
648
    return NULL;
×
649
  }
650

651
  pStbCur->pMeta = pMeta;
56,248,732✔
652
  pStbCur->suid = suid;
56,248,732✔
653
  metaRLock(pMeta);
56,249,381✔
654

655
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
56,252,104✔
656
  if (ret < 0) {
56,247,386✔
UNCOV
657
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
658
    metaULock(pMeta);
×
UNCOV
659
    taosMemoryFree(pStbCur);
×
UNCOV
660
    return NULL;
×
661
  }
662

663
  // move to the suid
664
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
56,247,386✔
665
  if (c > 0) {
56,253,402✔
666
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
667
  }
668

669
  return pStbCur;
56,252,104✔
670
}
671

672
void metaCloseStbCursor(SMStbCursor *pStbCur) {
56,251,455✔
673
  if (pStbCur) {
56,251,455✔
674
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
56,252,104✔
675
    if (pStbCur->pCur) {
56,253,402✔
676
      tdbTbcClose(pStbCur->pCur);
56,252,753✔
677

678
      tdbFree(pStbCur->pKey);
56,251,368✔
679
      tdbFree(pStbCur->pVal);
56,250,030✔
680
    }
681

682
    taosMemoryFree(pStbCur);
56,250,030✔
683
  }
684
}
56,250,806✔
685

686
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
174,131,459✔
687
  int ret;
688

689
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
174,131,459✔
690
  if (ret < 0) {
174,135,480✔
691
    return 0;
56,253,402✔
692
  }
693
  return *(tb_uid_t *)pStbCur->pKey;
117,882,078✔
694
}
695

696
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
145,537,499✔
697
  STSchema       *pTSchema = NULL;
145,537,499✔
698
  SSchemaWrapper *pSW = NULL;
145,537,499✔
699

700
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
145,537,499✔
701
  if (!pSW) return NULL;
145,527,444✔
702

703
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
145,307,200✔
704

705
  tDeleteSchemaWrapper(pSW);
706
  return pTSchema;
145,258,544✔
707
}
708

709
/**
710
 * Fetch rsma schema if table is rsma
711
 */
712
SRSchema *metaGetTbTSchemaR(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
54,504✔
713
  SRSchema       *pRSchema = NULL;
54,504✔
714
  SSchemaWrapper *pSW = NULL;
54,504✔
715

716
  if (!(pRSchema = (SRSchema *)taosMemoryCalloc(1, sizeof(SRSchema)))) goto _err;
54,504✔
717
  if (!(pSW = metaGetTableSchema(pMeta, uid, sver, lock, (SExtSchema **)&pRSchema->extSchema, 0x01))) goto _err;
54,504✔
718
  if (!(pRSchema->tSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version))) goto _err;
54,504✔
719

720
  if (pSW->pRsma) {
54,504✔
721
    if (!(pRSchema->funcIds = taosMemoryCalloc(pSW->nCols, sizeof(func_id_t)))) goto _err;
54,504✔
722
    memcpy(pRSchema->funcIds, pSW->pRsma->funcIds, pSW->nCols * sizeof(func_id_t));
54,504✔
723

724
    pRSchema->tbType = pSW->pRsma->tbType;
54,504✔
725
    pRSchema->tbUid = uid;
54,504✔
726
    tstrncpy(pRSchema->tbName, pSW->pRsma->tbName, TSDB_TABLE_NAME_LEN);
54,504✔
727
    pRSchema->interval[0] = pSW->pRsma->interval[0];
54,504✔
728
    pRSchema->interval[1] = pSW->pRsma->interval[1];
54,504✔
729
  }
730

731
_exit:
54,504✔
732
  tDeleteSchemaWrapper(pSW);
733
  return pRSchema;
54,504✔
UNCOV
734
_err:
×
735
  tDeleteSchemaWrapper(pSW);
736
  tFreeSRSchema(&pRSchema);
UNCOV
737
  return NULL;
×
738
}
739

740
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
407,021✔
741
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
407,021✔
742
  if (*ppTSchema == NULL) {
407,021✔
UNCOV
743
    return terrno;
×
744
  }
745
  return TSDB_CODE_SUCCESS;
406,559✔
746
}
747

748
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
138,197,341✔
749
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
138,197,341✔
750
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
138,126,539✔
UNCOV
751
    return terrno;
×
752
  }
753
  return TSDB_CODE_SUCCESS;
138,183,404✔
754
}
755

756
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
41,318,288✔
757
  int32_t code = 0;
41,318,288✔
758
  int32_t lino;
759

760
  void     *pData = NULL;
41,318,288✔
761
  int       nData = 0;
41,327,864✔
762
  SSkmDbKey skmDbKey;
41,314,325✔
763
  if (sver <= 0) {
41,323,562✔
764
    SMetaInfo info;
21,502,025✔
765
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
21,512,661✔
766
      sver = info.skmVer;
21,510,168✔
767
    } else {
768
      TBC *pSkmDbC = NULL;
5,394✔
769
      int  c;
5,980✔
770

771
      skmDbKey.uid = suid ? suid : uid;
5,980✔
772
      skmDbKey.sver = INT32_MAX;
5,980✔
773

774
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
5,980✔
775
      TSDB_CHECK_CODE(code, lino, _exit);
5,980✔
776
      metaRLock(pMeta);
5,980✔
777

778
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
5,980✔
UNCOV
779
        metaULock(pMeta);
×
UNCOV
780
        tdbTbcClose(pSkmDbC);
×
UNCOV
781
        code = TSDB_CODE_NOT_FOUND;
×
UNCOV
782
        goto _exit;
×
783
      }
784

785
      if (c == 0) {
5,980✔
786
        metaULock(pMeta);
×
787
        tdbTbcClose(pSkmDbC);
×
788
        code = TSDB_CODE_FAILED;
×
789
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
UNCOV
790
        goto _exit;
×
791
      }
792

793
      if (c < 0) {
5,980✔
794
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
780✔
795
      }
796

797
      const void *pKey = NULL;
5,980✔
798
      int32_t     nKey = 0;
5,980✔
799
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
5,980✔
800

801
      if (ret != 0 || ((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
5,980✔
UNCOV
802
        metaULock(pMeta);
×
UNCOV
803
        tdbTbcClose(pSkmDbC);
×
UNCOV
804
        code = TSDB_CODE_NOT_FOUND;
×
UNCOV
805
        goto _exit;
×
806
      }
807

808
      sver = ((SSkmDbKey *)pKey)->sver;
5,980✔
809

810
      metaULock(pMeta);
5,980✔
811
      tdbTbcClose(pSkmDbC);
5,980✔
812
    }
813
  }
814

815
  if (!(sver > 0)) {
41,323,681✔
UNCOV
816
    code = TSDB_CODE_NOT_FOUND;
×
UNCOV
817
    goto _exit;
×
818
  }
819

820
  skmDbKey.uid = suid ? suid : uid;
41,323,681✔
821
  skmDbKey.sver = sver;
41,323,681✔
822
  metaRLock(pMeta);
41,323,681✔
823
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
41,331,256✔
824
    metaULock(pMeta);
2,584✔
825
    code = TSDB_CODE_NOT_FOUND;
2,584✔
826
    goto _exit;
2,584✔
827
  }
828
  metaULock(pMeta);
41,316,296✔
829

830
  // decode
831
  SDecoder        dc = {0};
41,325,233✔
832
  SSchemaWrapper  schema;
41,308,788✔
833
  SSchemaWrapper *pSchemaWrapper = &schema;
41,328,312✔
834

835
  tDecoderInit(&dc, pData, nData);
41,328,312✔
836
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
41,340,253✔
837
  tDecoderClear(&dc);
41,340,253✔
838
  tdbFree(pData);
41,330,235✔
839
  if (TSDB_CODE_SUCCESS != code) {
41,319,413✔
UNCOV
840
    taosMemoryFree(pSchemaWrapper->pSchema);
×
UNCOV
841
    goto _exit;
×
842
  }
843

844
  // convert
845
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
41,319,413✔
846
  if (pTSchema == NULL) {
41,331,813✔
847
    code = TSDB_CODE_OUT_OF_MEMORY;
×
848
  }
849

850
  *ppTSchema = pTSchema;
41,331,813✔
851
  taosMemoryFree(pSchemaWrapper->pSchema);
41,334,476✔
852

853
_exit:
41,331,374✔
854
  return code;
41,336,251✔
855
}
856

857
// N.B. Called by statusReq per second
858
int64_t metaGetTbNum(SMeta *pMeta) {
118,196,437✔
859
  // num of child tables (excluding normal tables , stables and others)
860

861
  /* int64_t num = 0; */
862
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
863

864
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
118,196,437✔
865
}
866

867
void metaUpdTimeSeriesNum(SMeta *pMeta) {
56,251,008✔
868
  int64_t nCtbTimeSeries = 0;
56,251,008✔
869
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
56,251,657✔
870
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
56,251,008✔
871
  }
872
}
56,253,604✔
873

874
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
875
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
876
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
186,341,734✔
877
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
186,339,750✔
878
    metaUpdTimeSeriesNum(pMeta);
56,247,376✔
879
  }
880

881
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
186,348,715✔
882
}
883

884
// type: 1 reported timeseries
885
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
186,337,029✔
886
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
186,335,264✔
887
  if (type == 1) {
186,335,264✔
888
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
118,196,437✔
889
  }
890
  return nTimeSeries;
186,333,504✔
891
}
892

893
typedef struct {
894
  SMeta   *pMeta;
895
  TBC     *pCur;
896
  tb_uid_t uid;
897
  void    *pKey;
898
  void    *pVal;
899
  int      kLen;
900
  int      vLen;
901
} SMSmaCursor;
902

UNCOV
903
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
UNCOV
904
  SMSmaCursor *pSmaCur = NULL;
×
UNCOV
905
  SSmaIdxKey   smaIdxKey;
×
906
  int          ret;
UNCOV
907
  int          c;
×
908

UNCOV
909
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
910
  if (pSmaCur == NULL) {
×
911
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
912
    return NULL;
×
913
  }
914

UNCOV
915
  pSmaCur->pMeta = pMeta;
×
916
  pSmaCur->uid = uid;
×
917
  metaRLock(pMeta);
×
918

919
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
UNCOV
920
  if (ret < 0) {
×
UNCOV
921
    metaULock(pMeta);
×
922
    taosMemoryFree(pSmaCur);
×
923
    return NULL;
×
924
  }
925

926
  // move to the suid
927
  smaIdxKey.uid = uid;
×
928
  smaIdxKey.smaUid = INT64_MIN;
×
929
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
930
  if (c > 0) {
×
UNCOV
931
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
932
  }
933

934
  return pSmaCur;
×
935
}
936

937
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
938
  if (pSmaCur) {
×
UNCOV
939
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
UNCOV
940
    if (pSmaCur->pCur) {
×
941
      tdbTbcClose(pSmaCur->pCur);
×
UNCOV
942
      pSmaCur->pCur = NULL;
×
943

944
      tdbFree(pSmaCur->pKey);
×
945
      tdbFree(pSmaCur->pVal);
×
946
    }
947

948
    taosMemoryFree(pSmaCur);
×
949
  }
UNCOV
950
}
×
951

952
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
953
  int         ret;
954
  SSmaIdxKey *pSmaIdxKey;
955

UNCOV
956
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
957
  if (ret < 0) {
×
UNCOV
958
    return 0;
×
959
  }
960

UNCOV
961
  pSmaIdxKey = pSmaCur->pKey;
×
UNCOV
962
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
963
    return 0;
×
964
  }
965

UNCOV
966
  return pSmaIdxKey->uid;
×
967
}
968

969
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
970
  STSmaWrapper *pSW = NULL;
×
UNCOV
971
  SArray       *pSmaIds = NULL;
×
972

973
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
UNCOV
974
    return NULL;
×
975
  }
976

977
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
978
  if (!pSW) {
×
UNCOV
979
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
980
    goto _err;
×
981
  }
982

UNCOV
983
  pSW->number = taosArrayGetSize(pSmaIds);
×
984
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
985

986
  if (!pSW->tSma) {
×
987
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
988
    goto _err;
×
989
  }
990

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

UNCOV
1028
    ++smaIdx;
×
1029
  }
1030

1031
  if (smaIdx <= 0) goto _err;
×
1032
  pSW->number = smaIdx;
×
1033

UNCOV
1034
  metaReaderClear(&mr);
×
1035
  taosArrayDestroy(pSmaIds);
×
UNCOV
1036
  return pSW;
×
UNCOV
1037
_err:
×
1038
  metaReaderClear(&mr);
×
1039
  taosArrayDestroy(pSmaIds);
×
UNCOV
1040
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
1041
  return NULL;
×
1042
}
1043

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

1060
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1061

1062
  metaReaderClear(&mr);
×
1063
  return pTSma;
×
1064
}
1065

UNCOV
1066
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1067
  SArray     *pUids = NULL;
×
UNCOV
1068
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1069

1070
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
UNCOV
1071
  if (!pCur) {
×
UNCOV
1072
    return NULL;
×
1073
  }
1074

1075
  while (1) {
×
UNCOV
1076
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1077
    if (id == 0) {
×
1078
      break;
×
1079
    }
1080

UNCOV
1081
    if (!pUids) {
×
1082
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1083
      if (!pUids) {
×
1084
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1085
        metaCloseSmaCursor(pCur);
×
UNCOV
1086
        return NULL;
×
1087
      }
1088
    }
1089

1090
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1091

1092
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1093
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1094
      metaCloseSmaCursor(pCur);
×
UNCOV
1095
      taosArrayDestroy(pUids);
×
UNCOV
1096
      return NULL;
×
1097
    }
1098
  }
1099

1100
  metaCloseSmaCursor(pCur);
×
1101
  return pUids;
×
1102
}
1103

UNCOV
1104
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
UNCOV
1105
  SArray     *pUids = NULL;
×
UNCOV
1106
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1107
  tb_uid_t    lastUid = 0;
×
1108

UNCOV
1109
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
UNCOV
1110
  if (!pCur) {
×
1111
    return NULL;
×
1112
  }
1113

1114
  while (1) {
×
UNCOV
1115
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1116
    if (uid == 0) {
×
1117
      break;
×
1118
    }
1119

UNCOV
1120
    if (lastUid == uid) {
×
1121
      continue;
×
1122
    }
1123

1124
    lastUid = uid;
×
1125

UNCOV
1126
    if (!pUids) {
×
1127
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1128
      if (!pUids) {
×
UNCOV
1129
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1130
        metaCloseSmaCursor(pCur);
×
1131
        return NULL;
×
1132
      }
1133
    }
1134

1135
    if (!taosArrayPush(pUids, &uid)) {
×
1136
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1137
      metaCloseSmaCursor(pCur);
×
1138
      taosArrayDestroy(pUids);
×
UNCOV
1139
      return NULL;
×
1140
    }
1141
  }
1142

1143
  metaCloseSmaCursor(pCur);
×
1144
  return pUids;
×
1145
}
1146

1147
#endif
1148

1149
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
635,812,007✔
1150
  STag *tag = (STag *)pTag;
635,812,007✔
1151
  if (type == TSDB_DATA_TYPE_JSON) {
635,812,007✔
1152
    return tag;
1,502,165✔
1153
  }
1154
  bool find = tTagGet(tag, val);
634,309,842✔
1155

1156
  if (!find) {
634,374,625✔
1157
    return NULL;
5,369,719✔
1158
  }
1159

1160
  return val;
629,004,906✔
1161
}
1162

1163
typedef struct {
1164
  SMeta   *pMeta;
1165
  TBC     *pCur;
1166
  tb_uid_t suid;
1167
  int16_t  cid;
1168
  int16_t  type;
1169
  void    *pKey;
1170
  void    *pVal;
1171
  int32_t  kLen;
1172
  int32_t  vLen;
1173
} SIdxCursor;
1174

1175
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
2,732✔
1176
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
2,732✔
1177
  SMetaFltParam *param = arg;
2,732✔
1178
  int32_t        ret = 0;
2,732✔
1179

1180
  SIdxCursor *pCursor = NULL;
2,732✔
1181
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
2,732✔
1182
  if (pCursor == NULL) {
2,732✔
UNCOV
1183
    return terrno;
×
1184
  }
1185
  pCursor->pMeta = pMeta;
2,732✔
1186
  pCursor->suid = param->suid;
2,732✔
1187
  pCursor->cid = param->cid;
2,732✔
1188
  pCursor->type = param->type;
2,732✔
1189

1190
  metaRLock(pMeta);
2,732✔
1191
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
2,732✔
1192
  if (ret != 0) {
2,732✔
UNCOV
1193
    goto END;
×
1194
  }
1195
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
2,732✔
1196

1197
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
2,732✔
1198
  SBtimeIdxKey *pBtimeKey = &btimeKey;
2,732✔
1199

1200
  int cmp = 0;
2,732✔
1201
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
2,732✔
UNCOV
1202
    goto END;
×
1203
  }
1204

1205
  int32_t valid = 0;
2,732✔
1206
  int32_t count = 0;
2,732✔
1207

1208
  static const int8_t TRY_ERROR_LIMIT = 1;
1209
  do {
13,408,438✔
1210
    void   *entryKey = NULL;
13,411,170✔
1211
    int32_t nEntryKey = -1;
13,398,066✔
1212
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
13,410,162✔
1213
    if (valid < 0) break;
13,430,658✔
1214

1215
    SBtimeIdxKey *p = entryKey;
13,427,926✔
1216
    if (count > TRY_ERROR_LIMIT) break;
13,427,926✔
1217

1218
    terrno = TSDB_CODE_SUCCESS;
13,427,926✔
1219
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
13,409,110✔
1220
    if (terrno != TSDB_CODE_SUCCESS) {
13,388,950✔
UNCOV
1221
      ret = terrno;
×
UNCOV
1222
      break;
×
1223
    }
1224
    if (cmp == 0) {
13,354,678✔
1225
      if (taosArrayPush(pUids, &p->uid) == NULL) {
26,740,940✔
UNCOV
1226
        ret = terrno;
×
UNCOV
1227
        break;
×
1228
      }
1229
    } else {
UNCOV
1230
      if (param->equal == true) {
×
UNCOV
1231
        if (count > TRY_ERROR_LIMIT) break;
×
UNCOV
1232
        count++;
×
1233
      }
1234
    }
1235
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
13,386,262✔
1236
    if (valid < 0) break;
13,405,750✔
1237
  } while (1);
1238

1239
END:
2,060✔
1240
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
2,732✔
1241
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
2,732✔
1242
  taosMemoryFree(pCursor);
2,732✔
1243
  return ret;
2,732✔
1244
}
1245

UNCOV
1246
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
UNCOV
1247
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
UNCOV
1248
  SMetaFltParam *param = arg;
×
UNCOV
1249
  int32_t        ret = 0;
×
UNCOV
1250
  char          *buf = NULL;
×
1251

UNCOV
1252
  STagIdxKey *pKey = NULL;
×
1253
  int32_t     nKey = 0;
×
1254

1255
  SIdxCursor *pCursor = NULL;
×
1256
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1257
  if (pCursor == NULL) {
×
UNCOV
1258
    return terrno;
×
1259
  }
1260
  pCursor->pMeta = pMeta;
×
UNCOV
1261
  pCursor->suid = param->suid;
×
1262
  pCursor->cid = param->cid;
×
1263
  pCursor->type = param->type;
×
1264

1265
  char *pName = param->val;
×
1266

1267
  metaRLock(pMeta);
×
1268
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1269
  if (ret != 0) {
×
1270
    goto END;
×
1271
  }
1272

UNCOV
1273
  int cmp = 0;
×
1274
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1275
    goto END;
×
1276
  }
1277
  int32_t valid = 0;
×
UNCOV
1278
  int32_t count = 0;
×
1279

1280
  int32_t TRY_ERROR_LIMIT = 1;
×
1281
  do {
×
1282
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
UNCOV
1283
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1284
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1285
    if (valid < 0) break;
×
1286

1287
    if (count > TRY_ERROR_LIMIT) break;
×
1288

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

UNCOV
1314
END:
×
1315
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1316
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1317
  taosMemoryFree(buf);
×
UNCOV
1318
  taosMemoryFree(pKey);
×
1319

UNCOV
1320
  taosMemoryFree(pCursor);
×
1321

1322
  return ret;
×
1323
}
1324
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1325
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
UNCOV
1326
  SMetaFltParam *param = arg;
×
1327
  int32_t        ret = 0;
×
UNCOV
1328
  char          *buf = NULL;
×
1329

UNCOV
1330
  STtlIdxKey *pKey = NULL;
×
1331
  int32_t     nKey = 0;
×
1332

1333
  SIdxCursor *pCursor = NULL;
×
1334
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1335
  if (pCursor == NULL) {
×
UNCOV
1336
    return terrno;
×
1337
  }
1338
  pCursor->pMeta = pMeta;
×
UNCOV
1339
  pCursor->suid = param->suid;
×
1340
  pCursor->cid = param->cid;
×
1341
  pCursor->type = param->type;
×
1342

1343
  metaRLock(pMeta);
×
1344
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1345

1346
END:
×
1347
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1348
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
UNCOV
1349
  taosMemoryFree(buf);
×
1350
  taosMemoryFree(pKey);
×
1351

UNCOV
1352
  taosMemoryFree(pCursor);
×
1353

1354
  return ret;
×
1355
  // impl later
1356
  return 0;
1357
}
1358
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
3,517,304✔
1359
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,517,304✔
1360
  SMetaFltParam *param = arg;
3,518,199✔
1361

1362
  SMetaEntry oStbEntry = {0};
3,518,199✔
1363
  int32_t    code = 0;
3,512,384✔
1364
  char      *buf = NULL;
3,512,384✔
1365
  void      *pData = NULL;
3,512,384✔
1366
  int        nData = 0;
3,511,607✔
1367

1368
  SDecoder    dc = {0};
3,510,664✔
1369
  STbDbKey    tbDbKey = {0};
3,510,222✔
1370
  STagIdxKey *pKey = NULL;
3,513,521✔
1371
  int32_t     nKey = 0;
3,517,413✔
1372

1373
  SIdxCursor *pCursor = NULL;
3,519,640✔
1374
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,519,640✔
1375
  if (!pCursor) {
3,510,220✔
UNCOV
1376
    return terrno;
×
1377
  }
1378
  pCursor->pMeta = pMeta;
3,510,220✔
1379
  pCursor->suid = param->suid;
3,515,702✔
1380
  pCursor->cid = param->cid;
3,514,583✔
1381
  pCursor->type = param->type;
3,517,808✔
1382

1383
  metaRLock(pMeta);
3,513,350✔
1384

1385
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
3,515,080✔
1386

1387
  tbDbKey.uid = param->suid;
3,518,581✔
1388
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,519,230✔
1389

1390
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
3,517,261✔
1391

1392
  tDecoderInit(&dc, pData, nData);
3,518,825✔
1393

1394
  code = metaDecodeEntry(&dc, &oStbEntry);
3,519,669✔
1395
  if (code) {
3,517,489✔
UNCOV
1396
    tDecoderClear(&dc);
×
UNCOV
1397
    goto END;
×
1398
  }
1399

1400
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
3,517,489✔
UNCOV
1401
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1402
  }
1403

1404
  code = TSDB_CODE_INVALID_PARA;
3,517,489✔
1405

1406
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,721,407✔
1407
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,721,407✔
1408
    if (IS_IDX_ON(schema)) {
4,720,985✔
1409
      if (schema->colId == param->cid && param->type == schema->type) {
4,713,698✔
1410
        code = 0;
3,516,573✔
1411
        break;
3,516,573✔
1412
      }
1413
    }
1414
  }
1415

1416
  TAOS_CHECK_GOTO(code, NULL, END);
3,516,573✔
1417

1418
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,516,573✔
1419
  if (code != 0) {
3,518,109✔
UNCOV
1420
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1421
  }
1422

1423
  int32_t maxSize = 0;
3,518,109✔
1424
  int32_t nTagData = 0;
3,518,109✔
1425
  void   *tagData = NULL;
3,518,109✔
1426

1427
  if (param->val == NULL) {
3,518,109✔
UNCOV
1428
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
UNCOV
1429
    goto END;
×
1430
  } else {
1431
    if (IS_VAR_DATA_TYPE(param->type)) {
3,514,596✔
1432
      tagData = varDataVal(param->val);
509,556✔
1433
      nTagData = varDataLen(param->val);
509,861✔
1434

1435
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
509,393✔
1436
        maxSize = 4 * nTagData + 1;
35,881✔
1437
        buf = taosMemoryCalloc(1, maxSize);
35,881✔
1438
        if (buf == NULL) {
35,881✔
UNCOV
1439
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1440
        }
1441

1442
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
35,881✔
UNCOV
1443
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1444
        }
1445

1446
        tagData = buf;
35,881✔
1447
        nTagData = maxSize;
35,881✔
1448
      }
1449
    } else {
1450
      tagData = param->val;
3,007,992✔
1451
      nTagData = tDataTypes[param->type].bytes;
3,008,203✔
1452
    }
1453
  }
1454

1455
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
3,517,670✔
1456
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1457
                  NULL, END);
1458

1459
  int cmp = 0;
3,517,670✔
1460
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,518,109✔
1461

1462
  int     count = 0;
3,520,108✔
1463
  int32_t valid = 0;
3,520,108✔
1464
  bool    found = false;
3,520,108✔
1465

1466
  static const int8_t TRY_ERROR_LIMIT = 1;
1467

1468
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1469
  /// target:                        [suid, cid2, type2]
1470
  int diffCidCount = 0;
3,520,108✔
1471
  do {
35,173,315✔
1472
    void   *entryKey = NULL, *entryVal = NULL;
38,693,423✔
1473
    int32_t nEntryKey, nEntryVal;
38,687,057✔
1474

1475
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
38,692,655✔
1476
    if (valid < 0) {
38,696,798✔
1477
      break;
1,625,446✔
1478
    }
1479
    if (count > TRY_ERROR_LIMIT) {
37,071,352✔
1480
      break;
1,145,557✔
1481
    }
1482

1483
    STagIdxKey *p = entryKey;
35,925,795✔
1484
    if (p == NULL) break;
35,925,795✔
1485

1486
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
35,925,795✔
1487
      if (found == true) break;  //
1,142,049✔
1488
      if (diffCidCount > TRY_ERROR_LIMIT) break;
392,944✔
1489
      diffCidCount++;
392,944✔
1490
      count++;
392,944✔
1491
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
392,944✔
1492
      if (valid < 0) {
392,944✔
UNCOV
1493
        code = valid;
×
UNCOV
1494
        break;
×
1495
      } else {
1496
        continue;
392,944✔
1497
      }
1498
    }
1499

1500
    terrno = TSDB_CODE_SUCCESS;
34,781,360✔
1501
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
34,780,921✔
1502
    if (terrno != TSDB_CODE_SUCCESS) {
34,779,359✔
UNCOV
1503
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
UNCOV
1504
      break;
×
1505
    }
1506
    if (cmp == 0) {
34,781,118✔
1507
      // match
1508
      tb_uid_t tuid = 0;
31,034,419✔
1509
      if (IS_VAR_DATA_TYPE(pKey->type)) {
31,034,419✔
1510
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
308,047✔
1511
      } else {
1512
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
30,724,923✔
1513
      }
1514
      if (taosArrayPush(pUids, &tuid) == NULL) {
31,037,486✔
UNCOV
1515
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1516
      }
1517
      found = true;
31,037,486✔
1518
    } else {
1519
      if (param->equal == true) {
3,746,699✔
1520
        if (count > TRY_ERROR_LIMIT) break;
2,667,661✔
1521
        count++;
2,667,661✔
1522
      }
1523
    }
1524
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
34,785,502✔
1525
    if (valid < 0) {
34,781,723✔
UNCOV
1526
      code = valid;
×
UNCOV
1527
      break;
×
1528
    }
1529
  } while (1);
1530

1531
END:
3,519,626✔
1532
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,520,108✔
1533
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,519,669✔
1534
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,519,227✔
1535
  tDecoderClear(&dc);
3,519,227✔
1536
  tdbFree(pData);
3,520,108✔
1537

1538
  taosMemoryFree(buf);
3,520,108✔
1539
  taosMemoryFree(pKey);
3,520,108✔
1540

1541
  taosMemoryFree(pCursor);
3,518,791✔
1542

1543
  return code;
3,517,872✔
1544
}
1545

1546
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
39,181,451✔
1547
  int ret = 0;
39,181,451✔
1548
  if (lock) {
39,181,451✔
UNCOV
1549
    metaRLock(pMeta);
×
1550
  }
1551

1552
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
39,181,451✔
1553
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
39,182,691✔
1554
  if (lock) {
39,178,050✔
UNCOV
1555
    metaULock(pMeta);
×
1556
  }
1557

1558
  return ret;
39,178,050✔
1559
}
1560

1561
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
5,189,074✔
1562
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
5,189,074✔
1563
  const int32_t LIMIT = 128;
5,189,074✔
1564

1565
  int32_t isLock = false;
5,189,074✔
1566
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
5,189,074✔
1567
  for (int i = 0; i < sz; i++) {
44,371,323✔
1568
    STUidTagInfo *p = taosArrayGet(uidList, i);
39,182,688✔
1569

1570
    if (i % LIMIT == 0) {
39,182,688✔
1571
      if (isLock) metaULock(pMeta);
4,507,128✔
1572

1573
      metaRLock(pMeta);
4,507,128✔
1574
      isLock = true;
4,506,766✔
1575
    }
1576

1577
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1578
    void   *val = NULL;
39,182,326✔
1579
    int32_t len = 0;
39,181,887✔
1580
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
39,181,813✔
1581
      p->pTagVal = taosMemoryMalloc(len);
39,167,337✔
1582
      if (!p->pTagVal) {
39,168,201✔
UNCOV
1583
        if (isLock) metaULock(pMeta);
×
1584

UNCOV
1585
        TAOS_RETURN(terrno);
×
1586
      }
1587
      memcpy(p->pTagVal, val, len);
39,167,839✔
1588
      tdbFree(val);
39,168,201✔
1589
    } else {
1590
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
11,152✔
1591
    }
1592
  }
1593
  //  }
1594
  if (isLock) metaULock(pMeta);
5,188,635✔
1595
  return 0;
5,189,074✔
1596
}
1597

1598
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
19,908,658✔
1599
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
19,908,658✔
1600
  if (!pCur) {
19,906,241✔
UNCOV
1601
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1602
  }
1603

1604
  while (1) {
142,410,037✔
1605
    tb_uid_t uid = metaCtbCursorNext(pCur);
162,316,278✔
1606
    if (uid == 0) {
162,287,605✔
1607
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
19,906,884✔
1608
      break;
19,906,884✔
1609
    }
1610

1611
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
142,380,721✔
1612
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
142,385,594✔
1613
    if (!info.pTagVal) {
142,394,946✔
UNCOV
1614
      metaCloseCtbCursor(pCur);
×
UNCOV
1615
      return terrno;
×
1616
    }
1617
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
142,394,946✔
1618
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
142,409,749✔
UNCOV
1619
      taosMemoryFreeClear(info.pTagVal);
×
UNCOV
1620
      metaCloseCtbCursor(pCur);
×
UNCOV
1621
      return terrno;
×
1622
    }
1623
  }
1624
  metaCloseCtbCursor(pCur);
19,907,257✔
1625
  return TSDB_CODE_SUCCESS;
19,905,388✔
1626
}
1627

1628
int32_t metaFlagCache(SVnode *pVnode) {
×
1629
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
UNCOV
1630
  if (!pCur) {
×
UNCOV
1631
    return terrno;
×
1632
  }
1633

UNCOV
1634
  SArray *suids = NULL;
×
UNCOV
1635
  while (1) {
×
1636
    tb_uid_t id = metaStbCursorNext(pCur);
×
1637
    if (id == 0) {
×
1638
      break;
×
1639
    }
1640

UNCOV
1641
    if (!suids) {
×
1642
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1643
      if (!suids) {
×
1644
        return terrno;
×
1645
      }
1646
    }
1647

UNCOV
1648
    if (taosArrayPush(suids, &id) == NULL) {
×
1649
      taosArrayDestroy(suids);
×
1650
      return terrno;
×
1651
    }
1652
  }
1653

UNCOV
1654
  metaCloseStbCursor(pCur);
×
1655

1656
  for (int idx = 0; suids && idx < TARRAY_SIZE(suids); ++idx) {
×
1657
    tb_uid_t id = ((tb_uid_t *)TARRAY_DATA(suids))[idx];
×
1658
    STsdb   *pTsdb = pVnode->pTsdb;
×
UNCOV
1659
    SMeta   *pMeta = pVnode->pMeta;
×
UNCOV
1660
    SArray  *uids = NULL;
×
1661

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

1666
      taosArrayDestroy(uids);
×
1667
      taosArrayDestroy(suids);
×
1668

UNCOV
1669
      return code;
×
1670
    }
1671

1672
    if (uids && TARRAY_SIZE(uids) > 0) {
×
UNCOV
1673
      STSchema *pTSchema = NULL;
×
1674

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

UNCOV
1679
        taosArrayDestroy(uids);
×
1680
        taosArrayDestroy(suids);
×
1681

UNCOV
1682
        return code;
×
1683
      }
1684

1685
      int32_t nCol = pTSchema->numOfCols;
×
UNCOV
1686
      for (int32_t i = 0; i < nCol; ++i) {
×
1687
        int16_t cid = pTSchema->columns[i].colId;
×
1688
        int8_t  col_type = pTSchema->columns[i].type;
×
1689

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

1694
          tDestroyTSchema(pTSchema);
×
1695
          taosArrayDestroy(uids);
×
1696
          taosArrayDestroy(suids);
×
1697

1698
          return code;
×
1699
        }
1700
      }
1701

1702
      tDestroyTSchema(pTSchema);
×
1703
    }
1704

UNCOV
1705
    taosArrayDestroy(uids);
×
1706
  }
1707

UNCOV
1708
  taosArrayDestroy(suids);
×
1709

1710
  return TSDB_CODE_SUCCESS;
×
1711
}
1712

1713
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1714

1715
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
1,579,659,371✔
1716
  int32_t code = 0;
1,579,659,371✔
1717
  void   *pData = NULL;
1,579,659,371✔
1718
  int     nData = 0;
1,579,757,817✔
1719
  int     lock = 0;
1,579,801,209✔
1720

1721
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
1,579,801,209✔
1722
    lock = 1;
344,153,722✔
1723
  }
1724

1725
  if (!lock) metaRLock(pMeta);
1,579,795,620✔
1726

1727
  // search cache
1728
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
1,579,770,143✔
1729
    if (!lock) metaULock(pMeta);
1,495,052,842✔
1730
    goto _exit;
1,495,025,463✔
1731
  }
1732

1733
  // search TDB
1734
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
84,771,565✔
1735
    // not found
1736
    if (!lock) metaULock(pMeta);
75,999,266✔
1737
    goto _exit;
75,993,312✔
1738
  }
1739

1740
  if (!lock) metaULock(pMeta);
8,775,883✔
1741

1742
  pInfo->uid = uid;
8,775,883✔
1743
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
8,775,883✔
1744
  pInfo->version = ((SUidIdxVal *)pData)->version;
8,775,883✔
1745
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
8,775,883✔
1746

1747
  if (lock) {
8,775,883✔
1748
    metaULock(pReader->pMeta);
702,638✔
1749
    // metaReaderReleaseLock(pReader);
1750
  }
1751
  // upsert the cache
1752
  metaWLock(pMeta);
8,775,883✔
1753
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
8,775,883✔
1754
  if (ret != 0) {
8,775,883✔
UNCOV
1755
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1756
  }
1757
  metaULock(pMeta);
8,775,883✔
1758

1759
  if (lock) {
8,775,883✔
1760
    metaRLock(pReader->pMeta);
702,638✔
1761
  }
1762

1763
_exit:
1,579,634,765✔
1764
  tdbFree(pData);
1,579,796,606✔
1765
  return code;
1,579,764,815✔
1766
}
1767

1768
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
182,932,088✔
1769
  int32_t code = 0;
182,932,088✔
1770

1771
  if (!numOfTables && !numOfCols) goto _exit;
182,932,088✔
1772

1773
  SVnode *pVnodeObj = pVnode;
182,932,088✔
1774
  metaRLock(pVnodeObj->pMeta);
182,932,088✔
1775

1776
  // fast path: search cache
1777
  SMetaStbStats state = {0};
182,925,012✔
1778
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
182,930,786✔
1779
    metaULock(pVnodeObj->pMeta);
176,263,384✔
1780
    if (numOfTables) *numOfTables = state.ctbNum;
176,268,177✔
1781
    if (numOfCols) *numOfCols = state.colNum;
176,268,826✔
1782
    if (flags) *flags = state.flags;
176,260,901✔
1783
    goto _exit;
176,262,199✔
1784
  }
1785

1786
  // slow path: search TDB
1787
  int64_t ctbNum = 0;
6,671,920✔
1788
  int32_t colNum = 0;
6,673,757✔
1789
  int64_t keep = 0;
6,674,285✔
1790
  int8_t  flag = 0;
6,673,870✔
1791

1792
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
6,674,033✔
1793
  if (TSDB_CODE_SUCCESS == code) {
6,674,349✔
1794
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
6,674,442✔
1795
  }
1796
  if (TSDB_CODE_SUCCESS == code) {
6,674,256✔
1797
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
6,674,349✔
1798
  }
1799
  metaULock(pVnodeObj->pMeta);
6,674,200✔
1800
  if (TSDB_CODE_SUCCESS != code) {
6,673,720✔
UNCOV
1801
    goto _exit;
×
1802
  }
1803

1804
  if (numOfTables) *numOfTables = ctbNum;
6,673,720✔
1805
  if (numOfCols) *numOfCols = colNum;
6,673,720✔
1806
  if (flags) *flags = flag;
6,673,970✔
1807

1808
  state.uid = uid;
6,673,970✔
1809
  state.ctbNum = ctbNum;
6,673,970✔
1810
  state.colNum = colNum;
6,673,970✔
1811
  state.flags = flag;
6,673,970✔
1812
  state.keep = keep;
6,673,970✔
1813
  // upsert the cache
1814
  metaWLock(pVnodeObj->pMeta);
6,673,970✔
1815

1816
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
6,674,370✔
1817
  if (ret) {
6,673,893✔
UNCOV
1818
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1819
              uid, ctbNum, colNum, keep, flag);
1820
  }
1821

1822
  metaULock(pVnodeObj->pMeta);
6,673,893✔
1823

1824
_exit:
182,936,742✔
1825
  return code;
182,941,458✔
1826
}
1827

1828
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
67,294,735✔
1829
  SMetaStbStats stats = {0};
67,294,735✔
1830

1831
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
67,304,019✔
1832
    stats.ctbNum += deltaCtb;
56,742,513✔
1833
    stats.colNum += deltaCol;
56,742,513✔
1834
    if (deltaKeep > 0) {
56,742,513✔
1835
      stats.keep = deltaKeep;
9,038✔
1836
    }
1837

1838
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
56,742,513✔
1839
    if (code) {
56,734,848✔
UNCOV
1840
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1841
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1842
    }
1843
  }
1844
}
67,295,837✔
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