+ char *startp = p->bytes;
+ const char *src = startp;
+ char *endp = p->bytes + p->size;
+ ISAM_P pos;
+ struct ISAMB_block *sub_p1 = 0, *sub_p2 = 0;
+ char sub_item[DST_ITEM_MAX];
+ int sub_size;
+ int more = 0;
+ zint diff_terms = 0;
+ void *c1 = (*b->method->codec.start)();
+
+ *sp = 0;
+
+ assert(p->size >= 0);
+ decode_ptr(&src, &pos);
+ while (src != endp)
+ {
+ int d;
+ const char *src0 = src;
+#if INT_ENCODE
+ char file_item_buf[DST_ITEM_MAX];
+ char *file_item = file_item_buf;
+ (*b->method->codec.reset)(c1);
+ (*b->method->codec.decode)(c1, &file_item, &src);
+ d = (*b->method->compare_item)(file_item_buf, lookahead_item);
+ if (d > 0)
+ {
+ sub_p1 = open_block(b, pos);
+ assert (sub_p1);
+ diff_terms -= sub_p1->no_items;
+ more = insert_sub (b, &sub_p1, lookahead_item, mode,
+ stream, &sub_p2,
+ sub_item, &sub_size, file_item_buf);
+ diff_terms += sub_p1->no_items;
+ src = src0;
+ break;
+ }
+#else
+ zint item_len;
+ decode_item_len(&src, &item_len);
+ d = (*b->method->compare_item)(src, lookahead_item);
+ if (d > 0)
+ {
+ sub_p1 = open_block(b, pos);
+ assert (sub_p1);
+ diff_terms -= sub_p1->no_items;
+ more = insert_sub (b, &sub_p1, lookahead_item, mode,
+ stream, &sub_p2,
+ sub_item, &sub_size, src);
+ diff_terms += sub_p1->no_items;
+ src = src0;
+ break;
+ }
+ src += item_len;
+#endif
+ decode_ptr(&src, &pos);
+ }
+ if (!sub_p1)
+ {
+ /* we reached the end. So lookahead > last item */
+ sub_p1 = open_block(b, pos);
+ assert (sub_p1);
+ diff_terms -= sub_p1->no_items;
+ more = insert_sub (b, &sub_p1, lookahead_item, mode, stream, &sub_p2,
+ sub_item, &sub_size, last_max_item);
+ diff_terms += sub_p1->no_items;
+ }
+ if (sub_p2)
+ diff_terms += sub_p2->no_items;
+ if (diff_terms)
+ {
+ p->dirty = 1;
+ p->no_items += diff_terms;
+ }
+ if (sub_p2)
+ {
+ /* there was a split - must insert pointer in this one */
+ char dst_buf[DST_BUF_SIZE];
+ char *dst = dst_buf;
+#if INT_ENCODE
+ const char *sub_item_ptr = sub_item;
+#endif
+ assert (sub_size < 80 && sub_size > 1);
+
+ memcpy (dst, startp, src - startp);
+
+ dst += src - startp;
+
+#if INT_ENCODE
+ (*b->method->codec.reset)(c1);
+ (*b->method->codec.encode)(c1, &dst, &sub_item_ptr);
+#else
+ encode_item_len (&dst, sub_size); /* sub length and item */
+ memcpy (dst, sub_item, sub_size);
+ dst += sub_size;
+#endif
+
+ encode_ptr(&dst, sub_p2->pos); /* pos */
+
+ if (endp - src) /* remaining data */
+ {
+ memcpy (dst, src, endp - src);
+ dst += endp - src;
+ }
+ p->size = dst - dst_buf;
+ assert (p->size >= 0);
+
+ if (p->size <= b->file[p->cat].head.block_max)
+ {
+ /* it fits OK in this block */
+ memcpy (startp, dst_buf, dst - dst_buf);
+
+ close_block(b, sub_p2);
+ }
+ else
+ {
+ /* must split _this_ block as well .. */
+ struct ISAMB_block *sub_p3;
+#if INT_ENCODE
+ char file_item_buf[DST_ITEM_MAX];
+ char *file_item = file_item_buf;
+#else
+ zint split_size_tmp;
+#endif
+ zint no_items_first_half = 0;
+ int p_new_size;
+ const char *half;
+ src = dst_buf;
+ endp = dst;
+
+ p->dirty = 1;
+ close_block(b, sub_p2);
+
+ half = src + b->file[p->cat].head.block_size/2;
+ decode_ptr(&src, &pos);
+
+ /* read sub block so we can get no_items for it */
+ sub_p3 = open_block(b, pos);
+ no_items_first_half += sub_p3->no_items;
+ close_block(b, sub_p3);
+
+ while (src <= half)
+ {
+#if INT_ENCODE
+ file_item = file_item_buf;
+ (*b->method->codec.reset)(c1);
+ (*b->method->codec.decode)(c1, &file_item, &src);
+#else
+ decode_item_len(&src, &split_size_tmp);
+ *split_size = (int) split_size_tmp;
+ src += *split_size;
+#endif
+ decode_ptr(&src, &pos);
+
+ /* read sub block so we can get no_items for it */
+ sub_p3 = open_block(b, pos);
+ no_items_first_half += sub_p3->no_items;
+ close_block(b, sub_p3);
+ }
+ /* p is first half */
+ p_new_size = src - dst_buf;
+ memcpy (p->bytes, dst_buf, p_new_size);
+
+#if INT_ENCODE
+ file_item = file_item_buf;
+ (*b->method->codec.reset)(c1);
+ (*b->method->codec.decode)(c1, &file_item, &src);
+ *split_size = file_item - file_item_buf;
+ memcpy(split_item, file_item_buf, *split_size);
+#else
+ decode_item_len(&src, &split_size_tmp);
+ *split_size = (int) split_size_tmp;
+ memcpy (split_item, src, *split_size);
+ src += *split_size;
+#endif
+ /* *sp is second half */
+ *sp = new_int (b, p->cat);
+ (*sp)->size = endp - src;
+ memcpy ((*sp)->bytes, src, (*sp)->size);
+
+ p->size = p_new_size;
+
+ /* adjust no_items in first&second half */
+ (*sp)->no_items = p->no_items - no_items_first_half;
+ p->no_items = no_items_first_half;
+ }
+ p->dirty = 1;
+ }
+ close_block(b, sub_p1);
+ (*b->method->codec.stop)(c1);
+ return more;
+}
+
+int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
+ int *lookahead_mode, ISAMC_I *stream,
+ struct ISAMB_block **sp2,
+ void *sub_item, int *sub_size,
+ const void *max_item)
+{
+ struct ISAMB_block *p = *sp1;
+ char *endp = 0;
+ const char *src = 0;
+ char dst_buf[DST_BUF_SIZE], *dst = dst_buf;
+ int new_size;
+ void *c1 = (*b->method->codec.start)();
+ void *c2 = (*b->method->codec.start)();
+ int more = 1;
+ int quater = b->file[b->no_cat-1].head.block_max / 4;
+ char *mid_cut = dst_buf + quater * 2;
+ char *tail_cut = dst_buf + quater * 3;
+ char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
+ char *half1 = 0;
+ char *half2 = 0;
+ char cut_item_buf[DST_ITEM_MAX];
+ int cut_item_size = 0;
+ int no_items = 0; /* number of items (total) */
+ int no_items_1 = 0; /* number of items (first half) */
+ int inserted_dst_bytes = 0;
+
+ if (p && p->size)
+ {
+ char file_item_buf[DST_ITEM_MAX];
+ char *file_item = file_item_buf;
+
+ src = p->bytes;
+ endp = p->bytes + p->size;
+ (*b->method->codec.decode)(c1, &file_item, &src);
+ while (1)
+ {
+ const char *dst_item = 0; /* resulting item to be inserted */
+ char *lookahead_next;
+ char *dst_0 = dst;
+ int d = -1;
+
+ if (lookahead_item)
+ d = (*b->method->compare_item)(file_item_buf, lookahead_item);
+
+ /* d now holds comparison between existing file item and
+ lookahead item
+ d = 0: equal
+ d > 0: lookahead before file
+ d < 0: lookahead after file
+ */
+ if (d > 0)
+ {
+ /* lookahead must be inserted */
+ dst_item = lookahead_item;
+ /* if this is not an insertion, it's really bad .. */
+ if (!*lookahead_mode)
+ {
+ yaz_log(YLOG_WARN, "isamb: Inconsistent register (1)");
+ assert(*lookahead_mode);
+ }
+ }
+ else
+ dst_item = file_item_buf;
+
+ if (!*lookahead_mode && d == 0)
+ {
+ /* it's a deletion and they match so there is nothing to be
+ inserted anyway .. But mark the thing bad (file item
+ was part of input.. The item will not be part of output */
+ p->dirty = 1;
+ }
+ else if (!half1 && dst > mid_cut)
+ {
+ /* we have reached the splitting point for the first time */
+ const char *dst_item_0 = dst_item;
+ half1 = dst; /* candidate for splitting */
+
+ /* encode the resulting item */
+ (*b->method->codec.encode)(c2, &dst, &dst_item);
+
+ cut_item_size = dst_item - dst_item_0;
+ assert(cut_item_size > 0);
+ memcpy (cut_item_buf, dst_item_0, cut_item_size);
+
+ half2 = dst;
+ no_items_1 = no_items;
+ no_items++;
+ }
+ else
+ {
+ /* encode the resulting item */
+ (*b->method->codec.encode)(c2, &dst, &dst_item);
+ no_items++;
+ }
+
+ /* now move "pointers" .. result has been encoded .. */
+ if (d > 0)
+ {
+ /* we must move the lookahead pointer */
+
+ inserted_dst_bytes += (dst - dst_0);
+ if (inserted_dst_bytes >= quater)
+ /* no more room. Mark lookahead as "gone".. */
+ lookahead_item = 0;
+ else
+ {
+ /* move it really.. */
+ lookahead_next = lookahead_item;
+ if (!(*stream->read_item)(stream->clientData,
+ &lookahead_next,
+ lookahead_mode))
+ {
+ /* end of stream reached: no "more" and no lookahead */
+ lookahead_item = 0;
+ more = 0;
+ }
+ if (lookahead_item && max_item &&
+ (*b->method->compare_item)(max_item, lookahead_item) <= 0)
+ {
+ /* the lookahead goes beyond what we allow in this
+ leaf. Mark it as "gone" */
+ lookahead_item = 0;
+ }
+
+ p->dirty = 1;
+ }
+ }
+ else if (d == 0)
+ {
+ /* exact match .. move both pointers */
+
+ lookahead_next = lookahead_item;
+ if (!(*stream->read_item)(stream->clientData,
+ &lookahead_next, lookahead_mode))
+ {
+ lookahead_item = 0;
+ more = 0;
+ }
+ if (src == endp)
+ break; /* end of file stream reached .. */
+ file_item = file_item_buf; /* move file pointer */
+ (*b->method->codec.decode)(c1, &file_item, &src);
+ }
+ else
+ {
+ /* file pointer must be moved */
+ if (src == endp)
+ break;
+ file_item = file_item_buf;
+ (*b->method->codec.decode)(c1, &file_item, &src);
+ }
+ }
+ }
+
+ /* this loop runs when we are "appending" to a leaf page. That is
+ either it's empty (new) or all file items have been read in
+ previous loop */
+
+ maxp = dst_buf + b->file[b->no_cat-1].head.block_max + quater;
+ while (lookahead_item)
+ {
+ char *dst_item;
+ const char *src = lookahead_item;
+ char *dst_0 = dst;
+
+ /* if we have a lookahead item, we stop if we exceed the value of it */
+ if (max_item &&
+ (*b->method->compare_item)(max_item, lookahead_item) <= 0)
+ {
+ /* stop if we have reached the value of max item */
+ break;
+ }
+ if (!*lookahead_mode)
+ {
+ /* this is append. So a delete is bad */
+ yaz_log(YLOG_WARN, "isamb: Inconsistent register (2)");
+ assert(*lookahead_mode);
+ }
+ else if (!half1 && dst > tail_cut)
+ {
+ const char *src_0 = src;
+ half1 = dst; /* candidate for splitting */
+
+ (*b->method->codec.encode)(c2, &dst, &src);
+
+ cut_item_size = src - src_0;
+ assert(cut_item_size > 0);
+ memcpy (cut_item_buf, src_0, cut_item_size);
+
+ no_items_1 = no_items;
+ half2 = dst;
+ }
+ else
+ (*b->method->codec.encode)(c2, &dst, &src);
+
+ if (dst > maxp)
+ {
+ dst = dst_0;
+ break;
+ }
+ no_items++;
+ if (p)
+ p->dirty = 1;
+ dst_item = lookahead_item;
+ if (!(*stream->read_item)(stream->clientData, &dst_item,
+ lookahead_mode))
+ {
+ lookahead_item = 0;
+ more = 0;
+ }
+ }
+ new_size = dst - dst_buf;
+ if (p && p->cat != b->no_cat-1 &&
+ new_size > b->file[p->cat].head.block_max)
+ {
+ /* non-btree block will be removed */
+ p->deleted = 1;
+ close_block(b, p);
+ /* delete it too!! */
+ p = 0; /* make a new one anyway */
+ }
+ if (!p)
+ { /* must create a new one */
+ int i;
+ for (i = 0; i < b->no_cat; i++)
+ if (new_size <= b->file[i].head.block_max)
+ break;
+ if (i == b->no_cat)
+ i = b->no_cat - 1;
+ p = new_leaf (b, i);
+ }
+ if (new_size > b->file[p->cat].head.block_max)
+ {
+ char *first_dst;
+ const char *cut_item = cut_item_buf;
+
+ assert (half1);
+ assert (half2);
+
+ assert(cut_item_size > 0);
+
+ /* first half */
+ p->size = half1 - dst_buf;
+ assert(p->size <= b->file[p->cat].head.block_max);
+ memcpy (p->bytes, dst_buf, half1 - dst_buf);
+ p->no_items = no_items_1;
+
+ /* second half */
+ *sp2 = new_leaf (b, p->cat);
+
+ (*b->method->codec.reset)(c2);
+
+ first_dst = (*sp2)->bytes;
+
+ (*b->method->codec.encode)(c2, &first_dst, &cut_item);
+
+ memcpy (first_dst, half2, dst - half2);
+
+ (*sp2)->size = (first_dst - (*sp2)->bytes) + (dst - half2);
+ assert((*sp2)->size <= b->file[p->cat].head.block_max);
+ (*sp2)->no_items = no_items - no_items_1;
+ (*sp2)->dirty = 1;
+ p->dirty = 1;
+ memcpy (sub_item, cut_item_buf, cut_item_size);
+ *sub_size = cut_item_size;
+ }
+ else
+ {
+ memcpy (p->bytes, dst_buf, dst - dst_buf);
+ p->size = new_size;
+ p->no_items = no_items;
+ }
+ (*b->method->codec.stop)(c1);
+ (*b->method->codec.stop)(c2);
+ *sp1 = p;
+ return more;
+}
+
+int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
+ int *mode,
+ ISAMC_I *stream,
+ struct ISAMB_block **sp,
+ void *sub_item, int *sub_size,
+ const void *max_item)
+{
+ if (!*p || (*p)->leaf)
+ return insert_leaf (b, p, new_item, mode, stream, sp, sub_item,
+ sub_size, max_item);
+ else
+ return insert_int (b, *p, new_item, mode, stream, sp, sub_item,
+ sub_size, max_item);
+}
+
+int isamb_unlink (ISAMB b, ISAM_P pos)
+{
+ struct ISAMB_block *p1;
+
+ if (!pos)
+ return 0;
+ p1 = open_block(b, pos);
+ p1->deleted = 1;
+ if (!p1->leaf)