tsdbMain.h 10.4 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
TD-34  
hzcheng 已提交
15 16
#ifndef _TD_TSDB_MAIN_H_
#define _TD_TSDB_MAIN_H_
H
more  
Hongze Cheng 已提交
17

S
TD-1057  
Shengliang Guan 已提交
18
#include "os.h"
H
TD-353  
Hongze Cheng 已提交
19 20
#include "hash.h"
#include "tcoding.h"
S
slguan 已提交
21
#include "tglobal.h"
H
TD-353  
Hongze Cheng 已提交
22
#include "tkvstore.h"
H
hzcheng 已提交
23
#include "tlist.h"
H
TD-353  
Hongze Cheng 已提交
24
#include "tlog.h"
B
Bomin Zhang 已提交
25
#include "tlockfree.h"
H
hzcheng 已提交
26
#include "tsdb.h"
H
TD-34  
hzcheng 已提交
27 28
#include "tskiplist.h"
#include "tutil.h"
H
Hongze Cheng 已提交
29
#include "tchecksum.h"
H
Hongze Cheng 已提交
30
#include "tfs.h"
H
more  
Hongze Cheng 已提交
31

H
more  
hzcheng 已提交
32 33 34 35
#ifdef __cplusplus
extern "C" {
#endif

H
refact  
Hongze Cheng 已提交
36 37 38
typedef struct STsdbRepo STsdbRepo;

// ================= tsdbLog.h
H
hzcheng 已提交
39

H
refact  
Hongze Cheng 已提交
40
// ================= OTHERS
H
TD-353  
Hongze Cheng 已提交
41

H
Hongze Cheng 已提交
42 43
#define TAOS_IN_RANGE(key, keyMin, keyLast) (((key) >= (keyMin)) && ((key) <= (keyMax)))

44 45 46 47
// NOTE: Any file format change must increase this version number by 1
//       Also, implement the convert function
#define TSDB_FILE_VERSION ((uint32_t)0)

H
TD-353  
Hongze Cheng 已提交
48
// Definitions
H
refact  
Hongze Cheng 已提交
49
// ================= tsdbMeta.c
H
refact  
Hongze Cheng 已提交
50

H
refact  
Hongze Cheng 已提交
51
// ================= tsdbBuffer.c
H
refact  
Hongze Cheng 已提交
52

H
TD-353  
Hongze Cheng 已提交
53
// ------------------ tsdbMemTable.c
H
Hongze Cheng 已提交
54
// ================= tsdbFile.c
H
Hongze Cheng 已提交
55 56
/* Statistic information of the TSDB file system.
 */
H
Hongze Cheng 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
// ================= tsdbStore.c
#define KVSTORE_FILE_VERSION ((uint32_t)0)

typedef int (*iterFunc)(void*, void* cont, int contLen);
typedef void (*afterFunc)(void*);

typedef struct {
  SMFile    f;
  SHashObj* map;
  iterFunc  iFunc;
  afterFunc aFunc;
  void*     appH;
} SKVStore;

#define KVSTORE_MAGIC(s) (s)->f.info.magic

int       tdCreateKVStore(char* fname);
int       tdDestroyKVStore(char* fname);
SKVStore* tdOpenKVStore(char* fname, iterFunc iFunc, afterFunc aFunc, void* appH);
void      tdCloseKVStore(SKVStore* pStore);
int       tdKVStoreStartCommit(SKVStore* pStore);
int       tdUpdateKVStoreRecord(SKVStore* pStore, uint64_t uid, void* cont, int contLen);
int       tdDropKVStoreRecord(SKVStore* pStore, uint64_t uid);
int       tdKVStoreEndCommit(SKVStore* pStore);
void      tsdbGetStoreInfo(char* fname, uint32_t* magic, int64_t* size);
H
Hongze Cheng 已提交
82

H
Hongze Cheng 已提交
83
// ================= 
H
Hongze Cheng 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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
// extern const char* tsdbFileSuffix[];

// minFid <= midFid <= maxFid
// typedef struct {
//   int minFid;  // >= minFid && < midFid, at level 2
//   int midFid;  // >= midFid && < maxFid, at level 1
//   int maxFid;  // >= maxFid, at level 0
// } SFidGroup;

// typedef enum {
//   TSDB_FILE_TYPE_HEAD = 0,
//   TSDB_FILE_TYPE_DATA,
//   TSDB_FILE_TYPE_LAST,
//   TSDB_FILE_TYPE_STAT,
//   TSDB_FILE_TYPE_NHEAD,
//   TSDB_FILE_TYPE_NDATA,
//   TSDB_FILE_TYPE_NLAST,
//   TSDB_FILE_TYPE_NSTAT
// } TSDB_FILE_TYPE;

// #ifndef TDINTERNAL
// #define TSDB_FILE_TYPE_MAX (TSDB_FILE_TYPE_LAST+1)
// #else
// #define TSDB_FILE_TYPE_MAX (TSDB_FILE_TYPE_STAT+1)
// #endif

// typedef struct {
//   uint32_t magic;
//   uint32_t len;
//   uint32_t totalBlocks;
//   uint32_t totalSubBlocks;
//   uint32_t offset;
//   uint64_t size;      // total size of the file
//   uint64_t tombSize;  // unused file size
// } STsdbFileInfo;

// typedef struct {
//   TFILE         file;
//   STsdbFileInfo info;
//   int           fd;
// } SFile;

// typedef struct {
//   int   fileId;
//   int   state; // 0 for health, 1 for problem
//   SFile files[TSDB_FILE_TYPE_MAX];
// } SFileGroup;

// typedef struct {
//   pthread_rwlock_t fhlock;

//   int         maxFGroups;
//   int         nFGroups;
//   SFileGroup* pFGroup;
// } STsdbFileH;

// typedef struct {
//   int         direction;
//   STsdbFileH* pFileH;
//   int         fileId;
//   int         index;
// } SFileGroupIter;

// #define TSDB_FILE_NAME(pFile) ((pFile)->file.aname)
H
Hongze Cheng 已提交
148
#define TSDB_KEY_FILEID(key, daysPerFile, precision) ((key) / tsMsPerDay[(precision)] / (daysPerFile))
H
Hongze Cheng 已提交
149 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
// #define TSDB_MAX_FILE(keep, daysPerFile) ((keep) / (daysPerFile) + 3)
// #define TSDB_MIN_FILE_ID(fh) (fh)->pFGroup[0].fileId
// #define TSDB_MAX_FILE_ID(fh) (fh)->pFGroup[(fh)->nFGroups - 1].fileId
// #define TSDB_IS_FILE_OPENED(f) ((f)->fd > 0)
// #define TSDB_FGROUP_ITER_FORWARD TSDB_ORDER_ASC
// #define TSDB_FGROUP_ITER_BACKWARD TSDB_ORDER_DESC

// STsdbFileH* tsdbNewFileH(STsdbCfg* pCfg);
// void        tsdbFreeFileH(STsdbFileH* pFileH);
// int         tsdbOpenFileH(STsdbRepo* pRepo);
// void        tsdbCloseFileH(STsdbRepo* pRepo, bool isRestart);
// SFileGroup *tsdbCreateFGroup(STsdbRepo *pRepo, int fid, int level);
// void        tsdbInitFileGroupIter(STsdbFileH* pFileH, SFileGroupIter* pIter, int direction);
// void        tsdbSeekFileGroupIter(SFileGroupIter* pIter, int fid);
// SFileGroup* tsdbGetFileGroupNext(SFileGroupIter* pIter);
// int         tsdbOpenFile(SFile* pFile, int oflag);
// void        tsdbCloseFile(SFile* pFile);
// int         tsdbCreateFile(SFile* pFile, STsdbRepo* pRepo, int fid, int type);
// SFileGroup* tsdbSearchFGroup(STsdbFileH* pFileH, int fid, int flags);
// int         tsdbGetFidLevel(int fid, SFidGroup fidg);
// void        tsdbRemoveFilesBeyondRetention(STsdbRepo* pRepo, SFidGroup* pFidGroup);
// int         tsdbUpdateFileHeader(SFile* pFile);
// int         tsdbEncodeSFileInfo(void** buf, const STsdbFileInfo* pInfo);
// void*       tsdbDecodeSFileInfo(void* buf, STsdbFileInfo* pInfo);
// void        tsdbRemoveFileGroup(STsdbRepo* pRepo, SFileGroup* pFGroup);
// int         tsdbLoadFileHeader(SFile* pFile, uint32_t* version);
// void        tsdbGetFileInfoImpl(char* fname, uint32_t* magic, int64_t* size);
// void        tsdbGetFidGroup(STsdbCfg* pCfg, SFidGroup* pFidGroup);
// int         tsdbApplyRetention(STsdbRepo* pRepo, SFidGroup *pFidGroup);
H
Hongze Cheng 已提交
178

H
refact  
Hongze Cheng 已提交
179
// ================= tsdbMain.c
H
Hongze Cheng 已提交
180

H
TD-353  
Hongze Cheng 已提交
181

H
Hongze Cheng 已提交
182
#include "tsdbReadImpl.h"
183

H
Hongze Cheng 已提交
184
#if 0
H
Hongze Cheng 已提交
185
// ================= tsdbRWHelper.c
H
TD-34  
hzcheng 已提交
186

H
hzcheng 已提交
187 188 189
typedef enum { TSDB_WRITE_HELPER, TSDB_READ_HELPER } tsdb_rw_helper_t;

typedef struct {
H
Hongze Cheng 已提交
190 191
  TSKEY      minKey;
  TSKEY      maxKey;
H
Hongze Cheng 已提交
192 193
  SDFileSet  rSet;
  SDFileSet  wSet;
H
hzcheng 已提交
194 195 196
} SHelperFile;

typedef struct {
197 198
  uint64_t uid;
  int32_t  tid;
H
hzcheng 已提交
199 200
} SHelperTable;

H
Hongze Cheng 已提交
201
typedef struct {
H
refact  
Hongze Cheng 已提交
202
  SBlockIdx* pIdxArray;
H
Hongze Cheng 已提交
203 204 205 206
  int       numOfIdx;
  int       curIdx;
} SIdxH;

H
hzcheng 已提交
207
typedef struct {
H
TD-353  
Hongze Cheng 已提交
208
  tsdb_rw_helper_t type;
H
TD-100  
hzcheng 已提交
209

H
TD-353  
Hongze Cheng 已提交
210 211
  STsdbRepo* pRepo;
  int8_t     state;
H
TD-100  
hzcheng 已提交
212
  // For file set usage
H
hzcheng 已提交
213
  SHelperFile files;
H
Hongze Cheng 已提交
214
  SIdxH       idxH;
H
refact  
Hongze Cheng 已提交
215
  SBlockIdx    curCompIdx;
H
Hongze Cheng 已提交
216
  void*       pWIdx;
H
TD-100  
hzcheng 已提交
217
  // For table set usage
H
hzcheng 已提交
218
  SHelperTable tableInfo;
H
refact  
Hongze Cheng 已提交
219
  SBlockInfo*   pCompInfo;
H
TD-100  
hzcheng 已提交
220 221
  bool         hasOldLastBlock;
  // For block set usage
H
refact  
Hongze Cheng 已提交
222
  SBlockData* pCompData;
H
TD-353  
Hongze Cheng 已提交
223 224 225
  SDataCols* pDataCols[2];
  void*      pBuffer;     // Buffer to hold the whole data block
  void*      compBuffer;  // Buffer for temperary compress/decompress purpose
H
hzcheng 已提交
226 227
} SRWHelper;

H
TD-353  
Hongze Cheng 已提交
228 229
#define TSDB_HELPER_CLEAR_STATE 0x0        // Clear state
#define TSDB_HELPER_FILE_SET_AND_OPEN 0x1  // File is set
H
refact  
Hongze Cheng 已提交
230
#define TSDB_HELPER_IDX_LOAD 0x2           // SBlockIdx part is loaded
H
TD-353  
Hongze Cheng 已提交
231
#define TSDB_HELPER_TABLE_SET 0x4          // Table is set
H
refact  
Hongze Cheng 已提交
232 233
#define TSDB_HELPER_INFO_LOAD 0x8          // SBlockInfo part is loaded
#define TSDB_HELPER_FILE_DATA_LOAD 0x10    // SBlockData part is loaded
H
TD-353  
Hongze Cheng 已提交
234 235 236 237
#define helperSetState(h, s) (((h)->state) |= (s))
#define helperClearState(h, s) ((h)->state &= (~(s)))
#define helperHasState(h, s) ((((h)->state) & (s)) == (s))
#define blockAtIdx(h, idx) ((h)->pCompInfo->blocks + idx)
H
TD-353  
Hongze Cheng 已提交
238 239
#define TSDB_MAX_SUBBLOCKS 8
#define IS_SUB_BLOCK(pBlock) ((pBlock)->numOfSubBlocks == 0)
H
TD-353  
Hongze Cheng 已提交
240 241 242
#define helperType(h) (h)->type
#define helperRepo(h) (h)->pRepo
#define helperState(h) (h)->state
H
Hongze Cheng 已提交
243
#define TSDB_NLAST_FILE_OPENED(h) ((h)->files.nLastF.fd > 0)
H
Hongze Cheng 已提交
244 245 246 247 248 249
#define helperFileId(h) ((h)->files.fGroup.fileId)
#define helperHeadF(h) (&((h)->files.fGroup.files[TSDB_FILE_TYPE_HEAD]))
#define helperDataF(h) (&((h)->files.fGroup.files[TSDB_FILE_TYPE_DATA]))
#define helperLastF(h) (&((h)->files.fGroup.files[TSDB_FILE_TYPE_LAST]))
#define helperNewHeadF(h) (&((h)->files.nHeadF))
#define helperNewLastF(h) (&((h)->files.nLastF))
H
Hongze Cheng 已提交
250 251 252 253 254 255

int  tsdbInitReadHelper(SRWHelper* pHelper, STsdbRepo* pRepo);
int  tsdbInitWriteHelper(SRWHelper* pHelper, STsdbRepo* pRepo);
void tsdbDestroyHelper(SRWHelper* pHelper);
void tsdbResetHelper(SRWHelper* pHelper);
int  tsdbSetAndOpenHelperFile(SRWHelper* pHelper, SFileGroup* pGroup);
H
Hongze Cheng 已提交
256
int  tsdbCloseHelperFile(SRWHelper* pHelper, bool hasError, SFileGroup* pGroup);
H
Hongze Cheng 已提交
257
int  tsdbSetHelperTable(SRWHelper* pHelper, STable* pTable, STsdbRepo* pRepo);
H
Hongze Cheng 已提交
258 259 260 261
int  tsdbCommitTableData(SRWHelper* pHelper, SCommitIter* pCommitIter, SDataCols* pDataCols, TSKEY maxKey);
int  tsdbMoveLastBlockIfNeccessary(SRWHelper* pHelper);
int  tsdbWriteCompInfo(SRWHelper* pHelper);
int  tsdbWriteCompIdx(SRWHelper* pHelper);
H
Hongze Cheng 已提交
262
int  tsdbLoadCompIdxImpl(SFile* pFile, uint32_t offset, uint32_t len, void* buffer);
H
refact  
Hongze Cheng 已提交
263
int  tsdbDecodeSBlockIdxImpl(void* buffer, uint32_t len, SBlockIdx** ppCompIdx, int* numOfIdx);
H
Hongze Cheng 已提交
264
int  tsdbLoadCompIdx(SRWHelper* pHelper, void* target);
H
refact  
Hongze Cheng 已提交
265
int  tsdbLoadCompInfoImpl(SFile* pFile, SBlockIdx* pIdx, SBlockInfo** ppCompInfo);
H
Hongze Cheng 已提交
266
int  tsdbLoadCompInfo(SRWHelper* pHelper, void* target);
H
refact  
Hongze Cheng 已提交
267
int  tsdbLoadCompData(SRWHelper* phelper, SBlock* pcompblock, void* target);
H
Hongze Cheng 已提交
268
void tsdbGetDataStatis(SRWHelper* pHelper, SDataStatis* pStatis, int numOfCols);
H
refact  
Hongze Cheng 已提交
269
int  tsdbLoadBlockDataCols(SRWHelper* pHelper, SBlock* pCompBlock, SBlockInfo* pCompInfo, int16_t* colIds,
H
Hongze Cheng 已提交
270
                           int numOfColIds);
H
refact  
Hongze Cheng 已提交
271
int  tsdbLoadBlockData(SRWHelper* pHelper, SBlock* pCompBlock, SBlockInfo* pCompInfo);
H
Hongze Cheng 已提交
272 273 274 275 276 277 278 279 280 281

static FORCE_INLINE int compTSKEY(const void* key1, const void* key2) {
  if (*(TSKEY*)key1 > *(TSKEY*)key2) {
    return 1;
  } else if (*(TSKEY*)key1 == *(TSKEY*)key2) {
    return 0;
  } else {
    return -1;
  }
}
H
TD-353  
Hongze Cheng 已提交
282

H
Hongze Cheng 已提交
283 284
#endif

H
refact  
Hongze Cheng 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
// ================= tsdbScan.c
typedef struct {
  SFileGroup  fGroup;
  int         numOfIdx;
  SBlockIdx*  pCompIdx;
  SBlockInfo* pCompInfo;
  void*       pBuf;
  FILE*       tLogStream;
} STsdbScanHandle;

int              tsdbScanFGroup(STsdbScanHandle* pScanHandle, char* rootDir, int fid);
STsdbScanHandle* tsdbNewScanHandle();
void             tsdbSetScanLogStream(STsdbScanHandle* pScanHandle, FILE* fLogStream);
int              tsdbSetAndOpenScanFile(STsdbScanHandle* pScanHandle, char* rootDir, int fid);
int              tsdbScanSBlockIdx(STsdbScanHandle* pScanHandle);
int              tsdbScanSBlock(STsdbScanHandle* pScanHandle, int idx);
int              tsdbCloseScanFile(STsdbScanHandle* pScanHandle);
void             tsdbFreeScanHandle(STsdbScanHandle* pScanHandle);

H
Hongze Cheng 已提交
304 305 306
// ------------------ tsdbCommitQueue.c
int tsdbScheduleCommit(STsdbRepo *pRepo);

H
more  
hzcheng 已提交
307 308 309 310
#ifdef __cplusplus
}
#endif

H
TD-34  
hzcheng 已提交
311
#endif