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