- Records p;
- int i, r;
- int version;
- ZEBRA_RES ret = ZEBRA_OK;
-
- p = (Records) xmalloc(sizeof(*p));
- p->compression_method = compression_method;
- p->rw = rw;
- p->tmp_size = 1024;
- p->index_fname = "reci";
- p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
- if (p->index_BFile == NULL)
- {
- yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
- xfree(p);
- return 0;
- }
- p->tmp_buf = (char *) xmalloc(p->tmp_size);
- r = bf_read(p->index_BFile, 0, 0, 0, p->tmp_buf);
- switch (r)
- {
- case 0:
- memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
- sprintf(p->head.version, "%3d", REC_VERSION);
- p->head.index_free = 0;
- p->head.index_last = 1;
- p->head.no_records = 0;
- p->head.total_bytes = 0;
- for (i = 0; i<REC_BLOCK_TYPES; i++)
- {
- p->head.block_free[i] = 0;
- p->head.block_last[i] = 1;
- p->head.block_used[i] = 0;
- }
- p->head.block_size[0] = 128;
- p->head.block_move[0] = 0;
- for (i = 1; i<REC_BLOCK_TYPES; i++)
- {
- p->head.block_size[i] = p->head.block_size[i-1] * 4;
- p->head.block_move[i] = p->head.block_size[i] * 24;
- }
- if (rw)
- {
- if (rec_write_head(p) != ZEBRA_OK)
- ret = ZEBRA_FAIL;
- }
- break;
- case 1:
- memcpy(&p->head, p->tmp_buf, sizeof(p->head));
- if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
- {
- yaz_log(YLOG_FATAL, "file %s has bad format", p->index_fname);
- ret = ZEBRA_FAIL;
- }
- version = atoi(p->head.version);
- if (version != REC_VERSION)
- {
- yaz_log(YLOG_FATAL, "file %s is version %d, but version"
- " %d is required", p->index_fname, version, REC_VERSION);
- ret = ZEBRA_FAIL;
- }
- break;
- }
- for (i = 0; i<REC_BLOCK_TYPES; i++)
- {
- char str[80];
- sprintf(str, "recd%c", i + 'A');
- p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
- strcpy(p->data_fname[i], str);
- p->data_BFile[i] = NULL;
- }
- for (i = 0; i<REC_BLOCK_TYPES; i++)
- {
- if (!(p->data_BFile[i] = bf_open(bfs, p->data_fname[i],
- (int) (p->head.block_size[i]),
- rw)))
- {
- yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
- ret = ZEBRA_FAIL;
- }
- }
- p->cache_max = 400;
- p->cache_cur = 0;
- p->record_cache = (struct record_cache_entry *)
- xmalloc(sizeof(*p->record_cache)*p->cache_max);
- zebra_mutex_init(&p->mutex);
- if (ret == ZEBRA_FAIL)
- rec_close(&p);
- return p;