1 /* $Id: recindex.c,v 1.58 2007-11-23 13:52:52 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <yaz/yaz-util.h>
31 #define RIDX_CHUNK 128
39 recindex_t recindex_open(BFiles bfs, int rw)
41 recindex_t p = xmalloc(sizeof(*p));
42 p->index_fname = "reci";
43 p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
44 if (p->index_BFile == NULL)
46 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
53 void recindex_close(recindex_t p)
58 bf_close(p->index_BFile);
63 int recindex_read_head(recindex_t p, void *buf)
65 return bf_read(p->index_BFile, 0, 0, 0, buf);
68 const char *recindex_get_fname(recindex_t p)
70 return p->index_fname;
73 ZEBRA_RES recindex_write_head(recindex_t p, const void *buf, size_t len)
78 assert(p->index_BFile);
80 r = bf_write(p->index_BFile, 0, 0, len, buf);
83 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
89 int recindex_read_indx(recindex_t p, zint sysno, void *buf, int itemsize,
93 zint pos = (sysno-1)*itemsize;
94 int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK);
95 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
98 sz1 = itemsize; /* no more than itemsize bytes */
100 r = bf_read(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
101 if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
102 r = bf_read(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
104 if (r != 1 && !ignoreError)
106 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
107 p->index_fname, (long) pos);
112 void recindex_write_indx(recindex_t p, zint sysno, void *buf, int itemsize)
114 zint pos = (sysno-1)*itemsize;
115 int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK);
116 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
119 sz1 = itemsize; /* no more than itemsize bytes */
121 bf_write(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
122 if (sz1 < itemsize) /* boundary? must write second part */
123 bf_write(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
131 * indent-tabs-mode: nil
133 * vim: shiftwidth=4 tabstop=8 expandtab