tsdbCompact.c 28.2 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 已提交
17
#if 0
H
Hongze Cheng 已提交
18

H
Hongze Cheng 已提交
19 20 21 22
#define TSDB_ITER_TYPE_MEM 0x0
#define TSDB_ITER_TYPE_DAT 0x1
#define TSDB_ITER_TYPE_STT 0x2

H
Hongze Cheng 已提交
23 24
typedef struct {
} SMemDIter;
H
Hongze Cheng 已提交
25 26

typedef struct {
H
Hongze Cheng 已提交
27 28 29 30 31 32 33
  SDataFReader *pReader;
  SArray       *aBlockIdx;  // SArray<SBlockIdx>
  SMapData      mDataBlk;   // SMapData<SDataBlk>
  SBlockData    bData;
  int32_t       iBlockIdx;
  int32_t       iDataBlk;
  int32_t       iRow;
H
Hongze Cheng 已提交
34 35 36
} SDataDIter;

typedef struct {
H
Hongze Cheng 已提交
37 38 39 40 41 42
  SDataFReader *pReader;
  int32_t       iStt;
  SArray       *aSttBlk;  // SArray<SSttBlk>
  SBlockData    bData;
  int32_t       iSttBlk;
  int32_t       iRow;
H
Hongze Cheng 已提交
43 44
} SSttDIter;

H
Hongze Cheng 已提交
45 46 47
typedef struct STsdbDataIter {
  struct STsdbDataIter *next;

H
Hongze Cheng 已提交
48 49 50 51
  int32_t     flag;
  SRowInfo    rowInfo;
  SRBTreeNode n;
  char        handle[];
H
Hongze Cheng 已提交
52 53
} STsdbDataIter;

H
Hongze Cheng 已提交
54 55
#define TSDB_DATA_ITER_FROM_RBTN(N) ((STsdbDataIter *)((char *)N - offsetof(STsdbDataIter, n)))

H
Hongze Cheng 已提交
56
typedef struct {
H
Hongze Cheng 已提交
57 58
  STsdb     *pTsdb;
  int64_t    cid;
H
Hongze Cheng 已提交
59
  int8_t     cmprAlg;
H
Hongze Cheng 已提交
60 61 62 63 64 65
  int32_t    maxRows;
  int32_t    minRows;
  STsdbFS    fs;
  int32_t    fid;
  SDFileSet *pDFileSet;

H
Hongze Cheng 已提交
66 67 68 69 70 71
  // Tombstone
  SDelFReader *pDelFReader;
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
  SArray      *aSkyLine;  // SArray<TSDBKEY>
  TSDBKEY     *aTSDBKEY;
H
Hongze Cheng 已提交
72 73
  int32_t      iKey;
  TSDBKEY      sKey;
H
Hongze Cheng 已提交
74

H
Hongze Cheng 已提交
75
  // Reader
H
Hongze Cheng 已提交
76 77 78
  SDataFReader  *pReader;
  STsdbDataIter *iterList;  // list of iterators
  SRBTree        rtree;
H
Hongze Cheng 已提交
79
  STsdbDataIter *pIter;
H
Hongze Cheng 已提交
80
  SBlockData     bData;
H
Hongze Cheng 已提交
81
  SSkmInfo       tbSkm;
H
Hongze Cheng 已提交
82 83 84 85

  // Writer
  SDataFWriter *pWriter;
  SArray       *aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
86 87
  SMapData      mDataBlk;   // SMapData<SDataBlk>
  SArray       *aSttBlk;    // SArray<SSttBlk>
H
Hongze Cheng 已提交
88
  TABLEID       tableId;
H
Hongze Cheng 已提交
89 90
} STsdbCompactor;

H
Hongze Cheng 已提交
91 92
#define TSDB_FLG_DEEP_COMPACT 0x1

H
Hongze Cheng 已提交
93
// ITER =========================
H
Hongze Cheng 已提交
94
static int32_t tsdbDataIterNext(STsdbDataIter *pIter, TABLEID *pExcludeTableId);
H
Hongze Cheng 已提交
95

H
Hongze Cheng 已提交
96 97 98 99 100 101 102
static int32_t tsdbDataIterCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
  const STsdbDataIter *pIter1 = (STsdbDataIter *)((char *)n1 - offsetof(STsdbDataIter, n));
  const STsdbDataIter *pIter2 = (STsdbDataIter *)((char *)n2 - offsetof(STsdbDataIter, n));

  return tRowInfoCmprFn(&pIter1->rowInfo, &pIter2->rowInfo);
}

H
Hongze Cheng 已提交
103
static int32_t tsdbMemDIterOpen(STsdbDataIter **ppIter) {
H
Hongze Cheng 已提交
104 105
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
106 107 108 109 110 111 112

  STsdbDataIter *pIter = (STsdbDataIter *)taosMemoryCalloc(1, sizeof(*pIter) + sizeof(SMemDIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
113
  // TODO
H
Hongze Cheng 已提交
114

H
Hongze Cheng 已提交
115
_exit:
H
Hongze Cheng 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  if (code) {
    *ppIter = NULL;
  } else {
    *ppIter = pIter;
  }
  return code;
}

static int32_t tsdbDataDIterOpen(SDataFReader *pReader, STsdbDataIter **ppIter) {
  int32_t code = 0;
  int32_t lino = 0;

  STsdbDataIter *pIter = (STsdbDataIter *)taosMemoryCalloc(1, sizeof(*pIter) + sizeof(SDataDIter));
  if (NULL == pIter) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
133
  pIter->flag = TSDB_ITER_TYPE_DAT;
H
Hongze Cheng 已提交
134

H
Hongze Cheng 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147
  SDataDIter *pDataDIter = (SDataDIter *)pIter->handle;
  pDataDIter->pReader = pReader;
  pDataDIter->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pDataDIter->aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  code = tsdbReadBlockIdx(pReader, pDataDIter->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(pDataDIter->aBlockIdx) == 0) goto _clear_exit;

H
Hongze Cheng 已提交
148
  // TODO
H
Hongze Cheng 已提交
149 150 151
  code = tBlockDataCreate(&pDataDIter->bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
152
  pDataDIter->iBlockIdx = -1;
H
Hongze Cheng 已提交
153 154 155
  pDataDIter->iDataBlk = 0;
  pDataDIter->iRow = 0;

H
Hongze Cheng 已提交
156
  code = tsdbDataIterNext(pIter, NULL);
H
Hongze Cheng 已提交
157
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
158 159 160

_exit:
  if (code) {
H
Hongze Cheng 已提交
161
  _clear_exit:
H
Hongze Cheng 已提交
162
    *ppIter = NULL;
H
Hongze Cheng 已提交
163
    if (pIter) {
H
Hongze Cheng 已提交
164
      tBlockDataDestroy(&pDataDIter->bData);
H
Hongze Cheng 已提交
165 166 167 168
      tMapDataClear(&pDataDIter->mDataBlk);
      taosArrayDestroy(pDataDIter->aBlockIdx);
      taosMemoryFree(pIter);
    }
H
Hongze Cheng 已提交
169 170 171 172 173 174
  } else {
    *ppIter = pIter;
  }
  return code;
}

H
Hongze Cheng 已提交
175
static int32_t tsdbSttDIterOpen(SDataFReader *pReader, int32_t iStt, STsdbDataIter **ppIter) {
H
Hongze Cheng 已提交
176 177 178 179 180 181 182 183
  int32_t code = 0;
  int32_t lino = 0;

  STsdbDataIter *pIter = (STsdbDataIter *)taosMemoryCalloc(1, sizeof(*pIter) + sizeof(SSttDIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
184
  pIter->flag = TSDB_ITER_TYPE_STT;
H
Hongze Cheng 已提交
185

H
Hongze Cheng 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  SSttDIter *pSttDIter = (SSttDIter *)pIter->handle;
  pSttDIter->pReader = pReader;
  pSttDIter->iStt = iStt;
  pSttDIter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
  if (pSttDIter->aSttBlk == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  code = tsdbReadSttBlk(pReader, pSttDIter->iStt, pSttDIter->aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(pSttDIter->aSttBlk) == 0) goto _clear_exit;

  code = tBlockDataCreate(&pSttDIter->bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
203 204
  pSttDIter->iSttBlk = -1;
  pSttDIter->iRow = -1;
H
Hongze Cheng 已提交
205

H
Hongze Cheng 已提交
206
  code = tsdbDataIterNext(pIter, NULL);
H
Hongze Cheng 已提交
207
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
208 209 210

_exit:
  if (code) {
H
Hongze Cheng 已提交
211
  _clear_exit:
H
Hongze Cheng 已提交
212
    *ppIter = NULL;
H
Hongze Cheng 已提交
213
    if (pIter) {
H
Hongze Cheng 已提交
214
      tBlockDataDestroy(&pSttDIter->bData);
H
Hongze Cheng 已提交
215 216 217
      taosArrayDestroy(pSttDIter->aSttBlk);
      taosMemoryFree(pIter);
    }
H
Hongze Cheng 已提交
218 219 220
  } else {
    *ppIter = pIter;
  }
H
Hongze Cheng 已提交
221 222 223 224
  return code;
}

static void tsdbDataIterClose(STsdbDataIter *pIter) {
H
Hongze Cheng 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  if (pIter == NULL) return;

  if (pIter->flag & TSDB_ITER_TYPE_MEM) {
    ASSERT(0);
  } else if (pIter->flag & TSDB_ITER_TYPE_DAT) {
    ASSERT(0);
  } else if (pIter->flag & TSDB_ITER_TYPE_STT) {
    SSttDIter *pSttDIter = (SSttDIter *)pIter->handle;

    tBlockDataDestroy(&pSttDIter->bData);
    taosArrayDestroy(pSttDIter->aSttBlk);
  } else {
    ASSERT(0);
  }

  taosMemoryFree(pIter);
H
Hongze Cheng 已提交
241 242
}

H
Hongze Cheng 已提交
243
static int32_t tsdbDataIterNext(STsdbDataIter *pIter, TABLEID *pExcludeTableId) {
H
Hongze Cheng 已提交
244 245
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
246 247 248 249 250 251 252 253 254 255

  if (pIter->flag & TSDB_ITER_TYPE_MEM) {
    // TODO
    ASSERT(0);
  } else if (pIter->flag & TSDB_ITER_TYPE_DAT) {
    // TODO
    ASSERT(0);
  } else if (pIter->flag & TSDB_ITER_TYPE_STT) {
    SSttDIter *pSttDIter = (SSttDIter *)pIter->handle;

H
Hongze Cheng 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    for (;;) {
      if (++pSttDIter->iRow >= pSttDIter->bData.nRow) {
        for (;;) {
          if (++pSttDIter->iSttBlk < taosArrayGetSize(pSttDIter->aSttBlk)) {
            SSttBlk *pSttBlk = (SSttBlk *)taosArrayGet(pSttDIter->aSttBlk, pSttDIter->iSttBlk);

            // check exclusion
            if (pExcludeTableId) {
              if (pExcludeTableId->uid) {  // exclude (suid, uid)
                if (pSttBlk->minUid == pExcludeTableId->uid && pSttBlk->maxUid == pExcludeTableId->uid) continue;
              } else {  // exclude (suid, *)
                if (pSttBlk->suid == pExcludeTableId->suid) continue;
              }
            }

            code = tsdbReadSttBlockEx(pSttDIter->pReader, pSttDIter->iStt, pSttBlk, &pSttDIter->bData);
            TSDB_CHECK_CODE(code, lino, _exit);

            pIter->rowInfo.suid = pSttBlk->suid;
            pSttDIter->iRow = 0;
            break;
          } else {
            // iter end, all set 0 and exit
            pIter->rowInfo.suid = 0;
            pIter->rowInfo.uid = 0;
            goto _exit;
          }
        }
      }

H
Hongze Cheng 已提交
286 287
      pIter->rowInfo.uid = pSttDIter->bData.uid ? pSttDIter->bData.uid : pSttDIter->bData.aUid[pSttDIter->iRow];
      pIter->rowInfo.row = tsdbRowFromBlockData(&pSttDIter->bData, pSttDIter->iRow);
H
Hongze Cheng 已提交
288 289 290 291 292 293 294 295

      // check exclusion
      if (pExcludeTableId) {
        if (pExcludeTableId->uid) {  // exclude (suid, uid)
          if (pIter->rowInfo.uid == pExcludeTableId->uid) continue;
        } else {  // exclude (suid, *)
          if (pIter->rowInfo.suid == pExcludeTableId->suid) continue;
        }
H
Hongze Cheng 已提交
296
      }
H
Hongze Cheng 已提交
297 298

      break;
H
Hongze Cheng 已提交
299 300 301 302 303
    }
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
304 305 306
_exit:
  return code;
}
H
Hongze Cheng 已提交
307 308

// COMPACT =========================
H
Hongze Cheng 已提交
309
static int32_t tsdbBeginCompact(STsdb *pTsdb, STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
310 311 312
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
313
  pCompactor->pTsdb = pTsdb;
H
Hongze Cheng 已提交
314 315
  pCompactor->cid = 0;  // TODO
  pCompactor->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
316 317
  pCompactor->maxRows = pTsdb->pVnode->config.tsdbCfg.maxRows;
  pCompactor->minRows = pTsdb->pVnode->config.tsdbCfg.minRows;
H
Hongze Cheng 已提交
318 319 320 321 322 323

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

  pCompactor->fid = INT32_MIN;

H
Hongze Cheng 已提交
324 325 326
  code = tBlockDataCreate(&pCompactor->bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  // tombstone
  if (pCompactor->fs.pDelFile) {
    code = tsdbDelFReaderOpen(&pCompactor->pDelFReader, pCompactor->fs.pDelFile, pTsdb);
    TSDB_CHECK_CODE(code, lino, _exit);

    pCompactor->aDelIdx = taosArrayInit(0, sizeof(SDelIdx));
    if (pCompactor->aDelIdx == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    pCompactor->aDelData = taosArrayInit(0, sizeof(SDelData));
    if (pCompactor->aDelData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    pCompactor->aSkyLine = taosArrayInit(0, sizeof(TSDBKEY));
    if (pCompactor->aSkyLine == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    code = tsdbReadDelIdx(pCompactor->pDelFReader, pCompactor->aDelIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
354 355 356 357 358 359 360
_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 已提交
361
static void tsdbEndCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
362 363 364 365
  taosArrayDestroy(pCompactor->aSkyLine);
  taosArrayDestroy(pCompactor->aDelData);
  taosArrayDestroy(pCompactor->aDelIdx);
  tsdbDelFReaderClose(&pCompactor->pDelFReader);
H
Hongze Cheng 已提交
366 367 368 369 370 371 372 373 374
  tsdbFSDestroy(&pCompactor->fs);
  tBlockDataDestroy(&pCompactor->bData);
  tDestroyTSchema(pCompactor->tbSkm.pTSchema);
  pCompactor->tbSkm.pTSchema = NULL;
  taosArrayDestroy(pCompactor->aBlockIdx);
  tMapDataClear(&pCompactor->mDataBlk);
  taosArrayDestroy(pCompactor->aSttBlk);
}

H
Hongze Cheng 已提交
375
static int32_t tsdbCommitCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
376 377 378
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
379 380
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
381 382 383 384 385 386 387 388 389 390 391 392
  code = tsdbFSPrepareCommit(pTsdb, &pCompactor->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

  taosThreadRwlockWrlock(&pTsdb->rwLock);

  code = tsdbFSCommit(pTsdb);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
393 394 395 396 397 398 399 400

_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 已提交
401
static int32_t tsdbAbortCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
402 403 404
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
405 406
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
407 408 409 410 411 412 413 414 415
  // 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 已提交
416
static int32_t tsdbDeepCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
417 418 419
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
420
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
421

H
Hongze Cheng 已提交
422 423 424
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pTsdb, pCompactor->pDFileSet);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
425 426 427 428 429 430 431
_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 已提交
432
static int32_t tsdbShallowCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
433
  int32_t code = 0;
H
Hongze Cheng 已提交
434 435
  int32_t lino = 0;

H
Hongze Cheng 已提交
436 437
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
438 439 440 441 442 443 444
_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 已提交
445
static int32_t tsdbCompactNextRowImpl(STsdbCompactor *pCompactor, TABLEID *pExcludeTableId) {
H
Hongze Cheng 已提交
446 447
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
448

H
Hongze Cheng 已提交
449 450 451 452
  for (;;) {
    if (pCompactor->pIter) {
      code = tsdbDataIterNext(pCompactor->pIter, pExcludeTableId);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
453

H
Hongze Cheng 已提交
454 455 456 457 458 459
      if (pCompactor->pIter->rowInfo.suid == 0 && pCompactor->pIter->rowInfo.uid == 0) {
        pCompactor->pIter = NULL;
      } else {
        SRBTreeNode *pNode = tRBTreeMin(&pCompactor->rtree);
        if (pNode) {
          STsdbDataIter *pIter = TSDB_DATA_ITER_FROM_RBTN(pNode);
H
Hongze Cheng 已提交
460

H
Hongze Cheng 已提交
461 462
          int32_t c = tRowInfoCmprFn(&pCompactor->pIter->rowInfo, &pIter->rowInfo);
          ASSERT(c);
H
Hongze Cheng 已提交
463

H
Hongze Cheng 已提交
464 465 466 467
          if (c > 0) {
            tRBTreePut(&pCompactor->rtree, &pCompactor->pIter->n);
            pCompactor->pIter = NULL;
          }
H
Hongze Cheng 已提交
468 469 470
        }
      }
    }
H
Hongze Cheng 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

    if (pCompactor->pIter == NULL) {
      SRBTreeNode *pNode = tRBTreeMin(&pCompactor->rtree);
      if (pNode) {
        pCompactor->pIter = TSDB_DATA_ITER_FROM_RBTN(pNode);
        tRBTreeDrop(&pCompactor->rtree, pNode);

        if (pExcludeTableId) {
          if (pExcludeTableId->uid) {
            if (pCompactor->pIter->rowInfo.uid == pExcludeTableId->uid) continue;
          } else {
            if (pCompactor->pIter->rowInfo.suid == pExcludeTableId->suid) continue;
          }
        }
      }
    }

    break;
H
Hongze Cheng 已提交
489 490
  }

H
Hongze Cheng 已提交
491 492 493 494
_exit:
  return code;
}

H
Hongze Cheng 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
static int32_t tDelIdxCmprFn(const SDelIdx *pDelIdx1, const SDelIdx *pDelIdx2) {
  if (pDelIdx1->suid < pDelIdx2->suid) {
    return -1;
  } else if (pDelIdx1->suid > pDelIdx2->suid) {
    return 1;
  }

  if (pDelIdx1->uid < pDelIdx2->uid) {
    return -1;
  } else if (pDelIdx1->uid > pDelIdx2->uid) {
    return 1;
  }

  return 0;
}
H
Hongze Cheng 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

static bool tsdbCompactRowIsDeleted(STsdbCompactor *pCompactor, TSDBROW *pRow) {
  TSDBKEY tKey = TSDBROW_KEY(pRow);

  while (tKey.ts > pCompactor->sKey.ts) {
    pCompactor->sKey.version = pCompactor->aTSDBKEY[pCompactor->iKey].version;
    pCompactor->iKey++;
    if (pCompactor->iKey < taosArrayGetSize(pCompactor->aSkyLine)) {
      pCompactor->sKey.ts = pCompactor->aTSDBKEY[pCompactor->iKey].ts;
    } else {
      pCompactor->sKey.ts = TSKEY_MAX;
    }
  }

  if (tKey.ts < pCompactor->sKey.ts) {
    if (tKey.version > pCompactor->sKey.version) {
      return false;
    } else {
      return true;
    }
  } else if (tKey.ts == pCompactor->sKey.ts) {
    int64_t version;
    if (pCompactor->iKey < taosArrayGetSize(pCompactor->aSkyLine)) {
      version = TMAX(pCompactor->sKey.version, pCompactor->aTSDBKEY[pCompactor->iKey].version);
H
Hongze Cheng 已提交
534 535
    } else {
      version = pCompactor->sKey.version;
H
Hongze Cheng 已提交
536 537 538 539 540 541 542 543 544 545 546 547
    }

    if (tKey.version > version) {
      return false;
    } else {
      return true;
    }
  }

  return false;
}

H
Hongze Cheng 已提交
548 549 550 551
static int32_t tsdbCompactNextRow(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
552 553 554
  TABLEID  excludeTableId;
  TABLEID *pExcludeTableId = NULL;

H
Hongze Cheng 已提交
555
  for (;;) {
H
Hongze Cheng 已提交
556
    code = tsdbCompactNextRowImpl(pCompactor, pExcludeTableId);
H
Hongze Cheng 已提交
557 558 559 560
    TSDB_CHECK_CODE(code, lino, _exit);

    // check if the table of the row exists
    if (pCompactor->pIter) {
H
Hongze Cheng 已提交
561 562
      SRowInfo *pRowInfo = &pCompactor->pIter->rowInfo;

H
Hongze Cheng 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
      if (pRowInfo->uid != pCompactor->tbSkm.uid) {
        SMetaInfo info;
        if (pRowInfo->suid) {  // child table

          // check if super table exists
          if (pRowInfo->suid != pCompactor->tbSkm.suid) {
            if (metaGetInfo(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->uid, &info, NULL) != TSDB_CODE_SUCCESS) {
              excludeTableId.suid = pRowInfo->suid;
              excludeTableId.uid = 0;
              pExcludeTableId = &excludeTableId;
              continue;
            }

            // super table exists
            pCompactor->tbSkm.suid = pRowInfo->suid;
            pCompactor->tbSkm.uid = 0;
            tDestroyTSchema(pCompactor->tbSkm.pTSchema);
            pCompactor->tbSkm.pTSchema = metaGetTbTSchema(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->suid, -1, 1);
            if (pCompactor->tbSkm.pTSchema == NULL) {
              code = TSDB_CODE_OUT_OF_MEMORY;
              TSDB_CHECK_CODE(code, lino, _exit);
            }
          }
H
Hongze Cheng 已提交
586

H
Hongze Cheng 已提交
587 588 589 590 591 592 593
          // check if table exists
          if (metaGetInfo(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->uid, &info, NULL) != TSDB_CODE_SUCCESS) {
            excludeTableId.suid = pRowInfo->suid;
            excludeTableId.uid = pRowInfo->uid;
            pExcludeTableId = &excludeTableId;
            continue;
          }
H
Hongze Cheng 已提交
594

H
Hongze Cheng 已提交
595 596 597 598
          // table exists
          pCompactor->tbSkm.uid = pRowInfo->uid;
        } else {  // normal table
          // check if table exists
H
Hongze Cheng 已提交
599 600
          if (metaGetInfo(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->uid, &info, NULL) != TSDB_CODE_SUCCESS) {
            excludeTableId.suid = pRowInfo->suid;
H
Hongze Cheng 已提交
601
            excludeTableId.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
602 603 604 605
            pExcludeTableId = &excludeTableId;
            continue;
          }

H
Hongze Cheng 已提交
606
          // table exists
H
Hongze Cheng 已提交
607
          pCompactor->tbSkm.suid = pRowInfo->suid;
H
Hongze Cheng 已提交
608
          pCompactor->tbSkm.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
609
          tDestroyTSchema(pCompactor->tbSkm.pTSchema);
H
Hongze Cheng 已提交
610

H
Hongze Cheng 已提交
611 612
          pCompactor->tbSkm.pTSchema = metaGetTbTSchema(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->suid, -1, 1);
          if (pCompactor->tbSkm.pTSchema == NULL) {
H
Hongze Cheng 已提交
613 614 615
            code = TSDB_CODE_OUT_OF_MEMORY;
            TSDB_CHECK_CODE(code, lino, _exit);
          }
H
Hongze Cheng 已提交
616
        }
H
Hongze Cheng 已提交
617

H
Hongze Cheng 已提交
618 619 620 621 622 623 624 625
        // load delData and build the skyline
        if (pCompactor->pDelFReader) {
          SDelIdx *pDelIdx =
              taosArraySearch(pCompactor->aDelIdx, &(SDelIdx){.suid = pRowInfo->suid, .uid = pRowInfo->uid},
                              (__compar_fn_t)tDelIdxCmprFn, TD_EQ);
          if (pDelIdx) {
            code = tsdbReadDelData(pCompactor->pDelFReader, pDelIdx, pCompactor->aDelData);
            TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
626

H
Hongze Cheng 已提交
627 628 629
            code = tsdbBuildDeleteSkyline(pCompactor->aDelData, 0, taosArrayGetSize(pCompactor->aDelData) - 1,
                                          pCompactor->aSkyLine);
            TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
630

H
Hongze Cheng 已提交
631
            pCompactor->aTSDBKEY = (TSDBKEY *)TARRAY_DATA(pCompactor->aSkyLine);
H
Hongze Cheng 已提交
632 633 634
            pCompactor->iKey = 0;
            pCompactor->sKey.version = 0;
            pCompactor->sKey.ts = pCompactor->aTSDBKEY[0].ts;
H
Hongze Cheng 已提交
635 636 637
          } else {
            pCompactor->aTSDBKEY = NULL;
          }
H
Hongze Cheng 已提交
638 639
        }
      }
H
Hongze Cheng 已提交
640

H
Hongze Cheng 已提交
641
      ASSERT(pRowInfo->uid == pCompactor->tbSkm.uid);
H
Hongze Cheng 已提交
642

H
Hongze Cheng 已提交
643 644
      // check if the row is deleted
      if (pCompactor->aTSDBKEY && tsdbCompactRowIsDeleted(pCompactor, &pRowInfo->row)) {
H
Hongze Cheng 已提交
645 646 647 648
        continue;
      } else {
        break;
      }
H
Hongze Cheng 已提交
649 650 651
    } else {
      // iter end, just break out
      break;
H
Hongze Cheng 已提交
652 653 654 655
    }
  }

_exit:
H
Hongze Cheng 已提交
656 657 658 659
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
  }
H
Hongze Cheng 已提交
660 661 662
  return code;
}

H
Hongze Cheng 已提交
663
static int32_t tsdbCompactGetRow(STsdbCompactor *pCompactor, SRowInfo **ppRowInfo, STSchema **ppTSchema) {
H
Hongze Cheng 已提交
664 665 666 667 668 669 670 671 672
  int32_t code = 0;
  int32_t lino = 0;

  if (pCompactor->pIter == NULL) {
    code = tsdbCompactNextRow(pCompactor);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  if (pCompactor->pIter) {
H
Hongze Cheng 已提交
673 674
    ASSERT(pCompactor->pIter->rowInfo.suid == pCompactor->tbSkm.suid);
    ASSERT(pCompactor->pIter->rowInfo.uid == pCompactor->tbSkm.uid);
H
Hongze Cheng 已提交
675
    *ppRowInfo = &pCompactor->pIter->rowInfo;
H
Hongze Cheng 已提交
676
    *ppTSchema = pCompactor->tbSkm.pTSchema;
H
Hongze Cheng 已提交
677
  } else {
H
Hongze Cheng 已提交
678 679
    *ppRowInfo = NULL;
    *ppTSchema = NULL;
H
Hongze Cheng 已提交
680 681
  }

H
Hongze Cheng 已提交
682 683 684 685
_exit:
  return code;
}

H
Hongze Cheng 已提交
686
static int32_t tsdbOpenCompactor(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
687 688 689
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
690
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
691

H
Hongze Cheng 已提交
692
  // reader
H
Hongze Cheng 已提交
693
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pTsdb, pCompactor->pDFileSet);
H
Hongze Cheng 已提交
694 695
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
696 697
  // open iters
  STsdbDataIter *pIter;
H
Hongze Cheng 已提交
698

H
Hongze Cheng 已提交
699 700
  pCompactor->iterList = NULL;
  tRBTreeCreate(&pCompactor->rtree, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
701

H
Hongze Cheng 已提交
702 703 704 705 706 707 708 709 710 711 712
  code = tsdbDataDIterOpen(pCompactor->pReader, &pIter);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (pIter) {
    pIter->next = pCompactor->iterList;
    pCompactor->iterList = pIter;
    tRBTreePut(&pCompactor->rtree, &pIter->n);
  }

  for (int32_t iStt = 0; iStt < pCompactor->pReader->pSet->nSttF; iStt++) {
    code = tsdbSttDIterOpen(pCompactor->pReader, iStt, &pIter);
H
Hongze Cheng 已提交
713 714
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
715 716 717 718 719 720
    if (pIter) {
      pIter->next = pCompactor->iterList;
      pCompactor->iterList = pIter;
      tRBTreePut(&pCompactor->rtree, &pIter->n);
    }
  }
H
Hongze Cheng 已提交
721
  pCompactor->pIter = NULL;
H
Hongze Cheng 已提交
722
  tBlockDataReset(&pCompactor->bData);
H
Hongze Cheng 已提交
723

H
Hongze Cheng 已提交
724 725 726 727 728 729 730 731 732
  // writer
  SDFileSet wSet = {.diskId = (SDiskID){0},  // TODO
                    .fid = pCompactor->pDFileSet->fid,
                    .pHeadF = &(SHeadFile){.commitID = pCompactor->cid},
                    .pDataF = &(SDataFile){.commitID = pCompactor->cid},
                    .pSmaF = &(SSmaFile){.commitID = pCompactor->cid},
                    .nSttF = 1,
                    .aSttF = {&(SSttFile){.commitID = pCompactor->cid}}};
  code = tsdbDataFWriterOpen(&pCompactor->pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
  TSDB_CHECK_CODE(code, lino, _exit);

  if (pCompactor->aBlockIdx == NULL) {
    pCompactor->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
    if (pCompactor->aBlockIdx == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  } else {
    taosArrayClear(pCompactor->aBlockIdx);
  }

  tMapDataReset(&pCompactor->mDataBlk);

  if (pCompactor->aSttBlk == NULL) {
    pCompactor->aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
    if (pCompactor->aSttBlk == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  } else {
    taosArrayClear(pCompactor->aSttBlk);
  }

H
Hongze Cheng 已提交
757 758 759 760 761 762 763 764
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
  return code;
}
H
Hongze Cheng 已提交
765

H
Hongze Cheng 已提交
766 767 768 769 770 771
static void tsdbCloseCompactor(STsdbCompactor *pCompactor) {
  for (STsdbDataIter *pIter = pCompactor->iterList; pIter;) {
    STsdbDataIter *pIterNext = pIter->next;
    tsdbDataIterClose(pIter);
    pIter = pIterNext;
  }
H
Hongze Cheng 已提交
772

H
Hongze Cheng 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
  tDestroyTSchema(pCompactor->tbSkm.pTSchema);
  pCompactor->tbSkm.pTSchema = NULL;

  tsdbDataFReaderClose(&pCompactor->pReader);
}

extern int32_t tsdbWriteDataBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SMapData *mDataBlk, int8_t cmprAlg);
extern int32_t tsdbWriteSttBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SArray *aSttBlk, int8_t cmprAlg);
static int32_t tsdbCompactWriteBlockData(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

  SBlockData *pBData = &pCompactor->bData;

  if (pBData->nRow == 0) goto _exit;

  if (pBData->uid && pBData->nRow >= pCompactor->minRows) {  // write to .data file
    code = tsdbWriteDataBlock(pCompactor->pWriter, pBData, &pCompactor->mDataBlk, pCompactor->cmprAlg);
    TSDB_CHECK_CODE(code, lino, _exit);

    pCompactor->tableId.suid = pBData->suid;
    pCompactor->tableId.uid = pBData->uid;
  } else {  // write to .stt file
    code = tsdbWriteSttBlock(pCompactor->pWriter, pBData, pCompactor->aSttBlk, pCompactor->cmprAlg);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  tBlockDataClear(&pCompactor->bData);
H
Hongze Cheng 已提交
801

H
Hongze Cheng 已提交
802
_exit:
H
Hongze Cheng 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
  }
  return code;
}

static int32_t tsdbCompactWriteDataBlk(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

  if (pCompactor->mDataBlk.nItem == 0) goto _exit;

  SBlockIdx *pBlockIdx = (SBlockIdx *)taosArrayReserve(pCompactor->aBlockIdx, 1);
  if (pBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  pBlockIdx->suid = pCompactor->tableId.suid;
  pBlockIdx->uid = pCompactor->tableId.uid;

  code = tsdbWriteDataBlk(pCompactor->pWriter, &pCompactor->mDataBlk, pBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

  tMapDataReset(&pCompactor->mDataBlk);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
  }
  return code;
H
Hongze Cheng 已提交
836
}
H
Hongze Cheng 已提交
837

H
Hongze Cheng 已提交
838
static int32_t tsdbCompactFileSet(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
839 840 841
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
842 843 844
  // open compactor
  code = tsdbOpenCompactor(pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
845

H
Hongze Cheng 已提交
846 847 848 849
  // do compact
  SRowInfo *pRowInfo;
  STSchema *pTSchema;
  int64_t   nRow = 0;
H
Hongze Cheng 已提交
850

H
Hongze Cheng 已提交
851
  code = tsdbCompactGetRow(pCompactor, &pRowInfo, &pTSchema);
H
Hongze Cheng 已提交
852 853
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
854
  if (pRowInfo && (code = tBlockDataInit(&pCompactor->bData, (TABLEID *)pRowInfo, pTSchema, NULL, 0))) {
H
Hongze Cheng 已提交
855
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
856 857
    pCompactor->tableId.suid = pRowInfo->suid;
    pCompactor->tableId.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
858
  }
H
Hongze Cheng 已提交
859

H
Hongze Cheng 已提交
860
  while (pRowInfo) {
H
Hongze Cheng 已提交
861 862
    // if suid changed
    if (pCompactor->tableId.suid != pRowInfo->suid) {
H
Hongze Cheng 已提交
863 864
      code = tsdbCompactWriteBlockData(pCompactor);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
865

H
Hongze Cheng 已提交
866 867
      code = tsdbCompactWriteDataBlk(pCompactor);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
868

H
Hongze Cheng 已提交
869
      code = tBlockDataInit(&pCompactor->bData, (TABLEID *)pRowInfo, pTSchema, NULL, 0);
H
Hongze Cheng 已提交
870
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
871

H
Hongze Cheng 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
      pCompactor->tableId.suid = pRowInfo->suid;
      pCompactor->tableId.uid = pRowInfo->uid;
    }

    // if uid changed
    if (pCompactor->tableId.uid != pRowInfo->uid) {
      // if need to write the block data
      bool init = false;
      if (pCompactor->bData.suid == 0) {
        code = tsdbCompactWriteBlockData(pCompactor);
        TSDB_CHECK_CODE(code, lino, _exit);
        init = true;
      } else if (pCompactor->bData.uid && pCompactor->bData.nRow >= pCompactor->minRows) {
        code = tsdbCompactWriteBlockData(pCompactor);
        TSDB_CHECK_CODE(code, lino, _exit);
      }

      // write SDataBlk
      code = tsdbCompactWriteDataBlk(pCompactor);
      TSDB_CHECK_CODE(code, lino, _exit);

      // init block data if need
      if (init && (code = tBlockDataInit(&pCompactor->bData, (TABLEID *)pRowInfo, pTSchema, NULL, 0))) {
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
897 898 899

      pCompactor->tableId.suid = pRowInfo->suid;
      pCompactor->tableId.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
900 901
    }

H
Hongze Cheng 已提交
902 903
    // if append/merge the row causes nRow exceed maxRows
    if (tBlockDataTryUpsertRow(&pCompactor->bData, &pRowInfo->row, pRowInfo->uid) > pCompactor->maxRows) {
H
Hongze Cheng 已提交
904
      code = tsdbCompactWriteBlockData(pCompactor);
H
Hongze Cheng 已提交
905
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
906
    }
H
Hongze Cheng 已提交
907

H
Hongze Cheng 已提交
908
    // append/merge the row
H
Hongze Cheng 已提交
909 910
    code = tBlockDataUpsertRow(&pCompactor->bData, &pRowInfo->row, pTSchema, pRowInfo->uid);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
911 912

    // iter to the next row
H
Hongze Cheng 已提交
913 914
    code = tsdbCompactNextRow(pCompactor);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
915

H
Hongze Cheng 已提交
916 917 918
    code = tsdbCompactGetRow(pCompactor, &pRowInfo, &pTSchema);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
919

H
Hongze Cheng 已提交
920 921 922
  // handle remain data
  code = tsdbCompactWriteBlockData(pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
923

H
Hongze Cheng 已提交
924 925
  code = tsdbCompactWriteDataBlk(pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
926

H
Hongze Cheng 已提交
927 928
  code = tsdbWriteBlockIdx(pCompactor->pWriter, pCompactor->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
929

H
Hongze Cheng 已提交
930 931
  code = tsdbWriteSttBlk(pCompactor->pWriter, pCompactor->aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
932

H
Hongze Cheng 已提交
933 934
  code = tsdbUpdateDFileSetHeader(pCompactor->pWriter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
935

H
Hongze Cheng 已提交
936 937
  code = tsdbFSUpsertFSet(&pCompactor->fs, &pCompactor->pWriter->wSet);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
938

H
Hongze Cheng 已提交
939 940
  code = tsdbDataFWriterClose(&pCompactor->pWriter, 1);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
941

H
Hongze Cheng 已提交
942 943 944 945 946
_exit:
  // close compactor
  tsdbCloseCompactor(pCompactor);
  return code;
}
H
Hongze Cheng 已提交
947
#endif
H
Hongze Cheng 已提交
948

H
Hongze Cheng 已提交
949 950 951 952
int32_t tsdbCompact(STsdb *pTsdb, int32_t flag) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
953
#if 0 
H
Hongze Cheng 已提交
954
  STsdbCompactor *pCompactor = &(STsdbCompactor){0};
H
Hongze Cheng 已提交
955

H
Hongze Cheng 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968
  // begin compact
  code = tsdbBeginCompact(pTsdb, pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);

  // do compact each file set
  while (true) {
    pCompactor->pDFileSet = (SDFileSet *)taosArraySearch(pCompactor->fs.aDFileSet, &(SDFileSet){.fid = pCompactor->fid},
                                                         tDFileSetCmprFn, TD_GT);
    if (pCompactor->pDFileSet == NULL) break;

    pCompactor->fid = pCompactor->pDFileSet->fid;
    code = tsdbCompactFileSet(pCompactor);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
969 970
  }

H
Hongze Cheng 已提交
971 972 973
  code = tsdbFSUpsertDelFile(&pCompactor->fs, NULL);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
974
_exit:
H
Hongze Cheng 已提交
975
  // commit/abort compact
H
Hongze Cheng 已提交
976 977
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
978
    tsdbAbortCompact(pCompactor);
H
Hongze Cheng 已提交
979
  } else {
H
Hongze Cheng 已提交
980
    tsdbCommitCompact(pCompactor);
H
Hongze Cheng 已提交
981
  }
H
Hongze Cheng 已提交
982
  tsdbEndCompact(pCompactor);
H
Hongze Cheng 已提交
983
#endif
H
Hongze Cheng 已提交
984 985
  return code;
}