+#define ZEBRA_XML_HEADER_STR "<record xmlns=\"http://www.indexdata.com/zebra/\""
+
+struct special_fetch_s {
+ ZebraHandle zh;
+ const char *setname;
+ zint sysno;
+ int score;
+ NMEM nmem;
+};
+
+static int zebra_create_record_stream(ZebraHandle zh,
+ Record *rec,
+ struct ZebraRecStream *stream)
+{
+ RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, *rec);
+
+ if ((*rec)->size[recInfo_storeData] > 0
+ || (*rec)->info[recInfo_filename] == 0)
+ zebra_create_stream_mem(stream, (*rec)->info[recInfo_storeData],
+ (*rec)->size[recInfo_storeData]);
+ else
+ {
+ char full_rep[1024];
+ int fd;
+
+ if (zh->path_reg && !yaz_is_abspath((*rec)->info[recInfo_filename])){
+ strcpy(full_rep, zh->path_reg);
+ strcat(full_rep, "/");
+ strcat(full_rep, (*rec)->info[recInfo_filename]);
+ }
+ else
+ strcpy(full_rep, (*rec)->info[recInfo_filename]);
+
+ if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1){
+ yaz_log(YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
+ full_rep);
+ rec_free(rec);
+ return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+ }
+ zebra_create_stream_fd(stream, fd, recordAttr->recordOffset);
+ }
+ return 0;
+}
+
+
+struct index_spec {
+ const char *index_name;
+ const char *index_type;
+ const char *extra;
+ struct index_spec *next;
+};
+
+
+struct index_spec *parse_index_spec(const char *elem, NMEM nmem,
+ int *error)
+{
+ struct index_spec *first = 0;
+ struct index_spec **last = &first;
+ const char *cp = elem;
+
+ *error = 0;
+ if (cp[0] == ':' && cp[1] == ':')
+ {
+
+ cp++; /* skip first ':' */
+
+ for (;;)
+ {
+ const char *cp0;
+ struct index_spec *spec = nmem_malloc(nmem, sizeof(*spec));
+ spec->index_type = 0;
+ spec->next = 0;
+ spec->extra = 0;
+
+ if (!first)
+ first = spec;
+ *last = spec;
+ last = &spec->next;
+
+ cp++; /* skip ',' or second ':' */
+ cp0 = cp;
+ while (*cp != ':' && *cp != '\0' && *cp != ',')
+ cp++;
+ spec->index_name = nmem_strdupn(nmem, cp0, cp - cp0);
+ if (*cp == ':') /* type as well */
+ {
+ cp++;
+ cp0 = cp;
+
+ while (*cp != '\0' && *cp != ',' && *cp != ':')
+ cp++;
+ spec->index_type = nmem_strdupn(nmem, cp0, cp - cp0);
+ }
+ if (*cp == ':') /* extra arguments */
+ {
+ cp++;
+ cp0 = cp;
+
+ while (*cp != '\0' && *cp != ',' && *cp != ':')
+ cp++;
+ spec->extra = nmem_strdupn(nmem, cp0, cp - cp0);
+ }
+ if (*cp != ',')
+ break;
+ }
+ }
+ if (*cp != '\0')
+ *error = 1;
+ return first;
+}
+
+static int sort_fetch(
+ struct special_fetch_s *fi, const char *elemsetname,
+ const Odr_oid *input_format,
+ const Odr_oid **output_format,
+ WRBUF result, WRBUF addinfo)
+{
+ 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;
+}
+
+
+static void retrieve_puts_attr(WRBUF wrbuf, const char *name,
+ const char *value)
+{
+ if (value)
+ {
+ wrbuf_printf(wrbuf, " %s=\"", name);
+ wrbuf_xmlputs(wrbuf, value);
+ wrbuf_printf(wrbuf, "\"");
+ }
+}
+
+static void retrieve_puts_attr_int(WRBUF wrbuf, const char *name,
+ const int value)
+{
+ wrbuf_printf(wrbuf, " %s=\"%i\"", name, value);
+}
+
+static void retrieve_puts_str(WRBUF wrbuf, const char *name,
+ const char *value)
+{
+ if (value)
+ wrbuf_printf(wrbuf, "%s %s\n", name, value);
+}
+
+static void retrieve_puts_int(WRBUF wrbuf, const char *name,
+ const int value)
+{
+ wrbuf_printf(wrbuf, "%s %i\n", name, value);
+}
+
+
+static void snippet_check_fields(ZebraHandle zh, WRBUF wrbuf,
+ zebra_snippets *doc,
+ const zebra_snippet_word *doc_w,
+ const char *w_index_type)
+{
+ /* beginning of snippet. See which fields the snippet also
+ occur */
+ const zebra_snippet_word *w;
+ int no = 0;
+ for (w = zebra_snippets_constlist(doc); w; w = w->next)
+ {
+ /* same sequence but other field? */
+ if (w->seqno == doc_w->seqno && w->ord != doc_w->ord)
+ {
+ const char *index_type;
+ const char *db = 0;
+ const char *string_index = 0;
+
+ zebraExplain_lookup_ord(zh->reg->zei, w->ord,
+ &index_type, &db, &string_index);
+ /* only report for same index type */
+ if (!strcmp(w_index_type, index_type))
+ {
+ if (no == 0)
+ wrbuf_printf(wrbuf, " fields=\"%s", string_index);
+ else
+ wrbuf_printf(wrbuf, " %s", string_index);
+ no++;
+ }
+ }
+ }
+ if (no)
+ wrbuf_printf(wrbuf, "\"");
+}
+
+static void snippet_xml_record(ZebraHandle zh, WRBUF wrbuf, zebra_snippets *doc)
+{
+ const zebra_snippet_word *doc_w;
+ int mark_state = 0;
+
+ wrbuf_printf(wrbuf, "%s>\n", ZEBRA_XML_HEADER_STR);
+ for (doc_w = zebra_snippets_constlist(doc); doc_w; doc_w = doc_w->next)
+ {
+ if (doc_w->mark)
+ {
+ const char *index_type;
+ const char *db = 0;
+ const char *string_index = 0;
+
+ zebraExplain_lookup_ord(zh->reg->zei, doc_w->ord,
+ &index_type, &db, &string_index);
+
+ if (mark_state == 0)
+ {
+
+ wrbuf_printf(wrbuf, " <snippet name=\"%s\"", string_index);
+ wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
+ snippet_check_fields(zh, wrbuf, doc, doc_w, index_type);
+ wrbuf_printf(wrbuf, ">");
+ }
+ if (doc_w->match)
+ wrbuf_puts(wrbuf, "<s>");
+ /* not printing leading ws */
+ if (mark_state || !doc_w->ws || doc_w->match)
+ wrbuf_xmlputs(wrbuf, doc_w->term);
+ if (doc_w->match)
+ wrbuf_puts(wrbuf, "</s>");
+ }
+ else if (mark_state == 1)
+ {
+ wrbuf_puts(wrbuf, "</snippet>\n");
+ }
+ mark_state = doc_w->mark;
+ }
+ if (mark_state == 1)
+ {
+ wrbuf_puts(wrbuf, "</snippet>\n");
+ }
+ wrbuf_printf(wrbuf, "</record>");
+}
+
+int zebra_get_rec_snippets(ZebraHandle zh, zint sysno,
+ zebra_snippets *snippets)
+{
+ int return_code = 0;
+ Record rec = rec_get(zh->reg->records, sysno);
+ if (!rec)
+ {
+ yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
+ return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+ }
+ else
+ {
+ const char *file_type = rec->info[recInfo_fileType];
+ void *recTypeClientData;
+ RecType rt = recType_byName(zh->reg->recTypes, zh->res,
+ file_type, &recTypeClientData);
+
+ if (!rt)
+ return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+ else
+ {
+ struct ZebraRecStream stream;
+ return_code = zebra_create_record_stream(zh, &rec, &stream);
+ if (return_code == 0)
+ {
+ extract_snippet(zh, snippets, &stream,
+ rt, recTypeClientData);
+
+ stream.destroy(&stream);
+ }
+ }
+ rec_free(&rec);
+ }
+ return return_code;
+}
+
+static int snippet_fetch(
+ struct special_fetch_s *fi, const char *elemsetname,
+ const Odr_oid *input_format,
+ const Odr_oid **output_format,
+ WRBUF result, WRBUF addinfo)
+{
+ ZebraHandle zh = fi->zh;
+ zebra_snippets *rec_snippets = zebra_snippets_create();
+ int return_code = zebra_get_rec_snippets(zh, fi->sysno, rec_snippets);
+
+ if (!return_code)
+ {
+ WRBUF wrbuf = result;
+ zebra_snippets *hit_snippet = zebra_snippets_create();
+
+ zebra_snippets_hit_vector(zh, fi->setname, fi->sysno, hit_snippet);
+
+#if 0
+ /* for debugging purposes */
+ yaz_log(YLOG_LOG, "---------------------------");
+ yaz_log(YLOG_LOG, "REC SNIPPET:");
+ zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
+ yaz_log(YLOG_LOG, "---------------------------");
+ yaz_log(YLOG_LOG, "HIT SNIPPET:");
+ zebra_snippets_log(hit_snippet, YLOG_LOG, 1);
+#endif
+
+ zebra_snippets_ring(rec_snippets, hit_snippet, 5, 5);
+
+#if 0
+ yaz_log(YLOG_LOG, "---------------------------");
+ yaz_log(YLOG_LOG, "RING SNIPPET:");
+ zebra_snippets_log(rec_snippets, YLOG_LOG, 1);