2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1999-11-30 14:02:45 adam
10 * Revision 1.5 1999/07/14 10:59:27 adam
11 * Changed functions isc_getmethod, isams_getmethod.
12 * Improved fatal error handling (such as missing EXPLAIN schema).
14 * Revision 1.4 1999/05/26 07:49:14 adam
17 * Revision 1.3 1999/05/20 12:57:18 adam
18 * Implemented TCL filter. Updated recctrl system.
20 * Revision 1.2 1999/05/15 14:35:48 adam
23 * Revision 1.1 1999/05/12 13:08:06 adam
24 * First version of ISAMS.
40 typedef unsigned ISAMS_BLOCK_SIZE;
58 void *decodeClientData;
63 void isams_getmethod (ISAMS_M m)
69 m->compare_item = NULL;
75 ISAMS isams_open (BFiles bfs, const char *name, int writeflag,
78 ISAMS is = (ISAMS) xmalloc (sizeof(*is));
80 is->method = (ISAMS_M) xmalloc (sizeof(*is->method));
81 memcpy (is->method, method, sizeof(*method));
82 is->block_size = is->method->block_size;
83 is->debug = is->method->debug;
85 is->bf = bf_open (bfs, name, is->block_size, writeflag);
87 if (!bf_read (is->bf, 0, 0, sizeof(ISAMS_head), &is->head))
89 is->head.last_block = 1;
90 is->head.last_offset = 0;
92 memcpy (&is->head_old, &is->head, sizeof(is->head));
93 is->merge_buf = (char *) xmalloc(2*is->block_size);
94 memset(is->merge_buf, 0, 2*is->block_size);
98 int isams_close (ISAMS is)
100 if (memcmp(&is->head, &is->head_old, sizeof(is->head)))
102 if (is->head.last_offset)
103 bf_write(is->bf, is->head.last_block, 0, is->head.last_offset,
105 bf_write (is->bf, 0, 0, sizeof(is->head), &is->head);
108 xfree (is->merge_buf);
114 ISAMS_P isams_merge (ISAMS is, ISAMS_I data)
116 char i_item[128], *i_item_ptr;
119 int first_block = is->head.last_block;
120 int first_offset = is->head.last_offset;
123 r_clientData = (*is->method->code_start)(ISAMC_ENCODE);
125 is->head.last_offset += sizeof(int);
126 if (is->head.last_offset > is->block_size)
129 logf (LOG_LOG, "first_block=%d", first_block);
130 bf_write(is->bf, is->head.last_block, 0, 0, is->merge_buf);
131 (is->head.last_block)++;
132 is->head.last_offset -= is->block_size;
133 memcpy (is->merge_buf, is->merge_buf + is->block_size,
134 is->head.last_offset);
139 i_more = (*data->read_item)(data->clientData, &i_item_ptr, &i_mode);
146 char *r_out_ptr = is->merge_buf + is->head.last_offset;
149 (*is->method->code_item)(ISAMC_ENCODE, r_clientData,
150 &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->code_stop)(ISAMC_ENCODE, 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 logf (LOG_LOG, "isams: isams_pp_open pos=%ld", (long) pos);
194 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
197 pp->buf = (char *) xmalloc(is->block_size*2);
198 pp->block_no = pos/is->block_size;
199 pp->block_offset = pos - pp->block_no * is->block_size;
200 logf (LOG_LOG, "isams: isams_pp_open off=%d no=%d",
201 pp->block_offset, pp->block_no);
204 bf_read (is->bf, pp->block_no, 0, 0, pp->buf);
205 bf_read (is->bf, pp->block_no+1, 0, 0, pp->buf + is->block_size);
206 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;