Added other sort index test.
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 15 May 2008 09:45:16 +0000 (11:45 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 15 May 2008 09:45:16 +0000 (11:45 +0200)
include/sortidx.h
index/sortidx.c
test/api/.gitignore
test/api/Makefile.am
test/api/test_sort1.c
test/api/test_sortidx.c [new file with mode: 0644]

index b4f74e5..31d210b 100644 (file)
@@ -28,18 +28,63 @@ YAZ_BEGIN_CDECL
 
 #define SORT_IDX_ENTRYSIZE 64
 
 
 #define SORT_IDX_ENTRYSIZE 64
 
+/** \var zebra_sort_index_t
+    \brief sort index handle
+*/
 typedef struct zebra_sort_index *zebra_sort_index_t;
 
 #define ZEBRA_SORT_TYPE_FLAT 1
 #define ZEBRA_SORT_TYPE_ISAMB 2
 
 typedef struct zebra_sort_index *zebra_sort_index_t;
 
 #define ZEBRA_SORT_TYPE_FLAT 1
 #define ZEBRA_SORT_TYPE_ISAMB 2
 
+
+/** \brief creates sort handle
+    \param bfs block files handle
+    \param write_flag (0=read-only, 1=write and read)
+    \param sort_type one of ZEBRA_SORT_TYPE_..
+    \return sort index handle
+*/
 zebra_sort_index_t zebra_sort_open(BFiles bfs, int write_flag, int sort_type);
 zebra_sort_index_t zebra_sort_open(BFiles bfs, int write_flag, int sort_type);
+
+/** \brief frees sort handle
+*/
 void zebra_sort_close(zebra_sort_index_t si);
 void zebra_sort_close(zebra_sort_index_t si);
+
+/** \brief sets type for sort usage
+    \param si sort index handle
+    \param type opaque type .. A sort file for each type is created
+*/
 int zebra_sort_type(zebra_sort_index_t si, int type);
 int zebra_sort_type(zebra_sort_index_t si, int type);
+
+/** \brief sets sort system number for read / add / delete
+    \param si sort index handle
+    \param sysno system number
+*/
 void zebra_sort_sysno(zebra_sort_index_t si, zint sysno);
 void zebra_sort_sysno(zebra_sort_index_t si, zint sysno);
+
+/** \brief adds content to sort file
+    \param si sort index handle
+    \param buf buffer content
+    \param len length
+
+    zebra_sort_type and zebra_sort_sysno must be called prior to this
+*/
 void zebra_sort_add(zebra_sort_index_t si, const char *buf, int len);
 void zebra_sort_add(zebra_sort_index_t si, const char *buf, int len);
+
+
+/** \brief delete sort entry
+    \param si sort index handle
+
+    zebra_sort_type and zebra_sort_sysno must be called prior to this
+*/
 void zebra_sort_delete(zebra_sort_index_t si);
 void zebra_sort_delete(zebra_sort_index_t si);
-void zebra_sort_read(zebra_sort_index_t si, char *buf);
+
+/** \brief reads sort entry
+    \param si sort index handle
+    \param buf resulting buffer
+    \retval 0 could not be read
+    \retval 1 could be read (found)
+*/
+int zebra_sort_read(zebra_sort_index_t si, char *buf);
 
 YAZ_END_CDECL
 
 
 YAZ_END_CDECL
 
index 8fe9672..6f96c37 100644 (file)
@@ -47,7 +47,7 @@ static void sort_term_log_item(int level, const void *b, const char *txt)
     yaz_log(level, "%s " ZINT_FORMAT " %s", txt, a1.sysno, a1.term);
 }
 
     yaz_log(level, "%s " ZINT_FORMAT " %s", txt, a1.sysno, a1.term);
 }
 
-int sort_term_compare(const void *a, const void *b)
+static int sort_term_compare(const void *a, const void *b)
 {
     struct sort_term a1, b1;
 
 {
     struct sort_term a1, b1;
 
@@ -61,12 +61,12 @@ int sort_term_compare(const void *a, const void *b)
     return 0;
 }
 
     return 0;
 }
 
-void *sort_term_code_start(void)
+static void *sort_term_code_start(void)
 {
     return 0;
 }
 
 {
     return 0;
 }
 
-void sort_term_encode(void *p, char **dst, const char **src)
+static void sort_term_encode(void *p, char **dst, const char **src)
 {
     struct sort_term a1;
 
 {
     struct sort_term a1;
 
@@ -78,7 +78,7 @@ void sort_term_encode(void *p, char **dst, const char **src)
     *dst += strlen(a1.term) + 1;
 }
 
     *dst += strlen(a1.term) + 1;
 }
 
-void sort_term_decode(void *p, char **dst, const char **src)
+static void sort_term_decode(void *p, char **dst, const char **src)
 {
     struct sort_term a1;
 
 {
     struct sort_term a1;
 
@@ -91,22 +91,21 @@ void sort_term_decode(void *p, char **dst, const char **src)
     *dst += sizeof(a1);
 }
 
     *dst += sizeof(a1);
 }
 
-void sort_term_code_reset(void *p)
+static void sort_term_code_reset(void *p)
 {
 }
 
 {
 }
 
-void sort_term_code_stop(void *p)
+static void sort_term_code_stop(void *p)
 {
 }
 
 {
 }
 
-
 struct sort_term_stream {
     int no;
     int insert_flag;
     struct sort_term st;
 };
 
 struct sort_term_stream {
     int no;
     int insert_flag;
     struct sort_term st;
 };
 
-int sort_term_code_read(void *vp, char **dst, int *insertMode)
+static int sort_term_code_read(void *vp, char **dst, int *insertMode)
 {
     struct sort_term_stream *s = (struct sort_term_stream *) vp;
 
 {
     struct sort_term_stream *s = (struct sort_term_stream *) vp;
 
@@ -121,7 +120,6 @@ int sort_term_code_read(void *vp, char **dst, int *insertMode)
     return 1;
 }
 
     return 1;
 }
 
-        
 struct sortFileHead {
     zint sysno_max;
 };
 struct sortFileHead {
     zint sysno_max;
 };
@@ -190,6 +188,7 @@ void zebra_sort_close(zebra_sort_index_t si)
 int zebra_sort_type(zebra_sort_index_t si, int id)
 {
     int isam_block_size = 4096;
 int zebra_sort_type(zebra_sort_index_t si, int id)
 {
     int isam_block_size = 4096;
+
     ISAMC_M method;
     char fname[80];
     struct sortFile *sf;
     ISAMC_M method;
     char fname[80];
     struct sortFile *sf;
@@ -204,14 +203,6 @@ int zebra_sort_type(zebra_sort_index_t si, int id)
     sf = (struct sortFile *) xmalloc(sizeof(*sf));
     sf->id = id;
 
     sf = (struct sortFile *) xmalloc(sizeof(*sf));
     sf->id = id;
 
-    method.compare_item = sort_term_compare;
-    method.log_item = sort_term_log_item;
-    method.codec.start = sort_term_code_start;
-    method.codec.encode = sort_term_encode;
-    method.codec.decode = sort_term_decode;
-    method.codec.reset = sort_term_code_reset;
-    method.codec.stop = sort_term_code_stop;
-   
     switch(si->type)
     {
     case ZEBRA_SORT_TYPE_FLAT:
     switch(si->type)
     {
     case ZEBRA_SORT_TYPE_FLAT:
@@ -236,8 +227,15 @@ int zebra_sort_type(zebra_sort_index_t si, int id)
         }
         break;
     case ZEBRA_SORT_TYPE_ISAMB:
         }
         break;
     case ZEBRA_SORT_TYPE_ISAMB:
-        sprintf(fname, "sortb%d", id);
+        method.compare_item = sort_term_compare;
+        method.log_item = sort_term_log_item;
+        method.codec.start = sort_term_code_start;
+        method.codec.encode = sort_term_encode;
+        method.codec.decode = sort_term_decode;
+        method.codec.reset = sort_term_code_reset;
+        method.codec.stop = sort_term_code_stop;
         
         
+        sprintf(fname, "sortb%d", id);
         sf->u.isamb = isamb_open2(si->bfs, fname, si->write_flag, &method,
                                   /* cache */ 0,
                                   /* no_cat */ 1, &isam_block_size,
         sf->u.isamb = isamb_open2(si->bfs, fname, si->write_flag, &method,
                                   /* cache */ 0,
                                   /* no_cat */ 1, &isam_block_size,
@@ -358,12 +356,13 @@ void zebra_sort_add(zebra_sort_index_t si, const char *buf, int len)
     }
 }
 
     }
 }
 
-void zebra_sort_read(zebra_sort_index_t si, char *buf)
+int zebra_sort_read(zebra_sort_index_t si, char *buf)
 {
     int r;
     struct sortFile *sf = si->current_file;
 
     assert(sf);
 {
     int r;
     struct sortFile *sf = si->current_file;
 
     assert(sf);
+    assert(sf->u.bf);
 
     switch(si->type)
     {
 
     switch(si->type)
     {
@@ -371,44 +370,34 @@ void zebra_sort_read(zebra_sort_index_t si, char *buf)
         r = bf_read(sf->u.bf, si->sysno+1, 0, 0, buf);
         if (!r)
             memset(buf, 0, SORT_IDX_ENTRYSIZE);
         r = bf_read(sf->u.bf, si->sysno+1, 0, 0, buf);
         if (!r)
             memset(buf, 0, SORT_IDX_ENTRYSIZE);
+        if (buf[0] == 0)
+            return 0;
         break;
     case ZEBRA_SORT_TYPE_ISAMB:
         memset(buf, 0, SORT_IDX_ENTRYSIZE);
         break;
     case ZEBRA_SORT_TYPE_ISAMB:
         memset(buf, 0, SORT_IDX_ENTRYSIZE);
-        assert(sf->u.bf);
-        if (sf->u.bf)
+        if (!sf->isam_p)
+            return 0;
+        else
         {
             struct sort_term st, st_untilbuf;
 
             if (!sf->isam_pp)
                 sf->isam_pp = isamb_pp_open(sf->u.isamb, sf->isam_p, 1);
             if (!sf->isam_pp)
         {
             struct sort_term st, st_untilbuf;
 
             if (!sf->isam_pp)
                 sf->isam_pp = isamb_pp_open(sf->u.isamb, sf->isam_p, 1);
             if (!sf->isam_pp)
-                return;
+                return 0;
 
 
-#if 0
-            while (1)
-            {
-                r = isamb_pp_read(sf->isam_pp, &st);
-                if (!r)
-                    break;
-                if (st.sysno == si->sysno)
-                    break;
-                yaz_log(YLOG_LOG, "Received sysno=" ZINT_FORMAT " looking for "
-                        ZINT_FORMAT, st.sysno, si->sysno);
-            }
-#else
             st_untilbuf.sysno = si->sysno;
             st_untilbuf.term[0] = '\0';
             r = isamb_pp_forward(sf->isam_pp, &st, &st_untilbuf);
             if (!r)
             st_untilbuf.sysno = si->sysno;
             st_untilbuf.term[0] = '\0';
             r = isamb_pp_forward(sf->isam_pp, &st, &st_untilbuf);
             if (!r)
-                return;
-#endif
+                return 0;
             if (r)
             {
                 if (st.sysno != si->sysno)
                 {
                     yaz_log(YLOG_LOG, "Received sysno=" ZINT_FORMAT " looking for "
                             ZINT_FORMAT, st.sysno, si->sysno);
             if (r)
             {
                 if (st.sysno != si->sysno)
                 {
                     yaz_log(YLOG_LOG, "Received sysno=" ZINT_FORMAT " looking for "
                             ZINT_FORMAT, st.sysno, si->sysno);
-                    return;
+                    return 0;
                 }
                 if (strlen(st.term) < SORT_IDX_ENTRYSIZE)
                     strcpy(buf, st.term);
                 }
                 if (strlen(st.term) < SORT_IDX_ENTRYSIZE)
                     strcpy(buf, st.term);
@@ -418,6 +407,7 @@ void zebra_sort_read(zebra_sort_index_t si, char *buf)
         }
         break;
     }
         }
         break;
     }
+    return 1;
 }
 /*
  * Local variables:
 }
 /*
  * Local variables:
index c3d3367..0b12d85 100644 (file)
@@ -21,6 +21,7 @@ test_safari
 test_sortindex
 test_sort1
 test_sort2
 test_sortindex
 test_sort1
 test_sort2
+test_sortidx
 *.mf
 *.LCK
 *.log
 *.mf
 *.LCK
 *.log
index 4f0ce42..13b68de 100644 (file)
@@ -7,7 +7,7 @@ check_PROGRAMS = test_start_stop test_result_sets \
        test_rank test_private_attset \
        test_scan test_create_databases test_resources test_update_record \
        test_zebra_fork test_special_elements test_icu_indexing \
        test_rank test_private_attset \
        test_scan test_create_databases test_resources test_update_record \
        test_zebra_fork test_special_elements test_icu_indexing \
-       test_sortindex test_safari test_sort1 test_sort2
+       test_sortindex test_safari test_sort1 test_sort2 test_sortidx
 
 TESTS = $(check_PROGRAMS)
 
 
 TESTS = $(check_PROGRAMS)
 
@@ -42,6 +42,7 @@ test_sortindex_SOURCES = test_sortindex.c
 test_safari_SOURCES = test_safari.c 
 test_sort1_SOURCES = test_sort1.c
 test_sort2_SOURCES = test_sort2.c
 test_safari_SOURCES = test_safari.c 
 test_sort1_SOURCES = test_sort1.c
 test_sort2_SOURCES = test_sort2.c
+test_sortidx_SOURCES = test_sortidx.c
 
 AM_CPPFLAGS = -I$(top_srcdir)/include $(YAZINC)
 
 
 AM_CPPFLAGS = -I$(top_srcdir)/include $(YAZINC)
 
index cbe4ca3..36a19cc 100644 (file)
@@ -34,7 +34,7 @@ const char *myrec[] = {
     ,
     "<sort1>\n"
     "  <title>3rd computer</title>\n"
     ,
     "<sort1>\n"
     "  <title>3rd computer</title>\n"
-"  <dateTime>a^3</dateTime>\n"
+    "  <dateTime>a^3</dateTime>\n"
     "  <cost>15</cost>\n"
     "</sort1>\n"
     ,
     "  <cost>15</cost>\n"
     "</sort1>\n"
     ,
diff --git a/test/api/test_sortidx.c b/test/api/test_sortidx.c
new file mode 100644 (file)
index 0000000..5640ce6
--- /dev/null
@@ -0,0 +1,93 @@
+/* This file is part of the Zebra server.
+   Copyright (C) 1995-2008 Index Data
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+*/
+
+#include <idzebra/bfile.h>
+#include <sortidx.h>
+#include "testlib.h"
+
+static void tst1(zebra_sort_index_t si)
+{
+    zint sysno = 12; /* just some sysno */
+    int my_type = 2; /* just some type ID */
+    char read_buf[SORT_IDX_ENTRYSIZE];
+
+    zebra_sort_type(si, my_type);
+
+    zebra_sort_sysno(si, sysno);
+    YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 0);
+
+    zebra_sort_add(si, "abcde1", 6);
+
+    zebra_sort_sysno(si, sysno);
+    YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 1);
+    YAZ_CHECK(!strcmp(read_buf, "abcde1"));
+
+    zebra_sort_sysno(si, sysno+1);
+    YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 0);
+
+    zebra_sort_sysno(si, sysno-1);
+    YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 0);
+
+    zebra_sort_sysno(si, sysno);
+    zebra_sort_delete(si);
+    YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 0);
+}
+
+static void tst(int argc, char **argv)
+{
+    BFiles bfs = bfs_create(".:50M", 0);
+    zebra_sort_index_t si;
+
+    YAZ_CHECK(bfs);
+    if (bfs)
+    {
+        bf_reset(bfs);
+        si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_ISAMB);
+        YAZ_CHECK(si);
+        if (si)
+        {
+            tst1(si);
+            zebra_sort_close(si);
+        }
+    }
+
+    if (bfs)
+    {
+        bf_reset(bfs);
+        si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_FLAT);
+        YAZ_CHECK(si);
+        if (si)
+        {
+            tst1(si);
+            zebra_sort_close(si);
+        }
+    }
+    if (bfs)
+        bfs_destroy(bfs);
+}
+
+TL_MAIN
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+