+ else
+ {
+ if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
+ use_string)))
+ {
+ yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
+ curAttributeSet, use_value, r);
+ if (r == -1)
+ {
+ errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
+ if (use_string)
+ errString = nmem_strdup(stream, use_string);
+ else
+ errString = nmem_strdup_i (stream, use_value);
+ }
+ else
+ errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
+ continue;
+ }
+ }
+ if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
+ {
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ zh->errString = basenames[base_no];
+ return -1;
+ }
+ for (local_attr = attp.local_attributes; local_attr;
+ local_attr = local_attr->next)
+ {
+ int ord;
+ char ord_buf[32];
+ int i, ord_len;
+
+ ord = zebraExplain_lookup_attr_su(zh->reg->zei,
+ attp.attset_ordinal,
+ local_attr->local);
+ if (ord < 0)
+ continue;
+ if (prefix_len)
+ term_dict[prefix_len++] = '|';
+ else
+ term_dict[prefix_len++] = '(';
+
+ ord_len = key_SU_encode (ord, ord_buf);
+ for (i = 0; i<ord_len; i++)
+ {
+ term_dict[prefix_len++] = 1;
+ term_dict[prefix_len++] = ord_buf[i];
+ }
+ }
+ if (!prefix_len)
+ {
+ errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
+ errString = nmem_strdup_i(stream, use_value);
+ continue;
+ }
+ bases_ok++;
+ term_dict[prefix_len++] = ')';
+ term_dict[prefix_len++] = 1;
+ term_dict[prefix_len++] = reg_type;
+ yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
+ term_dict[prefix_len] = '\0';
+ if (!numeric_relation(zh, zapt, &termp, term_dict,
+ attributeSet, grep_info, &max_pos, reg_type,
+ term_dst, &relation_error))
+ {
+ if (relation_error)
+ {
+ zh->errCode = relation_error;
+ zh->errString = 0;
+ return ZEBRA_FAIL;
+ }
+ *term_sub = 0;
+ return ZEBRA_OK;
+ }
+ }
+ if (!bases_ok)
+ {
+ zh->errCode = errCode;
+ zh->errString = errString;
+ return ZEBRA_FAIL;
+ }
+ *term_sub = termp;
+ yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
+ return ZEBRA_OK;
+}
+
+static ZEBRA_RES rpn_search_APT_numeric(ZebraHandle zh,
+ Z_AttributesPlusTerm *zapt,
+ const char *termz,
+ oid_value attributeSet,
+ NMEM stream,
+ int reg_type, int complete_flag,
+ const char *rank_type, int xpath_use,
+ int num_bases, char **basenames,
+ NMEM rset_nmem,
+ RSET *rset)
+{
+ char term_dst[IT_MAX_WORD+1];
+ const char *termp = termz;
+ RSET *result_sets = 0;
+ int num_result_sets = 0;
+ ZEBRA_RES res;
+ struct grep_info grep_info;
+ int alloc_sets = 0;
+
+ yaz_log(log_level_rpn, "APT_numeric t='%s'", termz);
+ if (grep_info_prepare(zh, zapt, &grep_info, reg_type, stream))
+ return ZEBRA_FAIL;
+ while (1)
+ {
+ if (alloc_sets == num_result_sets)
+ {
+ int add = 10;
+ RSET *rnew = (RSET *) nmem_malloc(stream, (alloc_sets+add) *
+ sizeof(*rnew));
+ if (alloc_sets)
+ memcpy(rnew, result_sets, alloc_sets * sizeof(*rnew));
+ alloc_sets = alloc_sets + add;
+ result_sets = rnew;
+ }
+ yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
+ grep_info.isam_p_indx = 0;
+ res = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
+ reg_type, complete_flag, num_bases, basenames,
+ term_dst, xpath_use,
+ stream);
+ if (res == ZEBRA_FAIL || termp == 0)
+ break;
+ yaz_log(YLOG_DEBUG, "term: %s", term_dst);
+ result_sets[num_result_sets] =
+ rset_trunc(zh, grep_info.isam_p_buf,
+ grep_info.isam_p_indx, term_dst,
+ strlen(term_dst), rank_type,
+ 0 /* preserve position */,
+ zapt->term->which, rset_nmem,
+ key_it_ctrl,key_it_ctrl->scope);
+ if (!result_sets[num_result_sets])
+ break;
+ num_result_sets++;
+ }
+ grep_info_delete(&grep_info);
+ if (termp)
+ {
+ int i;
+ for (i = 0; i<num_result_sets; i++)
+ rset_delete(result_sets[i]);
+ return ZEBRA_FAIL;
+ }
+ if (num_result_sets == 0)
+ *rset = rsnull_create(rset_nmem, key_it_ctrl);
+ if (num_result_sets == 1)
+ *rset = result_sets[0];
+ else
+ *rset = rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
+ num_result_sets, result_sets);
+ if (!*rset)
+ return ZEBRA_FAIL;
+ return ZEBRA_OK;
+}
+
+static ZEBRA_RES rpn_search_APT_local(ZebraHandle zh,
+ Z_AttributesPlusTerm *zapt,
+ const char *termz,
+ oid_value attributeSet,
+ NMEM stream,
+ const char *rank_type, NMEM rset_nmem,
+ RSET *rset)
+{
+ RSFD rsfd;
+ struct it_key key;
+ int sys;
+ *rset = rstemp_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
+ res_get (zh->res, "setTmpDir"),0 );
+ rsfd = rset_open(*rset, RSETF_WRITE);
+
+ sys = atoi(termz);
+ if (sys <= 0)
+ sys = 1;
+ key.mem[0] = sys;
+ key.mem[1] = 1;
+ key.len = 2;
+ rset_write (rsfd, &key);
+ rset_close (rsfd);
+ return ZEBRA_OK;
+}
+
+static ZEBRA_RES rpn_sort_spec(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ oid_value attributeSet, NMEM stream,
+ Z_SortKeySpecList *sort_sequence,
+ const char *rank_type,
+ RSET *rset)
+{
+ int i;
+ int sort_relation_value;
+ AttrType sort_relation_type;
+ int use_value;
+ AttrType use_type;
+ Z_SortKeySpec *sks;
+ Z_SortKey *sk;
+ Z_AttributeElement *ae;
+ int oid[OID_SIZE];
+ oident oe;
+ char termz[20];
+
+ attr_init(&sort_relation_type, zapt, 7);
+ sort_relation_value = attr_find(&sort_relation_type, &attributeSet);
+
+ attr_init(&use_type, zapt, 1);
+ use_value = attr_find(&use_type, &attributeSet);
+
+ if (!sort_sequence->specs)
+ {
+ sort_sequence->num_specs = 10;
+ sort_sequence->specs = (Z_SortKeySpec **)
+ nmem_malloc(stream, sort_sequence->num_specs *
+ sizeof(*sort_sequence->specs));
+ for (i = 0; i<sort_sequence->num_specs; i++)
+ sort_sequence->specs[i] = 0;