tsdbIter.c 20.8 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16
#include "tsdbIter.h"
H
Hongze Cheng 已提交
17 18 19

// STsdbIter ================
struct STsdbIter {
H
Hongze Cheng 已提交
20
  EIterType type;
H
Hongze Cheng 已提交
21 22 23
  struct {
    bool noMoreData;
  } ctx[1];
H
Hongze Cheng 已提交
24 25 26 27
  union {
    SRowInfo    row[1];
    STombRecord record[1];
  };
H
Hongze Cheng 已提交
28
  SRBTreeNode node[1];
H
Hongze Cheng 已提交
29 30 31 32 33
  union {
    struct {
      SSttSegReader      *reader;
      const TSttBlkArray *sttBlkArray;
      int32_t             sttBlkArrayIdx;
H
Hongze Cheng 已提交
34 35 36
      SBlockData          blockData[1];
      int32_t             blockDataIdx;
    } sttData[1];
H
Hongze Cheng 已提交
37
    struct {
H
Hongze Cheng 已提交
38 39 40 41 42
      SDataFileReader     *reader;
      const TBrinBlkArray *brinBlkArray;
      int32_t              brinBlkArrayIdx;
      SBrinBlock           brinBlock[1];
      int32_t              brinBlockIdx;
H
Hongze Cheng 已提交
43 44 45
      SBlockData           blockData[1];
      int32_t              blockDataIdx;
    } dataData[1];
H
Hongze Cheng 已提交
46
    struct {
H
Hongze Cheng 已提交
47 48 49 50 51
      SMemTable  *memt;
      TSDBKEY     from[1];
      SRBTreeIter iter[1];
      STbData    *tbData;
      STbDataIter tbIter[1];
H
Hongze Cheng 已提交
52
    } memtData[1];
H
Hongze Cheng 已提交
53 54 55 56
    struct {
      SSttSegReader       *reader;
      const TTombBlkArray *tombBlkArray;
      int32_t              tombBlkArrayIdx;
H
Hongze Cheng 已提交
57 58
      STombBlock           tombBlock[1];
      int32_t              tombBlockIdx;
H
Hongze Cheng 已提交
59 60 61 62 63
    } sttTomb[1];
    struct {
      SDataFileReader     *reader;
      const TTombBlkArray *tombBlkArray;
      int32_t              tombBlkArrayIdx;
H
Hongze Cheng 已提交
64 65
      STombBlock           tombBlock[1];
      int32_t              tombBlockIdx;
H
Hongze Cheng 已提交
66
    } dataTomb[1];
H
Hongze Cheng 已提交
67 68 69 70 71 72
    struct {
      SMemTable  *memt;
      SRBTreeIter rbtIter[1];
      STbData    *tbData;
      SDelData   *delData;
    } memtTomb[1];
H
Hongze Cheng 已提交
73
  };
H
Hongze Cheng 已提交
74 75
};

H
Hongze Cheng 已提交
76 77
static int32_t tsdbSttIterNext(STsdbIter *iter, const TABLEID *tbid) {
  while (!iter->ctx->noMoreData) {
H
Hongze Cheng 已提交
78 79 80 81
    for (; iter->sttData->blockDataIdx < iter->sttData->blockData->nRow; iter->sttData->blockDataIdx++) {
      iter->row->suid = iter->sttData->blockData->suid;
      iter->row->uid = iter->sttData->blockData->uid ? iter->sttData->blockData->uid
                                                     : iter->sttData->blockData->aUid[iter->sttData->blockDataIdx];
H
Hongze Cheng 已提交
82 83 84 85 86

      if (tbid && iter->row->suid == tbid->suid && iter->row->uid == tbid->uid) {
        continue;
      }

H
Hongze Cheng 已提交
87 88
      iter->row->row = tsdbRowFromBlockData(iter->sttData->blockData, iter->sttData->blockDataIdx);
      iter->sttData->blockDataIdx++;
H
Hongze Cheng 已提交
89 90 91
      goto _exit;
    }

H
Hongze Cheng 已提交
92
    if (iter->sttData->sttBlkArrayIdx >= TARRAY2_SIZE(iter->sttData->sttBlkArray)) {
H
Hongze Cheng 已提交
93 94 95 96
      iter->ctx->noMoreData = true;
      break;
    }

H
Hongze Cheng 已提交
97 98
    for (; iter->sttData->sttBlkArrayIdx < TARRAY2_SIZE(iter->sttData->sttBlkArray); iter->sttData->sttBlkArrayIdx++) {
      const SSttBlk *sttBlk = TARRAY2_GET_PTR(iter->sttData->sttBlkArray, iter->sttData->sttBlkArrayIdx);
H
Hongze Cheng 已提交
99 100 101 102 103

      if (tbid && tbid->suid == sttBlk->suid && tbid->uid == sttBlk->minUid && tbid->uid == sttBlk->maxUid) {
        continue;
      }

H
Hongze Cheng 已提交
104
      int32_t code = tsdbSttFileReadBlockData(iter->sttData->reader, sttBlk, iter->sttData->blockData);
H
Hongze Cheng 已提交
105 106
      if (code) return code;

H
Hongze Cheng 已提交
107 108
      iter->sttData->blockDataIdx = 0;
      iter->sttData->sttBlkArrayIdx++;
H
Hongze Cheng 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
      break;
    }
  }

_exit:
  return 0;
}

static int32_t tsdbDataIterNext(STsdbIter *iter, const TABLEID *tbid) {
  int32_t code;

  while (!iter->ctx->noMoreData) {
    for (;;) {
H
Hongze Cheng 已提交
122
      // SBlockData
H
Hongze Cheng 已提交
123 124 125
      for (; iter->dataData->blockDataIdx < iter->dataData->blockData->nRow; iter->dataData->blockDataIdx++) {
        if (tbid && tbid->suid == iter->dataData->blockData->suid && tbid->uid == iter->dataData->blockData->uid) {
          iter->dataData->blockDataIdx = iter->dataData->blockData->nRow;
H
Hongze Cheng 已提交
126 127 128
          break;
        }

H
Hongze Cheng 已提交
129 130
        iter->row->row = tsdbRowFromBlockData(iter->dataData->blockData, iter->dataData->blockDataIdx);
        iter->dataData->blockDataIdx++;
H
Hongze Cheng 已提交
131 132 133
        goto _exit;
      }

H
Hongze Cheng 已提交
134
      // SBrinBlock
H
Hongze Cheng 已提交
135
      if (iter->dataData->brinBlockIdx >= BRIN_BLOCK_SIZE(iter->dataData->brinBlock)) {
H
Hongze Cheng 已提交
136 137 138
        break;
      }

H
Hongze Cheng 已提交
139 140
      for (; iter->dataData->brinBlockIdx < BRIN_BLOCK_SIZE(iter->dataData->brinBlock);
           iter->dataData->brinBlockIdx++) {
H
Hongze Cheng 已提交
141
        SBrinRecord record[1];
H
Hongze Cheng 已提交
142
        tBrinBlockGet(iter->dataData->brinBlock, iter->dataData->brinBlockIdx, record);
H
Hongze Cheng 已提交
143

H
Hongze Cheng 已提交
144 145
        if (tbid && tbid->suid == record->suid && tbid->uid == record->uid) {
          continue;
H
Hongze Cheng 已提交
146 147
        }

H
Hongze Cheng 已提交
148 149 150
        iter->row->suid = record->suid;
        iter->row->uid = record->uid;

H
Hongze Cheng 已提交
151
        code = tsdbDataFileReadBlockData(iter->dataData->reader, record, iter->dataData->blockData);
H
Hongze Cheng 已提交
152 153
        if (code) return code;

H
Hongze Cheng 已提交
154 155
        iter->dataData->blockDataIdx = 0;
        iter->dataData->brinBlockIdx++;
H
Hongze Cheng 已提交
156 157 158 159
        break;
      }
    }

H
Hongze Cheng 已提交
160
    if (iter->dataData->brinBlkArrayIdx >= TARRAY2_SIZE(iter->dataData->brinBlkArray)) {
H
Hongze Cheng 已提交
161 162 163 164
      iter->ctx->noMoreData = true;
      break;
    }

H
Hongze Cheng 已提交
165 166 167
    for (; iter->dataData->brinBlkArrayIdx < TARRAY2_SIZE(iter->dataData->brinBlkArray);
         iter->dataData->brinBlkArrayIdx++) {
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(iter->dataData->brinBlkArray, iter->dataData->brinBlkArrayIdx);
H
Hongze Cheng 已提交
168

H
Hongze Cheng 已提交
169
      if (tbid && tbid->uid == brinBlk->minTbid.uid && tbid->uid == brinBlk->maxTbid.uid) {
H
Hongze Cheng 已提交
170 171 172
        continue;
      }

H
Hongze Cheng 已提交
173
      code = tsdbDataFileReadBrinBlock(iter->dataData->reader, brinBlk, iter->dataData->brinBlock);
H
Hongze Cheng 已提交
174 175
      if (code) return code;

H
Hongze Cheng 已提交
176 177
      iter->dataData->brinBlockIdx = 0;
      iter->dataData->brinBlkArrayIdx++;
H
Hongze Cheng 已提交
178 179 180 181 182 183 184 185 186
      break;
    }
  }

_exit:
  return 0;
}

static int32_t tsdbMemTableIterNext(STsdbIter *iter, const TABLEID *tbid) {
H
Hongze Cheng 已提交
187 188 189
  SRBTreeNode *node;

  while (!iter->ctx->noMoreData) {
H
Hongze Cheng 已提交
190 191 192
    for (TSDBROW *row; iter->memtData->tbData && (row = tsdbTbDataIterGet(iter->memtData->tbIter));) {
      if (tbid && tbid->suid == iter->memtData->tbData->suid && tbid->uid == iter->memtData->tbData->uid) {
        iter->memtData->tbData = NULL;
H
Hongze Cheng 已提交
193 194
        break;
      }
H
Hongze Cheng 已提交
195 196
      iter->row->row = row[0];

H
Hongze Cheng 已提交
197
      tsdbTbDataIterNext(iter->memtData->tbIter);
H
Hongze Cheng 已提交
198 199 200 201
      goto _exit;
    }

    for (;;) {
H
Hongze Cheng 已提交
202
      node = tRBTreeIterNext(iter->memtData->iter);
H
Hongze Cheng 已提交
203 204
      if (!node) {
        iter->ctx->noMoreData = true;
H
Hongze Cheng 已提交
205
        goto _exit;
H
Hongze Cheng 已提交
206 207
      }

H
Hongze Cheng 已提交
208 209
      iter->memtData->tbData = TCONTAINER_OF(node, STbData, rbtn);
      if (tbid && tbid->suid == iter->memtData->tbData->suid && tbid->uid == iter->memtData->tbData->uid) {
H
Hongze Cheng 已提交
210 211
        continue;
      } else {
H
Hongze Cheng 已提交
212 213 214
        iter->row->suid = iter->memtData->tbData->suid;
        iter->row->uid = iter->memtData->tbData->uid;
        tsdbTbDataIterOpen(iter->memtData->tbData, iter->memtData->from, 0, iter->memtData->tbIter);
H
Hongze Cheng 已提交
215 216 217 218 219 220
        break;
      }
    }
  }

_exit:
H
Hongze Cheng 已提交
221 222 223
  return 0;
}

H
Hongze Cheng 已提交
224 225
static int32_t tsdbDataTombIterNext(STsdbIter *iter, const TABLEID *tbid) {
  while (!iter->ctx->noMoreData) {
H
Hongze Cheng 已提交
226 227 228
    for (; iter->dataTomb->tombBlockIdx < TOMB_BLOCK_SIZE(iter->dataTomb->tombBlock); iter->dataTomb->tombBlockIdx++) {
      iter->record->suid = TARRAY2_GET(iter->dataTomb->tombBlock->suid, iter->dataTomb->tombBlockIdx);
      iter->record->uid = TARRAY2_GET(iter->dataTomb->tombBlock->uid, iter->dataTomb->tombBlockIdx);
H
Hongze Cheng 已提交
229 230 231 232 233

      if (tbid && iter->record->suid == tbid->suid && iter->record->uid == tbid->uid) {
        continue;
      }

H
Hongze Cheng 已提交
234 235 236 237
      iter->record->version = TARRAY2_GET(iter->dataTomb->tombBlock->version, iter->dataTomb->tombBlockIdx);
      iter->record->skey = TARRAY2_GET(iter->dataTomb->tombBlock->skey, iter->dataTomb->tombBlockIdx);
      iter->record->ekey = TARRAY2_GET(iter->dataTomb->tombBlock->ekey, iter->dataTomb->tombBlockIdx);
      iter->dataTomb->tombBlockIdx++;
H
Hongze Cheng 已提交
238 239 240 241 242
      goto _exit;
    }

    if (iter->dataTomb->tombBlkArrayIdx >= TARRAY2_SIZE(iter->dataTomb->tombBlkArray)) {
      iter->ctx->noMoreData = true;
H
Hongze Cheng 已提交
243
      goto _exit;
H
Hongze Cheng 已提交
244 245 246 247 248 249
    }

    for (; iter->dataTomb->tombBlkArrayIdx < TARRAY2_SIZE(iter->dataTomb->tombBlkArray);
         iter->dataTomb->tombBlkArrayIdx++) {
      const STombBlk *tombBlk = TARRAY2_GET_PTR(iter->dataTomb->tombBlkArray, iter->dataTomb->tombBlkArrayIdx);

H
Hongze Cheng 已提交
250 251
      if (tbid && tbid->suid == tombBlk->minTbid.suid && tbid->uid == tombBlk->minTbid.uid &&
          tbid->suid == tombBlk->maxTbid.suid && tbid->uid == tombBlk->maxTbid.uid) {
H
Hongze Cheng 已提交
252 253 254
        continue;
      }

H
Hongze Cheng 已提交
255
      int32_t code = tsdbDataFileReadTombBlock(iter->dataTomb->reader, tombBlk, iter->dataTomb->tombBlock);
H
Hongze Cheng 已提交
256 257
      if (code) return code;

H
Hongze Cheng 已提交
258
      iter->dataTomb->tombBlockIdx = 0;
H
Hongze Cheng 已提交
259 260 261 262 263 264 265 266 267
      iter->dataTomb->tombBlkArrayIdx++;
      break;
    }
  }

_exit:
  return 0;
}

H
Hongze Cheng 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
static int32_t tsdbMemTombIterNext(STsdbIter *iter, const TABLEID *tbid) {
  while (!iter->ctx->noMoreData) {
    for (; iter->memtTomb->delData;) {
      if (tbid && tbid->uid == iter->memtTomb->tbData->uid) {
        iter->memtTomb->delData = NULL;
        break;
      }

      iter->record->suid = iter->memtTomb->tbData->suid;
      iter->record->uid = iter->memtTomb->tbData->uid;
      iter->record->version = iter->memtTomb->delData->version;
      iter->record->skey = iter->memtTomb->delData->sKey;
      iter->record->ekey = iter->memtTomb->delData->eKey;

      iter->memtTomb->delData = iter->memtTomb->delData->pNext;
      goto _exit;
    }

    for (;;) {
      SRBTreeNode *node = tRBTreeIterNext(iter->memtTomb->rbtIter);
      if (node == NULL) {
        iter->ctx->noMoreData = true;
        goto _exit;
      }

      iter->memtTomb->tbData = TCONTAINER_OF(node, STbData, rbtn);
      if (tbid && tbid->uid == iter->memtTomb->tbData->uid) {
        continue;
      } else {
        iter->memtTomb->delData = iter->memtTomb->tbData->pHead;
        break;
      }
    }
  }

_exit:
  return 0;
}

H
Hongze Cheng 已提交
307 308 309
static int32_t tsdbSttIterOpen(STsdbIter *iter) {
  int32_t code;

H
Hongze Cheng 已提交
310
  code = tsdbSttFileReadSttBlk(iter->sttData->reader, &iter->sttData->sttBlkArray);
H
Hongze Cheng 已提交
311 312
  if (code) return code;

H
Hongze Cheng 已提交
313
  if (TARRAY2_SIZE(iter->sttData->sttBlkArray) == 0) {
H
Hongze Cheng 已提交
314 315 316 317
    iter->ctx->noMoreData = true;
    return 0;
  }

H
Hongze Cheng 已提交
318 319 320
  iter->sttData->sttBlkArrayIdx = 0;
  tBlockDataCreate(iter->sttData->blockData);
  iter->sttData->blockDataIdx = 0;
H
Hongze Cheng 已提交
321 322 323 324 325 326 327

  return tsdbSttIterNext(iter, NULL);
}

static int32_t tsdbDataIterOpen(STsdbIter *iter) {
  int32_t code;

H
Hongze Cheng 已提交
328
  // SBrinBlk
H
Hongze Cheng 已提交
329
  code = tsdbDataFileReadBrinBlk(iter->dataData->reader, &iter->dataData->brinBlkArray);
H
Hongze Cheng 已提交
330 331
  if (code) return code;

H
Hongze Cheng 已提交
332
  if (TARRAY2_SIZE(iter->dataData->brinBlkArray) == 0) {
H
Hongze Cheng 已提交
333 334 335 336
    iter->ctx->noMoreData = true;
    return 0;
  }

H
Hongze Cheng 已提交
337
  iter->dataData->brinBlkArrayIdx = 0;
H
Hongze Cheng 已提交
338 339

  // SBrinBlock
H
Hongze Cheng 已提交
340 341
  tBrinBlockInit(iter->dataData->brinBlock);
  iter->dataData->brinBlockIdx = 0;
H
Hongze Cheng 已提交
342 343

  // SBlockData
H
Hongze Cheng 已提交
344 345
  tBlockDataCreate(iter->dataData->blockData);
  iter->dataData->blockDataIdx = 0;
H
Hongze Cheng 已提交
346 347 348 349 350

  return tsdbDataIterNext(iter, NULL);
}

static int32_t tsdbMemTableIterOpen(STsdbIter *iter) {
H
Hongze Cheng 已提交
351
  iter->memtData->iter[0] = tRBTreeIterCreate(iter->memtData->memt->tbDataTree, 1);
H
Hongze Cheng 已提交
352
  return tsdbMemTableIterNext(iter, NULL);
H
Hongze Cheng 已提交
353 354 355
}

static int32_t tsdbSttIterClose(STsdbIter *iter) {
H
Hongze Cheng 已提交
356
  tBlockDataDestroy(iter->sttData->blockData);
H
Hongze Cheng 已提交
357 358 359
  return 0;
}

H
Hongze Cheng 已提交
360 361 362 363 364 365 366 367 368 369
static int32_t tsdbDataTombIterOpen(STsdbIter *iter) {
  int32_t code;

  code = tsdbDataFileReadTombBlk(iter->dataTomb->reader, &iter->dataTomb->tombBlkArray);
  if (code) return code;

  if (TARRAY2_SIZE(iter->dataTomb->tombBlkArray) == 0) {
    iter->ctx->noMoreData = true;
    return 0;
  }
H
Hongze Cheng 已提交
370
  iter->dataTomb->tombBlkArrayIdx = 0;
H
Hongze Cheng 已提交
371

H
Hongze Cheng 已提交
372 373
  tTombBlockInit(iter->dataTomb->tombBlock);
  iter->dataTomb->tombBlockIdx = 0;
H
Hongze Cheng 已提交
374 375 376 377

  return tsdbDataTombIterNext(iter, NULL);
}

H
Hongze Cheng 已提交
378 379 380
static int32_t tsdbMemTombIterOpen(STsdbIter *iter) {
  int32_t code;

H
Hongze Cheng 已提交
381 382 383 384 385
  if (iter->memtTomb->memt->nDel == 0) {
    iter->ctx->noMoreData = true;
    return 0;
  }

H
Hongze Cheng 已提交
386 387 388 389
  iter->memtTomb->rbtIter[0] = tRBTreeIterCreate(iter->memtTomb->memt->tbDataTree, 1);
  return tsdbMemTombIterNext(iter, NULL);
}

H
Hongze Cheng 已提交
390
static int32_t tsdbDataIterClose(STsdbIter *iter) {
H
Hongze Cheng 已提交
391 392
  tBrinBlockDestroy(iter->dataData->brinBlock);
  tBlockDataDestroy(iter->dataData->blockData);
H
Hongze Cheng 已提交
393 394 395
  return 0;
}

H
Hongze Cheng 已提交
396
static int32_t tsdbMemTableIterClose(STsdbIter *iter) { return 0; }
H
Hongze Cheng 已提交
397

H
Hongze Cheng 已提交
398 399
static int32_t tsdbSttTombIterNext(STsdbIter *iter, const TABLEID *tbid) {
  while (!iter->ctx->noMoreData) {
H
Hongze Cheng 已提交
400 401 402
    for (; iter->sttTomb->tombBlockIdx < TOMB_BLOCK_SIZE(iter->sttTomb->tombBlock); iter->sttTomb->tombBlockIdx++) {
      iter->record->suid = TARRAY2_GET(iter->sttTomb->tombBlock->suid, iter->sttTomb->tombBlockIdx);
      iter->record->uid = TARRAY2_GET(iter->sttTomb->tombBlock->uid, iter->sttTomb->tombBlockIdx);
H
Hongze Cheng 已提交
403 404 405 406 407

      if (tbid && iter->record->suid == tbid->suid && iter->record->uid == tbid->uid) {
        continue;
      }

H
Hongze Cheng 已提交
408 409 410 411
      iter->record->version = TARRAY2_GET(iter->sttTomb->tombBlock->version, iter->sttTomb->tombBlockIdx);
      iter->record->skey = TARRAY2_GET(iter->sttTomb->tombBlock->skey, iter->sttTomb->tombBlockIdx);
      iter->record->ekey = TARRAY2_GET(iter->sttTomb->tombBlock->ekey, iter->sttTomb->tombBlockIdx);
      iter->sttTomb->tombBlockIdx++;
H
Hongze Cheng 已提交
412 413 414 415 416
      goto _exit;
    }

    if (iter->sttTomb->tombBlkArrayIdx >= TARRAY2_SIZE(iter->sttTomb->tombBlkArray)) {
      iter->ctx->noMoreData = true;
H
Hongze Cheng 已提交
417
      goto _exit;
H
Hongze Cheng 已提交
418 419 420 421 422 423
    }

    for (; iter->sttTomb->tombBlkArrayIdx < TARRAY2_SIZE(iter->sttTomb->tombBlkArray);
         iter->sttTomb->tombBlkArrayIdx++) {
      const STombBlk *tombBlk = TARRAY2_GET_PTR(iter->sttTomb->tombBlkArray, iter->sttTomb->tombBlkArrayIdx);

H
Hongze Cheng 已提交
424 425
      if (tbid && tbid->suid == tombBlk->minTbid.suid && tbid->uid == tombBlk->minTbid.uid &&
          tbid->suid == tombBlk->maxTbid.suid && tbid->uid == tombBlk->maxTbid.uid) {
H
Hongze Cheng 已提交
426 427 428
        continue;
      }

H
Hongze Cheng 已提交
429
      int32_t code = tsdbSttFileReadTombBlock(iter->sttTomb->reader, tombBlk, iter->sttTomb->tombBlock);
H
Hongze Cheng 已提交
430 431
      if (code) return code;

H
Hongze Cheng 已提交
432
      iter->sttTomb->tombBlockIdx = 0;
H
Hongze Cheng 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
      iter->sttTomb->tombBlkArrayIdx++;
      break;
    }
  }

_exit:
  return 0;
}

static int32_t tsdbSttTombIterOpen(STsdbIter *iter) {
  int32_t code;

  code = tsdbSttFileReadTombBlk(iter->sttTomb->reader, &iter->sttTomb->tombBlkArray);
  if (code) return code;

  if (TARRAY2_SIZE(iter->sttTomb->tombBlkArray) == 0) {
    iter->ctx->noMoreData = true;
    return 0;
  }

  iter->sttTomb->tombBlkArrayIdx = 0;
H
Hongze Cheng 已提交
454 455
  tTombBlockInit(iter->sttTomb->tombBlock);
  iter->sttTomb->tombBlockIdx = 0;
H
Hongze Cheng 已提交
456 457 458 459

  return tsdbSttTombIterNext(iter, NULL);
}

H
Hongze Cheng 已提交
460 461 462 463
int32_t tsdbIterOpen(const STsdbIterConfig *config, STsdbIter **iter) {
  int32_t code;

  iter[0] = taosMemoryCalloc(1, sizeof(*iter[0]));
H
Hongze Cheng 已提交
464 465 466
  if (iter[0] == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
H
Hongze Cheng 已提交
467 468 469 470 471

  iter[0]->type = config->type;
  iter[0]->ctx->noMoreData = false;
  switch (config->type) {
    case TSDB_ITER_TYPE_STT:
H
Hongze Cheng 已提交
472
      iter[0]->sttData->reader = config->sttReader;
H
Hongze Cheng 已提交
473 474 475
      code = tsdbSttIterOpen(iter[0]);
      break;
    case TSDB_ITER_TYPE_DATA:
H
Hongze Cheng 已提交
476
      iter[0]->dataData->reader = config->dataReader;
H
Hongze Cheng 已提交
477 478 479
      code = tsdbDataIterOpen(iter[0]);
      break;
    case TSDB_ITER_TYPE_MEMT:
H
Hongze Cheng 已提交
480 481
      iter[0]->memtData->memt = config->memt;
      iter[0]->memtData->from[0] = config->from[0];
H
Hongze Cheng 已提交
482 483
      code = tsdbMemTableIterOpen(iter[0]);
      break;
H
Hongze Cheng 已提交
484 485 486 487 488 489 490 491
    case TSDB_ITER_TYPE_STT_TOMB:
      iter[0]->sttTomb->reader = config->sttReader;
      code = tsdbSttTombIterOpen(iter[0]);
      break;
    case TSDB_ITER_TYPE_DATA_TOMB:
      iter[0]->dataTomb->reader = config->dataReader;
      code = tsdbDataTombIterOpen(iter[0]);
      break;
H
Hongze Cheng 已提交
492 493 494 495
    case TSDB_ITER_TYPE_MEMT_TOMB:
      iter[0]->memtTomb->memt = config->memt;
      code = tsdbMemTombIterOpen(iter[0]);
      break;
H
Hongze Cheng 已提交
496
    default:
H
Hongze Cheng 已提交
497
      code = TSDB_CODE_INVALID_PARA;
H
Hongze Cheng 已提交
498
      ASSERTS(false, "Not implemented");
H
Hongze Cheng 已提交
499 500 501 502 503 504 505 506 507
  }

  if (code) {
    taosMemoryFree(iter[0]);
    iter[0] = NULL;
  }
  return code;
}

H
Hongze Cheng 已提交
508
static int32_t tsdbSttTombIterClose(STsdbIter *iter) {
H
Hongze Cheng 已提交
509
  tTombBlockDestroy(iter->sttTomb->tombBlock);
H
Hongze Cheng 已提交
510 511 512 513
  return 0;
}

static int32_t tsdbDataTombIterClose(STsdbIter *iter) {
H
Hongze Cheng 已提交
514
  tTombBlockDestroy(iter->dataTomb->tombBlock);
H
Hongze Cheng 已提交
515 516 517
  return 0;
}

H
Hongze Cheng 已提交
518 519 520 521 522 523 524 525 526 527 528
int32_t tsdbIterClose(STsdbIter **iter) {
  switch (iter[0]->type) {
    case TSDB_ITER_TYPE_STT:
      tsdbSttIterClose(iter[0]);
      break;
    case TSDB_ITER_TYPE_DATA:
      tsdbDataIterClose(iter[0]);
      break;
    case TSDB_ITER_TYPE_MEMT:
      tsdbMemTableIterClose(iter[0]);
      break;
H
Hongze Cheng 已提交
529 530 531 532 533 534
    case TSDB_ITER_TYPE_STT_TOMB:
      tsdbSttTombIterClose(iter[0]);
      break;
    case TSDB_ITER_TYPE_DATA_TOMB:
      tsdbDataTombIterClose(iter[0]);
      break;
H
Hongze Cheng 已提交
535 536
    case TSDB_ITER_TYPE_MEMT_TOMB:
      break;
H
Hongze Cheng 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    default:
      ASSERT(false);
  }
  taosMemoryFree(iter[0]);
  iter[0] = NULL;
  return 0;
}

int32_t tsdbIterNext(STsdbIter *iter) {
  switch (iter->type) {
    case TSDB_ITER_TYPE_STT:
      return tsdbSttIterNext(iter, NULL);
    case TSDB_ITER_TYPE_DATA:
      return tsdbDataIterNext(iter, NULL);
    case TSDB_ITER_TYPE_MEMT:
      return tsdbMemTableIterNext(iter, NULL);
H
Hongze Cheng 已提交
553 554 555 556
    case TSDB_ITER_TYPE_STT_TOMB:
      return tsdbSttTombIterNext(iter, NULL);
    case TSDB_ITER_TYPE_DATA_TOMB:
      return tsdbDataTombIterNext(iter, NULL);
H
Hongze Cheng 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
    default:
      ASSERT(false);
  }
  return 0;
}

static int32_t tsdbIterSkipTableData(STsdbIter *iter, const TABLEID *tbid) {
  switch (iter->type) {
    case TSDB_ITER_TYPE_STT:
      return tsdbSttIterNext(iter, tbid);
    case TSDB_ITER_TYPE_DATA:
      return tsdbDataIterNext(iter, tbid);
    case TSDB_ITER_TYPE_MEMT:
      return tsdbMemTableIterNext(iter, tbid);
    default:
      ASSERT(false);
  }
H
Hongze Cheng 已提交
574 575 576 577 578 579 580 581 582
  return 0;
}

static int32_t tsdbIterCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
  STsdbIter *iter1 = TCONTAINER_OF(n1, STsdbIter, node);
  STsdbIter *iter2 = TCONTAINER_OF(n2, STsdbIter, node);
  return tRowInfoCmprFn(&iter1->row, &iter2->row);
}

H
Hongze Cheng 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
static int32_t tsdbTombIterCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
  STsdbIter *iter1 = TCONTAINER_OF(n1, STsdbIter, node);
  STsdbIter *iter2 = TCONTAINER_OF(n2, STsdbIter, node);

  if (iter1->record->suid < iter2->record->suid) {
    return -1;
  } else if (iter1->record->suid > iter2->record->suid) {
    return 1;
  }

  if (iter1->record->uid < iter2->record->uid) {
    return -1;
  } else if (iter1->record->uid > iter2->record->uid) {
    return 1;
  }

  if (iter1->record->version < iter2->record->version) {
    return -1;
  } else if (iter1->record->version > iter2->record->version) {
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
608 609
// SIterMerger ================
struct SIterMerger {
H
Hongze Cheng 已提交
610
  bool       isTomb;
H
Hongze Cheng 已提交
611 612 613 614
  STsdbIter *iter;
  SRBTree    iterTree[1];
};

H
Hongze Cheng 已提交
615
int32_t tsdbIterMergerOpen(const TTsdbIterArray *iterArray, SIterMerger **merger, bool isTomb) {
H
Hongze Cheng 已提交
616 617
  STsdbIter   *iter;
  SRBTreeNode *node;
H
Hongze Cheng 已提交
618 619

  merger[0] = taosMemoryCalloc(1, sizeof(*merger[0]));
H
Hongze Cheng 已提交
620 621 622
  if (merger[0] == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
H
Hongze Cheng 已提交
623

H
Hongze Cheng 已提交
624
  merger[0]->isTomb = isTomb;
H
Hongze Cheng 已提交
625 626 627 628 629
  if (isTomb) {
    tRBTreeCreate(merger[0]->iterTree, tsdbTombIterCmprFn);
  } else {
    tRBTreeCreate(merger[0]->iterTree, tsdbIterCmprFn);
  }
H
Hongze Cheng 已提交
630 631
  TARRAY2_FOREACH(iterArray, iter) {
    if (iter->ctx->noMoreData) continue;
H
Hongze Cheng 已提交
632
    node = tRBTreePut(merger[0]->iterTree, iter->node);
H
Hongze Cheng 已提交
633 634 635 636 637 638
    ASSERT(node);
  }

  return tsdbIterMergerNext(merger[0]);
}

H
Hongze Cheng 已提交
639
int32_t tsdbIterMergerClose(SIterMerger **merger) {
H
Hongze Cheng 已提交
640 641 642 643
  if (merger[0]) {
    taosMemoryFree(merger[0]);
    merger[0] = NULL;
  }
H
Hongze Cheng 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
  return 0;
}

int32_t tsdbIterMergerNext(SIterMerger *merger) {
  int32_t      code;
  int32_t      c;
  SRBTreeNode *node;

  if (merger->iter) {
    code = tsdbIterNext(merger->iter);
    if (code) return code;

    if (merger->iter->ctx->noMoreData) {
      merger->iter = NULL;
    } else if ((node = tRBTreeMin(merger->iterTree))) {
H
Hongze Cheng 已提交
659
      c = merger->iterTree->cmprFn(merger->iter->node, node);
H
Hongze Cheng 已提交
660 661 662 663
      ASSERT(c);
      if (c > 0) {
        node = tRBTreePut(merger->iterTree, merger->iter->node);
        ASSERT(node);
H
Hongze Cheng 已提交
664
        merger->iter = NULL;
H
Hongze Cheng 已提交
665 666 667 668
      }
    }
  }

H
Hongze Cheng 已提交
669
  if (merger->iter == NULL && (node = tRBTreeDropMin(merger->iterTree))) {
H
Hongze Cheng 已提交
670 671 672 673 674 675
    merger->iter = TCONTAINER_OF(node, STsdbIter, node);
  }

  return 0;
}

H
Hongze Cheng 已提交
676 677 678 679 680 681 682 683 684
SRowInfo *tsdbIterMergerGetData(SIterMerger *merger) {
  ASSERT(!merger->isTomb);
  return merger->iter ? merger->iter->row : NULL;
}

STombRecord *tsdbIterMergerGetTombRecord(SIterMerger *merger) {
  ASSERT(merger->isTomb);
  return merger->iter ? merger->iter->record : NULL;
}
H
Hongze Cheng 已提交
685 686 687 688 689 690 691

int32_t tsdbIterMergerSkipTableData(SIterMerger *merger, const TABLEID *tbid) {
  int32_t      code;
  int32_t      c;
  SRBTreeNode *node;

  while (merger->iter && tbid->suid == merger->iter->row->suid && tbid->uid == merger->iter->row->uid) {
H
Hongze Cheng 已提交
692
    int32_t code = tsdbIterSkipTableData(merger->iter, tbid);
H
Hongze Cheng 已提交
693 694 695 696
    if (code) return code;

    if (merger->iter->ctx->noMoreData) {
      merger->iter = NULL;
H
Hongze Cheng 已提交
697
    } else if ((node = tRBTreeMin(merger->iterTree))) {
H
Hongze Cheng 已提交
698
      c = merger->iterTree->cmprFn(merger->iter->node, node);
H
Hongze Cheng 已提交
699 700 701 702
      ASSERT(c);
      if (c > 0) {
        node = tRBTreePut(merger->iterTree, merger->iter->node);
        ASSERT(node);
H
Hongze Cheng 已提交
703
        merger->iter = NULL;
H
Hongze Cheng 已提交
704 705 706 707 708 709 710
      }
    }

    if (!merger->iter && (node = tRBTreeDropMin(merger->iterTree))) {
      merger->iter = TCONTAINER_OF(node, STsdbIter, node);
    }
  }
H
Hongze Cheng 已提交
711

H
Hongze Cheng 已提交
712 713
  return 0;
}