tsdbFile.h 5.5 KB
Newer Older
H
more  
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
more  
Hongze Cheng 已提交
15 16 17
#if !defined(_TD_TSDB_FILE_H_)
#define _TD_TSDB_FILE_H_

H
more  
hzcheng 已提交
18
#include <stdint.h>
H
hzcheng 已提交
19

H
TD-34  
hzcheng 已提交
20
#include "dataformat.h"
H
hzcheng 已提交
21
#include "taosdef.h"
H
TD-34  
hzcheng 已提交
22
#include "tglobalcfg.h"
H
more  
Hongze Cheng 已提交
23

H
more  
hzcheng 已提交
24 25 26 27
#ifdef __cplusplus
extern "C" {
#endif

H
TD-34  
hzcheng 已提交
28
#define TSDB_FILE_HEAD_SIZE 512
H
TD-34  
hzcheng 已提交
29
#define TSDB_FILE_DELIMITER 0xF00AFA0F
H
TD-34  
hzcheng 已提交
30

H
TD-34  
hzcheng 已提交
31 32 33
#define tsdbGetKeyFileId(key, daysPerFile, precision) ((key) / tsMsPerDay[(precision)] / (daysPerFile))
#define tsdbGetMaxNumOfFiles(keep, daysPerFile) ((keep) / (daysPerFile) + 3)

H
Hongze Cheng 已提交
34
typedef enum {
H
TD-34  
hzcheng 已提交
35 36 37 38
  TSDB_FILE_TYPE_HEAD = 0,  // .head file type
  TSDB_FILE_TYPE_DATA,      // .data file type
  TSDB_FILE_TYPE_LAST,      // .last file type
  TSDB_FILE_TYPE_MAX
H
more  
Hongze Cheng 已提交
39 40
} TSDB_FILE_TYPE;

H
TD-34  
hzcheng 已提交
41 42
#define IS_VALID_TSDB_FILE_TYPE(type) ((type) >= TSDB_FILE_TYPE_HEAD && (type) < TSDB_FILE_TYPE_MAX)

H
more  
hzcheng 已提交
43
extern const char *tsdbFileSuffix[];
H
Hongze Cheng 已提交
44

H
more  
Hongze Cheng 已提交
45
typedef struct {
H
TD-34  
hzcheng 已提交
46 47
  int64_t size;      // total size of the file
  int64_t tombSize;  // unused file size
H
TD-34  
hzcheng 已提交
48 49
  int32_t totalBlocks;
  int32_t totalSubBlocks;
H
TD-34  
hzcheng 已提交
50 51 52 53 54 55
} SFileInfo;

typedef struct {
  int     fd;
  char    fname[128];
  SFileInfo info;
H
hzcheng 已提交
56
} SFile;
H
more  
Hongze Cheng 已提交
57

H
TD-34  
hzcheng 已提交
58 59
#define TSDB_IS_FILE_OPENED(f) ((f)->fd != -1)

H
hzcheng 已提交
60 61
typedef struct {
  int32_t fileId;
H
TD-34  
hzcheng 已提交
62
  SFile   files[TSDB_FILE_TYPE_MAX];
H
hzcheng 已提交
63 64 65 66
} SFileGroup;

// TSDB file handle
typedef struct {
H
TD-34  
hzcheng 已提交
67 68 69
  int maxFGroups;
  int numOfFGroups;

H
hzcheng 已提交
70 71
  SFileGroup fGroup[];
} STsdbFileH;
H
more  
Hongze Cheng 已提交
72

H
TD-34  
hzcheng 已提交
73 74 75 76 77
#define TSDB_MIN_FILE_ID(fh) (fh)->fGroup[0].fileId
#define TSDB_MAX_FILE_ID(fh) (fh)->fGroup[(fh)->numOfFGroups - 1].fileId

STsdbFileH *tsdbInitFileH(char *dataDir, int maxFiles);
void        tsdbCloseFileH(STsdbFileH *pFileH);
H
TD-34  
hzcheng 已提交
78
int         tsdbCreateFile(char *dataDir, int fileId, char *suffix, int maxTables, SFile *pFile, int writeHeader, int toClose);
H
TD-34  
hzcheng 已提交
79
int         tsdbCreateFGroup(STsdbFileH *pFileH, char *dataDir, int fid, int maxTables);
H
TD-34  
hzcheng 已提交
80
int         tsdbOpenFile(SFile *pFile, int oflag);
H
TD-34  
hzcheng 已提交
81
int         tsdbCloseFile(SFile *pFile); SFileGroup *tsdbOpenFilesForCommit(STsdbFileH *pFileH, int fid);
H
TD-34  
hzcheng 已提交
82 83 84 85
int         tsdbRemoveFileGroup(STsdbFileH *pFile, int fid);

typedef struct {
  int32_t len;
H
TD-34  
hzcheng 已提交
86 87 88 89 90 91
  int32_t offset;
  int32_t hasLast : 1;
  int32_t numOfSuperBlocks : 31;
  int32_t checksum;
  TSKEY   maxKey;
} SCompIdx; /* sizeof(SCompIdx) = 24 */
H
TD-34  
hzcheng 已提交
92

93
/**
H
TD-34  
hzcheng 已提交
94 95 96 97 98 99
 * if numOfSubBlocks == 0, then the SCompBlock is a sub-block
 * if numOfSubBlocks >= 1, then the SCompBlock is a super-block
 *    - if numOfSubBlocks == 1, then the SCompBlock refers to the data block, and offset/len refer to
 *      the data block offset and length
 *    - if numOfSubBlocks > 1, then the offset/len refer to the offset of the first sub-block in the
 *      binary
100 101 102 103 104 105 106 107 108 109 110 111 112 113
 */
typedef struct {
  int64_t last : 1;          // If the block in data file or last file
  int64_t offset : 63;       // Offset of data block or sub-block index depending on numOfSubBlocks
  int32_t algorithm : 8;     // Compression algorithm
  int32_t numOfPoints : 24;  // Number of total points
  int32_t sversion;          // Schema version
  int32_t len;               // Data block length or nothing
  int16_t numOfSubBlocks;    // Number of sub-blocks;
  int16_t numOfCols;
  TSKEY   keyFirst;
  TSKEY   keyLast;
} SCompBlock;

H
TD-34  
hzcheng 已提交
114 115 116
#define IS_SUPER_BLOCK(pBlock) ((pBlock)->numOfSubBlocks >= 1)
#define IS_SUB_BLOCK(pBlock) ((pBlock)->numOfSubBlocks == 0)

H
TD-34  
hzcheng 已提交
117 118 119 120 121 122
typedef struct {
  int32_t    delimiter;  // For recovery usage
  int32_t    checksum;   // TODO: decide if checksum logic in this file or make it one API
  int64_t    uid;
  SCompBlock blocks[];
} SCompInfo;
H
hzcheng 已提交
123

H
TD-34  
hzcheng 已提交
124
#define TSDB_COMPBLOCK_AT(pCompInfo, idx) ((pCompInfo)->blocks + (idx))
H
TD-34  
hzcheng 已提交
125 126 127 128 129 130 131 132 133
#define TSDB_COMPBLOCK_GET_START_AND_SIZE(pCompInfo, pCompBlock, size)\
do {\
  if (pCompBlock->numOfSubBlocks > 1) {\
    pCompBlock = pCompInfo->blocks + pCompBlock->offset;\
    size = pCompBlock->numOfSubBlocks;\
  } else {\
    size = 1;\
  }\
} while (0)
H
TD-34  
hzcheng 已提交
134

H
TD-34  
hzcheng 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
// TODO: take pre-calculation into account
typedef struct {
  int16_t colId;  // Column ID
  int16_t len;    // Column length
  int32_t type : 8;
  int32_t offset : 24;
} SCompCol;

// TODO: Take recover into account
typedef struct {
  int32_t  delimiter;  // For recovery usage
  int32_t  numOfCols;  // For recovery usage
  int64_t  uid;        // For recovery usage
  SCompCol cols[];
} SCompData;
H
TD-34  
hzcheng 已提交
150 151

int tsdbCopyBlockDataInFile(SFile *pOutFile, SFile *pInFile, SCompInfo *pCompInfo, int idx, int isLast, SDataCols *pCols);
H
TD-34  
hzcheng 已提交
152 153 154 155 156

int tsdbLoadCompIdx(SFileGroup *pGroup, void *buf, int maxTables);
int tsdbLoadCompBlocks(SFileGroup *pGroup, SCompIdx *pIdx, void *buf);
int tsdbLoadCompCols(SFile *pFile, SCompBlock *pBlock, void *buf);
int tsdbLoadColData(SFile *pFile, SCompCol *pCol, int64_t blockBaseOffset, void *buf);
H
TD-34  
hzcheng 已提交
157 158
int tsdbLoadDataBlock(SFile *pFile, SCompBlock *pStartBlock, int numOfBlocks, SDataCols *pCols, SCompData *pCompData);

H
TD-34  
hzcheng 已提交
159 160
SFileGroup *tsdbSearchFGroup(STsdbFileH *pFileH, int fid);

H
TD-34  
hzcheng 已提交
161
// TODO: need an API to merge all sub-block data into one
H
TD-34  
hzcheng 已提交
162

H
TD-34  
hzcheng 已提交
163
void tsdbGetKeyRangeOfFileId(int32_t daysPerFile, int8_t precision, int32_t fileId, TSKEY *minKey, TSKEY *maxKey);
H
more  
hzcheng 已提交
164 165 166 167
#ifdef __cplusplus
}
#endif

H
more  
Hongze Cheng 已提交
168
#endif  // _TD_TSDB_FILE_H_