tsdb.h 10.7 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
hjxilinx 已提交
22
#include "dataformat.h"
H
hzcheng 已提交
23
#include "taosdef.h"
H
hzcheng 已提交
24
#include "taosmsg.h"
H
hjxilinx 已提交
25
#include "tarray.h"
26
#include "name.h"
H
Hongze Cheng 已提交
27

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

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

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

H
hzcheng 已提交
37 38 39 40 41 42 43 44 45
// --------- TSDB APPLICATION HANDLE DEFINITION
typedef struct {
  // WAL handle
  void *appH;
  int (*walCallBack)(void *);
  int (*eventCallBack)(void *);
  int (*cqueryCallBack)(void *);
} STsdbAppH;

H
hzcheng 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
// --------- TSDB REPOSITORY CONFIGURATION DEFINITION
typedef struct {
  int8_t  precision;
  int32_t tsdbId;
  int32_t maxTables;            // maximum number of tables this repository can have
  int32_t daysPerFile;          // day per file sharding policy
  int32_t minRowsPerFileBlock;  // minimum rows per file block
  int32_t maxRowsPerFileBlock;  // maximum rows per file block
  int32_t keep;                 // day of data to keep
  int64_t maxCacheSize;         // maximum cache size this TSDB can use
} STsdbCfg;

void      tsdbSetDefaultCfg(STsdbCfg *pCfg);
STsdbCfg *tsdbCreateDefaultCfg();
void      tsdbFreeCfg(STsdbCfg *pCfg);

// --------- TSDB REPOSITORY DEFINITION
H
hzcheng 已提交
63
typedef void tsdb_repo_t;  // use void to hide implementation details from outside
H
hzcheng 已提交
64

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
65
int            tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter);
H
hzcheng 已提交
66
int32_t        tsdbDropRepo(tsdb_repo_t *repo);
H
hzcheng 已提交
67
tsdb_repo_t *  tsdbOpenRepo(char *tsdbDir, STsdbAppH *pAppH);
H
hzcheng 已提交
68 69
int32_t        tsdbCloseRepo(tsdb_repo_t *repo);
int32_t        tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg);
H
TD-34  
hzcheng 已提交
70
int32_t        tsdbTriggerCommit(tsdb_repo_t *repo);
H
TD-34  
hzcheng 已提交
71 72
int32_t        tsdbLockRepo(tsdb_repo_t *repo);
int32_t        tsdbUnLockRepo(tsdb_repo_t *repo);
H
hzcheng 已提交
73 74

// --------- TSDB TABLE DEFINITION
H
more  
hzcheng 已提交
75
typedef struct {
H
more  
hzcheng 已提交
76 77
  int64_t uid;  // the unique table ID
  int32_t tid;  // the table ID in the repository.
H
more  
hzcheng 已提交
78 79
} STableId;

H
hzcheng 已提交
80 81 82 83
// --------- TSDB TABLE configuration
typedef struct {
  TSDB_TABLE_TYPE type;
  STableId        tableId;
H
hzcheng 已提交
84
  int32_t         sversion;
H
hzcheng 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  int64_t         superUid;
  STSchema *      schema;
  STSchema *      tagSchema;
  SDataRow        tagValues;
} STableCfg;

int  tsdbInitTableCfg(STableCfg *config, TSDB_TABLE_TYPE type, int64_t uid, int32_t tid);
int  tsdbTableSetSuperUid(STableCfg *config, int64_t uid);
int  tsdbTableSetSchema(STableCfg *config, STSchema *pSchema, bool dup);
int  tsdbTableSetTagSchema(STableCfg *config, STSchema *pSchema, bool dup);
int  tsdbTableSetTagValue(STableCfg *config, SDataRow row, bool dup);
void tsdbClearTableCfg(STableCfg *config);

int tsdbCreateTable(tsdb_repo_t *repo, STableCfg *pCfg);
int tsdbDropTable(tsdb_repo_t *pRepo, STableId tableId);
int tsdbAlterTable(tsdb_repo_t *repo, STableCfg *pCfg);

H
hzcheng 已提交
102 103 104 105 106 107 108 109 110
typedef struct {
  int32_t  totalLen;
  int32_t  len;
  SDataRow row;
} SSubmitBlkIter;

int      tsdbInitSubmitBlkIter(SSubmitBlk *pBlock, SSubmitBlkIter *pIter);
SDataRow tsdbGetSubmitBlkNext(SSubmitBlkIter *pIter);

H
hzcheng 已提交
111 112 113 114 115 116 117 118
#define TSDB_SUBMIT_MSG_HEAD_SIZE sizeof(SSubmitMsg)

// SSubmitMsg Iterator
typedef struct {
  int32_t totalLen;
  int32_t len;
  SSubmitBlk *pBlock;
} SSubmitMsgIter;
H
more  
hzcheng 已提交
119

H
hzcheng 已提交
120
int         tsdbInitSubmitMsgIter(SSubmitMsg *pMsg, SSubmitMsgIter *pIter);
H
hzcheng 已提交
121
SSubmitBlk *tsdbGetSubmitMsgNext(SSubmitMsgIter *pIter);
H
more  
hzcheng 已提交
122

H
Hongze Cheng 已提交
123
// the TSDB repository info
H
more  
hzcheng 已提交
124
typedef struct STsdbRepoInfo {
H
more  
hzcheng 已提交
125
  STsdbCfg tsdbCfg;
H
Hongze Cheng 已提交
126 127 128 129
  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 已提交
130
} STsdbRepoInfo;
H
hzcheng 已提交
131 132
STsdbRepoInfo *tsdbGetStatus(tsdb_repo_t *pRepo);

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

// -- For table manipulation

/**
 * Create/Alter a table in a TSDB repository handle
H
more  
hzcheng 已提交
146
 * @param repo the TSDB repository handle
H
Hongze Cheng 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
 * @param pCfg the table configurations, the upper layer should free the pointer
 *
 * @return 0 for success, -1 for failure and the error number is set
 */

/**
 * Drop a table in a repository and free all the resources it takes
 * @param pRepo the TSDB repository handle
 * @param tid the ID of the table to drop
 * @param error the error number to set when failure occurs
 *
 * @return 0 for success, -1 for failure and the error number is set
 */

/**
 * Get the information of a table in the repository
 * @param pRepo the TSDB repository handle
 * @param tid the ID of the table to drop
 * @param error the error number to set when failure occurs
 *
 * @return a table information handle for success, NULL for failure and the error number is set
 */

// -- 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
hzcheng 已提交
178
int32_t tsdbInsertData(tsdb_repo_t *pRepo, SSubmitMsg *pMsg);
H
Hongze Cheng 已提交
179 180 181

// -- FOR QUERY TIME SERIES DATA

H
hjxilinx 已提交
182
typedef void* tsdb_query_handle_t;  // Use void to hide implementation details
H
Hongze Cheng 已提交
183

184 185 186 187
typedef struct STableGroupList {    // qualified table object list in group
  SArray*  pGroupList;
  int32_t  numOfTables;
} STableGroupList;
H
more  
hzcheng 已提交
188

H
Hongze Cheng 已提交
189
// query condition to build vnode iterator
H
hjxilinx 已提交
190
typedef struct STsdbQueryCond {
H
Hongze Cheng 已提交
191 192
  STimeWindow       twindow;
  int32_t           order;  // desc/asc order to iterate the data block
H
hjxilinx 已提交
193
  SColumnInfoData*  colList;
H
hjxilinx 已提交
194
} STsdbQueryCond;
H
Hongze Cheng 已提交
195 196 197 198 199 200 201 202 203 204 205

typedef struct SBlockInfo {
  STimeWindow window;

  int32_t numOfRows;
  int32_t numOfCols;

  STableId tableId;
} SBlockInfo;

//  TODO: move this data struct out of the module
206 207 208 209
//typedef struct SData {
//  int32_t num;
//  char *  data;
//} SData;
H
Hongze Cheng 已提交
210

H
hjxilinx 已提交
211 212
typedef struct SDataBlockInfo {
  STimeWindow window;
213
  int32_t     rows;
H
hjxilinx 已提交
214 215 216 217
  int32_t     numOfCols;
  int64_t     uid;
  int32_t     sid;
} SDataBlockInfo;
H
Hongze Cheng 已提交
218

H
more  
hzcheng 已提交
219 220 221
typedef struct {
} SFields;

H
hjxilinx 已提交
222 223 224 225 226 227 228 229 230 231
#define TSDB_TS_GREATER_EQUAL 1
#define TSDB_TS_LESS_EQUAL 2

typedef struct SQueryRowCond {
  int32_t rel;
  TSKEY   ts;
} SQueryRowCond;

typedef void *tsdbpos_t;

H
Hongze Cheng 已提交
232 233 234 235 236 237
/**
 * Get the data block iterator, starting from position according to the query condition
 * @param pCond  query condition, only includes the filter on primary time stamp
 * @param pTableList    table sid list
 * @return
 */
238
tsdb_query_handle_t *tsdbQueryTables(tsdb_repo_t* tsdb, STsdbQueryCond *pCond, SArray *idList, SArray *pColumnInfo);
H
Hongze Cheng 已提交
239 240

/**
H
hjxilinx 已提交
241 242
 * move to next block
 * @param pQueryHandle
H
Hongze Cheng 已提交
243 244
 * @return
 */
H
hjxilinx 已提交
245
bool tsdbNextDataBlock(tsdb_query_handle_t *pQueryHandle);
H
Hongze Cheng 已提交
246 247

/**
H
hjxilinx 已提交
248
 * Get current data block information
H
Hongze Cheng 已提交
249 250 251 252
 *
 * @param pQueryHandle
 * @return
 */
H
hjxilinx 已提交
253
SDataBlockInfo tsdbRetrieveDataBlockInfo(tsdb_query_handle_t *pQueryHandle);
H
Hongze Cheng 已提交
254 255

/**
H
hjxilinx 已提交
256 257 258 259 260 261 262
 *
 * 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 已提交
263 264
 * @return
 */
265
int32_t tsdbRetrieveDataBlockStatisInfo(tsdb_query_handle_t *pQueryHandle, SDataStatis **pBlockStatis);
H
Hongze Cheng 已提交
266 267

/**
H
hjxilinx 已提交
268 269 270
 * 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 已提交
271 272 273 274
 *
 * @param pQueryHandle
 * @return
 */
H
hjxilinx 已提交
275
SArray *tsdbRetrieveDataBlock(tsdb_query_handle_t *pQueryHandle, SArray *pIdList);
H
Hongze Cheng 已提交
276 277

/**
H
hjxilinx 已提交
278
 *  todo remove the parameter of position, and order type
H
Hongze Cheng 已提交
279
 *
H
hjxilinx 已提交
280
 *  Reset to the start(end) position of current query, from which the iterator starts.
H
Hongze Cheng 已提交
281
 *
H
hjxilinx 已提交
282 283 284 285 286 287
 * @param pQueryHandle
 * @param position  set the iterator traverses position
 * @param order ascending order or descending order
 * @return
 */
int32_t tsdbResetQuery(tsdb_query_handle_t *pQueryHandle, STimeWindow* window, tsdbpos_t position, int16_t order);
H
Hongze Cheng 已提交
288

H
hjxilinx 已提交
289 290 291 292 293 294 295 296 297 298
/**
 * return the access position of current query handle
 * @param pQueryHandle
 * @return
 */
int32_t tsdbDataBlockSeek(tsdb_query_handle_t *pQueryHandle, tsdbpos_t pos);

/**
 * todo remove this function later
 * @param pQueryHandle
H
Hongze Cheng 已提交
299 300
 * @return
 */
H
hjxilinx 已提交
301
tsdbpos_t tsdbDataBlockTell(tsdb_query_handle_t *pQueryHandle);
H
Hongze Cheng 已提交
302 303

/**
H
hjxilinx 已提交
304 305 306 307 308 309 310 311 312
 * todo remove this function later
 * @param pQueryHandle
 * @param pIdList
 * @return
 */
SArray *tsdbRetrieveDataRow(tsdb_query_handle_t *pQueryHandle, SArray *pIdList, SQueryRowCond *pCond);

/**
 *  Get iterator for super tables, of which tags values satisfy the tag filter info
H
Hongze Cheng 已提交
313
 *
H
hjxilinx 已提交
314 315 316
 *  NOTE: the tagFilterStr is an bin-expression for tag filter, such as ((tag_col = 5) and (tag_col2 > 7))
 *  The filter string is sent from client directly.
 *  The build of the tags filter expression from string is done in the iterator generating function.
H
Hongze Cheng 已提交
317
 *
H
hjxilinx 已提交
318 319
 * @param pCond         query condition
 * @param pTagFilterStr tag filter info
H
Hongze Cheng 已提交
320 321
 * @return
 */
H
hjxilinx 已提交
322
tsdb_query_handle_t *tsdbQueryFromTagConds(STsdbQueryCond *pCond, int16_t stableId, const char *pTagFilterStr);
H
Hongze Cheng 已提交
323 324 325 326 327 328 329 330 331

/**
 * Get the qualified tables for (super) table query.
 * Used to handle the super table projection queries, the last_row query, the group by on normal columns query,
 * the interpolation query, and timestamp-comp query for join processing.
 *
 * @param pQueryHandle
 * @return table sid list. the invoker is responsible for the release of this the sid list.
 */
332
SArray *tsdbGetTableList(tsdb_query_handle_t *pQueryHandle);
H
Hongze Cheng 已提交
333 334

/**
335
 * Get the qualified table id for a super table according to the tag query expression.
H
Hongze Cheng 已提交
336 337 338 339
 * @param stableid. super table sid
 * @param pTagCond. tag query condition
 *
 */
340 341 342 343
int32_t tsdbQueryTags(tsdb_repo_t* tsdb, int64_t uid, const char* pTagCond, size_t len, SArray** pGroupList,
                      SColIndex* pColIndex, int32_t numOfCols);

int32_t tsdbGetOneTableGroup(tsdb_repo_t* tsdb, int64_t uid, SArray** pGroupList);
H
Hongze Cheng 已提交
344

345 346 347 348 349 350
/**
 * clean up the query handle
 * @param queryHandle
 */
void tsdbCleanupQueryHandle(tsdb_query_handle_t queryHandle);

H
more  
hzcheng 已提交
351 352 353 354
#ifdef __cplusplus
}
#endif

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