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
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;
73 ISAMS isams_open (BFiles bfs, const char *name, int writeflag,
76 ISAMS is = (ISAMS) xmalloc (sizeof(*is));
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;
83 is->bf = bf_open (bfs, name, is->block_size, writeflag);
85 if (!bf_read (is->bf, 0, 0, sizeof(ISAMS_head), &is->head))
87 is->head.last_block = 1;
88 is->head.last_offset = 0;
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);
96 int isams_close (ISAMS is)
98 if (memcmp(&is->head, &is->head_old, sizeof(is->head)))
100 if (is->head.last_offset)
101 bf_write(is->bf, is->head.last_block, 0, is->head.last_offset,
103 bf_write (is->bf, 0, 0, sizeof(is->head), &is->head);
106 xfree (is->merge_buf);
112 ISAMS_P isams_merge (ISAMS is, ISAMS_I data)
114 char i_item[128], *i_item_ptr;
117 int first_block = is->head.last_block;
118 int first_offset = is->head.last_offset;
121 r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
123 is->head.last_offset += sizeof(int);
124 if (is->head.last_offset > is->block_size)
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);
137 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
144 char *r_out_ptr = is->merge_buf + is->head.last_offset;
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)
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);
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)
166 int gap = first_offset + sizeof(int) - is->block_size;
167 assert (gap <= (int) sizeof(int));
170 if (gap < (int) sizeof(int))
171 bf_write(is->bf, first_block, first_offset, sizeof(int)-gap,
173 memcpy (is->merge_buf, ((char*)&count)+(sizeof(int)-gap), gap);
176 bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
180 bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
182 return first_block * is->block_size + first_offset;
185 ISAMS_PP isams_pp_open (ISAMS is, ISAMS_P pos)
187 ISAMS_PP pp = (ISAMS_PP) xmalloc (sizeof(*pp));
190 logf (LOG_LOG, "isams: isams_pp_open pos=%ld", (long) pos);
192 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
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;
199 logf (LOG_LOG, "isams: isams_pp_open off=%d no=%d",
200 pp->block_offset, pp->block_no);
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));
207 logf (LOG_LOG, "isams: isams_pp_open numKeys=%d", pp->numKeys);
208 pp->block_offset += sizeof(int);
213 void isams_pp_close (ISAMS_PP pp)
215 (*pp->is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
220 int isams_pp_num (ISAMS_PP pp)
225 int isams_pp_read (ISAMS_PP pp, void *buf)
227 return isams_read_item (pp, (char **) &buf);
230 int isams_read_item (ISAMS_PP pp, char **dst)
233 if (pp->numRead >= pp->numKeys)
236 if (pp->block_offset > pp->is->block_size)
238 pp->block_offset -= pp->is->block_size;
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);
244 src = pp->buf + pp->block_offset;
245 (*pp->is->method->code_item)(ISAMC_DECODE, pp->decodeClientData,
247 pp->block_offset = src - pp->buf;