2 * Copyright (c) 2000-2002, Index Data.
3 * See the file LICENSE for details.
5 * $Id: isamb.c,v 1.13 2002-04-30 19:31:09 adam Exp $
7 #include <yaz/xmalloc.h>
19 #define ISAMB_DATA_OFFSET 3
21 #define DST_ITEM_MAX 256
23 /* approx 2*4 K + max size of item */
24 #define DST_BUF_SIZE 8448
30 struct ISAMB_head head;
37 struct ISAMB_file *file;
50 void *decodeClientData;
59 struct ISAMB_block **block;
62 void encode_ptr (char **dst, int pos)
64 memcpy (*dst, &pos, sizeof(pos));
65 (*dst) += sizeof(pos);
68 void decode_ptr (char **src, int *pos)
70 memcpy (pos, *src, sizeof(*pos));
71 (*src) += sizeof(*pos);
74 ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method)
76 ISAMB isamb = xmalloc (sizeof(*isamb));
80 isamb->method = (ISAMC_M) xmalloc (sizeof(*method));
81 memcpy (isamb->method, method, sizeof(*method));
84 isamb->file = xmalloc (sizeof(*isamb->file) * isamb->no_cat);
85 for (i = 0; i<isamb->no_cat; i++)
87 char fname[DST_BUF_SIZE];
88 isamb->file[i].head.first_block = 1;
89 isamb->file[i].head.last_block = 1;
90 isamb->file[i].head.block_size = b_size;
91 isamb->file[i].head.block_max = b_size - ISAMB_DATA_OFFSET;
93 isamb->file[i].head_dirty = 0;
94 sprintf (fname, "%s%c", name, i+'A');
96 bf_open (bfs, fname, isamb->file[i].head.block_size, writeflag);
98 bf_read (isamb->file[i].bf, 0, 0, sizeof(struct ISAMB_head),
99 &isamb->file[i].head);
104 void isamb_close (ISAMB isamb)
107 for (i = 0; i<isamb->no_cat; i++)
109 if (isamb->file[i].head_dirty)
110 bf_write (isamb->file[i].bf, 0, 0,
111 sizeof(struct ISAMB_head), &isamb->file[i].head);
114 xfree (isamb->method);
118 struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
121 struct ISAMB_block *p;
124 p = xmalloc (sizeof(*p));
127 p->buf = xmalloc (b->file[cat].head.block_size);
128 bf_read (b->file[cat].bf, pos/4, 0, 0, p->buf);
129 p->bytes = p->buf + ISAMB_DATA_OFFSET;
131 p->size = p->buf[1] + 256 * p->buf[2] - ISAMB_DATA_OFFSET;
134 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
138 struct ISAMB_block *new_block (ISAMB b, int leaf, int cat)
140 struct ISAMB_block *p;
143 p = xmalloc (sizeof(*p));
144 block_no = b->file[cat].head.last_block++;
146 p->pos = block_no * 4 + cat;
148 b->file[cat].head_dirty = 1;
149 p->buf = xmalloc (b->file[cat].head.block_size);
150 memset (p->buf, 0, b->file[cat].head.block_size);
151 p->bytes = p->buf + ISAMB_DATA_OFFSET;
156 p->decodeClientData = (*b->method->code_start)(ISAMC_DECODE);
160 struct ISAMB_block *new_leaf (ISAMB b, int cat)
162 return new_block (b, 1, cat);
166 struct ISAMB_block *new_int (ISAMB b, int cat)
168 return new_block (b, 0, cat);
171 void close_block (ISAMB b, struct ISAMB_block *p)
177 int size = p->size + ISAMB_DATA_OFFSET;
179 p->buf[1] = size & 255;
180 p->buf[2] = size >> 8;
181 bf_write (b->file[p->cat].bf, p->pos/4, 0, 0, p->buf);
183 (*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
188 int insert_sub (ISAMB b, struct ISAMB_block **p,
189 void *new_item, int *mode,
191 struct ISAMB_block **sp,
192 void *sub_item, int *sub_size,
195 int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
197 ISAMC_I stream, struct ISAMB_block **sp,
198 void *split_item, int *split_size)
200 char *startp = p->bytes;
202 char *endp = p->bytes + p->size;
204 struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
205 char sub_item[DST_ITEM_MAX];
211 decode_ptr (&src, &pos);
216 decode_ptr (&src, &item_len);
217 d = (*b->method->compare_item)(src, lookahead_item);
220 sub_p1 = open_block (b, pos);
222 more = insert_sub (b, &sub_p1, lookahead_item, mode,
224 sub_item, &sub_size, src);
228 decode_ptr (&src, &pos);
232 sub_p1 = open_block (b, pos);
234 more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
235 sub_item, &sub_size, 0);
239 /* there was a split - must insert pointer in this one */
240 char dst_buf[DST_BUF_SIZE];
243 assert (sub_size < 30 && sub_size > 1);
245 memcpy (dst, startp, src - startp);
249 encode_ptr (&dst, sub_size); /* sub length and item */
250 memcpy (dst, sub_item, sub_size);
253 encode_ptr (&dst, sub_p2->pos); /* pos */
255 if (endp - src) /* remaining data */
257 memcpy (dst, src, endp - src);
260 p->size = dst - dst_buf;
261 if (p->size <= b->file[p->cat].head.block_max)
263 memcpy (startp, dst_buf, dst - dst_buf);
272 half = src + b->file[p->cat].head.block_size/2;
273 decode_ptr (&src, &pos);
276 decode_ptr (&src, split_size);
278 decode_ptr (&src, &pos);
280 p_new_size = src - dst_buf;
281 memcpy (p->bytes, dst_buf, p_new_size);
283 decode_ptr (&src, split_size);
284 memcpy (split_item, src, *split_size);
287 *sp = new_int (b, p->cat);
288 (*sp)->size = endp - src;
289 memcpy ((*sp)->bytes, src, (*sp)->size);
291 p->size = p_new_size;
294 close_block (b, sub_p2);
296 close_block (b, sub_p1);
301 int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
302 int *lookahead_mode, ISAMC_I stream, struct ISAMB_block **sp2,
303 void *sub_item, int *sub_size,
306 struct ISAMB_block *p = *sp1;
307 char *src = 0, *endp = 0;
308 char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
310 void *c1 = (*b->method->code_start)(ISAMC_DECODE);
311 void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
313 int quater = b->file[b->no_cat-1].head.block_max / 4;
314 char *cut = dst_buf + quater * 2;
315 char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
318 char cut_item_buf[DST_ITEM_MAX];
319 int cut_item_size = 0;
323 char file_item_buf[DST_ITEM_MAX];
324 char *file_item = file_item_buf;
327 endp = p->bytes + p->size;
328 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
333 char *lookahead_next;
337 d = (*b->method->compare_item)(file_item_buf, lookahead_item);
341 dst_item = lookahead_item;
342 assert (*lookahead_mode);
345 dst_item = file_item_buf;
346 if (!*lookahead_mode && d == 0)
350 else if (!half1 && dst > cut)
352 char *dst_item_0 = dst_item;
353 half1 = dst; /* candidate for splitting */
355 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
357 cut_item_size = dst_item - dst_item_0;
358 memcpy (cut_item_buf, dst_item_0, cut_item_size);
363 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
373 lookahead_next = lookahead_item;
374 if (!(*stream->read_item)(stream->clientData,
382 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
384 yaz_log (LOG_LOG, "max_item 1");
393 lookahead_next = lookahead_item;
394 if (!(*stream->read_item)(stream->clientData,
395 &lookahead_next, lookahead_mode))
402 file_item = file_item_buf;
403 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
409 file_item = file_item_buf;
410 (*b->method->code_item)(ISAMC_DECODE, c1, &file_item, &src);
414 maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
415 while (lookahead_item)
417 char *dst_item = lookahead_item;
421 (*b->method->compare_item)(max_item, lookahead_item) <= 0)
423 yaz_log (LOG_LOG, "max_item 2");
426 if (!*lookahead_mode)
428 yaz_log (LOG_WARN, "Inconsistent register (2)");
431 else if (!half1 && dst > cut)
433 char *dst_item_0 = dst_item;
434 half1 = dst; /* candidate for splitting */
436 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
438 cut_item_size = dst_item - dst_item_0;
439 memcpy (cut_item_buf, dst_item_0, cut_item_size);
444 (*b->method->code_item)(ISAMC_ENCODE, c2, &dst, &dst_item);
453 dst_item = lookahead_item;
454 if (!(*stream->read_item)(stream->clientData, &dst_item,
461 new_size = dst - dst_buf;
462 if (p && p->cat != b->no_cat-1 &&
463 new_size > b->file[p->cat].head.block_max)
465 /* non-btree block will be removed */
467 /* delete it too!! */
468 p = 0; /* make a new one anyway */
471 { /* must create a new one */
473 for (i = 0; i < b->no_cat; i++)
474 if (new_size <= b->file[i].head.block_max)
480 if (new_size > b->file[p->cat].head.block_max)
483 char *cut_item = cut_item_buf;
489 p->size = half1 - dst_buf;
490 memcpy (p->bytes, dst_buf, half1 - dst_buf);
493 *sp2 = new_leaf (b, p->cat);
495 (*b->method->code_reset)(c2);
497 first_dst = (*sp2)->bytes;
499 (*b->method->code_item)(ISAMC_ENCODE, c2, &first_dst, &cut_item);
501 memcpy (first_dst, half2, dst - half2);
503 (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
506 memcpy (sub_item, cut_item_buf, cut_item_size);
507 *sub_size = cut_item_size;
511 memcpy (p->bytes, dst_buf, dst - dst_buf);
514 (*b->method->code_stop)(ISAMC_DECODE, c1);
515 (*b->method->code_stop)(ISAMC_ENCODE, c2);
520 int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
523 struct ISAMB_block **sp,
524 void *sub_item, int *sub_size,
527 if (!*p || (*p)->leaf)
528 return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
531 return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
535 int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I stream)
537 char item_buf[DST_ITEM_MAX];
543 more = (*stream->read_item)(stream->clientData, &item_ptr, &i_mode);
546 struct ISAMB_block *p = 0, *sp = 0;
547 char sub_item[DST_ITEM_MAX];
551 p = open_block (b, pos);
552 more = insert_sub (b, &p, item_buf, &i_mode, stream, &sp,
553 sub_item, &sub_size, 0);
555 { /* increase level of tree by one */
556 struct ISAMB_block *p2 = new_int (b, p->cat);
557 char *dst = p2->bytes + p2->size;
559 encode_ptr (&dst, p->pos);
560 assert (sub_size < 20);
561 encode_ptr (&dst, sub_size);
562 memcpy (dst, sub_item, sub_size);
564 encode_ptr (&dst, sp->pos);
566 p2->size = dst - p2->bytes;
567 pos = p2->pos; /* return new super page */
572 pos = p->pos; /* return current one (again) */
578 ISAMB_PP isamb_pp_open_x (ISAMB isamb, ISAMB_P pos, int *level)
580 ISAMB_PP pp = xmalloc (sizeof(*pp));
583 pp->block = xmalloc (10 * sizeof(*pp->block));
591 struct ISAMB_block *p = open_block (isamb, pos);
592 char *src = p->bytes + p->offset;
593 pp->block[pp->level] = p;
595 pp->total_size += p->size;
601 decode_ptr (&src, &pos);
602 p->offset = src - p->bytes;
605 pp->block[pp->level+1] = 0;
611 ISAMB_PP isamb_pp_open (ISAMB isamb, ISAMB_P pos)
613 return isamb_pp_open_x (isamb, pos, 0);
616 void isamb_pp_close_x (ISAMB_PP pp, int *size, int *blocks)
622 *size = pp->total_size;
624 *blocks = pp->no_blocks;
625 for (i = 0; i <= pp->level; i++)
626 close_block (pp->isamb, pp->block[i]);
631 int isamb_block_info (ISAMB isamb, int cat)
633 if (cat >= 0 && cat < isamb->no_cat)
634 return isamb->file[cat].head.block_size;
638 void isamb_pp_close (ISAMB_PP pp)
640 return isamb_pp_close_x (pp, 0, 0);
643 int isamb_pp_read (ISAMB_PP pp, void *buf)
647 struct ISAMB_block *p = pp->block[pp->level];
651 while (p->offset == p->size)
654 while (p->offset == p->size)
658 close_block (pp->isamb, pp->block[pp->level]);
659 pp->block[pp->level] = 0;
661 p = pp->block[pp->level];
662 assert (!p->leaf); /* must be int */
664 src = p->bytes + p->offset;
666 decode_ptr (&src, &item_len);
668 decode_ptr (&src, &pos);
670 p->offset = src - (char*) p->bytes;
676 pp->block[pp->level] = p = open_block (pp->isamb, pos);
678 pp->total_size += p->size;
681 if (p->leaf) /* leaf */
685 src = p->bytes + p->offset;
686 decode_ptr (&src, &pos);
687 p->offset = src - (char*) p->bytes;
691 assert (p->offset < p->size);
693 src = p->bytes + p->offset;
694 (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
696 p->offset = src - (char*) p->bytes;
700 int isamb_pp_num (ISAMB_PP pp)