qTsbuf.h 4.0 KB
Newer Older
H
hjxilinx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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/>.
 */

#ifndef TDENGINE_STSBUF_H
#define TDENGINE_STSBUF_H

#ifdef __cplusplus
extern "C" {
#endif

#include "os.h"
#include "taosdef.h"
25
#include "tvariant.h"
H
hjxilinx 已提交
26 27 28

#define MEM_BUF_SIZE (1 << 20)
#define TS_COMP_FILE_MAGIC 0x87F5EC4C
H
Haojun Liao 已提交
29
#define TS_COMP_FILE_GROUP_MAX 512
H
hjxilinx 已提交
30 31 32 33 34 35 36 37 38

typedef struct STSList {
  char*   rawBuf;
  int32_t allocSize;
  int32_t threshold;
  int32_t len;
} STSList;

typedef struct STSElem {
39
  TSKEY     ts;
H
Haojun Liao 已提交
40
  tVariant* tag;
H
Haojun Liao 已提交
41
  int32_t   id;
H
hjxilinx 已提交
42 43 44
} STSElem;

typedef struct STSCursor {
45
  int32_t  vgroupIndex;
H
hjxilinx 已提交
46 47 48
  int32_t  blockIndex;
  int32_t  tsIndex;
  uint32_t order;
H
hjxilinx 已提交
49 50 51
} STSCursor;

typedef struct STSBlock {
52 53 54 55 56
  tVariant tag;        // tag value
  int32_t  numOfElem;  // number of elements
  int32_t  compLen;    // size after compressed
  int32_t  padding;    // 0xFFFFFFFF by default, after the payload
  char*    payload;    // actual data that is compressed
H
hjxilinx 已提交
57 58 59 60 61 62
} STSBlock;

/*
 * The size of buffer file should not be greater than 2G,
 * and the offset of int32_t type is enough
 */
H
Haojun Liao 已提交
63 64
typedef struct STSGroupBlockInfo {
  int32_t id;          // group id
H
hjxilinx 已提交
65 66 67
  int32_t offset;      // offset set value in file
  int32_t numOfBlocks; // number of total blocks
  int32_t compLen;     // compressed size
H
Haojun Liao 已提交
68
} STSGroupBlockInfo;
H
hjxilinx 已提交
69

H
Haojun Liao 已提交
70 71
typedef struct STSGroupBlockInfoEx {
  STSGroupBlockInfo info;
H
hjxilinx 已提交
72
  int32_t           len;  // length before compress
H
Haojun Liao 已提交
73
} STSGroupBlockInfoEx;
H
hjxilinx 已提交
74 75 76 77 78 79

typedef struct STSBuf {
  FILE*    f;
  char     path[PATH_MAX];
  uint32_t fileSize;

H
Haojun Liao 已提交
80
  // todo use array
H
Haojun Liao 已提交
81
  STSGroupBlockInfoEx* pData;
82
  uint32_t             numOfAlloc;
H
Haojun Liao 已提交
83
  uint32_t             numOfGroups;
H
hjxilinx 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96

  char*     assistBuf;
  int32_t   bufSize;
  STSBlock  block;
  STSList   tsData;  // uncompressed raw ts data
  uint64_t  numOfTotal;
  bool      autoDelete;
  int32_t   tsOrder;  // order of timestamp in ts comp buffer
  STSCursor cur;
} STSBuf;

typedef struct STSBufFileHeader {
  uint32_t magic;       // file magic number
H
Haojun Liao 已提交
97
  uint32_t numOfGroup;  // number of group stored in current file
H
Haojun Liao 已提交
98
  int32_t  tsOrder;     // timestamp order in current file
H
hjxilinx 已提交
99 100
} STSBufFileHeader;

H
Haojun Liao 已提交
101
STSBuf* tsBufCreate(bool autoDelete, int32_t order);
H
hjxilinx 已提交
102
STSBuf* tsBufCreateFromFile(const char* path, bool autoDelete);
H
Haojun Liao 已提交
103
STSBuf* tsBufCreateFromCompBlocks(const char* pData, int32_t numOfBlocks, int32_t len, int32_t tsOrder, int32_t id);
H
hjxilinx 已提交
104

H
Haojun Liao 已提交
105
void* tsBufDestroy(STSBuf* pTSBuf);
H
hjxilinx 已提交
106

H
Haojun Liao 已提交
107
void    tsBufAppend(STSBuf* pTSBuf, int32_t id, tVariant* tag, const char* pData, int32_t len);
H
Haojun Liao 已提交
108
int32_t tsBufMerge(STSBuf* pDestBuf, const STSBuf* pSrcBuf);
H
hjxilinx 已提交
109 110 111

STSBuf* tsBufClone(STSBuf* pTSBuf);

H
Haojun Liao 已提交
112
STSGroupBlockInfo* tsBufGetGroupBlockInfo(STSBuf* pTSBuf, int32_t id);
H
hjxilinx 已提交
113 114 115 116 117

void tsBufFlush(STSBuf* pTSBuf);

void    tsBufResetPos(STSBuf* pTSBuf);
STSElem tsBufGetElem(STSBuf* pTSBuf);
H
Haojun Liao 已提交
118

H
hjxilinx 已提交
119 120
bool    tsBufNextPos(STSBuf* pTSBuf);

H
Haojun Liao 已提交
121
STSElem tsBufGetElemStartPos(STSBuf* pTSBuf, int32_t id, tVariant* tag);
H
hjxilinx 已提交
122 123 124 125 126 127 128 129 130 131 132 133

STSCursor tsBufGetCursor(STSBuf* pTSBuf);
void      tsBufSetTraverseOrder(STSBuf* pTSBuf, int32_t order);

void tsBufSetCursor(STSBuf* pTSBuf, STSCursor* pCur);

/**
 * display all data in comp block file, for debug purpose only
 * @param pTSBuf
 */
void tsBufDisplay(STSBuf* pTSBuf);

H
Haojun Liao 已提交
134
int32_t tsBufGetNumOfGroup(STSBuf* pTSBuf);
H
Haojun Liao 已提交
135

H
Haojun Liao 已提交
136
void tsBufGetGroupIdList(STSBuf* pTSBuf, int32_t* num, int32_t** id);
H
Haojun Liao 已提交
137

H
Haojun Liao 已提交
138
int32_t dumpFileBlockByGroupId(STSBuf* pTSBuf, int32_t id, void* buf, int32_t* len, int32_t* numOfBlocks);
139

H
Haojun Liao 已提交
140 141 142 143
STSElem tsBufFindElemStartPosByTag(STSBuf* pTSBuf, tVariant* pTag);

bool tsBufIsValidElem(STSElem* pElem);

H
hjxilinx 已提交
144 145 146 147 148
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_STSBUF_H