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

taosdata / TDengine / #3597

05 Feb 2025 01:41AM UTC coverage: 63.546% (-0.02%) from 63.562%
#3597

push

travis-ci

web-flow
Merge pull request #29639 from taosdata/feat/3.0/TS-5795

Enh(tsdb):print fid while data file corrupted.

141230 of 285630 branches covered (49.45%)

Branch coverage included in aggregate %.

2 of 4 new or added lines in 1 file covered. (50.0%)

398 existing lines in 102 files now uncovered.

220015 of 282846 relevant lines covered (77.79%)

18937864.43 hits per line

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

78.99
/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) {
4,539,914✔
27
  *pInfo = NULL;
4,539,914✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
4,539,914!
30
  if (pLoadInfo == NULL) {
4,544,094!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
4,544,094✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
4,544,094✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
4,544,094✔
38

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

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

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
4,545,280✔
52
  if (pLoadInfo->aSttBlk == NULL) {
4,545,097✔
53
    taosMemoryFreeClear(pLoadInfo);
701!
54
    return terrno;
701✔
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
4,544,396✔
58
  pLoadInfo->colIds = colList;
4,544,396✔
59
  pLoadInfo->numOfCols = numOfCols;
4,544,396✔
60

61
  *pInfo = pLoadInfo;
4,544,396✔
62
  return code;
4,544,396✔
63
}
64

65
static void freeItem(void *pValue) {
31,853,821✔
66
  SValue *p = (SValue *)pValue;
31,853,821✔
67
  if (IS_VAR_DATA_TYPE(p->type)) {
31,853,821!
68
    taosMemoryFree(p->pData);
×
69
  }
70
}
31,865,204✔
71

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

77
  pLoadInfo->currentLoadBlockIndex = 1;
4,547,483✔
78

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

84
  pInfo = &pLoadInfo->blockData[1];
4,547,581✔
85
  tBlockDataDestroy(&pInfo->data);
4,547,581✔
86
  pInfo->sttBlockIndex = -1;
4,547,560✔
87
  pInfo->pin = false;
4,547,560✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
4,547,560✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
4,547,397✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
4,547,459✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
4,547,484✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
4,547,451✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
4,547,450✔
95

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

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
4,547,532✔
104
  taosMemoryFree(pLoadInfo);
4,547,596!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
4,546,288✔
108
  tLDataIterClose2(pIter);
4,546,288✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
4,547,700✔
110
  taosMemoryFree(pIter);
4,547,593!
111
}
4,547,734✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
10,193,693✔
114
  if (pLDataIterArray == NULL) {
10,193,693✔
115
    return;
63,214✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
10,130,479✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
14,673,512✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
4,541,644✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
9,089,363✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
4,546,538✔
123
      if (pIter->pBlockLoadInfo != NULL) {
4,546,107!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
4,546,212✔
125
        if (pLoadCost != NULL) {
4,546,212✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
4,546,202✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
4,546,202✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
4,546,202✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
4,546,202✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
4,546,107✔
134
    }
135

136
    taosArrayDestroy(pList);
4,541,381✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
10,131,868✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
3,012,907✔
150
}
3,012,907✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
706,153,259✔
153
  if (pResBlock != NULL) {
706,153,259!
154
    *pResBlock = NULL;
706,572,174✔
155
  }
156

157
  int32_t            code = 0;
706,153,259✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
706,153,259✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
706,153,259✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
463,892,159✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
378✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
378✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
463,892,159✔
169
    return code;
463,892,159✔
170
  }
171

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

180
    *pResBlock = &pInfo->blockData[1].data;
255,657,042✔
181
    return code;
255,657,042✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
3,013,437✔
189
  int64_t st = taosGetTimestampUs();
3,014,367✔
190

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

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

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
3,014,599✔
216
  return code;
3,014,599✔
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;
3,620,082✔
223
  int32_t step = backward ? 1 : -1;
3,620,082✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
7,279,949!
225
    i += step;
3,659,867✔
226
  }
227
  return i - step;
3,620,082✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
9,923,181✔
231
  int32_t midPos = -1;
9,923,181✔
232
  if (num <= 0) {
9,923,181✔
233
    return -1;
2,384,351✔
234
  }
235

236
  int32_t firstPos = 0;
7,538,830✔
237
  int32_t lastPos = num - 1;
7,538,830✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
7,538,830✔
241
    return -1;
3,918,522✔
242
  }
243

244
  while (1) {
368,555✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
3,988,863✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
3,520,879✔
247
    }
248

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

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

256
    if (uid < pBlockList[midPos].minUid) {
467,758✔
257
      lastPos = midPos - 1;
186,215✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
281,543✔
259
      firstPos = midPos + 1;
182,340✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
99,203✔
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;
3,540,101✔
269
  int32_t step = backward ? 1 : -1;
3,540,101✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
72,513,048✔
271
    i += step;
68,972,947✔
272
  }
273
  return i - step;
3,540,101✔
274
}
275

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

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

285
  while (1) {
1,092,074✔
286
    if (uid == uidList[firstPos]) {
4,680,921✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
2,870,663✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
1,810,258✔
291
      return -1;
48,746✔
292
    }
293

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

297
    if (uid < uidList[midPos]) {
1,761,512✔
298
      lastPos = midPos - 1;
438,426✔
299
    } else if (uid > uidList[midPos]) {
1,323,086✔
300
      firstPos = midPos + 1;
653,648✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
669,438✔
303
    }
304
  }
305
}
306

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

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

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
4,526,178✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
2,133,528✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
946,741✔
322
      pIter->iSttBlk = -1;
946,658✔
323
      return TSDB_CODE_SUCCESS;
946,658✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,186,787✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,186,871✔
327
      if (px == NULL) {
1,186,256!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
2,392,650✔
333
    if (pTmp == NULL) {
2,396,739!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
8,680,244✔
338
      SSttBlk *p = &pArray->data[i];
7,094,989✔
339
      if (p->suid < suid) {
7,094,989✔
340
        continue;
4,167,136✔
341
      }
342

343
      if (p->suid == suid) {
2,927,853✔
344
        void *px = taosArrayPush(pTmp, p);
2,116,663✔
345
        if (px == NULL) {
2,116,663!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
811,107!
350
        break;
811,480✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
2,396,735✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
2,397,556✔
356
  }
357

358
  return code;
3,583,812✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
2,067,109✔
362
  if (IS_VAR_DATA_TYPE(pVal->type)) {
2,067,109!
363
    char *p = (char *)pVal->pData;
×
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
×
365
    if (pBuf == NULL) {
6!
366
      return terrno;
×
367
    }
368

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

373
  return TSDB_CODE_SUCCESS;
2,067,199✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
4,536,722✔
385
  if (numOfBlocks <= 0) {
4,536,722✔
386
    return code;
9,653✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
5,403,520✔
390
    ++startIndex;
876,451✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
4,527,069✔
394
    return 0;
1,212,589✔
395
  }
396

397
  int32_t endIndex = startIndex;
3,314,480✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
6,631,587✔
399
    ++endIndex;
3,317,107✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
3,314,480✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
3,314,480✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
3,314,480✔
407
  QUERY_CHECK_CODE(code, lino, _end);
3,314,249!
408

409
  int64_t st = taosGetTimestampUs();
3,314,207✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
6,624,424✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
3,314,648✔
413
    QUERY_CHECK_CODE(code, lino, _end);
3,316,708!
414

415
    int32_t i = 0;
3,316,708✔
416
    int32_t rows = block.numOfRecords;
3,316,708✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
7,161,044✔
418
      ++i;
3,844,336✔
419
    }
420

421
    // existed
422
    if (i < rows) {
3,316,708✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
3,250,912✔
424

425
      if (pInfo->pUid == NULL) {
3,250,912✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
3,248,305✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
3,248,485✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
3,248,758✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
3,248,774✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
3,248,587✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
3,248,746✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
3,248,711!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
3,248,741✔
436
          code = terrno;
254✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
3,251,064✔
442
        int32_t size = rows - i;
2,510,079✔
443
        int32_t offset = i * sizeof(int64_t);
2,510,079✔
444

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

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

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
2,510,020✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,509,741!
453

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
2,509,741✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,509,779✔
456

457
        if (block.numOfPKs > 0) {
2,509,516✔
458
          SValue vFirst = {0}, vLast = {0};
14,979✔
459
          for (int32_t f = i; f < rows; ++f) {
1,048,617✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
1,033,636✔
461
            TSDB_CHECK_CODE(code, lino, _end);
1,033,641!
462

463
            code = tValueDupPayload(&vFirst);
1,033,641✔
464
            TSDB_CHECK_CODE(code, lino, _end);
1,033,664!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
1,033,664✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,033,631!
468

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

473
            code = tValueDupPayload(&vLast);
1,033,642✔
474
            TSDB_CHECK_CODE(code, lino, _end);
1,033,645!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
1,033,645✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,033,638!
478
          }
479
        } else {
480
          SValue vFirst = {0};
2,494,537✔
481
          for (int32_t j = 0; j < size; ++j) {
15,917,479✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
13,429,050✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
13,426,500!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
13,426,500✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
13,422,942!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
740,985✔
491
        while (i < rows) {
1,894,404✔
492
          code = tStatisBlockGet(&block, i, &record);
1,894,398✔
493
          TSDB_CHECK_CODE(code, lino, _end);
1,894,364!
494

495
          if (record.suid != suid) {
1,894,364✔
496
            break;
741,005✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
1,153,359✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,153,423!
501

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

505
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
1,153,423✔
506
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,153,418!
507

508
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
1,153,418✔
509
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,153,419!
510

511
          if (record.firstKey.numOfPKs > 0) {
1,153,419!
512
            SValue s = record.firstKey.pks[0];
×
513
            code = tValueDupPayload(&s);
×
514
            TSDB_CHECK_CODE(code, lino, _end);
×
515

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

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
×
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
525
          } else {
526
            SValue v = {0};
1,153,419✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
1,153,419✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,153,411!
529

530
            px = taosArrayPush(pInfo->pLastKey, &v);
1,153,411✔
531
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,153,419!
532
          }
533

534
          i += 1;
1,153,419✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
3,309,776✔
541
  tStatisBlockDestroy(&block);
3,309,776✔
542
  if (code != 0) {
3,314,541!
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;
3,314,654✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
3,314,654✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
4,536,913✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
4,538,216✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
4,538,216✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
4,538,216✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
4,538,216✔
562
  if (code != TSDB_CODE_SUCCESS) {
4,538,049!
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);
4,538,049✔
569
  if (code != TSDB_CODE_SUCCESS) {
4,539,820!
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;
4,539,820✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
4,539,820✔
577
  if (code != TSDB_CODE_SUCCESS) {
4,539,730!
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);
4,539,730✔
584
  if (code != TSDB_CODE_SUCCESS) {
4,540,873!
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);
4,540,873✔
590

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

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

600
  if (*pFirst == *pVal) {
9,808,599✔
601
    return 0;
3,575,411✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
6,233,188✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
9,926,568✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
9,926,568!
610
    return;
2,384,465✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
7,541,391✔
614
  if (index >= 0) {
7,542,537✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
3,575,513✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
3,574,517✔
617

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

620
    if (pRange->skey.numOfPKs > 0) {
3,574,586✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
10,637✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
10,636✔
623
    }
624
  }
625
}
626

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
9,924,557✔
641
  pIter->pReader = pSttFileReader;
9,924,557✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
9,924,557✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
9,924,557!
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) {
9,924,557✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
4,538,993✔
654
    if (code != TSDB_CODE_SUCCESS) {
4,540,213!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
9,925,777✔
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);
9,924,556✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
9,923,691✔
665
  if (pIter->iSttBlk != -1) {
9,923,659✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
3,623,502✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
3,623,099✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
3,623,099!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
3,204,240✔
671
      pIter->pSttBlk = NULL;
529✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
3,623,099!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
418,982!
676
      pIter->pSttBlk = NULL;
502✔
677
      pIter->ignoreEarlierTs = true;
502✔
678
    }
679
  }
680

681
  return code;
9,923,256✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
4,546,170✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
4,546,170✔
686
  pIter->pReader = NULL;
4,547,702✔
687
}
4,547,702✔
688

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

693
  pIter->iSttBlk += step;
3,671,191✔
694

695
  int32_t index = -1;
3,671,191✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
3,671,191✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
3,685,728!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
255,525✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
255,508✔
700
      break;
156,306✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
99,202✔
704
      break;
14,575✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
3,671,174✔
736
  if (index != -1) {
3,671,174✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
69,244✔
738

739
    pIter->iSttBlk = index;
69,242✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
69,242✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
69,244✔
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);
3,601,930✔
746
  }
747
}
3,671,175✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
364,488,178✔
756
  if (code) {
358,003,942!
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) {
358,003,942✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
3,589,328✔
764
    if (i == -1) {
3,589,995!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
48,872✔
767
      return code;
48,872✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
367,985,802!
772
    if (pData->aUid != NULL) {
367,029,938✔
773
      if (!pIter->backward) {
364,038,957✔
774
        if (pData->aUid[i] > pIter->uid) {
341,988,527✔
775
          break;
698,276✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
22,050,430✔
779
          break;
39,557✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
366,292,105✔
785
    if (!pIter->backward) {               // asc
366,292,105✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
343,584,990✔
787
        break;
231,765✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
343,353,225✔
790
          continue;
4,667,793✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
338,685,432✔
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
100,004!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
100,004✔
797
          if (ret < 0) {
100,004!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
22,707,115✔
804
        break;
131,989✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
22,575,126✔
807
          continue;
4,940,172✔
808
        }
809

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

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

826
    // todo opt handle desc case
827
    if (ver > pIter->verRange.maxVer) {
356,320,386✔
828
      continue;
24✔
829
    }
830

831
    hasVal = true;
356,320,362✔
832
    break;
356,320,362✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
358,377,813✔
836
  return code;
358,377,813✔
837
}
838

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

846
  *hasNext = false;
366,074,579✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
366,074,579✔
850
    return code;
6,303,899✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
359,770,680✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
364,425,357!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
364,750,276✔
860

861
  while (1) {
68,609✔
862
    bool skipBlock = false;
364,818,885✔
863
    code = findNextValidRow(pIter, idStr);
364,818,885✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
358,995,312!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
358,995,312!
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) {
358,995,312!
890
      tLDataIterNextBlock(pIter, idStr);
3,571,056✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
3,670,664✔
892
        goto _exit;
3,602,055✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
68,609!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
69,243✔
900
      if ((pBlockData == NULL) || (code != 0)) {
69,235!
901
        lino = __LINE__;
×
902
        goto _exit;
×
903
      }
904

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

910
  pIter->rInfo.suid = pBlockData->suid;
355,424,256✔
911
  pIter->rInfo.uid = pBlockData->uid;
355,424,256✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
355,424,256✔
913

914
_exit:
358,701,384✔
915
  if (code) {
358,701,384!
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);
358,774,840!
920
  return code;
358,774,840✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
46,646,411✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
46,739,490✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
46,739,490✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
46,739,490✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
46,739,490!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
47,094,235!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
46,800,972✔
933
  if (ret < 0) {
46,828,068✔
934
    return -1;
27,088,275✔
935
  } else if (ret > 0) {
19,739,793✔
936
    return 1;
8,079,080✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
11,660,713!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
11,660,713!
940

941
    if (ver1 < ver2) {
11,660,713✔
942
      return -1;
4,752,043✔
943
    } else if (ver1 > ver2) {
6,908,670!
944
      return 1;
7,436,797✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

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

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

963
  if (!pMTree->backward) {  // asc
10,342,770✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
8,704,882✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
1,637,888✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
10,344,819✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
10,344,819✔
973
  if (numOfLevels == 0) {
10,344,819✔
974
    goto _end;
621,858✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
9,722,961✔
978
  if (code) {
9,722,453!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
19,615,576✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
9,888,689✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
9,888,689✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
19,812,438✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
9,919,315✔
988

989
      SSttFileReader    *pSttFileReader = pIter->pReader;
9,916,693✔
990
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
9,916,693✔
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) {
9,916,693✔
995
        SSttFileReaderConfig conf = {.tsdb = pConf->pTsdb, .szPage = pConf->pTsdb->pVnode->config.tsdbPageSize};
4,535,538✔
996
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
4,535,538✔
997

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
4,535,538✔
999
        if (code != TSDB_CODE_SUCCESS) {
4,535,900!
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) {
9,917,402✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
4,539,115✔
1007
        if (code != TSDB_CODE_SUCCESS) {
4,538,346!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
9,916,633✔
1013

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

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

1024
      bool hasVal = NULL;
9,923,505✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
9,923,505✔
1026
      if (code) {
9,926,103!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
9,926,103✔
1031
        tMergeTreeAddIter(pMTree, pIter);
3,571,092✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
3,570,098✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
6,355,011✔
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
6,354,869✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
9,726,887✔
1049

1050
_end:
621,858✔
1051
  tMergeTreeClose(pMTree);
621,858✔
1052
  return code;
621,771✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
3,570,407✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
3,570,407✔
1057
}
3,570,682✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
224,081,691✔
1065
    pInfo->blockData[0].pin = true;
126,403,620✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
126,403,620✔
1067
    return;
126,456,602✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
97,678,071!
1071
    pInfo->blockData[1].pin = true;
102,258,139✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
102,258,139✔
1073
    return;
102,263,153✔
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) {
216,876,085✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
216,876,085✔
1081
  if (pInfo->blockData[0].pin) {
216,876,085✔
1082
    pInfo->blockData[0].pin = false;
123,754,170✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
123,754,170✔
1084
    return;
123,761,711✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
93,121,915!
1088
    pInfo->blockData[1].pin = false;
99,729,267✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
99,729,267✔
1090
    return;
99,733,097✔
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) {
224,043,468✔
1097
  if (pMTree->pIter == NULL) {
224,043,468!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
224,043,468✔
1102
  pMTree->pPinnedBlockIter = pIter;
224,043,468✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
224,043,468✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
216,963,424✔
1112
  pMTree->pPinnedBlockIter = NULL;
216,963,424✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
216,963,424✔
1114
}
1115

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

1122
  *pHasNext = false;
365,745,206✔
1123
  if (pMTree->pIter) {
365,745,206✔
1124
    SLDataIter *pIter = pMTree->pIter;
355,750,722✔
1125
    bool        hasVal = false;
355,750,722✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
355,750,722✔
1127
    if (!hasVal || (code != 0)) {
354,639,704✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
3,549,630!
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;
3,549,630✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
354,639,704✔
1137
    if (pMTree->pIter && pIter) {
354,639,704✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
26,817,896✔
1139
      if (c > 0) {
26,703,289✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
4,439,526✔
1141
        pMTree->pIter = NULL;
4,714,695✔
1142
      } else if (!c) {
22,263,763!
1143
        return TSDB_CODE_INTERNAL_ERROR;
×
1144
      }
1145
    }
1146
  }
1147

1148
  if (pMTree->pIter == NULL) {
364,794,750✔
1149
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
18,585,630✔
1150
    if (pMTree->pIter) {
18,585,630✔
1151
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
8,263,640✔
1152
    }
1153
  }
1154

1155
  *pHasNext = (pMTree->pIter != NULL);
365,192,394✔
1156
  return code;
365,192,394✔
1157
}
1158

1159
void tMergeTreeClose(SMergeTree *pMTree) {
21,015,691✔
1160
  pMTree->pIter = NULL;
21,015,691✔
1161
  pMTree->pPinnedBlockIter = NULL;
21,015,691✔
1162
}
21,015,691✔
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