7994294ed36beb85d339ec37a5e74d6022d89a5d
[idzebra-moved-to-github.git] / isams / isams.c
1 /* $Id: isams.c,v 1.5 2004-06-01 12:56:39 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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/log.h>
30 #include <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->code_start = NULL;
63     m->code_item = NULL;
64     m->code_stop = NULL;
65
66     m->compare_item = NULL;
67     m->log_item = NULL;
68
69     m->debug = 1;
70     m->block_size = 128;
71 }
72
73 ISAMS isams_open (BFiles bfs, const char *name, int writeflag,
74                   ISAMS_M *method)
75 {
76     ISAMS is = (ISAMS) xmalloc (sizeof(*is));
77
78     is->method = (ISAMS_M *) xmalloc (sizeof(*is->method));
79     memcpy (is->method, method, sizeof(*method));
80     is->block_size = is->method->block_size;
81     is->debug = is->method->debug;
82
83     is->bf = bf_open (bfs, name, is->block_size, writeflag);
84
85     if (!bf_read (is->bf, 0, 0, sizeof(ISAMS_head), &is->head))
86     {
87         is->head.last_block = 1;
88         is->head.last_offset = 0;
89     }
90     memcpy (&is->head_old, &is->head, sizeof(is->head));
91     is->merge_buf = (char *) xmalloc(2*is->block_size);
92     memset(is->merge_buf, 0, 2*is->block_size);
93     return is;
94 }
95
96 int isams_close (ISAMS is)
97 {
98     if (memcmp(&is->head, &is->head_old, sizeof(is->head)))
99     {
100         if (is->head.last_offset)
101             bf_write(is->bf, is->head.last_block, 0, is->head.last_offset,
102                      is->merge_buf);
103         bf_write (is->bf, 0, 0, sizeof(is->head), &is->head);
104     }
105     bf_close (is->bf);
106     xfree (is->merge_buf);
107     xfree (is->method);
108     xfree (is);
109     return 0;
110 }
111
112 ISAMS_P isams_merge (ISAMS is, ISAMS_I data)
113 {
114     char i_item[128], *i_item_ptr;
115     int i_more, i_mode;
116     void *r_clientData;
117     int first_block = is->head.last_block;
118     int first_offset = is->head.last_offset;
119     int count = 0;
120
121     r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
122
123     is->head.last_offset += sizeof(int);
124     if (is->head.last_offset > is->block_size)
125     {
126         if (is->debug > 2)
127             logf (LOG_LOG, "first_block=%d", first_block);
128         bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
129         (is->head.last_block)++;
130         is->head.last_offset -= is->block_size;
131         memcpy (is->merge_buf, is->merge_buf + is->block_size,
132                 is->head.last_offset);
133     }
134     while (1)
135     {
136         i_item_ptr = i_item;
137         i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
138         assert (i_mode);
139         
140         if (!i_more)
141             break;
142         else
143         {
144             char *r_out_ptr = is->merge_buf + is->head.last_offset;
145             
146             i_item_ptr = i_item;
147             (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
148                                      &r_out_ptr, &i_item_ptr);
149             is->head.last_offset = r_out_ptr - is->merge_buf;
150             if (is->head.last_offset > is->block_size)
151             {
152                 bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
153                 (is->head.last_block)++;
154                 is->head.last_offset -= is->block_size;
155                 memcpy (is->merge_buf, is->merge_buf + is->block_size,
156                         is->head.last_offset);
157             }
158             count++;
159         }
160     }
161     (*is->method->code_stop)(ISAMC_ENCODE, r_clientData);
162     if (first_block == is->head.last_block)
163         memcpy(is->merge_buf + first_offset, &count, sizeof(int));
164     else if (first_block == is->head.last_block-1)
165     {
166         int gap = first_offset + sizeof(int) - is->block_size;
167         assert (gap <= (int) sizeof(int));
168         if (gap > 0)
169         {
170             if (gap < (int) sizeof(int))
171                 bf_write(is->bf, first_block, first_offset, sizeof(int)-gap,
172                          &count);
173             memcpy (is->merge_buf, ((char*)&count)+(sizeof(int)-gap), gap);
174         }
175         else
176             bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
177     }
178     else
179     {
180         bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
181     }
182     return first_block * is->block_size + first_offset;
183 }
184
185 ISAMS_PP isams_pp_open (ISAMS is, ISAMS_P pos)
186 {
187     ISAMS_PP pp = (ISAMS_PP) xmalloc (sizeof(*pp));
188
189     if (is->debug > 1)
190         logf (LOG_LOG, "isams: isams_pp_open pos=%ld", (long) pos);
191     pp->is = is;
192     pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
193     pp->numKeys = 0;
194     pp->numRead = 0;
195     pp->buf = (char *) xmalloc(is->block_size*2);
196     pp->block_no = pos/is->block_size;
197     pp->block_offset = pos - pp->block_no * is->block_size;
198     if (is->debug)
199         logf (LOG_LOG, "isams: isams_pp_open off=%d no=%d",
200               pp->block_offset, pp->block_no);
201     if (pos)
202     {
203         bf_read (is->bf, pp->block_no, 0, 0, pp->buf);
204         bf_read (is->bf, pp->block_no+1, 0, 0, pp->buf + is->block_size);
205         memcpy(&pp->numKeys, pp->buf + pp->block_offset, sizeof(int));
206         if (is->debug)
207             logf (LOG_LOG, "isams: isams_pp_open numKeys=%d", pp->numKeys);
208         pp->block_offset += sizeof(int);
209     }
210     return pp;
211 }
212
213 void isams_pp_close (ISAMS_PP pp)
214 {
215     (*pp->is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
216     xfree(pp->buf);
217     xfree(pp);
218 }
219
220 int isams_pp_num (ISAMS_PP pp)
221 {
222     return pp->numKeys;
223 }
224
225 int isams_pp_read (ISAMS_PP pp, void *buf)
226 {
227     return isams_read_item (pp, (char **) &buf);
228 }
229
230 int isams_read_item (ISAMS_PP pp, char **dst)
231 {
232     char *src;
233     if (pp->numRead >= pp->numKeys)
234         return 0;
235     (pp->numRead)++;
236     if (pp->block_offset > pp->is->block_size)
237     {
238         pp->block_offset -= pp->is->block_size;
239         (pp->block_no)++;
240         memcpy (pp->buf, pp->buf + pp->is->block_size, pp->is->block_size);
241         bf_read (pp->is->bf, pp->block_no+1, 0, 0,
242                  pp->buf + pp->is->block_size);
243     }
244     src = pp->buf + pp->block_offset;
245     (*pp->is->method->code_item)(ISAMC_DECODE, pp->decodeClientData,
246                                  dst, &src);
247     pp->block_offset = src - pp->buf; 
248     return 1;
249 }
250
251