Truncation=104 (CCL) support
[idzebra-moved-to-github.git] / index / zrpn.c
index 80d66c7..0a9716f 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: zrpn.c,v 1.125 2002-10-03 10:16:23 adam Exp $
+/* $Id: zrpn.c,v 1.128 2003-02-26 21:46:37 adam Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
    Index Data Aps
 
@@ -31,6 +31,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <ctype.h>
 
 #include "index.h"
+#include <zebra_xpath.h>
 
 #include <charmap.h>
 #include <rstemp.h>
@@ -43,6 +44,14 @@ struct rpn_char_map_info {
     int reg_type;
 };
 
+typedef struct {
+    int type;
+    int major;
+    int minor;
+    Z_AttributesPlusTerm *zapt;
+} AttrType;
+
+
 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
 {
     struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
@@ -70,32 +79,17 @@ static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
     dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
 }
 
-typedef struct {
-    int type;
-    int major;
-    int minor;
-    Z_AttributesPlusTerm *zapt;
-} AttrType;
-
 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
                         const char **string_value)
 {
     int num_attributes;
 
-#ifdef ASN_COMPILED
     num_attributes = src->zapt->attributes->num_attributes;
-#else
-    num_attributes = src->zapt->num_attributes;
-#endif
     while (src->major < num_attributes)
     {
         Z_AttributeElement *element;
 
-#ifdef ASN_COMPILED
         element = src->zapt->attributes->attributes[src->major];
-#else
-        element = src->zapt->attributeList[src->major];
-#endif
         if (src->type == *element->attributeType)
         {
             switch (element->which) 
@@ -453,18 +447,43 @@ static int term_104 (ZebraMaps zebra_maps, int reg_type,
     int i = 0;
     int j = 0;
 
-    if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
+    if (!term_pre (zebra_maps, reg_type, src, "?*#", "?*#"))
         return 0;
     s0 = *src;
     while (*s0)
     {
-        if (*s0 == '#')
+        if (*s0 == '?')
+        {
+           dst_term[j++] = *s0++;
+            if (*s0 >= '0' && *s0 <= '9')
+            {
+                int limit = 0;
+                while (*s0 >= '0' && *s0 <= '9')
+                {
+                    limit = limit * 10 + (*s0 - '0');
+                    dst_term[j++] = *s0++;
+                }
+                if (limit > 20)
+                    limit = 20;
+                while (--limit >= 0)
+                {
+                    dst[i++] = '.';
+                    dst[i++] = '?';
+                }
+            }
+            else
+            {
+                dst[i++] = '.';
+                dst[i++] = '*';
+            }
+        }
+        else if (*s0 == '*')
         {
             dst[i++] = '.';
             dst[i++] = '*';
            dst_term[j++] = *s0++;
         }
-        else if (*s0 == '!')
+        else if (*s0 == '#')
        {
             dst[i++] = '.';
            dst_term[j++] = *s0++;
@@ -1755,20 +1774,20 @@ static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                         oid_value attributeSet, struct grep_info *grep_info,
                         int reg_type, int complete_flag,
                         int num_bases, char **basenames,
-                        char *term_dst)
+                        char *term_dst, int xpath_use)
 {
     char term_dict[2*IT_MAX_WORD+2];
     int r, base_no;
     AttrType use;
     int use_value;
+    const char *use_string = 0;
     oid_value curAttributeSet = attributeSet;
     const char *termp;
     struct rpn_char_map_info rcmi;
 
     rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
     attr_init (&use, zapt, 1);
-    use_value = attr_find (&use, &curAttributeSet);
-    logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
+    use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
 
     if (use_value == -1)
         use_value = 1016;
@@ -1776,19 +1795,38 @@ static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     for (base_no = 0; base_no < num_bases; base_no++)
     {
         attent attp;
+        data1_local_attribute id_xpath_attr;
         data1_local_attribute *local_attr;
         int max_pos, prefix_len = 0;
 
         termp = *term_sub;
-        if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
+        if (use_value == -2)  /* string attribute (assume IDXPATH/any) */
         {
-            logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
-                  curAttributeSet, use_value, r);
-           if (r == -1)
-               zh->errCode = 114;
-           else
-               zh->errCode = 121;
-            return -1;
+            use_value = xpath_use;
+            attp.local_attributes = &id_xpath_attr;
+            attp.attset_ordinal = VAL_IDXPATH;
+            id_xpath_attr.next = 0;
+            id_xpath_attr.local = use_value;
+        }
+       else if (curAttributeSet == VAL_IDXPATH)
+        {
+            attp.local_attributes = &id_xpath_attr;
+            attp.attset_ordinal = VAL_IDXPATH;
+            id_xpath_attr.next = 0;
+            id_xpath_attr.local = use_value;
+        }
+        else
+        {
+            if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
+            {
+                logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
+                      curAttributeSet, use_value, r);
+                if (r == -1)
+                    zh->errCode = 114;
+                else
+                    zh->errCode = 121;
+                return -1;
+            }
         }
         if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
         {
@@ -1845,7 +1883,7 @@ static RSET rpn_search_APT_numeric (ZebraHandle zh,
                                    oid_value attributeSet,
                                    NMEM stream,
                                    int reg_type, int complete_flag,
-                                   const char *rank_type,
+                                   const char *rank_type, int xpath_use,
                                    int num_bases, char **basenames)
 {
     char term_dst[IT_MAX_WORD+1];
@@ -1862,7 +1900,7 @@ static RSET rpn_search_APT_numeric (ZebraHandle zh,
        grep_info.isam_p_indx = 0;
         r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
                          reg_type, complete_flag, num_bases, basenames,
-                         term_dst);
+                         term_dst, xpath_use);
         if (r < 1)
             break;
        logf (LOG_DEBUG, "term: %s", term_dst);
@@ -2014,13 +2052,8 @@ static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
        nmem_malloc (stream, sizeof(*sks->caseSensitivity));
     *sks->caseSensitivity = 0;
 
-#ifdef ASN_COMPILED
     sks->which = Z_SortKeySpec_null;
     sks->u.null = odr_nullval ();
-#else
-    sks->missingValueAction = 0;
-#endif
-
     sort_sequence->specs[i] = sks;
 
     parms.rset_term = rset_term_create (termz, -1, rank_type,
@@ -2028,6 +2061,9 @@ static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     return rset_create (rset_kind_null, &parms);
 }
 
+/* pop - moved to xpath.c */
+#if 0
+
 struct xpath_predicate {
     int which;
     union {
@@ -2051,6 +2087,8 @@ struct xpath_location_step {
     struct xpath_predicate *predicate;
 };
 
+#endif
+
 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                        oid_value attributeSet,
                        struct xpath_location_step *xpath, NMEM mem)
@@ -2058,7 +2096,6 @@ static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     oid_value curAttributeSet = attributeSet;
     AttrType use;
     const char *use_string = 0;
-    const char *cp;
     int no = 0;
     
     attr_init (&use, zapt, 1);
@@ -2066,87 +2103,11 @@ static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
 
     if (!use_string || *use_string != '/')
         return -1;
-    cp = use_string;
-    while (*cp)
-    {
-        int i = 0;
-        while (*cp && !strchr("/[",*cp))
-        {
-            i++;
-            cp++;
-        }
-        xpath[no].predicate = 0;
-        xpath[no].part = nmem_malloc (mem, i+1);
-        memcpy (xpath[no].part,  cp - i, i);
-        xpath[no].part[i] = 0;
 
-        if (*cp == '[')
-        {
-            struct xpath_predicate *p = xpath[no].predicate =
-                nmem_malloc (mem, sizeof(struct xpath_predicate));
-
-            p->which = XPATH_PREDICATE_RELATION;
-            cp++;
-            while (*cp == ' ')
-                cp++;
-
-            for (i = 0; *cp && !strchr("><=] ", *cp); i++)
-                cp++;
-            p->u.relation.name = nmem_malloc (mem, i+1);
-            memcpy (p->u.relation.name, cp - i, i);
-            p->u.relation.name[i] = 0;
-            while (*cp == ' ')
-                cp++;
-            if (*cp != ']')
-            {
-                for (i = 0; *cp && strchr(">=<!", *cp); i++)
-                    cp++;
-
-                p->u.relation.op = nmem_malloc (mem, i+1);
-                if (i)
-                    memcpy (p->u.relation.op, cp - i, i);
-                p->u.relation.op[i] = 0;
-                
-                while (*cp == ' ')
-                    cp++;
-                
-                if (strchr("\"'", *cp))
-                {
-                    cp++;
-                    for (i = 0; *cp && !strchr("\"'", *cp); i++)
-                        cp++;
-
-                    p->u.relation.value = nmem_malloc (mem, i+1);
-                    if (i)
-                        memcpy (p->u.relation.value, cp - i, i);
-                    p->u.relation.value[i] = 0;
-                    yaz_log (LOG_LOG, "value=%s", p->u.relation.value);
-
-                    cp++;
-                }                           
-                else
-                {
-                    for (i = 0; *cp && !strchr(" ]", *cp); i++)
-                        cp++;
-                    p->u.relation.value = nmem_malloc (mem, i+1);
-                    if (i)
-                        memcpy (p->u.relation.value, cp - i, i);
-                    p->u.relation.value[i] = 0;
-                }
-                while (*cp == ' ')
-                    cp++;
-            }
-            if (*cp == ']')
-                cp++;
-        } /* end of ] predicate */
-        no++;
-        if (*cp != '/')
-            break;
-        cp++;
-    }
-    return no;
+    return (parse_xpath_str(use_string, xpath, mem));
 }
-                
+               
 
 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
                         int reg_type, const char *term, int use,
@@ -2420,6 +2381,7 @@ static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     {
        rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
                                       reg_id, complete_flag, rank_type,
+                                       xpath_use,
                                       num_bases, basenames);
     }
     else if (!strcmp (search_type, "always"))
@@ -2473,21 +2435,11 @@ static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
             r = rset_create (rset_kind_not, &bool_parms);
             break;
         case Z_Operator_prox:
-#ifdef ASN_COMPILED
             if (zop->u.prox->which != Z_ProximityOperator_known)
             {
                 zh->errCode = 132;
                 return NULL;
             }
-#else
-            if (zop->u.prox->which != Z_ProxCode_known)
-            {
-                zh->errCode = 132;
-                return NULL;
-            }
-#endif
-
-#ifdef ASN_COMPILED
             if (*zop->u.prox->u.known != Z_ProxUnit_word)
             {
                 char *val = (char *) nmem_malloc (stream, 16);
@@ -2496,16 +2448,6 @@ static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
                 sprintf (val, "%d", *zop->u.prox->u.known);
                 return NULL;
             }
-#else
-            if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
-            {
-                char *val = (char *) nmem_malloc (stream, 16);
-                zh->errCode = 132;
-                zh->errString = val;
-                sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
-                return NULL;
-            }
-#endif
            else
            {
                RSET rsets[2];