1 /* $Id: retrieve.c,v 1.79 2007-12-03 13:34:17 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <yaz/diagbib1.h>
37 #include <yaz/snprintf.h>
39 #include <yaz/oid_db.h>
40 #include <zebra_strmap.h>
42 #define MAX_SYSNOS_PER_RECORD 40
44 #define ZEBRA_XML_HEADER_STR "<record xmlns=\"http://www.indexdata.com/zebra/\""
46 static int zebra_create_record_stream(ZebraHandle zh,
48 struct ZebraRecStream *stream)
50 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, *rec);
52 if ((*rec)->size[recInfo_storeData] > 0)
53 zebra_create_stream_mem(stream, (*rec)->info[recInfo_storeData],
54 (*rec)->size[recInfo_storeData]);
60 if (zh->path_reg && !yaz_is_abspath((*rec)->info[recInfo_filename])){
61 strcpy(full_rep, zh->path_reg);
62 strcat(full_rep, "/");
63 strcat(full_rep, (*rec)->info[recInfo_filename]);
66 strcpy(full_rep, (*rec)->info[recInfo_filename]);
68 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1){
69 yaz_log(YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
72 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
74 zebra_create_stream_fd(stream, fd, recordAttr->recordOffset);
81 const char *index_name;
82 const char *index_type;
83 struct index_spec *next;
87 struct index_spec *parse_index_spec(const char *elem, NMEM nmem,
90 struct index_spec *first = 0;
91 struct index_spec **last = &first;
92 const char *cp = elem;
95 if (cp[0] == ':' && cp[1] == ':')
98 cp++; /* skip first ':' */
103 struct index_spec *spec = nmem_malloc(nmem, sizeof(*spec));
104 spec->index_type = 0;
112 cp++; /* skip ',' or second ':' */
114 while (*cp != ':' && *cp != '\0' && *cp != ',')
116 spec->index_name = nmem_strdupn(nmem, cp0, cp - cp0);
117 if (*cp == ':') /* type as well */
122 while (*cp != '\0' && *cp != ',')
124 spec->index_type = nmem_strdupn(nmem, cp0, cp - cp0);
135 static int parse_zebra_elem(const char *elem,
136 const char **index, size_t *index_len,
137 const char **type, size_t *type_len)
148 /* verify that '::' is in the beginning of *elem
149 and something more follows */
151 || !(elem +1) || ':' != *(elem +1)
152 || !(elem +2) || '\0' == *(elem +2))
155 /* pick out info from string after '::' */
157 cp = strchr(elem, ':');
159 if (!cp) /* index, no colon, no type */
162 *index_len = strlen(elem);
164 else if (cp[1] == '\0') /* colon, but no following type */
168 else /* index, colon and type */
171 *index_len = cp - elem;
173 *type_len = strlen(cp+1);
180 int zebra_special_sort_fetch(ZebraHandle zh, zint sysno, ODR odr,
181 const char *elemsetname,
182 const Odr_oid *input_format,
183 const Odr_oid **output_format,
184 char **rec_bufp, int *rec_lenp)
186 const char *retrieval_index;
187 size_t retrieval_index_len;
188 const char *retrieval_type;
189 size_t retrieval_type_len;
190 char retrieval_index_cstr[256];
191 char retrieval_type_cstr[256];
194 /* only accept XML and SUTRS requests */
195 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
196 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
198 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
201 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
204 if (!parse_zebra_elem(elemsetname,
205 &retrieval_index, &retrieval_index_len,
206 &retrieval_type, &retrieval_type_len))
208 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
211 if (retrieval_type_len == 0)
212 return -1; /* must have a register type specified */
213 if (!retrieval_index_len ||
214 retrieval_index_len >= sizeof(retrieval_index_cstr)-1)
216 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
219 memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
220 retrieval_index_cstr[retrieval_index_len] = '\0';
222 memcpy(retrieval_type_cstr, retrieval_type, retrieval_type_len);
223 retrieval_type_cstr[retrieval_type_len] = '\0';
225 ord = zebraExplain_lookup_attr_str(zh->reg->zei,
226 zinfo_index_category_sort,
228 retrieval_index_cstr);
230 return -1; /* is not a sort index */
233 char dst_buf[IT_MAX_WORD];
234 char str[IT_MAX_WORD];
235 const char *index_type;
237 const char *string_index = 0;
238 WRBUF wrbuf = wrbuf_alloc();
240 zebra_sort_sysno(zh->reg->sort_index, sysno);
241 zebra_sort_type(zh->reg->sort_index, ord);
242 zebra_sort_read(zh->reg->sort_index, str);
244 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, &string_index);
246 zebra_term_untrans(zh, index_type, dst_buf, str);
248 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
250 *output_format = yaz_oid_recsyn_xml;
251 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
252 " sysno=\"" ZINT_FORMAT "\""
253 " set=\"zebra::index%s/\">\n",
256 wrbuf_printf(wrbuf, " <index name=\"%s\"",
258 wrbuf_printf(wrbuf, " type=\"%s\">", index_type);
259 wrbuf_xmlputs(wrbuf, dst_buf);
260 wrbuf_printf(wrbuf, "</index>\n");
261 wrbuf_printf(wrbuf, "</record>\n");
263 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
265 *output_format = yaz_oid_recsyn_sutrs;
267 wrbuf_printf(wrbuf, "%s %s %s\n", string_index, index_type,
270 *rec_lenp = wrbuf_len(wrbuf);
271 *rec_bufp = odr_malloc(odr, *rec_lenp);
272 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
273 wrbuf_destroy(wrbuf);
278 int zebra_special_index_fetch(ZebraHandle zh, zint sysno, ODR odr,
280 const char *elemsetname,
281 const Odr_oid *input_format,
282 const Odr_oid **output_format,
283 char **rec_bufp, int *rec_lenp)
285 const char *retrieval_index;
286 size_t retrieval_index_len;
287 const char *retrieval_type;
288 size_t retrieval_type_len;
289 zebra_rec_keys_t keys;
291 char retrieval_type_cstr[256];
293 /* set output variables before processing possible error states */
296 /* only accept XML and SUTRS requests */
297 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
298 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
300 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
303 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
306 if (!parse_zebra_elem(elemsetname,
307 &retrieval_index, &retrieval_index_len,
308 &retrieval_type, &retrieval_type_len))
309 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
311 if (retrieval_type_len)
313 memcpy(retrieval_type_cstr, retrieval_type, retrieval_type_len);
314 retrieval_type_cstr[retrieval_type_len] = '\0';
317 if (retrieval_index_len)
319 char retrieval_index_cstr[256];
321 if (retrieval_index_len < sizeof(retrieval_index_cstr) -1)
323 memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
324 retrieval_index_cstr[retrieval_index_len] = '\0';
326 if (zebraExplain_lookup_attr_str(zh->reg->zei,
327 zinfo_index_category_index,
328 (retrieval_type_len == 0 ? 0 :
329 retrieval_type_cstr),
330 retrieval_index_cstr) == -1)
331 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
335 keys = zebra_rec_keys_open();
336 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
337 rec->size[recInfo_delKeys], 0);
339 if (!zebra_rec_keys_rewind(keys))
341 ret_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
347 struct it_key key_in;
348 WRBUF wrbuf = wrbuf_alloc();
350 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
352 *output_format = input_format;
353 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
354 " sysno=\"" ZINT_FORMAT "\""
355 " set=\"zebra::index%s/\">\n",
358 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
359 *output_format = input_format;
361 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
364 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
365 const char *index_type;
367 const char *string_index = 0;
368 size_t string_index_len;
369 char dst_buf[IT_MAX_WORD];
371 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
373 string_index_len = strlen(string_index);
375 /* process only if index is not defined,
376 or if defined and matching */
377 if (retrieval_index == 0
378 || (string_index_len == retrieval_index_len
379 && !memcmp(string_index, retrieval_index,
382 /* process only if type is not defined, or is matching */
383 if (retrieval_type == 0
384 || !strcmp(retrieval_type_cstr, index_type))
386 zebra_term_untrans(zh, index_type, dst_buf, str);
389 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
391 wrbuf_printf(wrbuf, " <index name=\"%s\"",
394 wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
396 wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">",
397 key_in.mem[key_in.len -1]);
399 wrbuf_xmlputs(wrbuf, dst_buf);
400 wrbuf_printf(wrbuf, "</index>\n");
404 wrbuf_printf(wrbuf, "%s ", string_index);
406 wrbuf_printf(wrbuf, "%s", index_type);
408 for (i = 1; i < key_in.len; i++)
409 wrbuf_printf(wrbuf, " " ZINT_FORMAT,
412 wrbuf_printf(wrbuf, " %s", dst_buf);
414 wrbuf_printf(wrbuf, "\n");
421 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
422 wrbuf_printf(wrbuf, "</record>\n");
423 *rec_lenp = wrbuf_len(wrbuf);
424 *rec_bufp = odr_malloc(odr, *rec_lenp);
425 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
426 wrbuf_destroy(wrbuf);
428 zebra_rec_keys_close(keys);
433 static void retrieve_puts_attr(WRBUF wrbuf, const char *name,
438 wrbuf_printf(wrbuf, " %s=\"", name);
439 wrbuf_xmlputs(wrbuf, value);
440 wrbuf_printf(wrbuf, "\"");
444 static void retrieve_puts_attr_int(WRBUF wrbuf, const char *name,
447 wrbuf_printf(wrbuf, " %s=\"%i\"", name, value);
450 static void retrieve_puts_str(WRBUF wrbuf, const char *name,
454 wrbuf_printf(wrbuf, "%s %s\n", name, value);
457 static void retrieve_puts_int(WRBUF wrbuf, const char *name,
460 wrbuf_printf(wrbuf, "%s %i\n", name, value);
464 static void snippet_xml_record(ZebraHandle zh, WRBUF wrbuf, zebra_snippets *doc)
466 const zebra_snippet_word *doc_w;
469 wrbuf_printf(wrbuf, "%s>\n", ZEBRA_XML_HEADER_STR);
470 for (doc_w = zebra_snippets_constlist(doc); doc_w; doc_w = doc_w->next)
474 const char *index_type;
476 const char *string_index = 0;
478 zebraExplain_lookup_ord(zh->reg->zei, doc_w->ord,
479 &index_type, &db, &string_index);
483 wrbuf_printf(wrbuf, " <snippet name=\"%s\"", string_index);
484 wrbuf_printf(wrbuf, " type=\"%s\">", index_type);
487 wrbuf_puts(wrbuf, "<s>");
488 /* not printing leading ws */
489 if (mark_state || !doc_w->ws || doc_w->match)
490 wrbuf_xmlputs(wrbuf, doc_w->term);
492 wrbuf_puts(wrbuf, "</s>");
494 else if (mark_state == 1)
496 wrbuf_puts(wrbuf, "</snippet>\n");
498 mark_state = doc_w->mark;
502 wrbuf_puts(wrbuf, "</snippet>\n");
504 wrbuf_printf(wrbuf, "</record>");
507 int zebra_get_rec_snippets(ZebraHandle zh, zint sysno,
508 zebra_snippets *snippets)
511 Record rec = rec_get(zh->reg->records, sysno);
514 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
515 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
519 const char *file_type = rec->info[recInfo_fileType];
520 void *recTypeClientData;
521 RecType rt = recType_byName(zh->reg->recTypes, zh->res,
522 file_type, &recTypeClientData);
525 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
528 struct ZebraRecStream stream;
529 return_code = zebra_create_record_stream(zh, &rec, &stream);
530 if (return_code == 0)
532 extract_snippet(zh, snippets, &stream,
533 rt, recTypeClientData);
535 stream.destroy(&stream);
543 static int snippet_fetch(ZebraHandle zh, const char *setname,
545 const char *elemsetname,
546 const Odr_oid *input_format,
547 const Odr_oid **output_format,
548 char **rec_bufp, int *rec_lenp)
550 zebra_snippets *rec_snippets = zebra_snippets_create();
551 int return_code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
555 WRBUF wrbuf = wrbuf_alloc();
556 zebra_snippets *hit_snippet = zebra_snippets_create();
558 zebra_snippets_hit_vector(zh, setname, sysno, hit_snippet);
561 /* for debugging purposes */
562 yaz_log(YLOG_LOG, "---------------------------");
563 yaz_log(YLOG_LOG, "REC SNIPPET:");
564 zebra_snippets_log(rec_snippet, YLOG_LOG, 1);
565 yaz_log(YLOG_LOG, "---------------------------");
566 yaz_log(YLOG_LOG, "HIT SNIPPET:");
567 zebra_snippets_log(hit_snippet, YLOG_LOG, 1);
570 zebra_snippets_ring(rec_snippets, hit_snippet, 5, 5);
573 yaz_log(YLOG_LOG, "---------------------------");
574 yaz_log(YLOG_LOG, "RING SNIPPET:");
575 zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
577 snippet_xml_record(zh, wrbuf, rec_snippets);
579 *output_format = yaz_oid_recsyn_xml;
581 if (return_code == 0)
583 *rec_lenp = wrbuf_len(wrbuf);
584 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
586 wrbuf_destroy(wrbuf);
587 zebra_snippets_destroy(hit_snippet);
589 zebra_snippets_destroy(rec_snippets);
593 struct term_collect {
599 zint freq_term(ZebraHandle zh, int ord, const char *term, RSET rset_set)
601 struct rset_key_control *kc = zebra_key_control_create(zh);
602 char ord_buf[IT_MAX_WORD];
603 int ord_len = key_SU_encode(ord, ord_buf);
606 NMEM nmem = nmem_create();
608 strcpy(ord_buf + ord_len, term);
610 info = dict_lookup(zh->reg->dict, ord_buf);
615 memcpy(&isam_p, info+1, sizeof(ISAM_P));
617 rsets[0] = zebra_create_rset_isam(zh, nmem, kc, kc->scope, isam_p, 0);
618 rsets[1] = rset_dup(rset_set);
620 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
622 zebra_count_set(zh, rset, &hits, zh->approx_limit);
624 rset_delete(rsets[0]);
632 void term_collect_freq(ZebraHandle zh,
633 struct term_collect *col, int no_terms_collect,
637 for (i = 0; i < no_terms_collect; i++)
640 col[i].set_occur = freq_term(zh, ord, col[i].term, rset);
644 struct term_collect *term_collect_create(zebra_strmap_t sm,
645 int no_terms_collect,
652 struct term_collect *col = nmem_malloc(nmem,
653 sizeof *col *no_terms_collect);
655 for (i = 0; i < no_terms_collect; i++)
659 col[i].set_occur = 0;
661 /* iterate over terms and collect the most frequent ones */
662 it = zebra_strmap_it_create(sm);
663 while ((term = zebra_strmap_it_next(it, &data_buf, &data_len)))
665 int oc = *(int*) data_buf;
667 /* insertion may be slow but terms terms will be "infrequent" and
668 thus number of iterations should be small below */
669 while (j < no_terms_collect && oc > col[j].oc)
674 memmove(col, col+1, sizeof(*col) * j);
679 zebra_strmap_it_destroy(it);
683 static ZEBRA_RES facet_fetch(ZebraHandle zh, const char *setname,
685 const char *elemsetname,
686 const Odr_oid *input_format,
687 const Odr_oid **output_format,
688 char **rec_bufp, int *rec_lenp)
692 int num_recs = 10; /* number of records to analyze */
693 int no_collect_terms = 20; /* number of term candidates */
694 ZebraMetaRecord *poset;
695 ZEBRA_RES ret = ZEBRA_OK;
697 WRBUF wr = wrbuf_alloc();
700 struct index_spec *spec, *spec_list;
704 spec_list = parse_index_spec(elemsetname, odr_getmem(odr), &error);
706 if (!spec_list || error)
707 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
709 for (spec = spec_list; spec; spec = spec->next)
711 if (!spec->index_type)
712 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
716 ord_array = odr_malloc(odr, sizeof(*ord_array) * no_ord);
718 for (spec = spec_list, i = 0; spec; spec = spec->next, i++)
720 int ord = zebraExplain_lookup_attr_str(zh->reg->zei,
721 zinfo_index_category_index,
726 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
731 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
732 for (i = 0; i < num_recs; i++)
734 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
737 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
744 zebra_strmap_t *map_array
745 = odr_malloc(odr, sizeof *map_array * no_ord);
746 for (i = 0; i < no_ord; i++)
747 map_array[i] = zebra_strmap_create();
749 for (i = 0; i < num_recs; i++)
752 zint sysnos[MAX_SYSNOS_PER_RECORD];
753 int no_sysnos = MAX_SYSNOS_PER_RECORD;
756 ret = zebra_result_recid_to_sysno(zh, setname,
759 assert(no_sysnos > 0);
760 for (j = 0; j < no_sysnos; j++)
764 struct it_key key_in;
765 Record rec = rec_get(zh->reg->records, sysnos[j]);
766 zebra_rec_keys_t keys = zebra_rec_keys_open();
767 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
768 rec->size[recInfo_delKeys], 0);
770 if (zebra_rec_keys_rewind(keys))
772 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
775 struct index_spec *spec;
776 for (spec = spec_list, i = 0; i < no_ord;
777 i++, spec = spec->next)
779 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
780 if (ord == ord_array[i] &&
781 str[0] != FIRST_IN_FIELD_CHAR)
784 zebra_strmap_t sm = map_array[i];
786 freq = zebra_strmap_lookup(sm, str, 0, 0);
792 zebra_strmap_add(sm, str, &v, sizeof v);
798 zebra_rec_keys_close(keys);
802 wrbuf_puts(wr, "<facets>\n");
803 for (spec = spec_list, i = 0; i < no_ord; i++, spec = spec->next)
806 NMEM nmem = nmem_create();
807 struct term_collect *col = term_collect_create(map_array[i],
810 term_collect_freq(zh, col, no_collect_terms, ord_array[i],
811 resultSetRef(zh, setname));
813 wrbuf_printf(wr, " <facet type=\"%s\" index=\"%s\">\n",
814 spec->index_type, spec->index_name);
815 for (j = 0; j < no_collect_terms; j++)
819 char dst_buf[IT_MAX_WORD];
820 zebra_term_untrans(zh, spec->index_type, dst_buf, col[j].term);
821 wrbuf_printf(wr, " <term coccur=\"%d\"", col[j].oc);
822 if (col[j].set_occur)
823 wrbuf_printf(wr, " occur=\"" ZINT_FORMAT "\"",
825 wrbuf_printf(wr, ">");
826 wrbuf_xmlputs(wr, dst_buf);
827 wrbuf_printf(wr, "</term>\n");
830 wrbuf_puts(wr, " </facet>\n");
833 wrbuf_puts(wr, "</facets>\n");
834 for (i = 0; i < no_ord; i++)
835 zebra_strmap_destroy(map_array[i]);
839 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wr));
841 *rec_lenp = strlen(*rec_bufp);
842 *output_format = yaz_oid_recsyn_xml;
845 zebra_meta_records_destroy(zh, poset, num_recs);
849 int zebra_special_fetch(ZebraHandle zh, const char *setname,
850 zint sysno, int score, ODR odr,
851 const char *elemsetname,
852 const Odr_oid *input_format,
853 const Odr_oid **output_format,
854 char **rec_bufp, int *rec_lenp)
858 /* set output variables before processing possible error states */
861 if (elemsetname && 0 == strncmp(elemsetname, "facet", 5))
863 return facet_fetch(zh, setname, odr,
865 input_format, output_format,
869 if (elemsetname && 0 == strcmp(elemsetname, "snippet"))
871 return snippet_fetch(zh, setname, sysno, odr,
873 input_format, output_format,
877 /* processing zebra::meta::sysno elemset without fetching binary data */
878 if (elemsetname && 0 == strcmp(elemsetname, "meta::sysno"))
881 WRBUF wrbuf = wrbuf_alloc();
882 if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
884 wrbuf_printf(wrbuf, ZINT_FORMAT, sysno);
885 *output_format = input_format;
887 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
889 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
890 " sysno=\"" ZINT_FORMAT "\"/>\n",
892 *output_format = input_format;
894 *rec_lenp = wrbuf_len(wrbuf);
896 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
898 ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
899 wrbuf_destroy(wrbuf);
903 /* processing special elementsetname zebra::index:: for sort elements */
904 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
906 int ret = zebra_special_sort_fetch(zh, sysno, odr,
908 input_format, output_format,
912 /* not a sort index so we continue to get the full record */
916 /* fetching binary record up for all other display elementsets */
917 rec = rec_get(zh->reg->records, sysno);
920 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
921 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
924 /* processing special elementsetnames zebra::data */
925 if (elemsetname && 0 == strcmp(elemsetname, "data"))
927 struct ZebraRecStream stream;
928 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
929 zebra_create_record_stream(zh, &rec, &stream);
930 *output_format = input_format;
931 *rec_lenp = recordAttr->recordSize;
932 *rec_bufp = (char *) odr_malloc(odr, *rec_lenp);
933 stream.readf(&stream, *rec_bufp, *rec_lenp);
934 stream.destroy(&stream);
939 /* only accept XML and SUTRS requests from now */
940 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
941 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
943 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
945 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
949 /* processing special elementsetnames zebra::meta:: */
950 if (elemsetname && 0 == strcmp(elemsetname, "meta"))
953 WRBUF wrbuf = wrbuf_alloc();
954 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
956 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
958 *output_format = input_format;
960 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
961 " sysno=\"" ZINT_FORMAT "\"", sysno);
962 retrieve_puts_attr(wrbuf, "base", rec->info[recInfo_databaseName]);
963 retrieve_puts_attr(wrbuf, "file", rec->info[recInfo_filename]);
964 retrieve_puts_attr(wrbuf, "type", rec->info[recInfo_fileType]);
966 retrieve_puts_attr_int(wrbuf, "score", score);
969 " rank=\"" ZINT_FORMAT "\""
971 " set=\"zebra::%s\"/>\n",
972 recordAttr->staticrank,
973 recordAttr->recordSize,
976 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
978 *output_format = input_format;
979 wrbuf_printf(wrbuf, "sysno " ZINT_FORMAT "\n", sysno);
980 retrieve_puts_str(wrbuf, "base", rec->info[recInfo_databaseName]);
981 retrieve_puts_str(wrbuf, "file", rec->info[recInfo_filename]);
982 retrieve_puts_str(wrbuf, "type", rec->info[recInfo_fileType]);
984 retrieve_puts_int(wrbuf, "score", score);
987 "rank " ZINT_FORMAT "\n"
990 recordAttr->staticrank,
991 recordAttr->recordSize,
994 *rec_lenp = wrbuf_len(wrbuf);
996 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
998 ret = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1000 wrbuf_destroy(wrbuf);
1005 /* processing special elementsetnames zebra::index:: */
1006 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
1008 int ret = zebra_special_index_fetch(zh, sysno, odr, rec,
1010 input_format, output_format,
1011 rec_bufp, rec_lenp);
1019 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1023 int zebra_record_fetch(ZebraHandle zh, const char *setname,
1024 zint sysno, int score,
1026 const Odr_oid *input_format, Z_RecordComposition *comp,
1027 const Odr_oid **output_format,
1028 char **rec_bufp, int *rec_lenp, char **basenamep,
1032 char *fname, *file_type, *basename;
1033 const char *elemsetname;
1034 struct ZebraRecStream stream;
1035 RecordAttr *recordAttr;
1037 int return_code = 0;
1038 zint sysnos[MAX_SYSNOS_PER_RECORD];
1039 int no_sysnos = MAX_SYSNOS_PER_RECORD;
1042 res = zebra_result_recid_to_sysno(zh, setname, sysno, sysnos, &no_sysnos);
1043 if (res != ZEBRA_OK)
1049 elemsetname = yaz_get_esn(comp);
1051 /* processing zebra special elementset names of form 'zebra:: */
1052 if (elemsetname && 0 == strncmp(elemsetname, "zebra::", 7))
1053 return zebra_special_fetch(zh, setname, sysno, score, odr,
1055 input_format, output_format,
1056 rec_bufp, rec_lenp);
1059 /* processing all other element set names */
1060 rec = rec_get(zh->reg->records, sysno);
1063 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
1065 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1069 recordAttr = rec_init_attr(zh->reg->zei, rec);
1071 file_type = rec->info[recInfo_fileType];
1072 fname = rec->info[recInfo_filename];
1073 basename = rec->info[recInfo_databaseName];
1074 *basenamep = (char *) odr_malloc(odr, strlen(basename)+1);
1075 strcpy(*basenamep, basename);
1077 yaz_log(YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
1080 return_code = zebra_create_record_stream(zh, &rec, &stream);
1085 struct recRetrieveCtrl retrieveCtrl;
1087 retrieveCtrl.stream = &stream;
1088 retrieveCtrl.fname = fname;
1089 retrieveCtrl.localno = sysno;
1090 retrieveCtrl.staticrank = recordAttr->staticrank;
1091 retrieveCtrl.score = score;
1092 retrieveCtrl.recordSize = recordAttr->recordSize;
1093 retrieveCtrl.odr = odr;
1094 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
1095 retrieveCtrl.comp = comp;
1096 retrieveCtrl.encoding = zh->record_encoding;
1097 retrieveCtrl.diagnostic = 0;
1098 retrieveCtrl.addinfo = 0;
1099 retrieveCtrl.dh = zh->reg->dh;
1100 retrieveCtrl.res = zh->res;
1101 retrieveCtrl.rec_buf = 0;
1102 retrieveCtrl.rec_len = -1;
1104 if (!(rt = recType_byName(zh->reg->recTypes, zh->res,
1105 file_type, &clientData)))
1107 char addinfo_str[100];
1109 sprintf(addinfo_str, "Could not handle record type %.40s",
1112 *addinfo = odr_strdup(odr, addinfo_str);
1113 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1117 (*rt->retrieve)(clientData, &retrieveCtrl);
1118 return_code = retrieveCtrl.diagnostic;
1120 *output_format = retrieveCtrl.output_format;
1121 *rec_bufp = (char *) retrieveCtrl.rec_buf;
1122 *rec_lenp = retrieveCtrl.rec_len;
1123 *addinfo = retrieveCtrl.addinfo;
1126 stream.destroy(&stream);
1136 * indent-tabs-mode: nil
1138 * vim: shiftwidth=4 tabstop=8 expandtab