1 /* $Id: isamc.c,v 1.23 2003-06-23 15:36:11 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
27 * Reduction to lower categories in isc_merge
37 static void flush_block (ISAMC is, int cat);
38 static void release_fc (ISAMC is, int cat);
39 static void init_fc (ISAMC is, int cat);
41 #define ISAMC_FREELIST_CHUNK 1
45 void isc_getmethod (ISAMC_M *m)
48 static struct ISAMC_filecat_s def_cat[] = {
54 { 128, 120, 100, 10 },
55 { 512, 490, 350, 10 },
56 { 2048, 1900, 1700, 10 },
57 { 8192, 8000, 7900, 10 },
58 { 32768, 32000, 31000, 0 },
68 m->compare_item = NULL;
72 m->max_blocks_mem = 10;
75 ISAMC isc_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method)
78 ISAMC_filecat filecat;
82 is = (ISAMC) xmalloc (sizeof(*is));
84 is->method = (ISAMC_M *) xmalloc (sizeof(*is->method));
85 memcpy (is->method, method, sizeof(*method));
86 filecat = is->method->filecat;
89 /* determine number of block categories */
90 if (is->method->debug)
91 logf (LOG_LOG, "isc: bsize ifill mfill mblocks");
94 if (is->method->debug)
95 logf (LOG_LOG, "isc:%6d %6d %6d %6d",
96 filecat[i].bsize, filecat[i].ifill,
97 filecat[i].mfill, filecat[i].mblocks);
98 if (max_buf_size < filecat[i].mblocks * filecat[i].bsize)
99 max_buf_size = filecat[i].mblocks * filecat[i].bsize;
100 } while (filecat[i++].mblocks);
103 /* max_buf_size is the larget buffer to be used during merge */
104 max_buf_size = (1 + max_buf_size / filecat[i].bsize) * filecat[i].bsize;
105 if (max_buf_size < (1+is->method->max_blocks_mem) * filecat[i].bsize)
106 max_buf_size = (1+is->method->max_blocks_mem) * filecat[i].bsize;
107 if (is->method->debug)
108 logf (LOG_LOG, "isc: max_buf_size %d", max_buf_size);
110 assert (is->no_files > 0);
111 is->files = (ISAMC_file) xmalloc (sizeof(*is->files)*is->no_files);
114 is->merge_buf = (char *) xmalloc (max_buf_size+256);
115 memset (is->merge_buf, 0, max_buf_size+256);
118 is->merge_buf = NULL;
119 for (i = 0; i<is->no_files; i++)
123 sprintf (fname, "%s%c", name, i+'A');
124 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
126 is->files[i].head_is_dirty = 0;
127 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
130 is->files[i].head.lastblock = 1;
131 is->files[i].head.freelist = 0;
133 is->files[i].alloc_entries_num = 0;
134 is->files[i].alloc_entries_max =
135 is->method->filecat[i].bsize / sizeof(int) - 1;
136 is->files[i].alloc_buf = (char *)
137 xmalloc (is->method->filecat[i].bsize);
138 is->files[i].no_writes = 0;
139 is->files[i].no_reads = 0;
140 is->files[i].no_skip_writes = 0;
141 is->files[i].no_allocated = 0;
142 is->files[i].no_released = 0;
143 is->files[i].no_remap = 0;
144 is->files[i].no_forward = 0;
145 is->files[i].no_backward = 0;
146 is->files[i].sum_forward = 0;
147 is->files[i].sum_backward = 0;
148 is->files[i].no_next = 0;
149 is->files[i].no_prev = 0;
156 int isc_block_used (ISAMC is, int type)
158 if (type < 0 || type >= is->no_files)
160 return is->files[type].head.lastblock-1;
163 int isc_block_size (ISAMC is, int type)
165 ISAMC_filecat filecat = is->method->filecat;
166 if (type < 0 || type >= is->no_files)
168 return filecat[type].bsize;
171 int isc_close (ISAMC is)
175 if (is->method->debug)
177 logf (LOG_LOG, "isc: next forw mid-f prev backw mid-b");
178 for (i = 0; i<is->no_files; i++)
179 logf (LOG_LOG, "isc:%8d%8d%8.1f%8d%8d%8.1f",
180 is->files[i].no_next,
181 is->files[i].no_forward,
182 is->files[i].no_forward ?
183 (double) is->files[i].sum_forward/is->files[i].no_forward
185 is->files[i].no_prev,
186 is->files[i].no_backward,
187 is->files[i].no_backward ?
188 (double) is->files[i].sum_backward/is->files[i].no_backward
191 if (is->method->debug)
192 logf (LOG_LOG, "isc: writes reads skipped alloc released remap");
193 for (i = 0; i<is->no_files; i++)
196 assert (is->files[i].bf);
197 if (is->files[i].head_is_dirty)
198 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMC_head),
200 if (is->method->debug)
201 logf (LOG_LOG, "isc:%8d%8d%8d%8d%8d%8d",
202 is->files[i].no_writes,
203 is->files[i].no_reads,
204 is->files[i].no_skip_writes,
205 is->files[i].no_allocated,
206 is->files[i].no_released,
207 is->files[i].no_remap);
208 xfree (is->files[i].fc_list);
210 bf_close (is->files[i].bf);
213 xfree (is->merge_buf);
219 int isc_read_block (ISAMC is, int cat, int pos, char *dst)
221 ++(is->files[cat].no_reads);
222 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
225 int isc_write_block (ISAMC is, int cat, int pos, char *src)
227 ++(is->files[cat].no_writes);
228 if (is->method->debug > 2)
229 logf (LOG_LOG, "isc: write_block %d %d", cat, pos);
230 return bf_write (is->files[cat].bf, pos, 0, 0, src);
233 int isc_write_dblock (ISAMC is, int cat, int pos, char *src,
234 int nextpos, int offset)
236 ISAMC_BLOCK_SIZE size = offset + ISAMC_BLOCK_OFFSET_N;
237 if (is->method->debug > 2)
238 logf (LOG_LOG, "isc: write_dblock. size=%d nextpos=%d",
239 (int) size, nextpos);
240 src -= ISAMC_BLOCK_OFFSET_N;
241 memcpy (src, &nextpos, sizeof(int));
242 memcpy (src + sizeof(int), &size, sizeof(size));
243 return isc_write_block (is, cat, pos, src);
246 #if ISAMC_FREELIST_CHUNK
247 static void flush_block (ISAMC is, int cat)
249 char *abuf = is->files[cat].alloc_buf;
250 int block = is->files[cat].head.freelist;
251 if (block && is->files[cat].alloc_entries_num)
253 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
254 bf_write (is->files[cat].bf, block, 0, 0, abuf);
255 is->files[cat].alloc_entries_num = 0;
260 static int alloc_block (ISAMC is, int cat)
262 int block = is->files[cat].head.freelist;
263 char *abuf = is->files[cat].alloc_buf;
265 (is->files[cat].no_allocated)++;
269 block = (is->files[cat].head.lastblock)++; /* no free list */
270 is->files[cat].head_is_dirty = 1;
274 if (!is->files[cat].alloc_entries_num) /* read first time */
276 bf_read (is->files[cat].bf, block, 0, 0, abuf);
277 memcpy (&is->files[cat].alloc_entries_num, abuf,
278 sizeof(is->files[cat].alloc_entries_num));
279 assert (is->files[cat].alloc_entries_num > 0);
281 /* have some free blocks now */
282 assert (is->files[cat].alloc_entries_num > 0);
283 is->files[cat].alloc_entries_num--;
284 if (!is->files[cat].alloc_entries_num) /* last one in block? */
286 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
288 is->files[cat].head_is_dirty = 1;
290 if (is->files[cat].head.freelist)
292 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
294 memcpy (&is->files[cat].alloc_entries_num, abuf,
295 sizeof(is->files[cat].alloc_entries_num));
296 assert (is->files[cat].alloc_entries_num);
300 memcpy (&block, abuf + sizeof(int) + sizeof(int) *
301 is->files[cat].alloc_entries_num, sizeof(int));
306 static void release_block (ISAMC is, int cat, int pos)
308 char *abuf = is->files[cat].alloc_buf;
309 int block = is->files[cat].head.freelist;
311 (is->files[cat].no_released)++;
313 if (block && !is->files[cat].alloc_entries_num) /* must read block */
315 bf_read (is->files[cat].bf, block, 0, 0, abuf);
316 memcpy (&is->files[cat].alloc_entries_num, abuf,
317 sizeof(is->files[cat].alloc_entries_num));
318 assert (is->files[cat].alloc_entries_num > 0);
320 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
321 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
324 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
325 bf_write (is->files[cat].bf, block, 0, 0, abuf);
326 is->files[cat].alloc_entries_num = 0;
328 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
330 memcpy (abuf + sizeof(int), &block, sizeof(int));
331 is->files[cat].head.freelist = pos;
332 is->files[cat].head_is_dirty = 1;
336 memcpy (abuf + sizeof(int) +
337 is->files[cat].alloc_entries_num*sizeof(int),
340 is->files[cat].alloc_entries_num++;
343 static void flush_block (ISAMC is, int cat)
345 char *abuf = is->files[cat].alloc_buf;
349 static int alloc_block (ISAMC is, int cat)
352 char buf[sizeof(int)];
354 is->files[cat].head_is_dirty = 1;
355 (is->files[cat].no_allocated)++;
356 if ((block = is->files[cat].head.freelist))
358 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
359 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
362 block = (is->files[cat].head.lastblock)++;
366 static void release_block (ISAMC is, int cat, int pos)
368 char buf[sizeof(int)];
370 (is->files[cat].no_released)++;
371 is->files[cat].head_is_dirty = 1;
372 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
373 is->files[cat].head.freelist = pos;
374 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
378 int isc_alloc_block (ISAMC is, int cat)
382 if (is->files[cat].fc_list)
385 for (j = 0; j < is->files[cat].fc_max; j++)
386 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
388 is->files[cat].fc_list[j] = 0;
394 block = alloc_block (is, cat);
395 if (is->method->debug > 3)
396 logf (LOG_LOG, "isc: alloc_block in cat %d: %d", cat, block);
400 void isc_release_block (ISAMC is, int cat, int pos)
402 if (is->method->debug > 3)
403 logf (LOG_LOG, "isc: release_block in cat %d: %d", cat, pos);
404 if (is->files[cat].fc_list)
407 for (j = 0; j<is->files[cat].fc_max; j++)
408 if (!is->files[cat].fc_list[j])
410 is->files[cat].fc_list[j] = pos;
414 release_block (is, cat, pos);
417 static void init_fc (ISAMC is, int cat)
421 is->files[cat].fc_max = j;
422 is->files[cat].fc_list = (int *)
423 xmalloc (sizeof(*is->files[0].fc_list) * j);
425 is->files[cat].fc_list[j] = 0;
428 static void release_fc (ISAMC is, int cat)
430 int b, j = is->files[cat].fc_max;
433 if ((b = is->files[cat].fc_list[j]))
435 release_block (is, cat, b);
436 is->files[cat].fc_list[j] = 0;
440 void isc_pp_close (ISAMC_PP pp)
444 (*is->method->code_stop)(ISAMC_DECODE, pp->decodeClientData);
449 ISAMC_PP isc_pp_open (ISAMC is, ISAMC_P ipos)
451 ISAMC_PP pp = (ISAMC_PP) xmalloc (sizeof(*pp));
454 pp->cat = isc_type(ipos);
455 pp->pos = isc_block(ipos);
457 src = pp->buf = (char *) xmalloc (is->method->filecat[pp->cat].bsize);
463 pp->decodeClientData = (*is->method->code_start)(ISAMC_DECODE);
470 isc_read_block (is, pp->cat, pp->pos, src);
471 memcpy (&pp->next, src, sizeof(pp->next));
472 src += sizeof(pp->next);
473 memcpy (&pp->size, src, sizeof(pp->size));
474 src += sizeof(pp->size);
475 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
476 src += sizeof(pp->numKeys);
477 assert (pp->next != pp->pos);
478 pp->offset = src - pp->buf;
479 assert (pp->offset == ISAMC_BLOCK_OFFSET_1);
480 if (is->method->debug > 2)
481 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
482 pp->size, pp->cat, pp->pos, pp->next);
487 /* returns non-zero if item could be read; 0 otherwise */
488 int isc_pp_read (ISAMC_PP pp, void *buf)
490 return isc_read_item (pp, (char **) &buf);
493 /* read one item from file - decode and store it in *dst.
496 1 if item could be read ok and NO boundary
497 2 if item could be read ok and boundary */
498 int isc_read_item (ISAMC_PP pp, char **dst)
501 char *src = pp->buf + pp->offset;
503 if (pp->offset >= pp->size)
508 return 0; /* end of file */
510 if (pp->next > pp->pos)
512 if (pp->next == pp->pos + 1)
513 is->files[pp->cat].no_next++;
516 is->files[pp->cat].no_forward++;
517 is->files[pp->cat].sum_forward += pp->next - pp->pos;
522 if (pp->next + 1 == pp->pos)
523 is->files[pp->cat].no_prev++;
526 is->files[pp->cat].no_backward++;
527 is->files[pp->cat].sum_backward += pp->pos - pp->next;
530 /* out new block position */
533 /* read block and save 'next' and 'size' entry */
534 isc_read_block (is, pp->cat, pp->pos, src);
535 memcpy (&pp->next, src, sizeof(pp->next));
536 src += sizeof(pp->next);
537 memcpy (&pp->size, src, sizeof(pp->size));
538 src += sizeof(pp->size);
539 /* assume block is non-empty */
540 assert (src - pp->buf == ISAMC_BLOCK_OFFSET_N);
541 assert (pp->next != pp->pos);
543 isc_release_block (is, pp->cat, pp->pos);
544 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
545 pp->offset = src - pp->buf;
546 if (is->method->debug > 2)
547 logf (LOG_LOG, "isc: read_block size=%d %d %d next=%d",
548 pp->size, pp->cat, pp->pos, pp->next);
551 (*is->method->code_item)(ISAMC_DECODE, pp->decodeClientData, dst, &src);
552 pp->offset = src - pp->buf;
556 int isc_pp_num (ISAMC_PP pp)