tsdb.h 33.4 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 21
// #include "../tsdb/tsdbFile2.h"
// #include "../tsdb/tsdbMerge.h"
// #include "../tsdb/tsdbSttFileRW.h"
H
Haojun Liao 已提交
22
#include "tsimplehash.h"
H
Hongze Cheng 已提交
23 24
#include "vnodeInt.h"

H
Hongze Cheng 已提交
25 26 27 28
#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
29 30
// tsdbDebug ================
// clang-format off
H
Hongze Cheng 已提交
31 32 33 34 35 36
#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 已提交
37
// clang-format on
H
Hongze Cheng 已提交
38

H
Hongze Cheng 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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 SDataBlk         SDataBlk;
typedef struct SSttBlk          SSttBlk;
typedef struct SDiskDataHdr     SDiskDataHdr;
typedef struct SBlockData       SBlockData;
typedef struct SDelFile         SDelFile;
typedef struct SHeadFile        SHeadFile;
typedef struct SDataFile        SDataFile;
typedef struct SSttFile         SSttFile;
typedef struct SSmaFile         SSmaFile;
typedef struct SDFileSet        SDFileSet;
typedef struct SDataFWriter     SDataFWriter;
typedef struct SDataFReader     SDataFReader;
typedef struct SDelFWriter      SDelFWriter;
typedef struct SDelFReader      SDelFReader;
63
typedef struct STSDBRowIter     STSDBRowIter;
H
Hongze Cheng 已提交
64 65 66 67 68 69 70 71 72 73
typedef struct STsdbFS          STsdbFS;
typedef struct SRowMerger       SRowMerger;
typedef struct STsdbReadSnap    STsdbReadSnap;
typedef struct SBlockInfo       SBlockInfo;
typedef struct SSmaInfo         SSmaInfo;
typedef struct SBlockCol        SBlockCol;
typedef struct SLDataIter       SLDataIter;
typedef struct SDiskCol         SDiskCol;
typedef struct SDiskData        SDiskData;
typedef struct SDiskDataBuilder SDiskDataBuilder;
H
Hongze Cheng 已提交
74
typedef struct SBlkInfo         SBlkInfo;
H
Hongze Cheng 已提交
75 76
typedef struct STsdbDataIter2   STsdbDataIter2;
typedef struct STsdbFilterInfo  STsdbFilterInfo;
H
Hongze Cheng 已提交
77

H
Hongze Cheng 已提交
78 79 80
#define TSDBROW_ROW_FMT ((int8_t)0x0)
#define TSDBROW_COL_FMT ((int8_t)0x1)

H
Hongze Cheng 已提交
81 82
#define TSDB_FILE_DLMT ((uint32_t)0xF00AFA0F)
#define TSDB_FHDR_SIZE 512
H
refact  
Hongze Cheng 已提交
83

H
Hongze Cheng 已提交
84 85 86
#define VERSION_MIN 0
#define VERSION_MAX INT64_MAX

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

H
Hongze Cheng 已提交
90 91
#define TABLE_SAME_SCHEMA(SUID1, UID1, SUID2, UID2) ((SUID1) ? (SUID1) == (SUID2) : (UID1) == (UID2))

H
Hongze Cheng 已提交
92 93 94 95 96 97
#define PAGE_CONTENT_SIZE(PAGE) ((PAGE) - sizeof(TSCKSUM))
#define LOGIC_TO_FILE_OFFSET(LOFFSET, PAGE) \
  ((LOFFSET) / PAGE_CONTENT_SIZE(PAGE) * (PAGE) + (LOFFSET) % PAGE_CONTENT_SIZE(PAGE))
#define FILE_TO_LOGIC_OFFSET(OFFSET, PAGE) ((OFFSET) / (PAGE)*PAGE_CONTENT_SIZE(PAGE) + (OFFSET) % (PAGE))
#define PAGE_OFFSET(PGNO, PAGE)            (((PGNO)-1) * (PAGE))
#define OFFSET_PGNO(OFFSET, PAGE)          ((OFFSET) / (PAGE) + 1)
H
Hongze Cheng 已提交
98 99 100 101 102

static FORCE_INLINE int64_t tsdbLogicToFileSize(int64_t lSize, int32_t szPage) {
  int64_t fOffSet = LOGIC_TO_FILE_OFFSET(lSize, szPage);
  int64_t pgno = OFFSET_PGNO(fOffSet, szPage);

H
Hongze Cheng 已提交
103
  if (fOffSet % szPage == 0) {
H
Hongze Cheng 已提交
104 105 106 107 108
    pgno--;
  }

  return pgno * szPage;
}
H
Hongze Cheng 已提交
109

H
Hongze Cheng 已提交
110
// tsdbUtil.c ==============================================================================================
H
Hongze Cheng 已提交
111
// TSDBROW
H
Hongze Cheng 已提交
112 113 114
#define TSDBROW_TS(ROW) (((ROW)->type == TSDBROW_ROW_FMT) ? (ROW)->pTSRow->ts : (ROW)->pBlockData->aTSKEY[(ROW)->iRow])
#define TSDBROW_VERSION(ROW) \
  (((ROW)->type == TSDBROW_ROW_FMT) ? (ROW)->version : (ROW)->pBlockData->aVersion[(ROW)->iRow])
H
Hongze Cheng 已提交
115
#define TSDBROW_SVERSION(ROW)            ((ROW)->type == TSDBROW_ROW_FMT ? (ROW)->pTSRow->sver : -1)
H
Hongze Cheng 已提交
116 117 118 119
#define TSDBROW_KEY(ROW)                 ((TSDBKEY){.version = TSDBROW_VERSION(ROW), .ts = TSDBROW_TS(ROW)})
#define tsdbRowFromTSRow(VERSION, TSROW) ((TSDBROW){.type = TSDBROW_ROW_FMT, .version = (VERSION), .pTSRow = (TSROW)})
#define tsdbRowFromBlockData(BLOCKDATA, IROW) \
  ((TSDBROW){.type = TSDBROW_COL_FMT, .pBlockData = (BLOCKDATA), .iRow = (IROW)})
H
Hongze Cheng 已提交
120 121

void    tsdbRowGetColVal(TSDBROW *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal);
H
Hongze Cheng 已提交
122
int32_t tsdbRowCmprFn(const void *p1, const void *p2);
123
// STSDBRowIter
H
Hongze Cheng 已提交
124 125
int32_t  tsdbRowIterOpen(STSDBRowIter *pIter, TSDBROW *pRow, STSchema *pTSchema);
void     tsdbRowClose(STSDBRowIter *pIter);
126
SColVal *tsdbRowIterNext(STSDBRowIter *pIter);
127

H
Hongze Cheng 已提交
128
// SRowMerger
129
int32_t tsdbRowMergerInit(SRowMerger *pMerger, STSchema *pSchema);
H
Hongze Cheng 已提交
130 131
int32_t tsdbRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema);
int32_t tsdbRowMergerGetRow(SRowMerger *pMerger, SRow **ppRow);
132 133
void    tsdbRowMergerClear(SRowMerger *pMerger);
void    tsdbRowMergerCleanup(SRowMerger *pMerger);
H
Haojun Liao 已提交
134

H
Hongze Cheng 已提交
135
// TABLEID
H
Hongze Cheng 已提交
136
int32_t tTABLEIDCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
137
// TSDBKEY
H
Hongze Cheng 已提交
138 139
#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 已提交
140 141 142
// SBlockCol
int32_t tPutBlockCol(uint8_t *p, void *ph);
int32_t tGetBlockCol(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
143
int32_t tBlockColCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
144
// SDataBlk
H
Hongze Cheng 已提交
145
void    tDataBlkReset(SDataBlk *pBlock);
H
Hongze Cheng 已提交
146 147
int32_t tPutDataBlk(uint8_t *p, void *ph);
int32_t tGetDataBlk(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
148 149
int32_t tDataBlkCmprFn(const void *p1, const void *p2);
bool    tDataBlkHasSma(SDataBlk *pDataBlk);
H
Hongze Cheng 已提交
150 151 152
// SSttBlk
int32_t tPutSttBlk(uint8_t *p, void *ph);
int32_t tGetSttBlk(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
153 154 155
// SBlockIdx
int32_t tPutBlockIdx(uint8_t *p, void *ph);
int32_t tGetBlockIdx(uint8_t *p, void *ph);
156
int32_t tCmprBlockIdx(void const *lhs, void const *rhs);
157
int32_t tCmprBlockL(void const *lhs, void const *rhs);
H
Hongze Cheng 已提交
158
// SBlockData
H
Hongze Cheng 已提交
159 160 161 162 163 164 165 166 167 168 169
#define tBlockDataFirstRow(PBLOCKDATA)             tsdbRowFromBlockData(PBLOCKDATA, 0)
#define tBlockDataLastRow(PBLOCKDATA)              tsdbRowFromBlockData(PBLOCKDATA, (PBLOCKDATA)->nRow - 1)
#define tBlockDataFirstKey(PBLOCKDATA)             TSDBROW_KEY(&tBlockDataFirstRow(PBLOCKDATA))
#define tBlockDataLastKey(PBLOCKDATA)              TSDBROW_KEY(&tBlockDataLastRow(PBLOCKDATA))
#define tBlockDataGetColDataByIdx(PBLOCKDATA, IDX) (&(PBLOCKDATA)->aColData[IDX])

int32_t tBlockDataCreate(SBlockData *pBlockData);
void    tBlockDataDestroy(SBlockData *pBlockData);
int32_t tBlockDataInit(SBlockData *pBlockData, TABLEID *pId, STSchema *pTSchema, int16_t *aCid, int32_t nCid);
void    tBlockDataReset(SBlockData *pBlockData);
int32_t tBlockDataAppendRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema, int64_t uid);
H
Hongze Cheng 已提交
170
int32_t tBlockDataUpdateRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema);
H
Hongze Cheng 已提交
171
int32_t tBlockDataTryUpsertRow(SBlockData *pBlockData, TSDBROW *pRow, int64_t uid);
H
Hongze Cheng 已提交
172
int32_t tBlockDataUpsertRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema, int64_t uid);
H
Hongze Cheng 已提交
173 174 175 176 177
void    tBlockDataClear(SBlockData *pBlockData);
void    tBlockDataGetColData(SBlockData *pBlockData, int16_t cid, SColData **ppColData);
int32_t tCmprBlockData(SBlockData *pBlockData, int8_t cmprAlg, uint8_t **ppOut, int32_t *szOut, uint8_t *aBuf[],
                       int32_t aBufN[]);
int32_t tDecmprBlockData(uint8_t *pIn, int32_t szIn, SBlockData *pBlockData, uint8_t *aBuf[]);
H
Hongze Cheng 已提交
178
// SDiskDataHdr
H
Hongze Cheng 已提交
179
int32_t tPutDiskDataHdr(uint8_t *p, const SDiskDataHdr *pHdr);
H
Hongze Cheng 已提交
180
int32_t tGetDiskDataHdr(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
181
// SDelIdx
H
Hongze Cheng 已提交
182 183
int32_t tPutDelIdx(uint8_t *p, void *ph);
int32_t tGetDelIdx(uint8_t *p, void *ph);
184
int32_t tCmprDelIdx(void const *lhs, void const *rhs);
H
Hongze Cheng 已提交
185
// SDelData
H
Hongze Cheng 已提交
186 187
int32_t tPutDelData(uint8_t *p, void *ph);
int32_t tGetDelData(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
188
// SMapData
H
Hongze Cheng 已提交
189
#define tMapDataInit() ((SMapData){0})
H
Hongze Cheng 已提交
190 191 192
void    tMapDataReset(SMapData *pMapData);
void    tMapDataClear(SMapData *pMapData);
int32_t tMapDataPutItem(SMapData *pMapData, void *pItem, int32_t (*tPutItemFn)(uint8_t *, void *));
H
Hongze Cheng 已提交
193
int32_t tMapDataCopy(SMapData *pFrom, SMapData *pTo);
H
Hongze Cheng 已提交
194
void    tMapDataGetItemByIdx(SMapData *pMapData, int32_t idx, void *pItem, int32_t (*tGetItemFn)(uint8_t *, void *));
H
Hongze Cheng 已提交
195 196
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 已提交
197 198
int32_t tPutMapData(uint8_t *p, SMapData *pMapData);
int32_t tGetMapData(uint8_t *p, SMapData *pMapData);
H
Hongze Cheng 已提交
199 200
int32_t tMapDataToArray(SMapData *pMapData, int32_t itemSize, int32_t (*tGetItemFn)(uint8_t *, void *),
                        SArray **ppArray);
H
Hongze Cheng 已提交
201 202 203
// 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 已提交
204
int32_t tsdbFidLevel(int32_t fid, STsdbKeepCfg *pKeepCfg, int64_t nowSec);
H
Hongze Cheng 已提交
205
int32_t tsdbBuildDeleteSkyline(SArray *aDelData, int32_t sidx, int32_t eidx, SArray *aSkyline);
H
Hongze Cheng 已提交
206 207
int32_t tPutColumnDataAgg(uint8_t *p, SColumnDataAgg *pColAgg);
int32_t tGetColumnDataAgg(uint8_t *p, SColumnDataAgg *pColAgg);
H
Hongze Cheng 已提交
208 209
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 已提交
210 211
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 已提交
212
int32_t tsdbCmprColData(SColData *pColData, int8_t cmprAlg, SBlockCol *pBlockCol, uint8_t **ppOut, int32_t nOut,
H
Hongze Cheng 已提交
213
                        uint8_t **ppBuf);
H
Hongze Cheng 已提交
214 215
int32_t tsdbDecmprColData(uint8_t *pIn, SBlockCol *pBlockCol, int8_t cmprAlg, int32_t nVal, SColData *pColData,
                          uint8_t **ppBuf);
H
Hongze Cheng 已提交
216
int32_t tRowInfoCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
217 218
// tsdbMemTable ==============================================================================================
// SMemTable
H
Hongze Cheng 已提交
219
int32_t  tsdbMemTableCreate(STsdb *pTsdb, SMemTable **ppMemTable);
H
Hongze Cheng 已提交
220
void     tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive);
H
Hongze Cheng 已提交
221
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid);
H
Hongze Cheng 已提交
222
int32_t  tsdbRefMemTable(SMemTable *pMemTable, SQueryNode *pQNode);
H
Hongze Cheng 已提交
223
int32_t  tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive);
H
Hongze Cheng 已提交
224
SArray  *tsdbMemTableGetTbDataArray(SMemTable *pMemTable);
H
Hongze Cheng 已提交
225
// STbDataIter
H
Hongze Cheng 已提交
226 227 228 229
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);
bool    tsdbTbDataIterNext(STbDataIter *pIter);
H
Haojun Liao 已提交
230
void    tsdbMemTableCountRows(SMemTable *pMemTable, SSHashObj *pTableMap, int64_t *rowsNum);
D
dapan1121 已提交
231

H
Hongze Cheng 已提交
232 233
// STbData
int32_t tsdbGetNRowsInTbData(STbData *pTbData);
H
Hongze Cheng 已提交
234
// tsdbFile.c ==============================================================================================
H
Hongze Cheng 已提交
235
typedef enum { TSDB_HEAD_FILE = 0, TSDB_DATA_FILE, TSDB_LAST_FILE, TSDB_SMA_FILE } EDataFileT;
H
Hongze Cheng 已提交
236

H
Hongze Cheng 已提交
237
bool    tsdbDelFileIsSame(SDelFile *pDelFile1, SDelFile *pDelFile2);
H
Hongze Cheng 已提交
238
int32_t tsdbDFileRollback(STsdb *pTsdb, SDFileSet *pSet, EDataFileT ftype);
H
Hongze Cheng 已提交
239 240
int32_t tPutHeadFile(uint8_t *p, SHeadFile *pHeadFile);
int32_t tPutDataFile(uint8_t *p, SDataFile *pDataFile);
H
Hongze Cheng 已提交
241
int32_t tPutSttFile(uint8_t *p, SSttFile *pSttFile);
H
Hongze Cheng 已提交
242
int32_t tPutSmaFile(uint8_t *p, SSmaFile *pSmaFile);
H
Hongze Cheng 已提交
243 244 245 246
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 已提交
247 248 249

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[]);
H
Hongze Cheng 已提交
250
void tsdbSttFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSttFile *pSttF, char fname[]);
H
Hongze Cheng 已提交
251
void tsdbSmaFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSmaFile *pSmaF, char fname[]);
252

H
Hongze Cheng 已提交
253
// SDelFile
H
Hongze Cheng 已提交
254
void tsdbDelFileName(STsdb *pTsdb, SDelFile *pFile, char fname[]);
H
Hongze Cheng 已提交
255
// tsdbFS.c ==============================================================================================
H
Hongze Cheng 已提交
256
int32_t tsdbFSOpen(STsdb *pTsdb, int8_t rollback);
H
Hongze Cheng 已提交
257 258 259 260
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);
H
Hongze Cheng 已提交
261 262 263
int32_t tsdbFSCommit(STsdb *pTsdb);
int32_t tsdbFSRollback(STsdb *pTsdb);
int32_t tsdbFSPrepareCommit(STsdb *pTsdb, STsdbFS *pFS);
H
Hongze Cheng 已提交
264 265 266 267 268
int32_t tsdbFSRef(STsdb *pTsdb, STsdbFS *pFS);
void    tsdbFSUnref(STsdb *pTsdb, STsdbFS *pFS);

int32_t tsdbFSUpsertFSet(STsdbFS *pFS, SDFileSet *pSet);
int32_t tsdbFSUpsertDelFile(STsdbFS *pFS, SDelFile *pDelFile);
H
Hongze Cheng 已提交
269 270 271
// tsdbReaderWriter.c ==============================================================================================
// SDataFWriter
int32_t tsdbDataFWriterOpen(SDataFWriter **ppWriter, STsdb *pTsdb, SDFileSet *pSet);
H
Hongze Cheng 已提交
272
int32_t tsdbDataFWriterClose(SDataFWriter **ppWriter, int8_t sync);
H
Hongze Cheng 已提交
273
int32_t tsdbUpdateDFileSetHeader(SDataFWriter *pWriter);
H
Hongze Cheng 已提交
274
int32_t tsdbWriteBlockIdx(SDataFWriter *pWriter, SArray *aBlockIdx);
H
Hongze Cheng 已提交
275
int32_t tsdbWriteDataBlk(SDataFWriter *pWriter, SMapData *mDataBlk, SBlockIdx *pBlockIdx);
H
Hongze Cheng 已提交
276
int32_t tsdbWriteSttBlk(SDataFWriter *pWriter, SArray *aSttBlk);
H
Hongze Cheng 已提交
277
int32_t tsdbWriteBlockData(SDataFWriter *pWriter, SBlockData *pBlockData, SBlockInfo *pBlkInfo, SSmaInfo *pSmaInfo,
H
Hongze Cheng 已提交
278
                           int8_t cmprAlg, int8_t toLast);
H
Hongze Cheng 已提交
279
int32_t tsdbWriteDiskData(SDataFWriter *pWriter, const SDiskData *pDiskData, SBlockInfo *pBlkInfo, SSmaInfo *pSmaInfo);
H
Hongze Cheng 已提交
280 281

int32_t tsdbDFileSetCopy(STsdb *pTsdb, SDFileSet *pSetFrom, SDFileSet *pSetTo);
H
Hongze Cheng 已提交
282 283
// SDataFReader
int32_t tsdbDataFReaderOpen(SDataFReader **ppReader, STsdb *pTsdb, SDFileSet *pSet);
H
Hongze Cheng 已提交
284
int32_t tsdbDataFReaderClose(SDataFReader **ppReader);
H
Hongze Cheng 已提交
285
int32_t tsdbReadBlockIdx(SDataFReader *pReader, SArray *aBlockIdx);
H
Hongze Cheng 已提交
286
int32_t tsdbReadDataBlk(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *mDataBlk);
H
Hongze Cheng 已提交
287
int32_t tsdbReadSttBlk(SDataFReader *pReader, int32_t iStt, SArray *aSttBlk);
H
Hongze Cheng 已提交
288 289
int32_t tsdbReadBlockSma(SDataFReader *pReader, SDataBlk *pBlock, SArray *aColumnDataAgg);
int32_t tsdbReadDataBlock(SDataFReader *pReader, SDataBlk *pBlock, SBlockData *pBlockData);
H
Hongze Cheng 已提交
290
int32_t tsdbReadDataBlockEx(SDataFReader *pReader, SDataBlk *pDataBlk, SBlockData *pBlockData);
H
Hongze Cheng 已提交
291
int32_t tsdbReadSttBlock(SDataFReader *pReader, int32_t iStt, SSttBlk *pSttBlk, SBlockData *pBlockData);
H
Hongze Cheng 已提交
292
int32_t tsdbReadSttBlockEx(SDataFReader *pReader, int32_t iStt, SSttBlk *pSttBlk, SBlockData *pBlockData);
H
Hongze Cheng 已提交
293 294
// SDelFWriter
int32_t tsdbDelFWriterOpen(SDelFWriter **ppWriter, SDelFile *pFile, STsdb *pTsdb);
H
Hongze Cheng 已提交
295
int32_t tsdbDelFWriterClose(SDelFWriter **ppWriter, int8_t sync);
H
Hongze Cheng 已提交
296 297
int32_t tsdbWriteDelData(SDelFWriter *pWriter, SArray *aDelData, SDelIdx *pDelIdx);
int32_t tsdbWriteDelIdx(SDelFWriter *pWriter, SArray *aDelIdx);
H
Hongze Cheng 已提交
298
int32_t tsdbUpdateDelFileHdr(SDelFWriter *pWriter);
H
Hongze Cheng 已提交
299
// SDelFReader
H
Hongze Cheng 已提交
300
int32_t tsdbDelFReaderOpen(SDelFReader **ppReader, SDelFile *pFile, STsdb *pTsdb);
H
Hongze Cheng 已提交
301
int32_t tsdbDelFReaderClose(SDelFReader **ppReader);
H
Haojun Liao 已提交
302
int32_t tsdbReadDelDatav1(SDelFReader *pReader, SDelIdx *pDelIdx, SArray *aDelData, int64_t maxVer);
H
Hongze Cheng 已提交
303 304
int32_t tsdbReadDelData(SDelFReader *pReader, SDelIdx *pDelIdx, SArray *aDelData);
int32_t tsdbReadDelIdx(SDelFReader *pReader, SArray *aDelIdx);
H
Hongze Cheng 已提交
305
// tsdbRead.c ==============================================================================================
H
Hongze Cheng 已提交
306
int32_t tsdbTakeReadSnap(STsdbReader *pReader, _query_reseek_func_t reseek, STsdbReadSnap **ppSnap);
307
void    tsdbUntakeReadSnap(STsdbReader *pReader, STsdbReadSnap *pSnap, bool proactive);
308 309 310

int32_t tsdbTakeReadSnap2(STsdbReader *pReader, _query_reseek_func_t reseek, STsdbReadSnap **ppSnap);
void    tsdbUntakeReadSnap2(STsdbReader *pReader, STsdbReadSnap *pSnap, bool proactive);
H
Hongze Cheng 已提交
311
// tsdbMerge.c ==============================================================================================
H
Hongze Cheng 已提交
312
int32_t tsdbMerge(void *arg);
H
Hongze Cheng 已提交
313

H
Hongze Cheng 已提交
314 315 316 317 318
// tsdbDiskData ==============================================================================================
int32_t tDiskDataBuilderCreate(SDiskDataBuilder **ppBuilder);
void   *tDiskDataBuilderDestroy(SDiskDataBuilder *pBuilder);
int32_t tDiskDataBuilderInit(SDiskDataBuilder *pBuilder, STSchema *pTSchema, TABLEID *pId, uint8_t cmprAlg,
                             uint8_t calcSma);
H
Hongze Cheng 已提交
319 320
int32_t tDiskDataBuilderClear(SDiskDataBuilder *pBuilder);
int32_t tDiskDataAddRow(SDiskDataBuilder *pBuilder, TSDBROW *pRow, STSchema *pTSchema, TABLEID *pId);
H
Hongze Cheng 已提交
321
int32_t tGnrtDiskData(SDiskDataBuilder *pBuilder, const SDiskData **ppDiskData, const SBlkInfo **ppBlkInfo);
H
Hongze Cheng 已提交
322 323 324 325 326 327
// tsdbDataIter.c ==============================================================================================
#define TSDB_MEM_TABLE_DATA_ITER 0
#define TSDB_DATA_FILE_DATA_ITER 1
#define TSDB_STT_FILE_DATA_ITER  2
#define TSDB_TOMB_FILE_DATA_ITER 3

H
Hongze Cheng 已提交
328 329 330
#define TSDB_FILTER_FLAG_BY_VERSION           0x1
#define TSDB_FILTER_FLAG_BY_TABLEID           0x2
#define TSDB_FILTER_FLAG_IGNORE_DROPPED_TABLE 0x4
H
Hongze Cheng 已提交
331 332 333 334 335 336 337 338 339 340 341 342

#define TSDB_RBTN_TO_DATA_ITER(pNode) ((STsdbDataIter2 *)(((char *)pNode) - offsetof(STsdbDataIter2, rbtn)))
/* open */
int32_t tsdbOpenDataFileDataIter(SDataFReader *pReader, STsdbDataIter2 **ppIter);
int32_t tsdbOpenSttFileDataIter(SDataFReader *pReader, int32_t iStt, STsdbDataIter2 **ppIter);
int32_t tsdbOpenTombFileDataIter(SDelFReader *pReader, STsdbDataIter2 **ppIter);
/* close */
void tsdbCloseDataIter2(STsdbDataIter2 *pIter);
/* cmpr */
int32_t tsdbDataIterCmprFn(const SRBTreeNode *pNode1, const SRBTreeNode *pNode2);
/* next */
int32_t tsdbDataIterNext2(STsdbDataIter2 *pIter, STsdbFilterInfo *pFilterInfo);
H
Hongze Cheng 已提交
343

H
Hongze Cheng 已提交
344
// structs =======================
H
Hongze Cheng 已提交
345 346 347 348 349
struct STsdbFS {
  SDelFile *pDelFile;
  SArray   *aDFileSet;  // SArray<SDFileSet>
};

350
typedef struct {
351 352 353 354 355 356 357 358 359
  rocksdb_t                           *db;
  rocksdb_comparator_t                *my_comparator;
  rocksdb_cache_t                     *blockcache;
  rocksdb_block_based_table_options_t *tableoptions;
  rocksdb_options_t                   *options;
  rocksdb_flushoptions_t              *flushoptions;
  rocksdb_writeoptions_t              *writeoptions;
  rocksdb_readoptions_t               *readoptions;
  rocksdb_writebatch_t                *writebatch;
360
  rocksdb_writebatch_t                *rwritebatch;
361 362
  TdThreadMutex                        rMutex;
  STSchema                            *pTSchema;
363 364
} SRocksCache;

D
dapan1121 已提交
365 366 367 368 369
typedef struct {
  STsdb *pTsdb;
  int    flush_count;
} SCacheFlushState;

H
Hongze Cheng 已提交
370
struct STsdb {
M
Minglei Jin 已提交
371 372 373 374 375 376 377 378 379 380 381 382
  char                *path;
  SVnode              *pVnode;
  STsdbKeepCfg         keepCfg;
  TdThreadRwlock       rwLock;
  SMemTable           *mem;
  SMemTable           *imem;
  STsdbFS              fs;  // old
  SLRUCache           *lruCache;
  SCacheFlushState     flushState;
  TdThreadMutex        lruMutex;
  SLRUCache           *biCache;
  TdThreadMutex        biMutex;
H
Hongze Cheng 已提交
383
  struct STFileSystem *pFS;  // new
M
Minglei Jin 已提交
384
  SRocksCache          rCache;
H
Hongze Cheng 已提交
385 386
};

H
refact  
Hongze Cheng 已提交
387 388 389 390 391
struct TSDBKEY {
  int64_t version;
  TSKEY   ts;
};

H
Hongze Cheng 已提交
392 393 394
typedef struct SMemSkipListNode SMemSkipListNode;
struct SMemSkipListNode {
  int8_t            level;
H
Hongze Cheng 已提交
395 396
  int8_t            flag;  // TSDBROW_ROW_FMT for row format, TSDBROW_COL_FMT for col format
  int32_t           iRow;
H
Hongze Cheng 已提交
397
  int64_t           version;
H
Hongze Cheng 已提交
398
  void             *pData;
H
Hongze Cheng 已提交
399 400
  SMemSkipListNode *forwards[0];
};
H
Hongze Cheng 已提交
401

H
Hongze Cheng 已提交
402 403
typedef struct SMemSkipList {
  int64_t           size;
H
Hongze Cheng 已提交
404
  uint32_t          seed;
H
Hongze Cheng 已提交
405 406 407 408 409 410
  int8_t            maxLevel;
  int8_t            level;
  SMemSkipListNode *pHead;
  SMemSkipListNode *pTail;
} SMemSkipList;

H
Hongze Cheng 已提交
411
struct STbData {
H
Hongze Cheng 已提交
412 413
  tb_uid_t     suid;
  tb_uid_t     uid;
H
Hongze Cheng 已提交
414 415
  TSKEY        minKey;
  TSKEY        maxKey;
H
Hongze Cheng 已提交
416 417
  SDelData    *pHead;
  SDelData    *pTail;
H
Hongze Cheng 已提交
418
  SMemSkipList sl;
H
Hongze Cheng 已提交
419
  STbData     *next;
H
Hongze Cheng 已提交
420
  SRBTreeNode  rbtn[1];
H
Hongze Cheng 已提交
421
};
H
Hongze Cheng 已提交
422

H
refact  
Hongze Cheng 已提交
423
struct SMemTable {
H
Hongze Cheng 已提交
424 425 426 427
  SRWLatch         latch;
  STsdb           *pTsdb;
  SVBufPool       *pPool;
  volatile int32_t nRef;
H
Hongze Cheng 已提交
428 429
  int64_t          minVer;
  int64_t          maxVer;
H
Hongze Cheng 已提交
430 431 432 433
  TSKEY            minKey;
  TSKEY            maxKey;
  int64_t          nRow;
  int64_t          nDel;
H
Hongze Cheng 已提交
434 435 436
  int32_t          nTbData;
  int32_t          nBucket;
  STbData        **aBucket;
H
Hongze Cheng 已提交
437
  SRBTree          tbDataTree[1];
H
Hongze Cheng 已提交
438
};
H
Hongze Cheng 已提交
439

H
Hongze Cheng 已提交
440
struct TSDBROW {
H
Hongze Cheng 已提交
441
  int8_t type;  // TSDBROW_ROW_FMT for row from tsRow, TSDBROW_COL_FMT for row from block data
H
Hongze Cheng 已提交
442 443 444
  union {
    struct {
      int64_t version;
H
Hongze Cheng 已提交
445
      SRow   *pTSRow;
H
Hongze Cheng 已提交
446 447
    };
    struct {
H
Hongze Cheng 已提交
448 449
      SBlockData *pBlockData;
      int32_t     iRow;
H
Hongze Cheng 已提交
450 451
    };
  };
H
Hongze Cheng 已提交
452 453
};

H
Hongze Cheng 已提交
454
struct SBlockIdx {
H
Hongze Cheng 已提交
455 456
  int64_t suid;
  int64_t uid;
H
Hongze Cheng 已提交
457 458 459 460
  int64_t offset;
  int64_t size;
};

H
Hongze Cheng 已提交
461 462
struct SMapData {
  int32_t  nItem;
H
Hongze Cheng 已提交
463
  int32_t  nData;
464
  int32_t *aOffset;
H
Hongze Cheng 已提交
465 466 467
  uint8_t *pData;
};

H
Hongze Cheng 已提交
468
struct SBlockCol {
H
Hongze Cheng 已提交
469 470 471 472 473 474 475 476 477
  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 已提交
478
};
H
Hongze Cheng 已提交
479

H
Hongze Cheng 已提交
480
struct SBlockInfo {
H
Hongze Cheng 已提交
481 482 483
  int64_t offset;  // block data offset
  int32_t szBlock;
  int32_t szKey;
H
Hongze Cheng 已提交
484
};
H
Hongze Cheng 已提交
485

H
Hongze Cheng 已提交
486
struct SSmaInfo {
H
Hongze Cheng 已提交
487 488
  int64_t offset;
  int32_t size;
H
Hongze Cheng 已提交
489
};
H
Hongze Cheng 已提交
490

H
Hongze Cheng 已提交
491
struct SBlkInfo {
H
Hongze Cheng 已提交
492 493
  int64_t minUid;
  int64_t maxUid;
H
Hongze Cheng 已提交
494 495
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
496 497
  int64_t minVer;
  int64_t maxVer;
H
Hongze Cheng 已提交
498 499 500
  TSDBKEY minTKey;
  TSDBKEY maxTKey;
};
H
Hongze Cheng 已提交
501

H
Hongze Cheng 已提交
502
struct SDataBlk {
H
Hongze Cheng 已提交
503 504 505 506 507 508 509
  TSDBKEY    minKey;
  TSDBKEY    maxKey;
  int64_t    minVer;
  int64_t    maxVer;
  int32_t    nRow;
  int8_t     hasDup;
  int8_t     nSubBlock;
H
Hongze Cheng 已提交
510
  SBlockInfo aSubBlock[1];
H
Hongze Cheng 已提交
511
  SSmaInfo   smaInfo;
H
refact  
Hongze Cheng 已提交
512
};
H
Hongze Cheng 已提交
513

H
Hongze Cheng 已提交
514
struct SSttBlk {
H
Hongze Cheng 已提交
515 516 517
  int64_t    suid;
  int64_t    minUid;
  int64_t    maxUid;
H
Hongze Cheng 已提交
518 519
  TSKEY      minKey;
  TSKEY      maxKey;
H
Hongze Cheng 已提交
520 521 522 523
  int64_t    minVer;
  int64_t    maxVer;
  int32_t    nRow;
  SBlockInfo bInfo;
H
Hongze Cheng 已提交
524 525
};

H
Hongze Cheng 已提交
526 527 528 529
// (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 已提交
530
struct SBlockData {
H
Hongze Cheng 已提交
531 532 533 534 535 536 537 538
  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
  int32_t   nColData;
  SColData *aColData;
H
Hongze Cheng 已提交
539
};
H
Hongze Cheng 已提交
540

H
Hongze Cheng 已提交
541 542 543 544 545
struct TABLEID {
  tb_uid_t suid;
  tb_uid_t uid;
};

H
Hongze Cheng 已提交
546 547 548 549
struct STbDataIter {
  STbData          *pTbData;
  int8_t            backward;
  SMemSkipListNode *pNode;
H
Hongze Cheng 已提交
550 551
  TSDBROW          *pRow;
  TSDBROW           row;
H
Hongze Cheng 已提交
552 553
};

H
Hongze Cheng 已提交
554
struct SDelData {
H
Hongze Cheng 已提交
555 556 557 558
  int64_t   version;
  TSKEY     sKey;
  TSKEY     eKey;
  SDelData *pNext;
H
Hongze Cheng 已提交
559
};
H
Hongze Cheng 已提交
560

H
Hongze Cheng 已提交
561
struct SDelIdx {
H
Hongze Cheng 已提交
562 563 564 565
  tb_uid_t suid;
  tb_uid_t uid;
  int64_t  offset;
  int64_t  size;
H
Hongze Cheng 已提交
566
};
H
Hongze Cheng 已提交
567

H
Hongze Cheng 已提交
568
struct SDiskDataHdr {
H
more  
Hongze Cheng 已提交
569
  uint32_t delimiter;
H
Hongze Cheng 已提交
570
  uint32_t fmtVer;
H
more  
Hongze Cheng 已提交
571 572
  int64_t  suid;
  int64_t  uid;
H
Hongze Cheng 已提交
573 574 575 576
  int32_t  szUid;
  int32_t  szVer;
  int32_t  szKey;
  int32_t  szBlkCol;
H
Hongze Cheng 已提交
577
  int32_t  nRow;
H
Hongze Cheng 已提交
578
  int8_t   cmprAlg;
H
Hongze Cheng 已提交
579 580
};

H
Hongze Cheng 已提交
581 582 583 584 585 586 587 588
struct SDelFile {
  volatile int32_t nRef;

  int64_t commitID;
  int64_t size;
  int64_t offset;
};

H
Hongze Cheng 已提交
589
struct SHeadFile {
H
Hongze Cheng 已提交
590 591
  volatile int32_t nRef;

H
Hongze Cheng 已提交
592
  int64_t commitID;
H
Hongze Cheng 已提交
593 594 595 596 597
  int64_t size;
  int64_t offset;
};

struct SDataFile {
H
Hongze Cheng 已提交
598 599
  volatile int32_t nRef;

H
Hongze Cheng 已提交
600
  int64_t commitID;
H
Hongze Cheng 已提交
601 602 603
  int64_t size;
};

H
Hongze Cheng 已提交
604
struct SSttFile {
H
Hongze Cheng 已提交
605 606
  volatile int32_t nRef;

H
Hongze Cheng 已提交
607
  int64_t commitID;
H
Hongze Cheng 已提交
608
  int64_t size;
H
Hongze Cheng 已提交
609
  int64_t offset;
H
Hongze Cheng 已提交
610 611 612
};

struct SSmaFile {
H
Hongze Cheng 已提交
613 614
  volatile int32_t nRef;

H
Hongze Cheng 已提交
615
  int64_t commitID;
H
Hongze Cheng 已提交
616 617 618 619
  int64_t size;
};

struct SDFileSet {
H
Hongze Cheng 已提交
620 621 622 623 624
  SDiskID    diskId;
  int32_t    fid;
  SHeadFile *pHeadF;
  SDataFile *pDataF;
  SSmaFile  *pSmaF;
H
Hongze Cheng 已提交
625
  uint8_t    nSttF;
H
Hongze Cheng 已提交
626
  SSttFile  *aSttF[TSDB_MAX_STT_TRIGGER];
H
Hongze Cheng 已提交
627 628
};

629
struct STSDBRowIter {
H
Hongze Cheng 已提交
630 631 632 633 634 635 636 637
  TSDBROW *pRow;
  union {
    SRowIter *pIter;
    struct {
      int32_t iColData;
      SColVal cv;
    };
  };
H
Hongze Cheng 已提交
638
};
H
Hongze Cheng 已提交
639 640 641 642 643
struct SRowMerger {
  STSchema *pTSchema;
  int64_t   version;
  SArray   *pArray;  // SArray<SColVal>
};
H
Hongze Cheng 已提交
644

H
more  
Hongze Cheng 已提交
645
typedef struct {
H
Hongze Cheng 已提交
646
  char     *path;
H
more  
Hongze Cheng 已提交
647
  int32_t   szPage;
H
Hongze Cheng 已提交
648 649 650
  int32_t   flag;
  TdFilePtr pFD;
  int64_t   pgno;
H
more  
Hongze Cheng 已提交
651
  uint8_t  *pBuf;
H
Hongze Cheng 已提交
652
  int64_t   szFile;
H
more  
Hongze Cheng 已提交
653
} STsdbFD;
H
Hongze Cheng 已提交
654

H
Hongze Cheng 已提交
655 656 657 658
struct SDelFWriter {
  STsdb   *pTsdb;
  SDelFile fDel;
  STsdbFD *pWriteH;
H
Hongze Cheng 已提交
659
  uint8_t *aBuf[1];
H
Hongze Cheng 已提交
660 661
};

M
Minglei Jin 已提交
662 663 664 665 666 667
#include "tarray2.h"
//#include "tsdbFS2.h"
// struct STFileSet;
typedef struct STFileSet STFileSet;
typedef TARRAY2(STFileSet *) TFileSetArray;

H
Hongze Cheng 已提交
668
struct STsdbReadSnap {
M
Minglei Jin 已提交
669 670 671 672 673 674
  SMemTable     *pMem;
  SQueryNode    *pNode;
  SMemTable     *pIMem;
  SQueryNode    *pINode;
  TFileSetArray *pfSetArray;
  STsdbFS        fs;
H
Hongze Cheng 已提交
675 676
};

H
Hongze Cheng 已提交
677 678 679 680
struct SDataFWriter {
  STsdb    *pTsdb;
  SDFileSet wSet;

H
Hongze Cheng 已提交
681 682 683
  STsdbFD *pHeadFD;
  STsdbFD *pDataFD;
  STsdbFD *pSmaFD;
H
Hongze Cheng 已提交
684
  STsdbFD *pSttFD;
H
Hongze Cheng 已提交
685 686 687 688

  SHeadFile fHead;
  SDataFile fData;
  SSmaFile  fSma;
H
Hongze Cheng 已提交
689
  SSttFile  fStt[TSDB_MAX_STT_TRIGGER];
H
Hongze Cheng 已提交
690

H
Hongze Cheng 已提交
691
  uint8_t *aBuf[4];
H
Hongze Cheng 已提交
692 693
};

H
Hongze Cheng 已提交
694 695 696
struct SDataFReader {
  STsdb     *pTsdb;
  SDFileSet *pSet;
H
Hongze Cheng 已提交
697 698 699
  STsdbFD   *pHeadFD;
  STsdbFD   *pDataFD;
  STsdbFD   *pSmaFD;
H
Hongze Cheng 已提交
700
  STsdbFD   *aSttFD[TSDB_MAX_STT_TRIGGER];
H
Hongze Cheng 已提交
701
  uint8_t   *aBuf[3];
H
Hongze Cheng 已提交
702 703
};

H
Hongze Cheng 已提交
704
// NOTE: do NOT change the order of the fields
H
Hongze Cheng 已提交
705 706 707 708 709 710
typedef struct {
  int64_t suid;
  int64_t uid;
  TSDBROW row;
} SRowInfo;

711 712 713 714 715 716 717
typedef struct SSttBlockLoadCostInfo {
  int64_t loadBlocks;
  int64_t loadStatisBlocks;
  double  blockElapsedTime;
  double  statisElapsedTime;
} SSttBlockLoadCostInfo;

718
typedef struct SSttBlockLoadInfo {
719 720 721
  SBlockData blockData[2];     // buffered block data
  int32_t    statisBlockIndex; // buffered statistics block index
  void      *statisBlock;      // buffered statistics block data
722
  void      *pSttStatisBlkArray;
723
  SArray    *aSttBlk;
724
  int32_t    blockIndex[2];  // to denote the loaded block in the corresponding position.
725 726 727 728
  int32_t    currentLoadBlockIndex;
  STSchema  *pSchema;
  int16_t   *colIds;
  int32_t    numOfCols;
729
  bool       checkRemainingRow;  // todo: no assign value?
730 731
  bool       isLast;
  bool       sttBlockLoaded;
H
Haojun Liao 已提交
732

733
  SSttBlockLoadCostInfo cost;
734 735
} SSttBlockLoadInfo;

736
typedef struct SMergeTree {
H
Hongze Cheng 已提交
737 738 739 740 741 742
  int8_t             backward;
  SRBTree            rbt;
  SLDataIter        *pIter;
  bool               destroyLoadInfo;
  SSttBlockLoadInfo *pLoadInfo;
  const char        *idStr;
D
dapan1121 已提交
743
  bool               ignoreEarlierTs;
744 745
} SMergeTree;

H
Hongze Cheng 已提交
746 747 748 749 750 751
typedef struct {
  int64_t   suid;
  int64_t   uid;
  STSchema *pTSchema;
} SSkmInfo;

H
Hongze Cheng 已提交
752 753 754 755 756
struct SDiskCol {
  SBlockCol      bCol;
  const uint8_t *pBit;
  const uint8_t *pOff;
  const uint8_t *pVal;
H
Hongze Cheng 已提交
757
  SColumnDataAgg agg;
H
Hongze Cheng 已提交
758 759 760 761 762 763 764 765 766 767
};

struct SDiskData {
  SDiskDataHdr   hdr;
  const uint8_t *pUid;
  const uint8_t *pVer;
  const uint8_t *pKey;
  SArray        *aDiskCol;  // SArray<SDiskCol>
};

H
Hongze Cheng 已提交
768 769 770 771 772 773 774 775 776 777 778 779
struct SDiskDataBuilder {
  int64_t      suid;
  int64_t      uid;
  int32_t      nRow;
  uint8_t      cmprAlg;
  uint8_t      calcSma;
  SCompressor *pUidC;
  SCompressor *pVerC;
  SCompressor *pKeyC;
  int32_t      nBuilder;
  SArray      *aBuilder;  // SArray<SDiskColBuilder>
  uint8_t     *aBuf[2];
H
Hongze Cheng 已提交
780
  SDiskData    dd;
H
Hongze Cheng 已提交
781
  SBlkInfo     bi;
H
Hongze Cheng 已提交
782 783
};

784
typedef struct SLDataIter {
H
Hongze Cheng 已提交
785 786
  SRBTreeNode            node;
  SSttBlk               *pSttBlk;
M
Minglei Jin 已提交
787
  int32_t                iStt;  // for debug purpose
H
Hongze Cheng 已提交
788 789 790 791 792 793 794 795 796 797
  int8_t                 backward;
  int32_t                iSttBlk;
  int32_t                iRow;
  SRowInfo               rInfo;
  uint64_t               uid;
  STimeWindow            timeWindow;
  SVersionRange          verRange;
  SSttBlockLoadInfo     *pBlockLoadInfo;
  bool                   ignoreEarlierTs;
  struct SSttFileReader *pReader;
798 799
} SLDataIter;

800
#define tMergeTreeGetRow(_t) (&((_t)->pIter->rInfo.row))
H
Haojun Liao 已提交
801
int32_t tMergeTreeOpen(SMergeTree *pMTree, int8_t backward, SDataFReader *pFReader, uint64_t suid, uint64_t uid,
802
                       STimeWindow *pTimeWindow, SVersionRange *pVerRange, SSttBlockLoadInfo *pBlockLoadInfo,
803
                       bool destroyLoadInfo, const char *idStr, bool strictTimeRange, SLDataIter *pLDataIter);
804

805 806 807 808
struct SSttFileReader;
typedef int32_t (*_load_tomb_fn)(STsdbReader *pReader, struct SSttFileReader *pSttFileReader,
                                 SSttBlockLoadInfo *pLoadInfo);

809
typedef struct {
M
Minglei Jin 已提交
810 811 812 813 814 815 816 817 818 819 820 821
  int8_t        backward;
  STsdb        *pTsdb;
  uint64_t      suid;
  uint64_t      uid;
  STimeWindow   timewindow;
  SVersionRange verRange;
  bool          strictTimeRange;
  SArray       *pSttFileBlockIterArray;
  void         *pCurrentFileset;
  STSchema     *pSchema;
  int16_t      *pCols;
  int32_t       numOfCols;
822
  _load_tomb_fn loadTombFn;
M
Minglei Jin 已提交
823 824
  void         *pReader;
  void         *idstr;
825
} SMergeTreeConf;
M
Minglei Jin 已提交
826
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf);
827

H
Hongze Cheng 已提交
828 829 830 831
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter);
bool tMergeTreeNext(SMergeTree *pMTree);
bool tMergeTreeIgnoreEarlierTs(SMergeTree *pMTree);
void tMergeTreeClose(SMergeTree *pMTree);
832

833
SSttBlockLoadInfo *tCreateLastBlockLoadInfo(STSchema *pSchema, int16_t *colList, int32_t numOfCols, int32_t numOfStt);
834
SSttBlockLoadInfo *tCreateOneLastBlockLoadInfo(STSchema *pSchema, int16_t *colList, int32_t numOfCols);
H
Hongze Cheng 已提交
835
void               resetLastBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo);
836
void               getSttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo, SSttBlockLoadCostInfo *pLoadCost);
H
Hongze Cheng 已提交
837
void              *destroyLastBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo);
838
void              *destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost);
839

840
// tsdbCache ==============================================================================================
841 842 843 844 845
typedef enum {
  READ_MODE_COUNT_ONLY = 0x1,
  READ_MODE_ALL,
} EReadMode;

846 847
typedef struct {
  TSKEY   ts;
848
  int8_t  dirty;
849 850 851 852 853
  SColVal colVal;
} SLastCol;

int32_t tsdbOpenCache(STsdb *pTsdb);
void    tsdbCloseCache(STsdb *pTsdb);
854
int32_t tsdbCacheUpdate(STsdb *pTsdb, tb_uid_t suid, tb_uid_t uid, TSDBROW *row);
855 856
int32_t tsdbCacheDel(STsdb *pTsdb, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey);

H
Hongze Cheng 已提交
857 858
int32_t tsdbCacheInsertLast(SLRUCache *pCache, tb_uid_t uid, TSDBROW *row, STsdb *pTsdb);
int32_t tsdbCacheInsertLastrow(SLRUCache *pCache, STsdb *pTsdb, tb_uid_t uid, TSDBROW *row, bool dup);
859 860
int32_t tsdbCacheRelease(SLRUCache *pCache, LRUHandle *h);

861 862 863
int32_t tsdbCacheGetBlockIdx(SLRUCache *pCache, SDataFReader *pFileReader, LRUHandle **handle);
int32_t tsdbBICacheRelease(SLRUCache *pCache, LRUHandle *h);

864 865 866 867
int32_t tsdbCacheDeleteLastrow(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey);
int32_t tsdbCacheDeleteLast(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey);
int32_t tsdbCacheDelete(SLRUCache *pCache, tb_uid_t uid, TSKEY eKey);

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
// ========== 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 已提交
888 889
// #define SL_NODE_FORWARD(n, l)  ((n)->forwards[l])
// #define SL_NODE_BACKWARD(n, l) ((n)->forwards[(n)->level + (l)])
H
Hongze Cheng 已提交
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

static FORCE_INLINE TSDBROW *tsdbTbDataIterGet(STbDataIter *pIter) {
  if (pIter == NULL) return NULL;

  if (pIter->pRow) {
    return pIter->pRow;
  }

  if (pIter->backward) {
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
      return NULL;
    }
  } else {
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
      return NULL;
    }
  }

  pIter->pRow = &pIter->row;
H
Hongze Cheng 已提交
909 910 911 912 913 914 915
  if (pIter->pNode->flag == TSDBROW_ROW_FMT) {
    pIter->row = tsdbRowFromTSRow(pIter->pNode->version, pIter->pNode->pData);
  } else if (pIter->pNode->flag == TSDBROW_COL_FMT) {
    pIter->row = tsdbRowFromBlockData(pIter->pNode->pData, pIter->pNode->iRow);
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
916 917 918 919

  return pIter->pRow;
}

H
Hongze Cheng 已提交
920 921
int32_t tRowInfoCmprFn(const void *p1, const void *p2);

H
Hongze Cheng 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
typedef struct {
  int64_t  suid;
  int64_t  uid;
  SDelData delData;
} SDelInfo;

struct STsdbDataIter2 {
  STsdbDataIter2 *next;
  SRBTreeNode     rbtn;

  int32_t  type;
  SRowInfo rowInfo;
  SDelInfo delInfo;
  union {
    // TSDB_MEM_TABLE_DATA_ITER
    struct {
      SMemTable *pMemTable;
    } mIter;

    // TSDB_DATA_FILE_DATA_ITER
    struct {
      SDataFReader *pReader;
      SArray       *aBlockIdx;  // SArray<SBlockIdx>
      SMapData      mDataBlk;
      SBlockData    bData;
      int32_t       iBlockIdx;
      int32_t       iDataBlk;
      int32_t       iRow;
    } dIter;

    // TSDB_STT_FILE_DATA_ITER
    struct {
      SDataFReader *pReader;
      int32_t       iStt;
      SArray       *aSttBlk;
      SBlockData    bData;
      int32_t       iSttBlk;
      int32_t       iRow;
    } sIter;
    // TSDB_TOMB_FILE_DATA_ITER
    struct {
      SDelFReader *pReader;
      SArray      *aDelIdx;
      SArray      *aDelData;
      int32_t      iDelIdx;
      int32_t      iDelData;
    } tIter;
  };
};

struct STsdbFilterInfo {
  int32_t flag;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
976
  TABLEID tbid;
H
Hongze Cheng 已提交
977 978
};

H
Hongze Cheng 已提交
979 980 981 982
#ifdef __cplusplus
}
#endif

983
#endif /*_TD_VNODE_TSDB_H_*/