]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/s-internal.h
Empty final bucket should be turned into a file as well.
[libucw.git] / lib / sorter / s-internal.h
1 /*
2  *      UCW Library -- Universal Sorter: Internal Sorting Module
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 typedef struct {
11   P(key) *key;
12   // FIXME: Add the hash here to save cache misses
13 } P(internal_item_t);
14
15 #define ASORT_PREFIX(x) SORT_PREFIX(array_##x)
16 #define ASORT_KEY_TYPE P(internal_item_t)
17 #define ASORT_ELT(i) ary[i]
18 #define ASORT_LT(x,y) (P(compare)((x).key, (y).key) < 0)
19 #define ASORT_EXTRA_ARGS , P(internal_item_t) *ary
20 #include "lib/arraysort.h"
21
22 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
23 {
24   sorter_alloc_buf(ctx);
25   struct fastbuf *in = sbuck_read(bin);
26
27   P(key) key, *keybuf = ctx->key_buf;
28   if (!keybuf)
29     keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
30   if (ctx->more_keys)
31     {
32       key = *keybuf;
33       ctx->more_keys = 0;
34     }
35   else if (!P(read_key)(in, &key))
36     return 0;
37
38 #ifdef SORT_VAR_DATA
39   if (sizeof(key) + 1024 + SORT_DATA_SIZE(key) > ctx->big_buf_half_size)
40     {
41       SORT_XTRACE("s-internal: Generating a giant run");
42       struct fastbuf *out = sorter_open_write(bout); /* FIXME: Using a non-direct buffer would be nice here */
43       P(copy_data)(&key, in, out);
44       bout->runs++;
45       return 1;                         // We don't know, but 1 is always safe
46     }
47 #endif
48
49   size_t bufsize = ctx->big_buf_half_size;      /* FIXME: In some cases, we can use the whole buffer */
50   bufsize = MIN((u64)bufsize, (u64)~0U * sizeof(P(internal_item_t)));   // The number of records must fit in uns
51
52   SORT_XTRACE("s-internal: Reading (bufsize=%zd)", bufsize);
53   P(internal_item_t) *item_array = ctx->big_buf, *item = item_array, *last_item;
54   byte *end = (byte *) ctx->big_buf + bufsize;
55   do
56     {
57       uns ksize = SORT_KEY_SIZE(key);
58 #ifdef SORT_UNIFY
59       uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
60 #else
61       uns ksize_aligned = ksize;
62 #endif
63       uns dsize = SORT_DATA_SIZE(key);
64       uns recsize = ALIGN_TO(ksize_aligned + dsize, CPU_STRUCT_ALIGN);
65       if (unlikely(sizeof(P(internal_item_t)) + recsize > (size_t)(end - (byte *) item)))
66         {
67           ctx->more_keys = 1;
68           *keybuf = key;
69           break;
70         }
71       end -= recsize;
72       memcpy(end, &key, ksize);
73 #ifdef SORT_VAR_DATA
74       breadb(in, end + ksize_aligned, dsize);
75 #endif
76       item->key = (P(key)*) end;
77       item++;
78     }
79   while (P(read_key)(in, &key));
80   last_item = item;
81
82   uns count = last_item - item_array;
83   SORT_XTRACE("s-internal: Sorting %d items", count);
84   P(array_sort)(count, item_array);
85
86   SORT_XTRACE("s-internal: Writing");
87   if (!ctx->more_keys)
88     bout = bout_only;
89   struct fastbuf *out = sbuck_write(bout);
90   bout->runs++;
91   /* FIXME: No unification done yet */
92   for (item = item_array; item < last_item; item++)
93     {
94       P(write_key)(out, item->key);
95 #ifdef SORT_VAR_DATA
96       uns ksize = SORT_KEY_SIZE(*item->key);
97 #ifdef SORT_UNIFY
98       ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
99 #endif
100       bwrite(out, (byte *) item->key + ksize, SORT_DATA_SIZE(*item->key));
101 #endif
102     }
103
104   return ctx->more_keys;
105 }