1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <yaz/diagbib1.h>
34 #include <yaz/snprintf.h>
36 #include <yaz/oid_db.h>
37 #include <zebra_strmap.h>
39 #define MAX_SYSNOS_PER_RECORD 40
41 #define ZEBRA_XML_HEADER_STR "<record xmlns=\"http://www.indexdata.com/zebra/\""
43 struct special_fetch_s {
51 static int zebra_create_record_stream(ZebraHandle zh,
53 struct ZebraRecStream *stream)
55 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, *rec);
57 if ((*rec)->size[recInfo_storeData] > 0
58 || (*rec)->info[recInfo_filename] == 0)
59 zebra_create_stream_mem(stream, (*rec)->info[recInfo_storeData],
60 (*rec)->size[recInfo_storeData]);
66 if (zh->path_reg && !yaz_is_abspath((*rec)->info[recInfo_filename])){
67 strcpy(full_rep, zh->path_reg);
68 strcat(full_rep, "/");
69 strcat(full_rep, (*rec)->info[recInfo_filename]);
72 strcpy(full_rep, (*rec)->info[recInfo_filename]);
74 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1){
75 yaz_log(YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
78 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
80 zebra_create_stream_fd(stream, fd, recordAttr->recordOffset);
87 const char *index_name;
88 const char *index_type;
90 struct index_spec *next;
94 struct index_spec *parse_index_spec(const char *elem, NMEM nmem,
97 struct index_spec *first = 0;
98 struct index_spec **last = &first;
99 const char *cp = elem;
102 if (cp[0] == ':' && cp[1] == ':')
105 cp++; /* skip first ':' */
110 struct index_spec *spec = nmem_malloc(nmem, sizeof(*spec));
111 spec->index_type = 0;
120 cp++; /* skip ',' or second ':' */
122 while (*cp != ':' && *cp != '\0' && *cp != ',')
124 spec->index_name = nmem_strdupn(nmem, cp0, cp - cp0);
125 if (*cp == ':') /* type as well */
130 while (*cp != '\0' && *cp != ',' && *cp != ':')
132 spec->index_type = nmem_strdupn(nmem, cp0, cp - cp0);
134 if (*cp == ':') /* extra arguments */
139 while (*cp != '\0' && *cp != ',' && *cp != ':')
141 spec->extra = nmem_strdupn(nmem, cp0, cp - cp0);
152 static int sort_fetch(
153 struct special_fetch_s *fi, const char *elemsetname,
154 const Odr_oid *input_format,
155 const Odr_oid **output_format,
156 WRBUF result, WRBUF addinfo)
159 ZebraHandle zh = fi->zh;
161 struct index_spec *spec;
163 spec = parse_index_spec(elemsetname, fi->nmem, &error);
165 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
167 /* for sort fetches.. We allow only one index and type must be given */
168 if (!spec || spec->next || !spec->index_type)
170 ord = zebraExplain_lookup_attr_str(zh->reg->zei,
171 zinfo_index_category_sort,
175 return -1; /* is not a sort index */
178 WRBUF wrbuf_str = wrbuf_alloc();
179 const char *index_type;
181 const char *string_index = 0;
182 WRBUF wrbuf_result = result;
185 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
187 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
189 *output_format = yaz_oid_recsyn_xml;
190 wrbuf_printf(wrbuf_result, ZEBRA_XML_HEADER_STR
191 " sysno=\"" ZINT_FORMAT "\""
192 " set=\"zebra::index%s\">\n",
193 fi->sysno, elemsetname);
195 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
197 *output_format = yaz_oid_recsyn_sutrs;
201 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
204 wrbuf_destroy(wrbuf_str);
205 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
207 zebra_sort_type(zh->reg->sort_index, ord);
208 zebra_sort_sysno(zh->reg->sort_index, fi->sysno);
209 zebra_sort_read(zh->reg->sort_index, 0, wrbuf_str);
211 while (off != wrbuf_len(wrbuf_str))
213 char dst_buf[IT_MAX_WORD];
214 assert(off < wrbuf_len(wrbuf_str));
215 zebra_term_untrans(zh, index_type, dst_buf,
216 wrbuf_buf(wrbuf_str)+off);
218 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
220 wrbuf_printf(wrbuf_result, " <index name=\"%s\"",
222 wrbuf_printf(wrbuf_result, " type=\"%s\">", index_type);
223 wrbuf_xmlputs(wrbuf_result, dst_buf);
224 wrbuf_printf(wrbuf_result, "</index>\n");
226 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
228 wrbuf_printf(wrbuf_result, "%s %s %s\n", string_index, index_type,
231 off += strlen(wrbuf_buf(wrbuf_str)+off) + 1;
233 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
235 wrbuf_printf(wrbuf_result, "</record>\n");
237 wrbuf_destroy(wrbuf_str);
242 static int special_index_fetch(
243 struct special_fetch_s *fi, const char *elemsetname,
244 const Odr_oid *input_format,
245 const Odr_oid **output_format,
246 WRBUF result, WRBUF addinfo,
249 zebra_rec_keys_t keys;
251 ZebraHandle zh = fi->zh;
252 struct index_spec *spec, *spec_list;
255 /* set output variables before processing possible error states */
258 /* only accept XML and SUTRS requests */
259 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
260 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
262 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
265 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
268 spec_list = parse_index_spec(elemsetname, fi->nmem, &error);
271 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
274 for (spec = spec_list; spec; spec = spec->next)
276 if (zebraExplain_lookup_attr_str(zh->reg->zei,
277 zinfo_index_category_index,
279 spec->index_name) == -1)
280 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
283 keys = zebra_rec_keys_open();
284 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
285 rec->size[recInfo_delKeys], 0);
287 if (!zebra_rec_keys_rewind(keys))
289 ret_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
295 struct it_key key_in;
296 WRBUF wrbuf = result;
298 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
300 *output_format = input_format;
301 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
302 " sysno=\"" ZINT_FORMAT "\""
303 " set=\"zebra::index%s\">\n",
304 fi->sysno, elemsetname);
306 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
307 *output_format = input_format;
309 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
312 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
313 const char *index_type;
315 const char *string_index = 0;
316 char dst_buf[IT_MAX_WORD];
319 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
322 match = 1; /* match all if no specs were given */
325 for (spec = spec_list; spec; spec = spec->next)
327 if ((!spec->index_type ||
328 !yaz_matchstr(spec->index_type, index_type))
330 !yaz_matchstr(spec->index_name, string_index))
336 if (zebra_term_untrans(zh, index_type, dst_buf, str))
337 *dst_buf = '\0'; /* untrans failed */
339 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
341 wrbuf_printf(wrbuf, " <index name=\"%s\"",
344 wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
346 wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">",
347 key_in.mem[key_in.len -1]);
348 wrbuf_xmlputs(wrbuf, dst_buf);
349 wrbuf_printf(wrbuf, "</index>\n");
353 wrbuf_printf(wrbuf, "%s ", string_index);
355 wrbuf_printf(wrbuf, "%s", index_type);
357 for (i = 1; i < key_in.len; i++)
358 wrbuf_printf(wrbuf, " " ZINT_FORMAT,
361 wrbuf_printf(wrbuf, " %s", dst_buf);
363 wrbuf_printf(wrbuf, "\n");
367 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
368 wrbuf_printf(wrbuf, "</record>\n");
370 zebra_rec_keys_close(keys);
375 static void retrieve_puts_attr(WRBUF wrbuf, const char *name,
380 wrbuf_printf(wrbuf, " %s=\"", name);
381 wrbuf_xmlputs(wrbuf, value);
382 wrbuf_printf(wrbuf, "\"");
386 static void retrieve_puts_attr_int(WRBUF wrbuf, const char *name,
389 wrbuf_printf(wrbuf, " %s=\"%i\"", name, value);
392 static void retrieve_puts_str(WRBUF wrbuf, const char *name,
396 wrbuf_printf(wrbuf, "%s %s\n", name, value);
399 static void retrieve_puts_int(WRBUF wrbuf, const char *name,
402 wrbuf_printf(wrbuf, "%s %i\n", name, value);
406 static void snippet_check_fields(ZebraHandle zh, WRBUF wrbuf,
408 const zebra_snippet_word *doc_w,
409 const char *w_index_type)
411 /* beginning of snippet. See which fields the snippet also
413 const zebra_snippet_word *w;
415 for (w = zebra_snippets_constlist(doc); w; w = w->next)
417 /* same sequence but other field? */
418 if (w->seqno == doc_w->seqno && w->ord != doc_w->ord)
420 const char *index_type;
422 const char *string_index = 0;
424 zebraExplain_lookup_ord(zh->reg->zei, w->ord,
425 &index_type, &db, &string_index);
426 /* only report for same index type */
427 if (!strcmp(w_index_type, index_type))
430 wrbuf_printf(wrbuf, " fields=\"%s", string_index);
432 wrbuf_printf(wrbuf, " %s", string_index);
438 wrbuf_printf(wrbuf, "\"");
441 static void snippet_xml_record(ZebraHandle zh, WRBUF wrbuf, zebra_snippets *doc)
443 const zebra_snippet_word *doc_w;
446 wrbuf_printf(wrbuf, "%s>\n", ZEBRA_XML_HEADER_STR);
447 for (doc_w = zebra_snippets_constlist(doc); doc_w; doc_w = doc_w->next)
451 const char *index_type;
453 const char *string_index = 0;
455 zebraExplain_lookup_ord(zh->reg->zei, doc_w->ord,
456 &index_type, &db, &string_index);
461 wrbuf_printf(wrbuf, " <snippet name=\"%s\"", string_index);
462 wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
463 snippet_check_fields(zh, wrbuf, doc, doc_w, index_type);
464 wrbuf_printf(wrbuf, ">");
467 wrbuf_puts(wrbuf, "<s>");
468 /* not printing leading ws */
469 if (mark_state || !doc_w->ws || doc_w->match)
470 wrbuf_xmlputs(wrbuf, doc_w->term);
472 wrbuf_puts(wrbuf, "</s>");
474 else if (mark_state == 1)
476 wrbuf_puts(wrbuf, "</snippet>\n");
478 mark_state = doc_w->mark;
482 wrbuf_puts(wrbuf, "</snippet>\n");
484 wrbuf_printf(wrbuf, "</record>");
487 int zebra_get_rec_snippets(ZebraHandle zh, zint sysno,
488 zebra_snippets *snippets)
491 Record rec = rec_get(zh->reg->records, sysno);
494 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
495 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
499 const char *file_type = rec->info[recInfo_fileType];
500 void *recTypeClientData;
501 RecType rt = recType_byName(zh->reg->recTypes, zh->res,
502 file_type, &recTypeClientData);
505 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
508 struct ZebraRecStream stream;
509 return_code = zebra_create_record_stream(zh, &rec, &stream);
510 if (return_code == 0)
512 extract_snippet(zh, snippets, &stream,
513 rt, recTypeClientData);
515 stream.destroy(&stream);
523 static int snippet_fetch(
524 struct special_fetch_s *fi, const char *elemsetname,
525 const Odr_oid *input_format,
526 const Odr_oid **output_format,
527 WRBUF result, WRBUF addinfo)
529 ZebraHandle zh = fi->zh;
530 zebra_snippets *rec_snippets = zebra_snippets_create();
531 int return_code = zebra_get_rec_snippets(zh, fi->sysno, rec_snippets);
535 WRBUF wrbuf = result;
536 zebra_snippets *hit_snippet = zebra_snippets_create();
538 zebra_snippets_hit_vector(zh, fi->setname, fi->sysno, hit_snippet);
541 /* for debugging purposes */
542 yaz_log(YLOG_LOG, "---------------------------");
543 yaz_log(YLOG_LOG, "REC SNIPPET:");
544 zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
545 yaz_log(YLOG_LOG, "---------------------------");
546 yaz_log(YLOG_LOG, "HIT SNIPPET:");
547 zebra_snippets_log(hit_snippet, YLOG_LOG, 1);
550 zebra_snippets_ring(rec_snippets, hit_snippet, 5, 5);
553 yaz_log(YLOG_LOG, "---------------------------");
554 yaz_log(YLOG_LOG, "RING SNIPPET:");
555 zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
557 snippet_xml_record(zh, wrbuf, rec_snippets);
559 *output_format = yaz_oid_recsyn_xml;
561 zebra_snippets_destroy(hit_snippet);
563 zebra_snippets_destroy(rec_snippets);
567 struct term_collect {
573 static zint freq_term(ZebraHandle zh, int ord, const char *term, RSET rset_set)
575 struct rset_key_control *kc = zebra_key_control_create(zh);
576 char ord_buf[IT_MAX_WORD];
577 int ord_len = key_SU_encode(ord, ord_buf);
580 NMEM nmem = nmem_create();
582 strcpy(ord_buf + ord_len, term);
584 info = dict_lookup(zh->reg->dict, ord_buf);
589 memcpy(&isam_p, info+1, sizeof(ISAM_P));
591 rsets[0] = zebra_create_rset_isam(zh, nmem, kc, kc->scope, isam_p, 0);
592 rsets[1] = rset_dup(rset_set);
594 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
596 zebra_count_set(zh, rset, &hits, zh->approx_limit);
598 rset_delete(rsets[0]);
606 static int term_qsort_handle(const void *a, const void *b)
608 const struct term_collect *l = a;
609 const struct term_collect *r = b;
610 if (l->set_occur < r->set_occur)
612 else if (l->set_occur > r->set_occur)
616 const char *lterm = l->term ? l->term : "";
617 const char *rterm = r->term ? r->term : "";
618 return strcmp(lterm, rterm);
622 static void term_collect_freq(ZebraHandle zh,
623 struct term_collect *col, int no_terms_collect,
624 int ord, RSET rset, double scale_factor)
627 for (i = 0; i < no_terms_collect; i++)
631 if (scale_factor < 0.0)
633 col[i].set_occur = freq_term(zh, ord, col[i].term, rset);
636 col[i].set_occur = scale_factor * col[i].oc;
639 qsort(col, no_terms_collect, sizeof(*col), term_qsort_handle);
642 static struct term_collect *term_collect_create(zebra_strmap_t sm,
643 int no_terms_collect,
650 struct term_collect *col = nmem_malloc(nmem,
651 sizeof *col *no_terms_collect);
653 for (i = 0; i < no_terms_collect; i++)
657 col[i].set_occur = 0;
659 /* iterate over terms and collect the most frequent ones */
660 it = zebra_strmap_it_create(sm);
661 while ((term = zebra_strmap_it_next(it, &data_buf, &data_len)))
664 col[0] has lowest oc . col[no_terms_collect-1] has highest oc */
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
670 while (j < no_terms_collect && oc > col[j].oc)
673 { /* oc <= col[j] and oc > col[j-1] */
675 memmove(col, col+1, sizeof(*col) * j);
680 zebra_strmap_it_destroy(it);
684 static int perform_facet_sort(ZebraHandle zh, int no_ord, int *ord_array,
685 zebra_strmap_t *map_array,
686 int num_recs, ZebraMetaRecord *poset)
689 WRBUF w = wrbuf_alloc();
692 for (ord_i = 0; ord_i < no_ord; ord_i++)
694 for (rec_i = 0; rec_i < num_recs; rec_i++)
696 if (!poset[rec_i].sysno)
699 zebra_sort_sysno(zh->reg->sort_index, poset[rec_i].sysno);
700 zebra_sort_type(zh->reg->sort_index, ord_array[ord_i]);
703 if (zebra_sort_read(zh->reg->sort_index, 0, w))
705 zebra_strmap_t sm = map_array[ord_i];
707 while (off != wrbuf_len(w))
709 const char *str = wrbuf_buf(w) + off;
710 int *freq = zebra_strmap_lookup(sm, str, 0, 0);
716 zebra_strmap_add(sm, str, &v, sizeof v);
718 off += strlen(str)+1;
728 static int perform_facet_index(ZebraHandle zh,
729 struct special_fetch_s *fi,
730 int no_ord, int *ord_array,
731 zebra_strmap_t *map_array,
732 int num_recs, ZebraMetaRecord *poset,
733 struct index_spec *spec_list)
737 res_get_int(zh->res, "facetMaxChunks", &max_chunks);
739 for (rec_i = 0; rec_i < num_recs; rec_i++)
743 zint sysnos[MAX_SYSNOS_PER_RECORD];
744 int no_sysnos = MAX_SYSNOS_PER_RECORD;
745 if (!poset[rec_i].sysno)
747 ret = zebra_result_recid_to_sysno(zh, fi->setname,
750 assert(no_sysnos > 0);
751 yaz_log(YLOG_DEBUG, "Analyzing rec=%d ISAM sysno=" ZINT_FORMAT " chunks=%d",
752 rec_i, poset[rec_i].sysno, no_sysnos);
753 for (j = 0; j < no_sysnos && j < max_chunks; j++)
757 struct it_key key_in;
758 Record rec = rec_get(zh->reg->records, sysnos[j]);
759 zebra_rec_keys_t keys = zebra_rec_keys_open();
760 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
761 rec->size[recInfo_delKeys], 0);
763 yaz_log(YLOG_DEBUG, "rec %d " ZINT_FORMAT " %s",
764 j, sysnos[j], zebra_rec_keys_empty(keys) ? "empty" : "non-empty");
765 if (zebra_rec_keys_rewind(keys))
767 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
770 struct index_spec *spec;
771 for (spec = spec_list, ord_i = 0; ord_i < no_ord;
772 ord_i++, spec = spec->next)
774 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
775 if (ord == ord_array[ord_i] &&
776 str[0] != FIRST_IN_FIELD_CHAR)
779 zebra_strmap_t sm = map_array[ord_i];
781 freq = zebra_strmap_lookup(sm, str, 0, 0);
787 zebra_strmap_add(sm, str, &v, sizeof v);
793 zebra_rec_keys_close(keys);
800 static int perform_facet(ZebraHandle zh,
801 struct special_fetch_s *fi,
803 int num_recs, ZebraMetaRecord *poset,
804 struct index_spec *spec_list,
805 int no_ord, int *ord_array,
807 zinfo_index_category_t cat)
812 struct index_spec *spec;
813 yaz_timing_t timing = yaz_timing_create();
814 zebra_strmap_t *map_array
815 = nmem_malloc(fi->nmem, sizeof *map_array * no_ord);
816 for (i = 0; i < no_ord; i++)
817 map_array[i] = zebra_strmap_create();
819 if (cat == zinfo_index_category_sort)
820 perform_facet_sort(zh, no_ord, ord_array, map_array,
823 perform_facet_index(zh, fi, no_ord, ord_array, map_array,
824 num_recs, poset, spec_list);
825 yaz_timing_stop(timing);
826 yaz_log(YLOG_LOG, "facet first phase real=%4.2f cat=%s",
827 yaz_timing_get_real(timing),
828 (cat == zinfo_index_category_sort) ? "sort" : "index");
829 yaz_timing_start(timing);
830 for (spec = spec_list, i = 0; i < no_ord; i++, spec = spec->next)
833 NMEM nmem = nmem_create();
834 struct term_collect *col;
835 int no_collect_terms = 20;
838 no_collect_terms = atoi(spec->extra);
839 if (no_collect_terms < 1)
840 no_collect_terms = 1;
841 col = term_collect_create(map_array[i], no_collect_terms, nmem);
842 term_collect_freq(zh, col, no_collect_terms, ord_array[i],
843 resultSetRef(zh, fi->setname),
844 cat == zinfo_index_category_sort ? 1.0 : -1.0);
847 wrbuf_printf(wr, " <facet type=\"%s\" index=\"%s\">\n",
848 spec->index_type, spec->index_name);
850 wrbuf_printf(wr, "facet %s %s\n",
851 spec->index_type, spec->index_name);
852 for (j = 0; j < no_collect_terms; j++)
856 char dst_buf[IT_MAX_WORD];
857 zebra_term_untrans(zh, spec->index_type, dst_buf, col[j].term);
860 wrbuf_printf(wr, " <term coccur=\"%d\"", col[j].oc);
861 if (col[j].set_occur)
862 wrbuf_printf(wr, " occur=\"" ZINT_FORMAT "\"",
864 wrbuf_printf(wr, ">");
865 wrbuf_xmlputs(wr, dst_buf);
866 wrbuf_printf(wr, "</term>\n");
870 wrbuf_printf(wr, "term %d", col[j].oc);
871 if (col[j].set_occur)
872 wrbuf_printf(wr, " " ZINT_FORMAT,
874 wrbuf_printf(wr, ": %s\n", dst_buf);
879 wrbuf_puts(wr, " </facet>\n");
882 for (i = 0; i < no_ord; i++)
883 zebra_strmap_destroy(map_array[i]);
884 yaz_timing_stop(timing);
885 yaz_log(YLOG_LOG, "facet second phase real=%4.2f",
886 yaz_timing_get_real(timing));
887 yaz_timing_destroy(&timing);
891 static int facet_fetch(
892 struct special_fetch_s *fi, const char *elemsetname,
893 const Odr_oid *input_format,
894 const Odr_oid **output_format,
895 WRBUF result, WRBUF addinfo)
899 int num_recs = 10; /* number of records to analyze */
900 ZebraMetaRecord *poset;
901 ZEBRA_RES ret = ZEBRA_OK;
905 struct index_spec *spec, *spec_list;
907 ZebraHandle zh = fi->zh;
908 /* whether sort or index based */
909 zinfo_index_category_t cat = zinfo_index_category_sort;
911 /* see if XML is required for response */
912 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml) == 0)
915 spec_list = parse_index_spec(elemsetname, fi->nmem, &error);
917 if (!spec_list || error)
919 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
922 for (spec = spec_list; spec; spec = spec->next)
924 if (!spec->index_type)
925 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
929 /* try to see if all specs are sort based.. If not, try the
931 ord_array = nmem_malloc(fi->nmem, sizeof(*ord_array) * no_ord);
933 for (spec = spec_list, i = 0; spec; spec = spec->next, i++)
935 int ord = zebraExplain_lookup_attr_str(zh->reg->zei,
936 zinfo_index_category_sort,
946 cat = zinfo_index_category_index;
947 for (spec = spec_list, i = 0; spec; spec = spec->next, i++)
949 int ord = zebraExplain_lookup_attr_str(zh->reg->zei,
950 zinfo_index_category_index,
960 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
962 res_get_int(zh->res, "facetNumRecs", &num_recs);
964 pos_array = (zint *) nmem_malloc(fi->nmem, num_recs * sizeof(*pos_array));
965 for (i = 0; i < num_recs; i++)
967 poset = zebra_meta_records_create(zh, fi->setname, num_recs, pos_array);
970 wrbuf_puts(addinfo, fi->setname);
971 return YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
977 wrbuf_printf(result, ZEBRA_XML_HEADER_STR ">\n");
979 ret = perform_facet(zh, fi, result, num_recs, poset,
980 spec_list, no_ord, ord_array, use_xml,
983 wrbuf_puts(result, "</record>\n");
985 *output_format = yaz_oid_recsyn_xml;
986 zebra_meta_records_destroy(zh, poset, num_recs);
991 static int zebra_special_fetch(
992 void *handle, const char *elemsetname,
993 const Odr_oid *input_format,
994 const Odr_oid **output_format,
995 WRBUF result, WRBUF addinfo)
998 struct special_fetch_s *fi = (struct special_fetch_s *) handle;
999 ZebraHandle zh = fi->zh;
1000 zint sysno = fi->sysno;
1002 /* processing zebra::facet */
1003 if (elemsetname && 0 == strncmp(elemsetname, "facet", 5))
1005 return facet_fetch(fi, elemsetname + 5,
1006 input_format, output_format,
1010 if (elemsetname && 0 == strcmp(elemsetname, "snippet"))
1012 return snippet_fetch(fi, elemsetname + 7,
1013 input_format, output_format,
1017 /* processing zebra::meta::sysno */
1018 if (elemsetname && 0 == strcmp(elemsetname, "meta::sysno"))
1021 if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
1023 wrbuf_printf(result, ZINT_FORMAT, fi->sysno);
1024 *output_format = input_format;
1026 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
1028 wrbuf_printf(result, ZEBRA_XML_HEADER_STR
1029 " sysno=\"" ZINT_FORMAT "\"/>\n",
1031 *output_format = input_format;
1034 ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
1038 /* processing special elementsetname zebra::index:: for sort elements */
1039 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
1041 int ret = sort_fetch(
1042 fi, elemsetname + 5,
1043 input_format, output_format,
1047 /* not a sort index so we continue to get the full record */
1051 /* fetching binary record up for all other display elementsets */
1052 rec = rec_get(zh->reg->records, sysno);
1055 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
1056 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1059 /* processing special elementsetnames zebra::data */
1060 if (elemsetname && 0 == strcmp(elemsetname, "data"))
1062 struct ZebraRecStream stream;
1063 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
1066 zebra_create_record_stream(zh, &rec, &stream);
1067 *output_format = input_format;
1069 b = nmem_malloc(fi->nmem, recordAttr->recordSize);
1070 stream.readf(&stream, b, recordAttr->recordSize);
1071 wrbuf_write(result, b, recordAttr->recordSize);
1073 stream.destroy(&stream);
1078 /* processing special elementsetnames zebra::meta:: */
1079 if (elemsetname && 0 == strcmp(elemsetname, "meta"))
1082 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
1084 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
1086 *output_format = input_format;
1088 wrbuf_printf(result, ZEBRA_XML_HEADER_STR
1089 " sysno=\"" ZINT_FORMAT "\"", sysno);
1090 retrieve_puts_attr(result, "base", rec->info[recInfo_databaseName]);
1091 retrieve_puts_attr(result, "file", rec->info[recInfo_filename]);
1092 retrieve_puts_attr(result, "type", rec->info[recInfo_fileType]);
1094 retrieve_puts_attr_int(result, "score", fi->score);
1096 wrbuf_printf(result,
1097 " rank=\"" ZINT_FORMAT "\""
1099 " set=\"zebra::%s\"/>\n",
1100 recordAttr->staticrank,
1101 recordAttr->recordSize,
1104 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
1106 *output_format = input_format;
1107 wrbuf_printf(result, "sysno " ZINT_FORMAT "\n", sysno);
1108 retrieve_puts_str(result, "base", rec->info[recInfo_databaseName]);
1109 retrieve_puts_str(result, "file", rec->info[recInfo_filename]);
1110 retrieve_puts_str(result, "type", rec->info[recInfo_fileType]);
1112 retrieve_puts_int(result, "score", fi->score);
1114 wrbuf_printf(result,
1115 "rank " ZINT_FORMAT "\n"
1118 recordAttr->staticrank,
1119 recordAttr->recordSize,
1123 ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
1129 /* processing special elementsetnames zebra::index:: */
1130 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
1132 int ret = special_index_fetch(
1133 fi, elemsetname + 5,
1134 input_format, output_format,
1135 result, addinfo, rec);
1142 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1145 int zebra_record_fetch(ZebraHandle zh, const char *setname,
1146 zint sysno, int score,
1148 const Odr_oid *input_format, Z_RecordComposition *comp,
1149 const Odr_oid **output_format,
1150 char **rec_bufp, int *rec_lenp, char **basenamep,
1154 char *fname, *file_type, *basename;
1155 const char *elemsetname;
1156 struct ZebraRecStream stream;
1157 RecordAttr *recordAttr;
1159 int return_code = 0;
1160 zint sysnos[MAX_SYSNOS_PER_RECORD];
1161 int no_sysnos = MAX_SYSNOS_PER_RECORD;
1163 struct special_fetch_s fetch_info;
1165 res = zebra_result_recid_to_sysno(zh, setname, sysno, sysnos, &no_sysnos);
1166 if (res != ZEBRA_OK)
1171 elemsetname = yaz_get_esn(comp);
1174 fetch_info.setname = setname;
1175 fetch_info.sysno = sysno;
1176 fetch_info.score = score;
1177 fetch_info.nmem = odr->mem;
1179 /* processing zebra special elementset names of form 'zebra:: */
1180 if (elemsetname && 0 == strncmp(elemsetname, "zebra::", 7))
1182 WRBUF result = wrbuf_alloc();
1183 int r = zebra_special_fetch(&fetch_info, elemsetname + 7,
1184 input_format, output_format,
1188 *rec_bufp = odr_strdup(odr, wrbuf_cstr(result));
1189 *rec_lenp = wrbuf_len(result);
1191 wrbuf_destroy(result);
1195 /* processing all other element set names */
1196 rec = rec_get(zh->reg->records, sysno);
1199 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
1201 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1205 recordAttr = rec_init_attr(zh->reg->zei, rec);
1207 file_type = rec->info[recInfo_fileType];
1208 fname = rec->info[recInfo_filename];
1209 basename = rec->info[recInfo_databaseName];
1210 *basenamep = (char *) odr_malloc(odr, strlen(basename)+1);
1211 strcpy(*basenamep, basename);
1213 yaz_log(YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
1216 return_code = zebra_create_record_stream(zh, &rec, &stream);
1221 struct recRetrieveCtrl retrieveCtrl;
1223 retrieveCtrl.stream = &stream;
1224 retrieveCtrl.fname = fname;
1225 retrieveCtrl.localno = sysno;
1226 retrieveCtrl.staticrank = recordAttr->staticrank;
1227 retrieveCtrl.score = score;
1228 retrieveCtrl.recordSize = recordAttr->recordSize;
1229 retrieveCtrl.odr = odr;
1230 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
1231 retrieveCtrl.comp = comp;
1232 retrieveCtrl.encoding = zh->record_encoding;
1233 retrieveCtrl.diagnostic = 0;
1234 retrieveCtrl.addinfo = 0;
1235 retrieveCtrl.dh = zh->reg->dh;
1236 retrieveCtrl.res = zh->res;
1237 retrieveCtrl.rec_buf = 0;
1238 retrieveCtrl.rec_len = -1;
1239 retrieveCtrl.handle = &fetch_info;
1240 retrieveCtrl.special_fetch = zebra_special_fetch;
1242 if (!(rt = recType_byName(zh->reg->recTypes, zh->res,
1243 file_type, &clientData)))
1245 wrbuf_printf(addinfo_w, "Could not handle record type %.40s",
1247 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1251 (*rt->retrieve)(clientData, &retrieveCtrl);
1252 return_code = retrieveCtrl.diagnostic;
1254 *output_format = retrieveCtrl.output_format;
1255 *rec_bufp = (char *) retrieveCtrl.rec_buf;
1256 *rec_lenp = retrieveCtrl.rec_len;
1257 if (retrieveCtrl.addinfo)
1258 wrbuf_puts(addinfo_w, retrieveCtrl.addinfo);
1261 stream.destroy(&stream);
1271 * c-file-style: "Stroustrup"
1272 * indent-tabs-mode: nil
1274 * vim: shiftwidth=4 tabstop=8 expandtab