tsdb.h 9.8 KB
Newer Older
H
more  
hzcheng 已提交
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
hzcheng 已提交
15
#ifndef _TD_TSDB_H_
H
Hongze Cheng 已提交
16 17 18 19 20 21
#define _TD_TSDB_H_

#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>

H
hzcheng 已提交
22
#include "taosdef.h"
H
hzcheng 已提交
23
#include "taosmsg.h"
H
hjxilinx 已提交
24
#include "tarray.h"
H
Hongze Cheng 已提交
25 26
#include "tdataformat.h"
#include "tname.h"
H
Haojun Liao 已提交
27
#include "hash.h"
H
Hongze Cheng 已提交
28

H
more  
hzcheng 已提交
29 30 31 32
#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
33 34 35
#define TSDB_VERSION_MAJOR 1
#define TSDB_VERSION_MINOR 0

H
hzcheng 已提交
36 37
#define TSDB_INVALID_SUPER_TABLE_ID -1

38
#define TSDB_STATUS_COMMIT_START 1
H
Hongze Cheng 已提交
39
#define TSDB_STATUS_COMMIT_OVER 2
40

H
hzcheng 已提交
41 42 43
// --------- TSDB APPLICATION HANDLE DEFINITION
typedef struct {
  void *appH;
J
jtao1735 已提交
44
  void *cqH;
45
  int (*notifyStatus)(void *, int status);
H
hzcheng 已提交
46
  int (*eventCallBack)(void *);
B
Bomin Zhang 已提交
47
  void *(*cqCreateFunc)(void *handle, uint64_t uid, int sid, char *sqlStr, STSchema *pSchema);
H
TD-354  
Hongze Cheng 已提交
48
  void (*cqDropFunc)(void *handle);
H
TD-90  
Hongze Cheng 已提交
49
  void *(*configFunc)(int32_t vgId, int32_t sid);
H
hzcheng 已提交
50 51
} STsdbAppH;

H
hzcheng 已提交
52 53 54
// --------- TSDB REPOSITORY CONFIGURATION DEFINITION
typedef struct {
  int32_t tsdbId;
S
slguan 已提交
55 56
  int32_t cacheBlockSize;
  int32_t totalBlocks;
H
Hongze Cheng 已提交
57 58 59
  int32_t maxTables;    // maximum number of tables this repository can have
  int32_t daysPerFile;  // day per file sharding policy
  int32_t keep;         // day of data to keep
S
slguan 已提交
60 61
  int32_t keep1;
  int32_t keep2;
H
hzcheng 已提交
62 63
  int32_t minRowsPerFileBlock;  // minimum rows per file block
  int32_t maxRowsPerFileBlock;  // maximum rows per file block
S
slguan 已提交
64 65 66
  int32_t commitTime;
  int8_t  precision;
  int8_t  compression;
H
hzcheng 已提交
67 68
} STsdbCfg;

T
Tao Liu 已提交
69 70
// --------- TSDB REPOSITORY USAGE STATISTICS
typedef struct {
71 72 73 74
  int64_t totalStorage;  // total bytes occupie
  int64_t compStorage;
  int64_t pointsWritten;  // total data points written
} STsdbStat;
T
Tao Liu 已提交
75

H
TD-353  
Hongze Cheng 已提交
76
typedef void TSDB_REPO_T;  // use void to hide implementation details from outside
77

H
TD-353  
Hongze Cheng 已提交
78
STsdbCfg *tsdbGetCfg(const TSDB_REPO_T *repo);
H
hzcheng 已提交
79 80

// --------- TSDB REPOSITORY DEFINITION
H
TD-353  
Hongze Cheng 已提交
81
int          tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg);
H
TD-353  
Hongze Cheng 已提交
82
int32_t      tsdbDropRepo(char *rootDir);
H
TD-353  
Hongze Cheng 已提交
83
TSDB_REPO_T *tsdbOpenRepo(char *rootDir, STsdbAppH *pAppH);
H
TD-353  
Hongze Cheng 已提交
84
void         tsdbCloseRepo(TSDB_REPO_T *repo, int toCommit);
H
Hongze Cheng 已提交
85
int32_t      tsdbConfigRepo(TSDB_REPO_T *repo, STsdbCfg *pCfg);
H
hzcheng 已提交
86 87

// --------- TSDB TABLE DEFINITION
H
more  
hzcheng 已提交
88
typedef struct {
89 90
  uint64_t uid;  // the unique table ID
  int32_t  tid;  // the table ID in the repository.
H
more  
hzcheng 已提交
91 92
} STableId;

H
hzcheng 已提交
93 94
// --------- TSDB TABLE configuration
typedef struct {
H
hzcheng 已提交
95 96 97 98 99
  ETableType type;
  char *     name;
  STableId   tableId;
  int32_t    sversion;
  char *     sname;  // super table name
100
  uint64_t   superUid;
H
hzcheng 已提交
101 102 103
  STSchema * schema;
  STSchema * tagSchema;
  SDataRow   tagValues;
H
TD-354  
Hongze Cheng 已提交
104
  char *     sql;
H
hzcheng 已提交
105 106 107 108
} STableCfg;

void tsdbClearTableCfg(STableCfg *config);

109 110 111
void* tsdbGetTableTagVal(const void* pTable, int32_t colId, int16_t type, int16_t bytes);
char* tsdbGetTableName(void *pTable);
STableId tsdbGetTableId(void *pTable);
H
Hongze Cheng 已提交
112
STableCfg *tsdbCreateTableCfgFromMsg(SMDCreateTableMsg *pMsg);
113

H
TD-353  
Hongze Cheng 已提交
114 115 116 117 118
int   tsdbCreateTable(TSDB_REPO_T *repo, STableCfg *pCfg);
int   tsdbDropTable(TSDB_REPO_T *pRepo, STableId tableId);
int   tsdbUpdateTagValue(TSDB_REPO_T *repo, SUpdateTableTagValMsg *pMsg);
TSKEY tsdbGetTableLastKey(TSDB_REPO_T *repo, uint64_t uid);
void  tsdbStartStream(TSDB_REPO_T *repo);
H
hzcheng 已提交
119

H
TD-353  
Hongze Cheng 已提交
120
uint32_t tsdbGetFileInfo(TSDB_REPO_T *repo, char *name, uint32_t *index, uint32_t eindex, int32_t *size);
121

H
Hongze Cheng 已提交
122
// the TSDB repository info
H
more  
hzcheng 已提交
123
typedef struct STsdbRepoInfo {
H
more  
hzcheng 已提交
124
  STsdbCfg tsdbCfg;
H
Hongze Cheng 已提交
125 126 127 128
  int64_t  version;            // version of the repository
  int64_t  tsdbTotalDataSize;  // the original inserted data size
  int64_t  tsdbTotalDiskSize;  // the total disk size taken by this TSDB repository
  // TODO: Other informations to add
H
more  
hzcheng 已提交
129
} STsdbRepoInfo;
H
TD-353  
Hongze Cheng 已提交
130
STsdbRepoInfo *tsdbGetStatus(TSDB_REPO_T *pRepo);
H
hzcheng 已提交
131

H
Hongze Cheng 已提交
132 133 134 135
// the meter information report structure
typedef struct {
  STableCfg tableCfg;
  int64_t   version;
H
more  
hzcheng 已提交
136 137
  int64_t   tableTotalDataSize;  // In bytes
  int64_t   tableTotalDiskSize;  // In bytes
H
Hongze Cheng 已提交
138
} STableInfo;
H
TD-353  
Hongze Cheng 已提交
139
STableInfo *tsdbGetTableInfo(TSDB_REPO_T *pRepo, STableId tid);
H
Hongze Cheng 已提交
140 141 142 143 144 145 146 147 148

// -- FOR INSERT DATA
/**
 * Insert data to a table in a repository
 * @param pRepo the TSDB repository handle
 * @param pData the data to insert (will give a more specific description)
 *
 * @return the number of points inserted, -1 for failure and the error number is set
 */
H
Hongze Cheng 已提交
149
int32_t tsdbInsertData(TSDB_REPO_T *repo, SSubmitMsg *pMsg, SShellSubmitRspMsg *pRsp);
H
Hongze Cheng 已提交
150 151 152

// -- FOR QUERY TIME SERIES DATA

H
hzcheng 已提交
153
typedef void *TsdbQueryHandleT;  // Use void to hide implementation details
H
Hongze Cheng 已提交
154 155

// query condition to build vnode iterator
H
hjxilinx 已提交
156
typedef struct STsdbQueryCond {
H
Hongze Cheng 已提交
157 158 159 160
  STimeWindow  twindow;
  int32_t      order;  // desc|asc order to iterate the data block
  int32_t      numOfCols;
  SColumnInfo *colList;
H
hjxilinx 已提交
161
} STsdbQueryCond;
H
Hongze Cheng 已提交
162

H
hjxilinx 已提交
163 164
typedef struct SDataBlockInfo {
  STimeWindow window;
165
  int32_t     rows;
H
hjxilinx 已提交
166 167
  int32_t     numOfCols;
  int64_t     uid;
H
hjxilinx 已提交
168
  int32_t     tid;
H
hjxilinx 已提交
169
} SDataBlockInfo;
H
Hongze Cheng 已提交
170

171
typedef struct {
H
Haojun Liao 已提交
172 173 174
  size_t   numOfTables;
  SArray   *pGroupList;
  SHashObj *map;         // speedup acquire the tableQueryInfo from STableId
175 176
} STableGroupInfo;

H
hjxilinx 已提交
177 178 179 180 181
typedef struct SQueryRowCond {
  int32_t rel;
  TSKEY   ts;
} SQueryRowCond;

H
Hongze Cheng 已提交
182 183
/**
 * Get the data block iterator, starting from position according to the query condition
H
hjxilinx 已提交
184 185 186
 *
 * @param tsdb       tsdb handle
 * @param pCond      query condition, including time window, result set order, and basic required columns for each block
187
 * @param tableqinfoGroupInfo  tableId list in the form of set, seperated into different groups according to group by condition
H
Haojun Liao 已提交
188
 * @param qinfo      query info handle from query processor
H
Hongze Cheng 已提交
189 190
 * @return
 */
191
TsdbQueryHandleT *tsdbQueryTables(TSDB_REPO_T *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableqinfoGroupInfo, void *qinfo);
H
Hongze Cheng 已提交
192 193

/**
H
hjxilinx 已提交
194 195 196 197 198
 * 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
H
Hongze Cheng 已提交
199 200
 * @param pCond       query condition, including time window, result set order, and basic required columns for each
 * block
201
 * @param tableqinfoGroupInfo   tableId list.
H
hjxilinx 已提交
202 203
 * @return
 */
204
TsdbQueryHandleT tsdbQueryLastRow(TSDB_REPO_T *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableqinfoGroupInfo, void *qinfo);
H
hjxilinx 已提交
205

206 207 208 209 210 211
/**
 * get the queried table object list
 * @param pHandle
 * @return
 */
SArray* tsdbGetQueriedTableList(TsdbQueryHandleT *pHandle);
212

213 214 215 216 217 218 219 220
/**
 * get the group list according to table id from client
 * @param tsdb
 * @param pCond
 * @param groupList
 * @param qinfo
 * @return
 */
H
Hongze Cheng 已提交
221 222
TsdbQueryHandleT tsdbQueryRowsInExternalWindow(TSDB_REPO_T *tsdb, STsdbQueryCond *pCond, STableGroupInfo *groupList,
                                               void *qinfo);
223

H
hjxilinx 已提交
224 225 226
/**
 * move to next block if exists
 *
H
hjxilinx 已提交
227
 * @param pQueryHandle
H
Hongze Cheng 已提交
228 229
 * @return
 */
H
hzcheng 已提交
230
bool tsdbNextDataBlock(TsdbQueryHandleT *pQueryHandle);
H
Hongze Cheng 已提交
231 232

/**
H
hjxilinx 已提交
233
 * Get current data block information
H
Hongze Cheng 已提交
234 235 236 237
 *
 * @param pQueryHandle
 * @return
 */
H
hzcheng 已提交
238
SDataBlockInfo tsdbRetrieveDataBlockInfo(TsdbQueryHandleT *pQueryHandle);
H
Hongze Cheng 已提交
239 240

/**
H
hjxilinx 已提交
241 242 243 244 245 246 247
 *
 * 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
H
Hongze Cheng 已提交
248 249
 * @return
 */
H
hzcheng 已提交
250
int32_t tsdbRetrieveDataBlockStatisInfo(TsdbQueryHandleT *pQueryHandle, SDataStatis **pBlockStatis);
H
Hongze Cheng 已提交
251 252

/**
H
hjxilinx 已提交
253
 *
H
hjxilinx 已提交
254 255 256
 * 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.
H
Hongze Cheng 已提交
257
 *
H
hjxilinx 已提交
258 259
 * @param pQueryHandle      query handle
 * @param pColumnIdList     required data columns id list
H
Hongze Cheng 已提交
260 261
 * @return
 */
H
hjxilinx 已提交
262
SArray *tsdbRetrieveDataBlock(TsdbQueryHandleT *pQueryHandle, SArray *pColumnIdList);
H
Hongze Cheng 已提交
263 264

/**
265
 * Get the qualified table id for a super table according to the tag query expression.
H
Hongze Cheng 已提交
266 267 268
 * @param stableid. super table sid
 * @param pTagCond. tag query condition
 */
H
TD-353  
Hongze Cheng 已提交
269
int32_t tsdbQuerySTableByTagCond(TSDB_REPO_T *tsdb, uint64_t uid, const char *pTagCond, size_t len,
H
Hongze Cheng 已提交
270 271
                                 int16_t tagNameRelType, const char *tbnameCond, STableGroupInfo *pGroupList,
                                 SColIndex *pColIndex, int32_t numOfCols);
H
hjxilinx 已提交
272

273 274 275 276 277 278
/**
 * destory the created table group list, which is generated by tag query
 * @param pGroupList
 */
void tsdbDestoryTableGroup(STableGroupInfo *pGroupList);

H
hjxilinx 已提交
279 280 281 282 283 284 285 286
/**
 * 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
TD-353  
Hongze Cheng 已提交
287
int32_t tsdbGetOneTableGroup(TSDB_REPO_T *tsdb, uint64_t uid, STableGroupInfo *pGroupInfo);
H
Hongze Cheng 已提交
288

289 290 291 292 293 294 295 296 297
/**
 *
 * @param tsdb
 * @param pTableIdList
 * @param pGroupInfo
 * @return
 */
int32_t tsdbGetTableGroupFromIdList(TSDB_REPO_T* tsdb, SArray* pTableIdList, STableGroupInfo* pGroupInfo);

298 299 300 301
/**
 * clean up the query handle
 * @param queryHandle
 */
H
hzcheng 已提交
302
void tsdbCleanupQueryHandle(TsdbQueryHandleT queryHandle);
303

T
Tao Liu 已提交
304 305 306 307 308 309 310 311 312
/**
 * get the statistics of repo usage
 * @param repo. point to the tsdbrepo
 * @param totalPoints. total data point written
 * @param totalStorage. total bytes took by the tsdb
 * @param compStorage. total bytes took by the tsdb after compressed
 */
void tsdbReportStat(void *repo, int64_t *totalPoints, int64_t *totalStorage, int64_t *compStorage);

H
more  
hzcheng 已提交
313 314 315 316
#ifdef __cplusplus
}
#endif

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
317
#endif  // _TD_TSDB_H_