Replace key_compare_it with key_compare
[idzebra-moved-to-github.git] / index / trunc.c
index 683460c..eb81fbb 100644 (file)
@@ -1,8 +1,5 @@
-/* $Id: trunc.c,v 1.53 2005-04-20 08:32:36 adam Exp $
-   Copyright (C) 1995-2005
-   Index Data ApS
-
-This file is part of the Zebra server.
+/* This file is part of the Zebra server.
+   Copyright (C) 1994-2011 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
@@ -15,12 +12,15 @@ 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 Zebra; see the file LICENSE.zebra.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 */
 
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <stdio.h>
 #include <assert.h>
 
@@ -127,15 +127,15 @@ static RSET rset_trunc_r(ZebraHandle zi, const char *term, int length,
                          const char *flags, ISAM_P *isam_p, int from, int to,
                         int merge_chunk, int preserve_position,
                         int term_type, NMEM rset_nmem,
-                        const struct key_control *kctrl, int scope,
+                        struct rset_key_control *kctrl, int scope,
                         TERMID termid)
 {
-    RSET result; 
+    RSET result;
     RSFD result_rsfd;
     int nn = 0;
 
-    result = rstemp_create( rset_nmem,kctrl, scope,
-                           res_get(zi->res, "setTmpDir"), termid);
+    result = rset_create_temp(rset_nmem, kctrl, scope,
+                              res_get(zi->res, "setTmpDir"), termid);
     result_rsfd = rset_open(result, RSETF_WRITE);
 
     if (to - from > merge_chunk)
@@ -146,31 +146,34 @@ static RSET rset_trunc_r(ZebraHandle zi, const char *term, int length,
         struct trunc_info *ti;
         int rscur = 0;
         int rsmax = (to-from)/i_add + 1;
-        
+       int cmp_border = preserve_position ? 0 : 1;
+       NMEM rset_nmem_sub = nmem_create(); /* all sub rsets not needed
+                                              after this */
+
         rset = (RSET *) xmalloc(sizeof(*rset) * rsmax);
         rsfd = (RSFD *) xmalloc(sizeof(*rsfd) * rsmax);
-        
+
         for (i = from; i < to; i += i_add)
         {
             if (i_add <= to - i)
                 rset[rscur] = rset_trunc_r(zi, term, length, flags,
                                           isam_p, i, i+i_add,
                                           merge_chunk, preserve_position,
-                                          term_type, rset_nmem, 
-                                          kctrl, scope,termid);
+                                          term_type, rset_nmem_sub,
+                                          kctrl, scope, 0);
             else
                 rset[rscur] = rset_trunc_r(zi, term, length, flags,
                                           isam_p, i, to,
                                           merge_chunk, preserve_position,
-                                          term_type, rset_nmem, 
-                                          kctrl, scope,termid);
+                                          term_type, rset_nmem_sub,
+                                          kctrl, scope, 0);
             rscur++;
         }
-        ti = heap_init (rscur, sizeof(struct it_key), key_compare_it);
+        ti = heap_init (rscur, sizeof(struct it_key), key_compare);
         for (i = rscur; --i >= 0; )
         {
             rsfd[i] = rset_open(rset[i], RSETF_READ);
-            if (rset_read(rsfd[i], ti->tmpbuf,0))
+            if (rset_read(rsfd[i], ti->tmpbuf, 0))
                 heap_insert(ti, ti->tmpbuf, i);
             else
             {
@@ -194,7 +197,7 @@ static RSET rset_trunc_r(ZebraHandle zi, const char *term, int length,
                     rset_delete(rset[n]);
                     break;
                 }
-                if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
+                if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > cmp_border)
                 {
                     heap_delete(ti);
                     heap_insert(ti, ti->tmpbuf, n);
@@ -205,6 +208,7 @@ static RSET rset_trunc_r(ZebraHandle zi, const char *term, int length,
         xfree(rset);
         xfree(rsfd);
         heap_close(ti);
+       nmem_destroy(rset_nmem_sub);
     }
     else if (zi->reg->isamc)
     {
@@ -215,7 +219,7 @@ static RSET rset_trunc_r(ZebraHandle zi, const char *term, int length,
         ispt = (ISAMC_PP *) xmalloc(sizeof(*ispt) * (to-from));
 
         ti = heap_init(to-from, sizeof(struct it_key),
-                      key_compare_it);
+                      key_compare);
         for (i = to-from; --i >= 0; )
         {
             ispt[i] = isamc_pp_open(zi->reg->isamc, isam_p[from+i]);
@@ -270,7 +274,7 @@ static RSET rset_trunc_r(ZebraHandle zi, const char *term, int length,
         ispt = (ISAMS_PP *) xmalloc(sizeof(*ispt) * (to-from));
 
         ti = heap_init(to-from, sizeof(struct it_key),
-                      key_compare_it);
+                      key_compare);
         for (i = to-from; --i >= 0; )
         {
             ispt[i] = isams_pp_open(zi->reg->isams, isam_p[from+i]);
@@ -313,7 +317,7 @@ static RSET rset_trunc_r(ZebraHandle zi, const char *term, int length,
         ispt = (ISAMB_PP *) xmalloc(sizeof(*ispt) * (to-from));
 
         ti = heap_init(to-from, sizeof(struct it_key),
-                      key_compare_it);
+                      key_compare);
         for (i = to-from; --i >= 0; )
         {
            if (isam_p[from+i]) {
@@ -396,60 +400,57 @@ static int isamc_trunc_cmp(const void *p1, const void *p2)
     return 0;
 }
 
-RSET rset_trunc(ZebraHandle zi, ISAM_P *isam_p, int no,
+RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no,
                const char *term, int length, const char *flags,
                int preserve_position, int term_type, NMEM rset_nmem,
-               const struct key_control *kctrl, int scope)
+               struct rset_key_control *kctrl, int scope,
+               struct ord_list *ol, const char *index_type,
+               zint hits_limit, const char *term_ref_id)
 {
     TERMID termid;
+    RSET result;
     int trunc_chunk;
-    yaz_log(YLOG_DEBUG, "rset_trunc no=%d", no);
+    int trunc_limit = atoi(res_get_def(zh->res, "trunclimit", "10000"));
+
+    termid = rset_term_create(term, length, flags, term_type, rset_nmem, ol,
+                             *index_type, hits_limit, term_ref_id);
+
     if (no < 1)
-       return rsnull_create(rset_nmem,kctrl);
-    termid = rset_term_create(term, length, flags, term_type,rset_nmem);
-    if (zi->reg->isams)
-    {
-        if (no == 1)
-            return rsisams_create(rset_nmem, kctrl, scope,
-                                 zi->reg->isams, *isam_p, termid);
-        qsort(isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
-    }
-    else if (zi->reg->isamc)
+       return rset_create_null(rset_nmem, kctrl, termid);
+    else if (no == 1)
+        return zebra_create_rset_isam(zh, rset_nmem, kctrl,
+                                      scope, *isam_p, termid);
+    else if (zh->reg->isamb && no > 1 && no < trunc_limit)
     {
-        if (no == 1)
-            return rsisamc_create(rset_nmem, kctrl, scope,
-                                 zi->reg->isamc, *isam_p, termid);
-        qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
+        RSET r;
+        RSET *rsets = xmalloc(no*sizeof(RSET)); /* use nmem! */
+        int i;
+        for (i = 0; i<no; i++)
+            rsets[i] = rsisamb_create(rset_nmem, kctrl, scope,
+                                      zh->reg->isamb, isam_p[i],
+                                      0 /* termid */);
+        r = rset_create_or(rset_nmem, kctrl, scope,
+                           termid, no, rsets);
+        xfree(rsets);
+        return r;
     }
-    else if (zi->reg->isamb)
-    {
-       int trunc_limit = atoi(res_get_def(zi->res, "trunclimit", "10000"));
-        if (no == 1)
-            return rsisamb_create(rset_nmem,kctrl, scope,
-                                 zi->reg->isamb, *isam_p, termid);
-        else if (no < trunc_limit) 
-        {
-            RSET r;
-            RSET *rsets = xmalloc(no*sizeof(RSET)); /* use nmem! */
-            int i;
-            for (i = 0; i<no; i++)
-                rsets[i] = rsisamb_create(rset_nmem, kctrl, scope,
-                                         zi->reg->isamb, isam_p[i], termid);
-            r = rsmulti_or_create( rset_nmem, kctrl, scope, no, rsets);
-            xfree(rsets);
-            return r;
-        } 
-       fprintf(stderr, "Using rset_trunc_r limit=%d\n", trunc_limit);
+    if (zh->reg->isamc)
         qsort(isam_p, no, sizeof(*isam_p), isamc_trunc_cmp);
-    }
     else
-    {
-        yaz_log(YLOG_WARN, "Unknown isam set in rset_trunc");
-       return rsnull_create(rset_nmem, kctrl);
-    }
-    trunc_chunk = atoi(res_get_def(zi->res, "truncchunk", "100"));
-    return rset_trunc_r(zi, term, length, flags, isam_p, 0, no, trunc_chunk,
-                       preserve_position, term_type, rset_nmem, kctrl, scope,
-                       termid);
+        qsort(isam_p, no, sizeof(*isam_p), isams_trunc_cmp);
+    trunc_chunk = atoi(res_get_def(zh->res, "truncchunk", "20"));
+    result = rset_trunc_r(zh, term, length, flags, isam_p, 0, no, trunc_chunk,
+                         preserve_position, term_type, rset_nmem, kctrl,
+                         scope, termid);
+    return result;
 }
 
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+