-/* $Id: isamb.c,v 1.26 2003-04-15 16:38:32 adam Exp $
- Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
+/* $Id: isamb.c,v 1.28 2004-05-30 18:04:49 adam Exp $
+ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
Index Data Aps
This file is part of the Zebra server.
#define ISAMB_DATA_OFFSET 3
+/* maximum size of encoded buffer */
#define DST_ITEM_MAX 256
-/* approx 2*4 K + max size of item */
-#define DST_BUF_SIZE 8448
+/* approx 2*max page + max size of item */
+#define DST_BUF_SIZE 16840
#define ISAMB_CACHE_ENTRY_SIZE 4096
+/* CAT_MAX: _must_ be power of 2 */
+#define CAT_MAX 4
+#define CAT_MASK (CAT_MAX-1)
+/* CAT_NO: <= CAT_MAX */
+#define CAT_NO 4
+
+/* ISAMB_PTR_CODEC=1 var, =0 fixed */
+#define ISAMB_PTR_CODEC 1
+
struct ISAMB_cache_entry {
ISAMB_P pos;
unsigned char *buf;
struct ISAMB_cache_entry *next;
};
-
struct ISAMB_file {
BFile bf;
int head_dirty;
struct ISAMB_s {
BFiles bfs;
- ISAMC_M method;
+ ISAMC_M *method;
struct ISAMB_file *file;
int no_cat;
struct ISAMB_block **block;
};
-void encode_ptr (char **dst, int pos)
+#if ISAMB_PTR_CODEC
+static void encode_ptr (char **dst, unsigned pos)
{
- memcpy (*dst, &pos, sizeof(pos));
+ unsigned char *bp = (unsigned char*) *dst;
+
+ while (pos > 127)
+ {
+ *bp++ = 128 | (pos & 127);
+ pos = pos >> 7;
+ }
+ *bp++ = pos;
+ *dst = (char *) bp;
+}
+#else
+static void encode_ptr (char **dst, unsigned pos)
+{
+ memcpy(*dst, &pos, sizeof(pos));
(*dst) += sizeof(pos);
}
+#endif
-void decode_ptr (char **src, int *pos)
+#if ISAMB_PTR_CODEC
+static void decode_ptr (char **src1, int *pos)
+{
+ unsigned char **src = (unsigned char **) src1;
+ unsigned d = 0;
+ unsigned char c;
+ unsigned r = 0;
+
+ while (((c = *(*src)++) & 128))
+ {
+ d += ((c & 127) << r);
+ r += 7;
+ }
+ d += (c << r);
+ *pos = d;
+}
+#else
+static void decode_ptr (char **src, int *pos)
{
- memcpy (pos, *src, sizeof(*pos));
- (*src) += sizeof(*pos);
+ memcpy (pos, *src, sizeof(*pos));
+ (*src) += sizeof(*pos);
}
+#endif
-ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M method,
+ISAMB isamb_open (BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
int cache)
{
ISAMB isamb = xmalloc (sizeof(*isamb));
int i, b_size = 32;
isamb->bfs = bfs;
- isamb->method = (ISAMC_M) xmalloc (sizeof(*method));
+ isamb->method = (ISAMC_M *) xmalloc (sizeof(*method));
memcpy (isamb->method, method, sizeof(*method));
- isamb->no_cat = 4;
+ isamb->no_cat = CAT_NO;
isamb->log_io = 0;
isamb->log_freelist = 0;
isamb->cache = cache;
static int get_block (ISAMB b, ISAMC_P pos, char *userbuf, int wr)
{
- int cat = pos&3;
- int off = ((pos/4) &
+ int cat = pos&CAT_MASK;
+ int off = ((pos/CAT_MAX) &
(ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size - 1))
* b->file[cat].head.block_size;
- int norm = pos / (4*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
+ int norm = pos / (CAT_MASK*ISAMB_CACHE_ENTRY_SIZE / b->file[cat].head.block_size);
int no = 0;
struct ISAMB_cache_entry **ce, *ce_this = 0, **ce_last = 0;
struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
{
- int cat = pos&3;
+ int cat = pos&CAT_MASK;
struct ISAMB_block *p;
if (!pos)
return 0;
p = xmalloc (sizeof(*p));
p->pos = pos;
- p->cat = pos & 3;
+ p->cat = pos & CAT_MASK;
p->buf = xmalloc (b->file[cat].head.block_size);
if (!get_block (b, pos, p->buf, 0))
{
yaz_log (b->log_io, "bf_read: open_block");
- if (!bf_read (b->file[cat].bf, pos/4, 0, 0, p->buf))
+ if (!bf_read (b->file[cat].bf, pos/CAT_MAX, 0, 0, p->buf))
{
yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
- (long) pos, (long) pos/4);
+ (long) pos, (long) pos/CAT_MAX);
abort();
}
}
{
int block_no;
block_no = b->file[cat].head.last_block++;
- p->pos = block_no * 4 + cat;
+ p->pos = block_no * CAT_MAX + cat;
}
else
{
p->pos = b->file[cat].head.free_list;
- assert((p->pos & 3) == cat);
+ assert((p->pos & CAT_MASK) == cat);
if (!get_block (b, p->pos, p->buf, 0))
{
yaz_log (b->log_io, "bf_read: new_block");
- if (!bf_read (b->file[cat].bf, p->pos/4, 0, 0, p->buf))
+ if (!bf_read (b->file[cat].bf, p->pos/CAT_MAX, 0, 0, p->buf))
{
yaz_log (LOG_FATAL, "isamb: read fail for pos=%ld block=%ld",
- (long) p->pos/4, (long) p->pos/4);
+ (long) p->pos/CAT_MAX, (long) p->pos/CAT_MAX);
abort ();
}
}
yaz_log (b->log_freelist, "got block %d from freelist %d:%d", p->pos,
- cat, p->pos/4);
+ cat, p->pos/CAT_MAX);
memcpy (&b->file[cat].head.free_list, p->buf, sizeof(int));
}
p->cat = cat;
int pos;
decode_ptr (&src, &pos);
- assert ((pos&3) == p->cat);
+ assert ((pos&CAT_MASK) == p->cat);
while (src != endp)
{
int item_len;
assert (item_len > 0 && item_len < 30);
src += item_len;
decode_ptr (&src, &pos);
- assert ((pos&3) == p->cat);
+ assert ((pos&CAT_MASK) == p->cat);
}
}
}
if (p->deleted)
{
yaz_log (b->log_freelist, "release block %d from freelist %d:%d",
- p->pos, p->cat, p->pos/4);
+ p->pos, p->cat, p->pos/CAT_MAX);
memcpy (p->buf, &b->file[p->cat].head.free_list, sizeof(int));
b->file[p->cat].head.free_list = p->pos;
if (!get_block (b, p->pos, p->buf, 1))
{
yaz_log (b->log_io, "bf_write: close_block (deleted)");
- bf_write (b->file[p->cat].bf, p->pos/4, 0, 0, p->buf);
+ bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
}
}
else if (p->dirty)
if (!get_block (b, p->pos, p->buf, 1))
{
yaz_log (b->log_io, "bf_write: close_block");
- bf_write (b->file[p->cat].bf, p->pos/4, 0, 0, p->buf);
+ bf_write (b->file[p->cat].bf, p->pos/CAT_MAX, 0, 0, p->buf);
}
}
(*b->method->code_stop)(ISAMC_DECODE, p->decodeClientData);
int insert_sub (ISAMB b, struct ISAMB_block **p,
void *new_item, int *mode,
- ISAMC_I stream,
+ ISAMC_I *stream,
struct ISAMB_block **sp,
void *sub_item, int *sub_size,
void *max_item);
int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
int *mode,
- ISAMC_I stream, struct ISAMB_block **sp,
+ ISAMC_I *stream, struct ISAMB_block **sp,
void *split_item, int *split_size, void *last_max_item)
{
char *startp = p->bytes;
int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
- int *lookahead_mode, ISAMC_I stream, struct ISAMB_block **sp2,
+ int *lookahead_mode, ISAMC_I *stream,
+ struct ISAMB_block **sp2,
void *sub_item, int *sub_size,
void *max_item)
{
void *c1 = (*b->method->code_start)(ISAMC_DECODE);
void *c2 = (*b->method->code_start)(ISAMC_ENCODE);
int more = 1;
- int quater = b->file[b->no_cat-1].head.block_max / 4;
+ int quater = b->file[b->no_cat-1].head.block_max / CAT_MAX;
char *cut = dst_buf + quater * 2;
char *maxp = dst_buf + b->file[b->no_cat-1].head.block_max;
char *half1 = 0;
int insert_sub (ISAMB b, struct ISAMB_block **p, void *new_item,
int *mode,
- ISAMC_I stream,
+ ISAMC_I *stream,
struct ISAMB_block **sp,
void *sub_item, int *sub_size,
void *max_item)
sub_size, max_item);
}
-int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I stream)
+int isamb_unlink (ISAMB b, ISAMC_P pos)
+{
+ struct ISAMB_block *p1;
+
+ if (!pos)
+ return 0;
+ p1 = open_block(b, pos);
+ p1->deleted = 1;
+ if (!p1->leaf)
+ {
+ int sub_p;
+ int item_len;
+ char *src = p1->bytes + p1->offset;
+
+ decode_ptr(&src, &sub_p);
+ isamb_unlink(b, sub_p);
+
+ while (src != p1->bytes + p1->size)
+ {
+ decode_ptr(&src, &item_len);
+ src += item_len;
+ decode_ptr(&src, &sub_p);
+ isamb_unlink(b, sub_p);
+ }
+ }
+ close_block(b, p1);
+ return 0;
+}
+
+int isamb_merge (ISAMB b, ISAMC_P pos, ISAMC_I *stream)
{
char item_buf[DST_ITEM_MAX];
char *item_ptr;