tsort.h 3.4 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
  };
47

wmmhello's avatar
wmmhello 已提交
48
} SSortSource;
49

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

H
Hongze Cheng 已提交
57
typedef struct SSortHandle  SSortHandle;
58 59 60 61 62 63 64 65 66 67
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
 * @return
 */
H
Hongze Cheng 已提交
68 69
SSortHandle* tsortCreateSortHandle(SArray* pOrderInfo, int32_t type, int32_t pageSize, int32_t numOfPages,
                                   SSDataBlock* pBlock, const char* idstr);
70 71 72 73 74

/**
 *
 * @param pSortHandle
 */
H
Haojun Liao 已提交
75
void tsortDestroySortHandle(SSortHandle* pSortHandle);
76 77 78 79 80 81

/**
 *
 * @param pHandle
 * @return
 */
H
Haojun Liao 已提交
82
int32_t tsortOpen(SSortHandle* pHandle);
83 84 85 86 87 88

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

/**
 *
 * @return
 */
H
Hongze Cheng 已提交
95 96
int32_t tsortSetFetchRawDataFp(SSortHandle* pHandle, _sort_fetch_block_fn_t fetchFp, void (*fp)(SSDataBlock*, void*),
                               void* param);
97 98 99 100 101 102 103

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

106 107 108 109 110
/**
 *
 */
int32_t tsortSetCompareGroupId(SSortHandle* pHandle, bool compareGroupId);

111 112 113 114 115 116
/**
 *
 * @param pHandle
 * @param pSource
 * @return success or failed
 */
H
Haojun Liao 已提交
117
int32_t tsortAddSource(SSortHandle* pSortHandle, void* pSource);
118 119 120 121 122 123

/**
 *
 * @param pHandle
 * @return
 */
H
Haojun Liao 已提交
124
STupleHandle* tsortNextTuple(SSortHandle* pHandle);
125 126 127 128

/**
 *
 * @param pHandle
129
 * @param colId
130 131
 * @return
 */
132
bool tsortIsNullVal(STupleHandle* pVHandle, int32_t colId);
133 134 135 136

/**
 *
 * @param pHandle
137
 * @param colId
138 139
 * @return
 */
140 141
void* tsortGetValue(STupleHandle* pVHandle, int32_t colId);

S
slzhou 已提交
142 143 144 145 146 147 148
/**
 *
 * @param pVHandle
 * @return
 */
uint64_t tsortGetGroupId(STupleHandle* pVHandle);

149 150 151 152 153 154
/**
 *
 * @param pSortHandle
 * @return
 */
SSDataBlock* tsortGetSortedDataBlock(const SSortHandle* pSortHandle);
155

156 157 158 159 160 161 162 163
/**
 * return the sort execution information.
 *
 * @param pHandle
 * @return
 */
SSortExecInfo tsortGetSortExecInfo(SSortHandle* pHandle);

164 165 166
/**
 * get proper sort buffer pages according to the row size
 * @param rowSize
167
 * @param numOfCols columns count that be put into page
168 169
 * @return
 */
170
int32_t getProperSortPageSize(size_t rowSize, uint32_t numOfCols);
171

172 173 174 175 176
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_TSORT_H