]> mj.ucw.cz Git - libucw.git/blob - ucw/sorter/sbuck.c
Merge branch 'master' into dev-sizet
[libucw.git] / ucw / sorter / sbuck.c
1 /*
2  *      UCW Library -- Universal Sorter: Operations on Contexts, Buffers and Buckets
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 #include <ucw/lib.h>
11 #include <ucw/fastbuf.h>
12 #include <ucw/mempool.h>
13 #include <ucw/stkstring.h>
14 #include <ucw/sorter/common.h>
15
16 #include <fcntl.h>
17
18 void *
19 sorter_alloc(struct sort_context *ctx, uint size)
20 {
21   return mp_alloc_zero(ctx->pool, size);
22 }
23
24 struct sort_bucket *
25 sbuck_new(struct sort_context *ctx)
26 {
27   struct sort_bucket *b = sorter_alloc(ctx, sizeof(struct sort_bucket));
28   b->ctx = ctx;
29   return b;
30 }
31
32 void
33 sbuck_drop(struct sort_bucket *b)
34 {
35   if (b)
36     {
37       ASSERT(!(b->flags & SBF_DESTROYED));
38       if (b->n.prev)
39         clist_remove(&b->n);
40       bclose(b->fb);
41       bzero(b, sizeof(*b));
42       b->flags = SBF_DESTROYED;
43     }
44 }
45
46 ucw_off_t
47 sbuck_size(struct sort_bucket *b)
48 {
49   if ((b->flags & SBF_OPEN_WRITE) && !(b->flags & SBF_SWAPPED_OUT))
50     return btell(b->fb);
51   else
52     return b->size;
53 }
54
55 int
56 sbuck_have(struct sort_bucket *b)
57 {
58   return b && sbuck_size(b);
59 }
60
61 int
62 sbuck_has_file(struct sort_bucket *b)
63 {
64   return (b->fb || (b->flags & SBF_SWAPPED_OUT));
65 }
66
67 static void
68 sbuck_swap_in(struct sort_bucket *b)
69 {
70   if (b->flags & SBF_SWAPPED_OUT)
71     {
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);
79     }
80 }
81
82 struct fastbuf *
83 sbuck_read(struct sort_bucket *b)
84 {
85   sbuck_swap_in(b);
86   if (b->flags & SBF_OPEN_READ)
87     return b->fb;
88   else if (b->flags & SBF_OPEN_WRITE)
89     {
90       b->size = btell(b->fb);
91       b->flags = (b->flags & ~SBF_OPEN_WRITE) | SBF_OPEN_READ;
92       brewind(b->fb);
93       return b->fb;
94     }
95   else
96     ASSERT(0);
97 }
98
99 struct fastbuf *
100 sbuck_write(struct sort_bucket *b)
101 {
102   sbuck_swap_in(b);
103   if (b->flags & SBF_OPEN_WRITE)
104     ASSERT(b->fb);
105   else
106     {
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);
113     }
114   return b->fb;
115 }
116
117 void
118 sbuck_swap_out(struct sort_bucket *b)
119 {
120   if ((b->flags & (SBF_OPEN_READ | SBF_OPEN_WRITE)) && b->fb && !(b->flags & SBF_SOURCE))
121     {
122       if (b->flags & SBF_OPEN_WRITE)
123         b->size = btell(b->fb);
124       bconfig(b->fb, BCONFIG_IS_TEMP_FILE, 0);
125       bclose(b->fb);
126       b->fb = NULL;
127       b->flags |= SBF_SWAPPED_OUT;
128       SORT_XTRACE(3, "Swapped out %s", b->filename);
129     }
130 }
131
132 void
133 sorter_prepare_buf(struct sort_context *ctx)
134 {
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;
139 }
140
141 void
142 sorter_alloc_buf(struct sort_context *ctx)
143 {
144   if (ctx->big_buf)
145     return;
146   ctx->big_buf = big_alloc(ctx->big_buf_size);
147   SORT_XTRACE(3, "Allocated sorting buffer (%s)", stk_fsize(ctx->big_buf_size));
148 }
149
150 void
151 sorter_free_buf(struct sort_context *ctx)
152 {
153   if (!ctx->big_buf)
154     return;
155   big_free(ctx->big_buf, ctx->big_buf_size);
156   ctx->big_buf = NULL;
157   SORT_XTRACE(3, "Freed sorting buffer");
158 }