tsort.h 3.6 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 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 67 68
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 已提交
69 70
SSortHandle* tsortCreateSortHandle(SArray* pOrderInfo, int32_t type, int32_t pageSize, int32_t numOfPages,
                                   SSDataBlock* pBlock, const char* idstr);
71 72 73 74 75

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

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

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

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

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

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

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

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

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

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

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

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

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

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

D
dapan1121 已提交
173 174 175 176

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

177 178 179 180 181
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_TSORT_H