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