tsdbCompact.c 4.4 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"

H
Hongze Cheng 已提交
18 19
// typedef struct {
// } SMemDIter;
H
Hongze Cheng 已提交
20 21 22 23

typedef struct {
  SArray    *aBlockIdx;  // SArray<SBlockIdx>
  SMapData   mDataBlk;   // SMapData<SDataBlk>
H
Hongze Cheng 已提交
24 25 26 27
  SBlockData bData;
  int32_t    iBlockIdx;
  int32_t    iDataBlk;
  int32_t    iRow;
H
Hongze Cheng 已提交
28 29 30 31 32
} SDataDIter;

typedef struct {
  SArray    *aSttBlk;  // SArray<SSttBlk>
  SBlockData bData;
H
Hongze Cheng 已提交
33 34
  int32_t    iSttBlk;
  int32_t    iRow;
H
Hongze Cheng 已提交
35 36
} SSttDIter;

H
Hongze Cheng 已提交
37 38 39 40 41 42
typedef struct {
  int32_t  flag;
  SRowInfo rowInfo;
  char     handle[];
} STsdbDataIter;

H
Hongze Cheng 已提交
43
typedef struct {
H
Hongze Cheng 已提交
44 45 46 47 48 49 50
  STsdb        *pTsdb;
  STsdbFS       fs;
  int64_t       cid;
  int32_t       fid;
  SDataFReader *pReader;
  SDFileSet    *pDFileSet;
  SRBTree       rtree;
H
Hongze Cheng 已提交
51 52
} STsdbCompactor;

H
Hongze Cheng 已提交
53 54
#define TSDB_FLG_DEEP_COMPACT 0x1

H
Hongze Cheng 已提交
55
// ITER =========================
H
Hongze Cheng 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
static int32_t tsdbDataIterOpen(STsdbDataIter *pIter) {
  int32_t code = 0;
  int32_t lino = 0;
  // TODO
_exit:
  return code;
}

static void tsdbDataIterClose(STsdbDataIter *pIter) {
  // TODO
}

static int32_t tsdbDataIterNext(STsdbDataIter *pIter) {
  int32_t code = 0;
  int32_t lino = 0;
  // TODO
_exit:
  return code;
}
H
Hongze Cheng 已提交
75 76

// COMPACT =========================
H
Hongze Cheng 已提交
77
static int32_t tsdbBeginCompact(STsdb *pTsdb, STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
78 79 80
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
81 82 83 84 85 86 87
  pCompactor->pTsdb = pTsdb;

  code = tsdbFSCopy(pTsdb, &pCompactor->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

  pCompactor->fid = INT32_MIN;

H
Hongze Cheng 已提交
88 89 90 91 92 93 94
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
95
static int32_t tsdbCommitCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
96 97 98
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
99 100
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
101 102 103 104 105 106 107 108 109
  // TODO

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
110
static int32_t tsdbAbortCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
111 112 113
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
114 115
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
116 117 118 119 120 121 122 123 124
  // TODO

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
125
static int32_t tsdbDeepCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
126 127 128
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
129
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
130

H
Hongze Cheng 已提交
131 132 133
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pTsdb, pCompactor->pDFileSet);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
134 135 136 137 138 139 140
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
141
static int32_t tsdbShallowCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
142
  int32_t code = 0;
H
Hongze Cheng 已提交
143 144
  int32_t lino = 0;

H
Hongze Cheng 已提交
145 146
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

int32_t tsdbCompact(STsdb *pTsdb, int32_t flag) {
  int32_t code = 0;
  int32_t lino = 0;

  // Check if can do compact (TODO)

  // Do compact
H
Hongze Cheng 已提交
161 162 163
  STsdbCompactor compactor = {0};

  code = tsdbBeginCompact(pTsdb, &compactor);
H
Hongze Cheng 已提交
164 165
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
166 167 168 169
  while (true) {
    compactor.pDFileSet = (SDFileSet *)taosArraySearch(compactor.fs.aDFileSet, &compactor.fid, tDFileSetCmprFn, TD_GT);
    if (compactor.pDFileSet == NULL) break;

H
Hongze Cheng 已提交
170 171
    compactor.fid = compactor.pDFileSet->fid;

H
Hongze Cheng 已提交
172 173 174 175 176 177 178
    if (flag & TSDB_FLG_DEEP_COMPACT) {
      code = tsdbDeepCompact(&compactor);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else {
      code = tsdbShallowCompact(&compactor);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
179 180 181 182 183
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
184
    tsdbAbortCompact(&compactor);
H
Hongze Cheng 已提交
185
  } else {
H
Hongze Cheng 已提交
186
    tsdbCommitCompact(&compactor);
H
Hongze Cheng 已提交
187
  }
H
Hongze Cheng 已提交
188 189
  return code;
}