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

taosdata / TDengine / #4808

16 Oct 2025 11:40AM UTC coverage: 57.938% (-0.6%) from 58.524%
#4808

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

137662 of 303532 branches covered (45.35%)

Branch coverage included in aggregate %.

209234 of 295200 relevant lines covered (70.88%)

4035326.15 hits per line

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

79.79
/source/dnode/vnode/src/tsdb/tsdbMergeTree.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 "tsdb.h"
17
#include "tsdbFSet2.h"
18
#include "tsdbMerge.h"
19
#include "tsdbReadUtil.h"
20
#include "tsdbSttFileRW.h"
21
#include "tsdbUtil2.h"
22

23
static void tLDataIterClose2(SLDataIter *pIter);
24

25
// SLDataIter =================================================
26
int32_t tCreateSttBlockLoadInfo(STSchema *pSchema, int16_t *colList, int32_t numOfCols, SSttBlockLoadInfo **pInfo) {
508,675✔
27
  *pInfo = NULL;
508,675✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
508,675!
30
  if (pLoadInfo == NULL) {
508,919!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
508,919✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
508,919✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
508,919✔
38

39
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
508,919✔
40
  if (code) {
508,844!
41
    taosMemoryFreeClear(pLoadInfo);
×
42
    return code;
×
43
  }
44

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
508,844✔
46
  if (code) {
508,861!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
508,861✔
52
  if (pLoadInfo->aSttBlk == NULL) {
508,975✔
53
    taosMemoryFreeClear(pLoadInfo);
20!
54
    return terrno;
20✔
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
508,955✔
58
  pLoadInfo->colIds = colList;
508,955✔
59
  pLoadInfo->numOfCols = numOfCols;
508,955✔
60

61
  *pInfo = pLoadInfo;
508,955✔
62
  return code;
508,955✔
63
}
64

65
static void freeItem(void *pValue) {
27,409,388✔
66
  SValue *p = (SValue *)pValue;
27,409,388✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
27,409,388!
68
    taosMemoryFree(p->pData);
14,412!
69
  }
70
}
27,410,296✔
71

72
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
509,055✔
73
  if (pLoadInfo == NULL) {
509,055!
74
    return;
×
75
  }
76

77
  pLoadInfo->currentLoadBlockIndex = 1;
509,055✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
509,055✔
80
  tBlockDataDestroy(&pInfo->data);
509,055✔
81
  pInfo->sttBlockIndex = -1;
509,048✔
82
  pInfo->pin = false;
509,048✔
83

84
  pInfo = &pLoadInfo->blockData[1];
509,048✔
85
  tBlockDataDestroy(&pInfo->data);
509,048✔
86
  pInfo->sttBlockIndex = -1;
509,043✔
87
  pInfo->pin = false;
509,043✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
509,043✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
509,033✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
509,039✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
509,039✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
509,037✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
509,040✔
95

96
  pLoadInfo->info.pUid = NULL;
509,052✔
97
  pLoadInfo->info.pFirstKey = NULL;
509,052✔
98
  pLoadInfo->info.pLastKey = NULL;
509,052✔
99
  pLoadInfo->info.pCount = NULL;
509,052✔
100
  pLoadInfo->info.pFirstTs = NULL;
509,052✔
101
  pLoadInfo->info.pLastTs = NULL;
509,052✔
102

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
509,052✔
104
  taosMemoryFree(pLoadInfo);
509,057!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
508,860✔
108
  tLDataIterClose2(pIter);
508,860✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
509,075✔
110
  taosMemoryFree(pIter);
509,064!
111
}
509,079✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
955,380✔
114
  if (pLDataIterArray == NULL) {
955,380✔
115
    return;
12,876✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
942,504✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
1,445,349✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
502,597✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
1,011,632✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
508,891✔
123
      if (pIter->pBlockLoadInfo != NULL) {
508,853!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
508,857✔
125
        if (pLoadCost != NULL) {
508,857✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
508,855✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
508,855✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
508,855✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
508,855✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
508,853✔
134
    }
135

136
    taosArrayDestroy(pList);
502,517✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
942,752✔
140
}
141

142
// choose the unpinned slot to load next data block
143
static void updateBlockLoadSlot(SSttBlockLoadInfo *pLoadInfo) {
326,859✔
144
  int32_t nextSlotIndex = pLoadInfo->currentLoadBlockIndex ^ 1;
326,859✔
145
  if (pLoadInfo->blockData[nextSlotIndex].pin) {
326,859!
146
    nextSlotIndex = nextSlotIndex ^ 1;
×
147
  }
148

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
326,859✔
150
}
326,859✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
182,649,117✔
153
  if (pResBlock != NULL) {
182,649,117!
154
    *pResBlock = NULL;
182,696,732✔
155
  }
156

157
  int32_t            code = 0;
182,649,117✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
182,649,117✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
182,649,117✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
103,370,237✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
246!
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
246✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
103,370,237✔
169
    return code;
103,370,237✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
79,278,880!
173
    if (pInfo->currentLoadBlockIndex != 1) {
85,370,992✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
231!
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
231✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
85,370,992✔
181
    return code;
85,370,992✔
182
  }
183

184
  if (pIter->pSttBlk == NULL || pInfo->pSchema == NULL) {
×
185
    return code;
×
186
  }
187

188
  updateBlockLoadSlot(pInfo);
326,919✔
189
  int64_t st = taosGetTimestampUs();
327,061✔
190

191
  SBlockData *pBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
327,061✔
192
  code = tsdbSttFileReadBlockDataByColumn(pIter->pReader, pIter->pSttBlk, pBlock, pInfo->pSchema, &pInfo->colIds[1],
327,061✔
193
                                          pInfo->numOfCols - 1);
327,061✔
194
  if (code != TSDB_CODE_SUCCESS) {
327,124!
195
    return code;
×
196
  }
197

198
  double el = (taosGetTimestampUs() - st) / 1000.0;
327,133✔
199
  pInfo->cost.blockElapsedTime += el;
327,133✔
200
  pInfo->cost.loadBlocks += 1;
327,133✔
201

202
  tsdbDebug("read stt block, total load:%" PRId64 ", trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
327,133✔
203
            ", last block index:%d, entry:%d, rows:%d, uidRange:%" PRId64 "-%" PRId64 " tsRange:%" PRId64 "-%" PRId64
204
            " %p, elapsed time:%.2f ms, %s",
205
            pInfo->cost.loadBlocks, pIter->uid, pIter->cid, pIter->iSttBlk, pInfo->currentLoadBlockIndex, pBlock->nRow,
206
            pIter->pSttBlk->minUid, pIter->pSttBlk->maxUid, pIter->pSttBlk->minKey, pIter->pSttBlk->maxKey, pBlock, el,
207
            idStr);
208

209
  pInfo->blockData[pInfo->currentLoadBlockIndex].sttBlockIndex = pIter->iSttBlk;
327,135✔
210
  pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].data.nRow : -1;
327,135✔
211

212
  tsdbDebug("last block index list:%d, %d, rowIndex:%d %s", pInfo->blockData[0].sttBlockIndex,
327,135✔
213
            pInfo->blockData[1].sttBlockIndex, pIter->iRow, idStr);
214

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
327,140✔
216
  return code;
327,140✔
217
}
218

219
// find the earliest block that contains the required records
220
static FORCE_INLINE int32_t findEarliestIndex(int32_t index, uint64_t uid, const SSttBlk *pBlockList, int32_t num,
221
                                              int32_t backward) {
222
  int32_t i = index;
407,607✔
223
  int32_t step = backward ? 1 : -1;
407,607✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
824,589!
225
    i += step;
416,982✔
226
  }
227
  return i - step;
407,607✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
729,364✔
231
  int32_t midPos = -1;
729,364✔
232
  if (num <= 0) {
729,364✔
233
    return -1;
152,208✔
234
  }
235

236
  int32_t firstPos = 0;
577,156✔
237
  int32_t lastPos = num - 1;
577,156✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
577,156✔
241
    return -1;
169,457✔
242
  }
243

244
  while (1) {
80,991✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
488,690✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
374,660✔
247
    }
248

249
    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
114,030!
250
      return -1;
92✔
251
    }
252

253
    int32_t numOfRows = lastPos - firstPos + 1;
113,938✔
254
    midPos = (numOfRows >> 1u) + firstPos;
113,938✔
255

256
    if (uid < pBlockList[midPos].minUid) {
113,938✔
257
      lastPos = midPos - 1;
36,830✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
77,108✔
259
      firstPos = midPos + 1;
44,161✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
32,947✔
262
    }
263
  }
264
}
265

266
static FORCE_INLINE int32_t findEarliestRow(int32_t index, uint64_t uid, const uint64_t *uidList, int32_t num,
267
                                            int32_t backward) {
268
  int32_t i = index;
415,959✔
269
  int32_t step = backward ? 1 : -1;
415,959✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
16,352,192!
271
    i += step;
15,936,233✔
272
  }
273
  return i - step;
415,959✔
274
}
275

276
static int32_t binarySearchForStartRowIndex(uint64_t *uidList, int32_t num, uint64_t uid, int32_t backward) {
418,506✔
277
  int32_t firstPos = 0;
418,506✔
278
  int32_t lastPos = num - 1;
418,506✔
279

280
  // find the first position which is bigger than the key
281
  if ((uid > uidList[lastPos]) || (uid < uidList[firstPos])) {
418,506!
282
    return -1;
×
283
  }
284

285
  while (1) {
261,260✔
286
    if (uid == uidList[firstPos]) {
679,766✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
285,790✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
393,976✔
291
      return -1;
2,547✔
292
    }
293

294
    int32_t numOfRows = lastPos - firstPos + 1;
391,429✔
295
    int32_t midPos = (numOfRows >> 1u) + firstPos;
391,429✔
296

297
    if (uid < uidList[midPos]) {
391,429✔
298
      lastPos = midPos - 1;
98,275✔
299
    } else if (uid > uidList[midPos]) {
293,154✔
300
      firstPos = midPos + 1;
162,985✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
130,169✔
303
    }
304
  }
305
}
306

307
static int32_t extractSttBlockInfo(SLDataIter *pIter, const TSttBlkArray *pArray, SSttBlockLoadInfo *pBlockLoadInfo,
508,700✔
308
                                   uint64_t suid) {
309
  void   *px = NULL;
508,700✔
310
  int32_t code = TSDB_CODE_SUCCESS;
508,700✔
311
  if (TARRAY2_SIZE(pArray) <= 0) {
508,700✔
312
    return code;
2,148✔
313
  }
314

315
  SSttBlk *pStart = &pArray->data[0];
506,552✔
316
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
506,552✔
317

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
506,552✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
443,308✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
112,518✔
322
      pIter->iSttBlk = -1;
112,506✔
323
      return TSDB_CODE_SUCCESS;
112,506✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
330,790✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
330,800✔
327
      if (px == NULL) {
330,665!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
63,244✔
333
    if (pTmp == NULL) {
63,453!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
245,888✔
338
      SSttBlk *p = &pArray->data[i];
204,567✔
339
      if (p->suid < suid) {
204,567✔
340
        continue;
121,465✔
341
      }
342

343
      if (p->suid == suid) {
83,102✔
344
        void *px = taosArrayPush(pTmp, p);
60,968✔
345
        if (px == NULL) {
60,968!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
22,135!
350
        break;
22,135✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
63,456✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
63,463✔
356
  }
357

358
  return code;
394,128✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
47,097✔
362
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
47,097!
363
    char *p = (char *)pVal->pData;
15,314✔
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
15,314!
365
    if (pBuf == NULL) {
15,315!
366
      return terrno;
×
367
    }
368

369
    memcpy(pBuf, p, pVal->nData);
15,315✔
370
    pVal->pData = (uint8_t *)pBuf;
15,315✔
371
  }
372

373
  return TSDB_CODE_SUCCESS;
47,098✔
374
}
375

376
static int32_t loadSttStatisticsBlockData(SSttFileReader *pSttFileReader, SSttBlockLoadInfo *pBlockLoadInfo,
508,648✔
377
                                          TStatisBlkArray *pStatisBlkArray, uint64_t suid, const char *id) {
378
  int32_t code = TSDB_CODE_SUCCESS;
508,648✔
379
  int32_t lino = 0;
508,648✔
380
  void   *px = NULL;
508,648✔
381
  int32_t startIndex = 0;
508,648✔
382
  int32_t ret = 0;
508,648✔
383

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
508,648✔
385
  if (numOfBlocks <= 0) {
508,648✔
386
    return code;
2,148✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
541,487✔
390
    ++startIndex;
34,987✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
506,500✔
394
    return 0;
123,759✔
395
  }
396

397
  int32_t endIndex = startIndex;
382,741✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
769,892✔
399
    ++endIndex;
387,151✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
382,741✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
382,741✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
382,741✔
407
  QUERY_CHECK_CODE(code, lino, _end);
382,744!
408

409
  int64_t st = taosGetTimestampUs();
382,731✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
748,451✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
386,883✔
413
    QUERY_CHECK_CODE(code, lino, _end);
387,096!
414

415
    int32_t i = 0;
387,096✔
416
    int32_t rows = block.numOfRecords;
387,096✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
479,144✔
418
      ++i;
92,048✔
419
    }
420

421
    // existed
422
    if (i < rows) {
387,096✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
385,438✔
424

425
      if (pInfo->pUid == NULL) {
385,438✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
381,016✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
381,071✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
381,086✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
381,082✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
381,076✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
381,086✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
381,070!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
381,078!
436
          code = terrno;
32✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
385,460✔
442
        int32_t size = rows - i;
369,293✔
443
        int32_t offset = i * sizeof(int64_t);
369,293✔
444

445
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
369,293✔
446
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
369,272!
447

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
369,272✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
369,240!
450

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
369,240✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
369,179!
453

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
369,179✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
369,204✔
456

457
        if (block.numOfPKs > 0) {
369,170✔
458
          SValue vFirst = {0}, vLast = {0};
13,524✔
459
          for (int32_t f = i; f < rows; ++f) {
31,074✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
17,540✔
461
            TSDB_CHECK_CODE(code, lino, _end);
17,544!
462

463
            code = tValueDupPayload(&vFirst);
17,544✔
464
            TSDB_CHECK_CODE(code, lino, _end);
17,546!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
17,546✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
17,547!
468

469
            // todo add api to clone the original data
470
            code = tValueColumnGet(&block.lastKeyPKs[0], f, &vLast);
17,547✔
471
            TSDB_CHECK_CODE(code, lino, _end);
17,550!
472

473
            code = tValueDupPayload(&vLast);
17,550✔
474
            TSDB_CHECK_CODE(code, lino, _end);
17,547!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
17,547✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
17,550!
478
          }
479
        } else {
480
          SValue vFirst = {0};
355,646✔
481
          for (int32_t j = 0; j < size; ++j) {
13,554,511✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
13,220,153✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
13,204,226!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
13,204,226✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
13,198,865!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
16,167✔
491
        while (i < rows) {
45,690!
492
          code = tStatisBlockGet(&block, i, &record);
45,691✔
493
          TSDB_CHECK_CODE(code, lino, _end);
45,683!
494

495
          if (record.suid != suid) {
45,683✔
496
            break;
16,171✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
29,512✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
29,522!
501

502
          px = taosArrayPush(pInfo->pCount, &record.count);
29,522✔
503
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
29,523!
504

505
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
29,523✔
506
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
29,519!
507

508
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
29,519✔
509
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
29,521!
510

511
          if (record.firstKey.numOfPKs > 0) {
29,521✔
512
            SValue s = record.firstKey.pks[0];
6,006✔
513
            code = tValueDupPayload(&s);
6,006✔
514
            TSDB_CHECK_CODE(code, lino, _end);
6,006!
515

516
            px = taosArrayPush(pInfo->pFirstKey, &s);
6,006✔
517
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
6,006!
518

519
            s = record.lastKey.pks[0];
6,006✔
520
            code = tValueDupPayload(&s);
6,006✔
521
            TSDB_CHECK_CODE(code, lino, _end);
6,006!
522

523
            px = taosArrayPush(pInfo->pLastKey, &s);
6,006✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
6,006!
525
          } else {
526
            SValue v = {0};
23,515✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
23,515✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
23,513!
529

530
            px = taosArrayPush(pInfo->pLastKey, &v);
23,513✔
531
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
23,517!
532
          }
533

534
          i += 1;
29,523✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
361,568✔
541
  tStatisBlockDestroy(&block);
361,568✔
542
  if (code != 0) {
382,669!
543
    tsdbError("%s error happens at:%s line number: %d, code:%s", id, __func__, lino, tstrerror(code));
×
544
  } else {
545
    double el = (taosGetTimestampUs() - st) / 1000.0;
382,682✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
382,682✔
547

548
    tsdbDebug("%s load %d statis blocks into buf, elapsed time:%.2fms", id, num, el);
382,682✔
549
  }
550
  return code;
382,688✔
551
}
552

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
508,833✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
508,938✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
508,938✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
508,938✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
508,938✔
562
  if (code != TSDB_CODE_SUCCESS) {
508,912!
563
    tsdbError("load stt blk failed, code:%s, %s", tstrerror(code), idStr);
×
564
    return code;
×
565
  }
566

567
  // load the stt block info for each stt file block
568
  code = extractSttBlockInfo(pIter, pSttBlkArray, pBlockLoadInfo, suid);
508,912✔
569
  if (code != TSDB_CODE_SUCCESS) {
508,768!
570
    tsdbError("load stt block info failed, code:%s, %s", tstrerror(code), idStr);
×
571
    return code;
×
572
  }
573

574
  // load stt statistics block for all stt-blocks, to decide if the data of queried table exists in current stt file
575
  TStatisBlkArray *pStatisBlkArray = NULL;
508,768✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
508,768✔
577
  if (code != TSDB_CODE_SUCCESS) {
508,846!
578
    tsdbError("failed to load stt block statistics, code:%s, %s", tstrerror(code), idStr);
×
579
    return code;
×
580
  }
581

582
  // load statistics block for all tables in current stt file
583
  code = loadSttStatisticsBlockData(pIter->pReader, pIter->pBlockLoadInfo, pStatisBlkArray, suid, idStr);
508,846✔
584
  if (code != TSDB_CODE_SUCCESS) {
508,829!
585
    tsdbError("failed to load stt statistics block data, code:%s, %s", tstrerror(code), idStr);
×
586
    return code;
×
587
  }
588

589
  code = loadTombFn(pReader1, pIter->pReader, pIter->pBlockLoadInfo);
508,829✔
590

591
  double el = (taosGetTimestampUs() - st) / 1000.0;
508,798✔
592
  tsdbDebug("load the stt file blk info completed, elapsed time:%.2fms, %s", el, idStr);
508,798✔
593
  return code;
508,800✔
594
}
595

596
static int32_t uidComparFn(const void *p1, const void *p2) {
1,030,414✔
597
  const uint64_t *pFirst = p1;
1,030,414✔
598
  const uint64_t *pVal = p2;
1,030,414✔
599

600
  if (*pFirst == *pVal) {
1,030,414✔
601
    return 0;
405,473✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
624,941✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
729,849✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
729,849!
610
    return;
152,248✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
577,564✔
614
  if (index >= 0) {
577,539✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
405,479✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
405,431✔
617

618
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
405,420✔
619

620
    if (pRange->skey.numOfPKs > 0) {
405,426✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
12,349✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
12,349✔
623
    }
624
  }
625
}
626

627
int32_t tLDataIterOpen2(SLDataIter *pIter, SSttFileReader *pSttFileReader, int32_t cid, int8_t backward,
730,035✔
628
                        SMergeTreeConf *pConf, SSttBlockLoadInfo *pBlockLoadInfo, SSttKeyRange *pKeyRange,
629
                        int64_t *numOfRows, const char *idStr) {
630
  int32_t code = TSDB_CODE_SUCCESS;
730,035✔
631

632
  pIter->uid = pConf->uid;
730,035✔
633
  pIter->cid = cid;
730,035✔
634
  pIter->backward = backward;
730,035✔
635
  pIter->verRange.minVer = pConf->verRange.minVer;
730,035✔
636
  pIter->verRange.maxVer = pConf->verRange.maxVer;
730,035✔
637
  pIter->timeWindow.skey = pConf->timewindow.skey;
730,035✔
638
  pIter->timeWindow.ekey = pConf->timewindow.ekey;
730,035✔
639

640
  pIter->pStartRowKey = pConf->pCurRowKey;
730,035✔
641
  pIter->pReader = pSttFileReader;
730,035✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
730,035✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
730,035!
646
    tsdbError("stt file reader is null, %s", idStr);
×
647
    pIter->pSttBlk = NULL;
×
648
    pIter->iSttBlk = -1;
×
649
    return TSDB_CODE_SUCCESS;
×
650
  }
651

652
  if (!pBlockLoadInfo->sttBlockLoaded) {
730,035✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
508,950✔
654
    if (code != TSDB_CODE_SUCCESS) {
508,759!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
729,844✔
660

661
  // find the start block, actually we could load the position to avoid repeatly searching for the start position when
662
  // the skey is updated.
663
  size_t size = taosArrayGetSize(pBlockLoadInfo->aSttBlk);
729,669✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
729,533✔
665
  if (pIter->iSttBlk != -1) {
729,340✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
407,975✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
407,967✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
407,967!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
392,812✔
671
      pIter->pSttBlk = NULL;
195✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
407,967!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
15,154!
676
      pIter->pSttBlk = NULL;
313✔
677
      pIter->ignoreEarlierTs = true;
313✔
678
    }
679
  }
680

681
  return code;
729,332✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
508,850✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
508,850✔
686
  pIter->pReader = NULL;
509,077✔
687
}
509,077✔
688

689
void tLDataIterNextBlock(SLDataIter *pIter, const char *idStr) {
423,459✔
690
  int32_t step = pIter->backward ? -1 : 1;
423,459✔
691
  int32_t oldIndex = pIter->iSttBlk;
423,459✔
692

693
  pIter->iSttBlk += step;
423,459✔
694

695
  int32_t index = -1;
423,459✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
423,459✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
424,593!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
78,673✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
78,666✔
700
      break;
50,904✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
27,762✔
704
      break;
2,873✔
705
    }
706

707
    // check uid firstly
708
    if (p->minUid <= pIter->uid && p->maxUid >= pIter->uid) {
24,889!
709
      if ((!pIter->backward) && p->minKey > pIter->timeWindow.ekey) {
24,894✔
710
        break;
235✔
711
      }
712

713
      if (pIter->backward && p->maxKey < pIter->timeWindow.skey) {
24,659✔
714
        break;
2✔
715
      }
716

717
      // check time range secondly
718
      if (p->minKey <= pIter->timeWindow.ekey && p->maxKey >= pIter->timeWindow.skey) {
24,657✔
719
        if ((!pIter->backward) && p->minVer > pIter->verRange.maxVer) {
23,519!
720
          break;
×
721
        }
722

723
        if (pIter->backward && p->maxVer < pIter->verRange.minVer) {
23,519!
724
          break;
×
725
        }
726

727
        if (p->minVer <= pIter->verRange.maxVer && p->maxVer >= pIter->verRange.minVer) {
23,519!
728
          index = i;
23,518✔
729
          break;
23,518✔
730
        }
731
      }
732
    }
733
  }
734

735
  pIter->pSttBlk = NULL;
423,452✔
736
  if (index != -1) {
423,452✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
23,517✔
738

739
    pIter->iSttBlk = index;
23,517✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
23,517✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
23,518✔
742
              ", uidRange:%" PRId64 "-%" PRId64 " %s",
743
              pIter->iSttBlk, oldIndex, pIter->uid, pIter->cid, p->minUid, p->maxUid, idStr);
744
  } else {
745
    tsdbDebug("no more last block qualified, uid:%" PRIu64 ", stt-file block:%d, %s", pIter->uid, oldIndex, idStr);
399,935✔
746
  }
747
}
423,454✔
748

749
static int32_t findNextValidRow(SLDataIter *pIter, const char *idStr) {
93,981,871✔
750
  bool        hasVal = false;
93,981,871✔
751
  int32_t     step = pIter->backward ? -1 : 1;
93,981,871✔
752
  int32_t     i = pIter->iRow;
93,981,871✔
753
  SBlockData *pData = NULL;
93,981,871✔
754

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
93,981,871✔
756
  if (code) {
93,035,767!
757
    tsdbError("failed to load stt block, code:%s, %s", tstrerror(code), idStr);
×
758
    return code;
×
759
  }
760

761
  // mostly we only need to find the start position for a given table
762
  if ((((i == 0) && (!pIter->backward)) || (i == pData->nRow - 1 && pIter->backward)) && pData->aUid != NULL) {
93,035,767✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
418,557✔
764
    if (i == -1) {
418,624!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
2,553✔
767
      return code;
2,553✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
99,099,420!
772
    if (pData->aUid != NULL) {
99,189,543✔
773
      if (!pIter->backward) {
97,499,477✔
774
        if (pData->aUid[i] > pIter->uid) {
94,791,243✔
775
          break;
119,051✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
2,708,234✔
779
          break;
7,351✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
99,063,141✔
785
    if (!pIter->backward) {               // asc
99,063,141✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
96,264,011✔
787
        break;
2,538✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
96,261,473✔
790
          continue;
4,720,011✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
91,541,462✔
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
252!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
252✔
797
          if (ret < 0) {
252!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
2,799,130✔
804
        break;
2,049✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
2,797,081✔
807
          continue;
1,252,912✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
1,544,169!
811
          SRowKey key;
812
          tColRowGetKey(pData, i, &key);
×
813
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
×
814
          if (ret > 0) {
×
815
            continue;
×
816
          }
817
        }
818
      }
819
    }
820

821
    int64_t ver = pData->aVersion[i];
93,085,631✔
822
    if (ver < pIter->verRange.minVer) {
93,085,631!
823
      continue;
×
824
    }
825

826
    // todo opt handle desc case
827
    if (ver > pIter->verRange.maxVer) {
93,085,631!
828
      continue;
×
829
    }
830

831
    hasVal = true;
93,085,631✔
832
    break;
93,085,631✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
93,126,497✔
836
  return code;
93,126,497✔
837
}
838

839
int32_t tLDataIterNextRow(SLDataIter *pIter, const char *idStr, bool *hasNext) {
94,267,287✔
840
  int32_t     step = pIter->backward ? -1 : 1;
94,267,287✔
841
  int32_t     code = 0;
94,267,287✔
842
  int32_t     iBlockL = pIter->iSttBlk;
94,267,287✔
843
  SBlockData *pBlockData = NULL;
94,267,287✔
844
  int32_t     lino = 0;
94,267,287✔
845

846
  *hasNext = false;
94,267,287✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
94,267,287✔
850
    return code;
322,244✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
93,945,043✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
93,918,644!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
93,970,832✔
860

861
  while (1) {
23,326✔
862
    bool skipBlock = false;
93,994,158✔
863
    code = findNextValidRow(pIter, idStr);
93,994,158✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
93,282,876!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
93,282,876!
867
      skipBlock = true;
×
868
      int16_t *aCols = pIter->pBlockLoadInfo->colIds;
×
869
      int      nCols = pIter->pBlockLoadInfo->numOfCols;
×
870
      bool     isLast = pIter->pBlockLoadInfo->isLast;
×
871
      for (int inputColIndex = 0; inputColIndex < nCols; ++inputColIndex) {
×
872
        for (int colIndex = 0; colIndex < pBlockData->nColData; ++colIndex) {
×
873
          SColData *pColData = &pBlockData->aColData[colIndex];
×
874
          int16_t   cid = pColData->cid;
×
875

876
          if (cid == aCols[inputColIndex]) {
×
877
            if (isLast && (pColData->flag & HAS_VALUE)) {
×
878
              skipBlock = false;
×
879
              break;
×
880
            } else if (pColData->flag & (HAS_VALUE | HAS_NULL)) {
×
881
              skipBlock = false;
×
882
              break;
×
883
            }
884
          }
885
        }
886
      }
887
    }
888

889
    if (skipBlock || pIter->iRow >= pBlockData->nRow || pIter->iRow < 0) {
93,282,876!
890
      tLDataIterNextBlock(pIter, idStr);
394,591✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
423,327✔
892
        goto _exit;
400,002✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
23,325!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
23,517✔
900
      if ((pBlockData == NULL) || (code != 0)) {
23,516!
901
        lino = __LINE__;
×
902
        goto _exit;
×
903
      }
904

905
      // set start row index
906
      pIter->iRow = pIter->backward ? pBlockData->nRow - 1 : 0;
23,518✔
907
    }
908
  }
909

910
  pIter->rInfo.suid = pBlockData->suid;
92,888,285✔
911
  pIter->rInfo.uid = pBlockData->uid;
92,888,285✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
92,888,285✔
913

914
_exit:
93,236,097✔
915
  if (code) {
93,236,097!
916
    tsdbError("failed to exec stt-file nextIter, lino:%d, code:%s, %s", lino, tstrerror(code), idStr);
×
917
  }
918

919
  *hasNext = (code == TSDB_CODE_SUCCESS) && (pIter->pSttBlk != NULL) && (pBlockData != NULL);
93,253,957!
920
  return code;
93,253,957✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
13,348,245✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
13,414,594✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
13,414,594✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
13,414,594✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
13,414,594!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
13,449,674!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
13,348,051✔
933
  if (ret < 0) {
13,345,670✔
934
    return -1;
8,376,443✔
935
  } else if (ret > 0) {
4,969,227✔
936
    return 1;
2,045,273✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
2,923,954!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
2,923,954!
940

941
    if (ver1 < ver2) {
2,923,954✔
942
      return -1;
1,178,222✔
943
    } else if (ver1 > ver2) {
1,745,732!
944
      return 1;
1,858,890✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

951
static FORCE_INLINE int32_t tLDataIterDescCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
66,349!
952
  return -1 * tLDataIterCmprFn(p1, p2);
66,349✔
953
}
954

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
813,766✔
956
  int32_t code = TSDB_CODE_SUCCESS;
813,766✔
957

958
  pMTree->pIter = NULL;
813,766✔
959
  pMTree->backward = pConf->backward;
813,766✔
960
  pMTree->idStr = pConf->idstr;
813,766✔
961
  int32_t lino = 0;
813,766✔
962

963
  if (!pMTree->backward) {  // asc
813,766✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
790,731✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
23,035✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
813,835✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
813,835✔
973
  if (numOfLevels == 0) {
813,835✔
974
    goto _end;
146,583✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
667,252✔
978
  if (code) {
667,148!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
1,387,873✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
721,030✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
721,030✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
1,450,564✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
729,839✔
988

989
      SSttFileReader    *pSttFileReader = pIter->pReader;
729,636✔
990
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
729,636✔
991

992
      // open stt file reader if not opened yet
993
      // if failed to open this stt file, ignore the error and try next one
994
      if (pSttFileReader == NULL) {
729,636✔
995
        SSttFileReaderConfig conf = {.tsdb = pConf->pTsdb, .szPage = pConf->pTsdb->pVnode->config.tsdbPageSize};
508,820✔
996
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
508,820✔
997

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
508,820✔
999
        if (code != TSDB_CODE_SUCCESS) {
508,787!
1000
          tsdbError("open stt file reader error. file name %s, code %s, %s", pSttLevel->fobjArr->data[i]->fname,
×
1001
                    tstrerror(code), pMTree->idStr);
1002
        }
1003
      }
1004

1005
      if (pLoadInfo == NULL) {
729,637✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
508,906✔
1007
        if (code != TSDB_CODE_SUCCESS) {
508,910!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
729,641✔
1013

1014
      SSttKeyRange range = {.skey.numOfPKs = pConf->pCurRowKey->numOfPKs, .ekey.numOfPKs = pConf->pCurRowKey->numOfPKs};
729,641✔
1015
      int64_t      numOfRows = 0;
729,641✔
1016
      int64_t      cid = pSttLevel->fobjArr->data[i]->f->cid;
729,641✔
1017

1018
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
729,641✔
1019
                             pMTree->idStr);
1020
      if (code != TSDB_CODE_SUCCESS) {
729,392!
1021
        goto _end;
×
1022
      }
1023

1024
      bool hasVal = NULL;
729,392✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
729,392✔
1026
      if (code) {
729,609!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
729,609✔
1031
        tMergeTreeAddIter(pMTree, pIter);
401,647✔
1032

1033
        // let's record the time window for current table of uid in the stt files
1034
        if (pSttDataInfo != NULL && numOfRows > 0) {
401,601!
1035
          void *px = taosArrayPush(pSttDataInfo->pKeyRangeList, &range);
401,636✔
1036
          QUERY_CHECK_NULL(px, code, lino, _end, terrno);
401,563!
1037

1038
          pSttDataInfo->numOfRows += numOfRows;
401,563✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
327,962✔
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
327,834✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
666,843✔
1049

1050
_end:
146,583✔
1051
  tMergeTreeClose(pMTree);
146,583✔
1052
  return code;
146,560✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
401,550✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
401,550✔
1057
}
401,606✔
1058

1059
bool tMergeTreeIgnoreEarlierTs(SMergeTree *pMTree) { return pMTree->ignoreEarlierTs; }
×
1060

1061
static void tLDataIterPinSttBlock(SLDataIter *pIter, const char *id) {
74,431,647✔
1062
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
74,431,647✔
1063

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
74,431,647✔
1065
    pInfo->blockData[0].pin = true;
41,693,357✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
41,693,357✔
1067
    return;
41,694,710✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
32,738,290!
1071
    pInfo->blockData[1].pin = true;
34,673,266✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
34,673,266✔
1073
    return;
34,661,500✔
1074
  }
1075

1076
  tsdbError("failed to pin any stt block, sttBlock:%d stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
×
1077
}
1078

1079
static void tLDataIterUnpinSttBlock(SLDataIter *pIter, const char *id) {
73,623,971✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
73,623,971✔
1081
  if (pInfo->blockData[0].pin) {
73,623,971✔
1082
    pInfo->blockData[0].pin = false;
41,482,009✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
41,482,009✔
1084
    return;
41,486,575✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
32,141,962!
1088
    pInfo->blockData[1].pin = false;
34,388,392✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
34,388,392✔
1090
    return;
34,390,608✔
1091
  }
1092

1093
  tsdbError("failed to unpin any stt block, sttBlock:%d stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
×
1094
}
1095

1096
void tMergeTreePinSttBlock(SMergeTree *pMTree) {
74,436,798✔
1097
  if (pMTree->pIter == NULL) {
74,436,798!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
74,436,798✔
1102
  pMTree->pPinnedBlockIter = pIter;
74,436,798✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
74,436,798✔
1104
}
1105

1106
void tMergeTreeUnpinSttBlock(SMergeTree *pMTree) {
73,646,446✔
1107
  if (pMTree->pPinnedBlockIter == NULL) {
73,646,446!
1108
    return;
×
1109
  }
1110

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
73,646,446✔
1112
  pMTree->pPinnedBlockIter = NULL;
73,646,446✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
73,646,446✔
1114
}
1115

1116
int32_t tMergeTreeNext(SMergeTree *pMTree, bool *pHasNext) {
94,328,062✔
1117
  int32_t code = 0;
94,328,062✔
1118
  if (pHasNext == NULL) {
94,328,062!
1119
    return TSDB_CODE_INVALID_PARA;
×
1120
  }
1121

1122
  *pHasNext = false;
94,328,062✔
1123
  while (pMTree->pIter) {
94,328,062✔
1124
    SLDataIter *pIter = pMTree->pIter;
93,510,495✔
1125
    bool        hasVal = false;
93,510,495✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
93,510,495✔
1127
    if (!hasVal || (code != 0)) {
92,752,781✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
394,078!
1129
        code = 0;  // suppress the file corrupt error to enable all queries within this cluster can run without failed.
×
1130
      }
1131

1132
      pMTree->pIter = NULL;
394,078✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
92,752,781✔
1137
    if (pMTree->pIter && pIter) {
92,752,781✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
8,369,241✔
1139
      if (c > 0) {
8,261,397✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
1,189,983✔
1141
        pMTree->pIter = NULL;
1,166,286✔
1142
      } else if (!c) {
7,071,414!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
92,621,240✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
93,438,807✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
2,366,816✔
1152
    if (pMTree->pIter) {
2,366,816✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
1,561,299✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
93,418,794✔
1158
  return code;
93,418,794✔
1159
}
1160

1161
void tMergeTreeClose(SMergeTree *pMTree) {
1,806,913✔
1162
  pMTree->pIter = NULL;
1,806,913✔
1163
  pMTree->pPinnedBlockIter = NULL;
1,806,913✔
1164
}
1,806,913✔
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