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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

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

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 hits per line

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

0.0
/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

UNCOV
20
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
×
UNCOV
21
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
×
UNCOV
22
  metaReaderDoInit(pReader, pMeta, flags);
×
UNCOV
23
  pReader->pAPI = pAPI;
×
UNCOV
24
}
×
25

UNCOV
26
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
×
UNCOV
27
  memset(pReader, 0, sizeof(*pReader));
×
UNCOV
28
  pReader->pMeta = pMeta;
×
UNCOV
29
  pReader->flags = flags;
×
UNCOV
30
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
×
UNCOV
31
    metaRLock(pMeta);
×
32
  }
UNCOV
33
}
×
34

UNCOV
35
void metaReaderReleaseLock(SMetaReader *pReader) {
×
UNCOV
36
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
×
UNCOV
37
    metaULock(pReader->pMeta);
×
UNCOV
38
    pReader->flags |= META_READER_NOLOCK;
×
39
  }
UNCOV
40
}
×
41

UNCOV
42
void metaReaderClear(SMetaReader *pReader) {
×
UNCOV
43
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
×
UNCOV
44
    metaULock(pReader->pMeta);
×
45
  }
UNCOV
46
  tDecoderClear(&pReader->coder);
×
UNCOV
47
  tdbFree(pReader->pBuf);
×
UNCOV
48
  pReader->pBuf = NULL;
×
UNCOV
49
}
×
50

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

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

61
  // decode the entry
UNCOV
62
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
×
63

UNCOV
64
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
×
UNCOV
65
  if (code) {
×
66
    tDecoderClear(&pReader->coder);
×
67
    return code;
×
68
  }
69
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
70

UNCOV
71
  return 0;
×
72
}
73

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

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

UNCOV
83
  metaULock(pVnodeObj->pMeta);
×
UNCOV
84
  return true;
×
85
}
86

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

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

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

UNCOV
100
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
×
UNCOV
101
  SMeta *pMeta = pReader->pMeta;
×
102

103
  SMetaInfo info;
UNCOV
104
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
×
UNCOV
105
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
106
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
107
  }
108

UNCOV
109
  return metaGetTableEntryByVersion(pReader, info.version, uid);
×
110
}
111

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

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

UNCOV
121
  uid = *(tb_uid_t *)pReader->pBuf;
×
UNCOV
122
  return metaReaderGetTableEntryByUid(pReader, uid);
×
123
}
124

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

UNCOV
130
  metaRLock(pMeta);
×
131

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

UNCOV
137
  metaULock(pMeta);
×
138

UNCOV
139
  return uid;
×
140
}
141

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

UNCOV
152
  STR_TO_VARSTR(tbName, mr.me.name);
×
UNCOV
153
  metaReaderClear(&mr);
×
154

UNCOV
155
  return 0;
×
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
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
168
  metaReaderClear(&mr);
×
169

170
  return 0;
×
171
}
172

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

UNCOV
178
  SMetaReader *pReader = &mr;
×
179

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

UNCOV
186
  *uid = *(tb_uid_t *)pReader->pBuf;
×
187

UNCOV
188
  metaReaderClear(&mr);
×
189

UNCOV
190
  return 0;
×
191
}
192

UNCOV
193
int metaGetTableTypeSuidByName(void *pVnode, char *tbName, ETableType *tbType, uint64_t* suid) {
×
UNCOV
194
  int         code = 0;
×
UNCOV
195
  SMetaReader mr = {0};
×
UNCOV
196
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK);
×
197

UNCOV
198
  code = metaGetTableEntryByName(&mr, tbName);
×
UNCOV
199
  if (code == 0) *tbType = mr.me.type;
×
UNCOV
200
  if (TSDB_CHILD_TABLE == mr.me.type) {
×
UNCOV
201
    *suid = mr.me.ctbEntry.suid;
×
UNCOV
202
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
×
UNCOV
203
    *suid = mr.me.uid;
×
204
  } else {
UNCOV
205
    *suid = 0;
×
206
  }
207

UNCOV
208
  metaReaderClear(&mr);
×
UNCOV
209
  return code;
×
210
}
211

212
int metaReadNext(SMetaReader *pReader) {
×
213
  SMeta *pMeta = pReader->pMeta;
×
214

215
  // TODO
216

217
  return 0;
×
218
}
219

220
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
221
  int         code = -1;
×
222
  SMetaReader mr = {0};
×
223
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK);
×
224
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
225
  if (code < 0) {
×
226
    goto _exit;
×
227
  }
228
  if (mr.me.type == TSDB_CHILD_TABLE) {
×
229
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
230
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
×
231
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
232
  } else {
233
    goto _exit;
×
234
  }
235

236
  code = 0;
×
237

238
_exit:
×
239
  metaReaderClear(&mr);
×
240
  return code;
×
241
}
242

243
#if 1  // ===================================================
UNCOV
244
SMTbCursor *metaOpenTbCursor(void *pVnode) {
×
UNCOV
245
  SMTbCursor *pTbCur = NULL;
×
246
  int32_t     code;
247

UNCOV
248
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
×
UNCOV
249
  if (pTbCur == NULL) {
×
250
    return NULL;
×
251
  }
252

UNCOV
253
  SVnode *pVnodeObj = pVnode;
×
254
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
UNCOV
255
  pTbCur->pMeta = pVnodeObj->pMeta;
×
UNCOV
256
  pTbCur->paused = 1;
×
UNCOV
257
  code = metaResumeTbCursor(pTbCur, 1, 0);
×
UNCOV
258
  if (code) {
×
UNCOV
259
    terrno = code;
×
260
    taosMemoryFree(pTbCur);
×
261
    return NULL;
×
262
  }
UNCOV
263
  return pTbCur;
×
264
}
265

UNCOV
266
void metaCloseTbCursor(SMTbCursor *pTbCur) {
×
UNCOV
267
  if (pTbCur) {
×
UNCOV
268
    tdbFree(pTbCur->pKey);
×
UNCOV
269
    tdbFree(pTbCur->pVal);
×
UNCOV
270
    if (!pTbCur->paused) {
×
UNCOV
271
      metaReaderClear(&pTbCur->mr);
×
UNCOV
272
      if (pTbCur->pDbc) {
×
UNCOV
273
        tdbTbcClose((TBC *)pTbCur->pDbc);
×
274
      }
275
    }
UNCOV
276
    taosMemoryFree(pTbCur);
×
277
  }
UNCOV
278
}
×
279

UNCOV
280
void metaPauseTbCursor(SMTbCursor *pTbCur) {
×
UNCOV
281
  if (!pTbCur->paused) {
×
UNCOV
282
    metaReaderClear(&pTbCur->mr);
×
UNCOV
283
    tdbTbcClose((TBC *)pTbCur->pDbc);
×
UNCOV
284
    pTbCur->paused = 1;
×
285
  }
UNCOV
286
}
×
UNCOV
287
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
×
UNCOV
288
  int32_t code = 0;
×
289
  int32_t lino;
UNCOV
290
  int8_t  locked = 0;
×
UNCOV
291
  if (pTbCur->paused) {
×
UNCOV
292
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK);
×
UNCOV
293
    locked = 1;
×
UNCOV
294
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
×
UNCOV
295
    if (code != 0) {
×
296
      TSDB_CHECK_CODE(code, lino, _exit);
×
297
    }
298

UNCOV
299
    if (first) {
×
UNCOV
300
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
×
UNCOV
301
      TSDB_CHECK_CODE(code, lino, _exit);
×
302
    } else {
UNCOV
303
      int c = 1;
×
UNCOV
304
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
×
UNCOV
305
      TSDB_CHECK_CODE(code, lino, _exit);
×
UNCOV
306
      if (c == 0) {
×
UNCOV
307
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
×
308
      } else if (c < 0) {
×
309
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
310
        TSDB_CHECK_CODE(code, lino, _exit);
×
311
      } else {
312
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
313
        TSDB_CHECK_CODE(code, lino, _exit);
×
314
      }
315
    }
316

UNCOV
317
    pTbCur->paused = 0;
×
318
  }
319

320
_exit:
×
UNCOV
321
  if (code != 0 && locked) {
×
322
    metaReaderReleaseLock(&pTbCur->mr);
×
323
  }
UNCOV
324
  return code;
×
325
}
326

UNCOV
327
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
328
  int    ret;
329
  void  *pBuf;
330
  STbCfg tbCfg;
331

332
  for (;;) {
UNCOV
333
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
UNCOV
334
    if (ret < 0) {
×
UNCOV
335
      return ret;
×
336
    }
337

UNCOV
338
    tDecoderClear(&pTbCur->mr.coder);
×
339

UNCOV
340
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
UNCOV
341
    if (ret) return ret;
×
342

UNCOV
343
    if (pTbCur->mr.me.type == jumpTableType) {
×
UNCOV
344
      continue;
×
345
    }
346

UNCOV
347
    break;
×
348
  }
349

UNCOV
350
  return 0;
×
351
}
352

353
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
354
  int    ret;
355
  void  *pBuf;
356
  STbCfg tbCfg;
357

358
  for (;;) {
359
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
360
    if (ret < 0) {
×
361
      return -1;
×
362
    }
363

364
    tDecoderClear(&pTbCur->mr.coder);
×
365

366
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
367
    if (ret < 0) {
×
368
      return ret;
×
369
    }
370

371
    if (pTbCur->mr.me.type == jumpTableType) {
×
372
      continue;
×
373
    }
374

375
    break;
×
376
  }
377

378
  return 0;
×
379
}
380

UNCOV
381
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
×
UNCOV
382
  void           *pData = NULL;
×
UNCOV
383
  int             nData = 0;
×
384
  int64_t         version;
UNCOV
385
  SSchemaWrapper  schema = {0};
×
UNCOV
386
  SSchemaWrapper *pSchema = NULL;
×
UNCOV
387
  SDecoder        dc = {0};
×
UNCOV
388
  if (lock) {
×
UNCOV
389
    metaRLock(pMeta);
×
390
  }
UNCOV
391
_query:
×
UNCOV
392
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
×
UNCOV
393
    goto _err;
×
394
  }
395

UNCOV
396
  version = ((SUidIdxVal *)pData)[0].version;
×
397

UNCOV
398
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
×
399
    goto _err;
×
400
  }
401

UNCOV
402
  SMetaEntry me = {0};
×
UNCOV
403
  tDecoderInit(&dc, pData, nData);
×
UNCOV
404
  int32_t code = metaDecodeEntry(&dc, &me);
×
UNCOV
405
  if (code) {
×
406
    tDecoderClear(&dc);
×
407
    goto _err;
×
408
  }
UNCOV
409
  if (me.type == TSDB_SUPER_TABLE) {
×
UNCOV
410
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
×
UNCOV
411
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
×
UNCOV
412
      tDecoderClear(&dc);
×
UNCOV
413
      goto _exit;
×
414
    }
UNCOV
415
  } else if (me.type == TSDB_CHILD_TABLE) {
×
UNCOV
416
    uid = me.ctbEntry.suid;
×
UNCOV
417
    tDecoderClear(&dc);
×
UNCOV
418
    goto _query;
×
419
  } else {
UNCOV
420
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
×
UNCOV
421
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
×
UNCOV
422
      tDecoderClear(&dc);
×
UNCOV
423
      goto _exit;
×
424
    }
425
  }
426
  tDecoderClear(&dc);
×
427

428
  // query from skm db
UNCOV
429
  if (tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData) < 0) {
×
430
    goto _err;
×
431
  }
432

UNCOV
433
  tDecoderInit(&dc, pData, nData);
×
UNCOV
434
  if (tDecodeSSchemaWrapperEx(&dc, &schema) != 0) {
×
435
    goto _err;
×
436
  }
UNCOV
437
  pSchema = tCloneSSchemaWrapper(&schema);
×
UNCOV
438
  tDecoderClear(&dc);
×
439

UNCOV
440
_exit:
×
UNCOV
441
  if (lock) {
×
UNCOV
442
    metaULock(pMeta);
×
443
  }
UNCOV
444
  tdbFree(pData);
×
UNCOV
445
  return pSchema;
×
446

UNCOV
447
_err:
×
UNCOV
448
  if (lock) {
×
UNCOV
449
    metaULock(pMeta);
×
450
  }
UNCOV
451
  tdbFree(pData);
×
UNCOV
452
  return NULL;
×
453
}
454

UNCOV
455
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
×
UNCOV
456
  void           *pData = NULL;
×
UNCOV
457
  int             nData = 0;
×
UNCOV
458
  int64_t         version = 0;
×
UNCOV
459
  SDecoder        dc = {0};
×
UNCOV
460
  int64_t         createTime = INT64_MAX;
×
UNCOV
461
  if (lock) {
×
UNCOV
462
    metaRLock(pMeta);
×
463
  }
464

UNCOV
465
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
×
466
    goto _exit;
×
467
  }
468

UNCOV
469
  version = ((SUidIdxVal *)pData)[0].version;
×
470

UNCOV
471
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
×
472
    goto _exit;
×
473
  }
474

UNCOV
475
  SMetaEntry me = {0};
×
UNCOV
476
  tDecoderInit(&dc, pData, nData);
×
UNCOV
477
  int32_t code = metaDecodeEntry(&dc, &me);
×
UNCOV
478
  if (code) {
×
479
    tDecoderClear(&dc);
×
480
    goto _exit;
×
481
  }
UNCOV
482
  if (me.type == TSDB_CHILD_TABLE) {
×
UNCOV
483
    createTime = me.ctbEntry.btime;
×
484
  }
UNCOV
485
  tDecoderClear(&dc);
×
486

UNCOV
487
  _exit:
×
UNCOV
488
  if (lock) {
×
UNCOV
489
    metaULock(pMeta);
×
490
  }
UNCOV
491
  tdbFree(pData);
×
UNCOV
492
  return createTime;
×
493
}
494

UNCOV
495
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock) {
×
UNCOV
496
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
×
UNCOV
497
  SMCtbCursor *pCtbCur = NULL;
×
498
  SCtbIdxKey   ctbIdxKey;
UNCOV
499
  int          ret = 0;
×
UNCOV
500
  int          c = 0;
×
501

UNCOV
502
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
×
UNCOV
503
  if (pCtbCur == NULL) {
×
504
    return NULL;
×
505
  }
506

UNCOV
507
  pCtbCur->pMeta = pMeta;
×
UNCOV
508
  pCtbCur->suid = uid;
×
UNCOV
509
  pCtbCur->lock = lock;
×
UNCOV
510
  pCtbCur->paused = 1;
×
511

UNCOV
512
  ret = metaResumeCtbCursor(pCtbCur, 1);
×
UNCOV
513
  if (ret < 0) {
×
514
    return NULL;
×
515
  }
UNCOV
516
  return pCtbCur;
×
517
}
518

UNCOV
519
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
×
UNCOV
520
  if (pCtbCur) {
×
UNCOV
521
    if (!pCtbCur->paused) {
×
UNCOV
522
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
×
UNCOV
523
      if (pCtbCur->pCur) {
×
UNCOV
524
        tdbTbcClose(pCtbCur->pCur);
×
525
      }
526
    }
UNCOV
527
    tdbFree(pCtbCur->pKey);
×
UNCOV
528
    tdbFree(pCtbCur->pVal);
×
529
  }
UNCOV
530
  taosMemoryFree(pCtbCur);
×
UNCOV
531
}
×
532

UNCOV
533
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
×
UNCOV
534
  if (!pCtbCur->paused) {
×
UNCOV
535
    tdbTbcClose((TBC *)pCtbCur->pCur);
×
UNCOV
536
    if (pCtbCur->lock) {
×
UNCOV
537
      metaULock(pCtbCur->pMeta);
×
538
    }
UNCOV
539
    pCtbCur->paused = 1;
×
540
  }
UNCOV
541
}
×
542

UNCOV
543
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
×
UNCOV
544
  if (pCtbCur->paused) {
×
UNCOV
545
    pCtbCur->paused = 0;
×
546

UNCOV
547
    if (pCtbCur->lock) {
×
UNCOV
548
      metaRLock(pCtbCur->pMeta);
×
549
    }
UNCOV
550
    int ret = 0;
×
UNCOV
551
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
×
UNCOV
552
    if (ret < 0) {
×
553
      metaCloseCtbCursor(pCtbCur);
×
554
      return -1;
×
555
    }
556

UNCOV
557
    if (first) {
×
558
      SCtbIdxKey ctbIdxKey;
559
      // move to the suid
UNCOV
560
      ctbIdxKey.suid = pCtbCur->suid;
×
UNCOV
561
      ctbIdxKey.uid = INT64_MIN;
×
UNCOV
562
      int c = 0;
×
UNCOV
563
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
×
UNCOV
564
      if (c > 0) {
×
UNCOV
565
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
566
      }
567
    } else {
568
      int c = 0;
×
569
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
570
      if (c < 0) {
×
571
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
572
      } else {
573
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
574
      }
575
    }
576
  }
UNCOV
577
  return 0;
×
578
}
579

UNCOV
580
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
×
581
  int         ret;
582
  SCtbIdxKey *pCtbIdxKey;
583

UNCOV
584
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
×
UNCOV
585
  if (ret < 0) {
×
UNCOV
586
    return 0;
×
587
  }
588

UNCOV
589
  pCtbIdxKey = pCtbCur->pKey;
×
UNCOV
590
  if (pCtbIdxKey->suid > pCtbCur->suid) {
×
UNCOV
591
    return 0;
×
592
  }
593

UNCOV
594
  return pCtbIdxKey->uid;
×
595
}
596

597
struct SMStbCursor {
598
  SMeta   *pMeta;
599
  TBC     *pCur;
600
  tb_uid_t suid;
601
  void    *pKey;
602
  void    *pVal;
603
  int      kLen;
604
  int      vLen;
605
};
606

UNCOV
607
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
×
UNCOV
608
  SMStbCursor *pStbCur = NULL;
×
UNCOV
609
  int          ret = 0;
×
UNCOV
610
  int          c = 0;
×
611

UNCOV
612
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
×
UNCOV
613
  if (pStbCur == NULL) {
×
614
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
615
    return NULL;
×
616
  }
617

UNCOV
618
  pStbCur->pMeta = pMeta;
×
UNCOV
619
  pStbCur->suid = suid;
×
UNCOV
620
  metaRLock(pMeta);
×
621

UNCOV
622
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
×
UNCOV
623
  if (ret < 0) {
×
624
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
625
    metaULock(pMeta);
×
626
    taosMemoryFree(pStbCur);
×
627
    return NULL;
×
628
  }
629

630
  // move to the suid
UNCOV
631
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
×
UNCOV
632
  if (c > 0) {
×
633
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
634
  }
635

UNCOV
636
  return pStbCur;
×
637
}
638

UNCOV
639
void metaCloseStbCursor(SMStbCursor *pStbCur) {
×
UNCOV
640
  if (pStbCur) {
×
UNCOV
641
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
×
UNCOV
642
    if (pStbCur->pCur) {
×
UNCOV
643
      tdbTbcClose(pStbCur->pCur);
×
644

UNCOV
645
      tdbFree(pStbCur->pKey);
×
UNCOV
646
      tdbFree(pStbCur->pVal);
×
647
    }
648

UNCOV
649
    taosMemoryFree(pStbCur);
×
650
  }
UNCOV
651
}
×
652

UNCOV
653
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
×
654
  int ret;
655

UNCOV
656
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
×
UNCOV
657
  if (ret < 0) {
×
UNCOV
658
    return 0;
×
659
  }
UNCOV
660
  return *(tb_uid_t *)pStbCur->pKey;
×
661
}
662

UNCOV
663
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
×
UNCOV
664
  STSchema       *pTSchema = NULL;
×
UNCOV
665
  SSchemaWrapper *pSW = NULL;
×
666

UNCOV
667
  pSW = metaGetTableSchema(pMeta, uid, sver, lock);
×
UNCOV
668
  if (!pSW) return NULL;
×
669

UNCOV
670
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
×
671

UNCOV
672
  taosMemoryFree(pSW->pSchema);
×
UNCOV
673
  taosMemoryFree(pSW);
×
UNCOV
674
  return pTSchema;
×
675
}
676

UNCOV
677
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
×
UNCOV
678
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
×
UNCOV
679
  if (*ppTSchema == NULL) {
×
680
    return terrno;
×
681
  }
UNCOV
682
  return TSDB_CODE_SUCCESS;
×
683
}
684

UNCOV
685
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
×
UNCOV
686
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
×
UNCOV
687
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
×
688
    return terrno;
×
689
  }
UNCOV
690
  return TSDB_CODE_SUCCESS;
×
691
}
692

UNCOV
693
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
×
UNCOV
694
  int32_t code = 0;
×
695
  int32_t lino;
696

UNCOV
697
  void     *pData = NULL;
×
UNCOV
698
  int       nData = 0;
×
699
  SSkmDbKey skmDbKey;
UNCOV
700
  if (sver <= 0) {
×
701
    SMetaInfo info;
UNCOV
702
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
×
UNCOV
703
      sver = info.skmVer;
×
704
    } else {
UNCOV
705
      TBC *pSkmDbC = NULL;
×
706
      int  c;
707

UNCOV
708
      skmDbKey.uid = suid ? suid : uid;
×
UNCOV
709
      skmDbKey.sver = INT32_MAX;
×
710

UNCOV
711
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
×
UNCOV
712
      TSDB_CHECK_CODE(code, lino, _exit);
×
UNCOV
713
      metaRLock(pMeta);
×
714

UNCOV
715
      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
×
716
        metaULock(pMeta);
×
717
        tdbTbcClose(pSkmDbC);
×
718
        code = TSDB_CODE_NOT_FOUND;
×
719
        goto _exit;
×
720
      }
721

UNCOV
722
      if (c == 0) {
×
723
        metaULock(pMeta);
×
724
        tdbTbcClose(pSkmDbC);
×
725
        code = TSDB_CODE_FAILED;
×
726
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
727
        goto _exit;
×
728
      }
729

UNCOV
730
      if (c < 0) {
×
UNCOV
731
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
×
732
      }
733

UNCOV
734
      const void *pKey = NULL;
×
UNCOV
735
      int32_t     nKey = 0;
×
UNCOV
736
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
×
737

UNCOV
738
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
×
739
        metaULock(pMeta);
×
740
        tdbTbcClose(pSkmDbC);
×
741
        code = TSDB_CODE_NOT_FOUND;
×
742
        goto _exit;
×
743
      }
744

UNCOV
745
      sver = ((SSkmDbKey *)pKey)->sver;
×
746

UNCOV
747
      metaULock(pMeta);
×
UNCOV
748
      tdbTbcClose(pSkmDbC);
×
749
    }
750
  }
751

UNCOV
752
  if (!(sver > 0)) {
×
753
    code = TSDB_CODE_NOT_FOUND;
×
754
    goto _exit;
×
755
  }
756

UNCOV
757
  skmDbKey.uid = suid ? suid : uid;
×
UNCOV
758
  skmDbKey.sver = sver;
×
UNCOV
759
  metaRLock(pMeta);
×
UNCOV
760
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
×
761
    metaULock(pMeta);
×
762
    code = TSDB_CODE_NOT_FOUND;
×
763
    goto _exit;
×
764
  }
UNCOV
765
  metaULock(pMeta);
×
766

767
  // decode
UNCOV
768
  SDecoder        dc = {0};
×
769
  SSchemaWrapper  schema;
UNCOV
770
  SSchemaWrapper *pSchemaWrapper = &schema;
×
771

UNCOV
772
  tDecoderInit(&dc, pData, nData);
×
UNCOV
773
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
×
UNCOV
774
  tDecoderClear(&dc);
×
UNCOV
775
  tdbFree(pData);
×
UNCOV
776
  if (TSDB_CODE_SUCCESS != code) {
×
777
    taosMemoryFree(pSchemaWrapper->pSchema);
×
778
    goto _exit;
×
779
  }
780

781
  // convert
UNCOV
782
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
×
UNCOV
783
  if (pTSchema == NULL) {
×
784
    code = TSDB_CODE_OUT_OF_MEMORY;
×
785
  }
786

UNCOV
787
  *ppTSchema = pTSchema;
×
UNCOV
788
  taosMemoryFree(pSchemaWrapper->pSchema);
×
789

UNCOV
790
_exit:
×
UNCOV
791
  return code;
×
792
}
793

794
// N.B. Called by statusReq per second
UNCOV
795
int64_t metaGetTbNum(SMeta *pMeta) {
×
796
  // num of child tables (excluding normal tables , stables and others)
797

798
  /* int64_t num = 0; */
799
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
800

UNCOV
801
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
×
802
}
803

UNCOV
804
void metaUpdTimeSeriesNum(SMeta *pMeta) {
×
UNCOV
805
  int64_t nCtbTimeSeries = 0;
×
UNCOV
806
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
×
UNCOV
807
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
×
808
  }
UNCOV
809
}
×
810

811
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
812
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
UNCOV
813
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
×
UNCOV
814
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
×
UNCOV
815
    metaUpdTimeSeriesNum(pMeta);
×
816
  }
817

UNCOV
818
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
×
819
}
820

821
// type: 1 reported timeseries
UNCOV
822
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
×
UNCOV
823
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
×
UNCOV
824
  if (type == 1) {
×
UNCOV
825
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
×
826
  }
UNCOV
827
  return nTimeSeries;
×
828
}
829

830
typedef struct {
831
  SMeta   *pMeta;
832
  TBC     *pCur;
833
  tb_uid_t uid;
834
  void    *pKey;
835
  void    *pVal;
836
  int      kLen;
837
  int      vLen;
838
} SMSmaCursor;
839

840
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
841
  SMSmaCursor *pSmaCur = NULL;
×
842
  SSmaIdxKey   smaIdxKey;
843
  int          ret;
844
  int          c;
845

846
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
847
  if (pSmaCur == NULL) {
×
848
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
849
    return NULL;
×
850
  }
851

852
  pSmaCur->pMeta = pMeta;
×
853
  pSmaCur->uid = uid;
×
854
  metaRLock(pMeta);
×
855

856
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
857
  if (ret < 0) {
×
858
    metaULock(pMeta);
×
859
    taosMemoryFree(pSmaCur);
×
860
    return NULL;
×
861
  }
862

863
  // move to the suid
864
  smaIdxKey.uid = uid;
×
865
  smaIdxKey.smaUid = INT64_MIN;
×
866
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
867
  if (c > 0) {
×
868
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
869
  }
870

871
  return pSmaCur;
×
872
}
873

874
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
875
  if (pSmaCur) {
×
876
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
877
    if (pSmaCur->pCur) {
×
878
      tdbTbcClose(pSmaCur->pCur);
×
879
      pSmaCur->pCur = NULL;
×
880

881
      tdbFree(pSmaCur->pKey);
×
882
      tdbFree(pSmaCur->pVal);
×
883
    }
884

885
    taosMemoryFree(pSmaCur);
×
886
  }
887
}
×
888

889
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
890
  int         ret;
891
  SSmaIdxKey *pSmaIdxKey;
892

893
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
894
  if (ret < 0) {
×
895
    return 0;
×
896
  }
897

898
  pSmaIdxKey = pSmaCur->pKey;
×
899
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
900
    return 0;
×
901
  }
902

903
  return pSmaIdxKey->uid;
×
904
}
905

906
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
907
  STSmaWrapper *pSW = NULL;
×
908
  SArray       *pSmaIds = NULL;
×
909

910
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
911
    return NULL;
×
912
  }
913

914
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
915
  if (!pSW) {
×
916
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
917
    goto _err;
×
918
  }
919

920
  pSW->number = taosArrayGetSize(pSmaIds);
×
921
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
922

923
  if (!pSW->tSma) {
×
924
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
925
    goto _err;
×
926
  }
927

928
  SMetaReader mr = {0};
×
929
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
930
  int64_t smaId;
931
  int     smaIdx = 0;
×
932
  STSma  *pTSma = NULL;
×
933
  for (int i = 0; i < pSW->number; ++i) {
×
934
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
935
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
936
      tDecoderClear(&mr.coder);
×
937
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
938
      continue;
×
939
    }
940
    tDecoderClear(&mr.coder);
×
941
    pTSma = pSW->tSma + smaIdx;
×
942
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
943
    if (deepCopy) {
×
944
      if (pTSma->exprLen > 0) {
×
945
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
946
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
947
          goto _err;
×
948
        }
949
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
950
      }
951
      if (pTSma->tagsFilterLen > 0) {
×
952
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
953
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
954
          goto _err;
×
955
        }
956
      }
957
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
958
    } else {
959
      pTSma->exprLen = 0;
×
960
      pTSma->expr = NULL;
×
961
      pTSma->tagsFilterLen = 0;
×
962
      pTSma->tagsFilter = NULL;
×
963
    }
964

965
    ++smaIdx;
×
966
  }
967

968
  if (smaIdx <= 0) goto _err;
×
969
  pSW->number = smaIdx;
×
970

971
  metaReaderClear(&mr);
×
972
  taosArrayDestroy(pSmaIds);
×
973
  return pSW;
×
974
_err:
×
975
  metaReaderClear(&mr);
×
976
  taosArrayDestroy(pSmaIds);
×
977
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
978
  return NULL;
×
979
}
980

UNCOV
981
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
×
UNCOV
982
  STSma      *pTSma = NULL;
×
UNCOV
983
  SMetaReader mr = {0};
×
UNCOV
984
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
UNCOV
985
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
×
986
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
987
    metaReaderClear(&mr);
×
988
    return NULL;
×
989
  }
UNCOV
990
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
×
UNCOV
991
  if (!pTSma) {
×
992
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
993
    metaReaderClear(&mr);
×
994
    return NULL;
×
995
  }
996

UNCOV
997
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
998

UNCOV
999
  metaReaderClear(&mr);
×
UNCOV
1000
  return pTSma;
×
1001
}
1002

1003
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
1004
  SArray     *pUids = NULL;
×
1005
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1006

1007
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
1008
  if (!pCur) {
×
1009
    return NULL;
×
1010
  }
1011

1012
  while (1) {
×
1013
    tb_uid_t id = metaSmaCursorNext(pCur);
×
1014
    if (id == 0) {
×
1015
      break;
×
1016
    }
1017

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

1027
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
1028

1029
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
1030
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1031
      metaCloseSmaCursor(pCur);
×
1032
      taosArrayDestroy(pUids);
×
1033
      return NULL;
×
1034
    }
1035
  }
1036

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

1041
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
1042
  SArray     *pUids = NULL;
×
1043
  SSmaIdxKey *pSmaIdxKey = NULL;
×
1044
  tb_uid_t    lastUid = 0;
×
1045

1046
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
1047
  if (!pCur) {
×
1048
    return NULL;
×
1049
  }
1050

1051
  while (1) {
×
1052
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
1053
    if (uid == 0) {
×
1054
      break;
×
1055
    }
1056

1057
    if (lastUid == uid) {
×
1058
      continue;
×
1059
    }
1060

1061
    lastUid = uid;
×
1062

1063
    if (!pUids) {
×
1064
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
1065
      if (!pUids) {
×
1066
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1067
        metaCloseSmaCursor(pCur);
×
1068
        return NULL;
×
1069
      }
1070
    }
1071

1072
    if (!taosArrayPush(pUids, &uid)) {
×
1073
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1074
      metaCloseSmaCursor(pCur);
×
1075
      taosArrayDestroy(pUids);
×
1076
      return NULL;
×
1077
    }
1078
  }
1079

1080
  metaCloseSmaCursor(pCur);
×
1081
  return pUids;
×
1082
}
1083

1084
#endif
1085

UNCOV
1086
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
×
UNCOV
1087
  STag *tag = (STag *)pTag;
×
UNCOV
1088
  if (type == TSDB_DATA_TYPE_JSON) {
×
UNCOV
1089
    return tag;
×
1090
  }
UNCOV
1091
  bool find = tTagGet(tag, val);
×
1092

UNCOV
1093
  if (!find) {
×
UNCOV
1094
    return NULL;
×
1095
  }
1096

UNCOV
1097
  return val;
×
1098
}
1099

1100
typedef struct {
1101
  SMeta   *pMeta;
1102
  TBC     *pCur;
1103
  tb_uid_t suid;
1104
  int16_t  cid;
1105
  int16_t  type;
1106
  void    *pKey;
1107
  void    *pVal;
1108
  int32_t  kLen;
1109
  int32_t  vLen;
1110
} SIdxCursor;
1111

UNCOV
1112
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
UNCOV
1113
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
UNCOV
1114
  SMetaFltParam *param = arg;
×
UNCOV
1115
  int32_t        ret = 0;
×
1116

UNCOV
1117
  SIdxCursor *pCursor = NULL;
×
UNCOV
1118
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
UNCOV
1119
  if (pCursor == NULL) {
×
1120
    return terrno;
×
1121
  }
UNCOV
1122
  pCursor->pMeta = pMeta;
×
UNCOV
1123
  pCursor->suid = param->suid;
×
UNCOV
1124
  pCursor->cid = param->cid;
×
UNCOV
1125
  pCursor->type = param->type;
×
1126

UNCOV
1127
  metaRLock(pMeta);
×
UNCOV
1128
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
×
UNCOV
1129
  if (ret != 0) {
×
1130
    goto END;
×
1131
  }
UNCOV
1132
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
×
1133

UNCOV
1134
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
×
UNCOV
1135
  SBtimeIdxKey *pBtimeKey = &btimeKey;
×
1136

UNCOV
1137
  int cmp = 0;
×
UNCOV
1138
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
×
1139
    goto END;
×
1140
  }
1141

UNCOV
1142
  int32_t valid = 0;
×
UNCOV
1143
  int32_t count = 0;
×
1144

1145
  static const int8_t TRY_ERROR_LIMIT = 1;
UNCOV
1146
  do {
×
UNCOV
1147
    void   *entryKey = NULL;
×
UNCOV
1148
    int32_t nEntryKey = -1;
×
UNCOV
1149
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
×
UNCOV
1150
    if (valid < 0) break;
×
1151

UNCOV
1152
    SBtimeIdxKey *p = entryKey;
×
UNCOV
1153
    if (count > TRY_ERROR_LIMIT) break;
×
1154

UNCOV
1155
    terrno = TSDB_CODE_SUCCESS;
×
UNCOV
1156
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
×
UNCOV
1157
    if (terrno != TSDB_CODE_SUCCESS) {
×
1158
      ret = terrno;
×
1159
      break;
×
1160
    }
UNCOV
1161
    if (cmp == 0) {
×
UNCOV
1162
      if (taosArrayPush(pUids, &p->uid) == NULL) {
×
1163
        ret = terrno;
×
1164
        break;
×
1165
      }
1166
    } else {
1167
      if (param->equal == true) {
×
1168
        if (count > TRY_ERROR_LIMIT) break;
×
1169
        count++;
×
1170
      }
1171
    }
UNCOV
1172
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
UNCOV
1173
    if (valid < 0) break;
×
1174
  } while (1);
1175

UNCOV
1176
END:
×
UNCOV
1177
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
UNCOV
1178
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
UNCOV
1179
  taosMemoryFree(pCursor);
×
UNCOV
1180
  return ret;
×
1181
}
1182

1183
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1184
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1185
  SMetaFltParam *param = arg;
×
1186
  int32_t        ret = 0;
×
1187
  char          *buf = NULL;
×
1188

1189
  STagIdxKey *pKey = NULL;
×
1190
  int32_t     nKey = 0;
×
1191

1192
  SIdxCursor *pCursor = NULL;
×
1193
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1194
  if (pCursor == NULL) {
×
1195
    return terrno;
×
1196
  }
1197
  pCursor->pMeta = pMeta;
×
1198
  pCursor->suid = param->suid;
×
1199
  pCursor->cid = param->cid;
×
1200
  pCursor->type = param->type;
×
1201

1202
  char *pName = param->val;
×
1203

1204
  metaRLock(pMeta);
×
1205
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
1206
  if (ret != 0) {
×
1207
    goto END;
×
1208
  }
1209

1210
  int cmp = 0;
×
1211
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
1212
    goto END;
×
1213
  }
1214
  int32_t valid = 0;
×
1215
  int32_t count = 0;
×
1216

1217
  int32_t TRY_ERROR_LIMIT = 1;
×
1218
  do {
×
1219
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
1220
    int32_t nEntryKey = -1, nEntryVal = 0;
×
1221
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
1222
    if (valid < 0) break;
×
1223

1224
    if (count > TRY_ERROR_LIMIT) break;
×
1225

1226
    char *pTableKey = (char *)pEntryKey;
×
1227
    terrno = TSDB_CODE_SUCCESS;
×
1228
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
1229
    if (terrno != TSDB_CODE_SUCCESS) {
×
1230
      ret = terrno;
×
1231
      goto END;
×
1232
    }
1233
    if (cmp == 0) {
×
1234
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
1235
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1236
        ret = terrno;
×
1237
        goto END;
×
1238
      }
1239
    } else {
1240
      if (param->equal == true) {
×
1241
        if (count > TRY_ERROR_LIMIT) break;
×
1242
        count++;
×
1243
      }
1244
    }
1245
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
1246
    if (valid < 0) {
×
1247
      break;
×
1248
    }
1249
  } while (1);
1250

1251
END:
×
1252
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1253
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1254
  taosMemoryFree(buf);
×
1255
  taosMemoryFree(pKey);
×
1256

1257
  taosMemoryFree(pCursor);
×
1258

1259
  return ret;
×
1260
}
1261
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
1262
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
1263
  SMetaFltParam *param = arg;
×
1264
  int32_t        ret = 0;
×
1265
  char          *buf = NULL;
×
1266

1267
  STtlIdxKey *pKey = NULL;
×
1268
  int32_t     nKey = 0;
×
1269

1270
  SIdxCursor *pCursor = NULL;
×
1271
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
1272
  if (pCursor == NULL) {
×
1273
    return terrno;
×
1274
  }
1275
  pCursor->pMeta = pMeta;
×
1276
  pCursor->suid = param->suid;
×
1277
  pCursor->cid = param->cid;
×
1278
  pCursor->type = param->type;
×
1279

1280
  metaRLock(pMeta);
×
1281
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
1282

1283
END:
×
1284
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
1285
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
1286
  taosMemoryFree(buf);
×
1287
  taosMemoryFree(pKey);
×
1288

1289
  taosMemoryFree(pCursor);
×
1290

1291
  return ret;
×
1292
  // impl later
1293
  return 0;
1294
}
UNCOV
1295
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
UNCOV
1296
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
UNCOV
1297
  SMetaFltParam *param = arg;
×
1298

UNCOV
1299
  SMetaEntry oStbEntry = {0};
×
UNCOV
1300
  int32_t    code = 0;
×
UNCOV
1301
  char      *buf = NULL;
×
UNCOV
1302
  void      *pData = NULL;
×
UNCOV
1303
  int        nData = 0;
×
1304

UNCOV
1305
  SDecoder    dc = {0};
×
UNCOV
1306
  STbDbKey    tbDbKey = {0};
×
UNCOV
1307
  STagIdxKey *pKey = NULL;
×
UNCOV
1308
  int32_t     nKey = 0;
×
1309

UNCOV
1310
  SIdxCursor *pCursor = NULL;
×
UNCOV
1311
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
UNCOV
1312
  if (!pCursor) {
×
1313
    return terrno;
×
1314
  }
UNCOV
1315
  pCursor->pMeta = pMeta;
×
UNCOV
1316
  pCursor->suid = param->suid;
×
UNCOV
1317
  pCursor->cid = param->cid;
×
UNCOV
1318
  pCursor->type = param->type;
×
1319

UNCOV
1320
  metaRLock(pMeta);
×
1321

UNCOV
1322
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
×
1323

UNCOV
1324
  tbDbKey.uid = param->suid;
×
UNCOV
1325
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
×
1326

UNCOV
1327
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
×
1328

UNCOV
1329
  tDecoderInit(&dc, pData, nData);
×
1330

UNCOV
1331
  code = metaDecodeEntry(&dc, &oStbEntry);
×
UNCOV
1332
  if (code) {
×
1333
    tDecoderClear(&dc);
×
1334
    goto END;
×
1335
  }
1336

UNCOV
1337
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
×
1338
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
1339
  }
1340

UNCOV
1341
  code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1342
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
×
UNCOV
1343
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
×
UNCOV
1344
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
×
UNCOV
1345
      code = 0;
×
1346
    } else {
UNCOV
1347
      TAOS_CHECK_GOTO(code, NULL, END);
×
1348
    }
1349
  }
1350

UNCOV
1351
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
×
UNCOV
1352
  if (code != 0) {
×
UNCOV
1353
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
1354
  }
1355

UNCOV
1356
  int32_t maxSize = 0;
×
UNCOV
1357
  int32_t nTagData = 0;
×
UNCOV
1358
  void   *tagData = NULL;
×
1359

UNCOV
1360
  if (param->val == NULL) {
×
1361
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
1362
    goto END;
×
1363
  } else {
UNCOV
1364
    if (IS_VAR_DATA_TYPE(param->type)) {
×
UNCOV
1365
      tagData = varDataVal(param->val);
×
UNCOV
1366
      nTagData = varDataLen(param->val);
×
1367

UNCOV
1368
      if (param->type == TSDB_DATA_TYPE_NCHAR) {
×
1369
        maxSize = 4 * nTagData + 1;
×
1370
        buf = taosMemoryCalloc(1, maxSize);
×
1371
        if (buf == NULL) {
×
1372
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1373
        }
1374

1375
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize, NULL)) {
×
1376
          TAOS_CHECK_GOTO(terrno, NULL, END);
×
1377
        }
1378

1379
        tagData = buf;
×
1380
        nTagData = maxSize;
×
1381
      }
1382
    } else {
UNCOV
1383
      tagData = param->val;
×
UNCOV
1384
      nTagData = tDataTypes[param->type].bytes;
×
1385
    }
1386
  }
1387

UNCOV
1388
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
×
1389
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
1390
                  NULL, END);
1391

UNCOV
1392
  int cmp = 0;
×
UNCOV
1393
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
×
1394

UNCOV
1395
  int     count = 0;
×
UNCOV
1396
  int32_t valid = 0;
×
UNCOV
1397
  bool    found = false;
×
1398

1399
  static const int8_t TRY_ERROR_LIMIT = 1;
1400

1401
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
1402
  /// target:                        [suid, cid2, type2]
UNCOV
1403
  int diffCidCount = 0;
×
UNCOV
1404
  do {
×
UNCOV
1405
    void   *entryKey = NULL, *entryVal = NULL;
×
1406
    int32_t nEntryKey, nEntryVal;
1407

UNCOV
1408
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
×
UNCOV
1409
    if (valid < 0) {
×
UNCOV
1410
      code = valid;
×
1411
    }
UNCOV
1412
    if (count > TRY_ERROR_LIMIT) {
×
UNCOV
1413
      break;
×
1414
    }
1415

UNCOV
1416
    STagIdxKey *p = entryKey;
×
UNCOV
1417
    if (p == NULL) break;
×
1418

UNCOV
1419
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
×
UNCOV
1420
      if (found == true) break;  //
×
UNCOV
1421
      if (diffCidCount > TRY_ERROR_LIMIT) break;
×
UNCOV
1422
      diffCidCount++;
×
UNCOV
1423
      count++;
×
UNCOV
1424
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
UNCOV
1425
      if (valid < 0) {
×
1426
        code = valid;
×
1427
        break;
×
1428
      } else {
UNCOV
1429
        continue;
×
1430
      }
1431
    }
1432

UNCOV
1433
    terrno = TSDB_CODE_SUCCESS;
×
UNCOV
1434
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
×
UNCOV
1435
    if (terrno != TSDB_CODE_SUCCESS) {
×
1436
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
1437
      break;
×
1438
    }
UNCOV
1439
    if (cmp == 0) {
×
1440
      // match
UNCOV
1441
      tb_uid_t tuid = 0;
×
UNCOV
1442
      if (IS_VAR_DATA_TYPE(pKey->type)) {
×
1443
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
×
1444
      } else {
UNCOV
1445
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
×
1446
      }
UNCOV
1447
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
1448
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
1449
      }
UNCOV
1450
      found = true;
×
1451
    } else {
UNCOV
1452
      if (param->equal == true) {
×
UNCOV
1453
        if (count > TRY_ERROR_LIMIT) break;
×
UNCOV
1454
        count++;
×
1455
      }
1456
    }
UNCOV
1457
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
UNCOV
1458
    if (valid < 0) {
×
1459
      code = valid;
×
1460
      break;
×
1461
    }
1462
  } while (1);
1463

UNCOV
1464
END:
×
UNCOV
1465
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
UNCOV
1466
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
UNCOV
1467
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
UNCOV
1468
  tDecoderClear(&dc);
×
UNCOV
1469
  tdbFree(pData);
×
1470

UNCOV
1471
  taosMemoryFree(buf);
×
UNCOV
1472
  taosMemoryFree(pKey);
×
1473

UNCOV
1474
  taosMemoryFree(pCursor);
×
1475

UNCOV
1476
  return code;
×
1477
}
1478

UNCOV
1479
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
×
UNCOV
1480
  int ret = 0;
×
UNCOV
1481
  if (lock) {
×
1482
    metaRLock(pMeta);
×
1483
  }
1484

UNCOV
1485
  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
×
UNCOV
1486
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
×
UNCOV
1487
  if (lock) {
×
1488
    metaULock(pMeta);
×
1489
  }
1490

UNCOV
1491
  return ret;
×
1492
}
1493

UNCOV
1494
int32_t metaGetTableTagsByUids(void *pVnode, int64_t suid, SArray *uidList) {
×
UNCOV
1495
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
×
UNCOV
1496
  const int32_t LIMIT = 128;
×
1497

UNCOV
1498
  int32_t isLock = false;
×
UNCOV
1499
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
×
UNCOV
1500
  for (int i = 0; i < sz; i++) {
×
UNCOV
1501
    STUidTagInfo *p = taosArrayGet(uidList, i);
×
1502

UNCOV
1503
    if (i % LIMIT == 0) {
×
UNCOV
1504
      if (isLock) metaULock(pMeta);
×
1505

UNCOV
1506
      metaRLock(pMeta);
×
UNCOV
1507
      isLock = true;
×
1508
    }
1509

1510
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
UNCOV
1511
    void   *val = NULL;
×
UNCOV
1512
    int32_t len = 0;
×
UNCOV
1513
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
×
UNCOV
1514
      p->pTagVal = taosMemoryMalloc(len);
×
UNCOV
1515
      if (!p->pTagVal) {
×
1516
        if (isLock) metaULock(pMeta);
×
1517

1518
        TAOS_RETURN(terrno);
×
1519
      }
UNCOV
1520
      memcpy(p->pTagVal, val, len);
×
UNCOV
1521
      tdbFree(val);
×
1522
    } else {
1523
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64 "", TD_VID(pMeta->pVnode), suid,
×
1524
                p->uid);
1525
    }
1526
  }
1527
  //  }
UNCOV
1528
  if (isLock) metaULock(pMeta);
×
UNCOV
1529
  return 0;
×
1530
}
1531

UNCOV
1532
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo) {
×
UNCOV
1533
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
×
UNCOV
1534
  if (!pCur) {
×
1535
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1536
  }
1537

1538
  // If len > 0 means there already have uids, and we only want the
1539
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
1540
  // in the hash map, that may require a lot of memory
UNCOV
1541
  SHashObj *pSepecifiedUidMap = NULL;
×
UNCOV
1542
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
×
UNCOV
1543
  if (numOfElems > 0) {
×
1544
    pSepecifiedUidMap =
UNCOV
1545
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
UNCOV
1546
    for (int i = 0; i < numOfElems; i++) {
×
UNCOV
1547
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
×
UNCOV
1548
      int32_t       code = taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
×
UNCOV
1549
      if (code) {
×
1550
        metaCloseCtbCursor(pCur);
×
1551
        taosHashCleanup(pSepecifiedUidMap);
×
1552
        return code;
×
1553
      }
1554
    }
1555
  }
1556

UNCOV
1557
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
×
UNCOV
1558
    while (1) {
×
UNCOV
1559
      tb_uid_t uid = metaCtbCursorNext(pCur);
×
UNCOV
1560
      if (uid == 0) {
×
UNCOV
1561
        break;
×
1562
      }
1563

UNCOV
1564
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
×
UNCOV
1565
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
×
UNCOV
1566
      if (!info.pTagVal) {
×
1567
        metaCloseCtbCursor(pCur);
×
1568
        taosHashCleanup(pSepecifiedUidMap);
×
1569
        return terrno;
×
1570
      }
UNCOV
1571
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
×
UNCOV
1572
      if (taosArrayPush(pUidTagInfo, &info) == NULL) {
×
1573
        taosMemoryFreeClear(info.pTagVal);
×
1574
        metaCloseCtbCursor(pCur);
×
1575
        taosHashCleanup(pSepecifiedUidMap);
×
1576
        return terrno;
×
1577
      }
1578
    }
1579
  } else {  // only the specified tables need to be added
UNCOV
1580
    while (1) {
×
UNCOV
1581
      tb_uid_t uid = metaCtbCursorNext(pCur);
×
UNCOV
1582
      if (uid == 0) {
×
UNCOV
1583
        break;
×
1584
      }
1585

UNCOV
1586
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
×
UNCOV
1587
      if (index == NULL) {
×
UNCOV
1588
        continue;
×
1589
      }
1590

UNCOV
1591
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
×
UNCOV
1592
      if (pTagInfo->pTagVal == NULL) {
×
UNCOV
1593
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
×
UNCOV
1594
        if (!pTagInfo->pTagVal) {
×
1595
          metaCloseCtbCursor(pCur);
×
1596
          taosHashCleanup(pSepecifiedUidMap);
×
1597
          return terrno;
×
1598
        }
UNCOV
1599
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
×
1600
      }
1601
    }
1602
  }
1603

UNCOV
1604
  taosHashCleanup(pSepecifiedUidMap);
×
UNCOV
1605
  metaCloseCtbCursor(pCur);
×
UNCOV
1606
  return TSDB_CODE_SUCCESS;
×
1607
}
1608

1609
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1610

UNCOV
1611
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
×
UNCOV
1612
  int32_t code = 0;
×
UNCOV
1613
  void   *pData = NULL;
×
UNCOV
1614
  int     nData = 0;
×
UNCOV
1615
  int     lock = 0;
×
1616

UNCOV
1617
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
×
UNCOV
1618
    lock = 1;
×
1619
  }
1620

UNCOV
1621
  if (!lock) metaRLock(pMeta);
×
1622

1623
  // search cache
UNCOV
1624
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
×
UNCOV
1625
    if (!lock) metaULock(pMeta);
×
UNCOV
1626
    goto _exit;
×
1627
  }
1628

1629
  // search TDB
UNCOV
1630
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
×
1631
    // not found
UNCOV
1632
    if (!lock) metaULock(pMeta);
×
UNCOV
1633
    goto _exit;
×
1634
  }
1635

UNCOV
1636
  if (!lock) metaULock(pMeta);
×
1637

UNCOV
1638
  pInfo->uid = uid;
×
UNCOV
1639
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
×
UNCOV
1640
  pInfo->version = ((SUidIdxVal *)pData)->version;
×
UNCOV
1641
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
×
1642

UNCOV
1643
  if (lock) {
×
UNCOV
1644
    metaULock(pReader->pMeta);
×
1645
    // metaReaderReleaseLock(pReader);
1646
  }
1647
  // upsert the cache
UNCOV
1648
  metaWLock(pMeta);
×
UNCOV
1649
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
×
UNCOV
1650
  if (ret != 0) {
×
1651
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
1652
  }
UNCOV
1653
  metaULock(pMeta);
×
1654

UNCOV
1655
  if (lock) {
×
UNCOV
1656
    metaRLock(pReader->pMeta);
×
1657
  }
1658

UNCOV
1659
_exit:
×
UNCOV
1660
  tdbFree(pData);
×
UNCOV
1661
  return code;
×
1662
}
1663

UNCOV
1664
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols) {
×
UNCOV
1665
  int32_t code = 0;
×
1666

UNCOV
1667
  if (!numOfTables && !numOfCols) goto _exit;
×
1668

UNCOV
1669
  SVnode *pVnodeObj = pVnode;
×
UNCOV
1670
  metaRLock(pVnodeObj->pMeta);
×
1671

1672
  // fast path: search cache
UNCOV
1673
  SMetaStbStats state = {0};
×
UNCOV
1674
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
×
UNCOV
1675
    metaULock(pVnodeObj->pMeta);
×
UNCOV
1676
    if (numOfTables) *numOfTables = state.ctbNum;
×
UNCOV
1677
    if (numOfCols) *numOfCols = state.colNum;
×
UNCOV
1678
    goto _exit;
×
1679
  }
1680

1681
  // slow path: search TDB
UNCOV
1682
  int64_t ctbNum = 0;
×
UNCOV
1683
  int32_t colNum = 0;
×
UNCOV
1684
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
×
UNCOV
1685
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1686
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
×
1687
  }
UNCOV
1688
  metaULock(pVnodeObj->pMeta);
×
UNCOV
1689
  if (TSDB_CODE_SUCCESS != code) {
×
1690
    goto _exit;
×
1691
  }
1692

UNCOV
1693
  if (numOfTables) *numOfTables = ctbNum;
×
UNCOV
1694
  if (numOfCols) *numOfCols = colNum;
×
1695

UNCOV
1696
  state.uid = uid;
×
UNCOV
1697
  state.ctbNum = ctbNum;
×
UNCOV
1698
  state.colNum = colNum;
×
1699

1700
  // upsert the cache
UNCOV
1701
  metaWLock(pVnodeObj->pMeta);
×
1702

UNCOV
1703
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
×
UNCOV
1704
  if (ret) {
×
1705
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d", uid, ctbNum, colNum);
×
1706
  }
1707

UNCOV
1708
  metaULock(pVnodeObj->pMeta);
×
1709

UNCOV
1710
_exit:
×
UNCOV
1711
  return code;
×
1712
}
1713

UNCOV
1714
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol) {
×
UNCOV
1715
  SMetaStbStats stats = {0};
×
1716

UNCOV
1717
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
×
UNCOV
1718
    stats.ctbNum += deltaCtb;
×
UNCOV
1719
    stats.colNum += deltaCol;
×
UNCOV
1720
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
×
UNCOV
1721
    if (code) {
×
1722
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d",
×
1723
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol);
1724
    }
1725
  }
UNCOV
1726
}
×
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