tsdbCommit.c 42.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 {
H
Hongze Cheng 已提交
19
  STsdb *pTsdb;
H
Hongze Cheng 已提交
20
  /* commit data */
H
Hongze Cheng 已提交
21
  int64_t commitID;
H
Hongze Cheng 已提交
22 23
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
24 25
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
26
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
27
  // --------------
H
Hongze Cheng 已提交
28
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
29 30 31
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
32
  // commit file data
H
Hongze Cheng 已提交
33
  SDataFReader *pReader;
H
Hongze Cheng 已提交
34 35 36 37
  SMapData      oBlockIdxMap;  // SMapData<SBlockIdx>, read from reader
  SMapData      oBlockMap;     // SMapData<SBlock>, read from reader
  SBlock        oBlock;
  SBlockData    oBlockData;
H
Hongze Cheng 已提交
38
  SDataFWriter *pWriter;
H
Hongze Cheng 已提交
39 40 41 42
  SMapData      nBlockIdxMap;  // SMapData<SBlockIdx>, build by committer
  SMapData      nBlockMap;     // SMapData<SBlock>
  SBlock        nBlock;
  SBlockData    nBlockData;
H
Hongze Cheng 已提交
43 44 45
  int64_t       suid;
  int64_t       uid;
  STSchema     *pTSchema;
H
Hongze Cheng 已提交
46
  /* commit del */
H
Hongze Cheng 已提交
47
  SDelFReader *pDelFReader;
H
Hongze Cheng 已提交
48 49
  SMapData     oDelIdxMap;   // SMapData<SDelIdx>, old
  SMapData     oDelDataMap;  // SMapData<SDelData>, old
H
Hongze Cheng 已提交
50
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
51 52
  SMapData     nDelIdxMap;   // SMapData<SDelIdx>, new
  SMapData     nDelDataMap;  // SMapData<SDelData>, new
H
Hongze Cheng 已提交
53
} SCommitter;
H
refact  
Hongze Cheng 已提交
54

H
Hongze Cheng 已提交
55 56 57 58 59
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 已提交
60

H
refact  
Hongze Cheng 已提交
61
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
62
  int32_t code = 0;
H
Hongze Cheng 已提交
63

64 65
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
66
  code = tsdbMemTableCreate(pTsdb, &pTsdb->mem);
H
Hongze Cheng 已提交
67
  if (code) goto _err;
H
Hongze Cheng 已提交
68

H
Hongze Cheng 已提交
69 70 71
  return code;

_err:
H
Hongze Cheng 已提交
72
  tsdbError("vgId:%d tsdb begin failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
73
  return code;
H
Hongze Cheng 已提交
74 75
}

H
more  
Hongze Cheng 已提交
76
int32_t tsdbCommit(STsdb *pTsdb) {
77
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
78

H
more  
Hongze Cheng 已提交
79
  int32_t    code = 0;
H
Hongze Cheng 已提交
80 81 82 83
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
84 85
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
    // TODO: lock?
H
Hongze Cheng 已提交
86 87 88 89
    pTsdb->mem = NULL;
    tsdbMemTableDestroy(pMemTable);
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
90

H
more  
Hongze Cheng 已提交
91
  // start commit
H
more  
Hongze Cheng 已提交
92
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
93
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
94

H
refact  
Hongze Cheng 已提交
95 96
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
97
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
98 99

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
100
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
101 102

  code = tsdbCommitCache(&commith);
H
Hongze Cheng 已提交
103
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
104 105

  // end commit
H
more  
Hongze Cheng 已提交
106
  code = tsdbEndCommit(&commith, 0);
H
Hongze Cheng 已提交
107
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
108

H
Hongze Cheng 已提交
109
_exit:
H
refact  
Hongze Cheng 已提交
110 111 112
  return code;

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

H
Hongze Cheng 已提交
118
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
119 120 121 122 123 124
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  SDelFile  *pDelFileR = NULL;  // TODO
  SDelFile  *pDelFileW = NULL;  // TODO

H
Hongze Cheng 已提交
125 126 127
  tMapDataReset(&pCommitter->oDelIdxMap);
  tMapDataReset(&pCommitter->nDelIdxMap);

H
Hongze Cheng 已提交
128
  // load old
H
Hongze Cheng 已提交
129
  if (pDelFileR) {
H
Hongze Cheng 已提交
130
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb, NULL);
H
Hongze Cheng 已提交
131
    if (code) goto _err;
H
Hongze Cheng 已提交
132

H
Hongze Cheng 已提交
133 134
    code = tsdbReadDelIdx(pCommitter->pDelFReader, &pCommitter->oDelIdxMap, NULL);
    if (code) goto _err;
H
Hongze Cheng 已提交
135 136
  }

H
Hongze Cheng 已提交
137
  // prepare new
H
Hongze Cheng 已提交
138
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, pDelFileW, pTsdb);
H
Hongze Cheng 已提交
139
  if (code) goto _err;
H
Hongze Cheng 已提交
140 141 142 143 144 145 146

_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 已提交
147 148 149
  return code;
}

H
Hongze Cheng 已提交
150
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
151
  int32_t   code = 0;
H
Hongze Cheng 已提交
152
  SDelData *pDelData = &(SDelData){};
H
Hongze Cheng 已提交
153 154 155
  tb_uid_t  suid;
  tb_uid_t  uid;
  SDelIdx   delIdx;  // TODO
H
Hongze Cheng 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

  // check no del data, just return
  if (pTbData && pTbData->pHead == NULL) {
    pTbData = NULL;
  }
  if (pTbData == NULL && pDelIdx == NULL) goto _exit;

  // prepare
  if (pTbData) {
    delIdx.suid = pTbData->suid;
    delIdx.uid = pTbData->uid;
  } else {
    delIdx.suid = pDelIdx->suid;
    delIdx.uid = pDelIdx->uid;
  }
  delIdx.minKey = TSKEY_MAX;
  delIdx.maxKey = TSKEY_MIN;
  delIdx.minVersion = INT64_MAX;
H
Hongze Cheng 已提交
174
  delIdx.maxVersion = INT64_MIN;
H
Hongze Cheng 已提交
175 176 177 178 179 180 181 182 183 184 185 186

  // start
  tMapDataReset(&pCommitter->oDelDataMap);
  tMapDataReset(&pCommitter->nDelDataMap);

  if (pDelIdx) {
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, &pCommitter->oDelDataMap, NULL);
    if (code) goto _err;
  }

  // disk
  for (int32_t iDelData = 0; iDelData < pCommitter->oDelDataMap.nItem; iDelData++) {
H
Hongze Cheng 已提交
187
    code = tMapDataGetItemByIdx(&pCommitter->oDelDataMap, iDelData, pDelData, tGetDelData);
H
Hongze Cheng 已提交
188 189
    if (code) goto _err;

H
Hongze Cheng 已提交
190
    code = tMapDataPutItem(&pCommitter->nDelDataMap, pDelData, tPutDelData);
H
Hongze Cheng 已提交
191 192
    if (code) goto _err;

H
Hongze Cheng 已提交
193 194 195 196
    if (delIdx.minKey > pDelData->sKey) delIdx.minKey = pDelData->sKey;
    if (delIdx.maxKey < pDelData->eKey) delIdx.maxKey = pDelData->eKey;
    if (delIdx.minVersion > pDelData->version) delIdx.minVersion = pDelData->version;
    if (delIdx.maxVersion < pDelData->version) delIdx.maxVersion = pDelData->version;
H
Hongze Cheng 已提交
197 198 199
  }

  // memory
H
Hongze Cheng 已提交
200 201 202
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
    code = tMapDataPutItem(&pCommitter->nDelDataMap, pDelData, tPutDelData);
H
Hongze Cheng 已提交
203 204
    if (code) goto _err;

H
Hongze Cheng 已提交
205 206 207 208
    if (delIdx.minKey > pDelData->sKey) delIdx.minKey = pDelData->sKey;
    if (delIdx.maxKey < pDelData->eKey) delIdx.maxKey = pDelData->eKey;
    if (delIdx.minVersion > pDelData->version) delIdx.minVersion = pDelData->version;
    if (delIdx.maxVersion < pDelData->version) delIdx.maxVersion = pDelData->version;
H
Hongze Cheng 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  }

  ASSERT(pCommitter->nDelDataMap.nItem > 0);

  // write
  code = tsdbWriteDelData(pCommitter->pDelFWriter, &pCommitter->nDelDataMap, NULL, &delIdx);
  if (code) goto _err;

  // put delIdx
  code = tMapDataPutItem(&pCommitter->nDelIdxMap, &delIdx, tPutDelIdx);
  if (code) goto _err;

_exit:
  return code;

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

H
Hongze Cheng 已提交
229
static int32_t tsdbCommitDelImpl(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  int32_t    iDelIdx = 0;
  int32_t    nDelIdx = pCommitter->oDelIdxMap.nItem;
  int32_t    iTbData = 0;
  int32_t    nTbData = taosArrayGetSize(pMemTable->aTbData);
  STbData   *pTbData;
  SDelIdx   *pDelIdx;
  SDelIdx    delIdx;
  int32_t    c;

  ASSERT(nTbData > 0);
H
Hongze Cheng 已提交
243

H
Hongze Cheng 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257
  pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
  if (iDelIdx < nDelIdx) {
    code = tMapDataGetItemByIdx(&pCommitter->oDelIdxMap, iDelIdx, &delIdx, tGetDelIdx);
    if (code) goto _err;
    pDelIdx = &delIdx;
  } else {
    pDelIdx = NULL;
  }

  while (true) {
    if (pTbData == NULL && pDelIdx == NULL) break;

    if (pTbData && pDelIdx) {
      c = tTABLEIDCmprFn(pTbData, pDelIdx);
H
Hongze Cheng 已提交
258
      if (c == 0) {
H
Hongze Cheng 已提交
259
        goto _commit_mem_and_disk_del;
H
Hongze Cheng 已提交
260
      } else if (c < 0) {
H
Hongze Cheng 已提交
261
        goto _commit_mem_del;
H
Hongze Cheng 已提交
262
      } else {
H
Hongze Cheng 已提交
263
        goto _commit_disk_del;
H
Hongze Cheng 已提交
264
      }
H
Hongze Cheng 已提交
265
    } else {
H
Hongze Cheng 已提交
266 267
      if (pTbData) goto _commit_mem_del;
      if (pDelIdx) goto _commit_disk_del;
H
Hongze Cheng 已提交
268 269
    }

H
Hongze Cheng 已提交
270 271 272 273 274 275 276
  _commit_mem_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, NULL);
    if (code) goto _err;
    iTbData++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    } else {
H
Hongze Cheng 已提交
277 278
      pTbData = NULL;
    }
H
Hongze Cheng 已提交
279
    continue;
H
Hongze Cheng 已提交
280

H
Hongze Cheng 已提交
281 282 283 284 285 286 287 288 289 290 291 292
  _commit_disk_del:
    code = tsdbCommitTableDel(pCommitter, NULL, pDelIdx);
    if (code) goto _err;
    iDelIdx++;
    if (iDelIdx < nDelIdx) {
      code = tMapDataGetItemByIdx(&pCommitter->oDelIdxMap, iDelIdx, &delIdx, tGetDelIdx);
      if (code) goto _err;
      pDelIdx = &delIdx;
    } else {
      pDelIdx = NULL;
    }
    continue;
H
Hongze Cheng 已提交
293

H
Hongze Cheng 已提交
294 295
  _commit_mem_and_disk_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, pDelIdx);
H
Hongze Cheng 已提交
296
    if (code) goto _err;
H
Hongze Cheng 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    iTbData++;
    iDelIdx++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    } else {
      pTbData = NULL;
    }
    if (iDelIdx < nDelIdx) {
      code = tMapDataGetItemByIdx(&pCommitter->oDelIdxMap, iDelIdx, &delIdx, tGetDelIdx);
      if (code) goto _err;
      pDelIdx = &delIdx;
    } else {
      pDelIdx = NULL;
    }
    continue;
H
Hongze Cheng 已提交
312 313
  }

H
Hongze Cheng 已提交
314
  return code;
H
Hongze Cheng 已提交
315 316 317 318

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

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

H
Hongze Cheng 已提交
324 325
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, &pCommitter->nDelIdxMap, NULL);
  if (code) goto _err;
H
Hongze Cheng 已提交
326

H
Hongze Cheng 已提交
327
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter, NULL);
H
Hongze Cheng 已提交
328
  if (code) goto _err;
H
Hongze Cheng 已提交
329 330

  code = tsdbDelFWriterClose(pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
331
  if (code) goto _err;
H
Hongze Cheng 已提交
332 333 334 335 336 337

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

H
Hongze Cheng 已提交
338 339 340
  return code;

_err:
H
Hongze Cheng 已提交
341
  tsdbError("vgId:%d commit del end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
342 343 344
  return 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 375 376
// static int32_t tsdbGetOverlapRowNumber(STbDataIter *pIter, SBlock *pBlock) {
//   int32_t     nRow = 0;
//   TSDBROW    *pRow;
//   TSDBKEY     key;
//   int32_t     c = 0;
//   STbDataIter iter = *pIter;

//   iter.pRow = NULL;
//   while (true) {
//     pRow = tsdbTbDataIterGet(pIter);

//     if (pRow == NULL) break;
//     key = tsdbRowKey(pRow);

//     c = tBlockCmprFn(&(SBlock){.info.maxKey = key, .info.minKey = key}, pBlock);
//     if (c == 0) {
//       nRow++;
//     } else if (c > 0) {
//       break;
//     } else {
//       ASSERT(0);
//     }
//   }

//   return nRow;
// }

static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
  SDFileSet  wSet;
H
Hongze Cheng 已提交
377

H
Hongze Cheng 已提交
378 379
  // memory
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
380

H
Hongze Cheng 已提交
381 382 383 384 385 386 387 388
  // old
  tMapDataReset(&pCommitter->oBlockIdxMap);
  tMapDataReset(&pCommitter->oBlockMap);
  tBlockReset(&pCommitter->oBlock);
  tBlockDataReset(&pCommitter->oBlockData);
  pRSet = tsdbFSStateGetDFileSet(pTsdb->fs->nState, pCommitter->commitFid);
  if (pRSet) {
    code = tsdbDataFReaderOpen(&pCommitter->pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
389 390
    if (code) goto _err;

H
Hongze Cheng 已提交
391
    code = tsdbReadBlockIdx(pCommitter->pReader, &pCommitter->oBlockIdxMap, NULL);
H
Hongze Cheng 已提交
392
    if (code) goto _err;
H
Hongze Cheng 已提交
393
  }
H
Hongze Cheng 已提交
394

H
Hongze Cheng 已提交
395 396 397 398
  // new
  tMapDataReset(&pCommitter->nBlockIdxMap);
  tMapDataReset(&pCommitter->nBlockMap);
  tBlockReset(&pCommitter->nBlock);
H
Hongze Cheng 已提交
399
  tBlockDataReset(&pCommitter->nBlockData);
H
Hongze Cheng 已提交
400 401 402 403 404 405 406 407 408 409
  if (pRSet) {
    wSet = (SDFileSet){.diskId = pRSet->diskId,
                       .fid = pCommitter->commitFid,
                       .fHead = {.commitID = pCommitter->commitID, .offset = 0, .size = 0},
                       .fData = pRSet->fData,
                       .fLast = {.commitID = pCommitter->commitID, .size = 0},
                       .fSma = pRSet->fSma};
  } else {
    STfs   *pTfs = pTsdb->pVnode->pTfs;
    SDiskID did = {.level = 0, .id = 0};
H
Hongze Cheng 已提交
410

H
Hongze Cheng 已提交
411 412
    // TODO: alloc a new disk
    // tfsAllocDisk(pTfs, 0, &did);
H
Hongze Cheng 已提交
413

H
Hongze Cheng 已提交
414 415
    // create the directory
    tfsMkdirRecurAt(pTfs, pTsdb->path, did);
H
Hongze Cheng 已提交
416

H
Hongze Cheng 已提交
417 418 419 420 421 422
    wSet = (SDFileSet){.diskId = did,
                       .fid = pCommitter->commitFid,
                       .fHead = {.commitID = pCommitter->commitID, .offset = 0, .size = 0},
                       .fData = {.commitID = pCommitter->commitID, .size = 0},
                       .fLast = {.commitID = pCommitter->commitID, .size = 0},
                       .fSma = {.commitID = pCommitter->commitID, .size = 0}};
H
Hongze Cheng 已提交
423
  }
H
Hongze Cheng 已提交
424 425
  code = tsdbDataFWriterOpen(&pCommitter->pWriter, pTsdb, &wSet);
  if (code) goto _err;
H
Hongze Cheng 已提交
426

H
Hongze Cheng 已提交
427
_exit:
H
Hongze Cheng 已提交
428 429 430
  return code;

_err:
H
Hongze Cheng 已提交
431
  tsdbError("vgId:%d commit file data start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
432
  return code;
H
Hongze Cheng 已提交
433 434
}

H
Hongze Cheng 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
static int32_t tsdbCommitterUpdateSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
  int32_t code = 0;

  if (pCommitter->pTSchema) {
    if (pCommitter->suid == suid) {
      if (suid == 0) {
        if (pCommitter->uid == uid && sver == pCommitter->pTSchema->version) goto _exit;
      } else {
        if (sver == pCommitter->pTSchema->version) goto _exit;
      }
    }
  }

_update_schema:
  pCommitter->suid = suid;
  pCommitter->uid = uid;
  tTSchemaDestroy(pCommitter->pTSchema);
  pCommitter->pTSchema = metaGetTbTSchema(pCommitter->pTsdb->pVnode->pMeta, uid, sver);
  if (pCommitter->pTSchema == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
461
static int32_t tsdbCommitMemoryData(SCommitter *pCommitter, STbData *pTbData) {
H
Hongze Cheng 已提交
462
  int32_t      code = 0;
H
Hongze Cheng 已提交
463 464 465
  STsdb       *pTsdb = pCommitter->pTsdb;
  STbDataIter *pIter = &(STbDataIter){0};
  TSDBKEY      key = {.ts = pCommitter->minKey, .version = VERSION_MIN};
H
Hongze Cheng 已提交
466
  TSDBROW      row;
H
Hongze Cheng 已提交
467 468 469
  TSDBROW     *pRow;

  // create iter
H
Hongze Cheng 已提交
470
  tsdbTbDataIterOpen(pTbData, &key, 0, pIter);
H
Hongze Cheng 已提交
471
  pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
472

H
Hongze Cheng 已提交
473
  if (pRow == NULL || TSDBROW_TS(pRow) > pCommitter->maxKey) goto _exit;
H
Hongze Cheng 已提交
474

H
Hongze Cheng 已提交
475 476
  // main loop
  SBlockIdx  *pBlockIdx = &(SBlockIdx){.suid = pTbData->suid, .uid = pTbData->uid};
H
Hongze Cheng 已提交
477
  SMapData   *mBlock = &pCommitter->nBlockMap;
H
Hongze Cheng 已提交
478 479
  SBlock     *pBlock = &pCommitter->nBlock;
  SBlockData *pBlockData = &pCommitter->nBlockData;
H
Hongze Cheng 已提交
480
  TSKEY       lastTS;
H
Hongze Cheng 已提交
481

H
Hongze Cheng 已提交
482
  tBlockIdxReset(pBlockIdx);
H
Hongze Cheng 已提交
483
  tMapDataReset(mBlock);
H
Hongze Cheng 已提交
484 485
  tBlockReset(pBlock);
  tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
486 487 488 489 490 491 492 493 494 495 496
  lastTS = TSKEY_MIN;
  while (1) {
    if (pRow == NULL || TSDBROW_TS(pRow) > pCommitter->maxKey) {
      if (pBlockData->nRow > 0) {
        goto _write_block;
      } else {
        break;
      }
    }

    // update schema
H
Hongze Cheng 已提交
497 498 499
    code = tsdbCommitterUpdateSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
    if (code) goto _err;

H
Hongze Cheng 已提交
500
    // append
H
Hongze Cheng 已提交
501
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->pTSchema);
H
Hongze Cheng 已提交
502
    if (code) goto _err;
H
Hongze Cheng 已提交
503

H
Hongze Cheng 已提交
504
    // update
H
Hongze Cheng 已提交
505 506 507
    pBlock->minVersion = TMIN(pBlock->minVersion, TSDBROW_VERSION(pRow));
    pBlock->maxVersion = TMAX(pBlock->maxVersion, TSDBROW_VERSION(pRow));
    pBlock->nRow++;
H
Hongze Cheng 已提交
508 509
    if (TSDBROW_TS(pRow) == lastTS) pBlock->hasDup = 1;
    lastTS = TSDBROW_TS(pRow);
H
more  
Hongze Cheng 已提交
510

H
Hongze Cheng 已提交
511
    // next
H
Hongze Cheng 已提交
512
    tsdbTbDataIterNext(pIter);
H
Hongze Cheng 已提交
513
    pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
514

H
Hongze Cheng 已提交
515 516 517
    // check
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) goto _write_block;
    continue;
H
Hongze Cheng 已提交
518

H
Hongze Cheng 已提交
519
  _write_block:
H
Hongze Cheng 已提交
520 521 522 523
    row = tBlockDataFirstRow(pBlockData);
    if (tsdbKeyCmprFn(&pBlock->minKey, &TSDBROW_KEY(&row)) > 0) pBlock->minKey = TSDBROW_KEY(&row);
    row = tBlockDataLastRow(pBlockData);
    if (tsdbKeyCmprFn(&pBlock->maxKey, &TSDBROW_KEY(&row)) < 0) pBlock->maxKey = TSDBROW_KEY(&row);
H
Hongze Cheng 已提交
524
    pBlock->last = pBlockData->nRow < pCommitter->minRow ? 1 : 0;
H
Hongze Cheng 已提交
525
    code = tsdbWriteBlockData(pCommitter->pWriter, pBlockData, NULL, NULL, pBlockIdx, pBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
526 527
    if (code) goto _err;

H
Hongze Cheng 已提交
528 529
    // Design SMA and write SMA to file

H
Hongze Cheng 已提交
530 531 532 533 534 535 536
    // SBlockIdx
    code = tMapDataPutItem(mBlock, pBlock, tPutBlock);
    if (code) goto _err;
    pBlockIdx->minKey = TMIN(pBlockIdx->minKey, pBlock->minKey.ts);
    pBlockIdx->maxKey = TMAX(pBlockIdx->maxKey, pBlock->maxKey.ts);
    pBlockIdx->minVersion = TMIN(pBlockIdx->minVersion, pBlock->minVersion);
    pBlockIdx->maxVersion = TMAX(pBlockIdx->maxVersion, pBlock->maxVersion);
H
Hongze Cheng 已提交
537 538 539 540

    tBlockReset(pBlock);
    tBlockDataReset(pBlockData);
    lastTS = TSKEY_MIN;
H
more  
Hongze Cheng 已提交
541 542
  }

H
Hongze Cheng 已提交
543 544 545 546 547 548 549
  // write block
  code = tsdbWriteBlock(pCommitter->pWriter, mBlock, NULL, pBlockIdx);
  if (code) goto _err;

  code = tMapDataPutItem(&pCommitter->nBlockIdxMap, pBlockIdx, tPutBlockIdx);
  if (code) goto _err;

H
Hongze Cheng 已提交
550
_exit:
H
Hongze Cheng 已提交
551
  if (pRow) pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
H
Hongze Cheng 已提交
552 553 554
  return code;

_err:
H
Hongze Cheng 已提交
555
  tsdbError("vgId:%d tsdb commit memory data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
556 557 558
  return code;
}

H
Hongze Cheng 已提交
559 560 561 562
static int32_t tsdbCommitDiskData(SCommitter *pCommitter, SBlockIdx *oBlockIdx) {
  int32_t     code = 0;
  SMapData   *mBlockO = &pCommitter->oBlockMap;
  SBlock     *pBlockO = &pCommitter->oBlock;
H
Hongze Cheng 已提交
563
  SMapData   *mBlockN = &pCommitter->nBlockMap;
H
Hongze Cheng 已提交
564
  SBlock     *pBlockN = &pCommitter->nBlock;
H
Hongze Cheng 已提交
565
  SBlockIdx  *pBlockIdx = &(SBlockIdx){0};
H
Hongze Cheng 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578
  SBlockData *pBlockDataO = &pCommitter->oBlockData;

  // read
  code = tsdbReadBlock(pCommitter->pReader, oBlockIdx, mBlockO, NULL);
  if (code) goto _err;

  // loop to add to new
  tMapDataReset(mBlockN);
  for (int32_t iBlock = 0; iBlock < mBlockO->nItem; iBlock++) {
    tMapDataGetItemByIdx(mBlockO, iBlock, pBlockO, tGetBlock);

    if (pBlockO->last) {
      ASSERT(iBlock == mBlockO->nItem - 1);
H
Hongze Cheng 已提交
579
      code = tsdbReadBlockData(pCommitter->pReader, oBlockIdx, pBlockO, pBlockDataO, NULL, NULL);
H
Hongze Cheng 已提交
580 581 582
      if (code) goto _err;

      tBlockReset(pBlockN);
H
Hongze Cheng 已提交
583 584 585 586 587 588 589
      pBlockN->minKey = pBlockO->minKey;
      pBlockN->maxKey = pBlockO->maxKey;
      pBlockN->minVersion = pBlockO->minVersion;
      pBlockN->maxVersion = pBlockO->maxVersion;
      pBlockN->nRow = pBlockO->nRow;
      pBlockN->last = pBlockO->last;
      pBlockN->hasDup = pBlockO->hasDup;
H
Hongze Cheng 已提交
590
      code = tsdbWriteBlockData(pCommitter->pWriter, pBlockDataO, NULL, NULL, pBlockIdx, pBlockN, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
591 592 593 594 595 596 597 598 599 600 601
      if (code) goto _err;

      code = tMapDataPutItem(mBlockN, pBlockN, tPutBlock);
      if (code) goto _err;
    } else {
      code = tMapDataPutItem(mBlockN, pBlockO, tPutBlock);
      if (code) goto _err;
    }
  }

  // SBlock
H
Hongze Cheng 已提交
602
  *pBlockIdx = *oBlockIdx;
H
Hongze Cheng 已提交
603 604 605 606 607 608 609 610 611 612
  code = tsdbWriteBlock(pCommitter->pWriter, mBlockN, NULL, pBlockIdx);
  if (code) goto _err;

  // SBlockIdx
  code = tMapDataPutItem(&pCommitter->nBlockIdxMap, pBlockIdx, tPutBlockIdx);
  if (code) goto _err;

  return code;

_err:
H
Hongze Cheng 已提交
613
  tsdbError("vgId:%d tsdb commit disk data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
614 615 616
  return code;
}

H
Hongze Cheng 已提交
617 618
static int32_t tsdbMergeTableData(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlockMerge, TSDBKEY toKey,
                                  int8_t toDataOnly) {
H
Hongze Cheng 已提交
619
  int32_t     code = 0;
H
Hongze Cheng 已提交
620 621 622 623
  SBlockIdx  *pBlockIdx = &(SBlockIdx){.suid = pIter->pTbData->suid, .uid = pIter->pTbData->uid};
  SBlockData *pBlockDataMerge = &pCommitter->oBlockData;
  SBlockData *pBlockData = &pCommitter->nBlockData;
  SBlock     *pBlock = &pCommitter->nBlock;
H
Hongze Cheng 已提交
624
  TSDBROW    *pRow1;
H
Hongze Cheng 已提交
625 626 627 628
  TSDBROW     row2;
  TSDBROW    *pRow2 = &row2;
  TSDBROW     row;
  TSDBROW    *pRow = &row;
H
Hongze Cheng 已提交
629
  int32_t     c = 0;
H
Hongze Cheng 已提交
630
  TSKEY       lastKey;
H
Hongze Cheng 已提交
631 632

  // read SBlockData
H
Hongze Cheng 已提交
633
  code = tsdbReadBlockData(pCommitter->pReader, pBlockIdx, pBlockMerge, pBlockDataMerge, NULL, NULL);
H
Hongze Cheng 已提交
634 635 636 637
  if (code) goto _err;

  // loop to merge
  pRow1 = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
638
  *pRow2 = tsdbRowFromBlockData(pBlockDataMerge, 0);
H
Hongze Cheng 已提交
639
  ASSERT(pRow1 && tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0);
H
Hongze Cheng 已提交
640 641 642 643
  ASSERT(tsdbKeyCmprFn(&TSDBROW_KEY(pRow2), &toKey) < 0);
  code = tsdbCommitterUpdateSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_SVERSION(pRow1));
  if (code) goto _err;

H
Hongze Cheng 已提交
644 645 646
  lastKey = TSKEY_MIN;
  tBlockReset(pBlock);
  tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
647 648
  while (true) {
    if (pRow1 == NULL && pRow2 == NULL) {
H
Hongze Cheng 已提交
649
      if (pBlockData->nRow == 0) {
H
Hongze Cheng 已提交
650 651 652 653 654 655 656
        break;
      } else {
        goto _write_block;
      }
    }

    if (pRow1 && pRow2) {
H
Hongze Cheng 已提交
657 658
      if (tsdbRowCmprFn(pRow1, pRow2) < 0) {
        *pRow = *pRow1;
H
Hongze Cheng 已提交
659 660 661 662

        tsdbTbDataIterNext(pIter);
        pRow1 = tsdbTbDataIterGet(pIter);

H
Hongze Cheng 已提交
663 664 665 666 667 668 669 670
        if (pRow1) {
          if (tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0) {
            code = tsdbCommitterUpdateSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_VERSION(pRow1));
            if (code) goto _err;
          } else {
            pRow1 = NULL;
          }
        }
H
Hongze Cheng 已提交
671
      } else if (tsdbRowCmprFn(pRow1, pRow2) > 0) {
H
Hongze Cheng 已提交
672 673 674 675 676 677 678
        *pRow = *pRow2;

        if (pRow2->iRow + 1 < pBlockDataMerge->nRow) {
          *pRow2 = tsdbRowFromBlockData(pBlockDataMerge, pRow2->iRow + 1);
        } else {
          pRow2 = NULL;
        }
H
Hongze Cheng 已提交
679 680 681 682
      } else {
        ASSERT(0);
      }
    } else if (pRow1) {
H
Hongze Cheng 已提交
683 684
      *pRow = *pRow1;

H
Hongze Cheng 已提交
685 686
      tsdbTbDataIterNext(pIter);
      pRow1 = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
687 688 689 690 691 692 693 694
      if (pRow1) {
        if (tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0) {
          code = tsdbCommitterUpdateSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_VERSION(pRow1));
          if (code) goto _err;
        } else {
          pRow1 = NULL;
        }
      }
H
Hongze Cheng 已提交
695
    } else {
H
Hongze Cheng 已提交
696
      *pRow = *pRow2;
H
Hongze Cheng 已提交
697

H
Hongze Cheng 已提交
698 699 700 701 702
      if (pRow2->iRow + 1 < pBlockDataMerge->nRow) {
        *pRow2 = tsdbRowFromBlockData(pBlockDataMerge, pRow2->iRow + 1);
      } else {
        pRow2 = NULL;
      }
H
Hongze Cheng 已提交
703 704
    }

H
Hongze Cheng 已提交
705
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->pTSchema);
H
Hongze Cheng 已提交
706 707 708 709 710 711 712
    if (code) goto _err;

    pBlock->minVersion = TMIN(pBlock->minVersion, TSDBROW_VERSION(pRow));
    pBlock->maxVersion = TMAX(pBlock->maxVersion, TSDBROW_VERSION(pRow));
    pBlock->nRow++;
    if (lastKey == TSDBROW_TS(pRow)) {
      pBlock->hasDup = 1;
H
Hongze Cheng 已提交
713
    } else {
H
Hongze Cheng 已提交
714
      lastKey = TSDBROW_TS(pRow);
H
Hongze Cheng 已提交
715 716
    }

H
Hongze Cheng 已提交
717 718 719
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) goto _write_block;
    continue;

H
Hongze Cheng 已提交
720
  _write_block:
H
Hongze Cheng 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
    if (!toDataOnly && pBlockData->nRow < pCommitter->minRow) {
      pBlock->last = 1;
    } else {
      pBlock->last = 0;
    }

    code = tsdbWriteBlockData(pCommitter->pWriter, pBlockData, NULL, NULL, pBlockIdx, pBlock, pCommitter->cmprAlg);
    if (code) goto _err;

    code = tMapDataPutItem(&pCommitter->nBlockMap, pBlock, tPutBlock);
    if (code) goto _err;

    lastKey = TSKEY_MIN;
    tBlockReset(pBlock);
    tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
736 737 738 739 740 741 742 743 744
  }

  return code;

_err:
  tsdbError("vgId:%d tsdb merge block and mem failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
  return code;
}

H
Hongze Cheng 已提交
745
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey, int8_t toDataOnly) {
H
Hongze Cheng 已提交
746 747
  int32_t     code = 0;
  TSDBROW    *pRow;
H
Hongze Cheng 已提交
748
  SBlock     *pBlock = &pCommitter->nBlock;
H
Hongze Cheng 已提交
749
  SBlockData *pBlockData = &pCommitter->nBlockData;
H
Hongze Cheng 已提交
750 751 752
  TSKEY       lastKey = TSKEY_MIN;
  int64_t     suid = pIter->pTbData->suid;
  int64_t     uid = pIter->pTbData->uid;
H
Hongze Cheng 已提交
753

H
Hongze Cheng 已提交
754
  tBlockReset(pBlock);
H
Hongze Cheng 已提交
755
  tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
756
  pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
757
  ASSERT(pRow && tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &toKey) < 0);
H
Hongze Cheng 已提交
758
  while (true) {
H
Hongze Cheng 已提交
759
    if (pRow == NULL) {
H
Hongze Cheng 已提交
760 761 762 763 764 765 766
      if (pBlockData->nRow > 0) {
        goto _write_block;
      } else {
        break;
      }
    }

H
Hongze Cheng 已提交
767
    // update schema
H
Hongze Cheng 已提交
768 769 770
    code = tsdbCommitterUpdateSchema(pCommitter, pIter->pTbData->suid, pIter->pTbData->uid, TSDBROW_SVERSION(pRow));
    if (code) goto _err;

H
Hongze Cheng 已提交
771
    // append
H
Hongze Cheng 已提交
772 773 774
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->pTSchema);
    if (code) goto _err;

H
Hongze Cheng 已提交
775 776 777 778 779 780 781 782 783 784
    // update
    pBlock->minVersion = TMIN(pBlock->minVersion, TSDBROW_VERSION(pRow));
    pBlock->maxVersion = TMIN(pBlock->maxVersion, TSDBROW_VERSION(pRow));
    pBlock->nRow++;
    if (TSDBROW_TS(pRow) == lastKey) {
      pBlock->hasDup = 1;
    } else {
      lastKey = TSDBROW_TS(pRow);
    }

H
Hongze Cheng 已提交
785 786
    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
787
    if (pRow && tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &toKey) >= 0) pRow = NULL;
H
Hongze Cheng 已提交
788 789

    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) goto _write_block;
H
Hongze Cheng 已提交
790
    continue;
H
Hongze Cheng 已提交
791 792

  _write_block:
H
Hongze Cheng 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806
    if (!toDataOnly && pBlockData->nRow < pCommitter->minKey) {
      pBlock->last = 1;
    } else {
      pBlock->last = 0;
    }

    code = tsdbWriteBlockData(pCommitter->pWriter, pBlockData, NULL, NULL, &(SBlockIdx){.suid = suid, .uid = uid},
                              pBlock, pCommitter->cmprAlg);
    if (code) goto _err;

    code = tMapDataPutItem(&pCommitter->nBlockMap, pBlock, tPutBlock);
    if (code) goto _err;

    tBlockReset(pBlock);
H
Hongze Cheng 已提交
807
    tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
808
    lastKey = TSKEY_MIN;
H
Hongze Cheng 已提交
809 810 811 812 813
  }

  return code;

_err:
H
Hongze Cheng 已提交
814
  tsdbError("vgId:%d tsdb commit table mem data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
815 816 817
  return code;
}

H
Hongze Cheng 已提交
818
static int32_t tsdbCommitTableDiskData(SCommitter *pCommitter, SBlock *pBlock, SBlockIdx *pBlockIdx) {
H
Hongze Cheng 已提交
819
  int32_t code = 0;
H
Hongze Cheng 已提交
820 821

  if (pBlock->last) {
H
Hongze Cheng 已提交
822
    code = tsdbReadBlockData(pCommitter->pReader, pBlockIdx, pBlock, &pCommitter->oBlockData, NULL, NULL);
H
Hongze Cheng 已提交
823 824
    if (code) goto _err;

H
Hongze Cheng 已提交
825 826 827 828 829 830 831 832 833
    tBlockReset(&pCommitter->nBlock);
    pCommitter->nBlock.minKey = pBlock->minKey;
    pCommitter->nBlock.maxKey = pBlock->maxKey;
    pCommitter->nBlock.minVersion = pBlock->minVersion;
    pCommitter->nBlock.nRow = pBlock->nRow;
    pCommitter->nBlock.last = pBlock->last;
    pCommitter->nBlock.hasDup = pBlock->hasDup;
    code = tsdbWriteBlockData(pCommitter->pWriter, &pCommitter->oBlockData, NULL, NULL, pBlockIdx, &pCommitter->nBlock,
                              pCommitter->cmprAlg);
H
Hongze Cheng 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846
    if (code) goto _err;

    code = tMapDataPutItem(&pCommitter->nBlockMap, &pCommitter->nBlock, tPutBlock);
    if (code) goto _err;
  } else {
    code = tMapDataPutItem(&pCommitter->nBlockMap, pBlock, tPutBlock);
    if (code) goto _err;
  }

  return code;

_err:
  tsdbError("vgId:%d tsdb commit table disk data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
847 848 849
  return code;
}

H
Hongze Cheng 已提交
850
static int32_t tsdbMergeMemDisk(SCommitter *pCommitter, STbData *pTbData, SBlockIdx *oBlockIdx) {
H
Hongze Cheng 已提交
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 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 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
  int32_t code = 0;
  //   STbDataIter *pIter = &(STbDataIter){0};
  //   TSDBROW     *pRow;

  //   // create iter
  //   tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = VERSION_MIN}, 0, pIter);
  //   pRow == tsdbTbDataIterGet(pIter);
  //   if (pRow == NULL || TSDBROW_TS(pRow) > pCommitter->maxKey) {
  //     code = tsdbCommitDiskData(pCommitter, oBlockIdx);
  //     if (code) {
  //       goto _err;
  //     } else {
  //       goto _exit;
  //     }
  //   }

  //   // start ==================
  //   // read
  //   code = tsdbReadBlock(pCommitter->pReader, oBlockIdx, &pCommitter->oBlockMap, NULL);
  //   if (code) goto _err;

  //   // loop to merge
  //   // SBlockData *pBlockData = &pCommitter->nBlockData;
  //   int32_t iBlock = 0;
  //   int32_t nBlock = pCommitter->oBlockMap.nItem;
  //   // SBlock     *pBlockO = &pCommitter->oBlock;
  //   SBlock *pBlock;
  //   int32_t c;

  //   // merge ===================
  //   while (true) {
  //     if ((pRow == NULL || TSDBROW_TS(pRow) > pCommitter->maxKey) && pBlock == NULL) break;

  //     if ((pRow && TSDBROW_TS(pRow) <= pCommitter->maxKey) && pBlock) {
  //       if (pBlock->last) {
  //         // merge memory data and disk data to write to .data/.last (todo)
  //         code = tsdbMergeTableData(pCommitter, pIter, oBlockIdx, pBlock,
  //                                   (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
  //         if (code) goto _err;

  //         pRow = tsdbTbDataIterGet(pIter);
  //         iBlock++;
  //       } else {
  //         c = tBlockCmprFn(&(SBlock){}, pBlock);

  //         if (c < 0) {
  //           // commit memory data until pBlock->minKey (not included) only to .data file (todo)
  //           code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey, 1);
  //           if (code) goto _err;

  //           pRow = tsdbTbDataIterGet(pIter);
  //         } else if (c > 0) {
  //           // just move the block (todo)
  //           // code = tsdbCommitTableDiskData(pCommitter, pBlock);
  //           if (code) goto _err;

  //           iBlock++;
  //           // TODO
  //         } else {
  //           int64_t nOvlp = 0;  // = tsdbOvlpRows();
  //           if (nOvlp + pBlock->nRow <= pCommitter->maxRow) {
  //             // add as a subblock
  //           } else {
  //             if (iBlock == nBlock - 1) {
  //               // merge memory data and disk data to .data/.last file
  //               code = tsdbMergeTableData(pCommitter, pIter, oBlockIdx, pBlock,
  //                                         (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
  //               if (code) goto _err;
  //             } else {
  //               // merge memory data and disk data to .data file only until pBlock[1].
  //               code = tsdbMergeTableData(pCommitter, pIter, oBlockIdx, pBlock, (TSDBKEY){0} /*TODO*/, 1);
  //             }
  //           }

  //           pRow = tsdbTbDataIterGet(pIter);
  //           iBlock++;
  //         }
  //       }
  //     } else if (pBlock) {
  //       // code = tsdbCommitTableDiskData(pCommitter, pBlock);
  //       if (code) goto _err;

  //       iBlock++;
  //       // next block
  //     } else {
  //       // commit only memory data until (pCommitter->maxKey, VERSION_MAX)
  //       code =
  //           tsdbCommitTableMemData(pCommitter, pIter, (TSDBKEY){.ts = pCommitter->maxKey + 1, .version =
  //           VERSION_MIN}, 0);
  //       if (code) goto _err;

  //       pRow = tsdbTbDataIterGet(pIter);
  //     }
  //   }

  //   // end =====================
  //   // SBlock
  //   // code = tsdbWriteBlock(pCommitter->pWriter, &pCommitter->nBlockMap, NULL, pBlockIdx);
  //   // if (code) goto _err;

  //   // // SBlockIdx
  //   // code = tMapDataPutItem(&pCommitter->nBlockIdxMap, pBlockIdx, tPutBlockIdx);
  //   // if (code) goto _err;

  // _exit:
  //   pRow = tsdbTbDataIterGet(pIter);
  //   if (pRow) {
  //     pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
  //   }
H
Hongze Cheng 已提交
960 961
  return code;

H
Hongze Cheng 已提交
962 963 964
  // _err:
  //   tsdbError("vgId:%d tsdb merge mem disk data failed since %s", TD_VID(pCommitter->pTsdb->pVnode),
  //   tstrerror(code)); return code;
H
Hongze Cheng 已提交
965 966
}

H
Hongze Cheng 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
static int32_t tsdbCommitTableDataEnd(SCommitter *pCommitter, int64_t suid, int64_t uid) {
  int32_t    code = 0;
  SBlockIdx *pBlockIdx = &(SBlockIdx){.suid = suid, .uid = uid};

  code = tsdbWriteBlock(pCommitter->pWriter, &pCommitter->nBlockMap, NULL, pBlockIdx);
  if (code) goto _err;

  code = tMapDataPutItem(&pCommitter->nBlockIdxMap, pBlockIdx, tPutBlockIdx);
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
984 985 986 987
static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData, SBlockIdx *pBlockIdx) {
  int32_t      code = 0;
  STbDataIter *pIter = &(STbDataIter){0};
  TSDBROW     *pRow;
H
Hongze Cheng 已提交
988
  int32_t      iBlock;
H
Hongze Cheng 已提交
989
  int32_t      nBlock;
H
Hongze Cheng 已提交
990 991
  int64_t      suid;
  int64_t      uid;
H
Hongze Cheng 已提交
992 993 994 995

  if (pTbData) {
    tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = VERSION_MIN}, 0, pIter);
    pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
996 997
    if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;

H
Hongze Cheng 已提交
998 999
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  } else {
    pIter = NULL;
    pRow = NULL;
  }

  if (pBlockIdx) {
    code = tsdbReadBlock(pCommitter->pReader, pBlockIdx, &pCommitter->oBlockMap, NULL);
    if (code) goto _err;

    nBlock = pCommitter->oBlockMap.nItem;
    ASSERT(nBlock > 0);
H
Hongze Cheng 已提交
1011

H
Hongze Cheng 已提交
1012 1013
    suid = pBlockIdx->suid;
    uid = pBlockIdx->uid;
H
Hongze Cheng 已提交
1014 1015 1016 1017
  } else {
    nBlock = 0;
  }

H
Hongze Cheng 已提交
1018
  if (pRow == NULL && nBlock == 0) goto _exit;
H
Hongze Cheng 已提交
1019 1020

  // start ===========
H
Hongze Cheng 已提交
1021
  tMapDataReset(&pCommitter->nBlockMap);
H
Hongze Cheng 已提交
1022
  SBlock *pBlock = &pCommitter->oBlock;
H
Hongze Cheng 已提交
1023

H
Hongze Cheng 已提交
1024
  iBlock = 0;
H
Hongze Cheng 已提交
1025 1026 1027 1028 1029 1030
  if (iBlock < nBlock) {
    tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
  } else {
    pBlock = NULL;
  }

H
Hongze Cheng 已提交
1031 1032
  // merge ===========
  while (true) {
H
Hongze Cheng 已提交
1033
    if (pRow == NULL && pBlock == NULL) break;
H
Hongze Cheng 已提交
1034

H
Hongze Cheng 已提交
1035
    if (pRow && pBlock) {
H
Hongze Cheng 已提交
1036
      if (pBlock->last) {
H
Hongze Cheng 已提交
1037
        code = tsdbMergeTableData(pCommitter, pIter, pBlock,
H
Hongze Cheng 已提交
1038
                                  (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
H
Hongze Cheng 已提交
1039 1040 1041
        if (code) goto _err;

        pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
1042
        if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
H
Hongze Cheng 已提交
1043
        iBlock++;
H
Hongze Cheng 已提交
1044 1045 1046 1047 1048
        if (iBlock < nBlock) {
          tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
        } else {
          pBlock = NULL;
        }
H
Hongze Cheng 已提交
1049 1050

        ASSERT(pRow == NULL && pBlock == NULL);
H
Hongze Cheng 已提交
1051
      } else {
H
Hongze Cheng 已提交
1052
        int32_t c = tBlockCmprFn(&(SBlock){.maxKey = TSDBROW_KEY(pRow), .minKey = TSDBROW_KEY(pRow)}, pBlock);
H
Hongze Cheng 已提交
1053
        if (c > 0) {
H
Hongze Cheng 已提交
1054
          // only disk data
H
Hongze Cheng 已提交
1055
          code = tsdbCommitTableDiskData(pCommitter, pBlock, pBlockIdx);
H
Hongze Cheng 已提交
1056 1057
          if (code) goto _err;

H
Hongze Cheng 已提交
1058 1059 1060 1061 1062 1063
          iBlock++;
          if (iBlock < nBlock) {
            tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
          } else {
            pBlock = NULL;
          }
H
Hongze Cheng 已提交
1064
        } else if (c < 0) {
H
Hongze Cheng 已提交
1065
          // only memory data
H
Hongze Cheng 已提交
1066
          code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey, 1);
H
Hongze Cheng 已提交
1067 1068 1069
          if (code) goto _err;

          pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
1070
          if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
H
Hongze Cheng 已提交
1071
        } else {
H
Hongze Cheng 已提交
1072
          // merge memory and disk
H
Hongze Cheng 已提交
1073 1074 1075 1076 1077
          int64_t nOvlp = 0;  // (todo)
          if (pBlock->nRow + nOvlp <= pCommitter->maxRow && pBlock->nSubBlock < TSDB_MAX_SUBBLOCKS) {
            // add as a subblock
          } else {
            if (iBlock == nBlock - 1) {
H
Hongze Cheng 已提交
1078
              code = tsdbMergeTableData(pCommitter, pIter, pBlock,
H
Hongze Cheng 已提交
1079
                                        (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
H
Hongze Cheng 已提交
1080 1081 1082

              if (code) goto _err;
            } else {
H
Hongze Cheng 已提交
1083
              // code = tsdbMergeTableData(pCommitter, pIter, pBlock, pBlock[1].minKey, 1);
H
Hongze Cheng 已提交
1084 1085 1086
              if (code) goto _err;
            }
          }
H
Hongze Cheng 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095

          pRow = tsdbTbDataIterGet(pIter);
          if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
          iBlock++;
          if (iBlock < nBlock) {
            tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
          } else {
            pBlock = NULL;
          }
H
Hongze Cheng 已提交
1096 1097 1098
        }
      }
    } else if (pBlock) {
H
Hongze Cheng 已提交
1099
      code = tsdbCommitTableDiskData(pCommitter, pBlock, pBlockIdx);
H
Hongze Cheng 已提交
1100 1101
      if (code) goto _err;

H
Hongze Cheng 已提交
1102 1103 1104 1105 1106 1107
      iBlock++;
      if (iBlock < nBlock) {
        tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
      } else {
        pBlock = NULL;
      }
H
Hongze Cheng 已提交
1108
    } else {
H
Hongze Cheng 已提交
1109 1110
      code =
          tsdbCommitTableMemData(pCommitter, pIter, (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
H
Hongze Cheng 已提交
1111 1112 1113
      if (code) goto _err;

      pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
1114 1115
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
      ASSERT(pRow == NULL);
H
Hongze Cheng 已提交
1116 1117 1118
    }
  }

H
Hongze Cheng 已提交
1119 1120
  // end =====================
  code = tsdbCommitTableDataEnd(pCommitter, suid, uid);
H
Hongze Cheng 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
  if (code) goto _err;

_exit:
  if (pIter) {
    pRow = tsdbTbDataIterGet(pIter);
    if (pRow) pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
  }
  return code;

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

H
Hongze Cheng 已提交
1135 1136
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t    code = 0;
H
Hongze Cheng 已提交
1137
  int32_t    c;
H
Hongze Cheng 已提交
1138 1139 1140 1141
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  int32_t    iTbData = 0;
  int32_t    nTbData = taosArrayGetSize(pMemTable->aTbData);
H
Hongze Cheng 已提交
1142
  int32_t    iBlockIdx = 0;
H
Hongze Cheng 已提交
1143
  int32_t    nBlockIdx = pCommitter->oBlockIdxMap.nItem;
H
Hongze Cheng 已提交
1144
  STbData   *pTbData;
H
Hongze Cheng 已提交
1145
  SBlockIdx *pBlockIdx = &(SBlockIdx){0};
H
Hongze Cheng 已提交
1146

H
Hongze Cheng 已提交
1147
  ASSERT(nTbData > 0);
H
Hongze Cheng 已提交
1148

H
Hongze Cheng 已提交
1149 1150
  pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
  if (iBlockIdx < nBlockIdx) {
H
Hongze Cheng 已提交
1151
    tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
H
Hongze Cheng 已提交
1152 1153
  } else {
    pBlockIdx = NULL;
H
Hongze Cheng 已提交
1154 1155
  }

H
Hongze Cheng 已提交
1156 1157 1158 1159 1160 1161
  // merge
  while (pTbData && pBlockIdx) {
    c = tTABLEIDCmprFn(pTbData, pBlockIdx);

    if (c == 0) {
      // merge commit
H
Hongze Cheng 已提交
1162 1163
      code = tsdbMergeMemDisk(pCommitter, pTbData, pBlockIdx);
      if (code) goto _err;
H
Hongze Cheng 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

      iTbData++;
      iBlockIdx++;
      if (iTbData < nTbData) {
        pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
      } else {
        pTbData = NULL;
      }
      if (iBlockIdx < nBlockIdx) {
        tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
      } else {
        pBlockIdx = NULL;
      }
    } else if (c < 0) {
      // commit memory data
H
Hongze Cheng 已提交
1179 1180
      code = tsdbCommitMemoryData(pCommitter, pTbData);
      if (code) goto _err;
H
Hongze Cheng 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189

      iTbData++;
      if (iTbData < nTbData) {
        pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
      } else {
        pTbData = NULL;
      }
    } else {
      // commit disk data
H
Hongze Cheng 已提交
1190 1191
      code = tsdbCommitDiskData(pCommitter, pBlockIdx);
      if (code) goto _err;
H
Hongze Cheng 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

      iBlockIdx++;
      if (iBlockIdx < nBlockIdx) {
        tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
      } else {
        pBlockIdx = NULL;
      }
    }
  }

  // disk
  while (pBlockIdx) {
    // commit disk data
H
Hongze Cheng 已提交
1205 1206
    code = tsdbCommitDiskData(pCommitter, pBlockIdx);
    if (code) goto _err;
H
Hongze Cheng 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

    iBlockIdx++;
    if (iBlockIdx < nBlockIdx) {
      tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
    } else {
      pBlockIdx = NULL;
    }
  }

  // memory
  while (pTbData) {
    // commit memory data
    code = tsdbCommitMemoryData(pCommitter, pTbData);
    if (code) goto _err;

    iTbData++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    } else {
      pTbData = NULL;
    }
  }

H
Hongze Cheng 已提交
1230 1231 1232
  return code;

_err:
H
Hongze Cheng 已提交
1233
  tsdbError("vgId:%d commit file data impl failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1234 1235 1236 1237 1238
  return code;
}

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

H
Hongze Cheng 已提交
1240
  // write blockIdx
H
Hongze Cheng 已提交
1241
  code = tsdbWriteBlockIdx(pCommitter->pWriter, &pCommitter->nBlockIdxMap, NULL);
H
Hongze Cheng 已提交
1242 1243
  if (code) goto _err;

H
Hongze Cheng 已提交
1244
  // update file header
H
Hongze Cheng 已提交
1245
  code = tsdbUpdateDFileSetHeader(pCommitter->pWriter);
H
Hongze Cheng 已提交
1246 1247
  if (code) goto _err;

H
Hongze Cheng 已提交
1248 1249 1250 1251
  // upsert SDFileSet
  code = tsdbFSStateUpsertDFileSet(pCommitter->pTsdb->fs->nState, tsdbDataFWriterGetWSet(pCommitter->pWriter));
  if (code) goto _err;

H
Hongze Cheng 已提交
1252
  // close and sync
H
Hongze Cheng 已提交
1253
  code = tsdbDataFWriterClose(&pCommitter->pWriter, 1);
H
Hongze Cheng 已提交
1254 1255 1256
  if (code) goto _err;

  if (pCommitter->pReader) {
H
Hongze Cheng 已提交
1257
    code = tsdbDataFReaderClose(&pCommitter->pReader);
H
Hongze Cheng 已提交
1258 1259 1260 1261 1262 1263 1264
    goto _err;
  }

_exit:
  return code;

_err:
H
Hongze Cheng 已提交
1265
  tsdbError("vgId:%d commit file data end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1266 1267 1268
  return code;
}

H
Hongze Cheng 已提交
1269
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1270 1271
  int32_t code = 0;

H
Hongze Cheng 已提交
1272 1273
  // commit file data start
  code = tsdbCommitFileDataStart(pCommitter);
H
Hongze Cheng 已提交
1274
  if (code) goto _err;
H
Hongze Cheng 已提交
1275

H
Hongze Cheng 已提交
1276 1277
  // commit file data impl
  code = tsdbCommitFileDataImpl(pCommitter);
H
Hongze Cheng 已提交
1278
  if (code) goto _err;
H
Hongze Cheng 已提交
1279

H
Hongze Cheng 已提交
1280 1281
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
1282
  if (code) goto _err;
H
Hongze Cheng 已提交
1283 1284 1285 1286

  return code;

_err:
H
Hongze Cheng 已提交
1287
  tsdbError("vgId:%d commit file data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1288 1289 1290
  return code;
}

H
Hongze Cheng 已提交
1291 1292
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1293
  int32_t code = 0;
H
Hongze Cheng 已提交
1294

H
Hongze Cheng 已提交
1295 1296
  memset(pCommitter, 0, sizeof(*pCommitter));
  ASSERT(pTsdb->mem && pTsdb->imem == NULL);
H
Hongze Cheng 已提交
1297

H
Hongze Cheng 已提交
1298 1299 1300 1301
  // lock();
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
  // unlock();
H
Hongze Cheng 已提交
1302

H
Hongze Cheng 已提交
1303
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
1304
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
1305 1306 1307 1308
  pCommitter->minutes = pTsdb->keepCfg.days;
  pCommitter->precision = pTsdb->keepCfg.precision;
  pCommitter->minRow = pTsdb->pVnode->config.tsdbCfg.minRows;
  pCommitter->maxRow = pTsdb->pVnode->config.tsdbCfg.maxRows;
H
Hongze Cheng 已提交
1309
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1310

H
Hongze Cheng 已提交
1311 1312 1313 1314 1315 1316 1317
  code = tsdbFSBegin(pTsdb->fs);
  if (code) goto _err;

  return code;

_err:
  tsdbError("vgId:%d tsdb start commit failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1318 1319 1320
  return code;
}

H
Hongze Cheng 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;

  pCommitter->pReader = NULL;
  pCommitter->oBlockIdxMap = tMapDataInit();
  pCommitter->oBlockMap = tMapDataInit();
  pCommitter->oBlock = tBlockInit();
  pCommitter->pWriter = NULL;
  pCommitter->nBlockIdxMap = tMapDataInit();
  pCommitter->nBlockMap = tMapDataInit();
  pCommitter->nBlock = tBlockInit();
  code = tBlockDataInit(&pCommitter->oBlockData);
  if (code) goto _exit;
  code = tBlockDataInit(&pCommitter->nBlockData);
  if (code) {
    tBlockDataClear(&pCommitter->oBlockData);
    goto _exit;
  }

_exit:
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1345 1346 1347 1348 1349 1350 1351 1352
  // tMapDataClear(&pCommitter->oBlockIdxMap);
  // tMapDataClear(&pCommitter->oBlockMap);
  // tBlockClear(&pCommitter->oBlock);
  // tBlockDataClear(&pCommitter->oBlockData);
  // tMapDataClear(&pCommitter->nBlockIdxMap);
  // tMapDataClear(&pCommitter->nBlockMap);
  // tBlockClear(&pCommitter->nBlock);
  // tBlockDataClear(&pCommitter->nBlockData);
H
Hongze Cheng 已提交
1353 1354
}

H
Hongze Cheng 已提交
1355 1356 1357 1358
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1359

H
Hongze Cheng 已提交
1360
  // check
H
Hongze Cheng 已提交
1361
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
1362

H
Hongze Cheng 已提交
1363 1364
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
1365
  if (code) goto _err;
H
Hongze Cheng 已提交
1366 1367 1368

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
1369 1370 1371 1372 1373
  while (pCommitter->nextKey < TSKEY_MAX) {
    pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
    tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
                    &pCommitter->maxKey);
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
1374
    if (code) goto _err;
H
Hongze Cheng 已提交
1375
  }
H
Hongze Cheng 已提交
1376

H
Hongze Cheng 已提交
1377 1378 1379
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
1380 1381 1382
_exit:
  tsdbDebug("vgId:%d commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
  return code;
H
Hongze Cheng 已提交
1383

H
Hongze Cheng 已提交
1384
_err:
H
Hongze Cheng 已提交
1385
  tsdbCommitDataEnd(pCommitter);
H
Hongze Cheng 已提交
1386 1387 1388
  tsdbError("vgId:%d commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}
H
Hongze Cheng 已提交
1389

H
Hongze Cheng 已提交
1390 1391 1392 1393
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1394

H
Hongze Cheng 已提交
1395 1396
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
1397
  }
H
Hongze Cheng 已提交
1398

H
Hongze Cheng 已提交
1399 1400 1401 1402 1403
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1404

H
Hongze Cheng 已提交
1405 1406 1407 1408 1409
  // impl
  code = tsdbCommitDelImpl(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1410

H
Hongze Cheng 已提交
1411 1412 1413 1414 1415
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1416

H
Hongze Cheng 已提交
1417
_exit:
H
Hongze Cheng 已提交
1418
  tsdbDebug("vgId:%d commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1419 1420 1421
  return code;

_err:
H
Hongze Cheng 已提交
1422
  tsdbError("vgId:%d commit del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1423
  return code;
H
Hongze Cheng 已提交
1424 1425 1426 1427 1428 1429 1430
}

static int32_t tsdbCommitCache(SCommitter *pCommitter) {
  int32_t code = 0;
  // TODO
  return code;
}
H
Hongze Cheng 已提交
1431 1432

static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

  if (eno == 0) {
    code = tsdbFSCommit(pTsdb->fs);
  } else {
    code = tsdbFSRollback(pTsdb->fs);
  }

  tsdbMemTableDestroy(pMemTable);
  pTsdb->imem = NULL;

  tsdbInfo("vgId:%d tsdb end commit", TD_VID(pTsdb->pVnode));
  return code;

_err:
  tsdbError("vgId:%d tsdb end commit failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1451 1452
  return code;
}