Implemented bug #588: allrecords search. Using @attr 1=allrecords ""
[idzebra-moved-to-github.git] / index / zrpn.c
index 66e6990..f7ff740 100644 (file)
@@ -1,5 +1,5 @@
-/* $Id: zrpn.c,v 1.205 2005-08-26 10:13:31 adam Exp $
-   Copyright (C) 1995-2005
+/* $Id: zrpn.c,v 1.213 2006-05-17 17:46:45 adam Exp $
+   Copyright (C) 1995-2006
    Index Data ApS
 
 This file is part of the Zebra server.
@@ -82,7 +82,7 @@ static void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
 }
 
 static int attr_find_ex(AttrType *src, oid_value *attributeSetP,
-                         const char **string_value)
+                       const char **string_value)
 {
     int num_attributes;
 
@@ -181,8 +181,12 @@ void zebra_term_untrans(ZebraHandle zh, int reg_type,
     {
         const char *cp = zebra_maps_output(zh->reg->zebra_maps,
                                           reg_type, &src);
-        if (!cp && len < IT_MAX_WORD-1)
-            dst[len++] = *src++;
+       if (!cp)
+       {
+           if (len < IT_MAX_WORD-1)
+               dst[len++] = *src;
+           src++;
+       }
         else
             while (*cp && len < IT_MAX_WORD-1)
                 dst[len++] = *cp++;
@@ -241,7 +245,7 @@ static void add_isam_p(const char *name, const char *info,
         zebra_term_untrans  (p->zh, p->reg_type, term_tmp, name+len+1);
         yaz_log(log_level_rpn, "grep: %d %c %s", ord, name[len], term_tmp);
         zebraExplain_lookup_ord (p->zh->reg->zei,
-                                 ord, 0 /* index_type */, &db, &set, &use);
+                                 ord, 0 /* index_type */, &db, &set, &use, 0);
         yaz_log(log_level_rpn, "grep:  set=%d use=%d db=%s", set, use, db);
         
         resultSetAddTerm(p->zh, p->termset, name[len], db,
@@ -965,7 +969,10 @@ static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         break;
     case 3:
     case 102:
+    case 103:
     case -1:
+        if (!**term_sub)
+            return 1;
         yaz_log(log_level_rpn, "Relation =");
         if (!term_100(zh->reg->zebra_maps, reg_type, term_sub,
                       term_component, space_split, term_dst))
@@ -993,19 +1000,28 @@ static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
 static ZEBRA_RES term_limits_APT(ZebraHandle zh,
                                 Z_AttributesPlusTerm *zapt,
                                 zint *hits_limit_value,
-                                const char **term_ref_id_str)
+                                const char **term_ref_id_str,
+                                NMEM nmem)
 {
     AttrType term_ref_id_attr;
     AttrType hits_limit_attr;
+    int term_ref_id_int;
  
     attr_init(&hits_limit_attr, zapt, 9);
     *hits_limit_value  = attr_find(&hits_limit_attr, NULL);
 
     attr_init(&term_ref_id_attr, zapt, 10);
-    attr_find_ex(&term_ref_id_attr, NULL, term_ref_id_str);
+    term_ref_id_int = attr_find_ex(&term_ref_id_attr, NULL, term_ref_id_str);
+    if (term_ref_id_int >= 0)
+    {
+       char *res = nmem_malloc(nmem, 20);
+       sprintf(res, "%d", term_ref_id_int);
+       *term_ref_id_str = res;
+    }
 
     /* no limit given ? */
     if (*hits_limit_value == -1)
+    {
        if (*term_ref_id_str)
        {
            /* use global if term_ref is present */
@@ -1016,6 +1032,7 @@ static ZEBRA_RES term_limits_APT(ZebraHandle zh,
            /* no counting if term_ref is not present */
            *hits_limit_value = 0;
        }
+    }
     else if (*hits_limit_value == 0)
     {
        /* 0 is the same as global limit */
@@ -1046,7 +1063,8 @@ static ZEBRA_RES term_trunc(ZebraHandle zh,
     const char *term_ref_id_str = 0;
     *rset = 0;
 
-    term_limits_APT(zh, zapt, &hits_limit_value, &term_ref_id_str);
+    term_limits_APT(zh, zapt, &hits_limit_value, &term_ref_id_str,
+                   stream);
     grep_info->isam_p_indx = 0;
     res = string_term(zh, zapt, term_sub, attributeSet, stream, grep_info,
                      reg_type, complete_flag, num_bases, basenames,
@@ -1224,17 +1242,14 @@ static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                term_dict[prefix_len++] = 1;
                term_dict[prefix_len++] = ord_buf[i];
            }
+           if (ord_len > init_pos)
+               init_pos = ord_len;
        }
        bases_ok++;
         if (prefix_len)
            attr_ok = 1;
 
         term_dict[prefix_len++] = ')';
-#if REG_TYPE_PREFIX
-        term_dict[prefix_len++] = 1;
-        term_dict[prefix_len++] = reg_type;
-        yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
-#endif
         term_dict[prefix_len] = '\0';
         j = prefix_len;
         switch (truncation_value)
@@ -1308,7 +1323,6 @@ static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         case 103:       /* Regexp-2 */
             regex_range = 1;
             term_dict[j++] = '(';
-           init_pos = 2;
             if (!term_103(zh->reg->zebra_maps, reg_type,
                           &termp, term_dict + j, &regex_range,
                          space_split, term_dst))
@@ -1557,7 +1571,9 @@ static ZEBRA_RES term_list_trunc(ZebraHandle zh,
     struct grep_info grep_info;
     const char *termp = termz;
     int alloc_sets = 0;
+    int empty_term = *termz ? 0 : 1;
 
+    empty_term = 0;
     *num_result_sets = 0;
     *term_dst = 0;
     if (grep_info_prepare(zh, zapt, &grep_info, reg_type) == ZEBRA_FAIL)
@@ -1595,6 +1611,11 @@ static ZEBRA_RES term_list_trunc(ZebraHandle zh,
        if ((*result_sets)[*num_result_sets] == 0)
            break;
        (*num_result_sets)++;
+
+        if (empty_term)
+            break;
+        if (!*termp)
+            break;
     }
     grep_info_delete(&grep_info);
     return ZEBRA_OK;
@@ -1882,11 +1903,6 @@ static ZEBRA_RES numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         }
         bases_ok++;
         term_dict[prefix_len++] = ')';
-#if REG_TYPE_PREFIX    
-        term_dict[prefix_len++] = 1;
-        term_dict[prefix_len++] = reg_type;
-        yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
-#endif
         term_dict[prefix_len] = '\0';
         if (!numeric_relation(zh, zapt, &termp, term_dict,
                              attributeSet, grep_info, &max_pos, reg_type,
@@ -1931,7 +1947,7 @@ static ZEBRA_RES rpn_search_APT_numeric(ZebraHandle zh,
     zint hits_limit_value;
     const char *term_ref_id_str = 0;
 
-    term_limits_APT(zh, zapt, &hits_limit_value, &term_ref_id_str);
+    term_limits_APT(zh, zapt, &hits_limit_value, &term_ref_id_str, stream);
 
     yaz_log(log_level_rpn, "APT_numeric t='%s'", termz);
     if (grep_info_prepare(zh, zapt, &grep_info, reg_type) == ZEBRA_FAIL)
@@ -2147,10 +2163,6 @@ static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
         term_dict[prefix_len++] = ord_buf[i];
     }
     term_dict[prefix_len++] = ')';
-#if REG_TYPE_PREFIX
-    term_dict[prefix_len++] = 1;
-    term_dict[prefix_len++] = reg_type;
-#endif
     strcpy(term_dict+prefix_len, term);
     
     grep_info.isam_p_indx = 0;
@@ -2409,6 +2421,15 @@ static ZEBRA_RES rpn_search_APT(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                                     num_bases, basenames, rset_nmem,
                                     rset, kc);
     }
+    else if (!strcmp(search_type, "always"))
+    {
+        *termz = '\0';
+        res = rpn_search_APT_phrase(zh, zapt, termz, attributeSet, stream,
+                                    reg_id, complete_flag, rank_type,
+                                    xpath_use,
+                                    num_bases, basenames, rset_nmem,
+                                    rset, kc);
+    }
     else
     {
        zebra_setError(zh, YAZ_BIB1_UNSUPP_STRUCTURE_ATTRIBUTE, 0);
@@ -2915,9 +2936,6 @@ ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
             scan_info->list[j].term = NULL;
 
         prefix_len += key_SU_encode (ords[i], termz + prefix_len);
-#if REG_TYPE_PREFIX
-        termz[prefix_len++] = reg_id;
-#endif
         termz[prefix_len] = 0;
         strcpy(scan_info->prefix, termz);
 
@@ -3128,3 +3146,11 @@ ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
     return ZEBRA_OK;
 }
 
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+