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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

web-flow
Merge pull request #28973 from taosdata/merge/mainto3.0

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

57.02
/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 "osMemory.h"
18
#include "tencode.h"
19

20
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
9,494,435✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
9,494,435✔
22
  metaReaderDoInit(pReader, pMeta, flags);
9,494,435✔
23
  pReader->pAPI = pAPI;
9,498,640✔
24
}
9,498,640✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
13,304,314✔
27
  memset(pReader, 0, sizeof(*pReader));
13,304,314✔
28
  pReader->pMeta = pMeta;
13,304,314✔
29
  pReader->flags = flags;
13,304,314✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
13,304,314!
31
    metaRLock(pMeta);
10,527,730✔
32
  }
33
}
13,305,486✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
2,204,098✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
2,204,098!
37
    metaULock(pReader->pMeta);
2,204,330✔
38
    pReader->flags |= META_READER_NOLOCK;
2,204,940✔
39
  }
40
}
2,204,708✔
41

42
void metaReaderClear(SMetaReader *pReader) {
13,992,993✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
13,992,993✔
44
    metaULock(pReader->pMeta);
8,322,789✔
45
  }
46
  tDecoderClear(&pReader->coder);
13,992,251✔
47
  tdbFree(pReader->pBuf);
14,002,839✔
48
  pReader->pBuf = NULL;
14,002,929✔
49
}
14,002,929✔
50

51
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
20,207,984✔
52
  int32_t  code = 0;
20,207,984✔
53
  SMeta   *pMeta = pReader->pMeta;
20,207,984✔
54
  STbDbKey tbDbKey = {.version = version, .uid = uid};
20,207,984✔
55

56
  // query table.db
57
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
20,207,984✔
58
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
116!
59
  }
60

61
  // decode the entry
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
20,216,593✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
20,192,922✔
65
  if (code) {
20,179,843!
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
20,179,843✔
72
}
73

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

78
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
771,180✔
79
    metaULock(pVnodeObj->pMeta);
1,727✔
80
    return false;
1,727✔
81
  }
82

83
  metaULock(pVnodeObj->pMeta);
769,452✔
84
  return true;
769,457✔
85
}
86

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

91
  // query uid.idx
92
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
4,695,808✔
93
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
133✔
94
  }
95

96
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
4,701,253✔
97
  return metaGetTableEntryByVersion(pReader, version1, uid);
4,701,253✔
98
}
99

100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
7,817,972✔
101
  SMeta *pMeta = pReader->pMeta;
7,817,972✔
102

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
7,817,972✔
105
  if (TSDB_CODE_SUCCESS != code) {
7,820,174✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
35!
107
  }
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
7,820,139✔
110
}
111

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

116
  // query name.idx
117
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
2,008,285✔
118
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
211,403✔
119
  }
120

121
  uid = *(tb_uid_t *)pReader->pBuf;
1,798,099✔
122
  return metaReaderGetTableEntryByUid(pReader, uid);
1,798,099✔
123
}
124

125
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
384,299✔
126
  void    *pData = NULL;
384,299✔
127
  int      nData = 0;
384,299✔
128
  tb_uid_t uid = 0;
384,299✔
129

130
  metaRLock(pMeta);
384,299✔
131

132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
384,390✔
133
    uid = *(tb_uid_t *)pData;
235,050✔
134
    tdbFree(pData);
235,050✔
135
  }
136

137
  metaULock(pMeta);
384,280✔
138

139
  return uid;
384,313✔
140
}
141

142
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
18,633✔
143
  int         code = 0;
18,633✔
144
  SMetaReader mr = {0};
18,633✔
145
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
18,633✔
146
  code = metaReaderGetTableEntryByUid(&mr, uid);
18,662✔
147
  if (code < 0) {
18,574✔
148
    metaReaderClear(&mr);
133✔
149
    return code;
133✔
150
  }
151

152
  STR_TO_VARSTR(tbName, mr.me.name);
18,441✔
153
  metaReaderClear(&mr);
18,441✔
154

155
  return 0;
18,527✔
156
}
157

158
int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
×
159
  int         code = 0;
×
160
  SMetaReader mr = {0};
×
161
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
162
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
163
  if (code < 0) {
×
164
    metaReaderClear(&mr);
×
165
    return code;
×
166
  }
167
  strncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
168
  metaReaderClear(&mr);
×
169

170
  return 0;
×
171
}
172

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

178
  SMetaReader *pReader = &mr;
448,329✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
445,717✔
187

188
  metaReaderClear(&mr);
445,717✔
189

190
  return 0;
445,714✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
445,715✔
199
  if (code == 0) *tbType = mr.me.type;
445,707!
200

201
  metaReaderClear(&mr);
445,707✔
202
  return code;
445,713✔
203
}
204

205
int metaReadNext(SMetaReader *pReader) {
×
206
  SMeta *pMeta = pReader->pMeta;
×
207

208
  // TODO
209

210
  return 0;
×
211
}
212

213
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
214
  int         code = -1;
×
215
  SMetaReader mr = {0};
×
216
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
217
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
218
  if (code < 0) {
×
219
    goto _exit;
×
220
  }
221
  if (mr.me.type == TSDB_CHILD_TABLE) {
×
222
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
223
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
×
224
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
225
  } else {
226
    goto _exit;
×
227
  }
228

229
  code = 0;
×
230

231
_exit:
×
232
  metaReaderClear(&mr);
×
233
  return code;
×
234
}
235

236
#if 1  // ===================================================
237
SMTbCursor *metaOpenTbCursor(void *pVnode) {
632,944✔
238
  SMTbCursor *pTbCur = NULL;
632,944✔
239
  int32_t     code;
240

241
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
632,944✔
242
  if (pTbCur == NULL) {
633,356!
243
    return NULL;
×
244
  }
245

246
  SVnode *pVnodeObj = pVnode;
633,356✔
247
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
248
  pTbCur->pMeta = pVnodeObj->pMeta;
633,356✔
249
  pTbCur->paused = 1;
633,356✔
250
  code = metaResumeTbCursor(pTbCur, 1, 0);
633,356✔
251
  if (code) {
633,030✔
252
    terrno = code;
239✔
253
    taosMemoryFree(pTbCur);
×
254
    return NULL;
×
255
  }
256
  return pTbCur;
632,791✔
257
}
258

259
void metaCloseTbCursor(SMTbCursor *pTbCur) {
1,267,780✔
260
  if (pTbCur) {
1,267,780✔
261
    tdbFree(pTbCur->pKey);
634,593✔
262
    tdbFree(pTbCur->pVal);
634,613✔
263
    if (!pTbCur->paused) {
634,643✔
264
      metaReaderClear(&pTbCur->mr);
50,760✔
265
      if (pTbCur->pDbc) {
50,760!
266
        tdbTbcClose((TBC *)pTbCur->pDbc);
50,760✔
267
      }
268
    }
269
    taosMemoryFree(pTbCur);
634,643✔
270
  }
271
}
1,267,851✔
272

273
void metaPauseTbCursor(SMTbCursor *pTbCur) {
584,532✔
274
  if (!pTbCur->paused) {
584,532!
275
    metaReaderClear(&pTbCur->mr);
584,606✔
276
    tdbTbcClose((TBC *)pTbCur->pDbc);
584,808✔
277
    pTbCur->paused = 1;
584,658✔
278
  }
279
}
584,584✔
280
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
634,696✔
281
  int32_t code = 0;
634,696✔
282
  int32_t lino;
283
  int8_t  locked = 0;
634,696✔
284
  if (pTbCur->paused) {
634,696!
285
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
634,777✔
286
    locked = 1;
635,180✔
287
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
635,180✔
288
    if (code != 0) {
634,474!
289
      TSDB_CHECK_CODE(code, lino, _exit);
×
290
    }
291

292
    if (first) {
634,474✔
293
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
633,552✔
294
      TSDB_CHECK_CODE(code, lino, _exit);
633,524!
295
    } else {
296
      int c = 1;
922✔
297
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
922✔
298
      TSDB_CHECK_CODE(code, lino, _exit);
922!
299
      if (c == 0) {
922!
300
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
922!
301
      } else if (c < 0) {
×
302
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
303
        TSDB_CHECK_CODE(code, lino, _exit);
×
304
      } else {
305
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
306
        TSDB_CHECK_CODE(code, lino, _exit);
×
307
      }
308
    }
309

310
    pTbCur->paused = 0;
634,446✔
311
  }
312

313
_exit:
×
314
  if (code != 0 && locked) {
634,365!
315
    metaReaderReleaseLock(&pTbCur->mr);
×
316
  }
317
  return code;
633,790✔
318
}
319

320
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
8,322,499✔
321
  int    ret;
322
  void  *pBuf;
323
  STbCfg tbCfg;
324

325
  for (;;) {
326
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
8,322,499✔
327
    if (ret < 0) {
8,316,330✔
328
      return ret;
634,473✔
329
    }
330

331
    tDecoderClear(&pTbCur->mr.coder);
7,681,857✔
332

333
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
7,714,608✔
334
    if (ret) return ret;
7,696,063!
335

336
    if (pTbCur->mr.me.type == jumpTableType) {
7,696,063✔
337
      continue;
1,236,583✔
338
    }
339

340
    break;
6,459,480✔
341
  }
342

343
  return 0;
6,459,480✔
344
}
345

346
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
347
  int    ret;
348
  void  *pBuf;
349
  STbCfg tbCfg;
350

351
  for (;;) {
352
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
353
    if (ret < 0) {
×
354
      return -1;
×
355
    }
356

357
    tDecoderClear(&pTbCur->mr.coder);
×
358

359
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
360
    if (ret < 0) {
×
361
      return ret;
×
362
    }
363

364
    if (pTbCur->mr.me.type == jumpTableType) {
×
365
      continue;
×
366
    }
367

368
    break;
×
369
  }
370

371
  return 0;
×
372
}
373

374
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, int64_t *createTime) {
5,008,631✔
375
  void           *pData = NULL;
5,008,631✔
376
  int             nData = 0;
5,008,631✔
377
  int64_t         version;
378
  SSchemaWrapper  schema = {0};
5,008,631✔
379
  SSchemaWrapper *pSchema = NULL;
5,008,631✔
380
  SDecoder        dc = {0};
5,008,631✔
381
  if (lock) {
5,008,631✔
382
    metaRLock(pMeta);
4,988,442✔
383
  }
384
_query:
5,020,354✔
385
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
5,100,906✔
386
    goto _err;
47✔
387
  }
388

389
  version = ((SUidIdxVal *)pData)[0].version;
5,106,540✔
390

391
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
5,106,540!
392
    goto _err;
×
393
  }
394

395
  SMetaEntry me = {0};
5,108,087✔
396
  tDecoderInit(&dc, pData, nData);
5,108,087✔
397
  int32_t code = metaDecodeEntry(&dc, &me);
5,102,420✔
398
  if (code) {
5,104,712!
399
    tDecoderClear(&dc);
×
400
    goto _err;
×
401
  }
402
  if (me.type == TSDB_SUPER_TABLE) {
5,104,712✔
403
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
4,679,593!
404
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
4,682,300✔
405
      tDecoderClear(&dc);
4,682,300✔
406
      goto _exit;
4,681,445✔
407
    }
408
  } else if (me.type == TSDB_CHILD_TABLE) {
425,119✔
409
    uid = me.ctbEntry.suid;
80,500✔
410
    if (createTime != NULL){
80,500✔
411
      *createTime = me.ctbEntry.btime;
78,307✔
412
    }
413
    tDecoderClear(&dc);
80,500✔
414
    goto _query;
80,552✔
415
  } else {
416
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
344,619✔
417
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
344,773✔
418
      tDecoderClear(&dc);
344,773✔
419
      goto _exit;
344,739✔
420
    }
421
  }
422
  tDecoderClear(&dc);
×
423

424
  // query from skm db
425
  if (tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData) < 0) {
130!
426
    goto _err;
×
427
  }
428

429
  tDecoderInit(&dc, pData, nData);
130✔
430
  if (tDecodeSSchemaWrapperEx(&dc, &schema) != 0) {
130!
431
    goto _err;
×
432
  }
433
  pSchema = tCloneSSchemaWrapper(&schema);
130✔
434
  tDecoderClear(&dc);
130✔
435

436
_exit:
5,026,314✔
437
  if (lock) {
5,026,314✔
438
    metaULock(pMeta);
4,994,215✔
439
  }
440
  tdbFree(pData);
5,029,548✔
441
  return pSchema;
5,027,728✔
442

443
_err:
47✔
444
  if (lock) {
47!
445
    metaULock(pMeta);
47✔
446
  }
447
  tdbFree(pData);
47✔
448
  return NULL;
47✔
449
}
450

451
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
3,938,181✔
452
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
3,938,181✔
453
  SMCtbCursor *pCtbCur = NULL;
3,938,181✔
454
  SCtbIdxKey   ctbIdxKey;
455
  int          ret = 0;
3,938,181✔
456
  int          c = 0;
3,938,181✔
457

458
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
3,938,181✔
459
  if (pCtbCur == NULL) {
3,948,452!
460
    return NULL;
×
461
  }
462

463
  pCtbCur->pMeta = pMeta;
3,948,452✔
464
  pCtbCur->suid = uid;
3,948,452✔
465
  pCtbCur->lock = lock;
3,948,452✔
466
  pCtbCur->paused = 1;
3,948,452✔
467

468
  ret = metaResumeCtbCursor(pCtbCur, 1);
3,948,452✔
469
  if (ret < 0) {
3,942,956!
470
    return NULL;
×
471
  }
472
  return pCtbCur;
3,942,956✔
473
}
474

475
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
3,947,202✔
476
  if (pCtbCur) {
3,947,202!
477
    if (!pCtbCur->paused) {
3,947,456✔
478
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
3,904,302!
479
      if (pCtbCur->pCur) {
3,906,017✔
480
        tdbTbcClose(pCtbCur->pCur);
3,905,864✔
481
      }
482
    }
483
    tdbFree(pCtbCur->pKey);
3,948,466✔
484
    tdbFree(pCtbCur->pVal);
3,947,851✔
485
  }
486
  taosMemoryFree(pCtbCur);
3,948,130✔
487
}
3,949,515✔
488

489
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,264✔
490
  if (!pCtbCur->paused) {
43,264!
491
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,279✔
492
    if (pCtbCur->lock) {
43,318!
493
      metaULock(pCtbCur->pMeta);
43,320✔
494
    }
495
    pCtbCur->paused = 1;
43,301✔
496
  }
497
}
43,286✔
498

499
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
3,941,211✔
500
  if (pCtbCur->paused) {
3,941,211!
501
    pCtbCur->paused = 0;
3,943,873✔
502

503
    if (pCtbCur->lock) {
3,943,873✔
504
      metaRLock(pCtbCur->pMeta);
3,924,657✔
505
    }
506
    int ret = 0;
3,943,295✔
507
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
3,943,295✔
508
    if (ret < 0) {
3,946,720✔
509
      metaCloseCtbCursor(pCtbCur);
144✔
510
      return -1;
×
511
    }
512

513
    if (first) {
3,946,576!
514
      SCtbIdxKey ctbIdxKey;
515
      // move to the suid
516
      ctbIdxKey.suid = pCtbCur->suid;
3,946,576✔
517
      ctbIdxKey.uid = INT64_MIN;
3,946,576✔
518
      int c = 0;
3,946,576✔
519
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
3,946,576✔
520
      if (c > 0) {
3,943,739✔
521
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
429,033✔
522
      }
523
    } else {
524
      int c = 0;
×
525
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
526
      if (c < 0) {
×
527
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
528
      } else {
529
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
530
      }
531
    }
532
  }
533
  return 0;
3,940,764✔
534
}
535

536
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
16,532,251✔
537
  int         ret;
538
  SCtbIdxKey *pCtbIdxKey;
539

540
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
16,532,251✔
541
  if (ret < 0) {
16,537,466✔
542
    return 0;
504,686✔
543
  }
544

545
  pCtbIdxKey = pCtbCur->pKey;
16,032,780✔
546
  if (pCtbIdxKey->suid > pCtbCur->suid) {
16,032,780✔
547
    return 0;
3,444,123✔
548
  }
549

550
  return pCtbIdxKey->uid;
12,588,657✔
551
}
552

553
struct SMStbCursor {
554
  SMeta   *pMeta;
555
  TBC     *pCur;
556
  tb_uid_t suid;
557
  void    *pKey;
558
  void    *pVal;
559
  int      kLen;
560
  int      vLen;
561
};
562

563
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
168,484✔
564
  SMStbCursor *pStbCur = NULL;
168,484✔
565
  int          ret = 0;
168,484✔
566
  int          c = 0;
168,484✔
567

568
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
168,484✔
569
  if (pStbCur == NULL) {
168,487!
570
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
571
    return NULL;
×
572
  }
573

574
  pStbCur->pMeta = pMeta;
168,487✔
575
  pStbCur->suid = suid;
168,487✔
576
  metaRLock(pMeta);
168,487✔
577

578
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
168,487✔
579
  if (ret < 0) {
168,486!
580
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
581
    metaULock(pMeta);
×
582
    taosMemoryFree(pStbCur);
×
583
    return NULL;
×
584
  }
585

586
  // move to the suid
587
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
168,486✔
588
  if (c > 0) {
168,484!
589
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
590
  }
591

592
  return pStbCur;
168,485✔
593
}
594

595
void metaCloseStbCursor(SMStbCursor *pStbCur) {
168,485✔
596
  if (pStbCur) {
168,485!
597
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
168,486✔
598
    if (pStbCur->pCur) {
168,488✔
599
      tdbTbcClose(pStbCur->pCur);
168,486✔
600

601
      tdbFree(pStbCur->pKey);
168,487✔
602
      tdbFree(pStbCur->pVal);
168,487✔
603
    }
604

605
    taosMemoryFree(pStbCur);
168,489✔
606
  }
607
}
168,486✔
608

609
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
297,868✔
610
  int ret;
611

612
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
297,868✔
613
  if (ret < 0) {
297,872✔
614
    return 0;
168,486✔
615
  }
616
  return *(tb_uid_t *)pStbCur->pKey;
129,386✔
617
}
618

619
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
4,863,025✔
620
  STSchema       *pTSchema = NULL;
4,863,025✔
621
  SSchemaWrapper *pSW = NULL;
4,863,025✔
622

623
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
4,863,025✔
624
  if (!pSW) return NULL;
4,878,585!
625

626
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
4,878,585✔
627

628
  taosMemoryFree(pSW->pSchema);
4,880,506✔
629
  taosMemoryFree(pSW);
4,881,106✔
630
  return pTSchema;
4,880,268✔
631
}
632

633
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
13,239✔
634
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
13,239✔
635
  if (*ppTSchema == NULL) {
13,255!
636
    return terrno;
×
637
  }
638
  return TSDB_CODE_SUCCESS;
13,255✔
639
}
640

641
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
4,850,773✔
642
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
4,850,773✔
643
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
4,866,487!
644
    return terrno;
7,998✔
645
  }
646
  return TSDB_CODE_SUCCESS;
4,858,489✔
647
}
648

649
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
628,031✔
650
  int32_t code = 0;
628,031✔
651
  int32_t lino;
652

653
  void     *pData = NULL;
628,031✔
654
  int       nData = 0;
628,031✔
655
  SSkmDbKey skmDbKey;
656
  if (sver <= 0) {
628,031✔
657
    SMetaInfo info;
658
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
380,588!
659
      sver = info.skmVer;
380,602✔
660
    } else {
661
      TBC *pSkmDbC = NULL;
×
662
      int  c;
663

664
      skmDbKey.uid = suid ? suid : uid;
×
665
      skmDbKey.sver = INT32_MAX;
×
666

667
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
×
668
      TSDB_CHECK_CODE(code, lino, _exit);
×
669
      metaRLock(pMeta);
×
670

671
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
×
672
        metaULock(pMeta);
×
673
        tdbTbcClose(pSkmDbC);
×
674
        code = TSDB_CODE_NOT_FOUND;
×
675
        goto _exit;
×
676
      }
677

678
      if (c == 0) {
×
679
        metaULock(pMeta);
×
680
        tdbTbcClose(pSkmDbC);
×
681
        code = TSDB_CODE_FAILED;
×
682
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
683
        goto _exit;
×
684
      }
685

686
      if (c < 0) {
×
687
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
688
      }
689

690
      const void *pKey = NULL;
×
691
      int32_t     nKey = 0;
×
692
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
×
693

694
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
×
695
        metaULock(pMeta);
×
696
        tdbTbcClose(pSkmDbC);
×
697
        code = TSDB_CODE_NOT_FOUND;
×
698
        goto _exit;
×
699
      }
700

701
      sver = ((SSkmDbKey *)pKey)->sver;
×
702

703
      metaULock(pMeta);
×
704
      tdbTbcClose(pSkmDbC);
×
705
    }
706
  }
707

708
  if (!(sver > 0)) {
628,027!
709
    code = TSDB_CODE_NOT_FOUND;
×
710
    goto _exit;
×
711
  }
712

713
  skmDbKey.uid = suid ? suid : uid;
628,027✔
714
  skmDbKey.sver = sver;
628,027✔
715
  metaRLock(pMeta);
628,027✔
716
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
628,198!
717
    metaULock(pMeta);
×
718
    code = TSDB_CODE_NOT_FOUND;
×
719
    goto _exit;
×
720
  }
721
  metaULock(pMeta);
628,049✔
722

723
  // decode
724
  SDecoder        dc = {0};
628,139✔
725
  SSchemaWrapper  schema;
726
  SSchemaWrapper *pSchemaWrapper = &schema;
628,139✔
727

728
  tDecoderInit(&dc, pData, nData);
628,139✔
729
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
626,711✔
730
  tDecoderClear(&dc);
626,711✔
731
  tdbFree(pData);
627,923✔
732
  if (TSDB_CODE_SUCCESS != code) {
627,987!
733
    taosMemoryFree(pSchemaWrapper->pSchema);
×
734
    goto _exit;
×
735
  }
736

737
  // convert
738
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
627,987✔
739
  if (pTSchema == NULL) {
628,084!
740
    code = TSDB_CODE_OUT_OF_MEMORY;
×
741
  }
742

743
  *ppTSchema = pTSchema;
628,084✔
744
  taosMemoryFree(pSchemaWrapper->pSchema);
628,084✔
745

746
_exit:
628,195✔
747
  return code;
628,195✔
748
}
749

750
// N.B. Called by statusReq per second
751
int64_t metaGetTbNum(SMeta *pMeta) {
980,282✔
752
  // num of child tables (excluding normal tables , stables and others)
753

754
  /* int64_t num = 0; */
755
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
756

757
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
980,282✔
758
}
759

760
void metaUpdTimeSeriesNum(SMeta *pMeta) {
168,445✔
761
  int64_t nCtbTimeSeries = 0;
168,445✔
762
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
168,445!
763
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
168,447✔
764
  }
765
}
168,447✔
766

767
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
768
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
769
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,163,060✔
770
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
1,163,073✔
771
    metaUpdTimeSeriesNum(pMeta);
164,979✔
772
  }
773

774
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
1,163,086✔
775
}
776

777
// type: 1 reported timeseries
778
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
1,163,060!
779
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
1,163,086✔
780
  if (type == 1) {
1,163,086✔
781
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
980,282✔
782
  }
783
  return nTimeSeries;
1,163,086✔
784
}
785

786
typedef struct {
787
  SMeta   *pMeta;
788
  TBC     *pCur;
789
  tb_uid_t uid;
790
  void    *pKey;
791
  void    *pVal;
792
  int      kLen;
793
  int      vLen;
794
} SMSmaCursor;
795

796
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
797
  SMSmaCursor *pSmaCur = NULL;
×
798
  SSmaIdxKey   smaIdxKey;
799
  int          ret;
800
  int          c;
801

802
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
803
  if (pSmaCur == NULL) {
×
804
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
805
    return NULL;
×
806
  }
807

808
  pSmaCur->pMeta = pMeta;
×
809
  pSmaCur->uid = uid;
×
810
  metaRLock(pMeta);
×
811

812
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
813
  if (ret < 0) {
×
814
    metaULock(pMeta);
×
815
    taosMemoryFree(pSmaCur);
×
816
    return NULL;
×
817
  }
818

819
  // move to the suid
820
  smaIdxKey.uid = uid;
×
821
  smaIdxKey.smaUid = INT64_MIN;
×
822
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
823
  if (c > 0) {
×
824
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
825
  }
826

827
  return pSmaCur;
×
828
}
829

830
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
831
  if (pSmaCur) {
×
832
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
833
    if (pSmaCur->pCur) {
×
834
      tdbTbcClose(pSmaCur->pCur);
×
835
      pSmaCur->pCur = NULL;
×
836

837
      tdbFree(pSmaCur->pKey);
×
838
      tdbFree(pSmaCur->pVal);
×
839
    }
840

841
    taosMemoryFree(pSmaCur);
×
842
  }
843
}
×
844

845
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
846
  int         ret;
847
  SSmaIdxKey *pSmaIdxKey;
848

849
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
850
  if (ret < 0) {
×
851
    return 0;
×
852
  }
853

854
  pSmaIdxKey = pSmaCur->pKey;
×
855
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
856
    return 0;
×
857
  }
858

859
  return pSmaIdxKey->uid;
×
860
}
861

862
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
863
  STSmaWrapper *pSW = NULL;
×
864
  SArray       *pSmaIds = NULL;
×
865

866
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
867
    return NULL;
×
868
  }
869

870
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
871
  if (!pSW) {
×
872
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
873
    goto _err;
×
874
  }
875

876
  pSW->number = taosArrayGetSize(pSmaIds);
×
877
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
878

879
  if (!pSW->tSma) {
×
880
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
881
    goto _err;
×
882
  }
883

884
  SMetaReader mr = {0};
×
885
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
886
  int64_t smaId;
887
  int     smaIdx = 0;
×
888
  STSma  *pTSma = NULL;
×
889
  for (int i = 0; i < pSW->number; ++i) {
×
890
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
891
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
892
      tDecoderClear(&mr.coder);
×
893
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
894
      continue;
×
895
    }
896
    tDecoderClear(&mr.coder);
×
897
    pTSma = pSW->tSma + smaIdx;
×
898
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
899
    if (deepCopy) {
×
900
      if (pTSma->exprLen > 0) {
×
901
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
902
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
903
          goto _err;
×
904
        }
905
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
906
      }
907
      if (pTSma->tagsFilterLen > 0) {
×
908
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
909
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
910
          goto _err;
×
911
        }
912
      }
913
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
914
    } else {
915
      pTSma->exprLen = 0;
×
916
      pTSma->expr = NULL;
×
917
      pTSma->tagsFilterLen = 0;
×
918
      pTSma->tagsFilter = NULL;
×
919
    }
920

921
    ++smaIdx;
×
922
  }
923

924
  if (smaIdx <= 0) goto _err;
×
925
  pSW->number = smaIdx;
×
926

927
  metaReaderClear(&mr);
×
928
  taosArrayDestroy(pSmaIds);
×
929
  return pSW;
×
930
_err:
×
931
  metaReaderClear(&mr);
×
932
  taosArrayDestroy(pSmaIds);
×
933
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
934
  return NULL;
×
935
}
936

937
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
4✔
938
  STSma      *pTSma = NULL;
4✔
939
  SMetaReader mr = {0};
4✔
940
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
4✔
941
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
4!
942
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
943
    metaReaderClear(&mr);
×
944
    return NULL;
×
945
  }
946
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
4✔
947
  if (!pTSma) {
4!
948
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
949
    metaReaderClear(&mr);
×
950
    return NULL;
×
951
  }
952

953
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
4✔
954

955
  metaReaderClear(&mr);
4✔
956
  return pTSma;
4✔
957
}
958

959
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
960
  SArray     *pUids = NULL;
×
961
  SSmaIdxKey *pSmaIdxKey = NULL;
×
962

963
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
964
  if (!pCur) {
×
965
    return NULL;
×
966
  }
967

968
  while (1) {
×
969
    tb_uid_t id = metaSmaCursorNext(pCur);
×
970
    if (id == 0) {
×
971
      break;
×
972
    }
973

974
    if (!pUids) {
×
975
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
976
      if (!pUids) {
×
977
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
978
        metaCloseSmaCursor(pCur);
×
979
        return NULL;
×
980
      }
981
    }
982

983
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
984

985
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
986
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
987
      metaCloseSmaCursor(pCur);
×
988
      taosArrayDestroy(pUids);
×
989
      return NULL;
×
990
    }
991
  }
992

993
  metaCloseSmaCursor(pCur);
×
994
  return pUids;
×
995
}
996

997
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
998
  SArray     *pUids = NULL;
×
999
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1000
  tb_uid_t    lastUid = 0;
×
1001

1002
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1003
  if (!pCur) {
×
1004
    return NULL;
×
1005
  }
1006

1007
  while (1) {
×
1008
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1009
    if (uid == 0) {
×
1010
      break;
×
1011
    }
1012

1013
    if (lastUid == uid) {
×
1014
      continue;
×
1015
    }
1016

1017
    lastUid = uid;
×
1018

1019
    if (!pUids) {
×
1020
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1021
      if (!pUids) {
×
1022
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1023
        metaCloseSmaCursor(pCur);
×
1024
        return NULL;
×
1025
      }
1026
    }
1027

1028
    if (!taosArrayPush(pUids, &uid)) {
×
1029
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1030
      metaCloseSmaCursor(pCur);
×
1031
      taosArrayDestroy(pUids);
×
1032
      return NULL;
×
1033
    }
1034
  }
1035

1036
  metaCloseSmaCursor(pCur);
×
1037
  return pUids;
×
1038
}
1039

1040
#endif
1041

1042
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
19,203,052✔
1043
  STag *tag = (STag *)pTag;
19,203,052✔
1044
  if (type == TSDB_DATA_TYPE_JSON) {
19,203,052✔
1045
    return tag;
10,214✔
1046
  }
1047
  bool find = tTagGet(tag, val);
19,192,838✔
1048

1049
  if (!find) {
19,235,073✔
1050
    return NULL;
120,756✔
1051
  }
1052

1053
  return val;
19,114,317✔
1054
}
1055

1056
typedef struct {
1057
  SMeta   *pMeta;
1058
  TBC     *pCur;
1059
  tb_uid_t suid;
1060
  int16_t  cid;
1061
  int16_t  type;
1062
  void    *pKey;
1063
  void    *pVal;
1064
  int32_t  kLen;
1065
  int32_t  vLen;
1066
} SIdxCursor;
1067

1068
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6✔
1069
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6✔
1070
  SMetaFltParam *param = arg;
6✔
1071
  int32_t        ret = 0;
6✔
1072

1073
  SIdxCursor *pCursor = NULL;
6✔
1074
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
6✔
1075
  if (pCursor == NULL) {
6!
1076
    return terrno;
×
1077
  }
1078
  pCursor->pMeta = pMeta;
6✔
1079
  pCursor->suid = param->suid;
6✔
1080
  pCursor->cid = param->cid;
6✔
1081
  pCursor->type = param->type;
6✔
1082

1083
  metaRLock(pMeta);
6✔
1084
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
6✔
1085
  if (ret != 0) {
6!
1086
    goto END;
×
1087
  }
1088
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
6✔
1089

1090
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
6✔
1091
  SBtimeIdxKey *pBtimeKey = &btimeKey;
6✔
1092

1093
  int cmp = 0;
6✔
1094
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
6!
1095
    goto END;
×
1096
  }
1097

1098
  int32_t valid = 0;
6✔
1099
  int32_t count = 0;
6✔
1100

1101
  static const int8_t TRY_ERROR_LIMIT = 1;
1102
  do {
30,790✔
1103
    void   *entryKey = NULL;
30,796✔
1104
    int32_t nEntryKey = -1;
30,796✔
1105
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
30,796✔
1106
    if (valid < 0) break;
30,942✔
1107

1108
    SBtimeIdxKey *p = entryKey;
30,936✔
1109
    if (count > TRY_ERROR_LIMIT) break;
30,936!
1110

1111
    terrno = TSDB_CODE_SUCCESS;
30,936✔
1112
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
30,887✔
1113
    if (terrno != TSDB_CODE_SUCCESS) {
30,877!
1114
      ret = terrno;
×
1115
      break;
×
1116
    }
1117
    if (cmp == 0) {
30,843!
1118
      if (taosArrayPush(pUids, &p->uid) == NULL) {
61,664!
1119
        ret = terrno;
×
1120
        break;
×
1121
      }
1122
    } else {
1123
      if (param->equal == true) {
×
1124
        if (count > TRY_ERROR_LIMIT) break;
×
1125
        count++;
×
1126
      }
1127
    }
1128
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
30,821✔
1129
    if (valid < 0) break;
30,790!
1130
  } while (1);
1131

1132
END:
6✔
1133
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6!
1134
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6!
1135
  taosMemoryFree(pCursor);
6✔
1136
  return ret;
6✔
1137
}
1138

1139
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1140
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1141
  SMetaFltParam *param = arg;
×
1142
  int32_t        ret = 0;
×
1143
  char          *buf = NULL;
×
1144

1145
  STagIdxKey *pKey = NULL;
×
1146
  int32_t     nKey = 0;
×
1147

1148
  SIdxCursor *pCursor = NULL;
×
1149
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1150
  if (pCursor == NULL) {
×
1151
    return terrno;
×
1152
  }
1153
  pCursor->pMeta = pMeta;
×
1154
  pCursor->suid = param->suid;
×
1155
  pCursor->cid = param->cid;
×
1156
  pCursor->type = param->type;
×
1157

1158
  char *pName = param->val;
×
1159

1160
  metaRLock(pMeta);
×
1161
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1162
  if (ret != 0) {
×
1163
    goto END;
×
1164
  }
1165

1166
  int cmp = 0;
×
1167
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1168
    goto END;
×
1169
  }
1170
  int32_t valid = 0;
×
1171
  int32_t count = 0;
×
1172

1173
  int32_t TRY_ERROR_LIMIT = 1;
×
1174
  do {
×
1175
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1176
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1177
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1178
    if (valid < 0) break;
×
1179

1180
    if (count > TRY_ERROR_LIMIT) break;
×
1181

1182
    char *pTableKey = (char *)pEntryKey;
×
1183
    terrno = TSDB_CODE_SUCCESS;
×
1184
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1185
    if (terrno != TSDB_CODE_SUCCESS) {
×
1186
      ret = terrno;
×
1187
      goto END;
×
1188
    }
1189
    if (cmp == 0) {
×
1190
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1191
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1192
        ret = terrno;
×
1193
        goto END;
×
1194
      }
1195
    } else {
1196
      if (param->equal == true) {
×
1197
        if (count > TRY_ERROR_LIMIT) break;
×
1198
        count++;
×
1199
      }
1200
    }
1201
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1202
    if (valid < 0) {
×
1203
      break;
×
1204
    }
1205
  } while (1);
1206

1207
END:
×
1208
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1209
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1210
  taosMemoryFree(buf);
×
1211
  taosMemoryFree(pKey);
×
1212

1213
  taosMemoryFree(pCursor);
×
1214

1215
  return ret;
×
1216
}
1217
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1218
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1219
  SMetaFltParam *param = arg;
×
1220
  int32_t        ret = 0;
×
1221
  char          *buf = NULL;
×
1222

1223
  STtlIdxKey *pKey = NULL;
×
1224
  int32_t     nKey = 0;
×
1225

1226
  SIdxCursor *pCursor = NULL;
×
1227
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1228
  if (pCursor == NULL) {
×
1229
    return terrno;
×
1230
  }
1231
  pCursor->pMeta = pMeta;
×
1232
  pCursor->suid = param->suid;
×
1233
  pCursor->cid = param->cid;
×
1234
  pCursor->type = param->type;
×
1235

1236
  metaRLock(pMeta);
×
1237
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1238

1239
END:
×
1240
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1241
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1242
  taosMemoryFree(buf);
×
1243
  taosMemoryFree(pKey);
×
1244

1245
  taosMemoryFree(pCursor);
×
1246

1247
  return ret;
×
1248
  // impl later
1249
  return 0;
1250
}
1251
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
1,010✔
1252
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
1,010✔
1253
  SMetaFltParam *param = arg;
1,010✔
1254

1255
  SMetaEntry oStbEntry = {0};
1,010✔
1256
  int32_t    code = 0;
1,010✔
1257
  char      *buf = NULL;
1,010✔
1258
  void      *pData = NULL;
1,010✔
1259
  int        nData = 0;
1,010✔
1260

1261
  SDecoder    dc = {0};
1,010✔
1262
  STbDbKey    tbDbKey = {0};
1,010✔
1263
  STagIdxKey *pKey = NULL;
1,010✔
1264
  int32_t     nKey = 0;
1,010✔
1265

1266
  SIdxCursor *pCursor = NULL;
1,010✔
1267
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
1,010✔
1268
  if (!pCursor) {
1,018!
1269
    return terrno;
×
1270
  }
1271
  pCursor->pMeta = pMeta;
1,018✔
1272
  pCursor->suid = param->suid;
1,018✔
1273
  pCursor->cid = param->cid;
1,018✔
1274
  pCursor->type = param->type;
1,018✔
1275

1276
  metaRLock(pMeta);
1,018✔
1277

1278
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
1,017!
1279

1280
  tbDbKey.uid = param->suid;
1,017✔
1281
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
1,017✔
1282

1283
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
1,017!
1284

1285
  tDecoderInit(&dc, pData, nData);
1,018✔
1286

1287
  code = metaDecodeEntry(&dc, &oStbEntry);
1,017✔
1288
  if (code) {
1,017!
1289
    tDecoderClear(&dc);
×
1290
    goto END;
×
1291
  }
1292

1293
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
1,017!
1294
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1295
  }
1296

1297
  code = TSDB_CODE_INVALID_PARA;
1,017✔
1298
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
2,283✔
1299
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
1,831✔
1300
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
1,831!
1301
      code = 0;
450✔
1302
    } else {
1303
      TAOS_CHECK_GOTO(code, NULL, END);
1,381✔
1304
    }
1305
  }
1306

1307
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
452✔
1308
  if (code != 0) {
449✔
1309
    TAOS_CHECK_GOTO(terrno, NULL, END);
1!
1310
  }
1311

1312
  int32_t maxSize = 0;
448✔
1313
  int32_t nTagData = 0;
448✔
1314
  void   *tagData = NULL;
448✔
1315

1316
  if (param->val == NULL) {
448!
1317
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1318
    goto END;
×
1319
  } else {
1320
    if (IS_VAR_DATA_TYPE(param->type)) {
448!
1321
      tagData = varDataVal(param->val);
1✔
1322
      nTagData = varDataLen(param->val);
1✔
1323

1324
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
1!
1325
        maxSize = 4 * nTagData + 1;
×
1326
        buf = taosMemoryCalloc(1, maxSize);
×
1327
        if (buf == NULL) {
×
1328
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1329
        }
1330

1331
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize)) {
×
1332
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1333
        }
1334

1335
        tagData = buf;
×
1336
        nTagData = maxSize;
×
1337
      }
1338
    } else {
1339
      tagData = param->val;
447✔
1340
      nTagData = tDataTypes[param->type].bytes;
447✔
1341
    }
1342
  }
1343

1344
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
448!
1345
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1346
                  NULL, END);
1347

1348
  int cmp = 0;
449✔
1349
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
449!
1350

1351
  int     count = 0;
449✔
1352
  int32_t valid = 0;
449✔
1353
  bool    found = false;
449✔
1354

1355
  static const int8_t TRY_ERROR_LIMIT = 1;
1356

1357
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1358
  /// target:                        [suid, cid2, type2]
1359
  int diffCidCount = 0;
449✔
1360
  do {
11,500✔
1361
    void   *entryKey = NULL, *entryVal = NULL;
11,949✔
1362
    int32_t nEntryKey, nEntryVal;
1363

1364
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
11,949✔
1365
    if (valid < 0) {
11,941✔
1366
      code = valid;
349✔
1367
    }
1368
    if (count > TRY_ERROR_LIMIT) {
11,941✔
1369
      break;
449✔
1370
    }
1371

1372
    STagIdxKey *p = entryKey;
11,916✔
1373
    if (p == NULL) break;
11,916✔
1374

1375
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
11,572!
1376
      if (found == true) break;  //
78!
UNCOV
1377
      if (diffCidCount > TRY_ERROR_LIMIT) break;
×
UNCOV
1378
      diffCidCount++;
×
UNCOV
1379
      count++;
×
UNCOV
1380
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1381
      if (valid < 0) {
2!
1382
        code = valid;
×
1383
        break;
×
1384
      } else {
1385
        continue;
2✔
1386
      }
1387
    }
1388

1389
    terrno = TSDB_CODE_SUCCESS;
11,494✔
1390
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
11,493✔
1391
    if (terrno != TSDB_CODE_SUCCESS) {
11,492!
1392
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1393
      break;
×
1394
    }
1395
    if (cmp == 0) {
11,499✔
1396
      // match
1397
      tb_uid_t tuid = 0;
10,682✔
1398
      if (IS_VAR_DATA_TYPE(pKey->type)) {
10,682!
1399
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
×
1400
      } else {
1401
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
10,683✔
1402
      }
1403
      if (taosArrayPush(pUids, &tuid) == NULL) {
10,690!
1404
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1405
      }
1406
      found = true;
10,690✔
1407
    } else {
1408
      if (param->equal == true) {
817✔
1409
        if (count > TRY_ERROR_LIMIT) break;
112!
1410
        count++;
112✔
1411
      }
1412
    }
1413
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
11,507✔
1414
    if (valid < 0) {
11,498!
1415
      code = valid;
×
1416
      break;
×
1417
    }
1418
  } while (1);
1419

1420
END:
1,014✔
1421
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
1,014!
1422
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
1,017✔
1423
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
1,017!
1424
  tDecoderClear(&dc);
1,017✔
1425
  tdbFree(pData);
1,015✔
1426

1427
  taosMemoryFree(buf);
1,018✔
1428
  taosMemoryFree(pKey);
1,017✔
1429

1430
  taosMemoryFree(pCursor);
1,017✔
1431

1432
  return code;
1,016✔
1433
}
1434

1435
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
267,334✔
1436
  int ret = 0;
267,334✔
1437
  if (lock) {
267,334!
1438
    metaRLock(pMeta);
×
1439
  }
1440

1441
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
267,334✔
1442
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
267,334✔
1443
  if (lock) {
267,336!
1444
    metaULock(pMeta);
×
1445
  }
1446

1447
  return ret;
267,336✔
1448
}
1449

1450
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
266,776✔
1451
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
266,776✔
1452
  const int32_t LIMIT = 128;
266,776✔
1453

1454
  int32_t isLock = false;
266,776✔
1455
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
266,776!
1456
  for (int i = 0; i < sz; i++) {
534,109✔
1457
    STUidTagInfo *p = taosArrayGet(uidList, i);
267,331✔
1458

1459
    if (i % LIMIT == 0) {
267,330✔
1460
      if (isLock) metaULock(pMeta);
266,776!
1461

1462
      metaRLock(pMeta);
266,776✔
1463
      isLock = true;
266,779✔
1464
    }
1465

1466
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
1467
    void   *val = NULL;
267,333✔
1468
    int32_t len = 0;
267,333✔
1469
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
267,333!
1470
      p->pTagVal = taosMemoryMalloc(len);
267,336✔
1471
      if (!p->pTagVal) {
267,337!
1472
        if (isLock) metaULock(pMeta);
×
1473

1474
        TAOS_RETURN(terrno);
×
1475
      }
1476
      memcpy(p->pTagVal, val, len);
267,337✔
1477
      tdbFree(val);
267,337✔
1478
    } else {
1479
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64 "", TD_VID(pMeta->pVnode), suid,
×
1480
                p->uid);
1481
    }
1482
  }
1483
  //  }
1484
  if (isLock) metaULock(pMeta);
266,778!
1485
  return 0;
266,780✔
1486
}
1487

1488
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
912,486✔
1489
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
912,486✔
1490
  if (!pCur) {
914,182!
1491
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1492
  }
1493

1494
  // If len > 0 means there already have uids, and we only want the
1495
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1496
  // in the hash map, that may require a lot of memory
1497
  SHashObj *pSepecifiedUidMap = NULL;
914,182✔
1498
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
914,182✔
1499
  if (numOfElems > 0) {
913,207✔
1500
    pSepecifiedUidMap =
1501
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
98,292✔
1502
    for (int i = 0; i < numOfElems; i++) {
394,137✔
1503
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
295,787✔
1504
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
295,705✔
1505
      if (code) {
295,880!
1506
        metaCloseCtbCursor(pCur);
×
1507
        taosHashCleanup(pSepecifiedUidMap);
×
1508
        return code;
×
1509
      }
1510
    }
1511
  }
1512

1513
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
913,265✔
1514
    while (1) {
2,344,294✔
1515
      tb_uid_t uid = metaCtbCursorNext(pCur);
3,159,064✔
1516
      if (uid == 0) {
3,156,051✔
1517
        break;
816,938✔
1518
      }
1519

1520
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
2,339,113✔
1521
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,339,113✔
1522
      if (!info.pTagVal) {
2,348,640!
1523
        metaCloseCtbCursor(pCur);
×
1524
        taosHashCleanup(pSepecifiedUidMap);
×
1525
        return terrno;
×
1526
      }
1527
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,348,640✔
1528
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,344,294!
1529
        taosMemoryFreeClear(info.pTagVal);
×
1530
        metaCloseCtbCursor(pCur);
×
1531
        taosHashCleanup(pSepecifiedUidMap);
×
1532
        return terrno;
×
1533
      }
1534
    }
1535
  } else {  // only the specified tables need to be added
1536
    while (1) {
406,126✔
1537
      tb_uid_t uid = metaCtbCursorNext(pCur);
504,621✔
1538
      if (uid == 0) {
504,146✔
1539
        break;
98,338✔
1540
      }
1541

1542
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
405,808✔
1543
      if (index == NULL) {
406,311✔
1544
        continue;
110,754✔
1545
      }
1546

1547
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
295,557✔
1548
      if (pTagInfo->pTagVal == NULL) {
295,532✔
1549
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
295,524✔
1550
        if (!pTagInfo->pTagVal) {
295,637✔
1551
          metaCloseCtbCursor(pCur);
273✔
1552
          taosHashCleanup(pSepecifiedUidMap);
×
1553
          return terrno;
×
1554
        }
1555
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
295,364✔
1556
      }
1557
    }
1558
  }
1559

1560
  taosHashCleanup(pSepecifiedUidMap);
915,276✔
1561
  metaCloseCtbCursor(pCur);
915,067✔
1562
  return TSDB_CODE_SUCCESS;
915,504✔
1563
}
1564

1565
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1566

1567
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
35,933,754✔
1568
  int32_t code = 0;
35,933,754✔
1569
  void   *pData = NULL;
35,933,754✔
1570
  int     nData = 0;
35,933,754✔
1571
  int     lock = 0;
35,933,754✔
1572

1573
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
35,933,754!
1574
    lock = 1;
7,820,987✔
1575
  }
1576

1577
  if (!lock) metaRLock(pMeta);
35,933,754✔
1578

1579
  // search cache
1580
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
35,941,445✔
1581
    if (!lock) metaULock(pMeta);
35,755,536✔
1582
    goto _exit;
35,753,827✔
1583
  }
1584

1585
  // search TDB
1586
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
189,025✔
1587
    // not found
1588
    if (!lock) metaULock(pMeta);
30,913✔
1589
    goto _exit;
30,912✔
1590
  }
1591

1592
  if (!lock) metaULock(pMeta);
158,322✔
1593

1594
  pInfo->uid = uid;
158,321✔
1595
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
158,321✔
1596
  pInfo->version = ((SUidIdxVal *)pData)->version;
158,321✔
1597
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
158,321✔
1598

1599
  if (lock) {
158,321✔
1600
    metaULock(pReader->pMeta);
39,956✔
1601
    // metaReaderReleaseLock(pReader);
1602
  }
1603
  // upsert the cache
1604
  metaWLock(pMeta);
158,321✔
1605
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
158,322✔
1606
  if (ret != 0) {
158,321!
1607
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1608
  }
1609
  metaULock(pMeta);
158,321✔
1610

1611
  if (lock) {
158,322✔
1612
    metaRLock(pReader->pMeta);
39,956✔
1613
  }
1614

1615
_exit:
118,366✔
1616
  tdbFree(pData);
35,943,061✔
1617
  return code;
35,941,753✔
1618
}
1619

1620
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
310,226✔
1621
  int32_t code = 0;
310,226✔
1622

1623
  if (!numOfTables && !numOfCols) goto _exit;
310,226!
1624

1625
  SVnode *pVnodeObj = pVnode;
310,226✔
1626
  metaRLock(pVnodeObj->pMeta);
310,226✔
1627

1628
  // fast path: search cache
1629
  SMetaStbStats state = {0};
310,332✔
1630
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
310,332✔
1631
    metaULock(pVnodeObj->pMeta);
286,543✔
1632
    if (numOfTables) *numOfTables = state.ctbNum;
286,549✔
1633
    if (numOfCols) *numOfCols = state.colNum;
286,549✔
1634
    goto _exit;
286,549✔
1635
  }
1636

1637
  // slow path: search TDB
1638
  int64_t ctbNum = 0;
23,771✔
1639
  int32_t colNum = 0;
23,771✔
1640
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
23,771✔
1641
  if (TSDB_CODE_SUCCESS == code) {
23,776!
1642
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
23,776✔
1643
  }
1644
  metaULock(pVnodeObj->pMeta);
23,774✔
1645
  if (TSDB_CODE_SUCCESS != code) {
23,771!
1646
    goto _exit;
×
1647
  }
1648

1649
  if (numOfTables) *numOfTables = ctbNum;
23,771✔
1650
  if (numOfCols) *numOfCols = colNum;
23,771✔
1651

1652
  state.uid = uid;
23,771✔
1653
  state.ctbNum = ctbNum;
23,771✔
1654
  state.colNum = colNum;
23,771✔
1655

1656
  // upsert the cache
1657
  metaWLock(pVnodeObj->pMeta);
23,771✔
1658

1659
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
23,775✔
1660
  if (ret) {
23,772!
1661
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d", uid, ctbNum, colNum);
×
1662
  }
1663

1664
  metaULock(pVnodeObj->pMeta);
23,772✔
1665

1666
_exit:
310,309✔
1667
  return code;
310,309✔
1668
}
1669

1670
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol) {
190,349✔
1671
  SMetaStbStats stats = {0};
190,349✔
1672

1673
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
190,349✔
1674
    stats.ctbNum += deltaCtb;
184,990✔
1675
    stats.colNum += deltaCol;
184,990✔
1676
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
184,990✔
1677
    if (code) {
184,982!
1678
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d",
×
1679
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol);
1680
    }
1681
  }
1682
}
190,349✔
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

© 2025 Coveralls, Inc