+static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ oid_value attributeSet, NMEM stream,
+ Z_SortKeySpecList *sort_sequence,
+ const char *rank_type)
+{
+ rset_null_parms parms;
+ 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;
+ }
+ if (zapt->term->which != Z_Term_general)
+ i = 0;
+ else
+ i = atoi_n ((char *) zapt->term->u.general->buf,
+ zapt->term->u.general->len);
+ if (i >= sort_sequence->num_specs)
+ i = 0;
+ sprintf (termz, "%d", i);
+
+ oe.proto = PROTO_Z3950;
+ oe.oclass = CLASS_ATTSET;
+ oe.value = attributeSet;
+ if (!oid_ent_to_oid (&oe, oid))
+ return 0;
+
+ sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
+ sks->sortElement = (Z_SortElement *)
+ nmem_malloc (stream, sizeof(*sks->sortElement));
+ sks->sortElement->which = Z_SortElement_generic;
+ sk = sks->sortElement->u.generic = (Z_SortKey *)
+ nmem_malloc (stream, sizeof(*sk));
+ sk->which = Z_SortKey_sortAttributes;
+ sk->u.sortAttributes = (Z_SortAttributes *)
+ nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
+
+ sk->u.sortAttributes->id = oid;
+ sk->u.sortAttributes->list = (Z_AttributeList *)
+ nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
+ sk->u.sortAttributes->list->num_attributes = 1;
+ sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
+ nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
+ ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
+ nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
+ ae->attributeSet = 0;
+ ae->attributeType = (int *)
+ nmem_malloc (stream, sizeof(*ae->attributeType));
+ *ae->attributeType = 1;
+ ae->which = Z_AttributeValue_numeric;
+ ae->value.numeric = (int *)
+ nmem_malloc (stream, sizeof(*ae->value.numeric));
+ *ae->value.numeric = use_value;
+
+ sks->sortRelation = (int *)
+ nmem_malloc (stream, sizeof(*sks->sortRelation));
+ if (sort_relation_value == 1)
+ *sks->sortRelation = Z_SortRelation_ascending;
+ else if (sort_relation_value == 2)
+ *sks->sortRelation = Z_SortRelation_descending;
+ else
+ *sks->sortRelation = Z_SortRelation_ascending;
+
+ sks->caseSensitivity = (int *)
+ 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);
+ return rset_create (rset_kind_null, &parms);
+}
+
+static RSET rpn_search_xpath (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ oid_value attributeSet,
+ int num_bases, char **basenames,
+ NMEM stream, const char *rank_type, RSET rset)
+{
+ AttrType use;
+ const char *use_string = 0;
+ oid_value curAttributeSet = attributeSet;
+ char term_dict[2048];
+ int base_no;
+ int reg_type = '0';
+ struct grep_info grep_info;
+
+ yaz_log (LOG_LOG, "rpn_search_xpath 1");
+ attr_init (&use, zapt, 1);
+ attr_find_ex (&use, &curAttributeSet, &use_string);
+
+ if (curAttributeSet != VAL_IDXPATH)
+ {
+ yaz_log (LOG_LOG, "rpn_search_xpath - not 1");
+ return rset;
+ }
+ if (!use_string)
+ {
+ yaz_log (LOG_LOG, "rpn_search_xpath - not 2");
+ return rset;
+ }
+
+ dict_grep_cmap (zh->reg->dict, 0, 0);
+ if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
+ return 0;
+
+ yaz_log (LOG_LOG, "rpn_search_xpath 2");
+ for (base_no = 0; base_no < num_bases; base_no++)
+ {
+ const char *termp = use_string;
+ rset_between_parms parms;
+ RSET rset_start_tag, rset_end_tag;
+ int ord, ord_len, i, r, max_pos;
+ int prefix_len ;
+ char ord_buf[32];
+ if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
+ {
+ zh->errCode = 109; /* Database unavailable */
+ zh->errString = basenames[base_no];
+ return rset;
+ }
+
+ prefix_len = 0;
+ ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, 1);
+ 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];
+ }
+ term_dict[prefix_len++] = ')';
+ term_dict[prefix_len++] = 1;
+ term_dict[prefix_len++] = reg_type;
+
+ termp = use_string;
+ strcpy (term_dict+prefix_len, use_string);
+
+ grep_info.isam_p_indx = 0;
+ yaz_log (LOG_LOG, "rpn_search_xpath 3 %s", term_dict+prefix_len);
+ r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
+ &grep_info, &max_pos, 0, grep_handle);
+ yaz_log (LOG_LOG, "%s %d positions", use_string,
+ grep_info.isam_p_indx);
+ rset_start_tag =
+ rset_trunc (zh, grep_info.isam_p_buf,
+ grep_info.isam_p_indx, use_string, strlen(use_string),
+ rank_type, 1);
+
+ prefix_len = 0;
+ ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, 2);
+ 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];
+ }
+ term_dict[prefix_len++] = ')';
+ term_dict[prefix_len++] = 1;
+ term_dict[prefix_len++] = reg_type;
+
+ termp = use_string;
+
+ strcpy (term_dict+prefix_len, use_string);
+
+ grep_info.isam_p_indx = 0;
+ r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
+ &grep_info, &max_pos, 0, grep_handle);
+
+ yaz_log (LOG_LOG, "%s %d positions", use_string,
+ grep_info.isam_p_indx);
+ rset_end_tag =
+ rset_trunc (zh, grep_info.isam_p_buf,
+ grep_info.isam_p_indx, use_string, strlen(use_string),
+ rank_type, 1);
+
+ parms.key_size = sizeof(struct it_key);
+ parms.cmp = key_compare_it;
+ parms.rset_l = rset_start_tag;
+ parms.rset_m = rset;
+ parms.rset_r = rset_end_tag;
+ parms.printer = key_print_it;
+ yaz_log (LOG_LOG, "rpn_search_xpath 4");
+ rset = rset_create (rset_kind_between, &parms);
+ }
+ grep_info_delete (&grep_info);
+
+ return rset;
+}
+
+
+
+static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ oid_value attributeSet, NMEM stream,
+ Z_SortKeySpecList *sort_sequence,