+ 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 if (d == 0 && *lookahead_mode == 2)
+ {
+ /* For mode == 2, we insert the new key anyway - even
+ though the comparison is 0. */
+ dst_item = lookahead_item;
+ p->dirty = 1;
+ }
+ else
+ dst_item = file_item_buf;
+
+ if (d == 0 && !*lookahead_mode)
+ {
+ /* it's a deletion and they match so there is nothing
+ to be inserted anyway .. But mark the thing dirty
+ (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);
+
+ b->number_of_leaf_splits++;
+
+ 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;