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

taosdata / TDengine / #3627

02 Mar 2025 11:16PM UTC coverage: 63.596% (-0.2%) from 63.764%
#3627

push

travis-ci

GitHub
Merge pull request #29973 from taosdata/doc/internal

148665 of 299855 branches covered (49.58%)

Branch coverage included in aggregate %.

233076 of 300407 relevant lines covered (77.59%)

17543856.65 hits per line

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

57.0
/source/dnode/vnode/src/meta/metaOpen.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 "vnd.h"
18

19
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
20
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
21
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
22
int        tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
23
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
24
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
25
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
26

27
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
28
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
29

30
static void metaInitLock(SMeta *pMeta) {
11,655✔
31
  TdThreadRwlockAttr attr;
32
  (void)taosThreadRwlockAttrInit(&attr);
11,655✔
33
  (void)taosThreadRwlockAttrSetKindNP(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
11,660✔
34
  (void)taosThreadRwlockInit(&pMeta->lock, &attr);
11,659✔
35
  (void)taosThreadRwlockAttrDestroy(&attr);
11,663✔
36
  return;
11,650✔
37
}
38
static void metaDestroyLock(SMeta *pMeta) { (void)taosThreadRwlockDestroy(&pMeta->lock); }
11,648✔
39

40
static void metaCleanup(SMeta **ppMeta);
41

42
static void doScan(SMeta *pMeta) {
×
43
  TBC    *cursor = NULL;
×
44
  int32_t code;
45

46
  // open file to write
47
  char path[TSDB_FILENAME_LEN] = {0};
×
48
  snprintf(path, TSDB_FILENAME_LEN - 1, "%s%s", pMeta->path, TD_DIRSEP "scan.txt");
×
49
  TdFilePtr fp = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
×
50
  if (fp == NULL) {
×
51
    metaError("failed to open file:%s, reason:%s", path, tstrerror(terrno));
×
52
    return;
×
53
  }
54

55
  code = tdbTbcOpen(pMeta->pTbDb, &cursor, NULL);
×
56
  if (code) {
×
57
    if (taosCloseFile(&fp) != 0) {
×
58
      metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
59
    }
60
    metaError("failed to open table.db cursor, reason:%s", tstrerror(terrno));
×
61
    return;
×
62
  }
63

64
  code = tdbTbcMoveToFirst(cursor);
×
65
  if (code) {
×
66
    if (taosCloseFile(&fp) != 0) {
×
67
      metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
68
    }
69
    tdbTbcClose(cursor);
×
70
    metaError("failed to move to first, reason:%s", tstrerror(terrno));
×
71
    return;
×
72
  }
73

74
  for (;;) {
×
75
    const void *pKey;
76
    int         kLen;
77
    const void *pVal;
78
    int         vLen;
79
    if (tdbTbcGet(cursor, &pKey, &kLen, &pVal, &vLen) < 0) {
×
80
      break;
×
81
    }
82

83
    // decode entry
84
    SDecoder   dc = {0};
×
85
    SMetaEntry me = {0};
×
86

87
    tDecoderInit(&dc, (uint8_t *)pVal, vLen);
×
88

89
    if (metaDecodeEntry(&dc, &me) < 0) {
×
90
      tDecoderClear(&dc);
×
91
      break;
×
92
    }
93

94
    // skip deleted entry
95
    if (tdbTbGet(pMeta->pUidIdx, &me.uid, sizeof(me.uid), NULL, NULL) == 0) {
×
96
      // print entry
97
      char buf[1024] = {0};
×
98
      if (me.type == TSDB_SUPER_TABLE) {
×
99
        snprintf(buf, sizeof(buf) - 1, "type: super table, version:%" PRId64 " uid: %" PRId64 " name: %s\n", me.version,
×
100
                 me.uid, me.name);
101

102
      } else if (me.type == TSDB_CHILD_TABLE) {
×
103
        snprintf(buf, sizeof(buf) - 1,
×
104
                 "type: child table, version:%" PRId64 " uid: %" PRId64 " name: %s suid:%" PRId64 "\n", me.version,
105
                 me.uid, me.name, me.ctbEntry.suid);
106
      } else {
107
        snprintf(buf, sizeof(buf) - 1, "type: normal table, version:%" PRId64 " uid: %" PRId64 " name: %s\n",
×
108
                 me.version, me.uid, me.name);
109
      }
110

111
      if (taosWriteFile(fp, buf, strlen(buf)) < 0) {
×
112
        metaError("failed to write file:%s, reason:%s", path, tstrerror(terrno));
×
113
        tDecoderClear(&dc);
×
114
        break;
×
115
      }
116
    }
117

118
    tDecoderClear(&dc);
×
119

120
    if (tdbTbcMoveToNext(cursor) < 0) {
×
121
      break;
×
122
    }
123
  }
124

125
  tdbTbcClose(cursor);
×
126

127
  // close file
128
  if (taosFsyncFile(fp) < 0) {
×
129
    metaError("failed to fsync file:%s, reason:%s", path, tstrerror(terrno));
×
130
  }
131
  if (taosCloseFile(&fp) < 0) {
×
132
    metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
133
  }
134
}
135

136
int32_t metaOpenImpl(SVnode *pVnode, SMeta **ppMeta, const char *metaDir, int8_t rollback) {
11,664✔
137
  SMeta  *pMeta = NULL;
11,664✔
138
  int32_t code = 0;
11,664✔
139
  int32_t lino;
140
  int32_t offset;
141
  int32_t pathLen = 0;
11,664✔
142
  char    path[TSDB_FILENAME_LEN] = {0};
11,664✔
143
  char    indexFullPath[128] = {0};
11,664✔
144

145
  // create handle
146
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
11,664✔
147
  offset = strlen(path);
11,676✔
148
  snprintf(path + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
11,676✔
149

150
  if (strncmp(metaDir, VNODE_META_TMP_DIR, strlen(VNODE_META_TMP_DIR)) == 0) {
11,676!
151
    taosRemoveDir(path);
×
152
  }
153

154
  pathLen = strlen(path) + 1;
11,676✔
155
  if ((pMeta = taosMemoryCalloc(1, sizeof(*pMeta) + pathLen)) == NULL) {
11,676!
156
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
157
  }
158

159
  metaInitLock(pMeta);
11,659✔
160

161
  pMeta->path = (char *)&pMeta[1];
11,655✔
162
  tstrncpy(pMeta->path, path, pathLen);
11,655✔
163
  int32_t ret = taosRealPath(pMeta->path, NULL, strlen(path) + 1);
11,655✔
164

165
  pMeta->pVnode = pVnode;
11,654✔
166

167
  // create path if not created yet
168
  code = taosMkDir(pMeta->path);
11,654✔
169
  TSDB_CHECK_CODE(code, lino, _exit);
11,666!
170

171
  // open env
172
  code = tdbOpen(pMeta->path, pVnode->config.szPage, pVnode->config.szCache, &pMeta->pEnv, rollback,
11,666✔
173
                 pVnode->config.tdbEncryptAlgorithm, pVnode->config.tdbEncryptKey);
11,666✔
174
  TSDB_CHECK_CODE(code, lino, _exit);
11,672!
175

176
  // open pTbDb
177
  code = tdbTbOpen("table.db", sizeof(STbDbKey), -1, tbDbKeyCmpr, pMeta->pEnv, &pMeta->pTbDb, 0);
11,672✔
178
  TSDB_CHECK_CODE(code, lino, _exit);
11,675!
179

180
  // open pSkmDb
181
  code = tdbTbOpen("schema.db", sizeof(SSkmDbKey), -1, skmDbKeyCmpr, pMeta->pEnv, &pMeta->pSkmDb, 0);
11,675✔
182
  TSDB_CHECK_CODE(code, lino, _exit);
11,674!
183

184
  // open pUidIdx
185
  code = tdbTbOpen("uid.idx", sizeof(tb_uid_t), sizeof(SUidIdxVal), uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pUidIdx, 0);
11,674✔
186
  TSDB_CHECK_CODE(code, lino, _exit);
11,675!
187

188
  // open pNameIdx
189
  code = tdbTbOpen("name.idx", -1, sizeof(tb_uid_t), NULL, pMeta->pEnv, &pMeta->pNameIdx, 0);
11,675✔
190
  TSDB_CHECK_CODE(code, lino, _exit);
11,675!
191

192
  // open pCtbIdx
193
  code = tdbTbOpen("ctb.idx", sizeof(SCtbIdxKey), -1, ctbIdxKeyCmpr, pMeta->pEnv, &pMeta->pCtbIdx, 0);
11,675✔
194
  TSDB_CHECK_CODE(code, lino, _exit);
11,677!
195

196
  // open pSuidIdx
197
  code = tdbTbOpen("suid.idx", sizeof(tb_uid_t), 0, uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pSuidIdx, 0);
11,677✔
198
  TSDB_CHECK_CODE(code, lino, _exit);
11,677!
199

200
  (void)tsnprintf(indexFullPath, sizeof(indexFullPath), "%s/%s", pMeta->path, "invert");
11,677✔
201
  ret = taosMkDir(indexFullPath);
11,676✔
202

203
  SIndexOpts opts = {.cacheSize = 8 * 1024 * 1024};
11,668✔
204
  code = indexOpen(&opts, indexFullPath, (SIndex **)&pMeta->pTagIvtIdx);
11,668✔
205
  TSDB_CHECK_CODE(code, lino, _exit);
11,660!
206

207
  code = tdbTbOpen("tag.idx", -1, 0, tagIdxKeyCmpr, pMeta->pEnv, &pMeta->pTagIdx, 0);
11,660✔
208
  TSDB_CHECK_CODE(code, lino, _exit);
11,677!
209

210
  // open pTtlMgr ("ttlv1.idx")
211
  char logPrefix[128] = {0};
11,677✔
212
  (void)tsnprintf(logPrefix, sizeof(logPrefix), "vgId:%d", TD_VID(pVnode));
11,677✔
213
  code = ttlMgrOpen(&pMeta->pTtlMgr, pMeta->pEnv, 0, logPrefix, tsTtlFlushThreshold);
11,669✔
214
  TSDB_CHECK_CODE(code, lino, _exit);
11,677!
215

216
  // open pSmaIdx
217
  code = tdbTbOpen("sma.idx", sizeof(SSmaIdxKey), 0, smaIdxKeyCmpr, pMeta->pEnv, &pMeta->pSmaIdx, 0);
11,677✔
218
  TSDB_CHECK_CODE(code, lino, _exit);
11,676!
219

220
  // idx table create time
221
  code = tdbTbOpen("ctime.idx", sizeof(SBtimeIdxKey), 0, btimeIdxCmpr, pMeta->pEnv, &pMeta->pBtimeIdx, 0);
11,676✔
222
  TSDB_CHECK_CODE(code, lino, _exit);
11,676!
223

224
  // idx num of col, normal table only
225
  code = tdbTbOpen("ncol.idx", sizeof(SNcolIdxKey), 0, ncolIdxCmpr, pMeta->pEnv, &pMeta->pNcolIdx, 0);
11,676✔
226
  TSDB_CHECK_CODE(code, lino, _exit);
11,677!
227

228
  code = tdbTbOpen("stream.task.db", sizeof(int64_t), -1, taskIdxKeyCmpr, pMeta->pEnv, &pMeta->pStreamDb, 0);
11,677✔
229
  TSDB_CHECK_CODE(code, lino, _exit);
11,677!
230

231
  code = metaCacheOpen(pMeta);
11,677✔
232
  TSDB_CHECK_CODE(code, lino, _exit);
11,677!
233

234
  code = metaInitTbFilterCache(pMeta);
11,677✔
235
  TSDB_CHECK_CODE(code, lino, _exit);
11,674!
236

237
#if 0
238
  // Do NOT remove this code, it is used to do debug stuff
239
  doScan(pMeta);
240
#endif
241

242
_exit:
11,674✔
243
  if (code) {
11,674!
244
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
245
    metaCleanup(&pMeta);
×
246
    *ppMeta = NULL;
×
247
  } else {
248
    metaDebug("vgId:%d %s success", TD_VID(pVnode), __func__);
11,674✔
249
    *ppMeta = pMeta;
11,675✔
250
  }
251
  return code;
11,675✔
252
}
253

254
void vnodeGetMetaPath(SVnode *pVnode, const char *metaDir, char *fname) {
23,335✔
255
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, fname, TSDB_FILENAME_LEN);
23,335✔
256
  int32_t offset = strlen(fname);
23,352✔
257
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
23,352✔
258
}
23,352✔
259

260
int32_t metaOpen(SVnode *pVnode, SMeta **ppMeta, int8_t rollback) {
11,667✔
261
  int32_t code = TSDB_CODE_SUCCESS;
11,667✔
262
  char    metaDir[TSDB_FILENAME_LEN] = {0};
11,667✔
263
  char    metaTempDir[TSDB_FILENAME_LEN] = {0};
11,667✔
264

265
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
11,667✔
266
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
11,664✔
267

268
  // Check file states
269
  if (!taosCheckExistFile(metaDir) && taosCheckExistFile(metaTempDir)) {
11,674!
270
    code = taosRenameFile(metaTempDir, metaDir);
×
271
    if (code) {
×
272
      metaError("vgId:%d, %s failed at %s:%d since %s: rename %s to %s failed", TD_VID(pVnode), __func__, __FILE__,
×
273
                __LINE__, tstrerror(code), metaTempDir, metaDir);
274
      return code;
×
275
    }
276
  }
277

278
  // Do open meta
279
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
11,672✔
280
  if (code) {
11,674!
281
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
282
    return code;
×
283
  }
284

285
  return TSDB_CODE_SUCCESS;
11,674✔
286
}
287

288
int32_t metaUpgrade(SVnode *pVnode, SMeta **ppMeta) {
11,677✔
289
  int32_t code = TSDB_CODE_SUCCESS;
11,677✔
290
  int32_t lino;
291
  SMeta  *pMeta = *ppMeta;
11,677✔
292

293
  if (ttlMgrNeedUpgrade(pMeta->pEnv)) {
11,677!
294
    code = metaBegin(pMeta, META_BEGIN_HEAP_OS);
×
295
    TSDB_CHECK_CODE(code, lino, _exit);
×
296

297
    code = ttlMgrUpgrade(pMeta->pTtlMgr, pMeta);
×
298
    TSDB_CHECK_CODE(code, lino, _exit);
×
299

300
    code = metaCommit(pMeta, pMeta->txn);
×
301
    TSDB_CHECK_CODE(code, lino, _exit);
×
302
  }
303

304
_exit:
11,676✔
305
  if (code) {
11,676!
306
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
307
    metaCleanup(ppMeta);
×
308
  }
309
  return code;
11,675✔
310
}
311

312
void metaClose(SMeta **ppMeta) {
11,643✔
313
  metaCleanup(ppMeta);
11,643✔
314
  return;
11,648✔
315
}
316

317
int metaAlterCache(SMeta *pMeta, int32_t nPage) {
×
318
  int32_t code = 0;
×
319
  metaWLock(pMeta);
×
320
  code = tdbAlter(pMeta->pEnv, nPage);
×
321
  metaULock(pMeta);
×
322

323
  if (code) {
×
324
    metaError("vgId:%d %s failed since %s", TD_VID(pMeta->pVnode), __func__, tstrerror(code));
×
325
  }
326
  return code;
×
327
}
328

329
void metaRLock(SMeta *pMeta) {
45,472,497✔
330
  metaTrace("meta rlock %p", &pMeta->lock);
45,472,497✔
331
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
45,472,497!
332
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
333
  }
334
}
45,494,539✔
335

336
void metaWLock(SMeta *pMeta) {
276,484✔
337
  metaTrace("meta wlock %p", &pMeta->lock);
276,484✔
338
  if (taosThreadRwlockWrlock(&pMeta->lock) != 0) {
276,484!
339
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
340
  }
341
}
276,577✔
342

343
void metaULock(SMeta *pMeta) {
45,756,582✔
344
  metaTrace("meta ulock %p", &pMeta->lock);
45,756,582✔
345
  if (taosThreadRwlockUnlock(&pMeta->lock) != 0) {
45,756,586!
346
    metaError("vgId:%d failed to unlock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
347
  }
348
}
45,769,685✔
349

350
static void metaCleanup(SMeta **ppMeta) {
11,641✔
351
  SMeta *pMeta = *ppMeta;
11,641✔
352
  if (pMeta) {
11,641!
353
    metaInfo("vgId:%d meta clean up, path:%s", TD_VID(pMeta->pVnode), pMeta->path);
11,641!
354
    if (pMeta->pEnv) metaAbort(pMeta);
11,647!
355
    if (pMeta->pCache) metaCacheClose(pMeta);
11,647!
356
#ifdef BUILD_NO_CALL
357
    if (pMeta->pIdx) metaCloseIdx(pMeta);
358
#endif
359
    if (pMeta->pStreamDb) tdbTbClose(pMeta->pStreamDb);
11,647!
360
    if (pMeta->pNcolIdx) tdbTbClose(pMeta->pNcolIdx);
11,647!
361
    if (pMeta->pBtimeIdx) tdbTbClose(pMeta->pBtimeIdx);
11,647!
362
    if (pMeta->pSmaIdx) tdbTbClose(pMeta->pSmaIdx);
11,647!
363
    if (pMeta->pTtlMgr) ttlMgrClose(pMeta->pTtlMgr);
11,647!
364
    if (pMeta->pTagIvtIdx) indexClose(pMeta->pTagIvtIdx);
11,647!
365
    if (pMeta->pTagIdx) tdbTbClose(pMeta->pTagIdx);
11,647!
366
    if (pMeta->pCtbIdx) tdbTbClose(pMeta->pCtbIdx);
11,647!
367
    if (pMeta->pSuidIdx) tdbTbClose(pMeta->pSuidIdx);
11,647!
368
    if (pMeta->pNameIdx) tdbTbClose(pMeta->pNameIdx);
11,647!
369
    if (pMeta->pUidIdx) tdbTbClose(pMeta->pUidIdx);
11,647!
370
    if (pMeta->pSkmDb) tdbTbClose(pMeta->pSkmDb);
11,647!
371
    if (pMeta->pTbDb) tdbTbClose(pMeta->pTbDb);
11,647!
372
    if (pMeta->pEnv) tdbClose(pMeta->pEnv);
11,647!
373
    metaDestroyLock(pMeta);
11,647✔
374

375
    taosMemoryFreeClear(*ppMeta);
11,648!
376
  }
377
}
11,648✔
378

379
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
167,566,796✔
380
  STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
167,566,796✔
381
  STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;
167,566,796✔
382

383
  if (pTbDbKey1->version > pTbDbKey2->version) {
167,566,796✔
384
    return 1;
77,692,602✔
385
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
89,874,194✔
386
    return -1;
55,046,209✔
387
  }
388

389
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
34,827,985✔
390
    return 1;
495,842✔
391
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
34,332,143✔
392
    return -1;
374,249✔
393
  }
394

395
  return 0;
33,957,894✔
396
}
397

398
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
1,033,980✔
399
  SSkmDbKey *pSkmDbKey1 = (SSkmDbKey *)pKey1;
1,033,980✔
400
  SSkmDbKey *pSkmDbKey2 = (SSkmDbKey *)pKey2;
1,033,980✔
401

402
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
1,033,980✔
403
    return 1;
275,721✔
404
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
758,259✔
405
    return -1;
77,505✔
406
  }
407

408
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
680,754✔
409
    return 1;
60,546✔
410
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
620,208✔
411
    return -1;
168,726✔
412
  }
413

414
  return 0;
451,482✔
415
}
416

417
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
60,763,297✔
418
  tb_uid_t uid1 = *(tb_uid_t *)pKey1;
60,763,297✔
419
  tb_uid_t uid2 = *(tb_uid_t *)pKey2;
60,763,297✔
420

421
  if (uid1 > uid2) {
60,763,297✔
422
    return 1;
30,441,965✔
423
  } else if (uid1 < uid2) {
30,321,332✔
424
    return -1;
17,131,484✔
425
  }
426

427
  return 0;
13,189,848✔
428
}
429

430
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
9,856,476✔
431
  SCtbIdxKey *pCtbIdxKey1 = (SCtbIdxKey *)pKey1;
9,856,476✔
432
  SCtbIdxKey *pCtbIdxKey2 = (SCtbIdxKey *)pKey2;
9,856,476✔
433

434
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
9,856,476✔
435
    return 1;
3,745,886✔
436
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
6,110,590✔
437
    return -1;
1,225,962✔
438
  }
439

440
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
4,884,628✔
441
    return 1;
422,288✔
442
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
4,462,340✔
443
    return -1;
4,203,119✔
444
  }
445

446
  return 0;
259,221✔
447
}
448

449
int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
820,301✔
450
  STagIdxKey *pTagIdxKey1 = (STagIdxKey *)pKey1;
820,301✔
451
  STagIdxKey *pTagIdxKey2 = (STagIdxKey *)pKey2;
820,301✔
452
  tb_uid_t    uid1 = 0, uid2 = 0;
820,301✔
453
  int         c;
454

455
  // compare suid
456
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
820,301✔
457
    return 1;
51,811✔
458
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
768,490✔
459
    return -1;
6,075✔
460
  }
461

462
  // compare column id
463
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
762,415✔
464
    return 1;
566✔
465
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
761,849✔
466
    return -1;
286✔
467
  }
468

469
  if (pTagIdxKey1->type != pTagIdxKey2->type) {
761,563!
470
    metaError("meta/open: incorrect tag idx type.");
×
471
    return TSDB_CODE_FAILED;
×
472
  }
473

474
  // check NULL, NULL is always the smallest
475
  if (pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
761,563✔
476
    return -1;
270✔
477
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
761,293✔
478
    return 1;
866✔
479
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
760,427!
480
    // all not NULL, compr tag vals
481
    __compar_fn_t func = getComparFunc(pTagIdxKey1->type, 0);
760,138✔
482
    if (func == NULL) {
760,123!
483
      metaError("meta/open: %s", terrstr());
×
484
      return TSDB_CODE_FAILED;
×
485
    }
486
    c = func(pTagIdxKey1->data, pTagIdxKey2->data);
760,123✔
487
    if (c) return c;
760,140✔
488
  }
489

490
  // both null or tag values are equal, then continue to compare uids
491
  if (IS_VAR_DATA_TYPE(pTagIdxKey1->type)) {
106,316!
492
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + varDataTLen(pTagIdxKey1->data));
80,878✔
493
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + varDataTLen(pTagIdxKey2->data));
80,878✔
494
  } else {
495
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + tDataTypes[pTagIdxKey1->type].bytes);
25,438✔
496
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + tDataTypes[pTagIdxKey2->type].bytes);
25,438✔
497
  }
498

499
  // compare uid
500
  if (uid1 < uid2) {
106,316✔
501
    return -1;
839✔
502
  } else if (uid1 > uid2) {
105,477✔
503
    return 1;
96,233✔
504
  } else {
505
    return 0;
9,244✔
506
  }
507

508
  return 0;
509
}
510

511
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
453,340✔
512
  SBtimeIdxKey *pBtimeIdxKey1 = (SBtimeIdxKey *)pKey1;
453,340✔
513
  SBtimeIdxKey *pBtimeIdxKey2 = (SBtimeIdxKey *)pKey2;
453,340✔
514
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
453,340✔
515
    return 1;
359,183✔
516
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
94,157✔
517
    return -1;
11,092✔
518
  }
519

520
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
83,065✔
521
    return 1;
74,091✔
522
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
8,974✔
523
    return -1;
1,602✔
524
  }
525

526
  return 0;
7,372✔
527
}
528

529
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
530
  SNcolIdxKey *pNcolIdxKey1 = (SNcolIdxKey *)pKey1;
×
531
  SNcolIdxKey *pNcolIdxKey2 = (SNcolIdxKey *)pKey2;
×
532

533
  if (pNcolIdxKey1->ncol > pNcolIdxKey2->ncol) {
×
534
    return 1;
×
535
  } else if (pNcolIdxKey1->ncol < pNcolIdxKey2->ncol) {
×
536
    return -1;
×
537
  }
538

539
  if (pNcolIdxKey1->uid > pNcolIdxKey2->uid) {
×
540
    return 1;
×
541
  } else if (pNcolIdxKey1->uid < pNcolIdxKey2->uid) {
×
542
    return -1;
×
543
  }
544

545
  return 0;
×
546
}
547

548
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
549
  SSmaIdxKey *pSmaIdxKey1 = (SSmaIdxKey *)pKey1;
×
550
  SSmaIdxKey *pSmaIdxKey2 = (SSmaIdxKey *)pKey2;
×
551

552
  if (pSmaIdxKey1->uid > pSmaIdxKey2->uid) {
×
553
    return 1;
×
554
  } else if (pSmaIdxKey1->uid < pSmaIdxKey2->uid) {
×
555
    return -1;
×
556
  }
557

558
  if (pSmaIdxKey1->smaUid > pSmaIdxKey2->smaUid) {
×
559
    return 1;
×
560
  } else if (pSmaIdxKey1->smaUid < pSmaIdxKey2->smaUid) {
×
561
    return -1;
×
562
  }
563

564
  return 0;
×
565
}
566

567
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
568
  int32_t uid1 = *(int32_t *)pKey1;
×
569
  int32_t uid2 = *(int32_t *)pKey2;
×
570

571
  if (uid1 > uid2) {
×
572
    return 1;
×
573
  } else if (uid1 < uid2) {
×
574
    return -1;
×
575
  }
576

577
  return 0;
×
578
}
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