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

taosdata / TDengine / #5008

29 Mar 2026 04:32AM UTC coverage: 72.241% (-0.009%) from 72.25%
#5008

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253593 of 351039 relevant lines covered (72.24%)

130989730.42 hits per line

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

59.14
/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) {
766,278,906✔
20
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
766,278,906✔
21
  metaReaderDoInit(pReader, pMeta, flags);
766,459,061✔
22
  pReader->pAPI = pAPI;
766,472,637✔
23
}
766,478,121✔
24

25
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
879,868,454✔
26
  memset(pReader, 0, sizeof(*pReader));
879,868,454✔
27
  pReader->pMeta = pMeta;
879,868,454✔
28
  pReader->flags = flags;
879,896,998✔
29
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
879,790,591✔
30
    metaRLock(pMeta);
759,029,644✔
31
  }
32
}
879,930,117✔
33

34
void metaReaderReleaseLock(SMetaReader *pReader) {
39,130,877✔
35
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
39,130,877✔
36
    metaULock(pReader->pMeta);
39,142,063✔
37
    pReader->flags |= META_READER_NOLOCK;
39,120,065✔
38
  }
39
}
39,118,679✔
40

41
void metaReaderClear(SMetaReader *pReader) {
1,078,626,895✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,078,626,895✔
43
    metaULock(pReader->pMeta);
719,820,970✔
44
  }
45
  tDecoderClear(&pReader->coder);
1,078,466,568✔
46
  tdbFree(pReader->pBuf);
1,078,875,533✔
47
  pReader->pBuf = NULL;
1,078,643,548✔
48
}
1,078,694,171✔
49

50
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
1,233,704,238✔
51
  int32_t  code = 0;
1,233,704,238✔
52
  SMeta   *pMeta = pReader->pMeta;
1,233,704,238✔
53
  STbDbKey tbDbKey = {.version = version, .uid = uid};
1,233,934,350✔
54

55
  // query table.db
56
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
1,233,935,741✔
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,233,823,262✔
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
1,233,801,771✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
1,233,832,327✔
65
  if (code) {
1,233,595,463✔
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
1,233,595,463✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
146,466,400✔
79
    metaULock(pVnodeObj->pMeta);
10,437✔
80
    return false;
10,437✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
146,470,297✔
84
  return true;
146,476,110✔
85
}
86

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

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

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
199,159,494✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
199,156,402✔
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:%" PRId64 " not found", __func__, uid, *version);
×
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) {
47,582,018✔
150
  if (version < 0) {
47,582,018✔
151
    return metaReaderGetTableEntryByUid(pReader, uid);
47,581,783✔
152
  }
153
  SMeta *pMeta = pReader->pMeta;
239✔
154

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

170
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
765,835,283✔
171
  SMeta *pMeta = pReader->pMeta;
765,835,283✔
172

173
  SMetaInfo info;
766,005,937✔
174
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
765,978,878✔
175
  if (TSDB_CODE_SUCCESS != code) {
765,941,183✔
176
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
6,563✔
177
  }
178

179
  return metaGetTableEntryByVersion(pReader, info.version, uid);
765,934,620✔
180
}
181

182
int metaGetTableEntryByVersionName(SMetaReader *pReader, int64_t version, const char *name) {
56,625,162✔
183
  SMeta   *pMeta = pReader->pMeta;
56,625,162✔
184
  tb_uid_t uid;
185

186
  // query name.idx
187
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
56,627,529✔
188
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
9,346,085✔
189
  }
190

191
  uid = *(tb_uid_t *)pReader->pBuf;
47,278,948✔
192
  return metaReaderGetTableEntryByVersionUid(pReader, version, uid);
47,279,068✔
193
}
194

195
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
56,574,692✔
196
  return metaGetTableEntryByVersionName(pReader, -1, name);
56,574,692✔
197
}
198

199
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
74,363,208✔
200
  void    *pData = NULL;
74,363,208✔
201
  int      nData = 0;
74,364,199✔
202
  tb_uid_t uid = 0;
74,364,649✔
203

204
  metaRLock(pMeta);
74,364,649✔
205

206
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
74,363,617✔
207
    uid = *(tb_uid_t *)pData;
10,021,178✔
208
    tdbFree(pData);
10,021,178✔
209
  }
210

211
  metaULock(pMeta);
74,358,552✔
212

213
  return uid;
74,358,733✔
214
}
215

216
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
7,364,603✔
217
  int         code = 0;
7,364,603✔
218
  SMetaReader mr = {0};
7,364,603✔
219
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
7,364,603✔
220
  code = metaReaderGetTableEntryByUid(&mr, uid);
7,364,476✔
221
  if (code < 0) {
7,365,051✔
222
    metaReaderClear(&mr);
263✔
223
    return code;
263✔
224
  }
225

226
  STR_TO_VARSTR(tbName, mr.me.name);
7,364,788✔
227
  metaReaderClear(&mr);
7,364,788✔
228

229
  return 0;
7,365,505✔
230
}
231

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

244
  return 0;
×
245
}
246

247
int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) {
696,708✔
248
  int         code = 0;
696,708✔
249
  SMetaReader mr = {0};
696,708✔
250
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
696,708✔
251

252
  SMetaReader *pReader = &mr;
696,729✔
253

254
  // query name.idx
255
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
696,729✔
256
    metaReaderClear(&mr);
285,695✔
257
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
285,695✔
258
  }
259

260
  *uid = *(tb_uid_t *)pReader->pBuf;
411,034✔
261

262
  metaReaderClear(&mr);
411,013✔
263

264
  return 0;
411,026✔
265
}
266

267
int metaGetTableTypeSuidByName(void *pVnode, char *tbName, ETableType *tbType, uint64_t *suid) {
409,640✔
268
  int         code = 0;
409,640✔
269
  SMetaReader mr = {0};
409,640✔
270
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
409,640✔
271

272
  code = metaGetTableEntryByName(&mr, tbName);
409,640✔
273
  if (code == 0) *tbType = mr.me.type;
409,640✔
274
  if (TSDB_CHILD_TABLE == mr.me.type || TSDB_VIRTUAL_CHILD_TABLE == mr.me.type) {
409,640✔
275
    *suid = mr.me.ctbEntry.suid;
403,203✔
276
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
6,437✔
277
    *suid = mr.me.uid;
4,913✔
278
  } else {
279
    *suid = 0;
1,524✔
280
  }
281

282
  metaReaderClear(&mr);
409,640✔
283
  return code;
409,640✔
284
}
285

286
int metaReadNext(SMetaReader *pReader) {
×
287
  SMeta *pMeta = pReader->pMeta;
×
288

289
  // TODO
290

291
  return 0;
×
292
}
293

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

310
  code = 0;
×
311

312
_exit:
×
313
  metaReaderClear(&mr);
×
314
  return code;
×
315
}
316

317
#if 1  // ===================================================
318
SMTbCursor *metaOpenTbCursor(void *pVnode) {
10,765,113✔
319
  SMTbCursor *pTbCur = NULL;
10,765,113✔
320
  int32_t     code;
321

322
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
10,765,113✔
323
  if (pTbCur == NULL) {
10,762,917✔
324
    return NULL;
×
325
  }
326

327
  SVnode *pVnodeObj = pVnode;
10,762,917✔
328
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
329
  pTbCur->pMeta = pVnodeObj->pMeta;
10,762,917✔
330
  pTbCur->paused = 1;
10,764,645✔
331
  code = metaResumeTbCursor(pTbCur, 1, 0);
10,764,645✔
332
  if (code) {
10,762,467✔
333
    terrno = code;
×
334
    taosMemoryFree(pTbCur);
×
335
    return NULL;
×
336
  }
337
  return pTbCur;
10,762,467✔
338
}
339

340
void metaCloseTbCursor(SMTbCursor *pTbCur) {
21,527,093✔
341
  if (pTbCur) {
21,527,093✔
342
    tdbFree(pTbCur->pKey);
10,765,803✔
343
    tdbFree(pTbCur->pVal);
10,765,803✔
344
    if (!pTbCur->paused) {
10,766,396✔
345
      metaReaderClear(&pTbCur->mr);
1,636,081✔
346
      if (pTbCur->pDbc) {
1,636,081✔
347
        tdbTbcClose((TBC *)pTbCur->pDbc);
1,636,081✔
348
      }
349
    }
350
    taosMemoryFree(pTbCur);
10,765,803✔
351
  }
352
}
21,526,500✔
353

354
void metaPauseTbCursor(SMTbCursor *pTbCur) {
9,132,214✔
355
  if (!pTbCur->paused) {
9,132,214✔
356
    metaReaderClear(&pTbCur->mr);
9,132,214✔
357
    tdbTbcClose((TBC *)pTbCur->pDbc);
9,132,120✔
358
    pTbCur->paused = 1;
9,132,729✔
359
  }
360
}
9,132,729✔
361
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
10,766,816✔
362
  int32_t code = 0;
10,766,816✔
363
  int32_t lino;
364
  int8_t  locked = 0;
10,766,816✔
365
  if (pTbCur->paused) {
10,766,816✔
366
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
10,766,926✔
367
    locked = 1;
10,767,050✔
368
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
10,767,050✔
369
    if (code != 0) {
10,766,724✔
370
      TSDB_CHECK_CODE(code, lino, _exit);
×
371
    }
372

373
    if (first) {
10,766,724✔
374
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
10,764,310✔
375
      TSDB_CHECK_CODE(code, lino, _exit);
10,759,314✔
376
    } else {
377
      int c = 1;
2,414✔
378
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
2,414✔
379
      TSDB_CHECK_CODE(code, lino, _exit);
2,414✔
380
      if (c == 0) {
2,414✔
381
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
2,414✔
382
      } else if (c < 0) {
×
383
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
384
        TSDB_CHECK_CODE(code, lino, _exit);
×
385
      } else {
386
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
387
        TSDB_CHECK_CODE(code, lino, _exit);
×
388
      }
389
    }
390

391
    pTbCur->paused = 0;
10,764,274✔
392
  }
393

394
_exit:
×
395
  if (code != 0 && locked) {
10,763,462✔
396
    metaReaderReleaseLock(&pTbCur->mr);
×
397
  }
398
  return code;
10,762,383✔
399
}
400

401
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
279,556,764✔
402
  int    ret;
403
  void  *pBuf;
404
  STbCfg tbCfg;
405

406
  for (;;) {
407
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
279,556,764✔
408
    if (ret < 0) {
279,555,073✔
409
      return ret;
10,761,926✔
410
    }
411

412
    tDecoderClear(&pTbCur->mr.coder);
268,793,147✔
413

414
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
268,783,629✔
415
    if (ret) return ret;
268,787,629✔
416

417
    if (pTbCur->mr.me.type == jumpTableType) {
268,787,629✔
418
      continue;
4,296,132✔
419
    }
420

421
    break;
264,479,985✔
422
  }
423

424
  return 0;
264,479,985✔
425
}
426

427
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
428
  int    ret;
429
  void  *pBuf;
430
  STbCfg tbCfg;
431

432
  for (;;) {
433
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
434
    if (ret < 0) {
×
435
      return -1;
×
436
    }
437

438
    tDecoderClear(&pTbCur->mr.coder);
×
439

440
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
441
    if (ret < 0) {
×
442
      return ret;
×
443
    }
444

445
    if (pTbCur->mr.me.type == jumpTableType) {
×
446
      continue;
×
447
    }
448

449
    break;
×
450
  }
451

452
  return 0;
×
453
}
454

455
/**
456
 * @param type 0x01 fetchRsmaSchema if table is rsma
457
 */
458
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
321,481,046✔
459
                                   int8_t type) {
460
  int32_t         code = 0;
321,481,046✔
461
  void           *pData = NULL;
321,481,046✔
462
  int             nData = 0;
321,486,633✔
463
  int64_t         version;
464
  SSchemaWrapper  schema = {0};
321,497,916✔
465
  SSchemaWrapper *pSchema = NULL;
321,425,694✔
466
  SDecoder        dc = {0};
321,425,694✔
467
  if (lock) {
321,233,931✔
468
    metaRLock(pMeta);
316,700,450✔
469
  }
470
_query:
355,382,127✔
471
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
355,471,140✔
472
    goto _err;
171,060✔
473
  }
474

475
  version = ((SUidIdxVal *)pData)[0].version;
355,335,437✔
476

477
  if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData)) !=
355,234,897✔
478
      0) {
479
    goto _err;
×
480
  }
481

482
  SMetaEntry me = {0};
355,351,990✔
483
  tDecoderInit(&dc, pData, nData);
355,267,101✔
484
  code = metaDecodeEntry(&dc, &me);
355,349,365✔
485
  if (code) {
355,241,281✔
486
    tDecoderClear(&dc);
×
487
    goto _err;
×
488
  }
489
  if (me.type == TSDB_SUPER_TABLE) {
355,241,281✔
490
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
256,773,121✔
491
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
256,735,642✔
492
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
256,735,642✔
493
      if (TABLE_IS_ROLLUP(me.flags)) {
256,744,644✔
494
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
109,109✔
495
          tDecoderClear(&dc);
×
496
          goto _err;
×
497
        }
498
      }
499
      tDecoderClear(&dc);
256,744,644✔
500
      goto _exit;
256,702,103✔
501
    }
502
  } else if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
98,468,160✔
503
    uid = me.ctbEntry.suid;
33,874,190✔
504
    tDecoderClear(&dc);
33,874,190✔
505
    goto _query;
33,881,739✔
506
  } else {
507
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
64,593,970✔
508
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
64,604,387✔
509
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
64,604,387✔
510
      tDecoderClear(&dc);
64,623,028✔
511
      goto _exit;
64,620,373✔
512
    }
513
  }
514
  if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
32,740✔
515
  tDecoderClear(&dc);
32,740✔
516

517
  // query from skm db
518
  if ((code = tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) < 0) {
23,954✔
519
    goto _err;
×
520
  }
521

522
  tDecoderInit(&dc, pData, nData);
23,954✔
523
  if ((code = tDecodeSSchemaWrapperEx(&dc, &schema)) != 0) {
23,954✔
524
    goto _err;
×
525
  }
526
  pSchema = tCloneSSchemaWrapper(&schema);
23,954✔
527
  if (pSchema == NULL) {
23,954✔
528
    code = terrno;
×
529
    goto _err;
×
530
  }
531
  tDecoderClear(&dc);
23,954✔
532

533
_exit:
321,346,430✔
534
  if (lock) {
321,372,440✔
535
    metaULock(pMeta);
316,819,948✔
536
  }
537
  tdbFree(pData);
321,325,886✔
538
  return pSchema;
321,355,066✔
539

540
_err:
172,253✔
541
  if (lock) {
171,060✔
542
    metaULock(pMeta);
170,490✔
543
  }
544
  tdbFree(pData);
170,830✔
545
  tDeleteSchemaWrapper(pSchema);
546
  if (extSchema != NULL) {
171,060✔
547
    taosMemoryFreeClear(*extSchema);
7,950✔
548
  }
549
  terrno = code;
171,060✔
550
  return NULL;
170,830✔
551
}
552

553
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
623,309✔
554
  void    *pData = NULL;
623,309✔
555
  int      nData = 0;
623,309✔
556
  int64_t  version = 0;
623,309✔
557
  SDecoder dc = {0};
623,309✔
558
  int64_t  createTime = INT64_MAX;
623,309✔
559
  if (lock) {
623,309✔
560
    metaRLock(pMeta);
623,309✔
561
  }
562

563
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
623,309✔
564
    goto _exit;
×
565
  }
566

567
  version = ((SUidIdxVal *)pData)[0].version;
623,309✔
568

569
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
623,309✔
570
    goto _exit;
×
571
  }
572

573
  SMetaEntry me = {0};
623,309✔
574
  tDecoderInit(&dc, pData, nData);
623,309✔
575
  int32_t code = metaDecodeEntry(&dc, &me);
623,309✔
576
  if (code) {
623,309✔
577
    tDecoderClear(&dc);
×
578
    goto _exit;
×
579
  }
580
  if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
623,309✔
581
    createTime = me.ctbEntry.btime;
622,969✔
582
  } else if (me.type == TSDB_NORMAL_TABLE || me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
340✔
583
    createTime = me.ntbEntry.btime;
340✔
584
  }
585
  tDecoderClear(&dc);
623,309✔
586

587
_exit:
623,309✔
588
  if (lock) {
623,309✔
589
    metaULock(pMeta);
623,309✔
590
  }
591
  tdbFree(pData);
623,309✔
592
  return createTime;
623,309✔
593
}
594

595
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
117,732,586✔
596
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
117,732,586✔
597
  SMCtbCursor *pCtbCur = NULL;
117,733,623✔
598
  SCtbIdxKey   ctbIdxKey;
599
  int          ret = 0;
117,733,623✔
600
  int          c = 0;
117,733,623✔
601

602
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
117,733,623✔
603
  if (pCtbCur == NULL) {
117,662,995✔
604
    return NULL;
×
605
  }
606

607
  pCtbCur->pMeta = pMeta;
117,662,995✔
608
  pCtbCur->suid = uid;
117,663,540✔
609
  pCtbCur->lock = lock;
117,673,226✔
610
  pCtbCur->paused = 1;
117,732,563✔
611

612
  ret = metaResumeCtbCursor(pCtbCur, 1);
117,721,274✔
613
  if (ret < 0) {
117,692,163✔
614
    return NULL;
×
615
  }
616
  return pCtbCur;
117,692,163✔
617
}
618

619
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
117,748,890✔
620
  if (pCtbCur) {
117,748,890✔
621
    if (!pCtbCur->paused) {
117,750,046✔
622
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
113,075,461✔
623
      if (pCtbCur->pCur) {
113,066,102✔
624
        tdbTbcClose(pCtbCur->pCur);
113,071,883✔
625
      }
626
    }
627
    tdbFree(pCtbCur->pKey);
117,730,273✔
628
    tdbFree(pCtbCur->pVal);
117,725,413✔
629
  }
630
  taosMemoryFree(pCtbCur);
117,712,693✔
631
}
117,714,807✔
632

633
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
4,675,527✔
634
  if (!pCtbCur->paused) {
4,675,527✔
635
    tdbTbcClose((TBC *)pCtbCur->pCur);
4,676,122✔
636
    if (pCtbCur->lock) {
4,677,150✔
637
      metaULock(pCtbCur->pMeta);
4,676,782✔
638
    }
639
    pCtbCur->paused = 1;
4,676,406✔
640
  }
641
}
4,679,081✔
642

643
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
117,728,836✔
644
  if (pCtbCur->paused) {
117,728,836✔
645
    pCtbCur->paused = 0;
117,705,182✔
646

647
    if (pCtbCur->lock) {
117,735,895✔
648
      metaRLock(pCtbCur->pMeta);
113,180,510✔
649
    }
650
    int ret = 0;
117,721,639✔
651
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
117,721,639✔
652
    if (ret < 0) {
117,707,524✔
653
      metaCloseCtbCursor(pCtbCur);
×
654
      return -1;
×
655
    }
656

657
    if (first) {
117,707,524✔
658
      SCtbIdxKey ctbIdxKey;
117,702,788✔
659
      // move to the suid
660
      ctbIdxKey.suid = pCtbCur->suid;
117,709,978✔
661
      ctbIdxKey.uid = INT64_MIN;
117,721,244✔
662
      int c = 0;
117,721,244✔
663
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
117,704,905✔
664
      if (c > 0) {
117,714,281✔
665
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
14,619,027✔
666
      }
667
    } else {
668
      int c = 0;
×
669
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
670
      if (c < 0) {
×
671
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
672
      } else {
673
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
674
      }
675
    }
676
  }
677
  return 0;
117,716,944✔
678
}
679

680
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
746,762,925✔
681
  int         ret;
682
  SCtbIdxKey *pCtbIdxKey;
683

684
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
746,762,925✔
685
  if (ret < 0) {
746,888,730✔
686
    return 0;
64,296,030✔
687
  }
688

689
  pCtbIdxKey = pCtbCur->pKey;
682,592,700✔
690
  if (pCtbIdxKey->suid > pCtbCur->suid) {
682,597,968✔
691
    return 0;
53,572,985✔
692
  }
693

694
  return pCtbIdxKey->uid;
629,107,420✔
695
}
696

697
struct SMStbCursor {
698
  SMeta   *pMeta;
699
  TBC     *pCur;
700
  tb_uid_t suid;
701
  void    *pKey;
702
  void    *pVal;
703
  int      kLen;
704
  int      vLen;
705
};
706

707
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
65,339,344✔
708
  SMStbCursor *pStbCur = NULL;
65,339,344✔
709
  int          ret = 0;
65,339,344✔
710
  int          c = 0;
65,339,344✔
711

712
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
65,339,910✔
713
  if (pStbCur == NULL) {
65,333,979✔
714
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
715
    return NULL;
×
716
  }
717

718
  pStbCur->pMeta = pMeta;
65,333,979✔
719
  pStbCur->suid = suid;
65,339,251✔
720
  metaRLock(pMeta);
65,337,933✔
721

722
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
65,339,910✔
723
  if (ret < 0) {
65,335,610✔
724
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
725
    metaULock(pMeta);
×
726
    taosMemoryFree(pStbCur);
×
727
    return NULL;
×
728
  }
729

730
  // move to the suid
731
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
65,335,610✔
732
  if (c > 0) {
65,341,228✔
733
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
734
  }
735

736
  return pStbCur;
65,340,569✔
737
}
738

739
void metaCloseStbCursor(SMStbCursor *pStbCur) {
65,340,223✔
740
  if (pStbCur) {
65,340,223✔
741
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
65,340,223✔
742
    if (pStbCur->pCur) {
65,341,887✔
743
      tdbTbcClose(pStbCur->pCur);
65,341,228✔
744

745
      tdbFree(pStbCur->pKey);
65,341,228✔
746
      tdbFree(pStbCur->pVal);
65,335,956✔
747
    }
748

749
    taosMemoryFree(pStbCur);
65,337,587✔
750
  }
751
}
65,339,910✔
752

753
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
112,182,096✔
754
  int ret;
755

756
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
112,182,096✔
757
  if (ret < 0) {
112,185,045✔
758
    return 0;
65,340,882✔
759
  }
760
  return *(tb_uid_t *)pStbCur->pKey;
46,844,163✔
761
}
762

763
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
269,538,958✔
764
  STSchema       *pTSchema = NULL;
269,538,958✔
765
  SSchemaWrapper *pSW = NULL;
269,538,958✔
766

767
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0);
269,538,958✔
768
  if (!pSW) return NULL;
269,555,441✔
769

770
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
269,520,111✔
771

772
  tDeleteSchemaWrapper(pSW);
773
  return pTSchema;
269,419,972✔
774
}
775

776
/**
777
 * Fetch rsma schema if table is rsma
778
 */
779
SRSchema *metaGetTbTSchemaR(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
28,231✔
780
  SRSchema       *pRSchema = NULL;
28,231✔
781
  SSchemaWrapper *pSW = NULL;
28,231✔
782

783
  if (!(pRSchema = (SRSchema *)taosMemoryCalloc(1, sizeof(SRSchema)))) goto _err;
28,231✔
784
  if (!(pSW = metaGetTableSchema(pMeta, uid, sver, lock, (SExtSchema **)&pRSchema->extSchema, 0x01))) goto _err;
28,231✔
785
  if (!(pRSchema->tSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version))) goto _err;
28,231✔
786

787
  if (pSW->pRsma) {
28,231✔
788
    if (!(pRSchema->funcIds = taosMemoryCalloc(pSW->nCols, sizeof(func_id_t)))) goto _err;
28,231✔
789
    memcpy(pRSchema->funcIds, pSW->pRsma->funcIds, pSW->nCols * sizeof(func_id_t));
28,231✔
790

791
    pRSchema->tbType = pSW->pRsma->tbType;
28,231✔
792
    pRSchema->tbUid = uid;
28,231✔
793
    tstrncpy(pRSchema->tbName, pSW->pRsma->tbName, TSDB_TABLE_NAME_LEN);
28,231✔
794
    pRSchema->interval[0] = pSW->pRsma->interval[0];
28,231✔
795
    pRSchema->interval[1] = pSW->pRsma->interval[1];
28,231✔
796
  }
797

798
_exit:
28,231✔
799
  tDeleteSchemaWrapper(pSW);
800
  return pRSchema;
28,231✔
801
_err:
×
802
  tDeleteSchemaWrapper(pSW);
803
  tFreeSRSchema(&pRSchema);
804
  return NULL;
×
805
}
806

807
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
629,396✔
808
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
629,396✔
809
  if (*ppTSchema == NULL) {
629,396✔
810
    return terrno;
×
811
  }
812
  return TSDB_CODE_SUCCESS;
630,149✔
813
}
814

815
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
268,818,354✔
816
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
268,818,354✔
817
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
268,712,038✔
818
    return terrno;
×
819
  }
820
  return TSDB_CODE_SUCCESS;
268,777,983✔
821
}
822

823
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
70,782,098✔
824
  int32_t code = 0;
70,782,098✔
825
  int32_t lino;
826

827
  void     *pData = NULL;
70,782,098✔
828
  int       nData = 0;
70,801,686✔
829
  SSkmDbKey skmDbKey;
70,811,271✔
830
  if (sver <= 0) {
70,807,161✔
831
    SMetaInfo info;
35,558,680✔
832
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
35,565,167✔
833
      sver = info.skmVer;
35,511,862✔
834
    } else {
835
      TBC *pSkmDbC = NULL;
51,367✔
836
      int  c;
35,330✔
837

838
      skmDbKey.uid = suid ? suid : uid;
35,330✔
839
      skmDbKey.sver = INT32_MAX;
35,330✔
840

841
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
35,330✔
842
      TSDB_CHECK_CODE(code, lino, _exit);
35,330✔
843
      metaRLock(pMeta);
35,330✔
844

845
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
35,330✔
846
        metaULock(pMeta);
×
847
        tdbTbcClose(pSkmDbC);
×
848
        code = TSDB_CODE_NOT_FOUND;
×
849
        goto _exit;
×
850
      }
851

852
      if (c == 0) {
35,330✔
853
        metaULock(pMeta);
×
854
        tdbTbcClose(pSkmDbC);
×
855
        code = TSDB_CODE_FAILED;
×
856
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
857
        goto _exit;
×
858
      }
859

860
      if (c < 0) {
35,330✔
861
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
862
      }
863

864
      const void *pKey = NULL;
35,330✔
865
      int32_t     nKey = 0;
35,330✔
866
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
35,330✔
867

868
      if (ret != 0 || ((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
35,330✔
869
        metaULock(pMeta);
×
870
        tdbTbcClose(pSkmDbC);
×
871
        code = TSDB_CODE_NOT_FOUND;
×
872
        goto _exit;
×
873
      }
874

875
      sver = ((SSkmDbKey *)pKey)->sver;
35,330✔
876

877
      metaULock(pMeta);
35,330✔
878
      tdbTbcClose(pSkmDbC);
35,330✔
879
    }
880
  }
881

882
  if (!(sver > 0)) {
70,805,212✔
883
    code = TSDB_CODE_NOT_FOUND;
×
884
    goto _exit;
×
885
  }
886

887
  skmDbKey.uid = suid ? suid : uid;
70,805,212✔
888
  skmDbKey.sver = sver;
70,805,212✔
889
  metaRLock(pMeta);
70,805,212✔
890
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
70,812,785✔
891
    metaULock(pMeta);
2,800✔
892
    code = TSDB_CODE_NOT_FOUND;
2,800✔
893
    goto _exit;
2,800✔
894
  }
895
  metaULock(pMeta);
70,782,380✔
896

897
  // decode
898
  SDecoder        dc = {0};
70,812,719✔
899
  SSchemaWrapper  schema;
70,794,007✔
900
  SSchemaWrapper *pSchemaWrapper = &schema;
70,806,529✔
901

902
  tDecoderInit(&dc, pData, nData);
70,806,529✔
903
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
70,833,821✔
904
  tDecoderClear(&dc);
70,833,821✔
905
  tdbFree(pData);
70,807,634✔
906
  if (TSDB_CODE_SUCCESS != code) {
70,773,853✔
907
    taosMemoryFree(pSchemaWrapper->pSchema);
×
908
    goto _exit;
×
909
  }
910

911
  // convert
912
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
70,773,853✔
913
  if (pTSchema == NULL) {
70,817,521✔
914
    code = TSDB_CODE_OUT_OF_MEMORY;
×
915
  }
916

917
  *ppTSchema = pTSchema;
70,817,521✔
918
  taosMemoryFree(pSchemaWrapper->pSchema);
70,823,469✔
919

920
_exit:
70,783,982✔
921
  return code;
70,785,675✔
922
}
923

924
// N.B. Called by statusReq per second
925
int64_t metaGetTbNum(SMeta *pMeta) {
132,374,444✔
926
  // num of child tables (excluding normal tables , stables and others)
927

928
  /* int64_t num = 0; */
929
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
930

931
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
132,374,444✔
932
}
933

934
void metaUpdTimeSeriesNum(SMeta *pMeta) {
65,333,249✔
935
  int64_t nCtbTimeSeries = 0;
65,333,249✔
936
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
65,333,908✔
937
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
65,334,254✔
938
  }
939
}
65,334,913✔
940

941
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
942
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
943
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
216,677,747✔
944
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
216,680,585✔
945
    metaUpdTimeSeriesNum(pMeta);
65,330,080✔
946
  }
947

948
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
216,686,899✔
949
}
950

951
// type: 1 reported timeseries
952
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
216,668,590✔
953
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
216,681,394✔
954
  if (type == 1) {
216,681,394✔
955
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
132,374,444✔
956
  }
957
  return nTimeSeries;
216,668,310✔
958
}
959

960
typedef struct {
961
  SMeta   *pMeta;
962
  TBC     *pCur;
963
  tb_uid_t uid;
964
  void    *pKey;
965
  void    *pVal;
966
  int      kLen;
967
  int      vLen;
968
} SMSmaCursor;
969

970
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
971
  SMSmaCursor *pSmaCur = NULL;
×
972
  SSmaIdxKey   smaIdxKey;
×
973
  int          ret;
974
  int          c;
×
975

976
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
977
  if (pSmaCur == NULL) {
×
978
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
979
    return NULL;
×
980
  }
981

982
  pSmaCur->pMeta = pMeta;
×
983
  pSmaCur->uid = uid;
×
984
  metaRLock(pMeta);
×
985

986
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
987
  if (ret < 0) {
×
988
    metaULock(pMeta);
×
989
    taosMemoryFree(pSmaCur);
×
990
    return NULL;
×
991
  }
992

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

1001
  return pSmaCur;
×
1002
}
1003

1004
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
1005
  if (pSmaCur) {
×
1006
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
1007
    if (pSmaCur->pCur) {
×
1008
      tdbTbcClose(pSmaCur->pCur);
×
1009
      pSmaCur->pCur = NULL;
×
1010

1011
      tdbFree(pSmaCur->pKey);
×
1012
      tdbFree(pSmaCur->pVal);
×
1013
    }
1014

1015
    taosMemoryFree(pSmaCur);
×
1016
  }
1017
}
×
1018

1019
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
1020
  int         ret;
1021
  SSmaIdxKey *pSmaIdxKey;
1022

1023
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
1024
  if (ret < 0) {
×
1025
    return 0;
×
1026
  }
1027

1028
  pSmaIdxKey = pSmaCur->pKey;
×
1029
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
1030
    return 0;
×
1031
  }
1032

1033
  return pSmaIdxKey->uid;
×
1034
}
1035

1036
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
1037
  STSmaWrapper *pSW = NULL;
×
1038
  SArray       *pSmaIds = NULL;
×
1039

1040
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
1041
    return NULL;
×
1042
  }
1043

1044
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
1045
  if (!pSW) {
×
1046
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1047
    goto _err;
×
1048
  }
1049

1050
  pSW->number = taosArrayGetSize(pSmaIds);
×
1051
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
1052

1053
  if (!pSW->tSma) {
×
1054
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1055
    goto _err;
×
1056
  }
1057

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

1095
    ++smaIdx;
×
1096
  }
1097

1098
  if (smaIdx <= 0) goto _err;
×
1099
  pSW->number = smaIdx;
×
1100

1101
  metaReaderClear(&mr);
×
1102
  taosArrayDestroy(pSmaIds);
×
1103
  return pSW;
×
1104
_err:
×
1105
  metaReaderClear(&mr);
×
1106
  taosArrayDestroy(pSmaIds);
×
1107
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
1108
  return NULL;
×
1109
}
1110

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

1127
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
1128

1129
  metaReaderClear(&mr);
×
1130
  return pTSma;
×
1131
}
1132

1133
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1134
  SArray     *pUids = NULL;
×
1135
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1136

1137
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1138
  if (!pCur) {
×
1139
    return NULL;
×
1140
  }
1141

1142
  while (1) {
×
1143
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1144
    if (id == 0) {
×
1145
      break;
×
1146
    }
1147

1148
    if (!pUids) {
×
1149
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1150
      if (!pUids) {
×
1151
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1152
        metaCloseSmaCursor(pCur);
×
1153
        return NULL;
×
1154
      }
1155
    }
1156

1157
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1158

1159
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1160
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1161
      metaCloseSmaCursor(pCur);
×
1162
      taosArrayDestroy(pUids);
×
1163
      return NULL;
×
1164
    }
1165
  }
1166

1167
  metaCloseSmaCursor(pCur);
×
1168
  return pUids;
×
1169
}
1170

1171
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1172
  SArray     *pUids = NULL;
×
1173
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1174
  tb_uid_t    lastUid = 0;
×
1175

1176
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1177
  if (!pCur) {
×
1178
    return NULL;
×
1179
  }
1180

1181
  while (1) {
×
1182
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1183
    if (uid == 0) {
×
1184
      break;
×
1185
    }
1186

1187
    if (lastUid == uid) {
×
1188
      continue;
×
1189
    }
1190

1191
    lastUid = uid;
×
1192

1193
    if (!pUids) {
×
1194
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1195
      if (!pUids) {
×
1196
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1197
        metaCloseSmaCursor(pCur);
×
1198
        return NULL;
×
1199
      }
1200
    }
1201

1202
    if (!taosArrayPush(pUids, &uid)) {
×
1203
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1204
      metaCloseSmaCursor(pCur);
×
1205
      taosArrayDestroy(pUids);
×
1206
      return NULL;
×
1207
    }
1208
  }
1209

1210
  metaCloseSmaCursor(pCur);
×
1211
  return pUids;
×
1212
}
1213

1214
#endif
1215

1216
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
1,133,315,852✔
1217
  STag *tag = (STag *)pTag;
1,133,315,852✔
1218
  if (type == TSDB_DATA_TYPE_JSON) {
1,133,315,852✔
1219
    return tag;
1,467,711✔
1220
  }
1221
  bool find = tTagGet(tag, val);
1,131,848,141✔
1222

1223
  if (!find) {
1,131,815,095✔
1224
    return NULL;
7,362,200✔
1225
  }
1226

1227
  return val;
1,124,452,895✔
1228
}
1229

1230
typedef struct {
1231
  SMeta   *pMeta;
1232
  TBC     *pCur;
1233
  tb_uid_t suid;
1234
  int16_t  cid;
1235
  int16_t  type;
1236
  void    *pKey;
1237
  void    *pVal;
1238
  int32_t  kLen;
1239
  int32_t  vLen;
1240
} SIdxCursor;
1241

1242
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
1,704✔
1243
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,704✔
1244
  SMetaFltParam *param = arg;
1,704✔
1245
  int32_t        ret = 0;
1,704✔
1246

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

1257
  metaRLock(pMeta);
1,704✔
1258
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
1,704✔
1259
  if (ret != 0) {
1,704✔
1260
    goto END;
×
1261
  }
1262
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
1,704✔
1263

1264
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
1,704✔
1265
  SBtimeIdxKey *pBtimeKey = &btimeKey;
1,704✔
1266

1267
  int cmp = 0;
1,704✔
1268
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
1,704✔
1269
    goto END;
×
1270
  }
1271

1272
  int32_t valid = 0;
1,704✔
1273
  int32_t count = 0;
1,704✔
1274

1275
  static const int8_t TRY_ERROR_LIMIT = 1;
1276
  do {
6,466,251✔
1277
    void   *entryKey = NULL;
6,467,955✔
1278
    int32_t nEntryKey = -1;
6,467,793✔
1279
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
6,467,793✔
1280
    if (valid < 0) break;
6,477,189✔
1281

1282
    SBtimeIdxKey *p = entryKey;
6,475,485✔
1283
    if (count > TRY_ERROR_LIMIT) break;
6,475,485✔
1284

1285
    terrno = TSDB_CODE_SUCCESS;
6,475,485✔
1286
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
6,475,809✔
1287
    if (terrno != TSDB_CODE_SUCCESS) {
6,440,169✔
1288
      ret = terrno;
×
1289
      break;
×
1290
    }
1291
    if (cmp == 0) {
6,462,849✔
1292
      if (taosArrayPush(pUids, &p->uid) == NULL) {
12,940,116✔
1293
        ret = terrno;
×
1294
        break;
×
1295
      }
1296
    } else {
1297
      if (param->equal == true) {
×
1298
        if (count > TRY_ERROR_LIMIT) break;
×
1299
        count++;
×
1300
      }
1301
    }
1302
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
6,477,267✔
1303
    if (valid < 0) break;
6,467,871✔
1304
  } while (1);
1305

1306
END:
1,704✔
1307
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
1,704✔
1308
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
1,704✔
1309
  taosMemoryFree(pCursor);
1,704✔
1310
  return ret;
1,704✔
1311
}
1312

1313
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1314
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1315
  SMetaFltParam *param = arg;
×
1316
  int32_t        ret = 0;
×
1317
  char          *buf = NULL;
×
1318

1319
  STagIdxKey *pKey = NULL;
×
1320
  int32_t     nKey = 0;
×
1321

1322
  SIdxCursor *pCursor = NULL;
×
1323
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1324
  if (pCursor == NULL) {
×
1325
    return terrno;
×
1326
  }
1327
  pCursor->pMeta = pMeta;
×
1328
  pCursor->suid = param->suid;
×
1329
  pCursor->cid = param->cid;
×
1330
  pCursor->type = param->type;
×
1331

1332
  char *pName = param->val;
×
1333

1334
  metaRLock(pMeta);
×
1335
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1336
  if (ret != 0) {
×
1337
    goto END;
×
1338
  }
1339

1340
  int cmp = 0;
×
1341
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1342
    goto END;
×
1343
  }
1344
  int32_t valid = 0;
×
1345
  int32_t count = 0;
×
1346

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

1354
    if (count > TRY_ERROR_LIMIT) break;
×
1355

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

1381
END:
×
1382
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1383
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1384
  taosMemoryFree(buf);
×
1385
  taosMemoryFree(pKey);
×
1386

1387
  taosMemoryFree(pCursor);
×
1388

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

1397
  STtlIdxKey *pKey = NULL;
×
1398
  int32_t     nKey = 0;
×
1399

1400
  SIdxCursor *pCursor = NULL;
×
1401
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1402
  if (pCursor == NULL) {
×
1403
    return terrno;
×
1404
  }
1405
  pCursor->pMeta = pMeta;
×
1406
  pCursor->suid = param->suid;
×
1407
  pCursor->cid = param->cid;
×
1408
  pCursor->type = param->type;
×
1409

1410
  metaRLock(pMeta);
×
1411
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1412

1413
END:
×
1414
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1415
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1416
  taosMemoryFree(buf);
×
1417
  taosMemoryFree(pKey);
×
1418

1419
  taosMemoryFree(pCursor);
×
1420

1421
  return ret;
×
1422
  // impl later
1423
  return 0;
1424
}
1425
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
3,062,414✔
1426
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
3,062,414✔
1427
  SMetaFltParam *param = arg;
3,063,059✔
1428

1429
  SMetaEntry oStbEntry = {0};
3,063,059✔
1430
  int32_t    code = 0;
3,060,163✔
1431
  char      *buf = NULL;
3,060,163✔
1432
  void      *pData = NULL;
3,060,163✔
1433
  int        nData = 0;
3,061,395✔
1434

1435
  SDecoder    dc = {0};
3,062,139✔
1436
  STbDbKey    tbDbKey = {0};
3,060,748✔
1437
  STagIdxKey *pKey = NULL;
3,059,419✔
1438
  int32_t     nKey = 0;
3,057,285✔
1439

1440
  SIdxCursor *pCursor = NULL;
3,057,598✔
1441
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
3,057,598✔
1442
  if (!pCursor) {
3,058,445✔
1443
    return terrno;
×
1444
  }
1445
  pCursor->pMeta = pMeta;
3,058,445✔
1446
  pCursor->suid = param->suid;
3,057,800✔
1447
  pCursor->cid = param->cid;
3,062,258✔
1448
  pCursor->type = param->type;
3,056,582✔
1449

1450
  metaRLock(pMeta);
3,061,526✔
1451

1452
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
3,060,579✔
1453

1454
  tbDbKey.uid = param->suid;
3,062,902✔
1455
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
3,061,513✔
1456

1457
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
3,061,513✔
1458

1459
  tDecoderInit(&dc, pData, nData);
3,063,562✔
1460

1461
  code = metaDecodeEntry(&dc, &oStbEntry);
3,063,562✔
1462
  if (code) {
3,061,788✔
1463
    tDecoderClear(&dc);
×
1464
    goto END;
×
1465
  }
1466

1467
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
3,061,788✔
1468
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1469
  }
1470

1471
  code = TSDB_CODE_INVALID_PARA;
3,061,788✔
1472

1473
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
4,164,889✔
1474
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
4,158,843✔
1475
    if (IS_IDX_ON(schema)) {
4,158,568✔
1476
      if (schema->colId == param->cid && param->type == schema->type) {
4,115,999✔
1477
        code = 0;
3,055,742✔
1478
        break;
3,055,742✔
1479
      }
1480
    }
1481
  }
1482

1483
  TAOS_CHECK_GOTO(code, NULL, END);
3,061,788✔
1484

1485
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
3,055,357✔
1486
  if (code != 0) {
3,055,742✔
1487
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1488
  }
1489

1490
  int32_t maxSize = 0;
3,055,742✔
1491
  int32_t nTagData = 0;
3,055,370✔
1492
  void   *tagData = NULL;
3,055,370✔
1493

1494
  if (param->val == NULL) {
3,055,370✔
1495
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1496
    goto END;
×
1497
  } else {
1498
    if (IS_VAR_DATA_TYPE(param->type)) {
3,055,742✔
1499
      tagData = varDataVal(param->val);
260,087✔
1500
      nTagData = varDataLen(param->val);
256,164✔
1501

1502
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
256,164✔
1503
        maxSize = 4 * nTagData + 1;
68,233✔
1504
        buf = taosMemoryCalloc(1, maxSize);
68,233✔
1505
        if (buf == NULL) {
68,233✔
1506
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1507
        }
1508

1509
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
68,233✔
1510
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1511
        }
1512

1513
        tagData = buf;
68,233✔
1514
        nTagData = maxSize;
68,233✔
1515
      }
1516
    } else {
1517
      tagData = param->val;
2,796,498✔
1518
      nTagData = tDataTypes[param->type].bytes;
2,800,967✔
1519
    }
1520
  }
1521

1522
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
3,054,725✔
1523
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1524
                  NULL, END);
1525

1526
  int cmp = 0;
3,051,046✔
1527
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
3,052,063✔
1528

1529
  int     count = 0;
3,055,839✔
1530
  int32_t valid = 0;
3,055,839✔
1531
  bool    found = false;
3,055,839✔
1532

1533
  static const int8_t TRY_ERROR_LIMIT = 1;
1534

1535
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1536
  /// target:                        [suid, cid2, type2]
1537
  int diffCidCount = 0;
3,055,839✔
1538
  do {
31,009,478✔
1539
    void   *entryKey = NULL, *entryVal = NULL;
34,065,317✔
1540
    int32_t nEntryKey, nEntryVal;
34,063,410✔
1541

1542
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
34,065,324✔
1543
    if (valid < 0) {
34,065,945✔
1544
      break;
1,645,623✔
1545
    }
1546
    if (count > TRY_ERROR_LIMIT) {
32,420,322✔
1547
      break;
833,648✔
1548
    }
1549

1550
    STagIdxKey *p = entryKey;
31,586,674✔
1551
    if (p == NULL) break;
31,586,674✔
1552

1553
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
31,586,674✔
1554
      if (found == true) break;  //
729,162✔
1555
      if (diffCidCount > TRY_ERROR_LIMIT) break;
151,302✔
1556
      diffCidCount++;
151,302✔
1557
      count++;
151,302✔
1558
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
151,302✔
1559
      if (valid < 0) {
151,302✔
1560
        code = valid;
×
1561
        break;
×
1562
      } else {
1563
        continue;
151,302✔
1564
      }
1565
    }
1566

1567
    terrno = TSDB_CODE_SUCCESS;
30,854,062✔
1568
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
30,854,819✔
1569
    if (terrno != TSDB_CODE_SUCCESS) {
30,857,785✔
1570
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1571
      break;
×
1572
    }
1573
    if (cmp == 0) {
30,858,159✔
1574
      // match
1575
      tb_uid_t tuid = 0;
27,650,957✔
1576
      if (IS_VAR_DATA_TYPE(pKey->type)) {
27,650,312✔
1577
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
370,982✔
1578
      } else {
1579
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
27,275,580✔
1580
      }
1581
      if (taosArrayPush(pUids, &tuid) == NULL) {
27,652,184✔
1582
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1583
      }
1584
      found = true;
27,652,184✔
1585
    } else {
1586
      if (param->equal == true) {
3,207,202✔
1587
        if (count > TRY_ERROR_LIMIT) break;
2,198,275✔
1588
        count++;
2,198,275✔
1589
      }
1590
    }
1591
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
30,860,031✔
1592
    if (valid < 0) {
30,857,435✔
1593
      code = valid;
×
1594
      break;
×
1595
    }
1596
  } while (1);
1597

1598
END:
3,061,068✔
1599
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
3,063,357✔
1600
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
3,062,996✔
1601
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
3,062,419✔
1602
  tDecoderClear(&dc);
3,062,419✔
1603
  tdbFree(pData);
3,061,968✔
1604

1605
  taosMemoryFree(buf);
3,062,613✔
1606
  taosMemoryFree(pKey);
3,062,917✔
1607

1608
  taosMemoryFree(pCursor);
3,063,190✔
1609

1610
  return code;
3,061,886✔
1611
}
1612

1613
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
50,169,907✔
1614
  int ret = 0;
50,169,907✔
1615
  if (lock) {
50,169,907✔
1616
    metaRLock(pMeta);
×
1617
  }
1618

1619
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
50,169,907✔
1620
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
50,170,445✔
1621
  if (lock) {
50,169,901✔
1622
    metaULock(pMeta);
×
1623
  }
1624

1625
  return ret;
50,169,901✔
1626
}
1627

1628
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
7,207,390✔
1629
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
7,207,390✔
1630
  const int32_t LIMIT = 128;
7,206,731✔
1631

1632
  int32_t isLock = false;
7,206,731✔
1633
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
7,206,731✔
1634
  for (int i = 0; i < sz; i++) {
57,377,067✔
1635
    STUidTagInfo *p = taosArrayGet(uidList, i);
50,168,788✔
1636
    if (p->pTagVal != NULL) continue;
50,170,196✔
1637

1638
    if (i % LIMIT == 0) {
50,170,734✔
1639
      if (isLock) metaULock(pMeta);
7,207,032✔
1640

1641
      metaRLock(pMeta);
7,207,032✔
1642
      isLock = true;
7,207,032✔
1643
    }
1644

1645
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1646
    void   *val = NULL;
50,170,734✔
1647
    int32_t len = 0;
50,170,362✔
1648
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
50,170,734✔
1649
      p->pTagVal = taosMemoryMalloc(len);
50,164,284✔
1650
      if (!p->pTagVal) {
50,164,327✔
1651
        if (isLock) metaULock(pMeta);
×
1652

1653
        TAOS_RETURN(terrno);
×
1654
      }
1655
      memcpy(p->pTagVal, val, len);
50,163,748✔
1656
      tdbFree(val);
50,164,779✔
1657
    } else {
1658
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64, TD_VID(pMeta->pVnode), suid, p->uid);
6,155✔
1659
    }
1660
  }
1661
  //  }
1662
  if (isLock) metaULock(pMeta);
7,208,279✔
1663
  return 0;
7,207,390✔
1664
}
1665

1666
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
10,922,926✔
1667
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
10,922,926✔
1668
  if (!pCur) {
10,914,553✔
1669
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1670
  }
1671

1672
  while (1) {
153,886,726✔
1673
    tb_uid_t uid = metaCtbCursorNext(pCur);
164,801,279✔
1674
    if (uid == 0) {
164,862,480✔
1675
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
10,926,845✔
1676
      break;
10,926,321✔
1677
    }
1678

1679
    STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
153,935,635✔
1680
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
153,945,761✔
1681
    if (!info.pTagVal) {
153,878,723✔
1682
      metaCloseCtbCursor(pCur);
×
1683
      return terrno;
×
1684
    }
1685
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
153,878,723✔
1686
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
153,883,409✔
1687
      taosMemoryFreeClear(info.pTagVal);
×
1688
      metaCloseCtbCursor(pCur);
×
1689
      return terrno;
×
1690
    }
1691
  }
1692
  metaCloseCtbCursor(pCur);
10,926,845✔
1693
  return TSDB_CODE_SUCCESS;
10,923,894✔
1694
}
1695

1696
int32_t metaFlagCache(SVnode *pVnode) {
×
1697
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
1698
  if (!pCur) {
×
1699
    return terrno;
×
1700
  }
1701

1702
  SArray *suids = NULL;
×
1703
  while (1) {
×
1704
    tb_uid_t id = metaStbCursorNext(pCur);
×
1705
    if (id == 0) {
×
1706
      break;
×
1707
    }
1708

1709
    if (!suids) {
×
1710
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
1711
      if (!suids) {
×
1712
        return terrno;
×
1713
      }
1714
    }
1715

1716
    if (taosArrayPush(suids, &id) == NULL) {
×
1717
      taosArrayDestroy(suids);
×
1718
      return terrno;
×
1719
    }
1720
  }
1721

1722
  metaCloseStbCursor(pCur);
×
1723

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

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

1734
      taosArrayDestroy(uids);
×
1735
      taosArrayDestroy(suids);
×
1736

1737
      return code;
×
1738
    }
1739

1740
    if (uids && TARRAY_SIZE(uids) > 0) {
×
1741
      STSchema *pTSchema = NULL;
×
1742

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

1747
        taosArrayDestroy(uids);
×
1748
        taosArrayDestroy(suids);
×
1749

1750
        return code;
×
1751
      }
1752

1753
      int32_t nCol = pTSchema->numOfCols;
×
1754
      for (int32_t i = 0; i < nCol; ++i) {
×
1755
        int16_t cid = pTSchema->columns[i].colId;
×
1756
        int8_t  col_type = pTSchema->columns[i].type;
×
1757

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

1762
          tDestroyTSchema(pTSchema);
×
1763
          taosArrayDestroy(uids);
×
1764
          taosArrayDestroy(suids);
×
1765

1766
          return code;
×
1767
        }
1768
      }
1769

1770
      tDestroyTSchema(pTSchema);
×
1771
    }
1772

1773
    taosArrayDestroy(uids);
×
1774
  }
1775

1776
  taosArrayDestroy(suids);
×
1777

1778
  return TSDB_CODE_SUCCESS;
×
1779
}
1780

1781
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1782

1783
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
2,096,136,031✔
1784
  int32_t code = 0;
2,096,136,031✔
1785
  void   *pData = NULL;
2,096,136,031✔
1786
  int     nData = 0;
2,096,185,359✔
1787
  int     lock = 0;
2,096,340,862✔
1788

1789
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
2,096,340,862✔
1790
    lock = 1;
765,968,121✔
1791
  }
1792

1793
  if (!lock) metaRLock(pMeta);
2,096,340,838✔
1794

1795
  // search cache
1796
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
2,096,356,048✔
1797
    if (!lock) metaULock(pMeta);
1,996,688,935✔
1798
    goto _exit;
1,996,695,108✔
1799
  }
1800

1801
  // search TDB
1802
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
99,766,097✔
1803
    // not found
1804
    if (!lock) metaULock(pMeta);
89,656,730✔
1805
    goto _exit;
89,643,644✔
1806
  }
1807

1808
  if (!lock) metaULock(pMeta);
10,135,816✔
1809

1810
  pInfo->uid = uid;
10,135,816✔
1811
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
10,135,816✔
1812
  pInfo->version = ((SUidIdxVal *)pData)->version;
10,135,816✔
1813
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
10,135,816✔
1814

1815
  if (lock) {
10,135,770✔
1816
    metaULock(pReader->pMeta);
1,215,602✔
1817
    // metaReaderReleaseLock(pReader);
1818
  }
1819
  // upsert the cache
1820
  metaWLock(pMeta);
10,135,770✔
1821
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
10,135,816✔
1822
  if (ret != 0) {
10,135,770✔
1823
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1824
  }
1825
  metaULock(pMeta);
10,135,770✔
1826

1827
  if (lock) {
10,135,770✔
1828
    metaRLock(pReader->pMeta);
1,215,602✔
1829
  }
1830

1831
_exit:
2,096,438,404✔
1832
  tdbFree(pData);
2,096,429,714✔
1833
  return code;
2,096,336,845✔
1834
}
1835

1836
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
123,736,836✔
1837
  int32_t code = 0;
123,736,836✔
1838

1839
  if (!numOfTables && !numOfCols) goto _exit;
123,736,836✔
1840

1841
  SVnode *pVnodeObj = pVnode;
123,736,836✔
1842
  metaRLock(pVnodeObj->pMeta);
123,736,836✔
1843

1844
  // fast path: search cache
1845
  SMetaStbStats state = {0};
123,745,069✔
1846
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
123,748,576✔
1847
    metaULock(pVnodeObj->pMeta);
119,213,849✔
1848
    if (numOfTables) *numOfTables = state.ctbNum;
119,213,140✔
1849
    if (numOfCols) *numOfCols = state.colNum;
119,213,140✔
1850
    if (flags) *flags = state.flags;
119,207,455✔
1851
    goto _exit;
119,206,796✔
1852
  }
1853

1854
  // slow path: search TDB
1855
  int64_t ctbNum = 0;
4,533,853✔
1856
  int32_t colNum = 0;
4,535,789✔
1857
  int64_t keep = 0;
4,536,128✔
1858
  int8_t  flag = 0;
4,535,562✔
1859

1860
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
4,535,392✔
1861
  if (TSDB_CODE_SUCCESS == code) {
4,534,757✔
1862
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
4,534,757✔
1863
  }
1864
  if (TSDB_CODE_SUCCESS == code) {
4,534,764✔
1865
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
4,535,450✔
1866
  }
1867
  metaULock(pVnodeObj->pMeta);
4,535,103✔
1868
  if (TSDB_CODE_SUCCESS != code) {
4,536,157✔
1869
    goto _exit;
×
1870
  }
1871

1872
  if (numOfTables) *numOfTables = ctbNum;
4,536,157✔
1873
  if (numOfCols) *numOfCols = colNum;
4,536,157✔
1874
  if (flags) *flags = flag;
4,535,789✔
1875

1876
  state.uid = uid;
4,535,789✔
1877
  state.ctbNum = ctbNum;
4,535,789✔
1878
  state.colNum = colNum;
4,535,789✔
1879
  state.flags = flag;
4,535,789✔
1880
  state.keep = keep;
4,535,789✔
1881
  // upsert the cache
1882
  metaWLock(pVnodeObj->pMeta);
4,535,789✔
1883

1884
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
4,535,789✔
1885
  if (ret) {
4,535,789✔
1886
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
1887
              uid, ctbNum, colNum, keep, flag);
1888
  }
1889

1890
  metaULock(pVnodeObj->pMeta);
4,535,789✔
1891

1892
_exit:
123,742,585✔
1893
  return code;
123,754,010✔
1894
}
1895

1896
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
79,382,396✔
1897
  SMetaStbStats stats = {0};
79,382,396✔
1898

1899
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
79,393,776✔
1900
    stats.ctbNum += deltaCtb;
68,204,364✔
1901
    stats.colNum += deltaCol;
68,204,364✔
1902
    if (deltaKeep > 0) {
68,204,364✔
1903
      stats.keep = deltaKeep;
7,809✔
1904
    }
1905

1906
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
68,204,364✔
1907
    if (code) {
68,185,718✔
1908
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
1909
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
1910
    }
1911
  }
1912
}
79,383,383✔
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