tsort.h 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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_TSORT_H
#define TDENGINE_TSORT_H

#ifdef __cplusplus
extern "C" {
#endif

#include "os.h"
H
Hongze Cheng 已提交
24
#include "tcommon.h"
25 26

enum {
H
Haojun Liao 已提交
27 28
  SORT_MULTISOURCE_MERGE = 0x1,
  SORT_SINGLESOURCE_SORT = 0x2,
S
slzhou 已提交
29
  SORT_BLOCK_TS_MERGE = 0x3
30 31 32 33 34
};

typedef struct SMultiMergeSource {
  int32_t      type;
  int32_t      rowIndex;
H
Hongze Cheng 已提交
35
  SSDataBlock* pBlock;
36 37
} SMultiMergeSource;

wmmhello's avatar
wmmhello 已提交
38
typedef struct SSortSource {
39
  SMultiMergeSource src;
40 41 42 43 44
  struct {
    SArray* pageIdList;
    int32_t pageIndex;
  };
  struct {
H
Hongze Cheng 已提交
45
    void* param;
46
    bool  onlyRef;
wmmhello's avatar
wmmhello 已提交
47
  };
D
dapan1121 已提交
48 49
  int64_t fetchUs;
  int64_t fetchNum;
wmmhello's avatar
wmmhello 已提交
50
} SSortSource;
51

52
typedef struct SMsortComparParam {
H
Hongze Cheng 已提交
53 54 55 56
  void**  pSources;
  int32_t numOfSources;
  SArray* orderInfo;  // SArray<SBlockOrderInfo>
  bool    cmpGroupId;
57 58

  int32_t sortType;
S
slzhou 已提交
59
  // the following field to speed up when sortType == SORT_BLOCK_TS_MERGE
60 61 62
  int32_t tsSlotId;
  int32_t order;
  __compar_fn_t cmpFn;
63 64
} SMsortComparParam;

H
Hongze Cheng 已提交
65
typedef struct SSortHandle  SSortHandle;
66 67 68 69 70 71 72 73
typedef struct STupleHandle STupleHandle;

typedef SSDataBlock* (*_sort_fetch_block_fn_t)(void* param);
typedef int32_t (*_sort_merge_compar_fn_t)(const void* p1, const void* p2, void* param);

/**
 *
 * @param type
74 75
 * @param maxRows keep maxRows at most, if 0, pq sort will not be used
 * @param maxTupleLength max len of one tuple, for check if pq sort is applicable
76
 * @param sortBufSize sort memory buf size, for check if heap sort is applicable
77 78
 * @return
 */
H
Hongze Cheng 已提交
79
SSortHandle* tsortCreateSortHandle(SArray* pOrderInfo, int32_t type, int32_t pageSize, int32_t numOfPages,
S
slzhou 已提交
80 81
                                   SSDataBlock* pBlock, const char* idstr, uint64_t pqMaxRows, uint32_t pqMaxTupleLength,
                                   uint32_t pqSortBufSize);
82

83 84
void tsortSetForceUsePQSort(SSortHandle* pHandle);

85 86 87 88
/**
 *
 * @param pSortHandle
 */
H
Haojun Liao 已提交
89
void tsortDestroySortHandle(SSortHandle* pSortHandle);
90 91 92 93 94 95

/**
 *
 * @param pHandle
 * @return
 */
H
Haojun Liao 已提交
96
int32_t tsortOpen(SSortHandle* pHandle);
97 98 99 100 101 102

/**
 *
 * @param pHandle
 * @return
 */
H
Haojun Liao 已提交
103
int32_t tsortClose(SSortHandle* pHandle);
104 105 106 107 108

/**
 *
 * @return
 */
H
Hongze Cheng 已提交
109 110
int32_t tsortSetFetchRawDataFp(SSortHandle* pHandle, _sort_fetch_block_fn_t fetchFp, void (*fp)(SSDataBlock*, void*),
                               void* param);
111 112 113 114 115 116 117

/**
 *
 * @param pHandle
 * @param fp
 * @return
 */
H
Haojun Liao 已提交
118
int32_t tsortSetComparFp(SSortHandle* pHandle, _sort_merge_compar_fn_t fp);
119

S
slzhou 已提交
120 121 122 123
/**
 * 
*/
void tsortSetMergeLimit(SSortHandle* pHandle, int64_t mergeLimit);
124 125 126 127 128
/**
 *
 */
int32_t tsortSetCompareGroupId(SSortHandle* pHandle, bool compareGroupId);

129 130 131 132 133 134
/**
 *
 * @param pHandle
 * @param pSource
 * @return success or failed
 */
H
Haojun Liao 已提交
135
int32_t tsortAddSource(SSortHandle* pSortHandle, void* pSource);
136 137 138 139 140 141

/**
 *
 * @param pHandle
 * @return
 */
H
Haojun Liao 已提交
142
STupleHandle* tsortNextTuple(SSortHandle* pHandle);
143 144 145 146

/**
 *
 * @param pHandle
147
 * @param colId
148 149
 * @return
 */
150
bool tsortIsNullVal(STupleHandle* pVHandle, int32_t colId);
151 152 153 154

/**
 *
 * @param pHandle
155
 * @param colId
156 157
 * @return
 */
158 159
void* tsortGetValue(STupleHandle* pVHandle, int32_t colId);

S
slzhou 已提交
160 161 162 163 164 165
/**
 *
 * @param pVHandle
 * @return
 */
uint64_t tsortGetGroupId(STupleHandle* pVHandle);
166
void*    tsortGetBlockInfo(STupleHandle* pVHandle);
167 168 169 170 171 172
/**
 *
 * @param pSortHandle
 * @return
 */
SSDataBlock* tsortGetSortedDataBlock(const SSortHandle* pSortHandle);
173

174 175 176 177 178 179 180 181
/**
 * return the sort execution information.
 *
 * @param pHandle
 * @return
 */
SSortExecInfo tsortGetSortExecInfo(SSortHandle* pHandle);

182 183 184
/**
 * get proper sort buffer pages according to the row size
 * @param rowSize
185
 * @param numOfCols columns count that be put into page
186 187
 * @return
 */
188
int32_t getProperSortPageSize(size_t rowSize, uint32_t numOfCols);
189

D
dapan1121 已提交
190 191 192 193

bool tsortIsClosed(SSortHandle* pHandle);
void tsortSetClosed(SSortHandle* pHandle);

194 195 196 197 198
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_TSORT_H