qExtbuffer.h 7.2 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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_TEXTBUFFER_H
#define TDENGINE_TEXTBUFFER_H

#ifdef __cplusplus
extern "C" {
#endif

H
hjxilinx 已提交
22
#include "os.h"
S
slguan 已提交
23
#include "taosmsg.h"
24 25

#include "tarray.h"
H
hjxilinx 已提交
26
#include "tutil.h"
S
slguan 已提交
27
#include "tdataformat.h"
28
#include "talgo.h"
H
hzcheng 已提交
29

H
Haojun Liao 已提交
30
#define MAX_TMPFILE_PATH_LENGTH        PATH_MAX
H
hjxilinx 已提交
31
#define INITIAL_ALLOCATION_BUFFER_SIZE 64
H
Haojun Liao 已提交
32
#define DEFAULT_PAGE_SIZE              (1024L)  // 16k larger than the SHistoInfo
H
hzcheng 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

typedef enum EXT_BUFFER_FLUSH_MODEL {
  /*
   * all data that have been flushed to disk is belonged to the same group
   * which means, all data in disk are sorted, or order is not matter in this case
   */
  SINGLE_APPEND_MODEL,

  /*
   * each flush operation to disk is completely independant to any other flush operation
   * we simply merge several set of data in one file, to reduce the count of flat files
   * in disk. So in this case, we need to keep the flush-out information in tFlushoutInfo
   * structure.
   */
  MULTIPLE_APPEND_MODEL,
} EXT_BUFFER_FLUSH_MODEL;

typedef struct tFlushoutInfo {
  uint32_t startPageId;
  uint32_t numOfPages;
} tFlushoutInfo;

typedef struct tFlushoutData {
  uint32_t       nAllocSize;
  uint32_t       nLength;
  tFlushoutInfo *pFlushoutInfo;
} tFlushoutData;

61
typedef struct SExtFileInfo {
H
hzcheng 已提交
62
  uint32_t      nFileSize;  // in pages
H
hjxilinx 已提交
63
  uint32_t      pageSize;
H
hzcheng 已提交
64 65
  uint32_t      numOfElemsInFile;
  tFlushoutData flushoutData;
66
} SExtFileInfo;
H
hzcheng 已提交
67 68

typedef struct tFilePage {
69
  uint64_t num;
H
hzcheng 已提交
70 71 72 73 74 75 76 77
  char     data[];
} tFilePage;

typedef struct tFilePagesItem {
  struct tFilePagesItem *pNext;
  tFilePage              item;
} tFilePagesItem;

78 79 80 81 82 83 84
typedef struct SSchema1 {
  uint8_t type;
  char    name[TSDB_COL_NAME_LEN];
  int16_t colId;
  int32_t bytes;
} SSchema1;

S
slguan 已提交
85
typedef struct SSchemaEx {
86
  SSchema1       field;
87
  int32_t        offset;
S
slguan 已提交
88
} SSchemaEx;
H
hjxilinx 已提交
89 90 91 92

typedef struct SColumnModel {
  int32_t    capacity;
  int32_t    numOfCols;
93
  int32_t    rowSize;
S
slguan 已提交
94
  SSchemaEx *pFields;
H
hjxilinx 已提交
95
} SColumnModel;
H
hzcheng 已提交
96

H
hjxilinx 已提交
97 98
typedef struct SColumnOrderInfo {
  int32_t numOfCols;
H
Haojun Liao 已提交
99
  int16_t colIndex[];
H
hjxilinx 已提交
100
} SColumnOrderInfo;
H
hzcheng 已提交
101 102

typedef struct tOrderDescriptor {
H
hjxilinx 已提交
103 104
  SColumnModel *   pColumnModel;
  int32_t          tsOrder;  // timestamp order type if exists
H
Haojun Liao 已提交
105
  SColumnOrderInfo orderInfo;
H
hzcheng 已提交
106 107 108
} tOrderDescriptor;

typedef struct tExtMemBuffer {
H
hjxilinx 已提交
109
  int32_t inMemCapacity;
H
hzcheng 已提交
110
  int32_t nElemSize;
H
hjxilinx 已提交
111 112
  int32_t pageSize;
  int32_t numOfTotalElems;
H
hzcheng 已提交
113 114
  int32_t numOfElemsInBuffer;
  int32_t numOfElemsPerPage;
H
hjxilinx 已提交
115
  int16_t numOfInMemPages;
H
hzcheng 已提交
116 117 118 119

  tFilePagesItem *pHead;
  tFilePagesItem *pTail;

H
hjxilinx 已提交
120 121
  char *    path;
  FILE *    file;
122
  SExtFileInfo fileMeta;
H
hzcheng 已提交
123

H
hjxilinx 已提交
124
  SColumnModel *         pColumnModel;
H
hzcheng 已提交
125 126 127
  EXT_BUFFER_FLUSH_MODEL flushModel;
} tExtMemBuffer;

H
hjxilinx 已提交
128 129 130 131 132 133
/**
 *
 * @param inMemSize
 * @param elemSize
 * @param pModel
 * @return
H
hzcheng 已提交
134
 */
H
Haojun Liao 已提交
135
tExtMemBuffer *createExtMemBuffer(int32_t inMemSize, int32_t elemSize, int32_t pagesize, SColumnModel *pModel);
H
hzcheng 已提交
136

H
hjxilinx 已提交
137 138 139 140
/**
 *
 * @param pMemBuffer
 * @return
H
hzcheng 已提交
141
 */
H
hjxilinx 已提交
142
void *destoryExtMemBuffer(tExtMemBuffer *pMemBuffer);
H
hzcheng 已提交
143

H
hjxilinx 已提交
144
/**
H
hzcheng 已提交
145 146 147 148 149 150 151 152
 * @param pMemBuffer
 * @param data       input data pointer
 * @param numOfRows  number of rows in data
 * @param pModel     column format model
 * @return           number of pages in memory
 */
int16_t tExtMemBufferPut(tExtMemBuffer *pMemBuffer, void *data, int32_t numOfRows);

H
hjxilinx 已提交
153 154 155 156
/**
 *
 * @param pMemBuffer
 * @return
H
hzcheng 已提交
157
 */
158
int32_t tExtMemBufferFlush(tExtMemBuffer *pMemBuffer);
H
hzcheng 已提交
159

H
hjxilinx 已提交
160 161
/**
 *
H
hzcheng 已提交
162 163 164 165 166 167 168 169 170 171 172 173
 * remove all data that has been put into buffer, including in buffer or
 * ext-buffer(disk)
 */
void tExtMemBufferClear(tExtMemBuffer *pMemBuffer);

/*
 * this function should be removed.
 * since the flush to disk operation is transparent to client this structure should provide stream operation for data,
 * and there is an internal cursor point to the data.
 */
bool tExtMemBufferLoadData(tExtMemBuffer *pMemBuffer, tFilePage *pFilePage, int32_t flushIdx, int32_t pageIdx);

H
hjxilinx 已提交
174 175 176 177 178
/**
 *
 * @param pMemBuffer
 * @return
 */
H
hzcheng 已提交
179 180
bool tExtMemBufferIsAllDataInMem(tExtMemBuffer *pMemBuffer);

H
hjxilinx 已提交
181 182 183 184 185 186 187
/**
 *
 * @param fields
 * @param numOfCols
 * @param blockCapacity
 * @return
 */
188
SColumnModel *createColumnModel(SSchema1 *fields, int32_t numOfCols, int32_t blockCapacity);
H
hjxilinx 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

/**
 *
 * @param pSrc
 * @return
 */
SColumnModel *cloneColumnModel(SColumnModel *pSrc);

/**
 *
 * @param pModel
 */
void destroyColumnModel(SColumnModel *pModel);

/*
 * compress data into consecutive block without hole in data
 */
void tColModelCompact(SColumnModel *pModel, tFilePage *inputBuffer, int32_t maxElemsCapacity);

void     tColModelErase(SColumnModel *pModel, tFilePage *inputBuffer, int32_t maxCapacity, int32_t s, int32_t e);
209
SSchema1 *getColumnModelSchema(SColumnModel *pColumnModel, int32_t index);
H
hzcheng 已提交
210

H
hjxilinx 已提交
211
int16_t getColumnModelOffset(SColumnModel *pColumnModel, int32_t index);
H
hzcheng 已提交
212 213 214 215 216 217 218 219 220

typedef struct SSrcColumnInfo {
  int32_t functionId;
  int32_t type;
} SSrcColumnInfo;

/*
 * display data in column format model for debug purpose only
 */
H
hjxilinx 已提交
221
void tColModelDisplay(SColumnModel *pModel, void *pData, int32_t numOfRows, int32_t maxCount);
H
hzcheng 已提交
222

H
hjxilinx 已提交
223
void tColModelDisplayEx(SColumnModel *pModel, void *pData, int32_t numOfRows, int32_t maxCount, SSrcColumnInfo *pInfo);
H
hzcheng 已提交
224

H
hjxilinx 已提交
225 226
tOrderDescriptor *tOrderDesCreate(const int32_t *orderColIdx, int32_t numOfOrderCols, SColumnModel *pModel,
                                  int32_t tsOrderType);
H
hzcheng 已提交
227 228 229

void tOrderDescDestroy(tOrderDescriptor *pDesc);

H
Haojun Liao 已提交
230 231
void taoscQSort(void** pCols, SSchema* pSchema, int32_t numOfCols, int32_t numOfRows, int32_t index, __compar_fn_t compareFn);

H
hjxilinx 已提交
232
void tColModelAppend(SColumnModel *dstModel, tFilePage *dstPage, void *srcData, int32_t srcStartRows,
H
hzcheng 已提交
233 234 235 236 237 238
                     int32_t numOfRowsToWrite, int32_t srcCapacity);

typedef int (*__col_compar_fn_t)(tOrderDescriptor *, int32_t numOfRows, int32_t idx1, int32_t idx2, char *data);

void tColDataQSort(tOrderDescriptor *, int32_t numOfRows, int32_t start, int32_t end, char *data, int32_t orderType);

239 240
void taoscQSort(void** pCols, SSchema* pSchema, int32_t numOfCols, int32_t numOfRows, int32_t index, __compar_fn_t compareFn);

H
hzcheng 已提交
241 242 243 244 245 246 247 248 249 250
int32_t compare_sa(tOrderDescriptor *, int32_t numOfRows, int32_t idx1, int32_t idx2, char *data);

int32_t compare_sd(tOrderDescriptor *, int32_t numOfRows, int32_t idx1, int32_t idx2, char *data);

int32_t compare_a(tOrderDescriptor *, int32_t numOfRow1, int32_t s1, char *data1, int32_t numOfRow2, int32_t s2,
                  char *data2);

int32_t compare_d(tOrderDescriptor *, int32_t numOfRow1, int32_t s1, char *data1, int32_t numOfRow2, int32_t s2,
                  char *data2);

H
Haojun Liao 已提交
251
struct SSDataBlock;
H
Haojun Liao 已提交
252
int32_t compare_aRv(struct SSDataBlock* pBlock, SArray* colIndex, int32_t numOfCols, int32_t rowIndex, char** buffer, int32_t order);
H
Haojun Liao 已提交
253

H
Haojun Liao 已提交
254 255
int32_t columnValueAscendingComparator(char *f1, char *f2, int32_t type, int32_t bytes);

H
hzcheng 已提交
256 257 258 259 260
#ifdef __cplusplus
}
#endif

#endif  // TBASE_SORT_H