tsdb.h 27.7 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 46 47 48 49 50 51 52 53 54 55 56 57 58
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;
59
typedef struct STSDBRowIter     STSDBRowIter;
H
Hongze Cheng 已提交
60 61 62 63 64 65 66 67 68 69 70
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 SVersionRange    SVersionRange;
typedef struct SLDataIter       SLDataIter;
typedef struct SDiskCol         SDiskCol;
typedef struct SDiskData        SDiskData;
typedef struct SDiskDataBuilder SDiskDataBuilder;
H
Hongze Cheng 已提交
71
typedef struct SBlkInfo         SBlkInfo;
H
Hongze Cheng 已提交
72

H
Hongze Cheng 已提交
73 74 75
#define TSDB_FILE_DLMT     ((uint32_t)0xF00AFA0F)
#define TSDB_MAX_SUBBLOCKS 8
#define TSDB_FHDR_SIZE     512
H
refact  
Hongze Cheng 已提交
76

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

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

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

H
Hongze Cheng 已提交
85 86 87 88 89 90
#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 已提交
91 92 93 94 95

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 已提交
96
  if (fOffSet % szPage == 0) {
H
Hongze Cheng 已提交
97 98 99 100 101
    pgno--;
  }

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

H
Hongze Cheng 已提交
103
// tsdbUtil.c ==============================================================================================
H
Hongze Cheng 已提交
104
// TSDBROW
H
Hongze Cheng 已提交
105 106
#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 已提交
107
#define TSDBROW_SVERSION(ROW)                 TD_ROW_SVER((ROW)->pTSRow)
H
Hongze Cheng 已提交
108
#define TSDBROW_KEY(ROW)                      ((TSDBKEY){.version = TSDBROW_VERSION(ROW), .ts = TSDBROW_TS(ROW)})
H
Hongze Cheng 已提交
109 110
#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 已提交
111 112
void tsdbRowGetColVal(TSDBROW *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal);
// int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow);
H
Hongze Cheng 已提交
113
int32_t tsdbRowCmprFn(const void *p1, const void *p2);
114 115 116
// STSDBRowIter
void     tsdbRowIterInit(STSDBRowIter *pIter, TSDBROW *pRow, STSchema *pTSchema);
SColVal *tsdbRowIterNext(STSDBRowIter *pIter);
H
Hongze Cheng 已提交
117
// SRowMerger
118 119 120
int32_t tRowMergerInit2(SRowMerger *pMerger, STSchema *pResTSchema, TSDBROW *pRow, STSchema *pTSchema);
int32_t tRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema);

H
Hongze Cheng 已提交
121 122 123 124
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 已提交
125
// TABLEID
H
Hongze Cheng 已提交
126
int32_t tTABLEIDCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
127
// TSDBKEY
H
Hongze Cheng 已提交
128 129
#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 已提交
130 131 132
// SBlockCol
int32_t tPutBlockCol(uint8_t *p, void *ph);
int32_t tGetBlockCol(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
133
int32_t tBlockColCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
134
// SDataBlk
H
Hongze Cheng 已提交
135
void    tDataBlkReset(SDataBlk *pBlock);
H
Hongze Cheng 已提交
136 137
int32_t tPutDataBlk(uint8_t *p, void *ph);
int32_t tGetDataBlk(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
138 139
int32_t tDataBlkCmprFn(const void *p1, const void *p2);
bool    tDataBlkHasSma(SDataBlk *pDataBlk);
H
Hongze Cheng 已提交
140 141 142
// SSttBlk
int32_t tPutSttBlk(uint8_t *p, void *ph);
int32_t tGetSttBlk(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
143 144 145
// SBlockIdx
int32_t tPutBlockIdx(uint8_t *p, void *ph);
int32_t tGetBlockIdx(uint8_t *p, void *ph);
146
int32_t tCmprBlockIdx(void const *lhs, void const *rhs);
147
int32_t tCmprBlockL(void const *lhs, void const *rhs);
H
Hongze Cheng 已提交
148
// SBlockData
H
Hongze Cheng 已提交
149 150
#define tBlockDataFirstRow(PBLOCKDATA) tsdbRowFromBlockData(PBLOCKDATA, 0)
#define tBlockDataLastRow(PBLOCKDATA)  tsdbRowFromBlockData(PBLOCKDATA, (PBLOCKDATA)->nRow - 1)
H
Hongze Cheng 已提交
151 152
#define tBlockDataFirstKey(PBLOCKDATA) TSDBROW_KEY(&tBlockDataFirstRow(PBLOCKDATA))
#define tBlockDataLastKey(PBLOCKDATA)  TSDBROW_KEY(&tBlockDataLastRow(PBLOCKDATA))
H
Hongze Cheng 已提交
153

H
Hongze Cheng 已提交
154 155
int32_t   tBlockDataCreate(SBlockData *pBlockData);
void      tBlockDataDestroy(SBlockData *pBlockData, int8_t deepClear);
H
Hongze Cheng 已提交
156
int32_t   tBlockDataInit(SBlockData *pBlockData, TABLEID *pId, STSchema *pTSchema, int16_t *aCid, int32_t nCid);
H
Hongze Cheng 已提交
157 158 159
void      tBlockDataReset(SBlockData *pBlockData);
int32_t   tBlockDataAppendRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema, int64_t uid);
void      tBlockDataClear(SBlockData *pBlockData);
H
Hongze Cheng 已提交
160 161
SColData *tBlockDataGetColDataByIdx(SBlockData *pBlockData, int32_t idx);
void      tBlockDataGetColData(SBlockData *pBlockData, int16_t cid, SColData **ppColData);
H
Hongze Cheng 已提交
162
int32_t   tBlockDataMerge(SBlockData *pBlockData1, SBlockData *pBlockData2, SBlockData *pBlockData);
H
Hongze Cheng 已提交
163
int32_t   tBlockDataAddColData(SBlockData *pBlockData, SColData **ppColData);
H
Hongze Cheng 已提交
164 165
int32_t   tCmprBlockData(SBlockData *pBlockData, int8_t cmprAlg, uint8_t **ppOut, int32_t *szOut, uint8_t *aBuf[],
                         int32_t aBufN[]);
H
Hongze Cheng 已提交
166
int32_t   tDecmprBlockData(uint8_t *pIn, int32_t szIn, SBlockData *pBlockData, uint8_t *aBuf[]);
H
Hongze Cheng 已提交
167
// SDiskDataHdr
H
Hongze Cheng 已提交
168
int32_t tPutDiskDataHdr(uint8_t *p, const SDiskDataHdr *pHdr);
H
Hongze Cheng 已提交
169
int32_t tGetDiskDataHdr(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
170
// SDelIdx
H
Hongze Cheng 已提交
171 172
int32_t tPutDelIdx(uint8_t *p, void *ph);
int32_t tGetDelIdx(uint8_t *p, void *ph);
173
int32_t tCmprDelIdx(void const *lhs, void const *rhs);
H
Hongze Cheng 已提交
174
// SDelData
H
Hongze Cheng 已提交
175 176
int32_t tPutDelData(uint8_t *p, void *ph);
int32_t tGetDelData(uint8_t *p, void *ph);
H
Hongze Cheng 已提交
177
// SMapData
H
Hongze Cheng 已提交
178
#define tMapDataInit() ((SMapData){0})
H
Hongze Cheng 已提交
179 180 181
void    tMapDataReset(SMapData *pMapData);
void    tMapDataClear(SMapData *pMapData);
int32_t tMapDataPutItem(SMapData *pMapData, void *pItem, int32_t (*tPutItemFn)(uint8_t *, void *));
H
Hongze Cheng 已提交
182
int32_t tMapDataCopy(SMapData *pFrom, SMapData *pTo);
H
Hongze Cheng 已提交
183
void    tMapDataGetItemByIdx(SMapData *pMapData, int32_t idx, void *pItem, int32_t (*tGetItemFn)(uint8_t *, void *));
H
Hongze Cheng 已提交
184 185
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 已提交
186 187
int32_t tPutMapData(uint8_t *p, SMapData *pMapData);
int32_t tGetMapData(uint8_t *p, SMapData *pMapData);
H
Hongze Cheng 已提交
188 189
int32_t tMapDataToArray(SMapData *pMapData, int32_t itemSize, int32_t (*tGetItemFn)(uint8_t *, void *),
                        SArray **ppArray);
H
Hongze Cheng 已提交
190 191 192
// 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 已提交
193
int32_t tsdbFidLevel(int32_t fid, STsdbKeepCfg *pKeepCfg, int64_t now);
H
Hongze Cheng 已提交
194
int32_t tsdbBuildDeleteSkyline(SArray *aDelData, int32_t sidx, int32_t eidx, SArray *aSkyline);
H
Hongze Cheng 已提交
195 196
int32_t tPutColumnDataAgg(uint8_t *p, SColumnDataAgg *pColAgg);
int32_t tGetColumnDataAgg(uint8_t *p, SColumnDataAgg *pColAgg);
H
Hongze Cheng 已提交
197 198
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 已提交
199 200
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 已提交
201
int32_t tsdbCmprColData(SColData *pColData, int8_t cmprAlg, SBlockCol *pBlockCol, uint8_t **ppOut, int32_t nOut,
H
Hongze Cheng 已提交
202
                        uint8_t **ppBuf);
H
Hongze Cheng 已提交
203 204
int32_t tsdbDecmprColData(uint8_t *pIn, SBlockCol *pBlockCol, int8_t cmprAlg, int32_t nVal, SColData *pColData,
                          uint8_t **ppBuf);
H
Hongze Cheng 已提交
205 206
// tsdbMemTable ==============================================================================================
// SMemTable
H
Hongze Cheng 已提交
207 208 209 210 211 212
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 已提交
213
// STbDataIter
H
Hongze Cheng 已提交
214 215 216 217
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
Hongze Cheng 已提交
218 219
// STbData
int32_t tsdbGetNRowsInTbData(STbData *pTbData);
H
Hongze Cheng 已提交
220
// tsdbFile.c ==============================================================================================
H
Hongze Cheng 已提交
221
typedef enum { TSDB_HEAD_FILE = 0, TSDB_DATA_FILE, TSDB_LAST_FILE, TSDB_SMA_FILE } EDataFileT;
H
Hongze Cheng 已提交
222

H
Hongze Cheng 已提交
223
bool    tsdbDelFileIsSame(SDelFile *pDelFile1, SDelFile *pDelFile2);
H
Hongze Cheng 已提交
224
int32_t tsdbDFileRollback(STsdb *pTsdb, SDFileSet *pSet, EDataFileT ftype);
H
Hongze Cheng 已提交
225 226
int32_t tPutHeadFile(uint8_t *p, SHeadFile *pHeadFile);
int32_t tPutDataFile(uint8_t *p, SDataFile *pDataFile);
H
Hongze Cheng 已提交
227
int32_t tPutSttFile(uint8_t *p, SSttFile *pSttFile);
H
Hongze Cheng 已提交
228
int32_t tPutSmaFile(uint8_t *p, SSmaFile *pSmaFile);
H
Hongze Cheng 已提交
229 230 231 232
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 已提交
233 234 235

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 已提交
236
void tsdbSttFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSttFile *pSttF, char fname[]);
H
Hongze Cheng 已提交
237
void tsdbSmaFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSmaFile *pSmaF, char fname[]);
H
Hongze Cheng 已提交
238
// SDelFile
H
Hongze Cheng 已提交
239
void tsdbDelFileName(STsdb *pTsdb, SDelFile *pFile, char fname[]);
H
Hongze Cheng 已提交
240
// tsdbFS.c ==============================================================================================
H
Hongze Cheng 已提交
241
int32_t tsdbFSOpen(STsdb *pTsdb, int8_t rollback);
H
Hongze Cheng 已提交
242 243 244 245
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 已提交
246 247 248
int32_t tsdbFSCommit(STsdb *pTsdb);
int32_t tsdbFSRollback(STsdb *pTsdb);
int32_t tsdbFSPrepareCommit(STsdb *pTsdb, STsdbFS *pFS);
H
Hongze Cheng 已提交
249 250 251 252 253
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 已提交
254 255 256
// tsdbReaderWriter.c ==============================================================================================
// SDataFWriter
int32_t tsdbDataFWriterOpen(SDataFWriter **ppWriter, STsdb *pTsdb, SDFileSet *pSet);
H
Hongze Cheng 已提交
257
int32_t tsdbDataFWriterClose(SDataFWriter **ppWriter, int8_t sync);
H
Hongze Cheng 已提交
258
int32_t tsdbUpdateDFileSetHeader(SDataFWriter *pWriter);
H
Hongze Cheng 已提交
259
int32_t tsdbWriteBlockIdx(SDataFWriter *pWriter, SArray *aBlockIdx);
H
Hongze Cheng 已提交
260
int32_t tsdbWriteDataBlk(SDataFWriter *pWriter, SMapData *mDataBlk, SBlockIdx *pBlockIdx);
H
Hongze Cheng 已提交
261
int32_t tsdbWriteSttBlk(SDataFWriter *pWriter, SArray *aSttBlk);
H
Hongze Cheng 已提交
262
int32_t tsdbWriteBlockData(SDataFWriter *pWriter, SBlockData *pBlockData, SBlockInfo *pBlkInfo, SSmaInfo *pSmaInfo,
H
Hongze Cheng 已提交
263
                           int8_t cmprAlg, int8_t toLast);
H
Hongze Cheng 已提交
264
int32_t tsdbWriteDiskData(SDataFWriter *pWriter, const SDiskData *pDiskData, SBlockInfo *pBlkInfo, SSmaInfo *pSmaInfo);
H
Hongze Cheng 已提交
265 266

int32_t tsdbDFileSetCopy(STsdb *pTsdb, SDFileSet *pSetFrom, SDFileSet *pSetTo);
H
Hongze Cheng 已提交
267 268
// SDataFReader
int32_t tsdbDataFReaderOpen(SDataFReader **ppReader, STsdb *pTsdb, SDFileSet *pSet);
H
Hongze Cheng 已提交
269
int32_t tsdbDataFReaderClose(SDataFReader **ppReader);
H
Hongze Cheng 已提交
270
int32_t tsdbReadBlockIdx(SDataFReader *pReader, SArray *aBlockIdx);
H
Hongze Cheng 已提交
271
int32_t tsdbReadDataBlk(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *mDataBlk);
H
Hongze Cheng 已提交
272
int32_t tsdbReadSttBlk(SDataFReader *pReader, int32_t iStt, SArray *aSttBlk);
H
Hongze Cheng 已提交
273 274
int32_t tsdbReadBlockSma(SDataFReader *pReader, SDataBlk *pBlock, SArray *aColumnDataAgg);
int32_t tsdbReadDataBlock(SDataFReader *pReader, SDataBlk *pBlock, SBlockData *pBlockData);
H
Hongze Cheng 已提交
275
int32_t tsdbReadSttBlock(SDataFReader *pReader, int32_t iStt, SSttBlk *pSttBlk, SBlockData *pBlockData);
H
Hongze Cheng 已提交
276
int32_t tsdbReadSttBlockEx(SDataFReader *pReader, int32_t iStt, SSttBlk *pSttBlk, SBlockData *pBlockData);
H
Hongze Cheng 已提交
277 278
// SDelFWriter
int32_t tsdbDelFWriterOpen(SDelFWriter **ppWriter, SDelFile *pFile, STsdb *pTsdb);
H
Hongze Cheng 已提交
279
int32_t tsdbDelFWriterClose(SDelFWriter **ppWriter, int8_t sync);
H
Hongze Cheng 已提交
280 281
int32_t tsdbWriteDelData(SDelFWriter *pWriter, SArray *aDelData, SDelIdx *pDelIdx);
int32_t tsdbWriteDelIdx(SDelFWriter *pWriter, SArray *aDelIdx);
H
Hongze Cheng 已提交
282
int32_t tsdbUpdateDelFileHdr(SDelFWriter *pWriter);
H
Hongze Cheng 已提交
283
// SDelFReader
H
Hongze Cheng 已提交
284
int32_t tsdbDelFReaderOpen(SDelFReader **ppReader, SDelFile *pFile, STsdb *pTsdb);
H
Hongze Cheng 已提交
285
int32_t tsdbDelFReaderClose(SDelFReader **ppReader);
H
Hongze Cheng 已提交
286 287
int32_t tsdbReadDelData(SDelFReader *pReader, SDelIdx *pDelIdx, SArray *aDelData);
int32_t tsdbReadDelIdx(SDelFReader *pReader, SArray *aDelIdx);
H
Hongze Cheng 已提交
288
// tsdbRead.c ==============================================================================================
H
Hongze Cheng 已提交
289 290
int32_t tsdbTakeReadSnap(STsdb *pTsdb, STsdbReadSnap **ppSnap, const char *id);
void    tsdbUntakeReadSnap(STsdb *pTsdb, STsdbReadSnap *pSnap, const char *id);
H
Hongze Cheng 已提交
291 292
// tsdbMerge.c ==============================================================================================
int32_t tsdbMerge(STsdb *pTsdb);
H
Hongze Cheng 已提交
293

294 295 296 297
#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)

H
Hongze Cheng 已提交
298 299 300 301 302
// 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 已提交
303 304
int32_t tDiskDataBuilderClear(SDiskDataBuilder *pBuilder);
int32_t tDiskDataAddRow(SDiskDataBuilder *pBuilder, TSDBROW *pRow, STSchema *pTSchema, TABLEID *pId);
H
Hongze Cheng 已提交
305
int32_t tGnrtDiskData(SDiskDataBuilder *pBuilder, const SDiskData **ppDiskData, const SBlkInfo **ppBlkInfo);
H
Hongze Cheng 已提交
306

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

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

H
refact  
Hongze Cheng 已提交
325 326 327 328 329
struct TSDBKEY {
  int64_t version;
  TSKEY   ts;
};

330 331 332 333 334
struct SVersionRange {
  uint64_t minVer;
  uint64_t maxVer;
};

H
Hongze Cheng 已提交
335 336 337
typedef struct SMemSkipListNode SMemSkipListNode;
struct SMemSkipListNode {
  int8_t            level;
H
Hongze Cheng 已提交
338 339
  int64_t           version;
  STSRow           *pTSRow;
H
Hongze Cheng 已提交
340 341 342 343
  SMemSkipListNode *forwards[0];
};
typedef struct SMemSkipList {
  int64_t           size;
H
Hongze Cheng 已提交
344
  uint32_t          seed;
H
Hongze Cheng 已提交
345 346 347 348 349 350
  int8_t            maxLevel;
  int8_t            level;
  SMemSkipListNode *pHead;
  SMemSkipListNode *pTail;
} SMemSkipList;

H
Hongze Cheng 已提交
351
struct STbData {
H
Hongze Cheng 已提交
352 353
  tb_uid_t     suid;
  tb_uid_t     uid;
H
Hongze Cheng 已提交
354 355
  TSKEY        minKey;
  TSKEY        maxKey;
H
Hongze Cheng 已提交
356 357
  SDelData    *pHead;
  SDelData    *pTail;
H
Hongze Cheng 已提交
358
  SMemSkipList sl;
H
Hongze Cheng 已提交
359
  STbData     *next;
H
Hongze Cheng 已提交
360
};
H
Hongze Cheng 已提交
361

H
refact  
Hongze Cheng 已提交
362
struct SMemTable {
H
Hongze Cheng 已提交
363 364 365 366 367 368 369 370
  SRWLatch         latch;
  STsdb           *pTsdb;
  SVBufPool       *pPool;
  volatile int32_t nRef;
  TSKEY            minKey;
  TSKEY            maxKey;
  int64_t          nRow;
  int64_t          nDel;
H
Hongze Cheng 已提交
371 372 373 374 375
  struct {
    int32_t   nTbData;
    int32_t   nBucket;
    STbData **aBucket;
  };
H
Hongze Cheng 已提交
376
};
H
Hongze Cheng 已提交
377

H
Hongze Cheng 已提交
378
struct TSDBROW {
H
Hongze Cheng 已提交
379
  int8_t type;  // 0 for row from tsRow, 1 for row from block data
H
Hongze Cheng 已提交
380 381 382 383 384 385
  union {
    struct {
      int64_t version;
      STSRow *pTSRow;
    };
    struct {
H
Hongze Cheng 已提交
386 387
      SBlockData *pBlockData;
      int32_t     iRow;
H
Hongze Cheng 已提交
388 389
    };
  };
H
Hongze Cheng 已提交
390 391
};

H
Hongze Cheng 已提交
392
struct SBlockIdx {
H
Hongze Cheng 已提交
393 394
  int64_t suid;
  int64_t uid;
H
Hongze Cheng 已提交
395 396 397 398
  int64_t offset;
  int64_t size;
};

H
Hongze Cheng 已提交
399 400
struct SMapData {
  int32_t  nItem;
H
Hongze Cheng 已提交
401
  int32_t  nData;
402
  int32_t *aOffset;
H
Hongze Cheng 已提交
403 404 405
  uint8_t *pData;
};

H
Hongze Cheng 已提交
406
struct SBlockCol {
H
Hongze Cheng 已提交
407 408 409 410 411 412 413 414 415
  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 已提交
416
};
H
Hongze Cheng 已提交
417

H
Hongze Cheng 已提交
418
struct SBlockInfo {
H
Hongze Cheng 已提交
419 420 421
  int64_t offset;  // block data offset
  int32_t szBlock;
  int32_t szKey;
H
Hongze Cheng 已提交
422
};
H
Hongze Cheng 已提交
423

H
Hongze Cheng 已提交
424
struct SSmaInfo {
H
Hongze Cheng 已提交
425 426
  int64_t offset;
  int32_t size;
H
Hongze Cheng 已提交
427
};
H
Hongze Cheng 已提交
428

H
Hongze Cheng 已提交
429
struct SBlkInfo {
H
Hongze Cheng 已提交
430 431
  int64_t minUid;
  int64_t maxUid;
H
Hongze Cheng 已提交
432 433
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
434 435
  int64_t minVer;
  int64_t maxVer;
H
Hongze Cheng 已提交
436 437 438
  TSDBKEY minTKey;
  TSDBKEY maxTKey;
};
H
Hongze Cheng 已提交
439

H
Hongze Cheng 已提交
440
struct SDataBlk {
H
Hongze Cheng 已提交
441 442 443 444 445 446 447 448 449
  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 已提交
450
};
H
Hongze Cheng 已提交
451

H
Hongze Cheng 已提交
452
struct SSttBlk {
H
Hongze Cheng 已提交
453 454 455
  int64_t    suid;
  int64_t    minUid;
  int64_t    maxUid;
H
Hongze Cheng 已提交
456 457
  TSKEY      minKey;
  TSKEY      maxKey;
H
Hongze Cheng 已提交
458 459 460 461
  int64_t    minVer;
  int64_t    maxVer;
  int32_t    nRow;
  SBlockInfo bInfo;
H
Hongze Cheng 已提交
462 463
};

H
Hongze Cheng 已提交
464 465 466 467
// (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 已提交
468
struct SBlockData {
H
Hongze Cheng 已提交
469 470 471 472 473 474
  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 已提交
475
  int32_t  nColData;
H
Hongze Cheng 已提交
476
  SArray  *aColData;  // SArray<SColData>
H
Hongze Cheng 已提交
477
};
H
Hongze Cheng 已提交
478

H
Hongze Cheng 已提交
479 480 481 482 483
struct TABLEID {
  tb_uid_t suid;
  tb_uid_t uid;
};

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

H
Hongze Cheng 已提交
492
struct SDelData {
H
Hongze Cheng 已提交
493 494 495 496
  int64_t   version;
  TSKEY     sKey;
  TSKEY     eKey;
  SDelData *pNext;
H
Hongze Cheng 已提交
497
};
H
Hongze Cheng 已提交
498

H
Hongze Cheng 已提交
499
struct SDelIdx {
H
Hongze Cheng 已提交
500 501 502 503
  tb_uid_t suid;
  tb_uid_t uid;
  int64_t  offset;
  int64_t  size;
H
Hongze Cheng 已提交
504
};
H
Hongze Cheng 已提交
505

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

H
Hongze Cheng 已提交
519 520 521 522 523 524 525 526
struct SDelFile {
  volatile int32_t nRef;

  int64_t commitID;
  int64_t size;
  int64_t offset;
};

H
Hongze Cheng 已提交
527
struct SHeadFile {
H
Hongze Cheng 已提交
528 529
  volatile int32_t nRef;

H
Hongze Cheng 已提交
530
  int64_t commitID;
H
Hongze Cheng 已提交
531 532 533 534 535
  int64_t size;
  int64_t offset;
};

struct SDataFile {
H
Hongze Cheng 已提交
536 537
  volatile int32_t nRef;

H
Hongze Cheng 已提交
538
  int64_t commitID;
H
Hongze Cheng 已提交
539 540 541
  int64_t size;
};

H
Hongze Cheng 已提交
542
struct SSttFile {
H
Hongze Cheng 已提交
543 544
  volatile int32_t nRef;

H
Hongze Cheng 已提交
545
  int64_t commitID;
H
Hongze Cheng 已提交
546
  int64_t size;
H
Hongze Cheng 已提交
547
  int64_t offset;
H
Hongze Cheng 已提交
548 549 550
};

struct SSmaFile {
H
Hongze Cheng 已提交
551 552
  volatile int32_t nRef;

H
Hongze Cheng 已提交
553
  int64_t commitID;
H
Hongze Cheng 已提交
554 555 556 557
  int64_t size;
};

struct SDFileSet {
H
Hongze Cheng 已提交
558 559 560 561 562
  SDiskID    diskId;
  int32_t    fid;
  SHeadFile *pHeadF;
  SDataFile *pDataF;
  SSmaFile  *pSmaF;
H
Hongze Cheng 已提交
563
  uint8_t    nSttF;
H
Hongze Cheng 已提交
564
  SSttFile  *aSttF[TSDB_MAX_STT_TRIGGER];
H
Hongze Cheng 已提交
565 566
};

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

H
more  
Hongze Cheng 已提交
579
typedef struct {
H
Hongze Cheng 已提交
580
  char     *path;
H
more  
Hongze Cheng 已提交
581
  int32_t   szPage;
H
Hongze Cheng 已提交
582 583 584
  int32_t   flag;
  TdFilePtr pFD;
  int64_t   pgno;
H
more  
Hongze Cheng 已提交
585
  uint8_t  *pBuf;
H
Hongze Cheng 已提交
586
  int64_t   szFile;
H
more  
Hongze Cheng 已提交
587
} STsdbFD;
H
Hongze Cheng 已提交
588

H
Hongze Cheng 已提交
589 590 591 592
struct SDelFWriter {
  STsdb   *pTsdb;
  SDelFile fDel;
  STsdbFD *pWriteH;
H
Hongze Cheng 已提交
593
  uint8_t *aBuf[1];
H
Hongze Cheng 已提交
594 595
};

H
Hongze Cheng 已提交
596 597 598 599 600 601
struct STsdbReadSnap {
  SMemTable *pMem;
  SMemTable *pIMem;
  STsdbFS    fs;
};

H
Hongze Cheng 已提交
602 603 604 605
struct SDataFWriter {
  STsdb    *pTsdb;
  SDFileSet wSet;

H
Hongze Cheng 已提交
606 607 608
  STsdbFD *pHeadFD;
  STsdbFD *pDataFD;
  STsdbFD *pSmaFD;
H
Hongze Cheng 已提交
609
  STsdbFD *pSttFD;
H
Hongze Cheng 已提交
610 611 612 613

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

H
Hongze Cheng 已提交
616
  uint8_t *aBuf[4];
H
Hongze Cheng 已提交
617 618
};

H
Hongze Cheng 已提交
619 620 621
struct SDataFReader {
  STsdb     *pTsdb;
  SDFileSet *pSet;
H
Hongze Cheng 已提交
622 623 624
  STsdbFD   *pHeadFD;
  STsdbFD   *pDataFD;
  STsdbFD   *pSmaFD;
H
Hongze Cheng 已提交
625
  STsdbFD   *aSttFD[TSDB_MAX_STT_TRIGGER];
H
Hongze Cheng 已提交
626
  uint8_t   *aBuf[3];
H
Hongze Cheng 已提交
627 628
};

H
Hongze Cheng 已提交
629 630 631 632 633 634
typedef struct {
  int64_t suid;
  int64_t uid;
  TSDBROW row;
} SRowInfo;

635
typedef struct SSttBlockLoadInfo {
H
Hongze Cheng 已提交
636 637 638 639 640 641
  SBlockData blockData[2];
  SArray    *aSttBlk;
  int32_t    blockIndex[2];  // to denote the loaded block in the corresponding position.
  int32_t    currentLoadBlockIndex;
  int32_t    loadBlocks;
  double     elapsedTime;
642 643 644
  STSchema  *pSchema;
  int16_t   *colIds;
  int32_t    numOfCols;
645
  bool       sttBlockLoaded;
H
Haojun Liao 已提交
646 647 648 649 650 651 652

  // keep the last access position, this position may be used to reduce the binary times for
  // starting last block data for a new table
  struct {
    int32_t blockIndex;
    int32_t rowIndex;
  } prevEndPos;
653 654
} SSttBlockLoadInfo;

655
typedef struct SMergeTree {
H
Hongze Cheng 已提交
656 657 658 659 660 661 662
  int8_t             backward;
  SRBTree            rbt;
  SArray            *pIterList;
  SLDataIter        *pIter;
  bool               destroyLoadInfo;
  SSttBlockLoadInfo *pLoadInfo;
  const char        *idStr;
663 664
} SMergeTree;

H
Hongze Cheng 已提交
665 666 667 668 669 670
typedef struct {
  int64_t   suid;
  int64_t   uid;
  STSchema *pTSchema;
} SSkmInfo;

H
Hongze Cheng 已提交
671 672 673 674 675
struct SDiskCol {
  SBlockCol      bCol;
  const uint8_t *pBit;
  const uint8_t *pOff;
  const uint8_t *pVal;
H
Hongze Cheng 已提交
676
  SColumnDataAgg agg;
H
Hongze Cheng 已提交
677 678 679 680 681 682 683 684 685 686
};

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

H
Hongze Cheng 已提交
687 688 689 690 691 692 693 694 695 696 697 698
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 已提交
699
  SDiskData    dd;
H
Hongze Cheng 已提交
700
  SBlkInfo     bi;
H
Hongze Cheng 已提交
701 702
};

H
Haojun Liao 已提交
703
int32_t tMergeTreeOpen(SMergeTree *pMTree, int8_t backward, SDataFReader *pFReader, uint64_t suid, uint64_t uid,
704 705
                       STimeWindow *pTimeWindow, SVersionRange *pVerRange, SSttBlockLoadInfo *pBlockLoadInfo,
                       bool destroyLoadInfo, const char *idStr);
H
Hongze Cheng 已提交
706
void    tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter);
H
Hongze Cheng 已提交
707
bool    tMergeTreeNext(SMergeTree *pMTree);
H
refact  
Hongze Cheng 已提交
708 709
TSDBROW tMergeTreeGetRow(SMergeTree *pMTree);
void    tMergeTreeClose(SMergeTree *pMTree);
710

H
Hongze Cheng 已提交
711
SSttBlockLoadInfo *tCreateLastBlockLoadInfo(STSchema *pSchema, int16_t *colList, int32_t numOfCols);
H
Hongze Cheng 已提交
712 713 714
void               resetLastBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo);
void               getLastBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo, int64_t *blocks, double *el);
void              *destroyLastBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo);
715

716 717
// tsdbCache ==============================================================================================
typedef struct SCacheRowsReader {
H
Haojun Liao 已提交
718 719 720 721 722 723 724 725
  SVnode            *pVnode;
  STSchema          *pSchema;
  uint64_t           uid;
  uint64_t           suid;
  char             **transferBuf;  // todo remove it soon
  int32_t            numOfCols;
  int32_t            type;
  int32_t            tableIndex;  // currently returned result tables
726 727
  STableKeyInfo     *pTableList;  // table id list
  int32_t            numOfTables;
728 729 730
  SSttBlockLoadInfo *pLoadInfo;
  STsdbReadSnap     *pReadSnap;
  SDataFReader      *pDataFReader;
731
  SDataFReader      *pDataFReaderLast;
H
Haojun Liao 已提交
732
  const char        *idstr;
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
} SCacheRowsReader;

typedef struct {
  TSKEY   ts;
  SColVal colVal;
} SLastCol;

int32_t tsdbOpenCache(STsdb *pTsdb);
void    tsdbCloseCache(STsdb *pTsdb);
int32_t tsdbCacheInsertLast(SLRUCache *pCache, tb_uid_t uid, STSRow *row, STsdb *pTsdb);
int32_t tsdbCacheInsertLastrow(SLRUCache *pCache, STsdb *pTsdb, tb_uid_t uid, STSRow *row, bool dup);
int32_t tsdbCacheGetLastH(SLRUCache *pCache, tb_uid_t uid, SCacheRowsReader *pr, LRUHandle **h);
int32_t tsdbCacheGetLastrowH(SLRUCache *pCache, tb_uid_t uid, SCacheRowsReader *pr, LRUHandle **h);
int32_t tsdbCacheRelease(SLRUCache *pCache, LRUHandle *h);

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

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

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
// ========== 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 已提交
775 776
// #define SL_NODE_FORWARD(n, l)  ((n)->forwards[l])
// #define SL_NODE_BACKWARD(n, l) ((n)->forwards[(n)->level + (l)])
H
Hongze Cheng 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795

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 已提交
796 797
  pIter->pRow->version = pIter->pNode->version;
  pIter->pRow->pTSRow = pIter->pNode->pTSRow;
H
Hongze Cheng 已提交
798 799 800 801

  return pIter->pRow;
}

H
Hongze Cheng 已提交
802 803 804 805
#ifdef __cplusplus
}
#endif

806
#endif /*_TD_VNODE_TSDB_H_*/