1 /* $Id: retrieve.c,v 1.82 2007-12-05 09:55:57 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;
84 struct index_spec *next;
88 struct index_spec *parse_index_spec(const char *elem, NMEM nmem,
91 struct index_spec *first = 0;
92 struct index_spec **last = &first;
93 const char *cp = elem;
96 if (cp[0] == ':' && cp[1] == ':')
99 cp++; /* skip first ':' */
104 struct index_spec *spec = nmem_malloc(nmem, sizeof(*spec));
105 spec->index_type = 0;
114 cp++; /* skip ',' or second ':' */
116 while (*cp != ':' && *cp != '\0' && *cp != ',')
118 spec->index_name = nmem_strdupn(nmem, cp0, cp - cp0);
119 if (*cp == ':') /* type as well */
124 while (*cp != '\0' && *cp != ',' && *cp != ':')
126 spec->index_type = nmem_strdupn(nmem, cp0, cp - cp0);
128 if (*cp == ':') /* extra arguments */
133 while (*cp != '\0' && *cp != ',' && *cp != ':')
135 spec->extra = nmem_strdupn(nmem, cp0, cp - cp0);
146 static int parse_zebra_elem(const char *elem,
147 const char **index, size_t *index_len,
148 const char **type, size_t *type_len)
159 /* verify that '::' is in the beginning of *elem
160 and something more follows */
162 || !(elem +1) || ':' != *(elem +1)
163 || !(elem +2) || '\0' == *(elem +2))
166 /* pick out info from string after '::' */
168 cp = strchr(elem, ':');
170 if (!cp) /* index, no colon, no type */
173 *index_len = strlen(elem);
175 else if (cp[1] == '\0') /* colon, but no following type */
179 else /* index, colon and type */
182 *index_len = cp - elem;
184 *type_len = strlen(cp+1);
191 int zebra_special_sort_fetch(ZebraHandle zh, zint sysno, ODR odr,
192 const char *elemsetname,
193 const Odr_oid *input_format,
194 const Odr_oid **output_format,
195 char **rec_bufp, int *rec_lenp)
197 const char *retrieval_index;
198 size_t retrieval_index_len;
199 const char *retrieval_type;
200 size_t retrieval_type_len;
201 char retrieval_index_cstr[256];
202 char retrieval_type_cstr[256];
205 /* only accept XML and SUTRS requests */
206 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
207 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
209 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
212 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
215 if (!parse_zebra_elem(elemsetname,
216 &retrieval_index, &retrieval_index_len,
217 &retrieval_type, &retrieval_type_len))
219 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
222 if (retrieval_type_len == 0)
223 return -1; /* must have a register type specified */
224 if (!retrieval_index_len ||
225 retrieval_index_len >= sizeof(retrieval_index_cstr)-1)
227 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
230 memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
231 retrieval_index_cstr[retrieval_index_len] = '\0';
233 memcpy(retrieval_type_cstr, retrieval_type, retrieval_type_len);
234 retrieval_type_cstr[retrieval_type_len] = '\0';
236 ord = zebraExplain_lookup_attr_str(zh->reg->zei,
237 zinfo_index_category_sort,
239 retrieval_index_cstr);
241 return -1; /* is not a sort index */
244 char dst_buf[IT_MAX_WORD];
245 char str[IT_MAX_WORD];
246 const char *index_type;
248 const char *string_index = 0;
249 WRBUF wrbuf = wrbuf_alloc();
251 zebra_sort_sysno(zh->reg->sort_index, sysno);
252 zebra_sort_type(zh->reg->sort_index, ord);
253 zebra_sort_read(zh->reg->sort_index, str);
255 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, &string_index);
257 zebra_term_untrans(zh, index_type, dst_buf, str);
259 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
261 *output_format = yaz_oid_recsyn_xml;
262 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
263 " sysno=\"" ZINT_FORMAT "\""
264 " set=\"zebra::index%s/\">\n",
267 wrbuf_printf(wrbuf, " <index name=\"%s\"",
269 wrbuf_printf(wrbuf, " type=\"%s\">", index_type);
270 wrbuf_xmlputs(wrbuf, dst_buf);
271 wrbuf_printf(wrbuf, "</index>\n");
272 wrbuf_printf(wrbuf, "</record>\n");
274 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
276 *output_format = yaz_oid_recsyn_sutrs;
278 wrbuf_printf(wrbuf, "%s %s %s\n", string_index, index_type,
281 *rec_lenp = wrbuf_len(wrbuf);
282 *rec_bufp = odr_malloc(odr, *rec_lenp);
283 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
284 wrbuf_destroy(wrbuf);
289 int zebra_special_index_fetch(ZebraHandle zh, zint sysno, ODR odr,
291 const char *elemsetname,
292 const Odr_oid *input_format,
293 const Odr_oid **output_format,
294 char **rec_bufp, int *rec_lenp)
296 const char *retrieval_index;
297 size_t retrieval_index_len;
298 const char *retrieval_type;
299 size_t retrieval_type_len;
300 zebra_rec_keys_t keys;
302 char retrieval_type_cstr[256];
304 /* set output variables before processing possible error states */
307 /* only accept XML and SUTRS requests */
308 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
309 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
311 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
314 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
317 if (!parse_zebra_elem(elemsetname,
318 &retrieval_index, &retrieval_index_len,
319 &retrieval_type, &retrieval_type_len))
320 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
322 if (retrieval_type_len)
324 memcpy(retrieval_type_cstr, retrieval_type, retrieval_type_len);
325 retrieval_type_cstr[retrieval_type_len] = '\0';
328 if (retrieval_index_len)
330 char retrieval_index_cstr[256];
332 if (retrieval_index_len < sizeof(retrieval_index_cstr) -1)
334 memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
335 retrieval_index_cstr[retrieval_index_len] = '\0';
337 if (zebraExplain_lookup_attr_str(zh->reg->zei,
338 zinfo_index_category_index,
339 (retrieval_type_len == 0 ? 0 :
340 retrieval_type_cstr),
341 retrieval_index_cstr) == -1)
342 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
346 keys = zebra_rec_keys_open();
347 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
348 rec->size[recInfo_delKeys], 0);
350 if (!zebra_rec_keys_rewind(keys))
352 ret_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
358 struct it_key key_in;
359 WRBUF wrbuf = wrbuf_alloc();
361 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
363 *output_format = input_format;
364 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
365 " sysno=\"" ZINT_FORMAT "\""
366 " set=\"zebra::index%s/\">\n",
369 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
370 *output_format = input_format;
372 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
375 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
376 const char *index_type;
378 const char *string_index = 0;
379 size_t string_index_len;
380 char dst_buf[IT_MAX_WORD];
382 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
384 string_index_len = strlen(string_index);
386 /* process only if index is not defined,
387 or if defined and matching */
388 if (retrieval_index == 0
389 || (string_index_len == retrieval_index_len
390 && !memcmp(string_index, retrieval_index,
393 /* process only if type is not defined, or is matching */
394 if (retrieval_type == 0
395 || !strcmp(retrieval_type_cstr, index_type))
397 zebra_term_untrans(zh, index_type, dst_buf, str);
400 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
402 wrbuf_printf(wrbuf, " <index name=\"%s\"",
405 wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
407 wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">",
408 key_in.mem[key_in.len -1]);
410 wrbuf_xmlputs(wrbuf, dst_buf);
411 wrbuf_printf(wrbuf, "</index>\n");
415 wrbuf_printf(wrbuf, "%s ", string_index);
417 wrbuf_printf(wrbuf, "%s", index_type);
419 for (i = 1; i < key_in.len; i++)
420 wrbuf_printf(wrbuf, " " ZINT_FORMAT,
423 wrbuf_printf(wrbuf, " %s", dst_buf);
425 wrbuf_printf(wrbuf, "\n");
432 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
433 wrbuf_printf(wrbuf, "</record>\n");
434 *rec_lenp = wrbuf_len(wrbuf);
435 *rec_bufp = odr_malloc(odr, *rec_lenp);
436 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
437 wrbuf_destroy(wrbuf);
439 zebra_rec_keys_close(keys);
444 static void retrieve_puts_attr(WRBUF wrbuf, const char *name,
449 wrbuf_printf(wrbuf, " %s=\"", name);
450 wrbuf_xmlputs(wrbuf, value);
451 wrbuf_printf(wrbuf, "\"");
455 static void retrieve_puts_attr_int(WRBUF wrbuf, const char *name,
458 wrbuf_printf(wrbuf, " %s=\"%i\"", name, value);
461 static void retrieve_puts_str(WRBUF wrbuf, const char *name,
465 wrbuf_printf(wrbuf, "%s %s\n", name, value);
468 static void retrieve_puts_int(WRBUF wrbuf, const char *name,
471 wrbuf_printf(wrbuf, "%s %i\n", name, value);
475 static void snippet_xml_record(ZebraHandle zh, WRBUF wrbuf, zebra_snippets *doc)
477 const zebra_snippet_word *doc_w;
480 wrbuf_printf(wrbuf, "%s>\n", ZEBRA_XML_HEADER_STR);
481 for (doc_w = zebra_snippets_constlist(doc); doc_w; doc_w = doc_w->next)
485 const char *index_type;
487 const char *string_index = 0;
489 zebraExplain_lookup_ord(zh->reg->zei, doc_w->ord,
490 &index_type, &db, &string_index);
494 wrbuf_printf(wrbuf, " <snippet name=\"%s\"", string_index);
495 wrbuf_printf(wrbuf, " type=\"%s\">", index_type);
498 wrbuf_puts(wrbuf, "<s>");
499 /* not printing leading ws */
500 if (mark_state || !doc_w->ws || doc_w->match)
501 wrbuf_xmlputs(wrbuf, doc_w->term);
503 wrbuf_puts(wrbuf, "</s>");
505 else if (mark_state == 1)
507 wrbuf_puts(wrbuf, "</snippet>\n");
509 mark_state = doc_w->mark;
513 wrbuf_puts(wrbuf, "</snippet>\n");
515 wrbuf_printf(wrbuf, "</record>");
518 int zebra_get_rec_snippets(ZebraHandle zh, zint sysno,
519 zebra_snippets *snippets)
522 Record rec = rec_get(zh->reg->records, sysno);
525 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
526 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
530 const char *file_type = rec->info[recInfo_fileType];
531 void *recTypeClientData;
532 RecType rt = recType_byName(zh->reg->recTypes, zh->res,
533 file_type, &recTypeClientData);
536 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
539 struct ZebraRecStream stream;
540 return_code = zebra_create_record_stream(zh, &rec, &stream);
541 if (return_code == 0)
543 extract_snippet(zh, snippets, &stream,
544 rt, recTypeClientData);
546 stream.destroy(&stream);
554 static int snippet_fetch(ZebraHandle zh, const char *setname,
556 const char *elemsetname,
557 const Odr_oid *input_format,
558 const Odr_oid **output_format,
559 char **rec_bufp, int *rec_lenp)
561 zebra_snippets *rec_snippets = zebra_snippets_create();
562 int return_code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
566 WRBUF wrbuf = wrbuf_alloc();
567 zebra_snippets *hit_snippet = zebra_snippets_create();
569 zebra_snippets_hit_vector(zh, setname, sysno, hit_snippet);
572 /* for debugging purposes */
573 yaz_log(YLOG_LOG, "---------------------------");
574 yaz_log(YLOG_LOG, "REC SNIPPET:");
575 zebra_snippets_log(rec_snippet, YLOG_LOG, 1);
576 yaz_log(YLOG_LOG, "---------------------------");
577 yaz_log(YLOG_LOG, "HIT SNIPPET:");
578 zebra_snippets_log(hit_snippet, YLOG_LOG, 1);
581 zebra_snippets_ring(rec_snippets, hit_snippet, 5, 5);
584 yaz_log(YLOG_LOG, "---------------------------");
585 yaz_log(YLOG_LOG, "RING SNIPPET:");
586 zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
588 snippet_xml_record(zh, wrbuf, rec_snippets);
590 *output_format = yaz_oid_recsyn_xml;
592 if (return_code == 0)
594 *rec_lenp = wrbuf_len(wrbuf);
595 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
597 wrbuf_destroy(wrbuf);
598 zebra_snippets_destroy(hit_snippet);
600 zebra_snippets_destroy(rec_snippets);
604 struct term_collect {
610 zint freq_term(ZebraHandle zh, int ord, const char *term, RSET rset_set)
612 struct rset_key_control *kc = zebra_key_control_create(zh);
613 char ord_buf[IT_MAX_WORD];
614 int ord_len = key_SU_encode(ord, ord_buf);
617 NMEM nmem = nmem_create();
619 strcpy(ord_buf + ord_len, term);
621 info = dict_lookup(zh->reg->dict, ord_buf);
626 memcpy(&isam_p, info+1, sizeof(ISAM_P));
628 rsets[0] = zebra_create_rset_isam(zh, nmem, kc, kc->scope, isam_p, 0);
629 rsets[1] = rset_dup(rset_set);
631 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
633 zebra_count_set(zh, rset, &hits, zh->approx_limit);
635 rset_delete(rsets[0]);
643 int term_qsort_handle(const void *a, const void *b)
645 const struct term_collect *l = a;
646 const struct term_collect *r = b;
647 if (l->set_occur < r->set_occur)
649 else if (l->set_occur > r->set_occur)
653 const char *lterm = l->term ? l->term : "";
654 const char *rterm = r->term ? r->term : "";
655 return strcmp(lterm, rterm);
659 void term_collect_freq(ZebraHandle zh,
660 struct term_collect *col, int no_terms_collect,
664 for (i = 0; i < no_terms_collect; i++)
667 col[i].set_occur = freq_term(zh, ord, col[i].term, rset);
669 qsort(col, no_terms_collect, sizeof(*col), term_qsort_handle);
672 struct term_collect *term_collect_create(zebra_strmap_t sm,
673 int no_terms_collect,
680 struct term_collect *col = nmem_malloc(nmem,
681 sizeof *col *no_terms_collect);
683 for (i = 0; i < no_terms_collect; i++)
687 col[i].set_occur = 0;
689 /* iterate over terms and collect the most frequent ones */
690 it = zebra_strmap_it_create(sm);
691 while ((term = zebra_strmap_it_next(it, &data_buf, &data_len)))
694 col[0] has lowest oc . col[no_terms_collect-1] has highest oc */
695 int oc = *(int*) data_buf;
697 /* insertion may be slow but terms terms will be "infrequent" and
698 thus number of iterations should be small below
700 while (j < no_terms_collect && oc > col[j].oc)
703 { /* oc <= col[j] and oc > col[j-1] */
705 memmove(col, col+1, sizeof(*col) * j);
710 zebra_strmap_it_destroy(it);
714 static ZEBRA_RES facet_fetch(ZebraHandle zh, const char *setname,
716 const char *elemsetname,
717 const Odr_oid *input_format,
718 const Odr_oid **output_format,
719 char **rec_bufp, int *rec_lenp)
723 int num_recs = 10; /* number of records to analyze */
724 ZebraMetaRecord *poset;
725 ZEBRA_RES ret = ZEBRA_OK;
727 WRBUF wr = wrbuf_alloc();
731 struct index_spec *spec, *spec_list;
734 /* see if XML is required for response */
735 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml) == 0)
738 spec_list = parse_index_spec(elemsetname, odr_getmem(odr), &error);
740 if (!spec_list || error)
744 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_,
749 for (spec = spec_list; spec; spec = spec->next)
751 if (!spec->index_type)
755 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_,
762 ord_array = odr_malloc(odr, sizeof(*ord_array) * no_ord);
764 for (spec = spec_list, i = 0; spec; spec = spec->next, i++)
766 int ord = zebraExplain_lookup_attr_str(zh->reg->zei,
767 zinfo_index_category_index,
774 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_,
780 pos_array = (zint *) odr_malloc(odr, num_recs * sizeof(*pos_array));
781 for (i = 0; i < num_recs; i++)
783 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
786 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
792 zebra_strmap_t *map_array
793 = odr_malloc(odr, sizeof *map_array * no_ord);
794 for (i = 0; i < no_ord; i++)
795 map_array[i] = zebra_strmap_create();
797 for (i = 0; i < num_recs; i++)
800 zint sysnos[MAX_SYSNOS_PER_RECORD];
801 int no_sysnos = MAX_SYSNOS_PER_RECORD;
804 ret = zebra_result_recid_to_sysno(zh, setname,
807 assert(no_sysnos > 0);
808 for (j = 0; j < no_sysnos; j++)
812 struct it_key key_in;
813 Record rec = rec_get(zh->reg->records, sysnos[j]);
814 zebra_rec_keys_t keys = zebra_rec_keys_open();
815 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
816 rec->size[recInfo_delKeys], 0);
818 if (zebra_rec_keys_rewind(keys))
820 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
823 struct index_spec *spec;
824 for (spec = spec_list, i = 0; i < no_ord;
825 i++, spec = spec->next)
827 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
828 if (ord == ord_array[i] &&
829 str[0] != FIRST_IN_FIELD_CHAR)
832 zebra_strmap_t sm = map_array[i];
834 freq = zebra_strmap_lookup(sm, str, 0, 0);
840 zebra_strmap_add(sm, str, &v, sizeof v);
846 zebra_rec_keys_close(keys);
851 wrbuf_puts(wr, "<facets>\n");
852 for (spec = spec_list, i = 0; i < no_ord; i++, spec = spec->next)
855 NMEM nmem = nmem_create();
856 struct term_collect *col;
857 int no_collect_terms = 20;
860 no_collect_terms = atoi(spec->extra);
861 if (no_collect_terms < 1)
862 no_collect_terms = 1;
863 col = term_collect_create(map_array[i], no_collect_terms, nmem);
864 term_collect_freq(zh, col, no_collect_terms, ord_array[i],
865 resultSetRef(zh, setname));
868 wrbuf_printf(wr, " <facet type=\"%s\" index=\"%s\">\n",
869 spec->index_type, spec->index_name);
871 wrbuf_printf(wr, "facet %s %s\n",
872 spec->index_type, spec->index_name);
873 for (j = 0; j < no_collect_terms; j++)
877 char dst_buf[IT_MAX_WORD];
878 zebra_term_untrans(zh, spec->index_type, dst_buf, col[j].term);
881 wrbuf_printf(wr, " <term coccur=\"%d\"", col[j].oc);
882 if (col[j].set_occur)
883 wrbuf_printf(wr, " occur=\"" ZINT_FORMAT "\"",
885 wrbuf_printf(wr, ">");
886 wrbuf_xmlputs(wr, dst_buf);
887 wrbuf_printf(wr, "</term>\n");
891 wrbuf_printf(wr, "term %d", col[j].oc);
892 if (col[j].set_occur)
893 wrbuf_printf(wr, " " ZINT_FORMAT,
895 wrbuf_printf(wr, ": %s\n", dst_buf);
900 wrbuf_puts(wr, " </facet>\n");
904 wrbuf_puts(wr, "</facets>\n");
905 for (i = 0; i < no_ord; i++)
906 zebra_strmap_destroy(map_array[i]);
910 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wr));
912 *rec_lenp = strlen(*rec_bufp);
913 *output_format = yaz_oid_recsyn_xml;
915 zebra_meta_records_destroy(zh, poset, num_recs);
919 int zebra_special_fetch(ZebraHandle zh, const char *setname,
920 zint sysno, int score, ODR odr,
921 const char *elemsetname,
922 const Odr_oid *input_format,
923 const Odr_oid **output_format,
924 char **rec_bufp, int *rec_lenp)
928 /* set output variables before processing possible error states */
931 if (elemsetname && 0 == strncmp(elemsetname, "facet", 5))
933 return facet_fetch(zh, setname, odr,
935 input_format, output_format,
939 if (elemsetname && 0 == strcmp(elemsetname, "snippet"))
941 return snippet_fetch(zh, setname, sysno, odr,
943 input_format, output_format,
947 /* processing zebra::meta::sysno elemset without fetching binary data */
948 if (elemsetname && 0 == strcmp(elemsetname, "meta::sysno"))
951 WRBUF wrbuf = wrbuf_alloc();
952 if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
954 wrbuf_printf(wrbuf, ZINT_FORMAT, sysno);
955 *output_format = input_format;
957 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
959 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
960 " sysno=\"" ZINT_FORMAT "\"/>\n",
962 *output_format = input_format;
964 *rec_lenp = wrbuf_len(wrbuf);
966 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
968 ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
969 wrbuf_destroy(wrbuf);
973 /* processing special elementsetname zebra::index:: for sort elements */
974 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
976 int ret = zebra_special_sort_fetch(zh, sysno, odr,
978 input_format, output_format,
982 /* not a sort index so we continue to get the full record */
986 /* fetching binary record up for all other display elementsets */
987 rec = rec_get(zh->reg->records, sysno);
990 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
991 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
994 /* processing special elementsetnames zebra::data */
995 if (elemsetname && 0 == strcmp(elemsetname, "data"))
997 struct ZebraRecStream stream;
998 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
999 zebra_create_record_stream(zh, &rec, &stream);
1000 *output_format = input_format;
1001 *rec_lenp = recordAttr->recordSize;
1002 *rec_bufp = (char *) odr_malloc(odr, *rec_lenp);
1003 stream.readf(&stream, *rec_bufp, *rec_lenp);
1004 stream.destroy(&stream);
1009 /* only accept XML and SUTRS requests from now */
1010 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
1011 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
1013 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
1015 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
1019 /* processing special elementsetnames zebra::meta:: */
1020 if (elemsetname && 0 == strcmp(elemsetname, "meta"))
1023 WRBUF wrbuf = wrbuf_alloc();
1024 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
1026 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
1028 *output_format = input_format;
1030 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
1031 " sysno=\"" ZINT_FORMAT "\"", sysno);
1032 retrieve_puts_attr(wrbuf, "base", rec->info[recInfo_databaseName]);
1033 retrieve_puts_attr(wrbuf, "file", rec->info[recInfo_filename]);
1034 retrieve_puts_attr(wrbuf, "type", rec->info[recInfo_fileType]);
1036 retrieve_puts_attr_int(wrbuf, "score", score);
1039 " rank=\"" ZINT_FORMAT "\""
1041 " set=\"zebra::%s\"/>\n",
1042 recordAttr->staticrank,
1043 recordAttr->recordSize,
1046 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
1048 *output_format = input_format;
1049 wrbuf_printf(wrbuf, "sysno " ZINT_FORMAT "\n", sysno);
1050 retrieve_puts_str(wrbuf, "base", rec->info[recInfo_databaseName]);
1051 retrieve_puts_str(wrbuf, "file", rec->info[recInfo_filename]);
1052 retrieve_puts_str(wrbuf, "type", rec->info[recInfo_fileType]);
1054 retrieve_puts_int(wrbuf, "score", score);
1057 "rank " ZINT_FORMAT "\n"
1060 recordAttr->staticrank,
1061 recordAttr->recordSize,
1064 *rec_lenp = wrbuf_len(wrbuf);
1066 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
1068 ret = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1070 wrbuf_destroy(wrbuf);
1075 /* processing special elementsetnames zebra::index:: */
1076 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
1078 int ret = zebra_special_index_fetch(zh, sysno, odr, rec,
1080 input_format, output_format,
1081 rec_bufp, rec_lenp);
1089 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1093 int zebra_record_fetch(ZebraHandle zh, const char *setname,
1094 zint sysno, int score,
1096 const Odr_oid *input_format, Z_RecordComposition *comp,
1097 const Odr_oid **output_format,
1098 char **rec_bufp, int *rec_lenp, char **basenamep,
1102 char *fname, *file_type, *basename;
1103 const char *elemsetname;
1104 struct ZebraRecStream stream;
1105 RecordAttr *recordAttr;
1107 int return_code = 0;
1108 zint sysnos[MAX_SYSNOS_PER_RECORD];
1109 int no_sysnos = MAX_SYSNOS_PER_RECORD;
1112 res = zebra_result_recid_to_sysno(zh, setname, sysno, sysnos, &no_sysnos);
1113 if (res != ZEBRA_OK)
1119 elemsetname = yaz_get_esn(comp);
1121 /* processing zebra special elementset names of form 'zebra:: */
1122 if (elemsetname && 0 == strncmp(elemsetname, "zebra::", 7))
1123 return zebra_special_fetch(zh, setname, sysno, score, odr,
1125 input_format, output_format,
1126 rec_bufp, rec_lenp);
1129 /* processing all other element set names */
1130 rec = rec_get(zh->reg->records, sysno);
1133 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
1135 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1139 recordAttr = rec_init_attr(zh->reg->zei, rec);
1141 file_type = rec->info[recInfo_fileType];
1142 fname = rec->info[recInfo_filename];
1143 basename = rec->info[recInfo_databaseName];
1144 *basenamep = (char *) odr_malloc(odr, strlen(basename)+1);
1145 strcpy(*basenamep, basename);
1147 yaz_log(YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
1150 return_code = zebra_create_record_stream(zh, &rec, &stream);
1155 struct recRetrieveCtrl retrieveCtrl;
1157 retrieveCtrl.stream = &stream;
1158 retrieveCtrl.fname = fname;
1159 retrieveCtrl.localno = sysno;
1160 retrieveCtrl.staticrank = recordAttr->staticrank;
1161 retrieveCtrl.score = score;
1162 retrieveCtrl.recordSize = recordAttr->recordSize;
1163 retrieveCtrl.odr = odr;
1164 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
1165 retrieveCtrl.comp = comp;
1166 retrieveCtrl.encoding = zh->record_encoding;
1167 retrieveCtrl.diagnostic = 0;
1168 retrieveCtrl.addinfo = 0;
1169 retrieveCtrl.dh = zh->reg->dh;
1170 retrieveCtrl.res = zh->res;
1171 retrieveCtrl.rec_buf = 0;
1172 retrieveCtrl.rec_len = -1;
1174 if (!(rt = recType_byName(zh->reg->recTypes, zh->res,
1175 file_type, &clientData)))
1177 char addinfo_str[100];
1179 sprintf(addinfo_str, "Could not handle record type %.40s",
1182 *addinfo = odr_strdup(odr, addinfo_str);
1183 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1187 (*rt->retrieve)(clientData, &retrieveCtrl);
1188 return_code = retrieveCtrl.diagnostic;
1190 *output_format = retrieveCtrl.output_format;
1191 *rec_bufp = (char *) retrieveCtrl.rec_buf;
1192 *rec_lenp = retrieveCtrl.rec_len;
1193 *addinfo = retrieveCtrl.addinfo;
1196 stream.destroy(&stream);
1206 * indent-tabs-mode: nil
1208 * vim: shiftwidth=4 tabstop=8 expandtab