2 * UCW Library -- Universal Sorter: Common Declarations
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #ifndef _UCW_SORTER_COMMON_H
11 #define _UCW_SORTER_COMMON_H
13 #include "lib/clists.h"
15 /* Configuration, some of the variables are used by the old sorter, too. */
16 extern uns sorter_trace, sorter_presort_bufsize, sorter_stream_bufsize;
17 extern uns sorter_debug, sorter_min_radix_bits, sorter_max_radix_bits;
18 extern u64 sorter_bufsize;
20 #define SORT_TRACE(x...) do { if (sorter_trace) log(L_DEBUG, x); } while(0)
21 #define SORT_XTRACE(level, x...) do { if (sorter_trace >= level) log(L_DEBUG, x); } while(0)
24 SORT_DEBUG_NO_PRESORT = 1,
25 SORT_DEBUG_NO_JOIN = 2,
26 SORT_DEBUG_KEEP_BUCKETS = 4,
27 SORT_DEBUG_NO_RADIX = 8,
33 struct fastbuf *in_fb;
34 struct fastbuf *out_fb;
40 void *big_buf, *big_buf_half;
41 size_t big_buf_size, big_buf_half_size;
43 int (*custom_presort)(struct fastbuf *dest, void *buf, size_t bufsize);
45 // Take as much as possible from the source bucket, sort it in memory and dump to destination bucket.
46 // Return 1 if there is more data available in the source bucket.
47 int (*internal_sort)(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket *out, struct sort_bucket *out_only);
49 // Estimate how much input data from `b' will fit in the internal sorting buffer.
50 u64 (*internal_estimate)(struct sort_context *ctx, struct sort_bucket *b);
52 // Two-way split/merge: merge up to 2 source buckets to up to 2 destination buckets.
53 // Bucket arrays are NULL-terminated.
54 void (*twoway_merge)(struct sort_context *ctx, struct sort_bucket **ins, struct sort_bucket **outs);
56 // Radix split according to hash function
57 void (*radix_split)(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket **outs, uns bitpos, uns numbits);
59 // State variables of internal_sort
64 timestamp_t start_time;
66 uns total_int_time, total_pre_time, total_ext_time;
69 void sorter_run(struct sort_context *ctx);
73 void *sorter_alloc(struct sort_context *ctx, uns size);
74 void sorter_prepare_buf(struct sort_context *ctx);
75 void sorter_alloc_buf(struct sort_context *ctx);
76 void sorter_free_buf(struct sort_context *ctx);
82 struct sort_context *ctx;
86 u64 size; // Size in bytes (not valid when writing)
87 uns runs; // Number of runs, 0 if not sorted
88 uns hash_bits; // Remaining bits of the hash function
89 byte *ident; // Identifier used in debug messages
92 enum sort_bucket_flags {
93 SBF_FINAL = 1, // This bucket corresponds to the final output file (always 1 run)
94 SBF_SOURCE = 2, // Contains the source file (always 0 runs)
95 SBF_CUSTOM_PRESORT = 4, // Contains source to read via custom presorter
96 SBF_OPEN_WRITE = 256, // We are currently writing to the fastbuf
97 SBF_OPEN_READ = 512, // We are reading from the fastbuf
98 SBF_DESTROYED = 1024, // Already done with, no further references allowed
99 SBF_SWAPPED_OUT = 2048, // Swapped out to a named file
102 struct sort_bucket *sbuck_new(struct sort_context *ctx);
103 void sbuck_drop(struct sort_bucket *b);
104 int sbuck_have(struct sort_bucket *b);
105 int sbuck_has_file(struct sort_bucket *b);
106 sh_off_t sbuck_size(struct sort_bucket *b);
107 struct fastbuf *sbuck_read(struct sort_bucket *b);
108 struct fastbuf *sbuck_write(struct sort_bucket *b);
109 void sbuck_swap_out(struct sort_bucket *b);
110 void format_size(byte *buf, u64 x);