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

taosdata / TDengine / #4888

17 Dec 2025 07:43AM UTC coverage: 65.281% (-0.008%) from 65.289%
#4888

push

travis-ci

web-flow
test: update ci env (#33959)

178960 of 274136 relevant lines covered (65.28%)

101703773.12 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) {
473,958,635✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
473,958,635✔
21
  metaReaderDoInit(pReader, pMeta, flags);
474,019,127✔
22
  pReader->pAPI = pAPI;
474,001,582✔
23
}
473,999,771✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
554,557,937✔
26
  memset(pReader, 0, sizeof(*pReader));
554,557,937✔
27
  pReader->pMeta = pMeta;
554,557,937✔
28
  pReader->flags = flags;
554,587,864✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
554,592,803✔
30
    metaRLock(pMeta);
380,571,798✔
31
  }
32
}
554,605,428✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
192,350,144✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
192,350,144✔
36
    metaULock(pReader->pMeta);
192,410,506✔
37
    pReader->flags |= META_READER_NOLOCK;
192,248,962✔
38
  }
39
}
192,358,530✔
40

41
void metaReaderClear(SMetaReader *pReader) {
561,839,857✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
561,839,857✔
43
    metaULock(pReader->pMeta);
188,110,516✔
44
  }
45
  tDecoderClear(&pReader->coder);
561,995,857✔
46
  tdbFree(pReader->pBuf);
562,003,833✔
47
  pReader->pBuf = NULL;
561,857,798✔
48
}
561,801,064✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
850,890,577✔
51
  int32_t  code = 0;
850,890,577✔
52
  SMeta   *pMeta = pReader->pMeta;
850,890,577✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
851,040,925✔
54

55
  // query table.db
56
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
851,027,708✔
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);
850,982,752✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
850,992,362✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
851,006,383✔
65
  if (code) {
850,717,880✔
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
850,717,880✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
50,468,339✔
79
    metaULock(pVnodeObj->pMeta);
48,265✔
80
    return false;
48,265✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
50,424,430✔
84
  return true;
50,424,430✔
85
}
86

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

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

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
221,509,277✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
221,522,280✔
98
}
99

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

107
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
349,700,825✔
108
  SMeta *pMeta = pReader->pMeta;
349,700,825✔
109

110
  SMetaInfo info;
349,755,576✔
111
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
349,762,778✔
112
  if (TSDB_CODE_SUCCESS != code) {
349,782,638✔
113
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
1,785✔
114
  }
115

116
  return metaGetTableEntryByVersion(pReader, info.version, uid);
349,780,853✔
117
}
118

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

123
  // query name.idx
124
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
32,761,163✔
125
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
11,540,391✔
126
  }
127

128
  uid = *(tb_uid_t *)pReader->pBuf;
21,220,320✔
129
  return metaReaderGetTableEntryByUid(pReader, uid);
21,219,645✔
130
}
131

132
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
76,003,178✔
133
  void    *pData = NULL;
76,003,178✔
134
  int      nData = 0;
76,003,542✔
135
  tb_uid_t uid = 0;
76,003,793✔
136

137
  metaRLock(pMeta);
76,003,793✔
138

139
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
76,003,009✔
140
    uid = *(tb_uid_t *)pData;
9,864,890✔
141
    tdbFree(pData);
9,864,890✔
142
  }
143

144
  metaULock(pMeta);
75,997,371✔
145

146
  return uid;
75,999,249✔
147
}
148

149
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
7,450,216✔
150
  int         code = 0;
7,450,216✔
151
  SMetaReader mr = {0};
7,450,216✔
152
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
7,450,797✔
153
  code = metaReaderGetTableEntryByUid(&mr, uid);
7,451,379✔
154
  if (code < 0) {
7,448,469✔
155
    metaReaderClear(&mr);
×
156
    return code;
×
157
  }
158

159
  STR_TO_VARSTR(tbName, mr.me.name);
7,448,469✔
160
  metaReaderClear(&mr);
7,447,488✔
161

162
  return 0;
7,451,379✔
163
}
164

165
int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
×
166
  int         code = 0;
×
167
  SMetaReader mr = {0};
×
168
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
169
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
170
  if (code < 0) {
×
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) {
781,349✔
181
  int         code = 0;
781,349✔
182
  SMetaReader mr = {0};
781,349✔
183
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
781,349✔
184

185
  SMetaReader *pReader = &mr;
781,349✔
186

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

193
  *uid = *(tb_uid_t *)pReader->pBuf;
500,765✔
194

195
  metaReaderClear(&mr);
500,765✔
196

197
  return 0;
500,765✔
198
}
199

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

205
  code = metaGetTableEntryByName(&mr, tbName);
504,681✔
206
  if (code == 0) *tbType = mr.me.type;
504,336✔
207
  if (TSDB_CHILD_TABLE == mr.me.type) {
504,336✔
208
    *suid = mr.me.ctbEntry.suid;
497,898✔
209
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
6,438✔
210
    *suid = mr.me.uid;
6,014✔
211
  } else {
212
    *suid = 0;
424✔
213
  }
214

215
  metaReaderClear(&mr);
504,336✔
216
  return code;
504,336✔
217
}
218

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

222
  // TODO
223

224
  return 0;
×
225
}
226

227
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
228
  int         code = -1;
×
229
  SMetaReader mr = {0};
×
230
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
231
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
232
  if (code < 0) {
×
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:
×
246
  metaReaderClear(&mr);
×
247
  return code;
×
248
}
249

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

255
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
4,457,068✔
256
  if (pTbCur == NULL) {
4,457,068✔
257
    return NULL;
×
258
  }
259

260
  SVnode *pVnodeObj = pVnode;
4,457,068✔
261
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
262
  pTbCur->pMeta = pVnodeObj->pMeta;
4,457,068✔
263
  pTbCur->paused = 1;
4,456,391✔
264
  code = metaResumeTbCursor(pTbCur, 1, 0);
4,456,391✔
265
  if (code) {
4,456,692✔
266
    terrno = code;
×
267
    taosMemoryFree(pTbCur);
×
268
    return NULL;
×
269
  }
270
  return pTbCur;
4,456,692✔
271
}
272

273
void metaCloseTbCursor(SMTbCursor *pTbCur) {
8,903,331✔
274
  if (pTbCur) {
8,903,331✔
275
    tdbFree(pTbCur->pKey);
4,456,354✔
276
    tdbFree(pTbCur->pVal);
4,456,310✔
277
    if (!pTbCur->paused) {
4,456,354✔
278
      metaReaderClear(&pTbCur->mr);
793,145✔
279
      if (pTbCur->pDbc) {
793,145✔
280
        tdbTbcClose((TBC *)pTbCur->pDbc);
793,145✔
281
      }
282
    }
283
    taosMemoryFree(pTbCur);
4,455,633✔
284
  }
285
}
8,903,758✔
286

287
void metaPauseTbCursor(SMTbCursor *pTbCur) {
3,665,829✔
288
  if (!pTbCur->paused) {
3,665,829✔
289
    metaReaderClear(&pTbCur->mr);
3,665,829✔
290
    tdbTbcClose((TBC *)pTbCur->pDbc);
3,665,829✔
291
    pTbCur->paused = 1;
3,665,829✔
292
  }
293
}
3,665,829✔
294
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
4,458,290✔
295
  int32_t code = 0;
4,458,290✔
296
  int32_t lino;
297
  int8_t  locked = 0;
4,458,290✔
298
  if (pTbCur->paused) {
4,458,290✔
299
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
4,458,974✔
300
    locked = 1;
4,458,297✔
301
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
4,458,297✔
302
    if (code != 0) {
4,457,613✔
303
      TSDB_CHECK_CODE(code, lino, _exit);
×
304
    }
305

306
    if (first) {
4,457,613✔
307
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
4,455,707✔
308
      TSDB_CHECK_CODE(code, lino, _exit);
4,451,991✔
309
    } else {
310
      int c = 1;
1,906✔
311
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
1,906✔
312
      TSDB_CHECK_CODE(code, lino, _exit);
1,906✔
313
      if (c == 0) {
1,906✔
314
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
1,906✔
315
      } else if (c < 0) {
×
316
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
317
        TSDB_CHECK_CODE(code, lino, _exit);
×
318
      } else {
319
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
320
        TSDB_CHECK_CODE(code, lino, _exit);
×
321
      }
322
    }
323

324
    pTbCur->paused = 0;
4,456,353✔
325
  }
326

327
_exit:
×
328
  if (code != 0 && locked) {
4,454,766✔
329
    metaReaderReleaseLock(&pTbCur->mr);
×
330
  }
331
  return code;
4,455,451✔
332
}
333

334
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
284,143,509✔
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);
284,143,509✔
341
    if (ret < 0) {
284,135,029✔
342
      return ret;
4,447,894✔
343
    }
344

345
    tDecoderClear(&pTbCur->mr.coder);
279,687,135✔
346

347
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
279,692,663✔
348
    if (ret) return ret;
279,691,165✔
349

350
    if (pTbCur->mr.me.type == jumpTableType) {
279,691,165✔
351
      continue;
3,859,516✔
352
    }
353

354
    break;
275,833,910✔
355
  }
356

357
  return 0;
275,833,910✔
358
}
359

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

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

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) {
×
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,
148,661,568✔
392
                                   int8_t type) {
393
  int32_t         code = 0;
148,661,568✔
394
  void           *pData = NULL;
148,661,568✔
395
  int             nData = 0;
148,680,805✔
396
  int64_t         version;
397
  SSchemaWrapper  schema = {0};
148,719,872✔
398
  SSchemaWrapper *pSchema = NULL;
148,696,026✔
399
  SDecoder        dc = {0};
148,696,026✔
400
  if (lock) {
148,631,430✔
401
    metaRLock(pMeta);
141,574,710✔
402
  }
403
_query:
161,797,149✔
404
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
161,826,614✔
405
    goto _err;
242,792✔
406
  }
407

408
  version = ((SUidIdxVal *)pData)[0].version;
161,622,470✔
409

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

415
  SMetaEntry me = {0};
161,623,621✔
416
  tDecoderInit(&dc, pData, nData);
161,571,574✔
417
  code = metaDecodeEntry(&dc, &me);
161,617,531✔
418
  if (code) {
161,490,528✔
419
    tDecoderClear(&dc);
×
420
    goto _err;
×
421
  }
422
  if (me.type == TSDB_SUPER_TABLE) {
161,490,528✔
423
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
127,967,990✔
424
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
127,943,727✔
425
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
127,943,727✔
426
      if (TABLE_IS_ROLLUP(me.flags)) {
127,940,838✔
427
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
99,717✔
428
          tDecoderClear(&dc);
×
429
          goto _err;
×
430
        }
431
      }
432
      tDecoderClear(&dc);
127,940,838✔
433
      goto _exit;
127,926,520✔
434
    }
435
  } else if (me.type == TSDB_CHILD_TABLE) {
33,522,538✔
436
    uid = me.ctbEntry.suid;
12,917,479✔
437
    tDecoderClear(&dc);
12,917,479✔
438
    goto _query;
12,995,493✔
439
  } else {
440
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
20,605,059✔
441
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
20,599,987✔
442
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
20,599,987✔
443
      tDecoderClear(&dc);
20,600,482✔
444
      goto _exit;
20,598,061✔
445
    }
446
  }
447
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
67,546✔
448
  tDecoderClear(&dc);
67,546✔
449

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

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

466
_exit:
148,541,492✔
467
  if (lock) {
148,569,055✔
468
    metaULock(pMeta);
141,526,866✔
469
  }
470
  tdbFree(pData);
148,460,861✔
471
  return pSchema;
148,500,166✔
472

473
_err:
242,573✔
474
  if (lock) {
242,792✔
475
    metaULock(pMeta);
242,792✔
476
  }
477
  tdbFree(pData);
242,730✔
478
  tDeleteSchemaWrapper(pSchema);
479
  if (extSchema != NULL) {
242,730✔
480
    taosMemoryFreeClear(*extSchema);
227,040✔
481
  }
482
  terrno = code;
242,730✔
483
  return NULL;
242,792✔
484
}
485

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

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

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

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

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

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

528
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
69,457,969✔
529
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
69,457,969✔
530
  SMCtbCursor *pCtbCur = NULL;
69,460,186✔
531
  SCtbIdxKey   ctbIdxKey;
532
  int          ret = 0;
69,460,186✔
533
  int          c = 0;
69,460,186✔
534

535
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
69,460,186✔
536
  if (pCtbCur == NULL) {
69,447,976✔
537
    return NULL;
×
538
  }
539

540
  pCtbCur->pMeta = pMeta;
69,447,976✔
541
  pCtbCur->suid = uid;
69,447,976✔
542
  pCtbCur->lock = lock;
69,448,328✔
543
  pCtbCur->paused = 1;
69,457,616✔
544

545
  ret = metaResumeCtbCursor(pCtbCur, 1);
69,454,585✔
546
  if (ret < 0) {
69,453,248✔
547
    return NULL;
×
548
  }
549
  return pCtbCur;
69,453,248✔
550
}
551

552
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
69,449,936✔
553
  if (pCtbCur) {
69,449,936✔
554
    if (!pCtbCur->paused) {
69,456,068✔
555
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
67,597,613✔
556
      if (pCtbCur->pCur) {
67,588,503✔
557
        tdbTbcClose(pCtbCur->pCur);
67,594,455✔
558
      }
559
    }
560
    tdbFree(pCtbCur->pKey);
69,447,126✔
561
    tdbFree(pCtbCur->pVal);
69,439,727✔
562
  }
563
  taosMemoryFree(pCtbCur);
69,435,413✔
564
}
69,437,587✔
565

566
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
1,859,919✔
567
  if (!pCtbCur->paused) {
1,859,919✔
568
    tdbTbcClose((TBC *)pCtbCur->pCur);
1,859,731✔
569
    if (pCtbCur->lock) {
1,859,634✔
570
      metaULock(pCtbCur->pMeta);
1,859,634✔
571
    }
572
    pCtbCur->paused = 1;
1,859,618✔
573
  }
574
}
1,860,215✔
575

576
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
69,458,617✔
577
  if (pCtbCur->paused) {
69,458,617✔
578
    pCtbCur->paused = 0;
69,454,299✔
579

580
    if (pCtbCur->lock) {
69,458,432✔
581
      metaRLock(pCtbCur->pMeta);
62,400,365✔
582
    }
583
    int ret = 0;
69,458,158✔
584
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
69,458,158✔
585
    if (ret < 0) {
69,453,198✔
586
      metaCloseCtbCursor(pCtbCur);
×
587
      return -1;
×
588
    }
589

590
    if (first) {
69,453,198✔
591
      SCtbIdxKey ctbIdxKey;
69,448,888✔
592
      // move to the suid
593
      ctbIdxKey.suid = pCtbCur->suid;
69,454,035✔
594
      ctbIdxKey.uid = INT64_MIN;
69,459,433✔
595
      int c = 0;
69,459,433✔
596
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
69,453,951✔
597
      if (c > 0) {
69,453,227✔
598
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
6,273,185✔
599
      }
600
    } else {
601
      int c = 0;
×
602
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
603
      if (c < 0) {
×
604
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
605
      } else {
606
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
607
      }
608
    }
609
  }
610
  return 0;
69,455,230✔
611
}
612

613
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
469,556,080✔
614
  int         ret;
615
  SCtbIdxKey *pCtbIdxKey;
616

617
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
469,556,080✔
618
  if (ret < 0) {
469,448,206✔
619
    return 0;
48,340,798✔
620
  }
621

622
  pCtbIdxKey = pCtbCur->pKey;
421,107,408✔
623
  if (pCtbIdxKey->suid > pCtbCur->suid) {
421,109,186✔
624
    return 0;
21,151,734✔
625
  }
626

627
  return pCtbIdxKey->uid;
399,978,840✔
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) {
72,481,151✔
641
  SMStbCursor *pStbCur = NULL;
72,481,151✔
642
  int          ret = 0;
72,481,151✔
643
  int          c = 0;
72,481,151✔
644

645
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
72,481,826✔
646
  if (pStbCur == NULL) {
72,475,153✔
647
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
648
    return NULL;
×
649
  }
650

651
  pStbCur->pMeta = pMeta;
72,475,153✔
652
  pStbCur->suid = suid;
72,480,553✔
653
  metaRLock(pMeta);
72,480,553✔
654

655
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
72,480,553✔
656
  if (ret < 0) {
72,475,036✔
657
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
658
    metaULock(pMeta);
×
659
    taosMemoryFree(pStbCur);
×
660
    return NULL;
×
661
  }
662

663
  // move to the suid
664
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
72,475,036✔
665
  if (c > 0) {
72,482,501✔
666
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
667
  }
668

669
  return pStbCur;
72,481,826✔
670
}
671

672
void metaCloseStbCursor(SMStbCursor *pStbCur) {
72,479,160✔
673
  if (pStbCur) {
72,479,160✔
674
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
72,479,160✔
675
    if (pStbCur->pCur) {
72,481,903✔
676
      tdbTbcClose(pStbCur->pCur);
72,481,903✔
677

678
      tdbFree(pStbCur->pKey);
72,479,784✔
679
      tdbFree(pStbCur->pVal);
72,481,151✔
680
    }
681

682
    taosMemoryFree(pStbCur);
72,481,151✔
683
  }
684
}
72,479,878✔
685

686
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
193,727,168✔
687
  int ret;
688

689
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
193,727,168✔
690
  if (ret < 0) {
193,728,509✔
691
    return 0;
72,480,510✔
692
  }
693
  return *(tb_uid_t *)pStbCur->pKey;
121,247,999✔
694
}
695

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

700
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
126,682,152✔
701
  if (!pSW) return NULL;
126,690,403✔
702

703
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
126,683,333✔
704

705
  tDeleteSchemaWrapper(pSW);
706
  return pTSchema;
126,604,053✔
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) {
27,828✔
713
  SRSchema       *pRSchema = NULL;
27,828✔
714
  SSchemaWrapper *pSW = NULL;
27,828✔
715

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

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

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

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

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

748
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
126,095,274✔
749
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
126,095,274✔
750
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
126,026,867✔
751
    return terrno;
×
752
  }
753
  return TSDB_CODE_SUCCESS;
126,082,827✔
754
}
755

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

760
  void     *pData = NULL;
74,881,566✔
761
  int       nData = 0;
75,062,678✔
762
  SSkmDbKey skmDbKey;
75,198,508✔
763
  if (sver <= 0) {
75,140,254✔
764
    SMetaInfo info;
38,344,317✔
765
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
38,378,683✔
766
      sver = info.skmVer;
38,410,108✔
767
    } else {
768
      TBC *pSkmDbC = NULL;
6,242✔
769
      int  c;
7,070✔
770

771
      skmDbKey.uid = suid ? suid : uid;
7,070✔
772
      skmDbKey.sver = INT32_MAX;
7,070✔
773

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

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

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

793
      if (c < 0) {
7,070✔
794
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
795
      }
796

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

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

808
      sver = ((SSkmDbKey *)pKey)->sver;
7,070✔
809

810
      metaULock(pMeta);
7,070✔
811
      tdbTbcClose(pSkmDbC);
7,070✔
812
    }
813
  }
814

815
  if (!(sver > 0)) {
74,964,484✔
816
    code = TSDB_CODE_NOT_FOUND;
×
817
    goto _exit;
×
818
  }
819

820
  skmDbKey.uid = suid ? suid : uid;
74,964,484✔
821
  skmDbKey.sver = sver;
74,964,484✔
822
  metaRLock(pMeta);
74,964,484✔
823
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
75,201,866✔
824
    metaULock(pMeta);
2,720✔
825
    code = TSDB_CODE_NOT_FOUND;
2,720✔
826
    goto _exit;
2,720✔
827
  }
828
  metaULock(pMeta);
74,848,287✔
829

830
  // decode
831
  SDecoder        dc = {0};
75,244,028✔
832
  SSchemaWrapper  schema;
75,270,828✔
833
  SSchemaWrapper *pSchemaWrapper = &schema;
75,229,244✔
834

835
  tDecoderInit(&dc, pData, nData);
75,229,244✔
836
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
75,281,417✔
837
  tDecoderClear(&dc);
75,281,417✔
838
  tdbFree(pData);
75,087,307✔
839
  if (TSDB_CODE_SUCCESS != code) {
74,817,105✔
840
    taosMemoryFree(pSchemaWrapper->pSchema);
×
841
    goto _exit;
×
842
  }
843

844
  // convert
845
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
74,817,105✔
846
  if (pTSchema == NULL) {
75,247,480✔
847
    code = TSDB_CODE_OUT_OF_MEMORY;
×
848
  }
849

850
  *ppTSchema = pTSchema;
75,247,480✔
851
  taosMemoryFree(pSchemaWrapper->pSchema);
75,230,221✔
852

853
_exit:
75,127,445✔
854
  return code;
75,192,047✔
855
}
856

857
// N.B. Called by statusReq per second
858
int64_t metaGetTbNum(SMeta *pMeta) {
105,529,637✔
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;
105,529,637✔
865
}
866

867
void metaUpdTimeSeriesNum(SMeta *pMeta) {
72,469,652✔
868
  int64_t nCtbTimeSeries = 0;
72,469,652✔
869
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
72,470,327✔
870
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
72,468,379✔
871
  }
872
}
72,469,054✔
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;
192,511,796✔
877
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
192,510,931✔
878
    metaUpdTimeSeriesNum(pMeta);
72,462,777✔
879
  }
880

881
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
192,518,557✔
882
}
883

884
// type: 1 reported timeseries
885
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
192,504,075✔
886
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
192,506,270✔
887
  if (type == 1) {
192,506,270✔
888
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
105,529,637✔
889
  }
890
  return nTimeSeries;
192,491,493✔
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

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

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

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

919
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
920
  if (ret < 0) {
×
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) {
×
931
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
932
  }
933

934
  return pSmaCur;
×
935
}
936

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

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

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

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

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

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

966
  return pSmaIdxKey->uid;
×
967
}
968

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

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

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

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;
×
988
    goto _err;
×
989
  }
990

991
  SMetaReader mr = {0};
×
992
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
993
  int64_t smaId;
994
  int     smaIdx = 0;
×
995
  STSma  *pTSma = NULL;
×
996
  for (int i = 0; i < pSW->number; ++i) {
×
997
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
998
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
999
      tDecoderClear(&mr.coder);
×
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))) {
×
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
      }
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;
×
1025
      pTSma->tagsFilter = NULL;
×
1026
    }
1027

1028
    ++smaIdx;
×
1029
  }
1030

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

1034
  metaReaderClear(&mr);
×
1035
  taosArrayDestroy(pSmaIds);
×
1036
  return pSW;
×
1037
_err:
×
1038
  metaReaderClear(&mr);
×
1039
  taosArrayDestroy(pSmaIds);
×
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) {
×
1049
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
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

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

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

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

1081
    if (!pUids) {
×
1082
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1083
      if (!pUids) {
×
1084
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1085
        metaCloseSmaCursor(pCur);
×
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;
×
1094
      metaCloseSmaCursor(pCur);
×
1095
      taosArrayDestroy(pUids);
×
1096
      return NULL;
×
1097
    }
1098
  }
1099

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

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

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

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

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

1124
    lastUid = uid;
×
1125

1126
    if (!pUids) {
×
1127
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1128
      if (!pUids) {
×
1129
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
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);
×
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) {
645,516,878✔
1150
  STag *tag = (STag *)pTag;
645,516,878✔
1151
  if (type == TSDB_DATA_TYPE_JSON) {
645,516,878✔
1152
    return tag;
1,695,451✔
1153
  }
1154
  bool find = tTagGet(tag, val);
643,821,427✔
1155

1156
  if (!find) {
643,887,603✔
1157
    return NULL;
5,252,011✔
1158
  }
1159

1160
  return val;
638,635,592✔
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) {
3,498✔
1176
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,498✔
1177
  SMetaFltParam *param = arg;
3,498✔
1178
  int32_t        ret = 0;
3,498✔
1179

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

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

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

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

1205
  int32_t valid = 0;
3,498✔
1206
  int32_t count = 0;
3,498✔
1207

1208
  static const int8_t TRY_ERROR_LIMIT = 1;
1209
  do {
13,510,533✔
1210
    void   *entryKey = NULL;
13,514,031✔
1211
    int32_t nEntryKey = -1;
13,508,607✔
1212
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
13,513,353✔
1213
    if (valid < 0) break;
13,560,474✔
1214

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

1218
    terrno = TSDB_CODE_SUCCESS;
13,556,976✔
1219
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
13,541,382✔
1220
    if (terrno != TSDB_CODE_SUCCESS) {
13,427,817✔
1221
      ret = terrno;
×
1222
      break;
×
1223
    }
1224
    if (cmp == 0) {
13,447,140✔
1225
      if (taosArrayPush(pUids, &p->uid) == NULL) {
26,980,047✔
1226
        ret = terrno;
×
1227
        break;
×
1228
      }
1229
    } else {
1230
      if (param->equal == true) {
×
1231
        if (count > TRY_ERROR_LIMIT) break;
×
1232
        count++;
×
1233
      }
1234
    }
1235
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
13,532,907✔
1236
    if (valid < 0) break;
13,471,548✔
1237
  } while (1);
1238

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

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

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) {
×
1258
    return terrno;
×
1259
  }
1260
  pCursor->pMeta = pMeta;
×
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

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

1280
  int32_t TRY_ERROR_LIMIT = 1;
×
1281
  do {
×
1282
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
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) {
×
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
    }
1308
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1309
    if (valid < 0) {
×
1310
      break;
×
1311
    }
1312
  } while (1);
1313

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

1320
  taosMemoryFree(pCursor);
×
1321

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

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) {
×
1336
    return terrno;
×
1337
  }
1338
  pCursor->pMeta = pMeta;
×
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);
×
1349
  taosMemoryFree(buf);
×
1350
  taosMemoryFree(pKey);
×
1351

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,550,877✔
1359
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,550,877✔
1360
  SMetaFltParam *param = arg;
3,554,186✔
1361

1362
  SMetaEntry oStbEntry = {0};
3,554,186✔
1363
  int32_t    code = 0;
3,547,552✔
1364
  char      *buf = NULL;
3,547,552✔
1365
  void      *pData = NULL;
3,547,552✔
1366
  int        nData = 0;
3,547,784✔
1367

1368
  SDecoder    dc = {0};
3,549,123✔
1369
  STbDbKey    tbDbKey = {0};
3,548,709✔
1370
  STagIdxKey *pKey = NULL;
3,544,115✔
1371
  int32_t     nKey = 0;
3,552,767✔
1372

1373
  SIdxCursor *pCursor = NULL;
3,552,909✔
1374
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,552,909✔
1375
  if (!pCursor) {
3,544,764✔
1376
    return terrno;
×
1377
  }
1378
  pCursor->pMeta = pMeta;
3,544,764✔
1379
  pCursor->suid = param->suid;
3,547,697✔
1380
  pCursor->cid = param->cid;
3,549,571✔
1381
  pCursor->type = param->type;
3,553,349✔
1382

1383
  metaRLock(pMeta);
3,545,671✔
1384

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

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

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

1392
  tDecoderInit(&dc, pData, nData);
3,553,586✔
1393

1394
  code = metaDecodeEntry(&dc, &oStbEntry);
3,553,725✔
1395
  if (code) {
3,551,845✔
1396
    tDecoderClear(&dc);
×
1397
    goto END;
×
1398
  }
1399

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

1404
  code = TSDB_CODE_INVALID_PARA;
3,551,845✔
1405

1406
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,805,027✔
1407
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,805,112✔
1408
    if (IS_IDX_ON(schema)) {
4,805,868✔
1409
      if (schema->colId == param->cid && param->type == schema->type) {
4,795,846✔
1410
        code = 0;
3,552,239✔
1411
        break;
3,552,239✔
1412
      }
1413
    }
1414
  }
1415

1416
  TAOS_CHECK_GOTO(code, NULL, END);
3,552,154✔
1417

1418
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,552,154✔
1419
  if (code != 0) {
3,551,173✔
1420
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1421
  }
1422

1423
  int32_t maxSize = 0;
3,551,173✔
1424
  int32_t nTagData = 0;
3,551,845✔
1425
  void   *tagData = NULL;
3,551,845✔
1426

1427
  if (param->val == NULL) {
3,551,845✔
1428
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1429
    goto END;
×
1430
  } else {
1431
    if (IS_VAR_DATA_TYPE(param->type)) {
3,549,118✔
1432
      tagData = varDataVal(param->val);
424,480✔
1433
      nTagData = varDataLen(param->val);
425,117✔
1434

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

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

1446
        tagData = buf;
37,794✔
1447
        nTagData = maxSize;
37,794✔
1448
      }
1449
    } else {
1450
      tagData = param->val;
3,127,019✔
1451
      nTagData = tDataTypes[param->type].bytes;
3,127,941✔
1452
    }
1453
  }
1454

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

1459
  int cmp = 0;
3,551,384✔
1460
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,552,221✔
1461

1462
  int     count = 0;
3,554,144✔
1463
  int32_t valid = 0;
3,554,144✔
1464
  bool    found = false;
3,554,144✔
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,554,144✔
1471
  do {
36,317,864✔
1472
    void   *entryKey = NULL, *entryVal = NULL;
39,872,008✔
1473
    int32_t nEntryKey, nEntryVal;
39,871,352✔
1474

1475
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
39,870,883✔
1476
    if (valid < 0) {
39,879,159✔
1477
      break;
1,801,900✔
1478
    }
1479
    if (count > TRY_ERROR_LIMIT) {
38,077,259✔
1480
      break;
1,038,030✔
1481
    }
1482

1483
    STagIdxKey *p = entryKey;
37,039,229✔
1484
    if (p == NULL) break;
37,039,229✔
1485

1486
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
37,039,229✔
1487
      if (found == true) break;  //
1,035,005✔
1488
      if (diffCidCount > TRY_ERROR_LIMIT) break;
319,621✔
1489
      diffCidCount++;
319,621✔
1490
      count++;
319,621✔
1491
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
319,621✔
1492
      if (valid < 0) {
319,177✔
1493
        code = valid;
×
1494
        break;
×
1495
      } else {
1496
        continue;
319,177✔
1497
      }
1498
    }
1499

1500
    terrno = TSDB_CODE_SUCCESS;
36,005,013✔
1501
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
36,004,951✔
1502
    if (terrno != TSDB_CODE_SUCCESS) {
36,000,499✔
1503
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1504
      break;
×
1505
    }
1506
    if (cmp == 0) {
36,002,935✔
1507
      // match
1508
      tb_uid_t tuid = 0;
32,244,754✔
1509
      if (IS_VAR_DATA_TYPE(pKey->type)) {
32,244,297✔
1510
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
270,641✔
1511
      } else {
1512
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
31,972,312✔
1513
      }
1514
      if (taosArrayPush(pUids, &tuid) == NULL) {
32,244,292✔
1515
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1516
      }
1517
      found = true;
32,244,753✔
1518
    } else {
1519
      if (param->equal == true) {
3,758,181✔
1520
        if (count > TRY_ERROR_LIMIT) break;
2,634,326✔
1521
        count++;
2,634,326✔
1522
      }
1523
    }
1524
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
36,002,210✔
1525
    if (valid < 0) {
35,996,520✔
1526
      code = valid;
×
1527
      break;
×
1528
    }
1529
  } while (1);
1530

1531
END:
3,551,577✔
1532
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,554,706✔
1533
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,556,379✔
1534
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,552,078✔
1535
  tDecoderClear(&dc);
3,552,078✔
1536
  tdbFree(pData);
3,553,153✔
1537

1538
  taosMemoryFree(buf);
3,552,977✔
1539
  taosMemoryFree(pKey);
3,554,647✔
1540

1541
  taosMemoryFree(pCursor);
3,551,092✔
1542

1543
  return code;
3,553,274✔
1544
}
1545

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

1552
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
40,970,398✔
1553
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
40,971,849✔
1554
  if (lock) {
40,970,743✔
1555
    metaULock(pMeta);
×
1556
  }
1557

1558
  return ret;
40,970,743✔
1559
}
1560

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

1565
  int32_t isLock = false;
5,415,481✔
1566
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
5,415,481✔
1567
  for (int i = 0; i < sz; i++) {
46,383,902✔
1568
    STUidTagInfo *p = taosArrayGet(uidList, i);
40,968,287✔
1569

1570
    if (i % LIMIT == 0) {
40,970,877✔
1571
      if (isLock) metaULock(pMeta);
4,739,327✔
1572

1573
      metaRLock(pMeta);
4,739,327✔
1574
      isLock = true;
4,739,063✔
1575
    }
1576

1577
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1578
    void   *val = NULL;
40,970,613✔
1579
    int32_t len = 0;
40,970,881✔
1580
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
40,970,613✔
1581
      p->pTagVal = taosMemoryMalloc(len);
40,965,715✔
1582
      if (!p->pTagVal) {
40,964,486✔
1583
        if (isLock) metaULock(pMeta);
×
1584

1585
        TAOS_RETURN(terrno);
×
1586
      }
1587
      memcpy(p->pTagVal, val, len);
40,964,724✔
1588
      tdbFree(val);
40,965,660✔
1589
    } else {
1590
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
4,173✔
1591
    }
1592
  }
1593
  //  }
1594
  if (isLock) metaULock(pMeta);
5,415,615✔
1595
  return 0;
5,416,283✔
1596
}
1597

1598
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
8,593,549✔
1599
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
8,593,549✔
1600
  if (!pCur) {
8,591,459✔
1601
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1602
  }
1603

1604
  while (1) {
141,662,772✔
1605
    tb_uid_t uid = metaCtbCursorNext(pCur);
150,254,231✔
1606
    if (uid == 0) {
150,233,979✔
1607
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
8,593,463✔
1608
      break;
8,593,844✔
1609
    }
1610

1611
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
141,640,516✔
1612
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
141,641,600✔
1613
    if (!info.pTagVal) {
141,638,724✔
1614
      metaCloseCtbCursor(pCur);
×
1615
      return terrno;
×
1616
    }
1617
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
141,638,724✔
1618
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
141,662,180✔
1619
      taosMemoryFreeClear(info.pTagVal);
×
1620
      metaCloseCtbCursor(pCur);
×
1621
      return terrno;
×
1622
    }
1623
  }
1624
  metaCloseCtbCursor(pCur);
8,593,844✔
1625
  return TSDB_CODE_SUCCESS;
8,590,116✔
1626
}
1627

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

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

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

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

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;
×
1659
    SMeta   *pMeta = pVnode->pMeta;
×
1660
    SArray  *uids = NULL;
×
1661

1662
    int32_t code = metaGetChildUidsOfSuperTable(pMeta, id, &uids);
×
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

1669
      return code;
×
1670
    }
1671

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

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

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

1682
        return code;
×
1683
      }
1684

1685
      int32_t nCol = pTSchema->numOfCols;
×
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);
×
1691
        if (code) {
×
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

1705
    taosArrayDestroy(uids);
×
1706
  }
1707

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,690,342,969✔
1716
  int32_t code = 0;
1,690,342,969✔
1717
  void   *pData = NULL;
1,690,342,969✔
1718
  int     nData = 0;
1,690,540,140✔
1719
  int     lock = 0;
1,690,861,854✔
1720

1721
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
1,690,861,854✔
1722
    lock = 1;
349,865,005✔
1723
  }
1724

1725
  if (!lock) metaRLock(pMeta);
1,690,854,986✔
1726

1727
  // search cache
1728
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
1,690,952,536✔
1729
    if (!lock) metaULock(pMeta);
1,587,246,187✔
1730
    goto _exit;
1,587,091,502✔
1731
  }
1732

1733
  // search TDB
1734
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
103,678,703✔
1735
    // not found
1736
    if (!lock) metaULock(pMeta);
94,498,523✔
1737
    goto _exit;
94,490,759✔
1738
  }
1739

1740
  if (!lock) metaULock(pMeta);
9,229,818✔
1741

1742
  pInfo->uid = uid;
9,229,818✔
1743
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
9,229,818✔
1744
  pInfo->version = ((SUidIdxVal *)pData)->version;
9,229,818✔
1745
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
9,229,818✔
1746

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

1759
  if (lock) {
9,229,818✔
1760
    metaRLock(pReader->pMeta);
835,593✔
1761
  }
1762

1763
_exit:
1,690,787,757✔
1764
  tdbFree(pData);
1,690,487,612✔
1765
  return code;
1,690,273,309✔
1766
}
1767

1768
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
189,068,025✔
1769
  int32_t code = 0;
189,068,025✔
1770

1771
  if (!numOfTables && !numOfCols) goto _exit;
189,068,025✔
1772

1773
  SVnode *pVnodeObj = pVnode;
189,068,025✔
1774
  metaRLock(pVnodeObj->pMeta);
189,068,025✔
1775

1776
  // fast path: search cache
1777
  SMetaStbStats state = {0};
189,067,488✔
1778
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
189,073,809✔
1779
    metaULock(pVnodeObj->pMeta);
182,022,748✔
1780
    if (numOfTables) *numOfTables = state.ctbNum;
182,030,882✔
1781
    if (numOfCols) *numOfCols = state.colNum;
182,030,959✔
1782
    if (flags) *flags = state.flags;
182,021,832✔
1783
    goto _exit;
182,021,875✔
1784
  }
1785

1786
  // slow path: search TDB
1787
  int64_t ctbNum = 0;
7,056,592✔
1788
  int32_t colNum = 0;
7,056,919✔
1789
  int64_t keep = 0;
7,056,891✔
1790
  int8_t  flag = 0;
7,056,995✔
1791

1792
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
7,056,839✔
1793
  if (TSDB_CODE_SUCCESS == code) {
7,056,943✔
1794
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
7,057,028✔
1795
  }
1796
  if (TSDB_CODE_SUCCESS == code) {
7,056,921✔
1797
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
7,057,006✔
1798
  }
1799
  metaULock(pVnodeObj->pMeta);
7,057,077✔
1800
  if (TSDB_CODE_SUCCESS != code) {
7,057,077✔
1801
    goto _exit;
×
1802
  }
1803

1804
  if (numOfTables) *numOfTables = ctbNum;
7,057,077✔
1805
  if (numOfCols) *numOfCols = colNum;
7,057,077✔
1806
  if (flags) *flags = flag;
7,057,077✔
1807

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

1816
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
7,057,077✔
1817
  if (ret) {
7,056,722✔
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);
7,056,722✔
1823

1824
_exit:
189,079,037✔
1825
  return code;
189,080,800✔
1826
}
1827

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

1831
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
70,480,095✔
1832
    stats.ctbNum += deltaCtb;
59,515,580✔
1833
    stats.colNum += deltaCol;
59,515,580✔
1834
    if (deltaKeep > 0) {
59,515,580✔
1835
      stats.keep = deltaKeep;
5,925✔
1836
    }
1837

1838
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
59,515,580✔
1839
    if (code) {
59,504,898✔
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
}
70,470,357✔
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