tsdbMain.h 24.0 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
S
Shengliang Guan 已提交
39
extern int32_t tsdbDebugFlag;
H
hzcheng 已提交
40

H
Haojun Liao 已提交
41 42 43 44 45 46
#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 已提交
47

H
refact  
Hongze Cheng 已提交
48
// ================= OTHERS
H
TD-353  
Hongze Cheng 已提交
49

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

52 53 54 55
// 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 已提交
56
// Definitions
H
refact  
Hongze Cheng 已提交
57
// ================= tsdbMeta.c
H
Hongze Cheng 已提交
58 59
#define TSDB_MAX_TABLE_SCHEMAS 16

H
TD-34  
hzcheng 已提交
60
typedef struct STable {
H
Haojun Liao 已提交
61
  STableId       tableId;
H
TD-353  
Hongze Cheng 已提交
62 63 64 65 66
  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 已提交
67
  STSchema*      schema[TSDB_MAX_TABLE_SCHEMAS];
H
TD-353  
Hongze Cheng 已提交
68 69
  STSchema*      tagSchema;
  SKVRow         tagVal;
H
TD-353  
Hongze Cheng 已提交
70
  SSkipList*     pIndex;         // For TSDB_SUPER_TABLE, it is the skiplist index
H
TD-353  
Hongze Cheng 已提交
71 72
  void*          eventHandler;   // TODO
  void*          streamHandler;  // TODO
H
Hongze Cheng 已提交
73 74
  TSKEY          lastKey;
  SDataRow       lastRow;
H
TD-353  
Hongze Cheng 已提交
75 76
  char*          sql;
  void*          cqhandle;
H
Hongze Cheng 已提交
77
  SRWLatch       latch;  // TODO: implementa latch functions
S
TD-1057  
Shengliang Guan 已提交
78
  T_REF_DECLARE()
H
TD-34  
hzcheng 已提交
79 80 81
} STable;

typedef struct {
H
TD-353  
Hongze Cheng 已提交
82 83 84
  pthread_rwlock_t rwLock;

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

H
refact  
Hongze Cheng 已提交
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 148 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 178 179 180 181
#define TSDB_INIT_NTABLES 1024
#define TABLE_TYPE(t) (t)->type
#define TABLE_NAME(t) (t)->name
#define TABLE_CHAR_NAME(t) TABLE_NAME(t)->data
#define TABLE_UID(t) (t)->tableId.uid
#define TABLE_TID(t) (t)->tableId.tid
#define TABLE_SUID(t) (t)->suid
#define TSDB_META_FILE_MAGIC(m) KVSTORE_MAGIC((m)->pStore)
#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))

STsdbMeta* tsdbNewMeta(STsdbCfg* pCfg);
void       tsdbFreeMeta(STsdbMeta* pMeta);
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);
void       tsdbRefTable(STable* pTable);
void       tsdbUnRefTable(STable* pTable);
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;

  if (lock) TSDB_RLOCK_TABLE(pDTable);
  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:
  if (lock) TSDB_RUNLOCK_TABLE(pDTable);
  return pSchema;
}

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

static FORCE_INLINE TSKEY tsdbGetTableLastKeyImpl(STable* pTable) {
  ASSERT(pTable->lastRow == NULL || pTable->lastKey == dataRowKey(pTable->lastRow));
  return pTable->lastKey;
}

H
refact  
Hongze Cheng 已提交
182
// ================= tsdbBuffer.c
H
TD-34  
hzcheng 已提交
183
typedef struct {
H
TD-353  
Hongze Cheng 已提交
184 185 186 187 188
  int64_t blockId;
  int     offset;
  int     remain;
  char    data[];
} STsdbBufBlock;
H
TD-34  
hzcheng 已提交
189 190

typedef struct {
H
TD-353  
Hongze Cheng 已提交
191 192 193 194 195 196 197 198
  pthread_cond_t poolNotEmpty;
  int            bufBlockSize;
  int            tBufBlocks;
  int            nBufBlocks;
  int64_t        index;
  SList*         bufBlockList;
} STsdbBufPool;

H
refact  
Hongze Cheng 已提交
199 200 201 202 203 204 205 206
#define TSDB_BUFFER_RESERVE 1024  // Reseve 1K as commit threshold

STsdbBufPool* tsdbNewBufPool();
void          tsdbFreeBufPool(STsdbBufPool* pBufPool);
int           tsdbOpenBufPool(STsdbRepo* pRepo);
void          tsdbCloseBufPool(STsdbRepo* pRepo);
SListNode*    tsdbAllocBufBlockFromPool(STsdbRepo* pRepo);

H
TD-353  
Hongze Cheng 已提交
207
// ------------------ tsdbMemTable.c
H
refact  
Hongze Cheng 已提交
208 209 210 211 212 213 214 215 216 217
typedef struct {
  int   rowsInserted;
  int   rowsUpdated;
  int   rowsDeleteSucceed;
  int   rowsDeleteFailed;
  int   nOperations;
  TSKEY keyFirst;
  TSKEY keyLast;
} SMergeInfo;

H
Hongze Cheng 已提交
218 219 220 221 222
typedef struct {
  STable *           pTable;
  SSkipListIterator *pIter;
} SCommitIter;

H
TD-34  
hzcheng 已提交
223
typedef struct {
H
TD-353  
Hongze Cheng 已提交
224 225 226 227 228 229
  uint64_t   uid;
  TSKEY      keyFirst;
  TSKEY      keyLast;
  int64_t    numOfRows;
  SSkipList* pData;
} STableData;
H
TD-34  
hzcheng 已提交
230 231

typedef struct {
S
TD-1057  
Shengliang Guan 已提交
232
  T_REF_DECLARE()
H
TD-987  
Hongze Cheng 已提交
233
  SRWLatch     latch;
H
TD-353  
Hongze Cheng 已提交
234 235 236
  TSKEY        keyFirst;
  TSKEY        keyLast;
  int64_t      numOfRows;
H
TD-987  
Hongze Cheng 已提交
237
  int32_t      maxTables;
H
TD-353  
Hongze Cheng 已提交
238 239
  STableData** tData;
  SList*       actList;
H
Hongze Cheng 已提交
240
  SList*       extraBuffList;
H
TD-353  
Hongze Cheng 已提交
241 242
  SList*       bufBlockList;
} SMemTable;
H
TD-34  
hzcheng 已提交
243

H
TD-353  
Hongze Cheng 已提交
244
enum { TSDB_UPDATE_META, TSDB_DROP_META };
S
TD-1057  
Shengliang Guan 已提交
245 246 247 248 249

#ifdef WINDOWS
#pragma pack(push ,1) 
typedef struct {
#else
H
TD-353  
Hongze Cheng 已提交
250
typedef struct __attribute__((packed)){
S
TD-1057  
Shengliang Guan 已提交
251
#endif
H
TD-353  
Hongze Cheng 已提交
252 253 254
  char     act;
  uint64_t uid;
} SActObj;
S
TD-1057  
Shengliang Guan 已提交
255 256 257
#ifdef WINDOWS
#pragma pack(pop) 
#endif
H
TD-353  
Hongze Cheng 已提交
258 259 260 261 262 263

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

H
refact  
Hongze Cheng 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
int   tsdbRefMemTable(STsdbRepo* pRepo, SMemTable* pMemTable);
int   tsdbUnRefMemTable(STsdbRepo* pRepo, SMemTable* pMemTable);
int   tsdbTakeMemSnapshot(STsdbRepo* pRepo, SMemTable** pMem, SMemTable** pIMem);
void  tsdbUnTakeMemSnapShot(STsdbRepo* pRepo, SMemTable* pMem, SMemTable* pIMem);
void* tsdbAllocBytes(STsdbRepo* pRepo, int bytes);
int   tsdbAsyncCommit(STsdbRepo* pRepo);
int   tsdbLoadDataFromCache(STable* pTable, SSkipListIterator* pIter, TSKEY maxKey, int maxRowsToRead, SDataCols* pCols,
                            TKEY* filterKeys, int nFilterKeys, bool keepDup, SMergeInfo* pMergeInfo);
void* tsdbCommitData(STsdbRepo* pRepo);

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

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

  return (SDataRow)SL_GET_NODE_DATA(node);
}

static FORCE_INLINE TSKEY tsdbNextIterKey(SSkipListIterator* pIter) {
  SDataRow row = tsdbNextIterRow(pIter);
  if (row == NULL) return TSDB_DATA_TIMESTAMP_NULL;

  return dataRowKey(row);
}

static FORCE_INLINE TKEY tsdbNextIterTKey(SSkipListIterator* pIter) {
  SDataRow row = tsdbNextIterRow(pIter);
  if (row == NULL) return TKEY_NULL;

  return dataRowTKey(row);
}

H
Hongze Cheng 已提交
297
// ================= tsdbFile.c
H
Hongze Cheng 已提交
298 299 300
#define TSDB_FILE_HEAD_SIZE 512
#define TSDB_FILE_DELIMITER 0xF00AFA0F
#define TSDB_FILE_INIT_MAGIC 0xFFFFFFFF
H
Hongze Cheng 已提交
301

H
Hongze Cheng 已提交
302 303 304 305 306 307 308 309
typedef enum {
  TSDB_FILE_HEAD = 0,
  TSDB_FILE_DATA,
  TSDB_FILE_LAST,
  TSDB_FILE_MAX,
  TSDB_FILE_META,
  TSDB_FILE_MANIFEST
} TSDB_FILE_T;
H
Hongze Cheng 已提交
310 311

// For meta file
H
Hongze Cheng 已提交
312
typedef struct {
H
Hongze Cheng 已提交
313 314 315 316 317 318 319 320 321 322 323 324
  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 已提交
325

H
Hongze Cheng 已提交
326 327 328 329 330 331 332 333 334
void    tsdbInitMFile(SMFile* pMFile, int vid, int ver, SMFInfo* pInfo);
int     tsdbOpenMFile(SMFile* pMFile, int flags);
void    tsdbCloseMFile(SMFile* pMFile);
int64_t tsdbSeekMFile(SMFile* pMFile, int64_t offset, int whence);
int64_t tsdbWriteMFile(SMFile* pMFile, void* buf, int64_t nbyte);
int64_t tsdbTellMFile(SMFile *pMFile);
int     tsdbEncodeMFile(void** buf, SMFile* pMFile);
void*   tsdbDecodeMFile(void* buf, SMFile* pMFile);

H
Hongze Cheng 已提交
335
// For .head/.data/.last file
H
more  
Hongze Cheng 已提交
336
typedef struct {
H
Hongze Cheng 已提交
337
  uint32_t magic;
H
TD-353  
Hongze Cheng 已提交
338 339 340
  uint32_t len;
  uint32_t totalBlocks;
  uint32_t totalSubBlocks;
H
Hongze Cheng 已提交
341
  uint32_t offset;
H
Hongze Cheng 已提交
342 343 344
  uint64_t size;
  uint64_t tombSize;
} SDFInfo;
H
TD-34  
hzcheng 已提交
345 346

typedef struct {
H
Hongze Cheng 已提交
347 348 349 350
  SDFInfo info;
  TFILE   f;
  int     fd;
} SDFile;
H
more  
Hongze Cheng 已提交
351

H
Hongze Cheng 已提交
352 353
void    tsdbInitDFile(SDFile* pDFile, int vid, int fid, int ver, int level, int id, const SDFInfo* pInfo,
                      TSDB_FILE_T ftype);
H
Hongze Cheng 已提交
354
void    tsdbInitDFileWithOld(SDFile* pDFile, SDFile* pOldDFile);
H
Hongze Cheng 已提交
355 356 357 358
int     tsdbOpenDFile(SDFile* pDFile, int flags);
void    tsdbCloseDFile(SDFile* pDFile);
int64_t tsdbSeekDFile(SDFile* pDFile, int64_t offset, int whence);
int64_t tsdbWriteDFile(SDFile* pDFile, void* buf, int64_t nbyte);
H
Hongze Cheng 已提交
359
int64_t tsdbAppendDFile(SDFile* pDFile, void* buf, int64_t nbyte, int64_t* offset);
H
Hongze Cheng 已提交
360 361 362
int64_t tsdbTellDFile(SDFile* pDFile);
int     tsdbEncodeDFile(void** buf, SDFile* pDFile);
void*   tsdbDecodeDFile(void* buf, SDFile* pDFile);
H
Hongze Cheng 已提交
363
void    tsdbUpdateDFileMagic(SDFile* pDFile, void* pCksm);
H
Hongze Cheng 已提交
364

H
hzcheng 已提交
365
typedef struct {
H
Hongze Cheng 已提交
366
  int    fid;
H
Hongze Cheng 已提交
367 368 369
  int    state;
  SDFile files[TSDB_FILE_MAX];
} SDFileSet;
H
hzcheng 已提交
370

H
Hongze Cheng 已提交
371
#define TSDB_FILE_FULL_NAME(f) TFILE_NAME(&((f)->f))
H
Hongze Cheng 已提交
372 373 374
#define TSDB_DFILE_IN_SET(s, t) ((s)->files + (t))

void tsdbInitDFileSet(SDFileSet* pSet, int vid, int fid, int ver, int level, int id);
H
Hongze Cheng 已提交
375
void tsdbInitDFileSetWithOld(SDFileSet *pSet, SDFileSet *pOldSet);
H
Hongze Cheng 已提交
376 377 378
int  tsdbOpenDFileSet(SDFileSet* pSet, int flags);
void tsdbCloseDFileSet(SDFileSet* pSet);
int  tsdbUpdateDFileSetHeader(SDFileSet* pSet);
H
Hongze Cheng 已提交
379
int  tsdbCopyDFileSet(SDFileSet* pFromSet, SDFileSet* pToSet);
H
Hongze Cheng 已提交
380

H
Hongze Cheng 已提交
381 382
/* Statistic information of the TSDB file system.
 */
H
hzcheng 已提交
383
typedef struct {
H
Hongze Cheng 已提交
384 385 386 387 388
  int64_t fsversion; // file system version, related to program
  int64_t version;
  int64_t totalPoints;
  int64_t totalStorage;
} STsdbFSMeta;
H
TD-353  
Hongze Cheng 已提交
389

H
Hongze Cheng 已提交
390 391 392 393
typedef struct {
  int64_t     version;
  STsdbFSMeta meta;
  SMFile      mf;  // meta file
H
Hongze Cheng 已提交
394 395
  SArray*     df;  // data file array
} SFSVer;
H
more  
Hongze Cheng 已提交
396

H
TD-34  
hzcheng 已提交
397
typedef struct {
H
Hongze Cheng 已提交
398 399
  pthread_rwlock_t lock;

H
Hongze Cheng 已提交
400
  SFSVer fsv;
H
Hongze Cheng 已提交
401 402
} STsdbFS;

H
Hongze Cheng 已提交
403 404 405 406 407 408 409
typedef struct {
  int        version;  // current FS version
  int        index;
  int        fid;
  SDFileSet* pSet;
} SFSIter;

H
Hongze Cheng 已提交
410 411 412 413
#define TSDB_FILE_INFO(tf) (&((tf)->info))
#define TSDB_FILE_F(tf) (&((tf)->f)))
#define TSDB_FILE_FD(tf) ((tf)->fd)

H
Hongze Cheng 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427
int        tsdbOpenFS(STsdbRepo* pRepo);
void       tsdbCloseFS(STsdbRepo* pRepo);
int        tsdbFSNewTxn(STsdbRepo* pRepo);
int        tsdbFSEndTxn(STsdbRepo* pRepo, bool hasError);
int        tsdbUpdateMFile(STsdbRepo* pRepo, SMFile* pMFile);
int        tsdbUpdateDFileSet(STsdbRepo* pRepo, SDFileSet* pSet);
void       tsdbRemoveExpiredDFileSet(STsdbRepo* pRepo, int mfid);
int        tsdbRemoveDFileSet(SDFileSet* pSet);
int        tsdbEncodeMFInfo(void** buf, SMFInfo* pInfo);
void*      tsdbDecodeMFInfo(void* buf, SMFInfo* pInfo);
SDFileSet  tsdbMoveDFileSet(SDFileSet* pOldSet, int to);
int        tsdbInitFSIter(STsdbRepo* pRepo, SFSIter* pIter);
SDFileSet* tsdbFSIterNext(SFSIter* pIter);
int        tsdbCreateDFileSet(int fid, int level, SDFileSet* pSet);
H
Hongze Cheng 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

static FORCE_INLINE int tsdbRLockFS(STsdbFS *pFs) {
  int code = pthread_rwlock_rdlock(&(pFs->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

static FORCE_INLINE int tsdbWLockFS(STsdbFS *pFs) {
  int code = pthread_rwlock_wrlock(&(pFs->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

static FORCE_INLINE int tsdbUnLockFS(STsdbFS *pFs) {
  int code = pthread_rwlock_unlock(&(pFs->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
// ================= 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 已提交
481

H
Hongze Cheng 已提交
482
// ================= 
H
Hongze Cheng 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
// 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 已提交
547
#define TSDB_KEY_FILEID(key, daysPerFile, precision) ((key) / tsMsPerDay[(precision)] / (daysPerFile))
H
Hongze Cheng 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
// #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);
H
Hongze Cheng 已提交
576
void        tsdbGetFidKeyRange(int daysPerFile, int8_t precision, int fileId, TSKEY *minKey, TSKEY *maxKey);
H
Hongze Cheng 已提交
577
// int         tsdbApplyRetention(STsdbRepo* pRepo, SFidGroup *pFidGroup);
H
Hongze Cheng 已提交
578

H
refact  
Hongze Cheng 已提交
579
// ================= tsdbMain.c
H
Hongze Cheng 已提交
580 581 582 583 584 585 586 587 588 589 590 591
typedef struct {
  int32_t  totalLen;
  int32_t  len;
  SDataRow row;
} SSubmitBlkIter;

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

H
refact  
Hongze Cheng 已提交
592
struct STsdbRepo {
H
TD-353  
Hongze Cheng 已提交
593 594 595 596 597 598 599 600 601 602
  int8_t state;

  char*           rootDir;
  STsdbCfg        config;
  STsdbAppH       appH;
  STsdbStat       stat;
  STsdbMeta*      tsdbMeta;
  STsdbBufPool*   pPool;
  SMemTable*      mem;
  SMemTable*      imem;
H
Hongze Cheng 已提交
603
  STsdbFS*        fs;
H
Hongze Cheng 已提交
604
  sem_t           readyToCommit;
H
TD-353  
Hongze Cheng 已提交
605 606
  pthread_mutex_t mutex;
  bool            repoLocked;
H
Hongze Cheng 已提交
607
  int32_t         code; // Commit code
H
refact  
Hongze Cheng 已提交
608 609 610
};

#define REPO_ID(r) (r)->config.tsdbId
H
Hongze Cheng 已提交
611
#define REPO_CFG(r) (&((r)->config))
H
refact  
Hongze Cheng 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
#define IS_REPO_LOCKED(r) (r)->repoLocked
#define TSDB_SUBMIT_MSG_HEAD_SIZE sizeof(SSubmitMsg)

char*       tsdbGetMetaFileName(char* rootDir);
void        tsdbGetDataFileName(char* rootDir, int vid, int fid, int type, char* fname);
int         tsdbLockRepo(STsdbRepo* pRepo);
int         tsdbUnlockRepo(STsdbRepo* pRepo);
char*       tsdbGetDataDirName(char* rootDir);
int         tsdbGetNextMaxTables(int tid);
STsdbMeta*  tsdbGetMeta(TSDB_REPO_T* pRepo);
STsdbFileH* tsdbGetFile(TSDB_REPO_T* pRepo);
int         tsdbCheckCommit(STsdbRepo* pRepo);

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

H
Hongze Cheng 已提交
638
#include "tsdbReadImpl.h"
639

H
Hongze Cheng 已提交
640
#if 0
H
Hongze Cheng 已提交
641
// ================= tsdbRWHelper.c
H
TD-34  
hzcheng 已提交
642

H
hzcheng 已提交
643 644 645
typedef enum { TSDB_WRITE_HELPER, TSDB_READ_HELPER } tsdb_rw_helper_t;

typedef struct {
H
Hongze Cheng 已提交
646 647
  TSKEY      minKey;
  TSKEY      maxKey;
H
Hongze Cheng 已提交
648 649
  SDFileSet  rSet;
  SDFileSet  wSet;
H
hzcheng 已提交
650 651 652
} SHelperFile;

typedef struct {
653 654
  uint64_t uid;
  int32_t  tid;
H
hzcheng 已提交
655 656
} SHelperTable;

H
Hongze Cheng 已提交
657
typedef struct {
H
refact  
Hongze Cheng 已提交
658
  SBlockIdx* pIdxArray;
H
Hongze Cheng 已提交
659 660 661 662
  int       numOfIdx;
  int       curIdx;
} SIdxH;

H
hzcheng 已提交
663
typedef struct {
H
TD-353  
Hongze Cheng 已提交
664
  tsdb_rw_helper_t type;
H
TD-100  
hzcheng 已提交
665

H
TD-353  
Hongze Cheng 已提交
666 667
  STsdbRepo* pRepo;
  int8_t     state;
H
TD-100  
hzcheng 已提交
668
  // For file set usage
H
hzcheng 已提交
669
  SHelperFile files;
H
Hongze Cheng 已提交
670
  SIdxH       idxH;
H
refact  
Hongze Cheng 已提交
671
  SBlockIdx    curCompIdx;
H
Hongze Cheng 已提交
672
  void*       pWIdx;
H
TD-100  
hzcheng 已提交
673
  // For table set usage
H
hzcheng 已提交
674
  SHelperTable tableInfo;
H
refact  
Hongze Cheng 已提交
675
  SBlockInfo*   pCompInfo;
H
TD-100  
hzcheng 已提交
676 677
  bool         hasOldLastBlock;
  // For block set usage
H
refact  
Hongze Cheng 已提交
678
  SBlockData* pCompData;
H
TD-353  
Hongze Cheng 已提交
679 680 681
  SDataCols* pDataCols[2];
  void*      pBuffer;     // Buffer to hold the whole data block
  void*      compBuffer;  // Buffer for temperary compress/decompress purpose
H
hzcheng 已提交
682 683
} SRWHelper;

H
TD-353  
Hongze Cheng 已提交
684 685
#define TSDB_HELPER_CLEAR_STATE 0x0        // Clear state
#define TSDB_HELPER_FILE_SET_AND_OPEN 0x1  // File is set
H
refact  
Hongze Cheng 已提交
686
#define TSDB_HELPER_IDX_LOAD 0x2           // SBlockIdx part is loaded
H
TD-353  
Hongze Cheng 已提交
687
#define TSDB_HELPER_TABLE_SET 0x4          // Table is set
H
refact  
Hongze Cheng 已提交
688 689
#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 已提交
690 691 692 693
#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 已提交
694 695
#define TSDB_MAX_SUBBLOCKS 8
#define IS_SUB_BLOCK(pBlock) ((pBlock)->numOfSubBlocks == 0)
H
TD-353  
Hongze Cheng 已提交
696 697 698
#define helperType(h) (h)->type
#define helperRepo(h) (h)->pRepo
#define helperState(h) (h)->state
H
Hongze Cheng 已提交
699
#define TSDB_NLAST_FILE_OPENED(h) ((h)->files.nLastF.fd > 0)
H
Hongze Cheng 已提交
700 701 702 703 704 705
#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 已提交
706 707 708 709 710 711

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 已提交
712
int  tsdbCloseHelperFile(SRWHelper* pHelper, bool hasError, SFileGroup* pGroup);
H
Hongze Cheng 已提交
713
int  tsdbSetHelperTable(SRWHelper* pHelper, STable* pTable, STsdbRepo* pRepo);
H
Hongze Cheng 已提交
714 715 716 717
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 已提交
718
int  tsdbLoadCompIdxImpl(SFile* pFile, uint32_t offset, uint32_t len, void* buffer);
H
refact  
Hongze Cheng 已提交
719
int  tsdbDecodeSBlockIdxImpl(void* buffer, uint32_t len, SBlockIdx** ppCompIdx, int* numOfIdx);
H
Hongze Cheng 已提交
720
int  tsdbLoadCompIdx(SRWHelper* pHelper, void* target);
H
refact  
Hongze Cheng 已提交
721
int  tsdbLoadCompInfoImpl(SFile* pFile, SBlockIdx* pIdx, SBlockInfo** ppCompInfo);
H
Hongze Cheng 已提交
722
int  tsdbLoadCompInfo(SRWHelper* pHelper, void* target);
H
refact  
Hongze Cheng 已提交
723
int  tsdbLoadCompData(SRWHelper* phelper, SBlock* pcompblock, void* target);
H
Hongze Cheng 已提交
724
void tsdbGetDataStatis(SRWHelper* pHelper, SDataStatis* pStatis, int numOfCols);
H
refact  
Hongze Cheng 已提交
725
int  tsdbLoadBlockDataCols(SRWHelper* pHelper, SBlock* pCompBlock, SBlockInfo* pCompInfo, int16_t* colIds,
H
Hongze Cheng 已提交
726
                           int numOfColIds);
H
refact  
Hongze Cheng 已提交
727
int  tsdbLoadBlockData(SRWHelper* pHelper, SBlock* pCompBlock, SBlockInfo* pCompInfo);
H
Hongze Cheng 已提交
728 729 730 731 732 733 734 735 736 737

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

H
Hongze Cheng 已提交
739 740
#endif

H
refact  
Hongze Cheng 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
// ================= 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 已提交
760 761 762
// ------------------ tsdbCommitQueue.c
int tsdbScheduleCommit(STsdbRepo *pRepo);

H
more  
hzcheng 已提交
763 764 765 766
#ifdef __cplusplus
}
#endif

H
TD-34  
hzcheng 已提交
767
#endif