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 44
  SBlockIdx    *pBlockIdx;
  SMapData      oBlock;
  SMapData      nBlock;
H
Hongze Cheng 已提交
45 46
  SColDataBlock oColDataBlock;
  SColDataBlock nColDataBlock;
H
Hongze Cheng 已提交
47
  /* commit del */
H
Hongze Cheng 已提交
48 49
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
50 51
  SDelIdx      delIdxOld;
  SDelIdx      delIdxNew;
H
Hongze Cheng 已提交
52 53
  STbData     *pTbData;
  SDelIdxItem *pDelIdxItem;
H
Hongze Cheng 已提交
54 55
  SDelData     delDataOld;
  SDelData     delDataNew;
H
Hongze Cheng 已提交
56
  SDelIdxItem  delIdxItem;
H
Hongze Cheng 已提交
57
  /* commit cache */
H
Hongze Cheng 已提交
58
};
H
refact  
Hongze Cheng 已提交
59

H
Hongze Cheng 已提交
60 61 62 63 64
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 已提交
65

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

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

H
Hongze Cheng 已提交
74 75 76 77
  return code;

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

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

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

H
refact  
Hongze Cheng 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  // 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 已提交
108 109 110 111
  code = tsdbEndCommit(&commith, 0);
  if (code) {
    goto _err;
  }
H
refact  
Hongze Cheng 已提交
112 113 114 115

  return code;

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

H
Hongze Cheng 已提交
120 121 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
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 已提交
147

H
Hongze Cheng 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  // 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 已提交
167
  tsdbDebug("vgId:%d commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
H
Hongze Cheng 已提交
168 169 170
  return code;

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

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

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

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

H
Hongze Cheng 已提交
196
  // prepare new
H
Hongze Cheng 已提交
197
  pCommitter->delIdxNew = (SDelIdx){0};
H
Hongze Cheng 已提交
198
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, pDelFileW, pTsdb);
H
Hongze Cheng 已提交
199 200 201 202 203 204 205 206 207 208
  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 已提交
209 210 211
  return code;
}

H
Hongze Cheng 已提交
212 213
static int32_t tsdbCommitTableDel(SCommitter *pCommitter);

H
Hongze Cheng 已提交
214
static int32_t tsdbCommitDelImpl(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
215 216 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
  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 已提交
250
    } else {
H
Hongze Cheng 已提交
251 252 253 254 255 256
      if (pTbData) {
        iTbData++;
      }
      if (pDelIdxItem) {
        iDelIdxItem++;
      }
H
Hongze Cheng 已提交
257 258
    }

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

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

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

H
Hongze Cheng 已提交
272
  return code;
H
Hongze Cheng 已提交
273 274 275 276

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
305 306 307
  return code;

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

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

H
Hongze Cheng 已提交
317
  if (pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    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 已提交
340
  tsdbDebug("vgId:%d commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
341 342 343
  return code;

_err:
H
Hongze Cheng 已提交
344
  tsdbError("vgId:%d commit del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
345 346 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
  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 已提交
375 376
  while (pCommitter->nextCommitKey < TSKEY_MAX) {
    pCommitter->commitFid = tsdbKeyFid(pCommitter->nextCommitKey, pCommitter->minutes, pCommitter->precision);
H
Hongze Cheng 已提交
377
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
378
    if (code) goto _err;
H
Hongze Cheng 已提交
379 380 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
  }

_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 已提交
428 429
  SDFileSet *pRSet = NULL;  // TODO
  SDFileSet *pWSet = NULL;  // TODO
H
Hongze Cheng 已提交
430

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

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

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

_exit:
  return code;

_err:
H
Hongze Cheng 已提交
454
  tsdbError("vgId:%d commit file data start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
455 456 457 458 459 460 461 462 463 464 465
  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 已提交
466 467
  int32_t    iBlockIdx = 0;
  int32_t    nBlockIdx = pCommitter->oBlockIdx.nItem;
H
Hongze Cheng 已提交
468
  STbData   *pTbData;
H
Hongze Cheng 已提交
469 470 471
  SBlockIdx *pBlockIdx;
  SBlockIdx  blockIdx;
  int32_t    c;
H
Hongze Cheng 已提交
472 473 474 475 476 477 478 479

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

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

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

    if (pTbData && pTbData->sl.size == 0) {
      pTbData = NULL;
H
Hongze Cheng 已提交
508 509
    }

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

H
Hongze Cheng 已提交
512 513
    pCommitter->pTbData = pTbData;
    pCommitter->pBlockIdx = pBlockIdx;
H
Hongze Cheng 已提交
514

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

  return code;

_err:
  return code;
}

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

  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 已提交
546 547 548 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
  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 已提交
583 584

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

  // new
H
Hongze Cheng 已提交
592
  tMapDataReset(&pCommitter->nBlock);
H
Hongze Cheng 已提交
593 594

_err:
H
Hongze Cheng 已提交
595 596 597 598
  return code;
}

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

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

H
Hongze Cheng 已提交
616
  // merge loop
H
Hongze Cheng 已提交
617
  for (;;) {
H
Hongze Cheng 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
    tsdbTbDataIterGet(pIter, pRow);

    code = tsdbColDataBlockAppend(&pCommitter->nColDataBlock, pRow, pTSchema);
    if (code) goto _err;

    if (pCommitter->nColDataBlock.nRow >= pCommitter->maxRow) {
      code = tsdbWriteColDataBlock(pCommitter->pWriter, &pCommitter->nColDataBlock, NULL);
      if (code) goto _err;

      tsdbColDataBlockReset(&pCommitter->nColDataBlock);
    }

    if (!tsdbTbDataIterNext(pIter)) break;
  }

  if (pCommitter->nColDataBlock.nRow) {
    code = tsdbWriteColDataBlock(pCommitter->pWriter, &pCommitter->nColDataBlock, NULL);
    if (code) goto _err;
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
}