1 /* $Id: isams.c,v 1.11 2005-01-15 19:38:31 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
30 #include <yaz/xmalloc.h>
31 #include <idzebra/isams.h>
38 typedef unsigned ISAMS_BLOCK_SIZE;
56 void *decodeClientData;
61 void isams_getmethod (ISAMS_M *m)
63 m->codec.start = NULL;
64 m->codec.decode = NULL;
65 m->codec.encode = NULL;
67 m->codec.reset = NULL;
69 m->compare_item = NULL;
76 ISAMS isams_open (BFiles bfs, const char *name, int writeflag,
79 ISAMS is = (ISAMS) xmalloc (sizeof(*is));
81 is->method = (ISAMS_M *) xmalloc (sizeof(*is->method));
82 memcpy (is->method, method, sizeof(*method));
83 is->block_size = is->method->block_size;
84 is->debug = is->method->debug;
86 is->bf = bf_open (bfs, name, is->block_size, writeflag);
88 if (!bf_read (is->bf, 0, 0, sizeof(ISAMS_head), &is->head))
90 is->head.last_block = 1;
91 is->head.last_offset = 0;
93 memcpy (&is->head_old, &is->head, sizeof(is->head));
94 is->merge_buf = (char *) xmalloc(2*is->block_size);
95 memset(is->merge_buf, 0, 2*is->block_size);
99 int isams_close (ISAMS is)
101 if (memcmp(&is->head, &is->head_old, sizeof(is->head)))
103 if (is->head.last_offset)
104 bf_write(is->bf, is->head.last_block, 0, is->head.last_offset,
106 bf_write (is->bf, 0, 0, sizeof(is->head), &is->head);
109 xfree (is->merge_buf);
115 ISAMS_P isams_merge (ISAMS is, ISAMS_I data)
120 int first_block = is->head.last_block;
121 int first_offset = is->head.last_offset;
124 r_clientData = (*is->method->codec.start)();
126 is->head.last_offset += sizeof(int);
127 if (is->head.last_offset > is->block_size)
130 yaz_log (YLOG_LOG, "first_block=%d", first_block);
131 bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
132 (is->head.last_block)++;
133 is->head.last_offset -= is->block_size;
134 memcpy (is->merge_buf, is->merge_buf + is->block_size,
135 is->head.last_offset);
139 char *tmp_ptr = i_item;
140 i_more = (*data->read_item)(data->clientData, &tmp_ptr, &i_mode);
147 char *r_out_ptr = is->merge_buf + is->head.last_offset;
149 const char *i_item_ptr = i_item;
150 (*is->method->codec.encode)(r_clientData, &r_out_ptr, &i_item_ptr);
151 is->head.last_offset = r_out_ptr - is->merge_buf;
152 if (is->head.last_offset > is->block_size)
154 bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
155 (is->head.last_block)++;
156 is->head.last_offset -= is->block_size;
157 memcpy (is->merge_buf, is->merge_buf + is->block_size,
158 is->head.last_offset);
163 (*is->method->codec.stop)(r_clientData);
164 if (first_block == is->head.last_block)
165 memcpy(is->merge_buf + first_offset, &count, sizeof(int));
166 else if (first_block == is->head.last_block-1)
168 int gap = first_offset + sizeof(int) - is->block_size;
169 assert (gap <= (int) sizeof(int));
172 if (gap < (int) sizeof(int))
173 bf_write(is->bf, first_block, first_offset, sizeof(int)-gap,
175 memcpy (is->merge_buf, ((char*)&count)+(sizeof(int)-gap), gap);
178 bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
182 bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
184 return first_block * is->block_size + first_offset;
187 ISAMS_PP isams_pp_open (ISAMS is, ISAMS_P pos)
189 ISAMS_PP pp = (ISAMS_PP) xmalloc (sizeof(*pp));
192 yaz_log (YLOG_LOG, "isams: isams_pp_open pos=%ld", (long) pos);
194 pp->decodeClientData = (*is->method->codec.start)();
197 pp->buf = (char *) xmalloc(is->block_size*2);
198 pp->block_no = (int) (pos/is->block_size);
199 pp->block_offset = (int) (pos - pp->block_no * is->block_size);
201 yaz_log (YLOG_LOG, "isams: isams_pp_open off=%d no=%d",
202 pp->block_offset, pp->block_no);
205 bf_read (is->bf, pp->block_no, 0, 0, pp->buf);
206 bf_read (is->bf, pp->block_no+1, 0, 0, pp->buf + is->block_size);
207 memcpy(&pp->numKeys, pp->buf + pp->block_offset, sizeof(int));
209 yaz_log (YLOG_LOG, "isams: isams_pp_open numKeys=%d", pp->numKeys);
210 pp->block_offset += sizeof(int);
215 void isams_pp_close (ISAMS_PP pp)
217 (*pp->is->method->codec.stop)(pp->decodeClientData);
222 int isams_pp_num (ISAMS_PP pp)
227 int isams_pp_read (ISAMS_PP pp, void *buf)
230 return isams_read_item (pp, &cp);
233 int isams_read_item (ISAMS_PP pp, char **dst)
236 if (pp->numRead >= pp->numKeys)
239 if (pp->block_offset > pp->is->block_size)
241 pp->block_offset -= pp->is->block_size;
243 memcpy (pp->buf, pp->buf + pp->is->block_size, pp->is->block_size);
244 bf_read (pp->is->bf, pp->block_no+1, 0, 0,
245 pp->buf + pp->is->block_size);
247 src = pp->buf + pp->block_offset;
248 (*pp->is->method->codec.decode)(pp->decodeClientData, dst, &src);
249 pp->block_offset = src - pp->buf;