+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;
+}
+
+int zebra_special_snippet_fetch(ZebraHandle zh, const char *setname,
+ zint sysno, ODR odr,
+ const char *elemsetname,
+ const Odr_oid *input_format,
+ const Odr_oid **output_format,
+ char **rec_bufp, int *rec_lenp)
+{
+ zebra_snippets *rec_snippets = zebra_snippets_create();
+ int return_code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
+
+ if (!return_code)
+ {
+ WRBUF wrbuf = wrbuf_alloc();
+ zebra_snippets *hit_snippet = zebra_snippets_create();
+
+ zebra_snippets_hit_vector(zh, setname, sysno, hit_snippet);
+
+#if 0
+ /* for debugging purposes */
+ yaz_log(YLOG_LOG, "---------------------------");
+ yaz_log(YLOG_LOG, "REC SNIPPET:");
+ zebra_snippets_log(rec_snippet, 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);
+#endif
+ snippet_xml_record(zh, wrbuf, rec_snippets);
+
+ *output_format = yaz_oid_recsyn_xml;
+
+ if (return_code == 0)
+ {
+ *rec_lenp = wrbuf_len(wrbuf);
+ *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
+ }
+ wrbuf_destroy(wrbuf);
+ zebra_snippets_destroy(hit_snippet);
+ }
+ zebra_snippets_destroy(rec_snippets);
+ return return_code;
+}
+
+int zebra_special_fetch(ZebraHandle zh, const char *setname,
+ zint sysno, int score, ODR odr,
+ const char *elemsetname,
+ const Odr_oid *input_format,
+ const Odr_oid **output_format,
+ char **rec_bufp, int *rec_lenp)
+{
+ Record rec;
+
+ /* set output variables before processing possible error states */
+ /* *rec_lenp = 0; */
+
+
+ if (elemsetname && 0 == strcmp(elemsetname, "snippet"))
+ {
+ return zebra_special_snippet_fetch(zh, setname, sysno, odr,
+ elemsetname + 7,
+ input_format, output_format,
+ rec_bufp, rec_lenp);
+ }
+
+ /* processing zebra::meta::sysno elemset without fetching binary data */
+ if (elemsetname && 0 == strcmp(elemsetname, "meta::sysno"))
+ {
+ int ret = 0;
+ WRBUF wrbuf = wrbuf_alloc();
+ if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
+ {
+ wrbuf_printf(wrbuf, ZINT_FORMAT, sysno);
+ *output_format = input_format;
+ }
+ else if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
+ {
+ wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
+ " sysno=\"" ZINT_FORMAT "\"/>\n",
+ sysno);
+ *output_format = input_format;
+ }
+ *rec_lenp = wrbuf_len(wrbuf);
+ if (*rec_lenp)
+ *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
+ else
+ ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
+ wrbuf_destroy(wrbuf);
+ return ret;
+ }
+
+ /* processing special elementsetname zebra::index:: for sort elements */
+ if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
+ {
+ int ret = zebra_special_sort_fetch(zh, sysno, odr,
+ elemsetname + 5,
+ input_format, output_format,
+ rec_bufp, rec_lenp);
+ if (ret != -1)
+ return ret;
+ /* not a sort index so we continue to get the full record */
+ }
+
+
+ /* fetching binary record up for all other display elementsets */
+ rec = rec_get(zh->reg->records, sysno);
+ if (!rec)
+ {
+ yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
+ return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+ }
+
+ /* processing special elementsetnames zebra::data */
+ if (elemsetname && 0 == strcmp(elemsetname, "data"))
+ {
+ struct ZebraRecStream stream;
+ RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
+ zebra_create_record_stream(zh, &rec, &stream);
+ *output_format = input_format;
+ *rec_lenp = recordAttr->recordSize;
+ *rec_bufp = (char *) odr_malloc(odr, *rec_lenp);
+ stream.readf(&stream, *rec_bufp, *rec_lenp);
+ stream.destroy(&stream);
+ rec_free(&rec);
+ return 0;
+ }
+
+ /* only accept XML and SUTRS requests from now */
+ 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);
+ return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
+ }
+
+
+ /* processing special elementsetnames zebra::meta:: */
+ if (elemsetname && 0 == strcmp(elemsetname, "meta"))
+ {
+ int ret = 0;
+ WRBUF wrbuf = wrbuf_alloc();
+ RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
+
+ if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
+ {
+ *output_format = input_format;
+
+ wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
+ " sysno=\"" ZINT_FORMAT "\"", sysno);
+ retrieve_puts_attr(wrbuf, "base", rec->info[recInfo_databaseName]);
+ retrieve_puts_attr(wrbuf, "file", rec->info[recInfo_filename]);
+ retrieve_puts_attr(wrbuf, "type", rec->info[recInfo_fileType]);
+ if (score >= 0)
+ retrieve_puts_attr_int(wrbuf, "score", score);
+
+ wrbuf_printf(wrbuf,
+ " rank=\"" ZINT_FORMAT "\""
+ " size=\"%i\""
+ " set=\"zebra::%s\"/>\n",
+ recordAttr->staticrank,
+ recordAttr->recordSize,
+ elemsetname);
+ }
+ else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
+ {
+ *output_format = input_format;
+ wrbuf_printf(wrbuf, "sysno " ZINT_FORMAT "\n", sysno);
+ retrieve_puts_str(wrbuf, "base", rec->info[recInfo_databaseName]);
+ retrieve_puts_str(wrbuf, "file", rec->info[recInfo_filename]);
+ retrieve_puts_str(wrbuf, "type", rec->info[recInfo_fileType]);
+ if (score >= 0)
+ retrieve_puts_int(wrbuf, "score", score);
+
+ wrbuf_printf(wrbuf,
+ "rank " ZINT_FORMAT "\n"
+ "size %i\n"
+ "set zebra::%s\n",
+ recordAttr->staticrank,
+ recordAttr->recordSize,
+ elemsetname);
+ }
+ *rec_lenp = wrbuf_len(wrbuf);
+ if (*rec_lenp)
+ *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
+ else
+ ret = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+
+ wrbuf_destroy(wrbuf);
+ rec_free(&rec);
+ return ret;
+ }
+
+ /* processing special elementsetnames zebra::index:: */
+ if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
+ {
+ int ret = zebra_special_index_fetch(zh, sysno, odr, rec,
+ elemsetname + 5,
+ input_format, output_format,
+ rec_bufp, rec_lenp);
+
+ rec_free(&rec);
+ return ret;
+ }
+
+ if (rec)
+ rec_free(&rec);
+ return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
+}
+
+
+int zebra_record_fetch(ZebraHandle zh, const char *setname,
+ zint sysno, int score,
+ zebra_snippets *hit_snippet, ODR odr,
+ const Odr_oid *input_format, Z_RecordComposition *comp,
+ const Odr_oid **output_format,
+ char **rec_bufp, int *rec_lenp, char **basenamep,
+ char **addinfo)