2 * UCW Library -- Universal Sorter: Internal Sorting Module
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 // FIXME: Add the hash here to save cache misses
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"
22 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
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;
28 P(key) key, *keybuf = ctx->key_buf;
30 keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
36 else if (!P(read_key)(in, &key))
40 if (sizeof(key) + 1024 + SORT_DATA_SIZE(key) > ctx->big_buf_half_size)
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);
46 return 1; // We don't know, but 1 is always safe
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
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;
58 uns ksize = SORT_KEY_SIZE(key);
60 uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
62 uns ksize_aligned = ksize;
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)))
73 memcpy(end, &key, ksize);
75 breadb(in, end + ksize_aligned, dsize);
77 item->key = (P(key)*) end;
80 while (P(read_key)(in, &key));
83 uns count = last_item - item_array;
84 SORT_XTRACE("s-internal: Sorting %d items", count);
85 P(array_sort)(count, item_array);
87 SORT_XTRACE("s-internal: Writing");
90 struct fastbuf *out = sbuck_open_write(bout);
92 /* FIXME: No unification done yet */
93 for (item = item_array; item < last_item; item++)
95 P(write_key)(out, item->key);
97 uns ksize = SORT_KEY_SIZE(*item->key);
99 ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
101 bwrite(out, (byte *) item->key + ksize, SORT_DATA_SIZE(*item->key));
105 return ctx->more_keys;