1 /* This file is part of the Zebra server.
2 Copyright (C) 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
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 struct special_fetch_s {
54 static int zebra_create_record_stream(ZebraHandle zh,
56 struct ZebraRecStream *stream)
58 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, *rec);
60 if ((*rec)->size[recInfo_storeData] > 0
61 || (*rec)->info[recInfo_filename] == 0)
62 zebra_create_stream_mem(stream, (*rec)->info[recInfo_storeData],
63 (*rec)->size[recInfo_storeData]);
69 if (zh->path_reg && !yaz_is_abspath((*rec)->info[recInfo_filename])){
70 strcpy(full_rep, zh->path_reg);
71 strcat(full_rep, "/");
72 strcat(full_rep, (*rec)->info[recInfo_filename]);
75 strcpy(full_rep, (*rec)->info[recInfo_filename]);
77 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1){
78 yaz_log(YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
81 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
83 zebra_create_stream_fd(stream, fd, recordAttr->recordOffset);
90 const char *index_name;
91 const char *index_type;
93 struct index_spec *next;
97 struct index_spec *parse_index_spec(const char *elem, NMEM nmem,
100 struct index_spec *first = 0;
101 struct index_spec **last = &first;
102 const char *cp = elem;
105 if (cp[0] == ':' && cp[1] == ':')
108 cp++; /* skip first ':' */
113 struct index_spec *spec = nmem_malloc(nmem, sizeof(*spec));
114 spec->index_type = 0;
123 cp++; /* skip ',' or second ':' */
125 while (*cp != ':' && *cp != '\0' && *cp != ',')
127 spec->index_name = nmem_strdupn(nmem, cp0, cp - cp0);
128 if (*cp == ':') /* type as well */
133 while (*cp != '\0' && *cp != ',' && *cp != ':')
135 spec->index_type = nmem_strdupn(nmem, cp0, cp - cp0);
137 if (*cp == ':') /* extra arguments */
142 while (*cp != '\0' && *cp != ',' && *cp != ':')
144 spec->extra = nmem_strdupn(nmem, cp0, cp - cp0);
155 static int sort_fetch(
156 struct special_fetch_s *fi, const char *elemsetname,
157 const Odr_oid *input_format,
158 const Odr_oid **output_format,
159 WRBUF result, WRBUF addinfo)
162 ZebraHandle zh = fi->zh;
164 struct index_spec *spec;
166 spec = parse_index_spec(elemsetname, fi->nmem, &error);
168 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
170 /* for sort fetches.. We allow only one index and type must be given */
171 if (!spec || spec->next || !spec->index_type)
173 ord = zebraExplain_lookup_attr_str(zh->reg->zei,
174 zinfo_index_category_sort,
178 return -1; /* is not a sort index */
181 WRBUF wrbuf_str = wrbuf_alloc();
182 const char *index_type;
184 const char *string_index = 0;
185 WRBUF wrbuf_result = result;
188 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
190 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
192 *output_format = yaz_oid_recsyn_xml;
193 wrbuf_printf(wrbuf_result, ZEBRA_XML_HEADER_STR
194 " sysno=\"" ZINT_FORMAT "\""
195 " set=\"zebra::index%s\">\n",
196 fi->sysno, elemsetname);
198 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
200 *output_format = yaz_oid_recsyn_sutrs;
204 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
207 wrbuf_destroy(wrbuf_str);
208 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
210 zebra_sort_type(zh->reg->sort_index, ord);
211 zebra_sort_sysno(zh->reg->sort_index, fi->sysno);
212 zebra_sort_read(zh->reg->sort_index, 0, wrbuf_str);
214 while (off != wrbuf_len(wrbuf_str))
216 char dst_buf[IT_MAX_WORD];
217 assert(off < wrbuf_len(wrbuf_str));
218 zebra_term_untrans(zh, index_type, dst_buf,
219 wrbuf_buf(wrbuf_str)+off);
221 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
223 wrbuf_printf(wrbuf_result, " <index name=\"%s\"",
225 wrbuf_printf(wrbuf_result, " type=\"%s\">", index_type);
226 wrbuf_xmlputs(wrbuf_result, dst_buf);
227 wrbuf_printf(wrbuf_result, "</index>\n");
229 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
231 wrbuf_printf(wrbuf_result, "%s %s %s\n", string_index, index_type,
234 off += strlen(wrbuf_buf(wrbuf_str)+off) + 1;
236 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
238 wrbuf_printf(wrbuf_result, "</record>\n");
240 wrbuf_destroy(wrbuf_str);
245 static int special_index_fetch(
246 struct special_fetch_s *fi, const char *elemsetname,
247 const Odr_oid *input_format,
248 const Odr_oid **output_format,
249 WRBUF result, WRBUF addinfo,
252 zebra_rec_keys_t keys;
254 ZebraHandle zh = fi->zh;
255 struct index_spec *spec, *spec_list;
258 /* set output variables before processing possible error states */
261 /* only accept XML and SUTRS requests */
262 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
263 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
265 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
268 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
271 spec_list = parse_index_spec(elemsetname, fi->nmem, &error);
274 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
277 for (spec = spec_list; spec; spec = spec->next)
279 if (zebraExplain_lookup_attr_str(zh->reg->zei,
280 zinfo_index_category_index,
282 spec->index_name) == -1)
283 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
286 keys = zebra_rec_keys_open();
287 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
288 rec->size[recInfo_delKeys], 0);
290 if (!zebra_rec_keys_rewind(keys))
292 ret_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
298 struct it_key key_in;
299 WRBUF wrbuf = result;
301 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
303 *output_format = input_format;
304 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
305 " sysno=\"" ZINT_FORMAT "\""
306 " set=\"zebra::index%s\">\n",
307 fi->sysno, elemsetname);
309 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
310 *output_format = input_format;
312 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
315 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
316 const char *index_type;
318 const char *string_index = 0;
319 char dst_buf[IT_MAX_WORD];
322 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
325 match = 1; /* match all if no specs were given */
328 for (spec = spec_list; spec; spec = spec->next)
330 if ((!spec->index_type ||
331 !yaz_matchstr(spec->index_type, index_type))
333 !yaz_matchstr(spec->index_name, string_index))
339 if (zebra_term_untrans(zh, index_type, dst_buf, str))
340 *dst_buf = '\0'; /* untrans failed */
342 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
344 wrbuf_printf(wrbuf, " <index name=\"%s\"",
347 wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
349 wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">",
350 key_in.mem[key_in.len -1]);
351 wrbuf_xmlputs(wrbuf, dst_buf);
352 wrbuf_printf(wrbuf, "</index>\n");
356 wrbuf_printf(wrbuf, "%s ", string_index);
358 wrbuf_printf(wrbuf, "%s", index_type);
360 for (i = 1; i < key_in.len; i++)
361 wrbuf_printf(wrbuf, " " ZINT_FORMAT,
364 wrbuf_printf(wrbuf, " %s", dst_buf);
366 wrbuf_printf(wrbuf, "\n");
370 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
371 wrbuf_printf(wrbuf, "</record>\n");
373 zebra_rec_keys_close(keys);
378 static void retrieve_puts_attr(WRBUF wrbuf, const char *name,
383 wrbuf_printf(wrbuf, " %s=\"", name);
384 wrbuf_xmlputs(wrbuf, value);
385 wrbuf_printf(wrbuf, "\"");
389 static void retrieve_puts_attr_int(WRBUF wrbuf, const char *name,
392 wrbuf_printf(wrbuf, " %s=\"%i\"", name, value);
395 static void retrieve_puts_str(WRBUF wrbuf, const char *name,
399 wrbuf_printf(wrbuf, "%s %s\n", name, value);
402 static void retrieve_puts_int(WRBUF wrbuf, const char *name,
405 wrbuf_printf(wrbuf, "%s %i\n", name, value);
409 static void snippet_check_fields(ZebraHandle zh, WRBUF wrbuf,
411 const zebra_snippet_word *doc_w,
412 const char *w_index_type)
414 /* beginning of snippet. See which fields the snippet also
416 const zebra_snippet_word *w;
418 for (w = zebra_snippets_constlist(doc); w; w = w->next)
420 /* same sequence but other field? */
421 if (w->seqno == doc_w->seqno && w->ord != doc_w->ord)
423 const char *index_type;
425 const char *string_index = 0;
427 zebraExplain_lookup_ord(zh->reg->zei, w->ord,
428 &index_type, &db, &string_index);
429 /* only report for same index type */
430 if (!strcmp(w_index_type, index_type))
433 wrbuf_printf(wrbuf, " fields=\"%s", string_index);
435 wrbuf_printf(wrbuf, " %s", string_index);
441 wrbuf_printf(wrbuf, "\"");
444 static void snippet_xml_record(ZebraHandle zh, WRBUF wrbuf, zebra_snippets *doc)
446 const zebra_snippet_word *doc_w;
449 wrbuf_printf(wrbuf, "%s>\n", ZEBRA_XML_HEADER_STR);
450 for (doc_w = zebra_snippets_constlist(doc); doc_w; doc_w = doc_w->next)
454 const char *index_type;
456 const char *string_index = 0;
458 zebraExplain_lookup_ord(zh->reg->zei, doc_w->ord,
459 &index_type, &db, &string_index);
464 wrbuf_printf(wrbuf, " <snippet name=\"%s\"", string_index);
465 wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
466 snippet_check_fields(zh, wrbuf, doc, doc_w, index_type);
467 wrbuf_printf(wrbuf, ">");
470 wrbuf_puts(wrbuf, "<s>");
471 /* not printing leading ws */
472 if (mark_state || !doc_w->ws || doc_w->match)
473 wrbuf_xmlputs(wrbuf, doc_w->term);
475 wrbuf_puts(wrbuf, "</s>");
477 else if (mark_state == 1)
479 wrbuf_puts(wrbuf, "</snippet>\n");
481 mark_state = doc_w->mark;
485 wrbuf_puts(wrbuf, "</snippet>\n");
487 wrbuf_printf(wrbuf, "</record>");
490 int zebra_get_rec_snippets(ZebraHandle zh, zint sysno,
491 zebra_snippets *snippets)
494 Record rec = rec_get(zh->reg->records, sysno);
497 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
498 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
502 const char *file_type = rec->info[recInfo_fileType];
503 void *recTypeClientData;
504 RecType rt = recType_byName(zh->reg->recTypes, zh->res,
505 file_type, &recTypeClientData);
508 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
511 struct ZebraRecStream stream;
512 return_code = zebra_create_record_stream(zh, &rec, &stream);
513 if (return_code == 0)
515 extract_snippet(zh, snippets, &stream,
516 rt, recTypeClientData);
518 stream.destroy(&stream);
526 static int snippet_fetch(
527 struct special_fetch_s *fi, const char *elemsetname,
528 const Odr_oid *input_format,
529 const Odr_oid **output_format,
530 WRBUF result, WRBUF addinfo)
532 ZebraHandle zh = fi->zh;
533 zebra_snippets *rec_snippets = zebra_snippets_create();
534 int return_code = zebra_get_rec_snippets(zh, fi->sysno, rec_snippets);
538 WRBUF wrbuf = result;
539 zebra_snippets *hit_snippet = zebra_snippets_create();
541 zebra_snippets_hit_vector(zh, fi->setname, fi->sysno, hit_snippet);
544 /* for debugging purposes */
545 yaz_log(YLOG_LOG, "---------------------------");
546 yaz_log(YLOG_LOG, "REC SNIPPET:");
547 zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
548 yaz_log(YLOG_LOG, "---------------------------");
549 yaz_log(YLOG_LOG, "HIT SNIPPET:");
550 zebra_snippets_log(hit_snippet, YLOG_LOG, 1);
553 zebra_snippets_ring(rec_snippets, hit_snippet, 5, 5);
556 yaz_log(YLOG_LOG, "---------------------------");
557 yaz_log(YLOG_LOG, "RING SNIPPET:");
558 zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
560 snippet_xml_record(zh, wrbuf, rec_snippets);
562 *output_format = yaz_oid_recsyn_xml;
564 zebra_snippets_destroy(hit_snippet);
566 zebra_snippets_destroy(rec_snippets);
570 struct term_collect {
576 static zint freq_term(ZebraHandle zh, int ord, const char *term, RSET rset_set)
578 struct rset_key_control *kc = zebra_key_control_create(zh);
579 char ord_buf[IT_MAX_WORD];
580 int ord_len = key_SU_encode(ord, ord_buf);
583 NMEM nmem = nmem_create();
585 strcpy(ord_buf + ord_len, term);
587 info = dict_lookup(zh->reg->dict, ord_buf);
592 memcpy(&isam_p, info+1, sizeof(ISAM_P));
594 rsets[0] = zebra_create_rset_isam(zh, nmem, kc, kc->scope, isam_p, 0);
595 rsets[1] = rset_dup(rset_set);
597 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
599 zebra_count_set(zh, rset, &hits, zh->approx_limit);
601 rset_delete(rsets[0]);
609 static int term_qsort_handle(const void *a, const void *b)
611 const struct term_collect *l = a;
612 const struct term_collect *r = b;
613 if (l->set_occur < r->set_occur)
615 else if (l->set_occur > r->set_occur)
619 const char *lterm = l->term ? l->term : "";
620 const char *rterm = r->term ? r->term : "";
621 return strcmp(lterm, rterm);
625 static void term_collect_freq(ZebraHandle zh,
626 struct term_collect *col, int no_terms_collect,
627 int ord, RSET rset, double scale_factor)
630 for (i = 0; i < no_terms_collect; i++)
634 if (scale_factor < 0.0)
636 col[i].set_occur = freq_term(zh, ord, col[i].term, rset);
639 col[i].set_occur = scale_factor * col[i].oc;
642 qsort(col, no_terms_collect, sizeof(*col), term_qsort_handle);
645 static struct term_collect *term_collect_create(zebra_strmap_t sm,
646 int no_terms_collect,
653 struct term_collect *col = nmem_malloc(nmem,
654 sizeof *col *no_terms_collect);
656 for (i = 0; i < no_terms_collect; i++)
660 col[i].set_occur = 0;
662 /* iterate over terms and collect the most frequent ones */
663 it = zebra_strmap_it_create(sm);
664 while ((term = zebra_strmap_it_next(it, &data_buf, &data_len)))
667 col[0] has lowest oc . col[no_terms_collect-1] has highest oc */
668 int oc = *(int*) data_buf;
670 /* insertion may be slow but terms terms will be "infrequent" and
671 thus number of iterations should be small below
673 while (j < no_terms_collect && oc > col[j].oc)
676 { /* oc <= col[j] and oc > col[j-1] */
678 memmove(col, col+1, sizeof(*col) * j);
683 zebra_strmap_it_destroy(it);
687 static int perform_facet_sort(ZebraHandle zh, int no_ord, int *ord_array,
688 zebra_strmap_t *map_array,
689 int num_recs, ZebraMetaRecord *poset)
692 WRBUF w = wrbuf_alloc();
695 for (ord_i = 0; ord_i < no_ord; ord_i++)
697 for (rec_i = 0; rec_i < num_recs; rec_i++)
699 if (!poset[rec_i].sysno)
702 zebra_sort_sysno(zh->reg->sort_index, poset[rec_i].sysno);
703 zebra_sort_type(zh->reg->sort_index, ord_array[ord_i]);
706 if (zebra_sort_read(zh->reg->sort_index, 0, w))
708 zebra_strmap_t sm = map_array[ord_i];
710 while (off != wrbuf_len(w))
712 const char *str = wrbuf_buf(w) + off;
713 int *freq = zebra_strmap_lookup(sm, str, 0, 0);
719 zebra_strmap_add(sm, str, &v, sizeof v);
721 off += strlen(str)+1;
731 static int perform_facet_index(ZebraHandle zh,
732 struct special_fetch_s *fi,
733 int no_ord, int *ord_array,
734 zebra_strmap_t *map_array,
735 int num_recs, ZebraMetaRecord *poset,
736 struct index_spec *spec_list)
740 res_get_int(zh->res, "facetMaxChunks", &max_chunks);
742 for (rec_i = 0; rec_i < num_recs; rec_i++)
746 zint sysnos[MAX_SYSNOS_PER_RECORD];
747 int no_sysnos = MAX_SYSNOS_PER_RECORD;
748 if (!poset[rec_i].sysno)
750 ret = zebra_result_recid_to_sysno(zh, fi->setname,
753 assert(no_sysnos > 0);
754 yaz_log(YLOG_DEBUG, "Analyzing rec=%d ISAM sysno=" ZINT_FORMAT " chunks=%d",
755 rec_i, poset[rec_i].sysno, no_sysnos);
756 for (j = 0; j < no_sysnos && j < max_chunks; j++)
760 struct it_key key_in;
761 Record rec = rec_get(zh->reg->records, sysnos[j]);
762 zebra_rec_keys_t keys = zebra_rec_keys_open();
763 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
764 rec->size[recInfo_delKeys], 0);
766 yaz_log(YLOG_DEBUG, "rec %d " ZINT_FORMAT " %s",
767 j, sysnos[j], zebra_rec_keys_empty(keys) ? "empty" : "non-empty");
768 if (zebra_rec_keys_rewind(keys))
770 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
773 struct index_spec *spec;
774 for (spec = spec_list, ord_i = 0; ord_i < no_ord;
775 ord_i++, spec = spec->next)
777 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
778 if (ord == ord_array[ord_i] &&
779 str[0] != FIRST_IN_FIELD_CHAR)
782 zebra_strmap_t sm = map_array[ord_i];
784 freq = zebra_strmap_lookup(sm, str, 0, 0);
790 zebra_strmap_add(sm, str, &v, sizeof v);
796 zebra_rec_keys_close(keys);
803 static int perform_facet(ZebraHandle zh,
804 struct special_fetch_s *fi,
806 int num_recs, ZebraMetaRecord *poset,
807 struct index_spec *spec_list,
808 int no_ord, int *ord_array,
810 zinfo_index_category_t cat)
815 struct index_spec *spec;
816 yaz_timing_t timing = yaz_timing_create();
817 zebra_strmap_t *map_array
818 = nmem_malloc(fi->nmem, sizeof *map_array * no_ord);
819 for (i = 0; i < no_ord; i++)
820 map_array[i] = zebra_strmap_create();
822 if (cat == zinfo_index_category_sort)
823 perform_facet_sort(zh, no_ord, ord_array, map_array,
826 perform_facet_index(zh, fi, no_ord, ord_array, map_array,
827 num_recs, poset, spec_list);
828 yaz_timing_stop(timing);
829 yaz_log(YLOG_LOG, "facet first phase real=%4.2f cat=%s",
830 yaz_timing_get_real(timing),
831 (cat == zinfo_index_category_sort) ? "sort" : "index");
832 yaz_timing_start(timing);
833 for (spec = spec_list, i = 0; i < no_ord; i++, spec = spec->next)
836 NMEM nmem = nmem_create();
837 struct term_collect *col;
838 int no_collect_terms = 20;
841 no_collect_terms = atoi(spec->extra);
842 if (no_collect_terms < 1)
843 no_collect_terms = 1;
844 col = term_collect_create(map_array[i], no_collect_terms, nmem);
845 term_collect_freq(zh, col, no_collect_terms, ord_array[i],
846 resultSetRef(zh, fi->setname),
847 cat == zinfo_index_category_sort ? 1.0 : -1.0);
850 wrbuf_printf(wr, " <facet type=\"%s\" index=\"%s\">\n",
851 spec->index_type, spec->index_name);
853 wrbuf_printf(wr, "facet %s %s\n",
854 spec->index_type, spec->index_name);
855 for (j = 0; j < no_collect_terms; j++)
859 char dst_buf[IT_MAX_WORD];
860 zebra_term_untrans(zh, spec->index_type, dst_buf, col[j].term);
863 wrbuf_printf(wr, " <term coccur=\"%d\"", col[j].oc);
864 if (col[j].set_occur)
865 wrbuf_printf(wr, " occur=\"" ZINT_FORMAT "\"",
867 wrbuf_printf(wr, ">");
868 wrbuf_xmlputs(wr, dst_buf);
869 wrbuf_printf(wr, "</term>\n");
873 wrbuf_printf(wr, "term %d", col[j].oc);
874 if (col[j].set_occur)
875 wrbuf_printf(wr, " " ZINT_FORMAT,
877 wrbuf_printf(wr, ": %s\n", dst_buf);
882 wrbuf_puts(wr, " </facet>\n");
885 for (i = 0; i < no_ord; i++)
886 zebra_strmap_destroy(map_array[i]);
887 yaz_timing_stop(timing);
888 yaz_log(YLOG_LOG, "facet second phase real=%4.2f",
889 yaz_timing_get_real(timing));
890 yaz_timing_destroy(&timing);
894 static int facet_fetch(
895 struct special_fetch_s *fi, const char *elemsetname,
896 const Odr_oid *input_format,
897 const Odr_oid **output_format,
898 WRBUF result, WRBUF addinfo)
902 int num_recs = 10; /* number of records to analyze */
903 ZebraMetaRecord *poset;
904 ZEBRA_RES ret = ZEBRA_OK;
908 struct index_spec *spec, *spec_list;
910 ZebraHandle zh = fi->zh;
911 /* whether sort or index based */
912 zinfo_index_category_t cat = zinfo_index_category_sort;
914 /* see if XML is required for response */
915 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml) == 0)
918 spec_list = parse_index_spec(elemsetname, fi->nmem, &error);
920 if (!spec_list || error)
922 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
925 for (spec = spec_list; spec; spec = spec->next)
927 if (!spec->index_type)
928 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
932 /* try to see if all specs are sort based.. If not, try the
934 ord_array = nmem_malloc(fi->nmem, sizeof(*ord_array) * no_ord);
936 for (spec = spec_list, i = 0; spec; spec = spec->next, i++)
938 int ord = zebraExplain_lookup_attr_str(zh->reg->zei,
939 zinfo_index_category_sort,
949 cat = zinfo_index_category_index;
950 for (spec = spec_list, i = 0; spec; spec = spec->next, i++)
952 int ord = zebraExplain_lookup_attr_str(zh->reg->zei,
953 zinfo_index_category_index,
963 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
965 res_get_int(zh->res, "facetNumRecs", &num_recs);
967 pos_array = (zint *) nmem_malloc(fi->nmem, num_recs * sizeof(*pos_array));
968 for (i = 0; i < num_recs; i++)
970 poset = zebra_meta_records_create(zh, fi->setname, num_recs, pos_array);
973 wrbuf_puts(addinfo, fi->setname);
974 return YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
980 wrbuf_printf(result, ZEBRA_XML_HEADER_STR ">\n");
982 ret = perform_facet(zh, fi, result, num_recs, poset,
983 spec_list, no_ord, ord_array, use_xml,
986 wrbuf_puts(result, "</record>\n");
988 *output_format = yaz_oid_recsyn_xml;
989 zebra_meta_records_destroy(zh, poset, num_recs);
994 static int zebra_special_fetch(
995 void *handle, const char *elemsetname,
996 const Odr_oid *input_format,
997 const Odr_oid **output_format,
998 WRBUF result, WRBUF addinfo)
1001 struct special_fetch_s *fi = (struct special_fetch_s *) handle;
1002 ZebraHandle zh = fi->zh;
1003 zint sysno = fi->sysno;
1005 /* processing zebra::facet */
1006 if (elemsetname && 0 == strncmp(elemsetname, "facet", 5))
1008 return facet_fetch(fi, elemsetname + 5,
1009 input_format, output_format,
1013 if (elemsetname && 0 == strcmp(elemsetname, "snippet"))
1015 return snippet_fetch(fi, elemsetname + 7,
1016 input_format, output_format,
1020 /* processing zebra::meta::sysno */
1021 if (elemsetname && 0 == strcmp(elemsetname, "meta::sysno"))
1024 if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
1026 wrbuf_printf(result, ZINT_FORMAT, fi->sysno);
1027 *output_format = input_format;
1029 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
1031 wrbuf_printf(result, ZEBRA_XML_HEADER_STR
1032 " sysno=\"" ZINT_FORMAT "\"/>\n",
1034 *output_format = input_format;
1037 ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
1041 /* processing special elementsetname zebra::index:: for sort elements */
1042 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
1044 int ret = sort_fetch(
1045 fi, elemsetname + 5,
1046 input_format, output_format,
1050 /* not a sort index so we continue to get the full record */
1054 /* fetching binary record up for all other display elementsets */
1055 rec = rec_get(zh->reg->records, sysno);
1058 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
1059 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1062 /* processing special elementsetnames zebra::data */
1063 if (elemsetname && 0 == strcmp(elemsetname, "data"))
1065 struct ZebraRecStream stream;
1066 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
1069 zebra_create_record_stream(zh, &rec, &stream);
1070 *output_format = input_format;
1072 b = nmem_malloc(fi->nmem, recordAttr->recordSize);
1073 stream.readf(&stream, b, recordAttr->recordSize);
1074 wrbuf_write(result, b, recordAttr->recordSize);
1076 stream.destroy(&stream);
1081 /* processing special elementsetnames zebra::meta:: */
1082 if (elemsetname && 0 == strcmp(elemsetname, "meta"))
1085 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
1087 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
1089 *output_format = input_format;
1091 wrbuf_printf(result, ZEBRA_XML_HEADER_STR
1092 " sysno=\"" ZINT_FORMAT "\"", sysno);
1093 retrieve_puts_attr(result, "base", rec->info[recInfo_databaseName]);
1094 retrieve_puts_attr(result, "file", rec->info[recInfo_filename]);
1095 retrieve_puts_attr(result, "type", rec->info[recInfo_fileType]);
1097 retrieve_puts_attr_int(result, "score", fi->score);
1099 wrbuf_printf(result,
1100 " rank=\"" ZINT_FORMAT "\""
1102 " set=\"zebra::%s\"/>\n",
1103 recordAttr->staticrank,
1104 recordAttr->recordSize,
1107 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
1109 *output_format = input_format;
1110 wrbuf_printf(result, "sysno " ZINT_FORMAT "\n", sysno);
1111 retrieve_puts_str(result, "base", rec->info[recInfo_databaseName]);
1112 retrieve_puts_str(result, "file", rec->info[recInfo_filename]);
1113 retrieve_puts_str(result, "type", rec->info[recInfo_fileType]);
1115 retrieve_puts_int(result, "score", fi->score);
1117 wrbuf_printf(result,
1118 "rank " ZINT_FORMAT "\n"
1121 recordAttr->staticrank,
1122 recordAttr->recordSize,
1126 ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
1132 /* processing special elementsetnames zebra::index:: */
1133 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
1135 int ret = special_index_fetch(
1136 fi, elemsetname + 5,
1137 input_format, output_format,
1138 result, addinfo, rec);
1145 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1148 int zebra_record_fetch(ZebraHandle zh, const char *setname,
1149 zint sysno, int score,
1151 const Odr_oid *input_format, Z_RecordComposition *comp,
1152 const Odr_oid **output_format,
1153 char **rec_bufp, int *rec_lenp, char **basenamep,
1157 char *fname, *file_type, *basename;
1158 const char *elemsetname;
1159 struct ZebraRecStream stream;
1160 RecordAttr *recordAttr;
1162 int return_code = 0;
1163 zint sysnos[MAX_SYSNOS_PER_RECORD];
1164 int no_sysnos = MAX_SYSNOS_PER_RECORD;
1166 struct special_fetch_s fetch_info;
1168 res = zebra_result_recid_to_sysno(zh, setname, sysno, sysnos, &no_sysnos);
1169 if (res != ZEBRA_OK)
1174 elemsetname = yaz_get_esn(comp);
1177 fetch_info.setname = setname;
1178 fetch_info.sysno = sysno;
1179 fetch_info.score = score;
1180 fetch_info.nmem = odr->mem;
1182 /* processing zebra special elementset names of form 'zebra:: */
1183 if (elemsetname && 0 == strncmp(elemsetname, "zebra::", 7))
1185 WRBUF result = wrbuf_alloc();
1186 int r = zebra_special_fetch(&fetch_info, elemsetname + 7,
1187 input_format, output_format,
1191 *rec_bufp = odr_strdup(odr, wrbuf_cstr(result));
1192 *rec_lenp = wrbuf_len(result);
1194 wrbuf_destroy(result);
1198 /* processing all other element set names */
1199 rec = rec_get(zh->reg->records, sysno);
1202 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
1204 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1208 recordAttr = rec_init_attr(zh->reg->zei, rec);
1210 file_type = rec->info[recInfo_fileType];
1211 fname = rec->info[recInfo_filename];
1212 basename = rec->info[recInfo_databaseName];
1213 *basenamep = (char *) odr_malloc(odr, strlen(basename)+1);
1214 strcpy(*basenamep, basename);
1216 yaz_log(YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
1219 return_code = zebra_create_record_stream(zh, &rec, &stream);
1224 struct recRetrieveCtrl retrieveCtrl;
1226 retrieveCtrl.stream = &stream;
1227 retrieveCtrl.fname = fname;
1228 retrieveCtrl.localno = sysno;
1229 retrieveCtrl.staticrank = recordAttr->staticrank;
1230 retrieveCtrl.score = score;
1231 retrieveCtrl.recordSize = recordAttr->recordSize;
1232 retrieveCtrl.odr = odr;
1233 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
1234 retrieveCtrl.comp = comp;
1235 retrieveCtrl.encoding = zh->record_encoding;
1236 retrieveCtrl.diagnostic = 0;
1237 retrieveCtrl.addinfo = 0;
1238 retrieveCtrl.dh = zh->reg->dh;
1239 retrieveCtrl.res = zh->res;
1240 retrieveCtrl.rec_buf = 0;
1241 retrieveCtrl.rec_len = -1;
1242 retrieveCtrl.handle = &fetch_info;
1243 retrieveCtrl.special_fetch = zebra_special_fetch;
1245 if (!(rt = recType_byName(zh->reg->recTypes, zh->res,
1246 file_type, &clientData)))
1248 wrbuf_printf(addinfo_w, "Could not handle record type %.40s",
1250 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1254 (*rt->retrieve)(clientData, &retrieveCtrl);
1255 return_code = retrieveCtrl.diagnostic;
1257 *output_format = retrieveCtrl.output_format;
1258 *rec_bufp = (char *) retrieveCtrl.rec_buf;
1259 *rec_lenp = retrieveCtrl.rec_len;
1260 if (retrieveCtrl.addinfo)
1261 wrbuf_puts(addinfo_w, retrieveCtrl.addinfo);
1264 stream.destroy(&stream);
1274 * c-file-style: "Stroustrup"
1275 * indent-tabs-mode: nil
1277 * vim: shiftwidth=4 tabstop=8 expandtab