tsdbMergeTree.c 15.6 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "tsdb.h"

18
// SLDataIter =================================================
H
Hongze Cheng 已提交
19
struct SLDataIter {
20
  SRBTreeNode   node;
H
Hongze Cheng 已提交
21
  SSttBlk      *pSttBlk;
H
Hongze Cheng 已提交
22
  SDataFReader *pReader;
H
Hongze Cheng 已提交
23
  int32_t       iStt;
H
Hongze Cheng 已提交
24
  int8_t        backward;
H
Hongze Cheng 已提交
25
  int32_t       iSttBlk;
H
Hongze Cheng 已提交
26 27
  int32_t       iRow;
  SRowInfo      rInfo;
28 29 30
  uint64_t      uid;
  STimeWindow   timeWindow;
  SVersionRange verRange;
31 32

  SSttBlockLoadInfo* pBlockLoadInfo;
H
Hongze Cheng 已提交
33
};
H
Hongze Cheng 已提交
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
SSttBlockLoadInfo* tCreateLastBlockLoadInfo() {
  SSttBlockLoadInfo* pLoadInfo = taosMemoryCalloc(TSDB_DEFAULT_STT_FILE, sizeof(SSttBlockLoadInfo));
  if (pLoadInfo == NULL) {
    terrno =  TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  for(int32_t i = 0; i < TSDB_DEFAULT_STT_FILE; ++i) {
    pLoadInfo[i].blockIndex[0] = -1;
    pLoadInfo[i].blockIndex[1] = -1;
    pLoadInfo[i].currentLoadBlockIndex = 1;

    int32_t code = tBlockDataCreate(&pLoadInfo[i].blockData[0]);
    if (code) {
      terrno = code;
    }

    code = tBlockDataCreate(&pLoadInfo[i].blockData[1]);
    if (code) {
      terrno = code;
    }

    pLoadInfo[i].aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
  }

  return pLoadInfo;
}

void resetLastBlockLoadInfo(SSttBlockLoadInfo* pLoadInfo) {
  for(int32_t i = 0; i < TSDB_DEFAULT_STT_FILE; ++i) {
    pLoadInfo[i].currentLoadBlockIndex = 1;
    pLoadInfo[i].blockIndex[0] = -1;
    pLoadInfo[i].blockIndex[1] = -1;
68

69
    taosArrayClear(pLoadInfo[i].aSttBlk);
H
Haojun Liao 已提交
70 71 72 73 74 75 76 77 78 79

    pLoadInfo[i].elapsedTime = 0;
    pLoadInfo[i].loadBlocks = 0;
  }
}

void getLastBlockLoadInfo(SSttBlockLoadInfo* pLoadInfo, int64_t* blocks, double* el) {
  for(int32_t i = 0; i < TSDB_DEFAULT_STT_FILE; ++i) {
    *el += pLoadInfo[i].elapsedTime;
    *blocks += pLoadInfo[i].loadBlocks;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  }
}

void* destroyLastBlockLoadInfo(SSttBlockLoadInfo* pLoadInfo) {
  for(int32_t i = 0; i < TSDB_DEFAULT_STT_FILE; ++i) {
    pLoadInfo[i].currentLoadBlockIndex = 1;
    pLoadInfo[i].blockIndex[0] = -1;
    pLoadInfo[i].blockIndex[1] = -1;

    tBlockDataDestroy(&pLoadInfo[i].blockData[0], true);
    tBlockDataDestroy(&pLoadInfo[i].blockData[1], true);

    taosArrayDestroy(pLoadInfo[i].aSttBlk);
  }

  taosMemoryFree(pLoadInfo);
  return NULL;
}

H
Haojun Liao 已提交
99
static SBlockData* loadLastBlock(SLDataIter *pIter, const char* idStr) {
100 101 102
  int32_t code = 0;

  SSttBlockLoadInfo* pInfo = pIter->pBlockLoadInfo;
103
  if (pInfo->blockIndex[0]  == pIter->iSttBlk) {
104 105 106
    return &pInfo->blockData[0];
  }

107
  if (pInfo->blockIndex[1] == pIter->iSttBlk) {
108 109 110 111 112
    return &pInfo->blockData[1];
  }

  pInfo->currentLoadBlockIndex ^= 1;
  if (pIter->pSttBlk != NULL) {  // current block not loaded yet
H
Haojun Liao 已提交
113
    int64_t st = taosGetTimestampUs();
114
    code = tsdbReadSttBlock(pIter->pReader, pIter->iStt, pIter->pSttBlk, &pInfo->blockData[pInfo->currentLoadBlockIndex]);
H
Haojun Liao 已提交
115 116 117 118 119
    double el = (taosGetTimestampUs() - st)/ 1000.0;
    pInfo->elapsedTime += el;
    pInfo->loadBlocks += 1;

    tsdbDebug("read last block, index:%d, last file index:%d, elapsed time:%.2f ms, %s", pIter->iSttBlk, pIter->iStt, el, idStr);
120 121 122 123
    if (code != TSDB_CODE_SUCCESS) {
      goto _exit;
    }

124
    pInfo->blockIndex[pInfo->currentLoadBlockIndex] = pIter->iSttBlk;
125 126 127 128 129 130 131 132 133 134 135
    pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].nRow : -1;
  }

  return &pInfo->blockData[pInfo->currentLoadBlockIndex];

  _exit:
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
  }

  return NULL;
136 137
}

138
// find the earliest block that contains the required records
139
static FORCE_INLINE int32_t findEarliestIndex(int32_t index, uint64_t uid, const SSttBlk* pBlockList, int32_t num, int32_t backward) {
140 141
  int32_t i = index;
  int32_t step = backward? 1:-1;
H
Haojun Liao 已提交
142
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    i += step;
  }
  return i - step;
}

static int32_t binarySearchForStartBlock(SSttBlk*pBlockList, int32_t num, uint64_t uid, int32_t backward) {
  int32_t midPos = -1;
  if (num <= 0) {
    return -1;
  }

  int32_t firstPos = 0;
  int32_t lastPos = num - 1;

  // find the first position which is bigger than the key
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
    return -1;
  }

  while (1) {
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
164
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
165 166 167 168 169 170 171 172 173 174 175 176 177 178
    }

    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
      return -1;
    }

    int32_t numOfRows = lastPos - firstPos + 1;
    midPos = (numOfRows >> 1u) + firstPos;

    if (uid < pBlockList[midPos].minUid) {
      lastPos = midPos - 1;
    } else if (uid > pBlockList[midPos].maxUid) {
      firstPos = midPos + 1;
    } else {
179
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
180 181 182 183
    }
  }
}

184
static FORCE_INLINE int32_t findEarliestRow(int32_t index, uint64_t uid, const uint64_t* uidList, int32_t num, int32_t backward) {
185 186
  int32_t i = index;
  int32_t step = backward? 1:-1;
H
Haojun Liao 已提交
187
  while (i >= 0 && i < num && uid == uidList[i]) {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    i += step;
  }
  return i - step;
}

static int32_t binarySearchForStartRowIndex(uint64_t* uidList, int32_t num, uint64_t uid, int32_t backward) {
  int32_t firstPos = 0;
  int32_t lastPos = num - 1;

  // find the first position which is bigger than the key
  if ((uid > uidList[lastPos]) || (uid < uidList[firstPos])) {
    return -1;
  }

  while (1) {
    if (uid == uidList[firstPos]) {
204
      return findEarliestRow(firstPos, uid, uidList, num, backward);
205 206 207 208 209 210 211 212 213 214 215 216 217 218
    }

    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
      return -1;
    }

    int32_t numOfRows = lastPos - firstPos + 1;
    int32_t midPos = (numOfRows >> 1u) + firstPos;

    if (uid < uidList[midPos]) {
      lastPos = midPos - 1;
    } else if (uid > uidList[midPos]) {
      firstPos = midPos + 1;
    } else {
219
      return findEarliestRow(midPos, uid, uidList, num, backward);
220 221 222 223
    }
  }
}

H
Haojun Liao 已提交
224
int32_t tLDataIterOpen(struct SLDataIter **pIter, SDataFReader *pReader, int32_t iStt, int8_t backward, uint64_t suid,
225
                       uint64_t uid, STimeWindow *pTimeWindow, SVersionRange *pRange, SSttBlockLoadInfo* pBlockLoadInfo) {
H
Hongze Cheng 已提交
226
  int32_t code = 0;
227
  *pIter = taosMemoryCalloc(1, sizeof(SLDataIter));
228 229 230 231
  if (*pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
232

233
  (*pIter)->uid = uid;
234
  (*pIter)->pReader = pReader;
H
Hongze Cheng 已提交
235
  (*pIter)->iStt = iStt;
236
  (*pIter)->backward = backward;
H
Haojun Liao 已提交
237 238
  (*pIter)->verRange = *pRange;
  (*pIter)->timeWindow = *pTimeWindow;
H
Hongze Cheng 已提交
239

240
  (*pIter)->pBlockLoadInfo = pBlockLoadInfo;
241
  if (taosArrayGetSize(pBlockLoadInfo->aSttBlk) == 0) {
242 243 244
    code = tsdbReadSttBlk(pReader, iStt, pBlockLoadInfo->aSttBlk);
    if (code) {
      goto _exit;
245 246 247 248 249 250 251 252 253 254 255 256
    } else {
      size_t size = taosArrayGetSize(pBlockLoadInfo->aSttBlk);
      SArray* pTmp = taosArrayInit(size, sizeof(SSttBlk));
      for(int32_t i = 0; i < size; ++i) {
        SSttBlk* p = taosArrayGet(pBlockLoadInfo->aSttBlk, i);
        if (p->suid == suid) {
          taosArrayPush(pTmp, p);
        }
      }

      taosArrayDestroy(pBlockLoadInfo->aSttBlk);
      pBlockLoadInfo->aSttBlk = pTmp;
257
    }
258
  }
H
Hongze Cheng 已提交
259

260
  size_t size = taosArrayGetSize(pBlockLoadInfo->aSttBlk);
261 262

  // find the start block
H
Haojun Liao 已提交
263 264
  (*pIter)->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, uid, backward);
  if ((*pIter)->iSttBlk != -1) {
265
    (*pIter)->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, (*pIter)->iSttBlk);
H
Haojun Liao 已提交
266
    (*pIter)->iRow = ((*pIter)->backward) ? (*pIter)->pSttBlk->nRow : -1;
H
Hongze Cheng 已提交
267 268 269 270 271 272 273
  }

_exit:
  return code;
}

void tLDataIterClose(SLDataIter *pIter) {
274
  taosMemoryFree(pIter);
H
Hongze Cheng 已提交
275 276 277
}

void tLDataIterNextBlock(SLDataIter *pIter) {
H
Hongze Cheng 已提交
278
  int32_t step = pIter->backward ? -1 : 1;
H
Hongze Cheng 已提交
279
  pIter->iSttBlk += step;
H
Hongze Cheng 已提交
280

281
  int32_t index = -1;
H
Haojun Liao 已提交
282
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
H
Hongze Cheng 已提交
283
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
284
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
285 286 287 288 289
    if ((!pIter->backward) && p->minUid > pIter->uid) {
      break;
    }

    if (pIter->backward && p->maxUid < pIter->uid) {
290 291 292
      break;
    }

293
    // check uid firstly
294
    if (p->minUid <= pIter->uid && p->maxUid >= pIter->uid) {
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
      if ((!pIter->backward) && p->minKey > pIter->timeWindow.ekey) {
        break;
      }

      if (pIter->backward && p->maxKey < pIter->timeWindow.skey) {
        break;
      }

      // check time range secondly
      if (p->minKey <= pIter->timeWindow.ekey && p->maxKey >= pIter->timeWindow.skey) {
        if ((!pIter->backward) && p->minVer > pIter->verRange.maxVer) {
          break;
        }

        if (pIter->backward && p->maxVer < pIter->verRange.minVer) {
          break;
        }

        if (p->minVer <= pIter->verRange.maxVer && p->maxVer >= pIter->verRange.minVer) {
          index = i;
          break;
        }
      }
318 319 320
    }
  }

321 322
  pIter->pSttBlk = NULL;
  if (index != -1) {
323
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
324 325 326
  }
}

H
Haojun Liao 已提交
327
static void findNextValidRow(SLDataIter *pIter, const char* idStr) {
H
Hongze Cheng 已提交
328
  int32_t step = pIter->backward ? -1 : 1;
329

H
Hongze Cheng 已提交
330 331
  bool        hasVal = false;
  int32_t     i = pIter->iRow;
332

H
Haojun Liao 已提交
333
  SBlockData *pBlockData = loadLastBlock(pIter, idStr);
334

335 336 337
  // mostly we only need to find the start position for a given table
  if ((((i == 0) && (!pIter->backward)) || (i == pBlockData->nRow - 1 && pIter->backward)) && pBlockData->aUid != NULL) {
    i = binarySearchForStartRowIndex((uint64_t*)pBlockData->aUid, pBlockData->nRow, pIter->uid, pIter->backward);
H
Haojun Liao 已提交
338 339 340 341
    if (i == -1) {
      pIter->iRow = -1;
      return;
    }
342 343
  }

344 345
  for (; i < pBlockData->nRow && i >= 0; i += step) {
    if (pBlockData->aUid != NULL) {
346
      if (!pIter->backward) {
347
        /*if (pBlockData->aUid[i] < pIter->uid) {
348
          continue;
349
        } else */if (pBlockData->aUid[i] > pIter->uid) {
350 351 352
          break;
        }
      } else {
353
        /*if (pBlockData->aUid[i] > pIter->uid) {
354
          continue;
355
        } else */if (pBlockData->aUid[i] < pIter->uid) {
356 357 358 359 360
          break;
        }
      }
    }

361
    int64_t ts = pBlockData->aTSKEY[i];
H
Hongze Cheng 已提交
362
    if (!pIter->backward) {               // asc
363 364 365 366 367 368 369 370 371 372 373
      if (ts > pIter->timeWindow.ekey) {  // no more data
        break;
      } else if (ts < pIter->timeWindow.skey) {
        continue;
      }
    } else {
      if (ts < pIter->timeWindow.skey) {
        break;
      } else if (ts > pIter->timeWindow.ekey) {
        continue;
      }
374 375
    }

376
    int64_t ver = pBlockData->aVersion[i];
377 378 379 380 381 382 383 384 385 386 387
    if (ver < pIter->verRange.minVer) {
      continue;
    }

    // todo opt handle desc case
    if (ver > pIter->verRange.maxVer) {
      continue;
    }

    hasVal = true;
    break;
H
Hongze Cheng 已提交
388
  }
389

H
Hongze Cheng 已提交
390
  pIter->iRow = (hasVal) ? i : -1;
H
Hongze Cheng 已提交
391 392
}

H
Haojun Liao 已提交
393
bool tLDataIterNextRow(SLDataIter *pIter, const char* idStr) {
H
Hongze Cheng 已提交
394
  int32_t code = 0;
H
Hongze Cheng 已提交
395
  int32_t step = pIter->backward ? -1 : 1;
396 397

  // no qualified last file block in current file, no need to fetch row
H
Hongze Cheng 已提交
398
  if (pIter->pSttBlk == NULL) {
399 400
    return false;
  }
H
Hongze Cheng 已提交
401

402
  int32_t iBlockL = pIter->iSttBlk;
H
Haojun Liao 已提交
403
  SBlockData *pBlockData = loadLastBlock(pIter, idStr);
404 405
  pIter->iRow += step;

H
Hongze Cheng 已提交
406
  while (1) {
H
Haojun Liao 已提交
407
    findNextValidRow(pIter, idStr);
408

409
    if (pIter->iRow >= pBlockData->nRow || pIter->iRow < 0) {
410
      tLDataIterNextBlock(pIter);
H
Hongze Cheng 已提交
411
      if (pIter->pSttBlk == NULL) {  // no more data
412 413 414 415
        goto _exit;
      }
    } else {
      break;
H
Hongze Cheng 已提交
416 417
    }

H
Hongze Cheng 已提交
418
    if (iBlockL != pIter->iSttBlk) {
H
Haojun Liao 已提交
419
      pBlockData = loadLastBlock(pIter, idStr);
H
Haojun Liao 已提交
420
      pIter->iRow += step;
H
Hongze Cheng 已提交
421 422 423
    }
  }

424 425 426
  pIter->rInfo.suid = pBlockData->suid;
  pIter->rInfo.uid = pBlockData->uid;
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
H
Hongze Cheng 已提交
427 428

_exit:
H
Hongze Cheng 已提交
429
  if (code != TSDB_CODE_SUCCESS) {
430
    terrno = code;
431
  }
432

H
Hongze Cheng 已提交
433
  return (code == TSDB_CODE_SUCCESS) && (pIter->pSttBlk != NULL);
H
Hongze Cheng 已提交
434 435
}

H
Hongze Cheng 已提交
436
SRowInfo *tLDataIterGet(SLDataIter *pIter) { return &pIter->rInfo; }
H
Hongze Cheng 已提交
437 438

// SMergeTree =================================================
H
Hongze Cheng 已提交
439
static FORCE_INLINE int32_t tLDataIterCmprFn(const void *p1, const void *p2) {
H
Hongze Cheng 已提交
440 441
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - sizeof(SRBTreeNode));
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - sizeof(SRBTreeNode));
H
Hongze Cheng 已提交
442

443 444
  TSDBKEY key1 = TSDBROW_KEY(&pIter1->rInfo.row);
  TSDBKEY key2 = TSDBROW_KEY(&pIter2->rInfo.row);
H
Hongze Cheng 已提交
445

446 447 448 449 450 451 452 453 454 455 456 457 458
  if (key1.ts < key2.ts) {
    return -1;
  } else if (key1.ts > key2.ts) {
    return 1;
  } else {
    if (key1.version < key2.version) {
      return -1;
    } else if (key1.version > key2.version) {
      return 1;
    } else {
      return 0;
    }
  }
H
Hongze Cheng 已提交
459 460
}

461
int32_t tMergeTreeOpen(SMergeTree *pMTree, int8_t backward, SDataFReader *pFReader, uint64_t suid, uint64_t uid,
H
Haojun Liao 已提交
462
                       STimeWindow *pTimeWindow, SVersionRange *pVerRange, void* pBlockLoadInfo, const char* idStr) {
H
Hongze Cheng 已提交
463
  pMTree->backward = backward;
464
  pMTree->pIter = NULL;
465
  pMTree->pIterList = taosArrayInit(4, POINTER_BYTES);
466 467 468
  if (pMTree->pIterList == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
469

H
Haojun Liao 已提交
470 471
  pMTree->idStr = idStr;

H
Hongze Cheng 已提交
472
  tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
473 474 475 476 477 478 479 480 481 482 483 484 485
  int32_t code = TSDB_CODE_SUCCESS;

  SSttBlockLoadInfo* pLoadInfo = NULL;
  if (pBlockLoadInfo == NULL) {
    if (pMTree->pLoadInfo == NULL) {
      pMTree->destroyLoadInfo = true;
      pMTree->pLoadInfo = tCreateLastBlockLoadInfo();
    }

    pLoadInfo = pMTree->pLoadInfo;
  } else {
    pLoadInfo = pBlockLoadInfo;
  }
486

H
Hongze Cheng 已提交
487
  for (int32_t i = 0; i < pFReader->pSet->nSttF; ++i) {  // open all last file
488 489
    struct SLDataIter* pIter = NULL;
    code = tLDataIterOpen(&pIter, pFReader, i, pMTree->backward, suid, uid, pTimeWindow, pVerRange, &pLoadInfo[i]);
490 491 492 493
    if (code != TSDB_CODE_SUCCESS) {
      goto _end;
    }

H
Haojun Liao 已提交
494
    bool hasVal = tLDataIterNextRow(pIter, pMTree->idStr);
495
    if (hasVal) {
496 497
      taosArrayPush(pMTree->pIterList, &pIter);
      tMergeTreeAddIter(pMTree, pIter);
498
    } else {
499
      tLDataIterClose(pIter);
500 501
    }
  }
502 503 504

  return code;

H
Hongze Cheng 已提交
505
_end:
506 507
  tMergeTreeClose(pMTree);
  return code;
H
Hongze Cheng 已提交
508
}
H
Hongze Cheng 已提交
509

510 511
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) { tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter); }

H
Hongze Cheng 已提交
512
bool tMergeTreeNext(SMergeTree *pMTree) {
513 514 515 516
  int32_t code = TSDB_CODE_SUCCESS;
  if (pMTree->pIter) {
    SLDataIter *pIter = pMTree->pIter;

H
Haojun Liao 已提交
517
    bool hasVal = tLDataIterNextRow(pIter, pMTree->idStr);
518 519 520 521 522 523 524
    if (!hasVal) {
      pMTree->pIter = NULL;
    }

    // compare with min in RB Tree
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
    if (pMTree->pIter && pIter) {
H
Hongze Cheng 已提交
525
      int32_t c = pMTree->rbt.cmprFn(RBTREE_NODE_PAYLOAD(&pMTree->pIter->node), RBTREE_NODE_PAYLOAD(&pIter->node));
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
      if (c > 0) {
        tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
        pMTree->pIter = NULL;
      } else {
        ASSERT(c);
      }
    }
  }

  if (pMTree->pIter == NULL) {
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
    if (pMTree->pIter) {
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
    }
  }

  return pMTree->pIter != NULL;
}

H
Hongze Cheng 已提交
545
TSDBROW tMergeTreeGetRow(SMergeTree *pMTree) { return pMTree->pIter->rInfo.row; }
546

H
Hongze Cheng 已提交
547
void tMergeTreeClose(SMergeTree *pMTree) {
548
  size_t size = taosArrayGetSize(pMTree->pIterList);
H
Hongze Cheng 已提交
549 550
  for (int32_t i = 0; i < size; ++i) {
    SLDataIter *pIter = taosArrayGetP(pMTree->pIterList, i);
551 552
    tLDataIterClose(pIter);
  }
553

554 555
  pMTree->pIterList = taosArrayDestroy(pMTree->pIterList);
  pMTree->pIter = NULL;
556 557 558 559 560

  if (pMTree->destroyLoadInfo) {
    pMTree->pLoadInfo = destroyLastBlockLoadInfo(pMTree->pLoadInfo);
    pMTree->destroyLoadInfo = false;
  }
561
}