2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1998-03-06 13:54:02 adam
8 * Fixed two nasty bugs in isc_merge.
10 * Revision 1.8 1997/09/17 12:19:20 adam
11 * Zebra version corresponds to YAZ version 1.4.
12 * Changed Zebra server so that it doesn't depend on global common_resource.
14 * Revision 1.7 1997/02/12 20:42:43 adam
15 * Bug fix: during isc_merge operations, some pages weren't marked dirty
16 * even though they should be. At this point the merge operation marks
17 * a page dirty if the previous page changed at all. A better approach is
18 * to mark it dirty if the last key written changed in previous page.
20 * Revision 1.6 1996/11/08 11:15:29 adam
21 * Number of keys in chain are stored in first block and the function
22 * to retrieve this information, isc_pp_num is implemented.
24 * Revision 1.5 1996/11/04 14:08:57 adam
25 * Optimized free block usage.
27 * Revision 1.4 1996/11/01 13:36:46 adam
28 * New element, max_blocks_mem, that control how many blocks of max size
29 * to store in memory during isc_merge.
30 * Function isc_merge now ignores delete/update of identical keys and
31 * the proper blocks are then non-dirty and not written in flush_blocks.
33 * Revision 1.3 1996/11/01 08:59:14 adam
34 * First version of isc_merge that supports update/delete.
36 * Revision 1.2 1996/10/29 16:44:56 adam
39 * Revision 1.1 1996/10/29 13:40:48 adam
46 * Reduction to lower categories in isc_merge
56 static void release_fc (ISAMC is, int cat);
57 static void init_fc (ISAMC is, int cat);
61 ISAMC_M isc_getmethod (void)
63 static struct ISAMC_filecat_s def_cat[] = {
69 { 512, 490, 100, 20 },
70 { 4096, 3950, 1000, 20 },
71 {32768, 32000, 10000, 0 },
74 ISAMC_M m = xmalloc (sizeof(*m));
81 m->compare_item = NULL;
85 m->max_blocks_mem = 10;
91 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
94 ISAMC_filecat filecat;
98 is = xmalloc (sizeof(*is));
100 is->method = xmalloc (sizeof(*is->method));
101 memcpy (is->method, method, sizeof(*method));
102 filecat = is->method->filecat;
105 /* determine number of block categories */
106 if (is->method->debug)
107 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
110 if (is->method->debug)
111 logf (LOG_LOG, "isc:%6d %6d %6d %6d",
112 filecat[i].bsize, filecat[i].ifill,
113 filecat[i].mfill, filecat[i].mblocks);
114 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
115 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
116 } while (filecat[i++].mblocks);
119 /* max_buf_size is the larget buffer to be used during merge */
120 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
121 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
122 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
123 if (is->method->debug)
124 logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
126 assert (is->no_files > 0);
127 is->files = xmalloc (sizeof(*is->files)*is->no_files);
130 is->merge_buf = xmalloc (max_buf_size+256);
131 memset (is->merge_buf, 0, max_buf_size+256);
134 is->merge_buf = NULL;
135 for (i = 0; i<is->no_files; i++)
139 sprintf (fname, "%s%c", name, i+'A');
140 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
142 is->files[i].head_is_dirty = 0;
143 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
146 is->files[i].head.lastblock = 1;
147 is->files[i].head.freelist = 0;
149 is->files[i].no_writes = 0;
150 is->files[i].no_reads = 0;
151 is->files[i].no_skip_writes = 0;
152 is->files[i].no_allocated = 0;
153 is->files[i].no_released = 0;
154 is->files[i].no_remap = 0;
161 int isc_close (ISAMC is)
165 if (is->method->debug)
166 logf (LOG_LOG, "isc: writes reads skipped alloc released remap");
167 for (i = 0; i<is->no_files; i++)
170 assert (is->files[i].bf);
171 if (is->files[i].head_is_dirty)
172 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
174 if (is->method->debug)
175 logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
176 is->files[i].no_writes,
177 is->files[i].no_reads,
178 is->files[i].no_skip_writes,
179 is->files[i].no_allocated,
180 is->files[i].no_released,
181 is->files[i].no_remap);
182 xfree (is->files[i].fc_list);
183 bf_close (is->files[i].bf);
186 xfree (is->merge_buf);
191 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
193 ++(is->files[cat].no_reads);
194 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
197 int isc_write_block (ISAMC is, int cat, int pos, char *src)
199 ++(is->files[cat].no_writes);
200 if (is->method->debug > 2)
201 logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
202 return bf_write (is->files[cat].bf, pos, 0, 0, src);
205 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
206 int nextpos, int offset)
208 unsigned short size = offset + ISAMC_BLOCK_OFFSET_N;
209 if (is->method->debug > 2)
210 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
211 (int) size, nextpos);
212 src -= ISAMC_BLOCK_OFFSET_N;
213 memcpy (src, &nextpos, sizeof(int));
214 memcpy (src + sizeof(int), &size, sizeof(size));
215 return isc_write_block (is, cat, pos, src);
218 static int alloc_block (ISAMC is, int cat)
221 char buf[sizeof(int)];
223 is->files[cat].head_is_dirty = 1;
224 (is->files[cat].no_allocated)++;
225 if ((block = is->files[cat].head.freelist))
227 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
228 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
231 block = (is->files[cat].head.lastblock)++;
235 int isc_alloc_block (ISAMC is, int cat)
239 if (is->files[cat].fc_list)
242 for (j = 0; j < is->files[cat].fc_max; j++)
243 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
245 is->files[cat].fc_list[j] = 0;
250 block = alloc_block (is, cat);
251 if (is->method->debug > 3)
252 logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
256 static void release_block (ISAMC is, int cat, int pos)
258 char buf[sizeof(int)];
260 (is->files[cat].no_released)++;
261 is->files[cat].head_is_dirty = 1;
262 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
263 is->files[cat].head.freelist = pos;
264 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
267 void isc_release_block (ISAMC is, int cat, int pos)
269 if (is->method->debug > 3)
270 logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
271 if (is->files[cat].fc_list)
274 for (j = 0; j<is->files[cat].fc_max; j++)
275 if (!is->files[cat].fc_list[j])
277 is->files[cat].fc_list[j] = pos;
281 release_block (is, cat, pos);
284 static void init_fc (ISAMC is, int cat)
288 is->files[cat].fc_max = j;
289 is->files[cat].fc_list = xmalloc (sizeof(*is->files[0].fc_list) * j);
291 is->files[cat].fc_list[j] = 0;
294 static void release_fc (ISAMC is, int cat)
296 int b, j = is->files[cat].fc_max;
299 if ((b = is->files[cat].fc_list[j]))
301 release_block (is, cat, b);
302 is->files[cat].fc_list[j] = 0;
306 void isc_pp_close (ISAMC_PP pp)
310 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
315 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
317 ISAMC_PP pp = xmalloc (sizeof(*pp));
320 pp->cat = isc_type(ipos);
321 pp->pos = isc_block(ipos);
323 src = pp->buf = xmalloc (is->method->filecat[pp->cat].bsize);
329 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
336 isc_read_block (is, pp->cat, pp->pos, src);
337 memcpy (&pp->next, src, sizeof(pp->next));
338 src += sizeof(pp->next);
339 memcpy (&pp->size, src, sizeof(pp->size));
340 src += sizeof(pp->size);
341 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
342 src += sizeof(pp->numKeys);
343 assert (pp->next != pp->pos);
344 pp->offset = src - pp->buf;
345 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
346 if (is->method->debug > 2)
347 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
348 pp->size, pp->cat, pp->pos, pp->next);
353 /* returns non-zero if item could be read; 0 otherwise */
354 int isc_pp_read (ISAMC_PP pp, void *buf)
356 return isc_read_item (pp, (char **) &buf);
359 /* read one item from file - decode and store it in *dst.
362 1 if item could be read ok and NO boundary
363 2 if item could be read ok and boundary */
364 int isc_read_item (ISAMC_PP pp, char **dst)
367 char *src = pp->buf + pp->offset;
369 if (pp->offset >= pp->size)
371 /* out new block position */
374 return 0; /* end of file */
376 /* read block and save 'next' and 'size' entry */
377 isc_read_block (is, pp->cat, pp->pos, src);
378 memcpy (&pp->next, src, sizeof(pp->next));
379 src += sizeof(pp->next);
380 memcpy (&pp->size, src, sizeof(pp->size));
381 src += sizeof(pp->size);
382 /* assume block is non-empty */
383 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
384 assert (pp->next != pp->pos);
386 isc_release_block (is, pp->cat, pp->pos);
387 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
388 pp->offset = src - pp->buf;
389 if (is->method->debug > 2)
390 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
391 pp->size, pp->cat, pp->pos, pp->next);
394 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
395 pp->offset = src - pp->buf;
399 int isc_pp_num (ISAMC_PP pp)