Extend isamb_merge read handler with new insert mode.
[idzebra-moved-to-github.git] / isamb / isamb.c
index 7cd0ea9..116cd9b 100644 (file)
@@ -1,5 +1,5 @@
-/* $Id: isamb.c,v 1.89 2006-12-18 23:40:08 adam Exp $
-   Copyright (C) 1995-2006
+/* $Id: isamb.c,v 1.94 2007-11-28 09:56:42 adam Exp $
+   Copyright (C) 1995-2007
    Index Data ApS
 
 This file is part of the Zebra server.
@@ -291,17 +291,20 @@ ISAMB isamb_open2(BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
            if (memcmp(hbuf, "isamb", 5))
            {
                yaz_log(YLOG_WARN, "bad isamb header for file %s", fname);
+                isamb_close(isamb);
                return 0;
            }
            if (sscanf(hbuf+5, "%d %d %d", &major, &minor, &len) != 3)
            {
                yaz_log(YLOG_WARN, "bad isamb header for file %s", fname);
+                isamb_close(isamb);
                return 0;
            }
            if (major != ISAMB_MAJOR_VERSION)
            {
                yaz_log(YLOG_WARN, "bad major version for file %s %d, must be %d",
                     fname, major, ISAMB_MAJOR_VERSION);
+                isamb_close(isamb);
                return 0;
            }
            for (left = len - sizes[i]; left > 0; left = left - sizes[i])
@@ -312,6 +315,7 @@ ISAMB isamb_open2(BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
                    yaz_log(YLOG_WARN, "truncated isamb header for " 
                         "file=%s len=%d pos=%d",
                         fname, len, pos);
+                    isamb_close(isamb);
                    return 0;
                }
            }
@@ -327,7 +331,11 @@ ISAMB isamb_open2(BFiles bfs, const char *name, int writeflag, ISAMC_M *method,
                 decode_ptr(&src, &isamb->root_ptr);
        }
         assert (isamb->file[i].head.block_size >= isamb->file[i].head.block_offset);
-        isamb->file[i].head_dirty = 0;
+        /* must rewrite the header if root ptr is in use (bug #1017) */
+        if (use_root_ptr && writeflag)
+            isamb->file[i].head_dirty = 1;
+        else
+            isamb->file[i].head_dirty = 0;
         assert(isamb->file[i].head.block_size == sizes[i]);
     }
 #if ISAMB_DEBUG
@@ -450,6 +458,7 @@ void isamb_close (ISAMB isamb)
     yaz_log(YLOG_DEBUG, "isamb_close returned "ZINT_FORMAT" values, "
                   "skipped "ZINT_FORMAT,
          isamb->skipped_numbers, isamb->returned_numbers);
+
     for (i = 0; i<isamb->no_cat; i++)
     {
         flush_blocks (isamb, i);
@@ -632,7 +641,7 @@ static void check_block (ISAMB b, struct ISAMB_block *p)
 #else
             zint item_len;
             decode_item_len(&src, &item_len);
-            assert (item_len > 0 && item_len < 80);
+            assert (item_len > 0 && item_len < 128);
             src += item_len;
 #endif
             decode_ptr(&src, &pos);
@@ -780,7 +789,7 @@ int insert_int (ISAMB b, struct ISAMB_block *p, void *lookahead_item,
 #if INT_ENCODE
        const char *sub_item_ptr = sub_item;
 #endif
-        assert (sub_size < 80 && sub_size > 1);
+        assert (sub_size < 128 && sub_size > 1);
 
         memcpy (dst, startp, src - startp);
                 
@@ -960,14 +969,22 @@ int insert_leaf (ISAMB b, struct ISAMB_block **sp1, void *lookahead_item,
                     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 (!*lookahead_mode && d == 0)
+            if (d == 0 && !*lookahead_mode)
             {
-               /* 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 */
+                /* 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)
@@ -1277,7 +1294,7 @@ void isamb_merge(ISAMB b, ISAM_P *pos, ISAMC_I *stream)
 #endif
 
             encode_ptr(&dst, p->pos);
-           assert (sub_size < 80 && sub_size > 1);
+           assert (sub_size < 128 && sub_size > 1);
 #if INT_ENCODE
            (*b->method->codec.reset)(c1);
            (*b->method->codec.encode)(c1, &dst, &sub_item_ptr);
@@ -1405,7 +1422,7 @@ void isamb_pp_close (ISAMB_PP pp)
 
 /* simple recursive dumper .. */
 static void isamb_dump_r (ISAMB b, ISAM_P pos, void (*pr)(const char *str),
-                         int level)
+                          int level)
 {
     char buf[1024];
     char prefix_str[1024];