tsdbFile.h 9.7 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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/>.
 */

#ifndef _TS_TSDB_FILE_H_
#define _TS_TSDB_FILE_H_

#ifdef __cplusplus
extern "C" {
#endif

#define TSDB_FILE_HEAD_SIZE 512
#define TSDB_FILE_DELIMITER 0xF00AFA0F
#define TSDB_FILE_INIT_MAGIC 0xFFFFFFFF
H
Hongze Cheng 已提交
26
#define TSDB_IVLD_FID INT_MIN
H
refact  
Hongze Cheng 已提交
27

H
refact  
Hongze Cheng 已提交
28 29 30
#define TSDB_FILE_INFO(tf) (&((tf)->info))
#define TSDB_FILE_F(tf) (&((tf)->f))
#define TSDB_FILE_FD(tf) ((tf)->fd)
H
Hongze Cheng 已提交
31 32
#define TSDB_FILE_FULL_NAME(tf) TFILE_NAME(TSDB_FILE_F(tf))
#define TSDB_FILE_OPENED(tf) (TSDB_FILE_FD(tf) >= 0)
H
Hongze Cheng 已提交
33
#define TSDB_FILE_CLOSED(tf) (!TSDB_FILE_OPENED(tf))
H
refact  
Hongze Cheng 已提交
34
#define TSDB_FILE_SET_CLOSED(f) (TSDB_FILE_FD(f) = -1)
H
Hongze Cheng 已提交
35 36
#define TSDB_FILE_LEVEL(tf) TFILE_LEVEL(TSDB_FILE_F(tf))
#define TSDB_FILE_ID(tf) TFILE_ID(TSDB_FILE_F(tf))
H
Hongze Cheng 已提交
37
#define TSDB_FILE_FSYNC(tf) fsync(TSDB_FILE_FD(tf))
H
refact  
Hongze Cheng 已提交
38

H
Hongze Cheng 已提交
39
typedef enum { TSDB_FILE_HEAD = 0, TSDB_FILE_DATA, TSDB_FILE_LAST, TSDB_FILE_MAX, TSDB_FILE_META } TSDB_FILE_T;
H
refact  
Hongze Cheng 已提交
40

H
refact  
Hongze Cheng 已提交
41
// =============== SMFile
H
refact  
Hongze Cheng 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55
typedef struct {
  int64_t  size;
  int64_t  tombSize;
  int64_t  nRecords;
  int64_t  nDels;
  uint32_t magic;
} SMFInfo;

typedef struct {
  SMFInfo info;
  TFILE   f;
  int     fd;
} SMFile;

H
Hongze Cheng 已提交
56
void  tsdbInitMFile(SMFile* pMFile, SDiskID did, int vid, uint32_t ver);
H
Hongze Cheng 已提交
57
void  tsdbInitMFileEx(SMFile* pMFile, SMFile* pOMFile);
H
refact  
Hongze Cheng 已提交
58 59
int   tsdbEncodeSMFile(void** buf, SMFile* pMFile);
void* tsdbDecodeSMFile(void* buf, SMFile* pMFile);
H
Hongze Cheng 已提交
60
int   tsdbApplyMFileChange(SMFile* from, SMFile* to);
H
Hongze Cheng 已提交
61 62 63
int   tsdbCreateMFile(SMFile* pMFile);
int   tsdbUpdateMFileHeader(SMFile* pMFile);

H
Hongze Cheng 已提交
64
static FORCE_INLINE void tsdbSetMFileInfo(SMFile* pMFile, SMFInfo* pInfo) { pMFile->info = *pInfo; }
H
refact  
Hongze Cheng 已提交
65

H
refact  
Hongze Cheng 已提交
66
static FORCE_INLINE int tsdbOpenMFile(SMFile* pMFile, int flags) {
H
Hongze Cheng 已提交
67
  ASSERT(TSDB_FILE_CLOSED(pMFile));
H
refact  
Hongze Cheng 已提交
68 69 70 71 72 73 74 75 76 77

  pMFile->fd = open(TSDB_FILE_FULL_NAME(pMFile), flags);
  if (pMFile->fd < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return 0;
}

H
refact  
Hongze Cheng 已提交
78
static FORCE_INLINE void tsdbCloseMFile(SMFile* pMFile) {
H
refact  
Hongze Cheng 已提交
79 80 81 82 83 84
  if (TSDB_FILE_OPENED(pMFile)) {
    close(pMFile->fd);
    TSDB_FILE_SET_CLOSED(pMFile);
  }
}

H
refact  
Hongze Cheng 已提交
85
static FORCE_INLINE int64_t tsdbSeekMFile(SMFile* pMFile, int64_t offset, int whence) {
H
refact  
Hongze Cheng 已提交
86 87 88 89 90 91 92 93 94 95
  ASSERT(TSDB_FILE_OPENED(pMFile));

  int64_t loffset = taosLSeek(TSDB_FILE_FD(pMFile), offset, whence);
  if (loffset < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return loffset;
}
H
refact  
Hongze Cheng 已提交
96

H
refact  
Hongze Cheng 已提交
97
static FORCE_INLINE int64_t tsdbWriteMFile(SMFile* pMFile, void* buf, int64_t nbyte) {
H
refact  
Hongze Cheng 已提交
98 99 100 101 102 103 104 105 106 107 108
  ASSERT(TSDB_FILE_OPENED(pMFile));

  int64_t nwrite = taosWrite(pMFile->fd, buf, nbyte);
  if (nwrite < nbyte) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return nwrite;
}

H
refact  
Hongze Cheng 已提交
109
static FORCE_INLINE void tsdbUpdateMFileMagic(SMFile* pMFile, void* pCksum) {
H
refact  
Hongze Cheng 已提交
110 111 112
  pMFile->info.magic = taosCalcChecksum(pMFile->info.magic, (uint8_t*)(pCksum), sizeof(TSCKSUM));
}

H
Hongze Cheng 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
static FORCE_INLINE int tsdbAppendMFile(SMFile* pMFile, void* buf, int64_t nbyte, int64_t* offset) {
  ASSERT(TSDB_FILE_OPENED(pMFile));

  int64_t toffset;

  if ((toffset = tsdbSeekMFile(pMFile, 0, SEEK_END)) < 0) {
    return -1;
  }

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

  if (offset) {
    *offset = toffset;
  }

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

  pMFile->info.size += nbyte;

  return 0;
}

static FORCE_INLINE int tsdbRemoveMFile(SMFile* pMFile) { return tfsremove(TSDB_FILE_F(pMFile)); }

static FORCE_INLINE int64_t tsdbReadMFile(SMFile* pMFile, void* buf, int64_t nbyte) {
  ASSERT(TSDB_FILE_OPENED(pMFile));

  int64_t nread = taosRead(pMFile->fd, buf, nbyte);
  if (nread < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return nread;
}
H
refact  
Hongze Cheng 已提交
150 151

// =============== SDFile
H
refact  
Hongze Cheng 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
typedef struct {
  uint32_t magic;
  uint32_t len;
  uint32_t totalBlocks;
  uint32_t totalSubBlocks;
  uint32_t offset;
  uint64_t size;
  uint64_t tombSize;
} SDFInfo;

typedef struct {
  SDFInfo info;
  TFILE   f;
  int     fd;
} SDFile;

H
Hongze Cheng 已提交
168 169
void  tsdbInitDFile(SDFile* pDFile, SDiskID did, int vid, int fid, uint32_t ver, TSDB_FILE_T ftype);
void  tsdbInitDFileEx(SDFile* pDFile, SDFile* pODFile);
H
refact  
Hongze Cheng 已提交
170 171
int   tsdbEncodeSDFile(void** buf, SDFile* pDFile);
void* tsdbDecodeSDFile(void* buf, SDFile* pDFile);
H
Hongze Cheng 已提交
172 173 174 175
int   tsdbCreateDFile(SDFile* pDFile);
int   tsdbUpdateDFileHeader(SDFile* pDFile);

static FORCE_INLINE void tsdbSetDFileInfo(SDFile* pDFile, SDFInfo* pInfo) { pDFile->info = *pInfo; }
H
refact  
Hongze Cheng 已提交
176

H
Hongze Cheng 已提交
177
static FORCE_INLINE int tsdbOpenDFile(SDFile* pDFile, int flags) {
H
refact  
Hongze Cheng 已提交
178 179
  ASSERT(!TSDB_FILE_OPENED(pDFile));

H
Hongze Cheng 已提交
180
  pDFile->fd = open(TSDB_FILE_FULL_NAME(pDFile), flags);
H
refact  
Hongze Cheng 已提交
181 182 183 184 185 186 187 188
  if (pDFile->fd < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return 0;
}

H
refact  
Hongze Cheng 已提交
189
static FORCE_INLINE void tsdbCloseDFile(SDFile* pDFile) {
H
refact  
Hongze Cheng 已提交
190 191 192 193 194 195
  if (TSDB_FILE_OPENED(pDFile)) {
    close(pDFile->fd);
    TSDB_FILE_SET_CLOSED(pDFile);
  }
}

H
Hongze Cheng 已提交
196
static FORCE_INLINE int64_t tsdbSeekDFile(SDFile* pDFile, int64_t offset, int whence) {
H
refact  
Hongze Cheng 已提交
197 198
  ASSERT(TSDB_FILE_OPENED(pDFile));

H
Hongze Cheng 已提交
199
  int64_t loffset = taosLSeek(TSDB_FILE_FD(pDFile), offset, whence);
H
refact  
Hongze Cheng 已提交
200 201 202 203 204 205 206 207
  if (loffset < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return loffset;
}

H
refact  
Hongze Cheng 已提交
208
static FORCE_INLINE int64_t tsdbWriteDFile(SDFile* pDFile, void* buf, int64_t nbyte) {
H
refact  
Hongze Cheng 已提交
209 210 211 212 213 214 215 216 217 218 219
  ASSERT(TSDB_FILE_OPENED(pDFile));

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

  return nwrite;
}

H
Hongze Cheng 已提交
220 221 222 223 224
static FORCE_INLINE void tsdbUpdateDFileMagic(SDFile* pDFile, void* pCksm) {
  pDFile->info.magic = taosCalcChecksum(pDFile->info.magic, (uint8_t*)(pCksm), sizeof(TSCKSUM));
}

static FORCE_INLINE int tsdbAppendDFile(SDFile* pDFile, void* buf, int64_t nbyte, int64_t* offset) {
H
refact  
Hongze Cheng 已提交
225 226
  ASSERT(TSDB_FILE_OPENED(pDFile));

H
Hongze Cheng 已提交
227
  int64_t toffset;
H
refact  
Hongze Cheng 已提交
228

H
Hongze Cheng 已提交
229 230 231
  if ((toffset = tsdbSeekDFile(pDFile, 0, SEEK_END)) < 0) {
    return -1;
  }
H
refact  
Hongze Cheng 已提交
232

H
Hongze Cheng 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245
  ASSERT(pDFile->info.size == toffset);

  if (offset) {
    *offset = toffset;
  }

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

  pDFile->info.size += nbyte;

  return 0;
H
refact  
Hongze Cheng 已提交
246 247
}

H
Hongze Cheng 已提交
248 249
static FORCE_INLINE int tsdbRemoveDFile(SDFile* pDFile) { return tfsremove(TSDB_FILE_F(pDFile)); }

H
refact  
Hongze Cheng 已提交
250
static FORCE_INLINE int64_t tsdbReadDFile(SDFile* pDFile, void* buf, int64_t nbyte) {
H
refact  
Hongze Cheng 已提交
251 252 253 254 255 256 257 258 259 260 261
  ASSERT(TSDB_FILE_OPENED(pDFile));

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

  return nread;
}

H
Hongze Cheng 已提交
262 263 264
static FORCE_INLINE int tsdbCopyDFile(SDFile* pSrc, SDFile* pDest) {
  if (tfscopy(TSDB_FILE_F(pSrc), TSDB_FILE_F(pDest)) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
265 266 267
    return -1;
  }

H
Hongze Cheng 已提交
268
  tsdbSetDFileInfo(pDest, TSDB_FILE_INFO(pSrc));
H
Hongze Cheng 已提交
269 270 271
  return 0;
}

H
refact  
Hongze Cheng 已提交
272
// =============== SDFileSet
H
refact  
Hongze Cheng 已提交
273 274 275 276 277 278
typedef struct {
  int    fid;
  int    state;
  SDFile files[TSDB_FILE_MAX];
} SDFileSet;

H
refact  
Hongze Cheng 已提交
279
#define TSDB_FSET_FID(s) ((s)->fid)
H
refact  
Hongze Cheng 已提交
280
#define TSDB_DFILE_IN_SET(s, t) ((s)->files + (t))
H
Hongze Cheng 已提交
281 282
#define TSDB_FSET_LEVEL(s) TSDB_FILE_LEVEL(TSDB_DFILE_IN_SET(s, 0))
#define TSDB_FSET_ID(s) TSDB_FILE_ID(TSDB_DFILE_IN_SET(s, 0))
H
Hongze Cheng 已提交
283 284 285 286 287 288
#define TSDB_FSET_SET_CLOSED(s)                                                \
  do {                                                                         \
    for (TSDB_FILE_T ftype = TSDB_FILE_HEAD; ftype < TSDB_FILE_MAX; ftype++) { \
      TSDB_FILE_SET_CLOSED(TSDB_DFILE_IN_SET(s, ftype));                       \
    }                                                                          \
  } while (0);
H
Hongze Cheng 已提交
289 290 291 292 293 294
#define TSDB_FSET_FSYNC(s)                                                     \
  do {                                                                         \
    for (TSDB_FILE_T ftype = TSDB_FILE_HEAD; ftype < TSDB_FILE_MAX; ftype++) { \
      TSDB_FILE_FSYNC(TSDB_DFILE_IN_SET(s, ftype));                            \
    }                                                                          \
  } while (0);
H
refact  
Hongze Cheng 已提交
295

H
Hongze Cheng 已提交
296
void  tsdbInitDFileSet(SDFileSet* pSet, SDiskID did, int vid, int fid, uint32_t ver);
H
Hongze Cheng 已提交
297 298 299
void  tsdbInitDFileSetEx(SDFileSet* pSet, SDFileSet* pOSet);
int   tsdbEncodeDFileSet(void** buf, SDFileSet* pSet);
void* tsdbDecodeDFileSet(void* buf, SDFileSet* pSet);
H
Hongze Cheng 已提交
300
int   tsdbApplyDFileSetChange(SDFileSet* from, SDFileSet* to);
H
Hongze Cheng 已提交
301 302
int   tsdbCreateDFileSet(SDFileSet* pSet);
int   tsdbUpdateDFileSetHeader(SDFileSet* pSet);
H
Hongze Cheng 已提交
303 304 305 306 307 308 309 310 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

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

static FORCE_INLINE 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;
}

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

static FORCE_INLINE 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;
}
H
refact  
Hongze Cheng 已提交
336

H
refact  
Hongze Cheng 已提交
337 338 339 340 341
static FORCE_INLINE void tsdbGetFidKeyRange(int days, int8_t precision, int fid, TSKEY* minKey, TSKEY* maxKey) {
  *minKey = fid * days * tsMsPerDay[precision];
  *maxKey = *minKey + days * tsMsPerDay[precision] - 1;
}

H
refact  
Hongze Cheng 已提交
342 343 344 345 346
#ifdef __cplusplus
}
#endif

#endif /* _TS_TSDB_FILE_H_ */