Factor records system mgt into recindex.c, records.c.
[idzebra-moved-to-github.git] / index / recindex.c
1 /* $Id: recindex.c,v 1.58 2007-11-23 13:52:52 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <string.h>
27
28 #include <yaz/yaz-util.h>
29 #include "recindex.h"
30
31 #define RIDX_CHUNK 128
32
33
34 struct recindex {
35     char *index_fname;
36     BFile index_BFile;
37 };
38
39 recindex_t recindex_open(BFiles bfs, int rw)
40 {
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)
45     {
46         yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
47         xfree(p);
48         return 0;
49     }
50     return p;
51 }
52
53 void recindex_close(recindex_t p)
54 {
55     if (p)
56     {
57         if (p->index_BFile)
58             bf_close(p->index_BFile);
59         xfree(p);
60     }
61 }
62
63 int recindex_read_head(recindex_t p, void *buf)
64 {
65     return bf_read(p->index_BFile, 0, 0, 0, buf);
66 }
67
68 const char *recindex_get_fname(recindex_t p)
69 {
70     return p->index_fname;
71 }
72
73 ZEBRA_RES recindex_write_head(recindex_t p, const void *buf, size_t len)
74 {
75     int r;
76
77     assert(p);
78     assert(p->index_BFile);
79
80     r = bf_write(p->index_BFile, 0, 0, len, buf);
81     if (r)
82     {
83         yaz_log(YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
84         return ZEBRA_FAIL;
85     }
86     return ZEBRA_OK;
87 }
88
89 int recindex_read_indx(recindex_t p, zint sysno, void *buf, int itemsize, 
90                        int ignoreError)
91 {
92     int r;
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.. */
96
97     if (sz1 > itemsize)
98         sz1 = itemsize;  /* no more than itemsize bytes */
99
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,
103                         (char*) buf + sz1);
104     if (r != 1 && !ignoreError)
105     {
106         yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
107                 p->index_fname, (long) pos);
108     }
109     return r;
110 }
111
112 void recindex_write_indx(recindex_t p, zint sysno, void *buf, int itemsize)
113 {
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.. */
117
118     if (sz1 > itemsize)
119         sz1 = itemsize;  /* no more than itemsize bytes */
120
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,
124                 (char*) buf + sz1);
125 }
126
127
128 /*
129  * Local variables:
130  * c-basic-offset: 4
131  * indent-tabs-mode: nil
132  * End:
133  * vim: shiftwidth=4 tabstop=8 expandtab
134  */
135