2 * Copyright (c) 1995-1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1997-09-17 12:19:20 adam
8 * Zebra version corresponds to YAZ version 1.4.
9 * Changed Zebra server so that it doesn't depend on global common_resource.
11 * Revision 1.7 1997/02/12 20:42:43 adam
12 * Bug fix: during isc_merge operations, some pages weren't marked dirty
13 * even though they should be. At this point the merge operation marks
14 * a page dirty if the previous page changed at all. A better approach is
15 * to mark it dirty if the last key written changed in previous page.
17 * Revision 1.6 1996/11/08 11:15:29 adam
18 * Number of keys in chain are stored in first block and the function
19 * to retrieve this information, isc_pp_num is implemented.
21 * Revision 1.5 1996/11/04 14:08:57 adam
22 * Optimized free block usage.
24 * Revision 1.4 1996/11/01 13:36:46 adam
25 * New element, max_blocks_mem, that control how many blocks of max size
26 * to store in memory during isc_merge.
27 * Function isc_merge now ignores delete/update of identical keys and
28 * the proper blocks are then non-dirty and not written in flush_blocks.
30 * Revision 1.3 1996/11/01 08:59:14 adam
31 * First version of isc_merge that supports update/delete.
33 * Revision 1.2 1996/10/29 16:44:56 adam
36 * Revision 1.1 1996/10/29 13:40:48 adam
43 * Reduction to lower categories in isc_merge
53 static void release_fc (ISAMC is, int cat);
54 static void init_fc (ISAMC is, int cat);
56 ISAMC_M isc_getmethod (void)
58 static struct ISAMC_filecat_s def_cat[] = {
60 { 512, 490, 100, 20 },
61 { 4096, 3950, 1000, 20 },
62 {32768, 32000, 10000, 0 },
64 ISAMC_M m = xmalloc (sizeof(*m));
71 m->compare_item = NULL;
75 m->max_blocks_mem = 10;
81 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
84 ISAMC_filecat filecat;
88 is = xmalloc (sizeof(*is));
90 is->method = xmalloc (sizeof(*is->method));
91 memcpy (is->method, method, sizeof(*method));
92 filecat = is->method->filecat;
95 /* determine number of block categories */
96 if (is->method->debug)
97 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
100 if (is->method->debug)
101 logf (LOG_LOG, "isc:%6d %6d %6d %6d",
102 filecat[i].bsize, filecat[i].ifill,
103 filecat[i].mfill, filecat[i].mblocks);
104 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
105 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
106 } while (filecat[i++].mblocks);
109 /* max_buf_size is the larget buffer to be used during merge */
110 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
111 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
112 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
113 if (is->method->debug)
114 logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
116 assert (is->no_files > 0);
117 is->files = xmalloc (sizeof(*is->files)*is->no_files);
120 is->merge_buf = xmalloc (max_buf_size+256);
121 memset (is->merge_buf, 0, max_buf_size+256);
124 is->merge_buf = NULL;
125 for (i = 0; i<is->no_files; i++)
129 sprintf (fname, "%s%c", name, i+'A');
130 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
132 is->files[i].head_is_dirty = 0;
133 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
136 is->files[i].head.lastblock = 1;
137 is->files[i].head.freelist = 0;
139 is->files[i].no_writes = 0;
140 is->files[i].no_reads = 0;
141 is->files[i].no_skip_writes = 0;
142 is->files[i].no_allocated = 0;
143 is->files[i].no_released = 0;
144 is->files[i].no_remap = 0;
151 int isc_close (ISAMC is)
155 if (is->method->debug)
156 logf (LOG_LOG, "isc: writes reads skipped alloc released remap");
157 for (i = 0; i<is->no_files; i++)
160 assert (is->files[i].bf);
161 if (is->files[i].head_is_dirty)
162 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
164 if (is->method->debug)
165 logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
166 is->files[i].no_writes,
167 is->files[i].no_reads,
168 is->files[i].no_skip_writes,
169 is->files[i].no_allocated,
170 is->files[i].no_released,
171 is->files[i].no_remap);
172 xfree (is->files[i].fc_list);
173 bf_close (is->files[i].bf);
176 xfree (is->merge_buf);
181 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
183 ++(is->files[cat].no_reads);
184 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
187 int isc_write_block (ISAMC is, int cat, int pos, char *src)
189 ++(is->files[cat].no_writes);
190 if (is->method->debug > 2)
191 logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
192 return bf_write (is->files[cat].bf, pos, 0, 0, src);
195 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
196 int nextpos, int offset)
198 unsigned short size = offset + ISAMC_BLOCK_OFFSET_N;
199 if (is->method->debug > 2)
200 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
201 (int) size, nextpos);
202 src -= ISAMC_BLOCK_OFFSET_N;
203 memcpy (src, &nextpos, sizeof(int));
204 memcpy (src + sizeof(int), &size, sizeof(size));
205 return isc_write_block (is, cat, pos, src);
208 static int alloc_block (ISAMC is, int cat)
211 char buf[sizeof(int)];
213 is->files[cat].head_is_dirty = 1;
214 (is->files[cat].no_allocated)++;
215 if ((block = is->files[cat].head.freelist))
217 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
218 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
221 block = (is->files[cat].head.lastblock)++;
225 int isc_alloc_block (ISAMC is, int cat)
229 if (is->files[cat].fc_list)
232 for (j = 0; j < is->files[cat].fc_max; j++)
233 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
235 is->files[cat].fc_list[j] = 0;
240 block = alloc_block (is, cat);
241 if (is->method->debug > 3)
242 logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
246 static void release_block (ISAMC is, int cat, int pos)
248 char buf[sizeof(int)];
250 (is->files[cat].no_released)++;
251 is->files[cat].head_is_dirty = 1;
252 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
253 is->files[cat].head.freelist = pos;
254 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
257 void isc_release_block (ISAMC is, int cat, int pos)
259 if (is->method->debug > 3)
260 logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
261 if (is->files[cat].fc_list)
264 for (j = 0; j<is->files[cat].fc_max; j++)
265 if (!is->files[cat].fc_list[j])
267 is->files[cat].fc_list[j] = pos;
271 release_block (is, cat, pos);
274 static void init_fc (ISAMC is, int cat)
278 is->files[cat].fc_max = j;
279 is->files[cat].fc_list = xmalloc (sizeof(*is->files[0].fc_list) * j);
281 is->files[cat].fc_list[j] = 0;
284 static void release_fc (ISAMC is, int cat)
286 int b, j = is->files[cat].fc_max;
289 if ((b = is->files[cat].fc_list[j]))
291 release_block (is, cat, b);
292 is->files[cat].fc_list[j] = 0;
296 void isc_pp_close (ISAMC_PP pp)
300 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
305 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
307 ISAMC_PP pp = xmalloc (sizeof(*pp));
310 pp->cat = isc_type(ipos);
311 pp->pos = isc_block(ipos);
313 src = pp->buf = xmalloc (is->method->filecat[pp->cat].bsize);
319 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
326 isc_read_block (is, pp->cat, pp->pos, src);
327 memcpy (&pp->next, src, sizeof(pp->next));
328 src += sizeof(pp->next);
329 memcpy (&pp->size, src, sizeof(pp->size));
330 src += sizeof(pp->size);
331 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
332 src += sizeof(pp->numKeys);
333 assert (pp->next != pp->pos);
334 pp->offset = src - pp->buf;
335 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
336 if (is->method->debug > 2)
337 logf (LOG_LOG, "isc: read_block size=%d %d %d",
338 pp->size, pp->cat, pp->pos);
343 /* returns non-zero if item could be read; 0 otherwise */
344 int isc_pp_read (ISAMC_PP pp, void *buf)
346 return isc_read_item (pp, (char **) &buf);
349 /* returns non-zero if item could be read; 0 otherwise */
350 int isc_read_item (ISAMC_PP pp, char **dst)
353 char *src = pp->buf + pp->offset;
355 if (pp->offset >= pp->size)
361 isc_read_block (is, pp->cat, pp->pos, src);
362 memcpy (&pp->next, src, sizeof(pp->next));
363 src += sizeof(pp->next);
364 memcpy (&pp->size, src, sizeof(pp->size));
365 src += sizeof(pp->size);
366 /* assume block is non-empty */
367 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
368 assert (pp->next != pp->pos);
370 isc_release_block (is, pp->cat, pp->pos);
371 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
372 pp->offset = src - pp->buf;
373 if (is->method->debug > 2)
374 logf (LOG_LOG, "isc: read_block size=%d %d %d",
375 pp->size, pp->cat, pp->pos);
378 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
379 pp->offset = src - pp->buf;
383 int isc_pp_num (ISAMC_PP pp)