tsdbMergeTree.c 3.6 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
 * 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"

// SLDataIter =================================================
typedef struct {
  SRBTreeNode node;
  SBlockL    *pBlockL;
  SRowInfo   *pRowInfo;

  SDataFReader *pReader;
  int32_t       iLast;
  int8_t        backward;
  SArray       *aBlockL;
  int32_t       iBlockL;
  SBlockData    bData;
  int32_t       iRow;
  SRowInfo      rInfo;
} SLDataIter;

int32_t tLDataIterOpen(SLDataIter *pIter, SDataFReader *pReader, int32_t iLast, int8_t backward) {
  int32_t code = 0;

  pIter->pReader = pReader;
  pIter->iLast = iLast;
  pIter->backward = backward;

  pIter->aBlockL = taosArrayInit(0, sizeof(SBlockL));
  if (pIter->aBlockL == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  code = tBlockDataCreate(&pIter->bData);
  if (code) goto _exit;

  code = tsdbReadBlockL(pReader, iLast, pIter->aBlockL);
  if (code) goto _exit;

  if (backward) {
    pIter->iBlockL = taosArrayGetSize(pIter->aBlockL) - 1;
  } else {
    pIter->iBlockL = 0;
  }

_exit:
  return code;
}

void tLDataIterClose(SLDataIter *pIter) {
  tBlockDataDestroy(&pIter->bData, 1);
  taosArrayDestroy(pIter->aBlockL);
}

extern int32_t tsdbReadLastBlockEx(SDataFReader *pReader, int32_t iLast, SBlockL *pBlockL, SBlockData *pBlockData);

void tLDataIterNextBlock(SLDataIter *pIter) {
  if (pIter->backward) {
    pIter->iBlockL--;
  } else {
    pIter->iBlockL++;
  }

  if (pIter->iBlockL >= 0 && pIter->iBlockL < taosArrayGetSize(pIter->aBlockL)) {
    pIter->pBlockL = (SBlockL *)taosArrayGet(pIter->aBlockL, pIter->iBlockL);
  } else {
    pIter->pBlockL = NULL;
  }
}

int32_t tLDataIterNextRow(SLDataIter *pIter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
87
  int32_t iBlockL = pIter->iBlockL;
H
Hongze Cheng 已提交
88 89 90
  if (pIter->backward) {
    pIter->iRow--;
    if (pIter->iRow < 0) {
H
Hongze Cheng 已提交
91
      tLDataIterNextBlock(pIter);
H
Hongze Cheng 已提交
92 93 94 95 96
    }
  } else {
    pIter->iRow++;
    if (pIter->iRow >= pIter->bData.nRow) {
      pIter->iBlockL++;
H
Hongze Cheng 已提交
97 98 99 100 101 102 103 104 105 106 107 108
      tLDataIterNextBlock(pIter);
    }
  }

  if (iBlockL != pIter->iBlockL) {
    if (pIter->pBlockL) {
      code = tsdbReadLastBlockEx(pIter->pReader, pIter->iLast, pIter->pBlockL, &pIter->bData);
      if (code) goto _exit;
      pIter->iRow = 0;
    } else {
      // no more data
      goto _exit;
H
Hongze Cheng 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }
  }

  pIter->rInfo.suid = pIter->bData.suid;
  pIter->rInfo.uid = pIter->bData.uid;
  pIter->rInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);

_exit:
  return code;
}

SRowInfo *tLDataIterGet(SLDataIter *pIter) {
  // TODO
  return NULL;
}

// SMergeTree =================================================
typedef struct {
  int8_t       backward;
  SRBTreeNode *pNode;
  SRBTree      rbt;
} SMergeTree;
H
Hongze Cheng 已提交
131

H
Hongze Cheng 已提交
132
static FORCE_INLINE int32_t tLDataIterCmprFn(const void *p1, const void *p2) {
H
Hongze Cheng 已提交
133 134 135 136 137 138 139 140 141
  SLDataIter *pIter1 = (SLDataIter *)p1;
  SLDataIter *pIter2 = (SLDataIter *)p2;

  // TODO
  ASSERT(0);

  return 0;
}

H
Hongze Cheng 已提交
142 143 144
void tMergeTreeOpen(SMergeTree *pMTree, int8_t backward) {
  pMTree->backward = backward;
  pMTree->pNode = NULL;
H
Hongze Cheng 已提交
145
  tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
H
Hongze Cheng 已提交
146
}
H
Hongze Cheng 已提交
147 148

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