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

taosdata / TDengine / #4766

28 Sep 2025 10:49AM UTC coverage: 58.606% (+0.03%) from 58.577%
#4766

push

travis-ci

web-flow
merge: set version (#33122)

139135 of 302095 branches covered (46.06%)

Branch coverage included in aggregate %.

210115 of 293830 relevant lines covered (71.51%)

23976146.11 hits per line

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

79.64
/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) {
11,755,085✔
27
  *pInfo = NULL;
11,755,085✔
28

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

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

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

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

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
11,761,523✔
46
  if (code) {
11,763,662!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
11,763,662✔
52
  if (pLoadInfo->aSttBlk == NULL) {
11,760,575✔
53
    taosMemoryFreeClear(pLoadInfo);
419!
54
    return terrno;
419✔
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
11,760,156✔
58
  pLoadInfo->colIds = colList;
11,760,156✔
59
  pLoadInfo->numOfCols = numOfCols;
11,760,156✔
60

61
  *pInfo = pLoadInfo;
11,760,156✔
62
  return code;
11,760,156✔
63
}
64

65
static void freeItem(void *pValue) {
50,794,349✔
66
  SValue *p = (SValue *)pValue;
50,794,349✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
50,794,349!
68
    taosMemoryFree(p->pData);
429,047!
69
  }
70
}
50,800,677✔
71

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

77
  pLoadInfo->currentLoadBlockIndex = 1;
11,769,514✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
11,769,514✔
80
  tBlockDataDestroy(&pInfo->data);
11,769,514✔
81
  pInfo->sttBlockIndex = -1;
11,768,498✔
82
  pInfo->pin = false;
11,768,498✔
83

84
  pInfo = &pLoadInfo->blockData[1];
11,768,498✔
85
  tBlockDataDestroy(&pInfo->data);
11,768,498✔
86
  pInfo->sttBlockIndex = -1;
11,768,227✔
87
  pInfo->pin = false;
11,768,227✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
11,768,227✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
11,768,030✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
11,768,037✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
11,768,177✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
11,767,951✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
11,767,936✔
95

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

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
11,768,133✔
104
  taosMemoryFree(pLoadInfo);
11,768,764!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
11,763,177✔
108
  tLDataIterClose2(pIter);
11,763,177✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
11,769,808✔
110
  taosMemoryFree(pIter);
11,769,343!
111
}
11,770,017✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
21,915,724✔
114
  if (pLDataIterArray == NULL) {
21,915,724✔
115
    return;
43,490✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
21,872,234✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
33,626,266✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
11,751,244✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
23,520,015✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
11,763,439✔
123
      if (pIter->pBlockLoadInfo != NULL) {
11,762,934!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
11,763,118✔
125
        if (pLoadCost != NULL) {
11,763,118✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
11,760,389✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
11,760,389✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
11,760,389✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
11,760,389✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
11,762,934✔
134
    }
135

136
    taosArrayDestroy(pList);
11,748,771✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
21,875,022✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
7,938,099✔
150
}
7,938,099✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
1,340,026,785✔
153
  if (pResBlock != NULL) {
1,340,026,785!
154
    *pResBlock = NULL;
1,340,355,896✔
155
  }
156

157
  int32_t            code = 0;
1,340,026,785✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
1,340,026,785✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
1,340,026,785✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
1,011,210,279✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
566!
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
566✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
1,011,210,279✔
169
    return code;
1,011,210,279✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
328,816,506!
173
    if (pInfo->currentLoadBlockIndex != 1) {
354,941,429✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
626!
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
626✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
354,941,429✔
181
    return code;
354,941,429✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
7,938,638✔
189
  int64_t st = taosGetTimestampUs();
7,940,576✔
190

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

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

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
7,942,434✔
216
  return code;
7,942,434✔
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;
9,256,140✔
223
  int32_t step = backward ? 1 : -1;
9,256,140✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
18,560,386!
225
    i += step;
9,304,246✔
226
  }
227
  return i - step;
9,256,140✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
25,764,429✔
231
  int32_t midPos = -1;
25,764,429✔
232
  if (num <= 0) {
25,764,429✔
233
    return -1;
6,176,005✔
234
  }
235

236
  int32_t firstPos = 0;
19,588,424✔
237
  int32_t lastPos = num - 1;
19,588,424✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
19,588,424✔
241
    return -1;
10,332,170✔
242
  }
243

244
  while (1) {
674,172✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
9,930,426✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
9,108,997✔
247
    }
248

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

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

256
    if (uid < pBlockList[midPos].minUid) {
821,315✔
257
      lastPos = midPos - 1;
331,262✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
490,053✔
259
      firstPos = midPos + 1;
342,910✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
147,143✔
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;
9,100,418✔
269
  int32_t step = backward ? 1 : -1;
9,100,418✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
135,721,874!
271
    i += step;
126,621,456✔
272
  }
273
  return i - step;
9,100,418✔
274
}
275

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

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

285
  while (1) {
1,827,804✔
286
    if (uid == uidList[firstPos]) {
11,035,479✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
7,749,640✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
3,285,839✔
291
      return -1;
107,257✔
292
    }
293

294
    int32_t numOfRows = lastPos - firstPos + 1;
3,178,582✔
295
    int32_t midPos = (numOfRows >> 1u) + firstPos;
3,178,582✔
296

297
    if (uid < uidList[midPos]) {
3,178,582✔
298
      lastPos = midPos - 1;
771,233✔
299
    } else if (uid > uidList[midPos]) {
2,407,349✔
300
      firstPos = midPos + 1;
1,056,571✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
1,350,778✔
303
    }
304
  }
305
}
306

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

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

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
11,722,719✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
4,130,321✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
2,012,141✔
322
      pIter->iSttBlk = -1;
2,011,281✔
323
      return TSDB_CODE_SUCCESS;
2,011,281✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
2,118,180✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
2,118,927✔
327
      if (px == NULL) {
2,118,174!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
7,592,398✔
333
    if (pTmp == NULL) {
7,601,921!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
26,424,066✔
338
      SSttBlk *p = &pArray->data[i];
21,426,307✔
339
      if (p->suid < suid) {
21,426,307✔
340
        continue;
12,583,393✔
341
      }
342

343
      if (p->suid == suid) {
8,842,914✔
344
        void *px = taosArrayPush(pTmp, p);
6,239,200✔
345
        if (px == NULL) {
6,239,200!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
2,603,579!
350
        break;
2,604,104✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
7,601,863✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
7,602,635✔
356
  }
357

358
  return code;
9,720,809✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
4,855,837✔
362
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
4,855,837!
363
    char *p = (char *)pVal->pData;
434,735✔
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
434,735!
365
    if (pBuf == NULL) {
435,041!
366
      return terrno;
×
367
    }
368

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

373
  return TSDB_CODE_SUCCESS;
4,856,143✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
11,746,749✔
385
  if (numOfBlocks <= 0) {
11,746,749✔
386
    return code;
21,673✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
14,397,906✔
390
    ++startIndex;
2,672,830✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
11,725,076✔
394
    return 0;
3,250,788✔
395
  }
396

397
  int32_t endIndex = startIndex;
8,474,288✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
16,961,115✔
399
    ++endIndex;
8,486,827✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
8,474,288✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
8,474,288✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
8,474,288✔
407
  QUERY_CHECK_CODE(code, lino, _end);
8,474,466!
408

409
  int64_t st = taosGetTimestampUs();
8,473,975✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
16,937,663✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
8,483,975✔
413
    QUERY_CHECK_CODE(code, lino, _end);
8,486,434!
414

415
    int32_t i = 0;
8,486,434✔
416
    int32_t rows = block.numOfRecords;
8,486,434✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
19,246,595✔
418
      ++i;
10,760,161✔
419
    }
420

421
    // existed
422
    if (i < rows) {
8,486,434✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
8,290,587✔
424

425
      if (pInfo->pUid == NULL) {
8,290,587✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
8,278,052✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
8,278,040✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
8,278,455✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
8,278,555✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
8,278,358✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
8,278,395✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
8,278,466!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
8,278,517!
436
          code = terrno;
×
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
8,291,114✔
442
        int32_t size = rows - i;
6,010,922✔
443
        int32_t offset = i * sizeof(int64_t);
6,010,922✔
444

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

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

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

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
6,010,044✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
6,010,175✔
456

457
        if (block.numOfPKs > 0) {
6,009,895✔
458
          SValue vFirst = {0}, vLast = {0};
253,889✔
459
          for (int32_t f = i; f < rows; ++f) {
2,612,204✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
2,358,007✔
461
            TSDB_CHECK_CODE(code, lino, _end);
2,358,089!
462

463
            code = tValueDupPayload(&vFirst);
2,358,089✔
464
            TSDB_CHECK_CODE(code, lino, _end);
2,358,235!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
2,358,235✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,358,297!
468

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

473
            code = tValueDupPayload(&vLast);
2,358,305✔
474
            TSDB_CHECK_CODE(code, lino, _end);
2,358,309!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
2,358,309✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,358,315!
478
          }
479
        } else {
480
          SValue vFirst = {0};
5,756,006✔
481
          for (int32_t j = 0; j < size; ++j) {
25,228,750✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
19,495,367✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
19,480,889!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
19,480,889✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
19,472,744!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
2,280,192✔
491
        while (i < rows) {
5,478,572✔
492
          code = tStatisBlockGet(&block, i, &record);
5,478,531✔
493
          TSDB_CHECK_CODE(code, lino, _end);
5,478,357!
494

495
          if (record.suid != suid) {
5,478,357✔
496
            break;
2,280,220✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
3,198,137✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,198,301!
501

502
          px = taosArrayPush(pInfo->pCount, &record.count);
3,198,301✔
503
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,198,317!
504

505
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
3,198,317✔
506
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,198,271!
507

508
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
3,198,271✔
509
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,198,266!
510

511
          if (record.firstKey.numOfPKs > 0) {
3,198,266✔
512
            SValue s = record.firstKey.pks[0];
70,287✔
513
            code = tValueDupPayload(&s);
70,287✔
514
            TSDB_CHECK_CODE(code, lino, _end);
70,310!
515

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

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
70,310✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
70,311!
525
          } else {
526
            SValue v = {0};
3,127,979✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
3,127,979✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,128,078!
529

530
            px = taosArrayPush(pInfo->pLastKey, &v);
3,128,078✔
531
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,128,069!
532
          }
533

534
          i += 1;
3,198,380✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
8,453,688✔
541
  tStatisBlockDestroy(&block);
8,453,688✔
542
  if (code != 0) {
8,473,890!
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;
8,473,880✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
8,473,880✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
11,746,947✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
11,747,217✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
11,747,217✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
11,747,217✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
11,747,217✔
562
  if (code != TSDB_CODE_SUCCESS) {
11,745,351!
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);
11,745,351✔
569
  if (code != TSDB_CODE_SUCCESS) {
11,751,977!
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;
11,751,977✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
11,751,977✔
577
  if (code != TSDB_CODE_SUCCESS) {
11,750,243!
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);
11,750,243✔
584
  if (code != TSDB_CODE_SUCCESS) {
11,754,520!
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);
11,754,520✔
590

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

596
static int32_t uidComparFn(const void *p1, const void *p2) {
24,141,280✔
597
  const uint64_t *pFirst = p1;
24,141,280✔
598
  const uint64_t *pVal = p2;
24,141,280✔
599

600
  if (*pFirst == *pVal) {
24,141,280✔
601
    return 0;
9,161,645✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
14,979,635✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
25,776,212✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
25,776,212!
610
    return;
6,177,462✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
19,595,544✔
614
  if (index >= 0) {
19,600,348✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
9,161,938✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
9,158,901✔
617

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

620
    if (pRange->skey.numOfPKs > 0) {
9,158,701✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
508,017✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
507,943✔
623
    }
624
  }
625
}
626

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
25,773,306✔
641
  pIter->pReader = pSttFileReader;
25,773,306✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
25,773,306✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
25,773,306!
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) {
25,773,306✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
11,753,038✔
654
    if (code != TSDB_CODE_SUCCESS) {
11,752,941!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
25,773,209✔
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);
25,766,277✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
25,764,376✔
665
  if (pIter->iSttBlk != -1) {
25,764,254✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
9,268,253✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
9,266,124✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
9,266,124!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
8,121,220!
671
      pIter->pSttBlk = NULL;
1,752✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
9,266,124!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
1,145,592!
676
      pIter->pSttBlk = NULL;
384✔
677
      pIter->ignoreEarlierTs = true;
384✔
678
    }
679
  }
680

681
  return code;
25,762,125✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
11,763,364✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
11,763,364✔
686
  pIter->pReader = NULL;
11,769,850✔
687
}
11,769,850✔
688

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

693
  pIter->iSttBlk += step;
9,315,376✔
694

695
  int32_t index = -1;
9,315,376✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
9,315,376✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
9,317,447!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
362,657✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
362,649✔
700
      break;
244,956✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
117,693✔
704
      break;
18,827✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
9,315,368✔
736
  if (index != -1) {
9,315,368✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
96,184✔
738

739
    pIter->iSttBlk = index;
96,183✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
96,183✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
96,183✔
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);
9,219,184✔
746
  }
747
}
9,315,367✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
683,179,509✔
756
  if (code) {
678,772,257!
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) {
678,772,257✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
9,208,024✔
764
    if (i == -1) {
9,208,377!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
107,294✔
767
      return code;
107,294✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
699,188,796!
772
    if (pData->aUid != NULL) {
695,288,547✔
773
      if (!pIter->backward) {
690,456,273✔
774
        if (pData->aUid[i] > pIter->uid) {
636,558,904✔
775
          break;
1,547,938✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
53,897,369✔
779
          break;
137,529✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
693,603,080✔
785
    if (!pIter->backward) {               // asc
693,603,080✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
638,790,601✔
787
        break;
506,297✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
638,284,304✔
790
          continue;
11,209,369✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
627,074,935✔
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) {
54,812,479✔
804
        break;
263,312✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
54,549,167✔
807
          continue;
8,733,729✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
45,815,438!
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];
672,890,373✔
822
    if (ver < pIter->verRange.minVer) {
672,890,373!
823
      continue;
×
824
    }
825

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

831
    hasVal = true;
672,890,373✔
832
    break;
672,890,373✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
679,245,698✔
836
  return code;
679,245,698✔
837
}
838

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

846
  *hasNext = false;
695,515,125✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
695,515,125✔
850
    return code;
16,508,658✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
679,006,467✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
682,595,796!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
683,134,734✔
860

861
  while (1) {
95,067✔
862
    bool skipBlock = false;
683,229,801✔
863
    code = findNextValidRow(pIter, idStr);
683,229,801✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
680,508,191!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
680,508,191!
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) {
680,508,191!
890
      tLDataIterNextBlock(pIter, idStr);
8,999,337✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
9,314,685✔
892
        goto _exit;
9,219,620✔
893
      }
894
    } else {
895
      break;
896
    }
897

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

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

910
  pIter->rInfo.suid = pBlockData->suid;
671,508,854✔
911
  pIter->rInfo.uid = pBlockData->uid;
671,508,854✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
671,508,854✔
913

914
_exit:
680,189,534✔
915
  if (code) {
680,189,534!
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);
680,582,809!
920
  return code;
680,582,809✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
51,600,335✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
51,886,772✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
51,886,772✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
51,886,772✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
51,886,772!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
52,217,569!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
51,885,491✔
933
  if (ret < 0) {
51,904,602✔
934
    return -1;
30,591,285✔
935
  } else if (ret > 0) {
21,313,317✔
936
    return 1;
8,877,052✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
12,436,265!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
12,436,265!
940

941
    if (ver1 < ver2) {
12,436,265✔
942
      return -1;
4,981,424✔
943
    } else if (ver1 > ver2) {
7,454,841!
944
      return 1;
7,956,578✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

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

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

963
  if (!pMTree->backward) {  // asc
26,150,082✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
21,921,276✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
4,228,806✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
26,154,882✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
26,154,882✔
973
  if (numOfLevels == 0) {
26,154,882✔
974
    goto _end;
629,088✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
25,525,794✔
978
  if (code) {
25,520,017!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
51,223,985✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
25,697,927✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
25,697,927✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
51,461,077✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
25,757,109✔
988

989
      SSttFileReader    *pSttFileReader = pIter->pReader;
25,752,338✔
990
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
25,752,338✔
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) {
25,752,338✔
995
        SSttFileReaderConfig conf = {.tsdb = pConf->pTsdb, .szPage = pConf->pTsdb->pVnode->config.tsdbPageSize};
11,747,754✔
996
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
11,747,754✔
997

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
11,747,754✔
999
        if (code != TSDB_CODE_SUCCESS) {
11,746,462!
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) {
25,753,087✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
11,752,138✔
1007
        if (code != TSDB_CODE_SUCCESS) {
11,750,051!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
25,751,000✔
1013

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

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

1024
      bool hasVal = NULL;
25,761,619✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
25,761,619✔
1026
      if (code) {
25,771,279!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
25,771,279✔
1031
        tMergeTreeAddIter(pMTree, pIter);
9,146,210✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
9,144,960✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
16,625,069!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
16,627,817✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
25,526,058✔
1049

1050
_end:
629,088✔
1051
  tMergeTreeClose(pMTree);
629,088✔
1052
  return code;
628,930✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
9,145,018✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
9,145,018✔
1057
}
9,145,748✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
379,557,784✔
1065
    pInfo->blockData[0].pin = true;
235,723,958✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
235,723,958✔
1067
    return;
235,749,522✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
143,833,826!
1071
    pInfo->blockData[1].pin = true;
156,421,851✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
156,421,851✔
1073
    return;
156,365,443✔
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) {
373,307,421✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
373,307,421✔
1081
  if (pInfo->blockData[0].pin) {
373,307,421✔
1082
    pInfo->blockData[0].pin = false;
233,325,103✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
233,325,103✔
1084
    return;
233,344,745✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
139,982,318!
1088
    pInfo->blockData[1].pin = false;
154,639,280✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
154,639,280✔
1090
    return;
154,644,115✔
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) {
380,645,784✔
1097
  if (pMTree->pIter == NULL) {
380,645,784!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
380,645,784✔
1102
  pMTree->pPinnedBlockIter = pIter;
380,645,784✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
380,645,784✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
373,736,324✔
1112
  pMTree->pPinnedBlockIter = NULL;
373,736,324✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
373,736,324✔
1114
}
1115

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

1122
  *pHasNext = false;
694,561,167✔
1123
  while (pMTree->pIter) {
694,561,167✔
1124
    SLDataIter *pIter = pMTree->pIter;
668,054,223✔
1125
    bool        hasVal = false;
668,054,223✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
668,054,223✔
1127
    if (!hasVal || (code != 0)) {
670,761,799✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
9,097,981!
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;
9,097,981✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
670,761,799✔
1137
    if (pMTree->pIter && pIter) {
670,761,799✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
30,250,913✔
1139
      if (c > 0) {
30,053,097✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
4,634,609✔
1141
        pMTree->pIter = NULL;
5,006,171✔
1142
      } else if (!c) {
25,418,488!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
670,935,545✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
697,442,489✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
40,201,103✔
1152
    if (pMTree->pIter) {
40,201,103✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
14,108,161✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
697,112,508✔
1158
  return code;
697,112,508✔
1159
}
1160

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