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

taosdata / TDengine / #3527

12 Nov 2024 08:50PM UTC coverage: 60.819% (+0.6%) from 60.225%
#3527

push

travis-ci

GitHub
Merge pull request #28730 from taosdata/doc/analysis

118574 of 249004 branches covered (47.62%)

Branch coverage included in aggregate %.

199137 of 273386 relevant lines covered (72.84%)

15792295.89 hits per line

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

74.31
/source/dnode/vnode/src/tsdb/tsdbReadUtil.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 "tsdbReadUtil.h"
17
#include "tsdb.h"
18
#include "tsdbDataFileRW.h"
19
#include "tsdbFS2.h"
20
#include "tsdbMerge.h"
21
#include "tsdbUtil2.h"
22
#include "tsimplehash.h"
23

24
static bool overlapWithDelSkylineWithoutVer(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord,
25
                                            int32_t order);
26

27
static int32_t initBlockScanInfoBuf(SBlockInfoBuf* pBuf, int32_t numOfTables) {
5,025,922✔
28
  int32_t num = numOfTables / pBuf->numPerBucket;
5,025,922✔
29
  int32_t remainder = numOfTables % pBuf->numPerBucket;
5,025,922✔
30

31
  if (pBuf->pData == NULL) {
5,025,922!
32
    pBuf->pData = taosArrayInit(num + 1, POINTER_BYTES);
5,031,201✔
33
    if (pBuf->pData == NULL) {
5,039,500!
34
      return terrno;
×
35
    }
36
  }
37

38
  for (int32_t i = 0; i < num; ++i) {
5,038,443✔
39
    char* p = taosMemoryCalloc(pBuf->numPerBucket, sizeof(STableBlockScanInfo));
82✔
40
    if (p == NULL) {
82!
41
      return terrno;
×
42
    }
43

44
    void* px = taosArrayPush(pBuf->pData, &p);
82✔
45
    if (px == NULL) {
82!
46
      return terrno;
×
47
    }
48
  }
49

50
  if (remainder > 0) {
5,038,361✔
51
    char* p = taosMemoryCalloc(remainder, sizeof(STableBlockScanInfo));
4,917,592✔
52
    if (p == NULL) {
4,922,557!
53
      return terrno;
×
54
    }
55
    void* px = taosArrayPush(pBuf->pData, &p);
4,922,557✔
56
    if (px == NULL) {
4,923,141!
57
      return terrno;
×
58
    }
59
  }
60

61
  pBuf->numOfTables = numOfTables;
5,043,910✔
62

63
  return TSDB_CODE_SUCCESS;
5,043,910✔
64
}
65

66
int32_t uidComparFunc(const void* p1, const void* p2) {
11,110,886✔
67
  uint64_t pu1 = *(uint64_t*)p1;
11,110,886✔
68
  uint64_t pu2 = *(uint64_t*)p2;
11,110,886✔
69
  if (pu1 == pu2) {
11,110,886!
70
    return 0;
×
71
  } else {
72
    return (pu1 < pu2) ? -1 : 1;
11,110,886✔
73
  }
74
}
75

76
int32_t ensureBlockScanInfoBuf(SBlockInfoBuf* pBuf, int32_t numOfTables) {
6✔
77
  if (numOfTables <= pBuf->numOfTables) {
6!
78
    return TSDB_CODE_SUCCESS;
×
79
  }
80

81
  if (pBuf->numOfTables > 0) {
6!
82
    STableBlockScanInfo** p = (STableBlockScanInfo**)taosArrayPop(pBuf->pData);
6✔
83
    taosMemoryFree(*p);
6✔
84
    pBuf->numOfTables /= pBuf->numPerBucket;
6✔
85
  }
86

87
  int32_t num = (numOfTables - pBuf->numOfTables) / pBuf->numPerBucket;
6✔
88
  int32_t remainder = (numOfTables - pBuf->numOfTables) % pBuf->numPerBucket;
6✔
89
  if (pBuf->pData == NULL) {
6!
90
    pBuf->pData = taosArrayInit(num + 1, POINTER_BYTES);
×
91
    if (pBuf->pData == NULL) {
×
92
      return terrno;
×
93
    }
94
  }
95

96
  for (int32_t i = 0; i < num; ++i) {
6!
97
    char* p = taosMemoryCalloc(pBuf->numPerBucket, sizeof(STableBlockScanInfo));
×
98
    if (p == NULL) {
×
99
      return terrno;
×
100
    }
101

102
    void* px = taosArrayPush(pBuf->pData, &p);
×
103
    if (px == NULL) {
×
104
      return terrno;
×
105
    }
106
  }
107

108
  if (remainder > 0) {
6!
109
    char* p = taosMemoryCalloc(remainder, sizeof(STableBlockScanInfo));
6✔
110
    if (p == NULL) {
6!
111
      return terrno;
×
112
    }
113
    void* px = taosArrayPush(pBuf->pData, &p);
6✔
114
    if (px == NULL) {
6!
115
      return terrno;
×
116
    }
117
  }
118

119
  pBuf->numOfTables = numOfTables;
6✔
120

121
  return TSDB_CODE_SUCCESS;
6✔
122
}
123

124
void clearBlockScanInfoBuf(SBlockInfoBuf* pBuf) {
5,092,557✔
125
  size_t num = taosArrayGetSize(pBuf->pData);
5,092,557✔
126
  for (int32_t i = 0; i < num; ++i) {
10,020,063✔
127
    char** p = taosArrayGet(pBuf->pData, i);
4,927,150✔
128
    if (p != NULL) {
4,926,879!
129
      taosMemoryFree(*p);
4,926,910✔
130
    }
131
  }
132

133
  taosArrayDestroy(pBuf->pData);
5,092,913✔
134
}
5,092,837✔
135

136
int32_t getPosInBlockInfoBuf(SBlockInfoBuf* pBuf, int32_t index, STableBlockScanInfo** pInfo) {
12,893,871✔
137
  *pInfo = NULL;
12,893,871✔
138

139
  int32_t bucketIndex = index / pBuf->numPerBucket;
12,893,871✔
140
  char**  pBucket = taosArrayGet(pBuf->pData, bucketIndex);
12,893,871✔
141
  if (pBucket == NULL) {
12,889,550!
142
    return TSDB_CODE_NOT_FOUND;
×
143
  }
144

145
  *pInfo = (STableBlockScanInfo*)((*pBucket) + (index % pBuf->numPerBucket) * sizeof(STableBlockScanInfo));
12,889,550✔
146
  return TSDB_CODE_SUCCESS;
12,889,550✔
147
}
148

149
int32_t getTableBlockScanInfo(SSHashObj* pTableMap, uint64_t uid, STableBlockScanInfo** pInfo, const char* id) {
1,927,877✔
150
  *pInfo = *(STableBlockScanInfo**)tSimpleHashGet(pTableMap, &uid, sizeof(uid));
1,927,877✔
151
  if (pInfo == NULL) {
1,927,927✔
152
    int32_t size = tSimpleHashGetSize(pTableMap);
159✔
153
    tsdbError("failed to locate the uid:%" PRIu64 " in query table uid list, total tables:%d, %s", uid, size, id);
×
154
    return TSDB_CODE_INVALID_PARA;
×
155
  }
156

157
  return TSDB_CODE_SUCCESS;
1,927,768✔
158
}
159

160
int32_t initRowKey(SRowKey* pKey, int64_t ts, int32_t numOfPks, int32_t type, int32_t len, bool asc) {
56,417,210✔
161
  pKey->numOfPKs = numOfPks;
56,417,210✔
162
  pKey->ts = ts;
56,417,210✔
163

164
  if (numOfPks > 0) {
56,417,210✔
165
    pKey->pks[0].type = type;
1,898,344✔
166

167
    if (IS_NUMERIC_TYPE(type)) {
1,898,344!
168
      if (asc) {
1,646,514✔
169
        switch (type) {
1,138,336!
170
          case TSDB_DATA_TYPE_BIGINT: {
862,390✔
171
            pKey->pks[0].val = INT64_MIN;
862,390✔
172
            break;
862,390✔
173
          }
174
          case TSDB_DATA_TYPE_INT: {
94,795✔
175
            int32_t min = INT32_MIN;
94,795✔
176
            (void)memcpy(&pKey->pks[0].val, &min, tDataTypes[type].bytes);
94,795✔
177
            break;
94,795✔
178
          }
179
          case TSDB_DATA_TYPE_SMALLINT: {
×
180
            int16_t min = INT16_MIN;
×
181
            (void)memcpy(&pKey->pks[0].val, &min, tDataTypes[type].bytes);
×
182
            break;
×
183
          }
184
          case TSDB_DATA_TYPE_TINYINT: {
×
185
            int8_t min = INT8_MIN;
×
186
            (void)memcpy(&pKey->pks[0].val, &min, tDataTypes[type].bytes);
×
187
            break;
×
188
          }
189
          case TSDB_DATA_TYPE_UTINYINT:
181,141✔
190
          case TSDB_DATA_TYPE_USMALLINT:
191
          case TSDB_DATA_TYPE_UINT:
192
          case TSDB_DATA_TYPE_UBIGINT: {
193
            pKey->pks[0].val = 0;
181,141✔
194
            break;
181,141✔
195
          }
196
          default:
10✔
197
            return TSDB_CODE_INVALID_PARA;
10✔
198
        }
199
      } else {
200
        switch (type) {
508,178!
201
          case TSDB_DATA_TYPE_BIGINT:
338,837✔
202
            pKey->pks[0].val = INT64_MAX;
338,837✔
203
            break;
338,837✔
204
          case TSDB_DATA_TYPE_INT:
57,223✔
205
            pKey->pks[0].val = INT32_MAX;
57,223✔
206
            break;
57,223✔
207
          case TSDB_DATA_TYPE_SMALLINT:
×
208
            pKey->pks[0].val = INT16_MAX;
×
209
            break;
×
210
          case TSDB_DATA_TYPE_TINYINT:
×
211
            pKey->pks[0].val = INT8_MAX;
×
212
            break;
×
213
          case TSDB_DATA_TYPE_UBIGINT:
55,449✔
214
            pKey->pks[0].val = UINT64_MAX;
55,449✔
215
            break;
55,449✔
216
          case TSDB_DATA_TYPE_UINT:
56,713✔
217
            pKey->pks[0].val = UINT32_MAX;
56,713✔
218
            break;
56,713✔
219
          case TSDB_DATA_TYPE_USMALLINT:
×
220
            pKey->pks[0].val = UINT16_MAX;
×
221
            break;
×
222
          case TSDB_DATA_TYPE_UTINYINT:
×
223
            pKey->pks[0].val = UINT8_MAX;
×
224
            break;
×
225
          default:
×
226
            return TSDB_CODE_INVALID_PARA;
×
227
        }
228
      }
229
    } else {
230
      pKey->pks[0].pData = taosMemoryCalloc(1, len);
251,830✔
231
      pKey->pks[0].nData = 0;
251,871✔
232

233
      if (pKey->pks[0].pData == NULL) {
251,871!
234
        return terrno;
×
235
      }
236

237
      if (!asc) {
266,978✔
238
        pKey->numOfPKs = 2;
109,502✔
239
      }
240
    }
241
  }
242

243
  return TSDB_CODE_SUCCESS;
56,432,392✔
244
}
245

246
void clearRowKey(SRowKey* pKey) {
56,510,064✔
247
  if (pKey == NULL || pKey->numOfPKs == 0 || (!IS_VAR_DATA_TYPE(pKey->pks[0].type))) {
56,510,064!
248
    return;
56,260,697✔
249
  }
250
  taosMemoryFreeClear(pKey->pks[0].pData);
249,367!
251
}
252

253
static int32_t initLastProcKey(STableBlockScanInfo* pScanInfo, STsdbReader* pReader) {
12,883,262✔
254
  int32_t code = 0;
12,883,262✔
255
  int32_t numOfPks = pReader->suppInfo.numOfPks;
12,883,262✔
256
  bool    asc = ASCENDING_TRAVERSE(pReader->info.order);
12,883,262✔
257
  int8_t  type = pReader->suppInfo.pk.type;
12,883,262✔
258
  int32_t bytes = pReader->suppInfo.pk.bytes;
12,883,262✔
259

260
  SRowKey* pRowKey = &pScanInfo->lastProcKey;
12,883,262✔
261
  if (asc) {
12,883,262✔
262
    int64_t skey = pReader->info.window.skey;
10,511,162✔
263
    int64_t ts = (skey > INT64_MIN) ? (skey - 1) : skey;
10,511,162!
264

265
    code = initRowKey(pRowKey, ts, numOfPks, type, bytes, asc);
10,511,162✔
266
    if (code != TSDB_CODE_SUCCESS) {
10,507,806!
267
      return code;
×
268
    }
269

270
    code = initRowKey(&pScanInfo->sttKeyInfo.nextProcKey, skey, numOfPks, type, bytes, asc);
10,507,806✔
271
    if (code != TSDB_CODE_SUCCESS) {
10,507,600!
272
      return code;
×
273
    }
274
  } else {
275
    int64_t ekey = pReader->info.window.ekey;
2,372,100✔
276
    int64_t ts = (ekey < INT64_MAX) ? (ekey + 1) : ekey;
2,372,100✔
277

278
    code = initRowKey(pRowKey, ts, numOfPks, type, bytes, asc);
2,372,100✔
279
    if (code != TSDB_CODE_SUCCESS) {
2,382,055!
280
      return code;
×
281
    }
282

283
    code = initRowKey(&pScanInfo->sttKeyInfo.nextProcKey, ekey, numOfPks, type, bytes, asc);
2,382,055✔
284
    if (code != TSDB_CODE_SUCCESS) {
2,382,184!
285
      return code;
×
286
    }
287
  }
288

289
  code = initRowKey(&pScanInfo->sttRange.skey, INT64_MAX, numOfPks, type, bytes, asc);
12,889,784✔
290
  if (code != TSDB_CODE_SUCCESS) {
12,878,048!
291
    return code;
×
292
  }
293

294
  code = initRowKey(&pScanInfo->sttRange.ekey, INT64_MIN, numOfPks, type, bytes, asc);
12,878,048✔
295
  return code;
12,881,487✔
296
}
297

298
int32_t initTableBlockScanInfo(STableBlockScanInfo* pScanInfo, uint64_t uid, SSHashObj* pTableMap,
12,886,254✔
299
                               STsdbReader* pReader) {
300
  pScanInfo->uid = uid;
12,886,254✔
301
  INIT_KEYRANGE(&pScanInfo->sttRange);
12,886,254✔
302
  INIT_TIMEWINDOW(&pScanInfo->filesetWindow);
12,886,254✔
303

304
  pScanInfo->cleanSttBlocks = false;
12,886,254✔
305
  pScanInfo->sttBlockReturned = false;
12,886,254✔
306

307
  int32_t code = initLastProcKey(pScanInfo, pReader);
12,886,254✔
308
  if (code != TSDB_CODE_SUCCESS) {
12,882,349!
309
    return code;
×
310
  }
311

312
  pScanInfo->sttKeyInfo.status = STT_FILE_READER_UNINIT;
12,882,349✔
313
  code = tSimpleHashPut(pTableMap, &pScanInfo->uid, sizeof(uint64_t), &pScanInfo, POINTER_BYTES);
12,882,349✔
314
  if (code != TSDB_CODE_SUCCESS) {
12,893,923!
315
    return code;
×
316
  }
317

318
  tsdbTrace("%p check table uid:%" PRId64 " from lastKey:%" PRId64 " %s", pReader, pScanInfo->uid,
12,893,923✔
319
            pScanInfo->lastProcKey.ts, pReader->idStr);
320
  return code;
12,894,890✔
321
}
322

323
// NOTE: speedup the whole processing by preparing the buffer for STableBlockScanInfo in batch model
324
int32_t createDataBlockScanInfo(STsdbReader* pTsdbReader, SBlockInfoBuf* pBuf, const STableKeyInfo* idList,
5,026,180✔
325
                                STableUidList* pUidList, int32_t numOfTables, SSHashObj** pHashObj) {
326
  int32_t code = 0;
5,026,180✔
327
  *pHashObj = NULL;
5,026,180✔
328

329
  // allocate buffer in order to load data blocks from file
330
  // todo use simple hash instead, optimize the memory consumption
331
  SSHashObj* pTableMap = tSimpleHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
5,026,180✔
332
  if (pTableMap == NULL) {
5,039,597!
333
    return terrno;
×
334
  }
335

336
  int64_t st = taosGetTimestampUs();
5,035,018✔
337
  code = initBlockScanInfoBuf(pBuf, numOfTables);
5,035,018✔
338
  if (code != TSDB_CODE_SUCCESS) {
5,037,763!
339
    tSimpleHashCleanup(pTableMap);
×
340
    return code;
×
341
  }
342

343
  pUidList->tableUidList = taosMemoryMalloc(numOfTables * sizeof(uint64_t));
5,037,763✔
344
  if (pUidList->tableUidList == NULL) {
5,039,834✔
345
    tSimpleHashCleanup(pTableMap);
4,121✔
346
    return terrno;
×
347
  }
348

349
  pUidList->currentIndex = 0;
5,035,713✔
350

351
  for (int32_t j = 0; j < numOfTables; ++j) {
17,911,955✔
352
    pUidList->tableUidList[j] = idList[j].uid;
12,877,565✔
353

354
    STableBlockScanInfo* pScanInfo = NULL;
12,877,565✔
355
    code = getPosInBlockInfoBuf(pBuf, j, &pScanInfo);
12,877,565✔
356
    if (code != TSDB_CODE_SUCCESS) {
12,872,289!
357
      break;
×
358
    }
359

360
    code = initTableBlockScanInfo(pScanInfo, idList[j].uid, pTableMap, pTsdbReader);
12,872,289✔
361
    if (code != TSDB_CODE_SUCCESS) {
12,876,242!
362
      break;
×
363
    }
364
  }
365

366
  taosSort(pUidList->tableUidList, numOfTables, sizeof(uint64_t), uidComparFunc);
5,034,390✔
367

368
  pTsdbReader->cost.createScanInfoList = (taosGetTimestampUs() - st) / 1000.0;
5,039,162✔
369
  tsdbDebug("%p create %d tables scan-info, size:%.2f Kb, elapsed time:%.2f ms, %s", pTsdbReader, numOfTables,
5,039,162✔
370
            (sizeof(STableBlockScanInfo) * numOfTables) / 1024.0, pTsdbReader->cost.createScanInfoList,
371
            pTsdbReader->idStr);
372

373
  *pHashObj = pTableMap;
5,040,549✔
374
  return code;
5,040,549✔
375
}
376

377
void resetAllDataBlockScanInfo(SSHashObj* pTableMap, int64_t ts, int32_t step) {
24,531✔
378
  void*   p = NULL;
24,531✔
379
  int32_t iter = 0;
24,531✔
380

381
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
49,100✔
382
    STableBlockScanInfo* pInfo = *(STableBlockScanInfo**)p;
24,569✔
383

384
    pInfo->iterInit = false;
24,569✔
385
    pInfo->iter.hasVal = false;
24,569✔
386
    pInfo->iiter.hasVal = false;
24,569✔
387

388
    if (pInfo->iter.iter != NULL) {
24,569✔
389
      pInfo->iter.iter = tsdbTbDataIterDestroy(pInfo->iter.iter);
3,816✔
390
    }
391

392
    if (pInfo->iiter.iter != NULL) {
24,569✔
393
      pInfo->iiter.iter = tsdbTbDataIterDestroy(pInfo->iiter.iter);
17✔
394
    }
395

396
    taosArrayDestroy(pInfo->delSkyline);
24,569✔
397
    pInfo->delSkyline = NULL;
24,569✔
398
    pInfo->lastProcKey.ts = ts;
24,569✔
399
    // todo check the nextProcKey info
400
    pInfo->sttKeyInfo.nextProcKey.ts = ts + step;
24,569✔
401
  }
402
}
24,529✔
403

404
void clearBlockScanInfo(STableBlockScanInfo* p) {
12,903,018✔
405
  p->iterInit = false;
12,903,018✔
406
  p->iter.hasVal = false;
12,903,018✔
407
  p->iiter.hasVal = false;
12,903,018✔
408
  p->sttKeyInfo.status = STT_FILE_READER_UNINIT;
12,903,018✔
409

410
  if (p->iter.iter != NULL) {
12,903,018✔
411
    p->iter.iter = tsdbTbDataIterDestroy(p->iter.iter);
4,563,803✔
412
  }
413

414
  if (p->iiter.iter != NULL) {
12,904,987✔
415
    p->iiter.iter = tsdbTbDataIterDestroy(p->iiter.iter);
26,771✔
416
  }
417

418
  taosArrayDestroy(p->delSkyline);
12,905,962✔
419
  p->delSkyline = NULL;
12,902,420✔
420
  taosArrayDestroy(p->pBlockList);
12,902,420✔
421
  p->pBlockList = NULL;
12,902,134✔
422
  taosArrayDestroy(p->pBlockIdxList);
12,902,134✔
423
  p->pBlockIdxList = NULL;
12,901,738✔
424
  taosArrayDestroy(p->pMemDelData);
12,901,738✔
425
  p->pMemDelData = NULL;
12,908,704✔
426
  taosArrayDestroy(p->pFileDelData);
12,908,704✔
427
  p->pFileDelData = NULL;
12,907,835✔
428

429
  clearRowKey(&p->lastProcKey);
12,907,835✔
430
  clearRowKey(&p->sttRange.skey);
12,906,339✔
431
  clearRowKey(&p->sttRange.ekey);
12,905,019✔
432
  clearRowKey(&p->sttKeyInfo.nextProcKey);
12,903,578✔
433
}
12,901,784✔
434

435
void destroyAllBlockScanInfo(SSHashObj* pTableMap) {
5,040,606✔
436
  void*   p = NULL;
5,040,606✔
437
  int32_t iter = 0;
5,040,606✔
438

439
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
17,922,860✔
440
    clearBlockScanInfo(*(STableBlockScanInfo**)p);
12,883,476✔
441
  }
442

443
  tSimpleHashCleanup(pTableMap);
5,036,094✔
444
}
5,041,365✔
445

446
static void doCleanupInfoForNextFileset(STableBlockScanInfo* pScanInfo) {
16,622,894✔
447
  // reset the index in last block when handing a new file
448
  taosArrayClear(pScanInfo->pBlockList);
16,622,894✔
449
  taosArrayClear(pScanInfo->pBlockIdxList);
16,625,441✔
450
  taosArrayClear(pScanInfo->pFileDelData);  // del data from each file set
16,625,214✔
451
  pScanInfo->cleanSttBlocks = false;
16,625,465✔
452
  pScanInfo->numOfRowsInStt = 0;
16,625,465✔
453
  pScanInfo->sttBlockReturned = false;
16,625,465✔
454
  INIT_KEYRANGE(&pScanInfo->sttRange);
16,625,465✔
455
  INIT_TIMEWINDOW(&pScanInfo->filesetWindow);
16,625,465✔
456
  pScanInfo->sttKeyInfo.status = STT_FILE_READER_UNINIT;
16,625,465✔
457
}
16,625,465✔
458

459
void cleanupInfoForNextFileset(SSHashObj* pTableMap) {
7,866,419✔
460
  STableBlockScanInfo** p = NULL;
7,866,419✔
461

462
  int32_t iter = 0;
7,866,419✔
463
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
24,490,031✔
464
    doCleanupInfoForNextFileset(*p);
16,626,818✔
465
  }
466
}
7,859,720✔
467

468
// brin records iterator
469
void initBrinRecordIter(SBrinRecordIter* pIter, SDataFileReader* pReader, SArray* pList) {
7,868,838✔
470
  (void)memset(&pIter->block, 0, sizeof(SBrinBlock));
7,868,838✔
471
  (void)memset(&pIter->record, 0, sizeof(SBrinRecord));
7,868,838✔
472
  pIter->blockIndex = -1;
7,868,838✔
473
  pIter->recordIndex = -1;
7,868,838✔
474

475
  pIter->pReader = pReader;
7,868,838✔
476
  pIter->pBrinBlockList = pList;
7,868,838✔
477
}
7,868,838✔
478

479
int32_t getNextBrinRecord(SBrinRecordIter* pIter, SBrinRecord** pRecord) {
10,051,335✔
480
  *pRecord = NULL;
10,051,335✔
481

482
  if (pIter->blockIndex == -1 || (pIter->recordIndex + 1) >= pIter->block.numOfRecords) {
10,051,335✔
483
    pIter->blockIndex += 1;
7,984,251✔
484
    if (pIter->blockIndex >= taosArrayGetSize(pIter->pBrinBlockList)) {
7,984,251✔
485
      return TSDB_CODE_SUCCESS;
7,857,023✔
486
    }
487

488
    pIter->pCurrentBlk = taosArrayGet(pIter->pBrinBlockList, pIter->blockIndex);
128,356✔
489
    if (pIter->pCurrentBlk == NULL) {
128,604!
490
      return TSDB_CODE_INVALID_PARA;
×
491
    }
492

493
    tBrinBlockClear(&pIter->block);
128,604✔
494
    int32_t code = tsdbDataFileReadBrinBlock(pIter->pReader, pIter->pCurrentBlk, &pIter->block);
128,784✔
495
    if (code != TSDB_CODE_SUCCESS) {
128,731!
496
      tsdbError("failed to read brinBlock from file, code:%s", tstrerror(code));
×
497
      return code;
×
498
    }
499

500
    pIter->recordIndex = -1;
128,731✔
501
  }
502

503
  pIter->recordIndex += 1;
2,195,815✔
504
  int32_t code = tBrinBlockGet(&pIter->block, pIter->recordIndex, &pIter->record);
2,195,815✔
505
  *pRecord = &pIter->record;
2,195,720✔
506

507
  return code;
2,195,720✔
508
}
509

510
void clearBrinBlockIter(SBrinRecordIter* pIter) { tBrinBlockDestroy(&pIter->block); }
7,867,654✔
511

512
// initialize the file block access order
513
//  sort the file blocks according to the offset of each data block in the files
514
static void cleanupBlockOrderSupporter(SBlockOrderSupporter* pSup) {
120,012✔
515
  taosMemoryFreeClear(pSup->numOfBlocksPerTable);
120,012!
516
  taosMemoryFreeClear(pSup->indexPerTable);
120,068!
517

518
  for (int32_t i = 0; i < pSup->numOfTables; ++i) {
533,795✔
519
    SBlockOrderWrapper* pBlockInfo = pSup->pDataBlockInfo[i];
413,760✔
520
    taosMemoryFreeClear(pBlockInfo);
413,760✔
521
  }
522

523
  taosMemoryFreeClear(pSup->pDataBlockInfo);
120,035!
524
}
120,039✔
525

526
static int32_t initBlockOrderSupporter(SBlockOrderSupporter* pSup, int32_t numOfTables) {
119,915✔
527
  pSup->pDataBlockInfo = taosMemoryCalloc(1, POINTER_BYTES * numOfTables);
119,915✔
528
  pSup->indexPerTable = taosMemoryCalloc(1, sizeof(int32_t) * numOfTables);
120,100✔
529
  pSup->numOfBlocksPerTable = taosMemoryCalloc(1, sizeof(int32_t) * numOfTables);
120,110✔
530
  pSup->numOfTables = 0;
120,110✔
531
  if (pSup->pDataBlockInfo == NULL || pSup->indexPerTable == NULL || pSup->numOfBlocksPerTable == NULL) {
120,110!
532
    cleanupBlockOrderSupporter(pSup);
5✔
533
    return terrno;
×
534
  }
535

536
  return TSDB_CODE_SUCCESS;
120,105✔
537
}
538

539
static int32_t fileDataBlockOrderCompar(const void* pLeft, const void* pRight, void* param) {
4,091,086✔
540
  int32_t leftIndex = *(int32_t*)pLeft;
4,091,086✔
541
  int32_t rightIndex = *(int32_t*)pRight;
4,091,086✔
542

543
  SBlockOrderSupporter* pSupporter = (SBlockOrderSupporter*)param;
4,091,086✔
544

545
  int32_t leftTableBlockIndex = pSupporter->indexPerTable[leftIndex];
4,091,086✔
546
  int32_t rightTableBlockIndex = pSupporter->indexPerTable[rightIndex];
4,091,086✔
547

548
  if (leftTableBlockIndex > pSupporter->numOfBlocksPerTable[leftIndex]) {
4,091,086✔
549
    /* left block is empty */
550
    return 1;
1,014,007✔
551
  } else if (rightTableBlockIndex > pSupporter->numOfBlocksPerTable[rightIndex]) {
3,077,079✔
552
    /* right block is empty */
553
    return -1;
293,605✔
554
  }
555

556
  SBlockOrderWrapper* pLeftBlock = &pSupporter->pDataBlockInfo[leftIndex][leftTableBlockIndex];
2,783,474✔
557
  SBlockOrderWrapper* pRightBlock = &pSupporter->pDataBlockInfo[rightIndex][rightTableBlockIndex];
2,783,474✔
558

559
  return pLeftBlock->offset > pRightBlock->offset ? 1 : -1;
2,783,474✔
560
}
561

562
int32_t recordToBlockInfo(SFileDataBlockInfo* pBlockInfo, SBrinRecord* record) {
648,143✔
563
  pBlockInfo->uid = record->uid;
648,143✔
564
  pBlockInfo->firstKey = record->firstKey.key.ts;
648,143✔
565
  pBlockInfo->lastKey = record->lastKey.key.ts;
648,143✔
566
  pBlockInfo->minVer = record->minVer;
648,143✔
567
  pBlockInfo->maxVer = record->maxVer;
648,143✔
568
  pBlockInfo->blockOffset = record->blockOffset;
648,143✔
569
  pBlockInfo->smaOffset = record->smaOffset;
648,143✔
570
  pBlockInfo->blockSize = record->blockSize;
648,143✔
571
  pBlockInfo->blockKeySize = record->blockKeySize;
648,143✔
572
  pBlockInfo->smaSize = record->smaSize;
648,143✔
573
  pBlockInfo->numRow = record->numRow;
648,143✔
574
  pBlockInfo->count = record->count;
648,143✔
575

576
  SRowKey* pFirstKey = &record->firstKey.key;
648,143✔
577
  if (pFirstKey->numOfPKs > 0) {
648,143✔
578
    if (IS_NUMERIC_TYPE(pFirstKey->pks[0].type)) {
243,653!
579
      pBlockInfo->firstPk.val = pFirstKey->pks[0].val;
243,653✔
580
      pBlockInfo->lastPk.val = record->lastKey.key.pks[0].val;
243,653✔
581
    } else {
582
      char* p = taosMemoryCalloc(1, pFirstKey->pks[0].nData + VARSTR_HEADER_SIZE);
×
583
      if (p == NULL) {
×
584
        return terrno;
×
585
      }
586
      memcpy(varDataVal(p), pFirstKey->pks[0].pData, pFirstKey->pks[0].nData);
×
587
      varDataSetLen(p, pFirstKey->pks[0].nData);
×
588
      pBlockInfo->firstPk.pData = (uint8_t*)p;
×
589

590
      int32_t keyLen = record->lastKey.key.pks[0].nData;
×
591
      p = taosMemoryCalloc(1, keyLen + VARSTR_HEADER_SIZE);
×
592
      if (p == NULL) {
×
593
        return terrno;
×
594
      }
595
      memcpy(varDataVal(p), record->lastKey.key.pks[0].pData, keyLen);
×
596
      varDataSetLen(p, keyLen);
×
597
      pBlockInfo->lastPk.pData = (uint8_t*)p;
×
598
    }
599
  }
600
  return TSDB_CODE_SUCCESS;
648,143✔
601
}
602

603
static void freePkItem(void* pItem) {
×
604
  SFileDataBlockInfo* p = pItem;
×
605
  taosMemoryFreeClear(p->firstPk.pData);
×
606
  taosMemoryFreeClear(p->lastPk.pData);
×
607
}
×
608

609
void clearDataBlockIterator(SDataBlockIter* pIter, bool needFree) {
7,998,458✔
610
  pIter->index = -1;
7,998,458✔
611
  pIter->numOfBlocks = 0;
7,998,458✔
612

613
  if (needFree) {
7,998,458✔
614
    taosArrayClearEx(pIter->blockList, freePkItem);
383,212✔
615
  } else {
616
    taosArrayClear(pIter->blockList);
7,615,246✔
617
  }
618
}
8,002,004✔
619

620
void cleanupDataBlockIterator(SDataBlockIter* pIter, bool needFree) {
5,091,418✔
621
  pIter->index = -1;
5,091,418✔
622
  pIter->numOfBlocks = 0;
5,091,418✔
623
  if (needFree) {
5,091,418✔
624
    taosArrayDestroyEx(pIter->blockList, freePkItem);
34,994✔
625
  } else {
626
    taosArrayDestroy(pIter->blockList);
5,056,424✔
627
  }
628
}
5,092,706✔
629

630
int32_t initBlockIterator(STsdbReader* pReader, SDataBlockIter* pBlockIter, int32_t numOfBlocks, SArray* pTableList) {
119,914✔
631
  bool asc = ASCENDING_TRAVERSE(pReader->info.order);
119,914✔
632

633
  SBlockOrderSupporter sup = {0};
119,914✔
634
  clearDataBlockIterator(pBlockIter, shouldFreePkBuf(&pReader->suppInfo));
119,914✔
635

636
  pBlockIter->numOfBlocks = numOfBlocks;
119,955✔
637

638
  // access data blocks according to the offset of each block in asc/desc order.
639
  int32_t numOfTables = taosArrayGetSize(pTableList);
119,955✔
640

641
  int64_t st = taosGetTimestampUs();
119,981✔
642
  int32_t code = initBlockOrderSupporter(&sup, numOfTables);
119,981✔
643
  if (code != TSDB_CODE_SUCCESS) {
120,100!
644
    return code;
×
645
  }
646

647
  int32_t cnt = 0;
120,100✔
648

649
  for (int32_t i = 0; i < numOfTables; ++i) {
533,658✔
650
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, i);
413,597✔
651

652
    size_t num = taosArrayGetSize(pTableScanInfo->pBlockList);
413,530✔
653
    sup.numOfBlocksPerTable[sup.numOfTables] = num;
413,534✔
654

655
    char* buf = taosMemoryMalloc(sizeof(SBlockOrderWrapper) * num);
413,534✔
656
    if (buf == NULL) {
413,587!
657
      cleanupBlockOrderSupporter(&sup);
×
658
      return terrno;
×
659
    }
660

661
    sup.pDataBlockInfo[sup.numOfTables] = (SBlockOrderWrapper*)buf;
413,587✔
662

663
    for (int32_t k = 0; k < num; ++k) {
1,062,492✔
664
      SFileDataBlockInfo* pBlockInfo = taosArrayGet(pTableScanInfo->pBlockList, k);
648,934✔
665
      if (pBlockInfo == NULL) {
648,905!
666
        return TSDB_CODE_INVALID_PARA;
×
667
      }
668

669
      sup.pDataBlockInfo[sup.numOfTables][k] =
648,905✔
670
          (SBlockOrderWrapper){.uid = pTableScanInfo->uid, .offset = pBlockInfo->blockOffset, .pInfo = pTableScanInfo};
648,905✔
671
      cnt++;
648,905✔
672
    }
673

674
    sup.numOfTables += 1;
413,558✔
675
  }
676

677
  if (numOfBlocks != cnt && sup.numOfTables != numOfTables) {
120,061!
678
    cleanupBlockOrderSupporter(&sup);
×
679
    return TSDB_CODE_INVALID_PARA;
×
680
  }
681

682
  // since there is only one table qualified, blocks are not sorted
683
  if (sup.numOfTables == 1) {
120,061✔
684
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, 0);
62,354✔
685
    for (int32_t i = 0; i < numOfBlocks; ++i) {
130,138✔
686
      STableDataBlockIdx tableDataBlockIdx = {.globalIndex = i};
67,777✔
687
      void*              px = taosArrayPush(pTableScanInfo->pBlockIdxList, &tableDataBlockIdx);
67,777✔
688
      if (px == NULL) {
67,800!
689
        return terrno;
×
690
      }
691
    }
692

693
    void* p = taosArrayAddAll(pBlockIter->blockList, pTableScanInfo->pBlockList);
62,361✔
694
    if (p == NULL) {
62,338!
695
      return TSDB_CODE_OUT_OF_MEMORY;
×
696
    }
697

698
    taosArrayDestroy(pTableScanInfo->pBlockList);
62,338✔
699
    pTableScanInfo->pBlockList = NULL;
62,378✔
700

701
    int64_t et = taosGetTimestampUs();
62,312✔
702
    tsdbDebug("%p create blocks info struct completed for one table, %d blocks not sorted, elapsed time:%.2f ms %s",
62,312✔
703
              pReader, numOfBlocks, (et - st) / 1000.0, pReader->idStr);
704

705
    pBlockIter->index = asc ? 0 : (numOfBlocks - 1);
62,327✔
706
    cleanupBlockOrderSupporter(&sup);
62,327✔
707
    return TSDB_CODE_SUCCESS;
62,421✔
708
  }
709

710
  tsdbDebug("%p create data blocks info struct completed, %d blocks in %d tables %s", pReader, cnt, sup.numOfTables,
57,707✔
711
            pReader->idStr);
712

713
  SMultiwayMergeTreeInfo* pTree = NULL;
57,707✔
714

715
  uint8_t ret = tMergeTreeCreate(&pTree, sup.numOfTables, &sup, fileDataBlockOrderCompar);
57,707✔
716
  if (ret != TSDB_CODE_SUCCESS) {
57,699!
717
    cleanupBlockOrderSupporter(&sup);
×
718
    return TSDB_CODE_OUT_OF_MEMORY;
×
719
  }
720

721
  int32_t numOfTotal = 0;
57,699✔
722
  while (numOfTotal < cnt) {
638,289✔
723
    int32_t pos = tMergeTreeGetChosenIndex(pTree);
580,593✔
724
    int32_t index = sup.indexPerTable[pos]++;
580,593✔
725

726
    SFileDataBlockInfo* pBlockInfo = taosArrayGet(sup.pDataBlockInfo[pos][index].pInfo->pBlockList, index);
580,593✔
727
    if (pBlockInfo == NULL) {
580,594!
728
      return TSDB_CODE_INVALID_PARA;
×
729
    }
730

731
    void* px = taosArrayPush(pBlockIter->blockList, pBlockInfo);
580,594✔
732
    if (px == NULL) {
580,513!
733
      return terrno;
×
734
    }
735

736
    STableBlockScanInfo* pTableScanInfo = sup.pDataBlockInfo[pos][index].pInfo;
580,513✔
737
    STableDataBlockIdx   tableDataBlockIdx = {.globalIndex = numOfTotal};
580,513✔
738

739
    px = taosArrayPush(pTableScanInfo->pBlockIdxList, &tableDataBlockIdx);
580,513✔
740
    if (px == NULL) {
580,385!
741
      return terrno;
×
742
    }
743

744
    // set data block index overflow, in order to disable the offset comparator
745
    if (sup.indexPerTable[pos] >= sup.numOfBlocksPerTable[pos]) {
580,385✔
746
      sup.indexPerTable[pos] = sup.numOfBlocksPerTable[pos] + 1;
351,258✔
747
    }
748

749
    numOfTotal += 1;
580,385✔
750
    code = tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree));
580,385✔
751
    if (TSDB_CODE_SUCCESS != code) {
580,590!
752
      return code;
×
753
    }
754
  }
755

756
  for (int32_t i = 0; i < numOfTables; ++i) {
409,040✔
757
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, i);
351,340✔
758
    taosArrayDestroy(pTableScanInfo->pBlockList);
351,332✔
759
    pTableScanInfo->pBlockList = NULL;
351,344✔
760
  }
761

762
  int64_t et = taosGetTimestampUs();
57,700✔
763
  tsdbDebug("%p %d data blocks access order completed, elapsed time:%.2f ms %s", pReader, numOfBlocks,
57,700✔
764
            (et - st) / 1000.0, pReader->idStr);
765
  cleanupBlockOrderSupporter(&sup);
57,700✔
766
  taosMemoryFree(pTree);
57,701✔
767

768
  pBlockIter->index = asc ? 0 : (numOfBlocks - 1);
57,701✔
769
  return TSDB_CODE_SUCCESS;
57,701✔
770
}
771

772
bool blockIteratorNext(SDataBlockIter* pBlockIter, const char* idStr) {
628,491✔
773
  bool asc = ASCENDING_TRAVERSE(pBlockIter->order);
628,491✔
774

775
  int32_t step = asc ? 1 : -1;
628,491✔
776
  if ((pBlockIter->index >= pBlockIter->numOfBlocks - 1 && asc) || (pBlockIter->index <= 0 && (!asc))) {
628,491✔
777
    return false;
119,957✔
778
  }
779

780
  pBlockIter->index += step;
508,534✔
781
  return true;
508,534✔
782
}
783

784
typedef enum {
785
  BLK_CHECK_CONTINUE = 0x1,
786
  BLK_CHECK_QUIT = 0x2,
787
} ETombBlkCheckEnum;
788

789
static int32_t loadNextStatisticsBlock(SSttFileReader* pSttFileReader, STbStatisBlock* pStatisBlock,
790
                                       const TStatisBlkArray* pStatisBlkArray, int32_t numOfRows, int32_t* i,
791
                                       int32_t* j);
792
static int32_t doCheckTombBlock(STombBlock* pBlock, STsdbReader* pReader, int32_t numOfTables, int32_t* j,
59,352✔
793
                                ETombBlkCheckEnum* pRet) {
794
  int32_t     code = 0;
59,352✔
795
  STombRecord record = {0};
59,352✔
796

797
  uint64_t             uid = pReader->status.uidList.tableUidList[*j];
59,352✔
798
  STableBlockScanInfo* pScanInfo = NULL;
59,352✔
799

800
  code = getTableBlockScanInfo(pReader->status.pTableMap, uid, &pScanInfo, pReader->idStr);
59,352✔
801
  if (code != TSDB_CODE_SUCCESS) {
59,352!
802
    return code;
×
803
  }
804

805
  if (pScanInfo->pFileDelData == NULL) {
59,352✔
806
    pScanInfo->pFileDelData = taosArrayInit(4, sizeof(SDelData));
27,959✔
807
    if (pScanInfo->pFileDelData == NULL) {
27,959!
808
      return terrno;
×
809
    }
810
  }
811

812
  for (int32_t k = 0; k < pBlock->numOfRecords; ++k) {
4,269,117✔
813
    code = tTombBlockGet(pBlock, k, &record);
4,246,281✔
814
    if (code != TSDB_CODE_SUCCESS) {
4,246,074!
815
      *pRet = BLK_CHECK_QUIT;
×
816
      return code;
×
817
    }
818

819
    if (record.suid < pReader->info.suid) {
4,246,074✔
820
      continue;
9,498✔
821
    }
822

823
    if (record.suid > pReader->info.suid) {
4,236,576✔
824
      *pRet = BLK_CHECK_QUIT;
9,801✔
825
      return TSDB_CODE_SUCCESS;
9,801✔
826
    }
827

828
    if (uid < record.uid) {
4,226,775✔
829
      while ((*j) < numOfTables && pReader->status.uidList.tableUidList[*j] < record.uid) {
122,856✔
830
        (*j) += 1;
63,443✔
831
      }
832

833
      if ((*j) >= numOfTables) {
59,413✔
834
        *pRet = BLK_CHECK_QUIT;
26,720✔
835
        return TSDB_CODE_SUCCESS;
26,720✔
836
      }
837

838
      uid = pReader->status.uidList.tableUidList[*j];
32,693✔
839
      code = getTableBlockScanInfo(pReader->status.pTableMap, uid, &pScanInfo, pReader->idStr);
32,693✔
840
      if (code != TSDB_CODE_SUCCESS) {
32,698!
841
        return code;
×
842
      }
843

844
      if (pScanInfo->pFileDelData == NULL) {
32,698✔
845
        pScanInfo->pFileDelData = taosArrayInit(4, sizeof(SDelData));
12,143✔
846
        if (pScanInfo->pFileDelData == NULL) {
12,171!
847
          return terrno;
×
848
        }
849
      }
850
    }
851

852
    if (record.uid < uid) {
4,200,248✔
853
      continue;
3,779,200✔
854
    }
855

856
    if (!(record.suid == pReader->info.suid && uid == record.uid)) {
421,048!
857
      tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__);
×
858
      return TSDB_CODE_INTERNAL_ERROR;
×
859
    }
860

861
    if (record.version <= pReader->info.verRange.maxVer) {
421,052!
862
      SDelData delData = {.version = record.version, .sKey = record.skey, .eKey = record.ekey};
421,053✔
863
      void*    px = taosArrayPush(pScanInfo->pFileDelData, &delData);
421,053✔
864
      if (px == NULL) {
421,068!
865
        return terrno;
×
866
      }
867
    }
868
  }
869

870
  *pRet = BLK_CHECK_CONTINUE;
22,836✔
871
  return TSDB_CODE_SUCCESS;
22,836✔
872
}
873

874
// load tomb data API
875
static int32_t doLoadTombDataFromTombBlk(const TTombBlkArray* pTombBlkArray, STsdbReader* pReader, void* pFileReader,
7,828,350✔
876
                                         bool isFile) {
877
  int32_t        code = 0;
7,828,350✔
878
  STableUidList* pList = &pReader->status.uidList;
7,828,350✔
879
  int32_t        numOfTables = tSimpleHashGetSize(pReader->status.pTableMap);
7,828,350✔
880

881
  int32_t i = 0, j = 0;
7,829,624✔
882
  while (i < pTombBlkArray->size && j < numOfTables) {
7,868,704!
883
    STombBlk* pTombBlk = &pTombBlkArray->data[i];
91,603✔
884
    if (pTombBlk->maxTbid.suid < pReader->info.suid) {
91,603✔
885
      i += 1;
9,569✔
886
      continue;
16,249✔
887
    }
888

889
    if (pTombBlk->minTbid.suid > pReader->info.suid) {
82,034✔
890
      break;
16,002✔
891
    }
892

893
    if (!(pTombBlk->minTbid.suid <= pReader->info.suid && pTombBlk->maxTbid.suid >= pReader->info.suid)) {
67,898!
894
      tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__);
×
895
      return TSDB_CODE_INTERNAL_ERROR;
36,521✔
896
    }
897
    if (pTombBlk->maxTbid.suid == pReader->info.suid && pTombBlk->maxTbid.uid < pList->tableUidList[0]) {
67,898✔
898
      i += 1;
6,680✔
899
      continue;
6,680✔
900
    }
901

902
    if (pTombBlk->minTbid.suid == pReader->info.suid && pTombBlk->minTbid.uid > pList->tableUidList[numOfTables - 1]) {
61,218✔
903
      break;
1,866✔
904
    }
905

906
    STombBlock block = {0};
59,352✔
907
    code = isFile ? tsdbDataFileReadTombBlock(pFileReader, &pTombBlkArray->data[i], &block)
26,300✔
908
                  : tsdbSttFileReadTombBlock(pFileReader, &pTombBlkArray->data[i], &block);
59,352✔
909
    if (code != TSDB_CODE_SUCCESS) {
59,352!
910
      return code;
×
911
    }
912

913
    //    uint64_t uid = pReader->status.uidList.tableUidList[j];
914

915
    //    STableBlockScanInfo* pScanInfo = getTableBlockScanInfo(pReader->status.pTableMap, uid, pReader->idStr);
916
    //    if (pScanInfo->pFileDelData == NULL) {
917
    //      pScanInfo->pFileDelData = taosArrayInit(4, sizeof(SDelData));
918
    //    }
919

920
    ETombBlkCheckEnum ret = 0;
59,352✔
921
    code = doCheckTombBlock(&block, pReader, numOfTables, &j, &ret);
59,352✔
922

923
    tTombBlockDestroy(&block);
59,352✔
924
    if (code != TSDB_CODE_SUCCESS || ret == BLK_CHECK_QUIT) {
59,352!
925
      return code;
36,521✔
926
    }
927

928
    i += 1;
22,831✔
929
  }
930

931
  return TSDB_CODE_SUCCESS;
7,793,103✔
932
}
933

934
int32_t loadDataFileTombDataForAll(STsdbReader* pReader) {
10,580,836✔
935
  if (pReader->status.pCurrentFileset == NULL || pReader->status.pCurrentFileset->farr[3] == NULL) {
10,580,836!
936
    return TSDB_CODE_SUCCESS;
10,548,567✔
937
  }
938

939
  const TTombBlkArray* pBlkArray = NULL;
32,269✔
940

941
  int32_t code = tsdbDataFileReadTombBlk(pReader->pFileReader, &pBlkArray);
32,269✔
942
  if (code != TSDB_CODE_SUCCESS) {
32,269!
943
    return code;
×
944
  }
945

946
  return doLoadTombDataFromTombBlk(pBlkArray, pReader, pReader->pFileReader, true);
32,269✔
947
}
948

949
int32_t loadSttTombDataForAll(STsdbReader* pReader, SSttFileReader* pSttFileReader, SSttBlockLoadInfo* pLoadInfo) {
7,797,947✔
950
  const TTombBlkArray* pBlkArray = NULL;
7,797,947✔
951
  int32_t              code = tsdbSttFileReadTombBlk(pSttFileReader, &pBlkArray);
7,797,947✔
952
  if (code != TSDB_CODE_SUCCESS) {
7,797,679!
953
    return code;
×
954
  }
955

956
  return doLoadTombDataFromTombBlk(pBlkArray, pReader, pSttFileReader, false);
7,797,679✔
957
}
958

959
int32_t loadMemTombData(SArray** ppMemDelData, STbData* pMemTbData, STbData* piMemTbData, int64_t ver) {
12,809,796✔
960
  if (*ppMemDelData == NULL) {
12,809,796✔
961
    *ppMemDelData = taosArrayInit(4, sizeof(SDelData));
12,805,983✔
962
    if (*ppMemDelData == NULL) {
12,808,571!
963
      return terrno;
×
964
    }
965
  }
966

967
  SArray* pMemDelData = *ppMemDelData;
12,812,384✔
968

969
  SDelData* p = NULL;
12,812,384✔
970
  if (pMemTbData != NULL) {
12,812,384✔
971
    taosRLockLatch(&pMemTbData->lock);
4,568,491✔
972
    p = pMemTbData->pHead;
4,570,136✔
973
    while (p) {
5,352,396✔
974
      if (p->version <= ver) {
782,270✔
975
        void* px = taosArrayPush(pMemDelData, p);
778,151✔
976
        if (px == NULL) {
778,151!
977
          taosRUnLockLatch(&pMemTbData->lock);
×
978
          return terrno;
×
979
        }
980
      }
981

982
      p = p->pNext;
782,260✔
983
    }
984
    taosRUnLockLatch(&pMemTbData->lock);
4,570,126✔
985
  }
986

987
  if (piMemTbData != NULL) {
12,814,041✔
988
    p = piMemTbData->pHead;
28,353✔
989
    while (p) {
91,935✔
990
      if (p->version <= ver) {
63,582!
991
        void* px = taosArrayPush(pMemDelData, p);
63,582✔
992
        if (px == NULL) {
63,582!
993
          return terrno;
×
994
        }
995
      }
996
      p = p->pNext;
63,582✔
997
    }
998
  }
999

1000
  return TSDB_CODE_SUCCESS;
12,814,041✔
1001
}
1002

1003
int32_t getNumOfRowsInSttBlock(SSttFileReader* pSttFileReader, SSttBlockLoadInfo* pBlockLoadInfo,
4,046✔
1004
                               TStatisBlkArray* pStatisBlkArray, uint64_t suid, const uint64_t* pUidList,
1005
                               int32_t numOfTables, int32_t* pNumOfRows) {
1006
  int32_t num = 0;
4,046✔
1007
  int32_t code = 0;
4,046✔
1008
  int32_t lino = 0;
4,046✔
1009

1010
  if (pNumOfRows != 0) {
4,046!
1011
    *pNumOfRows = 0;
4,047✔
1012
  }
1013

1014
  if (TARRAY2_SIZE(pStatisBlkArray) <= 0) {
4,046!
1015
    return code;
×
1016
  }
1017

1018
  int32_t i = 0;
4,046✔
1019
  while ((i < TARRAY2_SIZE(pStatisBlkArray)) && (pStatisBlkArray->data[i].maxTbid.suid < suid)) {
4,046!
1020
    ++i;
×
1021
  }
1022

1023
  if (i >= TARRAY2_SIZE(pStatisBlkArray)) {
4,046!
1024
    return code;
×
1025
  }
1026

1027
  SStatisBlk*     p = &pStatisBlkArray->data[i];
4,046✔
1028
  STbStatisBlock* pStatisBlock = taosMemoryCalloc(1, sizeof(STbStatisBlock));
4,046✔
1029
  TSDB_CHECK_NULL(pStatisBlock, code, lino, _err, terrno);
4,046!
1030

1031
  code = tStatisBlockInit(pStatisBlock);
4,046✔
1032
  TSDB_CHECK_CODE(code, lino, _err);
4,045!
1033

1034
  int64_t st = taosGetTimestampMs();
4,044✔
1035
  code = tsdbSttFileReadStatisBlock(pSttFileReader, p, pStatisBlock);
4,044✔
1036
  TSDB_CHECK_CODE(code, lino, _err);
4,051!
1037

1038
  double el = (taosGetTimestampMs() - st) / 1000.0;
4,052✔
1039
  pBlockLoadInfo->cost.loadStatisBlocks += 1;
4,052✔
1040
  pBlockLoadInfo->cost.statisElapsedTime += el;
4,052✔
1041

1042
  int32_t index = 0;
4,052✔
1043
  while (index < pStatisBlock->numOfRecords && ((int64_t*)pStatisBlock->suids.data)[index] < suid) {
8,100✔
1044
    ++index;
4,048✔
1045
  }
1046

1047
  if (index >= pStatisBlock->numOfRecords) {
4,052!
1048
    tStatisBlockDestroy(pStatisBlock);
×
1049
    taosMemoryFreeClear(pStatisBlock);
×
1050
    *pNumOfRows = num;
×
1051
    return code;
×
1052
  }
1053

1054
  int32_t j = index;
4,052✔
1055
  int32_t uidIndex = 0;
4,052✔
1056
  while (i < TARRAY2_SIZE(pStatisBlkArray) && uidIndex < numOfTables) {
8,102!
1057
    p = &pStatisBlkArray->data[i];
4,861✔
1058
    if (p->minTbid.suid > suid) {
4,861✔
1059
      tStatisBlockDestroy(pStatisBlock);
811✔
1060
      taosMemoryFreeClear(pStatisBlock);
811!
1061
      *pNumOfRows = num;
811✔
1062
      return code;
811✔
1063
    }
1064

1065
    uint64_t uid = pUidList[uidIndex];
4,050✔
1066

1067
    if (((int64_t*)pStatisBlock->uids.data)[j] == uid) {
4,050✔
1068
      num += ((int64_t*)pStatisBlock->counts.data)[j];
2,433✔
1069
      uidIndex += 1;
2,433✔
1070
      j += 1;
2,433✔
1071
      code = loadNextStatisticsBlock(pSttFileReader, pStatisBlock, pStatisBlkArray, pStatisBlock->numOfRecords, &i, &j);
2,433✔
1072
      TSDB_CHECK_CODE(code, lino, _err);
2,433!
1073
    } else if (((int64_t*)pStatisBlock->uids.data)[j] < uid) {
1,617!
1074
      j += 1;
1,618✔
1075
      code = loadNextStatisticsBlock(pSttFileReader, pStatisBlock, pStatisBlkArray, pStatisBlock->numOfRecords, &i, &j);
1,618✔
1076
      TSDB_CHECK_CODE(code, lino, _err);
1,618!
1077
    } else {
1078
      uidIndex += 1;
×
1079
    }
1080
  }
1081

1082
  tStatisBlockDestroy(pStatisBlock);
3,241✔
1083
  taosMemoryFreeClear(pStatisBlock);
3,237!
1084
  *pNumOfRows = num;
3,242✔
1085
  return code;
3,242✔
1086

1087
_err:
×
1088
  tsdbError("%p failed to get number of rows in stt block, %s at line:%d code:%s", pSttFileReader, __func__, lino,
×
1089
            tstrerror(code));
1090
  return code;
×
1091
}
1092

1093
// load next stt statistics block
1094
static int32_t loadNextStatisticsBlock(SSttFileReader* pSttFileReader, STbStatisBlock* pStatisBlock,
4,050✔
1095
                                       const TStatisBlkArray* pStatisBlkArray, int32_t numOfRows, int32_t* i,
1096
                                       int32_t* j) {
1097
  if ((*j) >= numOfRows) {
4,050✔
1098
    (*i) += 1;
3,241✔
1099
    (*j) = 0;
3,241✔
1100
    if ((*i) < TARRAY2_SIZE(pStatisBlkArray)) {
3,241!
1101
      int32_t code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[(*i)], pStatisBlock);
×
1102
      if (code != 0) {
×
1103
        tsdbError("%p failed to read statisBlock, code:%s", pSttFileReader, tstrerror(code));
×
1104
        return code;
×
1105
      }
1106
    }
1107
  }
1108

1109
  return 0;
4,050✔
1110
}
1111

1112
int32_t doAdjustValidDataIters(SArray* pLDIterList, int32_t numOfFileObj) {
16,415,551✔
1113
  int32_t size = taosArrayGetSize(pLDIterList);
16,415,551✔
1114

1115
  if (size < numOfFileObj) {
16,415,004✔
1116
    int32_t inc = numOfFileObj - size;
7,800,187✔
1117
    for (int32_t k = 0; k < inc; ++k) {
15,609,678✔
1118
      SLDataIter* pIter = taosMemoryCalloc(1, sizeof(SLDataIter));
7,800,062✔
1119
      if (!pIter) {
7,808,164!
1120
        return terrno;
×
1121
      }
1122
      void* px = taosArrayPush(pLDIterList, &pIter);
7,809,491✔
1123
      if (px == NULL) {
7,809,491!
1124
        taosMemoryFree(pIter);
×
1125
        return terrno;
×
1126
      }
1127
    }
1128
  } else if (size > numOfFileObj) {  // remove unused LDataIter
8,614,817!
1129
    int32_t inc = size - numOfFileObj;
×
1130

1131
    for (int i = 0; i < inc; ++i) {
×
1132
      SLDataIter* pIter = taosArrayPop(pLDIterList);
×
1133
      destroyLDataIter(pIter);
×
1134
    }
1135
  }
1136

1137
  return TSDB_CODE_SUCCESS;
16,418,300✔
1138
}
1139

1140
int32_t adjustSttDataIters(SArray* pSttFileBlockIterArray, STFileSet* pFileSet) {
16,231,194✔
1141
  int32_t numOfLevels = pFileSet->lvlArr->size;
16,231,194✔
1142
  int32_t code = 0;
16,231,194✔
1143

1144
  // add the list/iter placeholder
1145
  while (taosArrayGetSize(pSttFileBlockIterArray) < numOfLevels) {
24,033,787✔
1146
    SArray* pList = taosArrayInit(4, POINTER_BYTES);
7,786,438✔
1147
    if (pList == NULL) {
7,807,664!
1148
      return terrno;
×
1149
    }
1150
    void* px = taosArrayPush(pSttFileBlockIterArray, &pList);
7,802,593✔
1151
    if (px == NULL) {
7,802,593!
1152
      return terrno;
×
1153
    }
1154
  }
1155

1156
  for (int32_t j = 0; j < numOfLevels; ++j) {
32,655,524✔
1157
    SSttLvl* pSttLevel = pFileSet->lvlArr->data[j];
16,419,918✔
1158
    SArray*  pList = taosArrayGetP(pSttFileBlockIterArray, j);
16,419,918✔
1159
    code = doAdjustValidDataIters(pList, TARRAY2_SIZE(pSttLevel->fobjArr));
16,416,419✔
1160
    if (code != TSDB_CODE_SUCCESS) {
16,420,409!
1161
      return code;
×
1162
    }
1163
  }
1164

1165
  return TSDB_CODE_SUCCESS;
16,235,606✔
1166
}
1167

1168
int32_t tsdbGetRowsInSttFiles(STFileSet* pFileSet, SArray* pSttFileBlockIterArray, STsdb* pTsdb, SMergeTreeConf* pConf,
4,039✔
1169
                              const char* pstr) {
1170
  int32_t numOfRows = 0;
4,039✔
1171
  int32_t code = 0;
4,039✔
1172

1173
  // no data exists, go to end
1174
  int32_t numOfLevels = pFileSet->lvlArr->size;
4,039✔
1175
  if (numOfLevels == 0) {
4,039!
1176
    return numOfRows;
×
1177
  }
1178

1179
  // add the list/iter placeholder
1180
  code = adjustSttDataIters(pSttFileBlockIterArray, pFileSet);
4,039✔
1181
  if (code != TSDB_CODE_SUCCESS) {
4,048!
1182
    return numOfRows;
×
1183
  }
1184

1185
  for (int32_t j = 0; j < numOfLevels; ++j) {
8,100✔
1186
    SSttLvl* pSttLevel = pFileSet->lvlArr->data[j];
4,047✔
1187
    SArray*  pList = taosArrayGetP(pSttFileBlockIterArray, j);
4,047✔
1188

1189
    for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {  // open all last file
8,093✔
1190
      SLDataIter* pIter = taosArrayGetP(pList, i);
4,033✔
1191

1192
      // open stt file reader if not opened yet
1193
      // if failed to open this stt file, ignore the error and try next one
1194
      if (pIter->pReader == NULL) {
4,038!
1195
        SSttFileReaderConfig conf = {.tsdb = pTsdb, .szPage = pTsdb->pVnode->config.tsdbPageSize};
4,039✔
1196
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
4,039✔
1197

1198
        const char* pName = pSttLevel->fobjArr->data[i]->fname;
4,039✔
1199
        code = tsdbSttFileReaderOpen(pName, &conf, &pIter->pReader);
4,039✔
1200
        if (code != TSDB_CODE_SUCCESS) {
4,042!
1201
          tsdbError("open stt file reader error. file:%s, code %s, %s", pName, tstrerror(code), pstr);
×
1202
          continue;
×
1203
        }
1204
      }
1205

1206
      if (pIter->pBlockLoadInfo == NULL) {
4,041!
1207
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pIter->pBlockLoadInfo);
4,045✔
1208
        if (code != TSDB_CODE_SUCCESS) {
4,049!
1209
          tsdbError("failed to create block load info, code: out of memory, %s", pstr);
×
1210
          continue;
×
1211
        }
1212
      }
1213

1214
      // load stt blocks statis for all stt-blocks, to decide if the data of queried table exists in current stt file
1215
      TStatisBlkArray* pStatisBlkArray = NULL;
4,045✔
1216
      code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray**)&pStatisBlkArray);
4,045✔
1217
      if (code != TSDB_CODE_SUCCESS) {
4,050!
1218
        tsdbError("failed to load stt block statistics, code:%s, %s", tstrerror(code), pstr);
×
1219
        continue;
×
1220
      }
1221

1222
      // extract rows from each stt file one-by-one
1223
      STsdbReader* pReader = pConf->pReader;
4,050✔
1224
      int32_t      numOfTables = tSimpleHashGetSize(pReader->status.pTableMap);
4,050✔
1225
      uint64_t*    pUidList = pReader->status.uidList.tableUidList;
4,046✔
1226
      int32_t      n = 0;
4,046✔
1227
      code = getNumOfRowsInSttBlock(pIter->pReader, pIter->pBlockLoadInfo, pStatisBlkArray, pConf->suid, pUidList,
4,046✔
1228
                                    numOfTables, &n);
1229
      numOfRows += n;
4,053✔
1230
      if (code) {
4,053!
1231
        tsdbError("%s failed to get rows in stt blocks, code:%s", pstr, tstrerror(code));
×
1232
      }
1233
    }
1234
  }
1235

1236
  return numOfRows;
4,053✔
1237
}
1238

1239
static bool overlapHelper(const STimeWindow* pLeft, TSKEY minKey, TSKEY maxKey) {
39,711✔
1240
  return (pLeft->ekey >= minKey) && (pLeft->skey <= maxKey);
39,711✔
1241
}
1242

1243
static bool overlapWithTimeWindow(STimeWindow* p1, STimeWindow* pQueryWindow, STableBlockScanInfo* pBlockScanInfo,
105,907✔
1244
                                  int32_t order) {
1245
  // overlap with query window
1246
  if (!(p1->skey >= pQueryWindow->skey && p1->ekey <= pQueryWindow->ekey)) {
105,907✔
1247
    return true;
558✔
1248
  }
1249

1250
  SIterInfo* pMemIter = &pBlockScanInfo->iter;
105,349✔
1251
  SIterInfo* pIMemIter = &pBlockScanInfo->iiter;
105,349✔
1252

1253
  // overlap with mem data
1254
  if (pMemIter->hasVal) {
105,349✔
1255
    STbData* pTbData = pMemIter->iter->pTbData;
33,856✔
1256
    if (overlapHelper(p1, pTbData->minKey, pTbData->maxKey)) {
33,856✔
1257
      return true;
33,568✔
1258
    }
1259
  }
1260

1261
  // overlap with imem data
1262
  if (pIMemIter->hasVal) {
71,782✔
1263
    STbData* pITbData = pIMemIter->iter->pTbData;
1,537✔
1264
    if (overlapHelper(p1, pITbData->minKey, pITbData->maxKey)) {
1,537!
1265
      return true;
1,537✔
1266
    }
1267
  }
1268

1269
  // overlap with data file block
1270
  STimeWindow* pFileWin = &pBlockScanInfo->filesetWindow;
70,245✔
1271
  if ((taosArrayGetSize(pBlockScanInfo->pBlockIdxList) > 0) && overlapHelper(p1, pFileWin->skey, pFileWin->ekey)) {
70,245✔
1272
    return true;
4,303✔
1273
  }
1274

1275
  // overlap with deletion skyline
1276
  SBrinRecord record = {.firstKey = p1->skey, .lastKey = p1->ekey};
65,953✔
1277
  if (overlapWithDelSkylineWithoutVer(pBlockScanInfo, &record, order)) {
65,953✔
1278
    return true;
8,075✔
1279
  }
1280

1281
  return false;
57,920✔
1282
}
1283

1284
static int32_t sortUidComparFn(const void* p1, const void* p2) {
67,786✔
1285
  const SSttKeyRange* px1 = p1;
67,786✔
1286
  const SSttKeyRange* px2 = p2;
67,786✔
1287

1288
  int32_t ret = tRowKeyCompare(&px1->skey, &px2->skey);
67,786✔
1289
  return ret;
67,850✔
1290
}
1291

1292
bool isCleanSttBlock(SArray* pKeyRangeList, STimeWindow* pQueryWindow, STableBlockScanInfo* pScanInfo, int32_t order) {
273,989✔
1293
  // check if it overlap with del skyline
1294
  taosArraySort(pKeyRangeList, sortUidComparFn);
273,989✔
1295

1296
  int32_t num = taosArrayGetSize(pKeyRangeList);
274,043✔
1297
  if (num == 0) {
274,170✔
1298
    return false;
168,194✔
1299
  }
1300

1301
  SSttKeyRange* pRange = taosArrayGet(pKeyRangeList, 0);
105,976✔
1302
  if (pRange == NULL) {
105,924!
1303
    return false;
×
1304
  }
1305

1306
  STimeWindow w = {.skey = pRange->skey.ts, .ekey = pRange->ekey.ts};
105,924✔
1307
  if (overlapWithTimeWindow(&w, pQueryWindow, pScanInfo, order)) {
105,924✔
1308
    return false;
48,093✔
1309
  }
1310

1311
  for (int32_t i = 0; i < num - 1; ++i) {
57,925✔
1312
    SSttKeyRange* p1 = taosArrayGet(pKeyRangeList, i);
24,670✔
1313
    SSttKeyRange* p2 = taosArrayGet(pKeyRangeList, i + 1);
24,666✔
1314
    if (p1 == NULL || p2 == NULL) {
24,652!
1315
      return false;
24,641✔
1316
    }
1317

1318
    if (p1->ekey.ts >= p2->skey.ts) {
24,652✔
1319
      return false;
24,641✔
1320
    }
1321

1322
    STimeWindow w2 = {.skey = p2->skey.ts, .ekey = p2->ekey.ts};
11✔
1323
    bool        overlap = overlapWithTimeWindow(&w2, pQueryWindow, pScanInfo, order);
11✔
1324
    if (overlap) {
7!
1325
      return false;
×
1326
    }
1327
  }
1328

1329
  return true;
33,255✔
1330
}
1331

1332
static bool doCheckDatablockOverlap(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord,
36,590✔
1333
                                    int32_t startIndex) {
1334
  size_t num = taosArrayGetSize(pBlockScanInfo->delSkyline);
36,590✔
1335

1336
  for (int32_t i = startIndex; i < num; i += 1) {
107,882✔
1337
    TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, i);
107,868✔
1338
    if (p == NULL) {
107,868!
1339
      return false;
×
1340
    }
1341

1342
    if (p->ts >= pRecord->firstKey.key.ts && p->ts <= pRecord->lastKey.key.ts) {
107,868✔
1343
      if (p->version >= pRecord->minVer) {
25,523✔
1344
        return true;
23,685✔
1345
      }
1346
    } else if (p->ts < pRecord->firstKey.key.ts) {  // p->ts < pBlock->minKey.ts
82,345✔
1347
      if (p->version >= pRecord->minVer) {
77,624✔
1348
        if (i < num - 1) {
23,596!
1349
          TSDBKEY* pnext = taosArrayGet(pBlockScanInfo->delSkyline, i + 1);
23,596✔
1350
          if (pnext == NULL) {
23,596!
1351
            return false;
×
1352
          }
1353

1354
          if (pnext->ts >= pRecord->firstKey.key.ts) {
23,596✔
1355
            return true;
8,170✔
1356
          }
1357
        } else {  // it must be the last point
1358
          if (!(p->version == 0)) {
×
1359
            tsdbError("unexpected version:%" PRId64, p->version);
×
1360
          }
1361
        }
1362
      }
1363
    } else {  // (p->ts > pBlock->maxKey.ts) {
1364
      return false;
4,721✔
1365
    }
1366
  }
1367

1368
  return false;
14✔
1369
}
1370

1371
static bool doCheckDatablockOverlapWithoutVersion(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord,
8,075✔
1372
                                                  int32_t startIndex) {
1373
  size_t num = taosArrayGetSize(pBlockScanInfo->delSkyline);
8,075✔
1374

1375
  for (int32_t i = startIndex; i < num; i += 1) {
8,075!
1376
    TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, i);
8,075✔
1377
    if (p == NULL) {
8,075!
1378
      return false;
×
1379
    }
1380

1381
    if (p->ts >= pRecord->firstKey.key.ts && p->ts <= pRecord->lastKey.key.ts) {
8,075!
1382
      return true;
8,075✔
1383
    } else if (p->ts < pRecord->firstKey.key.ts) {  // p->ts < pBlock->minKey.ts
×
1384
      if (i < num - 1) {
×
1385
        TSDBKEY* pnext = taosArrayGet(pBlockScanInfo->delSkyline, i + 1);
×
1386
        if (pnext == NULL) {
×
1387
          return false;
×
1388
        }
1389

1390
        if (pnext->ts >= pRecord->firstKey.key.ts) {
×
1391
          return true;
×
1392
        }
1393
      }
1394
    } else {  // (p->ts > pBlock->maxKey.ts) {
1395
      return false;
×
1396
    }
1397
  }
1398

1399
  return false;
×
1400
}
1401

1402
bool overlapWithDelSkyline(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord, int32_t order) {
660,441✔
1403
  if (pBlockScanInfo->delSkyline == NULL || (taosArrayGetSize(pBlockScanInfo->delSkyline) == 0)) {
660,441✔
1404
    return false;
613,399✔
1405
  }
1406

1407
  // ts is not overlap
1408
  TSDBKEY* pFirst = taosArrayGet(pBlockScanInfo->delSkyline, 0);
47,042✔
1409
  TSDBKEY* pLast = taosArrayGetLast(pBlockScanInfo->delSkyline);
47,138✔
1410
  if (pFirst == NULL || pLast == NULL) {
47,138!
1411
    return false;
×
1412
  }
1413

1414
  if (pRecord->firstKey.key.ts > pLast->ts || pRecord->lastKey.key.ts < pFirst->ts) {
47,138✔
1415
    return false;
10,548✔
1416
  }
1417

1418
  // version is not overlap
1419
  if (ASCENDING_TRAVERSE(order)) {
36,590✔
1420
    return doCheckDatablockOverlap(pBlockScanInfo, pRecord, pBlockScanInfo->fileDelIndex);
27,286✔
1421
  } else {
1422
    int32_t index = pBlockScanInfo->fileDelIndex;
9,304✔
1423
    while (1) {
28,822✔
1424
      TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, index);
38,126✔
1425
      if (p == NULL) {
38,126!
1426
        return false;
×
1427
      }
1428

1429
      if (p->ts > pRecord->firstKey.key.ts && index > 0) {
38,126✔
1430
        index -= 1;
28,822✔
1431
      } else {  // find the first point that is smaller than the minKey.ts of dataBlock.
1432
        if (p->ts == pRecord->firstKey.key.ts && p->version < pRecord->maxVer && index > 0) {
9,304!
1433
          index -= 1;
24✔
1434
        }
1435
        break;
9,304✔
1436
      }
1437
    }
1438

1439
    return doCheckDatablockOverlap(pBlockScanInfo, pRecord, index);
9,304✔
1440
  }
1441
}
1442

1443
bool overlapWithDelSkylineWithoutVer(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord, int32_t order) {
65,979✔
1444
  if (pBlockScanInfo->delSkyline == NULL || (taosArrayGetSize(pBlockScanInfo->delSkyline) == 0)) {
65,979✔
1445
    return false;
57,740✔
1446
  }
1447

1448
  // ts is not overlap
1449
  TSDBKEY* pFirst = taosArrayGet(pBlockScanInfo->delSkyline, 0);
8,239✔
1450
  TSDBKEY* pLast = taosArrayGetLast(pBlockScanInfo->delSkyline);
8,262✔
1451
  if (pFirst == NULL || pLast == NULL) {
8,262!
1452
    return false;
×
1453
  }
1454

1455
  if (pRecord->firstKey.key.ts > pLast->ts || pRecord->lastKey.key.ts < pFirst->ts) {
8,262!
1456
    return false;
187✔
1457
  }
1458

1459
  // version is not overlap
1460
  if (ASCENDING_TRAVERSE(order)) {
8,075!
1461
    return doCheckDatablockOverlapWithoutVersion(pBlockScanInfo, pRecord, pBlockScanInfo->fileDelIndex);
8,075✔
1462
  } else {
1463
    int32_t index = pBlockScanInfo->fileDelIndex;
×
1464
    while (1) {
×
1465
      TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, index);
×
1466
      if (p == NULL) {
×
1467
        return false;
×
1468
      }
1469

1470
      if (p->ts > pRecord->firstKey.key.ts && index > 0) {
×
1471
        index -= 1;
×
1472
      } else {  // find the first point that is smaller than the minKey.ts of dataBlock.
1473
        if (p->ts == pRecord->firstKey.key.ts && index > 0) {
×
1474
          index -= 1;
×
1475
        }
1476
        break;
×
1477
      }
1478
    }
1479

1480
    return doCheckDatablockOverlapWithoutVersion(pBlockScanInfo, pRecord, index);
×
1481
  }
1482
}
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