| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <assert.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#ifdef _WIN32 |
| 33 |
#include <windows.h> |
| 34 |
#endif |
| 35 |
#include "bcftools.h" |
| 36 |
#include "extsort.h" |
| 37 |
#include "kheap.h" |
| 38 |
|
| 39 |
typedef struct |
| 40 |
{ |
| 41 |
extsort_t *es; |
| 42 |
int fd; |
| 43 |
char *fname; |
| 44 |
void *dat; |
| 45 |
} |
| 46 |
blk_t; |
| 47 |
|
| 48 |
static inline int blk_is_smaller(blk_t **aptr, blk_t **bptr); |
| 49 |
KHEAP_INIT(blk, blk_t*, blk_is_smaller) |
| 50 |
|
| 51 |
struct _extsort_t |
| 52 |
{ |
| 53 |
size_t dat_size, mem, max_mem; |
| 54 |
char *tmp_prefix; |
| 55 |
extsort_cmp_f cmp; |
| 56 |
|
| 57 |
size_t nbuf, mbuf, nblk; |
| 58 |
blk_t **blk; |
| 59 |
void **buf, *tmp_dat; |
| 60 |
khp_blk_t *bhp; |
| 61 |
}; |
| 62 |
|
| 63 |
static inline int blk_is_smaller(blk_t **aptr, blk_t **bptr) |
| 64 |
{ |
| 65 |
blk_t *a = *aptr; |
| 66 |
blk_t *b = *bptr; |
| 67 |
int ret = a->es->cmp(&a->dat,&b->dat); |
| 68 |
if ( ret < 0 ) return 1; |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
size_t parse_mem_string(const char *str); |
| 73 |
|
| 74 |
void extsort_set(extsort_t *es, extsort_opt_t key, void *value) |
| 75 |
{ |
| 76 |
if ( key==DAT_SIZE ) { es->dat_size = *((size_t*)value); return; } |
| 77 |
if ( key==MAX_MEM ) |
| 78 |
{ |
| 79 |
es->max_mem = parse_mem_string(*((const char**)value)); |
| 80 |
if ( es->max_mem <=0 ) error("Could not parse the memory string, expected positive number: %s\n",*((const char**)value)); |
| 81 |
return; |
| 82 |
} |
| 83 |
if ( key==TMP_PREFIX ) { es->tmp_prefix = init_tmp_prefix(*((const char**)value)); return; } |
| 84 |
if ( key==FUNC_CMP ) { es->cmp = *((extsort_cmp_f*)value); return; } |
| 85 |
} |
| 86 |
|
| 87 |
extsort_t *extsort_alloc(void) |
| 88 |
{ |
| 89 |
extsort_t *es = (extsort_t*) calloc(1,sizeof(*es)); |
| 90 |
es->max_mem = 100e6; |
| 91 |
return es; |
| 92 |
} |
| 93 |
void extsort_init(extsort_t *es) |
| 94 |
{ |
| 95 |
assert( es->cmp ); |
| 96 |
assert( es->dat_size ); |
| 97 |
if ( !es->tmp_prefix ) es->tmp_prefix = init_tmp_prefix(NULL); |
| 98 |
es->tmp_dat = malloc(es->dat_size); |
| 99 |
} |
| 100 |
|
| 101 |
void extsort_destroy(extsort_t *es) |
| 102 |
{ |
| 103 |
int i; |
| 104 |
for (i=0; i<es->nblk; i++) |
| 105 |
{ |
| 106 |
blk_t *blk = es->blk[i]; |
| 107 |
if ( blk->fd!=-1 ) |
| 108 |
#ifdef _WIN32 |
| 109 |
_close(blk->fd); |
| 110 |
#else |
| 111 |
close(blk->fd); |
| 112 |
#endif |
| 113 |
free(blk->fname); |
| 114 |
free(blk->dat); |
| 115 |
free(blk); |
| 116 |
} |
| 117 |
free(es->tmp_dat); |
| 118 |
free(es->tmp_prefix); |
| 119 |
free(es->blk); |
| 120 |
khp_destroy(blk, es->bhp); |
| 121 |
free(es); |
| 122 |
} |
| 123 |
|
| 124 |
static void _buf_flush(extsort_t *es) |
| 125 |
{ |
| 126 |
int i; |
| 127 |
if ( !es->nbuf ) return; |
| 128 |
|
| 129 |
qsort(es->buf, es->nbuf, sizeof(void*), es->cmp); |
| 130 |
|
| 131 |
es->nblk++; |
| 132 |
es->blk = (blk_t**) realloc(es->blk, sizeof(blk_t*)*es->nblk); |
| 133 |
es->blk[es->nblk-1] = (blk_t*) calloc(1,sizeof(blk_t)); |
| 134 |
blk_t *blk = es->blk[es->nblk-1]; |
| 135 |
blk->es = es; |
| 136 |
blk->dat = malloc(es->dat_size); |
| 137 |
blk->fname = strdup(es->tmp_prefix); |
| 138 |
#ifdef _WIN32 |
| 139 |
for (i=0; i<100000; i++) |
| 140 |
{ |
| 141 |
memcpy(blk->fname,es->tmp_prefix,strlen(es->tmp_prefix)); |
| 142 |
mktemp(blk->fname); |
| 143 |
blk->fd = _open(blk->fname, O_RDWR|O_CREAT|O_EXCL|O_BINARY|O_TEMPORARY, 0600); |
| 144 |
if ( blk->fd==-1 ) |
| 145 |
{ |
| 146 |
if ( errno==EEXIST ) continue; |
| 147 |
error("Error: failed to open a temporary file %s\n",blk->fname); |
| 148 |
} |
| 149 |
break; |
| 150 |
} |
| 151 |
if ( !blk->fd ) error("Error: failed to create a unique temporary file name from %s\n",es->tmp_prefix); |
| 152 |
if ( _chmod(blk->fname, S_IRUSR|S_IWUSR)!=0 ) error("Error: failed to set permissions of the temporary file %s\n",blk->fname); |
| 153 |
#else |
| 154 |
if ( (blk->fd = mkstemp(blk->fname))==-1 ) |
| 155 |
error("Error: failed to open a temporary file %s\n",blk->fname); |
| 156 |
if ( fchmod(blk->fd,S_IRUSR|S_IWUSR)!=0 ) error("Error: failed to set permissions of the temporary file %s\n",blk->fname); |
| 157 |
unlink(blk->fname); |
| 158 |
#endif |
| 159 |
|
| 160 |
for (i=0; i<es->nbuf; i++) |
| 161 |
{ |
| 162 |
#ifdef _WIN32 |
| 163 |
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); |
| 164 |
#else |
| 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 |
#endif |
| 167 |
free(es->buf[i]); |
| 168 |
} |
| 169 |
#ifdef _WIN32 |
| 170 |
if ( _lseek(blk->fd,0,SEEK_SET)!=0 ) error("Error: failed to lseek() to the start of the temporary file %s\n", blk->fname); |
| 171 |
#else |
| 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 |
#endif |
| 174 |
|
| 175 |
es->nbuf = 0; |
| 176 |
es->mem = 0; |
| 177 |
} |
| 178 |
|
| 179 |
void extsort_push(extsort_t *es, void *dat) |
| 180 |
{ |
| 181 |
int delta = sizeof(void*) + es->dat_size; |
| 182 |
if ( es->nbuf && es->mem + delta > es->max_mem ) _buf_flush(es); |
| 183 |
es->nbuf++; |
| 184 |
es->mem += delta; |
| 185 |
hts_expand(void*, es->nbuf, es->mbuf, es->buf); |
| 186 |
es->buf[es->nbuf-1] = dat; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
static ssize_t _blk_read(extsort_t *es, blk_t *blk) |
| 191 |
{ |
| 192 |
ssize_t ret = 0; |
| 193 |
if ( blk->fd==-1 ) return ret; |
| 194 |
#ifdef _WIN32 |
| 195 |
ret = _read(blk->fd, blk->dat, es->dat_size); |
| 196 |
#else |
| 197 |
ret = read(blk->fd, blk->dat, es->dat_size); |
| 198 |
#endif |
| 199 |
if ( ret < 0 ) error("Error: failed to read from the temporary file %s\n", blk->fname); |
| 200 |
if ( ret == 0 ) |
| 201 |
{ |
| 202 |
#ifdef _WIN32 |
| 203 |
if ( _close(blk->fd)!=0 ) error("Error: failed to close the temporary file %s\n", blk->fname); |
| 204 |
#else |
| 205 |
if ( close(blk->fd)!=0 ) error("Error: failed to close the temporary file %s\n", blk->fname); |
| 206 |
#endif |
| 207 |
blk->fd = -1; |
| 208 |
return ret; |
| 209 |
} |
| 210 |
if ( ret < es->dat_size ) error("Error: failed to read %zu bytes from the temporary file %s\n",es->dat_size,blk->fname); |
| 211 |
return ret; |
| 212 |
} |
| 213 |
|
| 214 |
void extsort_sort(extsort_t *es) |
| 215 |
{ |
| 216 |
_buf_flush(es); |
| 217 |
free(es->buf); |
| 218 |
es->buf = NULL; |
| 219 |
es->bhp = khp_init(blk); |
| 220 |
|
| 221 |
|
| 222 |
int i; |
| 223 |
for (i=0; i<es->nblk; i++) |
| 224 |
{ |
| 225 |
blk_t *blk = es->blk[i]; |
| 226 |
#ifdef _WIN32 |
| 227 |
if ( _lseek(blk->fd,0,SEEK_SET)!=0 ) error("Error: failed to lseek() to the start of the temporary file %s\n", blk->fname); |
| 228 |
#else |
| 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 |
#endif |
| 231 |
int ret = _blk_read(es, blk); |
| 232 |
if ( ret ) khp_insert(blk, es->bhp, &blk); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
void *extsort_shift(extsort_t *es) |
| 237 |
{ |
| 238 |
if ( !es->bhp->ndat ) return NULL; |
| 239 |
blk_t *blk = es->bhp->dat[0]; |
| 240 |
|
| 241 |
|
| 242 |
void *tmp = es->tmp_dat; es->tmp_dat = blk->dat; blk->dat = tmp; |
| 243 |
khp_delete(blk, es->bhp); |
| 244 |
|
| 245 |
int ret = _blk_read(es, blk); |
| 246 |
if ( ret ) khp_insert(blk, es->bhp, &blk); |
| 247 |
|
| 248 |
return es->tmp_dat; |
| 249 |
} |
| 250 |
|