tsdb.h 7.9 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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
refact  
Hongze Cheng 已提交
16 17
#ifndef _TD_TSDB_H_
#define _TD_TSDB_H_
H
refact  
Hongze Cheng 已提交
18

S
Shengliang Guan 已提交
19
#include "tmallocator.h"
H
more  
Hongze Cheng 已提交
20
#include "meta.h"
S
common  
Shengliang Guan 已提交
21
#include "tcommon.h"
S
Shengliang Guan 已提交
22
#include "tfs.h"
H
more  
Hongze Cheng 已提交
23

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

H
Hongze Cheng 已提交
28 29 30 31 32
typedef struct SDataStatis {
  int16_t colId;
  int16_t maxIndex;
  int16_t minIndex;
  int16_t numOfNull;
33 34 35
  int64_t sum;
  int64_t max;
  int64_t min;
H
Hongze Cheng 已提交
36 37 38
} SDataStatis;

typedef struct STable {
H
more  
Hongze Cheng 已提交
39
  uint64_t  tid;
H
Hongze Cheng 已提交
40 41 42 43
  uint64_t  uid;
  STSchema *pSchema;
} STable;

44 45 46 47
#define BLOCK_LOAD_OFFSET_SEQ_ORDER   1
#define BLOCK_LOAD_TABLE_SEQ_ORDER    2
#define BLOCK_LOAD_TABLE_RR_ORDER     3

H
Hongze Cheng 已提交
48 49 50
#define TABLE_TID(t) (t)->tid
#define TABLE_UID(t) (t)->uid

H
refact  
Hongze Cheng 已提交
51
// TYPES EXPOSED
H
more  
Hongze Cheng 已提交
52 53 54
typedef struct STsdb STsdb;

typedef struct STsdbCfg {
H
Hongze Cheng 已提交
55
  int8_t   precision;
56 57
  int8_t   update;
  int8_t   compression;
H
Hongze Cheng 已提交
58
  int32_t  daysPerFile;
H
Hongze Cheng 已提交
59 60 61 62 63
  int32_t  minRowsPerFileBlock;
  int32_t  maxRowsPerFileBlock;
  int32_t  keep;
  int32_t  keep1;
  int32_t  keep2;
64 65
  uint64_t lruCacheSize;
  SArray  *retentions;
H
more  
Hongze Cheng 已提交
66
} STsdbCfg;
H
more  
Hongze Cheng 已提交
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
// query condition to build multi-table data block iterator
typedef struct STsdbQueryCond {
  STimeWindow  twindow;
  int32_t      order;             // desc|asc order to iterate the data block
  int32_t      numOfCols;
  SColumnInfo *colList;
  bool         loadExternalRows;  // load external rows or not
  int32_t      type;              // data block load type:
} STsdbQueryCond;

typedef struct {
  TSKEY    lastKey;
  uint64_t uid;
} STableKeyInfo;

H
refact  
Hongze Cheng 已提交
83
// STsdb
S
Shengliang Guan 已提交
84
STsdb *tsdbOpen(const char *path, int32_t vgId, const STsdbCfg *pTsdbCfg, SMemAllocatorFactory *pMAF, SMeta *pMeta, STfs *pTfs);
H
more  
Hongze Cheng 已提交
85
void   tsdbClose(STsdb *);
H
refact  
Hongze Cheng 已提交
86
void   tsdbRemove(const char *path);
S
Shengliang Guan 已提交
87
int    tsdbInsertData(STsdb *pTsdb, SSubmitReq *pMsg, SSubmitRsp *pRsp);
H
more  
Hongze Cheng 已提交
88 89
int    tsdbPrepareCommit(STsdb *pTsdb);
int    tsdbCommit(STsdb *pTsdb);
H
more  
Hongze Cheng 已提交
90

C
Cary Xu 已提交
91 92 93 94

int32_t tsdbInitSma(STsdb *pTsdb);
int32_t tsdbCreateTSma(STsdb *pTsdb, char *pMsg);
int32_t tsdbDropTSma(STsdb *pTsdb, char *pMsg);
C
Cary Xu 已提交
95 96
/**
 * @brief When submit msg received, update the relative expired window synchronously.
C
Cary Xu 已提交
97 98 99 100
 *
 * @param pTsdb
 * @param msg
 * @return int32_t
C
Cary Xu 已提交
101
 */
C
Cary Xu 已提交
102
int32_t tsdbUpdateSmaWindow(STsdb *pTsdb, SSubmitReq *pMsg);
C
Cary Xu 已提交
103

C
Cary Xu 已提交
104 105
/**
 * @brief Insert tSma(Time-range-wise SMA) data from stream computing engine
C
Cary Xu 已提交
106 107
 *
 * @param pTsdb
C
Cary Xu 已提交
108
 * @param indexUid
C
Cary Xu 已提交
109 110
 * @param msg
 * @return int32_t
C
Cary Xu 已提交
111
 */
C
Cary Xu 已提交
112
int32_t tsdbInsertTSmaData(STsdb *pTsdb, int64_t indexUid, const char *msg);
C
Cary Xu 已提交
113 114 115 116 117 118 119 120

/**
 * @brief Drop tSma data and local cache.
 * 
 * @param pTsdb 
 * @param indexUid 
 * @return int32_t 
 */
C
Cary Xu 已提交
121
int32_t tsdbDropTSmaData(STsdb *pTsdb, int64_t indexUid);
C
Cary Xu 已提交
122

C
Cary Xu 已提交
123
/**
C
Cary Xu 已提交
124
 * @brief Insert RSma(Rollup SMA) data.
C
Cary Xu 已提交
125 126 127 128
 *
 * @param pTsdb
 * @param msg
 * @return int32_t
C
Cary Xu 已提交
129
 */
C
Cary Xu 已提交
130
int32_t tsdbInsertRSmaData(STsdb *pTsdb, char *msg);
C
Cary Xu 已提交
131

C
Cary Xu 已提交
132
// TODO: This is the basic params, and should wrap the params to a queryHandle.
C
Cary Xu 已提交
133 134 135 136 137 138 139 140 141 142
/**
 * @brief Get tSma(Time-range-wise SMA) data.
 * 
 * @param pTsdb 
 * @param pData 
 * @param indexUid 
 * @param querySKey 
 * @param nMaxResult 
 * @return int32_t 
 */
C
Cary Xu 已提交
143
int32_t tsdbGetTSmaData(STsdb *pTsdb, char *pData, int64_t indexUid, TSKEY querySKey, int32_t nMaxResult);
C
Cary Xu 已提交
144

H
refact  
Hongze Cheng 已提交
145 146 147
// STsdbCfg
int  tsdbOptionsInit(STsdbCfg *);
void tsdbOptionsClear(STsdbCfg *);
H
refact  
Hongze Cheng 已提交
148

H
Haojun Liao 已提交
149
typedef void* tsdbReaderT;
150

151 152 153 154 155 156 157 158 159 160
/**
 * Get the data block iterator, starting from position according to the query condition
 *
 * @param tsdb       tsdb handle
 * @param pCond      query condition, including time window, result set order, and basic required columns for each block
 * @param tableInfoGroup  table object list in the form of set, grouped into different sets according to the
 *                        group by condition
 * @param qinfo      query info handle from query processor
 * @return
 */
H
Haojun Liao 已提交
161
tsdbReaderT *tsdbQueryTables(STsdb *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableInfoGroup, uint64_t qId, uint64_t taskId);
162 163 164 165 166 167 168 169 170 171 172

/**
 * Get the last row of the given query time window for all the tables in STableGroupInfo object.
 * Note that only one data block with only row will be returned while invoking retrieve data block function for
 * all tables in this group.
 *
 * @param tsdb   tsdb handle
 * @param pCond  query condition, including time window, result set order, and basic required columns for each block
 * @param tableInfo  table list.
 * @return
 */
H
Haojun Liao 已提交
173
//tsdbReaderT tsdbQueryLastRow(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableInfo, uint64_t qId,
174 175 176
//                                  SMemRef *pRef);


H
Haojun Liao 已提交
177
tsdbReaderT tsdbQueryCacheLast(STsdb *tsdb, STsdbQueryCond *pCond, STableGroupInfo *groupList, uint64_t qId, void* pMemRef);
178

179
int32_t tsdbGetFileBlocksDistInfo(tsdbReaderT* pReader, STableBlockDistInfo* pTableBlockInfo);
H
Haojun Liao 已提交
180

181
bool isTsdbCacheLastRow(tsdbReaderT* pReader);
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
/**
 *
 * @param tsdb
 * @param uid
 * @param skey
 * @param pTagCond
 * @param len
 * @param tagNameRelType
 * @param tbnameCond
 * @param pGroupInfo
 * @param pColIndex
 * @param numOfCols
 * @param reqId
 * @return
 */
H
Haojun Liao 已提交
198
int32_t tsdbQuerySTableByTagCond(void* pMeta, uint64_t uid, TSKEY skey, const char* pTagCond, size_t len,
199
                                 int16_t tagNameRelType, const char* tbnameCond, STableGroupInfo* pGroupInfo,
200
                                 SColIndex* pColIndex, int32_t numOfCols, uint64_t reqId, uint64_t taskId);
201 202 203 204 205 206 207
/**
 * get num of rows in mem table
 *
 * @param pHandle
 * @return row size
 */

H
Haojun Liao 已提交
208
int64_t tsdbGetNumOfRowsInMemTable(tsdbReaderT* pHandle);
209 210 211 212 213 214 215

/**
 * move to next block if exists
 *
 * @param pTsdbReadHandle
 * @return
 */
H
Haojun Liao 已提交
216
bool tsdbNextDataBlock(tsdbReaderT pTsdbReadHandle);
217 218 219 220 221 222 223 224

/**
 * Get current data block information
 *
 * @param pTsdbReadHandle
 * @param pBlockInfo
 * @return
 */
H
Haojun Liao 已提交
225
void tsdbRetrieveDataBlockInfo(tsdbReaderT *pTsdbReadHandle, SDataBlockInfo *pBlockInfo);
226 227 228 229 230 231 232 233 234 235 236

/**
 *
 * Get the pre-calculated information w.r.t. current data block.
 *
 * In case of data block in cache, the pBlockStatis will always be NULL.
 * If a block is not completed loaded from disk, the pBlockStatis will be NULL.

 * @pBlockStatis the pre-calculated value for current data blocks. if the block is a cache block, always return 0
 * @return
 */
H
Haojun Liao 已提交
237
int32_t tsdbRetrieveDataBlockStatisInfo(tsdbReaderT *pTsdbReadHandle, SDataStatis **pBlockStatis);
238 239 240 241 242 243 244 245 246 247 248

/**
 *
 * The query condition with primary timestamp is passed to iterator during its constructor function,
 * the returned data block must be satisfied with the time window condition in any cases,
 * which means the SData data block is not actually the completed disk data blocks.
 *
 * @param pTsdbReadHandle      query handle
 * @param pColumnIdList     required data columns id list
 * @return
 */
H
Haojun Liao 已提交
249
SArray *tsdbRetrieveDataBlock(tsdbReaderT *pTsdbReadHandle, SArray *pColumnIdList);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

/**
 * destroy the created table group list, which is generated by tag query
 * @param pGroupList
 */
void tsdbDestroyTableGroup(STableGroupInfo *pGroupList);

/**
 * create the table group result including only one table, used to handle the normal table query
 *
 * @param tsdb        tsdbHandle
 * @param uid         table uid
 * @param pGroupInfo  the generated result
 * @return
 */
H
Haojun Liao 已提交
265
int32_t tsdbGetOneTableGroup(void *pMeta, uint64_t uid, TSKEY startKey, STableGroupInfo *pGroupInfo);
266 267 268 269 270 271 272 273 274 275 276 277 278 279

/**
 *
 * @param tsdb
 * @param pTableIdList
 * @param pGroupInfo
 * @return
 */
int32_t tsdbGetTableGroupFromIdList(STsdb *tsdb, SArray *pTableIdList, STableGroupInfo *pGroupInfo);

/**
 * clean up the query handle
 * @param queryHandle
 */
H
Haojun Liao 已提交
280
void tsdbCleanupReadHandle(tsdbReaderT queryHandle);
281

C
Cary Xu 已提交
282 283
int32_t tdScanAndConvertSubmitMsg(SSubmitReq *pMsg);

H
refact  
Hongze Cheng 已提交
284 285 286 287
#ifdef __cplusplus
}
#endif

D
dapan1121 已提交
288
#endif /*_TD_TSDB_H_*/