Not creating a temporary file when indexing, if there would only
[idzebra-moved-to-github.git] / index / zsets.c
index ed43fab..73e45b0 100644 (file)
@@ -1,10 +1,26 @@
-/*
- * Copyright (C) 1994-2002, Index Data
- * All rights reserved.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Id: zsets.c,v 1.36 2002-04-18 20:22:09 adam Exp $
- */
+/* $Id: zsets.c,v 1.46 2003-06-18 11:46:34 adam Exp $
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
+   Index Data Aps
+
+This file is part of the Zebra server.
+
+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 Zebra; see the file LICENSE.zebra.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+*/
+
+
 #include <stdio.h>
 #include <assert.h>
 #ifdef WIN32
@@ -54,7 +70,7 @@ struct zset_sort_info {
     struct zset_sort_entry **entries;
 };
 
-ZebraSet resultSetAddRPN (ZebraHandle zh, ODR input, ODR output,
+ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m,
                          Z_RPNQuery *rpn, int num_bases,
                           char **basenames, 
                          const char *setname)
@@ -71,7 +87,7 @@ ZebraSet resultSetAddRPN (ZebraHandle zh, ODR input, ODR output,
        return 0;
     zebraSet->locked = 1;
     zebraSet->rpn = 0;
-    zebraSet->nmem = nmem_create ();
+    zebraSet->nmem = m;
 
     zebraSet->num_bases = num_bases;
     zebraSet->basenames = 
@@ -80,7 +96,7 @@ ZebraSet resultSetAddRPN (ZebraHandle zh, ODR input, ODR output,
         zebraSet->basenames[i] = nmem_strdup (zebraSet->nmem, basenames[i]);
 
 
-    zebraSet->rset = rpn_search (zh, output->mem, rpn,
+    zebraSet->rset = rpn_search (zh, zebraSet->nmem, rpn,
                                  zebraSet->num_bases,
                                 zebraSet->basenames, zebraSet->name,
                                 zebraSet);
@@ -119,20 +135,55 @@ void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
 }
 
 
-const char *zebra_resultSetTerms (ZebraHandle zh, const char *setname, 
-                                  int no, int *count, int *no_max)
+int zebra_resultSetTerms (ZebraHandle zh, const char *setname, 
+                          int no, int *count, 
+                          int *type, char *out, size_t *len)
 {
     ZebraSet s = resultSetGet (zh, setname);
+    int no_max = 0;
 
-    *count = 0;
-    *no_max = 0;
+    if (count)
+        *count = 0;
     if (!s || !s->rset)
         return 0;
-    *no_max = s->rset->no_rset_terms;
-    if (no < 0 || no >= *no_max)
+    no_max = s->rset->no_rset_terms;
+    if (no < 0 || no >= no_max)
         return 0;
-    *count = s->rset->rset_terms[no]->count;
-    return s->rset->rset_terms[no]->name;
+    if (count)
+        *count = s->rset->rset_terms[no]->count;
+    if (type)
+        *type = s->rset->rset_terms[no]->type;
+    
+    if (out)
+    {
+        char *inbuf = s->rset->rset_terms[no]->name;
+        size_t inleft = strlen(inbuf);
+        size_t outleft = *len - 1;
+       int converted = 0;
+
+        if (zh->iconv_from_utf8 != 0)
+        {
+            char *outbuf = out;
+            size_t ret;
+            
+            ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
+                        &outbuf, &outleft);
+            if (ret == (size_t)(-1))
+                *len = 0;
+            else
+                *len = outbuf - out;
+           converted = 1;
+        }
+        if (!converted)
+        {
+            if (inleft > outleft)
+                inleft = outleft;
+            *len = inleft;
+            memcpy (out, inbuf, *len);
+        }
+        out[*len] = 0;
+    }
+    return no_max;
 }
 
 
@@ -146,7 +197,7 @@ ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
            break;
     if (s)
     {
-       logf (LOG_DEBUG, "updating result set %s", name);
+       yaz_log (LOG_DEBUG, "updating result set %s", name);
        if (!ov || s->locked)
            return NULL;
        if (s->rset)
@@ -156,7 +207,7 @@ ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
     }
     else
     {
-       logf (LOG_DEBUG, "adding result set %s", name);
+       yaz_log (LOG_DEBUG, "adding result set %s", name);
        s = (ZebraSet) xmalloc (sizeof(*s));
        s->next = zh->sets;
        zh->sets = s;
@@ -312,7 +363,7 @@ ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
                position = positions[i];
                if (position > 0 && position <= sort_info->num_entries)
                {
-                   logf (LOG_DEBUG, "got pos=%d (sorted)", position);
+                   yaz_log (LOG_DEBUG, "got pos=%d (sorted)", position);
                    sr[i].sysno = sort_info->entries[position-1]->sysno;
                    sr[i].score = sort_info->entries[position-1]->score;
                }
@@ -357,7 +408,7 @@ ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
                    if (position == positions[num_i])
                    {
                        sr[num_i].sysno = psysno;
-                       logf (LOG_DEBUG, "got pos=%d (unsorted)", position);
+                       yaz_log (LOG_DEBUG, "got pos=%d (unsorted)", position);
                        sr[num_i].score = -1;
                        num_i++;
                    }
@@ -519,7 +570,7 @@ void resultSetSort (ZebraHandle zh, NMEM nmem,
        zh->errCode = 230;
        return;
     }
-    logf (LOG_DEBUG, "result set sort input=%s output=%s",
+    yaz_log (LOG_DEBUG, "result set sort input=%s output=%s",
          *input_setnames, output_setname);
     sset = resultSetGet (zh, input_setnames[0]);
     if (!sset)
@@ -554,7 +605,7 @@ void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
     int term_index;
     RSFD rfd;
 
-    logf (LOG_LOG, "resultSetSortSingle start");
+    yaz_log (LOG_LOG, "resultSetSortSingle start");
     sset->sort_info->num_entries = 0;
 
     sset->hits = 0;
@@ -589,20 +640,20 @@ void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
        switch (sk->which)
        {
        case Z_SortKey_sortField:
-           logf (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
+           yaz_log (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
            zh->errCode = 207;
            return;
        case Z_SortKey_elementSpec:
-           logf (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
+           yaz_log (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
            zh->errCode = 207;
            return;
        case Z_SortKey_sortAttributes:
-           logf (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
+           yaz_log (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
            sort_criteria[i].attrUse =
                zebra_maps_sort (zh->reg->zebra_maps,
                                 sk->u.sortAttributes,
                                  &sort_criteria[i].numerical);
-           logf (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
+           yaz_log (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
            if (sort_criteria[i].attrUse == -1)
            {
                zh->errCode = 116;
@@ -637,10 +688,10 @@ void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
                  rset->rset_terms[i]->count);
 
     *sort_status = Z_SortStatus_success;
-    logf (LOG_LOG, "resultSetSortSingle end");
+    yaz_log (LOG_LOG, "resultSetSortSingle end");
 }
 
-RSET resultSetRef (ZebraHandle zh, Z_ResultSetId *resultSetId)
+RSET resultSetRef (ZebraHandle zh, const char *resultSetId)
 {
     ZebraSet s;
 
@@ -666,7 +717,7 @@ void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset)
 
     yaz_log (LOG_LOG, "resultSetRank");
 
-    rank_class = zebraRankLookup (zh, "rank-1");
+    rank_class = zebraRankLookup (zh, res_get_def(zh->res, "rank", "rank-1"));
     rc = rank_class->control;
 
     if (rset_read (rset, rfd, &key, &term_index))
@@ -714,7 +765,7 @@ ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
     if (p && !p->init_flag)
     {
        if (p->control->create)
-           p->class_handle = (*p->control->create)(zh->reg);
+           p->class_handle = (*p->control->create)(zh);
        p->init_flag = 1;
     }
     return p;