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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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