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

taosdata / TDengine / #4456

05 Jul 2025 10:40AM UTC coverage: 63.212% (+2.5%) from 60.734%
#4456

push

travis-ci

web-flow
Merge pull request #31673 from taosdata/fix/huoh/taos_log

test: fix mnode create failure

159366 of 321690 branches covered (49.54%)

Branch coverage included in aggregate %.

246655 of 320626 relevant lines covered (76.93%)

14815045.54 hits per line

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

79.75
/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,038,152✔
27
  *pInfo = NULL;
7,038,152✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
7,038,152!
30
  if (pLoadInfo == NULL) {
7,043,292!
31
    return terrno;
×
32
  }
33

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

37
  pLoadInfo->currentLoadBlockIndex = 1;
7,043,292✔
38

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

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

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
7,040,219✔
52
  if (pLoadInfo->aSttBlk == NULL) {
7,041,253✔
53
    taosMemoryFreeClear(pLoadInfo);
743!
54
    return terrno;
743✔
55
  }
56

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

61
  *pInfo = pLoadInfo;
7,040,510✔
62
  return code;
7,040,510✔
63
}
64

65
static void freeItem(void *pValue) {
37,029,125✔
66
  SValue *p = (SValue *)pValue;
37,029,125✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
37,029,125!
68
    taosMemoryFree(p->pData);
682,953!
69
  }
70
}
37,050,236✔
71

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

77
  pLoadInfo->currentLoadBlockIndex = 1;
7,049,552✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
7,049,552✔
80
  tBlockDataDestroy(&pInfo->data);
7,049,552✔
81
  pInfo->sttBlockIndex = -1;
7,047,864✔
82
  pInfo->pin = false;
7,047,864✔
83

84
  pInfo = &pLoadInfo->blockData[1];
7,047,864✔
85
  tBlockDataDestroy(&pInfo->data);
7,047,864✔
86
  pInfo->sttBlockIndex = -1;
7,047,427✔
87
  pInfo->pin = false;
7,047,427✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
7,047,427✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
7,047,278✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
7,047,314✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
7,047,654✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
7,047,510✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
7,047,299✔
95

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

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
7,047,527✔
104
  taosMemoryFree(pLoadInfo);
7,048,214!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
7,040,459✔
108
  tLDataIterClose2(pIter);
7,040,459✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
7,049,923✔
110
  taosMemoryFree(pIter);
7,049,096!
111
}
7,049,824✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
12,307,484✔
114
  if (pLDataIterArray == NULL) {
12,307,484✔
115
    return;
65,944✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
12,241,540✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
19,284,326✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
7,040,019✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
14,088,726✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
7,040,815✔
123
      if (pIter->pBlockLoadInfo != NULL) {
7,039,935!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
7,040,214✔
125
        if (pLoadCost != NULL) {
7,040,214✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
7,030,602✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
7,030,602✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
7,030,602✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
7,030,602✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
7,039,935✔
134
    }
135

136
    taosArrayDestroy(pList);
7,036,539✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
12,244,307✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
4,302,435✔
150
}
4,302,435✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
947,729,499✔
153
  if (pResBlock != NULL) {
947,729,499!
154
    *pResBlock = NULL;
948,134,891✔
155
  }
156

157
  int32_t            code = 0;
947,729,499✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
947,729,499✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
947,729,499✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
653,220,776✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
436✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
436✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
653,220,776✔
169
    return code;
653,220,776✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
294,508,723!
173
    if (pInfo->currentLoadBlockIndex != 1) {
309,715,656✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
415✔
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
415✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
309,715,656✔
181
    return code;
309,715,656✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
4,303,279✔
189
  int64_t st = taosGetTimestampUs();
4,305,342✔
190

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

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

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
4,306,829✔
216
  return code;
4,306,829✔
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;
5,452,257✔
223
  int32_t step = backward ? 1 : -1;
5,452,257✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
10,952,733!
225
    i += step;
5,500,476✔
226
  }
227
  return i - step;
5,452,257✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
13,887,969✔
231
  int32_t midPos = -1;
13,887,969✔
232
  if (num <= 0) {
13,887,969✔
233
    return -1;
4,387,439✔
234
  }
235

236
  int32_t firstPos = 0;
9,500,530✔
237
  int32_t lastPos = num - 1;
9,500,530✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
9,500,530✔
241
    return -1;
4,048,139✔
242
  }
243

244
  while (1) {
243,671✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
5,696,062✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
5,374,106✔
247
    }
248

249
    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
321,956✔
250
      return -1;
134✔
251
    }
252

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

256
    if (uid < pBlockList[midPos].minUid) {
321,822✔
257
      lastPos = midPos - 1;
108,398✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
213,424✔
259
      firstPos = midPos + 1;
135,273✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
78,151✔
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,408,263✔
269
  int32_t step = backward ? 1 : -1;
5,408,263✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
93,781,993!
271
    i += step;
88,373,730✔
272
  }
273
  return i - step;
5,408,263✔
274
}
275

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

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

285
  while (1) {
1,192,439✔
286
    if (uid == uidList[firstPos]) {
6,622,626✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
4,135,710✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
2,486,916✔
291
      return -1;
21,924✔
292
    }
293

294
    int32_t numOfRows = lastPos - firstPos + 1;
2,464,992✔
295
    int32_t midPos = (numOfRows >> 1u) + firstPos;
2,464,992✔
296

297
    if (uid < uidList[midPos]) {
2,464,992✔
298
      lastPos = midPos - 1;
408,175✔
299
    } else if (uid > uidList[midPos]) {
2,056,817✔
300
      firstPos = midPos + 1;
784,264✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
1,272,553✔
303
    }
304
  }
305
}
306

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

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

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
7,019,026✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
3,393,699✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,675,318✔
322
      pIter->iSttBlk = -1;
1,674,702✔
323
      return TSDB_CODE_SUCCESS;
1,674,702✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,718,381✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,719,746✔
327
      if (px == NULL) {
1,718,387!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
3,625,327✔
333
    if (pTmp == NULL) {
3,632,090!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
12,414,022✔
338
      SSttBlk *p = &pArray->data[i];
10,185,011✔
339
      if (p->suid < suid) {
10,185,011✔
340
        continue;
5,763,523✔
341
      }
342

343
      if (p->suid == suid) {
4,421,488✔
344
        void *px = taosArrayPush(pTmp, p);
3,019,149✔
345
        if (px == NULL) {
3,019,149!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
1,402,355!
350
        break;
1,403,143✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
3,632,154✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
3,632,702✔
356
  }
357

358
  return code;
5,351,089✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
1,760,894✔
362
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
1,760,894!
363
    char *p = (char *)pVal->pData;
702,541✔
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
702,541!
365
    if (pBuf == NULL) {
703,121!
366
      return terrno;
×
367
    }
368

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

373
  return TSDB_CODE_SUCCESS;
1,761,474✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
7,027,208✔
385
  if (numOfBlocks <= 0) {
7,027,208✔
386
    return code;
7,991✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
8,472,668✔
390
    ++startIndex;
1,453,451✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
7,019,217✔
394
    return 0;
2,094,850✔
395
  }
396

397
  int32_t endIndex = startIndex;
4,924,367✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
9,863,249✔
399
    ++endIndex;
4,938,882✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
4,924,367✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
4,924,367✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
4,924,367✔
407
  QUERY_CHECK_CODE(code, lino, _end);
4,924,210!
408

409
  int64_t st = taosGetTimestampUs();
4,923,912✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
9,846,317✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
4,934,575✔
413
    QUERY_CHECK_CODE(code, lino, _end);
4,938,993!
414

415
    int32_t i = 0;
4,938,993✔
416
    int32_t rows = block.numOfRecords;
4,938,993✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
10,332,414✔
418
      ++i;
5,393,421✔
419
    }
420

421
    // existed
422
    if (i < rows) {
4,938,993✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
4,681,654✔
424

425
      if (pInfo->pUid == NULL) {
4,681,654✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
4,667,723✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
4,667,539✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
4,667,981✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
4,668,138✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
4,667,853✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
4,668,028✔
433

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

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
4,682,249✔
442
        int32_t size = rows - i;
3,647,155✔
443
        int32_t offset = i * sizeof(int64_t);
3,647,155✔
444

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

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
3,647,023✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,646,650!
450

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

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

457
        if (block.numOfPKs > 0) {
3,645,764✔
458
          SValue vFirst = {0}, vLast = {0};
375,912✔
459
          for (int32_t f = i; f < rows; ++f) {
1,167,686✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
791,166✔
461
            TSDB_CHECK_CODE(code, lino, _end);
791,342!
462

463
            code = tValueDupPayload(&vFirst);
791,342✔
464
            TSDB_CHECK_CODE(code, lino, _end);
791,577!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
791,577✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
791,654!
468

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

473
            code = tValueDupPayload(&vLast);
791,677✔
474
            TSDB_CHECK_CODE(code, lino, _end);
791,751!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
791,751✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
791,774!
478
          }
479
        } else {
480
          SValue vFirst = {0};
3,269,852✔
481
          for (int32_t j = 0; j < size; ++j) {
18,826,581✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
15,573,154✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,559,150!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
15,559,150✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,556,729!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
1,035,094✔
491
        while (i < rows) {
2,627,237✔
492
          code = tStatisBlockGet(&block, i, &record);
2,627,221✔
493
          TSDB_CHECK_CODE(code, lino, _end);
2,627,052!
494

495
          if (record.suid != suid) {
2,627,052✔
496
            break;
1,035,103✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
1,591,949✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,592,063!
501

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

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

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

511
          if (record.firstKey.numOfPKs > 0) {
1,592,028✔
512
            SValue s = record.firstKey.pks[0];
89,781✔
513
            code = tValueDupPayload(&s);
89,781✔
514
            TSDB_CHECK_CODE(code, lino, _end);
89,808!
515

516
            px = taosArrayPush(pInfo->pFirstKey, &s);
89,808✔
517
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
89,806!
518

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
89,810✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
89,807!
525
          } else {
526
            SValue v = {0};
1,502,247✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
1,502,247✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,502,319!
529

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

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

540
_end:
4,911,742✔
541
  tStatisBlockDestroy(&block);
4,911,742✔
542
  if (code != 0) {
4,924,855!
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;
4,925,240✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
4,925,240✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
7,030,794✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
7,034,076✔
556

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

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
7,034,076✔
562
  if (code != TSDB_CODE_SUCCESS) {
7,031,318!
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,031,318✔
569
  if (code != TSDB_CODE_SUCCESS) {
7,031,633!
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,031,633✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
7,031,633✔
577
  if (code != TSDB_CODE_SUCCESS) {
7,031,573!
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,031,573✔
584
  if (code != TSDB_CODE_SUCCESS) {
7,033,443!
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,033,443✔
590

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

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

600
  if (*pFirst == *pVal) {
12,065,548✔
601
    return 0;
5,444,480✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
6,621,068✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
13,905,087✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
13,905,087!
610
    return;
4,390,477✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
9,507,788✔
614
  if (index >= 0) {
9,515,115✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
5,445,245✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
5,441,899✔
617

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

620
    if (pRange->skey.numOfPKs > 0) {
5,441,414✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
790,936✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
790,688✔
623
    }
624
  }
625
}
626

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
13,905,621✔
641
  pIter->pReader = pSttFileReader;
13,905,621✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
13,905,621✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
13,905,621!
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) {
13,905,621✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
7,034,232✔
654
    if (code != TSDB_CODE_SUCCESS) {
7,031,461!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
13,902,850✔
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);
13,894,148✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
13,889,233✔
665
  if (pIter->iSttBlk != -1) {
13,889,566✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
5,465,586✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
5,463,885✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
5,463,885!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
4,703,356✔
671
      pIter->pSttBlk = NULL;
2,568✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
5,463,885!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
760,920!
676
      pIter->pSttBlk = NULL;
378✔
677
      pIter->ignoreEarlierTs = true;
378✔
678
    }
679
  }
680

681
  return code;
13,887,865✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
7,040,627✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
7,040,627✔
686
  pIter->pReader = NULL;
7,050,005✔
687
}
7,050,005✔
688

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

693
  pIter->iSttBlk += step;
5,502,074✔
694

695
  int32_t index = -1;
5,502,074✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
5,502,074✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
5,504,088!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
224,264✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
224,256✔
700
      break;
134,218✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
90,038✔
704
      break;
5,800✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
5,502,066✔
736
  if (index != -1) {
5,502,066✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
81,486✔
738

739
    pIter->iSttBlk = index;
81,483✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
81,483✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
81,484✔
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,420,580✔
746
  }
747
}
5,502,069✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
485,806,088✔
756
  if (code) {
481,877,782!
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) {
481,877,782✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
5,430,748✔
764
    if (i == -1) {
5,431,403!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
22,073✔
767
      return code;
22,073✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
497,672,983!
772
    if (pData->aUid != NULL) {
495,945,789✔
773
      if (!pIter->backward) {
492,529,351✔
774
        if (pData->aUid[i] > pIter->uid) {
459,197,428✔
775
          break;
1,153,841✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
33,331,923✔
779
          break;
226,705✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
494,565,243✔
785
    if (!pIter->backward) {               // asc
494,565,243✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
460,770,445✔
787
        break;
243,176✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
460,527,269✔
790
          continue;
8,537,746✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
451,989,523✔
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) {
33,794,798✔
804
        break;
131,565✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
33,663,233✔
807
          continue;
6,568,542✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
27,094,691!
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];
479,084,214✔
822
    if (ver < pIter->verRange.minVer) {
479,084,214!
823
      continue;
×
824
    }
825

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

831
    hasVal = true;
479,084,214✔
832
    break;
479,084,214✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
482,566,695✔
836
  return code;
482,566,695✔
837
}
838

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

846
  *hasNext = false;
492,472,265✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
492,472,265✔
850
    return code;
8,437,820✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
484,034,445✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
485,478,811!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
485,844,521✔
860

861
  while (1) {
80,179✔
862
    bool skipBlock = false;
485,924,700✔
863
    code = findNextValidRow(pIter, idStr);
485,924,700✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
483,375,801!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
483,375,801!
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) {
483,375,801!
890
      tLDataIterNextBlock(pIter, idStr);
5,222,819✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
5,500,952✔
892
        goto _exit;
5,420,769✔
893
      }
894
    } else {
895
      break;
896
    }
897

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

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

910
  pIter->rInfo.suid = pBlockData->suid;
478,152,982✔
911
  pIter->rInfo.uid = pBlockData->uid;
478,152,982✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
478,152,982✔
913

914
_exit:
483,208,038✔
915
  if (code) {
483,208,038!
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);
483,399,254!
920
  return code;
483,399,254✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
71,833,690✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
72,184,157✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
72,184,157✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
72,184,157✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
72,184,157!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
72,566,157!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
72,051,495✔
933
  if (ret < 0) {
72,071,876✔
934
    return -1;
49,267,803✔
935
  } else if (ret > 0) {
22,804,073✔
936
    return 1;
8,973,236✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
13,830,837!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
13,830,837!
940

941
    if (ver1 < ver2) {
13,830,837✔
942
      return -1;
5,640,224✔
943
    } else if (ver1 > ver2) {
8,190,613!
944
      return 1;
8,750,438✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
14,242,229✔
956
  int32_t code = TSDB_CODE_SUCCESS;
14,242,229✔
957

958
  pMTree->pIter = NULL;
14,242,229✔
959
  pMTree->backward = pConf->backward;
14,242,229✔
960
  pMTree->idStr = pConf->idstr;
14,242,229✔
961
  int32_t lino = 0;
14,242,229✔
962

963
  if (!pMTree->backward) {  // asc
14,242,229✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
11,548,274✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
2,693,955✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
14,242,532✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
14,242,532✔
973
  if (numOfLevels == 0) {
14,242,532✔
974
    goto _end;
539,918✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
13,702,614✔
978
  if (code) {
13,698,663!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
27,570,013✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
13,870,883✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
13,870,883✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
27,761,886✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
13,890,536✔
988

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

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
7,027,856✔
999
        if (code != TSDB_CODE_SUCCESS) {
7,029,728!
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) {
13,888,341✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
7,035,167✔
1007
        if (code != TSDB_CODE_SUCCESS) {
7,030,649!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
13,883,823✔
1013

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

1018
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
13,883,823✔
1019
                             pMTree->idStr);
1020
      if (code != TSDB_CODE_SUCCESS) {
13,887,734!
1021
        goto _end;
×
1022
      }
1023

1024
      bool hasVal = NULL;
13,887,734✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
13,887,734✔
1026
      if (code) {
13,895,414!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
13,895,414✔
1031
        tMergeTreeAddIter(pMTree, pIter);
5,429,049✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
5,426,626✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
8,466,365!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
8,470,032✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
13,699,130✔
1049

1050
_end:
539,918✔
1051
  tMergeTreeClose(pMTree);
539,918✔
1052
  return code;
539,856✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
5,427,557✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
5,427,557✔
1057
}
5,428,317✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
328,297,972✔
1065
    pInfo->blockData[0].pin = true;
197,952,635✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
197,952,635✔
1067
    return;
197,992,611✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
130,345,337!
1071
    pInfo->blockData[1].pin = true;
133,801,850✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
133,801,850✔
1073
    return;
133,806,874✔
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) {
322,675,320✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
322,675,320✔
1081
  if (pInfo->blockData[0].pin) {
322,675,320✔
1082
    pInfo->blockData[0].pin = false;
195,614,650✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
195,614,650✔
1084
    return;
195,633,928✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
127,060,670!
1088
    pInfo->blockData[1].pin = false;
132,357,927✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
132,357,927✔
1090
    return;
132,359,922✔
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) {
328,388,353✔
1097
  if (pMTree->pIter == NULL) {
328,388,353!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
328,388,353✔
1102
  pMTree->pPinnedBlockIter = pIter;
328,388,353✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
328,388,353✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
322,744,150✔
1112
  pMTree->pPinnedBlockIter = NULL;
322,744,150✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
322,744,150✔
1114
}
1115

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

1122
  *pHasNext = false;
492,913,847✔
1123
  while (pMTree->pIter) {
492,913,847✔
1124
    SLDataIter *pIter = pMTree->pIter;
478,435,623✔
1125
    bool        hasVal = false;
478,435,623✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
478,435,623✔
1127
    if (!hasVal || (code != 0)) {
477,614,041✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
5,384,990!
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,384,990✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
477,614,041✔
1137
    if (pMTree->pIter && pIter) {
477,614,041✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
49,299,509✔
1139
      if (c > 0) {
48,980,174✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
4,775,781✔
1141
        pMTree->pIter = NULL;
5,425,257✔
1142
      } else if (!c) {
44,204,393!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
477,944,182✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
492,422,406✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
24,995,965✔
1152
    if (pMTree->pIter) {
24,995,965✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
10,814,369✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
492,212,736✔
1158
  return code;
492,212,736✔
1159
}
1160

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