Debian Codegraph @

bcftools/extsort.c.pysam.c

raw symbols SCIP
1 #include "bcftools.pysam.h"
2
3 /* ext-sort.h -- sort on disk
4
5 Copyright (C) 2020-2021 Genome Research Ltd.
6
7 Author: Petr Danecek <[email protected]>
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26
27 */
28
29 #include <stdio.h>
30 #include <unistd.h> // for unlink()
31 #include <sys/stat.h> // for chmod()
32 #include <assert.h>
33 #include <fcntl.h>
34 #ifdef _WIN32
35 #include <windows.h>
36 #endif
37 #include "bcftools.h"
38 #include "extsort.h"
39 #include "kheap.h"
40
41 typedef struct
42 {
43 extsort_t *es; // this is to get access to extsort_cmp_f from kheap
44 int fd;
45 char *fname;
46 void *dat;
47 }
48 blk_t;
49
50 static inline int blk_is_smaller(blk_t **aptr, blk_t **bptr);
51 KHEAP_INIT(blk, blk_t*, blk_is_smaller) /* defines khp_blk_t */
52
53 struct _extsort_t
54 {
55 size_t dat_size, mem, max_mem;
56 char *tmp_prefix;
57 extsort_cmp_f cmp;
58
59 size_t nbuf, mbuf, nblk;
60 blk_t **blk;
61 void **buf, *tmp_dat;
62 khp_blk_t *bhp;
63 };
64
65 static inline int blk_is_smaller(blk_t **aptr, blk_t **bptr)
66 {
67 blk_t *a = *aptr;
68 blk_t *b = *bptr;
69 int ret = a->es->cmp(&a->dat,&b->dat);
70 if ( ret < 0 ) return 1;
71 return 0;
72 }
73
74 size_t parse_mem_string(const char *str);
75
76 void extsort_set(extsort_t *es, extsort_opt_t key, void *value)
77 {
78 if ( key==DAT_SIZE ) { es->dat_size = *((size_t*)value); return; }
79 if ( key==MAX_MEM )
80 {
81 es->max_mem = parse_mem_string(*((const char**)value));
82 if ( es->max_mem <=0 ) error("Could not parse the memory string, expected positive number: %s\n",*((const char**)value));
83 return;
84 }
85 if ( key==TMP_PREFIX ) { es->tmp_prefix = init_tmp_prefix(*((const char**)value)); return; }
86 if ( key==FUNC_CMP ) { es->cmp = *((extsort_cmp_f*)value); return; }
87 }
88
89 extsort_t *extsort_alloc(void)
90 {
91 extsort_t *es = (extsort_t*) calloc(1,sizeof(*es));
92 es->max_mem = 100e6;
93 return es;
94 }
95 void extsort_init(extsort_t *es)
96 {
97 assert( es->cmp );
98 assert( es->dat_size );
99 if ( !es->tmp_prefix ) es->tmp_prefix = init_tmp_prefix(NULL);
100 es->tmp_dat = malloc(es->dat_size);
101 }
102
103 void extsort_destroy(extsort_t *es)
104 {
105 int i;
106 for (i=0; i<es->nblk; i++)
107 {
108 blk_t *blk = es->blk[i];
109 if ( blk->fd!=-1 )
110 #ifdef _WIN32
111 _close(blk->fd);
112 #else
113 close(blk->fd);
114 #endif
115 free(blk->fname);
116 free(blk->dat);
117 free(blk);
118 }
119 free(es->tmp_dat);
120 free(es->tmp_prefix);
121 free(es->blk);
122 khp_destroy(blk, es->bhp);
123 free(es);
124 }
125
126 static void _buf_flush(extsort_t *es)
127 {
128 int i;
129 if ( !es->nbuf ) return;
130
131 qsort(es->buf, es->nbuf, sizeof(void*), es->cmp);
132
133 es->nblk++;
134 es->blk = (blk_t**) realloc(es->blk, sizeof(blk_t*)*es->nblk);
135 es->blk[es->nblk-1] = (blk_t*) calloc(1,sizeof(blk_t));
136 blk_t *blk = es->blk[es->nblk-1];
137 blk->es = es;
138 blk->dat = malloc(es->dat_size);
139 blk->fname = strdup(es->tmp_prefix);
140 #ifdef _WIN32
141 for (i=0; i<100000; i++)
142 {
143 memcpy(blk->fname,es->tmp_prefix,strlen(es->tmp_prefix));
144 mktemp(blk->fname);
145 blk->fd = _open(blk->fname, O_RDWR|O_CREAT|O_EXCL|O_BINARY|O_TEMPORARY, 0600);
146 if ( blk->fd==-1 )
147 {
148 if ( errno==EEXIST ) continue;
149 error("Error: failed to open a temporary file %s\n",blk->fname);
150 }
151 break;
152 }
153 if ( !blk->fd ) error("Error: failed to create a unique temporary file name from %s\n",es->tmp_prefix);
154 if ( _chmod(blk->fname, S_IRUSR|S_IWUSR)!=0 ) error("Error: failed to set permissions of the temporary file %s\n",blk->fname);
155 #else
156 if ( (blk->fd = mkstemp(blk->fname))==-1 )
157 error("Error: failed to open a temporary file %s\n",blk->fname);
158 if ( fchmod(blk->fd,S_IRUSR|S_IWUSR)!=0 ) error("Error: failed to set permissions of the temporary file %s\n",blk->fname);
159 unlink(blk->fname); // should auto delete when closed on linux, the descriptor remains open
160 #endif
161
162 for (i=0; i<es->nbuf; i++)
163 {
164 #ifdef _WIN32
165 if ( _write(blk->fd, es->buf[i], es->dat_size)!=es->dat_size ) error("Error: failed to write %zu bytes to the temporary file %s\n",es->dat_size,blk->fname);
166 #else
167 if ( write(blk->fd, es->buf[i], es->dat_size)!=es->dat_size ) error("Error: failed to write %zu bytes to the temporary file %s\n",es->dat_size,blk->fname);
168 #endif
169 free(es->buf[i]);
170 }
171 #ifdef _WIN32
172 if ( _lseek(blk->fd,0,SEEK_SET)!=0 ) error("Error: failed to lseek() to the start of the temporary file %s\n", blk->fname);
173 #else
174 if ( lseek(blk->fd,0,SEEK_SET)!=0 ) error("Error: failed to lseek() to the start of the temporary file %s\n", blk->fname);
175 #endif
176
177 es->nbuf = 0;
178 es->mem = 0;
179 }
180
181 void extsort_push(extsort_t *es, void *dat)
182 {
183 int delta = sizeof(void*) + es->dat_size;
184 if ( es->nbuf && es->mem + delta > es->max_mem ) _buf_flush(es);
185 es->nbuf++;
186 es->mem += delta;
187 hts_expand(void*, es->nbuf, es->mbuf, es->buf);
188 es->buf[es->nbuf-1] = dat;
189 }
190
191 // return number of elements read
192 static ssize_t _blk_read(extsort_t *es, blk_t *blk)
193 {
194 ssize_t ret = 0;
195 if ( blk->fd==-1 ) return ret;
196 #ifdef _WIN32
197 ret = _read(blk->fd, blk->dat, es->dat_size);
198 #else
199 ret = read(blk->fd, blk->dat, es->dat_size);
200 #endif
201 if ( ret < 0 ) error("Error: failed to read from the temporary file %s\n", blk->fname);
202 if ( ret == 0 )
203 {
204 #ifdef _WIN32
205 if ( _close(blk->fd)!=0 ) error("Error: failed to close the temporary file %s\n", blk->fname);
206 #else
207 if ( close(blk->fd)!=0 ) error("Error: failed to close the temporary file %s\n", blk->fname);
208 #endif
209 blk->fd = -1;
210 return ret;
211 }
212 if ( ret < es->dat_size ) error("Error: failed to read %zu bytes from the temporary file %s\n",es->dat_size,blk->fname);
213 return ret;
214 }
215
216 void extsort_sort(extsort_t *es)
217 {
218 _buf_flush(es);
219 free(es->buf);
220 es->buf = NULL;
221 es->bhp = khp_init(blk);
222
223 // open all blocks, read one record from each, create a heap
224 int i;
225 for (i=0; i<es->nblk; i++)
226 {
227 blk_t *blk = es->blk[i];
228 #ifdef _WIN32
229 if ( _lseek(blk->fd,0,SEEK_SET)!=0 ) error("Error: failed to lseek() to the start of the temporary file %s\n", blk->fname);
230 #else
231 if ( lseek(blk->fd,0,SEEK_SET)!=0 ) error("Error: failed to lseek() to the start of the temporary file %s\n", blk->fname);
232 #endif
233 int ret = _blk_read(es, blk);
234 if ( ret ) khp_insert(blk, es->bhp, &blk);
235 }
236 }
237
238 void *extsort_shift(extsort_t *es)
239 {
240 if ( !es->bhp->ndat ) return NULL;
241 blk_t *blk = es->bhp->dat[0];
242
243 // swap the pointer which keeps the location of user data so that it is not overwritten by the next read
244 void *tmp = es->tmp_dat; es->tmp_dat = blk->dat; blk->dat = tmp;
245 khp_delete(blk, es->bhp);
246
247 int ret = _blk_read(es, blk);
248 if ( ret ) khp_insert(blk, es->bhp, &blk);
249
250 return es->tmp_dat;
251 }
252