Moved isam headers to include/idzebra.
[idzebra-moved-to-github.git] / isamb / tstisamb.c
index a2c9140..1eb402c 100644 (file)
@@ -1,5 +1,5 @@
-/* $Id: tstisamb.c,v 1.1 2003-06-23 15:36:11 adam Exp $
-   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
+/* $Id: tstisamb.c,v 1.12 2004-12-08 14:02:37 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.
@@ -22,10 +22,22 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 #include <string.h>
 #include <yaz/xmalloc.h>
-#include <yaz/log.h>
-#include <isamb.h>
+#include <yaz/ylog.h>
+#include <idzebra/isamb.h>
 #include <assert.h>
 
+static void log_item(int level, const void *b, const char *txt)
+{
+    int x;
+    memcpy(&x, b, sizeof(int));
+    yaz_log(YLOG_DEBUG, "%s %d", txt, x);
+}
+
+static void log_pr(const char *txt)
+{
+    yaz_log(YLOG_DEBUG, "%s", txt);
+}
+
 int compare_item(const void *a, const void *b)
 {
     int ia, ib;
@@ -35,12 +47,12 @@ int compare_item(const void *a, const void *b)
     return ia - ib;
 }
 
-void *code_start(int mode)
+void *code_start()
 {
     return 0;
 }
 
-void code_item(int mode, void *p, char **dst, char **src)
+void code_item(void *p, char **dst, const char **src)
 {
     memcpy (*dst, *src, sizeof(int));
     (*dst) += sizeof(int);
@@ -50,13 +62,15 @@ void code_item(int mode, void *p, char **dst, char **src)
 void code_reset(void *p)
 {
 }
-void code_stop(int mode, void *p)
+void code_stop(void *p)
 {
 }
 
 struct read_info {
     int no;
+    int step;
     int max;
+    int insertMode;
 };
 
 int code_read(void *vp, char **dst, int *insertMode)
@@ -64,19 +78,73 @@ int code_read(void *vp, char **dst, int *insertMode)
     struct read_info *ri = (struct read_info *)vp;
     int x;
 
-    if (ri->no > ri->max)
-       exit(3);
-    if (ri->no == ri->max)
+    if (ri->no >= ri->max)
        return 0;
     x = ri->no;
     memcpy (*dst, &x, sizeof(int));
     (*dst)+=sizeof(int);
 
-    (ri->no)++;
-    *insertMode = 1;
+    ri->no += ri->step;
+    *insertMode = ri->insertMode;
     return 1;
 }
 
+void tst_forward(ISAMB isb, int n)
+{
+    ISAMC_I isamc_i;
+    ISAMC_P isamc_p;
+    struct read_info ri;
+    int i;
+    ISAMB_PP pp;
+
+    /* insert a number of entries */
+    ri.no = 0;
+    ri.step = 1;
+    ri.max = n;
+    ri.insertMode = 1;
+
+    isamc_i.clientData = &ri;
+    isamc_i.read_item = code_read;
+    
+    isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
+
+    /* read the entries */
+    pp = isamb_pp_open (isb, isamc_p, 2);
+    
+    for (i = 0; i<ri.max; i +=2 )
+    {
+       int x = -1;
+       int xu = i;
+       isamb_pp_forward(pp, &x, &xu);
+       if (x != xu && xu != x+1)
+       {
+           yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
+                   x, xu);
+           exit(4);
+       }
+       ri.no++;
+    }
+    isamb_pp_close(pp);
+    
+    pp = isamb_pp_open (isb, isamc_p, 2);
+    for (i = 0; i<ri.max; i += 100)
+    {
+       int x = -1;
+       int xu = i;
+       isamb_pp_forward(pp, &x, &xu);
+       if (x != xu && xu != x+1)
+       {
+           yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
+                   x, xu);
+           exit(4);
+       }
+       ri.no++;
+    }
+    isamb_pp_close(pp);
+
+    isamb_unlink(isb, isamc_p);
+}
+
 void tst_insert(ISAMB isb, int n)
 {
     ISAMC_I isamc_i;
@@ -87,7 +155,9 @@ void tst_insert(ISAMB isb, int n)
 
     /* insert a number of entries */
     ri.no = 0;
+    ri.step = 1;
     ri.max = n;
+    ri.insertMode = 1;
 
     isamc_i.clientData = &ri;
     isamc_i.read_item = code_read;
@@ -95,7 +165,7 @@ void tst_insert(ISAMB isb, int n)
     isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
 
     /* read the entries */
-    pp = isamb_pp_open (isb, isamc_p);
+    pp = isamb_pp_open (isb, isamc_p, 2);
     
     ri.no = 0;
     while(isamb_pp_read (pp, key_buf))
@@ -104,7 +174,7 @@ void tst_insert(ISAMB isb, int n)
        memcpy (&x, key_buf, sizeof(int));
        if (x != ri.no)
        {
-           yaz_log(LOG_DEBUG, "isamb_pp_read. Got %d (expected %d)",
+           yaz_log(YLOG_WARN, "isamb_pp_read. Got %d (expected %d)",
                    x, ri.no);
            exit(3);
        }
@@ -112,12 +182,40 @@ void tst_insert(ISAMB isb, int n)
     }
     if (ri.no != ri.max)
     {
-       yaz_log(LOG_DEBUG, "ri.max != ri.max (%d != %d)", ri.no, ri.max);
+       yaz_log(YLOG_WARN, "ri.max != ri.max (%d != %d)", ri.no, ri.max);
        exit(3);
     }
     isamb_pp_close(pp);
 
-    isamb_unlink(isb, isamc_p);
+    isamb_dump(isb, isamc_p, log_pr);
+
+    /* delete a number of entries (even ones) */
+    ri.no = 0;
+    ri.step = 2;
+    ri.max = n;
+    ri.insertMode = 0;
+
+    isamc_i.clientData = &ri;
+    isamc_i.read_item = code_read;
+    
+    isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
+
+    /* delete a number of entries (odd ones) */
+    ri.no = 1;
+    ri.step = 2;
+    ri.max = n;
+    ri.insertMode = 0;
+
+    isamc_i.clientData = &ri;
+    isamc_i.read_item = code_read;
+    
+    isamc_p = isamb_merge (isb, isamc_p , &isamc_i);
+
+    if (isamc_p)
+    {
+       yaz_log(YLOG_WARN, "isamb_merge did not return empty list");
+       exit(3);
+    }
 }
 
 int main(int argc, char **argv)
@@ -125,30 +223,34 @@ int main(int argc, char **argv)
     BFiles bfs;
     ISAMB isb;
     ISAMC_M method;
-
+    
     if (argc == 2)
-       yaz_log_init_level(LOG_ALL);
+       yaz_log_init_level(YLOG_ALL);
        
     /* setup method (attributes) */
     method.compare_item = compare_item;
-    method.code_start = code_start;
-    method.code_item = code_item;
-    method.code_reset = code_reset;
-    method.code_stop = code_stop;
+    method.log_item = log_item;
+    method.codec.start = code_start;
+    method.codec.encode = code_item;
+    method.codec.decode = code_item;
+    method.codec.reset = code_reset;
+    method.codec.stop = code_stop;
 
     /* create block system */
     bfs = bfs_create(0, 0);
     if (!bfs)
     {
-       yaz_log(LOG_DEBUG, "bfs_create failed");
+       yaz_log(YLOG_WARN, "bfs_create failed");
        exit(1);
     }
 
+    bf_reset(bfs);
+
     /* create isam handle */
     isb = isamb_open (bfs, "isamb", 1, &method, 0);
     if (!isb)
     {
-       yaz_log(LOG_DEBUG, "isamb_open failed");
+       yaz_log(YLOG_WARN, "isamb_open failed");
        exit(2);
     }
     tst_insert(isb, 1);
@@ -157,7 +259,7 @@ int main(int argc, char **argv)
     tst_insert(isb, 100);
     tst_insert(isb, 500);
     tst_insert(isb, 10000);
-    
+    tst_forward(isb, 10000);
     /* close isam handle */
     isamb_close(isb);