tsdbMain.h 16.7 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
slguan 已提交
18
#include "tglobal.h"
H
hzcheng 已提交
19 20
#include "tlist.h"
#include "tsdb.h"
H
TD-34  
hzcheng 已提交
21 22
#include "tskiplist.h"
#include "tutil.h"
H
hzcheng 已提交
23
#include "tlog.h"
H
hzcheng 已提交
24
#include "tcoding.h"
H
more  
Hongze Cheng 已提交
25

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

H
hzcheng 已提交
30 31
extern int tsdbDebugFlag;

S
Shengliang Guan 已提交
32 33 34 35
#define tsdbError(...) { if (tsdbDebugFlag & DEBUG_ERROR) { taosPrintLog("ERROR TDB ", tsdbDebugFlag, __VA_ARGS__); }}
#define tsdbWarn(...)  { if (tsdbDebugFlag & DEBUG_WARN)  { taosPrintLog("WARN TDB ", tsdbDebugFlag, __VA_ARGS__); }}
#define tsdbTrace(...) { if (tsdbDebugFlag & DEBUG_TRACE) { taosPrintLog("TDB ", tsdbDebugFlag, __VA_ARGS__); }}
#define tsdbPrint(...) { taosPrintLog("TDB ", 255, __VA_ARGS__); }
H
hzcheng 已提交
36

H
TD-34  
hzcheng 已提交
37
// ------------------------------ TSDB META FILE INTERFACES ------------------------------
H
Hongze Cheng 已提交
38
#define TSDB_META_FILE_NAME "meta"
H
TD-34  
hzcheng 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#define TSDB_META_HASH_FRACTION 1.1

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

typedef struct {
  int       fd;        // File descriptor
  int       nDel;      // number of deletions
  int       tombSize;  // deleted size
  int64_t   size;      // Total file size
  void *    map;       // Map from uid ==> position
  iterFunc  iFunc;
  afterFunc aFunc;
  void *    appH;
} SMetaFile;

SMetaFile *tsdbInitMetaFile(char *rootDir, int32_t maxTables, iterFunc iFunc, afterFunc aFunc, void *appH);
56 57 58
int32_t    tsdbInsertMetaRecord(SMetaFile *mfh, uint64_t uid, void *cont, int32_t contLen);
int32_t    tsdbDeleteMetaRecord(SMetaFile *mfh, uint64_t uid);
int32_t    tsdbUpdateMetaRecord(SMetaFile *mfh, uint64_t uid, void *cont, int32_t contLen);
H
TD-34  
hzcheng 已提交
59 60 61 62 63 64 65 66
void       tsdbCloseMetaFile(SMetaFile *mfh);

// ------------------------------ TSDB META INTERFACES ------------------------------
#define IS_CREATE_STABLE(pCfg) ((pCfg)->tagValues != NULL)

typedef struct {
  TSKEY   keyFirst;
  TSKEY   keyLast;
H
Haojun Liao 已提交
67
  int32_t numOfRows;
H
TD-34  
hzcheng 已提交
68 69 70 71
  void *  pData;
} SMemTable;

// ---------- TSDB TABLE DEFINITION
H
TD-90  
Hongze Cheng 已提交
72
#define TSDB_MAX_TABLE_SCHEMAS 16
H
TD-34  
hzcheng 已提交
73 74 75
typedef struct STable {
  int8_t         type;
  STableId       tableId;
76
  uint64_t       superUid;  // Super table UID
H
TD-90  
Hongze Cheng 已提交
77 78
  int16_t        numOfSchemas;
  STSchema **    schema;
H
TD-34  
hzcheng 已提交
79
  STSchema *     tagSchema;
H
Hongze Cheng 已提交
80
  SKVRow         tagVal;
H
TD-34  
hzcheng 已提交
81 82 83 84 85
  SMemTable *    mem;
  SMemTable *    imem;
  void *         pIndex;         // For TSDB_SUPER_TABLE, it is the skiplist index
  void *         eventHandler;   // TODO
  void *         streamHandler;  // TODO
H
hzcheng 已提交
86
  TSKEY          lastKey;        // lastkey inserted in this table, initialized as 0, TODO: make a structure
H
TD-34  
hzcheng 已提交
87
  struct STable *next;           // TODO: remove the next
H
hzcheng 已提交
88
  struct STable *prev;
H
TD-354  
Hongze Cheng 已提交
89 90
  tstr *         name;  // NOTE: there a flexible string here
  char *         sql;
H
Hongze Cheng 已提交
91
  void *         cqhandle;
H
TD-34  
hzcheng 已提交
92 93
} STable;

H
hjxilinx 已提交
94
#define TSDB_GET_TABLE_LAST_KEY(tb) ((tb)->lastKey)
H
hzcheng 已提交
95

H
TD-354  
Hongze Cheng 已提交
96
void    tsdbEncodeTable(STable *pTable, char *buf, int *contLen);
H
TD-34  
hzcheng 已提交
97
STable *tsdbDecodeTable(void *cont, int contLen);
H
hzcheng 已提交
98
void    tsdbFreeEncode(void *cont);
H
TD-34  
hzcheng 已提交
99 100 101 102 103 104 105 106 107 108 109

// ---------- TSDB META HANDLE DEFINITION
typedef struct {
  int32_t maxTables;  // Max number of tables

  int32_t nTables;  // Tables created

  STable **tables;  // table array

  STable *superList;  // super table list TODO: change  it to list container

H
hzcheng 已提交
110
  void *map;  // table map of (uid ===> table)
H
TD-34  
hzcheng 已提交
111

H
hzcheng 已提交
112
  SMetaFile *mfh;  // meta file handle
H
TD-34  
hzcheng 已提交
113 114
  int        maxRowBytes;
  int        maxCols;
H
TD-354  
Hongze Cheng 已提交
115
  void *     pRepo;
H
TD-34  
hzcheng 已提交
116 117
} STsdbMeta;

H
hjxilinx 已提交
118 119 120 121 122 123
// element put in skiplist for each table
typedef struct STableIndexElem {
  STsdbMeta* pMeta;
  STable*    pTable;
} STableIndexElem;

H
TD-354  
Hongze Cheng 已提交
124
STsdbMeta *tsdbInitMeta(char *rootDir, int32_t maxTables, void *pRepo);
H
TD-34  
hzcheng 已提交
125
int32_t    tsdbFreeMeta(STsdbMeta *pMeta);
126
STSchema * tsdbGetTableTagSchema(STsdbMeta *pMeta, STable *pTable);
H
TD-34  
hzcheng 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

// ---- Operation on STable
#define TSDB_TABLE_ID(pTable) ((pTable)->tableId)
#define TSDB_TABLE_UID(pTable) ((pTable)->uid)
#define TSDB_TABLE_NAME(pTable) ((pTable)->tableName)
#define TSDB_TABLE_TYPE(pTable) ((pTable)->type)
#define TSDB_TABLE_SUPER_TABLE_UID(pTable) ((pTable)->stableUid)
#define TSDB_TABLE_IS_SUPER_TABLE(pTable) (TSDB_TABLE_TYPE(pTable) == TSDB_SUPER_TABLE)
#define TSDB_TABLE_TAG_VALUE(pTable) ((pTable)->pTagVal)
#define TSDB_TABLE_CACHE_DATA(pTable) ((pTable)->content.pData)
#define TSDB_SUPER_TABLE_INDEX(pTable) ((pTable)->content.pIndex)

// ---- Operation on SMetaHandle
#define TSDB_NUM_OF_TABLES(pHandle) ((pHandle)->numOfTables)
#define TSDB_NUM_OF_SUPER_TABLES(pHandle) ((pHandle)->numOfSuperTables)
#define TSDB_TABLE_OF_ID(pHandle, id) ((pHandle)->pTables)[id]
#define TSDB_GET_TABLE_OF_NAME(pHandle, name) /* TODO */

H
TD-353  
Hongze Cheng 已提交
145
STsdbMeta *tsdbGetMeta(TSDB_REPO_T *pRepo);
H
TD-34  
hzcheng 已提交
146 147 148

STable *tsdbIsValidTableToInsert(STsdbMeta *pMeta, STableId tableId);
// int32_t tsdbInsertRowToTableImpl(SSkipListNode *pNode, STable *pTable);
149
STable *tsdbGetTableByUid(STsdbMeta *pMeta, uint64_t uid);
H
hjxilinx 已提交
150
char *getTSTupleKey(const void * data);
H
TD-34  
hzcheng 已提交
151 152 153 154 155 156 157 158 159 160 161

typedef struct {
  int  blockId;
  int  offset;
  int  remain;
  int  padding;
  char data[];
} STsdbCacheBlock;

typedef struct {
  int64_t index;
162
  int     numOfCacheBlocks;
H
TD-34  
hzcheng 已提交
163
  SList * memPool;
H
TD-353  
Hongze Cheng 已提交
164
} STsdbBufferPool;
H
TD-34  
hzcheng 已提交
165 166 167 168

typedef struct {
  TSKEY   keyFirst;
  TSKEY   keyLast;
H
Haojun Liao 已提交
169
  int64_t numOfRows;
H
TD-34  
hzcheng 已提交
170 171 172 173 174 175
  SList * list;
} SCacheMem;

typedef struct {
  int              cacheBlockSize;
  int              totalCacheBlocks;
H
TD-353  
Hongze Cheng 已提交
176
  STsdbBufferPool   pool;
H
TD-34  
hzcheng 已提交
177 178 179
  STsdbCacheBlock *curBlock;
  SCacheMem *      mem;
  SCacheMem *      imem;
H
TD-353  
Hongze Cheng 已提交
180
  TSDB_REPO_T *      pRepo;
H
TD-34  
hzcheng 已提交
181 182
} STsdbCache;

H
TD-353  
Hongze Cheng 已提交
183
STsdbCache *tsdbInitCache(int cacheBlockSize, int totalBlocks, TSDB_REPO_T *pRepo);
H
TD-34  
hzcheng 已提交
184 185 186 187
void        tsdbFreeCache(STsdbCache *pCache);
void *      tsdbAllocFromCache(STsdbCache *pCache, int bytes, TSKEY key);

// ------------------------------ TSDB FILE INTERFACES ------------------------------
H
TD-34  
hzcheng 已提交
188
#define TSDB_FILE_HEAD_SIZE 512
H
TD-34  
hzcheng 已提交
189
#define TSDB_FILE_DELIMITER 0xF00AFA0F
H
TD-34  
hzcheng 已提交
190

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

H
Hongze Cheng 已提交
194
typedef enum {
H
TD-34  
hzcheng 已提交
195 196 197 198
  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 已提交
199 200
} TSDB_FILE_TYPE;

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

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

H
more  
Hongze Cheng 已提交
205
typedef struct {
H
TD-185  
Hongze Cheng 已提交
206 207 208 209 210 211
  uint32_t offset;
  uint32_t len;
  uint64_t size;      // total size of the file
  uint64_t tombSize;  // unused file size
  uint32_t totalBlocks;
  uint32_t totalSubBlocks;
212
} STsdbFileInfo;
H
TD-34  
hzcheng 已提交
213

H
Hongze Cheng 已提交
214 215
void *tsdbEncodeSFileInfo(void *buf, const STsdbFileInfo *pInfo);
void *tsdbDecodeSFileInfo(void *buf, STsdbFileInfo *pInfo);
H
TD-185  
Hongze Cheng 已提交
216

H
TD-34  
hzcheng 已提交
217
typedef struct {
H
hzcheng 已提交
218 219
  int       fd;
  char      fname[128];
220
  STsdbFileInfo info;
H
hzcheng 已提交
221
} SFile;
H
more  
Hongze Cheng 已提交
222

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

H
hzcheng 已提交
225 226
typedef struct {
  int32_t fileId;
H
TD-34  
hzcheng 已提交
227
  SFile   files[TSDB_FILE_TYPE_MAX];
H
hzcheng 已提交
228 229 230 231
} SFileGroup;

// TSDB file handle
typedef struct {
H
TD-34  
hzcheng 已提交
232 233 234
  int maxFGroups;
  int numOfFGroups;

H
hzcheng 已提交
235
  SFileGroup *fGroup;
H
hzcheng 已提交
236
} STsdbFileH;
H
more  
Hongze Cheng 已提交
237

H
TD-34  
hzcheng 已提交
238 239 240
#define TSDB_MIN_FILE_ID(fh) (fh)->fGroup[0].fileId
#define TSDB_MAX_FILE_ID(fh) (fh)->fGroup[(fh)->numOfFGroups - 1].fileId

H
hzcheng 已提交
241
STsdbFileH *tsdbInitFileH(char *dataDir, STsdbCfg *pCfg);
H
TD-34  
hzcheng 已提交
242
void        tsdbCloseFileH(STsdbFileH *pFileH);
H
TD-185  
Hongze Cheng 已提交
243
int         tsdbCreateFile(char *dataDir, int fileId, const char *suffix, SFile *pFile);
H
hzcheng 已提交
244
SFileGroup *tsdbCreateFGroup(STsdbFileH *pFileH, char *dataDir, int fid, int maxTables);
H
TD-34  
hzcheng 已提交
245
int         tsdbOpenFile(SFile *pFile, int oflag);
H
hzcheng 已提交
246 247
int         tsdbCloseFile(SFile *pFile);
SFileGroup *tsdbOpenFilesForCommit(STsdbFileH *pFileH, int fid);
H
TD-34  
hzcheng 已提交
248
int         tsdbRemoveFileGroup(STsdbFileH *pFile, int fid);
H
hzcheng 已提交
249
int         tsdbGetFileName(char *dataDir, int fileId, const char *suffix, char *fname);
H
TD-34  
hzcheng 已提交
250

H
hzcheng 已提交
251
#define TSDB_FGROUP_ITER_FORWARD TSDB_ORDER_ASC
252 253
#define TSDB_FGROUP_ITER_BACKWARD TSDB_ORDER_DESC

H
TD-34  
hzcheng 已提交
254 255 256 257 258 259 260 261 262 263 264
typedef struct {
  int         numOfFGroups;
  SFileGroup *base;
  SFileGroup *pFileGroup;
  int         direction;
} SFileGroupIter;

void        tsdbInitFileGroupIter(STsdbFileH *pFileH, SFileGroupIter *pIter, int direction);
void        tsdbSeekFileGroupIter(SFileGroupIter *pIter, int fid);
SFileGroup *tsdbGetFileGroupNext(SFileGroupIter *pIter);

H
TD-34  
hzcheng 已提交
265
typedef struct {
H
TD-185  
Hongze Cheng 已提交
266 267 268 269 270
  uint32_t len;
  uint32_t offset;
  uint32_t padding;  // For padding purpose
  uint32_t hasLast : 2;
  uint32_t numOfBlocks : 30;
271 272
  uint64_t uid;
  TSKEY    maxKey;
H
hzcheng 已提交
273
} SCompIdx; /* sizeof(SCompIdx) = 28 */
H
TD-34  
hzcheng 已提交
274

H
TD-185  
Hongze Cheng 已提交
275 276 277
void *tsdbEncodeSCompIdx(void *buf, SCompIdx *pIdx);
void *tsdbDecodeSCompIdx(void *buf, SCompIdx *pIdx);

278
/**
H
TD-34  
hzcheng 已提交
279 280 281 282 283 284
 * 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
285 286 287 288 289
 */
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
H
Haojun Liao 已提交
290
  int32_t numOfRows : 24;    // Number of total points
291 292 293 294 295 296 297 298
  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
hzcheng 已提交
299 300
// Maximum number of sub-blocks a super-block can have
#define TSDB_MAX_SUBBLOCKS 8
H
TD-34  
hzcheng 已提交
301 302 303
#define IS_SUPER_BLOCK(pBlock) ((pBlock)->numOfSubBlocks >= 1)
#define IS_SUB_BLOCK(pBlock) ((pBlock)->numOfSubBlocks == 0)

H
TD-34  
hzcheng 已提交
304 305 306
typedef struct {
  int32_t    delimiter;  // For recovery usage
  int32_t    checksum;   // TODO: decide if checksum logic in this file or make it one API
307
  uint64_t   uid;
H
TD-34  
hzcheng 已提交
308 309
  SCompBlock blocks[];
} SCompInfo;
H
hzcheng 已提交
310

H
TD-34  
hzcheng 已提交
311
#define TSDB_COMPBLOCK_AT(pCompInfo, idx) ((pCompInfo)->blocks + (idx))
H
hzcheng 已提交
312 313 314 315 316 317 318 319 320
#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 已提交
321

H
TD-34  
hzcheng 已提交
322 323 324
// TODO: take pre-calculation into account
typedef struct {
  int16_t colId;  // Column ID
H
TD-166  
hzcheng 已提交
325
  int16_t len;    // Column length // TODO: int16_t is not enough
H
TD-34  
hzcheng 已提交
326 327
  int32_t type : 8;
  int32_t offset : 24;
H
TD-321  
Hongze Cheng 已提交
328 329 330 331 332 333 334
  int64_t sum;
  int64_t max;
  int64_t min;
  int16_t maxIndex;
  int16_t minIndex;
  int16_t numOfNull;
  char    padding[2];
H
TD-34  
hzcheng 已提交
335 336 337 338 339 340
} SCompCol;

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

H
TD-353  
Hongze Cheng 已提交
345
STsdbFileH *tsdbGetFile(TSDB_REPO_T *pRepo);
H
TD-34  
hzcheng 已提交
346

H
hzcheng 已提交
347 348
int         tsdbCopyBlockDataInFile(SFile *pOutFile, SFile *pInFile, SCompInfo *pCompInfo, int idx, int isLast,
                                    SDataCols *pCols);
H
TD-34  
hzcheng 已提交
349
SFileGroup *tsdbSearchFGroup(STsdbFileH *pFileH, int fid);
H
TD-34  
hzcheng 已提交
350
void tsdbGetKeyRangeOfFileId(int32_t daysPerFile, int8_t precision, int32_t fileId, TSKEY *minKey, TSKEY *maxKey);
H
TD-34  
hzcheng 已提交
351 352

// TSDB repository definition
353
typedef struct STsdbRepo {
H
TD-34  
hzcheng 已提交
354 355 356 357
  char *rootDir;
  // TSDB configuration
  STsdbCfg config;

H
hzcheng 已提交
358 359
  STsdbAppH appH;

T
Tao Liu 已提交
360 361
  STsdbStat stat;

H
TD-34  
hzcheng 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  // The meter meta handle of this TSDB repository
  STsdbMeta *tsdbMeta;

  // The cache Handle
  STsdbCache *tsdbCache;

  // The TSDB file handle
  STsdbFileH *tsdbFileH;

  // Disk tier handle for multi-tier storage
  void *diskTier;

  pthread_mutex_t mutex;

  int       commit;
  pthread_t commitThread;

  // A limiter to monitor the resources used by tsdb
  void *limiter;

  int8_t state;

} STsdbRepo;

H
hzcheng 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
typedef struct {
  int32_t  totalLen;
  int32_t  len;
  SDataRow row;
} SSubmitBlkIter;

int      tsdbInitSubmitBlkIter(SSubmitBlk *pBlock, SSubmitBlkIter *pIter);
SDataRow tsdbGetSubmitBlkNext(SSubmitBlkIter *pIter);

#define TSDB_SUBMIT_MSG_HEAD_SIZE sizeof(SSubmitMsg)

// SSubmitMsg Iterator
typedef struct {
  int32_t     totalLen;
  int32_t     len;
  SSubmitBlk *pBlock;
} SSubmitMsgIter;

int         tsdbInitSubmitMsgIter(SSubmitMsg *pMsg, SSubmitMsgIter *pIter);
SSubmitBlk *tsdbGetSubmitMsgNext(SSubmitMsgIter *pIter);

H
TD-353  
Hongze Cheng 已提交
407 408 409
int32_t tsdbTriggerCommit(TSDB_REPO_T *repo);
int32_t tsdbLockRepo(TSDB_REPO_T *repo);
int32_t tsdbUnLockRepo(TSDB_REPO_T *repo);
H
TD-34  
hzcheng 已提交
410

H
hzcheng 已提交
411 412 413 414
typedef enum { TSDB_WRITE_HELPER, TSDB_READ_HELPER } tsdb_rw_helper_t;

typedef struct {
  tsdb_rw_helper_t type;  // helper type
H
TD-100  
hzcheng 已提交
415 416 417 418 419 420 421 422

  int    maxTables;
  int    maxRowSize;
  int    maxRows;
  int    maxCols;
  int    minRowsPerFileBlock;
  int    maxRowsPerFileBlock;
  int8_t compress;
H
hzcheng 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
} SHelperCfg;

typedef struct {
  int fid;
  TSKEY minKey;
  TSKEY maxKey;
  // For read/write purpose
  SFile headF;
  SFile dataF;
  SFile lastF;
  // For write purpose only
  SFile nHeadF;
  SFile nLastF;
} SHelperFile;

typedef struct {
439 440 441
  uint64_t uid;
  int32_t  tid;
  int32_t  sversion;
H
hzcheng 已提交
442 443 444 445 446 447
} SHelperTable;

typedef struct {
  // Global configuration
  SHelperCfg config;

H
TD-100  
hzcheng 已提交
448 449 450
  int8_t state;

  // For file set usage
H
hzcheng 已提交
451
  SHelperFile files;
H
TD-100  
hzcheng 已提交
452
  SCompIdx *  pCompIdx;
H
hzcheng 已提交
453

H
TD-100  
hzcheng 已提交
454
  // For table set usage
H
hzcheng 已提交
455
  SHelperTable tableInfo;
H
TD-100  
hzcheng 已提交
456 457
  SCompInfo *  pCompInfo;
  bool         hasOldLastBlock;
H
hzcheng 已提交
458

H
TD-100  
hzcheng 已提交
459
  // For block set usage
H
hzcheng 已提交
460 461 462
  SCompData *pCompData;
  SDataCols *pDataCols[2];

H
Hongze Cheng 已提交
463
  void *pBuffer;  // Buffer to hold the whole data block
H
TD-166  
hzcheng 已提交
464
  void *compBuffer;   // Buffer for temperary compress/decompress purpose
H
hzcheng 已提交
465 466 467
} SRWHelper;

// --------- Helper state
H
TD-100  
hzcheng 已提交
468 469 470 471 472 473
#define TSDB_HELPER_CLEAR_STATE 0x0        // Clear state
#define TSDB_HELPER_FILE_SET_AND_OPEN 0x1  // File is set
#define TSDB_HELPER_IDX_LOAD 0x2           // SCompIdx part is loaded
#define TSDB_HELPER_TABLE_SET 0x4          // Table is set
#define TSDB_HELPER_INFO_LOAD 0x8          // SCompInfo part is loaded
#define TSDB_HELPER_FILE_DATA_LOAD 0x10    // SCompData part is loaded
H
hzcheng 已提交
474 475 476 477 478 479

#define TSDB_HELPER_TYPE(h) ((h)->config.type)

#define helperSetState(h, s) (((h)->state) |= (s))
#define helperClearState(h, s) ((h)->state &= (~(s)))
#define helperHasState(h, s) ((((h)->state) & (s)) == (s))
H
TD-100  
hzcheng 已提交
480
#define blockAtIdx(h, idx) ((h)->pCompInfo->blocks + idx)
H
hzcheng 已提交
481

H
TD-100  
hzcheng 已提交
482 483
int  tsdbInitReadHelper(SRWHelper *pHelper, STsdbRepo *pRepo);
int  tsdbInitWriteHelper(SRWHelper *pHelper, STsdbRepo *pRepo);
H
hzcheng 已提交
484
void tsdbDestroyHelper(SRWHelper *pHelper);
H
TD-100  
hzcheng 已提交
485
void tsdbResetHelper(SRWHelper *pHelper);
H
hzcheng 已提交
486 487

// --------- For set operations
H
TD-100  
hzcheng 已提交
488 489
int tsdbSetAndOpenHelperFile(SRWHelper *pHelper, SFileGroup *pGroup);
void tsdbSetHelperTable(SRWHelper *pHelper, STable *pTable, STsdbRepo *pRepo);
H
hzcheng 已提交
490 491 492
int  tsdbCloseHelperFile(SRWHelper *pHelper, bool hasError);

// --------- For read operations
H
Hongze Cheng 已提交
493 494 495 496 497 498
int  tsdbLoadCompIdx(SRWHelper *pHelper, void *target);
int  tsdbLoadCompInfo(SRWHelper *pHelper, void *target);
int  tsdbLoadCompData(SRWHelper *pHelper, SCompBlock *pCompBlock, void *target);
int  tsdbLoadBlockDataCols(SRWHelper *pHelper, SDataCols *pDataCols, int blkIdx, int16_t *colIds, int numOfColIds);
int  tsdbLoadBlockData(SRWHelper *pHelper, SCompBlock *pCompBlock, SDataCols *target);
void tsdbGetDataStatis(SRWHelper *pHelper, SDataStatis *pStatis, int numOfCols);
H
hzcheng 已提交
499 500 501 502 503 504

// --------- For write operations
int tsdbWriteDataBlock(SRWHelper *pHelper, SDataCols *pDataCols);
int tsdbMoveLastBlockIfNeccessary(SRWHelper *pHelper);
int tsdbWriteCompInfo(SRWHelper *pHelper);
int tsdbWriteCompIdx(SRWHelper *pHelper);
H
TD-34  
hzcheng 已提交
505

H
hzcheng 已提交
506
// --------- Other functions need to further organize
H
TD-90  
Hongze Cheng 已提交
507 508 509 510 511 512 513 514 515 516
void      tsdbFitRetention(STsdbRepo *pRepo);
int       tsdbAlterCacheTotalBlocks(STsdbRepo *pRepo, int totalBlocks);
void      tsdbAdjustCacheBlocks(STsdbCache *pCache);
int32_t   tsdbGetMetaFileName(char *rootDir, char *fname);
int       tsdbUpdateFileHeader(SFile *pFile, uint32_t version);
int       tsdbUpdateTable(STsdbMeta *pMeta, STable *pTable, STableCfg *pCfg);
int       tsdbRemoveTableFromIndex(STsdbMeta *pMeta, STable *pTable);
int       tsdbAddTableIntoIndex(STsdbMeta *pMeta, STable *pTable);
STSchema *tsdbGetTableSchemaByVersion(STsdbMeta *pMeta, STable *pTable, int16_t version);
STSchema *tsdbGetTableSchema(STsdbMeta *pMeta, STable *pTable);
H
TD-90  
Hongze Cheng 已提交
517 518

#define DEFAULT_TAG_INDEX_COLUMN 0  // skip list built based on the first column of tags
H
hzcheng 已提交
519

H
Hongze Cheng 已提交
520 521
int compFGroupKey(const void *key, const void *fgroup);

H
more  
hzcheng 已提交
522 523 524 525
#ifdef __cplusplus
}
#endif

H
TD-34  
hzcheng 已提交
526
#endif