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

taosdata / TDengine / #4665

12 Aug 2025 07:34AM UTC coverage: 59.901% (-0.6%) from 60.536%
#4665

push

travis-ci

web-flow
Merge pull request #32547 from taosdata/refactor/wangxu/get-started-installer

refactor: get started for installer and docker

137370 of 291999 branches covered (47.04%)

Branch coverage included in aggregate %.

207872 of 284354 relevant lines covered (73.1%)

4846328.0 hits per line

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

79.94
/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) {
2,484,843✔
27
  *pInfo = NULL;
2,484,843✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
2,484,843!
30
  if (pLoadInfo == NULL) {
2,486,554!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
2,486,554✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
2,486,554✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
2,486,554✔
38

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

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

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
2,485,890✔
52
  if (pLoadInfo->aSttBlk == NULL) {
2,486,744✔
53
    taosMemoryFreeClear(pLoadInfo);
102!
54
    return terrno;
102✔
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
2,486,642✔
58
  pLoadInfo->colIds = colList;
2,486,642✔
59
  pLoadInfo->numOfCols = numOfCols;
2,486,642✔
60

61
  *pInfo = pLoadInfo;
2,486,642✔
62
  return code;
2,486,642✔
63
}
64

65
static void freeItem(void *pValue) {
30,928,797✔
66
  SValue *p = (SValue *)pValue;
30,928,797✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
30,928,797!
68
    taosMemoryFree(p->pData);
230,573!
69
  }
70
}
30,934,634✔
71

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

77
  pLoadInfo->currentLoadBlockIndex = 1;
2,488,701✔
78

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

84
  pInfo = &pLoadInfo->blockData[1];
2,488,186✔
85
  tBlockDataDestroy(&pInfo->data);
2,488,186✔
86
  pInfo->sttBlockIndex = -1;
2,488,070✔
87
  pInfo->pin = false;
2,488,070✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
2,488,070✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
2,487,816✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
2,488,020✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
2,488,162✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
2,488,103✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
2,488,167✔
95

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

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
2,488,162✔
104
  taosMemoryFree(pLoadInfo);
2,488,342!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
2,485,503✔
108
  tLDataIterClose2(pIter);
2,485,503✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
2,488,817✔
110
  taosMemoryFree(pIter);
2,488,567!
111
}
2,488,816✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
3,921,391✔
114
  if (pLDataIterArray == NULL) {
3,921,391✔
115
    return;
38,117✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
3,883,274✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
6,365,379✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
2,479,162✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
4,967,931✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
2,485,909✔
123
      if (pIter->pBlockLoadInfo != NULL) {
2,485,478!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
2,485,607✔
125
        if (pLoadCost != NULL) {
2,485,607✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
2,484,310✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
2,484,310✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
2,484,310✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
2,484,310✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
2,485,478✔
134
    }
135

136
    taosArrayDestroy(pList);
2,478,616✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
3,886,217✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
1,434,061✔
150
}
1,434,061✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
652,420,155✔
153
  if (pResBlock != NULL) {
652,420,155!
154
    *pResBlock = NULL;
652,715,902✔
155
  }
156

157
  int32_t            code = 0;
652,420,155✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
652,420,155✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
652,420,155✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
367,758,796✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
643✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
643✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
367,758,796✔
169
    return code;
367,758,796✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
284,661,359!
173
    if (pInfo->currentLoadBlockIndex != 1) {
312,778,584✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
687✔
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
687✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
312,778,584✔
181
    return code;
312,778,584✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
1,434,402✔
189
  int64_t st = taosGetTimestampUs();
1,435,019✔
190

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

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

202
  tsdbDebug("read stt block, total load:%" PRId64 ", trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
1,435,265✔
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;
1,435,112✔
210
  pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].data.nRow : -1;
1,435,112✔
211

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
1,435,144✔
216
  return code;
1,435,144✔
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;
1,930,535✔
223
  int32_t step = backward ? 1 : -1;
1,930,535✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
3,903,580!
225
    i += step;
1,973,045✔
226
  }
227
  return i - step;
1,930,535✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
3,876,589✔
231
  int32_t midPos = -1;
3,876,589✔
232
  if (num <= 0) {
3,876,589✔
233
    return -1;
1,253,872✔
234
  }
235

236
  int32_t firstPos = 0;
2,622,717✔
237
  int32_t lastPos = num - 1;
2,622,717✔
238

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

244
  while (1) {
271,604✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
2,202,272✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
1,821,319✔
247
    }
248

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

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

256
    if (uid < pBlockList[midPos].minUid) {
380,820✔
257
      lastPos = midPos - 1;
129,459✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
251,361✔
259
      firstPos = midPos + 1;
142,145✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
109,216✔
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;
1,922,488✔
269
  int32_t step = backward ? 1 : -1;
1,922,488✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
63,672,655!
271
    i += step;
61,750,167✔
272
  }
273
  return i - step;
1,922,488✔
274
}
275

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

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

285
  while (1) {
1,135,947✔
286
    if (uid == uidList[firstPos]) {
3,090,389✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
1,350,714✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
1,739,675✔
291
      return -1;
31,954✔
292
    }
293

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

297
    if (uid < uidList[midPos]) {
1,707,721✔
298
      lastPos = midPos - 1;
406,883✔
299
    } else if (uid > uidList[midPos]) {
1,300,838✔
300
      firstPos = midPos + 1;
729,064✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
571,774✔
303
    }
304
  }
305
}
306

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

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

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
2,481,969✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
2,087,412✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
710,392✔
322
      pIter->iSttBlk = -1;
710,065✔
323
      return TSDB_CODE_SUCCESS;
710,065✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,377,020✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,377,088✔
327
      if (px == NULL) {
1,376,360!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
394,557✔
333
    if (pTmp == NULL) {
396,952!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
1,468,460✔
338
      SSttBlk *p = &pArray->data[i];
1,233,048✔
339
      if (p->suid < suid) {
1,233,048✔
340
        continue;
753,470✔
341
      }
342

343
      if (p->suid == suid) {
479,578✔
344
        void *px = taosArrayPush(pTmp, p);
318,104✔
345
        if (px == NULL) {
318,104!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
161,465!
350
        break;
161,541✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
396,953✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
397,019✔
356
  }
357

358
  return code;
1,773,379✔
359
}
360

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

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

373
  return TSDB_CODE_SUCCESS;
588,441✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
2,484,403✔
385
  if (numOfBlocks <= 0) {
2,484,403✔
386
    return code;
2,405✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
2,808,201✔
390
    ++startIndex;
326,203✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
2,481,998✔
394
    return 0;
788,921✔
395
  }
396

397
  int32_t endIndex = startIndex;
1,693,077✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
3,394,030✔
399
    ++endIndex;
1,700,953✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
1,693,077✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
1,693,077✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
1,693,077✔
407
  QUERY_CHECK_CODE(code, lino, _end);
1,693,156!
408

409
  int64_t st = taosGetTimestampUs();
1,693,213✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
3,381,195✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
1,699,585✔
413
    QUERY_CHECK_CODE(code, lino, _end);
1,700,600!
414

415
    int32_t i = 0;
1,700,600✔
416
    int32_t rows = block.numOfRecords;
1,700,600✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
2,291,476✔
418
      ++i;
590,876✔
419
    }
420

421
    // existed
422
    if (i < rows) {
1,700,600✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
1,657,073✔
424

425
      if (pInfo->pUid == NULL) {
1,657,073✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
1,649,282✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
1,649,734✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
1,649,792✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
1,649,825✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
1,649,817✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
1,649,749✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
1,649,776!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
1,649,792!
436
          code = terrno;
94✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
1,657,473✔
442
        int32_t size = rows - i;
1,580,904✔
443
        int32_t offset = i * sizeof(int64_t);
1,580,904✔
444

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

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

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

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
1,580,480✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,580,147✔
456

457
        if (block.numOfPKs > 0) {
1,579,929✔
458
          SValue vFirst = {0}, vLast = {0};
142,702✔
459
          for (int32_t f = i; f < rows; ++f) {
402,682✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
259,873✔
461
            TSDB_CHECK_CODE(code, lino, _end);
259,958!
462

463
            code = tValueDupPayload(&vFirst);
259,958✔
464
            TSDB_CHECK_CODE(code, lino, _end);
259,975!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
259,975✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
259,992!
468

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

473
            code = tValueDupPayload(&vLast);
259,998✔
474
            TSDB_CHECK_CODE(code, lino, _end);
259,969!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
259,969✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
259,980!
478
          }
479
        } else {
480
          SValue vFirst = {0};
1,437,227✔
481
          for (int32_t j = 0; j < size; ++j) {
15,990,259✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
14,565,191✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
14,546,164!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
14,546,164✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
14,553,032!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
76,569✔
491
        while (i < rows) {
219,672✔
492
          code = tStatisBlockGet(&block, i, &record);
219,661✔
493
          TSDB_CHECK_CODE(code, lino, _end);
219,609!
494

495
          if (record.suid != suid) {
219,609✔
496
            break;
76,567✔
497
          }
498

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

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

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

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

511
          if (record.firstKey.numOfPKs > 0) {
143,086✔
512
            SValue s = record.firstKey.pks[0];
34,418✔
513
            code = tValueDupPayload(&s);
34,418✔
514
            TSDB_CHECK_CODE(code, lino, _end);
34,424!
515

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

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
34,424✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
34,425!
525
          } else {
526
            SValue v = {0};
108,668✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
108,668✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
108,658!
529

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

534
          i += 1;
143,103✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
1,681,610✔
541
  tStatisBlockDestroy(&block);
1,681,610✔
542
  if (code != 0) {
1,693,167!
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;
1,692,786✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
1,692,786✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
2,486,044✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
2,485,501✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
2,485,501✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
2,485,501✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
2,485,501✔
562
  if (code != TSDB_CODE_SUCCESS) {
2,485,997!
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);
2,485,997✔
569
  if (code != TSDB_CODE_SUCCESS) {
2,484,885!
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;
2,484,885✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
2,484,885✔
577
  if (code != TSDB_CODE_SUCCESS) {
2,485,987!
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);
2,485,987✔
584
  if (code != TSDB_CODE_SUCCESS) {
2,486,565!
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);
2,486,565✔
590

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

596
static int32_t uidComparFn(const void *p1, const void *p2) {
4,085,488✔
597
  const uint64_t *pFirst = p1;
4,085,488✔
598
  const uint64_t *pVal = p2;
4,085,488✔
599

600
  if (*pFirst == *pVal) {
4,085,488✔
601
    return 0;
1,902,287✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
2,183,201✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
3,880,057✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
3,880,057!
610
    return;
1,254,500✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
2,624,772✔
614
  if (index >= 0) {
2,625,476✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
1,902,278✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
1,901,903✔
617

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

620
    if (pRange->skey.numOfPKs > 0) {
1,901,724✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
260,837✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
260,799✔
623
    }
624
  }
625
}
626

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
3,880,826✔
641
  pIter->pReader = pSttFileReader;
3,880,826✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
3,880,826✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
3,880,826!
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) {
3,880,826✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
2,486,908✔
654
    if (code != TSDB_CODE_SUCCESS) {
2,486,181!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
3,880,099✔
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);
3,878,446✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
3,878,478✔
665
  if (pIter->iSttBlk != -1) {
3,877,397✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
1,933,908✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
1,933,856✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
1,933,856!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
1,801,575!
671
      pIter->pSttBlk = NULL;
910✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
1,933,856!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
131,981!
676
      pIter->pSttBlk = NULL;
363✔
677
      pIter->ignoreEarlierTs = true;
363✔
678
    }
679
  }
680

681
  return code;
3,877,345✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
2,485,344✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
2,485,344✔
686
  pIter->pReader = NULL;
2,488,821✔
687
}
2,488,821✔
688

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

693
  pIter->iSttBlk += step;
1,980,325✔
694

695
  int32_t index = -1;
1,980,325✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
1,980,325✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
1,982,544!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
238,558✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
238,549✔
700
      break;
138,655✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
99,894✔
704
      break;
11,152✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
1,980,316✔
736
  if (index != -1) {
1,980,316✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
85,802✔
738

739
    pIter->iSttBlk = index;
85,803✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
85,803✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
85,802✔
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);
1,894,514✔
746
  }
747
}
1,980,319✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
335,971,288✔
756
  if (code) {
334,593,966!
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) {
334,593,966✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
1,954,902✔
764
    if (i == -1) {
1,955,902!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
32,002✔
767
      return code;
32,002✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
348,652,923!
772
    if (pData->aUid != NULL) {
348,566,962✔
773
      if (!pIter->backward) {
346,512,092✔
774
        if (pData->aUid[i] > pIter->uid) {
337,200,523✔
775
          break;
531,201✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
9,311,569✔
779
          break;
72,722✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
347,963,039✔
785
    if (!pIter->backward) {               // asc
347,963,039✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
338,348,383✔
787
        break;
6,307✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
338,342,076✔
790
          continue;
9,286,125✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
329,055,951✔
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) {
9,614,656✔
804
        break;
3,040✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
9,611,616✔
807
          continue;
4,197,940✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
5,413,676!
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];
334,469,627✔
822
    if (ver < pIter->verRange.minVer) {
334,469,627!
823
      continue;
×
824
    }
825

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

831
    hasVal = true;
334,469,627✔
832
    break;
334,469,627✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
335,168,858✔
836
  return code;
335,168,858✔
837
}
838

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

846
  *hasNext = false;
336,498,070✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
336,498,070✔
850
    return code;
1,946,659✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
334,551,411✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
334,868,043!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
335,001,837✔
860

861
  while (1) {
84,765✔
862
    bool skipBlock = false;
335,086,602✔
863
    code = findNextValidRow(pIter, idStr);
335,086,602✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
336,056,646!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
336,056,646!
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) {
336,056,646✔
890
      tLDataIterNextBlock(pIter, idStr);
2,038,881✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
1,979,584✔
892
        goto _exit;
1,894,822✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
84,762!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
85,802✔
900
      if ((pBlockData == NULL) || (code != 0)) {
85,804!
901
        lino = __LINE__;
×
902
        goto _exit;
×
903
      }
904

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

910
  pIter->rInfo.suid = pBlockData->suid;
334,017,765✔
911
  pIter->rInfo.uid = pBlockData->uid;
334,017,765✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
334,017,765✔
913

914
_exit:
335,778,792✔
915
  if (code) {
335,778,792!
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);
335,942,029!
920
  return code;
335,942,029✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
62,812,193✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
63,139,707✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
63,139,707✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
63,139,707✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
63,139,707!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
63,600,478!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
63,441,253✔
933
  if (ret < 0) {
63,411,610✔
934
    return -1;
42,162,986✔
935
  } else if (ret > 0) {
21,248,624✔
936
    return 1;
8,490,958✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
12,757,666!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
12,757,666!
940

941
    if (ver1 < ver2) {
12,757,666✔
942
      return -1;
5,161,379✔
943
    } else if (ver1 > ver2) {
7,596,287!
944
      return 1;
8,067,457✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

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

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

963
  if (!pMTree->backward) {  // asc
4,226,443✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
3,781,770✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
444,673✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
4,227,191✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
4,227,191✔
973
  if (numOfLevels == 0) {
4,227,191✔
974
    goto _end;
543,144✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
3,684,047✔
978
  if (code) {
3,682,968!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
7,527,220✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
3,845,594✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
3,845,594✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
7,722,662✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
3,878,410✔
988

989
      SSttFileReader    *pSttFileReader = pIter->pReader;
3,877,987✔
990
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
3,877,987✔
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) {
3,877,987✔
995
        SSttFileReaderConfig conf = {.tsdb = pConf->pTsdb, .szPage = pConf->pTsdb->pVnode->config.tsdbPageSize};
2,486,144✔
996
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
2,486,144✔
997

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
2,486,144✔
999
        if (code != TSDB_CODE_SUCCESS) {
2,484,777!
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) {
3,876,177✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
2,485,462✔
1007
        if (code != TSDB_CODE_SUCCESS) {
2,486,590!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
3,877,305✔
1013

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

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

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

1030
      if (hasVal) {
3,877,456✔
1031
        tMergeTreeAddIter(pMTree, pIter);
1,892,522✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
1,891,736✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
1,984,934!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
1,985,719✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
3,681,626✔
1049

1050
_end:
543,144✔
1051
  tMergeTreeClose(pMTree);
543,144✔
1052
  return code;
543,069✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
1,892,209✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
1,892,209✔
1057
}
1,892,393✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
271,020,344✔
1065
    pInfo->blockData[0].pin = true;
151,990,266✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
151,990,266✔
1067
    return;
152,016,602✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
119,030,078!
1071
    pInfo->blockData[1].pin = true;
129,509,917✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
129,509,917✔
1073
    return;
129,509,582✔
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) {
271,231,242✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
271,231,242✔
1081
  if (pInfo->blockData[0].pin) {
271,231,242✔
1082
    pInfo->blockData[0].pin = false;
151,869,748✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
151,869,748✔
1084
    return;
151,865,805✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
119,361,494!
1088
    pInfo->blockData[1].pin = false;
129,518,448✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
129,518,448✔
1090
    return;
129,455,965✔
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) {
271,093,985✔
1097
  if (pMTree->pIter == NULL) {
271,093,985!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
271,093,985✔
1102
  pMTree->pPinnedBlockIter = pIter;
271,093,985✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
271,093,985✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
271,393,762✔
1112
  pMTree->pPinnedBlockIter = NULL;
271,393,762✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
271,393,762✔
1114
}
1115

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

1122
  *pHasNext = false;
336,601,527✔
1123
  while (pMTree->pIter) {
336,601,527✔
1124
    SLDataIter *pIter = pMTree->pIter;
332,134,815✔
1125
    bool        hasVal = false;
332,134,815✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
332,134,815✔
1127
    if (!hasVal || (code != 0)) {
333,722,018✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
1,854,344!
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;
1,854,344✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
333,722,018✔
1137
    if (pMTree->pIter && pIter) {
333,722,018✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
41,659,736✔
1139
      if (c > 0) {
41,804,789✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
4,081,878✔
1141
        pMTree->pIter = NULL;
5,002,517✔
1142
      } else if (!c) {
37,722,911!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
334,787,710✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
339,254,422✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
11,043,537✔
1152
    if (pMTree->pIter) {
11,043,537✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
6,859,980✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
338,520,848✔
1158
  return code;
338,520,848✔
1159
}
1160

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