-/*
- * Copyright (C) 1994-1995, Index Data I/S
- * All rights reserved.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Log: recindex.c,v $
- * Revision 1.1 1995-11-15 14:46:20 adam
- * Started work on better record management system.
- *
- */
+/* $Id: recindex.c,v 1.58 2007-11-23 13:52:52 adam Exp $
+ Copyright (C) 1995-2007
+ Index Data ApS
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+*/
+
#include <stdio.h>
+#include <stdlib.h>
#include <assert.h>
#include <string.h>
-#include <ctype.h>
-#include <assert.h>
-#include <fcntl.h>
-#include <unistd.h>
+#include <yaz/yaz-util.h>
#include "recindex.h"
-#define REC_HEAD_MAGIC "rechead"
+#define RIDX_CHUNK 128
-static void rec_write_head (Records p)
-{
- int r;
- assert (p);
- assert (p->fd != -1);
- if (lseek (p->fd, (off_t) 0, SEEK_SET) == -1)
- {
- logf (LOG_FATAL|LOG_ERRNO, "lseek to 0 in %s", p->fname);
- exit (1);
- }
- r = write (p->fd, &p->head, sizeof(p->head));
- switch (r)
+struct recindex {
+ char *index_fname;
+ BFile index_BFile;
+};
+
+recindex_t recindex_open(BFiles bfs, int rw)
+{
+ recindex_t p = xmalloc(sizeof(*p));
+ p->index_fname = "reci";
+ p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
+ if (p->index_BFile == NULL)
{
- case -1:
- logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->fname);
- exit (1);
- case sizeof(p->head):
- break;
- default:
- logf (LOG_FATAL, "write head of %s. wrote %d", p->fname, r);
- exit (1);
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
+ xfree(p);
+ return 0;
}
+ return p;
}
-Records rec_open (int rw)
+void recindex_close(recindex_t p)
{
- Records p;
- int r;
-
- if (!(p = malloc (sizeof(*p))))
- {
- logf (LOG_FATAL|LOG_ERRNO, "malloc");
- exit (1);
- }
- p->fname = "recindex";
- p->fd = open (p->fname, rw ? (O_RDWR|O_CREAT) : O_RDONLY, 0666);
- if (p->fd == -1)
- {
- logf (LOG_FATAL|LOG_ERRNO, "open %s", p->fname);
- exit (1);
- }
- r = read (p->fd, &p->head, sizeof(p->head));
- switch (r)
+ if (p)
{
- case -1:
- logf (LOG_FATAL|LOG_ERRNO, "read %s", p->fname);
- exit (1);
- case 0:
- p->head.no_records = 0;
- p->head.freelist = 0;
- if (rw)
- rec_write_head (p);
- break;
- case sizeof(p->head):
- if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
- {
- logf (LOG_FATAL, "read %s. bad header", p->fname);
- exit (1);
- }
- break;
- default:
- logf (LOG_FATAL, "read head of %s. expected %d. got %d",
- p->fname, sizeof(p->head), r);
- exit (1);
+ if (p->index_BFile)
+ bf_close(p->index_BFile);
+ xfree(p);
}
- return p;
}
-void rec_close (Records p)
+int recindex_read_head(recindex_t p, void *buf)
{
- if (p->fd != -1)
- close (p->fd);
- free (p);
+ return bf_read(p->index_BFile, 0, 0, 0, buf);
}
-Record rec_get (Records p, int sysno)
+const char *recindex_get_fname(recindex_t p)
{
- assert (p);
- return NULL;
+ return p->index_fname;
}
-Record rec_new (Records p)
+ZEBRA_RES recindex_write_head(recindex_t p, const void *buf, size_t len)
{
- assert (p);
- return NULL;
+ int r;
+
+ assert(p);
+ assert(p->index_BFile);
+
+ r = bf_write(p->index_BFile, 0, 0, len, buf);
+ if (r)
+ {
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
+ return ZEBRA_FAIL;
+ }
+ return ZEBRA_OK;
}
-void rec_put (Records p, Record rec)
+int recindex_read_indx(recindex_t p, zint sysno, void *buf, int itemsize,
+ int ignoreError)
{
- assert (p);
+ int r;
+ zint pos = (sysno-1)*itemsize;
+ int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK);
+ int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
+
+ if (sz1 > itemsize)
+ sz1 = itemsize; /* no more than itemsize bytes */
+
+ r = bf_read(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
+ if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
+ r = bf_read(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
+ (char*) buf + sz1);
+ if (r != 1 && !ignoreError)
+ {
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
+ p->index_fname, (long) pos);
+ }
+ return r;
}
+
+void recindex_write_indx(recindex_t p, zint sysno, void *buf, int itemsize)
+{
+ zint pos = (sysno-1)*itemsize;
+ int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK);
+ int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
+
+ if (sz1 > itemsize)
+ sz1 = itemsize; /* no more than itemsize bytes */
+
+ bf_write(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
+ if (sz1 < itemsize) /* boundary? must write second part */
+ bf_write(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
+ (char*) buf + sz1);
+}
+
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+