tsdbFile.c 12.6 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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
TD-353  
Hongze Cheng 已提交
15

H
refact  
Hongze Cheng 已提交
16
#include "tsdbint.h"
H
TD-353  
Hongze Cheng 已提交
17

H
refact  
Hongze Cheng 已提交
18
static const char *TSDB_FNAME_SUFFIX[] = {
H
Hongze Cheng 已提交
19 20 21 22 23
    ".head",  // TSDB_FILE_HEAD
    ".data",  // TSDB_FILE_DATA
    ".last",  // TSDB_FILE_LAST
    "",       // TSDB_FILE_MAX
    "meta"    // TSDB_FILE_META
H
refact  
Hongze Cheng 已提交
24
};
H
TD-353  
Hongze Cheng 已提交
25

H
Hongze Cheng 已提交
26 27 28 29 30 31
static void  tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, char *fname);
static int   tsdbRollBackMFile(SMFile *pMFile);
static int   tsdbEncodeDFInfo(void **buf, SDFInfo *pInfo);
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo);
static int   tsdbRollBackDFile(SDFile *pDFile);

H
refact  
Hongze Cheng 已提交
32
// ============== SMFile
H
Hongze Cheng 已提交
33
void tsdbInitMFile(SMFile *pMFile, SDiskID did, int vid, uint32_t ver) {
H
refact  
Hongze Cheng 已提交
34 35
  char fname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
36
  TSDB_FILE_SET_CLOSED(pMFile);
H
refact  
Hongze Cheng 已提交
37

H
Hongze Cheng 已提交
38 39
  memset(&(pMFile->info), 0, sizeof(pMFile->info));
  pMFile->info.magic = TSDB_FILE_INIT_MAGIC;
H
Hongze Cheng 已提交
40

H
refact  
Hongze Cheng 已提交
41
  tsdbGetFilename(vid, 0, ver, TSDB_FILE_META, fname);
H
Hongze Cheng 已提交
42 43 44 45 46 47
  tfsInitFile(TSDB_FILE_F(pMFile), did.level, did.id, fname);
}

void tsdbInitMFileEx(SMFile *pMFile, SMFile *pOMFile) {
  *pMFile = *pOMFile;
  TSDB_FILE_SET_CLOSED(pMFile);
H
Hongze Cheng 已提交
48
}
H
Hongze Cheng 已提交
49

H
refact  
Hongze Cheng 已提交
50
int tsdbEncodeSMFile(void **buf, SMFile *pMFile) {
H
Hongze Cheng 已提交
51
  int tlen = 0;
H
hzcheng 已提交
52

H
Hongze Cheng 已提交
53 54
  tlen += tsdbEncodeMFInfo(buf, &(pMFile->info));
  tlen += tfsEncodeFile(buf, &(pMFile->f));
H
hzcheng 已提交
55

H
Hongze Cheng 已提交
56
  return tlen;
H
Hongze Cheng 已提交
57 58
}

H
refact  
Hongze Cheng 已提交
59
void *tsdbDecodeSMFile(void *buf, SMFile *pMFile) {
H
Hongze Cheng 已提交
60 61
  buf = tsdbDecodeMFInfo(buf, &(pMFile->info));
  buf = tfsDecodeFile(buf, &(pMFile->f));
H
Hongze Cheng 已提交
62
  TSDB_FILE_SET_CLOSED(pMFile);
H
Hongze Cheng 已提交
63

H
Hongze Cheng 已提交
64 65
  return buf;
}
H
Hongze Cheng 已提交
66

H
Hongze Cheng 已提交
67 68 69 70 71
int tsdbApplyMFileChange(SMFile *from, SMFile *to) {
  ASSERT(from != NULL || to != NULL);

  if (from != NULL) {
    if (to == NULL) {
H
Hongze Cheng 已提交
72
      return tsdbRemoveMFile(from);
H
Hongze Cheng 已提交
73 74 75
    } else {
      if (tfsIsSameFile(TSDB_FILE_F(from), TSDB_FILE_F(to))) {
        if (from->info.size > to->info.size) {
H
Hongze Cheng 已提交
76
          tsdbRollBackMFile(to);
H
Hongze Cheng 已提交
77 78
        }
      } else {
H
Hongze Cheng 已提交
79
        return tsdbRemoveMFile(from);
H
Hongze Cheng 已提交
80 81 82 83 84 85 86
      }
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
87
int tsdbCreateMFile(SMFile *pMFile, bool updateHeader) {
H
Hongze Cheng 已提交
88 89 90 91
  ASSERT(pMFile->info.size == 0 && pMFile->info.magic == TSDB_FILE_INIT_MAGIC);

  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

H
Hongze Cheng 已提交
92
  pMFile->fd = open(TSDB_FILE_FULL_NAME(pMFile), O_WRONLY | O_CREAT | O_TRUNC, 0755);
H
Hongze Cheng 已提交
93 94
  if (pMFile->fd < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
95 96 97
    return -1;
  }

H
Hongze Cheng 已提交
98 99 100 101
  if (!updateHeader) {
    return 0;
  }

H
Hongze Cheng 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  void *ptr = buf;
  tsdbEncodeMFInfo(&ptr, &(pMFile->info));

  if (tsdbWriteMFile(pMFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
    tsdbCloseMFile(pMFile);
    tsdbRemoveMFile(pMFile);
    return -1;
  }

  pMFile->info.size += TSDB_FILE_HEAD_SIZE;

  return 0;
}

int tsdbUpdateMFileHeader(SMFile *pMFile) {
  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

  if (tsdbSeekMFile(pMFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  void *ptr = buf;
H
Hongze Cheng 已提交
124
  tsdbEncodeMFInfo(&ptr, TSDB_FILE_INFO(pMFile));
H
Hongze Cheng 已提交
125 126 127 128 129 130 131 132

  if (tsdbWriteMFile(pMFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

  return 0;
}

H
Hongze Cheng 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
int tsdbLoadMFileHeader(SMFile *pMFile, SMFInfo *pInfo) {
  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

  ASSERT(TSDB_FILE_OPENED(pMFile));

  if (tsdbSeekMFile(pMFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  if (tsdbReadMFile(pMFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

  tsdbDecodeMFInfo(buf, pInfo);
  return 0;
}

H
Hongze Cheng 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
int tsdbScanAndTryFixMFile(SMFile *pMFile) {
  struct stat mfstat;
  SMFile      mf = *pMFile;

  if (stat(TSDB_FILE_FULL_NAME(&mf), &mfstat) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  if (pMFile->info.size > mfstat.st_size) {
    if (tsdbOpenMFile(&mf, O_WRONLY) < 0) {
      return -1;
    }

    if (taosFtruncate(mf.fd, mf.info.size) < 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      tsdbCloseMFile(&mf);
      return -1;
    }

    if (tsdbUpdateMFileHeader(&mf) < 0) {
      tsdbCloseMFile(&mf);
      return -1;
    }

    tsdbCloseMFile(&mf);
  } else if (pMFile->info.size < mfstat.st_size) {
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
    return -1;
  }

  return 0;
}

H
Hongze Cheng 已提交
184
int tsdbEncodeMFInfo(void **buf, SMFInfo *pInfo) {
H
Hongze Cheng 已提交
185
  int tlen = 0;
H
Hongze Cheng 已提交
186

H
Hongze Cheng 已提交
187 188 189 190 191
  tlen += taosEncodeVariantI64(buf, pInfo->size);
  tlen += taosEncodeVariantI64(buf, pInfo->tombSize);
  tlen += taosEncodeVariantI64(buf, pInfo->nRecords);
  tlen += taosEncodeVariantI64(buf, pInfo->nDels);
  tlen += taosEncodeFixedU32(buf, pInfo->magic);
H
Hongze Cheng 已提交
192

H
Hongze Cheng 已提交
193
  return tlen;
H
Hongze Cheng 已提交
194 195
}

H
Hongze Cheng 已提交
196
void *tsdbDecodeMFInfo(void *buf, SMFInfo *pInfo) {
H
Hongze Cheng 已提交
197 198 199 200 201 202 203
  buf = taosDecodeVariantI64(buf, &(pInfo->size));
  buf = taosDecodeVariantI64(buf, &(pInfo->tombSize));
  buf = taosDecodeVariantI64(buf, &(pInfo->nRecords));
  buf = taosDecodeVariantI64(buf, &(pInfo->nDels));
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));

  return buf;
H
Hongze Cheng 已提交
204 205
}

H
Hongze Cheng 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
static int tsdbRollBackMFile(SMFile *pMFile) {
  SMFile mf = *pMFile;

  if (tsdbOpenMFile(&mf, O_WRONLY) < 0) {
    return -1;
  }

  if (taosFtruncate(TSDB_FILE_FD(&mf), pMFile->info.size) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    tsdbCloseMFile(&mf);
    return -1;
  }

  if (tsdbUpdateMFileHeader(&mf) < 0) {
    tsdbCloseMFile(&mf);
    return -1;
  }

  TSDB_FILE_FSYNC(&mf);

  tsdbCloseMFile(&mf);
  return 0;
}

H
Hongze Cheng 已提交
230
// ============== Operations on SDFile
H
Hongze Cheng 已提交
231
void tsdbInitDFile(SDFile *pDFile, SDiskID did, int vid, int fid, uint32_t ver, TSDB_FILE_T ftype) {
H
refact  
Hongze Cheng 已提交
232 233
  char fname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
234
  TSDB_FILE_SET_CLOSED(pDFile);
H
refact  
Hongze Cheng 已提交
235

H
Hongze Cheng 已提交
236 237
  memset(&(pDFile->info), 0, sizeof(pDFile->info));
  pDFile->info.magic = TSDB_FILE_INIT_MAGIC;
H
refact  
Hongze Cheng 已提交
238

H
Hongze Cheng 已提交
239
  tsdbGetFilename(vid, fid, ver, ftype, fname);
H
Hongze Cheng 已提交
240
  tfsInitFile(&(pDFile->f), did.level, did.id, fname);
H
Hongze Cheng 已提交
241 242
}

H
Hongze Cheng 已提交
243 244
void tsdbInitDFileEx(SDFile *pDFile, SDFile *pODFile) {
  *pDFile = *pODFile;
H
Hongze Cheng 已提交
245 246 247
  TSDB_FILE_SET_CLOSED(pDFile);
}

H
refact  
Hongze Cheng 已提交
248
int tsdbEncodeSDFile(void **buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
249
  int tlen = 0;
250

H
Hongze Cheng 已提交
251 252
  tlen += tsdbEncodeDFInfo(buf, &(pDFile->info));
  tlen += tfsEncodeFile(buf, &(pDFile->f));
H
TD-34  
hzcheng 已提交
253

H
Hongze Cheng 已提交
254
  return tlen;
H
TD-34  
hzcheng 已提交
255 256
}

H
refact  
Hongze Cheng 已提交
257
void *tsdbDecodeSDFile(void *buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
258 259
  buf = tsdbDecodeDFInfo(buf, &(pDFile->info));
  buf = tfsDecodeFile(buf, &(pDFile->f));
H
Hongze Cheng 已提交
260
  TSDB_FILE_SET_CLOSED(pDFile);
H
TD-353  
Hongze Cheng 已提交
261

H
Hongze Cheng 已提交
262
  return buf;
H
TD-353  
Hongze Cheng 已提交
263
}
H
TD-353  
Hongze Cheng 已提交
264

H
Hongze Cheng 已提交
265
int tsdbCreateDFile(SDFile *pDFile, bool updateHeader) {
H
Hongze Cheng 已提交
266 267 268 269
  ASSERT(pDFile->info.size == 0 && pDFile->info.magic == TSDB_FILE_INIT_MAGIC);

  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

H
Hongze Cheng 已提交
270
  pDFile->fd = open(TSDB_FILE_FULL_NAME(pDFile), O_WRONLY | O_CREAT | O_TRUNC, 0755);
H
Hongze Cheng 已提交
271 272
  if (pDFile->fd < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
273 274 275
    return -1;
  }

H
Hongze Cheng 已提交
276 277 278 279
  if (!updateHeader) {
    return 0;
  }

H
Hongze Cheng 已提交
280 281 282 283 284 285 286 287 288 289 290
  void *ptr = buf;
  tsdbEncodeDFInfo(&ptr, &(pDFile->info));

  if (tsdbWriteDFile(pDFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
    tsdbCloseDFile(pDFile);
    tsdbRemoveDFile(pDFile);
    return -1;
  }

  pDFile->info.size += TSDB_FILE_HEAD_SIZE;

H
Hongze Cheng 已提交
291 292 293
  return 0;
}

H
Hongze Cheng 已提交
294 295 296 297 298 299
int tsdbUpdateDFileHeader(SDFile *pDFile) {
  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

  if (tsdbSeekDFile(pDFile, 0, SEEK_SET) < 0) {
    return -1;
  }
H
Hongze Cheng 已提交
300

H
Hongze Cheng 已提交
301 302
  void *ptr = buf;
  tsdbEncodeDFInfo(&ptr, &(pDFile->info));
H
Hongze Cheng 已提交
303

H
Hongze Cheng 已提交
304
  if (tsdbWriteDFile(pDFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
H
Hongze Cheng 已提交
305 306
    return -1;
  }
H
Hongze Cheng 已提交
307 308

  return 0;
H
Hongze Cheng 已提交
309 310
}

H
Hongze Cheng 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
static int tsdbScanAndTryFixDFile(SDFile *pDFile) {
  struct stat dfstat;
  SDFile      df = *pDFile;

  if (stat(TSDB_FILE_FULL_NAME(&df), &dfstat) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  if (pDFile->info.size > dfstat.st_size) {
    if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
      return -1;
    }

    if (taosFtruncate(df.fd, df.info.size) < 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      tsdbCloseDFile(&df);
      return -1;
    }

    if (tsdbUpdateDFileHeader(&df) < 0) {
      tsdbCloseDFile(&df);
      return -1;
    }

    tsdbCloseDFile(&df);
  } else if (pDFile->info.size < dfstat.st_size) {
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
    return -1;
  }

  return 0;
}

H
Hongze Cheng 已提交
345
static int tsdbEncodeDFInfo(void **buf, SDFInfo *pInfo) {
H
TD-353  
Hongze Cheng 已提交
346
  int tlen = 0;
H
Hongze Cheng 已提交
347

H
Hongze Cheng 已提交
348
  tlen += taosEncodeFixedU32(buf, pInfo->magic);
H
TD-353  
Hongze Cheng 已提交
349 350 351
  tlen += taosEncodeFixedU32(buf, pInfo->len);
  tlen += taosEncodeFixedU32(buf, pInfo->totalBlocks);
  tlen += taosEncodeFixedU32(buf, pInfo->totalSubBlocks);
H
Hongze Cheng 已提交
352 353 354
  tlen += taosEncodeFixedU32(buf, pInfo->offset);
  tlen += taosEncodeFixedU64(buf, pInfo->size);
  tlen += taosEncodeFixedU64(buf, pInfo->tombSize);
H
TD-353  
Hongze Cheng 已提交
355

H
TD-353  
Hongze Cheng 已提交
356
  return tlen;
H
TD-353  
Hongze Cheng 已提交
357 358
}

H
Hongze Cheng 已提交
359
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo) {
H
Hongze Cheng 已提交
360
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));
H
TD-353  
Hongze Cheng 已提交
361 362 363
  buf = taosDecodeFixedU32(buf, &(pInfo->len));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalBlocks));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalSubBlocks));
H
Hongze Cheng 已提交
364 365 366
  buf = taosDecodeFixedU32(buf, &(pInfo->offset));
  buf = taosDecodeFixedU64(buf, &(pInfo->size));
  buf = taosDecodeFixedU64(buf, &(pInfo->tombSize));
H
TD-353  
Hongze Cheng 已提交
367 368 369 370

  return buf;
}

H
Hongze Cheng 已提交
371 372 373 374 375 376 377 378 379
static int tsdbApplyDFileChange(SDFile *from, SDFile *to) {
  ASSERT(from != NULL || to != NULL);

  if (from != NULL) {
    if (to == NULL) {
      tsdbRemoveDFile(from);
    } else {
      if (tfsIsSameFile(TSDB_FILE_F(from), TSDB_FILE_F(to))) {
        if (from->info.size > to->info.size) {
H
Hongze Cheng 已提交
380
          tsdbRollBackDFile(to);
H
Hongze Cheng 已提交
381 382 383 384 385 386 387 388 389 390
        }
      } else {
        tsdbRemoveDFile(from);
      }
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
391
static int tsdbRollBackDFile(SDFile *pDFile) {
H
Hongze Cheng 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
  SDFile df = *pDFile;

  if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
    return -1;
  }

  if (taosFtruncate(TSDB_FILE_FD(&df), pDFile->info.size) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    tsdbCloseDFile(&df);
    return -1;
  }

  if (tsdbUpdateDFileHeader(&df) < 0) {
    tsdbCloseDFile(&df);
    return -1;
  }

H
Hongze Cheng 已提交
409 410
  TSDB_FILE_FSYNC(&df);

H
Hongze Cheng 已提交
411 412 413 414
  tsdbCloseDFile(&df);
  return 0;
}

H
Hongze Cheng 已提交
415
// ============== Operations on SDFileSet
H
Hongze Cheng 已提交
416
void tsdbInitDFileSet(SDFileSet *pSet, SDiskID did, int vid, int fid, uint32_t ver) {
H
Hongze Cheng 已提交
417 418
  pSet->fid = fid;
  pSet->state = 0;
H
Hongze Cheng 已提交
419

H
Hongze Cheng 已提交
420 421
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    SDFile *pDFile = TSDB_DFILE_IN_SET(pSet, ftype);
H
Hongze Cheng 已提交
422
    tsdbInitDFile(pDFile, did, vid, fid, ver, ftype);
H
Hongze Cheng 已提交
423 424 425
  }
}

H
Hongze Cheng 已提交
426
void tsdbInitDFileSetEx(SDFileSet *pSet, SDFileSet *pOSet) {
H
Hongze Cheng 已提交
427
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
428
    tsdbInitDFileEx(TSDB_DFILE_IN_SET(pSet, ftype), TSDB_DFILE_IN_SET(pOSet, ftype));
H
Hongze Cheng 已提交
429
  }
H
Hongze Cheng 已提交
430 431
}

H
Hongze Cheng 已提交
432 433
int tsdbEncodeDFileSet(void **buf, SDFileSet *pSet) {
  int tlen = 0;
H
Hongze Cheng 已提交
434

H
Hongze Cheng 已提交
435
  tlen += taosEncodeFixedI32(buf, pSet->fid);
H
Hongze Cheng 已提交
436 437
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    tlen += tsdbEncodeSDFile(buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
438
  }
H
Hongze Cheng 已提交
439

H
Hongze Cheng 已提交
440
  return tlen;
H
Hongze Cheng 已提交
441 442
}

H
Hongze Cheng 已提交
443
void *tsdbDecodeDFileSet(void *buf, SDFileSet *pSet) {
H
Hongze Cheng 已提交
444 445 446 447
  int32_t fid;

  buf = taosDecodeFixedI32(buf, &(fid));
  pSet->fid = fid;
H
Hongze Cheng 已提交
448
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
449
    buf = tsdbDecodeSDFile(buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
450
  }
H
Hongze Cheng 已提交
451
  return buf;
H
Hongze Cheng 已提交
452 453
}

H
Hongze Cheng 已提交
454
int tsdbApplyDFileSetChange(SDFileSet *from, SDFileSet *to) {
H
Hongze Cheng 已提交
455
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
456 457 458
    SDFile *pDFileFrom = (from) ? TSDB_DFILE_IN_SET(from, ftype) : NULL;
    SDFile *pDFileTo = (to) ? TSDB_DFILE_IN_SET(to, ftype) : NULL;
    if (tsdbApplyDFileChange(pDFileFrom, pDFileTo) < 0) {
H
Hongze Cheng 已提交
459 460 461 462 463 464 465
      return -1;
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
466
int tsdbCreateDFileSet(SDFileSet *pSet, bool updateHeader) {
H
Hongze Cheng 已提交
467
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
468
    if (tsdbCreateDFile(TSDB_DFILE_IN_SET(pSet, ftype), updateHeader) < 0) {
H
Hongze Cheng 已提交
469 470 471 472 473 474
      tsdbCloseDFileSet(pSet);
      tsdbRemoveDFileSet(pSet);
      return -1;
    }
  }

H
Hongze Cheng 已提交
475 476
  return 0;
}
H
Hongze Cheng 已提交
477

H
Hongze Cheng 已提交
478
int tsdbUpdateDFileSetHeader(SDFileSet *pSet) {
H
Hongze Cheng 已提交
479
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
480
    if (tsdbUpdateDFileHeader(TSDB_DFILE_IN_SET(pSet, ftype)) < 0) {
H
Hongze Cheng 已提交
481 482 483
      return -1;
    }
  }
H
Hongze Cheng 已提交
484
  return 0;
H
refact  
Hongze Cheng 已提交
485 486
}

H
Hongze Cheng 已提交
487 488 489 490 491 492 493 494 495
int tsdbScanAndTryFixDFileSet(SDFileSet *pSet) {
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    if (tsdbScanAndTryFixDFile(TSDB_DFILE_IN_SET(pSet, ftype)) < 0) {
      return -1;
    }
  }
  return 0;
}

H
Hongze Cheng 已提交
496
static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, char *fname) {
H
refact  
Hongze Cheng 已提交
497 498 499 500
  ASSERT(ftype != TSDB_FILE_MAX);

  if (ftype < TSDB_FILE_MAX) {
    if (ver == 0) {
H
Hongze Cheng 已提交
501
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/data/v%df%d%s", vid, vid, fid, TSDB_FNAME_SUFFIX[ftype]);
H
refact  
Hongze Cheng 已提交
502
    } else {
H
Hongze Cheng 已提交
503
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/data/v%df%d%s-ver%" PRIu32, vid, vid, fid,
H
Hongze Cheng 已提交
504
               TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
505 506 507
    }
  } else {
    if (ver == 0) {
H
Hongze Cheng 已提交
508
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s", vid, TSDB_FNAME_SUFFIX[ftype]);
H
refact  
Hongze Cheng 已提交
509
    } else {
H
Hongze Cheng 已提交
510
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s-ver%" PRIu32, vid, TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
511 512
    }
  }
H
Hongze Cheng 已提交
513
}