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

taosdata / TDengine / #4978

06 Mar 2026 09:48AM UTC coverage: 68.439% (-0.02%) from 68.456%
#4978

push

travis-ci

web-flow
feat(TDgpt): support multiple input data columns for anomaly detection. (#34606)

0 of 93 new or added lines in 9 files covered. (0.0%)

3130 existing lines in 120 files now uncovered.

211124 of 308486 relevant lines covered (68.44%)

136029500.43 hits per line

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

59.58
/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) {
754,360,339✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
754,360,339✔
21
  metaReaderDoInit(pReader, pMeta, flags);
754,496,895✔
22
  pReader->pAPI = pAPI;
754,505,266✔
23
}
754,512,441✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
856,236,204✔
26
  memset(pReader, 0, sizeof(*pReader));
856,236,204✔
27
  pReader->pMeta = pMeta;
856,236,204✔
28
  pReader->flags = flags;
856,246,953✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
856,288,133✔
30
    metaRLock(pMeta);
729,507,474✔
31
  }
32
}
856,326,093✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
376,178,831✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
376,178,831✔
36
    metaULock(pReader->pMeta);
376,285,717✔
37
    pReader->flags |= META_READER_NOLOCK;
376,120,743✔
38
  }
39
}
376,320,513✔
40

41
void metaReaderClear(SMetaReader *pReader) {
1,062,787,246✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,062,787,246✔
43
    metaULock(pReader->pMeta);
353,151,874✔
44
  }
45
  tDecoderClear(&pReader->coder);
1,062,922,200✔
46
  tdbFree(pReader->pBuf);
1,062,948,787✔
47
  pReader->pBuf = NULL;
1,062,740,871✔
48
}
1,062,623,726✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
1,228,442,429✔
51
  int32_t  code = 0;
1,228,442,429✔
52
  SMeta   *pMeta = pReader->pMeta;
1,228,442,429✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
1,228,617,174✔
54

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

60
  // decode the entry
61
  tDecoderClear(&pReader->coder);
1,228,464,880✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
1,228,480,475✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
1,228,539,278✔
65
  if (code) {
1,228,287,088✔
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
1,228,287,088✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
142,174,367✔
79
    metaULock(pVnodeObj->pMeta);
44,634✔
80
    return false;
44,634✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
142,159,668✔
84
  return true;
142,162,272✔
85
}
86

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

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

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
204,853,265✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
204,854,663✔
98
}
99

100
static int32_t getUidVersion(SMetaReader *pReader, int64_t *version, tb_uid_t uid) {
×
101
  int32_t code = 0;
×
102
  SMeta *pMeta = pReader->pMeta;
×
103
  void* pKey = NULL;
×
104
  void* pVal = NULL;
×
105
  int   vLen = 0, kLen = 0;
×
106

107
  TBC* pCur = NULL;
×
108
  code = tdbTbcOpen(pMeta->pTbDb, (TBC**)&pCur, NULL);
×
109
  if (code != 0) {
×
110
    return TAOS_GET_TERRNO(code);
×
111
  }
112
  STbDbKey key = {.version = *version, .uid = INT64_MAX};
×
113
  int      c = 0;
×
114
  code = tdbTbcMoveTo(pCur, &key, sizeof(key), &c);
×
115
  if (code != 0) {
×
116
    goto END;
×
117
  }
118
  if (c >= 0){
×
119
    metaError("%s move to version:%"PRId64 " max failed", __func__, *version);
×
120
    code = TSDB_CODE_FAILED;
×
121
    goto END;
×
122
  }
123
  code = tdbTbcMoveToPrev(pCur);
×
124
  if (code != 0) {
×
125
    metaError("%s move to prev failed", __func__);
×
126
    goto END;
×
127
  }
128

129
  while (1) {
×
130
    int32_t ret = tdbTbcPrev(pCur, &pKey, &kLen, &pVal, &vLen);
×
131
    if (ret < 0) break;
×
132

133
    STbDbKey* tmp = (STbDbKey*)pKey;
×
134
    if (tmp->uid == uid) {
×
135
      *version = tmp->version;
×
136
      goto END;
×
137
    }
138
  }
139
  code = TSDB_CODE_NOT_FOUND;
×
140
  metaError("%s uid:%" PRId64 " version not found", __func__, uid);
×
141
END:
×
142
  tdbFree(pKey);
×
143
  tdbFree(pVal);
×
144
  tdbTbcClose(pCur);
×
145
  return code;
×
146
} 
147

148
// get table entry according to the latest version number that is less than or equal to version and uid, if version < 0, get latest version
149
int metaReaderGetTableEntryByVersionUid(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
169,707✔
150
  if (version < 0) {
169,707✔
151
    return metaReaderGetTableEntryByUid(pReader, uid);
×
152
  }
153
  SMeta *pMeta = pReader->pMeta;
169,707✔
154

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

170
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
733,901,148✔
171
  SMeta *pMeta = pReader->pMeta;
733,901,148✔
172

173
  SMetaInfo info;
734,030,332✔
174
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
734,024,108✔
175
  if (TSDB_CODE_SUCCESS != code) {
733,963,983✔
176
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
4,391✔
177
  }
178

179
  return metaGetTableEntryByVersion(pReader, info.version, uid);
733,959,592✔
180
}
181

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

186
  // query name.idx
187
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
59,403,055✔
188
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
8,472,861✔
189
  }
190

191
  uid = *(tb_uid_t *)pReader->pBuf;
50,924,055✔
192
  return metaReaderGetTableEntryByUid(pReader, uid);
50,924,983✔
193
}
194

195
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
76,012,132✔
196
  void    *pData = NULL;
76,012,132✔
197
  int      nData = 0;
76,012,614✔
198
  tb_uid_t uid = 0;
76,013,009✔
199

200
  metaRLock(pMeta);
76,013,009✔
201

202
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
76,010,762✔
203
    uid = *(tb_uid_t *)pData;
9,265,149✔
204
    tdbFree(pData);
9,265,149✔
205
  }
206

207
  metaULock(pMeta);
75,963,251✔
208

209
  return uid;
76,009,113✔
210
}
211

212
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
942,813✔
213
  int         code = 0;
942,813✔
214
  SMetaReader mr = {0};
942,813✔
215
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
942,813✔
216
  code = metaReaderGetTableEntryByUid(&mr, uid);
942,301✔
217
  if (code < 0) {
942,628✔
218
    metaReaderClear(&mr);
241✔
219
    return code;
241✔
220
  }
221

222
  STR_TO_VARSTR(tbName, mr.me.name);
942,387✔
223
  metaReaderClear(&mr);
942,271✔
224

225
  return 0;
941,908✔
226
}
227

228
int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
×
229
  int         code = 0;
×
230
  SMetaReader mr = {0};
×
231
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
232
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
233
  if (code < 0) {
×
234
    metaReaderClear(&mr);
×
235
    return code;
×
236
  }
237
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
238
  metaReaderClear(&mr);
×
239

240
  return 0;
×
241
}
242

243
int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) {
856,640✔
244
  int         code = 0;
856,640✔
245
  SMetaReader mr = {0};
856,640✔
246
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
856,640✔
247

248
  SMetaReader *pReader = &mr;
856,085✔
249

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

256
  *uid = *(tb_uid_t *)pReader->pBuf;
492,145✔
257

258
  metaReaderClear(&mr);
492,145✔
259

260
  return 0;
492,145✔
261
}
262

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

268
  code = metaGetTableEntryByName(&mr, tbName);
494,370✔
269
  if (code == 0) *tbType = mr.me.type;
494,370✔
270
  if (TSDB_CHILD_TABLE == mr.me.type || TSDB_VIRTUAL_CHILD_TABLE == mr.me.type) {
494,370✔
271
    *suid = mr.me.ctbEntry.suid;
486,816✔
272
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
7,554✔
273
    *suid = mr.me.uid;
4,689✔
274
  } else {
275
    *suid = 0;
2,865✔
276
  }
277

278
  metaReaderClear(&mr);
494,151✔
279
  return code;
494,151✔
280
}
281

282
int metaReadNext(SMetaReader *pReader) {
×
283
  SMeta *pMeta = pReader->pMeta;
×
284

285
  // TODO
286

287
  return 0;
×
288
}
289

290
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
291
  int         code = -1;
×
292
  SMetaReader mr = {0};
×
293
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
294
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
295
  if (code < 0) {
×
296
    goto _exit;
×
297
  }
298
  if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
×
299
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
300
  } else if (mr.me.type == TSDB_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
301
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
302
  } else {
303
    goto _exit;
×
304
  }
305

306
  code = 0;
×
307

308
_exit:
×
309
  metaReaderClear(&mr);
×
310
  return code;
×
311
}
312

313
#if 1  // ===================================================
314
SMTbCursor *metaOpenTbCursor(void *pVnode) {
11,075,590✔
315
  SMTbCursor *pTbCur = NULL;
11,075,590✔
316
  int32_t     code;
317

318
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
11,075,590✔
319
  if (pTbCur == NULL) {
11,074,607✔
320
    return NULL;
×
321
  }
322

323
  SVnode *pVnodeObj = pVnode;
11,074,607✔
324
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
325
  pTbCur->pMeta = pVnodeObj->pMeta;
11,074,607✔
326
  pTbCur->paused = 1;
11,076,472✔
327
  code = metaResumeTbCursor(pTbCur, 1, 0);
11,075,905✔
328
  if (code) {
11,073,993✔
329
    terrno = code;
×
330
    taosMemoryFree(pTbCur);
×
331
    return NULL;
×
332
  }
333
  return pTbCur;
11,073,993✔
334
}
335

336
void metaCloseTbCursor(SMTbCursor *pTbCur) {
22,151,107✔
337
  if (pTbCur) {
22,151,107✔
338
    tdbFree(pTbCur->pKey);
11,077,383✔
339
    tdbFree(pTbCur->pVal);
11,078,665✔
340
    if (!pTbCur->paused) {
11,078,592✔
341
      metaReaderClear(&pTbCur->mr);
1,509,447✔
342
      if (pTbCur->pDbc) {
1,508,810✔
343
        tdbTbcClose((TBC *)pTbCur->pDbc);
1,508,810✔
344
      }
345
    }
346
    taosMemoryFree(pTbCur);
11,077,897✔
347
  }
348
}
22,152,318✔
349

350
void metaPauseTbCursor(SMTbCursor *pTbCur) {
9,571,499✔
351
  if (!pTbCur->paused) {
9,571,499✔
352
    metaReaderClear(&pTbCur->mr);
9,571,499✔
353
    tdbTbcClose((TBC *)pTbCur->pDbc);
9,571,499✔
354
    pTbCur->paused = 1;
9,571,499✔
355
  }
356
}
9,572,136✔
357
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
11,078,134✔
358
  int32_t code = 0;
11,078,134✔
359
  int32_t lino;
360
  int8_t  locked = 0;
11,078,134✔
361
  if (pTbCur->paused) {
11,078,134✔
362
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
11,078,749✔
363
    locked = 1;
11,079,052✔
364
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
11,079,052✔
365
    if (code != 0) {
11,079,789✔
366
      TSDB_CHECK_CODE(code, lino, _exit);
×
367
    }
368

369
    if (first) {
11,079,789✔
370
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
11,077,456✔
371
      TSDB_CHECK_CODE(code, lino, _exit);
11,071,149✔
372
    } else {
373
      int c = 1;
2,333✔
374
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
2,333✔
375
      TSDB_CHECK_CODE(code, lino, _exit);
2,333✔
376
      if (c == 0) {
2,333✔
377
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
2,333✔
378
      } else if (c < 0) {
×
379
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
380
        TSDB_CHECK_CODE(code, lino, _exit);
×
381
      } else {
382
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
383
        TSDB_CHECK_CODE(code, lino, _exit);
×
384
      }
385
    }
386

387
    pTbCur->paused = 0;
11,073,214✔
388
  }
389

390
_exit:
×
391
  if (code != 0 && locked) {
11,078,982✔
392
    metaReaderReleaseLock(&pTbCur->mr);
×
393
  }
394
  return code;
11,078,459✔
395
}
396

397
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
300,645,023✔
398
  int    ret;
399
  void  *pBuf;
400
  STbCfg tbCfg;
401

402
  for (;;) {
403
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
300,645,023✔
404
    if (ret < 0) {
300,627,338✔
405
      return ret;
11,075,194✔
406
    }
407

408
    tDecoderClear(&pTbCur->mr.coder);
289,552,144✔
409

410
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
289,577,363✔
411
    if (ret) return ret;
289,567,943✔
412

413
    if (pTbCur->mr.me.type == jumpTableType) {
289,567,943✔
414
      continue;
4,156,119✔
415
    }
416

417
    break;
285,414,073✔
418
  }
419

420
  return 0;
285,414,073✔
421
}
422

423
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
424
  int    ret;
425
  void  *pBuf;
426
  STbCfg tbCfg;
427

428
  for (;;) {
429
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
430
    if (ret < 0) {
×
431
      return -1;
×
432
    }
433

434
    tDecoderClear(&pTbCur->mr.coder);
×
435

436
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
437
    if (ret < 0) {
×
438
      return ret;
×
439
    }
440

441
    if (pTbCur->mr.me.type == jumpTableType) {
×
442
      continue;
×
443
    }
444

445
    break;
×
446
  }
447

448
  return 0;
×
449
}
450

451
/**
452
 * @param type 0x01 fetchRsmaSchema if table is rsma
453
 */
454
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
317,267,783✔
455
                                   int8_t type) {
456
  int32_t         code = 0;
317,267,783✔
457
  void           *pData = NULL;
317,267,783✔
458
  int             nData = 0;
317,169,521✔
459
  int64_t         version;
460
  SSchemaWrapper  schema = {0};
317,171,769✔
461
  SSchemaWrapper *pSchema = NULL;
317,094,564✔
462
  SDecoder        dc = {0};
317,094,564✔
463
  if (lock) {
316,936,394✔
464
    metaRLock(pMeta);
312,616,490✔
465
  }
466
_query:
348,993,375✔
467
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
349,095,598✔
468
    goto _err;
345,767✔
469
  }
470

471
  version = ((SUidIdxVal *)pData)[0].version;
348,805,315✔
472

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

478
  SMetaEntry me = {0};
348,817,972✔
479
  tDecoderInit(&dc, pData, nData);
348,714,885✔
480
  code = metaDecodeEntry(&dc, &me);
348,815,934✔
481
  if (code) {
348,724,677✔
482
    tDecoderClear(&dc);
×
483
    goto _err;
×
484
  }
485
  if (me.type == TSDB_SUPER_TABLE) {
348,724,677✔
486
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
247,256,544✔
487
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
247,184,298✔
488
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
247,184,298✔
489
      if (TABLE_IS_ROLLUP(me.flags)) {
247,185,338✔
490
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
103,234✔
491
          tDecoderClear(&dc);
×
492
          goto _err;
×
493
        }
494
      }
495
      tDecoderClear(&dc);
247,185,338✔
496
      goto _exit;
247,146,674✔
497
    }
498
  } else if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
101,468,133✔
499
    uid = me.ctbEntry.suid;
31,742,221✔
500
    tDecoderClear(&dc);
31,742,221✔
501
    goto _query;
31,746,294✔
502
  } else {
503
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
69,725,912✔
504
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
69,747,117✔
505
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
69,747,117✔
506
      tDecoderClear(&dc);
69,740,922✔
507
      goto _exit;
69,741,208✔
508
    }
509
  }
510
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
53,130✔
511
  tDecoderClear(&dc);
53,130✔
512

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

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

529
_exit:
316,931,991✔
530
  if (lock) {
316,981,962✔
531
    metaULock(pMeta);
312,687,148✔
532
  }
533
  tdbFree(pData);
316,871,761✔
534
  return pSchema;
316,945,463✔
535

536
_err:
352,536✔
537
  if (lock) {
345,767✔
538
    metaULock(pMeta);
345,767✔
539
  }
540
  tdbFree(pData);
345,767✔
541
  tDeleteSchemaWrapper(pSchema);
542
  if (extSchema != NULL) {
345,767✔
543
    taosMemoryFreeClear(*extSchema);
201,567✔
544
  }
545
  terrno = code;
345,767✔
546
  return NULL;
345,767✔
547
}
548

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

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

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

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

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

583
_exit:
747,898✔
584
  if (lock) {
749,174✔
585
    metaULock(pMeta);
749,174✔
586
  }
587
  tdbFree(pData);
747,270✔
588
  return createTime;
748,217✔
589
}
590

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

598
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
116,940,422✔
599
  if (pCtbCur == NULL) {
116,876,914✔
600
    return NULL;
×
601
  }
602

603
  pCtbCur->pMeta = pMeta;
116,876,914✔
604
  pCtbCur->suid = uid;
116,877,307✔
605
  pCtbCur->lock = lock;
116,886,692✔
606
  pCtbCur->paused = 1;
116,929,221✔
607

608
  ret = metaResumeCtbCursor(pCtbCur, 1);
116,922,152✔
609
  if (ret < 0) {
116,922,239✔
610
    return NULL;
×
611
  }
612
  return pCtbCur;
116,922,239✔
613
}
614

615
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
116,951,272✔
616
  if (pCtbCur) {
116,951,272✔
617
    if (!pCtbCur->paused) {
116,952,781✔
618
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
112,495,980✔
619
      if (pCtbCur->pCur) {
112,485,005✔
620
        tdbTbcClose(pCtbCur->pCur);
112,492,896✔
621
      }
622
    }
623
    tdbFree(pCtbCur->pKey);
116,930,422✔
624
    tdbFree(pCtbCur->pVal);
116,920,625✔
625
  }
626
  taosMemoryFree(pCtbCur);
116,909,249✔
627
}
116,902,788✔
628

629
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
4,456,349✔
630
  if (!pCtbCur->paused) {
4,456,349✔
631
    tdbTbcClose((TBC *)pCtbCur->pCur);
4,455,864✔
632
    if (pCtbCur->lock) {
4,455,661✔
633
      metaULock(pCtbCur->pMeta);
4,455,661✔
634
    }
635
    pCtbCur->paused = 1;
4,455,863✔
636
  }
637
}
4,456,848✔
638

639
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
116,928,092✔
640
  if (pCtbCur->paused) {
116,928,092✔
641
    pCtbCur->paused = 0;
116,924,749✔
642

643
    if (pCtbCur->lock) {
116,930,807✔
644
      metaRLock(pCtbCur->pMeta);
112,610,468✔
645
    }
646
    int ret = 0;
116,925,282✔
647
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
116,925,282✔
648
    if (ret < 0) {
116,921,547✔
649
      metaCloseCtbCursor(pCtbCur);
×
650
      return -1;
×
651
    }
652

653
    if (first) {
116,921,547✔
654
      SCtbIdxKey ctbIdxKey;
116,919,738✔
655
      // move to the suid
656
      ctbIdxKey.suid = pCtbCur->suid;
116,921,572✔
657
      ctbIdxKey.uid = INT64_MIN;
116,935,768✔
658
      int c = 0;
116,935,768✔
659
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
116,917,642✔
660
      if (c > 0) {
116,912,243✔
661
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
23,839,772✔
662
      }
663
    } else {
664
      int c = 0;
×
665
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
666
      if (c < 0) {
×
667
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
668
      } else {
669
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
670
      }
671
    }
672
  }
673
  return 0;
116,928,109✔
674
}
675

676
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
705,061,653✔
677
  int         ret;
678
  SCtbIdxKey *pCtbIdxKey;
679

680
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
705,061,653✔
681
  if (ret < 0) {
704,866,860✔
682
    return 0;
85,819,848✔
683
  }
684

685
  pCtbIdxKey = pCtbCur->pKey;
619,047,012✔
686
  if (pCtbIdxKey->suid > pCtbCur->suid) {
619,303,278✔
687
    return 0;
31,234,447✔
688
  }
689

690
  return pCtbIdxKey->uid;
588,116,722✔
691
}
692

693
struct SMStbCursor {
694
  SMeta   *pMeta;
695
  TBC     *pCur;
696
  tb_uid_t suid;
697
  void    *pKey;
698
  void    *pVal;
699
  int      kLen;
700
  int      vLen;
701
};
702

703
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
65,205,838✔
704
  SMStbCursor *pStbCur = NULL;
65,205,838✔
705
  int          ret = 0;
65,205,838✔
706
  int          c = 0;
65,205,838✔
707

708
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
65,206,472✔
709
  if (pStbCur == NULL) {
65,194,835✔
710
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
711
    return NULL;
×
712
  }
713

714
  pStbCur->pMeta = pMeta;
65,194,835✔
715
  pStbCur->suid = suid;
65,204,256✔
716
  metaRLock(pMeta);
65,206,158✔
717

718
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
65,207,426✔
719
  if (ret < 0) {
65,199,199✔
720
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
721
    metaULock(pMeta);
×
722
    taosMemoryFree(pStbCur);
×
723
    return NULL;
×
724
  }
725

726
  // move to the suid
727
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
65,199,199✔
728
  if (c > 0) {
65,208,694✔
729
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
730
  }
731

732
  return pStbCur;
65,208,060✔
733
}
734

735
void metaCloseStbCursor(SMStbCursor *pStbCur) {
65,205,826✔
736
  if (pStbCur) {
65,205,826✔
737
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
65,206,460✔
738
    if (pStbCur->pCur) {
65,209,008✔
739
      tdbTbcClose(pStbCur->pCur);
65,208,374✔
740

741
      tdbFree(pStbCur->pKey);
65,207,254✔
742
      tdbFree(pStbCur->pVal);
65,207,426✔
743
    }
744

745
    taosMemoryFree(pStbCur);
65,206,247✔
746
  }
747
}
65,203,302✔
748

749
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
110,801,759✔
750
  int ret;
751

752
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
110,801,759✔
753
  if (ret < 0) {
110,798,026✔
754
    return 0;
65,208,362✔
755
  }
756
  return *(tb_uid_t *)pStbCur->pKey;
45,589,664✔
757
}
758

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

763
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
268,293,075✔
764
  if (!pSW) return NULL;
268,290,722✔
765

766
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
268,257,492✔
767

768
  tDeleteSchemaWrapper(pSW);
769
  return pTSchema;
268,149,528✔
770
}
771

772
/**
773
 * Fetch rsma schema if table is rsma
774
 */
775
SRSchema *metaGetTbTSchemaR(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
26,899✔
776
  SRSchema       *pRSchema = NULL;
26,899✔
777
  SSchemaWrapper *pSW = NULL;
26,899✔
778

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

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

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

794
_exit:
26,899✔
795
  tDeleteSchemaWrapper(pSW);
796
  return pRSchema;
26,899✔
797
_err:
×
798
  tDeleteSchemaWrapper(pSW);
799
  tFreeSRSchema(&pRSchema);
800
  return NULL;
×
801
}
802

803
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
489,987✔
804
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
489,987✔
805
  if (*ppTSchema == NULL) {
490,460✔
806
    return terrno;
×
807
  }
808
  return TSDB_CODE_SUCCESS;
490,065✔
809
}
810

811
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
267,660,351✔
812
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
267,660,351✔
813
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
267,510,704✔
UNCOV
814
    return terrno;
×
815
  }
816
  return TSDB_CODE_SUCCESS;
267,645,500✔
817
}
818

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

823
  void     *pData = NULL;
68,393,885✔
824
  int       nData = 0;
68,401,872✔
825
  SSkmDbKey skmDbKey;
68,408,722✔
826
  if (sver <= 0) {
68,405,206✔
827
    SMetaInfo info;
34,304,462✔
828
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
34,310,947✔
829
      sver = info.skmVer;
34,285,517✔
830
    } else {
831
      TBC *pSkmDbC = NULL;
31,691✔
832
      int  c;
33,230✔
833

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

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

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

848
      if (c == 0) {
33,230✔
849
        metaULock(pMeta);
×
850
        tdbTbcClose(pSkmDbC);
×
851
        code = TSDB_CODE_FAILED;
×
852
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
853
        goto _exit;
×
854
      }
855

856
      if (c < 0) {
33,230✔
857
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
3,330✔
858
      }
859

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

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

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

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

878
  if (!(sver > 0)) {
68,401,454✔
879
    code = TSDB_CODE_NOT_FOUND;
×
880
    goto _exit;
×
881
  }
882

883
  skmDbKey.uid = suid ? suid : uid;
68,401,454✔
884
  skmDbKey.sver = sver;
68,401,454✔
885
  metaRLock(pMeta);
68,401,454✔
886
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
68,407,484✔
887
    metaULock(pMeta);
2,516✔
888
    code = TSDB_CODE_NOT_FOUND;
2,516✔
889
    goto _exit;
2,516✔
890
  }
891
  metaULock(pMeta);
68,388,334✔
892

893
  // decode
894
  SDecoder        dc = {0};
68,411,444✔
895
  SSchemaWrapper  schema;
68,410,182✔
896
  SSchemaWrapper *pSchemaWrapper = &schema;
68,405,780✔
897

898
  tDecoderInit(&dc, pData, nData);
68,405,780✔
899
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
68,422,461✔
900
  tDecoderClear(&dc);
68,422,461✔
901
  tdbFree(pData);
68,405,508✔
902
  if (TSDB_CODE_SUCCESS != code) {
68,374,059✔
903
    taosMemoryFree(pSchemaWrapper->pSchema);
×
904
    goto _exit;
×
905
  }
906

907
  // convert
908
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
68,374,059✔
909
  if (pTSchema == NULL) {
68,413,611✔
910
    code = TSDB_CODE_OUT_OF_MEMORY;
×
911
  }
912

913
  *ppTSchema = pTSchema;
68,413,611✔
914
  taosMemoryFree(pSchemaWrapper->pSchema);
68,416,843✔
915

916
_exit:
68,404,481✔
917
  return code;
68,402,388✔
918
}
919

920
// N.B. Called by statusReq per second
921
int64_t metaGetTbNum(SMeta *pMeta) {
135,661,664✔
922
  // num of child tables (excluding normal tables , stables and others)
923

924
  /* int64_t num = 0; */
925
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
926

927
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
135,661,664✔
928
}
929

930
void metaUpdTimeSeriesNum(SMeta *pMeta) {
65,198,484✔
931
  int64_t nCtbTimeSeries = 0;
65,198,484✔
932
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
65,201,654✔
933
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
65,200,072✔
934
  }
935
}
65,201,340✔
936

937
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
938
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
939
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
222,490,729✔
940
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
222,489,844✔
941
    metaUpdTimeSeriesNum(pMeta);
65,188,139✔
942
  }
943

944
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
222,486,734✔
945
}
946

947
// type: 1 reported timeseries
948
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
222,483,222✔
949
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
222,500,290✔
950
  if (type == 1) {
222,500,290✔
951
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
135,661,664✔
952
  }
953
  return nTimeSeries;
222,479,712✔
954
}
955

956
typedef struct {
957
  SMeta   *pMeta;
958
  TBC     *pCur;
959
  tb_uid_t uid;
960
  void    *pKey;
961
  void    *pVal;
962
  int      kLen;
963
  int      vLen;
964
} SMSmaCursor;
965

966
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
967
  SMSmaCursor *pSmaCur = NULL;
×
968
  SSmaIdxKey   smaIdxKey;
×
969
  int          ret;
970
  int          c;
×
971

972
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
973
  if (pSmaCur == NULL) {
×
974
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
975
    return NULL;
×
976
  }
977

978
  pSmaCur->pMeta = pMeta;
×
979
  pSmaCur->uid = uid;
×
980
  metaRLock(pMeta);
×
981

982
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
983
  if (ret < 0) {
×
984
    metaULock(pMeta);
×
985
    taosMemoryFree(pSmaCur);
×
986
    return NULL;
×
987
  }
988

989
  // move to the suid
990
  smaIdxKey.uid = uid;
×
991
  smaIdxKey.smaUid = INT64_MIN;
×
992
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
993
  if (c > 0) {
×
994
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
995
  }
996

997
  return pSmaCur;
×
998
}
999

1000
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
1001
  if (pSmaCur) {
×
1002
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
1003
    if (pSmaCur->pCur) {
×
1004
      tdbTbcClose(pSmaCur->pCur);
×
1005
      pSmaCur->pCur = NULL;
×
1006

1007
      tdbFree(pSmaCur->pKey);
×
1008
      tdbFree(pSmaCur->pVal);
×
1009
    }
1010

1011
    taosMemoryFree(pSmaCur);
×
1012
  }
1013
}
×
1014

1015
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
1016
  int         ret;
1017
  SSmaIdxKey *pSmaIdxKey;
1018

1019
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
1020
  if (ret < 0) {
×
1021
    return 0;
×
1022
  }
1023

1024
  pSmaIdxKey = pSmaCur->pKey;
×
1025
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
1026
    return 0;
×
1027
  }
1028

1029
  return pSmaIdxKey->uid;
×
1030
}
1031

1032
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
1033
  STSmaWrapper *pSW = NULL;
×
1034
  SArray       *pSmaIds = NULL;
×
1035

1036
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
1037
    return NULL;
×
1038
  }
1039

1040
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
1041
  if (!pSW) {
×
1042
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1043
    goto _err;
×
1044
  }
1045

1046
  pSW->number = taosArrayGetSize(pSmaIds);
×
1047
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
1048

1049
  if (!pSW->tSma) {
×
1050
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1051
    goto _err;
×
1052
  }
1053

1054
  SMetaReader mr = {0};
×
1055
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
1056
  int64_t smaId;
1057
  int     smaIdx = 0;
×
1058
  STSma  *pTSma = NULL;
×
1059
  for (int i = 0; i < pSW->number; ++i) {
×
1060
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
1061
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
1062
      tDecoderClear(&mr.coder);
×
1063
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
1064
      continue;
×
1065
    }
1066
    tDecoderClear(&mr.coder);
×
1067
    pTSma = pSW->tSma + smaIdx;
×
1068
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1069
    if (deepCopy) {
×
1070
      if (pTSma->exprLen > 0) {
×
1071
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
1072
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1073
          goto _err;
×
1074
        }
1075
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
1076
      }
1077
      if (pTSma->tagsFilterLen > 0) {
×
1078
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
1079
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1080
          goto _err;
×
1081
        }
1082
      }
1083
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
1084
    } else {
1085
      pTSma->exprLen = 0;
×
1086
      pTSma->expr = NULL;
×
1087
      pTSma->tagsFilterLen = 0;
×
1088
      pTSma->tagsFilter = NULL;
×
1089
    }
1090

1091
    ++smaIdx;
×
1092
  }
1093

1094
  if (smaIdx <= 0) goto _err;
×
1095
  pSW->number = smaIdx;
×
1096

1097
  metaReaderClear(&mr);
×
1098
  taosArrayDestroy(pSmaIds);
×
1099
  return pSW;
×
1100
_err:
×
1101
  metaReaderClear(&mr);
×
1102
  taosArrayDestroy(pSmaIds);
×
1103
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
1104
  return NULL;
×
1105
}
1106

1107
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
×
1108
  STSma      *pTSma = NULL;
×
1109
  SMetaReader mr = {0};
×
1110
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
1111
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
×
1112
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
1113
    metaReaderClear(&mr);
×
1114
    return NULL;
×
1115
  }
1116
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
×
1117
  if (!pTSma) {
×
1118
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1119
    metaReaderClear(&mr);
×
1120
    return NULL;
×
1121
  }
1122

1123
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1124

1125
  metaReaderClear(&mr);
×
1126
  return pTSma;
×
1127
}
1128

1129
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1130
  SArray     *pUids = NULL;
×
1131
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1132

1133
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1134
  if (!pCur) {
×
1135
    return NULL;
×
1136
  }
1137

1138
  while (1) {
×
1139
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1140
    if (id == 0) {
×
1141
      break;
×
1142
    }
1143

1144
    if (!pUids) {
×
1145
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1146
      if (!pUids) {
×
1147
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1148
        metaCloseSmaCursor(pCur);
×
1149
        return NULL;
×
1150
      }
1151
    }
1152

1153
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1154

1155
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1156
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1157
      metaCloseSmaCursor(pCur);
×
1158
      taosArrayDestroy(pUids);
×
1159
      return NULL;
×
1160
    }
1161
  }
1162

1163
  metaCloseSmaCursor(pCur);
×
1164
  return pUids;
×
1165
}
1166

1167
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1168
  SArray     *pUids = NULL;
×
1169
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1170
  tb_uid_t    lastUid = 0;
×
1171

1172
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1173
  if (!pCur) {
×
1174
    return NULL;
×
1175
  }
1176

1177
  while (1) {
×
1178
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1179
    if (uid == 0) {
×
1180
      break;
×
1181
    }
1182

1183
    if (lastUid == uid) {
×
1184
      continue;
×
1185
    }
1186

1187
    lastUid = uid;
×
1188

1189
    if (!pUids) {
×
1190
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1191
      if (!pUids) {
×
1192
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1193
        metaCloseSmaCursor(pCur);
×
1194
        return NULL;
×
1195
      }
1196
    }
1197

1198
    if (!taosArrayPush(pUids, &uid)) {
×
1199
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1200
      metaCloseSmaCursor(pCur);
×
1201
      taosArrayDestroy(pUids);
×
1202
      return NULL;
×
1203
    }
1204
  }
1205

1206
  metaCloseSmaCursor(pCur);
×
1207
  return pUids;
×
1208
}
1209

1210
#endif
1211

1212
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
1,080,035,896✔
1213
  STag *tag = (STag *)pTag;
1,080,035,896✔
1214
  if (type == TSDB_DATA_TYPE_JSON) {
1,080,035,896✔
1215
    return tag;
1,423,438✔
1216
  }
1217
  bool find = tTagGet(tag, val);
1,078,612,458✔
1218

1219
  if (!find) {
1,078,862,393✔
1220
    return NULL;
7,584,211✔
1221
  }
1222

1223
  return val;
1,071,278,182✔
1224
}
1225

1226
typedef struct {
1227
  SMeta   *pMeta;
1228
  TBC     *pCur;
1229
  tb_uid_t suid;
1230
  int16_t  cid;
1231
  int16_t  type;
1232
  void    *pKey;
1233
  void    *pVal;
1234
  int32_t  kLen;
1235
  int32_t  vLen;
1236
} SIdxCursor;
1237

1238
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
1,642✔
1239
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,642✔
1240
  SMetaFltParam *param = arg;
1,642✔
1241
  int32_t        ret = 0;
1,642✔
1242

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

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

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

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

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

1271
  static const int8_t TRY_ERROR_LIMIT = 1;
1272
  do {
6,349,996✔
1273
    void   *entryKey = NULL;
6,351,638✔
1274
    int32_t nEntryKey = -1;
6,350,207✔
1275
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
6,351,161✔
1276
    if (valid < 0) break;
6,350,048✔
1277

1278
    SBtimeIdxKey *p = entryKey;
6,348,406✔
1279
    if (count > TRY_ERROR_LIMIT) break;
6,348,406✔
1280

1281
    terrno = TSDB_CODE_SUCCESS;
6,348,406✔
1282
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
6,346,657✔
1283
    if (terrno != TSDB_CODE_SUCCESS) {
6,343,000✔
1284
      ret = terrno;
×
1285
      break;
×
1286
    }
1287
    if (cmp == 0) {
6,341,410✔
1288
      if (taosArrayPush(pUids, &p->uid) == NULL) {
12,694,268✔
1289
        ret = terrno;
×
1290
        break;
×
1291
      }
1292
    } else {
1293
      if (param->equal == true) {
×
1294
        if (count > TRY_ERROR_LIMIT) break;
×
1295
        count++;
×
1296
      }
1297
    }
1298
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
6,352,858✔
1299
    if (valid < 0) break;
6,349,519✔
1300
  } while (1);
1301

1302
END:
1,642✔
1303
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
1,642✔
1304
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
1,642✔
1305
  taosMemoryFree(pCursor);
1,642✔
1306
  return ret;
1,642✔
1307
}
1308

1309
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1310
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1311
  SMetaFltParam *param = arg;
×
1312
  int32_t        ret = 0;
×
1313
  char          *buf = NULL;
×
1314

1315
  STagIdxKey *pKey = NULL;
×
1316
  int32_t     nKey = 0;
×
1317

1318
  SIdxCursor *pCursor = NULL;
×
1319
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1320
  if (pCursor == NULL) {
×
1321
    return terrno;
×
1322
  }
1323
  pCursor->pMeta = pMeta;
×
1324
  pCursor->suid = param->suid;
×
1325
  pCursor->cid = param->cid;
×
1326
  pCursor->type = param->type;
×
1327

1328
  char *pName = param->val;
×
1329

1330
  metaRLock(pMeta);
×
1331
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1332
  if (ret != 0) {
×
1333
    goto END;
×
1334
  }
1335

1336
  int cmp = 0;
×
1337
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1338
    goto END;
×
1339
  }
1340
  int32_t valid = 0;
×
1341
  int32_t count = 0;
×
1342

1343
  int32_t TRY_ERROR_LIMIT = 1;
×
1344
  do {
×
1345
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1346
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1347
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1348
    if (valid < 0) break;
×
1349

1350
    if (count > TRY_ERROR_LIMIT) break;
×
1351

1352
    char *pTableKey = (char *)pEntryKey;
×
1353
    terrno = TSDB_CODE_SUCCESS;
×
1354
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1355
    if (terrno != TSDB_CODE_SUCCESS) {
×
1356
      ret = terrno;
×
1357
      goto END;
×
1358
    }
1359
    if (cmp == 0) {
×
1360
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1361
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1362
        ret = terrno;
×
1363
        goto END;
×
1364
      }
1365
    } else {
1366
      if (param->equal == true) {
×
1367
        if (count > TRY_ERROR_LIMIT) break;
×
1368
        count++;
×
1369
      }
1370
    }
1371
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1372
    if (valid < 0) {
×
1373
      break;
×
1374
    }
1375
  } while (1);
1376

1377
END:
×
1378
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1379
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1380
  taosMemoryFree(buf);
×
1381
  taosMemoryFree(pKey);
×
1382

1383
  taosMemoryFree(pCursor);
×
1384

1385
  return ret;
×
1386
}
1387
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1388
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1389
  SMetaFltParam *param = arg;
×
1390
  int32_t        ret = 0;
×
1391
  char          *buf = NULL;
×
1392

1393
  STtlIdxKey *pKey = NULL;
×
1394
  int32_t     nKey = 0;
×
1395

1396
  SIdxCursor *pCursor = NULL;
×
1397
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1398
  if (pCursor == NULL) {
×
1399
    return terrno;
×
1400
  }
1401
  pCursor->pMeta = pMeta;
×
1402
  pCursor->suid = param->suid;
×
1403
  pCursor->cid = param->cid;
×
1404
  pCursor->type = param->type;
×
1405

1406
  metaRLock(pMeta);
×
1407
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1408

1409
END:
×
1410
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1411
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1412
  taosMemoryFree(buf);
×
1413
  taosMemoryFree(pKey);
×
1414

1415
  taosMemoryFree(pCursor);
×
1416

1417
  return ret;
×
1418
  // impl later
1419
  return 0;
1420
}
1421
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
3,107,537✔
1422
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,107,537✔
1423
  SMetaFltParam *param = arg;
3,109,710✔
1424

1425
  SMetaEntry oStbEntry = {0};
3,109,710✔
1426
  int32_t    code = 0;
3,106,850✔
1427
  char      *buf = NULL;
3,106,850✔
1428
  void      *pData = NULL;
3,106,850✔
1429
  int        nData = 0;
3,104,596✔
1430

1431
  SDecoder    dc = {0};
3,104,739✔
1432
  STbDbKey    tbDbKey = {0};
3,106,790✔
1433
  STagIdxKey *pKey = NULL;
3,105,950✔
1434
  int32_t     nKey = 0;
3,107,728✔
1435

1436
  SIdxCursor *pCursor = NULL;
3,109,870✔
1437
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,109,870✔
1438
  if (!pCursor) {
3,103,847✔
1439
    return terrno;
×
1440
  }
1441
  pCursor->pMeta = pMeta;
3,103,847✔
1442
  pCursor->suid = param->suid;
3,105,117✔
1443
  pCursor->cid = param->cid;
3,103,064✔
1444
  pCursor->type = param->type;
3,108,971✔
1445

1446
  metaRLock(pMeta);
3,104,499✔
1447

1448
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
3,106,907✔
1449

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

1453
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
3,108,620✔
1454

1455
  tDecoderInit(&dc, pData, nData);
3,110,061✔
1456

1457
  code = metaDecodeEntry(&dc, &oStbEntry);
3,110,061✔
1458
  if (code) {
3,108,590✔
1459
    tDecoderClear(&dc);
×
1460
    goto END;
×
1461
  }
1462

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

1467
  code = TSDB_CODE_INVALID_PARA;
3,108,590✔
1468

1469
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,125,989✔
1470
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,118,879✔
1471
    if (IS_IDX_ON(schema)) {
4,119,421✔
1472
      if (schema->colId == param->cid && param->type == schema->type) {
4,073,124✔
1473
        code = 0;
3,102,244✔
1474
        break;
3,102,244✔
1475
      }
1476
    }
1477
  }
1478

1479
  TAOS_CHECK_GOTO(code, NULL, END);
3,109,354✔
1480

1481
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,102,626✔
1482
  if (code != 0) {
3,101,893✔
1483
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1484
  }
1485

1486
  int32_t maxSize = 0;
3,101,893✔
1487
  int32_t nTagData = 0;
3,101,702✔
1488
  void   *tagData = NULL;
3,101,702✔
1489

1490
  if (param->val == NULL) {
3,101,702✔
1491
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1492
    goto END;
×
1493
  } else {
1494
    if (IS_VAR_DATA_TYPE(param->type)) {
3,100,218✔
1495
      tagData = varDataVal(param->val);
424,155✔
1496
      nTagData = varDataLen(param->val);
420,161✔
1497

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

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

1509
        tagData = buf;
79,529✔
1510
        nTagData = maxSize;
79,529✔
1511
      }
1512
    } else {
1513
      tagData = param->val;
2,682,083✔
1514
      nTagData = tDataTypes[param->type].bytes;
2,682,434✔
1515
    }
1516
  }
1517

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

1522
  int cmp = 0;
3,101,518✔
1523
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,102,053✔
1524

1525
  int     count = 0;
3,102,296✔
1526
  int32_t valid = 0;
3,102,296✔
1527
  bool    found = false;
3,102,296✔
1528

1529
  static const int8_t TRY_ERROR_LIMIT = 1;
1530

1531
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1532
  /// target:                        [suid, cid2, type2]
1533
  int diffCidCount = 0;
3,102,296✔
1534
  do {
29,228,992✔
1535
    void   *entryKey = NULL, *entryVal = NULL;
32,331,288✔
1536
    int32_t nEntryKey, nEntryVal;
32,330,663✔
1537

1538
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
32,330,535✔
1539
    if (valid < 0) {
32,317,110✔
1540
      break;
1,444,572✔
1541
    }
1542
    if (count > TRY_ERROR_LIMIT) {
30,872,538✔
1543
      break;
943,579✔
1544
    }
1545

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

1549
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
29,928,959✔
1550
      if (found == true) break;  //
1,071,311✔
1551
      if (diffCidCount > TRY_ERROR_LIMIT) break;
356,129✔
1552
      diffCidCount++;
356,129✔
1553
      count++;
356,129✔
1554
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
356,129✔
1555
      if (valid < 0) {
355,938✔
1556
        code = valid;
×
1557
        break;
×
1558
      } else {
1559
        continue;
355,938✔
1560
      }
1561
    }
1562

1563
    terrno = TSDB_CODE_SUCCESS;
28,855,264✔
1564
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
28,858,312✔
1565
    if (terrno != TSDB_CODE_SUCCESS) {
28,869,460✔
1566
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1567
      break;
×
1568
    }
1569
    if (cmp == 0) {
28,869,038✔
1570
      // match
1571
      tb_uid_t tuid = 0;
25,726,686✔
1572
      if (IS_VAR_DATA_TYPE(pKey->type)) {
25,726,178✔
1573
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
381,226✔
1574
      } else {
1575
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
25,352,996✔
1576
      }
1577
      if (taosArrayPush(pUids, &tuid) == NULL) {
25,730,073✔
1578
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1579
      }
1580
      found = true;
25,729,112✔
1581
    } else {
1582
      if (param->equal == true) {
3,142,352✔
1583
        if (count > TRY_ERROR_LIMIT) break;
2,197,533✔
1584
        count++;
2,197,533✔
1585
      }
1586
    }
1587
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
28,873,043✔
1588
    if (valid < 0) {
28,871,828✔
1589
      code = valid;
×
1590
      break;
×
1591
    }
1592
  } while (1);
1593

1594
END:
3,108,931✔
1595
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,109,901✔
1596
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,109,354✔
1597
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,108,100✔
1598
  tDecoderClear(&dc);
3,108,100✔
1599
  tdbFree(pData);
3,109,890✔
1600

1601
  taosMemoryFree(buf);
3,109,679✔
1602
  taosMemoryFree(pKey);
3,110,252✔
1603

1604
  taosMemoryFree(pCursor);
3,107,523✔
1605

1606
  return code;
3,108,961✔
1607
}
1608

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

1615
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
47,252,949✔
1616
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
47,253,464✔
1617
  if (lock) {
47,250,533✔
1618
    metaULock(pMeta);
×
1619
  }
1620

1621
  return ret;
47,250,533✔
1622
}
1623

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

1628
  int32_t isLock = false;
6,830,895✔
1629
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
6,830,895✔
1630
  for (int i = 0; i < sz; i++) {
54,081,989✔
1631
    STUidTagInfo *p = taosArrayGet(uidList, i);
47,251,940✔
1632
    if (p->pTagVal != NULL) continue;
47,252,986✔
1633

1634
    if (i % LIMIT == 0) {
47,253,102✔
1635
      if (isLock) metaULock(pMeta);
6,828,359✔
1636

1637
      metaRLock(pMeta);
6,828,359✔
1638
      isLock = true;
6,828,359✔
1639
    }
1640

1641
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1642
    void   *val = NULL;
47,253,102✔
1643
    int32_t len = 0;
47,252,740✔
1644
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
47,252,740✔
1645
      p->pTagVal = taosMemoryMalloc(len);
47,243,258✔
1646
      if (!p->pTagVal) {
47,242,822✔
1647
        if (isLock) metaULock(pMeta);
×
1648

1649
        TAOS_RETURN(terrno);
×
1650
      }
1651
      memcpy(p->pTagVal, val, len);
47,242,822✔
1652
      tdbFree(val);
47,244,531✔
1653
    } else {
1654
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
5,901✔
1655
    }
1656
  }
1657
  //  }
1658
  if (isLock) metaULock(pMeta);
6,830,049✔
1659
  return 0;
6,830,895✔
1660
}
1661

1662
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
10,383,789✔
1663
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
10,383,789✔
1664
  if (!pCur) {
10,383,807✔
1665
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1666
  }
1667

1668
  while (1) {
139,419,666✔
1669
    tb_uid_t uid = metaCtbCursorNext(pCur);
149,803,473✔
1670
    if (uid == 0) {
149,793,205✔
1671
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
10,387,925✔
1672
      break;
10,386,814✔
1673
    }
1674

1675
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
139,405,280✔
1676
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
139,408,000✔
1677
    if (!info.pTagVal) {
139,405,694✔
1678
      metaCloseCtbCursor(pCur);
×
1679
      return terrno;
×
1680
    }
1681
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
139,405,694✔
1682
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
139,420,895✔
1683
      taosMemoryFreeClear(info.pTagVal);
×
1684
      metaCloseCtbCursor(pCur);
×
1685
      return terrno;
×
1686
    }
1687
  }
1688
  metaCloseCtbCursor(pCur);
10,387,302✔
1689
  return TSDB_CODE_SUCCESS;
10,381,552✔
1690
}
1691

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

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

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

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

1718
  metaCloseStbCursor(pCur);
×
1719

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

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

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

1733
      return code;
×
1734
    }
1735

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

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

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

1746
        return code;
×
1747
      }
1748

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

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

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

1762
          return code;
×
1763
        }
1764
      }
1765

1766
      tDestroyTSchema(pTSchema);
×
1767
    }
1768

1769
    taosArrayDestroy(uids);
×
1770
  }
1771

1772
  taosArrayDestroy(suids);
×
1773

1774
  return TSDB_CODE_SUCCESS;
×
1775
}
1776

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

1779
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
2,147,483,647✔
1780
  int32_t code = 0;
2,147,483,647✔
1781
  void   *pData = NULL;
2,147,483,647✔
1782
  int     nData = 0;
2,147,483,647✔
1783
  int     lock = 0;
2,147,483,647✔
1784

1785
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
2,147,483,647✔
1786
    lock = 1;
734,248,349✔
1787
  }
1788

1789
  if (!lock) metaRLock(pMeta);
2,147,483,647✔
1790

1791
  // search cache
1792
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
2,147,483,647✔
1793
    if (!lock) metaULock(pMeta);
2,147,483,647✔
1794
    goto _exit;
2,147,483,647✔
1795
  }
1796

1797
  // search TDB
1798
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
101,219,866✔
1799
    // not found
1800
    if (!lock) metaULock(pMeta);
91,764,389✔
1801
    goto _exit;
91,756,497✔
1802
  }
1803

1804
  if (!lock) metaULock(pMeta);
9,469,193✔
1805

1806
  pInfo->uid = uid;
9,469,193✔
1807
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
9,469,193✔
1808
  pInfo->version = ((SUidIdxVal *)pData)->version;
9,469,193✔
1809
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
9,468,801✔
1810

1811
  if (lock) {
9,468,801✔
1812
    metaULock(pReader->pMeta);
1,158,632✔
1813
    // metaReaderReleaseLock(pReader);
1814
  }
1815
  // upsert the cache
1816
  metaWLock(pMeta);
9,468,167✔
1817
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
9,469,193✔
1818
  if (ret != 0) {
9,468,801✔
1819
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1820
  }
1821
  metaULock(pMeta);
9,468,801✔
1822

1823
  if (lock) {
9,468,801✔
1824
    metaRLock(pReader->pMeta);
1,158,240✔
1825
  }
1826

1827
_exit:
2,147,483,647✔
1828
  tdbFree(pData);
2,147,483,647✔
1829
  return code;
2,147,483,647✔
1830
}
1831

1832
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
124,935,016✔
1833
  int32_t code = 0;
124,935,016✔
1834

1835
  if (!numOfTables && !numOfCols) goto _exit;
124,935,016✔
1836

1837
  SVnode *pVnodeObj = pVnode;
124,935,016✔
1838
  metaRLock(pVnodeObj->pMeta);
124,935,016✔
1839

1840
  // fast path: search cache
1841
  SMetaStbStats state = {0};
124,927,334✔
1842
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
124,937,496✔
1843
    metaULock(pVnodeObj->pMeta);
120,626,112✔
1844
    if (numOfTables) *numOfTables = state.ctbNum;
120,635,451✔
1845
    if (numOfCols) *numOfCols = state.colNum;
120,628,477✔
1846
    if (flags) *flags = state.flags;
120,621,825✔
1847
    goto _exit;
120,621,825✔
1848
  }
1849

1850
  // slow path: search TDB
1851
  int64_t ctbNum = 0;
4,318,690✔
1852
  int32_t colNum = 0;
4,319,424✔
1853
  int64_t keep = 0;
4,320,045✔
1854
  int8_t  flag = 0;
4,320,045✔
1855

1856
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
4,320,877✔
1857
  if (TSDB_CODE_SUCCESS == code) {
4,320,953✔
1858
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
4,321,252✔
1859
  }
1860
  if (TSDB_CODE_SUCCESS == code) {
4,320,034✔
1861
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
4,320,632✔
1862
  }
1863
  metaULock(pVnodeObj->pMeta);
4,320,020✔
1864
  if (TSDB_CODE_SUCCESS != code) {
4,320,940✔
1865
    goto _exit;
×
1866
  }
1867

1868
  if (numOfTables) *numOfTables = ctbNum;
4,320,940✔
1869
  if (numOfCols) *numOfCols = colNum;
4,320,940✔
1870
  if (flags) *flags = flag;
4,320,923✔
1871

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

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

1886
  metaULock(pVnodeObj->pMeta);
4,320,343✔
1887

1888
_exit:
124,943,399✔
1889
  return code;
124,950,788✔
1890
}
1891

1892
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
81,657,016✔
1893
  SMetaStbStats stats = {0};
81,657,016✔
1894

1895
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
81,663,735✔
1896
    stats.ctbNum += deltaCtb;
71,008,398✔
1897
    stats.colNum += deltaCol;
71,008,398✔
1898
    if (deltaKeep > 0) {
71,008,398✔
1899
      stats.keep = deltaKeep;
5,646✔
1900
    }
1901

1902
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
71,008,398✔
1903
    if (code) {
70,991,497✔
1904
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1905
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1906
    }
1907
  }
1908
}
81,656,535✔
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