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

taosdata / TDengine / #4680

21 Aug 2025 12:03PM UTC coverage: 59.87% (-0.2%) from 60.031%
#4680

push

travis-ci

web-flow
jdbc release 3.7.3 (#32682)

* chore(jdbc): update jdbc version

* docs(jdbc): release 3.7.3

137338 of 292193 branches covered (47.0%)

Branch coverage included in aggregate %.

207909 of 284466 relevant lines covered (73.09%)

4731310.74 hits per line

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

80.01
/source/dnode/vnode/src/tsdb/tsdbMergeTree.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tsdb.h"
17
#include "tsdbFSet2.h"
18
#include "tsdbMerge.h"
19
#include "tsdbReadUtil.h"
20
#include "tsdbSttFileRW.h"
21
#include "tsdbUtil2.h"
22

23
static void tLDataIterClose2(SLDataIter *pIter);
24

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

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
2,669,991!
30
  if (pLoadInfo == NULL) {
2,672,026!
31
    return terrno;
×
32
  }
33

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

37
  pLoadInfo->currentLoadBlockIndex = 1;
2,672,026✔
38

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

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

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
2,671,338✔
52
  if (pLoadInfo->aSttBlk == NULL) {
2,672,395✔
53
    taosMemoryFreeClear(pLoadInfo);
255!
54
    return terrno;
255✔
55
  }
56

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

61
  *pInfo = pLoadInfo;
2,672,140✔
62
  return code;
2,672,140✔
63
}
64

65
static void freeItem(void *pValue) {
31,144,984✔
66
  SValue *p = (SValue *)pValue;
31,144,984✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
31,144,984!
68
    taosMemoryFree(p->pData);
344,992!
69
  }
70
}
31,148,044✔
71

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

77
  pLoadInfo->currentLoadBlockIndex = 1;
2,673,693✔
78

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

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

89
  taosArrayDestroy(pLoadInfo->info.pUid);
2,673,032✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
2,672,866✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
2,672,935✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
2,673,059✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
2,672,987✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
2,673,033✔
95

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

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
2,673,089✔
104
  taosMemoryFree(pLoadInfo);
2,673,254!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
2,670,736✔
108
  tLDataIterClose2(pIter);
2,670,736✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
2,673,804✔
110
  taosMemoryFree(pIter);
2,673,519!
111
}
2,673,801✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
3,976,891✔
114
  if (pLDataIterArray == NULL) {
3,976,891✔
115
    return;
37,557✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
3,939,334✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
6,606,328✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
2,664,975✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
5,338,465✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
2,671,426✔
123
      if (pIter->pBlockLoadInfo != NULL) {
2,670,627!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
2,670,791✔
125
        if (pLoadCost != NULL) {
2,670,791✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
2,667,728✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
2,667,728✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
2,667,728✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
2,667,728✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
2,670,627✔
134
    }
135

136
    taosArrayDestroy(pList);
2,663,105✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
3,941,353✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
1,462,181✔
150
}
1,462,181✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
576,506,798✔
153
  if (pResBlock != NULL) {
576,506,798!
154
    *pResBlock = NULL;
576,778,381✔
155
  }
156

157
  int32_t            code = 0;
576,506,798✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
576,506,798✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
576,506,798✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
329,541,511✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
482✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
482✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
329,541,511✔
169
    return code;
329,541,511✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
246,965,287!
173
    if (pInfo->currentLoadBlockIndex != 1) {
270,965,899✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
509✔
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
509✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
270,965,899✔
181
    return code;
270,965,899✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
1,462,556✔
189
  int64_t st = taosGetTimestampUs();
1,463,373✔
190

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

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

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

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
1,463,552✔
216
  return code;
1,463,552✔
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;
2,034,309✔
223
  int32_t step = backward ? 1 : -1;
2,034,309✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
4,100,968!
225
    i += step;
2,066,659✔
226
  }
227
  return i - step;
2,034,309✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
4,437,796✔
231
  int32_t midPos = -1;
4,437,796✔
232
  if (num <= 0) {
4,437,796✔
233
    return -1;
1,672,151✔
234
  }
235

236
  int32_t firstPos = 0;
2,765,645✔
237
  int32_t lastPos = num - 1;
2,765,645✔
238

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

244
  while (1) {
186,876✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
2,221,301✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
1,954,576✔
247
    }
248

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

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

256
    if (uid < pBlockList[midPos].minUid) {
266,609✔
257
      lastPos = midPos - 1;
85,911✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
180,698✔
259
      firstPos = midPos + 1;
100,965✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
79,733✔
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;
2,022,938✔
269
  int32_t step = backward ? 1 : -1;
2,022,938✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
51,883,655!
271
    i += step;
49,860,717✔
272
  }
273
  return i - step;
2,022,938✔
274
}
275

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

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

285
  while (1) {
1,030,862✔
286
    if (uid == uidList[firstPos]) {
3,067,394✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
1,389,848✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
1,677,546✔
291
      return -1;
13,594✔
292
    }
293

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

297
    if (uid < uidList[midPos]) {
1,663,952✔
298
      lastPos = midPos - 1;
346,917✔
299
    } else if (uid > uidList[midPos]) {
1,317,035✔
300
      firstPos = midPos + 1;
683,945✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
633,090✔
303
    }
304
  }
305
}
306

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

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

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
2,667,344✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
2,198,571✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
773,372✔
322
      pIter->iSttBlk = -1;
773,076✔
323
      return TSDB_CODE_SUCCESS;
773,076✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,425,199✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,425,235✔
327
      if (px == NULL) {
1,424,428!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
468,773✔
333
    if (pTmp == NULL) {
470,983!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
1,609,199✔
338
      SSttBlk *p = &pArray->data[i];
1,355,074✔
339
      if (p->suid < suid) {
1,355,074✔
340
        continue;
775,989✔
341
      }
342

343
      if (p->suid == suid) {
579,085✔
344
        void *px = taosArrayPush(pTmp, p);
362,311✔
345
        if (px == NULL) {
362,311!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
216,780!
350
        break;
216,866✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
470,991✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
470,951✔
356
  }
357

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

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

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

373
  return TSDB_CODE_SUCCESS;
849,322✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
2,670,032✔
385
  if (numOfBlocks <= 0) {
2,670,032✔
386
    return code;
2,638✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
3,084,614✔
390
    ++startIndex;
417,220✔
391
  }
392

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

397
  int32_t endIndex = startIndex;
1,779,958✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
3,572,729✔
399
    ++endIndex;
1,792,771✔
400
  }
401

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

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
1,779,958✔
407
  QUERY_CHECK_CODE(code, lino, _end);
1,779,981!
408

409
  int64_t st = taosGetTimestampUs();
1,780,011✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
3,567,047✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
1,791,542✔
413
    QUERY_CHECK_CODE(code, lino, _end);
1,792,122!
414

415
    int32_t i = 0;
1,792,122✔
416
    int32_t rows = block.numOfRecords;
1,792,122✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
2,460,652✔
418
      ++i;
668,530✔
419
    }
420

421
    // existed
422
    if (i < rows) {
1,792,122✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
1,748,372✔
424

425
      if (pInfo->pUid == NULL) {
1,748,372✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
1,735,573✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
1,736,031✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
1,736,067✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
1,736,076✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
1,736,100✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
1,736,018✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
1,736,023!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
1,735,969!
436
          code = terrno;
167✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
1,748,655✔
442
        int32_t size = rows - i;
1,653,513✔
443
        int32_t offset = i * sizeof(int64_t);
1,653,513✔
444

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

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

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

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

457
        if (block.numOfPKs > 0) {
1,652,602✔
458
          SValue vFirst = {0}, vLast = {0};
160,136✔
459
          for (int32_t f = i; f < rows; ++f) {
522,592✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
362,269✔
461
            TSDB_CHECK_CODE(code, lino, _end);
362,375!
462

463
            code = tValueDupPayload(&vFirst);
362,375✔
464
            TSDB_CHECK_CODE(code, lino, _end);
362,416!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
362,416✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
362,418!
468

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

473
            code = tValueDupPayload(&vLast);
362,450✔
474
            TSDB_CHECK_CODE(code, lino, _end);
362,460!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
362,460✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
362,456!
478
          }
479
        } else {
480
          SValue vFirst = {0};
1,492,466✔
481
          for (int32_t j = 0; j < size; ++j) {
16,165,784✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
14,677,959✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
14,678,788!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
14,678,788✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
14,673,318!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
95,142✔
491
        while (i < rows) {
231,324!
492
          code = tStatisBlockGet(&block, i, &record);
231,330✔
493
          TSDB_CHECK_CODE(code, lino, _end);
231,244!
494

495
          if (record.suid != suid) {
231,244✔
496
            break;
95,144✔
497
          }
498

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

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

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

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

511
          if (record.firstKey.numOfPKs > 0) {
136,119✔
512
            SValue s = record.firstKey.pks[0];
62,379✔
513
            code = tValueDupPayload(&s);
62,379✔
514
            TSDB_CHECK_CODE(code, lino, _end);
62,402!
515

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

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
62,405✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
62,403!
525
          } else {
526
            SValue v = {0};
73,740✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
73,740✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
73,762!
529

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

534
          i += 1;
136,182✔
535
        }
536
      }
537
    }
538
  }
539

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

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
2,671,191✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
2,670,767✔
556

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

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

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

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

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

589
  code = loadTombFn(pReader1, pIter->pReader, pIter->pBlockLoadInfo);
2,671,603✔
590

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

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

600
  if (*pFirst == *pVal) {
4,233,590✔
601
    return 0;
2,024,532✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
2,209,058✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
4,441,975✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
4,441,975!
610
    return;
1,673,022✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
2,768,084✔
614
  if (index >= 0) {
2,768,582✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
2,024,505✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
2,024,287✔
617

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

620
    if (pRange->skey.numOfPKs > 0) {
2,024,104✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
371,867✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
371,856✔
623
    }
624
  }
625
}
626

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
4,443,042✔
641
  pIter->pReader = pSttFileReader;
4,443,042✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
4,443,042✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
4,443,042!
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) {
4,443,042✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
2,672,100✔
654
    if (code != TSDB_CODE_SUCCESS) {
2,671,265!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
4,442,207✔
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);
4,440,339✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
4,440,033✔
665
  if (pIter->iSttBlk != -1) {
4,438,665✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
2,037,672✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
2,037,639✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
2,037,639!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
1,898,188!
671
      pIter->pSttBlk = NULL;
997✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
2,037,639!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
139,120!
676
      pIter->pSttBlk = NULL;
363✔
677
      pIter->ignoreEarlierTs = true;
363✔
678
    }
679
  }
680

681
  return code;
4,438,632✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
2,670,632✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
2,670,632✔
686
  pIter->pReader = NULL;
2,673,814✔
687
}
2,673,814✔
688

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

693
  pIter->iSttBlk += step;
2,076,606✔
694

695
  int32_t index = -1;
2,076,606✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
2,076,606✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
2,078,397!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
197,286✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
197,282✔
700
      break;
113,499✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
83,783✔
704
      break;
6,730✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
2,076,602✔
736
  if (index != -1) {
2,076,602✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
74,719✔
738

739
    pIter->iSttBlk = index;
74,719✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
74,719✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
74,719✔
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);
2,001,883✔
746
  }
747
}
2,076,604✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
298,024,257✔
756
  if (code) {
296,925,259!
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) {
296,925,259✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
2,036,908✔
764
    if (i == -1) {
2,037,874!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
13,636✔
767
      return code;
13,636✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
309,641,202!
772
    if (pData->aUid != NULL) {
309,445,369✔
773
      if (!pIter->backward) {
307,722,988✔
774
        if (pData->aUid[i] > pIter->uid) {
300,178,465✔
775
          break;
611,675✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
7,544,523✔
779
          break;
68,858✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
308,764,836✔
785
    if (!pIter->backward) {               // asc
308,764,836✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
300,937,606✔
787
        break;
5,630✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
300,931,976✔
790
          continue;
9,634,755✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
291,297,221✔
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) {
7,827,230✔
804
        break;
3,052✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
7,824,178✔
807
          continue;
2,569,238✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
5,254,940!
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];
296,552,161✔
822
    if (ver < pIter->verRange.minVer) {
296,552,161!
823
      continue;
×
824
    }
825

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

831
    hasVal = true;
296,552,161✔
832
    break;
296,552,161✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
297,437,209✔
836
  return code;
297,437,209✔
837
}
838

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

846
  *hasNext = false;
299,503,477✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
299,503,477✔
850
    return code;
2,404,002✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
297,099,475✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
297,098,612!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
297,234,132✔
860

861
  while (1) {
73,465✔
862
    bool skipBlock = false;
297,307,597✔
863
    code = findNextValidRow(pIter, idStr);
297,307,597✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
298,312,406!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
298,312,406!
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) {
298,312,406✔
890
      tLDataIterNextBlock(pIter, idStr);
2,082,433✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
2,075,608✔
892
        goto _exit;
2,002,144✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
73,464!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
74,719✔
900
      if ((pBlockData == NULL) || (code != 0)) {
74,716!
901
        lino = __LINE__;
×
902
        goto _exit;
×
903
      }
904

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

910
  pIter->rInfo.suid = pBlockData->suid;
296,229,973✔
911
  pIter->rInfo.uid = pBlockData->uid;
296,229,973✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
296,229,973✔
913

914
_exit:
298,096,593✔
915
  if (code) {
298,096,593!
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);
298,271,234!
920
  return code;
298,271,234✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
51,725,673✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
51,856,381✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
51,856,381✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
51,856,381✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
51,856,381!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
52,210,953!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
51,924,339✔
933
  if (ret < 0) {
51,916,226✔
934
    return -1;
32,810,587✔
935
  } else if (ret > 0) {
19,105,639✔
936
    return 1;
7,659,558✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
11,446,081!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
11,446,081!
940

941
    if (ver1 < ver2) {
11,446,081✔
942
      return -1;
4,623,528✔
943
    } else if (ver1 > ver2) {
6,822,553!
944
      return 1;
7,282,005✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

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

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

963
  if (!pMTree->backward) {  // asc
4,801,506✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
4,350,154✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
451,352✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
4,800,278✔
970

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

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
4,258,876✔
978
  if (code) {
4,256,631!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
8,673,779✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
4,419,236✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
4,419,236✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
8,855,912✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
4,438,764✔
988

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

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
2,670,708✔
999
        if (code != TSDB_CODE_SUCCESS) {
2,669,630!
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) {
4,433,861✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
2,670,452✔
1007
        if (code != TSDB_CODE_SUCCESS) {
2,672,063!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
4,435,472✔
1013

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

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

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

1030
      if (hasVal) {
4,438,169✔
1031
        tMergeTreeAddIter(pMTree, pIter);
2,014,951✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
2,014,124✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
2,423,218!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
2,424,349✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
4,254,543✔
1049

1050
_end:
541,402✔
1051
  tMergeTreeClose(pMTree);
541,402✔
1052
  return code;
541,320✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
2,014,728✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
2,014,728✔
1057
}
2,014,620✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
252,805,341✔
1065
    pInfo->blockData[0].pin = true;
143,126,225✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
143,126,225✔
1067
    return;
143,163,720✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
109,679,116!
1071
    pInfo->blockData[1].pin = true;
118,442,922✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
118,442,922✔
1073
    return;
118,442,606✔
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) {
251,415,704✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
251,415,704✔
1081
  if (pInfo->blockData[0].pin) {
251,415,704✔
1082
    pInfo->blockData[0].pin = false;
142,505,393✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
142,505,393✔
1084
    return;
142,505,037✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
108,910,311!
1088
    pInfo->blockData[1].pin = false;
118,051,949✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
118,051,949✔
1090
    return;
117,985,015✔
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) {
252,912,767✔
1097
  if (pMTree->pIter == NULL) {
252,912,767!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
252,912,767✔
1102
  pMTree->pPinnedBlockIter = pIter;
252,912,767✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
252,912,767✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
251,578,490✔
1112
  pMTree->pPinnedBlockIter = NULL;
251,578,490✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
251,578,490✔
1114
}
1115

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

1122
  *pHasNext = false;
299,675,511✔
1123
  while (pMTree->pIter) {
299,675,511✔
1124
    SLDataIter *pIter = pMTree->pIter;
294,714,892✔
1125
    bool        hasVal = false;
294,714,892✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
294,714,892✔
1127
    if (!hasVal || (code != 0)) {
296,020,108✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
1,980,293!
1129
        code = 0;  // suppress the file corrupt error to enable all queries within this cluster can run without failed.
×
1130
      }
1131

1132
      pMTree->pIter = NULL;
1,980,293✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
296,020,108✔
1137
    if (pMTree->pIter && pIter) {
296,020,108✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
32,556,272✔
1139
      if (c > 0) {
32,503,451✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
4,104,558✔
1141
        pMTree->pIter = NULL;
4,474,479✔
1142
      } else if (!c) {
28,398,893!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
296,337,208✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
301,297,827✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
11,214,032✔
1152
    if (pMTree->pIter) {
11,214,032✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
6,457,154✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
300,686,311✔
1158
  return code;
300,686,311✔
1159
}
1160

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