CUBRID Engine  latest
external_sort.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * external_sort.h - External sorting module
22  */
23 
24 #ifndef _EXTERNAL_SORT_H_
25 #define _EXTERNAL_SORT_H_
26 
27 #ident "$Id$"
28 
29 #if !defined (SERVER_MODE) && !defined (SA_MODE)
30 #error Belongs to server module
31 #endif /* !defined (SERVER_MODE) && !defined (SA_MODE) */
32 
33 #include "error_manager.h"
34 #include "query_list.h"
35 #include "storage_common.h"
36 #include "thread_compat.hpp"
37 
38 #define SORT_PUT_STOP 2
39 
40 #define NO_SORT_LIMIT (-1)
41 
42 #define SORT_RECORD_LENGTH_SIZE (sizeof(INT64)) /* for 8byte align */
43 #define SORT_RECORD_LENGTH(item_p) (*((int *) ((item_p) - SORT_RECORD_LENGTH_SIZE)))
44 
45 typedef enum
46 {
51 } SORT_STATUS;
52 
53 typedef enum
54 {
55  SORT_ELIM_DUP, /* eliminate duplicate */
56  SORT_DUP /* allow duplicate */
58 
59 typedef SORT_STATUS SORT_GET_FUNC (THREAD_ENTRY * thread_p, RECDES *, void *);
60 typedef int SORT_PUT_FUNC (THREAD_ENTRY * thread_p, const RECDES *, void *);
61 typedef int SORT_CMP_FUNC (const void *, const void *, void *);
62 
63 typedef struct SORT_REC SORT_REC;
64 typedef struct SUBKEY_INFO SUBKEY_INFO;
65 typedef struct SORTKEY_INFO SORTKEY_INFO;
66 typedef struct SORT_INFO SORT_INFO;
67 
68 struct SORT_REC
69 {
70  SORT_REC *next; /* forward link for duplicate sort_key value */
71  union
72  {
73  /* Bread crumbs back to the original tuple, so that we can go straight there after the keys have been sorted. */
74  struct
75  {
76  INT32 pageid; /* Page identifier */
77  INT16 volid; /* Volume identifier */
78  INT16 offset; /* offset in page */
79  char body[1]; /* sort_key body start position */
80  } original;
81 
82  /*
83  * The offset vector. A value of zero for an entry means that the
84  * corresponding column is null, and that there are no data bytes for
85  * the column. A non-zero entry is interpreted as the offset from
86  * the *start* of the SORT_REC to the data bytes for that column.
87  */
88  int offset[1];
89  } s;
90 };
91 
93 {
94  /* The actual column number in the list file tuple. */
95  int col;
96 
97 
99 
101 
102  TP_DOMAIN *cmp_dom; /* for median sorting string in different domain */
103 
104  // signature should match pr_type::data_cmpdisk_function_type
105  // todo - use a function type for both sort_f and pr_type::data_cmpdisk_function_type
106  DB_VALUE_COMPARE_RESULT (*sort_f) (void *tplp1, void *tplp2, TP_DOMAIN * dom, int do_coercion, int total_order,
107  int *start_col);
108 
109  /*
110  * Non-zero iff the sort on this column is descending. Factoring
111  * this decision out of the actual sort function allows to use only
112  * one of those guys, at no particularly great cost in performance,
113  * and a big win in maintainability.
114  */
115  int is_desc;
116 
118 
119  bool use_cmp_dom; /* when true, use cmp_dom to make comparing */
120 };
121 
123 {
124  int nkeys; /* The number of columns in use today. */
125  int use_original; /* False iff the sort keys consist of all of the input record fields, i.e., if we'll
126  * reconstruct the input records from the keys rather than look them up again in the
127  * original file. */
128  SUBKEY_INFO *key; /* Points to `default_keys' if `nkeys' <= 8; otherwise it points to malloc'ed space. */
129  SUBKEY_INFO default_keys[8]; /* Default storage; this ought to work for most cases. */
130  int error; /* median domain convert errors */
131 };
132 
133 struct SORT_INFO
134 {
135  SORTKEY_INFO key_info; /* All of the interesting key information. */
136  QFILE_SORT_SCAN_ID *s_id; /* A SCAN_ID for the input list file. This is stateful, and records the current
137  * location of the scan between calls to ls_sort_get_next(). */
138  QFILE_LIST_ID *output_file; /* The name of the output file. This is where ls_sort_put_next_*() deposits its stuff.
139  */
140  RECDES output_recdes; /* A working buffer for output of tuples; used only when we're using
141  * ls_sort_put_next_short() as the output function. */
142  void *extra_arg; /* extra information supplied by the caller */
143 };
144 
145 extern int sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GET_FUNC * get_fn,
146  void *get_arg, SORT_PUT_FUNC * put_fn, void *put_arg, SORT_CMP_FUNC * cmp_fn, void *cmp_arg,
147  SORT_DUP_OPTION option, int limit, bool includes_tde_class);
148 
149 #endif /* _EXTERNAL_SORT_H_ */
INT16 volid
Definition: external_sort.h:77
union SORT_REC::@159 s
int sort_listfile(THREAD_ENTRY *thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GET_FUNC *get_fn, void *get_arg, SORT_PUT_FUNC *put_fn, void *put_arg, SORT_CMP_FUNC *cmp_fn, void *cmp_arg, SORT_DUP_OPTION option, int limit, bool includes_tde_class)
QFILE_SORT_SCAN_ID * s_id
int SORT_PUT_FUNC(THREAD_ENTRY *thread_p, const RECDES *, void *)
Definition: external_sort.h:60
QFILE_LIST_ID * output_file
void * extra_arg
INT16 offset
Definition: external_sort.h:78
TP_DOMAIN * cmp_dom
char body[1]
Definition: external_sort.h:79
void THREAD_ENTRY
SORTKEY_INFO key_info
TP_DOMAIN * col_dom
SORT_REC * next
Definition: external_sort.h:70
SORT_STATUS
Definition: external_sort.h:45
struct SORT_REC::@159::@160 original
SORT_DUP_OPTION
Definition: external_sort.h:53
RECDES output_recdes
SUBKEY_INFO * key
DB_VALUE_COMPARE_RESULT
Definition: dbtype_def.h:199
int SORT_CMP_FUNC(const void *, const void *, void *)
Definition: external_sort.h:61
INT32 pageid
Definition: external_sort.h:76
SORT_STATUS SORT_GET_FUNC(THREAD_ENTRY *thread_p, RECDES *, void *)
Definition: external_sort.h:59