2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
6 * Isamd - isam with diffs
17 #include "../index/index.h" /* isamd uses the internal structure of it_key */
20 static void flush_block (ISAMD is, int cat);
21 static void release_fc (ISAMD is, int cat);
22 static void init_fc (ISAMD is, int cat);
24 #define ISAMD_FREELIST_CHUNK 1
28 ISAMD_M isamd_getmethod (ISAMD_M me)
30 static struct ISAMD_filecat_s def_cat[] = {
32 /* blocksz, max keys before switching size. Unused time being */
45 /* old values from isamc, long time ago...
54 /* assume about 2 bytes per pointer, when compressed. The head uses */
55 /* 16 bytes, and other blocks use 8 for header info... If you want 3 */
56 /* blocks of 32 bytes, say max 16+24+24 = 64 keys */
59 ISAMD_M m = (ISAMD_M) xmalloc (sizeof(*m)); /*??? never released??? */
67 m->compare_item = NULL;
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)
93 logf (LOG_LOG, "isamd: bsize maxkeys");
96 if (is->method->debug)
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);
109 /* TODO - what ever needs to be done here... */
115 for (i = 0; i<is->no_files; i++)
119 sprintf (fname, "%s%c", name, i+'A');
120 is->files[i].bf = bf_open (bfs, fname, is->method->filecat[i].bsize,
122 is->files[i].head_is_dirty = 0;
123 if (!bf_read (is->files[i].bf, 0, 0, sizeof(ISAMD_head),
126 is->files[i].head.lastblock = 1;
127 is->files[i].head.freelist = 0;
129 is->files[i].alloc_entries_num = 0;
130 is->files[i].alloc_entries_max =
131 is->method->filecat[i].bsize / sizeof(int) - 1;
132 is->files[i].alloc_buf = (char *)
133 xmalloc (is->method->filecat[i].bsize);
134 is->files[i].no_writes = 0; /* clear statistics */
135 is->files[i].no_reads = 0;
136 is->files[i].no_skip_writes = 0;
137 is->files[i].no_allocated = 0;
138 is->files[i].no_released = 0;
139 is->files[i].no_remap = 0;
140 is->files[i].no_forward = 0;
141 is->files[i].no_backward = 0;
142 is->files[i].sum_forward = 0;
143 is->files[i].sum_backward = 0;
144 is->files[i].no_next = 0;
145 is->files[i].no_prev = 0;
152 int isamd_block_used (ISAMD is, int type)
154 if (type < 0 || type >= is->no_files)
156 return is->files[type].head.lastblock-1;
159 int isamd_block_size (ISAMD is, int type)
161 ISAMD_filecat filecat = is->method->filecat;
162 if (type < 0 || type >= is->no_files)
164 return filecat[type].bsize;
167 int isamd_close (ISAMD is)
171 if (is->method->debug)
173 logf (LOG_LOG, "isamd: next forw mid-f prev backw mid-b");
174 for (i = 0; i<is->no_files; i++)
175 logf (LOG_LOG, "isamd:%8d%8d%8.1f%8d%8d%8.1f",
176 is->files[i].no_next,
177 is->files[i].no_forward,
178 is->files[i].no_forward ?
179 (double) is->files[i].sum_forward/is->files[i].no_forward
181 is->files[i].no_prev,
182 is->files[i].no_backward,
183 is->files[i].no_backward ?
184 (double) is->files[i].sum_backward/is->files[i].no_backward
187 if (is->method->debug)
188 logf (LOG_LOG, "isamd: writes reads skipped alloc released remap");
189 for (i = 0; i<is->no_files; i++)
192 assert (is->files[i].bf);
193 if (is->files[i].head_is_dirty)
194 bf_write (is->files[i].bf, 0, 0, sizeof(ISAMD_head),
196 if (is->method->debug)
197 logf (LOG_LOG, "isamd:%8d%8d%8d%8d%8d%8d",
198 is->files[i].no_writes,
199 is->files[i].no_reads,
200 is->files[i].no_skip_writes,
201 is->files[i].no_allocated,
202 is->files[i].no_released,
203 is->files[i].no_remap);
204 xfree (is->files[i].fc_list);
206 bf_close (is->files[i].bf);
214 int isamd_read_block (ISAMD is, int cat, int pos, char *dst)
216 ++(is->files[cat].no_reads);
217 return bf_read (is->files[cat].bf, pos, 0, 0, dst);
220 int isamd_write_block (ISAMD is, int cat, int pos, char *src)
222 ++(is->files[cat].no_writes);
223 if (is->method->debug > 2)
224 logf (LOG_LOG, "isamd: write_block %d %d", cat, pos);
225 return bf_write (is->files[cat].bf, pos, 0, 0, src);
228 int isamd_write_dblock (ISAMD is, int cat, int pos, char *src,
229 int nextpos, int offset)
231 ISAMD_BLOCK_SIZE size = offset + ISAMD_BLOCK_OFFSET_N;
232 if (is->method->debug > 2)
233 logf (LOG_LOG, "isamd: write_dblock. size=%d nextpos=%d",
234 (int) size, nextpos);
235 src -= ISAMD_BLOCK_OFFSET_N;
236 assert( ISAMD_BLOCK_OFFSET_N == sizeof(int)+sizeof(int) );
237 memcpy (src, &nextpos, sizeof(int));
238 memcpy (src + sizeof(int), &size, sizeof(size));
239 return isamd_write_block (is, cat, pos, src);
242 #if ISAMD_FREELIST_CHUNK
243 static void flush_block (ISAMD is, int cat)
245 char *abuf = is->files[cat].alloc_buf;
246 int block = is->files[cat].head.freelist;
247 if (block && is->files[cat].alloc_entries_num)
249 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
250 bf_write (is->files[cat].bf, block, 0, 0, abuf);
251 is->files[cat].alloc_entries_num = 0;
256 static int alloc_block (ISAMD is, int cat)
258 int block = is->files[cat].head.freelist;
259 char *abuf = is->files[cat].alloc_buf;
261 (is->files[cat].no_allocated)++;
265 block = (is->files[cat].head.lastblock)++; /* no free list */
266 is->files[cat].head_is_dirty = 1;
270 if (!is->files[cat].alloc_entries_num) /* read first time */
272 bf_read (is->files[cat].bf, block, 0, 0, abuf);
273 memcpy (&is->files[cat].alloc_entries_num, abuf,
274 sizeof(is->files[cat].alloc_entries_num));
275 assert (is->files[cat].alloc_entries_num > 0);
277 /* have some free blocks now */
278 assert (is->files[cat].alloc_entries_num > 0);
279 is->files[cat].alloc_entries_num--;
280 if (!is->files[cat].alloc_entries_num) /* last one in block? */
282 memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
284 is->files[cat].head_is_dirty = 1;
286 if (is->files[cat].head.freelist)
288 bf_read (is->files[cat].bf, is->files[cat].head.freelist,
290 memcpy (&is->files[cat].alloc_entries_num, abuf,
291 sizeof(is->files[cat].alloc_entries_num));
292 assert (is->files[cat].alloc_entries_num);
296 memcpy (&block, abuf + sizeof(int) + sizeof(int) *
297 is->files[cat].alloc_entries_num, sizeof(int));
302 static void release_block (ISAMD is, int cat, int pos)
304 char *abuf = is->files[cat].alloc_buf;
305 int block = is->files[cat].head.freelist;
307 (is->files[cat].no_released)++;
309 if (block && !is->files[cat].alloc_entries_num) /* must read block */
311 bf_read (is->files[cat].bf, block, 0, 0, abuf);
312 memcpy (&is->files[cat].alloc_entries_num, abuf,
313 sizeof(is->files[cat].alloc_entries_num));
314 assert (is->files[cat].alloc_entries_num > 0);
316 assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
317 if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
320 memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
321 bf_write (is->files[cat].bf, block, 0, 0, abuf);
322 is->files[cat].alloc_entries_num = 0;
324 if (!is->files[cat].alloc_entries_num) /* make new buffer? */
326 memcpy (abuf + sizeof(int), &block, sizeof(int));
327 is->files[cat].head.freelist = pos;
328 is->files[cat].head_is_dirty = 1;
332 memcpy (abuf + sizeof(int) +
333 is->files[cat].alloc_entries_num*sizeof(int),
336 is->files[cat].alloc_entries_num++;
339 static void flush_block (ISAMD is, int cat)
341 char *abuf = is->files[cat].alloc_buf;
345 static int alloc_block (ISAMD is, int cat)
348 char buf[sizeof(int)];
350 is->files[cat].head_is_dirty = 1;
351 (is->files[cat].no_allocated)++;
352 if ((block = is->files[cat].head.freelist))
354 bf_read (is->files[cat].bf, block, 0, sizeof(int), buf);
355 memcpy (&is->files[cat].head.freelist, buf, sizeof(int));
358 block = (is->files[cat].head.lastblock)++;
362 static void release_block (ISAMD is, int cat, int pos)
364 char buf[sizeof(int)];
366 (is->files[cat].no_released)++;
367 is->files[cat].head_is_dirty = 1;
368 memcpy (buf, &is->files[cat].head.freelist, sizeof(int));
369 is->files[cat].head.freelist = pos;
370 bf_write (is->files[cat].bf, pos, 0, sizeof(int), buf);
374 int isamd_alloc_block (ISAMD is, int cat)
378 if (is->files[cat].fc_list)
381 for (j = 0; j < is->files[cat].fc_max; j++)
382 if ((nb = is->files[cat].fc_list[j]) && (!block || nb < block))
384 is->files[cat].fc_list[j] = 0;
390 block = alloc_block (is, cat);
391 if (is->method->debug > 3)
392 logf (LOG_LOG, "isamd: alloc_block in cat %d: %d", cat, block);
396 void isamd_release_block (ISAMD is, int cat, int pos)
398 if (is->method->debug > 3)
399 logf (LOG_LOG, "isamd: release_block in cat %d: %d", cat, pos);
402 if (is->files[cat].fc_list)
405 for (j = 0; j<is->files[cat].fc_max; j++)
406 if (!is->files[cat].fc_list[j])
408 is->files[cat].fc_list[j] = pos;
412 release_block (is, cat, pos);
415 static void init_fc (ISAMD is, int cat)
419 is->files[cat].fc_max = j;
420 is->files[cat].fc_list = (int *)
421 xmalloc (sizeof(*is->files[0].fc_list) * j);
423 is->files[cat].fc_list[j] = 0;
426 static void release_fc (ISAMD is, int cat)
428 int b, j = is->files[cat].fc_max;
431 if ((b = is->files[cat].fc_list[j]))
433 release_block (is, cat, b);
434 is->files[cat].fc_list[j] = 0;
438 void isamd_pp_close (ISAMD_PP pp)
442 (*is->method->code_stop)(ISAMD_DECODE, pp->decodeClientData);
443 isamd_free_diffs(pp); /* see merge-d.h */
446 if (is->method->debug > 2)
447 logf (LOG_LOG, "isamd_pp_close %p %d=%d:%d sz=%d n=%d=%d:%d",
448 pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size,
449 pp->next, isamd_type(pp->next), isamd_block(pp->next) );
454 ISAMD_PP isamd_pp_open (ISAMD is, ISAMD_P ipos)
456 ISAMD_PP pp = (ISAMD_PP) xmalloc (sizeof(*pp));
458 int sz = is->method->filecat[is->max_cat].bsize;
459 /* always allocate for the largest blocks, saves trouble */
461 pp->cat = isamd_type(ipos);
462 pp->pos = isamd_block(ipos);
464 src = pp->buf = (char *) xmalloc (sz);
465 memset(src,'\0',sz); /* clear the buffer, for new blocks */
471 pp->decodeClientData = (*is->method->code_start)(ISAMD_DECODE);
481 isamd_read_block (is, pp->cat, pp->pos, src);
482 memcpy (&pp->next, src, sizeof(pp->next));
483 src += sizeof(pp->next);
484 memcpy (&pp->size, src, sizeof(pp->size));
485 src += sizeof(pp->size);
486 memcpy (&pp->numKeys, src, sizeof(pp->numKeys));
487 src += sizeof(pp->numKeys);
488 memcpy (&pp->diffs, src, sizeof(pp->diffs));
489 src += sizeof(pp->diffs);
490 assert (pp->next != pp->pos);
491 pp->offset = src - pp->buf;
492 assert (pp->offset == ISAMD_BLOCK_OFFSET_1);
493 // if (is->method->debug > 2)
494 // logf (LOG_LOG, "isamd_pp_open %p %d=%d:%d sz=%d n=%d=%d:%d",
495 // pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size,
496 // pp->next, isamd_type(pp->next), isamd_block(pp->next) );
498 if (is->method->debug > 2)
499 logf (LOG_LOG, "isamd_pp_open %p %d=%d:%d sz=%d n=%d=%d:%d",
500 pp, isamd_addr(pp->pos, pp->cat), pp->cat, pp->pos, pp->size,
501 pp->next, isamd_type(pp->next), isamd_block(pp->next) );
509 void isamd_buildfirstblock(ISAMD_PP pp){
512 assert(pp->next != pp->pos);
513 memcpy(dst, &pp->next, sizeof(pp->next) );
514 dst += sizeof(pp->next);
515 memcpy(dst, &pp->size,sizeof(pp->size));
516 dst += sizeof(pp->size);
517 memcpy(dst, &pp->numKeys, sizeof(pp->numKeys));
518 dst += sizeof(pp->numKeys);
519 memcpy(dst, &pp->diffs, sizeof(pp->diffs));
520 dst += sizeof(pp->diffs);
521 assert (dst - pp->buf == ISAMD_BLOCK_OFFSET_1);
522 if (pp->is->method->debug > 2)
523 logf (LOG_LOG, "isamd: first: sz=%d p=%d/%d>%d/%d nk=%d d=%d",
526 isamd_type(pp->next), isamd_block(pp->next),
527 pp->numKeys, pp->diffs);
530 void isamd_buildlaterblock(ISAMD_PP pp){
533 assert(pp->next != isamd_addr(pp->pos,pp->cat));
534 memcpy(dst, &pp->next, sizeof(pp->next) );
535 dst += sizeof(pp->next);
536 memcpy(dst, &pp->size,sizeof(pp->size));
537 dst += sizeof(pp->size);
538 assert (dst - pp->buf == ISAMD_BLOCK_OFFSET_N);
539 if (pp->is->method->debug > 2)
540 logf (LOG_LOG, "isamd: l8r: sz=%d p=%d/%d>%d/%d",
543 isamd_block(pp->next), isamd_type(pp->next) );
548 /* returns non-zero if item could be read; 0 otherwise */
549 int isamd_pp_read (ISAMD_PP pp, void *buf)
551 return isamd_read_item (pp, (char **) &buf);
552 /* note: isamd_read_item is in merge-d.c, because it is so */
553 /* convoluted with the merge process */
556 /* read one main item from file - decode and store it in *dst.
557 Does not worry about diffs
560 1 if item could be read ok
562 int isamd_read_main_item (ISAMD_PP pp, char **dst)
565 char *src = pp->buf + pp->offset;
568 if (pp->offset >= pp->size)
573 return 0; /* end of file */
575 if (pp->next > pp->pos)
577 if (pp->next == pp->pos + 1)
578 is->files[pp->cat].no_next++;
581 is->files[pp->cat].no_forward++;
582 is->files[pp->cat].sum_forward += pp->next - pp->pos;
587 if (pp->next + 1 == pp->pos)
588 is->files[pp->cat].no_prev++;
591 is->files[pp->cat].no_backward++;
592 is->files[pp->cat].sum_backward += pp->pos - pp->next;
595 /* out new block position */
596 newcat = isamd_type(pp->next);
597 pp->pos = isamd_block(pp->next);
598 pp->cat = isamd_type(pp->next);
601 /* read block and save 'next' and 'size' entry */
602 isamd_read_block (is, pp->cat, pp->pos, src);
603 memcpy (&pp->next, src, sizeof(pp->next));
604 src += sizeof(pp->next);
605 memcpy (&pp->size, src, sizeof(pp->size));
606 src += sizeof(pp->size);
607 /* assume block is non-empty */
608 assert (src - pp->buf == ISAMD_BLOCK_OFFSET_N);
609 assert (pp->next != isamd_addr(pp->pos,pp->cat));
610 //if (pp->deleteFlag)
611 // isamd_release_block (is, pp->cat, pp->pos);
612 (*is->method->code_reset)(pp->decodeClientData);
613 (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
614 pp->offset = src - pp->buf;
615 if (is->method->debug > 2)
616 logf (LOG_LOG, "isamd: read_block size=%d %d %d next=%d",
617 pp->size, pp->cat, pp->pos, pp->next);
620 (*is->method->code_item)(ISAMD_DECODE, pp->decodeClientData, dst, &src);
621 pp->offset = src - pp->buf;
625 int isamd_pp_num (ISAMD_PP pp)
630 static char *hexdump(unsigned char *p, int len, char *buff) {
631 static char localbuff[128];
633 if (!buff) buff=localbuff;
636 sprintf(bytebuff,"%02x",*p);
638 strcat(buff,bytebuff);
639 if (len) strcat(buff," ");
645 void isamd_pp_dump (ISAMD is, ISAMD_P ipos)
655 logf(LOG_LOG,"dumping isamd block %d (%d:%d)",
656 (int)ipos, isamd_type(ipos), isamd_block(ipos) );
657 pp=isamd_pp_open(is,ipos);
658 logf(LOG_LOG,"numKeys=%d, ofs=%d d=%d",
660 pp->offset, pp->diffs);
662 while(isamd_pp_read(pp, &key))
664 if (oldaddr != isamd_addr(pp->pos,pp->cat) )
666 oldaddr = isamd_addr(pp->pos,pp->cat);
667 logf(LOG_LOG,"block %d (%d:%d) sz=%d nx=%d (%d:%d) ofs=%d",
668 isamd_addr(pp->pos,pp->cat),
669 pp->cat, pp->pos, pp->size,
670 pp->next, isamd_type(pp->next), isamd_block(pp->next),
676 logf(LOG_LOG," %05x: %s",i,hexdump(pp->buf+i,n,hexbuff));
679 if (oldoffs > ISAMD_BLOCK_OFFSET_N)
680 oldoffs=ISAMD_BLOCK_OFFSET_N;
683 logf (LOG_LOG," got %d:%d=%x:%x from %s at %d=%x",
684 key.sysno, key.seqno,
685 key.sysno, key.seqno,
686 hexdump(pp->buf+oldoffs, pp->offset-oldoffs, hexbuff),
688 oldoffs = pp->offset;
690 /*!*/ /*TODO: dump diffs too!!! */
696 * Revision 1.7 1999-08-18 10:45:27 heikki
697 * Another fix for the difflen problem.
699 * Revision 1.6 1999/08/17 19:44:25 heikki
702 * Revision 1.4 1999/08/04 14:21:18 heikki
703 * isam-d seems to be working.
705 * Revision 1.3 1999/07/21 14:24:50 heikki
706 * isamd write and read functions ok, except when diff block full.
707 * (merge not yet done)
709 * Revision 1.1 1999/07/14 12:34:43 heikki
710 * Copied from isamh, starting to change things...