| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <esl_config.h> |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include "easel.h" |
| 17 |
#include "esl_ssi.h" |
| 18 |
|
| 19 |
static uint32_t v30magic = 0xd3d3c9b3; |
| 20 |
static uint32_t v30swap = 0xb3c9d3d3; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
static int binary_search(ESL_SSI *ssi, const char *key, uint32_t klen, off_t base, |
| 28 |
uint32_t recsize, uint64_t maxidx); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
int |
| 51 |
esl_ssi_Open(const char *filename, ESL_SSI **ret_ssi) |
| 52 |
{ |
| 53 |
ESL_SSI *ssi = NULL; |
| 54 |
int status; |
| 55 |
uint32_t magic; |
| 56 |
uint16_t i; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
ESL_ALLOC(ssi, sizeof(ESL_SSI)); |
| 61 |
ssi->fp = NULL; |
| 62 |
ssi->filename = NULL; |
| 63 |
ssi->fileformat = NULL; |
| 64 |
ssi->fileflags = NULL; |
| 65 |
ssi->bpl = NULL; |
| 66 |
ssi->rpl = NULL; |
| 67 |
ssi->nfiles = 0; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
status = eslENOTFOUND; |
| 72 |
if ((ssi->fp = fopen(filename, "rb")) == NULL) goto ERROR; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
status = eslEFORMAT; |
| 78 |
if (esl_fread_u32(ssi->fp, &magic) != eslOK) goto ERROR; |
| 79 |
if (magic != v30magic && magic != v30swap) goto ERROR; |
| 80 |
if (esl_fread_u32(ssi->fp, &(ssi->flags)) != eslOK) goto ERROR; |
| 81 |
if (esl_fread_u32(ssi->fp, &(ssi->offsz)) != eslOK) goto ERROR; |
| 82 |
|
| 83 |
status = eslERANGE; |
| 84 |
if (ssi->offsz != 4 && ssi->offsz != 8) goto ERROR; |
| 85 |
if (ssi->offsz > sizeof(off_t)) goto ERROR; |
| 86 |
|
| 87 |
|
| 88 |
status = eslEFORMAT; |
| 89 |
if (esl_fread_u16(ssi->fp, &(ssi->nfiles)) != eslOK) goto ERROR; |
| 90 |
if (esl_fread_u64(ssi->fp, &(ssi->nprimary)) != eslOK) goto ERROR; |
| 91 |
if (esl_fread_u64(ssi->fp, &(ssi->nsecondary)) != eslOK) goto ERROR; |
| 92 |
if (esl_fread_u32(ssi->fp, &(ssi->flen)) != eslOK) goto ERROR; |
| 93 |
if (esl_fread_u32(ssi->fp, &(ssi->plen)) != eslOK) goto ERROR; |
| 94 |
if (esl_fread_u32(ssi->fp, &(ssi->slen)) != eslOK) goto ERROR; |
| 95 |
if (esl_fread_u32(ssi->fp, &(ssi->frecsize)) != eslOK) goto ERROR; |
| 96 |
if (esl_fread_u32(ssi->fp, &(ssi->precsize)) != eslOK) goto ERROR; |
| 97 |
if (esl_fread_u32(ssi->fp, &(ssi->srecsize)) != eslOK) goto ERROR; |
| 98 |
|
| 99 |
if (esl_fread_offset(ssi->fp, ssi->offsz, &(ssi->foffset)) != eslOK) goto ERROR; |
| 100 |
if (esl_fread_offset(ssi->fp, ssi->offsz, &(ssi->poffset)) != eslOK) goto ERROR; |
| 101 |
if (esl_fread_offset(ssi->fp, ssi->offsz, &(ssi->soffset)) != eslOK) goto ERROR; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
status = eslEFORMAT; |
| 110 |
if (ssi->nfiles == 0) goto ERROR; |
| 111 |
|
| 112 |
ESL_ALLOC(ssi->filename, sizeof(char *) * ssi->nfiles); |
| 113 |
for (i = 0; i < ssi->nfiles; i++) ssi->filename[i] = NULL; |
| 114 |
ESL_ALLOC(ssi->fileformat, sizeof(uint32_t) * ssi->nfiles); |
| 115 |
ESL_ALLOC(ssi->fileflags, sizeof(uint32_t) * ssi->nfiles); |
| 116 |
ESL_ALLOC(ssi->bpl, sizeof(uint32_t) * ssi->nfiles); |
| 117 |
ESL_ALLOC(ssi->rpl, sizeof(uint32_t) * ssi->nfiles); |
| 118 |
|
| 119 |
|
| 120 |
for (i = 0; i < ssi->nfiles; i++) |
| 121 |
{ |
| 122 |
ESL_ALLOC(ssi->filename[i], sizeof(char)* ssi->flen); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
status = eslEFORMAT; |
| 128 |
if (fseeko(ssi->fp, ssi->foffset + (i * ssi->frecsize), SEEK_SET) != 0) goto ERROR; |
| 129 |
if (fread(ssi->filename[i],sizeof(char),ssi->flen, ssi->fp)!=ssi->flen) goto ERROR; |
| 130 |
if (esl_fread_u32(ssi->fp, &(ssi->fileformat[i]))) goto ERROR; |
| 131 |
if (esl_fread_u32(ssi->fp, &(ssi->fileflags[i]))) goto ERROR; |
| 132 |
if (esl_fread_u32(ssi->fp, &(ssi->bpl[i]))) goto ERROR; |
| 133 |
if (esl_fread_u32(ssi->fp, &(ssi->rpl[i]))) goto ERROR; |
| 134 |
} |
| 135 |
*ret_ssi = ssi; |
| 136 |
return eslOK; |
| 137 |
|
| 138 |
ERROR: |
| 139 |
if (ssi != NULL) esl_ssi_Close(ssi); |
| 140 |
*ret_ssi = NULL; |
| 141 |
return status; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
int |
| 172 |
esl_ssi_FindName(ESL_SSI *ssi, const char *key, uint16_t *ret_fh, off_t *ret_roff, off_t *opt_doff, int64_t *opt_L) |
| 173 |
{ |
| 174 |
int status; |
| 175 |
off_t doff; |
| 176 |
int64_t L; |
| 177 |
char *pkey = NULL; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
status = binary_search(ssi, key, ssi->plen, ssi->poffset, ssi->precsize, |
| 182 |
ssi->nprimary); |
| 183 |
|
| 184 |
if (status == eslOK) |
| 185 |
{ |
| 186 |
status = eslEFORMAT; |
| 187 |
if (esl_fread_u16(ssi->fp, ret_fh) != eslOK) goto ERROR; |
| 188 |
if (esl_fread_offset(ssi->fp, ssi->offsz, ret_roff) != eslOK) goto ERROR; |
| 189 |
if (esl_fread_offset(ssi->fp, ssi->offsz, &doff) != eslOK) goto ERROR; |
| 190 |
if (esl_fread_i64 (ssi->fp, &L) != eslOK) goto ERROR; |
| 191 |
} |
| 192 |
else if (status == eslENOTFOUND) |
| 193 |
{ |
| 194 |
if (ssi->nsecondary > 0) { |
| 195 |
if ((status = binary_search(ssi, key, ssi->slen, ssi->soffset, ssi->srecsize, ssi->nsecondary)) != eslOK) goto ERROR; |
| 196 |
|
| 197 |
|
| 198 |
ESL_ALLOC(pkey, sizeof(char) * ssi->plen); |
| 199 |
status = eslEFORMAT; |
| 200 |
if (fread(pkey, sizeof(char), ssi->plen, ssi->fp) != ssi->plen) goto ERROR; |
| 201 |
if ((status = esl_ssi_FindName(ssi, pkey, ret_fh, ret_roff, &doff, &L)) != eslOK) goto ERROR; |
| 202 |
} else goto ERROR; |
| 203 |
} else goto ERROR; |
| 204 |
|
| 205 |
if (pkey != NULL) free(pkey); |
| 206 |
if (opt_doff != NULL) *opt_doff = doff; |
| 207 |
if (opt_L != NULL) *opt_L = L; |
| 208 |
return eslOK; |
| 209 |
|
| 210 |
ERROR: |
| 211 |
if (pkey != NULL) free(pkey); |
| 212 |
*ret_fh = 0; |
| 213 |
*ret_roff = 0; |
| 214 |
if (opt_doff != NULL) *opt_doff = 0; |
| 215 |
if (opt_L != NULL) *opt_L = 0; |
| 216 |
return status; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
int |
| 253 |
esl_ssi_FindNumber(ESL_SSI *ssi, int64_t nkey, uint16_t *opt_fh, off_t *opt_roff, off_t *opt_doff, int64_t *opt_L, char **opt_pkey) |
| 254 |
{ |
| 255 |
int status; |
| 256 |
uint16_t fh; |
| 257 |
off_t doff, roff; |
| 258 |
uint64_t L; |
| 259 |
char *pkey = NULL; |
| 260 |
|
| 261 |
if (nkey >= ssi->nprimary) { status = eslENOTFOUND; goto ERROR; } |
| 262 |
ESL_ALLOC(pkey, sizeof(char) * ssi->plen); |
| 263 |
|
| 264 |
status = eslEFORMAT; |
| 265 |
if (fseeko(ssi->fp, ssi->poffset+ssi->precsize*nkey, SEEK_SET)!= 0) goto ERROR; |
| 266 |
if (fread(pkey, sizeof(char), ssi->plen, ssi->fp) != ssi->plen) goto ERROR; |
| 267 |
if (esl_fread_u16(ssi->fp, &fh) != eslOK) goto ERROR; |
| 268 |
if (esl_fread_offset(ssi->fp, ssi->offsz, &roff) != eslOK) goto ERROR; |
| 269 |
if (esl_fread_offset(ssi->fp, ssi->offsz, &doff) != eslOK) goto ERROR; |
| 270 |
if (esl_fread_u64 (ssi->fp, &L) != eslOK) goto ERROR; |
| 271 |
|
| 272 |
if (opt_fh != NULL) *opt_fh = fh; |
| 273 |
if (opt_roff != NULL) *opt_roff = roff; |
| 274 |
if (opt_doff != NULL) *opt_doff = doff; |
| 275 |
if (opt_L != NULL) *opt_L = L; |
| 276 |
if (opt_pkey != NULL) *opt_pkey = pkey; else free(pkey); |
| 277 |
return eslOK; |
| 278 |
|
| 279 |
ERROR: |
| 280 |
if (pkey != NULL) free(pkey); |
| 281 |
if (opt_fh != NULL) *opt_fh = 0; |
| 282 |
if (opt_roff != NULL) *opt_roff = 0; |
| 283 |
if (opt_doff != NULL) *opt_doff = 0; |
| 284 |
if (opt_L != NULL) *opt_L = 0; |
| 285 |
if (opt_pkey != NULL) *opt_pkey = NULL; |
| 286 |
return status; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
int |
| 407 |
esl_ssi_FindSubseq(ESL_SSI *ssi, const char *key, int64_t requested_start, |
| 408 |
uint16_t *ret_fh, off_t *ret_roff, off_t *ret_doff, int64_t *ret_L, int64_t *ret_actual_start) |
| 409 |
{ |
| 410 |
int status; |
| 411 |
uint64_t r, b, i, l; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
if ((status = esl_ssi_FindName(ssi, key, ret_fh, ret_roff, ret_doff, ret_L)) != eslOK) goto ERROR; |
| 416 |
if (requested_start < 0 || requested_start > *ret_L) { status = eslERANGE; goto ERROR; } |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
if (*ret_doff == 0 || ! (ssi->fileflags[*ret_fh] & eslSSI_FASTSUBSEQ)) |
| 421 |
{ |
| 422 |
*ret_actual_start = 1; |
| 423 |
return eslOK; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
r = ssi->rpl[*ret_fh]; |
| 430 |
b = ssi->bpl[*ret_fh]; |
| 431 |
i = requested_start; |
| 432 |
l = (i-1)/r; |
| 433 |
if (r == 0 || b == 0) { status = eslEINVAL; goto ERROR; } |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
if (b == r+1) |
| 439 |
{ |
| 440 |
*ret_doff += l*b + (i-1)%r; |
| 441 |
*ret_actual_start = requested_start; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
else |
| 448 |
{ |
| 449 |
*ret_doff += l*b; |
| 450 |
*ret_actual_start = 1 + l*r; |
| 451 |
} |
| 452 |
return eslOK; |
| 453 |
|
| 454 |
ERROR: |
| 455 |
*ret_fh = 0; |
| 456 |
*ret_roff = 0; |
| 457 |
*ret_doff = 0; |
| 458 |
*ret_L = 0; |
| 459 |
*ret_actual_start = 0; |
| 460 |
return status; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
int |
| 486 |
esl_ssi_FileInfo(ESL_SSI *ssi, uint16_t fh, char **ret_filename, int *ret_format) |
| 487 |
{ |
| 488 |
int status; |
| 489 |
|
| 490 |
if (fh >= ssi->nfiles) ESL_XEXCEPTION(eslEINVAL, "no such file number"); |
| 491 |
*ret_filename = ssi->filename[fh]; |
| 492 |
*ret_format = ssi->fileformat[fh]; |
| 493 |
return eslOK; |
| 494 |
|
| 495 |
ERROR: |
| 496 |
*ret_filename = NULL; |
| 497 |
*ret_format = 0; |
| 498 |
return status; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
void |
| 510 |
esl_ssi_Close(ESL_SSI *ssi) |
| 511 |
{ |
| 512 |
int i; |
| 513 |
|
| 514 |
if (ssi == NULL) return; |
| 515 |
|
| 516 |
if (ssi->fp != NULL) fclose(ssi->fp); |
| 517 |
if (ssi->filename != NULL) { |
| 518 |
for (i = 0; i < ssi->nfiles; i++) |
| 519 |
if (ssi->filename[i] != NULL) free(ssi->filename[i]); |
| 520 |
free(ssi->filename); |
| 521 |
} |
| 522 |
if (ssi->fileformat != NULL) free(ssi->fileformat); |
| 523 |
if (ssi->fileflags != NULL) free(ssi->fileflags); |
| 524 |
if (ssi->bpl != NULL) free(ssi->bpl); |
| 525 |
if (ssi->rpl != NULL) free(ssi->rpl); |
| 526 |
free(ssi); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
static int |
| 558 |
binary_search(ESL_SSI *ssi, const char *key, uint32_t klen, off_t base, |
| 559 |
uint32_t recsize, uint64_t maxidx) |
| 560 |
{ |
| 561 |
char *name; |
| 562 |
uint64_t left, right, mid; |
| 563 |
int cmp; |
| 564 |
int status; |
| 565 |
|
| 566 |
if (maxidx == 0) return eslENOTFOUND; |
| 567 |
|
| 568 |
ESL_ALLOC(name, (sizeof(char)*klen)); |
| 569 |
|
| 570 |
left = 0; |
| 571 |
right = maxidx-1; |
| 572 |
while (1) { |
| 573 |
mid = (left+right) / 2; |
| 574 |
|
| 575 |
status = eslEFORMAT; |
| 576 |
if (fseeko(ssi->fp, base + recsize*mid, SEEK_SET) != 0) goto ERROR; |
| 577 |
if (fread(name, sizeof(char), klen, ssi->fp) != klen) goto ERROR; |
| 578 |
|
| 579 |
status = eslENOTFOUND; |
| 580 |
cmp = strcmp(name, key); |
| 581 |
if (cmp == 0) break; |
| 582 |
else if (left >= right) goto ERROR; |
| 583 |
else if (cmp < 0) left = mid+1; |
| 584 |
else if (cmp > 0) { |
| 585 |
if (mid == 0) goto ERROR; |
| 586 |
else right = mid-1; |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
if (name != NULL) free(name); |
| 591 |
return eslOK; |
| 592 |
|
| 593 |
ERROR: |
| 594 |
if (name != NULL) free(name); |
| 595 |
return status; |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
static int current_newssi_size(const ESL_NEWSSI *ns); |
| 603 |
static int activate_external_sort(ESL_NEWSSI *ns); |
| 604 |
static int parse_pkey(char *buf, ESL_PKEY *pkey); |
| 605 |
static int parse_skey(char *buf, ESL_SKEY *skey); |
| 606 |
static int pkeysort(const void *k1, const void *k2); |
| 607 |
static int skeysort(const void *k1, const void *k2); |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
int |
| 627 |
esl_newssi_Open(const char *ssifile, int allow_overwrite, ESL_NEWSSI **ret_newssi) |
| 628 |
{ |
| 629 |
ESL_NEWSSI *ns = NULL; |
| 630 |
int i; |
| 631 |
int status; |
| 632 |
|
| 633 |
ESL_ALLOC(ns, sizeof(ESL_NEWSSI)); |
| 634 |
ns->ssifile = NULL; |
| 635 |
ns->ssifp = NULL; |
| 636 |
ns->external = FALSE; |
| 637 |
ns->max_ram = eslSSI_MAXRAM; |
| 638 |
ns->filenames = NULL; |
| 639 |
ns->fileformat = NULL; |
| 640 |
ns->bpl = NULL; |
| 641 |
ns->rpl = NULL; |
| 642 |
ns->flen = 0; |
| 643 |
ns->nfiles = 0; |
| 644 |
ns->pkeys = NULL; |
| 645 |
ns->plen = 0; |
| 646 |
ns->nprimary = 0; |
| 647 |
ns->ptmpfile = NULL; |
| 648 |
ns->ptmp = NULL; |
| 649 |
ns->skeys = NULL; |
| 650 |
ns->slen = 0; |
| 651 |
ns->nsecondary = 0; |
| 652 |
ns->stmpfile = NULL; |
| 653 |
ns->stmp = NULL; |
| 654 |
ns->errbuf[0] = '\0'; |
| 655 |
|
| 656 |
if ((status = esl_strdup(ssifile, -1, &(ns->ssifile))) != eslOK) goto ERROR; |
| 657 |
if ((status = esl_strdup(ssifile, -1, &(ns->ptmpfile))) != eslOK) goto ERROR; |
| 658 |
if ((status = esl_strdup(ssifile, -1, &(ns->stmpfile))) != eslOK) goto ERROR; |
| 659 |
if ((status = esl_strcat(&ns->ptmpfile, -1, ".1", 2)) != eslOK) goto ERROR; |
| 660 |
if ((status = esl_strcat(&ns->stmpfile, -1, ".2", 2)) != eslOK) goto ERROR; |
| 661 |
|
| 662 |
if (! allow_overwrite) |
| 663 |
{ |
| 664 |
if (esl_FileExists(ssifile) || |
| 665 |
esl_FileExists(ns->ptmpfile) || |
| 666 |
esl_FileExists(ns->stmpfile)) |
| 667 |
{ status = eslEOVERWRITE; goto ERROR; } |
| 668 |
} |
| 669 |
|
| 670 |
if ((ns->ssifp = fopen(ssifile, "w")) == NULL) { status = eslENOTFOUND; goto ERROR; } |
| 671 |
|
| 672 |
ESL_ALLOC(ns->filenames, sizeof(char *) * eslSSI_FCHUNK); |
| 673 |
for (i = 0; i < eslSSI_FCHUNK; i++) |
| 674 |
ns->filenames[i] = NULL; |
| 675 |
ESL_ALLOC(ns->fileformat, sizeof(uint32_t) * eslSSI_FCHUNK); |
| 676 |
ESL_ALLOC(ns->bpl, sizeof(uint32_t) * eslSSI_FCHUNK); |
| 677 |
ESL_ALLOC(ns->rpl, sizeof(uint32_t) * eslSSI_FCHUNK); |
| 678 |
ESL_ALLOC(ns->pkeys, sizeof(ESL_PKEY) * eslSSI_KCHUNK); |
| 679 |
for (i = 0; i < eslSSI_KCHUNK; i++) |
| 680 |
ns->pkeys[i].key = NULL; |
| 681 |
ESL_ALLOC(ns->skeys, sizeof(ESL_SKEY) * eslSSI_KCHUNK); |
| 682 |
for (i = 0; i < eslSSI_KCHUNK; i++) { |
| 683 |
ns->skeys[i].key = NULL; |
| 684 |
ns->skeys[i].pkey = NULL; |
| 685 |
} |
| 686 |
*ret_newssi = ns; |
| 687 |
return eslOK; |
| 688 |
|
| 689 |
ERROR: |
| 690 |
esl_newssi_Close(ns); |
| 691 |
return status; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
int |
| 718 |
esl_newssi_AddFile(ESL_NEWSSI *ns, const char *filename, int fmt, uint16_t *ret_fh) |
| 719 |
{ |
| 720 |
int status; |
| 721 |
uint16_t fh; |
| 722 |
int i; |
| 723 |
int n; |
| 724 |
|
| 725 |
if (ns->nfiles >= eslSSI_MAXFILES) ESL_XFAIL(eslERANGE, ns->errbuf, "exceeded the maximum number of files an SSI index can store"); |
| 726 |
|
| 727 |
n = strlen(filename); |
| 728 |
if ((n+1) > ns->flen) ns->flen = n+1; |
| 729 |
|
| 730 |
if ((status = esl_FileTail(filename, FALSE, &(ns->filenames[ns->nfiles]))) != eslOK) goto ERROR; |
| 731 |
|
| 732 |
ns->fileformat[ns->nfiles] = fmt; |
| 733 |
ns->bpl[ns->nfiles] = 0; |
| 734 |
ns->rpl[ns->nfiles] = 0; |
| 735 |
fh = ns->nfiles; |
| 736 |
ns->nfiles++; |
| 737 |
|
| 738 |
if (ns->nfiles % eslSSI_FCHUNK == 0) { |
| 739 |
ESL_REALLOC(ns->filenames, sizeof(char *) * (ns->nfiles+eslSSI_FCHUNK)); |
| 740 |
for (i = ns->nfiles; i < ns->nfiles+eslSSI_FCHUNK; i++) ns->filenames[i] = NULL; |
| 741 |
ESL_REALLOC(ns->fileformat, sizeof(uint32_t) * (ns->nfiles+eslSSI_FCHUNK)); |
| 742 |
ESL_REALLOC(ns->bpl, sizeof(uint32_t) * (ns->nfiles+eslSSI_FCHUNK)); |
| 743 |
ESL_REALLOC(ns->rpl, sizeof(uint32_t) * (ns->nfiles+eslSSI_FCHUNK)); |
| 744 |
} |
| 745 |
|
| 746 |
*ret_fh = fh; |
| 747 |
return eslOK; |
| 748 |
|
| 749 |
ERROR: |
| 750 |
*ret_fh = 0; |
| 751 |
return status; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
int |
| 778 |
esl_newssi_SetSubseq(ESL_NEWSSI *ns, uint16_t fh, uint32_t bpl, uint32_t rpl) |
| 779 |
{ |
| 780 |
int status; |
| 781 |
|
| 782 |
if (fh >= ns->nfiles) ESL_XEXCEPTION(eslEINVAL, "invalid file number"); |
| 783 |
if (bpl <= 0 || rpl <= 0) ESL_XEXCEPTION(eslEINVAL, "invalid bpl or rpl"); |
| 784 |
ns->bpl[fh] = bpl; |
| 785 |
ns->rpl[fh] = rpl; |
| 786 |
return eslOK; |
| 787 |
|
| 788 |
ERROR: |
| 789 |
return status; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
int |
| 843 |
esl_newssi_AddKey(ESL_NEWSSI *ns, const char *key, uint16_t fh, |
| 844 |
off_t r_off, off_t d_off, int64_t L) |
| 845 |
{ |
| 846 |
int status; |
| 847 |
int i; |
| 848 |
int n; |
| 849 |
|
| 850 |
if (fh >= eslSSI_MAXFILES) ESL_XEXCEPTION(eslEINVAL, "invalid fh"); |
| 851 |
if (ns->nprimary >= eslSSI_MAXKEYS) ESL_XFAIL(eslERANGE, ns->errbuf, "exceeded maximum number of primary keys allowed"); |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
if (!ns->external && current_newssi_size(ns) >= ns->max_ram) |
| 857 |
if ((status = activate_external_sort(ns)) != eslOK) goto ERROR; |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
n = strlen(key)+1; |
| 862 |
if (n > ns->plen) ns->plen = n; |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
if (ns->external) |
| 867 |
{ |
| 868 |
if (sizeof(off_t) == 4) { |
| 869 |
if (fprintf(ns->ptmp, "%s\t%d\t%" PRIu32 "\t%" PRIu32 "\t%" PRIi64 "\n", |
| 870 |
key, fh, (uint32_t) r_off, (uint32_t) d_off, L) <= 0) |
| 871 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi key tmp file write failed"); |
| 872 |
} else { |
| 873 |
if (fprintf(ns->ptmp, "%s\t%d\t%" PRIu64 "\t%" PRIu64 "\t%" PRIi64 "\n", |
| 874 |
key, fh, (uint64_t) r_off, (uint64_t) d_off, L) <= 0) |
| 875 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi key tmp file write failed"); |
| 876 |
} |
| 877 |
ns->nprimary++; |
| 878 |
} |
| 879 |
else |
| 880 |
{ |
| 881 |
|
| 882 |
|
| 883 |
if ((status = esl_strdup(key, n, &(ns->pkeys[ns->nprimary].key))) != eslOK) goto ERROR; |
| 884 |
ns->pkeys[ns->nprimary].fnum = fh; |
| 885 |
ns->pkeys[ns->nprimary].r_off = r_off; |
| 886 |
ns->pkeys[ns->nprimary].d_off = d_off; |
| 887 |
ns->pkeys[ns->nprimary].len = L; |
| 888 |
ns->nprimary++; |
| 889 |
|
| 890 |
|
| 891 |
if (ns->nprimary % eslSSI_KCHUNK == 0) { |
| 892 |
ESL_REALLOC(ns->pkeys, sizeof(ESL_PKEY) * (ns->nprimary+eslSSI_KCHUNK)); |
| 893 |
for (i = ns->nprimary; i < ns->nprimary + eslSSI_KCHUNK; i++) |
| 894 |
ns->pkeys[i].key = NULL; |
| 895 |
} |
| 896 |
} |
| 897 |
return eslOK; |
| 898 |
|
| 899 |
ERROR: |
| 900 |
return status; |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
int |
| 925 |
esl_newssi_AddAlias(ESL_NEWSSI *ns, const char *alias, const char *key) |
| 926 |
{ |
| 927 |
int status; |
| 928 |
int i; |
| 929 |
int n; |
| 930 |
|
| 931 |
if (ns->nsecondary >= eslSSI_MAXKEYS) ESL_XFAIL(eslERANGE, ns->errbuf, "exceeded maximum number of secondary keys allowed"); |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
if (!ns->external && current_newssi_size(ns) >= ns->max_ram) |
| 937 |
if ((status = activate_external_sort(ns)) != eslOK) goto ERROR; |
| 938 |
|
| 939 |
|
| 940 |
n = strlen(alias)+1; |
| 941 |
if (n > ns->slen) ns->slen = n; |
| 942 |
|
| 943 |
|
| 944 |
if (ns->external) |
| 945 |
{ |
| 946 |
if (fprintf(ns->stmp, "%s\t%s\n", alias, key) <= 0) ESL_XEXCEPTION_SYS(eslEWRITE, "ssi alias tmp file write failed"); |
| 947 |
ns->nsecondary++; |
| 948 |
} |
| 949 |
else |
| 950 |
{ |
| 951 |
if ((status = esl_strdup(alias, n, &(ns->skeys[ns->nsecondary].key))) != eslOK) goto ERROR; |
| 952 |
if ((status = esl_strdup(key, -1, &(ns->skeys[ns->nsecondary].pkey))) != eslOK) goto ERROR; |
| 953 |
ns->nsecondary++; |
| 954 |
|
| 955 |
if (ns->nsecondary % eslSSI_KCHUNK == 0) { |
| 956 |
ESL_REALLOC(ns->skeys, sizeof(ESL_SKEY) * (ns->nsecondary+eslSSI_KCHUNK)); |
| 957 |
for (i = ns->nsecondary; i < ns->nsecondary+eslSSI_KCHUNK; i++) { |
| 958 |
ns->skeys[i].key = NULL; |
| 959 |
ns->skeys[i].pkey = NULL; |
| 960 |
} |
| 961 |
} |
| 962 |
} |
| 963 |
return eslOK; |
| 964 |
|
| 965 |
ERROR: |
| 966 |
return status; |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
int |
| 1007 |
esl_newssi_Write(ESL_NEWSSI *ns) |
| 1008 |
{ |
| 1009 |
int status, |
| 1010 |
i; |
| 1011 |
uint32_t header_flags, |
| 1012 |
file_flags, |
| 1013 |
frecsize, |
| 1014 |
precsize, |
| 1015 |
srecsize; |
| 1016 |
off_t foffset, |
| 1017 |
poffset, |
| 1018 |
soffset; |
| 1019 |
char *fk = NULL, |
| 1020 |
*pk = NULL, |
| 1021 |
*sk = NULL, |
| 1022 |
*buf = NULL; |
| 1023 |
int n = 0; |
| 1024 |
ESL_PKEY pkey; |
| 1025 |
ESL_SKEY skey; |
| 1026 |
|
| 1027 |
if (ns->nsecondary > 0 && ns->slen == 0) |
| 1028 |
ESL_EXCEPTION(eslEINVAL, "zero secondary key length: shouldn't happen"); |
| 1029 |
if (ns->ssifp == NULL) |
| 1030 |
ESL_EXCEPTION(eslEINVAL, "SSI data were already written."); |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
ESL_ALLOC(fk, sizeof(char) * ns->flen); |
| 1040 |
ESL_ALLOC(pk, ESL_MAX(1, sizeof(char) * ns->plen)); |
| 1041 |
if (ns->nsecondary > 0) ESL_ALLOC(sk, sizeof(char) * ns->slen); |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
if (current_newssi_size(ns) >= 2047 && sizeof(off_t) != 8) |
| 1051 |
ESL_XFAIL(eslERANGE, ns->errbuf, "SSI index file file would be > 2G; your filesystem isn't capable of handling it"); |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
frecsize = 4*sizeof(uint32_t) + ns->flen; |
| 1057 |
precsize = 2*sizeof(off_t) + sizeof(uint16_t) + sizeof(uint64_t) + ns->plen; |
| 1058 |
srecsize = ns->slen + ns->plen; |
| 1059 |
header_flags = 0; |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
foffset = 9*sizeof(uint32_t)+2*sizeof(uint64_t)+sizeof(uint16_t)+3*sizeof(off_t); |
| 1065 |
poffset = foffset + frecsize*ns->nfiles; |
| 1066 |
soffset = poffset + precsize*ns->nprimary; |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
if (ns->external) |
| 1079 |
{ |
| 1080 |
char cmd[1024]; |
| 1081 |
|
| 1082 |
if (strlen(ns->ptmpfile) > 256 || strlen(ns->ptmpfile) > 256) |
| 1083 |
ESL_XEXCEPTION(eslEINVAL, "tmpfile name too long"); |
| 1084 |
|
| 1085 |
fclose(ns->ptmp); |
| 1086 |
ns->ptmp = NULL; |
| 1087 |
snprintf(cmd, 1024, "env LC_ALL=POSIX sort -o %s %s\n", ns->ptmpfile, ns->ptmpfile); |
| 1088 |
if (system(cmd) != 0) ESL_XFAIL(eslESYS, ns->errbuf, "external sort of primary keys failed"); |
| 1089 |
if ((ns->ptmp = fopen(ns->ptmpfile, "r")) == NULL) ESL_XFAIL(eslESYS, ns->errbuf, "failed to reopen primary key tmp file after sort"); |
| 1090 |
|
| 1091 |
fclose(ns->stmp); |
| 1092 |
ns->stmp = NULL; |
| 1093 |
snprintf(cmd, 1024, "env LC_ALL=POSIX sort -o %s %s\n", ns->stmpfile, ns->stmpfile); |
| 1094 |
if (system(cmd) != 0) ESL_XFAIL(eslESYS, ns->errbuf, "external sort of secondary keys failed"); |
| 1095 |
if ((ns->stmp = fopen(ns->stmpfile, "r")) == NULL) ESL_XFAIL(eslESYS, ns->errbuf, "failed to reopen secondary key tmp file after sort"); |
| 1096 |
} |
| 1097 |
else |
| 1098 |
{ |
| 1099 |
qsort((void *) ns->pkeys, ns->nprimary, sizeof(ESL_PKEY), pkeysort); |
| 1100 |
qsort((void *) ns->skeys, ns->nsecondary, sizeof(ESL_SKEY), skeysort); |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
if (esl_fwrite_u32(ns->ssifp, v30magic) != eslOK || |
| 1106 |
esl_fwrite_u32(ns->ssifp, header_flags) != eslOK || |
| 1107 |
esl_fwrite_u32(ns->ssifp, sizeof(off_t)) != eslOK || |
| 1108 |
esl_fwrite_u16(ns->ssifp, ns->nfiles) != eslOK || |
| 1109 |
esl_fwrite_u64(ns->ssifp, ns->nprimary) != eslOK || |
| 1110 |
esl_fwrite_u64(ns->ssifp, ns->nsecondary)!= eslOK || |
| 1111 |
esl_fwrite_u32(ns->ssifp, ns->flen) != eslOK || |
| 1112 |
esl_fwrite_u32(ns->ssifp, ns->plen) != eslOK || |
| 1113 |
esl_fwrite_u32(ns->ssifp, ns->slen) != eslOK || |
| 1114 |
esl_fwrite_u32(ns->ssifp, frecsize) != eslOK || |
| 1115 |
esl_fwrite_u32(ns->ssifp, precsize) != eslOK || |
| 1116 |
esl_fwrite_u32(ns->ssifp, srecsize) != eslOK || |
| 1117 |
esl_fwrite_offset(ns->ssifp, foffset) != eslOK || |
| 1118 |
esl_fwrite_offset(ns->ssifp, poffset) != eslOK || |
| 1119 |
esl_fwrite_offset(ns->ssifp, soffset) != eslOK) |
| 1120 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed"); |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
for (i = 0; i < ns->nfiles; i++) |
| 1125 |
{ |
| 1126 |
file_flags = 0; |
| 1127 |
if (ns->bpl[i] > 0 && ns->rpl[i] > 0) file_flags |= eslSSI_FASTSUBSEQ; |
| 1128 |
strncpy(fk, ns->filenames[i], ns->flen); |
| 1129 |
|
| 1130 |
if (fwrite(fk, sizeof(char), ns->flen, ns->ssifp) != ns->flen || |
| 1131 |
esl_fwrite_u32(ns->ssifp, ns->fileformat[i]) != eslOK || |
| 1132 |
esl_fwrite_u32(ns->ssifp, file_flags) != eslOK || |
| 1133 |
esl_fwrite_u32(ns->ssifp, ns->bpl[i]) != eslOK || |
| 1134 |
esl_fwrite_u32(ns->ssifp, ns->rpl[i]) != eslOK) |
| 1135 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed"); |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
if (ns->external) |
| 1141 |
{ |
| 1142 |
if (ns->nprimary) strncpy(pk, "", ns->plen); |
| 1143 |
for (i = 0; i < ns->nprimary; i++) |
| 1144 |
{ |
| 1145 |
if (esl_fgets(&buf, &n, ns->ptmp) != eslOK) ESL_XFAIL(eslESYS, ns->errbuf, "read from sorted primary key tmpfile failed"); |
| 1146 |
if (parse_pkey(buf, &pkey) != eslOK) ESL_XFAIL(eslESYS, ns->errbuf, "parse failed for a line of sorted primary key tmpfile failed"); |
| 1147 |
if (strcmp(pk, pkey.key) == 0) ESL_XFAIL(eslEDUP, ns->errbuf, "primary keys not unique: '%s' occurs more than once", pkey.key); |
| 1148 |
strncpy(pk, pkey.key, ns->plen); |
| 1149 |
|
| 1150 |
if (fwrite(pk,sizeof(char),ns->plen,ns->ssifp) != ns->plen || |
| 1151 |
esl_fwrite_u16( ns->ssifp, pkey.fnum) != eslOK || |
| 1152 |
esl_fwrite_offset(ns->ssifp, pkey.r_off) != eslOK || |
| 1153 |
esl_fwrite_offset(ns->ssifp, pkey.d_off) != eslOK || |
| 1154 |
esl_fwrite_i64( ns->ssifp, pkey.len) != eslOK) |
| 1155 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed"); |
| 1156 |
} |
| 1157 |
} |
| 1158 |
else |
| 1159 |
{ |
| 1160 |
if (ns->nprimary) strncpy(pk, "", ns->plen); |
| 1161 |
for (i = 0; i < ns->nprimary; i++) |
| 1162 |
{ |
| 1163 |
if (strcmp(pk, ns->pkeys[i].key) == 0) ESL_XFAIL(eslEDUP, ns->errbuf, "primary keys not unique: '%s' occurs more than once", ns->pkeys[i].key); |
| 1164 |
strncpy(pk, ns->pkeys[i].key, ns->plen); |
| 1165 |
|
| 1166 |
if (fwrite(pk,sizeof(char),ns->plen,ns->ssifp) != ns->plen || |
| 1167 |
esl_fwrite_u16( ns->ssifp, ns->pkeys[i].fnum) != eslOK || |
| 1168 |
esl_fwrite_offset(ns->ssifp, ns->pkeys[i].r_off) != eslOK || |
| 1169 |
esl_fwrite_offset(ns->ssifp, ns->pkeys[i].d_off) != eslOK || |
| 1170 |
esl_fwrite_i64( ns->ssifp, ns->pkeys[i].len) != eslOK) |
| 1171 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed"); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
if (ns->external) |
| 1179 |
{ |
| 1180 |
if (ns->nsecondary) strncpy(sk, "", ns->slen); |
| 1181 |
for (i = 0; i < ns->nsecondary; i++) |
| 1182 |
{ |
| 1183 |
if (esl_fgets(&buf, &n, ns->stmp) != eslOK) ESL_XFAIL(eslESYS, ns->errbuf, "read from sorted secondary key tmpfile failed"); |
| 1184 |
if (parse_skey(buf, &skey) != eslOK) ESL_XFAIL(eslESYS, ns->errbuf, "parse failed for a line of sorted secondary key tmpfile failed"); |
| 1185 |
if (strcmp(sk, skey.key) == 0) ESL_XFAIL(eslEDUP, ns->errbuf, "secondary keys not unique: '%s' occurs more than once", skey.key); |
| 1186 |
strncpy(sk, skey.key, ns->slen); |
| 1187 |
strncpy(pk, skey.pkey, ns->plen); |
| 1188 |
|
| 1189 |
if (fwrite(sk, sizeof(char), ns->slen, ns->ssifp) != ns->slen || |
| 1190 |
fwrite(pk, sizeof(char), ns->plen, ns->ssifp) != ns->plen) |
| 1191 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed"); |
| 1192 |
} |
| 1193 |
} |
| 1194 |
else |
| 1195 |
{ |
| 1196 |
|
| 1197 |
if (ns->nsecondary) strncpy(sk, "", ns->slen); |
| 1198 |
for (i = 0; i < ns->nsecondary; i++) |
| 1199 |
{ |
| 1200 |
if (strcmp(sk, ns->skeys[i].key) == 0) ESL_XFAIL(eslEDUP, ns->errbuf, "secondary keys not unique: '%s' occurs more than once", ns->skeys[i].key); |
| 1201 |
strncpy(sk, ns->skeys[i].key, ns->slen); |
| 1202 |
strncpy(pk, ns->skeys[i].pkey, ns->plen); |
| 1203 |
|
| 1204 |
if (fwrite(sk, sizeof(char), ns->slen, ns->ssifp) != ns->slen || |
| 1205 |
fwrite(pk, sizeof(char), ns->plen, ns->ssifp) != ns->plen) |
| 1206 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi write failed"); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
fclose(ns->ssifp); |
| 1211 |
ns->ssifp = NULL; |
| 1212 |
if (fk) free(fk); |
| 1213 |
if (pk) free(pk); |
| 1214 |
if (sk) free(sk); |
| 1215 |
if (buf) free(buf); |
| 1216 |
if (ns->ptmp) { fclose(ns->ptmp); ns->ptmp = NULL; } |
| 1217 |
if (ns->stmp) { fclose(ns->stmp); ns->stmp = NULL; } |
| 1218 |
return eslOK; |
| 1219 |
|
| 1220 |
ERROR: |
| 1221 |
remove(ns->ssifile); |
| 1222 |
if (ns->ssifp) { fclose(ns->ssifp); ns->ssifp = NULL; } |
| 1223 |
if (fk) free(fk); |
| 1224 |
if (pk) free(pk); |
| 1225 |
if (sk) free(sk); |
| 1226 |
if (buf) free(buf); |
| 1227 |
if (ns->ptmp) { fclose(ns->ptmp); ns->ptmp = NULL; } |
| 1228 |
if (ns->stmp) { fclose(ns->stmp); ns->stmp = NULL; } |
| 1229 |
return status; |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
void |
| 1238 |
esl_newssi_Close(ESL_NEWSSI *ns) |
| 1239 |
{ |
| 1240 |
int i; |
| 1241 |
if (ns == NULL) return; |
| 1242 |
|
| 1243 |
if (ns->external == FALSE) |
| 1244 |
{ |
| 1245 |
if (ns->pkeys != NULL) |
| 1246 |
{ |
| 1247 |
for (i = 0; i < ns->nprimary; i++) |
| 1248 |
if (ns->pkeys[i].key != NULL) free(ns->pkeys[i].key); |
| 1249 |
free(ns->pkeys); |
| 1250 |
} |
| 1251 |
if (ns->skeys != NULL) |
| 1252 |
{ |
| 1253 |
for (i = 0; i < ns->nsecondary; i++) |
| 1254 |
{ |
| 1255 |
if (ns->skeys[i].key != NULL) free(ns->skeys[i].key); |
| 1256 |
if (ns->skeys[i].pkey != NULL) free(ns->skeys[i].pkey); |
| 1257 |
} |
| 1258 |
free(ns->skeys); |
| 1259 |
} |
| 1260 |
} |
| 1261 |
else |
| 1262 |
{ |
| 1263 |
remove(ns->ptmpfile); |
| 1264 |
remove(ns->stmpfile); |
| 1265 |
} |
| 1266 |
|
| 1267 |
if (ns->filenames != NULL) |
| 1268 |
{ |
| 1269 |
for (i = 0; i < ns->nfiles; i++) |
| 1270 |
if (ns->filenames[i] != NULL) free(ns->filenames[i]); |
| 1271 |
free(ns->filenames); |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (ns->stmp) fclose(ns->stmp); |
| 1275 |
if (ns->stmpfile) free(ns->stmpfile); |
| 1276 |
if (ns->ptmp) fclose(ns->ptmp); |
| 1277 |
if (ns->ptmpfile) free(ns->ptmpfile); |
| 1278 |
if (ns->fileformat) free(ns->fileformat); |
| 1279 |
if (ns->bpl) free(ns->bpl); |
| 1280 |
if (ns->rpl) free(ns->rpl); |
| 1281 |
if (ns->ssifile) free(ns->ssifile); |
| 1282 |
if (ns->ssifp) fclose(ns->ssifp); |
| 1283 |
free(ns); |
| 1284 |
} |
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
static int |
| 1302 |
current_newssi_size(const ESL_NEWSSI *ns) |
| 1303 |
{ |
| 1304 |
uint64_t frecsize, precsize, srecsize; |
| 1305 |
uint64_t total; |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
frecsize = 4*sizeof(uint32_t) + ns->flen; |
| 1311 |
precsize = 2*sizeof(off_t) + sizeof(uint16_t) + sizeof(uint64_t) + ns->plen; |
| 1312 |
srecsize = ns->slen + ns->plen; |
| 1313 |
total = (9*sizeof(uint32_t)+2*sizeof(uint64_t)+sizeof(uint16_t)+3*sizeof(off_t)+ |
| 1314 |
frecsize * ns->nfiles + |
| 1315 |
precsize * ns->nprimary + |
| 1316 |
srecsize * ns->nsecondary) / |
| 1317 |
1048576L; |
| 1318 |
return (int) total; |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
static int |
| 1334 |
activate_external_sort(ESL_NEWSSI *ns) |
| 1335 |
{ |
| 1336 |
int status; |
| 1337 |
int i; |
| 1338 |
|
| 1339 |
if (ns->external) return eslOK; |
| 1340 |
|
| 1341 |
if ((ns->ptmp = fopen(ns->ptmpfile, "w")) == NULL) ESL_XFAIL(eslENOTFOUND, ns->errbuf, "Failed to open primary key tmpfile for external sort"); |
| 1342 |
if ((ns->stmp = fopen(ns->stmpfile, "w")) == NULL) ESL_XFAIL(eslENOTFOUND, ns->errbuf, "Failed to open secondary key tmpfile for external sort"); |
| 1343 |
|
| 1344 |
|
| 1345 |
for (i = 0; i < ns->nprimary; i++) { |
| 1346 |
if (sizeof(off_t) == 4) { |
| 1347 |
if (fprintf(ns->ptmp, "%s\t%u\t%lu\t%lu\t%lu\n", |
| 1348 |
ns->pkeys[i].key, |
| 1349 |
(unsigned int) ns->pkeys[i].fnum, |
| 1350 |
(unsigned long) ns->pkeys[i].r_off, |
| 1351 |
(unsigned long) ns->pkeys[i].d_off, |
| 1352 |
(unsigned long) ns->pkeys[i].len) <= 0) |
| 1353 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi key tmp file write failed"); |
| 1354 |
} else { |
| 1355 |
if (fprintf(ns->ptmp, "%s\t%u\t%llu\t%llu\t%lu\n", |
| 1356 |
ns->pkeys[i].key, |
| 1357 |
(unsigned int) ns->pkeys[i].fnum, |
| 1358 |
(unsigned long long) ns->pkeys[i].r_off, |
| 1359 |
(unsigned long long) ns->pkeys[i].d_off, |
| 1360 |
(unsigned long) ns->pkeys[i].len) <= 0) |
| 1361 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi key tmp file write failed"); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
for (i = 0; i < ns->nsecondary; i++) |
| 1365 |
if (fprintf(ns->stmp, "%s\t%s\n", ns->skeys[i].key, ns->skeys[i].pkey) <= 0) |
| 1366 |
ESL_XEXCEPTION_SYS(eslEWRITE, "ssi alias tmp file write failed"); |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
for (i = 0; i < ns->nprimary; i++) free(ns->pkeys[i].key); |
| 1371 |
for (i = 0; i < ns->nsecondary; i++) free(ns->skeys[i].key); |
| 1372 |
for (i = 0; i < ns->nsecondary; i++) free(ns->skeys[i].pkey); |
| 1373 |
if (ns->pkeys != NULL) free(ns->pkeys); |
| 1374 |
if (ns->skeys != NULL) free(ns->skeys); |
| 1375 |
ns->pkeys = NULL; |
| 1376 |
ns->skeys = NULL; |
| 1377 |
ns->external = TRUE; |
| 1378 |
return eslOK; |
| 1379 |
|
| 1380 |
ERROR: |
| 1381 |
if (ns->ptmp != NULL) { fclose(ns->ptmp); ns->ptmp = NULL; } |
| 1382 |
if (ns->stmp != NULL) { fclose(ns->stmp); ns->stmp = NULL; } |
| 1383 |
return status; |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
static int |
| 1404 |
parse_pkey(char *buf, ESL_PKEY *pkey) |
| 1405 |
{ |
| 1406 |
int status; |
| 1407 |
char *s, *tok; |
| 1408 |
|
| 1409 |
s = buf; |
| 1410 |
if (esl_strtok(&s, "\t\n", &(pkey->key)) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed"); |
| 1411 |
if (esl_strtok(&s, "\t\n", &tok) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed"); |
| 1412 |
|
| 1413 |
pkey->fnum = (uint16_t) atoi(tok); |
| 1414 |
if (esl_strtok(&s, "\t\n", &tok) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed"); |
| 1415 |
if (sizeof(off_t) == 4) pkey->r_off = (off_t) strtoul (tok, NULL, 10); |
| 1416 |
else if (sizeof(off_t) == 8) pkey->r_off = (off_t) strtoull(tok, NULL, 10); |
| 1417 |
else ESL_XEXCEPTION(eslEINCONCEIVABLE, "whoa - weird off_t"); |
| 1418 |
|
| 1419 |
if (esl_strtok(&s, "\t\n", &tok) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed"); |
| 1420 |
if (sizeof(off_t) == 4) pkey->d_off = (off_t) strtoul (tok, NULL, 10); |
| 1421 |
else if (sizeof(off_t) == 8) pkey->d_off = (off_t) strtoull(tok, NULL, 10); |
| 1422 |
else ESL_XEXCEPTION(eslEINCONCEIVABLE, "whoa - weird off_t"); |
| 1423 |
|
| 1424 |
if (esl_strtok(&s, "\t\n", &tok) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed"); |
| 1425 |
pkey->len = (uint64_t) strtoull(tok, NULL, 10); |
| 1426 |
return eslOK; |
| 1427 |
|
| 1428 |
ERROR: |
| 1429 |
return status; |
| 1430 |
} |
| 1431 |
static int |
| 1432 |
parse_skey(char *buf, ESL_SKEY *skey) |
| 1433 |
{ |
| 1434 |
int status; |
| 1435 |
char *s; |
| 1436 |
|
| 1437 |
s = buf; |
| 1438 |
if (esl_strtok(&s, "\t\n", &(skey->key)) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed"); |
| 1439 |
if (esl_strtok(&s, "\t\n", &(skey->pkey)) != eslOK) ESL_XEXCEPTION(eslEFORMAT, "parse failed"); |
| 1440 |
return eslOK; |
| 1441 |
|
| 1442 |
ERROR: |
| 1443 |
return status; |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
static int |
| 1448 |
pkeysort(const void *k1, const void *k2) |
| 1449 |
{ |
| 1450 |
ESL_PKEY *key1; |
| 1451 |
ESL_PKEY *key2; |
| 1452 |
key1 = (ESL_PKEY *) k1; |
| 1453 |
key2 = (ESL_PKEY *) k2; |
| 1454 |
return strcmp(key1->key, key2->key); |
| 1455 |
} |
| 1456 |
static int |
| 1457 |
skeysort(const void *k1, const void *k2) |
| 1458 |
{ |
| 1459 |
ESL_SKEY *key1; |
| 1460 |
ESL_SKEY *key2; |
| 1461 |
key1 = (ESL_SKEY *) k1; |
| 1462 |
key2 = (ESL_SKEY *) k2; |
| 1463 |
return strcmp(key1->key, key2->key); |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
void |
| 1477 |
esl_byteswap(char *swap, int nbytes) |
| 1478 |
{ |
| 1479 |
int x; |
| 1480 |
char byte; |
| 1481 |
|
| 1482 |
for (x = 0; x < nbytes / 2; x++) |
| 1483 |
{ |
| 1484 |
byte = swap[nbytes - x - 1]; |
| 1485 |
swap[nbytes - x - 1] = swap[x]; |
| 1486 |
swap[x] = byte; |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
uint16_t |
| 1500 |
esl_ntoh16(uint16_t netshort) |
| 1501 |
{ |
| 1502 |
#ifdef WORDS_BIGENDIAN |
| 1503 |
return netshort; |
| 1504 |
#else |
| 1505 |
esl_byteswap((char *) &netshort, 2); |
| 1506 |
return netshort; |
| 1507 |
#endif |
| 1508 |
} |
| 1509 |
uint32_t |
| 1510 |
esl_ntoh32(uint32_t netlong) |
| 1511 |
{ |
| 1512 |
#ifdef WORDS_BIGENDIAN |
| 1513 |
return netlong; |
| 1514 |
#else |
| 1515 |
esl_byteswap((char *) &netlong, 4); |
| 1516 |
return netlong; |
| 1517 |
#endif |
| 1518 |
} |
| 1519 |
uint64_t |
| 1520 |
esl_ntoh64(uint64_t net_int64) |
| 1521 |
{ |
| 1522 |
#ifdef WORDS_BIGENDIAN |
| 1523 |
return net_int64; |
| 1524 |
#else |
| 1525 |
esl_byteswap((char *) &net_int64, 8); |
| 1526 |
return net_int64; |
| 1527 |
#endif |
| 1528 |
} |
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
uint16_t |
| 1540 |
esl_hton16(uint16_t hostshort) |
| 1541 |
{ |
| 1542 |
#ifdef WORDS_BIGENDIAN |
| 1543 |
return hostshort; |
| 1544 |
#else |
| 1545 |
esl_byteswap((char *) &hostshort, 2); |
| 1546 |
return hostshort; |
| 1547 |
#endif |
| 1548 |
} |
| 1549 |
uint32_t |
| 1550 |
esl_hton32(uint32_t hostlong) |
| 1551 |
{ |
| 1552 |
#ifdef WORDS_BIGENDIAN |
| 1553 |
return hostlong; |
| 1554 |
#else |
| 1555 |
esl_byteswap((char *) &hostlong, 4); |
| 1556 |
return hostlong; |
| 1557 |
#endif |
| 1558 |
} |
| 1559 |
uint64_t |
| 1560 |
esl_hton64(uint64_t host_int64) |
| 1561 |
{ |
| 1562 |
#ifdef WORDS_BIGENDIAN |
| 1563 |
return host_int64; |
| 1564 |
#else |
| 1565 |
esl_byteswap((char *) &host_int64, 8); |
| 1566 |
return host_int64; |
| 1567 |
#endif |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
int |
| 1583 |
esl_fread_u16(FILE *fp, uint16_t *ret_result) |
| 1584 |
{ |
| 1585 |
uint16_t result; |
| 1586 |
if (fread(&result, sizeof(uint16_t), 1, fp) != 1) return eslFAIL; |
| 1587 |
*ret_result = esl_ntoh16(result); |
| 1588 |
return eslOK; |
| 1589 |
} |
| 1590 |
int |
| 1591 |
esl_fread_u32(FILE *fp, uint32_t *ret_result) |
| 1592 |
{ |
| 1593 |
uint32_t result; |
| 1594 |
if (fread(&result, sizeof(uint32_t), 1, fp) != 1) return eslFAIL; |
| 1595 |
*ret_result = esl_ntoh32(result); |
| 1596 |
return eslOK; |
| 1597 |
} |
| 1598 |
int |
| 1599 |
esl_fread_u64(FILE *fp, uint64_t *ret_result) |
| 1600 |
{ |
| 1601 |
uint64_t result; |
| 1602 |
if (fread(&result, sizeof(uint64_t), 1, fp) != 1) return eslFAIL; |
| 1603 |
*ret_result = esl_ntoh64(result); |
| 1604 |
return eslOK; |
| 1605 |
} |
| 1606 |
int |
| 1607 |
esl_fread_i16(FILE *fp, int16_t *ret_result) |
| 1608 |
{ |
| 1609 |
int16_t result; |
| 1610 |
if (fread(&result, sizeof(int16_t), 1, fp) != 1) return eslFAIL; |
| 1611 |
*ret_result = (int16_t) esl_ntoh16((uint16_t) result); |
| 1612 |
return eslOK; |
| 1613 |
} |
| 1614 |
int |
| 1615 |
esl_fread_i32(FILE *fp, int32_t *ret_result) |
| 1616 |
{ |
| 1617 |
int32_t result; |
| 1618 |
if (fread(&result, sizeof(int32_t), 1, fp) != 1) return eslFAIL; |
| 1619 |
*ret_result = (int32_t) esl_ntoh32((uint32_t) result); |
| 1620 |
return eslOK; |
| 1621 |
} |
| 1622 |
int |
| 1623 |
esl_fread_i64(FILE *fp, int64_t *ret_result) |
| 1624 |
{ |
| 1625 |
int64_t result; |
| 1626 |
if (fread(&result, sizeof(int64_t), 1, fp) != 1) return eslFAIL; |
| 1627 |
*ret_result = (int64_t) esl_ntoh64((uint64_t) result); |
| 1628 |
return eslOK; |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
int |
| 1644 |
esl_fwrite_u16(FILE *fp, uint16_t n) |
| 1645 |
{ |
| 1646 |
n = esl_hton16(n); |
| 1647 |
if (fwrite(&n, sizeof(uint16_t), 1, fp) != 1) return eslFAIL; |
| 1648 |
return eslOK; |
| 1649 |
} |
| 1650 |
int |
| 1651 |
esl_fwrite_u32(FILE *fp, uint32_t n) |
| 1652 |
{ |
| 1653 |
n = esl_hton32(n); |
| 1654 |
if (fwrite(&n, sizeof(uint32_t), 1, fp) != 1) return eslFAIL; |
| 1655 |
return eslOK; |
| 1656 |
} |
| 1657 |
int |
| 1658 |
esl_fwrite_u64(FILE *fp, uint64_t n) |
| 1659 |
{ |
| 1660 |
n = esl_hton64(n); |
| 1661 |
if (fwrite(&n, sizeof(uint64_t), 1, fp) != 1) return eslFAIL; |
| 1662 |
return eslOK; |
| 1663 |
} |
| 1664 |
int |
| 1665 |
esl_fwrite_i16(FILE *fp, int16_t n) |
| 1666 |
{ |
| 1667 |
n = (int16_t) esl_hton16((uint16_t) n); |
| 1668 |
if (fwrite(&n, sizeof(int16_t), 1, fp) != 1) return eslFAIL; |
| 1669 |
return eslOK; |
| 1670 |
} |
| 1671 |
int |
| 1672 |
esl_fwrite_i32(FILE *fp, int32_t n) |
| 1673 |
{ |
| 1674 |
n = (int32_t) esl_hton32((uint32_t) n); |
| 1675 |
if (fwrite(&n, sizeof(int32_t), 1, fp) != 1) return eslFAIL; |
| 1676 |
return eslOK; |
| 1677 |
} |
| 1678 |
int |
| 1679 |
esl_fwrite_i64(FILE *fp, int64_t n) |
| 1680 |
{ |
| 1681 |
n = (int64_t) esl_hton64((uint64_t) n); |
| 1682 |
if (fwrite(&n, sizeof(int64_t), 1, fp) != 1) return eslFAIL; |
| 1683 |
return eslOK; |
| 1684 |
} |
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
int |
| 1723 |
esl_fread_offset(FILE *fp, int sz, off_t *ret_offset) |
| 1724 |
{ |
| 1725 |
int status; |
| 1726 |
uint32_t x32; |
| 1727 |
uint64_t x64; |
| 1728 |
|
| 1729 |
if (sz == 8) |
| 1730 |
{ |
| 1731 |
if (esl_fread_u64(fp, &x64) != eslOK) { status = eslFAIL; goto ERROR; } |
| 1732 |
if (sizeof(off_t) == 4 && x64 > INT32_MAX) |
| 1733 |
ESL_XEXCEPTION(eslEINCOMPAT, "can't read 64-bit off_t on this 32-bit host"); |
| 1734 |
*ret_offset = (off_t) x64; |
| 1735 |
} |
| 1736 |
else if (sz == 4) |
| 1737 |
{ |
| 1738 |
if (esl_fread_u32(fp, &x32) != eslOK) { status = eslFAIL; goto ERROR; } |
| 1739 |
*ret_offset = (off_t) x32; |
| 1740 |
} |
| 1741 |
else ESL_XEXCEPTION(eslEINVAL, "offsets must be 32 or 64 bits"); |
| 1742 |
return eslOK; |
| 1743 |
|
| 1744 |
ERROR: |
| 1745 |
*ret_offset = 0; |
| 1746 |
return status; |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
int |
| 1762 |
esl_fwrite_offset(FILE *fp, off_t offset) |
| 1763 |
{ |
| 1764 |
if (sizeof(off_t) == 4) return esl_fwrite_u32(fp, offset); |
| 1765 |
else if (sizeof(off_t) == 8) return esl_fwrite_u64(fp, offset); |
| 1766 |
else ESL_EXCEPTION(eslEINVAL, "off_t is neither 32-bit nor 64-bit"); |
| 1767 |
|
| 1768 |
return eslEINCONCEIVABLE; |
| 1769 |
} |
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
#ifdef eslSSI_TESTDRIVE |
| 1776 |
|
| 1777 |
#include "esl_arr2.h" |
| 1778 |
#include "esl_getopts.h" |
| 1779 |
#include "esl_sq.h" |
| 1780 |
#include "esl_sqio.h" |
| 1781 |
#include "esl_random.h" |
| 1782 |
#include "esl_randomseq.h" |
| 1783 |
|
| 1784 |
struct ssi_testdata { |
| 1785 |
int nfiles; |
| 1786 |
int nseq; |
| 1787 |
int maxL; |
| 1788 |
|
| 1789 |
char **sqfile; |
| 1790 |
char **seqname; |
| 1791 |
char **seqdesc; |
| 1792 |
int *seqlen; |
| 1793 |
char **seq; |
| 1794 |
}; |
| 1795 |
|
| 1796 |
static struct ssi_testdata * |
| 1797 |
ssi_testdata_create(ESL_RANDOMNESS *rng, int max_nfiles, int max_nseq, int maxL, int do_dupname) |
| 1798 |
{ |
| 1799 |
char msg[] = "esl_ssi test data creation failed"; |
| 1800 |
struct ssi_testdata *td; |
| 1801 |
double p[4] = { 0.25, 0.25, 0.25, 0.25 }; |
| 1802 |
FILE *fp; |
| 1803 |
ESL_SQ *sq; |
| 1804 |
int i,j; |
| 1805 |
|
| 1806 |
if ( (td = malloc(sizeof(struct ssi_testdata))) == NULL) esl_fatal(msg); |
| 1807 |
td->nfiles = 1 + esl_rnd_Roll(rng, max_nfiles); |
| 1808 |
td->nseq = 2 + esl_rnd_Roll(rng, max_nseq-1); |
| 1809 |
td->maxL = maxL; |
| 1810 |
|
| 1811 |
|
| 1812 |
if ( (td->sqfile = malloc(sizeof(char *) * td->nfiles)) == NULL) esl_fatal(msg); |
| 1813 |
for (j = 0; j < td->nfiles; j++) |
| 1814 |
if ( esl_sprintf(&(td->sqfile[j]), "esltmpXXXXXX" ) != eslOK) esl_fatal(msg); |
| 1815 |
|
| 1816 |
|
| 1817 |
if ( (td->seq = malloc(sizeof(char *) * td->nseq * td->nfiles)) == NULL) esl_fatal(msg); |
| 1818 |
if ( (td->seqname = malloc(sizeof(char *) * td->nseq * td->nfiles)) == NULL) esl_fatal(msg); |
| 1819 |
if ( (td->seqlen = malloc(sizeof(int) * td->nseq * td->nfiles)) == NULL) esl_fatal(msg); |
| 1820 |
if ( (td->seqdesc = malloc(sizeof(char *) * td->nseq * td->nfiles)) == NULL) esl_fatal(msg); |
| 1821 |
for (i = 0; i < td->nseq*td->nfiles; i++) |
| 1822 |
{ |
| 1823 |
td->seqlen[i] = 1 + esl_rnd_Roll(rng, td->maxL); |
| 1824 |
if ( (td->seq[i] = malloc(sizeof(char) * (td->seqlen[i]+1))) == NULL) esl_fatal(msg); |
| 1825 |
if ( esl_rsq_IID(rng, "ACGT", p, 4, td->seqlen[i], td->seq[i]) != eslOK) esl_fatal(msg); |
| 1826 |
if ( esl_sprintf(&(td->seqname[i]), "seq%d-file%d", i, i/td->nseq) != eslOK) esl_fatal(msg); |
| 1827 |
if ( esl_sprintf(&(td->seqdesc[i]), "desc%d-file%d", i, i/td->nseq) != eslOK) esl_fatal(msg); |
| 1828 |
} |
| 1829 |
|
| 1830 |
|
| 1831 |
if (do_dupname) |
| 1832 |
{ |
| 1833 |
i = esl_rnd_Roll(rng, td->nseq * td->nfiles - 1); |
| 1834 |
j = i + 1 + esl_rnd_Roll(rng, td->nseq * td->nfiles - i - 1); |
| 1835 |
if (esl_rnd_Roll(rng, 2) == 0) { |
| 1836 |
strcpy(td->seqname[i], "DUP"); |
| 1837 |
strcpy(td->seqname[j], "DUP"); |
| 1838 |
} else { |
| 1839 |
strcpy(td->seqdesc[i], "DUP"); |
| 1840 |
strcpy(td->seqdesc[j], "DUP"); |
| 1841 |
} |
| 1842 |
} |
| 1843 |
|
| 1844 |
|
| 1845 |
for (j = 0; j < td->nfiles; j++) |
| 1846 |
{ |
| 1847 |
if (esl_tmpfile_named(td->sqfile[j], &fp) != eslOK) esl_fatal(msg); |
| 1848 |
for (i = j*td->nseq; i < (j+1)*td->nseq; i++) |
| 1849 |
{ |
| 1850 |
if ( (sq = esl_sq_CreateFrom(td->seqname[i], td->seq[i], td->seqdesc[i], NULL, NULL)) == NULL) esl_fatal(msg); |
| 1851 |
if ( esl_sqio_Write(fp, sq, eslSQFILE_FASTA, FALSE) != eslOK) esl_fatal(msg); |
| 1852 |
esl_sq_Destroy(sq); |
| 1853 |
} |
| 1854 |
fclose(fp); |
| 1855 |
} |
| 1856 |
|
| 1857 |
return td; |
| 1858 |
} |
| 1859 |
|
| 1860 |
static void |
| 1861 |
ssi_testdata_destroy(struct ssi_testdata *td) |
| 1862 |
{ |
| 1863 |
int j; |
| 1864 |
for (j = 0; j < td->nfiles; j++) remove(td->sqfile[j]); |
| 1865 |
esl_arr2_Destroy((void **) td->sqfile, td->nfiles); |
| 1866 |
esl_arr2_Destroy((void **) td->seqname, td->nseq*td->nfiles); |
| 1867 |
esl_arr2_Destroy((void **) td->seqdesc, td->nseq*td->nfiles); |
| 1868 |
esl_arr2_Destroy((void **) td->seq, td->nseq*td->nfiles); |
| 1869 |
free(td->seqlen); |
| 1870 |
free(td); |
| 1871 |
} |
| 1872 |
|
| 1873 |
static void |
| 1874 |
utest_enchilada(ESL_GETOPTS *go, ESL_RANDOMNESS *rng, int do_external, int do_dupkeys) |
| 1875 |
{ |
| 1876 |
char msg[] = "esl_ssi whole enchilada test failed"; |
| 1877 |
struct ssi_testdata *td = NULL; |
| 1878 |
int nq = 10; |
| 1879 |
char *ssifile = NULL; |
| 1880 |
ESL_NEWSSI *ns = NULL; |
| 1881 |
ESL_SQFILE *sqfp = NULL; |
| 1882 |
ESL_SQ *sq = NULL; |
| 1883 |
uint16_t fh; |
| 1884 |
ESL_SSI *ssi = NULL; |
| 1885 |
char query[32]; |
| 1886 |
char *qfile; |
| 1887 |
int qfmt; |
| 1888 |
off_t roff; |
| 1889 |
int i,j; |
| 1890 |
int status; |
| 1891 |
|
| 1892 |
td = ssi_testdata_create(rng, |
| 1893 |
esl_opt_GetInteger(go, "-F"), |
| 1894 |
esl_opt_GetInteger(go, "-N"), |
| 1895 |
esl_opt_GetInteger(go, "-L"), |
| 1896 |
do_dupkeys); |
| 1897 |
|
| 1898 |
|
| 1899 |
if (esl_strdup(td->sqfile[0], -1, &ssifile) != eslOK) esl_fatal(msg); |
| 1900 |
if (esl_strcat(&ssifile, -1, ".ssi", 4) != eslOK) esl_fatal(msg); |
| 1901 |
if (esl_newssi_Open(ssifile, TRUE, &ns) != eslOK) esl_fatal(msg); |
| 1902 |
if ((sq = esl_sq_Create()) == NULL) esl_fatal(msg); |
| 1903 |
if (do_external) |
| 1904 |
if (activate_external_sort(ns) != eslOK) esl_fatal(msg); |
| 1905 |
for (j = 0; j < td->nfiles; j++) |
| 1906 |
{ |
| 1907 |
if (esl_sqfile_Open(td->sqfile[j], eslSQFILE_UNKNOWN, NULL, &sqfp) != eslOK) esl_fatal(msg); |
| 1908 |
if (esl_newssi_AddFile(ns, td->sqfile[j], sqfp->format, &fh) != eslOK) esl_fatal(msg); |
| 1909 |
while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) |
| 1910 |
{ |
| 1911 |
if (esl_newssi_AddKey (ns, sq->name, fh, sq->roff, sq->doff, sq->L) != eslOK) esl_fatal(msg); |
| 1912 |
if (esl_newssi_AddAlias(ns, sq->desc, sq->name) != eslOK) esl_fatal(msg); |
| 1913 |
esl_sq_Reuse(sq); |
| 1914 |
} |
| 1915 |
if (status != eslEOF) esl_fatal(msg); |
| 1916 |
esl_sqfile_Close(sqfp); |
| 1917 |
} |
| 1918 |
esl_sq_Destroy(sq); |
| 1919 |
|
| 1920 |
|
| 1921 |
status = esl_newssi_Write(ns); |
| 1922 |
if ( do_dupkeys && status != eslEDUP) esl_fatal(msg); |
| 1923 |
if (! do_dupkeys && status != eslOK) esl_fatal(msg); |
| 1924 |
esl_newssi_Close(ns); |
| 1925 |
|
| 1926 |
|
| 1927 |
if (! do_dupkeys) |
| 1928 |
{ |
| 1929 |
if (esl_ssi_Open(ssifile, &ssi) != eslOK) esl_fatal(msg); |
| 1930 |
sq = esl_sq_Create(); |
| 1931 |
while (nq--) |
| 1932 |
{ |
| 1933 |
|
| 1934 |
i = esl_rnd_Roll(rng, td->nseq*td->nfiles); |
| 1935 |
j = i/td->nseq; |
| 1936 |
if (esl_rnd_Roll(rng, 2) == 0) snprintf(query, 32, "seq%d-file%d", i, j); |
| 1937 |
else snprintf(query, 32, "desc%d-file%d", i, j); |
| 1938 |
|
| 1939 |
|
| 1940 |
if ( esl_ssi_FindName(ssi, query, &fh, &roff, NULL, NULL) != eslOK) esl_fatal(msg); |
| 1941 |
if ( esl_ssi_FileInfo(ssi, fh, &qfile, &qfmt) != eslOK) esl_fatal(msg); |
| 1942 |
if ( esl_sqfile_Open(qfile, qfmt, NULL, &sqfp) != eslOK) esl_fatal(msg); |
| 1943 |
if ( esl_sqfile_Position(sqfp, roff) != eslOK) esl_fatal(msg); |
| 1944 |
if ( esl_sqio_Read(sqfp, sq) != eslOK) esl_fatal(msg); |
| 1945 |
|
| 1946 |
|
| 1947 |
if (strcmp(sq->name, query) != 0 && strcmp(sq->desc, query) != 0) esl_fatal(msg); |
| 1948 |
if (sq->n != td->seqlen[i]) esl_fatal(msg); |
| 1949 |
if (strcmp(sq->seq, td->seq[i]) != 0) esl_fatal(msg); |
| 1950 |
if (strcmp(qfile, td->sqfile[j]) != 0) esl_fatal(msg); |
| 1951 |
|
| 1952 |
esl_sq_Reuse(sq); |
| 1953 |
esl_sqfile_Close(sqfp); |
| 1954 |
} |
| 1955 |
remove(ssifile); |
| 1956 |
esl_sq_Destroy(sq); |
| 1957 |
esl_ssi_Close(ssi); |
| 1958 |
} |
| 1959 |
|
| 1960 |
ssi_testdata_destroy(td); |
| 1961 |
free(ssifile); |
| 1962 |
} |
| 1963 |
#endif |
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
#ifdef eslSSI_TESTDRIVE |
| 1971 |
#include <esl_config.h> |
| 1972 |
|
| 1973 |
#include <stdlib.h> |
| 1974 |
#include <stdio.h> |
| 1975 |
#include <string.h> |
| 1976 |
|
| 1977 |
#include "easel.h" |
| 1978 |
#include "esl_getopts.h" |
| 1979 |
#include "esl_sq.h" |
| 1980 |
#include "esl_sqio.h" |
| 1981 |
#include "esl_ssi.h" |
| 1982 |
#include "esl_random.h" |
| 1983 |
#include "esl_randomseq.h" |
| 1984 |
|
| 1985 |
static ESL_OPTIONS options[] = { |
| 1986 |
|
| 1987 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1988 |
{ "-F", eslARG_INT, "3", NULL, NULL, NULL, NULL, NULL, "max number of test files", 0 }, |
| 1989 |
{ "-L", eslARG_INT, "1000", NULL, NULL, NULL, NULL, NULL, "max length of test sequences", 0 }, |
| 1990 |
{ "-N", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "max number of test sequences per file", 0 }, |
| 1991 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 1992 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1993 |
}; |
| 1994 |
static char usage[] = "[-options]"; |
| 1995 |
static char banner[] = "test driver for ssi module"; |
| 1996 |
|
| 1997 |
int |
| 1998 |
main(int argc, char **argv) |
| 1999 |
{ |
| 2000 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 2001 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 2002 |
|
| 2003 |
fprintf(stderr, "## %s\n", argv[0]); |
| 2004 |
fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 2005 |
|
| 2006 |
|
| 2007 |
utest_enchilada(go, rng, FALSE, FALSE); |
| 2008 |
utest_enchilada(go, rng, TRUE, FALSE); |
| 2009 |
utest_enchilada(go, rng, FALSE, TRUE); |
| 2010 |
utest_enchilada(go, rng, TRUE, TRUE); |
| 2011 |
|
| 2012 |
esl_randomness_Destroy(rng); |
| 2013 |
esl_getopts_Destroy(go); |
| 2014 |
|
| 2015 |
fprintf(stderr, "# status = ok\n"); |
| 2016 |
return eslOK; |
| 2017 |
} |
| 2018 |
#endif |
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
#ifdef eslSSI_EXAMPLE |
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
#include <stdio.h> |
| 2032 |
#include "easel.h" |
| 2033 |
#include "esl_ssi.h" |
| 2034 |
|
| 2035 |
int |
| 2036 |
main(int argc, char **argv) |
| 2037 |
{ |
| 2038 |
ESL_NEWSSI *ns; |
| 2039 |
char *fafile; |
| 2040 |
FILE *fp; |
| 2041 |
char *ssifile; |
| 2042 |
uint16_t fh; |
| 2043 |
char *buf = NULL; |
| 2044 |
int n = 0; |
| 2045 |
char *s, *seqname; |
| 2046 |
off_t seq_offset; |
| 2047 |
int status; |
| 2048 |
|
| 2049 |
|
| 2050 |
fafile = argv[1]; |
| 2051 |
esl_strdup(fafile, -1, &ssifile); |
| 2052 |
esl_strcat(&ssifile, -1, ".ssi", 4); |
| 2053 |
status = esl_newssi_Open(ssifile, FALSE, &ns); |
| 2054 |
if (status == eslENOTFOUND) esl_fatal("failed to open SSI index %s", ssifile); |
| 2055 |
else if (status == eslEOVERWRITE) esl_fatal("SSI index %s already exists; delete or rename it", ssifile); |
| 2056 |
else if (status != eslOK) esl_fatal("failed to create a new SSI index"); |
| 2057 |
|
| 2058 |
|
| 2059 |
if ((fp = fopen(fafile, "r")) == NULL) esl_fatal("failed to open %s", fafile); |
| 2060 |
if (esl_newssi_AddFile(ns, fafile, 1, &fh) != eslOK) esl_fatal("failed to add %s to index: %s", fafile, ns->errbuf); |
| 2061 |
seq_offset = ftello(fp); |
| 2062 |
while (esl_fgets(&buf, &n, fp) == eslOK) |
| 2063 |
{ |
| 2064 |
if (*buf == '>') { |
| 2065 |
s = buf+1; |
| 2066 |
esl_strtok(&s, " \t\n", &seqname); |
| 2067 |
if (esl_newssi_AddKey(ns, seqname, fh, seq_offset, 0, 0) != eslOK) |
| 2068 |
esl_fatal("failed to add key %s to index: %s", seqname, ns->errbuf); |
| 2069 |
} |
| 2070 |
seq_offset = ftello(fp); |
| 2071 |
} |
| 2072 |
free(buf); |
| 2073 |
fclose(fp); |
| 2074 |
|
| 2075 |
|
| 2076 |
status = esl_newssi_Write(ns); |
| 2077 |
if (status == eslEDUP) esl_fatal("SSI index construction failed:\n %s", ns->errbuf); |
| 2078 |
else if (status == eslERANGE) esl_fatal("SSI index file size exceeds maximum allowed by your filesystem"); |
| 2079 |
else if (status == eslESYS) esl_fatal("SSI index sort failed:\n %s", ns->errbuf); |
| 2080 |
else if (status != eslOK) esl_fatal("SSI indexing failed:\n %s", ns->errbuf); |
| 2081 |
esl_newssi_Close(ns); |
| 2082 |
free(ssifile); |
| 2083 |
return 0; |
| 2084 |
} |
| 2085 |
|
| 2086 |
#endif |
| 2087 |
|
| 2088 |
|
| 2089 |
#ifdef eslSSI_EXAMPLE2 |
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
|
| 2094 |
#include <stdio.h> |
| 2095 |
#include "easel.h" |
| 2096 |
#include "esl_ssi.h" |
| 2097 |
|
| 2098 |
int main(int argc, char **argv) |
| 2099 |
{ |
| 2100 |
ESL_SSI *ssi; |
| 2101 |
char *seqname; |
| 2102 |
char *ssifile; |
| 2103 |
uint16_t fh; |
| 2104 |
char *fafile; |
| 2105 |
int fmt; |
| 2106 |
off_t offset; |
| 2107 |
FILE *fp; |
| 2108 |
char *buf = NULL; |
| 2109 |
int n = 0; |
| 2110 |
|
| 2111 |
seqname = argv[1]; |
| 2112 |
ssifile = argv[2]; |
| 2113 |
|
| 2114 |
if (esl_ssi_Open(ssifile, &ssi) != eslOK) esl_fatal("open failed"); |
| 2115 |
if (esl_ssi_FindName(ssi, seqname, &fh, &offset, NULL, NULL) != eslOK) esl_fatal("find failed"); |
| 2116 |
if (esl_ssi_FileInfo(ssi, fh, &fafile, &fmt) != eslOK) esl_fatal("info failed"); |
| 2117 |
|
| 2118 |
|
| 2119 |
if ((fp = fopen(fafile, "r")) == NULL) esl_fatal("failed to open %s", fafile); |
| 2120 |
if (fseeko(fp, offset, SEEK_SET) != 0) esl_fatal("failed to position %s", fafile); |
| 2121 |
if (esl_fgets(&buf, &n, fp) != eslOK) esl_fatal("failed to get name/desc line"); |
| 2122 |
do { |
| 2123 |
printf("%s", buf); |
| 2124 |
} while (esl_fgets(&buf, &n, fp) == eslOK && *buf != '>'); |
| 2125 |
|
| 2126 |
esl_ssi_Close(ssi); |
| 2127 |
fclose(fp); |
| 2128 |
free(buf); |
| 2129 |
return 0; |
| 2130 |
} |
| 2131 |
|
| 2132 |
#endif |
| 2133 |
|