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 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 parse_zebra_elem(const char *elem,
153 const char **index, size_t *index_len,
154 const char **type, size_t *type_len)
165 /* verify that '::' is in the beginning of *elem
166 and something more follows */
168 || !(elem +1) || ':' != *(elem +1)
169 || !(elem +2) || '\0' == *(elem +2))
172 /* pick out info from string after '::' */
174 cp = strchr(elem, ':');
176 if (!cp) /* index, no colon, no type */
179 *index_len = strlen(elem);
181 else if (cp[1] == '\0') /* colon, but no following type */
185 else /* index, colon and type */
188 *index_len = cp - elem;
190 *type_len = strlen(cp+1);
197 static int sort_fetch(
198 struct special_fetch_s *fi, const char *elemsetname,
199 const Odr_oid *input_format,
200 const Odr_oid **output_format,
201 WRBUF result, WRBUF addinfo)
203 const char *retrieval_index;
204 size_t retrieval_index_len;
205 const char *retrieval_type;
206 size_t retrieval_type_len;
207 char retrieval_index_cstr[256];
208 char retrieval_type_cstr[256];
210 ZebraHandle zh = fi->zh;
212 if (!parse_zebra_elem(elemsetname,
213 &retrieval_index, &retrieval_index_len,
214 &retrieval_type, &retrieval_type_len))
216 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
219 if (retrieval_type_len == 0)
220 return -1; /* must have a register type specified */
221 if (!retrieval_index_len ||
222 retrieval_index_len >= sizeof(retrieval_index_cstr)-1)
224 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
227 memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
228 retrieval_index_cstr[retrieval_index_len] = '\0';
230 memcpy(retrieval_type_cstr, retrieval_type, retrieval_type_len);
231 retrieval_type_cstr[retrieval_type_len] = '\0';
233 ord = zebraExplain_lookup_attr_str(zh->reg->zei,
234 zinfo_index_category_sort,
236 retrieval_index_cstr);
238 return -1; /* is not a sort index */
241 WRBUF wrbuf_str = wrbuf_alloc();
242 const char *index_type;
244 const char *string_index = 0;
245 WRBUF wrbuf_result = result;
248 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
250 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
252 *output_format = yaz_oid_recsyn_xml;
253 wrbuf_printf(wrbuf_result, ZEBRA_XML_HEADER_STR
254 " sysno=\"" ZINT_FORMAT "\""
255 " set=\"zebra::index%s/\">\n",
256 fi->sysno, elemsetname);
258 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
260 *output_format = yaz_oid_recsyn_sutrs;
264 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
267 wrbuf_destroy(wrbuf_str);
268 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
270 zebra_sort_type(zh->reg->sort_index, ord);
271 zebra_sort_sysno(zh->reg->sort_index, fi->sysno);
272 zebra_sort_read(zh->reg->sort_index, wrbuf_str);
274 while (off != wrbuf_len(wrbuf_str))
276 char dst_buf[IT_MAX_WORD];
277 assert(off < wrbuf_len(wrbuf_str));
278 zebra_term_untrans(zh, index_type, dst_buf,
279 wrbuf_buf(wrbuf_str)+off);
281 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
283 wrbuf_printf(wrbuf_result, " <index name=\"%s\"",
285 wrbuf_printf(wrbuf_result, " type=\"%s\">", index_type);
286 wrbuf_xmlputs(wrbuf_result, dst_buf);
287 wrbuf_printf(wrbuf_result, "</index>\n");
289 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
291 wrbuf_printf(wrbuf_result, "%s %s %s\n", string_index, index_type,
294 off += strlen(wrbuf_buf(wrbuf_str)+off) + 1;
296 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
298 wrbuf_printf(wrbuf_result, "</record>\n");
300 wrbuf_destroy(wrbuf_str);
305 static int special_index_fetch(
306 struct special_fetch_s *fi, const char *elemsetname,
307 const Odr_oid *input_format,
308 const Odr_oid **output_format,
309 WRBUF result, WRBUF addinfo,
312 const char *retrieval_index;
313 size_t retrieval_index_len;
314 const char *retrieval_type;
315 size_t retrieval_type_len;
316 zebra_rec_keys_t keys;
318 char retrieval_type_cstr[256];
319 ZebraHandle zh = fi->zh;
321 /* set output variables before processing possible error states */
324 /* only accept XML and SUTRS requests */
325 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml)
326 && oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
328 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
331 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
334 if (!parse_zebra_elem(elemsetname,
335 &retrieval_index, &retrieval_index_len,
336 &retrieval_type, &retrieval_type_len))
337 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
339 if (retrieval_type_len)
341 memcpy(retrieval_type_cstr, retrieval_type, retrieval_type_len);
342 retrieval_type_cstr[retrieval_type_len] = '\0';
345 if (retrieval_index_len)
347 char retrieval_index_cstr[256];
349 if (retrieval_index_len < sizeof(retrieval_index_cstr) -1)
351 memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
352 retrieval_index_cstr[retrieval_index_len] = '\0';
354 if (zebraExplain_lookup_attr_str(zh->reg->zei,
355 zinfo_index_category_index,
356 (retrieval_type_len == 0 ? 0 :
357 retrieval_type_cstr),
358 retrieval_index_cstr) == -1)
359 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
363 keys = zebra_rec_keys_open();
364 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
365 rec->size[recInfo_delKeys], 0);
367 if (!zebra_rec_keys_rewind(keys))
369 ret_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
375 struct it_key key_in;
376 WRBUF wrbuf = result;
378 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
380 *output_format = input_format;
381 wrbuf_printf(wrbuf, ZEBRA_XML_HEADER_STR
382 " sysno=\"" ZINT_FORMAT "\""
383 " set=\"zebra::index%s/\">\n",
384 fi->sysno, elemsetname);
386 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
387 *output_format = input_format;
389 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
392 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
393 const char *index_type;
395 const char *string_index = 0;
396 size_t string_index_len;
397 char dst_buf[IT_MAX_WORD];
399 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
401 string_index_len = strlen(string_index);
403 /* process only if index is not defined,
404 or if defined and matching */
405 if (retrieval_index == 0
406 || (string_index_len == retrieval_index_len
407 && !memcmp(string_index, retrieval_index,
410 /* process only if type is not defined, or is matching */
411 if (retrieval_type == 0
412 || !strcmp(retrieval_type_cstr, index_type))
414 if (zebra_term_untrans(zh, index_type, dst_buf, str))
415 *dst_buf = '\0'; /* untrans failed */
417 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
419 wrbuf_printf(wrbuf, " <index name=\"%s\"",
422 wrbuf_printf(wrbuf, " type=\"%s\"", index_type);
424 wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">",
425 key_in.mem[key_in.len -1]);
426 wrbuf_xmlputs(wrbuf, dst_buf);
427 wrbuf_printf(wrbuf, "</index>\n");
431 wrbuf_printf(wrbuf, "%s ", string_index);
433 wrbuf_printf(wrbuf, "%s", index_type);
435 for (i = 1; i < key_in.len; i++)
436 wrbuf_printf(wrbuf, " " ZINT_FORMAT,
439 wrbuf_printf(wrbuf, " %s", dst_buf);
441 wrbuf_printf(wrbuf, "\n");
448 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
449 wrbuf_printf(wrbuf, "</record>\n");
451 zebra_rec_keys_close(keys);
456 static void retrieve_puts_attr(WRBUF wrbuf, const char *name,
461 wrbuf_printf(wrbuf, " %s=\"", name);
462 wrbuf_xmlputs(wrbuf, value);
463 wrbuf_printf(wrbuf, "\"");
467 static void retrieve_puts_attr_int(WRBUF wrbuf, const char *name,
470 wrbuf_printf(wrbuf, " %s=\"%i\"", name, value);
473 static void retrieve_puts_str(WRBUF wrbuf, const char *name,
477 wrbuf_printf(wrbuf, "%s %s\n", name, value);
480 static void retrieve_puts_int(WRBUF wrbuf, const char *name,
483 wrbuf_printf(wrbuf, "%s %i\n", name, value);
487 static void snippet_xml_record(ZebraHandle zh, WRBUF wrbuf, zebra_snippets *doc)
489 const zebra_snippet_word *doc_w;
492 wrbuf_printf(wrbuf, "%s>\n", ZEBRA_XML_HEADER_STR);
493 for (doc_w = zebra_snippets_constlist(doc); doc_w; doc_w = doc_w->next)
497 const char *index_type;
499 const char *string_index = 0;
501 zebraExplain_lookup_ord(zh->reg->zei, doc_w->ord,
502 &index_type, &db, &string_index);
506 wrbuf_printf(wrbuf, " <snippet name=\"%s\"", string_index);
507 wrbuf_printf(wrbuf, " type=\"%s\">", index_type);
510 wrbuf_puts(wrbuf, "<s>");
511 /* not printing leading ws */
512 if (mark_state || !doc_w->ws || doc_w->match)
513 wrbuf_xmlputs(wrbuf, doc_w->term);
515 wrbuf_puts(wrbuf, "</s>");
517 else if (mark_state == 1)
519 wrbuf_puts(wrbuf, "</snippet>\n");
521 mark_state = doc_w->mark;
525 wrbuf_puts(wrbuf, "</snippet>\n");
527 wrbuf_printf(wrbuf, "</record>");
530 int zebra_get_rec_snippets(ZebraHandle zh, zint sysno,
531 zebra_snippets *snippets)
534 Record rec = rec_get(zh->reg->records, sysno);
537 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
538 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
542 const char *file_type = rec->info[recInfo_fileType];
543 void *recTypeClientData;
544 RecType rt = recType_byName(zh->reg->recTypes, zh->res,
545 file_type, &recTypeClientData);
548 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
551 struct ZebraRecStream stream;
552 return_code = zebra_create_record_stream(zh, &rec, &stream);
553 if (return_code == 0)
555 extract_snippet(zh, snippets, &stream,
556 rt, recTypeClientData);
558 stream.destroy(&stream);
566 static int snippet_fetch(
567 struct special_fetch_s *fi, const char *elemsetname,
568 const Odr_oid *input_format,
569 const Odr_oid **output_format,
570 WRBUF result, WRBUF addinfo)
572 ZebraHandle zh = fi->zh;
573 zebra_snippets *rec_snippets = zebra_snippets_create();
574 int return_code = zebra_get_rec_snippets(zh, fi->sysno, rec_snippets);
578 WRBUF wrbuf = result;
579 zebra_snippets *hit_snippet = zebra_snippets_create();
581 zebra_snippets_hit_vector(zh, fi->setname, fi->sysno, hit_snippet);
584 /* for debugging purposes */
585 yaz_log(YLOG_LOG, "---------------------------");
586 yaz_log(YLOG_LOG, "REC SNIPPET:");
587 zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
588 yaz_log(YLOG_LOG, "---------------------------");
589 yaz_log(YLOG_LOG, "HIT SNIPPET:");
590 zebra_snippets_log(hit_snippet, YLOG_LOG, 1);
593 zebra_snippets_ring(rec_snippets, hit_snippet, 5, 5);
596 yaz_log(YLOG_LOG, "---------------------------");
597 yaz_log(YLOG_LOG, "RING SNIPPET:");
598 zebra_snippets_log(rec_snippets, YLOG_LOG, 1);
600 snippet_xml_record(zh, wrbuf, rec_snippets);
602 *output_format = yaz_oid_recsyn_xml;
604 zebra_snippets_destroy(hit_snippet);
606 zebra_snippets_destroy(rec_snippets);
610 struct term_collect {
616 static zint freq_term(ZebraHandle zh, int ord, const char *term, RSET rset_set)
618 struct rset_key_control *kc = zebra_key_control_create(zh);
619 char ord_buf[IT_MAX_WORD];
620 int ord_len = key_SU_encode(ord, ord_buf);
623 NMEM nmem = nmem_create();
625 strcpy(ord_buf + ord_len, term);
627 info = dict_lookup(zh->reg->dict, ord_buf);
632 memcpy(&isam_p, info+1, sizeof(ISAM_P));
634 rsets[0] = zebra_create_rset_isam(zh, nmem, kc, kc->scope, isam_p, 0);
635 rsets[1] = rset_dup(rset_set);
637 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
639 zebra_count_set(zh, rset, &hits, zh->approx_limit);
641 rset_delete(rsets[0]);
649 static int term_qsort_handle(const void *a, const void *b)
651 const struct term_collect *l = a;
652 const struct term_collect *r = b;
653 if (l->set_occur < r->set_occur)
655 else if (l->set_occur > r->set_occur)
659 const char *lterm = l->term ? l->term : "";
660 const char *rterm = r->term ? r->term : "";
661 return strcmp(lterm, rterm);
665 static void term_collect_freq(ZebraHandle zh,
666 struct term_collect *col, int no_terms_collect,
670 for (i = 0; i < no_terms_collect; i++)
673 col[i].set_occur = freq_term(zh, ord, col[i].term, rset);
675 qsort(col, no_terms_collect, sizeof(*col), term_qsort_handle);
678 static struct term_collect *term_collect_create(zebra_strmap_t sm,
679 int no_terms_collect,
686 struct term_collect *col = nmem_malloc(nmem,
687 sizeof *col *no_terms_collect);
689 for (i = 0; i < no_terms_collect; i++)
693 col[i].set_occur = 0;
695 /* iterate over terms and collect the most frequent ones */
696 it = zebra_strmap_it_create(sm);
697 while ((term = zebra_strmap_it_next(it, &data_buf, &data_len)))
700 col[0] has lowest oc . col[no_terms_collect-1] has highest oc */
701 int oc = *(int*) data_buf;
703 /* insertion may be slow but terms terms will be "infrequent" and
704 thus number of iterations should be small below
706 while (j < no_terms_collect && oc > col[j].oc)
709 { /* oc <= col[j] and oc > col[j-1] */
711 memmove(col, col+1, sizeof(*col) * j);
716 zebra_strmap_it_destroy(it);
720 static int facet_fetch(
721 struct special_fetch_s *fi, const char *elemsetname,
722 const Odr_oid *input_format,
723 const Odr_oid **output_format,
724 WRBUF result, WRBUF addinfo)
728 int num_recs = 10; /* number of records to analyze */
730 ZebraMetaRecord *poset;
731 ZEBRA_RES ret = ZEBRA_OK;
736 struct index_spec *spec, *spec_list;
738 ZebraHandle zh = fi->zh;
740 res_get_int(zh->res, "facetNumRecs", &num_recs);
741 res_get_int(zh->res, "facetMaxChunks", &max_chunks);
743 /* see if XML is required for response */
744 if (oid_oidcmp(input_format, yaz_oid_recsyn_xml) == 0)
747 spec_list = parse_index_spec(elemsetname, fi->nmem, &error);
749 if (!spec_list || error)
751 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
754 for (spec = spec_list; spec; spec = spec->next)
756 if (!spec->index_type)
757 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
761 ord_array = nmem_malloc(fi->nmem, sizeof(*ord_array) * no_ord);
763 for (spec = spec_list, i = 0; spec; spec = spec->next, i++)
765 int ord = zebraExplain_lookup_attr_str(zh->reg->zei,
766 zinfo_index_category_index,
771 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
775 pos_array = (zint *) nmem_malloc(fi->nmem, num_recs * sizeof(*pos_array));
776 for (i = 0; i < num_recs; i++)
778 poset = zebra_meta_records_create(zh, fi->setname, num_recs, pos_array);
781 wrbuf_puts(addinfo, fi->setname);
782 return YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
786 yaz_timing_t timing = yaz_timing_create();
787 zebra_strmap_t *map_array
788 = nmem_malloc(fi->nmem, sizeof *map_array * no_ord);
789 for (i = 0; i < no_ord; i++)
790 map_array[i] = zebra_strmap_create();
792 for (i = 0; i < num_recs; i++)
795 zint sysnos[MAX_SYSNOS_PER_RECORD];
796 int no_sysnos = MAX_SYSNOS_PER_RECORD;
799 ret = zebra_result_recid_to_sysno(zh, fi->setname,
802 assert(no_sysnos > 0);
803 yaz_log(YLOG_DEBUG, "Analyzing rec=%d ISAM sysno=" ZINT_FORMAT " chunks=%d",
804 i, poset[i].sysno, no_sysnos);
805 for (j = 0; j < no_sysnos && j < max_chunks; j++)
809 struct it_key key_in;
810 Record rec = rec_get(zh->reg->records, sysnos[j]);
811 zebra_rec_keys_t keys = zebra_rec_keys_open();
812 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
813 rec->size[recInfo_delKeys], 0);
815 yaz_log(YLOG_DEBUG, "rec %d " ZINT_FORMAT " %s",
816 j, sysnos[j], zebra_rec_keys_empty(keys) ? "empty" : "non-empty");
817 if (zebra_rec_keys_rewind(keys))
819 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
822 struct index_spec *spec;
823 for (spec = spec_list, i = 0; i < no_ord;
824 i++, spec = spec->next)
826 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
827 if (ord == ord_array[i] &&
828 str[0] != FIRST_IN_FIELD_CHAR)
831 zebra_strmap_t sm = map_array[i];
833 freq = zebra_strmap_lookup(sm, str, 0, 0);
839 zebra_strmap_add(sm, str, &v, sizeof v);
845 zebra_rec_keys_close(keys);
849 yaz_timing_stop(timing);
850 yaz_log(YLOG_LOG, "facet first phase real=%4.2f",
851 yaz_timing_get_real(timing));
852 yaz_timing_start(timing);
854 wrbuf_puts(wr, "<facets>\n");
855 for (spec = spec_list, i = 0; i < no_ord; i++, spec = spec->next)
858 NMEM nmem = nmem_create();
859 struct term_collect *col;
860 int no_collect_terms = 20;
863 no_collect_terms = atoi(spec->extra);
864 if (no_collect_terms < 1)
865 no_collect_terms = 1;
866 col = term_collect_create(map_array[i], no_collect_terms, nmem);
867 term_collect_freq(zh, col, no_collect_terms, ord_array[i],
868 resultSetRef(zh, fi->setname));
871 wrbuf_printf(wr, " <facet type=\"%s\" index=\"%s\">\n",
872 spec->index_type, spec->index_name);
874 wrbuf_printf(wr, "facet %s %s\n",
875 spec->index_type, spec->index_name);
876 for (j = 0; j < no_collect_terms; j++)
880 char dst_buf[IT_MAX_WORD];
881 zebra_term_untrans(zh, spec->index_type, dst_buf, col[j].term);
884 wrbuf_printf(wr, " <term coccur=\"%d\"", col[j].oc);
885 if (col[j].set_occur)
886 wrbuf_printf(wr, " occur=\"" ZINT_FORMAT "\"",
888 wrbuf_printf(wr, ">");
889 wrbuf_xmlputs(wr, dst_buf);
890 wrbuf_printf(wr, "</term>\n");
894 wrbuf_printf(wr, "term %d", col[j].oc);
895 if (col[j].set_occur)
896 wrbuf_printf(wr, " " ZINT_FORMAT,
898 wrbuf_printf(wr, ": %s\n", dst_buf);
903 wrbuf_puts(wr, " </facet>\n");
907 wrbuf_puts(wr, "</facets>\n");
908 for (i = 0; i < no_ord; i++)
909 zebra_strmap_destroy(map_array[i]);
910 yaz_timing_stop(timing);
911 yaz_log(YLOG_LOG, "facet second phase real=%4.2f",
912 yaz_timing_get_real(timing));
913 yaz_timing_destroy(&timing);
915 *output_format = yaz_oid_recsyn_xml;
916 zebra_meta_records_destroy(zh, poset, num_recs);
921 static int zebra_special_fetch(
922 void *handle, const char *elemsetname,
923 const Odr_oid *input_format,
924 const Odr_oid **output_format,
925 WRBUF result, WRBUF addinfo)
928 struct special_fetch_s *fi = (struct special_fetch_s *) handle;
929 ZebraHandle zh = fi->zh;
930 zint sysno = fi->sysno;
932 /* processing zebra::facet */
933 if (elemsetname && 0 == strncmp(elemsetname, "facet", 5))
935 return facet_fetch(fi, elemsetname + 5,
936 input_format, output_format,
940 if (elemsetname && 0 == strcmp(elemsetname, "snippet"))
942 return snippet_fetch(fi, elemsetname + 7,
943 input_format, output_format,
947 /* processing zebra::meta::sysno */
948 if (elemsetname && 0 == strcmp(elemsetname, "meta::sysno"))
951 if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
953 wrbuf_printf(result, ZINT_FORMAT, fi->sysno);
954 *output_format = input_format;
956 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
958 wrbuf_printf(result, ZEBRA_XML_HEADER_STR
959 " sysno=\"" ZINT_FORMAT "\"/>\n",
961 *output_format = input_format;
964 ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
968 /* processing special elementsetname zebra::index:: for sort elements */
969 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
971 int ret = sort_fetch(
973 input_format, output_format,
977 /* not a sort index so we continue to get the full record */
981 /* fetching binary record up for all other display elementsets */
982 rec = rec_get(zh->reg->records, sysno);
985 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
986 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
989 /* processing special elementsetnames zebra::data */
990 if (elemsetname && 0 == strcmp(elemsetname, "data"))
992 struct ZebraRecStream stream;
993 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
996 zebra_create_record_stream(zh, &rec, &stream);
997 *output_format = input_format;
999 b = nmem_malloc(fi->nmem, recordAttr->recordSize);
1000 stream.readf(&stream, b, recordAttr->recordSize);
1001 wrbuf_write(result, b, recordAttr->recordSize);
1003 stream.destroy(&stream);
1008 /* processing special elementsetnames zebra::meta:: */
1009 if (elemsetname && 0 == strcmp(elemsetname, "meta"))
1012 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
1014 if (!oid_oidcmp(input_format, yaz_oid_recsyn_xml))
1016 *output_format = input_format;
1018 wrbuf_printf(result, ZEBRA_XML_HEADER_STR
1019 " sysno=\"" ZINT_FORMAT "\"", sysno);
1020 retrieve_puts_attr(result, "base", rec->info[recInfo_databaseName]);
1021 retrieve_puts_attr(result, "file", rec->info[recInfo_filename]);
1022 retrieve_puts_attr(result, "type", rec->info[recInfo_fileType]);
1024 retrieve_puts_attr_int(result, "score", fi->score);
1026 wrbuf_printf(result,
1027 " rank=\"" ZINT_FORMAT "\""
1029 " set=\"zebra::%s\"/>\n",
1030 recordAttr->staticrank,
1031 recordAttr->recordSize,
1034 else if (!oid_oidcmp(input_format, yaz_oid_recsyn_sutrs))
1036 *output_format = input_format;
1037 wrbuf_printf(result, "sysno " ZINT_FORMAT "\n", sysno);
1038 retrieve_puts_str(result, "base", rec->info[recInfo_databaseName]);
1039 retrieve_puts_str(result, "file", rec->info[recInfo_filename]);
1040 retrieve_puts_str(result, "type", rec->info[recInfo_fileType]);
1042 retrieve_puts_int(result, "score", fi->score);
1044 wrbuf_printf(result,
1045 "rank " ZINT_FORMAT "\n"
1048 recordAttr->staticrank,
1049 recordAttr->recordSize,
1053 ret = YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
1059 /* processing special elementsetnames zebra::index:: */
1060 if (elemsetname && 0 == strncmp(elemsetname, "index", 5))
1062 int ret = special_index_fetch(
1063 fi, elemsetname + 5,
1064 input_format, output_format,
1065 result, addinfo, rec);
1072 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1075 int zebra_record_fetch(ZebraHandle zh, const char *setname,
1076 zint sysno, int score,
1078 const Odr_oid *input_format, Z_RecordComposition *comp,
1079 const Odr_oid **output_format,
1080 char **rec_bufp, int *rec_lenp, char **basenamep,
1084 char *fname, *file_type, *basename;
1085 const char *elemsetname;
1086 struct ZebraRecStream stream;
1087 RecordAttr *recordAttr;
1089 int return_code = 0;
1090 zint sysnos[MAX_SYSNOS_PER_RECORD];
1091 int no_sysnos = MAX_SYSNOS_PER_RECORD;
1093 struct special_fetch_s fetch_info;
1095 res = zebra_result_recid_to_sysno(zh, setname, sysno, sysnos, &no_sysnos);
1096 if (res != ZEBRA_OK)
1102 elemsetname = yaz_get_esn(comp);
1105 fetch_info.setname = setname;
1106 fetch_info.sysno = sysno;
1107 fetch_info.score = score;
1108 fetch_info.nmem = odr->mem;
1110 /* processing zebra special elementset names of form 'zebra:: */
1111 if (elemsetname && 0 == strncmp(elemsetname, "zebra::", 7))
1113 WRBUF result = wrbuf_alloc();
1114 WRBUF addinfo_w = wrbuf_alloc();
1115 int r = zebra_special_fetch(&fetch_info, elemsetname + 7,
1116 input_format, output_format,
1120 *rec_bufp = odr_strdup(odr, wrbuf_cstr(result));
1121 *rec_lenp = wrbuf_len(result);
1125 if (wrbuf_len(addinfo_w))
1126 *addinfo = odr_strdup(odr, wrbuf_cstr(addinfo_w));
1128 wrbuf_destroy(result);
1129 wrbuf_destroy(addinfo_w);
1133 /* processing all other element set names */
1134 rec = rec_get(zh->reg->records, sysno);
1137 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
1139 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1143 recordAttr = rec_init_attr(zh->reg->zei, rec);
1145 file_type = rec->info[recInfo_fileType];
1146 fname = rec->info[recInfo_filename];
1147 basename = rec->info[recInfo_databaseName];
1148 *basenamep = (char *) odr_malloc(odr, strlen(basename)+1);
1149 strcpy(*basenamep, basename);
1151 yaz_log(YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
1154 return_code = zebra_create_record_stream(zh, &rec, &stream);
1159 struct recRetrieveCtrl retrieveCtrl;
1161 retrieveCtrl.stream = &stream;
1162 retrieveCtrl.fname = fname;
1163 retrieveCtrl.localno = sysno;
1164 retrieveCtrl.staticrank = recordAttr->staticrank;
1165 retrieveCtrl.score = score;
1166 retrieveCtrl.recordSize = recordAttr->recordSize;
1167 retrieveCtrl.odr = odr;
1168 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
1169 retrieveCtrl.comp = comp;
1170 retrieveCtrl.encoding = zh->record_encoding;
1171 retrieveCtrl.diagnostic = 0;
1172 retrieveCtrl.addinfo = 0;
1173 retrieveCtrl.dh = zh->reg->dh;
1174 retrieveCtrl.res = zh->res;
1175 retrieveCtrl.rec_buf = 0;
1176 retrieveCtrl.rec_len = -1;
1177 retrieveCtrl.handle = &fetch_info;
1178 retrieveCtrl.special_fetch = zebra_special_fetch;
1180 if (!(rt = recType_byName(zh->reg->recTypes, zh->res,
1181 file_type, &clientData)))
1183 char addinfo_str[100];
1185 sprintf(addinfo_str, "Could not handle record type %.40s",
1188 *addinfo = odr_strdup(odr, addinfo_str);
1189 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1193 (*rt->retrieve)(clientData, &retrieveCtrl);
1194 return_code = retrieveCtrl.diagnostic;
1196 *output_format = retrieveCtrl.output_format;
1197 *rec_bufp = (char *) retrieveCtrl.rec_buf;
1198 *rec_lenp = retrieveCtrl.rec_len;
1199 *addinfo = retrieveCtrl.addinfo;
1202 stream.destroy(&stream);
1212 * indent-tabs-mode: nil
1214 * vim: shiftwidth=4 tabstop=8 expandtab