]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/s-internal.h
c05ca077e574868bb7e27cd94c85ddc6cf24ee10
[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   ASSERT(bin->fb);                      // Expects the input bucket to be already open for reading
26   struct fastbuf *in = bin->fb;
27
28   P(key) key, *keybuf = ctx->key_buf;
29   if (!keybuf)
30     keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
31   if (ctx->more_keys)
32     {
33       key = *keybuf;
34       ctx->more_keys = 0;
35     }
36   else if (!P(read_key)(in, &key))
37     return 0;
38
39 #ifdef SORT_VAR_DATA
40   if (sizeof(key) + 1024 + SORT_DATA_SIZE(key) > ctx->big_buf_half_size)
41     {
42       SORT_XTRACE("s-internal: Generating a giant run");
43       struct fastbuf *out = sorter_open_write(bout); /* FIXME: Using a non-direct buffer would be nice here */
44       P(copy_data)(&key, in, out);
45       bout->runs++;
46       return 1;                         // We don't know, but 1 is always safe
47     }
48 #endif
49
50   size_t bufsize = ctx->big_buf_half_size;      /* FIXME: In some cases, we can use the whole buffer */
51   bufsize = MIN((u64)bufsize, (u64)~0U * sizeof(P(internal_item_t)));   // The number of records must fit in uns
52
53   SORT_XTRACE("s-internal: Reading (bufsize=%zd)", bufsize);
54   P(internal_item_t) *item_array = ctx->big_buf, *item = item_array, *last_item;
55   byte *end = (byte *) ctx->big_buf + bufsize;
56   do
57     {
58       uns ksize = SORT_KEY_SIZE(key);
59 #ifdef SORT_UNIFY
60       uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
61 #else
62       uns ksize_aligned = ksize;
63 #endif
64       uns dsize = SORT_DATA_SIZE(key);
65       uns recsize = ALIGN_TO(ksize_aligned + dsize, CPU_STRUCT_ALIGN);
66       if (unlikely(sizeof(P(internal_item_t)) + recsize > (size_t)(end - (byte *) item)))
67         {
68           ctx->more_keys = 1;
69           *keybuf = key;
70           break;
71         }
72       end -= recsize;
73       memcpy(end, &key, ksize);
74 #ifdef SORT_VAR_DATA
75       breadb(in, end + ksize_aligned, dsize);
76 #endif
77       item->key = (P(key)*) end;
78       item++;
79     }
80   while (P(read_key)(in, &key));
81   last_item = item;
82
83   uns count = last_item - item_array;
84   SORT_XTRACE("s-internal: Sorting %d items", count);
85   P(array_sort)(count, item_array);
86
87   SORT_XTRACE("s-internal: Writing");
88   if (!ctx->more_keys)
89     bout = bout_only;
90   struct fastbuf *out = sbuck_write(bout);
91   bout->runs++;
92   /* FIXME: No unification done yet */
93   for (item = item_array; item < last_item; item++)
94     {
95       P(write_key)(out, item->key);
96 #ifdef SORT_VAR_DATA
97       uns ksize = SORT_KEY_SIZE(*item->key);
98 #ifdef SORT_UNIFY
99       ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
100 #endif
101       bwrite(out, (byte *) item->key + ksize, SORT_DATA_SIZE(*item->key));
102 #endif
103     }
104
105   return ctx->more_keys;
106 }