]> mj.ucw.cz Git - libucw.git/blob - sorter/common.h
Released as 6.5.16.
[libucw.git] / sorter / common.h
1 /*
2  *      UCW Library -- Universal Sorter: Common Declarations
3  *
4  *      (c) 2007 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_SORTER_COMMON_H
11 #define _UCW_SORTER_COMMON_H
12
13 #include "lib/clists.h"
14
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 uns sorter_min_multiway_bits, sorter_max_multiway_bits;
19 extern u64 sorter_bufsize;
20 extern struct fb_params sorter_fb_params;
21
22 #define SORT_TRACE(x...) do { if (sorter_trace) msg(L_DEBUG, x); } while(0)
23 #define SORT_XTRACE(level, x...) do { if (sorter_trace >= level) msg(L_DEBUG, x); } while(0)
24
25 enum sort_debug {
26   SORT_DEBUG_NO_PRESORT = 1,
27   SORT_DEBUG_NO_JOIN = 2,
28   SORT_DEBUG_KEEP_BUCKETS = 4,
29   SORT_DEBUG_NO_RADIX = 8,
30   SORT_DEBUG_NO_MULTIWAY = 16,
31   SORT_DEBUG_ASORT_NO_RADIX = 32,
32   SORT_DEBUG_ASORT_NO_THREADS = 64
33 };
34
35 struct sort_bucket;
36
37 struct sort_context {
38   struct fastbuf *in_fb;
39   struct fastbuf *out_fb;
40   uns hash_bits;
41   u64 in_size;
42
43   struct mempool *pool;
44   clist bucket_list;
45   void *big_buf;
46   size_t big_buf_size;
47
48   int (*custom_presort)(struct fastbuf *dest, void *buf, size_t bufsize);
49
50   // Take as much as possible from the source bucket, sort it in memory and dump to destination bucket.
51   // Return 1 if there is more data available in the source bucket.
52   int (*internal_sort)(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket *out, struct sort_bucket *out_only);
53
54   // Estimate how much input data from `b' will fit in the internal sorting buffer.
55   u64 (*internal_estimate)(struct sort_context *ctx, struct sort_bucket *b);
56
57   // Two-way split/merge: merge up to 2 source buckets to up to 2 destination buckets.
58   // Bucket arrays are NULL-terminated.
59   void (*twoway_merge)(struct sort_context *ctx, struct sort_bucket **ins, struct sort_bucket **outs);
60
61   // Multi-way merge: merge an arbitrary number of source buckets to a single destination bucket.
62   void (*multiway_merge)(struct sort_context *ctx, struct sort_bucket **ins, struct sort_bucket *out);
63
64   // Radix split according to hash function
65   void (*radix_split)(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket **outs, uns bitpos, uns numbits);
66
67   // State variables of internal_sort
68   void *key_buf;
69   int more_keys;
70
71   // Timing
72   timestamp_t start_time;
73   uns last_pass_time;
74   uns total_int_time, total_pre_time, total_ext_time;
75 };
76
77 void sorter_run(struct sort_context *ctx);
78
79 /* Buffers */
80
81 void *sorter_alloc(struct sort_context *ctx, uns size);
82 void sorter_prepare_buf(struct sort_context *ctx);
83 void sorter_alloc_buf(struct sort_context *ctx);
84 void sorter_free_buf(struct sort_context *ctx);
85
86 /* Buckets */
87
88 struct sort_bucket {
89   cnode n;
90   struct sort_context *ctx;
91   uns flags;
92   struct fastbuf *fb;
93   byte *filename;
94   u64 size;                             // Size in bytes (not valid when writing)
95   uns runs;                             // Number of runs, 0 if not sorted
96   uns hash_bits;                        // Remaining bits of the hash function
97   byte *ident;                          // Identifier used in debug messages
98 };
99
100 enum sort_bucket_flags {
101   SBF_FINAL = 1,                        // This bucket corresponds to the final output file (always 1 run)
102   SBF_SOURCE = 2,                       // Contains the source file (always 0 runs)
103   SBF_CUSTOM_PRESORT = 4,               // Contains source to read via custom presorter
104   SBF_OPEN_WRITE = 256,                 // We are currently writing to the fastbuf
105   SBF_OPEN_READ = 512,                  // We are reading from the fastbuf
106   SBF_DESTROYED = 1024,                 // Already done with, no further references allowed
107   SBF_SWAPPED_OUT = 2048,               // Swapped out to a named file
108 };
109
110 struct sort_bucket *sbuck_new(struct sort_context *ctx);
111 void sbuck_drop(struct sort_bucket *b);
112 int sbuck_have(struct sort_bucket *b);
113 int sbuck_has_file(struct sort_bucket *b);
114 sh_off_t sbuck_size(struct sort_bucket *b);
115 struct fastbuf *sbuck_read(struct sort_bucket *b);
116 struct fastbuf *sbuck_write(struct sort_bucket *b);
117 void sbuck_swap_out(struct sort_bucket *b);
118
119 /* Contexts and helper functions for the array sorter */
120
121 struct asort_context {
122   void *array;                          // Array to sort
123   void *buffer;                         // Auxiliary buffer (required when radix-sorting)
124   uns num_elts;                         // Number of elements in the array
125   uns elt_size;                         // Bytes per element
126   uns hash_bits;                        // Remaining bits of hash function
127   uns radix_bits;                       // How many bits to process in a single radix-sort pass
128   void (*quicksort)(void *array_ptr, uns num_elts);
129   void (*quicksplit)(void *array_ptr, uns num_elts, uns *leftp, uns *rightp);
130   void (*radix_count)(void *src_ptr, uns num_elts, uns *cnt, uns shift);
131   void (*radix_split)(void *src_ptr, void *dest_ptr, uns num_elts, uns *ptrs, uns shift);
132 };
133
134 void asort_run(struct asort_context *ctx);
135
136 #endif