Moved isam headers to include/idzebra.
[idzebra-moved-to-github.git] / isams / isams.c
1 /* $Id: isams.c,v 1.9 2004-12-08 14:02:37 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 #include <stdlib.h>
25 #include <assert.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <yaz/ylog.h>
30 #include <idzebra/isams.h>
31
32 typedef struct {
33     int last_offset;
34     int last_block;
35 } ISAMS_head;
36
37 typedef unsigned ISAMS_BLOCK_SIZE;
38
39 struct ISAMS_s {
40     ISAMS_M *method;
41     ISAMS_head head;
42     ISAMS_head head_old;
43     char *merge_buf;
44
45     int block_size;
46     int debug;
47     BFile bf;
48 }; 
49
50 struct ISAMS_PP_s {
51     ISAMS is;
52     char *buf;
53     int block_offset;
54     int block_no;
55     void *decodeClientData;
56     int numKeys;
57     int numRead;
58 };
59
60 void isams_getmethod (ISAMS_M *m)
61 {
62     m->codec.start = NULL;
63     m->codec.decode = NULL;
64     m->codec.encode = NULL;
65     m->codec.stop = NULL;
66     m->codec.reset = NULL;
67
68     m->compare_item = NULL;
69     m->log_item = NULL;
70
71     m->debug = 1;
72     m->block_size = 128;
73 }
74
75 ISAMS isams_open (BFiles bfs, const char *name, int writeflag,
76                   ISAMS_M *method)
77 {
78     ISAMS is = (ISAMS) xmalloc (sizeof(*is));
79
80     is->method = (ISAMS_M *) xmalloc (sizeof(*is->method));
81     memcpy (is->method, method, sizeof(*method));
82     is->block_size = is->method->block_size;
83     is->debug = is->method->debug;
84
85     is->bf = bf_open (bfs, name, is->block_size, writeflag);
86
87     if (!bf_read (is->bf, 0, 0, sizeof(ISAMS_head), &is->head))
88     {
89         is->head.last_block = 1;
90         is->head.last_offset = 0;
91     }
92     memcpy (&is->head_old, &is->head, sizeof(is->head));
93     is->merge_buf = (char *) xmalloc(2*is->block_size);
94     memset(is->merge_buf, 0, 2*is->block_size);
95     return is;
96 }
97
98 int isams_close (ISAMS is)
99 {
100     if (memcmp(&is->head, &is->head_old, sizeof(is->head)))
101     {
102         if (is->head.last_offset)
103             bf_write(is->bf, is->head.last_block, 0, is->head.last_offset,
104                      is->merge_buf);
105         bf_write (is->bf, 0, 0, sizeof(is->head), &is->head);
106     }
107     bf_close (is->bf);
108     xfree (is->merge_buf);
109     xfree (is->method);
110     xfree (is);
111     return 0;
112 }
113
114 ISAMS_P isams_merge (ISAMS is, ISAMS_I data)
115 {
116     char i_item[128];
117     int i_more, i_mode;
118     void *r_clientData;
119     int first_block = is->head.last_block;
120     int first_offset = is->head.last_offset;
121     int count = 0;
122
123     r_clientData = (*is->method->codec.start)();
124
125     is->head.last_offset += sizeof(int);
126     if (is->head.last_offset > is->block_size)
127     {
128         if (is->debug > 2)
129             yaz_log (YLOG_LOG, "first_block=%d", first_block);
130         bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
131         (is->head.last_block)++;
132         is->head.last_offset -= is->block_size;
133         memcpy (is->merge_buf, is->merge_buf + is->block_size,
134                 is->head.last_offset);
135     }
136     while (1)
137     {
138         char *tmp_ptr = i_item;
139         i_more = (*data->read_item)(data->clientData, &tmp_ptr, &i_mode);
140         assert (i_mode);
141         
142         if (!i_more)
143             break;
144         else
145         {
146             char *r_out_ptr = is->merge_buf + is->head.last_offset;
147             
148             const char *i_item_ptr = i_item;
149             (*is->method->codec.encode)(r_clientData, &r_out_ptr, &i_item_ptr);
150             is->head.last_offset = r_out_ptr - is->merge_buf;
151             if (is->head.last_offset > is->block_size)
152             {
153                 bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
154                 (is->head.last_block)++;
155                 is->head.last_offset -= is->block_size;
156                 memcpy (is->merge_buf, is->merge_buf + is->block_size,
157                         is->head.last_offset);
158             }
159             count++;
160         }
161     }
162     (*is->method->codec.stop)(r_clientData);
163     if (first_block == is->head.last_block)
164         memcpy(is->merge_buf + first_offset, &count, sizeof(int));
165     else if (first_block == is->head.last_block-1)
166     {
167         int gap = first_offset + sizeof(int) - is->block_size;
168         assert (gap <= (int) sizeof(int));
169         if (gap > 0)
170         {
171             if (gap < (int) sizeof(int))
172                 bf_write(is->bf, first_block, first_offset, sizeof(int)-gap,
173                          &count);
174             memcpy (is->merge_buf, ((char*)&count)+(sizeof(int)-gap), gap);
175         }
176         else
177             bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
178     }
179     else
180     {
181         bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
182     }
183     return first_block * is->block_size + first_offset;
184 }
185
186 ISAMS_PP isams_pp_open (ISAMS is, ISAMS_P pos)
187 {
188     ISAMS_PP pp = (ISAMS_PP) xmalloc (sizeof(*pp));
189
190     if (is->debug > 1)
191         yaz_log (YLOG_LOG, "isams: isams_pp_open pos=%ld", (long) pos);
192     pp->is = is;
193     pp->decodeClientData = (*is->method->codec.start)();
194     pp->numKeys = 0;
195     pp->numRead = 0;
196     pp->buf = (char *) xmalloc(is->block_size*2);
197     pp->block_no = (int) (pos/is->block_size);
198     pp->block_offset = (int) (pos - pp->block_no * is->block_size);
199     if (is->debug)
200         yaz_log (YLOG_LOG, "isams: isams_pp_open off=%d no=%d",
201               pp->block_offset, pp->block_no);
202     if (pos)
203     {
204         bf_read (is->bf, pp->block_no, 0, 0, pp->buf);
205         bf_read (is->bf, pp->block_no+1, 0, 0, pp->buf + is->block_size);
206         memcpy(&pp->numKeys, pp->buf + pp->block_offset, sizeof(int));
207         if (is->debug)
208             yaz_log (YLOG_LOG, "isams: isams_pp_open numKeys=%d", pp->numKeys);
209         pp->block_offset += sizeof(int);
210     }
211     return pp;
212 }
213
214 void isams_pp_close (ISAMS_PP pp)
215 {
216     (*pp->is->method->codec.stop)(pp->decodeClientData);
217     xfree(pp->buf);
218     xfree(pp);
219 }
220
221 int isams_pp_num (ISAMS_PP pp)
222 {
223     return pp->numKeys;
224 }
225
226 int isams_pp_read (ISAMS_PP pp, void *buf)
227 {
228     char *cp = buf;
229     return isams_read_item (pp, &cp);
230 }
231
232 int isams_read_item (ISAMS_PP pp, char **dst)
233 {
234     const char *src;
235     if (pp->numRead >= pp->numKeys)
236         return 0;
237     (pp->numRead)++;
238     if (pp->block_offset > pp->is->block_size)
239     {
240         pp->block_offset -= pp->is->block_size;
241         (pp->block_no)++;
242         memcpy (pp->buf, pp->buf + pp->is->block_size, pp->is->block_size);
243         bf_read (pp->is->bf, pp->block_no+1, 0, 0,
244                  pp->buf + pp->is->block_size);
245     }
246     src = pp->buf + pp->block_offset;
247     (*pp->is->method->codec.decode)(pp->decodeClientData, dst, &src);
248     pp->block_offset = src - pp->buf; 
249     return 1;
250 }
251
252