2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1999-05-15 14:35:48 adam
10 * Revision 1.1 1999/05/12 13:08:06 adam
11 * First version of ISAMS.
27 typedef unsigned ISAMS_BLOCK_SIZE;
45 void *decodeClientData;
50 ISAMS_M isams_getmethod (void)
52 ISAMS_M m = xmalloc (sizeof(*m));
58 m->compare_item = NULL;
66 ISAMS isams_open (BFiles bfs, const char *name, int writeflag,
69 ISAMS is = xmalloc (sizeof(*is));
71 is->method = xmalloc (sizeof(*is->method));
72 memcpy (is->method, method, sizeof(*method));
73 is->block_size = is->method->block_size;
74 is->debug = is->method->debug;
76 is->bf = bf_open (bfs, name, is->block_size, writeflag);
78 if (!bf_read (is->bf, 0, 0, sizeof(ISAMS_head), &is->head))
80 is->head.last_block = 1;
81 is->head.last_offset = 0;
83 memcpy (&is->head_old, &is->head, sizeof(is->head));
84 is->merge_buf = xmalloc(2*is->block_size);
85 memset(is->merge_buf, 0, 2*is->block_size);
89 int isams_close (ISAMS is)
91 if (memcmp(&is->head, &is->head_old, sizeof(is->head)))
93 if (is->head.last_offset)
94 bf_write(is->bf, is->head.last_block, 0, is->head.last_offset,
96 bf_write (is->bf, 0, 0, sizeof(is->head), &is->head);
99 xfree (is->merge_buf);
105 ISAMS_P isams_merge (ISAMS is, ISAMS_I data)
107 char i_item[128], *i_item_ptr;
110 int first_block = is->head.last_block;
111 int first_offset = is->head.last_offset;
114 r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
116 is->head.last_offset += sizeof(int);
117 if (is->head.last_offset > is->block_size)
120 logf (LOG_LOG, "first_block=%d", first_block);
121 bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
122 (is->head.last_block)++;
123 is->head.last_offset -= is->block_size;
124 memcpy (is->merge_buf, is->merge_buf + is->block_size,
125 is->head.last_offset);
130 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
137 char *r_out_ptr = is->merge_buf + is->head.last_offset;
140 (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
141 &r_out_ptr, &i_item_ptr);
142 is->head.last_offset = r_out_ptr - is->merge_buf;
143 if (is->head.last_offset > is->block_size)
145 bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
146 (is->head.last_block)++;
147 is->head.last_offset -= is->block_size;
148 memcpy (is->merge_buf, is->merge_buf + is->block_size,
149 is->head.last_offset);
154 (*is->method->code_stop)(ISAMC_ENCODE, r_clientData);
155 if (first_block == is->head.last_block)
156 memcpy(is->merge_buf + first_offset, &count, sizeof(int));
157 else if (first_block == is->head.last_block-1)
159 int gap = first_offset + sizeof(int) - is->block_size;
160 assert (gap <= (int) sizeof(int));
163 if (gap < sizeof(int))
164 bf_write(is->bf, first_block, first_offset, sizeof(int)-gap,
166 memcpy (is->merge_buf, ((char*)&count)+(sizeof(int)-gap), gap);
169 bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
173 bf_write(is->bf, first_block, first_offset, sizeof(int), &count);
175 return first_block * is->block_size + first_offset;
178 ISAMS_PP isams_pp_open (ISAMS is, ISAMS_P pos)
180 ISAMS_PP pp = xmalloc (sizeof(*pp));
183 logf (LOG_LOG, "isams: isams_pp_open pos=%ld", (long) pos);
185 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
188 pp->buf = xmalloc(is->block_size*2);
189 pp->block_no = pos/is->block_size;
190 pp->block_offset = pos - pp->block_no * is->block_size;
191 logf (LOG_LOG, "isams: isams_pp_open off=%d no=%d",
192 pp->block_offset, pp->block_no);
195 bf_read (is->bf, pp->block_no, 0, 0, pp->buf);
196 bf_read (is->bf, pp->block_no+1, 0, 0, pp->buf + is->block_size);
197 memcpy(&pp->numKeys, pp->buf + pp->block_offset, sizeof(int));
198 logf (LOG_LOG, "isams: isams_pp_open numKeys=%d", pp->numKeys);
199 pp->block_offset += sizeof(int);
204 void isams_pp_close (ISAMS_PP pp)
206 (*pp->is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
211 int isams_pp_num (ISAMS_PP pp)
216 int isams_pp_read (ISAMS_PP pp, void *buf)
218 return isams_read_item (pp, (char **) &buf);
221 int isams_read_item (ISAMS_PP pp, char **dst)
224 if (pp->numRead >= pp->numKeys)
227 if (pp->block_offset > pp->is->block_size)
229 pp->block_offset -= pp->is->block_size;
231 memcpy (pp->buf, pp->buf + pp->is->block_size, pp->is->block_size);
232 bf_read (pp->is->bf, pp->block_no+1, 0, 0,
233 pp->buf + pp->is->block_size);
235 src = pp->buf + pp->block_offset;
236 (*pp->is->method->code_item)(ISAMC_DECODE, pp->decodeClientData,
238 pp->block_offset = src - pp->buf;