41 for (i = 0; i<ptr; i++)
44 if (no_dirty*4 < ptr*3)
47 for (i = 0; i<ptr; i++)
49 int tmp, j, j_min = -1;
50 for (j = i; j<ptr; j++)
52 if (j_min < 0 || mb[j_min].block > mb[j].block)
56 tmp = mb[j_min].
block;
67 char *r_buf,
zint *firstpos,
int cat,
int last,
72 for (i = 0; i<ptr; i++)
82 if (last && i == ptr-1)
84 else if (!mb[i+1].block)
92 for (i = 0; i<ptr; i++)
102 assert (mb[i].block);
104 *firstpos = mb[i].
block;
106 yaz_log (YLOG_LOG,
"isc: skip ptr=%d size=%d %d " ZINT_FORMAT,
107 i, ssize, cat, mb[i].block);
115 *firstpos = mb[i].
block;
119 memcpy (src+
sizeof(
zint)+
sizeof(ssize), numkeys,
sizeof(*numkeys));
121 yaz_log (YLOG_LOG,
"isc: flush ptr=%d numk=" ZINT_FORMAT " size=%d nextpos="
122 ZINT_FORMAT, i, *numkeys, (
int) ssize, mb[i+1].block);
129 yaz_log (YLOG_LOG,
"isc: flush ptr=%d size=%d nextpos=" ZINT_FORMAT,
130 i, (
int) ssize, mb[i+1].block);
132 memcpy (src, &mb[i+1].block,
sizeof(
zint));
133 memcpy (src+
sizeof(
zint), &ssize,
sizeof(ssize));
139 int cat,
zint firstpos)
151 return mb[ptr].
offset + fill - off;
157 char i_item[128], *i_item_ptr;
158 int i_more, i_mode, i;
161 char f_item[128], *f_item_ptr;
170 char r_item_buf[128];
190 yaz_log (YLOG_LOG,
"isc: isamc_merge begin %d " ZINT_FORMAT, cat, pp->
pos);
200 border =
get_border (is, mb, ptr, cat, firstpos);
201 while (i_more || f_more)
203 char *r_item = r_item_buf;
221 yaz_log (YLOG_LOG,
"isc: release A");
234 mb[ptr].
dirty = last_dirty;
235 mb[ptr].
offset = r_offset;
237 yaz_log (YLOG_LOG,
"isc: bound ptr=%d,offset=%d",
245 yaz_log (YLOG_LOG,
"isc: flush A %d sections", ptr);
250 memcpy (r_buf, r_buf + mb[ptr-1].
offset,
258 r_offset = mb[ptr].
offset;
262 border =
get_border (is, mb, ptr, cat, firstpos);
285 memcpy (r_item, i_item, i_item_ptr - i_item);
300 memcpy (r_item, f_item, f_item_ptr - f_item);
313 memcpy (r_item, f_item, f_item_ptr - f_item);
322 yaz_log (YLOG_FATAL,
"Inconsistent register at offset %d",
326 memcpy (r_item, i_item, i_item_ptr - i_item);
336 char *r_out_ptr = r_buf + r_offset;
337 const char *src = r_item;
341 new_offset = r_out_ptr - r_buf;
345 if (border < new_offset && border >= r_offset)
354 mb[ptr].
offset = r_offset;
362 yaz_log (YLOG_LOG,
"isc: flush B %d sections", ptr-1);
367 memcpy (r_buf, r_buf + mb[ptr-1].
offset,
374 memcpy (r_buf + mb[1].
offset, r_buf + r_offset,
375 new_offset - r_offset);
376 new_offset = (new_offset - r_offset) + mb[1].
offset;
379 border =
get_border (is, mb, ptr, cat, firstpos);
381 r_offset = new_offset;
392 for (i = 0; i < ptr; i++)
399 assert (mb[j].
offset == 0);
403 mb[ptr].
offset = r_offset;
404 for (i = 1; i < ptr; i++)
409 yaz_log (YLOG_LOG,
"isc: remap %d border=%d", i, border);
413 yaz_log (YLOG_LOG,
"isc: to %d %d", j, mb[i].
offset);
420 yaz_log (YLOG_LOG,
"isc: remap from %d to %d sections to cat %d",
423 border =
get_border (is, mb, ptr, cat, firstpos);
425 yaz_log (YLOG_LOG,
"isc: border=" ZINT_FORMAT " r_offset=%d",
429 if (mb[ptr].
offset < r_offset)
433 mb[ptr].
offset = r_offset;
437 if (cat == pp->
cat && mb[ptr].
block)
440 yaz_log (YLOG_LOG,
"isc: release C");
449 yaz_log (YLOG_LOG,
"isc: flush C, %d sections", ptr);
461 sizeof(numKeys), &numKeys);
471 flush_blocks (is, mb, ptr, r_buf, &firstpos, cat, 1, &numKeys);
477 yaz_log (YLOG_LOG,
"isc: isamc_merge return %d " ZINT_FORMAT, cat, firstpos);
479 *ipos = cat + firstpos * 8;
int bf_write(BFile bf, zint no, int offset, int nbytes, const void *buf)
writes block of bytes to file (may call exit)
int isamc_write_block(ISAMC is, int cat, zint pos, char *src)
#define ISAMC_BLOCK_OFFSET_N
unsigned ISAMC_BLOCK_SIZE
zint isamc_alloc_block(ISAMC is, int cat)
void isamc_release_block(ISAMC is, int cat, zint pos)
#define ISAMC_BLOCK_OFFSET_1
zint isamc_pp_num(ISAMC_PP pp)
int isamc_read_item(ISAMC_PP pp, char **dst)
void isamc_pp_close(ISAMC_PP pp)
ISAMC_PP isamc_pp_open(ISAMC is, ISAM_P pos)
static void flush_blocks(ISAMC is, struct isamc_merge_block *mb, int ptr, char *r_buf, zint *firstpos, int cat, int last, zint *numkeys)
void isamc_merge(ISAMC is, ISAM_P *ipos, ISAMC_I *data)
static int get_border(ISAMC is, struct isamc_merge_block *mb, zint ptr, int cat, zint firstpos)
int(* read_item)(void *clientData, char **dst, int *insertMode)
int(* compare_item)(const void *a, const void *b)
void(* encode)(void *p, char **dst, const char **src)