tsort.h 3.9 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,
29 30 31 32 33
};

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

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

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

H
Hongze Cheng 已提交
58
typedef struct SSortHandle  SSortHandle;
59 60 61 62 63 64 65 66
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
67 68 69
 * @param maxRows keep maxRows at most
 * @param maxTupleLength max len of one tuple, for check if heap sort is applicable
 * @param sortBufSize sort memory buf size, for check if heap sort is applicable
70 71
 * @return
 */
H
Hongze Cheng 已提交
72
SSortHandle* tsortCreateSortHandle(SArray* pOrderInfo, int32_t type, int32_t pageSize, int32_t numOfPages,
73 74
                                   SSDataBlock* pBlock, const char* idstr, uint64_t maxRows, uint32_t maxTupleLength,
                                   uint32_t sortBufSize);
75 76 77 78 79

/**
 *
 * @param pSortHandle
 */
H
Haojun Liao 已提交
80
void tsortDestroySortHandle(SSortHandle* pSortHandle);
81 82 83 84 85 86

/**
 *
 * @param pHandle
 * @return
 */
H
Haojun Liao 已提交
87
int32_t tsortOpen(SSortHandle* pHandle);
88 89 90 91 92 93

/**
 *
 * @param pHandle
 * @return
 */
H
Haojun Liao 已提交
94
int32_t tsortClose(SSortHandle* pHandle);
95 96 97 98 99

/**
 *
 * @return
 */
H
Hongze Cheng 已提交
100 101
int32_t tsortSetFetchRawDataFp(SSortHandle* pHandle, _sort_fetch_block_fn_t fetchFp, void (*fp)(SSDataBlock*, void*),
                               void* param);
102 103 104 105 106 107 108

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

111 112 113 114 115
/**
 *
 */
int32_t tsortSetCompareGroupId(SSortHandle* pHandle, bool compareGroupId);

116 117 118 119 120 121
/**
 *
 * @param pHandle
 * @param pSource
 * @return success or failed
 */
H
Haojun Liao 已提交
122
int32_t tsortAddSource(SSortHandle* pSortHandle, void* pSource);
123 124 125 126 127 128

/**
 *
 * @param pHandle
 * @return
 */
H
Haojun Liao 已提交
129
STupleHandle* tsortNextTuple(SSortHandle* pHandle);
130 131 132 133

/**
 *
 * @param pHandle
134
 * @param colId
135 136
 * @return
 */
137
bool tsortIsNullVal(STupleHandle* pVHandle, int32_t colId);
138 139 140 141

/**
 *
 * @param pHandle
142
 * @param colId
143 144
 * @return
 */
145 146
void* tsortGetValue(STupleHandle* pVHandle, int32_t colId);

S
slzhou 已提交
147 148 149 150 151 152
/**
 *
 * @param pVHandle
 * @return
 */
uint64_t tsortGetGroupId(STupleHandle* pVHandle);
153
void*    tsortGetBlockInfo(STupleHandle* pVHandle);
154 155 156 157 158 159
/**
 *
 * @param pSortHandle
 * @return
 */
SSDataBlock* tsortGetSortedDataBlock(const SSortHandle* pSortHandle);
160

161 162 163 164 165 166 167 168
/**
 * return the sort execution information.
 *
 * @param pHandle
 * @return
 */
SSortExecInfo tsortGetSortExecInfo(SSortHandle* pHandle);

169 170 171
/**
 * get proper sort buffer pages according to the row size
 * @param rowSize
172
 * @param numOfCols columns count that be put into page
173 174
 * @return
 */
175
int32_t getProperSortPageSize(size_t rowSize, uint32_t numOfCols);
176

D
dapan1121 已提交
177 178 179 180

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

181 182 183 184 185
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_TSORT_H