- p = xmalloc (sizeof(*p));
- p->rw = rw;
- p->tmp_size = 1024;
- p->tmp_buf = xmalloc (p->tmp_size);
- p->index_fname = "recindex";
- p->index_BFile = bf_open (p->index_fname, 128, rw);
- if (p->index_BFile == NULL)
- {
- logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
- exit (1);
- }
- 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));
- p->head.index_free = 0;
- p->head.index_last = 1;
- p->head.no_records = 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] * 3;
- }
- if (rw)
- rec_write_head (p);
- break;
- case 1:
- memcpy (&p->head, p->tmp_buf, sizeof(p->head));
- if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
- {
- logf (LOG_FATAL, "read %s. bad header", p->index_fname);
- exit (1);
- }
- break;
- }
- for (i = 0; i<REC_BLOCK_TYPES; i++)
- {
- char str[80];
- sprintf (str, "recdata%c", i + 'A');
- p->data_fname[i] = 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 (p->data_fname[i],
- p->head.block_size[i],
- rw)))
- {
- logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
- exit (1);
- }
- }
- p->cache_max = 10;
- p->cache_cur = 0;
- p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
- return p;
-}