1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 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 static int zebra_create_record_stream(ZebraHandle zh,
45 struct ZebraRecStream *stream)
47 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, *rec);
49 if ((*rec)->size[recInfo_storeData] > 0
50 || (*rec)->info[recInfo_filename] == 0)
51 zebra_create_stream_mem(stream, (*rec)->info[recInfo_storeData],
52 (*rec)->size[recInfo_storeData]);
58 if (zh->path_reg && !yaz_is_abspath((*rec)->info[recInfo_filename])){
59 strcpy(full_rep, zh->path_reg);
60 strcat(full_rep, "/");
61 strcat(full_rep, (*rec)->info[recInfo_filename]);
64 strcpy(full_rep, (*rec)->info[recInfo_filename]);
66 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1){
67 yaz_log(YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
70 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
72 zebra_create_stream_fd(stream, fd, recordAttr->recordOffset);
79 const char *index_name;
80 const char *index_type;
82 struct index_spec *next;
86 struct index_spec *parse_index_spec(const char *elem, NMEM nmem,
89 struct index_spec *first = 0;
90 struct index_spec **last = &first;
91 const char *cp = elem;
94 if (cp[0] == ':' && cp[1] == ':')
97 cp++; /* skip first ':' */
102 struct index_spec *spec = nmem_malloc(nmem, sizeof(*spec));
103 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 != ',' && *cp != ':')
124 spec->index_type = nmem_strdupn(nmem, cp0, cp - cp0);
126 if (*cp == ':') /* extra arguments */
131 while (*cp != '\0' && *cp != ',' && *cp != ':')
133 spec->extra = nmem_strdupn(nmem, cp0, cp - cp0);
144 static int parse_zebra_elem(const char *elem,
145 const char **index, size_t *index_len,
146 const char **type, size_t *type_len)
157 /* verify that '::' is in the beginning of *elem
158 and something more follows */
160 || !(elem +1) || ':' != *(elem +1)
161 || !(elem +2) || '\0' == *(elem +2))
164 /* pick out info from string after '::' */
166 cp = strchr(elem, ':');
168 if (!cp) /* index, no colon, no type */
171 *index_len = strlen(elem);
173 else if (cp[1] == '\0') /* colon, but no following type */
177 else /* index, colon and type */
180 *index_len = cp - elem;
182 *type_len = strlen(cp+1);
189 int zebra_special_sort_fetch(ZebraHandle zh, zint sysno, ODR odr,
190 const char *elemsetname,
191 const Odr_oid *input_format,
192 const Odr_oid **output_format,
193 char **rec_bufp, int *rec_lenp)
195 const char *retrieval_index;
196 size_t retrieval_index_len;
197 const char *retrieval_type;
198 size_t retrieval_type_len;
199 char retrieval_index_cstr[256];
200 char retrieval_type_cstr[256];
203 /* only accept XML and SUTRS requests */
204 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
205 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
207 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
210 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
213 if (!parse_zebra_elem(elemsetname,
214 &retrieval_index, &retrieval_index_len,
215 &retrieval_type, &retrieval_type_len))
217 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
220 if (retrieval_type_len == 0)
221 return -1; /* must have a register type specified */
222 if (!retrieval_index_len ||
223 retrieval_index_len >= sizeof(retrieval_index_cstr)-1)
225 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
228 memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
229 retrieval_index_cstr[retrieval_index_len] = '\0';
231 memcpy(retrieval_type_cstr, retrieval_type, retrieval_type_len);
232 retrieval_type_cstr[retrieval_type_len] = '\0';
234 ord = zebraExplain_lookup_attr_str(zh->reg->zei,
235 zinfo_index_category_sort,
237 retrieval_index_cstr);
239 return -1; /* is not a sort index */
242 char dst_buf[IT_MAX_WORD];
243 char str[IT_MAX_WORD];
244 const char *index_type;
246 const char *string_index = 0;
247 WRBUF wrbuf = wrbuf_alloc();
249 zebra_sort_sysno(zh->reg->sort_index, sysno);
250 zebra_sort_type(zh->reg->sort_index, ord);
251 zebra_sort_read(zh->reg->sort_index, str);
253 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, &string_index);
255 zebra_term_untrans(zh, index_type, dst_buf, str);
257 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
259 *output_format = yaz_oid_recsyn_xml;
260 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
261 " sysno=\"" ZINT_FORMAT "\""
262 " set=\"zebra::index%s/\">\n",
265 wrbuf_printf(wrbuf, " <index name=\"%s\"",
267 wrbuf_printf(wrbuf, " type=\"%s\">", index_type);
268 wrbuf_xmlputs(wrbuf, dst_buf);
269 wrbuf_printf(wrbuf, "</index>\n");
270 wrbuf_printf(wrbuf, "</record>\n");
272 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
274 *output_format = yaz_oid_recsyn_sutrs;
276 wrbuf_printf(wrbuf, "%s %s %s\n", string_index, index_type,
279 *rec_lenp = wrbuf_len(wrbuf);
280 *rec_bufp = odr_malloc(odr, *rec_lenp);
281 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
282 wrbuf_destroy(wrbuf);
287 int zebra_special_index_fetch(ZebraHandle zh, zint sysno, ODR odr,
289 const char *elemsetname,
290 const Odr_oid *input_format,
291 const Odr_oid **output_format,
292 char **rec_bufp, int *rec_lenp)
294 const char *retrieval_index;
295 size_t retrieval_index_len;
296 const char *retrieval_type;
297 size_t retrieval_type_len;
298 zebra_rec_keys_t keys;
300 char retrieval_type_cstr[256];
302 /* set output variables before processing possible error states */
305 /* only accept XML and SUTRS requests */
306 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
307 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
309 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
312 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
315 if (!parse_zebra_elem(elemsetname,
316 &retrieval_index, &retrieval_index_len,
317 &retrieval_type, &retrieval_type_len))
318 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
320 if (retrieval_type_len)
322 memcpy(retrieval_type_cstr, retrieval_type, retrieval_type_len);
323 retrieval_type_cstr[retrieval_type_len] = '\0';
326 if (retrieval_index_len)
328 char retrieval_index_cstr[256];
330 if (retrieval_index_len < sizeof(retrieval_index_cstr) -1)
332 memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
333 retrieval_index_cstr[retrieval_index_len] = '\0';
335 if (zebraExplain_lookup_attr_str(zh->reg->zei,
336 zinfo_index_category_index,
337 (retrieval_type_len == 0 ? 0 :
338 retrieval_type_cstr),
339 retrieval_index_cstr) == -1)
340 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
344 keys = zebra_rec_keys_open();
345 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
346 rec->size[recInfo_delKeys], 0);
348 if (!zebra_rec_keys_rewind(keys))
350 ret_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
356 struct it_key key_in;
357 WRBUF wrbuf = wrbuf_alloc();
359 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
361 *output_format = input_format;
362 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
363 " sysno=\"" ZINT_FORMAT "\""
364 " set=\"zebra::index%s/\">\n",
367 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
368 *output_format = input_format;
370 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
373 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
374 const char *index_type;
376 const char *string_index = 0;
377 size_t string_index_len;
378 char dst_buf[IT_MAX_WORD];
380 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
382 string_index_len = strlen(string_index);
384 /* process only if index is not defined,
385 or if defined and matching */
386 if (retrieval_index == 0
387 || (string_index_len == retrieval_index_len
388 && !memcmp(string_index, retrieval_index,
391 /* process only if type is not defined, or is matching */
392 if (retrieval_type == 0
393 || !strcmp(retrieval_type_cstr, index_type))
395 if (zebra_term_untrans(zh, index_type, dst_buf, str))
396 *dst_buf = '\0'; /* untrans failed */
398 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
400 wrbuf_printf(wrbuf, " <index name=\"%s\"",
403 wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
405 wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">",
406 key_in.mem[key_in.len -1]);
407 wrbuf_xmlputs(wrbuf, dst_buf);
408 wrbuf_printf(wrbuf, "</index>\n");
412 wrbuf_printf(wrbuf, "%s ", string_index);
414 wrbuf_printf(wrbuf, "%s", index_type);
416 for (i = 1; i < key_in.len; i++)
417 wrbuf_printf(wrbuf, " " ZINT_FORMAT,
420 wrbuf_printf(wrbuf, " %s", dst_buf);
422 wrbuf_printf(wrbuf, "\n");
429 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
430 wrbuf_printf(wrbuf, "</record>\n");
431 *rec_lenp = wrbuf_len(wrbuf);
432 *rec_bufp = odr_malloc(odr, *rec_lenp);
433 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
434 wrbuf_destroy(wrbuf);
436 zebra_rec_keys_close(keys);
441 static void retrieve_puts_attr(WRBUF wrbuf, const char *name,
446 wrbuf_printf(wrbuf, " %s=\"", name);
447 wrbuf_xmlputs(wrbuf, value);
448 wrbuf_printf(wrbuf, "\"");
452 static void retrieve_puts_attr_int(WRBUF wrbuf, const char *name,
455 wrbuf_printf(wrbuf, " %s=\"%i\"", name, value);
458 static void retrieve_puts_str(WRBUF wrbuf, const char *name,
462 wrbuf_printf(wrbuf, "%s %s\n", name, value);
465 static void retrieve_puts_int(WRBUF wrbuf, const char *name,
468 wrbuf_printf(wrbuf, "%s %i\n", name, value);
472 static void snippet_xml_record(ZebraHandle zh, WRBUF wrbuf, zebra_snippets *doc)
474 const zebra_snippet_word *doc_w;
477 wrbuf_printf(wrbuf, "%s>\n", ZEBRA_XML_HEADER_STR);
478 for (doc_w = zebra_snippets_constlist(doc); doc_w; doc_w = doc_w->next)
482 const char *index_type;
484 const char *string_index = 0;
486 zebraExplain_lookup_ord(zh->reg->zei, doc_w->ord,
487 &index_type, &db, &string_index);
491 wrbuf_printf(wrbuf, " <snippet name=\"%s\"", string_index);
492 wrbuf_printf(wrbuf, " type=\"%s\">", index_type);
495 wrbuf_puts(wrbuf, "<s>");
496 /* not printing leading ws */
497 if (mark_state || !doc_w->ws || doc_w->match)
498 wrbuf_xmlputs(wrbuf, doc_w->term);
500 wrbuf_puts(wrbuf, "</s>");
502 else if (mark_state == 1)
504 wrbuf_puts(wrbuf, "</snippet>\n");
506 mark_state = doc_w->mark;
510 wrbuf_puts(wrbuf, "</snippet>\n");
512 wrbuf_printf(wrbuf, "</record>");
515 int zebra_get_rec_snippets(ZebraHandle zh, zint sysno,
516 zebra_snippets *snippets)
519 Record rec = rec_get(zh->reg->records, sysno);
522 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
523 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
527 const char *file_type = rec->info[recInfo_fileType];
528 void *recTypeClientData;
529 RecType rt = recType_byName(zh->reg->recTypes, zh->res,
530 file_type, &recTypeClientData);
533 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
536 struct ZebraRecStream stream;
537 return_code = zebra_create_record_stream(zh, &rec, &stream);
538 if (return_code == 0)
540 extract_snippet(zh, snippets, &stream,
541 rt, recTypeClientData);
543 stream.destroy(&stream);
551 static int snippet_fetch(ZebraHandle zh, const char *setname,
553 const char *elemsetname,
554 const Odr_oid *input_format,
555 const Odr_oid **output_format,
556 char **rec_bufp, int *rec_lenp)
558 zebra_snippets *rec_snippets = zebra_snippets_create();
559 int return_code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
563 WRBUF wrbuf = wrbuf_alloc();
564 zebra_snippets *hit_snippet = zebra_snippets_create();
566 zebra_snippets_hit_vector(zh, setname, sysno, hit_snippet);
569 /* for debugging purposes */
570 yaz_log(YLOG_LOG, "---------------------------");
571 yaz_log(YLOG_LOG, "REC SNIPPET:");
572 zebra_snippets_log(rec_snippet, YLOG_LOG, 1);
573 yaz_log(YLOG_LOG, "---------------------------");
574 yaz_log(YLOG_LOG, "HIT SNIPPET:");
575 zebra_snippets_log(hit_snippet, YLOG_LOG, 1);
578 zebra_snippets_ring(rec_snippets, hit_snippet, 5, 5);
581 yaz_log(YLOG_LOG, "---------------------------");
582 yaz_log(YLOG_LOG, "RING SNIPPET:");
583 zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
585 snippet_xml_record(zh, wrbuf, rec_snippets);
587 *output_format = yaz_oid_recsyn_xml;
589 if (return_code == 0)
591 *rec_lenp = wrbuf_len(wrbuf);
592 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
594 wrbuf_destroy(wrbuf);
595 zebra_snippets_destroy(hit_snippet);
597 zebra_snippets_destroy(rec_snippets);
601 struct term_collect {
607 zint freq_term(ZebraHandle zh, int ord, const char *term, RSET rset_set)
609 struct rset_key_control *kc = zebra_key_control_create(zh);
610 char ord_buf[IT_MAX_WORD];
611 int ord_len = key_SU_encode(ord, ord_buf);
614 NMEM nmem = nmem_create();
616 strcpy(ord_buf + ord_len, term);
618 info = dict_lookup(zh->reg->dict, ord_buf);
623 memcpy(&isam_p, info+1, sizeof(ISAM_P));
625 rsets[0] = zebra_create_rset_isam(zh, nmem, kc, kc->scope, isam_p, 0);
626 rsets[1] = rset_dup(rset_set);
628 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
630 zebra_count_set(zh, rset, &hits, zh->approx_limit);
632 rset_delete(rsets[0]);
640 int term_qsort_handle(const void *a, const void *b)
642 const struct term_collect *l = a;
643 const struct term_collect *r = b;
644 if (l->set_occur < r->set_occur)
646 else if (l->set_occur > r->set_occur)
650 const char *lterm = l->term ? l->term : "";
651 const char *rterm = r->term ? r->term : "";
652 return strcmp(lterm, rterm);
656 void term_collect_freq(ZebraHandle zh,
657 struct term_collect *col, int no_terms_collect,
661 for (i = 0; i < no_terms_collect; i++)
664 col[i].set_occur = freq_term(zh, ord, col[i].term, rset);
666 qsort(col, no_terms_collect, sizeof(*col), term_qsort_handle);
669 struct term_collect *term_collect_create(zebra_strmap_t sm,
670 int no_terms_collect,
677 struct term_collect *col = nmem_malloc(nmem,
678 sizeof *col *no_terms_collect);
680 for (i = 0; i < no_terms_collect; i++)
684 col[i].set_occur = 0;
686 /* iterate over terms and collect the most frequent ones */
687 it = zebra_strmap_it_create(sm);
688 while ((term = zebra_strmap_it_next(it, &data_buf, &data_len)))
691 col[0] has lowest oc . col[no_terms_collect-1] has highest oc */
692 int oc = *(int*) data_buf;
694 /* insertion may be slow but terms terms will be "infrequent" and
695 thus number of iterations should be small below
697 while (j < no_terms_collect && oc > col[j].oc)
700 { /* oc <= col[j] and oc > col[j-1] */
702 memmove(col, col+1, sizeof(*col) * j);
707 zebra_strmap_it_destroy(it);
711 static ZEBRA_RES facet_fetch(ZebraHandle zh, const char *setname,
713 const char *elemsetname,
714 const Odr_oid *input_format,
715 const Odr_oid **output_format,
716 char **rec_bufp, int *rec_lenp)
720 int num_recs = 10; /* number of records to analyze */
722 ZebraMetaRecord *poset;
723 ZEBRA_RES ret = ZEBRA_OK;
725 WRBUF wr = wrbuf_alloc();
728 struct index_spec *spec, *spec_list;
731 res_get_int(zh->res, "facetNumRecs", &num_recs);
732 res_get_int(zh->res, "facetMaxChunks", &max_chunks);
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 yaz_timing_t timing = yaz_timing_create();
793 zebra_strmap_t *map_array
794 = odr_malloc(odr, sizeof *map_array * no_ord);
795 for (i = 0; i < no_ord; i++)
796 map_array[i] = zebra_strmap_create();
798 for (i = 0; i < num_recs; i++)
801 zint sysnos[MAX_SYSNOS_PER_RECORD];
802 int no_sysnos = MAX_SYSNOS_PER_RECORD;
805 ret = zebra_result_recid_to_sysno(zh, setname,
808 assert(no_sysnos > 0);
809 yaz_log(YLOG_DEBUG, "Analyzing rec=%d ISAM sysno=" ZINT_FORMAT " chunks=%d",
810 i, poset[i].sysno, no_sysnos);
811 for (j = 0; j < no_sysnos && j < max_chunks; j++)
815 struct it_key key_in;
816 Record rec = rec_get(zh->reg->records, sysnos[j]);
817 zebra_rec_keys_t keys = zebra_rec_keys_open();
818 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
819 rec->size[recInfo_delKeys], 0);
821 yaz_log(YLOG_DEBUG, "rec %d " ZINT_FORMAT " %s",
822 j, sysnos[j], zebra_rec_keys_empty(keys) ? "empty" : "non-empty");
823 if (zebra_rec_keys_rewind(keys))
825 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
828 struct index_spec *spec;
829 for (spec = spec_list, i = 0; i < no_ord;
830 i++, spec = spec->next)
832 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
833 if (ord == ord_array[i] &&
834 str[0] != FIRST_IN_FIELD_CHAR)
837 zebra_strmap_t sm = map_array[i];
839 freq = zebra_strmap_lookup(sm, str, 0, 0);
845 zebra_strmap_add(sm, str, &v, sizeof v);
851 zebra_rec_keys_close(keys);
855 yaz_timing_stop(timing);
856 yaz_log(YLOG_LOG, "facet first phase real=%4.2f",
857 yaz_timing_get_real(timing));
858 yaz_timing_start(timing);
860 wrbuf_puts(wr, "<facets>\n");
861 for (spec = spec_list, i = 0; i < no_ord; i++, spec = spec->next)
864 NMEM nmem = nmem_create();
865 struct term_collect *col;
866 int no_collect_terms = 20;
869 no_collect_terms = atoi(spec->extra);
870 if (no_collect_terms < 1)
871 no_collect_terms = 1;
872 col = term_collect_create(map_array[i], no_collect_terms, nmem);
873 term_collect_freq(zh, col, no_collect_terms, ord_array[i],
874 resultSetRef(zh, setname));
877 wrbuf_printf(wr, " <facet type=\"%s\" index=\"%s\">\n",
878 spec->index_type, spec->index_name);
880 wrbuf_printf(wr, "facet %s %s\n",
881 spec->index_type, spec->index_name);
882 for (j = 0; j < no_collect_terms; j++)
886 char dst_buf[IT_MAX_WORD];
887 zebra_term_untrans(zh, spec->index_type, dst_buf, col[j].term);
890 wrbuf_printf(wr, " <term coccur=\"%d\"", col[j].oc);
891 if (col[j].set_occur)
892 wrbuf_printf(wr, " occur=\"" ZINT_FORMAT "\"",
894 wrbuf_printf(wr, ">");
895 wrbuf_xmlputs(wr, dst_buf);
896 wrbuf_printf(wr, "</term>\n");
900 wrbuf_printf(wr, "term %d", col[j].oc);
901 if (col[j].set_occur)
902 wrbuf_printf(wr, " " ZINT_FORMAT,
904 wrbuf_printf(wr, ": %s\n", dst_buf);
909 wrbuf_puts(wr, " </facet>\n");
913 wrbuf_puts(wr, "</facets>\n");
914 for (i = 0; i < no_ord; i++)
915 zebra_strmap_destroy(map_array[i]);
916 yaz_timing_stop(timing);
917 yaz_log(YLOG_LOG, "facet second phase real=%4.2f",
918 yaz_timing_get_real(timing));
919 yaz_timing_destroy(&timing);
921 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wr));
923 *rec_lenp = strlen(*rec_bufp);
924 *output_format = yaz_oid_recsyn_xml;
926 zebra_meta_records_destroy(zh, poset, num_recs);
930 int zebra_special_fetch(ZebraHandle zh, const char *setname,
931 zint sysno, int score, ODR odr,
932 const char *elemsetname,
933 const Odr_oid *input_format,
934 const Odr_oid **output_format,
935 char **rec_bufp, int *rec_lenp)
939 /* set output variables before processing possible error states */
942 if (elemsetname && 0 == strncmp(elemsetname, "facet", 5))
944 return facet_fetch(zh, setname, odr,
946 input_format, output_format,
950 if (elemsetname && 0 == strcmp(elemsetname, "snippet"))
952 return snippet_fetch(zh, setname, sysno, odr,
954 input_format, output_format,
958 /* processing zebra::meta::sysno elemset without fetching binary data */
959 if (elemsetname && 0 == strcmp(elemsetname, "meta::sysno"))
962 WRBUF wrbuf = wrbuf_alloc();
963 if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
965 wrbuf_printf(wrbuf, ZINT_FORMAT, sysno);
966 *output_format = input_format;
968 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
970 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
971 " sysno=\"" ZINT_FORMAT "\"/>\n",
973 *output_format = input_format;
975 *rec_lenp = wrbuf_len(wrbuf);
977 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
979 ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
980 wrbuf_destroy(wrbuf);
984 /* processing special elementsetname zebra::index:: for sort elements */
985 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
987 int ret = zebra_special_sort_fetch(zh, sysno, odr,
989 input_format, output_format,
993 /* not a sort index so we continue to get the full record */
997 /* fetching binary record up for all other display elementsets */
998 rec = rec_get(zh->reg->records, sysno);
1001 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
1002 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1005 /* processing special elementsetnames zebra::data */
1006 if (elemsetname && 0 == strcmp(elemsetname, "data"))
1008 struct ZebraRecStream stream;
1009 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
1010 zebra_create_record_stream(zh, &rec, &stream);
1011 *output_format = input_format;
1012 *rec_lenp = recordAttr->recordSize;
1013 *rec_bufp = (char *) odr_malloc(odr, *rec_lenp);
1014 stream.readf(&stream, *rec_bufp, *rec_lenp);
1015 stream.destroy(&stream);
1020 /* only accept XML and SUTRS requests from now */
1021 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
1022 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
1024 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
1026 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
1030 /* processing special elementsetnames zebra::meta:: */
1031 if (elemsetname && 0 == strcmp(elemsetname, "meta"))
1034 WRBUF wrbuf = wrbuf_alloc();
1035 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
1037 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
1039 *output_format = input_format;
1041 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
1042 " sysno=\"" ZINT_FORMAT "\"", sysno);
1043 retrieve_puts_attr(wrbuf, "base", rec->info[recInfo_databaseName]);
1044 retrieve_puts_attr(wrbuf, "file", rec->info[recInfo_filename]);
1045 retrieve_puts_attr(wrbuf, "type", rec->info[recInfo_fileType]);
1047 retrieve_puts_attr_int(wrbuf, "score", score);
1050 " rank=\"" ZINT_FORMAT "\""
1052 " set=\"zebra::%s\"/>\n",
1053 recordAttr->staticrank,
1054 recordAttr->recordSize,
1057 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
1059 *output_format = input_format;
1060 wrbuf_printf(wrbuf, "sysno " ZINT_FORMAT "\n", sysno);
1061 retrieve_puts_str(wrbuf, "base", rec->info[recInfo_databaseName]);
1062 retrieve_puts_str(wrbuf, "file", rec->info[recInfo_filename]);
1063 retrieve_puts_str(wrbuf, "type", rec->info[recInfo_fileType]);
1065 retrieve_puts_int(wrbuf, "score", score);
1068 "rank " ZINT_FORMAT "\n"
1071 recordAttr->staticrank,
1072 recordAttr->recordSize,
1075 *rec_lenp = wrbuf_len(wrbuf);
1077 *rec_bufp = odr_strdup(odr, wrbuf_cstr(wrbuf));
1079 ret = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1081 wrbuf_destroy(wrbuf);
1086 /* processing special elementsetnames zebra::index:: */
1087 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
1089 int ret = zebra_special_index_fetch(zh, sysno, odr, rec,
1091 input_format, output_format,
1092 rec_bufp, rec_lenp);
1100 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1104 int zebra_record_fetch(ZebraHandle zh, const char *setname,
1105 zint sysno, int score,
1107 const Odr_oid *input_format, Z_RecordComposition *comp,
1108 const Odr_oid **output_format,
1109 char **rec_bufp, int *rec_lenp, char **basenamep,
1113 char *fname, *file_type, *basename;
1114 const char *elemsetname;
1115 struct ZebraRecStream stream;
1116 RecordAttr *recordAttr;
1118 int return_code = 0;
1119 zint sysnos[MAX_SYSNOS_PER_RECORD];
1120 int no_sysnos = MAX_SYSNOS_PER_RECORD;
1123 res = zebra_result_recid_to_sysno(zh, setname, sysno, sysnos, &no_sysnos);
1124 if (res != ZEBRA_OK)
1130 elemsetname = yaz_get_esn(comp);
1132 /* processing zebra special elementset names of form 'zebra:: */
1133 if (elemsetname && 0 == strncmp(elemsetname, "zebra::", 7))
1134 return zebra_special_fetch(zh, setname, sysno, score, odr,
1136 input_format, output_format,
1137 rec_bufp, rec_lenp);
1140 /* processing all other element set names */
1141 rec = rec_get(zh->reg->records, sysno);
1144 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
1146 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1150 recordAttr = rec_init_attr(zh->reg->zei, rec);
1152 file_type = rec->info[recInfo_fileType];
1153 fname = rec->info[recInfo_filename];
1154 basename = rec->info[recInfo_databaseName];
1155 *basenamep = (char *) odr_malloc(odr, strlen(basename)+1);
1156 strcpy(*basenamep, basename);
1158 yaz_log(YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
1161 return_code = zebra_create_record_stream(zh, &rec, &stream);
1166 struct recRetrieveCtrl retrieveCtrl;
1168 retrieveCtrl.stream = &stream;
1169 retrieveCtrl.fname = fname;
1170 retrieveCtrl.localno = sysno;
1171 retrieveCtrl.staticrank = recordAttr->staticrank;
1172 retrieveCtrl.score = score;
1173 retrieveCtrl.recordSize = recordAttr->recordSize;
1174 retrieveCtrl.odr = odr;
1175 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
1176 retrieveCtrl.comp = comp;
1177 retrieveCtrl.encoding = zh->record_encoding;
1178 retrieveCtrl.diagnostic = 0;
1179 retrieveCtrl.addinfo = 0;
1180 retrieveCtrl.dh = zh->reg->dh;
1181 retrieveCtrl.res = zh->res;
1182 retrieveCtrl.rec_buf = 0;
1183 retrieveCtrl.rec_len = -1;
1185 if (!(rt = recType_byName(zh->reg->recTypes, zh->res,
1186 file_type, &clientData)))
1188 char addinfo_str[100];
1190 sprintf(addinfo_str, "Could not handle record type %.40s",
1193 *addinfo = odr_strdup(odr, addinfo_str);
1194 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1198 (*rt->retrieve)(clientData, &retrieveCtrl);
1199 return_code = retrieveCtrl.diagnostic;
1201 *output_format = retrieveCtrl.output_format;
1202 *rec_bufp = (char *) retrieveCtrl.rec_buf;
1203 *rec_lenp = retrieveCtrl.rec_len;
1204 *addinfo = retrieveCtrl.addinfo;
1207 stream.destroy(&stream);
1217 * indent-tabs-mode: nil
1219 * vim: shiftwidth=4 tabstop=8 expandtab