Update copyright year + FSF address
[idzebra-moved-to-github.git] / index / sortidx.c
1 /* $Id: sortidx.c,v 1.18 2006-08-14 10:40:15 adam Exp $
2    Copyright (C) 1995-2006
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
24 #include <assert.h> 
25 #include <string.h>
26
27 #include <yaz/log.h>
28 #include <yaz/xmalloc.h>
29 #include <idzebra/bfile.h>
30 #include <sortidx.h>
31 #include "recindex.h"
32
33 #define SORT_IDX_BLOCKSIZE 64
34
35 struct sortFileHead {
36     SYSNO sysno_max;
37 };
38
39 struct sortFile {
40     int type;
41     BFile bf;
42     struct sortFile *next;
43     struct sortFileHead head;
44 };
45
46 struct sortIdx {
47     BFiles bfs;
48     int write_flag;
49     SYSNO sysno;
50     char *entry_buf;
51     struct sortFile *current_file;
52     struct sortFile *files;
53 };
54
55 SortIdx sortIdx_open (BFiles bfs, int write_flag)
56 {
57     SortIdx si = (SortIdx) xmalloc (sizeof(*si));
58     si->bfs = bfs;
59     si->write_flag = write_flag;
60     si->current_file = NULL;
61     si->files = NULL;
62     si->entry_buf = (char *) xmalloc (SORT_IDX_ENTRYSIZE);
63     return si;
64 }
65
66 void sortIdx_close (SortIdx si)
67 {
68     struct sortFile *sf = si->files;
69     while (sf)
70     {
71         struct sortFile *sf_next = sf->next;
72         if (sf->bf)
73             bf_close (sf->bf);
74         xfree (sf);
75         sf = sf_next;
76     }
77     xfree (si->entry_buf);
78     xfree (si);
79 }
80
81 int sortIdx_type (SortIdx si, int type)
82 {
83     char fname[80];
84     struct sortFile *sf;
85     if (si->current_file && si->current_file->type == type)
86         return 0;
87     for (sf = si->files; sf; sf = sf->next)
88         if (sf->type == type)
89         {
90             si->current_file = sf;
91             return 0;
92         }
93     sf = (struct sortFile *) xmalloc (sizeof(*sf));
94     sf->type = type;
95     sf->bf = NULL;
96     sprintf (fname, "sort%d", type);
97     yaz_log (YLOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
98     sf->bf = bf_open (si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
99     if (!sf->bf)
100     {
101         xfree (sf);
102         return -1;
103     }
104     if (!bf_read (sf->bf, 0, 0, sizeof(sf->head), &sf->head))
105     {
106         sf->head.sysno_max = 0;
107         if (!si->write_flag)
108         {
109             bf_close (sf->bf);
110             xfree (sf);
111             return -1;
112         }
113     }
114     sf->next = si->files;
115     si->current_file = si->files = sf;
116     return 0;
117 }
118
119 void sortIdx_sysno (SortIdx si, SYSNO sysno)
120 {
121     si->sysno = rec_sysno_to_int(sysno);
122 }
123
124 void sortIdx_add (SortIdx si, const char *buf, int len)
125 {
126     if (!si->current_file || !si->current_file->bf)
127         return;
128     if (len > SORT_IDX_ENTRYSIZE)
129     {
130         len = SORT_IDX_ENTRYSIZE;
131         memcpy (si->entry_buf, buf, len);
132     }
133     else
134     {
135         memcpy (si->entry_buf, buf, len);
136         memset (si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
137     }
138     bf_write (si->current_file->bf, si->sysno+1, 0, 0, si->entry_buf);
139 }
140
141 void sortIdx_read (SortIdx si, char *buf)
142 {
143     int r;
144
145     assert(si->current_file);
146     r = bf_read (si->current_file->bf, si->sysno+1, 0, 0, buf);
147     if (!r)
148         memset (buf, 0, SORT_IDX_ENTRYSIZE);
149 }
150 /*
151  * Local variables:
152  * c-basic-offset: 4
153  * indent-tabs-mode: nil
154  * End:
155  * vim: shiftwidth=4 tabstop=8 expandtab
156  */
157