]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/sorter.h
The write_merged() hook is no longer required in simple cases.
[libucw.git] / lib / sorter / sorter.h
1 /*
2  *      UCW Library -- Universal Sorter
3  *
4  *      (c) 2001--2007 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 /*
12  *  This is not a normal header file, but a generator of sorting
13  *  routines.  Each time you include it with parameters set in the
14  *  corresponding preprocessor macros, it generates a file sorter
15  *  with the parameters given.
16  *
17  *  FIXME: explain the basics (keys and data, callbacks etc.)
18  *
19  *  Basic parameters and callbacks:
20  *
21  *  SORT_PREFIX(x)      add a name prefix (used on all global names
22  *                      defined by the sorter)
23  *
24  *  SORT_KEY            data type capable of storing a single key.
25  *  SORT_KEY_REGULAR    data type holding a single key both in memory and on disk;
26  *                      in this case, bread() and bwrite() is used to read/write keys
27  *                      and it's also assumed that the keys are not very long.
28  *  int PREFIX_compare(SORT_KEY *a, SORT_KEY *b)
29  *                      compares two keys, result like strcmp(). Mandatory.
30  *  int PREFIX_read_key(struct fastbuf *f, SORT_KEY *k)
31  *                      reads a key from a fastbuf, returns nonzero=ok, 0=EOF.
32  *                      Mandatory unless SORT_KEY_REGULAR is defined.
33  *  void PREFIX_write_key(struct fastbuf *f, SORT_KEY *k)
34  *                      writes a key to a fastbuf. Mandatory unless SORT_KEY_REGULAR.
35  *
36  *  SORT_KEY_SIZE(key)  returns the real size of a key (a SORT_KEY type in memory
37  *                      can be truncated to this number of bytes without any harm;
38  *                      used to save memory when the keys have variable sizes).
39  *                      Default: always store the whole SORT_KEY.
40  *  SORT_DATA_SIZE(key) gets a key and returns the amount of data following it.
41  *                      Default: records consist of keys only.
42  *
43  *  Integer sorting:
44  *
45  *  SORT_INT(key)       We are sorting by an integer value. In this mode, PREFIX_compare
46  *                      is supplied automatically and the sorting function gets an extra
47  *                      parameter specifying a range of the integers. The better the range
48  *                      fits, the faster we sort. Sets up SORT_HASH_xxx automatically.
49  *
50  *  Hashing (optional, but it can speed sorting up):
51  *
52  *  SORT_HASH_BITS      signals that a monotone hashing function returning a given number of
53  *                      bits is available. Monotone hash is a function f such that f(x) < f(y)
54  *                      implies x < y and which is approximately uniformly distributed.
55  *  uns PREFIX_hash(SORT_KEY *a)
56  *
57  *  Unification:
58  *
59  *  SORT_UNIFY          merge items with identical keys, needs the following functions:
60  *  void PREFIX_write_merged(struct fastbuf *f, SORT_KEY **keys, void **data, uns n, void *buf)
61  *                      takes n records in memory with keys which compare equal and writes
62  *                      a single record to the given fastbuf. `buf' points to a buffer which
63  *                      is guaranteed to hold the sum of workspace requirements (see below)
64  *                      over all given records.
65  *  void PREFIX_copy_merged(SORT_KEY **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
66  *                      takes n records with keys in memory and data in fastbufs and writes
67  *                      a single record. Used only if SORT_DATA_SIZE or SORT_UNIFY_WORKSPACE is defined.
68  *  SORT_UNIFY_WORKSPACE(key)  gets a key and returns the amount of workspace required when merging
69  *                      the given record. Defaults to 0.
70  *
71  *  Input (choose one of these):
72  *
73  *  SORT_INPUT_FILE     file of a given name
74  *  SORT_INPUT_FB       seekable fastbuf stream
75  *  SORT_INPUT_PIPE     non-seekable fastbuf stream
76  *  SORT_INPUT_PRESORT  custom presorter. Calls function
77  *  int PREFIX_presort(struct fastbuf *dest, void *buf, size_t bufsize);
78  *                      to get successive batches of pre-sorted data.
79  *                      The function is passed a page-aligned presorting buffer.
80  *                      It returns 1 on success or 0 on EOF.
81  *  SORT_DELETE_INPUT   A C expression, if true, then the input files are deleted
82  *                      as soon as possible.
83  *
84  *  Output (chose one of these):
85  *
86  *  SORT_OUTPUT_FILE    file of a given name
87  *  SORT_OUTPUT_FB      temporary fastbuf stream
88  *  SORT_OUTPUT_THIS_FB a given fastbuf stream which can already contain a header
89  *
90  *  Other switches:
91  *
92  *  SORT_UNIQUE         all items have distinct keys (checked in debug mode)
93  *
94  *  The function generated:
95  *
96  *  <outfb> PREFIX_sort(<in>, <out> [,<range>]), where:
97  *                      <in> = input file name/fastbuf or NULL
98  *                      <out> = output file name/fastbuf or NULL
99  *                      <range> = maximum integer value for the SORT_INT mode
100  *                      <outfb> = output fastbuf (in SORT_OUTPUT_FB mode)
101  *
102  *  After including this file, all parameter macros are automatically
103  *  undef'd.
104  */
105
106 #include "lib/sorter/common.h"
107 #include "lib/fastbuf.h"
108
109 #include <fcntl.h>
110
111 #define P(x) SORT_PREFIX(x)
112
113 #ifdef SORT_KEY_REGULAR
114 typedef SORT_KEY_REGULAR P(key);
115 static inline int P(read_key) (struct fastbuf *f, P(key) *k)
116 {
117   return breadb(f, k, sizeof(P(key)));
118 }
119 static inline void P(write_key) (struct fastbuf *f, P(key) *k)
120 {
121   bwrite(f, k, sizeof(P(key)));
122 }
123 #elif defined(SORT_KEY)
124 typedef SORT_KEY P(key);
125 #else
126 #error Missing definition of sorting key.
127 #endif
128
129 #ifdef SORT_INT
130 static inline int P(compare) (P(key) *x, P(key) *y)
131 {
132   if (SORT_INT(*x) < SORT_INT(*y))
133     return -1;
134   if (SORT_INT(*x) > SORT_INT(*y))
135     return 1;
136   return 0;
137 }
138
139 #ifndef SORT_HASH_BITS
140 static inline int P(hash) (P(key) *x)
141 {
142   return SORT_INT((*x));
143 }
144 #endif
145 #endif
146
147 #ifdef SORT_UNIFY
148 #define LESS <
149 #else
150 #define LESS <=
151 #endif
152 #define SWAP(x,y,z) do { z=x; x=y; y=z; } while(0)
153
154 #if defined(SORT_UNIQUE) && defined(DEBUG_ASSERTS)
155 #define SORT_ASSERT_UNIQUE
156 #endif
157
158 #ifdef SORT_KEY_SIZE
159 #define SORT_VAR_KEY
160 #else
161 #define SORT_KEY_SIZE(key) sizeof(key)
162 #endif
163
164 #ifdef SORT_DATA_SIZE
165 #define SORT_VAR_DATA
166 #else
167 #define SORT_DATA_SIZE(key) 0
168 #endif
169
170 static inline void P(copy_data)(P(key) *key, struct fastbuf *in, struct fastbuf *out)
171 {
172   P(write_key)(out, key);
173 #ifdef SORT_VAR_DATA
174   bbcopy(in, out, SORT_DATA_SIZE(*key));
175 #else
176   (void) in;
177 #endif
178 }
179
180 #if defined(SORT_UNIFY) && !defined(SORT_VAR_DATA) && !defined(SORT_UNIFY_WORKSPACE)
181 static inline void P(copy_merged)(P(key) **keys, struct fastbuf **data UNUSED, uns n, struct fastbuf *dest)
182 {
183   P(write_merged)(dest, keys, NULL, n, NULL);
184 }
185 #endif
186
187 #if defined(SORT_VAR_KEY) || defined(SORT_VAR_DATA) || defined(SORT_UNIFY_WORKSPACE)
188 #include "lib/sorter/s-internal.h"
189 #else
190 #include "lib/sorter/s-fixint.h"
191 #endif
192
193 #include "lib/sorter/s-twoway.h"
194
195 #if defined(SORT_HASH_BITS) || defined(SORT_INT)
196 #include "lib/sorter/s-radix.h"
197 #endif
198
199 static struct fastbuf *P(sort)(
200 #ifdef SORT_INPUT_FILE
201                                byte *in,
202 #else
203                                struct fastbuf *in,
204 #endif
205 #ifdef SORT_OUTPUT_FILE
206                                byte *out
207 #else
208                                struct fastbuf *out
209 #endif
210 #ifdef SORT_INT
211                                , uns int_range
212 #endif
213                                )
214 {
215   struct sort_context ctx;
216   bzero(&ctx, sizeof(ctx));
217
218 #ifdef SORT_INPUT_FILE
219   ctx.in_fb = bopen(in, O_RDONLY, sorter_stream_bufsize);
220   ctx.in_size = bfilesize(ctx.in_fb);
221 #elif defined(SORT_INPUT_FB)
222   ctx.in_fb = in;
223   ctx.in_size = bfilesize(in);
224 #elif defined(SORT_INPUT_PIPE)
225   ctx.in_fb = in;
226   ctx.in_size = ~(u64)0;
227 #elif defined(SORT_INPUT_PRESORT)
228   ASSERT(!in);
229   ctx.custom_presort = P(presort);
230   ctx.in_size = ~(u64)0;
231 #else
232 #error No input given.
233 #endif
234 #ifdef SORT_DELETE_INPUT
235   if (SORT_DELETE_INPUT)
236     bconfig(ctx.in_fb, BCONFIG_IS_TEMP_FILE, 1);
237 #endif
238
239 #ifdef SORT_OUTPUT_FB
240   ASSERT(!out);
241 #elif defined(SORT_OUTPUT_THIS_FB)
242   ctx.out_fb = out;
243 #elif defined(SORT_OUTPUT_FILE)
244   /* Just assume fastbuf output and rename the fastbuf later */
245 #else
246 #error No output given.
247 #endif
248
249 #ifdef SORT_HASH_BITS
250   ctx.hash_bits = SORT_HASH_BITS;
251   ctx.radix_split = P(radix_split);
252 #elif defined(SORT_INT)
253   ctx.hash_bits = 0;
254   while (ctx.hash_bits < 32 && (int_range >> ctx.hash_bits))
255     ctx.hash_bits++;
256   ctx.radix_split = P(radix_split);
257 #endif
258
259   ctx.internal_sort = P(internal);
260   ctx.internal_estimate = P(internal_estimate);
261   ctx.twoway_merge = P(twoway_merge);
262
263   sorter_run(&ctx);
264
265 #ifdef SORT_OUTPUT_FILE
266   if (rename(ctx.out_fb->name, out) < 0)
267     die("Cannot rename %s to %s: %m", ctx.out_fb->name, out);
268   bconfig(ctx.out_fb, BCONFIG_IS_TEMP_FILE, 0);
269   bclose(ctx.out_fb);
270   ctx.out_fb = NULL;
271 #endif
272   return ctx.out_fb;
273 }
274
275 #undef SORT_PREFIX
276 #undef SORT_KEY
277 #undef SORT_KEY_REGULAR
278 #undef SORT_KEY_SIZE
279 #undef SORT_DATA_SIZE
280 #undef SORT_VAR_KEY
281 #undef SORT_VAR_DATA
282 #undef SORT_INT
283 #undef SORT_HASH_BITS
284 #undef SORT_UNIFY
285 #undef SORT_UNIFY_WORKSPACE
286 #undef SORT_INPUT_FILE
287 #undef SORT_INPUT_FB
288 #undef SORT_INPUT_PRESORT
289 #undef SORT_OUTPUT_FILE
290 #undef SORT_OUTPUT_FB
291 #undef SORT_OUTPUT_THIS_FB
292 #undef SORT_UNIQUE
293 #undef SORT_ASSERT_UNIQUE
294 #undef SORT_DELETE_INPUT
295 #undef SWAP
296 #undef LESS
297 #undef P
298 /* FIXME: Check that we undef everything we should. */