tsdbMain.h 19.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
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 "tfs.h"
H
more  
Hongze Cheng 已提交
30

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

S
Shengliang Guan 已提交
35
extern int32_t tsdbDebugFlag;
H
hzcheng 已提交
36

H
Haojun Liao 已提交
37 38 39 40 41 42
#define tsdbFatal(...) do { if (tsdbDebugFlag & DEBUG_FATAL) { taosPrintLog("TDB FATAL ", 255, __VA_ARGS__); }}     while(0)
#define tsdbError(...) do { if (tsdbDebugFlag & DEBUG_ERROR) { taosPrintLog("TDB ERROR ", 255, __VA_ARGS__); }}     while(0)
#define tsdbWarn(...)  do { if (tsdbDebugFlag & DEBUG_WARN)  { taosPrintLog("TDB WARN ", 255, __VA_ARGS__); }}      while(0)
#define tsdbInfo(...)  do { if (tsdbDebugFlag & DEBUG_INFO)  { taosPrintLog("TDB ", 255, __VA_ARGS__); }}           while(0)
#define tsdbDebug(...) do { if (tsdbDebugFlag & DEBUG_DEBUG) { taosPrintLog("TDB ", tsdbDebugFlag, __VA_ARGS__); }} while(0)
#define tsdbTrace(...) do { if (tsdbDebugFlag & DEBUG_TRACE) { taosPrintLog("TDB ", tsdbDebugFlag, __VA_ARGS__); }} while(0)
H
hzcheng 已提交
43

H
TD-90  
Hongze Cheng 已提交
44
#define TSDB_MAX_TABLE_SCHEMAS 16
H
Haojun Liao 已提交
45 46 47
#define TSDB_FILE_HEAD_SIZE    512
#define TSDB_FILE_DELIMITER    0xF00AFA0F
#define TSDB_FILE_INIT_MAGIC   0xFFFFFFFF
H
TD-353  
Hongze Cheng 已提交
48

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

51 52 53 54
// 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 已提交
55 56
// Definitions
// ------------------ tsdbMeta.c
H
TD-34  
hzcheng 已提交
57
typedef struct STable {
H
Haojun Liao 已提交
58
  STableId       tableId;
H
TD-353  
Hongze Cheng 已提交
59 60 61 62 63
  ETableType     type;
  tstr*          name;  // NOTE: there a flexible string here
  uint64_t       suid;
  struct STable* pSuper;  // super table pointer
  uint8_t        numOfSchemas;
H
TD-353  
Hongze Cheng 已提交
64
  STSchema*      schema[TSDB_MAX_TABLE_SCHEMAS];
H
TD-353  
Hongze Cheng 已提交
65 66
  STSchema*      tagSchema;
  SKVRow         tagVal;
H
TD-353  
Hongze Cheng 已提交
67
  SSkipList*     pIndex;         // For TSDB_SUPER_TABLE, it is the skiplist index
H
TD-353  
Hongze Cheng 已提交
68 69
  void*          eventHandler;   // TODO
  void*          streamHandler;  // TODO
H
Hongze Cheng 已提交
70 71
  TSKEY          lastKey;
  SDataRow       lastRow;
H
TD-353  
Hongze Cheng 已提交
72 73
  char*          sql;
  void*          cqhandle;
H
Hongze Cheng 已提交
74
  SRWLatch       latch;  // TODO: implementa latch functions
S
TD-1057  
Shengliang Guan 已提交
75
  T_REF_DECLARE()
H
TD-34  
hzcheng 已提交
76 77 78
} STable;

typedef struct {
H
TD-353  
Hongze Cheng 已提交
79 80 81
  pthread_rwlock_t rwLock;

  int32_t   nTables;
H
TD-987  
Hongze Cheng 已提交
82
  int32_t   maxTables;
H
TD-353  
Hongze Cheng 已提交
83 84 85 86
  STable**  tables;
  SList*    superList;
  SHashObj* uidMap;
  SKVStore* pStore;
H
TD-353  
Hongze Cheng 已提交
87 88
  int       maxRowBytes;
  int       maxCols;
H
TD-34  
hzcheng 已提交
89 90
} STsdbMeta;

H
TD-353  
Hongze Cheng 已提交
91
// ------------------ tsdbBuffer.c
H
TD-34  
hzcheng 已提交
92
typedef struct {
H
TD-353  
Hongze Cheng 已提交
93 94 95 96 97
  int64_t blockId;
  int     offset;
  int     remain;
  char    data[];
} STsdbBufBlock;
H
TD-34  
hzcheng 已提交
98 99

typedef struct {
H
TD-353  
Hongze Cheng 已提交
100 101 102 103 104 105 106 107 108
  pthread_cond_t poolNotEmpty;
  int            bufBlockSize;
  int            tBufBlocks;
  int            nBufBlocks;
  int64_t        index;
  SList*         bufBlockList;
} STsdbBufPool;

// ------------------ tsdbMemTable.c
H
Hongze Cheng 已提交
109 110 111 112 113
typedef struct {
  STable *           pTable;
  SSkipListIterator *pIter;
} SCommitIter;

H
TD-34  
hzcheng 已提交
114
typedef struct {
H
TD-353  
Hongze Cheng 已提交
115 116 117 118 119 120
  uint64_t   uid;
  TSKEY      keyFirst;
  TSKEY      keyLast;
  int64_t    numOfRows;
  SSkipList* pData;
} STableData;
H
TD-34  
hzcheng 已提交
121 122

typedef struct {
S
TD-1057  
Shengliang Guan 已提交
123
  T_REF_DECLARE()
H
TD-987  
Hongze Cheng 已提交
124
  SRWLatch     latch;
H
TD-353  
Hongze Cheng 已提交
125 126 127
  TSKEY        keyFirst;
  TSKEY        keyLast;
  int64_t      numOfRows;
H
TD-987  
Hongze Cheng 已提交
128
  int32_t      maxTables;
H
TD-353  
Hongze Cheng 已提交
129 130
  STableData** tData;
  SList*       actList;
H
Hongze Cheng 已提交
131
  SList*       extraBuffList;
H
TD-353  
Hongze Cheng 已提交
132 133
  SList*       bufBlockList;
} SMemTable;
H
TD-34  
hzcheng 已提交
134

H
TD-353  
Hongze Cheng 已提交
135
enum { TSDB_UPDATE_META, TSDB_DROP_META };
S
TD-1057  
Shengliang Guan 已提交
136 137 138 139 140

#ifdef WINDOWS
#pragma pack(push ,1) 
typedef struct {
#else
H
TD-353  
Hongze Cheng 已提交
141
typedef struct __attribute__((packed)){
S
TD-1057  
Shengliang Guan 已提交
142
#endif
H
TD-353  
Hongze Cheng 已提交
143 144 145
  char     act;
  uint64_t uid;
} SActObj;
S
TD-1057  
Shengliang Guan 已提交
146 147 148
#ifdef WINDOWS
#pragma pack(pop) 
#endif
H
TD-353  
Hongze Cheng 已提交
149 150 151 152 153 154

typedef struct {
  int  len;
  char cont[];
} SActCont;

H
TD-353  
Hongze Cheng 已提交
155
// ------------------ tsdbFile.c
H
TD-353  
Hongze Cheng 已提交
156
extern const char* tsdbFileSuffix[];
H
Hongze Cheng 已提交
157 158 159 160 161 162 163 164

// 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;

H
TD-353  
Hongze Cheng 已提交
165
typedef enum {
H
Hongze Cheng 已提交
166
  TSDB_FILE_TYPE_HEAD = 0,
H
TD-353  
Hongze Cheng 已提交
167 168
  TSDB_FILE_TYPE_DATA,
  TSDB_FILE_TYPE_LAST,
H
Hongze Cheng 已提交
169
  TSDB_FILE_TYPE_STAT,
H
TD-353  
Hongze Cheng 已提交
170
  TSDB_FILE_TYPE_NHEAD,
H
Hongze Cheng 已提交
171 172 173
  TSDB_FILE_TYPE_NDATA,
  TSDB_FILE_TYPE_NLAST,
  TSDB_FILE_TYPE_NSTAT
H
TD-353  
Hongze Cheng 已提交
174
} TSDB_FILE_TYPE;
H
Hongze Cheng 已提交
175

H
Hongze Cheng 已提交
176 177 178 179 180 181
#ifndef TDINTERNAL
#define TSDB_FILE_TYPE_MAX (TSDB_FILE_TYPE_LAST+1)
#else
#define TSDB_FILE_TYPE_MAX (TSDB_FILE_TYPE_STAT+1)
#endif

H
more  
Hongze Cheng 已提交
182
typedef struct {
H
Hongze Cheng 已提交
183
  uint32_t magic;
H
TD-353  
Hongze Cheng 已提交
184 185 186
  uint32_t len;
  uint32_t totalBlocks;
  uint32_t totalSubBlocks;
H
Hongze Cheng 已提交
187
  uint32_t offset;
H
Hongze Cheng 已提交
188 189
  uint64_t size;      // total size of the file
  uint64_t tombSize;  // unused file size
190
} STsdbFileInfo;
H
TD-34  
hzcheng 已提交
191 192

typedef struct {
H
Hongze Cheng 已提交
193
  TFILE         file;
H
TD-353  
Hongze Cheng 已提交
194
  STsdbFileInfo info;
H
Hongze Cheng 已提交
195
  int           fd;
H
hzcheng 已提交
196
} SFile;
H
more  
Hongze Cheng 已提交
197

H
hzcheng 已提交
198
typedef struct {
H
TD-353  
Hongze Cheng 已提交
199
  int   fileId;
H
Hongze Cheng 已提交
200
  int   state; // 0 for health, 1 for problem
H
TD-353  
Hongze Cheng 已提交
201
  SFile files[TSDB_FILE_TYPE_MAX];
H
hzcheng 已提交
202 203 204
} SFileGroup;

typedef struct {
H
TD-353  
Hongze Cheng 已提交
205 206
  pthread_rwlock_t fhlock;

H
TD-353  
Hongze Cheng 已提交
207 208 209
  int         maxFGroups;
  int         nFGroups;
  SFileGroup* pFGroup;
H
hzcheng 已提交
210
} STsdbFileH;
H
more  
Hongze Cheng 已提交
211

H
TD-34  
hzcheng 已提交
212 213
typedef struct {
  int         direction;
214 215 216
  STsdbFileH* pFileH;
  int         fileId;
  int         index;
H
TD-34  
hzcheng 已提交
217 218
} SFileGroupIter;

H
Hongze Cheng 已提交
219
#define TSDB_FILE_NAME(pFile) ((pFile)->file.aname)
H
Hongze Cheng 已提交
220

H
TD-353  
Hongze Cheng 已提交
221
// ------------------ tsdbMain.c
H
Hongze Cheng 已提交
222 223 224 225 226 227 228 229 230 231 232 233
typedef struct {
  int32_t  totalLen;
  int32_t  len;
  SDataRow row;
} SSubmitBlkIter;

typedef struct {
  int32_t totalLen;
  int32_t len;
  void *  pMsg;
} SSubmitMsgIter;

H
TD-353  
Hongze Cheng 已提交
234 235 236 237 238 239 240 241 242 243 244 245
typedef struct {
  int8_t state;

  char*           rootDir;
  STsdbCfg        config;
  STsdbAppH       appH;
  STsdbStat       stat;
  STsdbMeta*      tsdbMeta;
  STsdbBufPool*   pPool;
  SMemTable*      mem;
  SMemTable*      imem;
  STsdbFileH*     tsdbFileH;
H
Hongze Cheng 已提交
246
  sem_t           readyToCommit;
H
TD-353  
Hongze Cheng 已提交
247 248
  pthread_mutex_t mutex;
  bool            repoLocked;
H
Hongze Cheng 已提交
249
  int32_t         code; // Commit code
H
TD-353  
Hongze Cheng 已提交
250 251
} STsdbRepo;

H
TD-353  
Hongze Cheng 已提交
252
// ------------------ tsdbRWHelper.c
H
TD-34  
hzcheng 已提交
253
typedef struct {
H
Hongze Cheng 已提交
254
  int32_t  tid;
H
TD-185  
Hongze Cheng 已提交
255 256 257 258
  uint32_t len;
  uint32_t offset;
  uint32_t hasLast : 2;
  uint32_t numOfBlocks : 30;
259 260
  uint64_t uid;
  TSKEY    maxKey;
H
refact  
Hongze Cheng 已提交
261
} SBlockIdx;
H
TD-353  
Hongze Cheng 已提交
262

263
typedef struct {
H
TD-353  
Hongze Cheng 已提交
264 265 266 267 268
  int64_t last : 1;
  int64_t offset : 63;
  int32_t algorithm : 8;
  int32_t numOfRows : 24;
  int32_t len;
H
refact  
Hongze Cheng 已提交
269
  int32_t keyLen;     // key column length, keyOffset = offset+sizeof(SBlockData)+sizeof(SBlockCol)*numOfCols
H
TD-353  
Hongze Cheng 已提交
270
  int16_t numOfSubBlocks;
H
Hongze Cheng 已提交
271
  int16_t numOfCols; // not including timestamp column
272 273
  TSKEY   keyFirst;
  TSKEY   keyLast;
H
refact  
Hongze Cheng 已提交
274
} SBlock;
275

H
TD-34  
hzcheng 已提交
276 277
typedef struct {
  int32_t    delimiter;  // For recovery usage
278
  int32_t    tid;
279
  uint64_t   uid;
H
refact  
Hongze Cheng 已提交
280 281
  SBlock blocks[];
} SBlockInfo;
H
hzcheng 已提交
282

H
TD-34  
hzcheng 已提交
283
typedef struct {
H
TD-353  
Hongze Cheng 已提交
284
  int16_t colId;
H
TD-541  
Hongze Cheng 已提交
285
  int32_t len;
H
TD-34  
hzcheng 已提交
286 287
  int32_t type : 8;
  int32_t offset : 24;
H
TD-321  
Hongze Cheng 已提交
288 289 290 291 292 293 294
  int64_t sum;
  int64_t max;
  int64_t min;
  int16_t maxIndex;
  int16_t minIndex;
  int16_t numOfNull;
  char    padding[2];
H
refact  
Hongze Cheng 已提交
295
} SBlockCol;
H
TD-34  
hzcheng 已提交
296 297 298 299

typedef struct {
  int32_t  delimiter;  // For recovery usage
  int32_t  numOfCols;  // For recovery usage
300
  uint64_t uid;        // For recovery usage
H
refact  
Hongze Cheng 已提交
301 302
  SBlockCol cols[];
} SBlockData;
H
TD-34  
hzcheng 已提交
303

H
hzcheng 已提交
304 305 306
typedef enum { TSDB_WRITE_HELPER, TSDB_READ_HELPER } tsdb_rw_helper_t;

typedef struct {
H
Hongze Cheng 已提交
307 308 309 310 311
  TSKEY      minKey;
  TSKEY      maxKey;
  SFileGroup fGroup;
  SFile      nHeadF;
  SFile      nLastF;
H
hzcheng 已提交
312 313 314
} SHelperFile;

typedef struct {
315 316
  uint64_t uid;
  int32_t  tid;
H
hzcheng 已提交
317 318
} SHelperTable;

H
Hongze Cheng 已提交
319
typedef struct {
H
refact  
Hongze Cheng 已提交
320
  SBlockIdx* pIdxArray;
H
Hongze Cheng 已提交
321 322 323 324
  int       numOfIdx;
  int       curIdx;
} SIdxH;

H
hzcheng 已提交
325
typedef struct {
H
TD-353  
Hongze Cheng 已提交
326
  tsdb_rw_helper_t type;
H
TD-100  
hzcheng 已提交
327

H
TD-353  
Hongze Cheng 已提交
328 329
  STsdbRepo* pRepo;
  int8_t     state;
H
TD-100  
hzcheng 已提交
330
  // For file set usage
H
hzcheng 已提交
331
  SHelperFile files;
H
Hongze Cheng 已提交
332
  SIdxH       idxH;
H
refact  
Hongze Cheng 已提交
333
  SBlockIdx    curCompIdx;
H
Hongze Cheng 已提交
334
  void*       pWIdx;
H
TD-100  
hzcheng 已提交
335
  // For table set usage
H
hzcheng 已提交
336
  SHelperTable tableInfo;
H
refact  
Hongze Cheng 已提交
337
  SBlockInfo*   pCompInfo;
H
TD-100  
hzcheng 已提交
338 339
  bool         hasOldLastBlock;
  // For block set usage
H
refact  
Hongze Cheng 已提交
340
  SBlockData* pCompData;
H
TD-353  
Hongze Cheng 已提交
341 342 343
  SDataCols* pDataCols[2];
  void*      pBuffer;     // Buffer to hold the whole data block
  void*      compBuffer;  // Buffer for temperary compress/decompress purpose
H
hzcheng 已提交
344 345
} SRWHelper;

H
TD-1548  
Hongze Cheng 已提交
346 347 348 349 350 351 352 353 354
typedef struct {
  int   rowsInserted;
  int   rowsUpdated;
  int   rowsDeleteSucceed;
  int   rowsDeleteFailed;
  int   nOperations;
  TSKEY keyFirst;
  TSKEY keyLast;
} SMergeInfo;
H
TD-1027  
Hongze Cheng 已提交
355 356 357 358
// ------------------ tsdbScan.c
typedef struct {
  SFileGroup fGroup;
  int        numOfIdx;
H
refact  
Hongze Cheng 已提交
359 360
  SBlockIdx*  pCompIdx;
  SBlockInfo* pCompInfo;
H
TD-1027  
Hongze Cheng 已提交
361 362 363 364
  void*      pBuf;
  FILE*      tLogStream;
} STsdbScanHandle;

H
TD-353  
Hongze Cheng 已提交
365 366
// Operations
// ------------------ tsdbMeta.c
H
TD-987  
Hongze Cheng 已提交
367
#define TSDB_INIT_NTABLES 1024
H
TD-353  
Hongze Cheng 已提交
368 369 370
#define TABLE_TYPE(t) (t)->type
#define TABLE_NAME(t) (t)->name
#define TABLE_CHAR_NAME(t) TABLE_NAME(t)->data
H
TD-353  
Hongze Cheng 已提交
371
#define TABLE_UID(t) (t)->tableId.uid
H
TD-353  
Hongze Cheng 已提交
372
#define TABLE_TID(t) (t)->tableId.tid
H
TD-353  
Hongze Cheng 已提交
373
#define TABLE_SUID(t) (t)->suid
374
#define TSDB_META_FILE_MAGIC(m) KVSTORE_MAGIC((m)->pStore)
375 376 377 378
#define TSDB_RLOCK_TABLE(t) taosRLockLatch(&((t)->latch))
#define TSDB_RUNLOCK_TABLE(t) taosRUnLockLatch(&((t)->latch))
#define TSDB_WLOCK_TABLE(t) taosWLockLatch(&((t)->latch))
#define TSDB_WUNLOCK_TABLE(t) taosWUnLockLatch(&((t)->latch))
H
TD-353  
Hongze Cheng 已提交
379 380 381

STsdbMeta* tsdbNewMeta(STsdbCfg* pCfg);
void       tsdbFreeMeta(STsdbMeta* pMeta);
H
TD-353  
Hongze Cheng 已提交
382 383 384 385 386 387 388
int        tsdbOpenMeta(STsdbRepo* pRepo);
int        tsdbCloseMeta(STsdbRepo* pRepo);
STable*    tsdbGetTableByUid(STsdbMeta* pMeta, uint64_t uid);
STSchema*  tsdbGetTableSchemaByVersion(STable* pTable, int16_t version);
int        tsdbWLockRepoMeta(STsdbRepo* pRepo);
int        tsdbRLockRepoMeta(STsdbRepo* pRepo);
int        tsdbUnlockRepoMeta(STsdbRepo* pRepo);
H
TD-353  
Hongze Cheng 已提交
389 390
void       tsdbRefTable(STable* pTable);
void       tsdbUnRefTable(STable* pTable);
H
Hongze Cheng 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
void       tsdbUpdateTableSchema(STsdbRepo* pRepo, STable* pTable, STSchema* pSchema, bool insertAct);

static FORCE_INLINE int tsdbCompareSchemaVersion(const void *key1, const void *key2) {
  if (*(int16_t *)key1 < schemaVersion(*(STSchema **)key2)) {
    return -1;
  } else if (*(int16_t *)key1 > schemaVersion(*(STSchema **)key2)) {
    return 1;
  } else {
    return 0;
  }
}

static FORCE_INLINE STSchema* tsdbGetTableSchemaImpl(STable* pTable, bool lock, bool copy, int16_t version) {
  STable*   pDTable = (TABLE_TYPE(pTable) == TSDB_CHILD_TABLE) ? pTable->pSuper : pTable;
  STSchema* pSchema = NULL;
  STSchema* pTSchema = NULL;

408
  if (lock) TSDB_RLOCK_TABLE(pDTable);
H
Hongze Cheng 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  if (version < 0) {  // get the latest version of schema
    pTSchema = pDTable->schema[pDTable->numOfSchemas - 1];
  } else {  // get the schema with version
    void* ptr = taosbsearch(&version, pDTable->schema, pDTable->numOfSchemas, sizeof(STSchema*),
                            tsdbCompareSchemaVersion, TD_EQ);
    if (ptr == NULL) {
      terrno = TSDB_CODE_TDB_IVD_TB_SCHEMA_VERSION;
      goto _exit;
    }
    pTSchema = *(STSchema**)ptr;
  }

  ASSERT(pTSchema != NULL);

  if (copy) {
    if ((pSchema = tdDupSchema(pTSchema)) == NULL) terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
  } else {
    pSchema = pTSchema;
  }

_exit:
430
  if (lock) TSDB_RUNLOCK_TABLE(pDTable);
H
Hongze Cheng 已提交
431 432
  return pSchema;
}
H
TD-353  
Hongze Cheng 已提交
433

H
Hongze Cheng 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
static FORCE_INLINE STSchema* tsdbGetTableSchema(STable* pTable) {
  return tsdbGetTableSchemaImpl(pTable, false, false, -1);
}

static FORCE_INLINE STSchema *tsdbGetTableTagSchema(STable *pTable) {
  if (pTable->type == TSDB_CHILD_TABLE) {  // check child table first
    STable *pSuper = pTable->pSuper;
    if (pSuper == NULL) return NULL;
    return pSuper->tagSchema;
  } else if (pTable->type == TSDB_SUPER_TABLE) {
    return pTable->tagSchema;
  } else {
    return NULL;
  }
}

H
Hongze Cheng 已提交
450 451 452
static FORCE_INLINE TSKEY tsdbGetTableLastKeyImpl(STable* pTable) {
  ASSERT(pTable->lastRow == NULL || pTable->lastKey == dataRowKey(pTable->lastRow));
  return pTable->lastKey;
453 454
}

H
TD-353  
Hongze Cheng 已提交
455
// ------------------ tsdbBuffer.c
H
Hongze Cheng 已提交
456 457
#define TSDB_BUFFER_RESERVE 1024  // Reseve 1K as commit threshold

H
TD-353  
Hongze Cheng 已提交
458 459 460
STsdbBufPool* tsdbNewBufPool();
void          tsdbFreeBufPool(STsdbBufPool* pBufPool);
int           tsdbOpenBufPool(STsdbRepo* pRepo);
H
TD-353  
Hongze Cheng 已提交
461
void          tsdbCloseBufPool(STsdbRepo* pRepo);
H
TD-353  
Hongze Cheng 已提交
462 463 464
SListNode*    tsdbAllocBufBlockFromPool(STsdbRepo* pRepo);

// ------------------ tsdbMemTable.c
H
TD-353  
Hongze Cheng 已提交
465 466 467
int   tsdbRefMemTable(STsdbRepo* pRepo, SMemTable* pMemTable);
int   tsdbUnRefMemTable(STsdbRepo* pRepo, SMemTable* pMemTable);
int   tsdbTakeMemSnapshot(STsdbRepo* pRepo, SMemTable** pMem, SMemTable** pIMem);
H
TD-987  
Hongze Cheng 已提交
468
void  tsdbUnTakeMemSnapShot(STsdbRepo* pRepo, SMemTable* pMem, SMemTable* pIMem);
H
TD-353  
Hongze Cheng 已提交
469
void* tsdbAllocBytes(STsdbRepo* pRepo, int bytes);
H
TD-353  
Hongze Cheng 已提交
470
int   tsdbAsyncCommit(STsdbRepo* pRepo);
H
Hongze Cheng 已提交
471
int   tsdbLoadDataFromCache(STable* pTable, SSkipListIterator* pIter, TSKEY maxKey, int maxRowsToRead, SDataCols* pCols,
H
TD-1548  
Hongze Cheng 已提交
472
                            TKEY* filterKeys, int nFilterKeys, bool keepDup, SMergeInfo* pMergeInfo);
H
Hongze Cheng 已提交
473
void* tsdbCommitData(STsdbRepo* pRepo);
H
Hongze Cheng 已提交
474 475 476 477 478 479 480

static FORCE_INLINE SDataRow tsdbNextIterRow(SSkipListIterator* pIter) {
  if (pIter == NULL) return NULL;

  SSkipListNode* node = tSkipListIterGet(pIter);
  if (node == NULL) return NULL;

H
TD-1194  
Hongze Cheng 已提交
481
  return (SDataRow)SL_GET_NODE_DATA(node);
H
Hongze Cheng 已提交
482 483 484 485
}

static FORCE_INLINE TSKEY tsdbNextIterKey(SSkipListIterator* pIter) {
  SDataRow row = tsdbNextIterRow(pIter);
H
TD-1548  
Hongze Cheng 已提交
486
  if (row == NULL) return TSDB_DATA_TIMESTAMP_NULL;
H
Hongze Cheng 已提交
487 488 489

  return dataRowKey(row);
}
H
TD-353  
Hongze Cheng 已提交
490

H
TD-1548  
Hongze Cheng 已提交
491 492 493 494 495 496 497
static FORCE_INLINE TKEY tsdbNextIterTKey(SSkipListIterator* pIter) {
  SDataRow row = tsdbNextIterRow(pIter);
  if (row == NULL) return TKEY_NULL;

  return dataRowTKey(row);
}

H
Hongze Cheng 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510
static FORCE_INLINE STsdbBufBlock* tsdbGetCurrBufBlock(STsdbRepo* pRepo) {
  ASSERT(pRepo != NULL);
  if (pRepo->mem == NULL) return NULL;

  SListNode* pNode = listTail(pRepo->mem->bufBlockList);
  if (pNode == NULL) return NULL;

  STsdbBufBlock* pBufBlock = NULL;
  tdListNodeGetData(pRepo->mem->bufBlockList, pNode, (void*)(&pBufBlock));

  return pBufBlock;
}

H
TD-353  
Hongze Cheng 已提交
511 512 513 514 515
// ------------------ tsdbFile.c
#define TSDB_KEY_FILEID(key, daysPerFile, precision) ((key) / tsMsPerDay[(precision)] / (daysPerFile))
#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
H
TD-353  
Hongze Cheng 已提交
516
#define TSDB_IS_FILE_OPENED(f) ((f)->fd > 0)
H
TD-353  
Hongze Cheng 已提交
517 518 519
#define TSDB_FGROUP_ITER_FORWARD TSDB_ORDER_ASC
#define TSDB_FGROUP_ITER_BACKWARD TSDB_ORDER_DESC

H
TD-353  
Hongze Cheng 已提交
520 521
STsdbFileH* tsdbNewFileH(STsdbCfg* pCfg);
void        tsdbFreeFileH(STsdbFileH* pFileH);
H
TD-353  
Hongze Cheng 已提交
522
int         tsdbOpenFileH(STsdbRepo* pRepo);
H
fix bug  
Hongze Cheng 已提交
523
void        tsdbCloseFileH(STsdbRepo* pRepo, bool isRestart);
H
Hongze Cheng 已提交
524
SFileGroup *tsdbCreateFGroup(STsdbRepo *pRepo, int fid, int level);
H
TD-353  
Hongze Cheng 已提交
525 526 527 528 529
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);
H
Hongze Cheng 已提交
530
int         tsdbCreateFile(SFile* pFile, STsdbRepo* pRepo, int fid, int type);
H
TD-353  
Hongze Cheng 已提交
531
SFileGroup* tsdbSearchFGroup(STsdbFileH* pFileH, int fid, int flags);
H
Hongze Cheng 已提交
532
int         tsdbGetFidLevel(int fid, SFidGroup fidg);
H
Hongze Cheng 已提交
533
void        tsdbRemoveFilesBeyondRetention(STsdbRepo* pRepo, SFidGroup* pFidGroup);
534
int         tsdbUpdateFileHeader(SFile* pFile);
H
TD-353  
Hongze Cheng 已提交
535
int         tsdbEncodeSFileInfo(void** buf, const STsdbFileInfo* pInfo);
H
TD-353  
Hongze Cheng 已提交
536
void*       tsdbDecodeSFileInfo(void* buf, STsdbFileInfo* pInfo);
H
TD-353  
Hongze Cheng 已提交
537
void        tsdbRemoveFileGroup(STsdbRepo* pRepo, SFileGroup* pFGroup);
H
Hongze Cheng 已提交
538
int         tsdbLoadFileHeader(SFile* pFile, uint32_t* version);
539
void        tsdbGetFileInfoImpl(char* fname, uint32_t* magic, int64_t* size);
H
Hongze Cheng 已提交
540
void        tsdbGetFidGroup(STsdbCfg* pCfg, SFidGroup* pFidGroup);
541
void        tsdbGetFidKeyRange(int daysPerFile, int8_t precision, int fileId, TSKEY *minKey, TSKEY *maxKey);
H
Hongze Cheng 已提交
542
int         tsdbApplyRetention(STsdbRepo* pRepo, SFidGroup *pFidGroup);
H
TD-353  
Hongze Cheng 已提交
543 544

// ------------------ tsdbRWHelper.c
H
TD-353  
Hongze Cheng 已提交
545 546
#define TSDB_HELPER_CLEAR_STATE 0x0        // Clear state
#define TSDB_HELPER_FILE_SET_AND_OPEN 0x1  // File is set
H
refact  
Hongze Cheng 已提交
547
#define TSDB_HELPER_IDX_LOAD 0x2           // SBlockIdx part is loaded
H
TD-353  
Hongze Cheng 已提交
548
#define TSDB_HELPER_TABLE_SET 0x4          // Table is set
H
refact  
Hongze Cheng 已提交
549 550
#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 已提交
551 552 553 554
#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 已提交
555 556
#define TSDB_MAX_SUBBLOCKS 8
#define IS_SUB_BLOCK(pBlock) ((pBlock)->numOfSubBlocks == 0)
H
TD-353  
Hongze Cheng 已提交
557 558 559
#define helperType(h) (h)->type
#define helperRepo(h) (h)->pRepo
#define helperState(h) (h)->state
H
Hongze Cheng 已提交
560
#define TSDB_NLAST_FILE_OPENED(h) ((h)->files.nLastF.fd > 0)
H
Hongze Cheng 已提交
561 562 563 564 565 566
#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 已提交
567 568 569 570 571 572

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 已提交
573
int  tsdbCloseHelperFile(SRWHelper* pHelper, bool hasError, SFileGroup* pGroup);
H
Hongze Cheng 已提交
574
int  tsdbSetHelperTable(SRWHelper* pHelper, STable* pTable, STsdbRepo* pRepo);
H
Hongze Cheng 已提交
575 576 577 578
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 已提交
579
int  tsdbLoadCompIdxImpl(SFile* pFile, uint32_t offset, uint32_t len, void* buffer);
H
refact  
Hongze Cheng 已提交
580
int  tsdbDecodeSBlockIdxImpl(void* buffer, uint32_t len, SBlockIdx** ppCompIdx, int* numOfIdx);
H
Hongze Cheng 已提交
581
int  tsdbLoadCompIdx(SRWHelper* pHelper, void* target);
H
refact  
Hongze Cheng 已提交
582
int  tsdbLoadCompInfoImpl(SFile* pFile, SBlockIdx* pIdx, SBlockInfo** ppCompInfo);
H
Hongze Cheng 已提交
583
int  tsdbLoadCompInfo(SRWHelper* pHelper, void* target);
H
refact  
Hongze Cheng 已提交
584
int  tsdbLoadCompData(SRWHelper* phelper, SBlock* pcompblock, void* target);
H
Hongze Cheng 已提交
585
void tsdbGetDataStatis(SRWHelper* pHelper, SDataStatis* pStatis, int numOfCols);
H
refact  
Hongze Cheng 已提交
586
int  tsdbLoadBlockDataCols(SRWHelper* pHelper, SBlock* pCompBlock, SBlockInfo* pCompInfo, int16_t* colIds,
H
Hongze Cheng 已提交
587
                           int numOfColIds);
H
refact  
Hongze Cheng 已提交
588
int  tsdbLoadBlockData(SRWHelper* pHelper, SBlock* pCompBlock, SBlockInfo* pCompInfo);
H
Hongze Cheng 已提交
589 590 591 592 593 594 595 596 597 598

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 已提交
599

H
TD-353  
Hongze Cheng 已提交
600 601
// ------------------ tsdbMain.c
#define REPO_ID(r) (r)->config.tsdbId
H
TD-353  
Hongze Cheng 已提交
602
#define IS_REPO_LOCKED(r) (r)->repoLocked
H
TD-353  
Hongze Cheng 已提交
603
#define TSDB_SUBMIT_MSG_HEAD_SIZE sizeof(SSubmitMsg)
H
TD-353  
Hongze Cheng 已提交
604

H
TD-353  
Hongze Cheng 已提交
605
char*       tsdbGetMetaFileName(char* rootDir);
H
Hongze Cheng 已提交
606
void        tsdbGetDataFileName(char* rootDir, int vid, int fid, int type, char* fname);
H
TD-353  
Hongze Cheng 已提交
607 608 609
int         tsdbLockRepo(STsdbRepo* pRepo);
int         tsdbUnlockRepo(STsdbRepo* pRepo);
char*       tsdbGetDataDirName(char* rootDir);
H
TD-987  
Hongze Cheng 已提交
610
int         tsdbGetNextMaxTables(int tid);
H
TD-353  
Hongze Cheng 已提交
611 612
STsdbMeta*  tsdbGetMeta(TSDB_REPO_T* pRepo);
STsdbFileH* tsdbGetFile(TSDB_REPO_T* pRepo);
H
Hongze Cheng 已提交
613
int         tsdbCheckCommit(STsdbRepo* pRepo);
H
TD-353  
Hongze Cheng 已提交
614

H
TD-1027  
Hongze Cheng 已提交
615 616 617 618 619
// ------------------ tsdbScan.c
int              tsdbScanFGroup(STsdbScanHandle* pScanHandle, char* rootDir, int fid);
STsdbScanHandle* tsdbNewScanHandle();
void             tsdbSetScanLogStream(STsdbScanHandle* pScanHandle, FILE* fLogStream);
int              tsdbSetAndOpenScanFile(STsdbScanHandle* pScanHandle, char* rootDir, int fid);
H
refact  
Hongze Cheng 已提交
620 621
int              tsdbScanSBlockIdx(STsdbScanHandle* pScanHandle);
int              tsdbScanSBlock(STsdbScanHandle* pScanHandle, int idx);
H
TD-1027  
Hongze Cheng 已提交
622 623 624
int              tsdbCloseScanFile(STsdbScanHandle* pScanHandle);
void             tsdbFreeScanHandle(STsdbScanHandle* pScanHandle);

H
Hongze Cheng 已提交
625 626 627
// ------------------ tsdbCommitQueue.c
int tsdbScheduleCommit(STsdbRepo *pRepo);

H
more  
hzcheng 已提交
628 629 630 631
#ifdef __cplusplus
}
#endif

H
TD-34  
hzcheng 已提交
632
#endif