]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/common.h
15083663f3cb03aead02787bdba87d2b7ae4982b
[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 u64 sorter_bufsize;
18
19 #define SORT_TRACE(x...) do { if (sorter_trace) log(L_DEBUG, x); } while(0)
20 #define SORT_XTRACE(x...) do { if (sorter_trace > 1) log(L_DEBUG, x); } while(0)
21
22 struct sort_bucket {
23   cnode n;
24   uns flags;
25   struct fastbuf *fb;
26   byte *name;
27   u64 size;                             // Size in bytes
28   uns runs;                             // Number of runs, 0 if not sorted
29   uns hash_bits;                        // Remaining bits of the hash function
30   byte *ident;                          // Identifier used in debug messages
31 };
32
33 enum sort_bucket_flags {
34   SBF_FINAL = 1,                        // This bucket corresponds to the final output file (always 1 run)
35   SBF_SOURCE = 2,                       // Contains the source file (always 0 runs)
36   SBF_CUSTOM_PRESORT = 4,               // Contains source to read via custom presorter
37 };
38
39 struct sort_context {
40   struct fastbuf *in_fb;
41   struct fastbuf *out_fb;
42   uns hash_bits;
43
44   struct mempool *pool;
45   clist bucket_list;
46   void *big_buf, *big_buf_half;
47   size_t big_buf_size, big_buf_half_size;
48
49   int (*custom_presort)(struct fastbuf *dest, byte *buf, size_t bufsize);
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   // Two-way split/merge: merge up to 2 source buckets to up to 2 destination buckets.
54   // Bucket arrays are NULL-terminated.
55   void (*twoway_merge)(struct sort_context *ctx, struct sort_bucket **ins, struct sort_bucket **outs);
56
57   // State variables of internal_sort
58   void *key_buf;
59   int more_keys;
60 };
61
62 void sorter_run(struct sort_context *ctx);
63
64 void *sorter_alloc(struct sort_context *ctx, uns size);
65 void sorter_alloc_buf(struct sort_context *ctx);
66 void sorter_free_buf(struct sort_context *ctx);
67
68 // Operations on buckets
69 struct sort_bucket *sbuck_new(struct sort_context *ctx);
70 void sbuck_drop(struct sort_bucket *b);
71 int sbuck_can_read(struct sort_bucket *b);
72 struct fastbuf *sbuck_open_read(struct sort_bucket *b);
73 struct fastbuf *sbuck_open_write(struct sort_bucket *b);
74 void sbuck_close_read(struct sort_bucket *b);
75 void sbuck_close_write(struct sort_bucket *b);
76
77 #endif