2 * UCW Library -- Universal Sorter: Operations on Contexts, Buffers and Buckets
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.
11 #include "ucw/fastbuf.h"
12 #include "ucw/mempool.h"
13 #include "ucw/stkstring.h"
14 #include "ucw/sorter/common.h"
19 sorter_alloc(struct sort_context *ctx, uns size)
21 return mp_alloc_zero(ctx->pool, size);
25 sbuck_new(struct sort_context *ctx)
27 struct sort_bucket *b = sorter_alloc(ctx, sizeof(struct sort_bucket));
33 sbuck_drop(struct sort_bucket *b)
37 ASSERT(!(b->flags & SBF_DESTROYED));
42 b->flags = SBF_DESTROYED;
47 sbuck_size(struct sort_bucket *b)
49 if ((b->flags & SBF_OPEN_WRITE) && !(b->flags & SBF_SWAPPED_OUT))
56 sbuck_have(struct sort_bucket *b)
58 return b && sbuck_size(b);
62 sbuck_has_file(struct sort_bucket *b)
64 return (b->fb || (b->flags & SBF_SWAPPED_OUT));
68 sbuck_swap_in(struct sort_bucket *b)
70 if (b->flags & SBF_SWAPPED_OUT)
72 b->fb = bopen_file(b->filename, O_RDWR, b->ctx->fb_params);
73 if (b->flags & SBF_OPEN_WRITE)
74 bseek(b->fb, 0, SEEK_END);
75 if (!(sorter_debug & SORT_DEBUG_KEEP_BUCKETS))
76 bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 1);
77 b->flags &= ~SBF_SWAPPED_OUT;
78 SORT_XTRACE(3, "Swapped in %s", b->filename);
83 sbuck_read(struct sort_bucket *b)
86 if (b->flags & SBF_OPEN_READ)
88 else if (b->flags & SBF_OPEN_WRITE)
90 b->size = btell(b->fb);
91 b->flags = (b->flags & ~SBF_OPEN_WRITE) | SBF_OPEN_READ;
100 sbuck_write(struct sort_bucket *b)
103 if (b->flags & SBF_OPEN_WRITE)
107 ASSERT(!(b->flags & (SBF_OPEN_READ | SBF_DESTROYED)));
108 b->fb = bopen_tmp_file(b->ctx->fb_params);
109 if (sorter_debug & SORT_DEBUG_KEEP_BUCKETS)
110 bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 0);
111 b->flags |= SBF_OPEN_WRITE;
112 b->filename = mp_strdup(b->ctx->pool, b->fb->name);
118 sbuck_swap_out(struct sort_bucket *b)
120 if ((b->flags & (SBF_OPEN_READ | SBF_OPEN_WRITE)) && b->fb && !(b->flags & SBF_SOURCE))
122 if (b->flags & SBF_OPEN_WRITE)
123 b->size = btell(b->fb);
124 bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 0);
127 b->flags |= SBF_SWAPPED_OUT;
128 SORT_XTRACE(3, "Swapped out %s", b->filename);
133 sorter_prepare_buf(struct sort_context *ctx)
135 u64 bs = sorter_bufsize;
136 bs = ALIGN_TO(bs, (u64)CPU_PAGE_SIZE);
137 bs = MAX(bs, 2*(u64)CPU_PAGE_SIZE);
138 ctx->big_buf_size = bs;
142 sorter_alloc_buf(struct sort_context *ctx)
146 ctx->big_buf = big_alloc(ctx->big_buf_size);
147 SORT_XTRACE(3, "Allocated sorting buffer (%s)", stk_fsize(ctx->big_buf_size));
151 sorter_free_buf(struct sort_context *ctx)
155 big_free(ctx->big_buf, ctx->big_buf_size);
157 SORT_XTRACE(3, "Freed sorting buffer");