+ 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 ZEBRA_FAIL;
+
+ 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_SortKeySpec_ascending;
+ else if (sort_relation_value == 2)
+ *sks->sortRelation = Z_SortKeySpec_descending;
+ else
+ *sks->sortRelation = Z_SortKeySpec_ascending;
+
+ sks->caseSensitivity = (int *)
+ nmem_malloc(stream, sizeof(*sks->caseSensitivity));
+ *sks->caseSensitivity = 0;
+
+ sks->which = Z_SortKeySpec_null;
+ sks->u.null = odr_nullval ();
+ sort_sequence->specs[i] = sks;
+ *rset = rsnull_create (NULL, key_it_ctrl);
+ return ZEBRA_OK;
+}
+
+
+static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ oid_value attributeSet,
+ struct xpath_location_step *xpath, int max, NMEM mem)
+{
+ oid_value curAttributeSet = attributeSet;
+ AttrType use;
+ const char *use_string = 0;
+
+ attr_init(&use, zapt, 1);
+ attr_find_ex(&use, &curAttributeSet, &use_string);
+
+ if (!use_string || *use_string != '/')
+ return -1;
+
+ return zebra_parse_xpath_str(use_string, xpath, max, mem);
+}
+
+
+
+static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
+ int reg_type, const char *term, int use,
+ oid_value curAttributeSet, NMEM rset_nmem)
+{
+ RSET rset;
+ struct grep_info grep_info;
+ char term_dict[2048];
+ char ord_buf[32];
+ int prefix_len = 0;
+ int ord = zebraExplain_lookup_attr_su(zh->reg->zei, curAttributeSet, use);
+ int ord_len, i, r, max_pos;
+ int term_type = Z_Term_characterString;
+ const char *flags = "void";
+
+ if (grep_info_prepare(zh, 0 /* zapt */, &grep_info, '0', stream))
+ return rsnull_create (rset_nmem,key_it_ctrl);
+
+ if (ord < 0)
+ return rsnull_create (rset_nmem,key_it_ctrl);
+ 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;
+
+ strcpy(term_dict+prefix_len, term);
+
+ 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(YLOG_DEBUG, "%s %d positions", term,
+ grep_info.isam_p_indx);
+ rset = rset_trunc(zh, grep_info.isam_p_buf,
+ grep_info.isam_p_indx, term, strlen(term),
+ flags, 1, term_type,rset_nmem,
+ key_it_ctrl, key_it_ctrl->scope);
+ grep_info_delete(&grep_info);
+ return rset;
+}
+
+static RSET rpn_search_xpath(ZebraHandle zh,
+ oid_value attributeSet,
+ int num_bases, char **basenames,
+ NMEM stream, const char *rank_type, RSET rset,
+ int xpath_len, struct xpath_location_step *xpath,
+ NMEM rset_nmem)
+{
+ oid_value curAttributeSet = attributeSet;
+ int base_no;
+ int i;
+
+ if (xpath_len < 0)
+ return rset;
+
+ yaz_log(YLOG_DEBUG, "xpath len=%d", xpath_len);
+ for (i = 0; i<xpath_len; i++)
+ {
+ yaz_log(log_level_rpn, "XPATH %d %s", i, xpath[i].part);
+
+ }
+
+ curAttributeSet = VAL_IDXPATH;
+
+ /*
+ //a -> a/.*
+ //a/b -> b/a/.*
+ /a -> a/
+ /a/b -> b/a/
+
+ / -> none
+
+ a[@attr = value]/b[@other = othervalue]
+
+ /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
+ /a/b val range(b/a/,freetext(w,1016,val),b/a/)
+ /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
+ /a/b[@c = y] val range(b/a/,freetext(w,1016,val),b/a/,@c = y)
+ /a[@c = y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c = y)
+ /a[@c = x]/b[@c = y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c = y),a/,@c = x)
+
+ */
+
+ dict_grep_cmap (zh->reg->dict, 0, 0);
+
+ for (base_no = 0; base_no < num_bases; base_no++)
+ {
+ int level = xpath_len;
+ int first_path = 1;
+
+ if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
+ {
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ zh->errString = basenames[base_no];
+ return rset;
+ }
+ while (--level >= 0)
+ {
+ char xpath_rev[128];
+ int i, len;
+ RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
+
+ *xpath_rev = 0;
+ len = 0;
+ for (i = level; i >= 1; --i)
+ {
+ const char *cp = xpath[i].part;
+ if (*cp)
+ {
+ for (;*cp; cp++)
+ if (*cp == '*')
+ {
+ memcpy (xpath_rev + len, "[^/]*", 5);
+ len += 5;
+ }
+ else if (*cp == ' ')
+ {
+
+ xpath_rev[len++] = 1;
+ xpath_rev[len++] = ' ';
+ }
+
+ else
+ xpath_rev[len++] = *cp;
+ xpath_rev[len++] = '/';
+ }
+ else if (i == 1) /* // case */
+ {
+ xpath_rev[len++] = '.';
+ xpath_rev[len++] = '*';
+ }
+ }
+ xpath_rev[len] = 0;
+
+ if (xpath[level].predicate &&
+ xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
+ xpath[level].predicate->u.relation.name[0])
+ {
+ WRBUF wbuf = wrbuf_alloc();
+ wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
+ if (xpath[level].predicate->u.relation.value)
+ {
+ const char *cp = xpath[level].predicate->u.relation.value;
+ wrbuf_putc(wbuf, '=');
+
+ while (*cp)
+ {
+ if (strchr(REGEX_CHARS, *cp))
+ wrbuf_putc(wbuf, '\\');
+ wrbuf_putc(wbuf, *cp);
+ cp++;
+ }
+ }
+ wrbuf_puts(wbuf, "");
+ rset_attr = xpath_trunc(
+ zh, stream, '0', wrbuf_buf(wbuf), 3,
+ curAttributeSet,rset_nmem);
+ wrbuf_free(wbuf, 1);
+ }
+ else
+ {
+ if (!first_path)
+ continue;
+ }
+ yaz_log(log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
+ if (strlen(xpath_rev))
+ {
+ rset_start_tag = xpath_trunc(zh, stream, '0',
+ xpath_rev, 1, curAttributeSet, rset_nmem);
+
+ rset_end_tag = xpath_trunc(zh, stream, '0',
+ xpath_rev, 2, curAttributeSet, rset_nmem);
+
+ rset = rsbetween_create(rset_nmem, key_it_ctrl,
+ key_it_ctrl->scope,
+ rset_start_tag, rset,
+ rset_end_tag, rset_attr);
+ }
+ first_path = 0;
+ }
+ }
+
+ return rset;
+}
+
+
+
+static ZEBRA_RES rpn_search_APT(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ oid_value attributeSet, NMEM stream,
+ Z_SortKeySpecList *sort_sequence,
+ int num_bases, char **basenames,
+ NMEM rset_nmem,
+ RSET *rset)
+{
+ ZEBRA_RES res = ZEBRA_OK;
+ unsigned reg_id;
+ char *search_type = NULL;
+ char rank_type[128];
+ int complete_flag;
+ int sort_flag;
+ char termz[IT_MAX_WORD+1];
+ int xpath_len;
+ int xpath_use = 0;
+ struct xpath_location_step xpath[10];
+
+ if (!log_level_set)
+ {
+ log_level_rpn = yaz_log_module_level("rpn");
+ log_level_set = 1;
+ }
+ zebra_maps_attr(zh->reg->zebra_maps, zapt, ®_id, &search_type,
+ rank_type, &complete_flag, &sort_flag);
+
+ yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
+ yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
+ yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
+ yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
+
+ if (zapt_term_to_utf8(zh, zapt, termz) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+
+ if (sort_flag)
+ return rpn_sort_spec(zh, zapt, attributeSet, stream, sort_sequence,
+ rank_type, rset);
+ xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
+ if (xpath_len >= 0)
+ {
+ xpath_use = 1016;
+ if (xpath[xpath_len-1].part[0] == '@')
+ xpath_use = 1015;
+ }
+
+ if (!strcmp(search_type, "phrase"))
+ {
+ res = rpn_search_APT_phrase(zh, zapt, termz, attributeSet, stream,
+ reg_id, complete_flag, rank_type,
+ xpath_use,
+ num_bases, basenames, rset_nmem,
+ rset);
+ }
+ else if (!strcmp(search_type, "and-list"))
+ {
+ res = rpn_search_APT_and_list(zh, zapt, termz, attributeSet, stream,
+ reg_id, complete_flag, rank_type,
+ xpath_use,
+ num_bases, basenames, rset_nmem,
+ rset);
+ }
+ else if (!strcmp(search_type, "or-list"))
+ {
+ res = rpn_search_APT_or_list(zh, zapt, termz, attributeSet, stream,
+ reg_id, complete_flag, rank_type,
+ xpath_use,
+ num_bases, basenames, rset_nmem,
+ rset);
+ }
+ else if (!strcmp(search_type, "local"))
+ {
+ res = rpn_search_APT_local(zh, zapt, termz, attributeSet, stream,
+ rank_type, rset_nmem, rset);
+ }
+ else if (!strcmp(search_type, "numeric"))
+ {
+ res = rpn_search_APT_numeric(zh, zapt, termz, attributeSet, stream,
+ reg_id, complete_flag, rank_type,
+ xpath_use,
+ num_bases, basenames, rset_nmem, rset);
+ }
+ else
+ {
+ zh->errCode = YAZ_BIB1_UNSUPP_STRUCTURE_ATTRIBUTE;
+ return ZEBRA_FAIL;
+ }
+ if (res != ZEBRA_OK)
+ return res;
+ if (!*rset)
+ return ZEBRA_FAIL;
+ *rset = rpn_search_xpath(zh, attributeSet, num_bases, basenames,
+ stream, rank_type, *rset,
+ xpath_len, xpath, rset_nmem);
+ if (!*rset)
+ return ZEBRA_FAIL;
+ return ZEBRA_OK;
+}
+
+static ZEBRA_RES rpn_search_structure(ZebraHandle zh, Z_RPNStructure *zs,
+ oid_value attributeSet,
+ NMEM stream, NMEM rset_nmem,
+ Z_SortKeySpecList *sort_sequence,
+ int num_bases, char **basenames,
+ RSET **result_sets, int *num_result_sets,
+ Z_Operator *parent_op);
+
+ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
+ oid_value attributeSet,
+ NMEM stream, NMEM rset_nmem,
+ Z_SortKeySpecList *sort_sequence,
+ int num_bases, char **basenames,
+ RSET *result_set)
+{
+ RSET *result_sets = 0;
+ int num_result_sets = 0;
+ ZEBRA_RES res = rpn_search_structure(zh, zs, attributeSet,
+ stream, rset_nmem,
+ sort_sequence,
+ num_bases, basenames,
+ &result_sets, &num_result_sets,
+ 0 /* no op */);
+ if (res != ZEBRA_OK)
+ {
+ int i;
+ for (i = 0; i<num_result_sets; i++)
+ rset_delete(result_sets[i]);
+ *result_set = 0;
+ return res;
+ }
+ assert(num_result_sets == 1);
+ assert(result_sets);
+ assert(*result_sets);
+ *result_set = *result_sets;
+ return ZEBRA_OK;
+}
+
+ZEBRA_RES rpn_search_structure(ZebraHandle zh, Z_RPNStructure *zs,
+ oid_value attributeSet,
+ NMEM stream, NMEM rset_nmem,
+ Z_SortKeySpecList *sort_sequence,
+ int num_bases, char **basenames,
+ RSET **result_sets, int *num_result_sets,
+ Z_Operator *parent_op)
+{
+ *num_result_sets = 0;
+ if (zs->which == Z_RPNStructure_complex)
+ {
+ ZEBRA_RES res;
+ Z_Operator *zop = zs->u.complex->roperator;
+ RSET *result_sets_l = 0;
+ int num_result_sets_l = 0;
+ RSET *result_sets_r = 0;
+ int num_result_sets_r = 0;
+
+ res = rpn_search_structure(zh, zs->u.complex->s1,
+ attributeSet, stream, rset_nmem,
+ sort_sequence,
+ num_bases, basenames,
+ &result_sets_l, &num_result_sets_l,
+ zop);
+ if (res != ZEBRA_OK)
+ {
+ int i;
+ for (i = 0; i<num_result_sets_l; i++)
+ rset_delete(result_sets_l[i]);
+ return res;
+ }
+ res = rpn_search_structure(zh, zs->u.complex->s2,
+ attributeSet, stream, rset_nmem,
+ sort_sequence,
+ num_bases, basenames,
+ &result_sets_r, &num_result_sets_r,
+ zop);
+ if (res != ZEBRA_OK)
+ {
+ int i;
+ for (i = 0; i<num_result_sets_l; i++)
+ rset_delete(result_sets_l[i]);
+ for (i = 0; i<num_result_sets_r; i++)
+ rset_delete(result_sets_r[i]);
+ return res;
+ }
+
+ /* make a new list of result for all children */
+ *num_result_sets = num_result_sets_l + num_result_sets_r;
+ *result_sets = nmem_malloc(stream, *num_result_sets *
+ sizeof(**result_sets));
+ memcpy(*result_sets, result_sets_l,
+ num_result_sets_l * sizeof(**result_sets));
+ memcpy(*result_sets + num_result_sets_l, result_sets_r,
+ num_result_sets_r * sizeof(**result_sets));
+
+ if (!parent_op || parent_op->which != zop->which
+ || (zop->which != Z_Operator_and &&
+ zop->which != Z_Operator_or))
+ {
+ /* parent node different from this one (or non-present) */
+ /* we must combine result sets now */
+ RSET rset;
+ switch (zop->which)
+ {
+ case Z_Operator_and:
+ rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
+ key_it_ctrl->scope,
+ *num_result_sets, *result_sets);
+ break;
+ case Z_Operator_or:
+ rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
+ key_it_ctrl->scope,
+ *num_result_sets, *result_sets);
+ break;
+ case Z_Operator_and_not:
+ rset = rsbool_create_not(rset_nmem, key_it_ctrl,
+ key_it_ctrl->scope,
+ (*result_sets)[0],
+ (*result_sets)[1]);
+ break;
+ case Z_Operator_prox:
+ if (zop->u.prox->which != Z_ProximityOperator_known)
+ {
+ zh->errCode = YAZ_BIB1_UNSUPP_PROX_UNIT_CODE;
+ return ZEBRA_FAIL;
+ }
+ if (*zop->u.prox->u.known != Z_ProxUnit_word)
+ {
+ char *val = (char *) nmem_malloc(stream, 16);
+ zh->errCode = YAZ_BIB1_UNSUPP_PROX_UNIT_CODE;
+ zh->errString = val;
+ sprintf(val, "%d", *zop->u.prox->u.known);
+ return ZEBRA_FAIL;
+ }
+ else
+ {
+ rset = rsprox_create(rset_nmem, key_it_ctrl,
+ key_it_ctrl->scope,
+ *num_result_sets, *result_sets,
+ *zop->u.prox->ordered,
+ (!zop->u.prox->exclusion ?
+ 0 : *zop->u.prox->exclusion),
+ *zop->u.prox->relationType,
+ *zop->u.prox->distance );
+ }
+ break;
+ default:
+ zh->errCode = YAZ_BIB1_OPERATOR_UNSUPP;
+ return ZEBRA_FAIL;
+ }
+ *num_result_sets = 1;
+ *result_sets = nmem_malloc(stream, *num_result_sets *
+ sizeof(**result_sets));
+ (*result_sets)[0] = rset;
+ }
+ }
+ else if (zs->which == Z_RPNStructure_simple)
+ {
+ RSET rset;
+ ZEBRA_RES res;
+
+ if (zs->u.simple->which == Z_Operand_APT)
+ {
+ yaz_log(YLOG_DEBUG, "rpn_search_APT");
+ res = rpn_search_APT(zh, zs->u.simple->u.attributesPlusTerm,
+ attributeSet, stream, sort_sequence,
+ num_bases, basenames, rset_nmem, &rset);
+ if (res != ZEBRA_OK)
+ return res;
+ }
+ else if (zs->u.simple->which == Z_Operand_resultSetId)
+ {
+ yaz_log(YLOG_DEBUG, "rpn_search_ref");
+ rset = resultSetRef(zh, zs->u.simple->u.resultSetId);
+ if (!rset)
+ {
+ zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
+ zh->errString =
+ nmem_strdup(stream, zs->u.simple->u.resultSetId);
+ return ZEBRA_FAIL;
+ }
+ rset_dup(rset);
+ }
+ else
+ {
+ zh->errCode = YAZ_BIB1_UNSUPP_SEARCH;
+ return ZEBRA_FAIL;
+ }
+ *num_result_sets = 1;
+ *result_sets = nmem_malloc(stream, *num_result_sets *
+ sizeof(**result_sets));
+ (*result_sets)[0] = rset;
+ }
+ else
+ {
+ zh->errCode = YAZ_BIB1_UNSUPP_SEARCH;
+ return ZEBRA_FAIL;
+ }
+ return ZEBRA_OK;
+}
+
+struct scan_info_entry {
+ char *term;
+ ISAM_P isam_p;
+};
+
+struct scan_info {
+ struct scan_info_entry *list;
+ ODR odr;
+ int before, after;
+ char prefix[20];
+};
+
+static int scan_handle (char *name, const char *info, int pos, void *client)
+{
+ int len_prefix, idx;
+ struct scan_info *scan_info = (struct scan_info *) client;
+
+ len_prefix = strlen(scan_info->prefix);
+ if (memcmp (name, scan_info->prefix, len_prefix))
+ return 1;
+ if (pos > 0)
+ idx = scan_info->after - pos + scan_info->before;
+ else
+ idx = - pos - 1;
+
+ if (idx < 0)
+ return 0;
+ scan_info->list[idx].term = (char *)
+ odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
+ strcpy(scan_info->list[idx].term, name + len_prefix);
+ assert (*info == sizeof(ISAM_P));
+ memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));