2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * $Id: isamd.c,v 1.8 1999-08-18 13:28:16 heikki Exp $
6 * Isamd - isam with diffs
7 * Programmed by: Heikki Levanto
10 * - Statistics are missing and/or completely wrong
11 * - Lots of code stolen from isamc, not all needed any more
21 #include "../index/index.h" /* isamd uses the internal structure of it_key */
24 static void flush_block (ISAMD is, int cat);
25 static void release_fc (ISAMD is, int cat);
26 static void init_fc (ISAMD is, int cat);
28 #define ISAMD_FREELIST_CHUNK 1
32 ISAMD_M isamd_getmethod (ISAMD_M me)
34 static struct ISAMD_filecat_s def_cat[] = {
36 /* blocksz, max. Unused time being */
49 /* old values from isamc, long time ago...
59 ISAMD_M m = (ISAMD_M) xmalloc (sizeof(*m)); /* never released! */
60 m->filecat = def_cat; /* ok, only alloc'd once */
67 m->compare_item = NULL;
69 m->debug = 0; /* default to no debug */
71 m->max_blocks_mem = 10;
78 ISAMD isamd_open (BFiles bfs, const char *name, int writeflag, ISAMD_M method)
81 ISAMD_filecat filecat;
84 is = (ISAMD) xmalloc (sizeof(*is));
86 is->method = (ISAMD_M) xmalloc (sizeof(*is->method));
87 memcpy (is->method, method, sizeof(*method));
88 filecat = is->method->filecat;
91 /* determine number of block categories */
92 if (is->method->debug>0)
93 logf (LOG_LOG, "isamd: bsize maxkeys");
96 if (is->method->debug>0)
97 logf (LOG_LOG, "isamd:%6d %6d",
98 filecat[i].bsize, filecat[i].mblocks);
99 } while (filecat[i++].mblocks);
103 assert (is->no_files > 0);
104 assert (is->max_cat <=8 ); /* we have only 3 bits for it */
106 is->files = (ISAMD_file) xmalloc (sizeof(*is->files)*is->no_files);
108 for (i = 0; i<is->no_files; i++)
112 sprintf (fname, "%s%c", name, i+'A');
113 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
115 is->files[i].head_is_dirty = 0;
116 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMD_head),
119 is->files[i].head.lastblock = 1;
120 is->files[i].head.freelist = 0;
122 is->files[i].alloc_entries_num = 0;
123 is->files[i].alloc_entries_max =
124 is->method->filecat[i].bsize / sizeof(int) - 1;
125 is->files[i].alloc_buf = (char *)
126 xmalloc (is->method->filecat[i].bsize);
127 is->files[i].no_writes = 0; /* clear statistics */
128 is->files[i].no_reads = 0;
129 is->files[i].no_skip_writes = 0;
130 is->files[i].no_allocated = 0;
131 is->files[i].no_released = 0;
132 is->files[i].no_remap = 0;
133 is->files[i].no_forward = 0;
134 is->files[i].no_backward = 0;
135 is->files[i].sum_forward = 0;
136 is->files[i].sum_backward = 0;
137 is->files[i].no_next = 0;
138 is->files[i].no_prev = 0;
145 int isamd_block_used (ISAMD is, int type)
147 if (type < 0 || type >= is->no_files)
149 return is->files[type].head.lastblock-1;
152 int isamd_block_size (ISAMD is, int type)
154 ISAMD_filecat filecat = is->method->filecat;
155 if (type < 0 || type >= is->no_files)
157 return filecat[type].bsize;
160 int isamd_close (ISAMD is)
164 if (is->method->debug>0)
166 logf (LOG_LOG, "isamd: next forw mid-f prev backw mid-b");
167 for (i = 0; i<is->no_files; i++)
168 logf (LOG_LOG, "isamd:%8d%8d%8.1f%8d%8d%8.1f",
169 is->files[i].no_next,
170 is->files[i].no_forward,
171 is->files[i].no_forward ?
172 (double) is->files[i].sum_forward/is->files[i].no_forward
174 is->files[i].no_prev,
175 is->files[i].no_backward,
176 is->files[i].no_backward ?
177 (double) is->files[i].sum_backward/is->files[i].no_backward
180 if (is->method->debug>0)
181 logf (LOG_LOG, "isamd: writes reads skipped alloc released remap");
182 for (i = 0; i<is->no_files; i++)
185 assert (is->files[i].bf);
186 if (is->files[i].head_is_dirty)
187 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMD_head),
189 if (is->method->debug>0)
190 logf (LOG_LOG, "isamd:%8d%8d%8d%8d%8d%8d",
191 is->files[i].no_writes,
192 is->files[i].no_reads,
193 is->files[i].no_skip_writes,
194 is->files[i].no_allocated,
195 is->files[i].no_released,
196 is->files[i].no_remap);
197 xfree (is->files[i].fc_list);
199 bf_close (is->files[i].bf);
207 int isamd_read_block (ISAMD is, int cat, int pos, char *dst)
209 ++(is->files[cat].no_reads);
210 if (is->method->debug > 6)
211 logf (LOG_LOG, "isamd: read_block %d %d", cat, pos);
212 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
215 int isamd_write_block (ISAMD is, int cat, int pos, char *src)
217 ++(is->files[cat].no_writes);
218 if (is->method->debug > 6)
219 logf (LOG_LOG, "isamd: write_block %d %d", cat, pos);
220 return bf_write (is->files[cat].bf, pos, 0, 0, src);
223 int isamd_write_dblock (ISAMD is, int cat, int pos, char *src,
224 int nextpos, int offset)
226 ISAMD_BLOCK_SIZE size = offset + ISAMD_BLOCK_OFFSET_N;
227 if (is->method->debug > 4)
228 logf (LOG_LOG, "isamd: write_dblock. size=%d nextpos=%d",
229 (int) size, nextpos);
230 src -= ISAMD_BLOCK_OFFSET_N;
231 assert( ISAMD_BLOCK_OFFSET_N == sizeof(int)+sizeof(int) );
232 memcpy (src, &nextpos, sizeof(int));
233 memcpy (src + sizeof(int), &size, sizeof(size));
234 return isamd_write_block (is, cat, pos, src);
237 #if ISAMD_FREELIST_CHUNK
238 static void flush_block (ISAMD is, int cat)
240 char *abuf = is->files[cat].alloc_buf;
241 int block = is->files[cat].head.freelist;
242 if (block && is->files[cat].alloc_entries_num)
244 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
245 bf_write (is->files[cat].bf, block, 0, 0, abuf);
246 is->files[cat].alloc_entries_num = 0;
251 static int alloc_block (ISAMD is, int cat)
253 int block = is->files[cat].head.freelist;
254 char *abuf = is->files[cat].alloc_buf;
256 (is->files[cat].no_allocated)++;
260 block = (is->files[cat].head.lastblock)++; /* no free list */
261 is->files[cat].head_is_dirty = 1;
265 if (!is->files[cat].alloc_entries_num) /* read first time */
267 bf_read (is->files[cat].bf, block, 0, 0, abuf);
268 memcpy (&is->files[cat].alloc_entries_num, abuf,
269 sizeof(is->files[cat].alloc_entries_num));
270 assert (is->files[cat].alloc_entries_num > 0);
272 /* have some free blocks now */
273 assert (is->files[cat].alloc_entries_num > 0);
274 is->files[cat].alloc_entries_num--;
275 if (!is->files[cat].alloc_entries_num) /* last one in block? */
277 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
279 is->files[cat].head_is_dirty = 1;
281 if (is->files[cat].head.freelist)
283 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
285 memcpy (&is->files[cat].alloc_entries_num, abuf,
286 sizeof(is->files[cat].alloc_entries_num));
287 assert (is->files[cat].alloc_entries_num);
291 memcpy (&block, abuf + sizeof(int) + sizeof(int) *
292 is->files[cat].alloc_entries_num, sizeof(int));
297 static void release_block (ISAMD is, int cat, int pos)
299 char *abuf = is->files[cat].alloc_buf;
300 int block = is->files[cat].head.freelist;
302 (is->files[cat].no_released)++;
304 if (block && !is->files[cat].alloc_entries_num) /* must read block */
306 bf_read (is->files[cat].bf, block, 0, 0, abuf);
307 memcpy (&is->files[cat].alloc_entries_num, abuf,
308 sizeof(is->files[cat].alloc_entries_num));
309 assert (is->files[cat].alloc_entries_num > 0);
311 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
312 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
315 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
316 bf_write (is->files[cat].bf, block, 0, 0, abuf);
317 is->files[cat].alloc_entries_num = 0;
319 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
321 memcpy (abuf + sizeof(int), &block, sizeof(int));
322 is->files[cat].head.freelist = pos;
323 is->files[cat].head_is_dirty = 1;
327 memcpy (abuf + sizeof(int) +
328 is->files[cat].alloc_entries_num*sizeof(int),
331 is->files[cat].alloc_entries_num++;
334 static void flush_block (ISAMD is, int cat)
336 char *abuf = is->files[cat].alloc_buf;
340 static int alloc_block (ISAMD is, int cat)
343 char buf[sizeof(int)];
345 is->files[cat].head_is_dirty = 1;
346 (is->files[cat].no_allocated)++;
347 if ((block = is->files[cat].head.freelist))
349 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
350 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
353 block = (is->files[cat].head.lastblock)++;
357 static void release_block (ISAMD is, int cat, int pos)
359 char buf[sizeof(int)];
361 (is->files[cat].no_released)++;
362 is->files[cat].head_is_dirty = 1;
363 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
364 is->files[cat].head.freelist = pos;
365 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
369 int isamd_alloc_block (ISAMD is, int cat)
373 if (is->files[cat].fc_list)
376 for (j = 0; j < is->files[cat].fc_max; j++)
377 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
379 is->files[cat].fc_list[j] = 0;
385 block = alloc_block (is, cat);
386 if (is->method->debug > 4)
387 logf (LOG_LOG, "isamd: alloc_block in cat %d: %d", cat, block);
391 void isamd_release_block (ISAMD is, int cat, int pos)
393 if (is->method->debug > 4)
394 logf (LOG_LOG, "isamd: release_block in cat %d: %d", cat, pos);
397 if (is->files[cat].fc_list)
400 for (j = 0; j<is->files[cat].fc_max; j++)
401 if (!is->files[cat].fc_list[j])
403 is->files[cat].fc_list[j] = pos;
407 release_block (is, cat, pos);
410 static void init_fc (ISAMD is, int cat)
414 is->files[cat].fc_max = j;
415 is->files[cat].fc_list = (int *)
416 xmalloc (sizeof(*is->files[0].fc_list) * j);
418 is->files[cat].fc_list[j] = 0;
421 static void release_fc (ISAMD is, int cat)
423 int b, j = is->files[cat].fc_max;
426 if ((b = is->files[cat].fc_list[j]))
428 release_block (is, cat, b);
429 is->files[cat].fc_list[j] = 0;
433 void isamd_pp_close (ISAMD_PP pp)
437 (*is->method->code_stop)(ISAMD_DECODE, pp->decodeClientData);
438 isamd_free_diffs(pp); /* see merge-d.h */
441 if (is->method->debug > 5)
442 logf (LOG_LOG, "isamd_pp_close %p %d=%d:%d sz=%d n=%d=%d:%d",
443 pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size,
444 pp->next, isamd_type(pp->next), isamd_block(pp->next) );
449 ISAMD_PP isamd_pp_open (ISAMD is, ISAMD_P ipos)
451 ISAMD_PP pp = (ISAMD_PP) xmalloc (sizeof(*pp));
453 int sz = is->method->filecat[is->max_cat].bsize;
454 /* always allocate for the largest blocks, saves trouble */
456 pp->cat = isamd_type(ipos);
457 pp->pos = isamd_block(ipos);
459 src = pp->buf = (char *) xmalloc (sz);
460 memset(src,'\0',sz); /* clear the buffer, for new blocks */
466 pp->decodeClientData = (*is->method->code_start)(ISAMD_DECODE);
476 isamd_read_block (is, pp->cat, pp->pos, src);
477 memcpy (&pp->next, src, sizeof(pp->next));
478 src += sizeof(pp->next);
479 memcpy (&pp->size, src, sizeof(pp->size));
480 src += sizeof(pp->size);
481 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
482 src += sizeof(pp->numKeys);
483 memcpy (&pp->diffs, src, sizeof(pp->diffs));
484 src += sizeof(pp->diffs);
485 assert (pp->next != pp->pos);
486 pp->offset = src - pp->buf;
487 assert (pp->offset == ISAMD_BLOCK_OFFSET_1);
489 if (is->method->debug > 5)
490 logf (LOG_LOG, "isamd_pp_open %p %d=%d:%d sz=%d n=%d=%d:%d",
491 pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size,
492 pp->next, isamd_type(pp->next), isamd_block(pp->next) );
500 void isamd_buildfirstblock(ISAMD_PP pp){
503 assert(pp->next != pp->pos);
504 memcpy(dst, &pp->next, sizeof(pp->next) );
505 dst += sizeof(pp->next);
506 memcpy(dst, &pp->size,sizeof(pp->size));
507 dst += sizeof(pp->size);
508 memcpy(dst, &pp->numKeys, sizeof(pp->numKeys));
509 dst += sizeof(pp->numKeys);
510 memcpy(dst, &pp->diffs, sizeof(pp->diffs));
511 dst += sizeof(pp->diffs);
512 assert (dst - pp->buf == ISAMD_BLOCK_OFFSET_1);
513 if (pp->is->method->debug > 5)
514 logf (LOG_LOG, "isamd: first: sz=%d p=%d/%d>%d/%d nk=%d d=%d",
517 isamd_type(pp->next), isamd_block(pp->next),
518 pp->numKeys, pp->diffs);
521 void isamd_buildlaterblock(ISAMD_PP pp){
524 assert(pp->next != isamd_addr(pp->pos,pp->cat));
525 memcpy(dst, &pp->next, sizeof(pp->next) );
526 dst += sizeof(pp->next);
527 memcpy(dst, &pp->size,sizeof(pp->size));
528 dst += sizeof(pp->size);
529 assert (dst - pp->buf == ISAMD_BLOCK_OFFSET_N);
530 if (pp->is->method->debug > 5)
531 logf (LOG_LOG, "isamd: l8r: sz=%d p=%d/%d>%d/%d",
534 isamd_block(pp->next), isamd_type(pp->next) );
539 /* returns non-zero if item could be read; 0 otherwise */
540 int isamd_pp_read (ISAMD_PP pp, void *buf)
542 return isamd_read_item (pp, (char **) &buf);
543 /* note: isamd_read_item is in merge-d.c, because it is so */
544 /* convoluted with the merge process */
547 /* read one main item from file - decode and store it in *dst.
548 Does not worry about diffs
551 1 if item could be read ok
553 int isamd_read_main_item (ISAMD_PP pp, char **dst)
556 char *src = pp->buf + pp->offset;
559 if (pp->offset >= pp->size)
564 return 0; /* end of file */
566 if (pp->next > pp->pos)
568 if (pp->next == pp->pos + 1)
569 is->files[pp->cat].no_next++;
572 is->files[pp->cat].no_forward++;
573 is->files[pp->cat].sum_forward += pp->next - pp->pos;
578 if (pp->next + 1 == pp->pos)
579 is->files[pp->cat].no_prev++;
582 is->files[pp->cat].no_backward++;
583 is->files[pp->cat].sum_backward += pp->pos - pp->next;
586 /* out new block position */
587 newcat = isamd_type(pp->next);
588 pp->pos = isamd_block(pp->next);
589 pp->cat = isamd_type(pp->next);
592 /* read block and save 'next' and 'size' entry */
593 isamd_read_block (is, pp->cat, pp->pos, src);
594 memcpy (&pp->next, src, sizeof(pp->next));
595 src += sizeof(pp->next);
596 memcpy (&pp->size, src, sizeof(pp->size));
597 src += sizeof(pp->size);
598 /* assume block is non-empty */
599 assert (src - pp->buf == ISAMD_BLOCK_OFFSET_N);
600 assert (pp->next != isamd_addr(pp->pos,pp->cat));
601 (*is->method->code_reset)(pp->decodeClientData);
602 (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
603 pp->offset = src - pp->buf;
604 if (is->method->debug > 4)
605 logf (LOG_LOG, "isamd: read_block size=%d %d %d next=%d",
606 pp->size, pp->cat, pp->pos, pp->next);
609 (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
610 pp->offset = src - pp->buf;
614 int isamd_pp_num (ISAMD_PP pp)
619 static char *hexdump(unsigned char *p, int len, char *buff) {
620 static char localbuff[128];
622 if (!buff) buff=localbuff;
625 sprintf(bytebuff,"%02x",*p);
627 strcat(buff,bytebuff);
628 if (len) strcat(buff," ");
634 void isamd_pp_dump (ISAMD is, ISAMD_P ipos)
644 logf(LOG_LOG,"dumping isamd block %d (%d:%d)",
645 (int)ipos, isamd_type(ipos), isamd_block(ipos) );
646 pp=isamd_pp_open(is,ipos);
647 logf(LOG_LOG,"numKeys=%d, ofs=%d d=%d",
649 pp->offset, pp->diffs);
651 while(isamd_pp_read(pp, &key))
653 if (oldaddr != isamd_addr(pp->pos,pp->cat) )
655 oldaddr = isamd_addr(pp->pos,pp->cat);
656 logf(LOG_LOG,"block %d (%d:%d) sz=%d nx=%d (%d:%d) ofs=%d",
657 isamd_addr(pp->pos,pp->cat),
658 pp->cat, pp->pos, pp->size,
659 pp->next, isamd_type(pp->next), isamd_block(pp->next),
665 logf(LOG_LOG," %05x: %s",i,hexdump(pp->buf+i,n,hexbuff));
668 if (oldoffs > ISAMD_BLOCK_OFFSET_N)
669 oldoffs=ISAMD_BLOCK_OFFSET_N;
672 logf (LOG_LOG," got %d:%d=%x:%x from %s at %d=%x",
673 key.sysno, key.seqno,
674 key.sysno, key.seqno,
675 hexdump(pp->buf+oldoffs, pp->offset-oldoffs, hexbuff),
677 oldoffs = pp->offset;
679 /*!*/ /*TODO: dump diffs too!!! */
685 * Revision 1.8 1999-08-18 13:28:16 heikki
686 * Set log levels to decent values
688 * Revision 1.6 1999/08/17 19:44:25 heikki
691 * Revision 1.4 1999/08/04 14:21:18 heikki
692 * isam-d seems to be working.
694 * Revision 1.3 1999/07/21 14:24:50 heikki
695 * isamd write and read functions ok, except when diff block full.
696 * (merge not yet done)
698 * Revision 1.1 1999/07/14 12:34:43 heikki
699 * Copied from isamh, starting to change things...