tsdb.h 7.7 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
more  
Hongze Cheng 已提交
58
  uint64_t lruCacheSize;
H
Hongze Cheng 已提交
59
  int32_t  daysPerFile;
H
Hongze Cheng 已提交
60 61 62 63 64
  int32_t  minRowsPerFileBlock;
  int32_t  maxRowsPerFileBlock;
  int32_t  keep;
  int32_t  keep1;
  int32_t  keep2;
H
more  
Hongze Cheng 已提交
65
} STsdbCfg;
H
more  
Hongze Cheng 已提交
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
// 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 已提交
82
// STsdb
S
Shengliang Guan 已提交
83
STsdb *tsdbOpen(const char *path, int32_t vgId, const STsdbCfg *pTsdbCfg, SMemAllocatorFactory *pMAF, SMeta *pMeta, STfs *pTfs);
H
more  
Hongze Cheng 已提交
84
void   tsdbClose(STsdb *);
H
refact  
Hongze Cheng 已提交
85
void   tsdbRemove(const char *path);
S
Shengliang Guan 已提交
86
int    tsdbInsertData(STsdb *pTsdb, SSubmitReq *pMsg, SSubmitRsp *pRsp);
H
more  
Hongze Cheng 已提交
87 88
int    tsdbPrepareCommit(STsdb *pTsdb);
int    tsdbCommit(STsdb *pTsdb);
H
more  
Hongze Cheng 已提交
89

C
Cary Xu 已提交
90 91 92 93 94 95 96 97 98
/**
 * @brief When submit msg received, update the relative expired window synchronously.
 * 
 * @param pTsdb 
 * @param msg 
 * @return int32_t 
 */
int32_t tsdbUpdateSmaWindow(STsdb *pTsdb, const char *msg);

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

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

C
Cary Xu 已提交
118
/**
C
Cary Xu 已提交
119
 * @brief Insert RSma(Rollup SMA) data.
C
Cary Xu 已提交
120 121 122 123
 *
 * @param pTsdb
 * @param msg
 * @return int32_t
C
Cary Xu 已提交
124
 */
C
Cary Xu 已提交
125
int32_t tsdbInsertRSmaData(STsdb *pTsdb, char *msg);
C
Cary Xu 已提交
126

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

H
refact  
Hongze Cheng 已提交
140 141 142
// STsdbCfg
int  tsdbOptionsInit(STsdbCfg *);
void tsdbOptionsClear(STsdbCfg *);
H
refact  
Hongze Cheng 已提交
143

H
Haojun Liao 已提交
144
typedef void* tsdbReaderT;
145

146 147 148 149 150 151 152 153 154 155
/**
 * 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 已提交
156
tsdbReaderT *tsdbQueryTables(STsdb *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableInfoGroup, uint64_t qId, uint64_t taskId);
157 158 159 160 161 162 163 164 165 166 167

/**
 * 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 已提交
168
//tsdbReaderT tsdbQueryLastRow(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableInfo, uint64_t qId,
169 170 171
//                                  SMemRef *pRef);


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

H
Haojun Liao 已提交
174
bool isTsdbCacheLastRow(tsdbReaderT* pTsdbReadHandle);
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/**
 *
 * @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 已提交
191
int32_t tsdbQuerySTableByTagCond(void* pMeta, uint64_t uid, TSKEY skey, const char* pTagCond, size_t len,
192
                                 int16_t tagNameRelType, const char* tbnameCond, STableGroupInfo* pGroupInfo,
193
                                 SColIndex* pColIndex, int32_t numOfCols, uint64_t reqId, uint64_t taskId);
194 195 196 197 198 199 200
/**
 * get num of rows in mem table
 *
 * @param pHandle
 * @return row size
 */

H
Haojun Liao 已提交
201
int64_t tsdbGetNumOfRowsInMemTable(tsdbReaderT* pHandle);
202 203 204 205 206 207 208

/**
 * move to next block if exists
 *
 * @param pTsdbReadHandle
 * @return
 */
H
Haojun Liao 已提交
209
bool tsdbNextDataBlock(tsdbReaderT pTsdbReadHandle);
210 211 212 213 214 215 216 217

/**
 * Get current data block information
 *
 * @param pTsdbReadHandle
 * @param pBlockInfo
 * @return
 */
H
Haojun Liao 已提交
218
void tsdbRetrieveDataBlockInfo(tsdbReaderT *pTsdbReadHandle, SDataBlockInfo *pBlockInfo);
219 220 221 222 223 224 225 226 227 228 229

/**
 *
 * 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 已提交
230
int32_t tsdbRetrieveDataBlockStatisInfo(tsdbReaderT *pTsdbReadHandle, SDataStatis **pBlockStatis);
231 232 233 234 235 236 237 238 239 240 241

/**
 *
 * 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 已提交
242
SArray *tsdbRetrieveDataBlock(tsdbReaderT *pTsdbReadHandle, SArray *pColumnIdList);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

/**
 * 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 已提交
258
int32_t tsdbGetOneTableGroup(void *pMeta, uint64_t uid, TSKEY startKey, STableGroupInfo *pGroupInfo);
259 260 261 262 263 264 265 266 267 268 269 270 271 272

/**
 *
 * @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 已提交
273
void tsdbCleanupReadHandle(tsdbReaderT queryHandle);
274

C
Cary Xu 已提交
275 276
int32_t tdScanAndConvertSubmitMsg(SSubmitReq *pMsg);

H
refact  
Hongze Cheng 已提交
277 278 279 280
#ifdef __cplusplus
}
#endif

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