tsdbFile.c 16.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
Hongze Cheng 已提交
16
#include "tsdb.h"
H
TD-353  
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static const char *tsdbFileSuffix[] = {".tombstone", ".cache", ".index", ".data", ".last", ".sma", ""};

// .tombstone

struct STsdbIndexFile {
  int64_t size;
  int64_t offset;
  int32_t nRef;
};

struct STsdbDataFile {
  int64_t size;
  int32_t nRef;
};

struct STsdbLastFile {
  int64_t size;
  int32_t nRef;
};

struct STsdbSmaFile {
  int64_t size;
  int32_t nRef;
};

struct SDFileSet {
  STsdbIndexFile *pIndexF;
  STsdbDataFile  *pDataF;
  STsdbLastFile  *pLastF;
  STsdbSmaFile   *pSmaF;
};

#if 0
H
refact  
Hongze Cheng 已提交
51
static const char *TSDB_FNAME_SUFFIX[] = {
H
Hongze Cheng 已提交
52 53 54
    "head",  // TSDB_FILE_HEAD
    "data",  // TSDB_FILE_DATA
    "last",  // TSDB_FILE_LAST
55 56
    "smad",  // TSDB_FILE_SMAD
    "smal",  // TSDB_FILE_SMAL
H
Hongze Cheng 已提交
57 58
    "",      // TSDB_FILE_MAX
    "meta",  // TSDB_FILE_META
C
Cary Xu 已提交
59 60
};

H
refact  
Hongze Cheng 已提交
61
static void  tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, const char *dname, char *fname);
H
Hongze Cheng 已提交
62 63 64 65
static int   tsdbEncodeDFInfo(void **buf, SDFInfo *pInfo);
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo);
static int   tsdbRollBackDFile(SDFile *pDFile);

H
Hongze Cheng 已提交
66
// ============== Operations on SDFile
S
Shengliang Guan 已提交
67
void tsdbInitDFile(STsdb *pRepo, SDFile *pDFile, SDiskID did, int fid, uint32_t ver, TSDB_FILE_T ftype) {
H
refact  
Hongze Cheng 已提交
68 69
  char fname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
70 71
  TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_OK);

H
Hongze Cheng 已提交
72
  TSDB_FILE_SET_CLOSED(pDFile);
H
refact  
Hongze Cheng 已提交
73

H
Hongze Cheng 已提交
74 75
  memset(&(pDFile->info), 0, sizeof(pDFile->info));
  pDFile->info.magic = TSDB_FILE_INIT_MAGIC;
76
  pDFile->info.fver = tsdbGetDFSVersion(ftype);
H
refact  
Hongze Cheng 已提交
77

78
  tsdbGetFilename(REPO_ID(pRepo), fid, ver, ftype, pRepo->dir, fname);
H
Hongze Cheng 已提交
79
  tfsInitFile(REPO_TFS(pRepo), &(pDFile->f), did, fname);
H
Hongze Cheng 已提交
80 81
}

H
Hongze Cheng 已提交
82 83
void tsdbInitDFileEx(SDFile *pDFile, SDFile *pODFile) {
  *pDFile = *pODFile;
H
Hongze Cheng 已提交
84 85 86
  TSDB_FILE_SET_CLOSED(pDFile);
}

H
refact  
Hongze Cheng 已提交
87
int tsdbEncodeSDFile(void **buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
88
  int tlen = 0;
89

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

H
Hongze Cheng 已提交
93
  return tlen;
H
TD-34  
hzcheng 已提交
94 95
}

S
Shengliang Guan 已提交
96
void *tsdbDecodeSDFile(STsdb *pRepo, void *buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
97
  buf = tsdbDecodeDFInfo(buf, &(pDFile->info));
H
Hongze Cheng 已提交
98
  buf = tfsDecodeFile(REPO_TFS(pRepo), buf, &(pDFile->f));
H
Hongze Cheng 已提交
99
  TSDB_FILE_SET_CLOSED(pDFile);
H
TD-353  
Hongze Cheng 已提交
100

H
Hongze Cheng 已提交
101
  return buf;
H
TD-353  
Hongze Cheng 已提交
102
}
H
TD-353  
Hongze Cheng 已提交
103

H
Hongze Cheng 已提交
104 105 106 107 108 109 110 111 112 113
static int tsdbEncodeSDFileEx(void **buf, SDFile *pDFile) {
  int tlen = 0;

  tlen += tsdbEncodeDFInfo(buf, &(pDFile->info));
  tlen += taosEncodeString(buf, TSDB_FILE_FULL_NAME(pDFile));

  return tlen;
}

static void *tsdbDecodeSDFileEx(void *buf, SDFile *pDFile) {
114
  char *aname = NULL;
H
Hongze Cheng 已提交
115 116 117 118 119

  buf = tsdbDecodeDFInfo(buf, &(pDFile->info));
  buf = taosDecodeString(buf, &aname);
  strncpy(TSDB_FILE_FULL_NAME(pDFile), aname, TSDB_FILENAME_LEN);
  TSDB_FILE_SET_CLOSED(pDFile);
wafwerar's avatar
wafwerar 已提交
120
  taosMemoryFreeClear(aname);
H
Hongze Cheng 已提交
121 122 123 124

  return buf;
}

125
int tsdbCreateDFile(STsdb *pRepo, SDFile *pDFile, bool updateHeader, TSDB_FILE_T fType) {
H
Hongze Cheng 已提交
126 127
  ASSERT(pDFile->info.size == 0 && pDFile->info.magic == TSDB_FILE_INIT_MAGIC);

128
  pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
129
  if (pDFile->pFile == NULL) {
H
Hongze Cheng 已提交
130 131
    if (errno == ENOENT) {
      // Try to create directory recursively
S
Shengliang Guan 已提交
132
      char *s = strdup(TSDB_FILE_REL_NAME(pDFile));
H
Hongze Cheng 已提交
133
      if (tfsMkdirRecurAt(REPO_TFS(pRepo), taosDirName(s), TSDB_FILE_DID(pDFile)) < 0) {
wafwerar's avatar
wafwerar 已提交
134
        taosMemoryFreeClear(s);
H
Hongze Cheng 已提交
135 136
        return -1;
      }
wafwerar's avatar
wafwerar 已提交
137
      taosMemoryFreeClear(s);
H
Hongze Cheng 已提交
138

139
      pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
140
      if (pDFile->pFile == NULL) {
H
Hongze Cheng 已提交
141 142 143 144 145 146 147
        terrno = TAOS_SYSTEM_ERROR(errno);
        return -1;
      }
    } else {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
H
Hongze Cheng 已提交
148 149
  }

H
Hongze Cheng 已提交
150 151 152 153
  if (!updateHeader) {
    return 0;
  }

H
Hongze Cheng 已提交
154
  pDFile->info.size += TSDB_FILE_HEAD_SIZE;
155
  pDFile->info.fver = tsdbGetDFSVersion(fType);
H
Hongze Cheng 已提交
156

H
Hongze Cheng 已提交
157
  if (tsdbUpdateDFileHeader(pDFile) < 0) {
H
Hongze Cheng 已提交
158 159 160 161 162
    tsdbCloseDFile(pDFile);
    tsdbRemoveDFile(pDFile);
    return -1;
  }

H
Hongze Cheng 已提交
163 164 165
  return 0;
}

H
Hongze Cheng 已提交
166 167 168 169 170 171
int tsdbUpdateDFileHeader(SDFile *pDFile) {
  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

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

H
Hongze Cheng 已提交
173
  void *ptr = buf;
C
Cary Xu 已提交
174
  // taosEncodeFixedU32(&ptr, 0); // fver moved to SDFInfo and saved to current
H
Hongze Cheng 已提交
175
  tsdbEncodeDFInfo(&ptr, &(pDFile->info));
H
Hongze Cheng 已提交
176

H
refact  
Hongze Cheng 已提交
177
  taosCalcChecksumAppend(0, (uint8_t *)buf, TSDB_FILE_HEAD_SIZE);
H
Hongze Cheng 已提交
178
  if (tsdbWriteDFile(pDFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
H
Hongze Cheng 已提交
179 180
    return -1;
  }
H
Hongze Cheng 已提交
181 182

  return 0;
H
Hongze Cheng 已提交
183 184
}

H
Hongze Cheng 已提交
185
int tsdbLoadDFileHeader(SDFile *pDFile, SDFInfo *pInfo) {
H
Hongze Cheng 已提交
186
  char     buf[TSDB_FILE_HEAD_SIZE] = "\0";
187
  uint32_t _version;
H
Hongze Cheng 已提交
188 189 190 191 192 193 194 195 196 197 198

  ASSERT(TSDB_FILE_OPENED(pDFile));

  if (tsdbSeekDFile(pDFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  if (tsdbReadDFile(pDFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

H
refact  
Hongze Cheng 已提交
199 200 201 202 203
  if (!taosCheckChecksumWhole((uint8_t *)buf, TSDB_FILE_HEAD_SIZE)) {
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
    return -1;
  }

H
Hongze Cheng 已提交
204
  void *pBuf = buf;
C
Cary Xu 已提交
205
  // pBuf = taosDecodeFixedU32(pBuf, &_version);
H
Hongze Cheng 已提交
206
  pBuf = tsdbDecodeDFInfo(pBuf, pInfo);
H
Hongze Cheng 已提交
207 208 209
  return 0;
}

H
Hongze Cheng 已提交
210
static int tsdbScanAndTryFixDFile(STsdb *pRepo, SDFile *pDFile) {
H
Hongze Cheng 已提交
211
  SDFile df;
H
refact  
Hongze Cheng 已提交
212 213

  tsdbInitDFileEx(&df, pDFile);
H
Hongze Cheng 已提交
214

215
  if (!taosCheckExistFile(TSDB_FILE_FULL_NAME(pDFile))) {
S
Shengliang Guan 已提交
216
    tsdbError("vgId:%d, data file %s not exit, report to upper layer to fix it", REPO_ID(pRepo),
H
Hongze Cheng 已提交
217
              TSDB_FILE_FULL_NAME(pDFile));
H
Hongze Cheng 已提交
218
    // pRepo->state |= TSDB_STATE_BAD_DATA;
H
Hongze Cheng 已提交
219
    TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
220 221
    return 0;
  }
222 223
  int64_t file_size = 0;
  if (taosStatFile(TSDB_FILE_FULL_NAME(&df), &file_size, NULL) < 0) {
H
Hongze Cheng 已提交
224 225 226 227
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

228
  if (pDFile->info.size < file_size) {
229 230
    // if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
    if (tsdbOpenDFile(&df, TD_FILE_WRITE) < 0) {
H
Hongze Cheng 已提交
231 232 233
      return -1;
    }

234
    if (taosFtruncateFile(df.pFile, df.info.size) < 0) {
H
Hongze Cheng 已提交
235 236 237 238 239 240 241 242 243 244 245
      terrno = TAOS_SYSTEM_ERROR(errno);
      tsdbCloseDFile(&df);
      return -1;
    }

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

    tsdbCloseDFile(&df);
S
Shengliang Guan 已提交
246
    tsdbInfo("vgId:%d, file %s is truncated from %" PRId64 " to %" PRId64, REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile),
247 248
             file_size, pDFile->info.size);
  } else if (pDFile->info.size > file_size) {
S
Shengliang Guan 已提交
249
    tsdbError("vgId:%d, data file %s has wrong size %" PRId64 " expected %" PRId64 ", report to upper layer to fix it",
250
              REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile), file_size, pDFile->info.size);
H
Hongze Cheng 已提交
251
    // pRepo->state |= TSDB_STATE_BAD_DATA;
H
Hongze Cheng 已提交
252
    TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
253
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
H
Hongze Cheng 已提交
254
    return 0;
H
refact  
Hongze Cheng 已提交
255
  } else {
S
Shengliang Guan 已提交
256
    tsdbDebug("vgId:%d, file %s passes the scan", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile));
H
Hongze Cheng 已提交
257 258 259 260 261
  }

  return 0;
}

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

H
Hongze Cheng 已提交
265
  tlen += taosEncodeFixedU32(buf, pInfo->magic);
266
  tlen += taosEncodeFixedU32(buf, pInfo->fver);
H
TD-353  
Hongze Cheng 已提交
267 268 269
  tlen += taosEncodeFixedU32(buf, pInfo->len);
  tlen += taosEncodeFixedU32(buf, pInfo->totalBlocks);
  tlen += taosEncodeFixedU32(buf, pInfo->totalSubBlocks);
H
Hongze Cheng 已提交
270 271 272
  tlen += taosEncodeFixedU32(buf, pInfo->offset);
  tlen += taosEncodeFixedU64(buf, pInfo->size);
  tlen += taosEncodeFixedU64(buf, pInfo->tombSize);
H
TD-353  
Hongze Cheng 已提交
273

H
TD-353  
Hongze Cheng 已提交
274
  return tlen;
H
TD-353  
Hongze Cheng 已提交
275 276
}

H
Hongze Cheng 已提交
277
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo) {
H
Hongze Cheng 已提交
278
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));
279
  buf = taosDecodeFixedU32(buf, &(pInfo->fver));
H
TD-353  
Hongze Cheng 已提交
280 281 282
  buf = taosDecodeFixedU32(buf, &(pInfo->len));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalBlocks));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalSubBlocks));
H
Hongze Cheng 已提交
283 284 285
  buf = taosDecodeFixedU32(buf, &(pInfo->offset));
  buf = taosDecodeFixedU64(buf, &(pInfo->size));
  buf = taosDecodeFixedU64(buf, &(pInfo->tombSize));
H
TD-353  
Hongze Cheng 已提交
286 287 288 289

  return buf;
}

H
Hongze Cheng 已提交
290 291 292 293 294 295 296 297 298
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 已提交
299
          tsdbRollBackDFile(to);
H
Hongze Cheng 已提交
300 301
        }
      } else {
302
        (void)tsdbRemoveDFile(from);
H
Hongze Cheng 已提交
303 304 305 306 307 308 309
      }
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
310
static int tsdbRollBackDFile(SDFile *pDFile) {
H
Hongze Cheng 已提交
311 312
  SDFile df = *pDFile;

313 314
  // if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
  if (tsdbOpenDFile(&df, TD_FILE_WRITE) < 0) {
H
Hongze Cheng 已提交
315 316 317
    return -1;
  }

318
  if (taosFtruncateFile(TSDB_FILE_PFILE(&df), pDFile->info.size) < 0) {
H
Hongze Cheng 已提交
319 320 321 322 323 324 325 326 327 328
    terrno = TAOS_SYSTEM_ERROR(errno);
    tsdbCloseDFile(&df);
    return -1;
  }

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

H
Hongze Cheng 已提交
329 330
  TSDB_FILE_FSYNC(&df);

H
Hongze Cheng 已提交
331 332 333 334
  tsdbCloseDFile(&df);
  return 0;
}

H
Hongze Cheng 已提交
335
// ============== Operations on SDFileSet
S
Shengliang Guan 已提交
336
void tsdbInitDFileSet(STsdb *pRepo, SDFileSet *pSet, SDiskID did, int fid, uint32_t ver) {
337 338 339 340
  TSDB_FSET_FID(pSet) = fid;
  TSDB_FSET_VER(pSet) = TSDB_LATEST_FSET_VER;
  TSDB_FSET_STATE(pSet) = 0;
  pSet->reserve = 0;
H
Hongze Cheng 已提交
341

H
Hongze Cheng 已提交
342 343
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    SDFile *pDFile = TSDB_DFILE_IN_SET(pSet, ftype);
S
Shengliang Guan 已提交
344
    tsdbInitDFile(pRepo, pDFile, did, fid, ver, ftype);
H
Hongze Cheng 已提交
345 346 347
  }
}

H
Hongze Cheng 已提交
348
void tsdbInitDFileSetEx(SDFileSet *pSet, SDFileSet *pOSet) {
349
  TSDB_FSET_FID(pSet) = TSDB_FSET_FID(pOSet);
H
Hongze Cheng 已提交
350
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
351
    tsdbInitDFileEx(TSDB_DFILE_IN_SET(pSet, ftype), TSDB_DFILE_IN_SET(pOSet, ftype));
H
Hongze Cheng 已提交
352
  }
H
Hongze Cheng 已提交
353 354
}

H
Hongze Cheng 已提交
355 356
int tsdbEncodeDFileSet(void **buf, SDFileSet *pSet) {
  int tlen = 0;
H
Hongze Cheng 已提交
357

358 359 360 361
  tlen += taosEncodeFixedI32(buf, TSDB_FSET_FID(pSet));
  // state not included
  tlen += taosEncodeFixedU8(buf, TSDB_FSET_VER(pSet));
  tlen += taosEncodeFixedU16(buf, pSet->reserve);
H
Hongze Cheng 已提交
362 363
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    tlen += tsdbEncodeSDFile(buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
364
  }
H
Hongze Cheng 已提交
365

H
Hongze Cheng 已提交
366
  return tlen;
H
Hongze Cheng 已提交
367 368
}

S
Shengliang Guan 已提交
369
void *tsdbDecodeDFileSet(STsdb *pRepo, void *buf, SDFileSet *pSet) {
370 371 372 373
  buf = taosDecodeFixedI32(buf, &(TSDB_FSET_FID(pSet)));
  TSDB_FSET_STATE(pSet) = 0;
  buf = taosDecodeFixedU8(buf, &(TSDB_FSET_VER(pSet)));
  buf = taosDecodeFixedU16(buf, &(pSet->reserve));
H
Hongze Cheng 已提交
374

H
Hongze Cheng 已提交
375
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
S
Shengliang Guan 已提交
376
    buf = tsdbDecodeSDFile(pRepo, buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
377
  }
H
Hongze Cheng 已提交
378
  return buf;
H
Hongze Cheng 已提交
379 380
}

H
Hongze Cheng 已提交
381 382 383
int tsdbEncodeDFileSetEx(void **buf, SDFileSet *pSet) {
  int tlen = 0;

384 385 386
  tlen += taosEncodeFixedI32(buf, TSDB_FSET_FID(pSet));
  tlen += taosEncodeFixedU8(buf, TSDB_FSET_VER(pSet));
  tlen += taosEncodeFixedU16(buf, pSet->reserve);
H
Hongze Cheng 已提交
387 388 389 390 391 392 393 394
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    tlen += tsdbEncodeSDFileEx(buf, TSDB_DFILE_IN_SET(pSet, ftype));
  }

  return tlen;
}

void *tsdbDecodeDFileSetEx(void *buf, SDFileSet *pSet) {
395 396 397
  buf = taosDecodeFixedI32(buf, &(TSDB_FSET_FID(pSet)));
  buf = taosDecodeFixedU8(buf, &(TSDB_FSET_VER(pSet)));
  buf = taosDecodeFixedU16(buf, &(pSet->reserve));
H
Hongze Cheng 已提交
398 399 400 401 402 403 404

  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    buf = tsdbDecodeSDFileEx(buf, TSDB_DFILE_IN_SET(pSet, ftype));
  }
  return buf;
}

H
Hongze Cheng 已提交
405
int tsdbApplyDFileSetChange(SDFileSet *from, SDFileSet *to) {
H
Hongze Cheng 已提交
406
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
407 408 409
    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 已提交
410 411 412 413 414 415 416
      return -1;
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
417
int tsdbCreateDFileSet(STsdb *pRepo, SDFileSet *pSet, bool updateHeader) {
H
Hongze Cheng 已提交
418
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
419
    if (tsdbCreateDFile(pRepo, TSDB_DFILE_IN_SET(pSet, ftype), updateHeader, ftype) < 0) {
H
Hongze Cheng 已提交
420 421 422 423 424 425
      tsdbCloseDFileSet(pSet);
      tsdbRemoveDFileSet(pSet);
      return -1;
    }
  }

H
Hongze Cheng 已提交
426 427
  return 0;
}
H
Hongze Cheng 已提交
428

H
Hongze Cheng 已提交
429
int tsdbUpdateDFileSetHeader(SDFileSet *pSet) {
H
Hongze Cheng 已提交
430
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
431
    if (tsdbUpdateDFileHeader(TSDB_DFILE_IN_SET(pSet, ftype)) < 0) {
H
Hongze Cheng 已提交
432 433 434
      return -1;
    }
  }
H
Hongze Cheng 已提交
435
  return 0;
H
refact  
Hongze Cheng 已提交
436 437
}

H
Hongze Cheng 已提交
438
int tsdbScanAndTryFixDFileSet(STsdb *pRepo, SDFileSet *pSet) {
H
Hongze Cheng 已提交
439
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
440
    if (tsdbScanAndTryFixDFile(pRepo, TSDB_DFILE_IN_SET(pSet, ftype)) < 0) {
H
Hongze Cheng 已提交
441 442 443 444 445 446
      return -1;
    }
  }
  return 0;
}

447
int tsdbParseDFilename(const char *fname, int *vid, int *fid, TSDB_FILE_T *ftype, uint32_t *_version) {
H
Hongze Cheng 已提交
448
  char *p = NULL;
449
  *_version = 0;
H
Hongze Cheng 已提交
450 451
  *ftype = TSDB_FILE_MAX;

452
  sscanf(fname, "v%df%d.%m[a-z]-ver%" PRIu32, vid, fid, &p, _version);
H
Hongze Cheng 已提交
453 454 455 456 457 458 459
  for (TSDB_FILE_T i = 0; i < TSDB_FILE_MAX; i++) {
    if (strcmp(p, TSDB_FNAME_SUFFIX[i]) == 0) {
      *ftype = i;
      break;
    }
  }

wafwerar's avatar
wafwerar 已提交
460
  taosMemoryFreeClear(p);
H
Hongze Cheng 已提交
461 462 463
  return 0;
}

C
Cary Xu 已提交
464
static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, const char *dname, char *fname) {
H
refact  
Hongze Cheng 已提交
465 466 467 468
  ASSERT(ftype != TSDB_FILE_MAX);

  if (ftype < TSDB_FILE_MAX) {
    if (ver == 0) {
C
Cary Xu 已提交
469 470
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/%s/data/v%df%d.%s", vid, dname, vid, fid,
               TSDB_FNAME_SUFFIX[ftype]);
H
refact  
Hongze Cheng 已提交
471
    } else {
C
Cary Xu 已提交
472
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/%s/data/v%df%d.%s-ver%" PRIu32, vid, dname, vid, fid,
H
Hongze Cheng 已提交
473
               TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
474 475 476
    }
  } else {
    if (ver == 0) {
H
Hongze Cheng 已提交
477
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s", vid, TSDB_FNAME_SUFFIX[ftype]);
H
refact  
Hongze Cheng 已提交
478
    } else {
H
Hongze Cheng 已提交
479
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s-ver%" PRIu32, vid, TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
480 481
    }
  }
H
refact  
Hongze Cheng 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 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
}

int tsdbOpenDFile(SDFile *pDFile, int flags) {
  ASSERT(!TSDB_FILE_OPENED(pDFile));

  pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), flags);
  if (pDFile->pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return 0;
}

void tsdbCloseDFile(SDFile *pDFile) {
  if (TSDB_FILE_OPENED(pDFile)) {
    taosCloseFile(&pDFile->pFile);
    TSDB_FILE_SET_CLOSED(pDFile);
  }
}

int64_t tsdbSeekDFile(SDFile *pDFile, int64_t offset, int whence) {
  // ASSERT(TSDB_FILE_OPENED(pDFile));

  int64_t loffset = taosLSeekFile(TSDB_FILE_PFILE(pDFile), offset, whence);
  if (loffset < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return loffset;
}

int64_t tsdbWriteDFile(SDFile *pDFile, void *buf, int64_t nbyte) {
  ASSERT(TSDB_FILE_OPENED(pDFile));

  int64_t nwrite = taosWriteFile(pDFile->pFile, buf, nbyte);
  if (nwrite < nbyte) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return nwrite;
}

void tsdbUpdateDFileMagic(SDFile *pDFile, void *pCksm) {
  pDFile->info.magic = taosCalcChecksum(pDFile->info.magic, (uint8_t *)(pCksm), sizeof(TSCKSUM));
}

int tsdbAppendDFile(SDFile *pDFile, void *buf, int64_t nbyte, int64_t *offset) {
  ASSERT(TSDB_FILE_OPENED(pDFile));

  int64_t toffset;

  if ((toffset = tsdbSeekDFile(pDFile, 0, SEEK_END)) < 0) {
    return -1;
  }

  ASSERT(pDFile->info.size == toffset);

  if (offset) {
    *offset = toffset;
  }

  if (tsdbWriteDFile(pDFile, buf, nbyte) < 0) {
    return -1;
  }

  pDFile->info.size += nbyte;

  return (int)nbyte;
}

int tsdbRemoveDFile(SDFile *pDFile) { return tfsRemoveFile(TSDB_FILE_F(pDFile)); }

int64_t tsdbReadDFile(SDFile *pDFile, void *buf, int64_t nbyte) {
  ASSERT(TSDB_FILE_OPENED(pDFile));

  int64_t nread = taosReadFile(pDFile->pFile, buf, nbyte);
  if (nread < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return nread;
}

int tsdbCopyDFile(SDFile *pSrc, SDFile *pDest) {
  if (tfsCopyFile(TSDB_FILE_F(pSrc), TSDB_FILE_F(pDest)) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  pDest->info = pSrc->info;
  return 0;
H
Hongze Cheng 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
}

void tsdbCloseDFileSet(SDFileSet *pSet) {
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    tsdbCloseDFile(TSDB_DFILE_IN_SET(pSet, ftype));
  }
}

int tsdbOpenDFileSet(SDFileSet *pSet, int flags) {
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    if (tsdbOpenDFile(TSDB_DFILE_IN_SET(pSet, ftype), flags) < 0) {
      tsdbCloseDFileSet(pSet);
      return -1;
    }
  }
  return 0;
}

void tsdbRemoveDFileSet(SDFileSet *pSet) {
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    (void)tsdbRemoveDFile(TSDB_DFILE_IN_SET(pSet, ftype));
  }
}

int tsdbCopyDFileSet(SDFileSet *pSrc, SDFileSet *pDest) {
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    if (tsdbCopyDFile(TSDB_DFILE_IN_SET(pSrc, ftype), TSDB_DFILE_IN_SET(pDest, ftype)) < 0) {
      tsdbRemoveDFileSet(pDest);
      return -1;
    }
  }

  return 0;
}

void tsdbGetFidKeyRange(int days, int8_t precision, int fid, TSKEY *minKey, TSKEY *maxKey) {
  *minKey = fid * days * tsTickPerMin[precision];
  *maxKey = *minKey + days * tsTickPerMin[precision] - 1;
H
Hongze Cheng 已提交
615 616
}
#endif