tsdb.h 10.9 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
TD-1027  
Hongze Cheng 已提交
41 42
// TSDB STATE DEFINITION
#define TSDB_STATE_OK 0x0
H
Hongze Cheng 已提交
43 44
#define TSDB_STATE_BAD_META 0x1
#define TSDB_STATE_BAD_DATA 0x2
H
TD-1027  
Hongze Cheng 已提交
45

H
hzcheng 已提交
46 47 48
// --------- TSDB APPLICATION HANDLE DEFINITION
typedef struct {
  void *appH;
J
jtao1735 已提交
49
  void *cqH;
50
  int (*notifyStatus)(void *, int status, int eno);
H
hzcheng 已提交
51
  int (*eventCallBack)(void *);
H
Hongze Cheng 已提交
52
  void *(*cqCreateFunc)(void *handle, uint64_t uid, int32_t sid, const char *dstTable, char *sqlStr, STSchema *pSchema);
H
TD-354  
Hongze Cheng 已提交
53
  void (*cqDropFunc)(void *handle);
H
hzcheng 已提交
54 55
} STsdbAppH;

H
hzcheng 已提交
56 57 58
// --------- TSDB REPOSITORY CONFIGURATION DEFINITION
typedef struct {
  int32_t tsdbId;
S
slguan 已提交
59 60
  int32_t cacheBlockSize;
  int32_t totalBlocks;
H
Hongze Cheng 已提交
61 62
  int32_t daysPerFile;  // day per file sharding policy
  int32_t keep;         // day of data to keep
S
slguan 已提交
63 64
  int32_t keep1;
  int32_t keep2;
H
hzcheng 已提交
65 66
  int32_t minRowsPerFileBlock;  // minimum rows per file block
  int32_t maxRowsPerFileBlock;  // maximum rows per file block
S
slguan 已提交
67 68
  int8_t  precision;
  int8_t  compression;
H
TD-1438  
Hongze Cheng 已提交
69
  int8_t  update;
70
  int8_t  cacheLastRow;
H
hzcheng 已提交
71 72
} STsdbCfg;

T
Tao Liu 已提交
73 74
// --------- TSDB REPOSITORY USAGE STATISTICS
typedef struct {
75 76 77 78
  int64_t totalStorage;  // total bytes occupie
  int64_t compStorage;
  int64_t pointsWritten;  // total data points written
} STsdbStat;
T
Tao Liu 已提交
79

H
Hongze Cheng 已提交
80
typedef struct STsdbRepo STsdbRepo;
81

H
Hongze Cheng 已提交
82
STsdbCfg *tsdbGetCfg(const STsdbRepo *repo);
H
hzcheng 已提交
83 84

// --------- TSDB REPOSITORY DEFINITION
H
Hongze Cheng 已提交
85 86 87 88 89 90
int32_t    tsdbCreateRepo(int repoid);
int32_t    tsdbDropRepo(int repoid);
STsdbRepo *tsdbOpenRepo(STsdbCfg *pCfg, STsdbAppH *pAppH);
int        tsdbCloseRepo(STsdbRepo *repo, int toCommit);
int32_t    tsdbConfigRepo(STsdbRepo *repo, STsdbCfg *pCfg);
int        tsdbGetState(STsdbRepo *repo);
H
hzcheng 已提交
91 92

// --------- TSDB TABLE DEFINITION
H
more  
hzcheng 已提交
93
typedef struct {
94 95
  uint64_t uid;  // the unique table ID
  int32_t  tid;  // the table ID in the repository.
H
more  
hzcheng 已提交
96 97
} STableId;

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

void tsdbClearTableCfg(STableCfg *config);

H
Hongze Cheng 已提交
114 115
void *tsdbGetTableTagVal(const void *pTable, int32_t colId, int16_t type, int16_t bytes);
char *tsdbGetTableName(void *pTable);
H
Haojun Liao 已提交
116 117

#define TSDB_TABLEID(_table) ((STableId*) (_table))
118 119
#define TSDB_PREV_ROW  0x1
#define TSDB_NEXT_ROW  0x2
H
Haojun Liao 已提交
120

H
Hongze Cheng 已提交
121
STableCfg *tsdbCreateTableCfgFromMsg(SMDCreateTableMsg *pMsg);
122

H
Hongze Cheng 已提交
123 124 125
int tsdbCreateTable(STsdbRepo *repo, STableCfg *pCfg);
int tsdbDropTable(STsdbRepo *pRepo, STableId tableId);
int tsdbUpdateTableTagValue(STsdbRepo *repo, SUpdateTableTagValMsg *pMsg);
H
hzcheng 已提交
126

H
Hongze Cheng 已提交
127
uint32_t tsdbGetFileInfo(STsdbRepo *repo, char *name, uint32_t *index, uint32_t eindex, int64_t *size);
128

H
Hongze Cheng 已提交
129
// the TSDB repository info
H
more  
hzcheng 已提交
130
typedef struct STsdbRepoInfo {
H
more  
hzcheng 已提交
131
  STsdbCfg tsdbCfg;
S
TD-1919  
Shengliang Guan 已提交
132
  uint64_t version;            // version of the repository
H
Hongze Cheng 已提交
133 134 135
  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 已提交
136
} STsdbRepoInfo;
H
Hongze Cheng 已提交
137
STsdbRepoInfo *tsdbGetStatus(STsdbRepo *pRepo);
H
hzcheng 已提交
138

H
Hongze Cheng 已提交
139 140 141
// the meter information report structure
typedef struct {
  STableCfg tableCfg;
S
TD-1919  
Shengliang Guan 已提交
142
  uint64_t  version;
H
more  
hzcheng 已提交
143 144
  int64_t   tableTotalDataSize;  // In bytes
  int64_t   tableTotalDiskSize;  // In bytes
H
Hongze Cheng 已提交
145 146 147 148 149 150 151 152 153 154
} STableInfo;

// -- 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 已提交
155
int32_t tsdbInsertData(STsdbRepo *repo, SSubmitMsg *pMsg, SShellSubmitRspMsg *pRsp);
H
Hongze Cheng 已提交
156 157 158

// -- FOR QUERY TIME SERIES DATA

H
hzcheng 已提交
159
typedef void *TsdbQueryHandleT;  // Use void to hide implementation details
H
Hongze Cheng 已提交
160 161

// query condition to build vnode iterator
H
hjxilinx 已提交
162
typedef struct STsdbQueryCond {
H
Hongze Cheng 已提交
163
  STimeWindow  twindow;
164
  int32_t      order;             // desc|asc order to iterate the data block
H
Hongze Cheng 已提交
165 166
  int32_t      numOfCols;
  SColumnInfo *colList;
167
  bool         loadExternalRows;  // load external rows or not
H
hjxilinx 已提交
168
} STsdbQueryCond;
H
Hongze Cheng 已提交
169

H
Haojun Liao 已提交
170
typedef struct SMemRef {
H
Hongze Cheng 已提交
171 172 173
  int32_t ref;
  void *  mem;
  void *  imem;
H
Haojun Liao 已提交
174 175
} SMemRef;

H
hjxilinx 已提交
176 177
typedef struct SDataBlockInfo {
  STimeWindow window;
178
  int32_t     rows;
H
hjxilinx 已提交
179 180
  int32_t     numOfCols;
  int64_t     uid;
H
hjxilinx 已提交
181
  int32_t     tid;
H
hjxilinx 已提交
182
} SDataBlockInfo;
H
Hongze Cheng 已提交
183

184
typedef struct {
H
Hongze Cheng 已提交
185 186
  void *pTable;
  TSKEY lastKey;
H
Haojun Liao 已提交
187 188 189 190
} STableKeyInfo;

typedef struct {
  size_t    numOfTables;
H
Hongze Cheng 已提交
191 192
  SArray *  pGroupList;
  SHashObj *map;  // speedup acquire the tableQueryInfo by table uid
193 194
} STableGroupInfo;

H
Hongze Cheng 已提交
195 196
/**
 * Get the data block iterator, starting from position according to the query condition
H
hjxilinx 已提交
197 198 199
 *
 * @param tsdb       tsdb handle
 * @param pCond      query condition, including time window, result set order, and basic required columns for each block
H
Haojun Liao 已提交
200 201
 * @param tableInfoGroup  table object list in the form of set, grouped into different sets according to the
 *                        group by condition
H
Haojun Liao 已提交
202
 * @param qinfo      query info handle from query processor
H
Hongze Cheng 已提交
203 204
 * @return
 */
H
Hongze Cheng 已提交
205 206
TsdbQueryHandleT *tsdbQueryTables(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableInfoGroup, void *qinfo,
                                  SMemRef *pRef);
H
Hongze Cheng 已提交
207 208

/**
H
hjxilinx 已提交
209 210 211 212
 * 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.
 *
H
Haojun Liao 已提交
213 214 215
 * @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.
H
hjxilinx 已提交
216 217
 * @return
 */
H
Hongze Cheng 已提交
218 219
TsdbQueryHandleT tsdbQueryLastRow(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableInfo, void *qinfo,
                                  SMemRef *pRef);
H
hjxilinx 已提交
220

221 222 223 224 225
/**
 * get the queried table object list
 * @param pHandle
 * @return
 */
H
Hongze Cheng 已提交
226
SArray *tsdbGetQueriedTableList(TsdbQueryHandleT *pHandle);
227

228 229 230 231 232 233 234 235
/**
 * get the group list according to table id from client
 * @param tsdb
 * @param pCond
 * @param groupList
 * @param qinfo
 * @return
 */
H
Hongze Cheng 已提交
236 237
TsdbQueryHandleT tsdbQueryRowsInExternalWindow(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *groupList,
                                               void *qinfo, SMemRef *pRef);
238

Y
yihaoDeng 已提交
239 240 241 242 243 244 245 246 247 248

/**
 * get num of rows in mem table 
 *
 * @param pHandle
 * @return row size
 */

int64_t tsdbGetNumOfRowsInMemTable(TsdbQueryHandleT* pHandle);

H
hjxilinx 已提交
249
/**
Y
yihaoDeng 已提交
250
 * move to next block if exists 
H
hjxilinx 已提交
251
 *
H
hjxilinx 已提交
252
 * @param pQueryHandle
H
Hongze Cheng 已提交
253 254
 * @return
 */
H
hzcheng 已提交
255
bool tsdbNextDataBlock(TsdbQueryHandleT *pQueryHandle);
H
Haojun Liao 已提交
256

Y
yihaoDeng 已提交
257 258 259 260 261 262 263
/**
 * move to next block if exists but not merge data in memtable 
 *
 * @param pQueryHandle
 * @return
 */
bool tsdbNextDataBlockWithoutMerge(TsdbQueryHandleT *pQueryHandle);
H
Hongze Cheng 已提交
264

265
SArray* tsdbGetExternalRow(TsdbQueryHandleT *pHandle, SMemRef* pMemRef, int16_t type);
H
Hongze Cheng 已提交
266 267

/**
H
hjxilinx 已提交
268
 * Get current data block information
H
Hongze Cheng 已提交
269 270
 *
 * @param pQueryHandle
H
Haojun Liao 已提交
271
 * @param pBlockInfo
H
Hongze Cheng 已提交
272 273
 * @return
 */
H
Hongze Cheng 已提交
274
void tsdbRetrieveDataBlockInfo(TsdbQueryHandleT *pQueryHandle, SDataBlockInfo *pBlockInfo);
H
Hongze Cheng 已提交
275 276

/**
H
hjxilinx 已提交
277 278 279 280 281 282 283
 *
 * 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 已提交
284 285
 * @return
 */
H
hzcheng 已提交
286
int32_t tsdbRetrieveDataBlockStatisInfo(TsdbQueryHandleT *pQueryHandle, SDataStatis **pBlockStatis);
H
Hongze Cheng 已提交
287 288

/**
H
hjxilinx 已提交
289
 *
H
hjxilinx 已提交
290 291 292
 * 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 已提交
293
 *
H
hjxilinx 已提交
294 295
 * @param pQueryHandle      query handle
 * @param pColumnIdList     required data columns id list
H
Hongze Cheng 已提交
296 297
 * @return
 */
H
hjxilinx 已提交
298
SArray *tsdbRetrieveDataBlock(TsdbQueryHandleT *pQueryHandle, SArray *pColumnIdList);
H
Hongze Cheng 已提交
299 300

/**
301
 * Get the qualified table id for a super table according to the tag query expression.
H
Hongze Cheng 已提交
302 303 304
 * @param stableid. super table sid
 * @param pTagCond. tag query condition
 */
H
Hongze Cheng 已提交
305
int32_t tsdbQuerySTableByTagCond(STsdbRepo *tsdb, uint64_t uid, TSKEY key, const char *pTagCond, size_t len,
H
Hongze Cheng 已提交
306 307
                                 int16_t tagNameRelType, const char *tbnameCond, STableGroupInfo *pGroupList,
                                 SColIndex *pColIndex, int32_t numOfCols);
H
hjxilinx 已提交
308

309
/**
H
Haojun Liao 已提交
310
 * destroy the created table group list, which is generated by tag query
311 312
 * @param pGroupList
 */
H
Haojun Liao 已提交
313
void tsdbDestroyTableGroup(STableGroupInfo *pGroupList);
314

H
hjxilinx 已提交
315 316 317 318 319 320 321 322
/**
 * 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
Hongze Cheng 已提交
323
int32_t tsdbGetOneTableGroup(STsdbRepo *tsdb, uint64_t uid, TSKEY startKey, STableGroupInfo *pGroupInfo);
H
Hongze Cheng 已提交
324

325 326 327 328 329 330 331
/**
 *
 * @param tsdb
 * @param pTableIdList
 * @param pGroupInfo
 * @return
 */
H
Hongze Cheng 已提交
332
int32_t tsdbGetTableGroupFromIdList(STsdbRepo *tsdb, SArray *pTableIdList, STableGroupInfo *pGroupInfo);
333

334 335 336 337
/**
 * clean up the query handle
 * @param queryHandle
 */
H
hzcheng 已提交
338
void tsdbCleanupQueryHandle(TsdbQueryHandleT queryHandle);
339

H
Haojun Liao 已提交
340 341
void tsdbResetQueryHandle(TsdbQueryHandleT queryHandle, STsdbQueryCond *pCond);

H
Haojun Liao 已提交
342 343
void tsdbResetQueryHandleForNewTable(TsdbQueryHandleT queryHandle, STsdbQueryCond *pCond, STableGroupInfo* groupList);

T
Tao Liu 已提交
344 345 346 347 348 349 350 351 352
/**
 * 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);

353
int  tsdbInitCommitQueue();
H
Hongze Cheng 已提交
354
void tsdbDestroyCommitQueue();
H
Hongze Cheng 已提交
355
int  tsdbSyncCommit(STsdbRepo *repo);
S
TD-2072  
Shengliang Guan 已提交
356
void tsdbIncCommitRef(int vgId);
S
TD-2072  
Shengliang Guan 已提交
357
void tsdbDecCommitRef(int vgId);
H
Hongze Cheng 已提交
358

H
Hongze Cheng 已提交
359
// For TSDB file sync
S
TD-1207  
Shengliang Guan 已提交
360 361
int tsdbSyncSend(void *pRepo, SOCKET socketFd);
int tsdbSyncRecv(void *pRepo, SOCKET socketFd);
H
Hongze Cheng 已提交
362

H
more  
hzcheng 已提交
363 364 365 366
#ifdef __cplusplus
}
#endif

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