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

taosdata / TDengine / #3858

17 Apr 2025 01:40PM UTC coverage: 62.968% (+0.5%) from 62.513%
#3858

push

travis-ci

web-flow
docs(opc): add perssit data support (#30783)

156194 of 316378 branches covered (49.37%)

Branch coverage included in aggregate %.

242021 of 316027 relevant lines covered (76.58%)

19473613.85 hits per line

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

80.4
/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,188,753✔
27
  *pInfo = NULL;
7,188,753✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
7,188,753!
30
  if (pLoadInfo == NULL) {
7,194,362!
31
    return terrno;
×
32
  }
33

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

37
  pLoadInfo->currentLoadBlockIndex = 1;
7,194,362✔
38

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

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

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
7,195,838✔
52
  if (pLoadInfo->aSttBlk == NULL) {
7,194,558✔
53
    taosMemoryFreeClear(pLoadInfo);
1,130!
54
    return terrno;
1,130✔
55
  }
56

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

61
  *pInfo = pLoadInfo;
7,193,428✔
62
  return code;
7,193,428✔
63
}
64

65
static void freeItem(void *pValue) {
155,040,595✔
66
  SValue *p = (SValue *)pValue;
155,040,595✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
155,040,595!
68
    taosMemoryFree(p->pData);
444,869!
69
  }
70
}
155,060,210✔
71

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

77
  pLoadInfo->currentLoadBlockIndex = 1;
7,200,665✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
7,200,665✔
80
  tBlockDataDestroy(&pInfo->data);
7,200,665✔
81
  pInfo->sttBlockIndex = -1;
7,199,777✔
82
  pInfo->pin = false;
7,199,777✔
83

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

89
  taosArrayDestroy(pLoadInfo->info.pUid);
7,199,526✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
7,199,186✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
7,199,364✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
7,199,512✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
7,199,288✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
7,199,460✔
95

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

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
7,199,569✔
104
  taosMemoryFree(pLoadInfo);
7,199,777!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
7,193,594✔
108
  tLDataIterClose2(pIter);
7,193,594✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
7,200,816✔
110
  taosMemoryFree(pIter);
7,200,373!
111
}
7,200,918✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
12,714,419✔
114
  if (pLDataIterArray == NULL) {
12,714,419✔
115
    return;
68,435✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
12,645,984✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
19,679,353✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
7,029,158✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
14,228,848✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
7,194,532✔
123
      if (pIter->pBlockLoadInfo != NULL) {
7,193,152!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
7,193,396✔
125
        if (pLoadCost != NULL) {
7,193,396✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
7,191,139✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
7,191,139✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
7,191,139✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
7,191,139✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
7,193,152✔
134
    }
135

136
    taosArrayDestroy(pList);
7,027,401✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
12,650,195✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
4,719,343✔
150
}
4,719,343✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
991,214,278✔
153
  if (pResBlock != NULL) {
991,214,278!
154
    *pResBlock = NULL;
991,594,022✔
155
  }
156

157
  int32_t            code = 0;
991,214,278✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
991,214,278✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
991,214,278✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
690,130,198✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
261✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
261✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
690,130,198✔
169
    return code;
690,130,198✔
170
  }
171

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

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

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

188
  updateBlockLoadSlot(pInfo);
4,720,242✔
189
  int64_t st = taosGetTimestampUs();
4,721,980✔
190

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

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

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
4,722,921✔
216
  return code;
4,722,921✔
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,920,192✔
223
  int32_t step = backward ? 1 : -1;
5,920,192✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
11,885,334!
225
    i += step;
5,965,142✔
226
  }
227
  return i - step;
5,920,192✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
15,202,611✔
231
  int32_t midPos = -1;
15,202,611✔
232
  if (num <= 0) {
15,202,611✔
233
    return -1;
4,192,285✔
234
  }
235

236
  int32_t firstPos = 0;
11,010,326✔
237
  int32_t lastPos = num - 1;
11,010,326✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
11,010,326✔
241
    return -1;
5,089,954✔
242
  }
243

244
  while (1) {
1,902,018✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
7,822,390✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
5,671,200✔
247
    }
248

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

253
    int32_t numOfRows = lastPos - firstPos + 1;
2,151,010✔
254
    midPos = (numOfRows >> 1u) + firstPos;
2,151,010✔
255

256
    if (uid < pBlockList[midPos].minUid) {
2,151,010✔
257
      lastPos = midPos - 1;
876,794✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
1,274,216✔
259
      firstPos = midPos + 1;
1,025,224✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
248,992✔
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,576,853✔
269
  int32_t step = backward ? 1 : -1;
5,576,853✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
120,007,117!
271
    i += step;
114,430,264✔
272
  }
273
  return i - step;
5,576,853✔
274
}
275

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

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

285
  while (1) {
3,624,035✔
286
    if (uid == uidList[firstPos]) {
9,522,608✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
4,297,408✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
5,225,200✔
291
      return -1;
321,720✔
292
    }
293

294
    int32_t numOfRows = lastPos - firstPos + 1;
4,903,480✔
295
    int32_t midPos = (numOfRows >> 1u) + firstPos;
4,903,480✔
296

297
    if (uid < uidList[midPos]) {
4,903,480✔
298
      lastPos = midPos - 1;
1,759,455✔
299
    } else if (uid > uidList[midPos]) {
3,144,025✔
300
      firstPos = midPos + 1;
1,864,580✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
1,279,445✔
303
    }
304
  }
305
}
306

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

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

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
7,175,661✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
3,550,563✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,653,270✔
322
      pIter->iSttBlk = -1;
1,652,513✔
323
      return TSDB_CODE_SUCCESS;
1,652,513✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,897,293✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,897,822✔
327
      if (px == NULL) {
1,896,544!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
3,625,098✔
333
    if (pTmp == NULL) {
3,632,441!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
12,696,176✔
338
      SSttBlk *p = &pArray->data[i];
10,521,409✔
339
      if (p->suid < suid) {
10,521,409✔
340
        continue;
5,879,270✔
341
      }
342

343
      if (p->suid == suid) {
4,642,139✔
344
        void *px = taosArrayPush(pTmp, p);
3,185,347✔
345
        if (px == NULL) {
3,185,347!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
1,457,039!
350
        break;
1,458,022✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
3,632,789✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
3,633,214✔
356
  }
357

358
  return code;
5,529,758✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
119,027,553✔
362
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
119,027,553!
363
    char *p = (char *)pVal->pData;
454,553✔
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
454,553!
365
    if (pBuf == NULL) {
463,932!
366
      return terrno;
×
367
    }
368

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

373
  return TSDB_CODE_SUCCESS;
119,036,932✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
7,185,176✔
385
  if (numOfBlocks <= 0) {
7,185,176✔
386
    return code;
8,383✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
8,593,141✔
390
    ++startIndex;
1,416,348✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
7,176,793✔
394
    return 0;
1,952,626✔
395
  }
396

397
  int32_t endIndex = startIndex;
5,224,167✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
10,464,190✔
399
    ++endIndex;
5,240,023✔
400
  }
401

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

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
5,224,167✔
407
  QUERY_CHECK_CODE(code, lino, _end);
5,224,250!
408

409
  int64_t st = taosGetTimestampUs();
5,224,520✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
10,439,950✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
5,236,779✔
413
    QUERY_CHECK_CODE(code, lino, _end);
5,240,026!
414

415
    int32_t i = 0;
5,240,026✔
416
    int32_t rows = block.numOfRecords;
5,240,026✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
10,985,014✔
418
      ++i;
5,744,988✔
419
    }
420

421
    // existed
422
    if (i < rows) {
5,240,026✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
5,035,130✔
424

425
      if (pInfo->pUid == NULL) {
5,035,130✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
5,019,037✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
5,019,580✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
5,020,044✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
5,020,108✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
5,020,150✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
5,020,046✔
433

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

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

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

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
3,855,095✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,854,745!
450

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

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

457
        if (block.numOfPKs > 0) {
3,854,166✔
458
          SValue vFirst = {0}, vLast = {0};
536,119✔
459
          for (int32_t f = i; f < rows; ++f) {
60,014,285✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
59,476,084✔
461
            TSDB_CHECK_CODE(code, lino, _end);
59,479,258!
462

463
            code = tValueDupPayload(&vFirst);
59,479,258✔
464
            TSDB_CHECK_CODE(code, lino, _end);
59,479,805!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
59,479,805✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
59,476,735!
468

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

473
            code = tValueDupPayload(&vLast);
59,482,303✔
474
            TSDB_CHECK_CODE(code, lino, _end);
59,480,182!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
59,480,182✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
59,478,166!
478
          }
479
        } else {
480
          SValue vFirst = {0};
3,318,047✔
481
          for (int32_t j = 0; j < size; ++j) {
18,871,187✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
15,579,470✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,554,075!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
15,554,075✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,553,140!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
1,180,589✔
491
        while (i < rows) {
3,077,962✔
492
          code = tStatisBlockGet(&block, i, &record);
3,077,938✔
493
          TSDB_CHECK_CODE(code, lino, _end);
3,077,834!
494

495
          if (record.suid != suid) {
3,077,834✔
496
            break;
1,180,592✔
497
          }
498

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

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

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

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

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

516
            px = taosArrayPush(pInfo->pFirstKey, &s);
67,153✔
517
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
67,154!
518

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
67,156✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
67,152!
525
          } else {
526
            SValue v = {0};
1,830,196✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
1,830,196✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,830,200!
529

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

534
          i += 1;
1,897,373✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
5,203,171✔
541
  tStatisBlockDestroy(&block);
5,203,171✔
542
  if (code != 0) {
5,225,077!
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,224,753✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
5,224,753✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
7,185,578✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
7,189,033✔
556

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

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
7,189,033✔
562
  if (code != TSDB_CODE_SUCCESS) {
7,187,227!
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,187,227✔
569
  if (code != TSDB_CODE_SUCCESS) {
7,187,426!
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,187,426✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
7,187,426✔
577
  if (code != TSDB_CODE_SUCCESS) {
7,188,843!
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,188,843✔
584
  if (code != TSDB_CODE_SUCCESS) {
7,191,633!
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,191,633✔
590

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

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

600
  if (*pFirst == *pVal) {
16,397,908✔
601
    return 0;
5,606,224✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
10,791,684✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
15,209,145✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
15,209,145!
610
    return;
4,193,559✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
11,014,061✔
614
  if (index >= 0) {
11,017,376✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
5,606,561✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
5,604,704✔
617

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

620
    if (pRange->skey.numOfPKs > 0) {
5,604,826✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
549,350✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
549,304✔
623
    }
624
  }
625
}
626

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
15,209,060✔
641
  pIter->pReader = pSttFileReader;
15,209,060✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
15,209,060✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
15,209,060!
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) {
15,209,060✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
7,187,784✔
654
    if (code != TSDB_CODE_SUCCESS) {
7,189,581!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
15,210,857✔
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);
15,205,842✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
15,204,471✔
665
  if (pIter->iSttBlk != -1) {
15,201,856✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
5,926,997✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
5,926,134✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
5,926,134!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
5,083,046!
671
      pIter->pSttBlk = NULL;
1,473✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
5,926,134!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
842,882!
676
      pIter->pSttBlk = NULL;
409✔
677
      pIter->ignoreEarlierTs = true;
409✔
678
    }
679
  }
680

681
  return code;
15,200,993✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
7,193,235✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
7,193,235✔
686
  pIter->pReader = NULL;
7,200,855✔
687
}
7,200,855✔
688

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

693
  pIter->iSttBlk += step;
5,982,409✔
694

695
  int32_t index = -1;
5,982,409✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
5,982,409✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
5,983,992!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
543,477✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
543,468✔
700
      break;
343,628✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
199,840✔
704
      break;
105,861✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
5,982,400✔
736
  if (index != -1) {
5,982,400✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
91,757✔
738

739
    pIter->iSttBlk = index;
91,754✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
91,754✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
91,755✔
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,890,643✔
746
  }
747
}
5,982,399✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
507,129,341✔
756
  if (code) {
500,800,869!
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) {
500,800,869✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
5,899,011✔
764
    if (i == -1) {
5,899,947!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
321,758✔
767
      return code;
321,758✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
529,281,180!
772
    if (pData->aUid != NULL) {
529,024,037✔
773
      if (!pIter->backward) {
524,506,131✔
774
        if (pData->aUid[i] > pIter->uid) {
476,927,888✔
775
          break;
1,263,897✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
47,578,243✔
779
          break;
159,416✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
527,600,724✔
785
    if (!pIter->backward) {               // asc
527,600,724✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
479,248,934✔
787
        break;
243,914✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
479,005,020✔
790
          continue;
16,444,487✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
462,560,533✔
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
364!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
364✔
797
          if (ret < 0) {
364!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
48,351,790✔
804
        break;
159,407✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
48,192,383✔
807
          continue;
11,704,403✔
808
        }
809

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

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

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

831
    hasVal = true;
499,048,513✔
832
    break;
499,048,513✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
501,132,290✔
836
  return code;
501,132,290✔
837
}
838

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

846
  *hasNext = false;
513,576,728✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
513,576,728✔
850
    return code;
9,282,596✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
504,294,132✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
507,296,201!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
507,514,144✔
860

861
  while (1) {
90,595✔
862
    bool skipBlock = false;
507,604,739✔
863
    code = findNextValidRow(pIter, idStr);
507,604,739✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
505,026,856!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
505,026,856!
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) {
505,026,856✔
890
      tLDataIterNextBlock(pIter, idStr);
6,003,959✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
5,981,227✔
892
        goto _exit;
5,890,633✔
893
      }
894
    } else {
895
      break;
896
    }
897

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

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

910
  pIter->rInfo.suid = pBlockData->suid;
499,022,897✔
911
  pIter->rInfo.uid = pBlockData->uid;
499,022,897✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
499,022,897✔
913

914
_exit:
504,695,586✔
915
  if (code) {
504,695,586!
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);
504,863,961!
920
  return code;
504,863,961✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
71,212,560✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
71,503,945✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
71,503,945✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
71,503,945✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
71,503,945!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
71,845,457!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
71,604,968✔
933
  if (ret < 0) {
71,626,441✔
934
    return -1;
42,639,172✔
935
  } else if (ret > 0) {
28,987,269✔
936
    return 1;
11,439,246✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
17,548,023!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
17,548,023!
940

941
    if (ver1 < ver2) {
17,548,023✔
942
      return -1;
7,068,688✔
943
    } else if (ver1 > ver2) {
10,479,335!
944
      return 1;
10,976,876✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
15,480,291✔
956
  int32_t code = TSDB_CODE_SUCCESS;
15,480,291✔
957

958
  pMTree->pIter = NULL;
15,480,291✔
959
  pMTree->backward = pConf->backward;
15,480,291✔
960
  pMTree->idStr = pConf->idstr;
15,480,291✔
961
  int32_t lino = 0;
15,480,291✔
962

963
  if (!pMTree->backward) {  // asc
15,480,291✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
13,017,079✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
2,463,212✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
15,482,999✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
15,482,999✔
973
  if (numOfLevels == 0) {
15,482,999✔
974
    goto _end;
624,095✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
14,858,904✔
978
  if (code) {
14,853,813!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
29,873,571✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
15,014,964✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
15,014,964✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
30,212,704✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
15,192,946✔
988

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

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
7,183,161✔
999
        if (code != TSDB_CODE_SUCCESS) {
7,185,461!
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) {
15,186,298✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
7,186,157✔
1007
        if (code != TSDB_CODE_SUCCESS) {
7,188,706!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
15,188,847✔
1013

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

1018
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
15,188,847✔
1019
                             pMTree->idStr);
1020
      if (code != TSDB_CODE_SUCCESS) {
15,200,667!
1021
        goto _end;
×
1022
      }
1023

1024
      bool hasVal = NULL;
15,200,667✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
15,200,667✔
1026
      if (code) {
15,204,723!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
15,204,723✔
1031
        tMergeTreeAddIter(pMTree, pIter);
5,595,228✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
5,593,907✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
9,609,495!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
9,612,469✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
14,858,607✔
1049

1050
_end:
624,095✔
1051
  tMergeTreeClose(pMTree);
624,095✔
1052
  return code;
623,918✔
1053
}
1054

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

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
295,405,718✔
1065
    pInfo->blockData[0].pin = true;
167,817,452✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
167,817,452✔
1067
    return;
167,885,760✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
127,588,266!
1071
    pInfo->blockData[1].pin = true;
134,045,285✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
134,045,285✔
1073
    return;
134,066,528✔
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) {
293,646,546✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
293,646,546✔
1081
  if (pInfo->blockData[0].pin) {
293,646,546✔
1082
    pInfo->blockData[0].pin = false;
167,165,852✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
167,165,852✔
1084
    return;
167,025,034✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
126,480,694!
1088
    pInfo->blockData[1].pin = false;
132,781,006✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
132,781,006✔
1090
    return;
132,787,955✔
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) {
296,541,871✔
1097
  if (pMTree->pIter == NULL) {
296,541,871!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
296,541,871✔
1102
  pMTree->pPinnedBlockIter = pIter;
296,541,871✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
296,541,871✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
293,683,515✔
1112
  pMTree->pPinnedBlockIter = NULL;
293,683,515✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
293,683,515✔
1114
}
1115

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

1122
  *pHasNext = false;
511,155,697✔
1123
  if (pMTree->pIter) {
511,155,697✔
1124
    SLDataIter *pIter = pMTree->pIter;
496,320,522✔
1125
    bool        hasVal = false;
496,320,522✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
496,320,522✔
1127
    if (!hasVal || (code != 0)) {
498,395,225✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
5,559,402!
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,559,402✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
498,395,225✔
1137
    if (pMTree->pIter && pIter) {
498,395,225✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
42,354,644✔
1139
      if (c > 0) {
42,204,881✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
6,072,775✔
1141
        pMTree->pIter = NULL;
6,954,116✔
1142
      } else if (!c) {
36,132,106!
1143
        return TSDB_CODE_INTERNAL_ERROR;
×
1144
      }
1145
    }
1146
  }
1147

1148
  if (pMTree->pIter == NULL) {
513,961,978✔
1149
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
27,953,175✔
1150
    if (pMTree->pIter) {
27,953,175✔
1151
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
12,516,155✔
1152
    }
1153
  }
1154

1155
  *pHasNext = (pMTree->pIter != NULL);
513,515,821✔
1156
  return code;
513,515,821✔
1157
}
1158

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