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

taosdata / TDengine / #4106

19 May 2025 07:15AM UTC coverage: 62.857% (-0.2%) from 63.042%
#4106

push

travis-ci

GitHub
Merge pull request #31115 from taosdata/merge/mainto3.0

156749 of 318088 branches covered (49.28%)

Branch coverage included in aggregate %.

242535 of 317143 relevant lines covered (76.47%)

18746393.97 hits per line

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

79.51
/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) {
7,822,881✔
27
  *pInfo = NULL;
7,822,881✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
7,822,881!
30
  if (pLoadInfo == NULL) {
7,830,296!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
7,830,296✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
7,830,296✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
7,830,296✔
38

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

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
7,829,342✔
46
  if (code) {
7,830,724!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
7,830,724✔
52
  if (pLoadInfo->aSttBlk == NULL) {
7,829,228✔
53
    taosMemoryFreeClear(pLoadInfo);
1,468!
54
    return terrno;
1,468✔
55
  }
56

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

61
  *pInfo = pLoadInfo;
7,827,760✔
62
  return code;
7,827,760✔
63
}
64

65
static void freeItem(void *pValue) {
74,675,222✔
66
  SValue *p = (SValue *)pValue;
74,675,222✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
74,675,222!
68
    taosMemoryFree(p->pData);
645,378!
69
  }
70
}
74,685,200✔
71

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

77
  pLoadInfo->currentLoadBlockIndex = 1;
7,838,235✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
7,838,235✔
80
  tBlockDataDestroy(&pInfo->data);
7,838,235✔
81
  pInfo->sttBlockIndex = -1;
7,836,296✔
82
  pInfo->pin = false;
7,836,296✔
83

84
  pInfo = &pLoadInfo->blockData[1];
7,836,296✔
85
  tBlockDataDestroy(&pInfo->data);
7,836,296✔
86
  pInfo->sttBlockIndex = -1;
7,835,694✔
87
  pInfo->pin = false;
7,835,694✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
7,835,694✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
7,834,968✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
7,835,322✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
7,835,758✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
7,835,257✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
7,835,286✔
95

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

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
7,835,531✔
104
  taosMemoryFree(pLoadInfo);
7,836,318!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
7,825,460✔
108
  tLDataIterClose2(pIter);
7,825,460✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
7,838,516✔
110
  taosMemoryFree(pIter);
7,837,708!
111
}
7,838,653✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
14,003,430✔
114
  if (pLDataIterArray == NULL) {
14,003,430✔
115
    return;
68,599✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
13,934,831✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
21,735,876✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
7,794,529✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
15,631,069✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
7,827,625✔
123
      if (pIter->pBlockLoadInfo != NULL) {
7,824,829!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
7,825,210✔
125
        if (pLoadCost != NULL) {
7,825,210✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
7,820,318✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
7,820,318✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
7,820,318✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
7,820,318✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
7,824,829✔
134
    }
135

136
    taosArrayDestroy(pList);
7,789,868✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
13,941,347✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
4,992,850✔
150
}
4,992,850✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
971,971,150✔
153
  if (pResBlock != NULL) {
971,971,150!
154
    *pResBlock = NULL;
972,305,530✔
155
  }
156

157
  int32_t            code = 0;
971,971,150✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
971,971,150✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
971,971,150✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
679,389,306✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
323✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
323✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
679,389,306✔
169
    return code;
679,389,306✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
292,581,844!
173
    if (pInfo->currentLoadBlockIndex != 1) {
307,133,420✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
319✔
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
319✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
307,133,420✔
181
    return code;
307,133,420✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
4,993,624✔
189
  int64_t st = taosGetTimestampUs();
4,995,633✔
190

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

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

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
4,997,458✔
216
  return code;
4,997,458✔
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;
6,020,945✔
223
  int32_t step = backward ? 1 : -1;
6,020,945✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
12,087,528!
225
    i += step;
6,066,583✔
226
  }
227
  return i - step;
6,020,945✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
16,428,610✔
231
  int32_t midPos = -1;
16,428,610✔
232
  if (num <= 0) {
16,428,610✔
233
    return -1;
4,371,863✔
234
  }
235

236
  int32_t firstPos = 0;
12,056,747✔
237
  int32_t lastPos = num - 1;
12,056,747✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
12,056,747✔
241
    return -1;
6,035,632✔
242
  }
243

244
  while (1) {
776,395✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
6,797,510!
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
5,890,629✔
247
    }
248

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

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

256
    if (uid < pBlockList[midPos].minUid) {
906,711✔
257
      lastPos = midPos - 1;
354,244✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
552,467✔
259
      firstPos = midPos + 1;
422,151✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
130,316✔
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;
5,886,767✔
269
  int32_t step = backward ? 1 : -1;
5,886,767✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
104,525,855!
271
    i += step;
98,639,088✔
272
  }
273
  return i - step;
5,886,767✔
274
}
275

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

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

285
  while (1) {
1,913,085✔
286
    if (uid == uidList[firstPos]) {
7,918,980✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
4,782,890✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
3,136,090✔
291
      return -1;
119,128✔
292
    }
293

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

297
    if (uid < uidList[midPos]) {
3,016,962✔
298
      lastPos = midPos - 1;
819,747✔
299
    } else if (uid > uidList[midPos]) {
2,197,215✔
300
      firstPos = midPos + 1;
1,093,338✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
1,103,877✔
303
    }
304
  }
305
}
306

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

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

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
7,807,747✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
4,145,863✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
2,002,582✔
322
      pIter->iSttBlk = -1;
2,001,627✔
323
      return TSDB_CODE_SUCCESS;
2,001,627✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
2,143,281✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
2,144,794✔
327
      if (px == NULL) {
2,143,338!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
3,661,884✔
333
    if (pTmp == NULL) {
3,670,971!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
12,589,148✔
338
      SSttBlk *p = &pArray->data[i];
10,515,891✔
339
      if (p->suid < suid) {
10,515,891✔
340
        continue;
5,677,804✔
341
      }
342

343
      if (p->suid == suid) {
4,838,087✔
344
        void *px = taosArrayPush(pTmp, p);
3,241,258✔
345
        if (px == NULL) {
3,241,258!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
1,596,792!
350
        break;
1,597,744✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
3,671,001✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
3,671,743✔
356
  }
357

358
  return code;
5,815,081✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
38,458,665✔
362
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
38,458,665!
363
    char *p = (char *)pVal->pData;
652,371✔
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
652,371!
365
    if (pBuf == NULL) {
654,198!
366
      return terrno;
×
367
    }
368

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

373
  return TSDB_CODE_SUCCESS;
38,460,492✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
7,818,500✔
385
  if (numOfBlocks <= 0) {
7,818,500✔
386
    return code;
9,545✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
9,338,713✔
390
    ++startIndex;
1,529,758✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
7,808,955✔
394
    return 0;
2,359,934✔
395
  }
396

397
  int32_t endIndex = startIndex;
5,449,021✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
10,913,226✔
399
    ++endIndex;
5,464,205✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
5,449,021✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
5,449,021✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
5,449,021✔
407
  QUERY_CHECK_CODE(code, lino, _end);
5,448,900!
408

409
  int64_t st = taosGetTimestampUs();
5,448,623✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
10,908,628✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
5,460,274✔
413
    QUERY_CHECK_CODE(code, lino, _end);
5,464,146!
414

415
    int32_t i = 0;
5,464,146✔
416
    int32_t rows = block.numOfRecords;
5,464,146✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
10,751,974✔
418
      ++i;
5,287,828✔
419
    }
420

421
    // existed
422
    if (i < rows) {
5,464,146✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
5,344,065✔
424

425
      if (pInfo->pUid == NULL) {
5,344,065✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
5,328,604✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
5,329,168✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
5,329,594✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
5,329,706✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
5,329,816✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
5,329,538✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
5,329,669!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
5,329,610!
436
          code = terrno;
206✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
5,344,924✔
442
        int32_t size = rows - i;
3,982,493✔
443
        int32_t offset = i * sizeof(int64_t);
3,982,493✔
444

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

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
3,982,123✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,981,651!
450

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

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
3,981,171✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,980,524✔
456

457
        if (block.numOfPKs > 0) {
3,980,441✔
458
          SValue vFirst = {0}, vLast = {0};
493,488✔
459
          for (int32_t f = i; f < rows; ++f) {
19,653,866✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
19,159,658✔
461
            TSDB_CHECK_CODE(code, lino, _end);
19,161,140!
462

463
            code = tValueDupPayload(&vFirst);
19,161,140✔
464
            TSDB_CHECK_CODE(code, lino, _end);
19,161,294!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
19,161,294✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
19,160,883!
468

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

473
            code = tValueDupPayload(&vLast);
19,161,390✔
474
            TSDB_CHECK_CODE(code, lino, _end);
19,160,721!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
19,160,721✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
19,160,378!
478
          }
479
        } else {
480
          SValue vFirst = {0};
3,486,953✔
481
          for (int32_t j = 0; j < size; ++j) {
19,004,546✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
15,521,309✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,505,047!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
15,505,047✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,517,593!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
1,362,431✔
491
        while (i < rows) {
3,234,095✔
492
          code = tStatisBlockGet(&block, i, &record);
3,234,046✔
493
          TSDB_CHECK_CODE(code, lino, _end);
3,233,945!
494

495
          if (record.suid != suid) {
3,233,945✔
496
            break;
1,362,430✔
497
          }
498

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

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

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

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

511
          if (record.firstKey.numOfPKs > 0) {
1,871,634✔
512
            SValue s = record.firstKey.pks[0];
73,214✔
513
            code = tValueDupPayload(&s);
73,214✔
514
            TSDB_CHECK_CODE(code, lino, _end);
73,231!
515

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

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
73,227✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
73,228!
525
          } else {
526
            SValue v = {0};
1,798,420✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
1,798,420✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,798,388!
529

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

534
          i += 1;
1,871,664✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
5,448,354✔
541
  tStatisBlockDestroy(&block);
5,448,354✔
542
  if (code != 0) {
5,449,751!
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;
5,449,244✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
5,449,244✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
7,822,106✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
7,819,306✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
7,819,306✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
7,819,306✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
7,819,306✔
562
  if (code != TSDB_CODE_SUCCESS) {
7,822,159!
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);
7,822,159✔
569
  if (code != TSDB_CODE_SUCCESS) {
7,822,266!
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;
7,822,266✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
7,822,266✔
577
  if (code != TSDB_CODE_SUCCESS) {
7,822,403!
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);
7,822,403✔
584
  if (code != TSDB_CODE_SUCCESS) {
7,826,293!
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);
7,826,293✔
590

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

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

600
  if (*pFirst == *pVal) {
15,406,147✔
601
    return 0;
5,913,923✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
9,492,224✔
604
  }
605
}
606

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

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
12,063,233✔
614
  if (index >= 0) {
12,068,792✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
5,914,732✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
5,911,417✔
617

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

620
    if (pRange->skey.numOfPKs > 0) {
5,911,381✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
763,041✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
762,948✔
623
    }
624
  }
625
}
626

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

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

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

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
16,443,813!
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) {
16,443,813✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
7,825,239✔
654
    if (code != TSDB_CODE_SUCCESS) {
7,823,275!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
16,441,849✔
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);
16,434,496✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
16,432,994✔
665
  if (pIter->iSttBlk != -1) {
16,431,270✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
6,032,842✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
6,030,325✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
6,030,325!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
5,105,998!
671
      pIter->pSttBlk = NULL;
1,142✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
6,030,325!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
924,188!
676
      pIter->pSttBlk = NULL;
582✔
677
      pIter->ignoreEarlierTs = true;
582✔
678
    }
679
  }
680

681
  return code;
16,428,753✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
7,824,828✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
7,824,828✔
686
  pIter->pReader = NULL;
7,838,615✔
687
}
7,838,615✔
688

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

693
  pIter->iSttBlk += step;
6,073,087✔
694

695
  int32_t index = -1;
6,073,087✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
6,073,087✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
6,074,658!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
319,173✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
319,167✔
700
      break;
196,893✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
122,274✔
704
      break;
37,974✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
6,073,081✔
736
  if (index != -1) {
6,073,081✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
82,258✔
738

739
    pIter->iSttBlk = index;
82,258✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
82,258✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
82,259✔
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);
5,990,823✔
746
  }
747
}
6,073,084✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
494,240,770✔
756
  if (code) {
489,613,343!
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) {
489,613,343✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
6,006,958✔
764
    if (i == -1) {
6,008,118!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
119,265✔
767
      return code;
119,265✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
504,683,110!
772
    if (pData->aUid != NULL) {
502,386,700✔
773
      if (!pIter->backward) {
499,034,365✔
774
        if (pData->aUid[i] > pIter->uid) {
455,838,375✔
775
          break;
1,168,375✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
43,195,990✔
779
          break;
186,273✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
501,032,052✔
785
    if (!pIter->backward) {               // asc
501,032,052✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
457,505,265✔
787
        break;
272,026✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
457,233,239✔
790
          continue;
8,609,692✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
448,623,547!
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
×
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
×
797
          if (ret < 0) {
×
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
43,526,787✔
804
        break;
151,332✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
43,375,455✔
807
          continue;
5,857,166✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
37,518,289!
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];
486,141,836✔
822
    if (ver < pIter->verRange.minVer) {
486,141,836!
823
      continue;
×
824
    }
825

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

831
    hasVal = true;
486,141,836✔
832
    break;
486,141,836✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
490,216,252✔
836
  return code;
490,216,252✔
837
}
838

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

846
  *hasNext = false;
502,849,248✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
502,849,248✔
850
    return code;
10,408,989✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
492,440,259✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
494,345,282!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
494,548,288✔
860

861
  while (1) {
80,520✔
862
    bool skipBlock = false;
494,628,808✔
863
    code = findNextValidRow(pIter, idStr);
494,628,808✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
491,817,136!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
491,817,136!
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) {
491,817,136✔
890
      tLDataIterNextBlock(pIter, idStr);
6,038,480✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
6,071,560✔
892
        goto _exit;
5,991,040✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
80,520!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
82,259✔
900
      if ((pBlockData == NULL) || (code != 0)) {
82,257!
901
        lino = __LINE__;
×
902
        goto _exit;
×
903
      }
904

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

910
  pIter->rInfo.suid = pBlockData->suid;
485,778,656✔
911
  pIter->rInfo.uid = pBlockData->uid;
485,778,656✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
485,778,656✔
913

914
_exit:
491,566,688✔
915
  if (code) {
491,566,688!
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);
491,843,951!
920
  return code;
491,843,951✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
61,337,508✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
61,478,865✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
61,478,865✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
61,478,865✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
61,478,865!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
61,749,075!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
61,631,170✔
933
  if (ret < 0) {
61,612,520✔
934
    return -1;
34,232,945✔
935
  } else if (ret > 0) {
27,379,575✔
936
    return 1;
10,751,125✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
16,628,450!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
16,628,450!
940

941
    if (ver1 < ver2) {
16,628,450✔
942
      return -1;
6,715,994✔
943
    } else if (ver1 > ver2) {
9,912,456!
944
      return 1;
10,401,570✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

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

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

963
  if (!pMTree->backward) {  // asc
16,891,664✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
13,887,898✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
3,003,766✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
16,889,807✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
16,889,807✔
973
  if (numOfLevels == 0) {
16,889,807✔
974
    goto _end;
669,744✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
16,220,063✔
978
  if (code) {
16,217,765!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
32,603,276✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
16,381,431✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
16,381,431✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
32,802,881✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
16,417,370✔
988

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

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
7,809,011✔
999
        if (code != TSDB_CODE_SUCCESS) {
7,819,525!
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) {
16,410,991✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
7,821,959✔
1007
        if (code != TSDB_CODE_SUCCESS) {
7,822,825!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
16,411,857✔
1013

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

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

1024
      bool hasVal = NULL;
16,427,768✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
16,427,768✔
1026
      if (code) {
16,435,328!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
16,435,328✔
1031
        tMergeTreeAddIter(pMTree, pIter);
5,903,109✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
5,900,438✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
10,532,219!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
10,536,231✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
16,221,845✔
1049

1050
_end:
669,744✔
1051
  tMergeTreeClose(pMTree);
669,744✔
1052
  return code;
669,636✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
5,902,282✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
5,902,282✔
1057
}
5,902,260✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
293,446,109✔
1065
    pInfo->blockData[0].pin = true;
170,092,198✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
170,092,198✔
1067
    return;
170,169,207✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
123,353,911!
1071
    pInfo->blockData[1].pin = true;
130,051,474✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
130,051,474✔
1073
    return;
130,066,088✔
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) {
291,744,690✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
291,744,690✔
1081
  if (pInfo->blockData[0].pin) {
291,744,690✔
1082
    pInfo->blockData[0].pin = false;
169,624,094✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
169,624,094✔
1084
    return;
169,641,013✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
122,120,596!
1088
    pInfo->blockData[1].pin = false;
129,458,014✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
129,458,014✔
1090
    return;
129,402,546✔
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) {
293,522,383✔
1097
  if (pMTree->pIter == NULL) {
293,522,383!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
293,522,383✔
1102
  pMTree->pPinnedBlockIter = pIter;
293,522,383✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
293,522,383✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
292,029,822✔
1112
  pMTree->pPinnedBlockIter = NULL;
292,029,822✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
292,029,822✔
1114
}
1115

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

1122
  *pHasNext = false;
502,240,353✔
1123
  if (pMTree->pIter) {
502,240,353✔
1124
    SLDataIter *pIter = pMTree->pIter;
485,865,668✔
1125
    bool        hasVal = false;
485,865,668✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
485,865,668✔
1127
    if (!hasVal || (code != 0)) {
485,576,189✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
5,861,017!
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;
5,861,017✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
485,576,189✔
1137
    if (pMTree->pIter && pIter) {
485,576,189✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
33,930,546✔
1139
      if (c > 0) {
33,934,888✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
6,281,832✔
1141
        pMTree->pIter = NULL;
6,581,018✔
1142
      } else if (!c) {
27,653,056!
1143
        return TSDB_CODE_INTERNAL_ERROR;
×
1144
      }
1145
    }
1146
  }
1147

1148
  if (pMTree->pIter == NULL) {
502,254,402✔
1149
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
29,278,183✔
1150
    if (pMTree->pIter) {
29,278,183✔
1151
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
12,442,324✔
1152
    }
1153
  }
1154

1155
  *pHasNext = (pMTree->pIter != NULL);
502,283,166✔
1156
  return code;
502,283,166✔
1157
}
1158

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