]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/common.h
Fixed a bug.
[libucw.git] / lib / 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 u64 sorter_bufsize;
19 extern struct fb_params sorter_fb_params;
20
21 #define SORT_TRACE(x...) do { if (sorter_trace) msg(L_DEBUG, x); } while(0)
22 #define SORT_XTRACE(level, x...) do { if (sorter_trace >= level) msg(L_DEBUG, x); } while(0)
23
24 enum sort_debug {
25   SORT_DEBUG_NO_PRESORT = 1,
26   SORT_DEBUG_NO_JOIN = 2,
27   SORT_DEBUG_KEEP_BUCKETS = 4,
28   SORT_DEBUG_NO_RADIX = 8,
29   SORT_DEBUG_NO_MULTIWAY = 16,
30 };
31
32 struct sort_bucket;
33
34 struct sort_context {
35   struct fastbuf *in_fb;
36   struct fastbuf *out_fb;
37   uns hash_bits;
38   u64 in_size;
39
40   struct mempool *pool;
41   clist bucket_list;
42   void *big_buf;
43   size_t big_buf_size;
44
45   int (*custom_presort)(struct fastbuf *dest, void *buf, size_t bufsize);
46
47   // Take as much as possible from the source bucket, sort it in memory and dump to destination bucket.
48   // Return 1 if there is more data available in the source bucket.
49   int (*internal_sort)(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket *out, struct sort_bucket *out_only);
50
51   // Estimate how much input data from `b' will fit in the internal sorting buffer.
52   u64 (*internal_estimate)(struct sort_context *ctx, struct sort_bucket *b);
53
54   // Two-way split/merge: merge up to 2 source buckets to up to 2 destination buckets.
55   // Bucket arrays are NULL-terminated.
56   void (*twoway_merge)(struct sort_context *ctx, struct sort_bucket **ins, struct sort_bucket **outs);
57
58   // Multi-way merge: merge an arbitrary number of source buckets to a single destination bucket.
59   void (*multiway_merge)(struct sort_context *ctx, struct sort_bucket **ins, struct sort_bucket *out);
60
61   // Radix split according to hash function
62   void (*radix_split)(struct sort_context *ctx, struct sort_bucket *in, struct sort_bucket **outs, uns bitpos, uns numbits);
63
64   // State variables of internal_sort
65   void *key_buf;
66   int more_keys;
67
68   // Timing
69   timestamp_t start_time;
70   uns last_pass_time;
71   uns total_int_time, total_pre_time, total_ext_time;
72 };
73
74 void sorter_run(struct sort_context *ctx);
75
76 /* Buffers */
77
78 void *sorter_alloc(struct sort_context *ctx, uns size);
79 void sorter_prepare_buf(struct sort_context *ctx);
80 void sorter_alloc_buf(struct sort_context *ctx);
81 void sorter_free_buf(struct sort_context *ctx);
82
83 /* Buckets */
84
85 struct sort_bucket {
86   cnode n;
87   struct sort_context *ctx;
88   uns flags;
89   struct fastbuf *fb;
90   byte *filename;
91   u64 size;                             // Size in bytes (not valid when writing)
92   uns runs;                             // Number of runs, 0 if not sorted
93   uns hash_bits;                        // Remaining bits of the hash function
94   byte *ident;                          // Identifier used in debug messages
95 };
96
97 enum sort_bucket_flags {
98   SBF_FINAL = 1,                        // This bucket corresponds to the final output file (always 1 run)
99   SBF_SOURCE = 2,                       // Contains the source file (always 0 runs)
100   SBF_CUSTOM_PRESORT = 4,               // Contains source to read via custom presorter
101   SBF_OPEN_WRITE = 256,                 // We are currently writing to the fastbuf
102   SBF_OPEN_READ = 512,                  // We are reading from the fastbuf
103   SBF_DESTROYED = 1024,                 // Already done with, no further references allowed
104   SBF_SWAPPED_OUT = 2048,               // Swapped out to a named file
105 };
106
107 struct sort_bucket *sbuck_new(struct sort_context *ctx);
108 void sbuck_drop(struct sort_bucket *b);
109 int sbuck_have(struct sort_bucket *b);
110 int sbuck_has_file(struct sort_bucket *b);
111 sh_off_t sbuck_size(struct sort_bucket *b);
112 struct fastbuf *sbuck_read(struct sort_bucket *b);
113 struct fastbuf *sbuck_write(struct sort_bucket *b);
114 void sbuck_swap_out(struct sort_bucket *b);
115
116 #endif