+ int ord;
+ ZebraHandle zh = fi->zh;
+ int error;
+ struct index_spec *spec;
+
+ spec = parse_index_spec(elemsetname, fi->nmem, &error);
+ if (error)
+ return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
+
+ /* for sort fetches.. We allow only one index and type must be given */
+ if (!spec || spec->next || !spec->index_type)
+ return -1;
+ ord = zebraExplain_lookup_attr_str(zh->reg->zei,
+ zinfo_index_category_sort,
+ spec->index_type,
+ spec->index_name);
+ if (ord == -1)
+ return -1; /* is not a sort index */
+ else
+ {
+ WRBUF wrbuf_str = wrbuf_alloc();
+ const char *index_type;
+ const char *db = 0;
+ const char *string_index = 0;
+ WRBUF wrbuf_result = result;
+ int off = 0;
+
+ zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
+ &string_index);
+ if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
+ {
+ *output_format = yaz_oid_recsyn_xml;
+ wrbuf_printf(wrbuf_result, ZEBRA_XML_HEADER_STR
+ " sysno=\"" ZINT_FORMAT "\""
+ " set=\"zebra::index%s\">\n",
+ fi->sysno, elemsetname);
+ }
+ else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
+ {
+ *output_format = yaz_oid_recsyn_sutrs;
+ }
+ else
+ {
+ yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
+ elemsetname);
+ *output_format = 0;
+ wrbuf_destroy(wrbuf_str);
+ return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
+ }
+ zebra_sort_type(zh->reg->sort_index, ord);
+ zebra_sort_sysno(zh->reg->sort_index, fi->sysno);
+ zebra_sort_read(zh->reg->sort_index, 0, wrbuf_str);
+
+ while (off != wrbuf_len(wrbuf_str))
+ {
+ char dst_buf[IT_MAX_WORD];
+ assert(off < wrbuf_len(wrbuf_str));
+ zebra_term_untrans(zh, index_type, dst_buf,
+ wrbuf_buf(wrbuf_str)+off);
+
+ if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
+ {
+ wrbuf_printf(wrbuf_result, " <index name=\"%s\"",
+ string_index);
+ wrbuf_printf(wrbuf_result, " type=\"%s\">", index_type);
+ wrbuf_xmlputs(wrbuf_result, dst_buf);
+ wrbuf_printf(wrbuf_result, "</index>\n");
+ }
+ else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
+ {
+ wrbuf_printf(wrbuf_result, "%s %s %s\n", string_index, index_type,
+ dst_buf);
+ }
+ off += strlen(wrbuf_buf(wrbuf_str)+off) + 1;
+ }
+ if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
+ {
+ wrbuf_printf(wrbuf_result, "</record>\n");
+ }
+ wrbuf_destroy(wrbuf_str);
+ return 0;
+ }
+}
+
+static int special_index_fetch(
+ struct special_fetch_s *fi, const char *elemsetname,
+ const Odr_oid *input_format,
+ const Odr_oid **output_format,
+ WRBUF result, WRBUF addinfo,
+ Record rec)
+{
+ zebra_rec_keys_t keys;
+ int ret_code = 0;
+ ZebraHandle zh = fi->zh;
+ struct index_spec *spec, *spec_list;
+ int error;
+
+ /* set output variables before processing possible error states */
+ /* *rec_lenp = 0; */
+
+ /* only accept XML and SUTRS requests */
+ if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
+ && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
+ {
+ yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
+ elemsetname);
+ *output_format = 0;
+ return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
+ }
+
+ spec_list = parse_index_spec(elemsetname, fi->nmem, &error);
+ if (error)
+ {
+ return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
+ }
+
+ for (spec = spec_list; spec; spec = spec->next)
+ {
+ if (zebraExplain_lookup_attr_str(zh->reg->zei,
+ zinfo_index_category_index,
+ spec->index_type,
+ spec->index_name) == -1)
+ return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
+ }
+
+ keys = zebra_rec_keys_open();
+ zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
+ rec->size[recInfo_delKeys], 0);
+
+ if (!zebra_rec_keys_rewind(keys))
+ {
+ ret_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+ }
+ else
+ {
+ size_t slen;
+ const char *str;
+ struct it_key key_in;
+ WRBUF wrbuf = result;
+
+ if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
+ {
+ *output_format = input_format;
+ wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
+ " sysno=\"" ZINT_FORMAT "\""
+ " set=\"zebra::index%s\">\n",
+ fi->sysno, elemsetname);
+ }
+ else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
+ *output_format = input_format;
+
+ while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
+ {
+ int i;
+ int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
+ const char *index_type;
+ const char *db = 0;
+ const char *string_index = 0;
+ char dst_buf[IT_MAX_WORD];
+ int match = 0;
+
+ zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
+ &string_index);
+ if (!spec_list)
+ match = 1; /* match all if no specs were given */
+ else
+ {
+ for (spec = spec_list; spec; spec = spec->next)
+ {
+ if ((!spec->index_type ||
+ !yaz_matchstr(spec->index_type, index_type))
+ &&
+ !yaz_matchstr(spec->index_name, string_index))
+ match = 1;
+ }
+ }
+ if (match)
+ {
+ if (zebra_term_untrans(zh, index_type, dst_buf, str))
+ *dst_buf = '\0'; /* untrans failed */
+
+ if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
+ {
+ wrbuf_printf(wrbuf, " <index name=\"%s\"",
+ string_index);
+
+ wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
+
+ wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">",
+ key_in.mem[key_in.len -1]);
+ wrbuf_xmlputs(wrbuf, dst_buf);
+ wrbuf_printf(wrbuf, "</index>\n");
+ }
+ else
+ {
+ wrbuf_printf(wrbuf, "%s ", string_index);
+
+ wrbuf_printf(wrbuf, "%s", index_type);
+
+ for (i = 1; i < key_in.len; i++)
+ wrbuf_printf(wrbuf, " " ZINT_FORMAT,
+ key_in.mem[i]);
+
+ wrbuf_printf(wrbuf, " %s", dst_buf);
+
+ wrbuf_printf(wrbuf, "\n");
+ }
+ }
+ }
+ if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
+ wrbuf_printf(wrbuf, "</record>\n");
+ }
+ zebra_rec_keys_close(keys);
+ return ret_code;