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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

6067577.83 hits per line

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

56.27
/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) {
2,863,538✔
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
2,863,538✔
22
  metaReaderDoInit(pReader, pMeta, flags);
2,863,538✔
23
  pReader->pAPI = pAPI;
2,864,641✔
24
}
2,864,641✔
25

26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
3,274,493✔
27
  memset(pReader, 0, sizeof(*pReader));
3,274,493✔
28
  pReader->pMeta = pMeta;
3,274,493✔
29
  pReader->flags = flags;
3,274,493✔
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
3,274,493✔
31
    metaRLock(pMeta);
2,861,806✔
32
  }
33
}
3,274,747✔
34

35
void metaReaderReleaseLock(SMetaReader *pReader) {
1,135,303✔
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
1,135,303!
37
    metaULock(pReader->pMeta);
1,135,754✔
38
    pReader->flags |= META_READER_NOLOCK;
1,136,273✔
39
  }
40
}
1,135,822✔
41

42
void metaReaderClear(SMetaReader *pReader) {
3,298,032✔
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
3,298,032✔
44
    metaULock(pReader->pMeta);
1,725,348✔
45
  }
46
  tDecoderClear(&pReader->coder);
3,298,023✔
47
  tdbFree(pReader->pBuf);
3,298,502✔
48
  pReader->pBuf = NULL;
3,299,376✔
49
}
3,299,376✔
50

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

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

61
  // decode the entry
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
4,048,224✔
63

64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
4,046,849✔
65
  if (code) {
4,044,388!
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

71
  return 0;
4,044,388✔
72
}
73

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

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

83
  metaULock(pVnodeObj->pMeta);
496,718✔
84
  return true;
496,731✔
85
}
86

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

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

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

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

103
  SMetaInfo info;
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
2,817,865✔
105
  if (TSDB_CODE_SUCCESS != code) {
2,818,343✔
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
32!
107
  }
108

109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
2,818,311✔
110
}
111

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

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

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

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

130
  metaRLock(pMeta);
293,191✔
131

132
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
293,226✔
133
    uid = *(tb_uid_t *)pData;
178,690✔
134
    tdbFree(pData);
178,690✔
135
  }
136

137
  metaULock(pMeta);
293,168✔
138

139
  return uid;
293,198✔
140
}
141

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

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

155
  return 0;
16,402✔
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) {
5,882✔
174
  int         code = 0;
5,882✔
175
  SMetaReader mr = {0};
5,882✔
176
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
5,882✔
177

178
  SMetaReader *pReader = &mr;
5,885✔
179

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

186
  *uid = *(tb_uid_t *)pReader->pBuf;
3,271✔
187

188
  metaReaderClear(&mr);
3,271✔
189

190
  return 0;
3,271✔
191
}
192

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

198
  code = metaGetTableEntryByName(&mr, tbName);
3,267✔
199
  if (code == 0) *tbType = mr.me.type;
3,266!
200

201
  metaReaderClear(&mr);
3,266✔
202
  return code;
3,267✔
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) {
20,525✔
238
  SMTbCursor *pTbCur = NULL;
20,525✔
239
  int32_t     code;
240

241
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
20,525✔
242
  if (pTbCur == NULL) {
20,528!
243
    return NULL;
×
244
  }
245

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

259
void metaCloseTbCursor(SMTbCursor *pTbCur) {
41,083✔
260
  if (pTbCur) {
41,083✔
261
    tdbFree(pTbCur->pKey);
20,537✔
262
    tdbFree(pTbCur->pVal);
20,538✔
263
    if (!pTbCur->paused) {
20,538✔
264
      metaReaderClear(&pTbCur->mr);
539✔
265
      if (pTbCur->pDbc) {
539!
266
        tdbTbcClose((TBC *)pTbCur->pDbc);
539✔
267
      }
268
    }
269
    taosMemoryFree(pTbCur);
20,538✔
270
  }
271
}
41,086✔
272

273
void metaPauseTbCursor(SMTbCursor *pTbCur) {
20,916✔
274
  if (!pTbCur->paused) {
20,916!
275
    metaReaderClear(&pTbCur->mr);
20,921✔
276
    tdbTbcClose((TBC *)pTbCur->pDbc);
20,918✔
277
    pTbCur->paused = 1;
20,923✔
278
  }
279
}
20,918✔
280
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
21,449✔
281
  int32_t code = 0;
21,449✔
282
  int32_t lino;
283
  int8_t  locked = 0;
21,449✔
284
  if (pTbCur->paused) {
21,449!
285
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
21,453✔
286
    locked = 1;
21,455✔
287
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
21,455✔
288
    if (code != 0) {
21,453!
289
      TSDB_CHECK_CODE(code, lino, _exit);
×
290
    }
291

292
    if (first) {
21,453✔
293
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
20,531✔
294
      TSDB_CHECK_CODE(code, lino, _exit);
20,532!
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;
21,454✔
311
  }
312

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

320
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
687,266✔
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);
687,266✔
327
    if (ret < 0) {
685,947✔
328
      return ret;
20,537✔
329
    }
330

331
    tDecoderClear(&pTbCur->mr.coder);
665,410✔
332

333
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
667,333✔
334
    if (ret) return ret;
664,625!
335

336
    if (pTbCur->mr.me.type == jumpTableType) {
664,625✔
337
      continue;
13,210✔
338
    }
339

340
    break;
651,415✔
341
  }
342

343
  return 0;
651,415✔
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) {
1,393,931✔
375
  void           *pData = NULL;
1,393,931✔
376
  int             nData = 0;
1,393,931✔
377
  int64_t         version;
378
  SSchemaWrapper  schema = {0};
1,393,931✔
379
  SSchemaWrapper *pSchema = NULL;
1,393,931✔
380
  SDecoder        dc = {0};
1,393,931✔
381
  if (lock) {
1,393,931✔
382
    metaRLock(pMeta);
1,373,997✔
383
  }
384
_query:
1,396,032✔
385
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
1,475,356✔
386
    goto _err;
49✔
387
  }
388

389
  version = ((SUidIdxVal *)pData)[0].version;
1,475,724✔
390

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

395
  SMetaEntry me = {0};
1,475,825✔
396
  tDecoderInit(&dc, pData, nData);
1,475,825✔
397
  int32_t code = metaDecodeEntry(&dc, &me);
1,475,372✔
398
  if (code) {
1,475,223!
399
    tDecoderClear(&dc);
×
400
    goto _err;
×
401
  }
402
  if (me.type == TSDB_SUPER_TABLE) {
1,475,223✔
403
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
1,135,278✔
404
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
1,135,709✔
405
      tDecoderClear(&dc);
1,135,709✔
406
      goto _exit;
1,135,189✔
407
    }
408
  } else if (me.type == TSDB_CHILD_TABLE) {
339,945✔
409
    uid = me.ctbEntry.suid;
79,296✔
410
    if (createTime != NULL){
79,296✔
411
      *createTime = me.ctbEntry.btime;
77,572✔
412
    }
413
    tDecoderClear(&dc);
79,296✔
414
    goto _query;
79,324✔
415
  } else {
416
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
260,649✔
417
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
260,749✔
418
      tDecoderClear(&dc);
260,749✔
419
      goto _exit;
260,722✔
420
    }
421
  }
422
  tDecoderClear(&dc);
370✔
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:
1,396,041✔
437
  if (lock) {
1,396,041✔
438
    metaULock(pMeta);
1,374,881✔
439
  }
440
  tdbFree(pData);
1,396,734✔
441
  return pSchema;
1,396,723✔
442

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

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

458
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
851,914✔
459
  if (pCtbCur == NULL) {
853,473!
460
    return NULL;
×
461
  }
462

463
  pCtbCur->pMeta = pMeta;
853,473✔
464
  pCtbCur->suid = uid;
853,473✔
465
  pCtbCur->lock = lock;
853,473✔
466
  pCtbCur->paused = 1;
853,473✔
467

468
  ret = metaResumeCtbCursor(pCtbCur, 1);
853,473✔
469
  if (ret < 0) {
852,715!
470
    return NULL;
×
471
  }
472
  return pCtbCur;
852,715✔
473
}
474

475
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
853,178✔
476
  if (pCtbCur) {
853,178!
477
    if (!pCtbCur->paused) {
853,260✔
478
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
809,938!
479
      if (pCtbCur->pCur) {
810,220!
480
        tdbTbcClose(pCtbCur->pCur);
810,231✔
481
      }
482
    }
483
    tdbFree(pCtbCur->pKey);
853,457✔
484
    tdbFree(pCtbCur->pVal);
853,419✔
485
  }
486
  taosMemoryFree(pCtbCur);
853,332✔
487
}
853,649✔
488

489
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
43,313✔
490
  if (!pCtbCur->paused) {
43,313!
491
    tdbTbcClose((TBC *)pCtbCur->pCur);
43,337✔
492
    if (pCtbCur->lock) {
43,375!
493
      metaULock(pCtbCur->pMeta);
43,377✔
494
    }
495
    pCtbCur->paused = 1;
43,366✔
496
  }
497
}
43,342✔
498

499
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
852,342✔
500
  if (pCtbCur->paused) {
852,342!
501
    pCtbCur->paused = 0;
852,814✔
502

503
    if (pCtbCur->lock) {
852,814✔
504
      metaRLock(pCtbCur->pMeta);
832,952✔
505
    }
506
    int ret = 0;
852,854✔
507
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
852,854✔
508
    if (ret < 0) {
853,288!
509
      metaCloseCtbCursor(pCtbCur);
×
510
      return -1;
×
511
    }
512

513
    if (first) {
853,340!
514
      SCtbIdxKey ctbIdxKey;
515
      // move to the suid
516
      ctbIdxKey.suid = pCtbCur->suid;
853,340✔
517
      ctbIdxKey.uid = INT64_MIN;
853,340✔
518
      int c = 0;
853,340✔
519
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
853,340✔
520
      if (c > 0) {
852,898✔
521
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
91,172✔
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;
852,405✔
534
}
535

536
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
4,133,488✔
537
  int         ret;
538
  SCtbIdxKey *pCtbIdxKey;
539

540
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
4,133,488✔
541
  if (ret < 0) {
4,129,474✔
542
    return 0;
366,857✔
543
  }
544

545
  pCtbIdxKey = pCtbCur->pKey;
3,762,617✔
546
  if (pCtbIdxKey->suid > pCtbCur->suid) {
3,762,617✔
547
    return 0;
487,167✔
548
  }
549

550
  return pCtbIdxKey->uid;
3,275,450✔
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) {
94,848✔
564
  SMStbCursor *pStbCur = NULL;
94,848✔
565
  int          ret = 0;
94,848✔
566
  int          c = 0;
94,848✔
567

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

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

578
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
94,855✔
579
  if (ret < 0) {
94,863!
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);
94,863✔
588
  if (c > 0) {
94,857!
589
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
590
  }
591

592
  return pStbCur;
94,858✔
593
}
594

595
void metaCloseStbCursor(SMStbCursor *pStbCur) {
94,854✔
596
  if (pStbCur) {
94,854!
597
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
94,856!
598
    if (pStbCur->pCur) {
94,855!
599
      tdbTbcClose(pStbCur->pCur);
94,858✔
600

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

605
    taosMemoryFree(pStbCur);
94,853✔
606
  }
607
}
94,861✔
608

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

612
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
197,822✔
613
  if (ret < 0) {
197,840✔
614
    return 0;
94,861✔
615
  }
616
  return *(tb_uid_t *)pStbCur->pKey;
102,979✔
617
}
618

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

623
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL);
1,251,974✔
624
  if (!pSW) return NULL;
1,254,275✔
625

626
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
1,254,274✔
627

628
  taosMemoryFree(pSW->pSchema);
1,254,457✔
629
  taosMemoryFree(pSW);
1,253,922✔
630
  return pTSchema;
1,254,379✔
631
}
632

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

641
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,246,837✔
642
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,246,837✔
643
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
1,249,044!
UNCOV
644
    return terrno;
×
645
  }
646
  return TSDB_CODE_SUCCESS;
1,249,095✔
647
}
648

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

653
  void     *pData = NULL;
420,872✔
654
  int       nData = 0;
420,872✔
655
  SSkmDbKey skmDbKey;
656
  if (sver <= 0) {
420,872✔
657
    SMetaInfo info;
658
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
178,964!
659
      sver = info.skmVer;
178,968✔
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)) {
420,849!
709
    code = TSDB_CODE_NOT_FOUND;
×
710
    goto _exit;
×
711
  }
712

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

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

728
  tDecoderInit(&dc, pData, nData);
420,964✔
729
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
419,209✔
730
  tDecoderClear(&dc);
419,209✔
731
  tdbFree(pData);
420,758✔
732
  if (TSDB_CODE_SUCCESS != code) {
420,811!
733
    taosMemoryFree(pSchemaWrapper->pSchema);
×
734
    goto _exit;
×
735
  }
736

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

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

746
_exit:
420,988✔
747
  return code;
420,988✔
748
}
749

750
// N.B. Called by statusReq per second
751
int64_t metaGetTbNum(SMeta *pMeta) {
199,016✔
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;
199,016✔
758
}
759

760
void metaUpdTimeSeriesNum(SMeta *pMeta) {
94,824✔
761
  int64_t nCtbTimeSeries = 0;
94,824✔
762
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
94,824!
763
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
94,821✔
764
  }
765
}
94,822✔
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;
337,800✔
770
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
337,814✔
771
    metaUpdTimeSeriesNum(pMeta);
92,565✔
772
  }
773

774
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
337,820✔
775
}
776

777
// type: 1 reported timeseries
778
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
337,800!
779
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
337,820✔
780
  if (type == 1) {
337,820✔
781
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
199,016✔
782
  }
783
  return nTimeSeries;
337,824✔
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) {
5,870,738✔
1043
  STag *tag = (STag *)pTag;
5,870,738✔
1044
  if (type == TSDB_DATA_TYPE_JSON) {
5,870,738✔
1045
    return tag;
10,215✔
1046
  }
1047
  bool find = tTagGet(tag, val);
5,860,523✔
1048

1049
  if (!find) {
5,863,841✔
1050
    return NULL;
38,409✔
1051
  }
1052

1053
  return val;
5,825,432✔
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 {
34,835✔
1103
    void   *entryKey = NULL;
34,841✔
1104
    int32_t nEntryKey = -1;
34,841✔
1105
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
34,841✔
1106
    if (valid < 0) break;
35,200✔
1107

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

1111
    terrno = TSDB_CODE_SUCCESS;
35,194✔
1112
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
35,196✔
1113
    if (terrno != TSDB_CODE_SUCCESS) {
34,960!
1114
      ret = terrno;
×
1115
      break;
×
1116
    }
1117
    if (cmp == 0) {
34,941!
1118
      if (taosArrayPush(pUids, &p->uid) == NULL) {
69,807!
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);
34,866✔
1129
    if (valid < 0) break;
34,835!
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) {
449✔
1252
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
449✔
1253
  SMetaFltParam *param = arg;
449✔
1254

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

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

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

1276
  metaRLock(pMeta);
450✔
1277

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

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

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

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

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

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

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

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

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

1316
  if (param->val == NULL) {
450!
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)) {
450!
1321
      tagData = varDataVal(param->val);
×
1322
      nTagData = varDataLen(param->val);
×
1323

1324
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
×
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;
450✔
1340
      nTagData = tDataTypes[param->type].bytes;
450✔
1341
    }
1342
  }
1343

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

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

1351
  int     count = 0;
450✔
1352
  int32_t valid = 0;
450✔
1353
  bool    found = false;
450✔
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;
450✔
1360
  do {
11,534✔
1361
    void   *entryKey = NULL, *entryVal = NULL;
11,984✔
1362
    int32_t nEntryKey, nEntryVal;
1363

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

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

1375
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
11,605!
1376
      if (found == true) break;  //
78!
1377
      if (diffCidCount > TRY_ERROR_LIMIT) break;
×
1378
      diffCidCount++;
×
1379
      count++;
×
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,527✔
1390
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
11,527✔
1391
    if (terrno != TSDB_CODE_SUCCESS) {
11,519!
1392
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1393
      break;
×
1394
    }
1395
    if (cmp == 0) {
11,527✔
1396
      // match
1397
      tb_uid_t tuid = 0;
10,707✔
1398
      if (IS_VAR_DATA_TYPE(pKey->type)) {
10,707!
1399
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
×
1400
      } else {
1401
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
10,708✔
1402
      }
1403
      if (taosArrayPush(pUids, &tuid) == NULL) {
10,713!
1404
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1405
      }
1406
      found = true;
10,713✔
1407
    } else {
1408
      if (param->equal == true) {
820✔
1409
        if (count > TRY_ERROR_LIMIT) break;
113!
1410
        count++;
113✔
1411
      }
1412
    }
1413
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
11,533✔
1414
    if (valid < 0) {
11,532!
1415
      code = valid;
×
1416
      break;
×
1417
    }
1418
  } while (1);
1419

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

1427
  taosMemoryFree(buf);
450✔
1428
  taosMemoryFree(pKey);
450✔
1429

1430
  taosMemoryFree(pCursor);
450✔
1431

1432
  return code;
450✔
1433
}
1434

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

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

1447
  return ret;
596✔
1448
}
1449

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

1454
  int32_t isLock = false;
42✔
1455
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
42!
1456
  for (int i = 0; i < sz; i++) {
640✔
1457
    STUidTagInfo *p = taosArrayGet(uidList, i);
598✔
1458

1459
    if (i % LIMIT == 0) {
598✔
1460
      if (isLock) metaULock(pMeta);
42!
1461

1462
      metaRLock(pMeta);
42✔
1463
      isLock = true;
42✔
1464
    }
1465

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

1474
        TAOS_RETURN(terrno);
×
1475
      }
1476
      memcpy(p->pTagVal, val, len);
599✔
1477
      tdbFree(val);
599✔
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);
42!
1485
  return 0;
42✔
1486
}
1487

1488
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
96,418✔
1489
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
96,418✔
1490
  if (!pCur) {
96,516!
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;
96,516✔
1498
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
96,516✔
1499
  if (numOfElems > 0) {
96,526✔
1500
    pSepecifiedUidMap =
1501
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
44,874✔
1502
    for (int i = 0; i < numOfElems; i++) {
241,300✔
1503
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
196,423✔
1504
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
196,419✔
1505
      if (code) {
196,472!
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
96,529✔
1514
    while (1) {
314,738✔
1515
      tb_uid_t uid = metaCtbCursorNext(pCur);
366,329✔
1516
      if (uid == 0) {
361,125✔
1517
        break;
51,721✔
1518
      }
1519

1520
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
309,404✔
1521
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
309,404✔
1522
      if (!info.pTagVal) {
315,543!
1523
        metaCloseCtbCursor(pCur);
×
1524
        taosHashCleanup(pSepecifiedUidMap);
×
1525
        return terrno;
×
1526
      }
1527
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
315,543✔
1528
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
314,738!
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) {
237,109✔
1537
      tb_uid_t uid = metaCtbCursorNext(pCur);
282,047✔
1538
      if (uid == 0) {
281,834✔
1539
        break;
44,872✔
1540
      }
1541

1542
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
236,962✔
1543
      if (index == NULL) {
237,289✔
1544
        continue;
41,044✔
1545
      }
1546

1547
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
196,245✔
1548
      if (pTagInfo->pTagVal == NULL) {
196,246✔
1549
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
196,240✔
1550
        if (!pTagInfo->pTagVal) {
196,325✔
1551
          metaCloseCtbCursor(pCur);
266✔
1552
          taosHashCleanup(pSepecifiedUidMap);
×
1553
          return terrno;
×
1554
        }
1555
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
196,059✔
1556
      }
1557
    }
1558
  }
1559

1560
  taosHashCleanup(pSepecifiedUidMap);
96,593✔
1561
  metaCloseCtbCursor(pCur);
96,587✔
1562
  return TSDB_CODE_SUCCESS;
96,623✔
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) {
8,533,015✔
1568
  int32_t code = 0;
8,533,015✔
1569
  void   *pData = NULL;
8,533,015✔
1570
  int     nData = 0;
8,533,015✔
1571
  int     lock = 0;
8,533,015✔
1572

1573
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
8,533,015!
1574
    lock = 1;
2,818,600✔
1575
  }
1576

1577
  if (!lock) metaRLock(pMeta);
8,533,015✔
1578

1579
  // search cache
1580
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
8,540,800✔
1581
    if (!lock) metaULock(pMeta);
8,504,015✔
1582
    goto _exit;
8,504,369✔
1583
  }
1584

1585
  // search TDB
1586
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
34,842✔
1587
    // not found
1588
    if (!lock) metaULock(pMeta);
29,491✔
1589
    goto _exit;
29,492✔
1590
  }
1591

1592
  if (!lock) metaULock(pMeta);
5,602✔
1593

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

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

1611
  if (lock) {
5,602✔
1612
    metaRLock(pReader->pMeta);
3,280✔
1613
  }
1614

1615
_exit:
2,322✔
1616
  tdbFree(pData);
8,539,463✔
1617
  return code;
8,538,599✔
1618
}
1619

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

1623
  if (!numOfTables && !numOfCols) goto _exit;
240,858!
1624

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

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

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

1649
  if (numOfTables) *numOfTables = ctbNum;
20,498✔
1650
  if (numOfCols) *numOfCols = colNum;
20,498✔
1651

1652
  state.uid = uid;
20,498✔
1653
  state.ctbNum = ctbNum;
20,498✔
1654
  state.colNum = colNum;
20,498✔
1655

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

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

1664
  metaULock(pVnodeObj->pMeta);
20,498✔
1665

1666
_exit:
240,923✔
1667
  return code;
240,923✔
1668
}
1669

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

1673
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
141,094✔
1674
    stats.ctbNum += deltaCtb;
140,739✔
1675
    stats.colNum += deltaCol;
140,739✔
1676
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
140,739✔
1677
    if (code) {
140,731!
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
}
141,088✔
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