Towards GPL
[idzebra-moved-to-github.git] / index / sortidx.c
1 /* $Id: sortidx.c,v 1.7 2002-08-02 19:26:55 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24  
25 #include <string.h>
26
27 #include <yaz/log.h>
28 #include <bfile.h>
29 #include <sortidx.h>
30
31 #define SORT_IDX_BLOCKSIZE 64
32
33 struct sortFileHead {
34     int sysno_max;
35 };
36
37 struct sortFile {
38     int type;
39     BFile bf;
40     struct sortFile *next;
41     struct sortFileHead head;
42 };
43
44 struct sortIdx {
45     BFiles bfs;
46     int write_flag;
47     int sysno;
48     char *entry_buf;
49     struct sortFile *current_file;
50     struct sortFile *files;
51 };
52
53 SortIdx sortIdx_open (BFiles bfs, int write_flag)
54 {
55     SortIdx si = (SortIdx) xmalloc (sizeof(*si));
56     si->bfs = bfs;
57     si->write_flag = write_flag;
58     si->current_file = NULL;
59     si->files = NULL;
60     si->entry_buf = (char *) xmalloc (SORT_IDX_ENTRYSIZE);
61     return si;
62 }
63
64 void sortIdx_close (SortIdx si)
65 {
66     struct sortFile *sf = si->files;
67     while (sf)
68     {
69         struct sortFile *sf_next = sf->next;
70         if (sf->bf)
71             bf_close (sf->bf);
72         xfree (sf);
73         sf = sf_next;
74     }
75     xfree (si->entry_buf);
76     xfree (si);
77 }
78
79 int sortIdx_type (SortIdx si, int type)
80 {
81     char fname[80];
82     struct sortFile *sf;
83     if (si->current_file && si->current_file->type == type)
84         return 0;
85     for (sf = si->files; sf; sf = sf->next)
86         if (sf->type == type)
87         {
88             si->current_file = sf;
89             return 0;
90         }
91     sf = (struct sortFile *) xmalloc (sizeof(*sf));
92     sf->type = type;
93     sf->bf = NULL;
94     sprintf (fname, "sort%d", type);
95     logf (LOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
96     sf->bf = bf_open (si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
97     if (!sf->bf)
98     {
99         xfree (sf);
100         return -1;
101     }
102     if (!bf_read (sf->bf, 0, 0, sizeof(sf->head), &sf->head))
103     {
104         sf->head.sysno_max = 0;
105         if (!si->write_flag)
106         {
107             bf_close (sf->bf);
108             xfree (sf);
109             return -1;
110         }
111     }
112     sf->next = si->files;
113     si->current_file = si->files = sf;
114     return 0;
115 }
116
117 void sortIdx_sysno (SortIdx si, int sysno)
118 {
119     si->sysno = sysno;
120 }
121
122 void sortIdx_add (SortIdx si, const char *buf, int len)
123 {
124     if (!si->current_file || !si->current_file->bf)
125         return;
126     if (len > SORT_IDX_ENTRYSIZE)
127     {
128         len = SORT_IDX_ENTRYSIZE;
129         memcpy (si->entry_buf, buf, len);
130     }
131     else
132     {
133         memcpy (si->entry_buf, buf, len);
134         memset (si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
135     }
136     bf_write (si->current_file->bf, si->sysno+1, 0, 0, si->entry_buf);
137 }
138
139 void sortIdx_read (SortIdx si, char *buf)
140 {
141     bf_read (si->current_file->bf, si->sysno+1, 0, 0, buf);
142 }