Moved truncation code to trunc.c.
[idzebra-moved-to-github.git] / index / zrpn.c
index a5da244..8d5da19 100644 (file)
@@ -4,7 +4,13 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: zrpn.c,v $
- * Revision 1.53  1996-06-26 09:21:43  adam
+ * Revision 1.55  1996-11-04 14:07:44  adam
+ * Moved truncation code to trunc.c.
+ *
+ * Revision 1.54  1996/10/29 14:09:52  adam
+ * Use of cisam system - enabled if setting isamc is 1.
+ *
+ * Revision 1.53  1996/06/26 09:21:43  adam
  * Bug fix: local attribute set wasn't obeyed in scan.
  *
  * Revision 1.52  1996/06/17  14:26:20  adam
 #include "attribute.h"
 
 #include <charmap.h>
-#include <rsisam.h>
 #include <rstemp.h>
 #include <rsnull.h>
 #include <rsbool.h>
@@ -258,248 +263,6 @@ static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
     src->minor = 0;
 }
 
-struct trunc_info {
-    int  *ptr;
-    int  *indx;
-    char **heap;
-    int  heapnum;
-    int  (*cmp)(const void *p1, const void *p2);
-    int  keysize;
-    char *swapbuf;
-    char *tmpbuf;
-    char *buf;
-};
-
-static void heap_swap (struct trunc_info *ti, int i1, int i2)
-{
-    int swap;
-
-    swap = ti->ptr[i1];
-    ti->ptr[i1] = ti->ptr[i2];
-    ti->ptr[i2] = swap;
-}
-
-static void heap_delete (struct trunc_info *ti)
-{
-    int cur = 1, child = 2;
-
-    heap_swap (ti, 1, ti->heapnum--);
-    while (child <= ti->heapnum) {
-        if (child < ti->heapnum &&
-            (*ti->cmp)(ti->heap[ti->ptr[child]],
-                       ti->heap[ti->ptr[1+child]]) > 0)
-            child++;
-        if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
-                       ti->heap[ti->ptr[child]]) > 0)
-        {
-            heap_swap (ti, cur, child);
-            cur = child;
-            child = 2*cur;
-        }
-        else
-            break;
-    }
-}
-
-static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
-{
-    int cur, parent;
-
-    cur = ++(ti->heapnum);
-    memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
-    ti->indx[ti->ptr[cur]] = indx;
-    parent = cur/2;
-    while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
-                                ti->heap[ti->ptr[cur]]) > 0)
-    {
-        heap_swap (ti, cur, parent);
-        cur = parent;
-        parent = cur/2;
-    }
-}
-
-static
-struct trunc_info *heap_init (int size, int key_size,
-                              int (*cmp)(const void *p1, const void *p2))
-{
-    struct trunc_info *ti = xmalloc (sizeof(*ti));
-    int i;
-
-    ++size;
-    ti->heapnum = 0;
-    ti->keysize = key_size;
-    ti->cmp = cmp;
-    ti->indx = xmalloc (size * sizeof(*ti->indx));
-    ti->heap = xmalloc (size * sizeof(*ti->heap));
-    ti->ptr = xmalloc (size * sizeof(*ti->ptr));
-    ti->swapbuf = xmalloc (ti->keysize);
-    ti->tmpbuf = xmalloc (ti->keysize);
-    ti->buf = xmalloc (size * ti->keysize);
-    for (i = size; --i >= 0; )
-    {
-        ti->ptr[i] = i;
-        ti->heap[i] = ti->buf + ti->keysize * i;
-    }
-    return ti;
-}
-
-static void heap_close (struct trunc_info *ti)
-{
-    xfree (ti->ptr);
-    xfree (ti->indx);
-    xfree (ti->heap);
-    xfree (ti->swapbuf);
-    xfree (ti->tmpbuf);
-    xfree (ti);
-}
-
-static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
-                         int merge_chunk)
-{
-    RSET result; 
-    RSFD result_rsfd;
-    rset_temp_parms parms;
-
-    parms.key_size = sizeof(struct it_key);
-    result = rset_create (rset_kind_temp, &parms);
-    result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
-
-    if (to - from > merge_chunk)
-    {
-        RSFD *rsfd;
-        RSET *rset;
-        int i, i_add = (to-from)/merge_chunk + 1;
-        struct trunc_info *ti;
-        int rscur = 0;
-        int rsmax = (to-from)/i_add + 1;
-        
-        rset = xmalloc (sizeof(*rset) * rsmax);
-        rsfd = xmalloc (sizeof(*rsfd) * rsmax);
-        
-        for (i = from; i < to; i += i_add)
-        {
-            if (i_add <= to - i)
-                rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
-                                            merge_chunk);
-            else
-                rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
-                                            merge_chunk);
-            rscur++;
-        }
-        ti = heap_init (rscur, sizeof(struct it_key), key_compare);
-        for (i = rscur; --i >= 0; )
-        {
-            rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
-            if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
-                heap_insert (ti, ti->tmpbuf, i);
-            else
-            {
-                rset_close (rset[i], rsfd[i]);
-                rset_delete (rset[i]);
-            }
-        }
-        while (ti->heapnum)
-        {
-            int n = ti->indx[ti->ptr[1]];
-
-            rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
-
-            while (1)
-            {
-                if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
-                {
-                    heap_delete (ti);
-                    rset_close (rset[n], rsfd[n]);
-                    rset_delete (rset[n]);
-                    break;
-                }
-                if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
-                {
-                    heap_delete (ti);
-                    heap_insert (ti, ti->tmpbuf, n);
-                    break;
-                }
-            }
-        }
-        xfree (rset);
-        xfree (rsfd);
-        heap_close (ti);
-    }
-    else
-    {
-        ISPT *ispt;
-        int i;
-        struct trunc_info *ti;
-
-        ispt = xmalloc (sizeof(*ispt) * (to-from));
-
-        ti = heap_init (to-from, sizeof(struct it_key),
-                        key_compare);
-        for (i = to-from; --i >= 0; )
-        {
-            ispt[i] = is_position (isam, isam_p[from+i]);
-            if (is_readkey (ispt[i], ti->tmpbuf))
-                heap_insert (ti, ti->tmpbuf, i);
-            else
-                is_pt_free (ispt[i]);
-        }
-        while (ti->heapnum)
-        {
-            int n = ti->indx[ti->ptr[1]];
-
-            rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
-#if 0
-/* section that preserve all keys */
-            heap_delete (ti);
-            if (is_readkey (ispt[n], ti->tmpbuf))
-                heap_insert (ti, ti->tmpbuf, n);
-            else
-                is_pt_free (ispt[n]);
-#else
-/* section that preserve all keys with unique sysnos */
-            while (1)
-            {
-                if (!is_readkey (ispt[n], ti->tmpbuf))
-                {
-                    heap_delete (ti);
-                    is_pt_free (ispt[n]);
-                    break;
-                }
-                if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
-                {
-                    heap_delete (ti);
-                    heap_insert (ti, ti->tmpbuf, n);
-                    break;
-                }
-            }
-#endif
-        }
-        heap_close (ti);
-        xfree (ispt);
-    }
-    rset_close (result, result_rsfd);
-    return result;
-}
-
-static int isam_trunc_cmp (const void *p1, const void *p2)
-{
-    ISAM_P i1 = *(ISAM_P*) p1;
-    ISAM_P i2 = *(ISAM_P*) p2;
-    int d;
-
-    d = is_type (i1) - is_type (i2);
-    if (d)
-        return d;
-    return is_block (i1) - is_block (i2);
-}
-
-static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
-{
-
-    qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
-    return rset_trunc_r (isam, isam_p, 0, no, 100);
-}
-
 #define TERM_COUNT        
        
 struct grep_info {        
@@ -709,7 +472,7 @@ static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
         return 0;
     }
     logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
-    r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
+    r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
                           0, grep_handle);
     if (r)
         logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
@@ -806,7 +569,7 @@ static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
                 for (i = 0; term_sub[i]; i++)
                     verbatim_char (term_sub[i], &j, term_dict);
                 strcpy (term_dict+j, ")");
-                r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
+                r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
                                       &max_pos, 0, grep_handle);
                 if (r)
                     logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
@@ -816,7 +579,7 @@ static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
                 for (i = 0; term_sub[i]; i++)
                     verbatim_char (term_sub[i], &j, term_dict);
                 strcpy (term_dict+j, ".*)");
-                dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
+                dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
                                   &max_pos, 0, grep_handle);
                 break;
             case 2:          /* left truncation */
@@ -834,7 +597,7 @@ static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
                     else
                         verbatim_char (term_sub[i], &j, term_dict);
                 strcpy (term_dict+j, ")");
-                r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
+                r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
                                       &max_pos, 0, grep_handle);
                 if (r)
                     logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
@@ -842,7 +605,7 @@ static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
                 break;
             case 102:        /* regular expression */
                sprintf (term_dict + j, "(%s)", term_sub);
-                r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
+                r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
                                       &max_pos, 0, grep_handle);
                 if (r)
                     logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
@@ -857,7 +620,7 @@ static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
                     cp += 2;
                 }
                sprintf (term_dict + j, "(%s)", cp);
-                r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
+                r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
                                       &max_pos, j, grep_handle);
                 if (r)
                     logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
@@ -888,7 +651,7 @@ static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
 {
     Z_Term *term = zapt->term;
     char **map;
-    const char *cp = (const char *) term->u.general->buf;
+    char *cp = (char*) term->u.general->buf;
     const char *cp_end = cp + term->u.general->len;
     const char *src;
     int i = 0;
@@ -928,7 +691,7 @@ static RSET rpn_search_APT_relevance (ZServerInfo *zi,
     parms.key_size = sizeof(struct it_key);
     parms.max_rec = 100;
     parms.cmp = key_compare;
-    parms.is = zi->wordIsam;
+    parms.is = zi->isam;
     parms.no_terms = 0;
 
     if (zapt->term->which != Z_Term_general)
@@ -999,7 +762,6 @@ static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
                                     oid_value attributeSet,
                                     int num_bases, char **basenames)
 {
-    rset_isam_parms parms;
     char termz[IT_MAX_WORD+1];
     struct grep_info grep_info;
     RSET result;
@@ -1021,17 +783,7 @@ static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
     if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
                     num_bases, basenames))
         return NULL;
-    if (grep_info.isam_p_indx < 1)
-        result = rset_create (rset_kind_null, NULL);
-    else if (grep_info.isam_p_indx == 1)
-    {
-        parms.is = zi->wordIsam;
-        parms.pos = *grep_info.isam_p_buf;
-        result = rset_create (rset_kind_isam, &parms);
-    }
-    else
-        result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
-                             grep_info.isam_p_indx);
+    result = rset_trunc (zi, grep_info.isam_p_buf, grep_info.isam_p_indx);
 #ifdef TERM_COUNT
     xfree(grep_info.term_no);
 #endif
@@ -1183,20 +935,8 @@ static RSET rpn_search_APT_phrase (ZServerInfo *zi,
         if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
                         num_bases, basenames))
             return NULL;
-        if (grep_info.isam_p_indx == 0)
-            rset[rset_no] = rset_create (rset_kind_null, NULL);
-        else if (grep_info.isam_p_indx > 1)
-            rset[rset_no] = rset_trunc (zi->wordIsam,
-                                        grep_info.isam_p_buf,
-                                        grep_info.isam_p_indx);
-        else
-        {
-            rset_isam_parms parms;
-            
-            parms.is = zi->wordIsam;
-            parms.pos = *grep_info.isam_p_buf;
-            rset[rset_no] = rset_create (rset_kind_isam, &parms);
-        }
+        rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
+                                    grep_info.isam_p_indx);
         assert (rset[rset_no]);
         if (++rset_no >= sizeof(rset)/sizeof(*rset))
             break;
@@ -1423,6 +1163,7 @@ void count_set_save (RSET *r, int *count)
     rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
     while (rset_read (*r, rfd, &key))
     {
+        logf (LOG_DEBUG, "sysno=%-7d seqno=%d", key.sysno, key.seqno);
         if (key.sysno != psysno)
         {
             rset_write (w, wfd, &key);
@@ -1469,7 +1210,7 @@ int rpn_search (ZServerInfo *zi,
     oident *attrset;
     oid_value attributeSet;
 
-    dict_grep_cmap (zi->wordDict, map_chrs_input);
+    dict_grep_cmap (zi->dict, map_chrs_input);
     zlog_rpn (rpn);
 
     zi->errCode = 0;
@@ -1631,7 +1372,7 @@ int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
 
         trans_scan_term (zi, zapt, termz+prefix_len);
                     
-        dict_scan (zi->wordDict, termz, &before_tmp, &after_tmp, scan_info,
+        dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
                    scan_handle);
     }
     glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
@@ -1645,7 +1386,6 @@ int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
         const char *mterm = NULL;
         const char *tst;
         RSET rset;
-        rset_isam_parms parms;
         
         for (j = 0; j < ord_no; j++)
         {
@@ -1660,9 +1400,7 @@ int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
         if (j0 == -1)
             break;
         scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
-        parms.is = zi->wordIsam;
-        parms.pos = scan_info_array[j0].list[ptr[j0]].isam_p;
-        rset = rset_create (rset_kind_isam, &parms);
+        rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
 
         ptr[j0]++;
         for (j = j0+1; j<ord_no; j++)
@@ -1671,13 +1409,11 @@ int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
                 (tst=scan_info_array[j].list[ptr[j]].term) &&
                 !strcmp (tst, mterm))
             {
-                rset_isam_parms parms;
                 rset_bool_parms bool_parms;
                 RSET rset2;
 
-                parms.is = zi->wordIsam;
-                parms.pos = scan_info_array[j].list[ptr[j]].isam_p;
-                rset2 = rset_create (rset_kind_isam, &parms);
+                rset2 =
+                   rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
 
                 bool_parms.key_size = sizeof(struct it_key);
                 bool_parms.cmp = key_compare;
@@ -1707,7 +1443,6 @@ int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
         const char *mterm = NULL;
         const char *tst;
         RSET rset;
-        rset_isam_parms parms;
         
         for (j = 0; j <ord_no; j++)
         {
@@ -1724,9 +1459,8 @@ int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
 
         scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
 
-        parms.is = zi->wordIsam;
-        parms.pos = scan_info_array[j0].list[before-1-ptr[j0]].isam_p;
-        rset = rset_create (rset_kind_isam, &parms);
+        rset = rset_trunc
+               (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
 
         ptr[j0]++;
 
@@ -1736,13 +1470,11 @@ int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
                 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
                 !strcmp (tst, mterm))
             {
-                rset_isam_parms parms;
                 rset_bool_parms bool_parms;
                 RSET rset2;
 
-                parms.is = zi->wordIsam;
-                parms.pos = scan_info_array[j].list[before-1-ptr[j]].isam_p;
-                rset2 = rset_create (rset_kind_isam, &parms);
+                rset2 = rset_trunc (zi,
+                         &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
 
                 bool_parms.key_size = sizeof(struct it_key);
                 bool_parms.cmp = key_compare;