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

taosdata / TDengine / #4856

17 Nov 2025 09:53AM UTC coverage: 64.286% (+0.2%) from 64.039%
#4856

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

4657 existing lines in 112 files now uncovered.

151658 of 235910 relevant lines covered (64.29%)

116320814.6 hits per line

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

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

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

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

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

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

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

56
static void tLDataIterClose2(SLDataIter *pIter);
57

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

62
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
183,737,735✔
63
  if (pLoadInfo == NULL) {
183,645,273✔
64
    return terrno;
×
65
  }
66

67
  pLoadInfo->blockData[0].sttBlockIndex = -1;
183,645,273✔
68
  pLoadInfo->blockData[1].sttBlockIndex = -1;
183,650,404✔
69

70
  pLoadInfo->currentLoadBlockIndex = 1;
183,668,201✔
71

72
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
183,696,889✔
73
  if (code) {
183,755,392✔
74
    taosMemoryFreeClear(pLoadInfo);
×
75
    return code;
×
76
  }
77

78
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
183,755,392✔
79
  if (code) {
183,740,665✔
80
    taosMemoryFreeClear(pLoadInfo);
×
81
    return code;
×
82
  }
83

84
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
183,740,665✔
85
  if (pLoadInfo->aSttBlk == NULL) {
183,782,974✔
86
    taosMemoryFreeClear(pLoadInfo);
×
87
    return terrno;
×
88
  }
89

90
  pLoadInfo->pSchema = pSchema;
183,781,707✔
91
  pLoadInfo->colIds = colList;
183,800,098✔
92
  pLoadInfo->numOfCols = numOfCols;
183,788,928✔
93

94
  *pInfo = pLoadInfo;
183,802,731✔
95
  return code;
183,770,130✔
96
}
97

98
static void freeItem(void *pValue) {
2,147,483,647✔
99
  SValue *p = (SValue *)pValue;
2,147,483,647✔
100
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647✔
101
    taosMemoryFree(p->pData);
5,760,654✔
102
  }
103
}
2,147,483,647✔
104

105
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
183,690,036✔
106
  if (pLoadInfo == NULL) {
183,690,036✔
107
    return;
×
108
  }
109

110
  pLoadInfo->currentLoadBlockIndex = 1;
183,690,036✔
111

112
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
183,757,843✔
113
  tBlockDataDestroy(&pInfo->data);
183,721,845✔
114
  pInfo->sttBlockIndex = -1;
183,759,133✔
115
  pInfo->pin = false;
183,773,317✔
116

117
  pInfo = &pLoadInfo->blockData[1];
183,762,098✔
118
  tBlockDataDestroy(&pInfo->data);
183,736,447✔
119
  pInfo->sttBlockIndex = -1;
183,795,627✔
120
  pInfo->pin = false;
183,799,358✔
121

122
  taosArrayDestroy(pLoadInfo->info.pUid);
183,787,474✔
123
  taosArrayDestroy(pLoadInfo->info.pCount);
183,777,740✔
124
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
183,788,244✔
125
  taosArrayDestroy(pLoadInfo->info.pLastTs);
183,779,050✔
126

127
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
183,780,615✔
128
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
183,760,462✔
129

130
  pLoadInfo->info.pUid = NULL;
183,761,727✔
131
  pLoadInfo->info.pFirstKey = NULL;
183,791,692✔
132
  pLoadInfo->info.pLastKey = NULL;
183,790,899✔
133
  pLoadInfo->info.pCount = NULL;
183,791,650✔
134
  pLoadInfo->info.pFirstTs = NULL;
183,773,469✔
135
  pLoadInfo->info.pLastTs = NULL;
183,805,730✔
136

137
  taosArrayDestroy(pLoadInfo->aSttBlk);
183,800,089✔
138
  taosMemoryFree(pLoadInfo);
183,787,954✔
139
}
140

141
void destroyLDataIter(SLDataIter *pIter) {
183,797,868✔
142
  tLDataIterClose2(pIter);
183,797,868✔
143
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
183,745,754✔
144
  taosMemoryFree(pIter);
183,752,045✔
145
}
183,775,449✔
146

147
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
323,542,441✔
148
  if (pLDataIterArray == NULL) {
323,542,441✔
149
    return;
3,585,169✔
150
  }
151

152
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
319,957,272✔
153
  for (int32_t i = 0; i < numOfLevel; ++i) {
503,661,075✔
154
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
183,662,891✔
155
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
367,434,698✔
156
      SLDataIter *pIter = taosArrayGetP(pList, j);
183,810,452✔
157
      if (pIter->pBlockLoadInfo != NULL) {
183,811,360✔
158
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
183,812,521✔
159
        if (pLoadCost != NULL) {
183,812,032✔
160
          pLoadCost->loadBlocks += pCost->loadBlocks;
183,811,643✔
161
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
183,809,897✔
162
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
183,805,167✔
163
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
183,805,794✔
164
        }
165
      }
166

167
      destroyLDataIter(pIter);
183,794,881✔
168
    }
169

170
    taosArrayDestroy(pList);
183,633,491✔
171
  }
172

173
  taosArrayDestroy(pLDataIterArray);
319,998,184✔
174
}
175

176
// choose the unpinned slot to load next data block
177
static void updateBlockLoadSlot(SSttBlockLoadInfo *pLoadInfo) {
121,729,071✔
178
  int32_t nextSlotIndex = pLoadInfo->currentLoadBlockIndex ^ 1;
121,729,071✔
179
  if (pLoadInfo->blockData[nextSlotIndex].pin) {
121,823,687✔
UNCOV
180
    nextSlotIndex = nextSlotIndex ^ 1;
×
181
  }
182

183
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
121,816,055✔
184
}
121,764,449✔
185

186
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
2,147,483,647✔
187
  if (pResBlock != NULL) {
2,147,483,647✔
188
    *pResBlock = NULL;
2,147,483,647✔
189
  }
190

191
  int32_t            code = 0;
2,147,483,647✔
192
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
2,147,483,647✔
193

194
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647✔
195
    if (pInfo->currentLoadBlockIndex != 0) {
2,147,483,647✔
196
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
35,498✔
197
                ", load data, %s",
198
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
199
      pInfo->currentLoadBlockIndex = 0;
35,498✔
200
    }
201

202
    *pResBlock = &pInfo->blockData[0].data;
2,147,483,647✔
203
    return code;
2,147,483,647✔
204
  }
205

206
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647✔
207
    if (pInfo->currentLoadBlockIndex != 1) {
2,147,483,647✔
208
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
40,988✔
209
                ", load data, %s",
210
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
211
      pInfo->currentLoadBlockIndex = 1;
40,988✔
212
    }
213

214
    *pResBlock = &pInfo->blockData[1].data;
2,147,483,647✔
215
    return code;
2,147,483,647✔
216
  }
217

218
  if (pIter->pSttBlk == NULL || pInfo->pSchema == NULL) {
121,722,805✔
UNCOV
219
    return code;
×
220
  }
221

222
  updateBlockLoadSlot(pInfo);
121,804,432✔
223
  int64_t st = taosGetTimestampUs();
121,838,147✔
224

225
  SBlockData *pBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
121,838,147✔
226
  code = tsdbSttFileReadBlockDataByColumn(pIter->pReader, pIter->pSttBlk, pBlock, pInfo->pSchema, &pInfo->colIds[1],
121,785,260✔
227
                                          pInfo->numOfCols - 1);
121,840,189✔
228
  if (code != TSDB_CODE_SUCCESS) {
121,805,951✔
UNCOV
229
    return code;
×
230
  }
231

232
  double el = (taosGetTimestampUs() - st) / 1000.0;
121,832,638✔
233
  pInfo->cost.blockElapsedTime += el;
121,832,638✔
234
  pInfo->cost.loadBlocks += 1;
121,834,804✔
235

236
  tsdbDebug("read stt block, total load:%" PRId64 ", trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
121,845,001✔
237
            ", last block index:%d, entry:%d, rows:%d, uidRange:%" PRId64 "-%" PRId64 " tsRange:%" PRId64 "-%" PRId64
238
            " %p, elapsed time:%.2f ms, %s",
239
            pInfo->cost.loadBlocks, pIter->uid, pIter->cid, pIter->iSttBlk, pInfo->currentLoadBlockIndex, pBlock->nRow,
240
            pIter->pSttBlk->minUid, pIter->pSttBlk->maxUid, pIter->pSttBlk->minKey, pIter->pSttBlk->maxKey, pBlock, el,
241
            idStr);
242

243
  pInfo->blockData[pInfo->currentLoadBlockIndex].sttBlockIndex = pIter->iSttBlk;
121,846,224✔
244
  pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].data.nRow : -1;
121,861,560✔
245

246
  tsdbDebug("last block index list:%d, %d, rowIndex:%d %s", pInfo->blockData[0].sttBlockIndex,
121,843,864✔
247
            pInfo->blockData[1].sttBlockIndex, pIter->iRow, idStr);
248

249
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
121,844,033✔
250
  return code;
121,772,038✔
251
}
252

253
// find the earliest block that contains the required records
254
static FORCE_INLINE int32_t findEarliestIndex(int32_t index, uint64_t uid, const SSttBlk *pBlockList, int32_t num,
255
                                              int32_t backward) {
256
  int32_t i = index;
136,941,672✔
257
  int32_t step = backward ? 1 : -1;
136,941,672✔
258
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
275,223,809✔
259
    i += step;
138,282,137✔
260
  }
261
  return i - step;
136,875,147✔
262
}
263

264
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
255,685,866✔
265
  int32_t midPos = -1;
255,685,866✔
266
  if (num <= 0) {
255,685,866✔
267
    return -1;
52,202,750✔
268
  }
269

270
  int32_t firstPos = 0;
203,483,116✔
271
  int32_t lastPos = num - 1;
203,483,116✔
272

273
  // find the first position which is bigger than the key
274
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
203,483,116✔
275
    return -1;
66,634,118✔
276
  }
277

278
  while (1) {
5,682,919✔
279
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
142,525,900✔
280
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
130,735,641✔
281
    }
282

283
    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
11,794,230✔
284
      return -1;
7,322✔
285
    }
286

287
    int32_t numOfRows = lastPos - firstPos + 1;
11,821,620✔
288
    midPos = (numOfRows >> 1u) + firstPos;
11,821,620✔
289

290
    if (uid < pBlockList[midPos].minUid) {
11,821,620✔
291
      lastPos = midPos - 1;
2,598,202✔
292
    } else if (uid > pBlockList[midPos].maxUid) {
9,223,888✔
293
      firstPos = midPos + 1;
3,084,717✔
294
    } else {
295
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
6,139,506✔
296
    }
297
  }
298
}
299

300
static FORCE_INLINE int32_t findEarliestRow(int32_t index, uint64_t uid, const uint64_t *uidList, int32_t num,
301
                                            int32_t backward) {
302
  int32_t i = index;
138,645,480✔
303
  int32_t step = backward ? 1 : -1;
138,645,480✔
304
  while (i >= 0 && i < num && uid == uidList[i]) {
2,147,483,647✔
305
    i += step;
2,147,483,647✔
306
  }
307
  return i - step;
138,639,091✔
308
}
309

310
static int32_t binarySearchForStartRowIndex(uint64_t *uidList, int32_t num, uint64_t uid, int32_t backward) {
138,688,224✔
311
  int32_t firstPos = 0;
138,688,224✔
312
  int32_t lastPos = num - 1;
138,688,224✔
313

314
  // find the first position which is bigger than the key
315
  if ((uid > uidList[lastPos]) || (uid < uidList[firstPos])) {
138,688,224✔
UNCOV
316
    return -1;
×
317
  }
318

319
  while (1) {
42,603,245✔
320
    if (uid == uidList[firstPos]) {
181,355,521✔
321
      return findEarliestRow(firstPos, uid, uidList, num, backward);
115,757,277✔
322
    }
323

324
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
65,591,924✔
325
      return -1;
109,668✔
326
    }
327

328
    int32_t numOfRows = lastPos - firstPos + 1;
65,479,298✔
329
    int32_t midPos = (numOfRows >> 1u) + firstPos;
65,479,298✔
330

331
    if (uid < uidList[midPos]) {
65,479,298✔
332
      lastPos = midPos - 1;
13,218,194✔
333
    } else if (uid > uidList[midPos]) {
52,265,397✔
334
      firstPos = midPos + 1;
29,385,051✔
335
    } else {
336
      return findEarliestRow(midPos, uid, uidList, num, backward);
22,881,814✔
337
    }
338
  }
339
}
340

341
static int32_t extractSttBlockInfo(SLDataIter *pIter, const TSttBlkArray *pArray, SSttBlockLoadInfo *pBlockLoadInfo,
183,690,494✔
342
                                   uint64_t suid) {
343
  void   *px = NULL;
183,690,494✔
344
  int32_t code = TSDB_CODE_SUCCESS;
183,690,494✔
345
  if (TARRAY2_SIZE(pArray) <= 0) {
183,690,494✔
346
    return code;
146,220✔
347
  }
348

349
  SSttBlk *pStart = &pArray->data[0];
183,604,710✔
350
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
183,602,055✔
351

352
  // all identical
353
  if (pStart->suid == pEnd->suid) {
183,563,241✔
354
    if (pStart->suid != suid) {  // no qualified stt block existed
158,867,211✔
355
      taosArrayClear(pBlockLoadInfo->aSttBlk);
34,821,450✔
356
      pIter->iSttBlk = -1;
34,814,763✔
357
      return TSDB_CODE_SUCCESS;
34,819,240✔
358
    } else {  // all blocks are qualified
359
      taosArrayClear(pBlockLoadInfo->aSttBlk);
124,046,192✔
360
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
124,036,566✔
361
      if (px == NULL) {
124,056,319✔
UNCOV
362
        return terrno;
×
363
      }
364
    }
365
  } else {
366
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
24,751,969✔
367
    if (pTmp == NULL) {
24,753,702✔
UNCOV
368
      return terrno;
×
369
    }
370

371
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
79,630,442✔
372
      SSttBlk *p = &pArray->data[i];
68,239,958✔
373
      if (p->suid < suid) {
68,238,512✔
374
        continue;
34,359,019✔
375
      }
376

377
      if (p->suid == suid) {
33,882,245✔
378
        void *px = taosArrayPush(pTmp, p);
20,518,228✔
379
        if (px == NULL) {
20,518,228✔
UNCOV
380
          code = terrno;
×
UNCOV
381
          break;
×
382
        }
383
      } else if (p->suid > suid) {
13,363,878✔
384
        break;
13,364,959✔
385
      }
386
    }
387

388
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
24,753,464✔
389
    pBlockLoadInfo->aSttBlk = pTmp;
24,755,655✔
390
  }
391

392
  return code;
148,724,848✔
393
}
394

395
static int32_t tValueDupPayload(SValue *pVal) {
20,556,504✔
396
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
20,556,504✔
397
    char *p = (char *)pVal->pData;
5,761,142✔
398
    char *pBuf = taosMemoryMalloc(pVal->nData);
5,761,142✔
399
    if (pBuf == NULL) {
5,759,514✔
UNCOV
400
      return terrno;
×
401
    }
402

403
    memcpy(pBuf, p, pVal->nData);
5,759,514✔
404
    pVal->pData = (uint8_t *)pBuf;
5,760,818✔
405
  }
406

407
  return TSDB_CODE_SUCCESS;
20,556,996✔
408
}
409

410
static int32_t loadSttStatisticsBlockData(SSttFileReader *pSttFileReader, SSttBlockLoadInfo *pBlockLoadInfo,
183,691,292✔
411
                                          TStatisBlkArray *pStatisBlkArray, uint64_t suid, const char *id) {
412
  int32_t code = TSDB_CODE_SUCCESS;
183,691,292✔
413
  int32_t lino = 0;
183,691,292✔
414
  void   *px = NULL;
183,691,292✔
415
  int32_t startIndex = 0;
183,691,292✔
416
  int32_t ret = 0;
183,691,292✔
417

418
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
183,691,292✔
419
  if (numOfBlocks <= 0) {
183,685,267✔
420
    return code;
146,220✔
421
  }
422

423
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
195,491,191✔
424
    ++startIndex;
11,952,144✔
425
  }
426

427
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
183,563,664✔
428
    return 0;
37,363,612✔
429
  }
430

431
  int32_t endIndex = startIndex;
146,244,224✔
432
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
295,010,342✔
433
    ++endIndex;
148,766,118✔
434
  }
435

436
  int32_t num = endIndex - startIndex;
146,248,958✔
437
  pBlockLoadInfo->cost.loadStatisBlocks += num;
146,248,958✔
438

439
  STbStatisBlock block;
146,235,949✔
440
  code = tStatisBlockInit(&block);
146,197,061✔
441
  QUERY_CHECK_CODE(code, lino, _end);
146,281,077✔
442

443
  int64_t st = taosGetTimestampUs();
146,277,197✔
444

445
  for (int32_t k = startIndex; k < endIndex; ++k) {
295,023,435✔
446
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
148,692,492✔
447
    QUERY_CHECK_CODE(code, lino, _end);
148,787,964✔
448

449
    int32_t i = 0;
148,787,964✔
450
    int32_t rows = block.numOfRecords;
148,787,964✔
451
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
186,964,781✔
452
      ++i;
38,176,817✔
453
    }
454

455
    // existed
456
    if (i < rows) {
148,776,428✔
457
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
144,563,405✔
458

459
      if (pInfo->pUid == NULL) {
144,564,014✔
460
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
142,049,486✔
461
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
142,039,212✔
462
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
142,043,930✔
463
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
142,046,019✔
464

465
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
142,041,013✔
466
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
142,011,856✔
467

468
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
142,038,768✔
469
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
142,052,302✔
470
          code = terrno;
27,139✔
UNCOV
471
          goto _end;
×
472
        }
473

474
        pInfo->memSize = sizeof(SSttTableRowsInfo) + sizeof(SArray) * 6;
142,033,536✔
475
      }
476

477
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
144,567,623✔
478
        int32_t size = rows - i;
139,185,566✔
479
        int32_t offset = i * sizeof(int64_t);
139,185,566✔
480

481
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
139,185,566✔
482
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
139,166,817✔
483

484
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
139,166,817✔
485
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
139,193,259✔
486

487
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
139,193,259✔
488
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
139,207,421✔
489

490
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
139,207,421✔
491
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
139,214,288✔
492

493
        pInfo->memSize += size * sizeof(int64_t) * 4;
139,214,288✔
494
        pInfo->memSize += size * sizeof(SValue) * 2;
139,193,182✔
495

496
        if (block.numOfPKs > 0) {
139,118,577✔
497
          SValue vFirst = {0}, vLast = {0};
5,642,058✔
498
          for (int32_t f = i; f < rows; ++f) {
12,672,604✔
499
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
7,031,198✔
500
            TSDB_CHECK_CODE(code, lino, _end);
7,031,198✔
501

502
            code = tValueDupPayload(&vFirst);
7,031,198✔
503
            TSDB_CHECK_CODE(code, lino, _end);
7,031,362✔
504

505
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
7,031,362✔
506
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
7,031,198✔
507

508
            // todo add api to clone the original data
509
            code = tValueColumnGet(&block.lastKeyPKs[0], f, &vLast);
7,031,198✔
510
            TSDB_CHECK_CODE(code, lino, _end);
7,031,198✔
511

512
            code = tValueDupPayload(&vLast);
7,031,198✔
513
            TSDB_CHECK_CODE(code, lino, _end);
7,031,198✔
514

515
            px = taosArrayPush(pInfo->pLastKey, &vLast);
7,031,198✔
516
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
7,031,198✔
517

518
            pInfo->memSize += (IS_VAR_DATA_TYPE(vFirst.type) || vFirst.type == TSDB_DATA_TYPE_DECIMAL)? (vFirst.nData + vLast.nData):0;
7,031,198✔
519
          }
520
        } else {
521
          SValue vFirst = {0};
133,476,519✔
522
          for (int32_t j = 0; j < size; ++j) {
1,836,449,126✔
523
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
1,703,358,212✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,702,867,944✔
525

526
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
1,702,867,944✔
527
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,702,965,052✔
528
          }
529
        }
530
      } else {
531
        STbStatisRecord record = {0};
5,364,152✔
532
        while (i < rows) {
12,496,560✔
533
          code = tStatisBlockGet(&block, i, &record);
12,496,838✔
534
          TSDB_CHECK_CODE(code, lino, _end);
12,496,383✔
535

536
          if (record.suid != suid) {
12,496,383✔
537
            break;
5,365,306✔
538
          }
539

540
          px = taosArrayPush(pInfo->pUid, &record.uid);
7,131,077✔
541
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
7,132,380✔
542

543
          px = taosArrayPush(pInfo->pCount, &record.count);
7,132,380✔
544
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
7,134,789✔
545

546
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
7,134,789✔
547
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
7,134,743✔
548

549
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
7,134,743✔
550
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
7,134,160✔
551

552
          pInfo->memSize += (sizeof(int64_t) * 4 + sizeof(SValue) * 2);
7,134,160✔
553

554
          if (record.firstKey.numOfPKs > 0) {
7,131,839✔
555
            SValue first = record.firstKey.pks[0];
3,247,544✔
556
            code = tValueDupPayload(&first);
3,247,544✔
557
            TSDB_CHECK_CODE(code, lino, _end);
3,247,544✔
558

559
            px = taosArrayPush(pInfo->pFirstKey, &first);
3,247,544✔
560
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,247,544✔
561

562
            SValue last = record.lastKey.pks[0];
3,247,544✔
563
            code = tValueDupPayload(&last);
3,247,544✔
564
            TSDB_CHECK_CODE(code, lino, _end);
3,247,544✔
565

566
            px = taosArrayPush(pInfo->pLastKey, &last);
3,247,544✔
567
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,247,544✔
568

569
            pInfo->memSize += (IS_VAR_DATA_TYPE(first.type) || first.type == TSDB_DATA_TYPE_DECIMAL)? (first.nData + last.nData):0;
3,247,544✔
570
          } else {
571
            SValue v = {0};
3,884,295✔
572
            px = taosArrayPush(pInfo->pFirstKey, &v);
3,885,172✔
573
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,885,879✔
574

575
            px = taosArrayPush(pInfo->pLastKey, &v);
3,885,879✔
576
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,886,034✔
577
          }
578

579
          i += 1;
7,132,408✔
580
        }
581
      }
582
    }
583
  }
584

585
_end:
146,376,900✔
586
  tStatisBlockDestroy(&block);
146,204,806✔
587
  if (code != 0) {
146,261,774✔
UNCOV
588
    tsdbError("%s error happens at:%s line number: %d, code:%s", id, __func__, lino, tstrerror(code));
×
589
  } else {
590
    double el = (taosGetTimestampUs() - st) / 1000.0;
146,259,142✔
591
    pBlockLoadInfo->cost.statisElapsedTime += el;
146,259,142✔
592

593
    tsdbDebug("%s load %d statis blocks into buf, elapsed time:%.2fms", id, num, el);
146,287,448✔
594
  }
595
  return code;
146,272,083✔
596
}
597

598
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
183,681,400✔
599
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr, bool loadFromDisk) {
600
  int64_t st = taosGetTimestampUs();
183,762,048✔
601

602
  const TSttBlkArray *pSttBlkArray = NULL;
183,762,048✔
603
  pBlockLoadInfo->sttBlockLoaded = true;
183,724,451✔
604

605
  // load the stt block info for each stt-block
606
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
183,777,235✔
607
  if (code != TSDB_CODE_SUCCESS) {
183,695,256✔
UNCOV
608
    tsdbError("load stt blk failed, code:%s, %s", tstrerror(code), idStr);
×
UNCOV
609
    return code;
×
610
  }
611

612
  // load the stt block info for each stt file block
613
  code = extractSttBlockInfo(pIter, pSttBlkArray, pBlockLoadInfo, suid);
183,695,256✔
614
  if (code != TSDB_CODE_SUCCESS) {
183,679,699✔
615
    tsdbError("load stt block info failed, code:%s, %s", tstrerror(code), idStr);
883✔
UNCOV
616
    return code;
×
617
  }
618

619
  if (loadFromDisk) {
183,685,310✔
620
    // load stt statistics block for all stt-blocks, to decide if the data of queried table exists in current stt file
621
    TStatisBlkArray *pStatisBlkArray = NULL;
183,681,271✔
622
    code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
183,679,352✔
623
    if (code != TSDB_CODE_SUCCESS) {
183,708,886✔
UNCOV
624
      tsdbError("failed to load stt block statistics, code:%s, %s", tstrerror(code), idStr);
×
UNCOV
625
      return code;
×
626
    }
627

628
    // load statistics block for all tables in current stt file
629
    code = loadSttStatisticsBlockData(pIter->pReader, pIter->pBlockLoadInfo, pStatisBlkArray, suid, idStr);
183,708,886✔
630
    if (code != TSDB_CODE_SUCCESS) {
183,692,043✔
UNCOV
631
      tsdbError("failed to load stt statistics block data, code:%s, %s", tstrerror(code), idStr);
×
UNCOV
632
      return code;
×
633
    }
634
  } else {
635
    tsdbDebug("stt block statis info loaded from cache, %s", idStr);
4,039✔
636
  }
637

638
  code = loadTombFn(pReader1, pIter->pReader, pIter->pBlockLoadInfo);
183,763,193✔
639

640
  double el = (taosGetTimestampUs() - st) / 1000.0;
183,777,599✔
641
  tsdbDebug("load the stt file blk info completed, elapsed time:%.2fms, %s", el, idStr);
183,777,599✔
642
  return code;
183,741,159✔
643
}
644

645
static int32_t uidComparFn(const void *p1, const void *p2) {
271,964,450✔
646
  const uint64_t *pFirst = p1;
271,964,450✔
647
  const uint64_t *pVal = p2;
271,964,450✔
648

649
  if (*pFirst == *pVal) {
271,964,450✔
650
    return 0;
136,844,797✔
651
  } else {
652
    return *pFirst < *pVal ? -1 : 1;
135,225,693✔
653
  }
654
}
655

656
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
255,683,521✔
657
                                      int64_t *numOfRows) {
658
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
255,683,521✔
659
    return;
52,203,898✔
660
  }
661

662
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
203,564,509✔
663
  if (index >= 0) {
203,444,551✔
664
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
136,794,934✔
665
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
136,853,061✔
666

667
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
136,859,911✔
668

669
    if (pRange->skey.numOfPKs > 0) {
136,850,165✔
670
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
3,627,151✔
671
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
3,630,087✔
672
    }
673
  }
674
}
675

676
int32_t tLDataIterOpen2(SLDataIter *pIter, SSttFileReader *pSttFileReader, int32_t cid, int8_t backward,
255,743,929✔
677
                        SMergeTreeConf *pConf, SSttBlockLoadInfo *pBlockLoadInfo, SSttKeyRange *pKeyRange,
678
                        int64_t *numOfRows, const char *idStr, bool loadFromDisk) {
679
  int32_t code = TSDB_CODE_SUCCESS;
255,743,929✔
680

681
  pIter->uid = pConf->uid;
255,743,929✔
682
  pIter->cid = cid;
255,794,207✔
683
  pIter->backward = backward;
255,836,290✔
684
  pIter->verRange.minVer = pConf->verRange.minVer;
255,817,083✔
685
  pIter->verRange.maxVer = pConf->verRange.maxVer;
255,754,947✔
686
  pIter->timeWindow.skey = pConf->timewindow.skey;
255,797,989✔
687
  pIter->timeWindow.ekey = pConf->timewindow.ekey;
255,735,056✔
688

689
  pIter->pStartRowKey = pConf->pCurRowKey;
255,803,366✔
690
  pIter->pReader = pSttFileReader;
255,736,007✔
691
  pIter->pBlockLoadInfo = pBlockLoadInfo;
255,827,428✔
692

693
  // open stt file failed, ignore and continue
694
  if (pIter->pReader == NULL) {
255,815,304✔
UNCOV
695
    tsdbError("stt file reader is null, %s", idStr);
×
UNCOV
696
    pIter->pSttBlk = NULL;
×
UNCOV
697
    pIter->iSttBlk = -1;
×
698
    return TSDB_CODE_SUCCESS;
×
699
  }
700

701
  if (!pBlockLoadInfo->sttBlockLoaded) {
255,726,584✔
702
      code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr, loadFromDisk);
183,691,715✔
703
  }
704

705
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
255,722,349✔
706

707
  // find the start block, actually we could load the position to avoid repeatly searching for the start position when
708
  // the skey is updated.
709
  size_t size = taosArrayGetSize(pBlockLoadInfo->aSttBlk);
255,708,985✔
710
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
255,790,306✔
711
  if (pIter->iSttBlk != -1) {
255,693,798✔
712
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
136,946,017✔
713
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
136,905,446✔
714

715
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
136,908,943✔
716
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
130,934,080✔
717
      pIter->pSttBlk = NULL;
20,735✔
718
    }
719

720
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
136,844,513✔
721
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
5,993,550✔
722
      pIter->pSttBlk = NULL;
179,407✔
723
      pIter->ignoreEarlierTs = true;
183,897✔
724
    }
725
  }
726

727
  return code;
255,587,899✔
728
}
729

730
void tLDataIterClose2(SLDataIter *pIter) {
183,773,339✔
731
  tsdbSttFileReaderClose(&pIter->pReader);
183,773,339✔
732
  pIter->pReader = NULL;
183,762,860✔
733
}
183,771,668✔
734

735
void tLDataIterNextBlock(SLDataIter *pIter, const char *idStr) {
141,744,423✔
736
  int32_t step = pIter->backward ? -1 : 1;
141,744,423✔
737
  int32_t oldIndex = pIter->iSttBlk;
141,789,215✔
738

739
  pIter->iSttBlk += step;
141,802,890✔
740

741
  int32_t index = -1;
141,791,809✔
742
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
141,791,809✔
743
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
142,293,313✔
744
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
16,019,399✔
745
    if ((!pIter->backward) && p->minUid > pIter->uid) {
16,019,597✔
746
      break;
7,585,143✔
747
    }
748

749
    if (pIter->backward && p->maxUid < pIter->uid) {
8,435,440✔
750
      break;
1,240,227✔
751
    }
752

753
    // check uid firstly
754
    if (p->minUid <= pIter->uid && p->maxUid >= pIter->uid) {
7,194,720✔
755
      if ((!pIter->backward) && p->minKey > pIter->timeWindow.ekey) {
7,194,720✔
756
        break;
83,189✔
757
      }
758

759
      if (pIter->backward && p->maxKey < pIter->timeWindow.skey) {
7,111,531✔
760
        break;
108✔
761
      }
762

763
      // check time range secondly
764
      if (p->minKey <= pIter->timeWindow.ekey && p->maxKey >= pIter->timeWindow.skey) {
7,111,423✔
765
        if ((!pIter->backward) && p->minVer > pIter->verRange.maxVer) {
6,616,199✔
UNCOV
766
          break;
×
767
        }
768

769
        if (pIter->backward && p->maxVer < pIter->verRange.minVer) {
6,616,199✔
UNCOV
770
          break;
×
771
        }
772

773
        if (p->minVer <= pIter->verRange.maxVer && p->maxVer >= pIter->verRange.minVer) {
6,616,199✔
774
          index = i;
6,616,199✔
775
          break;
6,616,199✔
776
        }
777
      }
778
    }
779
  }
780

781
  pIter->pSttBlk = NULL;
141,812,293✔
782
  if (index != -1) {
141,729,637✔
783
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
6,616,199✔
784

785
    pIter->iSttBlk = index;
6,616,199✔
786
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
6,616,199✔
787
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
6,616,199✔
788
              ", uidRange:%" PRId64 "-%" PRId64 " %s",
789
              pIter->iSttBlk, oldIndex, pIter->uid, pIter->cid, p->minUid, p->maxUid, idStr);
790
  } else {
791
    tsdbDebug("no more last block qualified, uid:%" PRIu64 ", stt-file block:%d, %s", pIter->uid, oldIndex, idStr);
135,113,438✔
792
  }
793
}
141,729,637✔
794

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

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

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

817
  for (; i < pData->nRow && i >= 0; i += step) {
2,147,483,647✔
818
    if (pData->aUid != NULL) {
2,147,483,647✔
819
      if (!pIter->backward) {
2,147,483,647✔
820
        if (pData->aUid[i] > pIter->uid) {
2,147,483,647✔
821
          break;
23,974,809✔
822
        }
823
      } else {
824
        if (pData->aUid[i] < pIter->uid) {
506,476,537✔
825
          break;
2,042,071✔
826
        }
827
      }
828
    }
829

830
    int64_t ts = pData->aTSKEY[i];
2,147,483,647✔
831
    if (!pIter->backward) {               // asc
2,147,483,647✔
832
      if (ts > pIter->timeWindow.ekey) {  // no more data
2,147,483,647✔
833
        break;
595,864✔
834
      } else {
835
        if (ts < pIter->timeWindow.skey) {
2,147,483,647✔
836
          continue;
1,683,451,841✔
837
        }
838

839
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
2,147,483,647✔
840
          SRowKey key;
162,348✔
841
          tColRowGetKey(pData, i, &key);
162,348✔
842
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
162,348✔
843
          if (ret < 0) {
162,348✔
UNCOV
844
            continue;
×
845
          }
846
        }
847
      }
848
    } else {
849
      if (ts < pIter->timeWindow.skey) {
520,176,782✔
850
        break;
787,234✔
851
      } else {
852
        if (ts > pIter->timeWindow.ekey) {
519,383,559✔
853
          continue;
81,230,722✔
854
        }
855

856
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
438,170,652✔
UNCOV
857
          SRowKey key;
×
UNCOV
858
          tColRowGetKey(pData, i, &key);
×
UNCOV
859
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
×
860
          if (ret > 0) {
×
861
            continue;
×
862
          }
863
        }
864
      }
865
    }
866

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

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

877
    hasVal = true;
2,147,483,647✔
878
    break;
2,147,483,647✔
879
  }
880

881
  pIter->iRow = (hasVal) ? i : -1;
2,147,483,647✔
882
  return code;
2,147,483,647✔
883
}
884

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

892
  *hasNext = false;
2,147,483,647✔
893

894
  // no qualified last file block in current file, no need to fetch row
895
  if (pIter->pSttBlk == NULL) {
2,147,483,647✔
896
    return code;
119,078,458✔
897
  }
898

899
  code = loadLastBlock(pIter, idStr, &pBlockData);
2,147,483,647✔
900
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
UNCOV
901
    lino = __LINE__;
×
UNCOV
902
    goto _exit;
×
903
  }
904

905
  pIter->iRow += step;
2,147,483,647✔
906

907
  while (1) {
6,615,175✔
908
    bool skipBlock = false;
2,147,483,647✔
909
    code = findNextValidRow(pIter, idStr);
2,147,483,647✔
910
    TSDB_CHECK_CODE(code, lino, _exit);
2,147,483,647✔
911

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

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

935
    if (skipBlock || pIter->iRow >= pBlockData->nRow || pIter->iRow < 0) {
2,147,483,647✔
936
      tLDataIterNextBlock(pIter, idStr);
148,980,646✔
937
      if (pIter->pSttBlk == NULL) {  // no more data
141,725,875✔
938
        goto _exit;
135,192,317✔
939
      }
940
    } else {
941
      break;
942
    }
943

944
    if (iBlockL != pIter->iSttBlk) {
6,616,199✔
945
      code = loadLastBlock(pIter, idStr, &pBlockData);
6,616,199✔
946
      if ((pBlockData == NULL) || (code != 0)) {
6,615,985✔
UNCOV
947
        lino = __LINE__;
×
UNCOV
948
        goto _exit;
×
949
      }
950

951
      // set start row index
952
      pIter->iRow = pIter->backward ? pBlockData->nRow - 1 : 0;
6,616,199✔
953
    }
954
  }
955

956
  pIter->rInfo.suid = pBlockData->suid;
2,147,483,647✔
957
  pIter->rInfo.uid = pBlockData->uid;
2,147,483,647✔
958
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
2,147,483,647✔
959

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

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

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

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

978
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
2,147,483,647✔
979
  if (ret < 0) {
2,147,483,647✔
980
    return -1;
1,454,699,457✔
981
  } else if (ret > 0) {
2,147,483,647✔
982
    return 1;
1,907,069,027✔
983
  } else {
984
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
2,147,483,647✔
985
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
2,147,483,647✔
986

987
    if (ver1 < ver2) {
2,147,483,647✔
988
      return -1;
1,268,139,083✔
989
    } else if (ver1 > ver2) {
1,907,758,113✔
990
      return 1;
1,907,765,211✔
991
    } else {
UNCOV
992
      return 0;
×
993
    }
994
  }
995
}
996

997
static FORCE_INLINE int32_t tLDataIterDescCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
26,655,995✔
998
  return -1 * tLDataIterCmprFn(p1, p2);
26,655,995✔
999
}
1000

1001
static void clearTableRowsInfoCache(void) {
132,626✔
1002
  int32_t items = (statisCacheInfo.pStatisFileCache != NULL)? taosLRUCacheGetElems(statisCacheInfo.pStatisFileCache):0;
132,626✔
1003
  tsdbInfo("start to free %d items in statisCache", items);
132,626✔
1004

1005
  taosLRUCacheCleanup(statisCacheInfo.pStatisFileCache);
132,626✔
1006
  (void)taosThreadMutexDestroy(&statisCacheInfo.lock);
132,626✔
1007
}
132,626✔
1008

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

1016
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
264,100,749✔
1017
  int32_t               code = TSDB_CODE_SUCCESS;
264,100,749✔
1018
  STFileSet *           pFset = (STFileSet *)pConf->pCurrentFileset;
264,100,749✔
1019
  bool                  loadStatisFromDisk = true;
264,125,001✔
1020
  int32_t               lino = 0;
264,125,001✔
1021
  int32_t               numOfLevels = pFset->lvlArr->size;
264,125,001✔
1022
  SSttStatisCacheValue* pValue = NULL;
264,123,248✔
1023
  LRUHandle*            pHandle = NULL;
264,114,258✔
1024
  SSttStatisCacheKey    key = {.suid = pConf->suid, .fid = pFset->fid, .vgId = TD_VID(pConf->pTsdb->pVnode)};
264,118,497✔
1025

1026
  (void)taosThreadOnce(&tsCacheInit, initTableRowsInfoCache);
264,129,416✔
1027

1028
  pMTree->pIter = NULL;
264,032,316✔
1029
  pMTree->backward = pConf->backward;
264,091,063✔
1030
  pMTree->idStr = pConf->idstr;
263,976,717✔
1031

1032
  if (!pMTree->backward) {  // asc
264,067,386✔
1033
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
255,846,072✔
1034
  } else {  // desc
1035
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
8,237,443✔
1036
  }
1037

1038
  pMTree->ignoreEarlierTs = false;
264,076,561✔
1039

1040
  // no data exists, go to end
1041
  if (numOfLevels == 0) {
264,031,333✔
1042
    goto _end;
27,130,015✔
1043
  }
1044

1045
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
236,901,318✔
1046
  if (code) {
236,915,195✔
UNCOV
1047
    goto _end;
×
1048
  }
1049

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

1066
  for (int32_t j = 0; j < numOfLevels; ++j) {
492,290,827✔
1067
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
255,349,898✔
1068
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
255,430,002✔
1069

1070
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
511,255,473✔
1071
      SLDataIter *pIter = taosArrayGetP(pList, i);
255,762,254✔
1072

1073
      SSttFileReader    *pSttFileReader = pIter->pReader;
255,809,170✔
1074
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
255,774,242✔
1075

1076
      // open stt file reader if not opened yet
1077
      // if failed to open this stt file, ignore the error and try next one
1078
      if (pSttFileReader == NULL) {
255,820,731✔
1079
        int32_t pgSize = pConf->pTsdb->pVnode->config.tsdbPageSize;
183,722,100✔
1080

1081
        SSttFileReaderConfig conf = {
367,576,099✔
1082
            .tsdb = pConf->pTsdb, .szPage = pgSize, .file[0] = *pSttLevel->fobjArr->data[i]->f};
183,763,403✔
1083

1084
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
183,795,904✔
1085
        if (code != TSDB_CODE_SUCCESS) {
183,658,389✔
UNCOV
1086
          tsdbError("open stt file reader error. file name %s, code %s, %s", pSttLevel->fobjArr->data[i]->fname,
×
1087
                    tstrerror(code), pMTree->idStr);
1088
        }
1089
      }
1090

1091
      if (pLoadInfo == NULL) {
255,745,919✔
1092
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
183,658,079✔
1093
        if (code != TSDB_CODE_SUCCESS) {
183,756,350✔
UNCOV
1094
          goto _end;
×
1095
        }
1096
      }
1097

1098
      if (!loadStatisFromDisk && (pLoadInfo->info.pCount == NULL)) {
255,844,190✔
1099
          code = getSttTableRowsInfo(pValue, pConf->pCurRowKey->numOfPKs, j, i, &pLoadInfo->info);
4,555✔
1100
          if (code != TSDB_CODE_SUCCESS) {
4,555✔
UNCOV
1101
            loadStatisFromDisk = true;  // failed to get statis info from cache, load it from stt file
×
1102
          }
1103
      }
1104

1105
      memset(pIter, 0, sizeof(SLDataIter));
255,844,190✔
1106

1107
      SSttKeyRange range = {.skey.numOfPKs = pConf->pCurRowKey->numOfPKs, .ekey.numOfPKs = pConf->pCurRowKey->numOfPKs};
255,844,190✔
1108
      int64_t      numOfRows = 0;
255,798,652✔
1109
      int64_t      cid = pSttLevel->fobjArr->data[i]->f->cid;
255,798,941✔
1110

1111
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
255,829,874✔
1112
                             pMTree->idStr, loadStatisFromDisk);
1113
      if (code != TSDB_CODE_SUCCESS) {
255,677,779✔
UNCOV
1114
        goto _end;
×
1115
      }
1116

1117
      bool hasVal = NULL;
255,677,779✔
1118
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
255,746,121✔
1119
      if (code) {
255,616,603✔
UNCOV
1120
        goto _end;
×
1121
      }
1122

1123
      if (hasVal) {
255,616,603✔
1124
        tMergeTreeAddIter(pMTree, pIter);
135,197,915✔
1125

1126
        // let's record the time window for current table of uid in the stt files
1127
        if (pSttDataInfo != NULL && numOfRows > 0) {
135,241,188✔
1128
          void *px = taosArrayPush(pSttDataInfo->pKeyRangeList, &range);
135,254,849✔
1129
          QUERY_CHECK_NULL(px, code, lino, _end, terrno);
135,284,198✔
1130

1131
          pSttDataInfo->numOfRows += numOfRows;
135,284,198✔
1132
        }
1133
      } else {
1134
        if (!pMTree->ignoreEarlierTs) {
120,418,688✔
1135
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
120,308,760✔
1136
        }
1137
      }
1138
    }
1139
  }
1140

1141
  if (pConf->cacheStatis && loadStatisFromDisk) {
236,940,929✔
1142
    SSttStatisCacheKey    k = {0};
1,032✔
1143
    SSttStatisCacheValue *pVal = NULL;
1,032✔
1144

1145
    code = buildSttTableRowsInfoKV(pConf, TD_VID(pConf->pTsdb->pVnode), &k, &pVal);
1,032✔
1146
    if (code == TSDB_CODE_SUCCESS) {
863✔
1147
      code = putStatisInfoIntoCache(statisCacheInfo.pStatisFileCache, &k, pVal, pConf->idstr);
863✔
1148
    }
1149
  }
1150

1151
  if (pHandle != NULL && pConf->cacheStatis) {
236,840,242✔
1152
    releaseCacheHandle(statisCacheInfo.pStatisFileCache, &pHandle, true);
6,192✔
1153
  }
1154

1155
  return code;
236,852,703✔
1156

1157
_end:
27,109,934✔
1158
  if (pHandle != NULL && pConf->cacheStatis) {
27,129,180✔
UNCOV
1159
    releaseCacheHandle(statisCacheInfo.pStatisFileCache, &pHandle, true);
×
1160
  }
1161

1162
  tMergeTreeClose(pMTree);
27,129,180✔
1163
  return code;
27,130,015✔
1164
}
1165

1166
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
135,181,303✔
1167
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
135,181,303✔
1168
}
135,238,082✔
1169

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

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

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

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

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

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

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

UNCOV
1204
  tsdbError("failed to unpin any stt block, sttBlock:%d stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
×
1205
}
1206

1207
void tMergeTreePinSttBlock(SMergeTree *pMTree) {
2,147,483,647✔
1208
  if (pMTree->pIter == NULL) {
2,147,483,647✔
UNCOV
1209
    return;
×
1210
  }
1211

1212
  SLDataIter *pIter = pMTree->pIter;
2,147,483,647✔
1213
  pMTree->pPinnedBlockIter = pIter;
2,147,483,647✔
1214
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
2,147,483,647✔
1215
}
1216

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

1222
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
2,147,483,647✔
1223
  pMTree->pPinnedBlockIter = NULL;
2,147,483,647✔
1224
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
2,147,483,647✔
1225
}
1226

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

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

1243
      pMTree->pIter = NULL;
126,068,431✔
1244
    }
1245

1246
    // compare with min in RB Tree
1247
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
2,147,483,647✔
1248
    if (pMTree->pIter && pIter) {
2,147,483,647✔
1249
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
1,446,018,122✔
1250
      if (c > 0) {
1,446,002,363✔
1251
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
1,250,477,869✔
1252
        pMTree->pIter = NULL;
1,250,491,065✔
1253
      } else if (!c) {
195,524,663✔
UNCOV
1254
        continue;
×
1255
      }
1256
    }
1257

1258
    break;
2,147,483,647✔
1259
  }
1260

1261
  if (pMTree->pIter == NULL) {
2,147,483,647✔
1262
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
1,646,801,323✔
1263
    if (pMTree->pIter) {
1,646,765,887✔
1264
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
1,384,246,350✔
1265
    }
1266
  }
1267

1268
  *pHasNext = (pMTree->pIter != NULL);
2,147,483,647✔
1269
  return code;
2,147,483,647✔
1270
}
1271

1272
void tMergeTreeClose(SMergeTree *pMTree) {
555,372,094✔
1273
  pMTree->pIter = NULL;
555,372,094✔
1274
  pMTree->pPinnedBlockIter = NULL;
555,507,741✔
1275
}
555,471,905✔
1276

1277
int32_t buildSttTableRowsInfoKV(SMergeTreeConf *pConf, int32_t vgId, SSttStatisCacheKey *pKey,
1,032✔
1278
                                SSttStatisCacheValue **pValue) {
1279
  *pValue = taosMemoryCalloc(1, sizeof(SSttStatisCacheValue));
1,032✔
1280
  if (*pValue == NULL) {
1,032✔
UNCOV
1281
    return terrno;
×
1282
  }
1283

1284
  memset(pKey, 0, sizeof(SSttStatisCacheKey));
1,032✔
1285

1286
  STFileSet *pFset = (STFileSet *)pConf->pCurrentFileset;
1,032✔
1287

1288
  pKey->suid = pConf->suid;
1,032✔
1289
  pKey->vgId = vgId;
1,032✔
1290
  pKey->fid = pFset->fid;
1,032✔
1291

1292
  (*pValue)->commitTs = pFset->lastCommit;
1,032✔
1293

1294
  int32_t numOfLevels = TARRAY2_SIZE(pFset->lvlArr);
1,032✔
1295

1296
  (*pValue)->pLevel = taosArrayInit(numOfLevels, sizeof(void *));
1,032✔
1297
  if ((*pValue)->pLevel == NULL) {
1,032✔
UNCOV
1298
    return terrno;
×
1299
  }
1300

1301
  for (int32_t j = 0; j < numOfLevels; ++j) {
1,895✔
1302
    SSttLvl *pSttLevel = pFset->lvlArr->data[j];
1,032✔
1303
    SArray * pIterList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
1,032✔
1304
    if (pIterList == NULL) {
1,032✔
UNCOV
1305
      return terrno;
×
1306
    }
1307

1308
    SArray *pRowsInfoArr = taosArrayInit(TARRAY2_SIZE(pSttLevel->fobjArr), sizeof(SSttTableRowsInfo));
1,032✔
1309
    if (pRowsInfoArr == NULL) {
863✔
UNCOV
1310
      return terrno;
×
1311
    }
1312

1313
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all stt file
1,726✔
1314
      SLDataIter *       pIter = taosArrayGetP(pIterList, i);
863✔
1315
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
863✔
1316

1317
      void *px = taosArrayPush(pRowsInfoArr, &pLoadInfo->info);
863✔
1318
      if (px == NULL) {
863✔
UNCOV
1319
        return terrno;
×
1320
      }
1321

1322
      memset(&pLoadInfo->info, 0, sizeof(SSttTableRowsInfo));
863✔
1323
    }
1324

1325
    void *px = taosArrayPush((*pValue)->pLevel, &pRowsInfoArr);
863✔
1326
    if (px == NULL) {
863✔
UNCOV
1327
      return terrno;
×
1328
    }
1329
  }
1330

1331
  // todo handle memory failure
1332

1333
  return TSDB_CODE_SUCCESS;
863✔
1334
}
1335

1336
int32_t getStatisInfoFromCache(SLRUCache *pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue **pValue,
7,055✔
1337
                               LRUHandle **pHandle, const char *id) {
1338
  *pValue = NULL;
7,055✔
1339
  *pHandle = NULL;
7,055✔
1340

1341
  (void)taosThreadMutexLock(&statisCacheInfo.lock);
7,055✔
1342
  LRUHandle *pItemHandle = taosLRUCacheLookup(pCache, pKey, sizeof(SSttStatisCacheKey));
7,224✔
1343
  if (pItemHandle == NULL) {
7,224✔
1344
    (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
1,032✔
1345
    return TSDB_CODE_NOT_FOUND;
1,032✔
1346
  }
1347

1348
  void *p = taosLRUCacheValue(pCache, pItemHandle);
6,192✔
1349

1350
  *pValue = p;
6,192✔
1351
  *pHandle = pItemHandle;
6,192✔
1352

1353
  tsdbDebug("get statis info from cache suid:%" PRId64 ", vgId:%d, fid:%d, %s, commitTs:%" PRId64, pKey->suid,
6,192✔
1354
            pKey->vgId, pKey->fid, id, (*pValue)->commitTs);
1355

1356
  // (*pEntry)->hitTimes += 1;
1357
  (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
6,192✔
1358
  return TSDB_CODE_SUCCESS;
6,192✔
1359
}
1360

1361
void releaseCacheHandle(SLRUCache *pCache, LRUHandle **pHandle, bool lock) {
6,192✔
1362
  if (lock) {
6,192✔
1363
    (void)taosThreadMutexLock(&statisCacheInfo.lock);
6,192✔
1364
  }
1365

1366
  bool ret = taosLRUCacheRelease(pCache, *pHandle, false);
6,192✔
1367
  *pHandle = NULL;
6,192✔
1368

1369
  if (lock) {
6,192✔
1370
    (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
6,192✔
1371
  }
1372
}
6,192✔
1373

1374
void freeStatisFileItems(const void* key, size_t keyLen, void* value, void* ud) {
1,032✔
1375
  (void)ud;
1376

1377
  if (value == NULL) {
1,032✔
UNCOV
1378
    return;
×
1379
  }
1380

1381
  SSttStatisCacheKey *  pKey = (SSttStatisCacheKey *)key;
1,032✔
1382
  SSttStatisCacheValue *pVal = value;
1,032✔
1383

1384
  for(int32_t i = 0; i < taosArrayGetSize(pVal->pLevel); ++i) {
2,064✔
1385
    SArray* pInfos = taosArrayGetP(pVal->pLevel, i);
1,032✔
1386

1387
    for(int32_t j = 0; j < taosArrayGetSize(pInfos); ++j) {
2,064✔
1388
      SSttTableRowsInfo* p = taosArrayGet(pInfos, j);
1,032✔
1389
      taosArrayDestroy(p->pCount);
1,032✔
1390
      taosArrayDestroyEx(p->pFirstKey, freeItem);
1,032✔
1391
      taosArrayDestroyEx(p->pLastKey, freeItem);
1,032✔
1392
      taosArrayDestroy(p->pFirstTs);
1,032✔
1393
      taosArrayDestroy(p->pLastTs);
1,032✔
1394
      taosArrayDestroy(p->pUid);
1,032✔
1395
    }
1396

1397
    taosArrayDestroy(pInfos);
1,032✔
1398
  }
1399

1400
  taosArrayDestroy(pVal->pLevel);
1,032✔
1401
  taosMemoryFree(pVal);
1,032✔
1402
}
1403

1404
int32_t putStatisInfoIntoCache(SLRUCache *pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue *pValue,
863✔
1405
                               const char *id) {
1406
  (void)taosThreadMutexLock(&statisCacheInfo.lock);
863✔
1407

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

1421
  (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
1,032✔
1422
  return TSDB_CODE_SUCCESS;
1,032✔
1423
}
1424

UNCOV
1425
void clearStatisInfoCache(SLRUCache *pStatisCache, SSttStatisCacheKey *pKey, LRUHandle** pHandle) {
×
UNCOV
1426
  (void)taosThreadMutexLock(&statisCacheInfo.lock);
×
UNCOV
1427
  releaseCacheHandle(statisCacheInfo.pStatisFileCache, pHandle, false);
×
1428
  taosLRUCacheErase(pStatisCache, pKey, sizeof(SSttStatisCacheKey));
×
1429
  (void)taosThreadMutexUnlock(&statisCacheInfo.lock);
×
1430
}
×
1431

1432
static int32_t dupPlayload(SValue *p){
×
1433
  if (p != NULL){
×
UNCOV
1434
    int32_t code = tValueDupPayload(p);
×
1435
    if (code != TSDB_CODE_SUCCESS) {
×
1436
      return code;
×
1437
    }
1438
  }
1439
  return 0;
×
1440
}
1441

1442
int32_t sttRowInfoDeepCopy(SSttTableRowsInfo *pDst, SSttTableRowsInfo *pInfo, int32_t numOfPKs) {
4,555✔
1443
  int32_t code = 0;
4,555✔
1444

1445
  pDst->pCount = taosArrayDup(pInfo->pCount, NULL);
4,555✔
1446
  pDst->pFirstKey = taosArrayDup(pInfo->pFirstKey, NULL);
4,386✔
1447
  pDst->pLastKey = taosArrayDup(pInfo->pLastKey, NULL);
4,555✔
1448
  pDst->pFirstTs = taosArrayDup(pInfo->pFirstTs, NULL);
4,555✔
1449
  pDst->pLastTs = taosArrayDup(pInfo->pLastTs, NULL);
4,555✔
1450
  pDst->pUid = taosArrayDup(pInfo->pUid, NULL);
4,555✔
1451

1452
  pDst->memSize = pInfo->memSize;
4,555✔
1453
  
1454
  if (numOfPKs > 0) {
4,555✔
UNCOV
1455
    int32_t len = taosArrayGetSize(pDst->pCount);
×
UNCOV
1456
    for (int32_t i = 0; i < len; ++i) {
×
UNCOV
1457
      SValue *p1 = (SValue *)taosArrayGet(pDst->pFirstKey, i);
×
UNCOV
1458
      if (p1 == NULL) {
×
UNCOV
1459
        return terrno;
×
1460
      }
1461
      code = tValueDupPayload(p1);
×
1462
      if (code != TSDB_CODE_SUCCESS) {
×
1463
        return code;
×
1464
      }
UNCOV
1465
      SValue *p2 = (SValue *)taosArrayGet(pDst->pLastKey, i);
×
1466
      if (p2 == NULL) {
×
1467
        return terrno;
×
1468
      }
UNCOV
1469
      code = tValueDupPayload(p2);
×
1470
      if (code != TSDB_CODE_SUCCESS) {
×
1471
        return code;
×
1472
      }
1473
    }
1474
  }
1475

1476
  return TSDB_CODE_SUCCESS;
4,555✔
1477
}
1478

1479
int32_t getSttTableRowsInfo(SSttStatisCacheValue *pValue, int32_t numOfPKs, int32_t levelIdx, int32_t fileIdx,
4,555✔
1480
                            SSttTableRowsInfo *pRowInfo) {
1481
  if (levelIdx >= taosArrayGetSize(pValue->pLevel)) {
4,555✔
UNCOV
1482
    return TSDB_CODE_INVALID_PARA;
×
1483
  }
1484

1485
  SArray *pRowsInfoArr = *(SArray **)taosArrayGet(pValue->pLevel, levelIdx);
4,555✔
1486
  if (pRowsInfoArr == NULL) {
4,555✔
1487
    return TSDB_CODE_INVALID_PARA;
×
1488
  }
1489

1490
  if (fileIdx >= taosArrayGetSize(pRowsInfoArr)) {
4,555✔
UNCOV
1491
    return TSDB_CODE_INVALID_PARA;
×
1492
  }
1493

1494
  void* p = (SSttTableRowsInfo *)taosArrayGet(pRowsInfoArr, fileIdx);
4,555✔
1495
  if (p == NULL) {
4,555✔
1496
    return TSDB_CODE_INVALID_PARA;
×
1497
  }
1498

1499
  return sttRowInfoDeepCopy(pRowInfo, p, numOfPKs);
4,555✔
1500
}
1501

1502
int32_t getCacheValueSize(const SSttStatisCacheValue *pValue) {
1,032✔
1503
  int32_t size = sizeof(SSttStatisCacheValue) + sizeof(SArray);
1,032✔
1504
  for (int32_t i = 0; i < taosArrayGetSize(pValue->pLevel); ++i) {
2,064✔
1505
    SArray *pRowsInfoArr = *(SArray **)taosArrayGet(pValue->pLevel, i);
1,032✔
1506
    
1507
    size += sizeof(SArray);
1,032✔
1508
    for (int32_t j = 0; j < taosArrayGetSize(pRowsInfoArr); ++j) {
2,064✔
1509
      SSttTableRowsInfo *p = (SSttTableRowsInfo *)taosArrayGet(pRowsInfoArr, j);
1,032✔
1510
      size += p->memSize;
1,032✔
1511
    }
1512
  }
1513

1514
  return size;
1,032✔
1515
}
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