Added other sort index test.
[idzebra-moved-to-github.git] / index / sortidx.c
index fb87df6..6f96c37 100644 (file)
@@ -1,8 +1,5 @@
-/* $Id: sortidx.c,v 1.22 2006-12-19 16:57:38 adam Exp $
-   Copyright (C) 1995-2006
-   Index Data ApS
-
-This file is part of the Zebra server.
+/* 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
@@ -50,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);
 }
 
-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;
 
@@ -64,12 +61,12 @@ int sort_term_compare(const void *a, const void *b)
     return 0;
 }
 
-void *sort_term_code_start(void)
+static void *sort_term_code_start(void)
 {
     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;
 
@@ -81,7 +78,7 @@ void sort_term_encode(void *p, char **dst, const char **src)
     *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;
 
@@ -94,22 +91,21 @@ void sort_term_decode(void *p, char **dst, const char **src)
     *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;
 };
 
-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;
 
@@ -124,7 +120,6 @@ int sort_term_code_read(void *vp, char **dst, int *insertMode)
     return 1;
 }
 
-        
 struct sortFileHead {
     zint sysno_max;
 };
@@ -193,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;
+
     ISAMC_M method;
     char fname[80];
     struct sortFile *sf;
@@ -207,14 +203,6 @@ int zebra_sort_type(zebra_sort_index_t si, int 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:
@@ -239,8 +227,15 @@ int zebra_sort_type(zebra_sort_index_t si, int id)
         }
         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,
@@ -253,10 +248,10 @@ int zebra_sort_type(zebra_sort_index_t si, int id)
         else
         {
             sf->isam_p = isamb_get_root_ptr(sf->u.isamb);
-            sf->isam_pp = 0;
         }
         break;
     }
+    sf->isam_pp = 0;
     sf->no_inserted = 0;
     sf->no_deleted = 0;
     sf->next = si->files;
@@ -273,7 +268,7 @@ void zebra_sort_sysno(zebra_sort_index_t si, zint sysno)
     {
         sf->no_inserted = 0;
         sf->no_deleted = 0;
-        if (new_sysno < si->sysno && sf->isam_pp)
+        if (sf->isam_pp && new_sysno < si->sysno && sf->isam_pp)
         {
             isamb_pp_close(sf->isam_pp);
             sf->isam_pp = 0;
@@ -361,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);
+    assert(sf->u.bf);
 
     switch(si->type)
     {
@@ -374,47 +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);
+        if (buf[0] == 0)
+            return 0;
         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;
 
-            st.sysno = 99999;
             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)
-                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);
-                    return;
+                    return 0;
                 }
                 if (strlen(st.term) < SORT_IDX_ENTRYSIZE)
                     strcpy(buf, st.term);
@@ -424,6 +407,7 @@ void zebra_sort_read(zebra_sort_index_t si, char *buf)
         }
         break;
     }
+    return 1;
 }
 /*
  * Local variables: