tsdbCommit.c 18.4 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 "tsdb.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
typedef struct SCommitter SCommitter;
H
more  
Hongze Cheng 已提交
19

H
Hongze Cheng 已提交
20
struct SCommitter {
H
Hongze Cheng 已提交
21 22 23
  STsdb   *pTsdb;
  uint8_t *pBuf1;
  uint8_t *pBuf2;
H
Hongze Cheng 已提交
24
  uint8_t *pBuf3;
H
Hongze Cheng 已提交
25 26
  uint8_t *pBuf4;
  uint8_t *pBuf5;
H
Hongze Cheng 已提交
27
  /* commit data */
H
Hongze Cheng 已提交
28 29
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
30 31
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
32 33 34 35 36 37 38
  TSKEY   nextCommitKey;
  // commit file data
  int32_t          commitFid;
  TSKEY            minKey;
  TSKEY            maxKey;
  SDFileSetReader *pReader;
  SDFileSetWriter *pWriter;
H
Hongze Cheng 已提交
39 40
  SMapData         oBlockIdx;
  SMapData         nBlockIdx;
H
Hongze Cheng 已提交
41
  // commit table data
H
Hongze Cheng 已提交
42 43
  STbDataIter   iter;
  STbDataIter  *pIter;
H
Hongze Cheng 已提交
44 45 46
  SBlockIdx    *pBlockIdx;
  SMapData      oBlock;
  SMapData      nBlock;
H
Hongze Cheng 已提交
47 48
  SColDataBlock oColDataBlock;
  SColDataBlock nColDataBlock;
H
Hongze Cheng 已提交
49
  /* commit del */
H
Hongze Cheng 已提交
50 51
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
52 53
  SDelIdx      delIdxOld;
  SDelIdx      delIdxNew;
H
Hongze Cheng 已提交
54 55
  STbData     *pTbData;
  SDelIdxItem *pDelIdxItem;
H
Hongze Cheng 已提交
56 57
  SDelData     delDataOld;
  SDelData     delDataNew;
H
Hongze Cheng 已提交
58
  SDelIdxItem  delIdxItem;
H
Hongze Cheng 已提交
59
  /* commit cache */
H
Hongze Cheng 已提交
60
};
H
refact  
Hongze Cheng 已提交
61

H
Hongze Cheng 已提交
62 63 64 65 66
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter);
static int32_t tsdbCommitData(SCommitter *pCommitter);
static int32_t tsdbCommitDel(SCommitter *pCommitter);
static int32_t tsdbCommitCache(SCommitter *pCommitter);
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno);
H
refact  
Hongze Cheng 已提交
67

H
refact  
Hongze Cheng 已提交
68
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
69
  int32_t code = 0;
H
Hongze Cheng 已提交
70

H
Hongze Cheng 已提交
71 72 73
  code = tsdbMemTableCreate(pTsdb, &pTsdb->mem);
  if (code) {
    goto _err;
H
Hongze Cheng 已提交
74 75
  }

H
Hongze Cheng 已提交
76 77 78 79
  return code;

_err:
  return code;
H
Hongze Cheng 已提交
80 81
}

H
more  
Hongze Cheng 已提交
82 83
int32_t tsdbCommit(STsdb *pTsdb) {
  int32_t    code = 0;
H
refact  
Hongze Cheng 已提交
84
  SCommitter commith = {0};
H
Hongze Cheng 已提交
85
  int        fid;
H
refact  
Hongze Cheng 已提交
86

H
more  
Hongze Cheng 已提交
87
  // start commit
H
more  
Hongze Cheng 已提交
88 89 90
  code = tsdbStartCommit(pTsdb, &commith);
  if (code) {
    goto _err;
H
refact  
Hongze Cheng 已提交
91 92
  }

H
refact  
Hongze Cheng 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  // commit impl
  code = tsdbCommitData(&commith);
  if (code) {
    goto _err;
  }

  code = tsdbCommitDel(&commith);
  if (code) {
    goto _err;
  }

  code = tsdbCommitCache(&commith);
  if (code) {
    goto _err;
  }

  // end commit
H
more  
Hongze Cheng 已提交
110 111 112 113
  code = tsdbEndCommit(&commith, 0);
  if (code) {
    goto _err;
  }
H
refact  
Hongze Cheng 已提交
114 115 116 117

  return code;

_err:
C
Cary Xu 已提交
118
  tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
refact  
Hongze Cheng 已提交
119 120 121
  return code;
}

H
Hongze Cheng 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
  int32_t code = 0;

  ASSERT(pTsdb->mem && pTsdb->imem == NULL);
  // lock();
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
  // unlock();

  pCommitter->pTsdb = pTsdb;

  return code;
}

static int32_t tsdbCommitDataStart(SCommitter *pCommitter);
static int32_t tsdbCommitDataImpl(SCommitter *pCommitter);
static int32_t tsdbCommitDataEnd(SCommitter *pCommitter);

static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

  // no data, just return
  if (pMemTable->nRow == 0) {
    goto _exit;
  }
H
Hongze Cheng 已提交
149

H
Hongze Cheng 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  // start
  code = tsdbCommitDataStart(pCommitter);
  if (code) {
    goto _err;
  }

  // commit
  code = tsdbCommitDataImpl(pCommitter);
  if (code) {
    goto _err;
  }

  // end
  code = tsdbCommitDataEnd(pCommitter);
  if (code) {
    goto _err;
  }

_exit:
H
Hongze Cheng 已提交
169
  tsdbDebug("vgId:%d commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
H
Hongze Cheng 已提交
170 171 172
  return code;

_err:
H
Hongze Cheng 已提交
173
  tsdbError("vgId:%d commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
174 175 176 177
  return code;
}

static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
178 179 180 181 182 183
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  SDelFile  *pDelFileR = NULL;  // TODO
  SDelFile  *pDelFileW = NULL;  // TODO

H
Hongze Cheng 已提交
184
  // load old
H
Hongze Cheng 已提交
185
  pCommitter->delIdxOld = (SDelIdx){0};
H
Hongze Cheng 已提交
186
  if (pDelFileR) {
H
Hongze Cheng 已提交
187
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb, NULL);
H
Hongze Cheng 已提交
188 189 190 191
    if (code) {
      goto _err;
    }

H
Hongze Cheng 已提交
192
    code = tsdbReadDelIdx(pCommitter->pDelFReader, &pCommitter->delIdxOld, &pCommitter->pBuf1);
H
Hongze Cheng 已提交
193 194 195 196 197
    if (code) {
      goto _err;
    }
  }

H
Hongze Cheng 已提交
198
  // prepare new
H
Hongze Cheng 已提交
199
  pCommitter->delIdxNew = (SDelIdx){0};
H
Hongze Cheng 已提交
200
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, pDelFileW, pTsdb);
H
Hongze Cheng 已提交
201 202 203 204 205 206 207 208 209 210
  if (code) {
    goto _err;
  }

_exit:
  tsdbDebug("vgId:%d commit del start", TD_VID(pTsdb->pVnode));
  return code;

_err:
  tsdbError("vgId:%d commit del start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
211 212 213
  return code;
}

H
Hongze Cheng 已提交
214 215
static int32_t tsdbCommitTableDel(SCommitter *pCommitter);

H
Hongze Cheng 已提交
216
static int32_t tsdbCommitDelImpl(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
  int32_t      code = 0;
  STsdb       *pTsdb = pCommitter->pTsdb;
  SMemTable   *pMemTable = pTsdb->imem;
  int32_t      c;
  int32_t      iTbData = 0;
  int32_t      nTbData = taosArrayGetSize(pMemTable->aTbData);
  int32_t      iDelIdxItem = 0;
  int32_t      nDelIdxItem = pCommitter->delIdxOld.offset.nOffset;
  STbData     *pTbData = NULL;
  SDelIdxItem *pDelIdxItem = NULL;
  SDelIdxItem  item;

  while (iTbData < nTbData || iDelIdxItem < nDelIdxItem) {
    pTbData = NULL;
    pDelIdxItem = NULL;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    }
    if (iDelIdxItem < nDelIdxItem) {
      tDelIdxGetItemByIdx(&pCommitter->delIdxOld, &item, iDelIdxItem);
      pDelIdxItem = &item;
    }

    if (pTbData && pDelIdxItem) {
      c = tTABLEIDCmprFn(pTbData, pDelIdxItem);
      if (c == 0) {
        iTbData++;
        iDelIdxItem++;
      } else if (c < 0) {
        iTbData++;
        pDelIdxItem = NULL;
      } else {
        iDelIdxItem++;
        pTbData = NULL;
      }
H
Hongze Cheng 已提交
252
    } else {
H
Hongze Cheng 已提交
253 254 255 256 257 258
      if (pTbData) {
        iTbData++;
      }
      if (pDelIdxItem) {
        iDelIdxItem++;
      }
H
Hongze Cheng 已提交
259 260
    }

H
Hongze Cheng 已提交
261 262 263 264 265 266 267 268 269
    if (pTbData && pTbData->pHead == NULL) {
      pTbData = NULL;
    }

    if (pTbData == NULL && pDelIdxItem == NULL) continue;

    // do merge
    pCommitter->pTbData = pTbData;
    pCommitter->pDelIdxItem = pDelIdxItem;
H
Hongze Cheng 已提交
270 271
    code = tsdbCommitTableDel(pCommitter);
    if (code) goto _err;
H
Hongze Cheng 已提交
272 273
  }

H
Hongze Cheng 已提交
274
  return code;
H
Hongze Cheng 已提交
275 276 277 278

_err:
  tsdbError("vgId:%d commit del impl failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
H
Hongze Cheng 已提交
279 280 281 282
}

static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
283

H
Hongze Cheng 已提交
284
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, &pCommitter->delIdxNew, NULL);
H
Hongze Cheng 已提交
285 286 287 288
  if (code) {
    goto _err;
  }

H
Hongze Cheng 已提交
289 290 291 292 293 294
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter, NULL);
  if (code) {
    goto _err;
  }

  code = tsdbDelFWriterClose(pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
295 296 297 298 299 300 301 302 303
  if (code) {
    goto _err;
  }

  if (pCommitter->pDelFReader) {
    code = tsdbDelFReaderClose(pCommitter->pDelFReader);
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
304
  tDelDataClear(&pCommitter->delDataNew);
H
more  
Hongze Cheng 已提交
305 306
  tDelIdxClear(&pCommitter->delIdxNew);

H
Hongze Cheng 已提交
307 308 309
  return code;

_err:
H
Hongze Cheng 已提交
310
  tsdbError("vgId:%d commit del end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
311 312 313 314 315 316 317 318
  return code;
}

static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
319
  if (pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    goto _exit;
  }

  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }

  // impl
  code = tsdbCommitDelImpl(pCommitter);
  if (code) {
    goto _err;
  }

  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }

_exit:
H
more  
Hongze Cheng 已提交
342
  tsdbDebug("vgId:%d commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
343 344 345
  return code;

_err:
H
Hongze Cheng 已提交
346
  tsdbError("vgId:%d commit del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  return code;
}

static int32_t tsdbCommitCache(SCommitter *pCommitter) {
  int32_t code = 0;
  // TODO
  return code;
}

static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
  int32_t code = 0;
  // TODO
  return code;
}

static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

  pCommitter->nextCommitKey = pMemTable->minKey.ts;

  return code;
}

static int32_t tsdbCommitFileData(SCommitter *pCommitter);

static int32_t tsdbCommitDataImpl(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
377 378
  while (pCommitter->nextCommitKey < TSKEY_MAX) {
    pCommitter->commitFid = tsdbKeyFid(pCommitter->nextCommitKey, pCommitter->minutes, pCommitter->precision);
H
Hongze Cheng 已提交
379
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
380
    if (code) goto _err;
H
Hongze Cheng 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  }

_exit:
  return code;

_err:
  return code;
}

static int32_t tsdbCommitDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;
  // TODO
  return code;
}

static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter);
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter);
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter);

static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
  int32_t code = 0;

  // commit file data start
  code = tsdbCommitFileDataStart(pCommitter);
  if (code) {
    goto _err;
  }

  // commit file data impl
  code = tsdbCommitFileDataImpl(pCommitter);
  if (code) {
    goto _err;
  }

  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
  if (code) {
    goto _err;
  }

  return code;

_err:
  return code;
}

static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
430 431
  SDFileSet *pRSet = NULL;  // TODO
  SDFileSet *pWSet = NULL;  // TODO
H
Hongze Cheng 已提交
432

H
Hongze Cheng 已提交
433 434 435 436
  // load old
  pCommitter->oBlockIdx.nItem = 0;
  pCommitter->oBlockIdx.flag = 0;
  pCommitter->oBlockIdx.nData = 0;
H
Hongze Cheng 已提交
437
  if (pRSet) {
H
Hongze Cheng 已提交
438
    code = tsdbDFileSetReaderOpen(&pCommitter->pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
439 440
    if (code) goto _err;

H
Hongze Cheng 已提交
441 442
    code = tsdbReadBlockIdx(pCommitter->pReader, &pCommitter->oBlockIdx);
    if (code) goto _err;
H
Hongze Cheng 已提交
443 444
  }

H
Hongze Cheng 已提交
445 446 447 448 449
  // create new
  pCommitter->nBlockIdx.nItem = 0;
  pCommitter->nBlockIdx.flag = 0;
  pCommitter->nBlockIdx.nData = 0;
  code = tsdbDFileSetWriterOpen(&pCommitter->pWriter, pTsdb, pWSet);
H
Hongze Cheng 已提交
450 451 452 453 454 455
  if (code) goto _err;

_exit:
  return code;

_err:
H
Hongze Cheng 已提交
456
  tsdbError("vgId:%d commit file data start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
457 458 459 460 461 462 463 464 465 466 467
  return code;
}

static int32_t tsdbCommitTableData(SCommitter *pCommitter);

static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  int32_t    iTbData = 0;
  int32_t    nTbData = taosArrayGetSize(pMemTable->aTbData);
H
Hongze Cheng 已提交
468 469
  int32_t    iBlockIdx = 0;
  int32_t    nBlockIdx = pCommitter->oBlockIdx.nItem;
H
Hongze Cheng 已提交
470
  STbData   *pTbData;
H
Hongze Cheng 已提交
471 472 473
  SBlockIdx *pBlockIdx;
  SBlockIdx  blockIdx;
  int32_t    c;
H
Hongze Cheng 已提交
474 475 476 477 478 479 480 481

  while (iTbData < nTbData || iBlockIdx < nBlockIdx) {
    pTbData = NULL;
    pBlockIdx = NULL;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    }
    if (iBlockIdx < nBlockIdx) {
H
Hongze Cheng 已提交
482 483
      tMapDataGetItemByIdx(&pCommitter->oBlockIdx, iBlockIdx, &blockIdx, NULL /* TODO */);
      pBlockIdx = &blockIdx;
H
Hongze Cheng 已提交
484 485 486
    }

    if (pTbData && pBlockIdx) {
H
Hongze Cheng 已提交
487 488
      c = tTABLEIDCmprFn(pTbData, pBlockIdx);

H
Hongze Cheng 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502
      if (c == 0) {
        iTbData++;
        iBlockIdx++;
      } else if (c < 0) {
        iTbData++;
        pBlockIdx = NULL;
      } else {
        iBlockIdx++;
        pTbData = NULL;
      }
    } else {
      if (pTbData) {
        iBlockIdx++;
      }
H
Hongze Cheng 已提交
503 504 505 506 507
      if (pBlockIdx) {
        iTbData++;
      }
    }

H
Hongze Cheng 已提交
508 509
    if (pTbData && !tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = 0}), 0,
        &pCommitter->iter) {
H
Hongze Cheng 已提交
510
      pTbData = NULL;
H
Hongze Cheng 已提交
511 512
    }

H
Hongze Cheng 已提交
513 514
    if (pTbData == NULL && pBlockIdx == NULL) continue;

H
Hongze Cheng 已提交
515 516
    pCommitter->pTbData = pTbData;
    pCommitter->pBlockIdx = pBlockIdx;
H
Hongze Cheng 已提交
517

H
Hongze Cheng 已提交
518
    code = tsdbCommitTableData(pCommitter);
H
Hongze Cheng 已提交
519
    if (code) goto _err;
H
Hongze Cheng 已提交
520 521 522 523 524 525 526 527 528 529
  }

  return code;

_err:
  return code;
}

static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548

  code = tsdbWriteBlockIdx(pCommitter->pWriter, pCommitter->nBlockIdx, NULL);
  if (code) goto _err;

  code = tsdbUpdateDFileSetHeader(pCommitter->pWriter, NULL);
  if (code) goto _err;

  code = tsdbDFileSetWriterClose(pCommitter->pWriter, 1);
  if (code) goto _err;

  if (pCommitter->pReader) {
    code = tsdbDFileSetReaderClose(pCommitter->pReader);
    goto _err;
  }

_exit:
  return code;

_err:
H
Hongze Cheng 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  return code;
}

static int32_t tsdbCommitTableDataStart(SCommitter *pCommitter);
static int32_t tsdbCommitTableDataImpl(SCommitter *pCommitter);
static int32_t tsdbCommitTableDataEnd(SCommitter *pCommitter);

static int32_t tsdbCommitTableData(SCommitter *pCommitter) {
  int32_t code = 0;

  // start
  code = tsdbCommitTableDataStart(pCommitter);
  if (code) {
    goto _err;
  }

  // impl
  code = tsdbCommitTableDataImpl(pCommitter);
  if (code) {
    goto _err;
  }

  // end
  code = tsdbCommitTableDataEnd(pCommitter);
  if (code) {
    goto _err;
  }

_exit:
  return code;

_err:
  return code;
}

static int32_t tsdbCommitTableDataStart(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
586 587

  // old
H
Hongze Cheng 已提交
588
  tMapDataReset(&pCommitter->oBlock);
H
Hongze Cheng 已提交
589 590 591 592 593 594
  if (pCommitter->pBlockIdx) {
    code = tsdbReadBlock(pCommitter->pReader, &pCommitter->oBlock, NULL);
    if (code) goto _err;
  }

  // new
H
Hongze Cheng 已提交
595
  tMapDataReset(&pCommitter->nBlock);
H
Hongze Cheng 已提交
596 597

_err:
H
Hongze Cheng 已提交
598 599 600 601
  return code;
}

static int32_t tsdbCommitTableDataImpl(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
602
  int32_t      code = 0;
H
Hongze Cheng 已提交
603
  STsdb       *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
604 605 606 607 608 609 610
  STbDataIter *pIter = NULL;
  int32_t      iBlock = 0;
  int32_t      nBlock = pCommitter->nBlock.nItem;
  SBlock      *pBlock;
  SBlock       block;
  TSDBROW     *pRow;
  TSDBROW      row;
H
Hongze Cheng 已提交
611 612
  int32_t      iRow = 0;
  STSchema    *pTSchema = NULL;
H
Hongze Cheng 已提交
613 614

  if (pCommitter->pTbData) {
H
Hongze Cheng 已提交
615
    code = tsdbTbDataIterCreate(pCommitter->pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = 0}, 0, &pIter);
H
Hongze Cheng 已提交
616 617 618
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
619 620 621 622 623
  if (iBlock < nBlock) {
    pBlock = &block;
  } else {
    pBlock = NULL;
  }
H
Hongze Cheng 已提交
624

H
Hongze Cheng 已提交
625
  tsdbTbDataIterGet(pIter, pRow);
H
Hongze Cheng 已提交
626

H
Hongze Cheng 已提交
627 628 629 630 631 632 633 634
  // loop to merge memory data and disk data
  for (; pBlock == NULL || (pRow && pRow->pTSRow->ts <= pCommitter->maxKey);) {
    if (pRow == NULL || pRow->pTSRow->ts > pCommitter->maxKey) {
      // only has block data, then move to new index file
    } else if (0) {
      // only commit memory data
    } else {
      // merge memory and block data
H
Hongze Cheng 已提交
635
    }
H
Hongze Cheng 已提交
636 637
  }

H
Hongze Cheng 已提交
638
  tsdbTbDataIterDestroy(pIter);
H
Hongze Cheng 已提交
639 640 641
  return code;

_err:
H
Hongze Cheng 已提交
642 643
  tsdbError("vgId:%d commit table data impl failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  tsdbTbDataIterDestroy(pIter);
H
Hongze Cheng 已提交
644 645 646 647 648 649 650 651 652
  return code;
}

static int32_t tsdbCommitTableDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
653
static int32_t tsdbCommitTableDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
654 655 656 657 658 659 660 661
  int32_t  code = 0;
  tb_uid_t suid;
  tb_uid_t uid;

  if (pCommitter->pTbData) {
    suid = pCommitter->pTbData->suid;
    uid = pCommitter->pTbData->uid;
  }
H
Hongze Cheng 已提交
662 663

  // load old
H
Hongze Cheng 已提交
664
  pCommitter->delDataOld = (SDelData){0};
H
Hongze Cheng 已提交
665 666 667 668 669
  if (pCommitter->pDelIdxItem) {
    suid = pCommitter->pDelIdxItem->suid;
    uid = pCommitter->pDelIdxItem->uid;
    code =
        tsdbReadDelData(pCommitter->pDelFReader, pCommitter->pDelIdxItem, &pCommitter->delDataOld, &pCommitter->pBuf5);
H
Hongze Cheng 已提交
670 671 672 673
    if (code) goto _err;
  }

  // prepare new
H
Hongze Cheng 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
  pCommitter->delDataNew.suid = suid;
  pCommitter->delDataNew.uid = uid;
  pCommitter->delDataNew.offset.flag = 0;
  pCommitter->delDataNew.offset.nOffset = 0;
  pCommitter->delDataNew.nData = 0;
  pCommitter->delIdxItem = (SDelIdxItem){
      .suid = suid,
      .uid = uid,
      .minKey = TSKEY_MAX,
      .maxKey = TSKEY_MIN,
      .minVersion = INT64_MAX,
      .maxVersion = INT64_MIN,
      .offset = -1,
      .size = -1,
  };
H
Hongze Cheng 已提交
689 690 691 692 693 694 695 696 697

  return code;

_err:
  tsdbError("vgId:%d commit table del start failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
  return code;
}

static int32_t tsdbCommitTableDelImpl(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
  int32_t      code = 0;
  SDelDataItem item;

  // old
  if (pCommitter->pDelIdxItem) {
    for (int32_t iDelIdxItem = 0; iDelIdxItem < pCommitter->delDataOld.offset.nOffset; iDelIdxItem++) {
      code = tDelDataGetItemByIdx(&pCommitter->delDataOld, &item, iDelIdxItem);
      if (code) goto _err;

      code = tDelDataPutItem(&pCommitter->delDataNew, &item);
      if (code) goto _err;

      // update index
      if (item.version < pCommitter->delIdxItem.minVersion) pCommitter->delIdxItem.minVersion = item.version;
      if (item.version > pCommitter->delIdxItem.maxVersion) pCommitter->delIdxItem.maxVersion = item.version;
      if (item.sKey < pCommitter->delIdxItem.minKey) pCommitter->delIdxItem.minKey = item.sKey;
      if (item.eKey > pCommitter->delIdxItem.maxKey) pCommitter->delIdxItem.maxKey = item.eKey;
    }
  }
H
Hongze Cheng 已提交
717

H
Hongze Cheng 已提交
718 719 720 721
  // new
  if (pCommitter->pTbData) {
    for (SDelOp *pDelOp = pCommitter->pTbData->pHead; pDelOp; pDelOp = pDelOp->pNext) {
      item = (SDelDataItem){.version = pDelOp->version, .sKey = pDelOp->sKey, .eKey = pDelOp->eKey};
H
Hongze Cheng 已提交
722

H
Hongze Cheng 已提交
723 724 725 726 727 728 729 730 731
      code = tDelDataPutItem(&pCommitter->delDataNew, &item);
      if (code) goto _err;

      // update index
      if (item.version < pCommitter->delIdxItem.minVersion) pCommitter->delIdxItem.minVersion = item.version;
      if (item.version > pCommitter->delIdxItem.maxVersion) pCommitter->delIdxItem.maxVersion = item.version;
      if (item.sKey < pCommitter->delIdxItem.minKey) pCommitter->delIdxItem.minKey = item.sKey;
      if (item.eKey > pCommitter->delIdxItem.maxKey) pCommitter->delIdxItem.maxKey = item.eKey;
    }
H
Hongze Cheng 已提交
732 733 734 735 736 737 738 739 740 741 742 743
  }

  return code;

_err:
  return code;
}

static int32_t tsdbCommitTableDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;

  // write table del data
H
Hongze Cheng 已提交
744 745
  code = tsdbWriteDelData(pCommitter->pDelFWriter, &pCommitter->delDataNew, NULL, &pCommitter->delIdxItem.offset,
                          &pCommitter->delIdxItem.size);
H
Hongze Cheng 已提交
746 747 748
  if (code) goto _err;

  // add SDelIdxItem
H
Hongze Cheng 已提交
749
  code = tDelIdxPutItem(&pCommitter->delIdxNew, &pCommitter->delIdxItem);
H
Hongze Cheng 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
  if (code) goto _err;

  return code;

_err:
  tsdbError("vgId:%d commit table del end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
  return code;
}

static int32_t tsdbCommitTableDel(SCommitter *pCommitter) {
  int32_t code = 0;

  // start
  code = tsdbCommitTableDelStart(pCommitter);
  if (code) goto _err;

  // impl
  code = tsdbCommitTableDelImpl(pCommitter);
  if (code) goto _err;

  // end
  code = tsdbCommitTableDelEnd(pCommitter);
  if (code) goto _err;

  return code;

_err:
  return code;
H
Hongze Cheng 已提交
778
}