tsdb.h 22.3 KB
Newer Older
L
Liu Jicong 已提交
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
Hongze Cheng 已提交
15 16 17 18

#ifndef _TD_VNODE_TSDB_H_
#define _TD_VNODE_TSDB_H_

H
Hongze Cheng 已提交
19 20
#include "vnodeInt.h"

H
Hongze Cheng 已提交
21 22 23 24
#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
25 26
// tsdbDebug ================
// clang-format off
H
Hongze Cheng 已提交
27 28 29 30 31 32
#define tsdbFatal(...) do { if (tsdbDebugFlag & DEBUG_FATAL) { taosPrintLog("TSD FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }}     while(0)
#define tsdbError(...) do { if (tsdbDebugFlag & DEBUG_ERROR) { taosPrintLog("TSD ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }}     while(0)
#define tsdbWarn(...)  do { if (tsdbDebugFlag & DEBUG_WARN)  { taosPrintLog("TSD WARN ", DEBUG_WARN, 255, __VA_ARGS__); }}       while(0)
#define tsdbInfo(...)  do { if (tsdbDebugFlag & DEBUG_INFO)  { taosPrintLog("TSD ", DEBUG_INFO, 255, __VA_ARGS__); }}            while(0)
#define tsdbDebug(...) do { if (tsdbDebugFlag & DEBUG_DEBUG) { taosPrintLog("TSD ", DEBUG_DEBUG, tsdbDebugFlag, __VA_ARGS__); }} while(0)
#define tsdbTrace(...) do { if (tsdbDebugFlag & DEBUG_TRACE) { taosPrintLog("TSD ", DEBUG_TRACE, tsdbDebugFlag, __VA_ARGS__); }} while(0)
H
Hongze Cheng 已提交
33
// clang-format on
H
Hongze Cheng 已提交
34

H
Hongze Cheng 已提交
35 36 37 38 39 40 41 42 43 44 45
typedef struct TSDBROW       TSDBROW;
typedef struct TABLEID       TABLEID;
typedef struct TSDBKEY       TSDBKEY;
typedef struct SDelData      SDelData;
typedef struct SDelIdx       SDelIdx;
typedef struct STbData       STbData;
typedef struct SMemTable     SMemTable;
typedef struct STbDataIter   STbDataIter;
typedef struct SMapData      SMapData;
typedef struct SBlockIdx     SBlockIdx;
typedef struct SBlock        SBlock;
H
Hongze Cheng 已提交
46
typedef struct SBlockL       SBlockL;
H
Hongze Cheng 已提交
47
typedef struct SColData      SColData;
H
Hongze Cheng 已提交
48
typedef struct SDiskDataHdr  SDiskDataHdr;
H
Hongze Cheng 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
typedef struct SBlockData    SBlockData;
typedef struct SDelFile      SDelFile;
typedef struct SHeadFile     SHeadFile;
typedef struct SDataFile     SDataFile;
typedef struct SLastFile     SLastFile;
typedef struct SSmaFile      SSmaFile;
typedef struct SDFileSet     SDFileSet;
typedef struct SDataFWriter  SDataFWriter;
typedef struct SDataFReader  SDataFReader;
typedef struct SDelFWriter   SDelFWriter;
typedef struct SDelFReader   SDelFReader;
typedef struct SRowIter      SRowIter;
typedef struct STsdbFS       STsdbFS;
typedef struct SRowMerger    SRowMerger;
H
Hongze Cheng 已提交
63
typedef struct STsdbReadSnap STsdbReadSnap;
H
Hongze Cheng 已提交
64 65
typedef struct SBlockInfo    SBlockInfo;
typedef struct SSmaInfo      SSmaInfo;
H
Hongze Cheng 已提交
66
typedef struct SBlockCol     SBlockCol;
H
Hongze Cheng 已提交
67

H
Hongze Cheng 已提交
68
#define TSDB_FILE_DLMT     ((uint32_t)0xF00AFA0F)
H
Hongze Cheng 已提交
69
#define TSDB_MAX_SUBBLOCKS 8
H
Hongze Cheng 已提交
70
#define TSDB_FHDR_SIZE     512
H
refact  
Hongze Cheng 已提交
71

H
Hongze Cheng 已提交
72 73 74
#define HAS_NONE  ((int8_t)0x1)
#define HAS_NULL  ((int8_t)0x2)
#define HAS_VALUE ((int8_t)0x4)
H
Hongze Cheng 已提交
75

H
Hongze Cheng 已提交
76 77 78
#define VERSION_MIN 0
#define VERSION_MAX INT64_MAX

H
Hongze Cheng 已提交
79 80 81
#define TSDBKEY_MIN ((TSDBKEY){.ts = TSKEY_MIN, .version = VERSION_MIN})
#define TSDBKEY_MAX ((TSDBKEY){.ts = TSKEY_MAX, .version = VERSION_MAX})

H
Hongze Cheng 已提交
82
// tsdbUtil.c ==============================================================================================
H
Hongze Cheng 已提交
83
// TSDBROW
H
Hongze Cheng 已提交
84 85
#define TSDBROW_TS(ROW)                       (((ROW)->type == 0) ? (ROW)->pTSRow->ts : (ROW)->pBlockData->aTSKEY[(ROW)->iRow])
#define TSDBROW_VERSION(ROW)                  (((ROW)->type == 0) ? (ROW)->version : (ROW)->pBlockData->aVersion[(ROW)->iRow])
H
Hongze Cheng 已提交
86
#define TSDBROW_SVERSION(ROW)                 TD_ROW_SVER((ROW)->pTSRow)
H
Hongze Cheng 已提交
87
#define TSDBROW_KEY(ROW)                      ((TSDBKEY){.version = TSDBROW_VERSION(ROW), .ts = TSDBROW_TS(ROW)})
H
Hongze Cheng 已提交
88 89
#define tsdbRowFromTSRow(VERSION, TSROW)      ((TSDBROW){.type = 0, .version = (VERSION), .pTSRow = (TSROW)})
#define tsdbRowFromBlockData(BLOCKDATA, IROW) ((TSDBROW){.type = 1, .pBlockData = (BLOCKDATA), .iRow = (IROW)})
H
Hongze Cheng 已提交
90 91 92
void    tsdbRowGetColVal(TSDBROW *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal);
int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow);
int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow);
H
Hongze Cheng 已提交
93
int32_t tsdbRowCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
94
// SRowIter
H
Hongze Cheng 已提交
95
void     tRowIterInit(SRowIter *pIter, TSDBROW *pRow, STSchema *pTSchema);
H
Hongze Cheng 已提交
96
SColVal *tRowIterNext(SRowIter *pIter);
H
Hongze Cheng 已提交
97
// SRowMerger
98 99 100
int32_t tRowMergerInit2(SRowMerger *pMerger, STSchema *pResTSchema, TSDBROW *pRow, STSchema *pTSchema);
int32_t tRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema);

H
Hongze Cheng 已提交
101 102 103 104
int32_t tRowMergerInit(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema);
void    tRowMergerClear(SRowMerger *pMerger);
int32_t tRowMerge(SRowMerger *pMerger, TSDBROW *pRow);
int32_t tRowMergerGetRow(SRowMerger *pMerger, STSRow **ppRow);
H
Hongze Cheng 已提交
105
// TABLEID
H
Hongze Cheng 已提交
106
int32_t tTABLEIDCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
107
// TSDBKEY
H
Hongze Cheng 已提交
108 109
#define MIN_TSDBKEY(KEY1, KEY2) ((tsdbKeyCmprFn(&(KEY1), &(KEY2)) < 0) ? (KEY1) : (KEY2))
#define MAX_TSDBKEY(KEY1, KEY2) ((tsdbKeyCmprFn(&(KEY1), &(KEY2)) > 0) ? (KEY1) : (KEY2))
H
Hongze Cheng 已提交
110 111 112
// SBlockCol
int32_t tPutBlockCol(uint8_t *p, void *ph);
int32_t tGetBlockCol(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
113
int32_t tBlockColCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
114
// SBlock
H
Hongze Cheng 已提交
115
void    tBlockReset(SBlock *pBlock);
H
Hongze Cheng 已提交
116 117
int32_t tPutBlock(uint8_t *p, void *ph);
int32_t tGetBlock(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
118
int32_t tBlockCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
119
bool    tBlockHasSma(SBlock *pBlock);
H
Hongze Cheng 已提交
120 121 122
// SBlockL
int32_t tPutBlockL(uint8_t *p, void *ph);
int32_t tGetBlockL(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
123 124 125
// SBlockIdx
int32_t tPutBlockIdx(uint8_t *p, void *ph);
int32_t tGetBlockIdx(uint8_t *p, void *ph);
126
int32_t tCmprBlockIdx(void const *lhs, void const *rhs);
127
int32_t tCmprBlockL(void const *lhs, void const *rhs);
H
Hongze Cheng 已提交
128
// SColdata
H
Hongze Cheng 已提交
129 130
void    tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t smaOn);
void    tColDataReset(SColData *pColData);
H
Hongze Cheng 已提交
131
void    tColDataClear(void *ph);
H
Hongze Cheng 已提交
132
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal);
H
Hongze Cheng 已提交
133
int32_t tColDataGetValue(SColData *pColData, int32_t iRow, SColVal *pColVal);
H
Hongze Cheng 已提交
134
int32_t tColDataCopy(SColData *pColDataSrc, SColData *pColDataDest);
H
Hongze Cheng 已提交
135 136
int32_t tPutColData(uint8_t *p, SColData *pColData);
int32_t tGetColData(uint8_t *p, SColData *pColData);
H
Hongze Cheng 已提交
137
// SBlockData
H
Hongze Cheng 已提交
138 139
#define tBlockDataFirstRow(PBLOCKDATA) tsdbRowFromBlockData(PBLOCKDATA, 0)
#define tBlockDataLastRow(PBLOCKDATA)  tsdbRowFromBlockData(PBLOCKDATA, (PBLOCKDATA)->nRow - 1)
H
Hongze Cheng 已提交
140 141
#define tBlockDataFirstKey(PBLOCKDATA) TSDBROW_KEY(&tBlockDataFirstRow(PBLOCKDATA))
#define tBlockDataLastKey(PBLOCKDATA)  TSDBROW_KEY(&tBlockDataLastRow(PBLOCKDATA))
H
Hongze Cheng 已提交
142

H
Hongze Cheng 已提交
143 144 145
int32_t   tBlockDataCreate(SBlockData *pBlockData);
void      tBlockDataDestroy(SBlockData *pBlockData, int8_t deepClear);
int32_t   tBlockDataInit(SBlockData *pBlockData, int64_t suid, int64_t uid, STSchema *pTSchema);
H
Hongze Cheng 已提交
146
int32_t   tBlockDataInitEx(SBlockData *pBlockData, SBlockData *pBlockDataFrom);
H
Hongze Cheng 已提交
147 148 149
void      tBlockDataReset(SBlockData *pBlockData);
int32_t   tBlockDataAppendRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema, int64_t uid);
void      tBlockDataClear(SBlockData *pBlockData);
H
Hongze Cheng 已提交
150 151
SColData *tBlockDataGetColDataByIdx(SBlockData *pBlockData, int32_t idx);
void      tBlockDataGetColData(SBlockData *pBlockData, int16_t cid, SColData **ppColData);
H
Hongze Cheng 已提交
152 153
int32_t   tBlockDataCopy(SBlockData *pBlockDataSrc, SBlockData *pBlockDataDest);
int32_t   tBlockDataMerge(SBlockData *pBlockData1, SBlockData *pBlockData2, SBlockData *pBlockData);
H
Hongze Cheng 已提交
154
int32_t   tBlockDataAddColData(SBlockData *pBlockData, int32_t iColData, SColData **ppColData);
H
Hongze Cheng 已提交
155 156
int32_t   tCmprBlockData(SBlockData *pBlockData, int8_t cmprAlg, uint8_t **ppOut, int32_t *szOut, uint8_t *aBuf[],
                         int32_t aBufN[]);
H
Hongze Cheng 已提交
157
int32_t   tDecmprBlockData(uint8_t *pIn, int32_t szIn, SBlockData *pBlockData, uint8_t *aBuf[]);
H
Hongze Cheng 已提交
158 159 160
// SDiskDataHdr
int32_t tPutDiskDataHdr(uint8_t *p, void *ph);
int32_t tGetDiskDataHdr(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
161
// SDelIdx
H
Hongze Cheng 已提交
162 163
int32_t tPutDelIdx(uint8_t *p, void *ph);
int32_t tGetDelIdx(uint8_t *p, void *ph);
164
int32_t tCmprDelIdx(void const *lhs, void const *rhs);
H
Hongze Cheng 已提交
165
// SDelData
H
Hongze Cheng 已提交
166 167
int32_t tPutDelData(uint8_t *p, void *ph);
int32_t tGetDelData(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
168
// SMapData
H
Hongze Cheng 已提交
169
#define tMapDataInit() ((SMapData){0})
H
Hongze Cheng 已提交
170 171 172
void    tMapDataReset(SMapData *pMapData);
void    tMapDataClear(SMapData *pMapData);
int32_t tMapDataPutItem(SMapData *pMapData, void *pItem, int32_t (*tPutItemFn)(uint8_t *, void *));
H
Hongze Cheng 已提交
173
void    tMapDataGetItemByIdx(SMapData *pMapData, int32_t idx, void *pItem, int32_t (*tGetItemFn)(uint8_t *, void *));
H
Hongze Cheng 已提交
174 175
int32_t tMapDataSearch(SMapData *pMapData, void *pSearchItem, int32_t (*tGetItemFn)(uint8_t *, void *),
                       int32_t (*tItemCmprFn)(const void *, const void *), void *pItem);
H
Hongze Cheng 已提交
176 177 178 179 180
int32_t tPutMapData(uint8_t *p, SMapData *pMapData);
int32_t tGetMapData(uint8_t *p, SMapData *pMapData);
// other
int32_t tsdbKeyFid(TSKEY key, int32_t minutes, int8_t precision);
void    tsdbFidKeyRange(int32_t fid, int32_t minutes, int8_t precision, TSKEY *minKey, TSKEY *maxKey);
H
Hongze Cheng 已提交
181
int32_t tsdbFidLevel(int32_t fid, STsdbKeepCfg *pKeepCfg, int64_t now);
H
Hongze Cheng 已提交
182
int32_t tsdbBuildDeleteSkyline(SArray *aDelData, int32_t sidx, int32_t eidx, SArray *aSkyline);
H
Hongze Cheng 已提交
183
void    tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg);
H
Hongze Cheng 已提交
184 185
int32_t tPutColumnDataAgg(uint8_t *p, SColumnDataAgg *pColAgg);
int32_t tGetColumnDataAgg(uint8_t *p, SColumnDataAgg *pColAgg);
H
Hongze Cheng 已提交
186 187
int32_t tsdbCmprData(uint8_t *pIn, int32_t szIn, int8_t type, int8_t cmprAlg, uint8_t **ppOut, int32_t nOut,
                     int32_t *szOut, uint8_t **ppBuf);
H
Hongze Cheng 已提交
188 189
int32_t tsdbDecmprData(uint8_t *pIn, int32_t szIn, int8_t type, int8_t cmprAlg, uint8_t **ppOut, int32_t szOut,
                       uint8_t **ppBuf);
H
Hongze Cheng 已提交
190
int32_t tsdbCmprColData(SColData *pColData, int8_t cmprAlg, SBlockCol *pBlockCol, uint8_t **ppOut, int32_t nOut,
H
Hongze Cheng 已提交
191
                        uint8_t **ppBuf);
H
Hongze Cheng 已提交
192 193
int32_t tsdbDecmprColData(uint8_t *pIn, SBlockCol *pBlockCol, int8_t cmprAlg, int32_t nVal, SColData *pColData,
                          uint8_t **ppBuf);
H
Hongze Cheng 已提交
194
int32_t tsdbReadAndCheck(TdFilePtr pFD, int64_t offset, uint8_t **ppOut, int32_t size, int8_t toCheck);
H
Hongze Cheng 已提交
195 196
// tsdbMemTable ==============================================================================================
// SMemTable
H
Hongze Cheng 已提交
197 198 199 200 201 202
int32_t  tsdbMemTableCreate(STsdb *pTsdb, SMemTable **ppMemTable);
void     tsdbMemTableDestroy(SMemTable *pMemTable);
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid);
void     tsdbRefMemTable(SMemTable *pMemTable);
void     tsdbUnrefMemTable(SMemTable *pMemTable);
SArray  *tsdbMemTableGetTbDataArray(SMemTable *pMemTable);
H
Hongze Cheng 已提交
203 204 205 206 207 208
// STbDataIter
int32_t  tsdbTbDataIterCreate(STbData *pTbData, TSDBKEY *pFrom, int8_t backward, STbDataIter **ppIter);
void    *tsdbTbDataIterDestroy(STbDataIter *pIter);
void     tsdbTbDataIterOpen(STbData *pTbData, TSDBKEY *pFrom, int8_t backward, STbDataIter *pIter);
TSDBROW *tsdbTbDataIterGet(STbDataIter *pIter);
bool     tsdbTbDataIterNext(STbDataIter *pIter);
H
Hongze Cheng 已提交
209 210
// STbData
int32_t tsdbGetNRowsInTbData(STbData *pTbData);
H
Hongze Cheng 已提交
211
// tsdbFile.c ==============================================================================================
H
Hongze Cheng 已提交
212
typedef enum { TSDB_HEAD_FILE = 0, TSDB_DATA_FILE, TSDB_LAST_FILE, TSDB_SMA_FILE } EDataFileT;
H
Hongze Cheng 已提交
213

H
Hongze Cheng 已提交
214
bool    tsdbDelFileIsSame(SDelFile *pDelFile1, SDelFile *pDelFile2);
H
Hongze Cheng 已提交
215
int32_t tsdbDFileRollback(STsdb *pTsdb, SDFileSet *pSet, EDataFileT ftype);
H
Hongze Cheng 已提交
216 217 218 219
int32_t tPutHeadFile(uint8_t *p, SHeadFile *pHeadFile);
int32_t tPutDataFile(uint8_t *p, SDataFile *pDataFile);
int32_t tPutLastFile(uint8_t *p, SLastFile *pLastFile);
int32_t tPutSmaFile(uint8_t *p, SSmaFile *pSmaFile);
H
Hongze Cheng 已提交
220 221 222 223
int32_t tPutDelFile(uint8_t *p, SDelFile *pDelFile);
int32_t tGetDelFile(uint8_t *p, SDelFile *pDelFile);
int32_t tPutDFileSet(uint8_t *p, SDFileSet *pSet);
int32_t tGetDFileSet(uint8_t *p, SDFileSet *pSet);
H
Hongze Cheng 已提交
224 225 226 227 228

void tsdbHeadFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SHeadFile *pHeadF, char fname[]);
void tsdbDataFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SDataFile *pDataF, char fname[]);
void tsdbLastFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SLastFile *pLastF, char fname[]);
void tsdbSmaFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSmaFile *pSmaF, char fname[]);
H
Hongze Cheng 已提交
229
// SDelFile
H
Hongze Cheng 已提交
230
void tsdbDelFileName(STsdb *pTsdb, SDelFile *pFile, char fname[]);
H
Hongze Cheng 已提交
231
// tsdbFS.c ==============================================================================================
H
Hongze Cheng 已提交
232 233 234 235 236 237 238 239 240 241
int32_t tsdbFSOpen(STsdb *pTsdb);
int32_t tsdbFSClose(STsdb *pTsdb);
int32_t tsdbFSCopy(STsdb *pTsdb, STsdbFS *pFS);
void    tsdbFSDestroy(STsdbFS *pFS);
int32_t tDFileSetCmprFn(const void *p1, const void *p2);
int32_t tsdbFSCommit1(STsdb *pTsdb, STsdbFS *pFS);
int32_t tsdbFSCommit2(STsdb *pTsdb, STsdbFS *pFS);
int32_t tsdbFSRef(STsdb *pTsdb, STsdbFS *pFS);
void    tsdbFSUnref(STsdb *pTsdb, STsdbFS *pFS);

H
Hongze Cheng 已提交
242
int32_t tsdbFSRollback(STsdbFS *pFS);
H
Hongze Cheng 已提交
243

H
Hongze Cheng 已提交
244 245
int32_t tsdbFSUpsertFSet(STsdbFS *pFS, SDFileSet *pSet);
int32_t tsdbFSUpsertDelFile(STsdbFS *pFS, SDelFile *pDelFile);
H
Hongze Cheng 已提交
246 247 248
// tsdbReaderWriter.c ==============================================================================================
// SDataFWriter
int32_t tsdbDataFWriterOpen(SDataFWriter **ppWriter, STsdb *pTsdb, SDFileSet *pSet);
H
Hongze Cheng 已提交
249
int32_t tsdbDataFWriterClose(SDataFWriter **ppWriter, int8_t sync);
H
Hongze Cheng 已提交
250
int32_t tsdbUpdateDFileSetHeader(SDataFWriter *pWriter);
H
Hongze Cheng 已提交
251 252 253
int32_t tsdbWriteBlockIdx(SDataFWriter *pWriter, SArray *aBlockIdx);
int32_t tsdbWriteBlock(SDataFWriter *pWriter, SMapData *pMapData, SBlockIdx *pBlockIdx);
int32_t tsdbWriteBlockL(SDataFWriter *pWriter, SArray *aBlockL);
H
Hongze Cheng 已提交
254
int32_t tsdbWriteBlockData(SDataFWriter *pWriter, SBlockData *pBlockData, SBlockInfo *pBlkInfo, SSmaInfo *pSmaInfo,
H
Hongze Cheng 已提交
255
                           int8_t cmprAlg, int8_t toLast);
H
Hongze Cheng 已提交
256 257

int32_t tsdbDFileSetCopy(STsdb *pTsdb, SDFileSet *pSetFrom, SDFileSet *pSetTo);
H
Hongze Cheng 已提交
258 259
// SDataFReader
int32_t tsdbDataFReaderOpen(SDataFReader **ppReader, STsdb *pTsdb, SDFileSet *pSet);
H
Hongze Cheng 已提交
260
int32_t tsdbDataFReaderClose(SDataFReader **ppReader);
H
Hongze Cheng 已提交
261 262
int32_t tsdbReadBlockIdx(SDataFReader *pReader, SArray *aBlockIdx);
int32_t tsdbReadBlock(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *pMapData);
H
Hongze Cheng 已提交
263
int32_t tsdbReadBlockL(SDataFReader *pReader, SArray *aBlockL);
H
Hongze Cheng 已提交
264
int32_t tsdbReadBlockSma(SDataFReader *pReader, SBlock *pBlock, SArray *aColumnDataAgg);
H
Hongze Cheng 已提交
265 266
int32_t tsdbReadDataBlock(SDataFReader *pReader, SBlock *pBlock, SBlockData *pBlockData);
int32_t tsdbReadLastBlock(SDataFReader *pReader, SBlockL *pBlockL, SBlockData *pBlockData);
H
Hongze Cheng 已提交
267 268
// SDelFWriter
int32_t tsdbDelFWriterOpen(SDelFWriter **ppWriter, SDelFile *pFile, STsdb *pTsdb);
H
Hongze Cheng 已提交
269
int32_t tsdbDelFWriterClose(SDelFWriter **ppWriter, int8_t sync);
H
Hongze Cheng 已提交
270 271
int32_t tsdbWriteDelData(SDelFWriter *pWriter, SArray *aDelData, SDelIdx *pDelIdx);
int32_t tsdbWriteDelIdx(SDelFWriter *pWriter, SArray *aDelIdx);
H
Hongze Cheng 已提交
272
int32_t tsdbUpdateDelFileHdr(SDelFWriter *pWriter);
H
Hongze Cheng 已提交
273
// SDelFReader
H
Hongze Cheng 已提交
274
int32_t tsdbDelFReaderOpen(SDelFReader **ppReader, SDelFile *pFile, STsdb *pTsdb);
H
Hongze Cheng 已提交
275
int32_t tsdbDelFReaderClose(SDelFReader **ppReader);
H
Hongze Cheng 已提交
276 277
int32_t tsdbReadDelData(SDelFReader *pReader, SDelIdx *pDelIdx, SArray *aDelData);
int32_t tsdbReadDelIdx(SDelFReader *pReader, SArray *aDelIdx);
H
Hongze Cheng 已提交
278 279 280
// tsdbRead.c ==============================================================================================
int32_t tsdbTakeReadSnap(STsdb *pTsdb, STsdbReadSnap **ppSnap);
void    tsdbUntakeReadSnap(STsdb *pTsdb, STsdbReadSnap *pSnap);
H
Hongze Cheng 已提交
281

282 283 284 285
#define TSDB_CACHE_NO(c)       ((c).cacheLast == 0)
#define TSDB_CACHE_LAST_ROW(c) (((c).cacheLast & 1) > 0)
#define TSDB_CACHE_LAST(c)     (((c).cacheLast & 2) > 0)

286 287
// tsdbCache
int32_t tsdbOpenCache(STsdb *pTsdb);
288
void    tsdbCloseCache(STsdb *pTsdb);
289
int32_t tsdbCacheInsertLast(SLRUCache *pCache, tb_uid_t uid, STSRow *row, STsdb *pTsdb);
290
int32_t tsdbCacheInsertLastrow(SLRUCache *pCache, STsdb *pTsdb, tb_uid_t uid, STSRow *row, bool dup);
291 292
int32_t tsdbCacheGetLastH(SLRUCache *pCache, tb_uid_t uid, STsdb *pTsdb, LRUHandle **h);
int32_t tsdbCacheGetLastrowH(SLRUCache *pCache, tb_uid_t uid, STsdb *pTsdb, LRUHandle **h);
M
Minglei Jin 已提交
293
int32_t tsdbCacheRelease(SLRUCache *pCache, LRUHandle *h);
294

295 296
int32_t tsdbCacheDeleteLastrow(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey);
int32_t tsdbCacheDeleteLast(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey);
297
int32_t tsdbCacheDelete(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey);
298

299 300 301
void   tsdbCacheSetCapacity(SVnode *pVnode, size_t capacity);
size_t tsdbCacheGetCapacity(SVnode *pVnode);

302 303
int32_t tsdbCacheLastArray2Row(SArray *pLastArray, STSRow **ppRow, STSchema *pSchema);

H
Hongze Cheng 已提交
304
// structs =======================
H
Hongze Cheng 已提交
305 306 307 308 309
struct STsdbFS {
  SDelFile *pDelFile;
  SArray   *aDFileSet;  // SArray<SDFileSet>
};

H
Hongze Cheng 已提交
310
struct STsdb {
H
Hongze Cheng 已提交
311 312 313
  char          *path;
  SVnode        *pVnode;
  STsdbKeepCfg   keepCfg;
H
more  
Hongze Cheng 已提交
314
  TdThreadRwlock rwLock;
H
Hongze Cheng 已提交
315 316
  SMemTable     *mem;
  SMemTable     *imem;
H
Hongze Cheng 已提交
317
  STsdbFS        fs;
H
Hongze Cheng 已提交
318
  SLRUCache     *lruCache;
319
  TdThreadMutex  lruMutex;
H
Hongze Cheng 已提交
320 321
};

H
refact  
Hongze Cheng 已提交
322 323 324 325 326
struct TSDBKEY {
  int64_t version;
  TSKEY   ts;
};

H
Hongze Cheng 已提交
327 328 329 330 331 332 333
typedef struct SMemSkipListNode SMemSkipListNode;
struct SMemSkipListNode {
  int8_t            level;
  SMemSkipListNode *forwards[0];
};
typedef struct SMemSkipList {
  int64_t           size;
H
Hongze Cheng 已提交
334
  uint32_t          seed;
H
Hongze Cheng 已提交
335 336 337 338 339 340
  int8_t            maxLevel;
  int8_t            level;
  SMemSkipListNode *pHead;
  SMemSkipListNode *pTail;
} SMemSkipList;

H
Hongze Cheng 已提交
341
struct STbData {
H
Hongze Cheng 已提交
342 343
  tb_uid_t     suid;
  tb_uid_t     uid;
H
Hongze Cheng 已提交
344 345
  TSKEY        minKey;
  TSKEY        maxKey;
H
Hongze Cheng 已提交
346 347
  SDelData    *pHead;
  SDelData    *pTail;
H
Hongze Cheng 已提交
348
  SMemSkipList sl;
H
Hongze Cheng 已提交
349
  STbData     *next;
H
Hongze Cheng 已提交
350
};
H
Hongze Cheng 已提交
351

H
refact  
Hongze Cheng 已提交
352
struct SMemTable {
H
Hongze Cheng 已提交
353 354 355 356 357 358 359 360
  SRWLatch         latch;
  STsdb           *pTsdb;
  SVBufPool       *pPool;
  volatile int32_t nRef;
  TSKEY            minKey;
  TSKEY            maxKey;
  int64_t          nRow;
  int64_t          nDel;
H
Hongze Cheng 已提交
361 362 363 364 365
  struct {
    int32_t   nTbData;
    int32_t   nBucket;
    STbData **aBucket;
  };
H
Hongze Cheng 已提交
366
};
H
Hongze Cheng 已提交
367

H
Hongze Cheng 已提交
368
struct TSDBROW {
H
Hongze Cheng 已提交
369
  int8_t type;  // 0 for row from tsRow, 1 for row from block data
H
Hongze Cheng 已提交
370 371 372 373 374 375
  union {
    struct {
      int64_t version;
      STSRow *pTSRow;
    };
    struct {
H
Hongze Cheng 已提交
376 377
      SBlockData *pBlockData;
      int32_t     iRow;
H
Hongze Cheng 已提交
378 379
    };
  };
H
Hongze Cheng 已提交
380 381
};

H
Hongze Cheng 已提交
382
struct SBlockIdx {
H
Hongze Cheng 已提交
383 384
  int64_t suid;
  int64_t uid;
H
Hongze Cheng 已提交
385 386 387 388
  int64_t offset;
  int64_t size;
};

H
Hongze Cheng 已提交
389 390
struct SMapData {
  int32_t  nItem;
H
Hongze Cheng 已提交
391
  int32_t  nData;
392
  int32_t *aOffset;
H
Hongze Cheng 已提交
393 394 395
  uint8_t *pData;
};

H
Hongze Cheng 已提交
396
struct SBlockCol {
H
Hongze Cheng 已提交
397 398 399 400 401 402 403 404 405
  int16_t cid;
  int8_t  type;
  int8_t  smaOn;
  int8_t  flag;      // HAS_NONE|HAS_NULL|HAS_VALUE
  int32_t szOrigin;  // original column value size (only save for variant data type)
  int32_t szBitmap;  // bitmap size, 0 only for flag == HAS_VAL
  int32_t szOffset;  // offset size, 0 only for non-variant-length type
  int32_t szValue;   // value size, 0 when flag == (HAS_NULL | HAS_NONE)
  int32_t offset;
H
Hongze Cheng 已提交
406
};
H
Hongze Cheng 已提交
407

H
Hongze Cheng 已提交
408
struct SBlockInfo {
H
Hongze Cheng 已提交
409 410 411
  int64_t offset;  // block data offset
  int32_t szBlock;
  int32_t szKey;
H
Hongze Cheng 已提交
412
};
H
Hongze Cheng 已提交
413

H
Hongze Cheng 已提交
414
struct SSmaInfo {
H
Hongze Cheng 已提交
415 416
  int64_t offset;
  int32_t size;
H
Hongze Cheng 已提交
417
};
H
Hongze Cheng 已提交
418

H
Hongze Cheng 已提交
419
struct SBlock {
H
Hongze Cheng 已提交
420 421 422 423 424 425 426 427 428
  TSDBKEY    minKey;
  TSDBKEY    maxKey;
  int64_t    minVer;
  int64_t    maxVer;
  int32_t    nRow;
  int8_t     hasDup;
  int8_t     nSubBlock;
  SBlockInfo aSubBlock[TSDB_MAX_SUBBLOCKS];
  SSmaInfo   smaInfo;
H
refact  
Hongze Cheng 已提交
429
};
H
Hongze Cheng 已提交
430

H
Hongze Cheng 已提交
431
struct SBlockL {
H
Hongze Cheng 已提交
432 433 434
  int64_t    suid;
  int64_t    minUid;
  int64_t    maxUid;
H
Hongze Cheng 已提交
435 436
  TSKEY      minKey;
  TSKEY      maxKey;
H
Hongze Cheng 已提交
437 438 439 440
  int64_t    minVer;
  int64_t    maxVer;
  int32_t    nRow;
  SBlockInfo bInfo;
H
Hongze Cheng 已提交
441 442
};

H
Hongze Cheng 已提交
443 444 445
struct SColData {
  int16_t  cid;
  int8_t   type;
H
Hongze Cheng 已提交
446
  int8_t   smaOn;
H
Hongze Cheng 已提交
447 448
  int32_t  nVal;
  uint8_t  flag;
H
Hongze Cheng 已提交
449
  uint8_t *pBitMap;
H
Hongze Cheng 已提交
450 451
  int32_t *aOffset;
  int32_t  nData;
H
Hongze Cheng 已提交
452
  uint8_t *pData;
H
refact  
Hongze Cheng 已提交
453
};
H
Hongze Cheng 已提交
454

H
Hongze Cheng 已提交
455 456 457 458
// (SBlockData){.suid = 0, .uid = 0}: block data not initialized
// (SBlockData){.suid = suid, .uid = uid}: block data for ONE child table int .data file
// (SBlockData){.suid = suid, .uid = 0}: block data for N child tables int .last file
// (SBlockData){.suid = 0, .uid = uid}: block data for 1 normal table int .last/.data file
H
Hongze Cheng 已提交
459
struct SBlockData {
H
Hongze Cheng 已提交
460 461 462 463 464 465
  int64_t  suid;      // 0 means normal table block data, otherwise child table block data
  int64_t  uid;       // 0 means block data in .last file, otherwise in .data file
  int32_t  nRow;      // number of rows
  int64_t *aUid;      // uids of each row, only exist in block data in .last file (uid == 0)
  int64_t *aVersion;  // versions of each row
  TSKEY   *aTSKEY;    // timestamp of each row
H
Hongze Cheng 已提交
466 467
  SArray  *aIdx;      // SArray<int32_t>
  SArray  *aColData;  // SArray<SColData>
H
Hongze Cheng 已提交
468
};
H
Hongze Cheng 已提交
469 470 471 472

// ================== TSDB global config
extern bool tsdbForceKeepFile;

H
Hongze Cheng 已提交
473
#define TSDB_FS_ITER_FORWARD  TSDB_ORDER_ASC
H
Hongze Cheng 已提交
474 475
#define TSDB_FS_ITER_BACKWARD TSDB_ORDER_DESC

H
Hongze Cheng 已提交
476 477 478 479 480
struct TABLEID {
  tb_uid_t suid;
  tb_uid_t uid;
};

H
Hongze Cheng 已提交
481 482 483 484
struct STbDataIter {
  STbData          *pTbData;
  int8_t            backward;
  SMemSkipListNode *pNode;
H
Hongze Cheng 已提交
485 486
  TSDBROW          *pRow;
  TSDBROW           row;
H
Hongze Cheng 已提交
487 488
};

H
Hongze Cheng 已提交
489
struct SDelData {
H
Hongze Cheng 已提交
490 491 492 493
  int64_t   version;
  TSKEY     sKey;
  TSKEY     eKey;
  SDelData *pNext;
H
Hongze Cheng 已提交
494
};
H
Hongze Cheng 已提交
495

H
Hongze Cheng 已提交
496
struct SDelIdx {
H
Hongze Cheng 已提交
497 498 499 500
  tb_uid_t suid;
  tb_uid_t uid;
  int64_t  offset;
  int64_t  size;
H
Hongze Cheng 已提交
501
};
H
Hongze Cheng 已提交
502

H
Hongze Cheng 已提交
503
struct SDiskDataHdr {
H
more  
Hongze Cheng 已提交
504
  uint32_t delimiter;
H
Hongze Cheng 已提交
505
  uint32_t fmtVer;
H
more  
Hongze Cheng 已提交
506 507
  int64_t  suid;
  int64_t  uid;
H
Hongze Cheng 已提交
508 509 510 511
  int32_t  szUid;
  int32_t  szVer;
  int32_t  szKey;
  int32_t  szBlkCol;
H
Hongze Cheng 已提交
512
  int32_t  nRow;
H
Hongze Cheng 已提交
513
  int8_t   cmprAlg;
H
Hongze Cheng 已提交
514 515
};

H
Hongze Cheng 已提交
516 517 518 519 520 521 522 523
struct SDelFile {
  volatile int32_t nRef;

  int64_t commitID;
  int64_t size;
  int64_t offset;
};

H
Hongze Cheng 已提交
524
struct SHeadFile {
H
Hongze Cheng 已提交
525 526
  volatile int32_t nRef;

H
Hongze Cheng 已提交
527
  int64_t commitID;
H
Hongze Cheng 已提交
528 529 530 531 532
  int64_t size;
  int64_t offset;
};

struct SDataFile {
H
Hongze Cheng 已提交
533 534
  volatile int32_t nRef;

H
Hongze Cheng 已提交
535
  int64_t commitID;
H
Hongze Cheng 已提交
536 537 538 539
  int64_t size;
};

struct SLastFile {
H
Hongze Cheng 已提交
540 541
  volatile int32_t nRef;

H
Hongze Cheng 已提交
542
  int64_t commitID;
H
Hongze Cheng 已提交
543
  int64_t size;
H
Hongze Cheng 已提交
544
  int64_t offset;
H
Hongze Cheng 已提交
545 546 547
};

struct SSmaFile {
H
Hongze Cheng 已提交
548 549
  volatile int32_t nRef;

H
Hongze Cheng 已提交
550
  int64_t commitID;
H
Hongze Cheng 已提交
551 552 553 554
  int64_t size;
};

struct SDFileSet {
H
Hongze Cheng 已提交
555 556 557 558 559 560
  SDiskID    diskId;
  int32_t    fid;
  SHeadFile *pHeadF;
  SDataFile *pDataF;
  SLastFile *pLastF;
  SSmaFile  *pSmaF;
H
Hongze Cheng 已提交
561 562 563 564
};

struct SRowIter {
  TSDBROW  *pRow;
H
Hongze Cheng 已提交
565
  STSchema *pTSchema;
H
Hongze Cheng 已提交
566 567
  SColVal   colVal;
  int32_t   i;
H
Hongze Cheng 已提交
568
};
H
Hongze Cheng 已提交
569 570 571 572 573
struct SRowMerger {
  STSchema *pTSchema;
  int64_t   version;
  SArray   *pArray;  // SArray<SColVal>
};
H
Hongze Cheng 已提交
574

H
Hongze Cheng 已提交
575 576 577 578
struct SDelFWriter {
  STsdb    *pTsdb;
  SDelFile  fDel;
  TdFilePtr pWriteH;
H
Hongze Cheng 已提交
579

H
Hongze Cheng 已提交
580
  uint8_t *aBuf[1];
H
Hongze Cheng 已提交
581 582
};

H
Hongze Cheng 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595
struct SDataFWriter {
  STsdb    *pTsdb;
  SDFileSet wSet;

  TdFilePtr pHeadFD;
  TdFilePtr pDataFD;
  TdFilePtr pLastFD;
  TdFilePtr pSmaFD;

  SHeadFile fHead;
  SDataFile fData;
  SLastFile fLast;
  SSmaFile  fSma;
H
Hongze Cheng 已提交
596

H
Hongze Cheng 已提交
597
  uint8_t *aBuf[4];
H
Hongze Cheng 已提交
598 599 600 601 602 603 604 605
};

struct STsdbReadSnap {
  SMemTable *pMem;
  SMemTable *pIMem;
  STsdbFS    fs;
};

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
// ========== inline functions ==========
static FORCE_INLINE int32_t tsdbKeyCmprFn(const void *p1, const void *p2) {
  TSDBKEY *pKey1 = (TSDBKEY *)p1;
  TSDBKEY *pKey2 = (TSDBKEY *)p2;

  if (pKey1->ts < pKey2->ts) {
    return -1;
  } else if (pKey1->ts > pKey2->ts) {
    return 1;
  }

  if (pKey1->version < pKey2->version) {
    return -1;
  } else if (pKey1->version > pKey2->version) {
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
626 627 628 629
#ifdef __cplusplus
}
#endif

630
#endif /*_TD_VNODE_TSDB_H_*/