1 /* $Id: isams.c,v 1.4 2003-06-23 15:36:12 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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
37 typedef unsigned ISAMS_BLOCK_SIZE;
55 void *decodeClientData;
60 void isams_getmethod (ISAMS_M *m)
66 m->compare_item = NULL;
72 ISAMS isams_open (BFiles bfs, const char *name, int writeflag,
75 ISAMS is = (ISAMS) xmalloc (sizeof(*is));
77 is->method = (ISAMS_M *) xmalloc (sizeof(*is->method));
78 memcpy (is->method, method, sizeof(*method));
79 is->block_size = is->method->block_size;
80 is->debug = is->method->debug;
82 is->bf = bf_open (bfs, name, is->block_size, writeflag);
84 if (!bf_read (is->bf, 0, 0, sizeof(ISAMS_head), &is->head))
86 is->head.last_block = 1;
87 is->head.last_offset = 0;
89 memcpy (&is->head_old, &is->head, sizeof(is->head));
90 is->merge_buf = (char *) xmalloc(2*is->block_size);
91 memset(is->merge_buf, 0, 2*is->block_size);
95 int isams_close (ISAMS is)
97 if (memcmp(&is->head, &is->head_old, sizeof(is->head)))
99 if (is->head.last_offset)
100 bf_write(is->bf, is->head.last_block, 0, is->head.last_offset,
102 bf_write (is->bf, 0, 0, sizeof(is->head), &is->head);
105 xfree (is->merge_buf);
111 ISAMS_P isams_merge (ISAMS is, ISAMS_I data)
113 char i_item[128], *i_item_ptr;
116 int first_block = is->head.last_block;
117 int first_offset = is->head.last_offset;
120 r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
122 is->head.last_offset += sizeof(int);
123 if (is->head.last_offset > is->block_size)
126 logf (LOG_LOG, "first_block=%d", first_block);
127 bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
128 (is->head.last_block)++;
129 is->head.last_offset -= is->block_size;
130 memcpy (is->merge_buf, is->merge_buf + is->block_size,
131 is->head.last_offset);
136 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
143 char *r_out_ptr = is->merge_buf + is->head.last_offset;
146 (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
147 &r_out_ptr, &i_item_ptr);
148 is->head.last_offset = r_out_ptr - is->merge_buf;
149 if (is->head.last_offset > is->block_size)
151 bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
152 (is->head.last_block)++;
153 is->head.last_offset -= is->block_size;
154 memcpy (is->merge_buf, is->merge_buf + is->block_size,
155 is->head.last_offset);
160 (*is->method->code_stop)(ISAMC_ENCODE, r_clientData);
161 if (first_block == is->head.last_block)
162 memcpy(is->merge_buf + first_offset, &count, sizeof(int));
163 else if (first_block == is->head.last_block-1)
165 int gap = first_offset + sizeof(int) - is->block_size;
166 assert (gap <= (int) sizeof(int));
169 if (gap < (int) sizeof(int))
170 bf_write(is->bf, first_block, first_offset, sizeof(int)-gap,
172 memcpy (is->merge_buf, ((char*)&count)+(sizeof(int)-gap), gap);
175 bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
179 bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
181 return first_block * is->block_size + first_offset;
184 ISAMS_PP isams_pp_open (ISAMS is, ISAMS_P pos)
186 ISAMS_PP pp = (ISAMS_PP) xmalloc (sizeof(*pp));
189 logf (LOG_LOG, "isams: isams_pp_open pos=%ld", (long) pos);
191 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
194 pp->buf = (char *) xmalloc(is->block_size*2);
195 pp->block_no = pos/is->block_size;
196 pp->block_offset = pos - pp->block_no * is->block_size;
198 logf (LOG_LOG, "isams: isams_pp_open off=%d no=%d",
199 pp->block_offset, pp->block_no);
202 bf_read (is->bf, pp->block_no, 0, 0, pp->buf);
203 bf_read (is->bf, pp->block_no+1, 0, 0, pp->buf + is->block_size);
204 memcpy(&pp->numKeys, pp->buf + pp->block_offset, sizeof(int));
206 logf (LOG_LOG, "isams: isams_pp_open numKeys=%d", pp->numKeys);
207 pp->block_offset += sizeof(int);
212 void isams_pp_close (ISAMS_PP pp)
214 (*pp->is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
219 int isams_pp_num (ISAMS_PP pp)
224 int isams_pp_read (ISAMS_PP pp, void *buf)
226 return isams_read_item (pp, (char **) &buf);
229 int isams_read_item (ISAMS_PP pp, char **dst)
232 if (pp->numRead >= pp->numKeys)
235 if (pp->block_offset > pp->is->block_size)
237 pp->block_offset -= pp->is->block_size;
239 memcpy (pp->buf, pp->buf + pp->is->block_size, pp->is->block_size);
240 bf_read (pp->is->bf, pp->block_no+1, 0, 0,
241 pp->buf + pp->is->block_size);
243 src = pp->buf + pp->block_offset;
244 (*pp->is->method->code_item)(ISAMC_DECODE, pp->decodeClientData,
246 pp->block_offset = src - pp->buf;