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

taosdata / TDengine / #4853

14 Nov 2025 08:06AM UTC coverage: 63.951% (+0.1%) from 63.812%
#4853

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

355 of 675 new or added lines in 18 files covered. (52.59%)

2781 existing lines in 25 files now uncovered.

150719 of 235679 relevant lines covered (63.95%)

117936996.02 hits per line

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

86.46
/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 "taos.h"
17
#include "tlrucache.h"
18
#include "tsdb.h"
19
#include "tsdbFSet2.h"
20
#include "tsdbMerge.h"
21
#include "tsdbReadUtil.h"
22
#include "tsdbSttFileRW.h"
23
#include "ttypes.h"
24

25
typedef struct SSttStatisCacheKey {
26
  int64_t suid;
27
  int32_t vgId;
28
  int32_t fid;
29
} SSttStatisCacheKey;
30

31
typedef struct SSttStatisCacheValue {
32
  int64_t commitTs;
33
  SArray *pLevel;  // SArray<SArray<SSttTableRowsInfo>>
34
} SSttStatisCacheValue;
35

36
typedef struct SSttStatisFileCacheInfo {
37
  SLRUCache *    pStatisFileCache;
38
  TdThreadMutex  lock;
39
} SSttStatisFileCacheInfo;
40

41
static SSttStatisFileCacheInfo statisCacheInfo;
42
static TdThreadOnce tsCacheInit = PTHREAD_ONCE_INIT;
43

44
static int32_t getSttTableRowsInfo(SSttStatisCacheValue *pValue, int32_t numOfPKs, int32_t levelIdx, int32_t fileIdx,
45
                                   SSttTableRowsInfo *pSttTableRowsInfo);
46
static int32_t buildSttTableRowsInfoKV(SMergeTreeConf *pConf, int32_t vgId, SSttStatisCacheKey *pKey,
47
                                       SSttStatisCacheValue **pValue);
48
static void    clearStatisInfoCache(SLRUCache *pStatisCache, SSttStatisCacheKey *pKey, LRUHandle **pHandle);
49
static int32_t putStatisInfoIntoCache(SLRUCache *pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue *pValue,
50
                                      const char *id);
51
static int32_t getStatisInfoFromCache(SLRUCache *pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue **pValue,
52
                                      LRUHandle **pHandle, const char *id);
53
static void    releaseCacheHandle(SLRUCache *pCache, LRUHandle **pHandle, bool lock);
54
static void    freeStatisFileItems(const void *key, size_t keyLen, void *value, void *ud);
55

56
static void tLDataIterClose2(SLDataIter *pIter);
57

58
// SLDataIter =================================================
59
int32_t tCreateSttBlockLoadInfo(STSchema *pSchema, int16_t *colList, int32_t numOfCols, SSttBlockLoadInfo **pInfo) {
214,371,770✔
60
  *pInfo = NULL;
214,371,770✔
61

62
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
214,419,453✔
63
  if (pLoadInfo == NULL) {
214,315,103✔
UNCOV
64
    return terrno;
×
65
  }
66

67
  pLoadInfo->blockData[0].sttBlockIndex = -1;
214,315,103✔
68
  pLoadInfo->blockData[1].sttBlockIndex = -1;
214,317,599✔
69

70
  pLoadInfo->currentLoadBlockIndex = 1;
214,323,765✔
71

72
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
214,342,673✔
73
  if (code) {
214,418,863✔
UNCOV
74
    taosMemoryFreeClear(pLoadInfo);
×
UNCOV
75
    return code;
×
76
  }
77

78
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
214,418,863✔
79
  if (code) {
214,401,945✔
UNCOV
80
    taosMemoryFreeClear(pLoadInfo);
×
UNCOV
81
    return code;
×
82
  }
83

84
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
214,401,945✔
85
  if (pLoadInfo->aSttBlk == NULL) {
214,434,266✔
UNCOV
86
    taosMemoryFreeClear(pLoadInfo);
×
UNCOV
87
    return terrno;
×
88
  }
89

90
  pLoadInfo->pSchema = pSchema;
214,447,239✔
91
  pLoadInfo->colIds = colList;
214,445,630✔
92
  pLoadInfo->numOfCols = numOfCols;
214,451,686✔
93

94
  *pInfo = pLoadInfo;
214,465,730✔
95
  return code;
214,443,539✔
96
}
97

98
static void freeItem(void *pValue) {
18,589,012✔
99
  SValue *p = (SValue *)pValue;
18,589,012✔
100
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
18,589,012✔
101
    taosMemoryFree(p->pData);
18,355,032✔
102
  }
103
}
18,591,109✔
104

105
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
214,385,053✔
106
  if (pLoadInfo == NULL) {
214,385,053✔
107
    return;
×
108
  }
109

110
  pLoadInfo->currentLoadBlockIndex = 1;
214,385,053✔
111

112
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
214,445,816✔
113
  tBlockDataDestroy(&pInfo->data);
214,412,883✔
114
  pInfo->sttBlockIndex = -1;
214,431,556✔
115
  pInfo->pin = false;
214,432,713✔
116

117
  pInfo = &pLoadInfo->blockData[1];
214,431,413✔
118
  tBlockDataDestroy(&pInfo->data);
214,441,516✔
119
  pInfo->sttBlockIndex = -1;
214,463,784✔
120
  pInfo->pin = false;
214,419,805✔
121

122
  taosArrayDestroy(pLoadInfo->info.pUid);
214,472,464✔
123
  taosArrayDestroy(pLoadInfo->info.pCount);
214,443,843✔
124
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
214,448,418✔
125
  taosArrayDestroy(pLoadInfo->info.pLastTs);
214,457,363✔
126

127
  if (taosArrayGetSize(pLoadInfo->info.pFirstKey) > 0) {
214,453,451✔
128
    SValue *pVal = taosArrayGet(pLoadInfo->info.pFirstKey, 0);
147,803,563✔
129
    if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
147,801,400✔
130
      taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
5,806,331✔
131
      taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
5,807,411✔
132
    } else {
133
      taosArrayDestroy(pLoadInfo->info.pFirstKey);
141,966,999✔
134
      taosArrayDestroy(pLoadInfo->info.pLastKey);
141,976,384✔
135
    }
136
  }
137

138
  pLoadInfo->info.pUid = NULL;
214,446,553✔
139
  pLoadInfo->info.pFirstKey = NULL;
214,445,529✔
140
  pLoadInfo->info.pLastKey = NULL;
214,455,870✔
141
  pLoadInfo->info.pCount = NULL;
214,448,683✔
142
  pLoadInfo->info.pFirstTs = NULL;
214,443,237✔
143
  pLoadInfo->info.pLastTs = NULL;
214,442,870✔
144

145
  taosArrayDestroy(pLoadInfo->aSttBlk);
214,440,204✔
146
  taosMemoryFree(pLoadInfo);
214,434,245✔
147
}
148

149
void destroyLDataIter(SLDataIter *pIter) {
214,471,028✔
150
  tLDataIterClose2(pIter);
214,471,028✔
151
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
214,436,838✔
152
  taosMemoryFree(pIter);
214,444,515✔
153
}
214,405,584✔
154

155
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
359,943,486✔
156
  if (pLDataIterArray == NULL) {
359,943,486✔
157
    return;
3,673,579✔
158
  }
159

160
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
356,269,907✔
161
  for (int32_t i = 0; i < numOfLevel; ++i) {
570,604,272✔
162
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
214,308,563✔
163
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
428,758,356✔
164
      SLDataIter *pIter = taosArrayGetP(pList, j);
214,482,431✔
165
      if (pIter->pBlockLoadInfo != NULL) {
214,485,350✔
166
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
214,495,170✔
167
        if (pLoadCost != NULL) {
214,483,465✔
168
          pLoadCost->loadBlocks += pCost->loadBlocks;
214,481,270✔
169
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
214,487,034✔
170
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
214,482,294✔
171
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
214,470,465✔
172
        }
173
      }
174

175
      destroyLDataIter(pIter);
214,474,344✔
176
    }
177

178
    taosArrayDestroy(pList);
214,278,051✔
179
  }
180

181
  taosArrayDestroy(pLDataIterArray);
356,295,709✔
182
}
183

184
// choose the unpinned slot to load next data block
185
static void updateBlockLoadSlot(SSttBlockLoadInfo *pLoadInfo) {
123,058,855✔
186
  int32_t nextSlotIndex = pLoadInfo->currentLoadBlockIndex ^ 1;
123,058,855✔
187
  if (pLoadInfo->blockData[nextSlotIndex].pin) {
123,132,016✔
UNCOV
188
    nextSlotIndex = nextSlotIndex ^ 1;
×
189
  }
190

191
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
123,082,942✔
192
}
123,079,286✔
193

194
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
2,147,483,647✔
195
  if (pResBlock != NULL) {
2,147,483,647✔
196
    *pResBlock = NULL;
2,147,483,647✔
197
  }
198

199
  int32_t            code = 0;
2,147,483,647✔
200
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
2,147,483,647✔
201

202
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647✔
203
    if (pInfo->currentLoadBlockIndex != 0) {
2,147,483,647✔
204
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
12,800✔
205
                ", load data, %s",
206
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
207
      pInfo->currentLoadBlockIndex = 0;
12,800✔
208
    }
209

210
    *pResBlock = &pInfo->blockData[0].data;
2,147,483,647✔
211
    return code;
2,147,483,647✔
212
  }
213

214
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647✔
215
    if (pInfo->currentLoadBlockIndex != 1) {
2,147,483,647✔
216
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
7,535✔
217
                ", load data, %s",
218
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
219
      pInfo->currentLoadBlockIndex = 1;
7,535✔
220
    }
221

222
    *pResBlock = &pInfo->blockData[1].data;
2,147,483,647✔
223
    return code;
2,147,483,647✔
224
  }
225

226
  if (pIter->pSttBlk == NULL || pInfo->pSchema == NULL) {
123,011,815✔
227
    return code;
174✔
228
  }
229

230
  updateBlockLoadSlot(pInfo);
123,081,012✔
231
  int64_t st = taosGetTimestampUs();
123,085,582✔
232

233
  SBlockData *pBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
123,085,582✔
234
  code = tsdbSttFileReadBlockDataByColumn(pIter->pReader, pIter->pSttBlk, pBlock, pInfo->pSchema, &pInfo->colIds[1],
123,072,700✔
235
                                          pInfo->numOfCols - 1);
123,033,266✔
236
  if (code != TSDB_CODE_SUCCESS) {
123,113,458✔
UNCOV
237
    return code;
×
238
  }
239

240
  double el = (taosGetTimestampUs() - st) / 1000.0;
123,117,156✔
241
  pInfo->cost.blockElapsedTime += el;
123,117,156✔
242
  pInfo->cost.loadBlocks += 1;
123,114,712✔
243

244
  tsdbDebug("read stt block, total load:%" PRId64 ", trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
123,089,462✔
245
            ", last block index:%d, entry:%d, rows:%d, uidRange:%" PRId64 "-%" PRId64 " tsRange:%" PRId64 "-%" PRId64
246
            " %p, elapsed time:%.2f ms, %s",
247
            pInfo->cost.loadBlocks, pIter->uid, pIter->cid, pIter->iSttBlk, pInfo->currentLoadBlockIndex, pBlock->nRow,
248
            pIter->pSttBlk->minUid, pIter->pSttBlk->maxUid, pIter->pSttBlk->minKey, pIter->pSttBlk->maxKey, pBlock, el,
249
            idStr);
250

251
  pInfo->blockData[pInfo->currentLoadBlockIndex].sttBlockIndex = pIter->iSttBlk;
123,091,192✔
252
  pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].data.nRow : -1;
123,110,273✔
253

254
  tsdbDebug("last block index list:%d, %d, rowIndex:%d %s", pInfo->blockData[0].sttBlockIndex,
123,098,338✔
255
            pInfo->blockData[1].sttBlockIndex, pIter->iRow, idStr);
256

257
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
123,098,338✔
258
  return code;
123,073,509✔
259
}
260

261
// find the earliest block that contains the required records
262
static FORCE_INLINE int32_t findEarliestIndex(int32_t index, uint64_t uid, const SSttBlk *pBlockList, int32_t num,
263
                                              int32_t backward) {
264
  int32_t i = index;
143,988,696✔
265
  int32_t step = backward ? 1 : -1;
143,988,696✔
266
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
289,353,649✔
267
    i += step;
145,364,953✔
268
  }
269
  return i - step;
143,956,049✔
270
}
271

272
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
310,638,904✔
273
  int32_t midPos = -1;
310,638,904✔
274
  if (num <= 0) {
310,638,904✔
275
    return -1;
97,806,542✔
276
  }
277

278
  int32_t firstPos = 0;
212,832,362✔
279
  int32_t lastPos = num - 1;
212,832,362✔
280

281
  // find the first position which is bigger than the key
282
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
212,832,362✔
283
    return -1;
68,913,455✔
284
  }
285

286
  while (1) {
6,521,885✔
287
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
150,422,049✔
288
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
138,905,509✔
289
    }
290

291
    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
11,541,561✔
292
      return -1;
8,726✔
293
    }
294

295
    int32_t numOfRows = lastPos - firstPos + 1;
11,572,452✔
296
    midPos = (numOfRows >> 1u) + firstPos;
11,572,452✔
297

298
    if (uid < pBlockList[midPos].minUid) {
11,572,452✔
299
      lastPos = midPos - 1;
2,961,435✔
300
    } else if (uid > pBlockList[midPos].maxUid) {
8,610,846✔
301
      firstPos = midPos + 1;
3,560,450✔
302
    } else {
303
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
5,050,540✔
304
    }
305
  }
306
}
307

308
static FORCE_INLINE int32_t findEarliestRow(int32_t index, uint64_t uid, const uint64_t *uidList, int32_t num,
309
                                            int32_t backward) {
310
  int32_t i = index;
144,192,333✔
311
  int32_t step = backward ? 1 : -1;
144,192,333✔
312
  while (i >= 0 && i < num && uid == uidList[i]) {
2,147,483,647✔
313
    i += step;
2,147,483,647✔
314
  }
315
  return i - step;
144,223,667✔
316
}
317

318
static int32_t binarySearchForStartRowIndex(uint64_t *uidList, int32_t num, uint64_t uid, int32_t backward) {
144,295,246✔
319
  int32_t firstPos = 0;
144,295,246✔
320
  int32_t lastPos = num - 1;
144,295,246✔
321

322
  // find the first position which is bigger than the key
323
  if ((uid > uidList[lastPos]) || (uid < uidList[firstPos])) {
144,295,246✔
UNCOV
324
    return -1;
×
325
  }
326

327
  while (1) {
41,398,690✔
328
    if (uid == uidList[firstPos]) {
185,756,750✔
329
      return findEarliestRow(firstPos, uid, uidList, num, backward);
112,012,967✔
330
    }
331

332
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
73,711,419✔
333
      return -1;
103,779✔
334
    }
335

336
    int32_t numOfRows = lastPos - firstPos + 1;
73,607,961✔
337
    int32_t midPos = (numOfRows >> 1u) + firstPos;
73,607,961✔
338

339
    if (uid < uidList[midPos]) {
73,607,961✔
340
      lastPos = midPos - 1;
13,486,156✔
341
    } else if (uid > uidList[midPos]) {
60,123,803✔
342
      firstPos = midPos + 1;
27,912,534✔
343
    } else {
344
      return findEarliestRow(midPos, uid, uidList, num, backward);
32,210,700✔
345
    }
346
  }
347
}
348

349
static int32_t extractSttBlockInfo(SLDataIter *pIter, const TSttBlkArray *pArray, SSttBlockLoadInfo *pBlockLoadInfo,
214,373,214✔
350
                                   uint64_t suid) {
351
  void   *px = NULL;
214,373,214✔
352
  int32_t code = TSDB_CODE_SUCCESS;
214,373,214✔
353
  if (TARRAY2_SIZE(pArray) <= 0) {
214,373,214✔
354
    return code;
265,249✔
355
  }
356

357
  SSttBlk *pStart = &pArray->data[0];
214,146,865✔
358
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
214,137,563✔
359

360
  // all identical
361
  if (pStart->suid == pEnd->suid) {
214,127,657✔
362
    if (pStart->suid != suid) {  // no qualified stt block existed
172,749,416✔
363
      taosArrayClear(pBlockLoadInfo->aSttBlk);
50,589,817✔
364
      pIter->iSttBlk = -1;
50,590,965✔
365
      return TSDB_CODE_SUCCESS;
50,596,435✔
366
    } else {  // all blocks are qualified
367
      taosArrayClear(pBlockLoadInfo->aSttBlk);
122,138,612✔
368
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
122,207,687✔
369
      if (px == NULL) {
122,235,886✔
UNCOV
370
        return terrno;
×
371
      }
372
    }
373
  } else {
374
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
41,343,286✔
375
    if (pTmp == NULL) {
41,348,487✔
UNCOV
376
      return terrno;
×
377
    }
378

379
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
123,056,515✔
380
      SSttBlk *p = &pArray->data[i];
108,015,587✔
381
      if (p->suid < suid) {
108,012,085✔
382
        continue;
51,911,221✔
383
      }
384

385
      if (p->suid == suid) {
56,109,680✔
386
        void *px = taosArrayPush(pTmp, p);
29,796,807✔
387
        if (px == NULL) {
29,796,807✔
UNCOV
388
          code = terrno;
×
UNCOV
389
          break;
×
390
        }
391
      } else if (p->suid > suid) {
26,314,113✔
392
        break;
26,313,236✔
393
      }
394
    }
395

396
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
41,345,285✔
397
    pBlockLoadInfo->aSttBlk = pTmp;
41,348,806✔
398
  }
399

400
  return code;
163,527,284✔
401
}
402

403
static int32_t tValueDupPayload(SValue *pVal) {
57,997,212✔
404
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
57,997,212✔
405
    char *p = (char *)pVal->pData;
19,312,730✔
406
    char *pBuf = taosMemoryMalloc(pVal->nData);
19,311,306✔
407
    if (pBuf == NULL) {
19,310,831✔
UNCOV
408
      return terrno;
×
409
    }
410

411
    memcpy(pBuf, p, pVal->nData);
19,310,831✔
412
    pVal->pData = (uint8_t *)pBuf;
19,310,174✔
413
  }
414

415
  return TSDB_CODE_SUCCESS;
58,004,897✔
416
}
417

418
static int32_t loadSttStatisticsBlockData(SSttFileReader *pSttFileReader, SSttBlockLoadInfo *pBlockLoadInfo,
214,343,709✔
419
                                          TStatisBlkArray *pStatisBlkArray, uint64_t suid, const char *id) {
420
  int32_t code = TSDB_CODE_SUCCESS;
214,343,709✔
421
  int32_t lino = 0;
214,343,709✔
422
  void   *px = NULL;
214,343,709✔
423
  int32_t startIndex = 0;
214,343,709✔
424
  int32_t ret = 0;
214,343,709✔
425

426
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
214,343,709✔
427
  if (numOfBlocks <= 0) {
214,347,564✔
428
    return code;
265,249✔
429
  }
430

431
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
243,402,767✔
432
    ++startIndex;
29,320,452✔
433
  }
434

435
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
214,085,696✔
436
    return 0;
57,560,991✔
437
  }
438

439
  int32_t endIndex = startIndex;
156,509,796✔
440
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
316,547,612✔
441
    ++endIndex;
160,037,816✔
442
  }
443

444
  int32_t num = endIndex - startIndex;
156,498,945✔
445
  pBlockLoadInfo->cost.loadStatisBlocks += num;
156,498,945✔
446

447
  STbStatisBlock block;
156,541,078✔
448
  code = tStatisBlockInit(&block);
156,592,065✔
449
  QUERY_CHECK_CODE(code, lino, _end);
156,613,374✔
450

451
  int64_t st = taosGetTimestampUs();
156,628,561✔
452

453
  for (int32_t k = startIndex; k < endIndex; ++k) {
316,677,523✔
454
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
160,011,024✔
455
    QUERY_CHECK_CODE(code, lino, _end);
160,069,367✔
456

457
    int32_t i = 0;
160,069,367✔
458
    int32_t rows = block.numOfRecords;
160,069,367✔
459
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
213,519,166✔
460
      ++i;
53,449,799✔
461
    }
462

463
    // existed
464
    if (i < rows) {
160,100,204✔
465
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
151,234,043✔
466

467
      if (pInfo->pUid == NULL) {
151,221,377✔
468
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
147,781,329✔
469
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
147,777,073✔
470
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
147,789,336✔
471
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
147,781,505✔
472

473
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
147,789,662✔
474
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
147,751,332✔
475

476
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
147,753,677✔
477
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
147,786,697✔
478
          code = terrno;
7,199✔
UNCOV
479
          goto _end;
×
480
        }
481
      }
482

483
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
151,200,924✔
484
        int32_t size = rows - i;
142,279,104✔
485
        int32_t offset = i * sizeof(int64_t);
142,279,104✔
486

487
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
142,279,104✔
488
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
142,292,065✔
489

490
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
142,292,065✔
491
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
142,261,016✔
492

493
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
142,261,016✔
494
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
142,264,928✔
495

496
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
142,264,928✔
497
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
142,226,207✔
498

499
        if (block.numOfPKs > 0) {
142,226,207✔
500
          SValue vFirst = {0}, vLast = {0};
14,407,329✔
501
          for (int32_t f = i; f < rows; ++f) {
35,693,884✔
502
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
21,281,864✔
503
            TSDB_CHECK_CODE(code, lino, _end);
21,270,590✔
504

505
            code = tValueDupPayload(&vFirst);
21,270,590✔
506
            TSDB_CHECK_CODE(code, lino, _end);
21,279,753✔
507

508
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
21,279,753✔
509
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
21,284,947✔
510

511
            // todo add api to clone the original data
512
            code = tValueColumnGet(&block.lastKeyPKs[0], f, &vLast);
21,284,947✔
513
            TSDB_CHECK_CODE(code, lino, _end);
21,286,095✔
514

515
            code = tValueDupPayload(&vLast);
21,286,095✔
516
            TSDB_CHECK_CODE(code, lino, _end);
21,286,458✔
517

518
            px = taosArrayPush(pInfo->pLastKey, &vLast);
21,286,458✔
519
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
21,285,499✔
520
          }
521
        } else {
522
          SValue vFirst = {0};
127,818,878✔
523
          for (int32_t j = 0; j < size; ++j) {
1,940,311,481✔
524
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
1,812,614,803✔
525
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,812,439,276✔
526

527
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
1,812,439,276✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,812,473,054✔
529
          }
530
        }
531
      } else {
532
        STbStatisRecord record = {0};
8,960,684✔
533
        while (i < rows) {
20,714,965✔
534
          code = tStatisBlockGet(&block, i, &record);
20,716,153✔
535
          TSDB_CHECK_CODE(code, lino, _end);
20,716,934✔
536

537
          if (record.suid != suid) {
20,716,934✔
538
            break;
8,963,744✔
539
          }
540

541
          px = taosArrayPush(pInfo->pUid, &record.uid);
11,753,190✔
542
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
11,754,135✔
543

544
          px = taosArrayPush(pInfo->pCount, &record.count);
11,754,135✔
545
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
11,755,547✔
546

547
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
11,755,547✔
548
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
11,754,814✔
549

550
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
11,754,814✔
551
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
11,755,283✔
552

553
          if (record.firstKey.numOfPKs > 0) {
11,755,283✔
554
            SValue s = record.firstKey.pks[0];
7,719,344✔
555
            code = tValueDupPayload(&s);
7,719,548✔
556
            TSDB_CHECK_CODE(code, lino, _end);
7,720,001✔
557

558
            px = taosArrayPush(pInfo->pFirstKey, &s);
7,720,001✔
559
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
7,720,205✔
560

561
            s = record.lastKey.pks[0];
7,720,205✔
562
            code = tValueDupPayload(&s);
7,720,205✔
563
            TSDB_CHECK_CODE(code, lino, _end);
7,719,695✔
564

565
            px = taosArrayPush(pInfo->pLastKey, &s);
7,719,695✔
566
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
7,720,103✔
567
          } else {
568
            SValue v = {0};
4,035,939✔
569
            px = taosArrayPush(pInfo->pFirstKey, &v);
4,034,352✔
570
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
4,034,888✔
571

572
            px = taosArrayPush(pInfo->pLastKey, &v);
4,034,888✔
573
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
4,034,997✔
574
          }
575

576
          i += 1;
11,752,760✔
577
        }
578
      }
579
    }
580
  }
581

582
_end:
156,732,287✔
583
  tStatisBlockDestroy(&block);
156,603,882✔
584
  if (code != 0) {
156,624,742✔
UNCOV
585
    tsdbError("%s error happens at:%s line number: %d, code:%s", id, __func__, lino, tstrerror(code));
×
586
  } else {
587
    double el = (taosGetTimestampUs() - st) / 1000.0;
156,614,267✔
588
    pBlockLoadInfo->cost.statisElapsedTime += el;
156,614,267✔
589

590
    tsdbDebug("%s load %d statis blocks into buf, elapsed time:%.2fms", id, num, el);
156,606,371✔
591
  }
592
  return code;
156,574,805✔
593
}
594

595
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
214,305,956✔
596
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr, bool loadFromDisk) {
597
  int64_t st = taosGetTimestampUs();
214,438,364✔
598

599
  const TSttBlkArray *pSttBlkArray = NULL;
214,438,364✔
600
  pBlockLoadInfo->sttBlockLoaded = true;
214,400,291✔
601

602
  // load the stt block info for each stt-block
603
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
214,445,506✔
604
  if (code != TSDB_CODE_SUCCESS) {
214,358,471✔
UNCOV
605
    tsdbError("load stt blk failed, code:%s, %s", tstrerror(code), idStr);
×
UNCOV
606
    return code;
×
607
  }
608

609
  // load the stt block info for each stt file block
610
  code = extractSttBlockInfo(pIter, pSttBlkArray, pBlockLoadInfo, suid);
214,358,471✔
611
  if (code != TSDB_CODE_SUCCESS) {
214,381,379✔
612
    tsdbError("load stt block info failed, code:%s, %s", tstrerror(code), idStr);
28,564✔
UNCOV
613
    return code;
×
614
  }
615

616
  if (loadFromDisk) {
214,352,876✔
617
    // load stt statistics block for all stt-blocks, to decide if the data of queried table exists in current stt file
618
    TStatisBlkArray *pStatisBlkArray = NULL;
214,348,936✔
619
    code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
214,368,562✔
620
    if (code != TSDB_CODE_SUCCESS) {
214,360,618✔
NEW
621
      tsdbError("failed to load stt block statistics, code:%s, %s", tstrerror(code), idStr);
×
NEW
622
      return code;
×
623
    }
624

625
    // load statistics block for all tables in current stt file
626
    code = loadSttStatisticsBlockData(pIter->pReader, pIter->pBlockLoadInfo, pStatisBlkArray, suid, idStr);
214,360,618✔
627
    if (code != TSDB_CODE_SUCCESS) {
214,329,626✔
NEW
628
      tsdbError("failed to load stt statistics block data, code:%s, %s", tstrerror(code), idStr);
×
NEW
629
      return code;
×
630
    }
631
  } else {
632
    tsdbDebug("stt block statis info loaded from cache, %s", idStr);
3,940✔
633
  }
634

635
  code = loadTombFn(pReader1, pIter->pReader, pIter->pBlockLoadInfo);
214,431,084✔
636

637
  double el = (taosGetTimestampUs() - st) / 1000.0;
214,431,196✔
638
  tsdbDebug("load the stt file blk info completed, elapsed time:%.2fms, %s", el, idStr);
214,431,196✔
639
  return code;
214,445,357✔
640
}
641

642
static int32_t uidComparFn(const void *p1, const void *p2) {
288,941,855✔
643
  const uint64_t *pFirst = p1;
288,941,855✔
644
  const uint64_t *pVal = p2;
288,941,855✔
645

646
  if (*pFirst == *pVal) {
288,941,855✔
647
    return 0;
143,901,840✔
648
  } else {
649
    return *pFirst < *pVal ? -1 : 1;
145,133,086✔
650
  }
651
}
652

653
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
310,657,200✔
654
                                      int64_t *numOfRows) {
655
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
310,657,200✔
656
    return;
97,815,822✔
657
  }
658

659
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
212,911,602✔
660
  if (index >= 0) {
212,852,467✔
661
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
143,879,192✔
662
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
143,909,135✔
663

664
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
143,877,261✔
665

666
    if (pRange->skey.numOfPKs > 0) {
143,888,768✔
667
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
15,150,136✔
668
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
15,144,385✔
669
    }
670
  }
671
}
672

673
int32_t tLDataIterOpen2(SLDataIter *pIter, SSttFileReader *pSttFileReader, int32_t cid, int8_t backward,
310,732,400✔
674
                        SMergeTreeConf *pConf, SSttBlockLoadInfo *pBlockLoadInfo, SSttKeyRange *pKeyRange,
675
                        int64_t *numOfRows, const char *idStr, bool loadFromDisk) {
676
  int32_t code = TSDB_CODE_SUCCESS;
310,732,400✔
677

678
  pIter->uid = pConf->uid;
310,732,400✔
679
  pIter->cid = cid;
310,789,918✔
680
  pIter->backward = backward;
310,766,588✔
681
  pIter->verRange.minVer = pConf->verRange.minVer;
310,724,048✔
682
  pIter->verRange.maxVer = pConf->verRange.maxVer;
310,751,841✔
683
  pIter->timeWindow.skey = pConf->timewindow.skey;
310,663,761✔
684
  pIter->timeWindow.ekey = pConf->timewindow.ekey;
310,749,165✔
685

686
  pIter->pStartRowKey = pConf->pCurRowKey;
310,641,361✔
687
  pIter->pReader = pSttFileReader;
310,692,740✔
688
  pIter->pBlockLoadInfo = pBlockLoadInfo;
310,693,917✔
689

690
  // open stt file failed, ignore and continue
691
  if (pIter->pReader == NULL) {
310,626,602✔
UNCOV
692
    tsdbError("stt file reader is null, %s", idStr);
×
UNCOV
693
    pIter->pSttBlk = NULL;
×
UNCOV
694
    pIter->iSttBlk = -1;
×
UNCOV
695
    return TSDB_CODE_SUCCESS;
×
696
  }
697

698
  if (!pBlockLoadInfo->sttBlockLoaded) {
310,588,594✔
699
      code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr, loadFromDisk);
214,378,015✔
700
  }
701

702
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
310,668,695✔
703

704
  // find the start block, actually we could load the position to avoid repeatly searching for the start position when
705
  // the skey is updated.
706
  size_t size = taosArrayGetSize(pBlockLoadInfo->aSttBlk);
310,643,705✔
707
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
310,703,192✔
708
  if (pIter->iSttBlk != -1) {
310,702,669✔
709
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
143,965,661✔
710
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
143,965,345✔
711

712
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
143,954,907✔
713
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
136,306,428✔
714
      pIter->pSttBlk = NULL;
16,454✔
715
    }
716

717
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
143,919,407✔
718
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
7,655,593✔
719
      pIter->pSttBlk = NULL;
284,009✔
720
      pIter->ignoreEarlierTs = true;
186,640✔
721
    }
722
  }
723

724
  return code;
310,543,706✔
725
}
726

727
void tLDataIterClose2(SLDataIter *pIter) {
214,451,181✔
728
  tsdbSttFileReaderClose(&pIter->pReader);
214,451,181✔
729
  pIter->pReader = NULL;
214,437,738✔
730
}
214,439,884✔
731

732
void tLDataIterNextBlock(SLDataIter *pIter, const char *idStr) {
147,667,210✔
733
  int32_t step = pIter->backward ? -1 : 1;
147,667,210✔
734
  int32_t oldIndex = pIter->iSttBlk;
147,690,926✔
735

736
  pIter->iSttBlk += step;
147,700,501✔
737

738
  int32_t index = -1;
147,666,560✔
739
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
147,666,560✔
740
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
148,182,256✔
741
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
14,356,042✔
742
    if ((!pIter->backward) && p->minUid > pIter->uid) {
14,357,148✔
743
      break;
6,543,985✔
744
    }
745

746
    if (pIter->backward && p->maxUid < pIter->uid) {
7,813,306✔
747
      break;
783,012✔
748
    }
749

750
    // check uid firstly
751
    if (p->minUid <= pIter->uid && p->maxUid >= pIter->uid) {
7,030,583✔
752
      if ((!pIter->backward) && p->minKey > pIter->timeWindow.ekey) {
7,030,583✔
753
        break;
83,705✔
754
      }
755

756
      if (pIter->backward && p->maxKey < pIter->timeWindow.skey) {
6,946,878✔
757
        break;
134✔
758
      }
759

760
      // check time range secondly
761
      if (p->minKey <= pIter->timeWindow.ekey && p->maxKey >= pIter->timeWindow.skey) {
6,947,456✔
762
        if ((!pIter->backward) && p->minVer > pIter->verRange.maxVer) {
6,445,036✔
UNCOV
763
          break;
×
764
        }
765

766
        if (pIter->backward && p->maxVer < pIter->verRange.minVer) {
6,445,036✔
UNCOV
767
          break;
×
768
        }
769

770
        if (p->minVer <= pIter->verRange.maxVer && p->maxVer >= pIter->verRange.minVer) {
6,445,036✔
771
          index = i;
6,445,036✔
772
          break;
6,445,036✔
773
        }
774
      }
775
    }
776
  }
777

778
  pIter->pSttBlk = NULL;
147,725,250✔
779
  if (index != -1) {
147,646,352✔
780
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
6,445,036✔
781

782
    pIter->iSttBlk = index;
6,445,036✔
783
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
6,445,036✔
784
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
6,444,963✔
785
              ", uidRange:%" PRId64 "-%" PRId64 " %s",
786
              pIter->iSttBlk, oldIndex, pIter->uid, pIter->cid, p->minUid, p->maxUid, idStr);
787
  } else {
788
    tsdbDebug("no more last block qualified, uid:%" PRIu64 ", stt-file block:%d, %s", pIter->uid, oldIndex, idStr);
141,201,316✔
789
  }
790
}
147,646,279✔
791

792
static int32_t findNextValidRow(SLDataIter *pIter, const char *idStr) {
2,147,483,647✔
793
  bool        hasVal = false;
2,147,483,647✔
794
  int32_t     step = pIter->backward ? -1 : 1;
2,147,483,647✔
795
  int32_t     i = pIter->iRow;
2,147,483,647✔
796
  SBlockData *pData = NULL;
2,147,483,647✔
797

798
  int32_t code = loadLastBlock(pIter, idStr, &pData);
2,147,483,647✔
799
  if (code) {
2,147,483,647✔
UNCOV
800
    tsdbError("failed to load stt block, code:%s, %s", tstrerror(code), idStr);
×
UNCOV
801
    return code;
×
802
  }
803

804
  // mostly we only need to find the start position for a given table
805
  if ((((i == 0) && (!pIter->backward)) || (i == pData->nRow - 1 && pIter->backward)) && pData->aUid != NULL) {
2,147,483,647✔
806
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
144,300,703✔
807
    if (i == -1) {
144,280,227✔
808
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
103,779✔
809
      pIter->iRow = -1;
103,779✔
810
      return code;
103,779✔
811
    }
812
  }
813

814
  for (; i < pData->nRow && i >= 0; i += step) {
2,147,483,647✔
815
    if (pData->aUid != NULL) {
2,147,483,647✔
816
      if (!pIter->backward) {
2,147,483,647✔
817
        if (pData->aUid[i] > pIter->uid) {
2,147,483,647✔
818
          break;
28,986,082✔
819
        }
820
      } else {
821
        if (pData->aUid[i] < pIter->uid) {
527,741,264✔
822
          break;
2,453,480✔
823
        }
824
      }
825
    }
826

827
    int64_t ts = pData->aTSKEY[i];
2,147,483,647✔
828
    if (!pIter->backward) {               // asc
2,147,483,647✔
829
      if (ts > pIter->timeWindow.ekey) {  // no more data
2,147,483,647✔
830
        break;
670,694✔
831
      } else {
832
        if (ts < pIter->timeWindow.skey) {
2,147,483,647✔
833
          continue;
1,686,157,927✔
834
        }
835

836
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
2,147,483,647✔
837
          SRowKey key;
295,792✔
838
          tColRowGetKey(pData, i, &key);
295,792✔
839
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
295,792✔
840
          if (ret < 0) {
295,792✔
UNCOV
841
            continue;
×
842
          }
843
        }
844
      }
845
    } else {
846
      if (ts < pIter->timeWindow.skey) {
542,849,050✔
847
        break;
811,306✔
848
      } else {
849
        if (ts > pIter->timeWindow.ekey) {
541,596,207✔
850
          continue;
85,943,902✔
851
        }
852

853
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
455,886,470✔
UNCOV
854
          SRowKey key;
×
UNCOV
855
          tColRowGetKey(pData, i, &key);
×
UNCOV
856
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
×
UNCOV
857
          if (ret > 0) {
×
UNCOV
858
            continue;
×
859
          }
860
        }
861
      }
862
    }
863

864
    int64_t ver = pData->aVersion[i];
2,147,483,647✔
865
    if (ver < pIter->verRange.minVer) {
2,147,483,647✔
UNCOV
866
      continue;
×
867
    }
868

869
    // todo opt handle desc case
870
    if (ver > pIter->verRange.maxVer) {
2,147,483,647✔
UNCOV
871
      continue;
×
872
    }
873

874
    hasVal = true;
2,147,483,647✔
875
    break;
2,147,483,647✔
876
  }
877

878
  pIter->iRow = (hasVal) ? i : -1;
2,147,483,647✔
879
  return code;
2,147,483,647✔
880
}
881

882
int32_t tLDataIterNextRow(SLDataIter *pIter, const char *idStr, bool *hasNext) {
2,147,483,647✔
883
  int32_t     step = pIter->backward ? -1 : 1;
2,147,483,647✔
884
  int32_t     code = 0;
2,147,483,647✔
885
  int32_t     iBlockL = pIter->iSttBlk;
2,147,483,647✔
886
  SBlockData *pBlockData = NULL;
2,147,483,647✔
887
  int32_t     lino = 0;
2,147,483,647✔
888

889
  *hasNext = false;
2,147,483,647✔
890

891
  // no qualified last file block in current file, no need to fetch row
892
  if (pIter->pSttBlk == NULL) {
2,147,483,647✔
893
    return code;
166,948,730✔
894
  }
895

896
  code = loadLastBlock(pIter, idStr, &pBlockData);
2,147,483,647✔
897
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
898
    lino = __LINE__;
101,823✔
899
    goto _exit;
101,823✔
900
  }
901

902
  pIter->iRow += step;
2,147,483,647✔
903

904
  while (1) {
6,444,116✔
905
    bool skipBlock = false;
2,147,483,647✔
906
    code = findNextValidRow(pIter, idStr);
2,147,483,647✔
907
    TSDB_CHECK_CODE(code, lino, _exit);
2,147,483,647✔
908

909
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
2,147,483,647✔
UNCOV
910
      skipBlock = true;
×
UNCOV
911
      int16_t *aCols = pIter->pBlockLoadInfo->colIds;
×
UNCOV
912
      int      nCols = pIter->pBlockLoadInfo->numOfCols;
×
UNCOV
913
      bool     isLast = pIter->pBlockLoadInfo->isLast;
×
914
      for (int inputColIndex = 0; inputColIndex < nCols; ++inputColIndex) {
×
UNCOV
915
        for (int colIndex = 0; colIndex < pBlockData->nColData; ++colIndex) {
×
UNCOV
916
          SColData *pColData = &pBlockData->aColData[colIndex];
×
UNCOV
917
          int16_t   cid = pColData->cid;
×
918

UNCOV
919
          if (cid == aCols[inputColIndex]) {
×
UNCOV
920
            if (isLast && (pColData->flag & HAS_VALUE)) {
×
UNCOV
921
              skipBlock = false;
×
UNCOV
922
              break;
×
UNCOV
923
            } else if (pColData->flag & (HAS_VALUE | HAS_NULL)) {
×
UNCOV
924
              skipBlock = false;
×
UNCOV
925
              break;
×
926
            }
927
          }
928
        }
929
      }
930
    }
931

932
    if (skipBlock || pIter->iRow >= pBlockData->nRow || pIter->iRow < 0) {
2,147,483,647✔
933
      tLDataIterNextBlock(pIter, idStr);
153,700,919✔
934
      if (pIter->pSttBlk == NULL) {  // no more data
147,630,284✔
935
        goto _exit;
141,244,759✔
936
      }
937
    } else {
938
      break;
939
    }
940

941
    if (iBlockL != pIter->iSttBlk) {
6,444,963✔
942
      code = loadLastBlock(pIter, idStr, &pBlockData);
6,444,963✔
943
      if ((pBlockData == NULL) || (code != 0)) {
6,444,225✔
UNCOV
944
        lino = __LINE__;
×
UNCOV
945
        goto _exit;
×
946
      }
947

948
      // set start row index
949
      pIter->iRow = pIter->backward ? pBlockData->nRow - 1 : 0;
6,444,334✔
950
    }
951
  }
952

953
  pIter->rInfo.suid = pBlockData->suid;
2,147,483,647✔
954
  pIter->rInfo.uid = pBlockData->uid;
2,147,483,647✔
955
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
2,147,483,647✔
956

957
_exit:
2,147,483,647✔
958
  if (code) {
2,147,483,647✔
959
    tsdbError("failed to exec stt-file nextIter, lino:%d, code:%s, %s", lino, tstrerror(code), idStr);
×
960
  }
961

962
  *hasNext = (code == TSDB_CODE_SUCCESS) && (pIter->pSttBlk != NULL) && (pBlockData != NULL);
2,147,483,647✔
963
  return code;
2,147,483,647✔
964
}
965

966
// SMergeTree =================================================
967
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
2,147,483,647✔
968
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
2,147,483,647✔
969
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
2,147,483,647✔
970

971
  SRowKey rkey1 = {0}, rkey2 = {0};
2,147,483,647✔
972
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
2,147,483,647✔
973
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
2,147,483,647✔
974

975
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
2,147,483,647✔
976
  if (ret < 0) {
2,147,483,647✔
977
    return -1;
1,391,736,155✔
978
  } else if (ret > 0) {
2,147,483,647✔
979
    return 1;
1,884,674,670✔
980
  } else {
981
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
2,147,483,647✔
982
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
2,147,483,647✔
983

984
    if (ver1 < ver2) {
2,147,483,647✔
985
      return -1;
1,252,951,636✔
986
    } else if (ver1 > ver2) {
1,884,972,248✔
987
      return 1;
1,884,977,856✔
988
    } else {
UNCOV
989
      return 0;
×
990
    }
991
  }
992
}
993

994
static FORCE_INLINE int32_t tLDataIterDescCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
27,272,738✔
995
  return -1 * tLDataIterCmprFn(p1, p2);
27,272,738✔
996
}
997

998
static void clearTableRowsInfoCache(void) {
137,230✔
999
  int32_t items = (statisCacheInfo.pStatisFileCache != NULL)? taosLRUCacheGetElems(statisCacheInfo.pStatisFileCache):0;
137,230✔
1000
  tsdbInfo("start to free %d items in statisCache", items);
137,230✔
1001

1002
  taosLRUCacheCleanup(statisCacheInfo.pStatisFileCache);
137,230✔
1003
  (void)taosThreadMutexDestroy(&statisCacheInfo.lock);
137,230✔
1004
}
137,230✔
1005

1006
// init the statis file cache, 10MiB by default
1007
static void initTableRowsInfoCache(void) {
137,230✔
1008
  statisCacheInfo.pStatisFileCache = taosLRUCacheInit(40 * 1024 * 1024, -1, 0.5);
137,230✔
1009
  (void)taosThreadMutexInit(&statisCacheInfo.lock, NULL);
137,230✔
1010
  (void) atexit(clearTableRowsInfoCache);
137,230✔
1011
}
137,230✔
1012

1013
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
308,469,669✔
1014
  int32_t               code = TSDB_CODE_SUCCESS;
308,469,669✔
1015
  STFileSet *           pFset = (STFileSet *)pConf->pCurrentFileset;
308,469,669✔
1016
  bool                  loadStatisFromDisk = true;
308,502,309✔
1017
  int32_t               lino = 0;
308,502,309✔
1018
  int32_t               numOfLevels = pFset->lvlArr->size;
308,502,309✔
1019
  SSttStatisCacheValue* pValue = NULL;
308,485,355✔
1020
  LRUHandle*            pHandle = NULL;
308,485,798✔
1021
  SSttStatisCacheKey    key = {.suid = pConf->suid, .fid = pFset->fid, .vgId = TD_VID(pConf->pTsdb->pVnode)};
308,484,366✔
1022

1023
  (void)taosThreadOnce(&tsCacheInit, initTableRowsInfoCache);
308,508,742✔
1024

1025
  pMTree->pIter = NULL;
308,422,824✔
1026
  pMTree->backward = pConf->backward;
308,429,713✔
1027
  pMTree->idStr = pConf->idstr;
308,464,282✔
1028

1029
  if (!pMTree->backward) {  // asc
308,437,793✔
1030
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
292,411,257✔
1031
  } else {  // desc
1032
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
16,024,117✔
1033
  }
1034

1035
  pMTree->ignoreEarlierTs = false;
308,383,778✔
1036

1037
  // no data exists, go to end
1038
  if (numOfLevels == 0) {
308,380,530✔
1039
    goto _end;
26,549,786✔
1040
  }
1041

1042
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
281,830,744✔
1043
  if (code) {
281,859,462✔
UNCOV
1044
    goto _end;
×
1045
  }
1046

1047
  if (pConf->cacheStatis) {
281,859,462✔
1048
    int32_t ret = getStatisInfoFromCache(statisCacheInfo.pStatisFileCache, &key, &pValue, &pHandle, pConf->idstr);
6,899✔
1049
    if (ret == TSDB_CODE_SUCCESS) {  // use cached statis info
6,899✔
1050
      if (pValue->commitTs == pFset->lastCommit) {
6,092✔
1051
        loadStatisFromDisk = false;
6,092✔
1052
      } else {  // release the handle ref, and then remove it from lru cache
NEW
1053
        int64_t ts = pValue->commitTs;
×
NEW
1054
        clearStatisInfoCache(statisCacheInfo.pStatisFileCache, &key, &pHandle);
×
NEW
1055
        tsdbInfo(
×
1056
            "cache expired since new commit occurs, remove the cache and load from disk, vgId:%d, fid:%d, suid:%" PRId64
1057
            ", commitTs:%" PRId64 ", new commitTs:%" PRId64,
1058
            key.vgId, key.fid, key.suid, ts, pFset->lastCommit);
1059
      }
1060
    }
1061
  }
1062

1063
  for (int32_t j = 0; j < numOfLevels; ++j) {
592,176,123✔
1064
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
310,317,217✔
1065
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
310,422,949✔
1066

1067
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
621,145,448✔
1068
      SLDataIter *pIter = taosArrayGetP(pList, i);
310,702,572✔
1069

1070
      SSttFileReader    *pSttFileReader = pIter->pReader;
310,730,698✔
1071
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
310,730,440✔
1072

1073
      // open stt file reader if not opened yet
1074
      // if failed to open this stt file, ignore the error and try next one
1075
      if (pSttFileReader == NULL) {
310,717,595✔
1076
        int32_t pgSize = pConf->pTsdb->pVnode->config.tsdbPageSize;
214,365,894✔
1077

1078
        SSttFileReaderConfig conf = {
428,882,137✔
1079
            .tsdb = pConf->pTsdb, .szPage = pgSize, .file[0] = *pSttLevel->fobjArr->data[i]->f};
214,440,582✔
1080

1081
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
214,447,881✔
1082
        if (code != TSDB_CODE_SUCCESS) {
214,358,435✔
UNCOV
1083
          tsdbError("open stt file reader error. file name %s, code %s, %s", pSttLevel->fobjArr->data[i]->fname,
×
1084
                    tstrerror(code), pMTree->idStr);
1085
        }
1086
      }
1087

1088
      if (pLoadInfo == NULL) {
310,654,681✔
1089
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
214,397,524✔
1090
        if (code != TSDB_CODE_SUCCESS) {
214,426,962✔
UNCOV
1091
          goto _end;
×
1092
        }
1093
      }
1094

1095
      if (!loadStatisFromDisk && (pLoadInfo->info.pCount == NULL)) {
310,684,119✔
1096
          code = getSttTableRowsInfo(pValue, pConf->pCurRowKey->numOfPKs, j, i, &pLoadInfo->info);
4,478✔
1097
          if (code != TSDB_CODE_SUCCESS) {
4,478✔
NEW
1098
            loadStatisFromDisk = true;  // failed to get statis info from cache, load it from stt file
×
1099
          }
1100
      }
1101

1102
      memset(pIter, 0, sizeof(SLDataIter));
310,684,119✔
1103

1104
      SSttKeyRange range = {.skey.numOfPKs = pConf->pCurRowKey->numOfPKs, .ekey.numOfPKs = pConf->pCurRowKey->numOfPKs};
310,684,119✔
1105
      int64_t      numOfRows = 0;
310,764,584✔
1106
      int64_t      cid = pSttLevel->fobjArr->data[i]->f->cid;
310,761,562✔
1107

1108
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
310,759,518✔
1109
                             pMTree->idStr, loadStatisFromDisk);
1110
      if (code != TSDB_CODE_SUCCESS) {
310,611,876✔
UNCOV
1111
        goto _end;
×
1112
      }
1113

1114
      bool hasVal = NULL;
310,611,876✔
1115
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
310,678,011✔
1116
      if (code) {
310,578,126✔
UNCOV
1117
        goto _end;
×
1118
      }
1119

1120
      if (hasVal) {
310,578,126✔
1121
        tMergeTreeAddIter(pMTree, pIter);
142,160,532✔
1122

1123
        // let's record the time window for current table of uid in the stt files
1124
        if (pSttDataInfo != NULL && numOfRows > 0) {
142,157,809✔
1125
          void *px = taosArrayPush(pSttDataInfo->pKeyRangeList, &range);
142,155,209✔
1126
          QUERY_CHECK_NULL(px, code, lino, _end, terrno);
142,220,998✔
1127

1128
          pSttDataInfo->numOfRows += numOfRows;
142,220,998✔
1129
        }
1130
      } else {
1131
        if (!pMTree->ignoreEarlierTs) {
168,417,594✔
1132
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
168,375,589✔
1133
        }
1134
      }
1135
    }
1136
  }
1137

1138
  if (pConf->cacheStatis && loadStatisFromDisk) {
281,858,906✔
1139
    SSttStatisCacheKey    k = {0};
807✔
1140
    SSttStatisCacheValue *pVal = NULL;
807✔
1141

1142
    code = buildSttTableRowsInfoKV(pConf, TD_VID(pConf->pTsdb->pVnode), &k, &pVal);
807✔
1143
    if (code == TSDB_CODE_SUCCESS) {
807✔
1144
      code = putStatisInfoIntoCache(statisCacheInfo.pStatisFileCache, &k, pVal, pConf->idstr);
807✔
1145
    }
1146
  }
1147

1148
  if (pHandle != NULL && pConf->cacheStatis) {
281,803,960✔
1149
    releaseCacheHandle(statisCacheInfo.pStatisFileCache, &pHandle, true);
6,092✔
1150
  }
1151

1152
  return code;
281,780,621✔
1153

1154
_end:
26,562,601✔
1155
  if (pHandle != NULL && pConf->cacheStatis) {
26,551,486✔
NEW
1156
    releaseCacheHandle(statisCacheInfo.pStatisFileCache, &pHandle, true);
×
1157
  }
1158

1159
  tMergeTreeClose(pMTree);
26,551,486✔
1160
  return code;
26,551,656✔
1161
}
1162

1163
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
142,145,429✔
1164
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
142,145,429✔
1165
}
142,173,180✔
1166

UNCOV
1167
bool tMergeTreeIgnoreEarlierTs(SMergeTree *pMTree) { return pMTree->ignoreEarlierTs; }
×
1168

1169
static void tLDataIterPinSttBlock(SLDataIter *pIter, const char *id) {
2,147,483,647✔
1170
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
2,147,483,647✔
1171

1172
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647✔
1173
    pInfo->blockData[0].pin = true;
2,147,483,647✔
1174
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
2,147,483,647✔
1175
    return;
2,147,483,647✔
1176
  }
1177

1178
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647✔
1179
    pInfo->blockData[1].pin = true;
2,147,483,647✔
1180
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
2,147,483,647✔
1181
    return;
2,147,483,647✔
1182
  }
1183

UNCOV
1184
  tsdbError("failed to pin any stt block, sttBlock:%d stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
×
1185
}
1186

1187
static void tLDataIterUnpinSttBlock(SLDataIter *pIter, const char *id) {
2,147,483,647✔
1188
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
2,147,483,647✔
1189
  if (pInfo->blockData[0].pin) {
2,147,483,647✔
1190
    pInfo->blockData[0].pin = false;
2,147,483,647✔
1191
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
2,147,483,647✔
1192
    return;
2,147,483,647✔
1193
  }
1194

1195
  if (pInfo->blockData[1].pin) {
2,147,483,647✔
1196
    pInfo->blockData[1].pin = false;
2,147,483,647✔
1197
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
2,147,483,647✔
1198
    return;
2,147,483,647✔
1199
  }
1200

1201
  tsdbError("failed to unpin any stt block, sttBlock:%d stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
90,470✔
1202
}
1203

1204
void tMergeTreePinSttBlock(SMergeTree *pMTree) {
2,147,483,647✔
1205
  if (pMTree->pIter == NULL) {
2,147,483,647✔
1206
    return;
×
1207
  }
1208

1209
  SLDataIter *pIter = pMTree->pIter;
2,147,483,647✔
1210
  pMTree->pPinnedBlockIter = pIter;
2,147,483,647✔
1211
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
2,147,483,647✔
1212
}
1213

1214
void tMergeTreeUnpinSttBlock(SMergeTree *pMTree) {
2,147,483,647✔
1215
  if (pMTree->pPinnedBlockIter == NULL) {
2,147,483,647✔
UNCOV
1216
    return;
×
1217
  }
1218

1219
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
2,147,483,647✔
1220
  pMTree->pPinnedBlockIter = NULL;
2,147,483,647✔
1221
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
2,147,483,647✔
1222
}
1223

1224
int32_t tMergeTreeNext(SMergeTree *pMTree, bool *pHasNext) {
2,147,483,647✔
1225
  int32_t code = 0;
2,147,483,647✔
1226
  if (pHasNext == NULL) {
2,147,483,647✔
UNCOV
1227
    return TSDB_CODE_INVALID_PARA;
×
1228
  }
1229

1230
  *pHasNext = false;
2,147,483,647✔
1231
  while (pMTree->pIter) {
2,147,483,647✔
1232
    SLDataIter *pIter = pMTree->pIter;
2,147,483,647✔
1233
    bool        hasVal = false;
2,147,483,647✔
1234
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
2,147,483,647✔
1235
    if (!hasVal || (code != 0)) {
2,147,483,647✔
1236
      if (code == TSDB_CODE_FILE_CORRUPTED) {
134,382,103✔
UNCOV
1237
        code = 0;  // suppress the file corrupt error to enable all queries within this cluster can run without failed.
×
1238
      }
1239

1240
      pMTree->pIter = NULL;
134,382,103✔
1241
    }
1242

1243
    // compare with min in RB Tree
1244
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
2,147,483,647✔
1245
    if (pMTree->pIter && pIter) {
2,147,483,647✔
1246
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
1,382,507,482✔
1247
      if (c > 0) {
1,382,492,867✔
1248
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
1,234,868,109✔
1249
        pMTree->pIter = NULL;
1,234,879,003✔
1250
      } else if (!c) {
147,624,758✔
UNCOV
1251
        continue;
×
1252
      }
1253
    }
1254

1255
    break;
2,147,483,647✔
1256
  }
1257

1258
  if (pMTree->pIter == NULL) {
2,147,483,647✔
1259
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
1,680,499,644✔
1260
    if (pMTree->pIter) {
1,680,511,639✔
1261
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
1,374,578,656✔
1262
    }
1263
  }
1264

1265
  *pHasNext = (pMTree->pIter != NULL);
2,147,483,647✔
1266
  return code;
2,147,483,647✔
1267
}
1268

1269
void tMergeTreeClose(SMergeTree *pMTree) {
625,803,198✔
1270
  pMTree->pIter = NULL;
625,803,198✔
1271
  pMTree->pPinnedBlockIter = NULL;
625,902,148✔
1272
}
625,894,105✔
1273

1274
int32_t buildSttTableRowsInfoKV(SMergeTreeConf *pConf, int32_t vgId, SSttStatisCacheKey *pKey,
807✔
1275
                                SSttStatisCacheValue **pValue) {
1276
  *pValue = taosMemoryCalloc(1, sizeof(SSttStatisCacheValue));
807✔
1277
  if (*pValue == NULL) {
807✔
NEW
1278
    return terrno;
×
1279
  }
1280

1281
  memset(pKey, 0, sizeof(SSttStatisCacheKey));
807✔
1282

1283
  STFileSet *pFset = (STFileSet *)pConf->pCurrentFileset;
807✔
1284

1285
  pKey->suid = pConf->suid;
807✔
1286
  pKey->vgId = vgId;
807✔
1287
  pKey->fid = pFset->fid;
807✔
1288

1289
  (*pValue)->commitTs = pFset->lastCommit;
807✔
1290

1291
  int32_t numOfLevels = TARRAY2_SIZE(pFset->lvlArr);
807✔
1292

1293
  (*pValue)->pLevel = taosArrayInit(numOfLevels, sizeof(void *));
807✔
1294
  if ((*pValue)->pLevel == NULL) {
807✔
NEW
1295
    return terrno;
×
1296
  }
1297

1298
  for (int32_t j = 0; j < numOfLevels; ++j) {
1,614✔
1299
    SSttLvl *pSttLevel = pFset->lvlArr->data[j];
807✔
1300
    SArray * pIterList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
807✔
1301
    if (pIterList == NULL) {
807✔
NEW
1302
      return terrno;
×
1303
    }
1304

1305
    SArray *pRowsInfoArr = taosArrayInit(TARRAY2_SIZE(pSttLevel->fobjArr), sizeof(SSttTableRowsInfo));
807✔
1306
    if (pRowsInfoArr == NULL) {
807✔
NEW
1307
      return terrno;
×
1308
    }
1309

1310
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all stt file
1,614✔
1311
      SLDataIter *       pIter = taosArrayGetP(pIterList, i);
807✔
1312
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
807✔
1313

1314
      void *px = taosArrayPush(pRowsInfoArr, &pLoadInfo->info);
807✔
1315
      if (px == NULL) {
807✔
NEW
1316
        return terrno;
×
1317
      }
1318

1319
      memset(&pLoadInfo->info, 0, sizeof(SSttTableRowsInfo));
807✔
1320
    }
1321

1322
    void *px = taosArrayPush((*pValue)->pLevel, &pRowsInfoArr);
807✔
1323
    if (px == NULL) {
807✔
NEW
1324
      return terrno;
×
1325
    }
1326
  }
1327

1328
  // todo handle memory failure
1329

1330
  return TSDB_CODE_SUCCESS;
807✔
1331
}
1332

1333
int32_t getStatisInfoFromCache(SLRUCache *pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue **pValue,
6,899✔
1334
                               LRUHandle **pHandle, const char *id) {
1335
  *pValue = NULL;
6,899✔
1336
  *pHandle = NULL;
6,899✔
1337

1338
  (void)taosThreadMutexLock(&statisCacheInfo.lock);
6,899✔
1339
  LRUHandle *pItemHandle = taosLRUCacheLookup(pCache, pKey, sizeof(SSttStatisCacheKey));
6,899✔
1340
  if (pItemHandle == NULL) {
6,899✔
1341
    (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
807✔
1342
    return TSDB_CODE_NOT_FOUND;
807✔
1343
  }
1344

1345
  void *p = taosLRUCacheValue(pCache, pItemHandle);
6,092✔
1346

1347
  *pValue = p;
6,092✔
1348
  *pHandle = pItemHandle;
6,092✔
1349

1350
  tsdbDebug("get statis info from cache suid:%" PRId64 ", vgId:%d, fid:%d, %s, commitTs:%" PRId64, pKey->suid,
6,092✔
1351
            pKey->vgId, pKey->fid, id, (*pValue)->commitTs);
1352

1353
  // (*pEntry)->hitTimes += 1;
1354
  (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
6,092✔
1355
  return TSDB_CODE_SUCCESS;
6,092✔
1356
}
1357

1358
void releaseCacheHandle(SLRUCache *pCache, LRUHandle **pHandle, bool lock) {
5,918✔
1359
  if (lock) {
5,918✔
1360
    (void)taosThreadMutexLock(&statisCacheInfo.lock);
5,918✔
1361
  }
1362

1363
  bool ret = taosLRUCacheRelease(pCache, *pHandle, false);
6,092✔
1364
  *pHandle = NULL;
6,092✔
1365

1366
  if (lock) {
6,092✔
1367
    (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
6,092✔
1368
  }
1369
}
6,092✔
1370

1371
void freeStatisFileItems(const void* key, size_t keyLen, void* value, void* ud) {
807✔
1372
  (void)ud;
1373

1374
  if (value == NULL) {
807✔
NEW
1375
    return;
×
1376
  }
1377

1378
  SSttStatisCacheKey *  pKey = (SSttStatisCacheKey *)key;
807✔
1379
  SSttStatisCacheValue *pVal = value;
807✔
1380

1381
  for(int32_t i = 0; i < taosArrayGetSize(pVal->pLevel); ++i) {
1,614✔
1382
    SArray* pInfos = taosArrayGetP(pVal->pLevel, i);
807✔
1383

1384
    for(int32_t j = 0; j < taosArrayGetSize(pInfos); ++j) {
1,614✔
1385
      SSttTableRowsInfo* p = taosArrayGet(pInfos, j);
807✔
1386
      taosArrayDestroy(p->pCount);
807✔
1387
      taosArrayDestroyEx(p->pFirstKey, freeItem);
807✔
1388
      taosArrayDestroyEx(p->pLastKey, freeItem);
807✔
1389
      taosArrayDestroy(p->pFirstTs);
807✔
1390
      taosArrayDestroy(p->pLastTs);
807✔
1391
      taosArrayDestroy(p->pUid);
807✔
1392
    }
1393

1394
    taosArrayDestroy(pInfos);
807✔
1395
  }
1396

1397
  taosArrayDestroy(pVal->pLevel);
807✔
1398
  taosMemoryFree(pVal);
807✔
1399
}
1400

1401
int32_t putStatisInfoIntoCache(SLRUCache *pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue *pValue,
807✔
1402
                               const char *id) {
1403
  (void)taosThreadMutexLock(&statisCacheInfo.lock);
807✔
1404

1405
  LRUStatus status = taosLRUCacheInsert(pCache, pKey, sizeof(SSttStatisCacheKey), pValue, sizeof(SSttStatisCacheValue),
807✔
1406
                                        freeStatisFileItems, NULL, NULL, TAOS_LRU_PRIORITY_LOW, NULL);
1407
  if (status != TAOS_LRU_STATUS_OK) {
807✔
NEW
1408
    if (status == TAOS_LRU_STATUS_FAIL) {
×
NEW
1409
      tsdbError("%s failed to insert items into statis cache, status:%d", id, status);
×
NEW
1410
      freeStatisFileItems(NULL, 0, pValue, NULL);
×
1411
    }
1412
  } else {
1413
    int32_t total = taosLRUCacheGetElems(pCache);
807✔
1414
    tsdbDebug("%s put statis info into cache, total:%d suid:%" PRId64 ", vgId:%d, fid:%d", id, total, pKey->suid,
807✔
1415
              pKey->vgId, pKey->fid);
1416
  }
1417

1418
  (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
807✔
1419
  return TSDB_CODE_SUCCESS;
807✔
1420
}
1421

NEW
1422
void clearStatisInfoCache(SLRUCache *pStatisCache, SSttStatisCacheKey *pKey, LRUHandle** pHandle) {
×
NEW
1423
  (void)taosThreadMutexLock(&statisCacheInfo.lock);
×
NEW
1424
  releaseCacheHandle(statisCacheInfo.pStatisFileCache, pHandle, false);
×
NEW
1425
  taosLRUCacheErase(pStatisCache, pKey, sizeof(SSttStatisCacheKey));
×
NEW
1426
  (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
×
NEW
1427
}
×
1428

NEW
1429
static int32_t dupPlayload(SValue *p){
×
NEW
1430
  if (p != NULL){
×
NEW
1431
    int32_t code = tValueDupPayload(p);
×
NEW
1432
    if (code != TSDB_CODE_SUCCESS) {
×
NEW
1433
      return code;
×
1434
    }
1435
  }
NEW
1436
  return 0;
×
1437
}
1438

1439
int32_t sttRowInfoDeepCopy(SSttTableRowsInfo *pDst, SSttTableRowsInfo *pInfo, int32_t numOfPKs) {
4,478✔
1440
  int32_t code = 0;
4,478✔
1441

1442
  pDst->pCount = taosArrayDup(pInfo->pCount, NULL);
4,478✔
1443
  pDst->pFirstKey = taosArrayDup(pInfo->pFirstKey, NULL);
4,478✔
1444
  pDst->pLastKey = taosArrayDup(pInfo->pLastKey, NULL);
4,478✔
1445
  pDst->pFirstTs = taosArrayDup(pInfo->pFirstTs, NULL);
4,478✔
1446
  pDst->pLastTs = taosArrayDup(pInfo->pLastTs, NULL);
4,478✔
1447
  pDst->pUid = taosArrayDup(pInfo->pUid, NULL);
4,478✔
1448
  
1449
  if (numOfPKs > 0) {
4,478✔
NEW
1450
    int32_t len = taosArrayGetSize(pDst->pCount);
×
NEW
1451
    for (int32_t i = 0; i < len; ++i) {
×
NEW
1452
      SValue *p1 = (SValue *)taosArrayGet(pDst->pFirstKey, i);
×
NEW
1453
      if (p1 == NULL) {
×
NEW
1454
        return terrno;
×
1455
      }
NEW
1456
      code = tValueDupPayload(p1);
×
NEW
1457
      if (code != TSDB_CODE_SUCCESS) {
×
NEW
1458
        return code;
×
1459
      }
NEW
1460
      SValue *p2 = (SValue *)taosArrayGet(pDst->pLastKey, i);
×
NEW
1461
      if (p2 == NULL) {
×
NEW
1462
        return terrno;
×
1463
      }
NEW
1464
      code = tValueDupPayload(p2);
×
NEW
1465
      if (code != TSDB_CODE_SUCCESS) {
×
NEW
1466
        return code;
×
1467
      }
1468
    }
1469
  }
1470

1471
  return TSDB_CODE_SUCCESS;
4,478✔
1472
}
1473

1474
int32_t getSttTableRowsInfo(SSttStatisCacheValue *pValue, int32_t numOfPKs, int32_t levelIdx, int32_t fileIdx,
4,478✔
1475
                            SSttTableRowsInfo *pRowInfo) {
1476
  if (levelIdx >= taosArrayGetSize(pValue->pLevel)) {
4,478✔
NEW
1477
    return TSDB_CODE_INVALID_PARA;
×
1478
  }
1479

1480
  SArray *pRowsInfoArr = *(SArray **)taosArrayGet(pValue->pLevel, levelIdx);
4,478✔
1481
  if (pRowsInfoArr == NULL) {
4,478✔
NEW
1482
    return TSDB_CODE_INVALID_PARA;
×
1483
  }
1484

1485
  if (fileIdx >= taosArrayGetSize(pRowsInfoArr)) {
4,478✔
NEW
1486
    return TSDB_CODE_INVALID_PARA;
×
1487
  }
1488

1489
  void* p = (SSttTableRowsInfo *)taosArrayGet(pRowsInfoArr, fileIdx);
4,478✔
1490
  if (p == NULL) {
4,478✔
NEW
1491
    return TSDB_CODE_INVALID_PARA;
×
1492
  }
1493

1494
  return sttRowInfoDeepCopy(pRowInfo, p, numOfPKs);
4,478✔
1495
}
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