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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 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) {
365,837,620✔
60
  *pInfo = NULL;
365,837,620✔
61

62
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
365,925,471✔
63
  if (pLoadInfo == NULL) {
365,725,271✔
64
    return terrno;
×
65
  }
66

67
  pLoadInfo->blockData[0].sttBlockIndex = -1;
365,725,271✔
68
  pLoadInfo->blockData[1].sttBlockIndex = -1;
365,738,063✔
69

70
  pLoadInfo->currentLoadBlockIndex = 1;
365,757,225✔
71

72
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
365,781,795✔
73
  if (code) {
365,988,471✔
74
    taosMemoryFreeClear(pLoadInfo);
×
75
    return code;
×
76
  }
77

78
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
365,988,471✔
79
  if (code) {
365,991,918✔
80
    taosMemoryFreeClear(pLoadInfo);
×
81
    return code;
×
82
  }
83

84
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
365,991,918✔
85
  if (pLoadInfo->aSttBlk == NULL) {
366,012,599✔
86
    taosMemoryFreeClear(pLoadInfo);
×
87
    return terrno;
×
88
  }
89

90
  pLoadInfo->pSchema = pSchema;
366,035,520✔
91
  pLoadInfo->colIds = colList;
366,041,843✔
92
  pLoadInfo->numOfCols = numOfCols;
366,027,526✔
93

94
  *pInfo = pLoadInfo;
366,062,792✔
95
  return code;
366,053,291✔
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);
136,330,328✔
102
  }
103
}
2,147,483,647✔
104

105
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
365,890,870✔
106
  if (pLoadInfo == NULL) {
365,890,870✔
107
    return;
×
108
  }
109

110
  pLoadInfo->currentLoadBlockIndex = 1;
365,890,870✔
111

112
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
366,021,133✔
113
  tBlockDataDestroy(&pInfo->data);
365,985,475✔
114
  pInfo->sttBlockIndex = -1;
365,966,785✔
115
  pInfo->pin = false;
365,994,788✔
116

117
  pInfo = &pLoadInfo->blockData[1];
365,961,548✔
118
  tBlockDataDestroy(&pInfo->data);
365,925,824✔
119
  pInfo->sttBlockIndex = -1;
366,072,748✔
120
  pInfo->pin = false;
366,073,704✔
121

122
  taosArrayDestroy(pLoadInfo->info.pUid);
366,070,649✔
123
  taosArrayDestroy(pLoadInfo->info.pCount);
366,038,865✔
124
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
366,046,059✔
125
  taosArrayDestroy(pLoadInfo->info.pLastTs);
366,054,207✔
126

127
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
366,053,619✔
128
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
365,982,788✔
129

130
  pLoadInfo->info.pUid = NULL;
366,039,772✔
131
  pLoadInfo->info.pFirstKey = NULL;
366,074,132✔
132
  pLoadInfo->info.pLastKey = NULL;
366,059,005✔
133
  pLoadInfo->info.pCount = NULL;
366,069,383✔
134
  pLoadInfo->info.pFirstTs = NULL;
366,015,692✔
135
  pLoadInfo->info.pLastTs = NULL;
366,093,066✔
136

137
  taosArrayDestroy(pLoadInfo->aSttBlk);
366,087,569✔
138
  taosMemoryFree(pLoadInfo);
366,054,751✔
139
}
140

141
void destroyLDataIter(SLDataIter *pIter) {
366,060,995✔
142
  tLDataIterClose2(pIter);
366,060,995✔
143
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
366,030,524✔
144
  taosMemoryFree(pIter);
366,008,660✔
145
}
366,049,337✔
146

147
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
604,695,543✔
148
  if (pLDataIterArray == NULL) {
604,695,543✔
149
    return;
5,632,215✔
150
  }
151

152
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
599,063,328✔
153
  for (int32_t i = 0; i < numOfLevel; ++i) {
965,070,487✔
154
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
365,938,209✔
155
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
731,985,751✔
156
      SLDataIter *pIter = taosArrayGetP(pList, j);
366,123,634✔
157
      if (pIter->pBlockLoadInfo != NULL) {
366,117,521✔
158
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
366,121,994✔
159
        if (pLoadCost != NULL) {
366,122,307✔
160
          pLoadCost->loadBlocks += pCost->loadBlocks;
365,593,090✔
161
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
365,589,768✔
162
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
365,577,833✔
163
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
365,577,016✔
164
        }
165
      }
166

167
      destroyLDataIter(pIter);
366,091,073✔
168
    }
169

170
    taosArrayDestroy(pList);
365,876,698✔
171
  }
172

173
  taosArrayDestroy(pLDataIterArray);
599,132,278✔
174
}
175

176
// choose the unpinned slot to load next data block
177
static void updateBlockLoadSlot(SSttBlockLoadInfo *pLoadInfo) {
168,926,549✔
178
  int32_t nextSlotIndex = pLoadInfo->currentLoadBlockIndex ^ 1;
168,926,549✔
179
  if (pLoadInfo->blockData[nextSlotIndex].pin) {
169,040,479✔
180
    nextSlotIndex = nextSlotIndex ^ 1;
×
181
  }
182

183
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
169,046,038✔
184
}
169,035,440✔
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
96,531✔
197
                ", load data, %s",
198
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
199
      pInfo->currentLoadBlockIndex = 0;
96,531✔
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
91,743✔
209
                ", load data, %s",
210
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
211
      pInfo->currentLoadBlockIndex = 1;
91,743✔
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) {
168,944,533✔
219
    return code;
×
220
  }
221

222
  updateBlockLoadSlot(pInfo);
169,014,345✔
223
  int64_t st = taosGetTimestampUs();
169,076,489✔
224

225
  SBlockData *pBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
169,076,489✔
226
  code = tsdbSttFileReadBlockDataByColumn(pIter->pReader, pIter->pSttBlk, pBlock, pInfo->pSchema, &pInfo->colIds[1],
169,022,546✔
227
                                          pInfo->numOfCols - 1);
169,050,355✔
228
  if (code != TSDB_CODE_SUCCESS) {
169,075,686✔
229
    return code;
×
230
  }
231

232
  double el = (taosGetTimestampUs() - st) / 1000.0;
169,146,993✔
233
  pInfo->cost.blockElapsedTime += el;
169,146,993✔
234
  pInfo->cost.loadBlocks += 1;
169,101,450✔
235

236
  tsdbDebug("read stt block, total load:%" PRId64 ", trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
169,120,148✔
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;
169,120,148✔
244
  pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].data.nRow : -1;
169,134,002✔
245

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

249
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
169,112,294✔
250
  return code;
169,085,099✔
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;
302,128,211✔
257
  int32_t step = backward ? 1 : -1;
302,128,211✔
258
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
607,915,655✔
259
    i += step;
305,787,444✔
260
  }
261
  return i - step;
302,088,335✔
262
}
263

264
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
929,958,155✔
265
  int32_t midPos = -1;
929,958,155✔
266
  if (num <= 0) {
929,958,155✔
267
    return -1;
538,446,251✔
268
  }
269

270
  int32_t firstPos = 0;
391,511,904✔
271
  int32_t lastPos = num - 1;
391,511,904✔
272

273
  // find the first position which is bigger than the key
274
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
391,511,904✔
275
    return -1;
89,580,729✔
276
  }
277

278
  while (1) {
24,825,865✔
279
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
326,877,678✔
280
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
287,576,624✔
281
    }
282

283
    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
39,368,748✔
284
      return -1;
36,480✔
285
    }
286

287
    int32_t numOfRows = lastPos - firstPos + 1;
39,333,517✔
288
    midPos = (numOfRows >> 1u) + firstPos;
39,333,517✔
289

290
    if (uid < pBlockList[midPos].minUid) {
39,333,517✔
291
      lastPos = midPos - 1;
11,958,997✔
292
    } else if (uid > pBlockList[midPos].maxUid) {
27,369,467✔
293
      firstPos = midPos + 1;
12,866,868✔
294
    } else {
295
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
14,511,711✔
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;
301,019,886✔
303
  int32_t step = backward ? 1 : -1;
301,019,886✔
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;
301,032,340✔
308
}
309

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

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

319
  while (1) {
225,898,459✔
320
    if (uid == uidList[firstPos]) {
526,939,362✔
321
      return findEarliestRow(firstPos, uid, uidList, num, backward);
160,630,233✔
322
    }
323

324
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
366,313,865✔
325
      return -1;
27,773✔
326
    }
327

328
    int32_t numOfRows = lastPos - firstPos + 1;
366,266,377✔
329
    int32_t midPos = (numOfRows >> 1u) + firstPos;
366,266,377✔
330

331
    if (uid < uidList[midPos]) {
366,266,377✔
332
      lastPos = midPos - 1;
83,418,640✔
333
    } else if (uid > uidList[midPos]) {
282,873,534✔
334
      firstPos = midPos + 1;
142,479,819✔
335
    } else {
336
      return findEarliestRow(midPos, uid, uidList, num, backward);
140,402,107✔
337
    }
338
  }
339
}
340

341
static int32_t extractSttBlockInfo(SLDataIter *pIter, const TSttBlkArray *pArray, SSttBlockLoadInfo *pBlockLoadInfo,
365,859,068✔
342
                                   uint64_t suid) {
343
  void   *px = NULL;
365,859,068✔
344
  int32_t code = TSDB_CODE_SUCCESS;
365,859,068✔
345
  if (TARRAY2_SIZE(pArray) <= 0) {
365,859,068✔
346
    return code;
138,010✔
347
  }
348

349
  SSttBlk *pStart = &pArray->data[0];
365,822,244✔
350
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
365,880,352✔
351

352
  // all identical
353
  if (pStart->suid == pEnd->suid) {
365,822,307✔
354
    if (pStart->suid != suid) {  // no qualified stt block existed
245,003,007✔
355
      taosArrayClear(pBlockLoadInfo->aSttBlk);
98,680,725✔
356
      pIter->iSttBlk = -1;
98,646,800✔
357
      return TSDB_CODE_SUCCESS;
98,682,141✔
358
    } else {  // all blocks are qualified
359
      taosArrayClear(pBlockLoadInfo->aSttBlk);
146,317,782✔
360
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
146,319,219✔
361
      if (px == NULL) {
146,366,115✔
362
        return terrno;
×
363
      }
364
    }
365
  } else {
366
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
120,848,493✔
367
    if (pTmp == NULL) {
120,865,273✔
368
      return terrno;
×
369
    }
370

371
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
391,833,729✔
372
      SSttBlk *p = &pArray->data[i];
337,859,966✔
373
      if (p->suid < suid) {
337,864,365✔
374
        continue;
199,600,147✔
375
      }
376

377
      if (p->suid == suid) {
138,273,335✔
378
        void *px = taosArrayPush(pTmp, p);
71,367,509✔
379
        if (px == NULL) {
71,367,509✔
380
          code = terrno;
×
381
          break;
×
382
        }
383
      } else if (p->suid > suid) {
66,908,868✔
384
        break;
66,910,520✔
385
      }
386
    }
387

388
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
120,872,797✔
389
    pBlockLoadInfo->aSttBlk = pTmp;
120,881,883✔
390
  }
391

392
  return code;
267,148,841✔
393
}
394

395
static int32_t tValueDupPayload(SValue *pVal) {
344,834,232✔
396
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
344,834,232✔
397
    char *p = (char *)pVal->pData;
136,335,103✔
398
    char *pBuf = taosMemoryMalloc(pVal->nData);
136,326,086✔
399
    if (pBuf == NULL) {
136,324,284✔
400
      return terrno;
×
401
    }
402

403
    memcpy(pBuf, p, pVal->nData);
136,324,284✔
404
    pVal->pData = (uint8_t *)pBuf;
136,324,887✔
405
  }
406

407
  return TSDB_CODE_SUCCESS;
344,848,115✔
408
}
409

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

418
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
365,925,170✔
419
  if (numOfBlocks <= 0) {
365,984,375✔
420
    return code;
138,010✔
421
  }
422

423
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
477,337,296✔
424
    ++startIndex;
111,490,931✔
425
  }
426

427
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
365,811,617✔
428
    return 0;
128,708,366✔
429
  }
430

431
  int32_t endIndex = startIndex;
237,172,245✔
432
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
482,349,661✔
433
    ++endIndex;
245,177,416✔
434
  }
435

436
  int32_t num = endIndex - startIndex;
237,178,167✔
437
  pBlockLoadInfo->cost.loadStatisBlocks += num;
237,178,167✔
438

439
  STbStatisBlock block;
237,158,260✔
440
  code = tStatisBlockInit(&block);
237,100,540✔
441
  QUERY_CHECK_CODE(code, lino, _end);
237,213,587✔
442

443
  int64_t st = taosGetTimestampUs();
237,182,711✔
444

445
  for (int32_t k = startIndex; k < endIndex; ++k) {
482,384,036✔
446
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
245,115,849✔
447
    QUERY_CHECK_CODE(code, lino, _end);
245,221,546✔
448

449
    int32_t i = 0;
245,221,546✔
450
    int32_t rows = block.numOfRecords;
245,221,546✔
451
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
576,356,089✔
452
      ++i;
331,134,543✔
453
    }
454

455
    // existed
456
    if (i < rows) {
245,219,653✔
457
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
214,199,913✔
458

459
      if (pInfo->pUid == NULL) {
214,200,500✔
460
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
206,184,454✔
461
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
206,194,860✔
462
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
206,207,960✔
463
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
206,206,152✔
464

465
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
206,187,953✔
466
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
206,157,614✔
467

468
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
206,158,927✔
469
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
206,192,667✔
470
          code = terrno;
1,878✔
471
          goto _end;
×
472
        }
473

474
        pInfo->memSize = sizeof(SSttTableRowsInfo) + sizeof(SArray) * 6;
206,166,092✔
475
      }
476

477
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
214,180,536✔
478
        int32_t size = rows - i;
190,450,036✔
479
        int32_t offset = i * sizeof(int64_t);
190,450,036✔
480

481
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
190,450,036✔
482
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
190,439,201✔
483

484
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
190,439,201✔
485
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
190,430,703✔
486

487
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
190,430,703✔
488
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
190,494,074✔
489

490
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
190,494,074✔
491
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
190,493,258✔
492

493
        pInfo->memSize += size * sizeof(int64_t) * 4;
190,493,258✔
494
        pInfo->memSize += size * sizeof(SValue) * 2;
190,466,827✔
495

496
        if (block.numOfPKs > 0) {
190,392,151✔
497
          SValue vFirst = {0}, vLast = {0};
37,942,341✔
498
          for (int32_t f = i; f < rows; ++f) {
185,890,358✔
499
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
147,933,531✔
500
            TSDB_CHECK_CODE(code, lino, _end);
147,931,077✔
501

502
            code = tValueDupPayload(&vFirst);
147,931,077✔
503
            TSDB_CHECK_CODE(code, lino, _end);
147,940,136✔
504

505
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
147,940,136✔
506
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
147,953,409✔
507

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

512
            code = tValueDupPayload(&vLast);
147,960,673✔
513
            TSDB_CHECK_CODE(code, lino, _end);
147,958,857✔
514

515
            px = taosArrayPush(pInfo->pLastKey, &vLast);
147,958,857✔
516
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
147,960,673✔
517

518
            pInfo->memSize += (IS_VAR_DATA_TYPE(vFirst.type) || vFirst.type == TSDB_DATA_TYPE_DECIMAL)? (vFirst.nData + vLast.nData):0;
147,960,673✔
519
          }
520
        } else {
521
          SValue vFirst = {0};
152,449,810✔
522
          for (int32_t j = 0; j < size; ++j) {
2,147,483,647✔
523
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
2,147,483,647✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,147,483,647✔
525

526
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
2,147,483,647✔
527
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,147,483,647✔
528
          }
529
        }
530
      } else {
531
        STbStatisRecord record = {0};
23,719,290✔
532
        while (i < rows) {
60,063,593✔
533
          code = tStatisBlockGet(&block, i, &record);
60,067,374✔
534
          TSDB_CHECK_CODE(code, lino, _end);
60,069,616✔
535

536
          if (record.suid != suid) {
60,069,616✔
537
            break;
23,725,772✔
538
          }
539

540
          px = taosArrayPush(pInfo->pUid, &record.uid);
36,343,844✔
541
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
36,347,201✔
542

543
          px = taosArrayPush(pInfo->pCount, &record.count);
36,347,201✔
544
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
36,346,742✔
545

546
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
36,346,742✔
547
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
36,347,804✔
548

549
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
36,347,804✔
550
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
36,347,804✔
551

552
          pInfo->memSize += (sizeof(int64_t) * 4 + sizeof(SValue) * 2);
36,347,804✔
553

554
          if (record.firstKey.numOfPKs > 0) {
36,345,365✔
555
            SValue first = record.firstKey.pks[0];
24,476,072✔
556
            code = tValueDupPayload(&first);
24,476,072✔
557
            TSDB_CHECK_CODE(code, lino, _end);
24,474,866✔
558

559
            px = taosArrayPush(pInfo->pFirstKey, &first);
24,474,866✔
560
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
24,476,675✔
561

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

566
            px = taosArrayPush(pInfo->pLastKey, &last);
24,476,675✔
567
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
24,476,675✔
568

569
            pInfo->memSize += (IS_VAR_DATA_TYPE(first.type) || first.type == TSDB_DATA_TYPE_DECIMAL)? (first.nData + last.nData):0;
24,476,675✔
570
          } else {
571
            SValue v = {0};
11,869,293✔
572
            px = taosArrayPush(pInfo->pFirstKey, &v);
11,867,457✔
573
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
11,871,129✔
574

575
            px = taosArrayPush(pInfo->pLastKey, &v);
11,871,129✔
576
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
11,871,129✔
577
          }
578

579
          i += 1;
36,344,041✔
580
        }
581
      }
582
    }
583
  }
584

585
_end:
237,326,266✔
586
  tStatisBlockDestroy(&block);
237,162,316✔
587
  if (code != 0) {
237,229,366✔
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;
237,228,130✔
591
    pBlockLoadInfo->cost.statisElapsedTime += el;
237,228,130✔
592

593
    tsdbDebug("%s load %d statis blocks into buf, elapsed time:%.2fms", id, num, el);
237,232,840✔
594
  }
595
  return code;
237,202,832✔
596
}
597

598
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
365,864,890✔
599
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr, bool loadFromDisk) {
600
  int64_t st = taosGetTimestampUs();
366,063,346✔
601

602
  const TSttBlkArray *pSttBlkArray = NULL;
366,063,346✔
603
  pBlockLoadInfo->sttBlockLoaded = true;
366,035,543✔
604

605
  // load the stt block info for each stt-block
606
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
366,054,091✔
607
  if (code != TSDB_CODE_SUCCESS) {
365,947,138✔
608
    tsdbError("load stt blk failed, code:%s, %s", tstrerror(code), idStr);
×
609
    return code;
×
610
  }
611

612
  // load the stt block info for each stt file block
613
  code = extractSttBlockInfo(pIter, pSttBlkArray, pBlockLoadInfo, suid);
365,947,138✔
614
  if (code != TSDB_CODE_SUCCESS) {
365,915,041✔
615
    tsdbError("load stt block info failed, code:%s, %s", tstrerror(code), idStr);
844✔
616
    return code;
×
617
  }
618

619
  if (loadFromDisk) {
365,924,495✔
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;
365,923,775✔
622
    code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
365,914,062✔
623
    if (code != TSDB_CODE_SUCCESS) {
365,995,770✔
624
      tsdbError("failed to load stt block statistics, code:%s, %s", tstrerror(code), idStr);
×
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);
365,995,770✔
630
    if (code != TSDB_CODE_SUCCESS) {
365,902,549✔
631
      tsdbError("failed to load stt statistics block data, code:%s, %s", tstrerror(code), idStr);
×
632
      return code;
×
633
    }
634
  } else {
635
    tsdbDebug("stt block statis info loaded from cache, %s", idStr);
720✔
636
  }
637

638
  code = loadTombFn(pReader1, pIter->pReader, pIter->pBlockLoadInfo);
366,041,378✔
639

640
  double el = (taosGetTimestampUs() - st) / 1000.0;
366,056,446✔
641
  tsdbDebug("load the stt file blk info completed, elapsed time:%.2fms, %s", el, idStr);
366,056,446✔
642
  return code;
365,999,500✔
643
}
644

645
static int32_t uidComparFn(const void *p1, const void *p2) {
762,211,070✔
646
  const uint64_t *pFirst = p1;
762,211,070✔
647
  const uint64_t *pVal = p2;
762,211,070✔
648

649
  if (*pFirst == *pVal) {
762,211,070✔
650
    return 0;
302,160,498✔
651
  } else {
652
    return *pFirst < *pVal ? -1 : 1;
460,252,068✔
653
  }
654
}
655

656
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
930,072,419✔
657
                                      int64_t *numOfRows) {
658
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
930,072,419✔
659
    return;
538,513,750✔
660
  }
661

662
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
391,754,621✔
663
  if (index >= 0) {
391,713,107✔
664
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
302,157,978✔
665
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
302,164,579✔
666

667
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
302,156,226✔
668

669
    if (pRange->skey.numOfPKs > 0) {
302,161,323✔
670
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
129,583,611✔
671
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
129,592,649✔
672
    }
673
  }
674
}
675

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

681
  pIter->uid = pConf->uid;
930,192,542✔
682
  pIter->cid = cid;
930,380,123✔
683
  pIter->backward = backward;
930,399,498✔
684
  pIter->verRange.minVer = pConf->verRange.minVer;
930,374,829✔
685
  pIter->verRange.maxVer = pConf->verRange.maxVer;
930,247,495✔
686
  pIter->timeWindow.skey = pConf->timewindow.skey;
930,338,235✔
687
  pIter->timeWindow.ekey = pConf->timewindow.ekey;
930,234,202✔
688

689
  pIter->pStartRowKey = pConf->pCurRowKey;
930,330,258✔
690
  pIter->pReader = pSttFileReader;
930,163,303✔
691
  pIter->pBlockLoadInfo = pBlockLoadInfo;
930,335,555✔
692

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

701
  if (!pBlockLoadInfo->sttBlockLoaded) {
930,132,873✔
702
      code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr, loadFromDisk);
365,817,694✔
703
  }
704

705
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
930,238,281✔
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);
930,191,986✔
710
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
930,205,626✔
711
  if (pIter->iSttBlk != -1) {
930,122,759✔
712
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
302,116,382✔
713
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
302,148,715✔
714

715
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
302,061,591✔
716
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
245,050,054✔
717
      pIter->pSttBlk = NULL;
57,443✔
718
    }
719

720
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
302,049,244✔
721
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
57,081,501✔
722
      pIter->pSttBlk = NULL;
222,906✔
723
      pIter->ignoreEarlierTs = true;
166,257✔
724
    }
725
  }
726

727
  return code;
929,819,431✔
728
}
729

730
void tLDataIterClose2(SLDataIter *pIter) {
366,044,625✔
731
  tsdbSttFileReaderClose(&pIter->pReader);
366,044,625✔
732
  pIter->pReader = NULL;
366,023,781✔
733
}
366,042,192✔
734

735
void tLDataIterNextBlock(SLDataIter *pIter, const char *idStr) {
308,877,218✔
736
  int32_t step = pIter->backward ? -1 : 1;
308,877,218✔
737
  int32_t oldIndex = pIter->iSttBlk;
308,933,900✔
738

739
  pIter->iSttBlk += step;
308,967,328✔
740

741
  int32_t index = -1;
308,927,839✔
742
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
308,927,839✔
743
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
309,477,772✔
744
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
33,773,856✔
745
    if ((!pIter->backward) && p->minUid > pIter->uid) {
33,777,730✔
746
      break;
18,718,171✔
747
    }
748

749
    if (pIter->backward && p->maxUid < pIter->uid) {
15,062,049✔
750
      break;
3,164,310✔
751
    }
752

753
    // check uid firstly
754
    if (p->minUid <= pIter->uid && p->maxUid >= pIter->uid) {
11,895,649✔
755
      if ((!pIter->backward) && p->minKey > pIter->timeWindow.ekey) {
11,894,794✔
756
        break;
82,963✔
757
      }
758

759
      if (pIter->backward && p->maxKey < pIter->timeWindow.skey) {
11,811,332✔
760
        break;
592✔
761
      }
762

763
      // check time range secondly
764
      if (p->minKey <= pIter->timeWindow.ekey && p->maxKey >= pIter->timeWindow.skey) {
11,810,740✔
765
        if ((!pIter->backward) && p->minVer > pIter->verRange.maxVer) {
11,261,000✔
766
          break;
×
767
        }
768

769
        if (pIter->backward && p->maxVer < pIter->verRange.minVer) {
11,261,499✔
770
          break;
×
771
        }
772

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

781
  pIter->pSttBlk = NULL;
308,987,858✔
782
  if (index != -1) {
308,802,481✔
783
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
11,261,000✔
784

785
    pIter->iSttBlk = index;
11,261,000✔
786
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
11,261,000✔
787
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
11,261,000✔
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);
297,541,481✔
792
  }
793
}
308,802,481✔
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✔
803
    tsdbError("failed to load stt block, code:%s, %s", tstrerror(code), idStr);
×
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);
301,086,464✔
810
    if (i == -1) {
301,026,970✔
811
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
27,773✔
812
      pIter->iRow = -1;
27,773✔
813
      return code;
27,773✔
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;
106,138,372✔
822
        }
823
      } else {
824
        if (pData->aUid[i] < pIter->uid) {
2,147,483,647✔
825
          break;
46,565,213✔
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;
850,376✔
834
      } else {
835
        if (ts < pIter->timeWindow.skey) {
2,147,483,647✔
836
          continue;
2,147,483,647✔
837
        }
838

839
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
2,147,483,647✔
840
          SRowKey key;
27,138,948✔
841
          tColRowGetKey(pData, i, &key);
27,138,948✔
842
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
27,138,948✔
843
          if (ret < 0) {
27,138,948✔
844
            continue;
×
845
          }
846
        }
847
      }
848
    } else {
849
      if (ts < pIter->timeWindow.skey) {
2,147,483,647✔
850
        break;
769,744✔
851
      } else {
852
        if (ts > pIter->timeWindow.ekey) {
2,147,483,647✔
853
          continue;
50,698,667✔
854
        }
855

856
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
2,147,483,647✔
857
          SRowKey key;
×
858
          tColRowGetKey(pData, i, &key);
×
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✔
869
      continue;
×
870
    }
871

872
    // todo opt handle desc case
873
    if (ver > pIter->verRange.maxVer) {
2,147,483,647✔
874
      continue;
1,088✔
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;
628,371,158✔
897
  }
898

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

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

907
  while (1) {
11,260,002✔
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✔
913
      skipBlock = true;
×
914
      int16_t *aCols = pIter->pBlockLoadInfo->colIds;
×
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)) {
×
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);
320,631,283✔
937
      if (pIter->pSttBlk == NULL) {  // no more data
308,812,759✔
938
        goto _exit;
297,653,621✔
939
      }
940
    } else {
941
      break;
942
    }
943

944
    if (iBlockL != pIter->iSttBlk) {
11,261,000✔
945
      code = loadLastBlock(pIter, idStr, &pBlockData);
11,261,000✔
946
      if ((pBlockData == NULL) || (code != 0)) {
11,260,501✔
UNCOV
947
        lino = __LINE__;
×
UNCOV
948
        goto _exit;
×
949
      }
950

951
      // set start row index
952
      pIter->iRow = pIter->backward ? pBlockData->nRow - 1 : 0;
11,260,501✔
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✔
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;
2,147,483,647✔
981
  } else if (ret > 0) {
2,147,483,647✔
982
    return 1;
2,147,483,647✔
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,382,475,564✔
989
    } else if (ver1 > ver2) {
2,105,396,894✔
990
      return 1;
2,105,508,074✔
991
    } else {
992
      return 0;
×
993
    }
994
  }
995
}
996

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

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

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

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

1016
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
960,124,355✔
1017
  int32_t               code = TSDB_CODE_SUCCESS;
960,124,355✔
1018
  STFileSet *           pFset = (STFileSet *)pConf->pCurrentFileset;
960,124,355✔
1019
  bool                  loadStatisFromDisk = true;
960,190,746✔
1020
  int32_t               lino = 0;
960,190,746✔
1021
  int32_t               numOfLevels = pFset->lvlArr->size;
960,190,746✔
1022
  SSttStatisCacheValue* pValue = NULL;
960,206,332✔
1023
  LRUHandle*            pHandle = NULL;
960,212,630✔
1024
  SSttStatisCacheKey    key = {.suid = pConf->suid, .fid = pFset->fid, .vgId = TD_VID(pConf->pTsdb->pVnode)};
960,220,375✔
1025

1026
  (void)taosThreadOnce(&tsCacheInit, initTableRowsInfoCache);
960,206,975✔
1027

1028
  pMTree->pIter = NULL;
960,005,779✔
1029
  pMTree->backward = pConf->backward;
960,093,448✔
1030
  pMTree->idStr = pConf->idstr;
960,102,719✔
1031

1032
  if (!pMTree->backward) {  // asc
960,051,159✔
1033
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
707,828,639✔
1034
  } else {  // desc
1035
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
252,233,436✔
1036
  }
1037

1038
  pMTree->ignoreEarlierTs = false;
960,019,413✔
1039

1040
  // no data exists, go to end
1041
  if (numOfLevels == 0) {
959,963,239✔
1042
    goto _end;
59,663,488✔
1043
  }
1044

1045
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
900,299,751✔
1046
  if (code) {
900,341,554✔
1047
    goto _end;
×
1048
  }
1049

1050
  if (pConf->cacheStatis) {
900,341,554✔
1051
    int32_t ret = getStatisInfoFromCache(statisCacheInfo.pStatisFileCache, &key, &pValue, &pHandle, pConf->idstr);
1,140✔
1052
    if (ret == TSDB_CODE_SUCCESS) {  // use cached statis info
1,170✔
1053
      if (pValue->commitTs == pFset->lastCommit) {
1,050✔
1054
        loadStatisFromDisk = false;
1,050✔
1055
      } else {  // release the handle ref, and then remove it from lru cache
1056
        int64_t ts = pValue->commitTs;
×
1057
        clearStatisInfoCache(statisCacheInfo.pStatisFileCache, &key, &pHandle);
×
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) {
1,830,155,508✔
1067
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
929,637,575✔
1068
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
929,828,077✔
1069

1070
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
1,860,209,563✔
1071
      SLDataIter *pIter = taosArrayGetP(pList, i);
930,197,113✔
1072

1073
      SSttFileReader    *pSttFileReader = pIter->pReader;
930,254,040✔
1074
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
930,244,190✔
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) {
930,262,486✔
1079
        int32_t pgSize = pConf->pTsdb->pVnode->config.tsdbPageSize;
366,025,811✔
1080

1081
        SSttFileReaderConfig conf = {
732,126,551✔
1082
            .tsdb = pConf->pTsdb, .szPage = pgSize, .file[0] = *pSttLevel->fobjArr->data[i]->f};
366,025,328✔
1083

1084
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
366,075,729✔
1085
        if (code != TSDB_CODE_SUCCESS) {
365,812,809✔
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) {
929,961,903✔
1092
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
365,763,897✔
1093
        if (code != TSDB_CODE_SUCCESS) {
366,030,712✔
1094
          goto _end;
×
1095
        }
1096
      }
1097

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

1105
      memset(pIter, 0, sizeof(SLDataIter));
930,228,718✔
1106

1107
      SSttKeyRange range = {.skey.numOfPKs = pConf->pCurRowKey->numOfPKs, .ekey.numOfPKs = pConf->pCurRowKey->numOfPKs};
930,228,718✔
1108
      int64_t      numOfRows = 0;
930,363,159✔
1109
      int64_t      cid = pSttLevel->fobjArr->data[i]->f->cid;
930,391,743✔
1110

1111
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
930,389,424✔
1112
                             pMTree->idStr, loadStatisFromDisk);
1113
      if (code != TSDB_CODE_SUCCESS) {
929,994,539✔
1114
        goto _end;
×
1115
      }
1116

1117
      bool hasVal = NULL;
929,994,539✔
1118
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
930,117,772✔
1119
      if (code) {
929,882,329✔
1120
        goto _end;
×
1121
      }
1122

1123
      if (hasVal) {
929,882,329✔
1124
        tMergeTreeAddIter(pMTree, pIter);
299,828,982✔
1125

1126
        // let's record the time window for current table of uid in the stt files
1127
        if (pSttDataInfo != NULL && numOfRows > 0) {
299,898,697✔
1128
          void *px = taosArrayPush(pSttDataInfo->pKeyRangeList, &range);
299,882,587✔
1129
          QUERY_CHECK_NULL(px, code, lino, _end, terrno);
299,985,256✔
1130

1131
          pSttDataInfo->numOfRows += numOfRows;
299,985,256✔
1132
        }
1133
      } else {
1134
        if (!pMTree->ignoreEarlierTs) {
630,053,347✔
1135
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
630,001,158✔
1136
        }
1137
      }
1138
    }
1139
  }
1140

1141
  if (pConf->cacheStatis && loadStatisFromDisk) {
900,517,933✔
1142
    SSttStatisCacheKey    k = {0};
120✔
1143
    SSttStatisCacheValue *pVal = NULL;
120✔
1144

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

1151
  if (pHandle != NULL && pConf->cacheStatis) {
900,216,774✔
1152
    releaseCacheHandle(statisCacheInfo.pStatisFileCache, &pHandle, true);
1,020✔
1153
  }
1154

1155
  return code;
900,263,864✔
1156

1157
_end:
59,610,416✔
1158
  if (pHandle != NULL && pConf->cacheStatis) {
59,664,365✔
1159
    releaseCacheHandle(statisCacheInfo.pStatisFileCache, &pHandle, true);
×
1160
  }
1161

1162
  tMergeTreeClose(pMTree);
59,664,365✔
1163
  return code;
59,663,501✔
1164
}
1165

1166
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
299,854,290✔
1167
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
299,854,290✔
1168
}
299,903,535✔
1169

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

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

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✔
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✔
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✔
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) {
292,229,497✔
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;
292,229,497✔
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);
2,147,483,647✔
1250
      if (c > 0) {
2,147,483,647✔
1251
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
1,351,371,144✔
1252
        pMTree->pIter = NULL;
1,351,532,464✔
1253
      } else if (!c) {
1,758,123,150✔
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);
2,147,483,647✔
1263
    if (pMTree->pIter) {
2,147,483,647✔
1264
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
1,647,508,534✔
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) {
1,472,487,059✔
1273
  pMTree->pIter = NULL;
1,472,487,059✔
1274
  pMTree->pPinnedBlockIter = NULL;
1,472,749,241✔
1275
}
1,472,719,580✔
1276

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

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

1286
  STFileSet *pFset = (STFileSet *)pConf->pCurrentFileset;
120✔
1287

1288
  pKey->suid = pConf->suid;
120✔
1289
  pKey->vgId = vgId;
120✔
1290
  pKey->fid = pFset->fid;
120✔
1291

1292
  (*pValue)->commitTs = pFset->lastCommit;
120✔
1293

1294
  int32_t numOfLevels = TARRAY2_SIZE(pFset->lvlArr);
120✔
1295

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

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

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

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

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

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

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

1331
  // todo handle memory failure
1332

1333
  return TSDB_CODE_SUCCESS;
120✔
1334
}
1335

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

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

1348
  void *p = taosLRUCacheValue(pCache, pItemHandle);
1,050✔
1349

1350
  *pValue = p;
1,050✔
1351
  *pHandle = pItemHandle;
1,050✔
1352

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

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

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

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

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

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

1377
  if (value == NULL) {
120✔
1378
    return;
×
1379
  }
1380

1381
  SSttStatisCacheKey *  pKey = (SSttStatisCacheKey *)key;
120✔
1382
  SSttStatisCacheValue *pVal = value;
120✔
1383

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

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

1397
    taosArrayDestroy(pInfos);
120✔
1398
  }
1399

1400
  taosArrayDestroy(pVal->pLevel);
120✔
1401
  taosMemoryFree(pVal);
120✔
1402
}
1403

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

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

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

1425
void clearStatisInfoCache(SLRUCache *pStatisCache, SSttStatisCacheKey *pKey, LRUHandle** pHandle) {
×
1426
  (void)taosThreadMutexLock(&statisCacheInfo.lock);
×
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){
×
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) {
780✔
1443
  int32_t code = 0;
780✔
1444

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

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

1476
  return TSDB_CODE_SUCCESS;
780✔
1477
}
1478

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

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

1490
  if (fileIdx >= taosArrayGetSize(pRowsInfoArr)) {
780✔
1491
    return TSDB_CODE_INVALID_PARA;
×
1492
  }
1493

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

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

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

1514
  return size;
120✔
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