1 /* $Id: retrieve.c,v 1.55 2006-11-21 22:17:49 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <yaz/diagbib1.h>
42 static int zebra_create_record_stream(ZebraHandle zh,
44 struct ZebraRecStream *stream){
46 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, *rec);
48 if ((*rec)->size[recInfo_storeData] > 0){
49 zebra_create_stream_mem(stream, (*rec)->info[recInfo_storeData],
50 (*rec)->size[recInfo_storeData]);
56 if (zh->path_reg && !yaz_is_abspath((*rec)->info[recInfo_filename])){
57 strcpy(full_rep, zh->path_reg);
58 strcat(full_rep, "/");
59 strcat(full_rep, (*rec)->info[recInfo_filename]);
62 strcpy(full_rep, (*rec)->info[recInfo_filename]);
64 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1){
65 yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
70 zebra_create_stream_fd(stream, fd, recordAttr->recordOffset);
77 static void parse_zebra_elem(const char *elem,
78 const char **index, size_t *index_len,
79 const char **type, size_t *type_len)
89 const char *cp = strchr(elem, ':');
91 if (!cp) /* no colon */
94 *index_len = strlen(elem);
96 else if (cp[1] == '\0') /* 'index:' */
99 *index_len = cp - elem;
104 *index_len = cp - elem;
106 *type_len = strlen(cp+1);
112 int zebra_special_index_fetch(ZebraHandle zh, zint sysno, ODR odr,
114 const char *elemsetname,
115 oid_value input_format,
116 oid_value *output_format,
117 char **rec_bufp, int *rec_lenp)
119 const char *retrieval_index;
120 size_t retrieval_index_len;
121 const char *retrieval_type;
122 size_t retrieval_type_len;
124 zebra_rec_keys_t keys;
126 /* set output variables before processing possible error states */
129 /* only accept XML and SUTRS requests */
130 if (input_format != VAL_TEXT_XML
131 && input_format != VAL_SUTRS){
132 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
134 *output_format = VAL_NONE;
135 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
138 parse_zebra_elem(elemsetname,
139 &retrieval_index, &retrieval_index_len,
140 &retrieval_type, &retrieval_type_len);
142 if (retrieval_type_len != 0 && retrieval_type_len != 1)
144 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
147 if (retrieval_index_len)
149 char retrieval_index_cstr[256];
151 if (retrieval_index_len < sizeof(retrieval_index_cstr) -1)
153 memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
154 retrieval_index_cstr[retrieval_index_len] = '\0';
156 if (zebraExplain_lookup_attr_str(zh->reg->zei,
157 zinfo_index_category_index,
158 (retrieval_type_len == 0 ? -1 :
160 retrieval_index_cstr) == -1)
162 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
167 keys = zebra_rec_keys_open();
168 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
169 rec->size[recInfo_delKeys], 0);
171 wrbuf = wrbuf_alloc();
172 if (zebra_rec_keys_rewind(keys)){
175 struct it_key key_in;
177 if (input_format == VAL_TEXT_XML){
178 *output_format = VAL_TEXT_XML;
179 /*wrbuf_printf(wrbuf,
180 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");*/
184 "\"http://www.indexdata.com/zebra/\""
185 " sysno=\"" ZINT_FORMAT "\""
186 " set=\"zebra::index::%s/\">\n",
189 else if (input_format == VAL_SUTRS)
190 *output_format = VAL_SUTRS;
193 while(zebra_rec_keys_read(keys, &str, &slen, &key_in)){
195 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
198 const char *string_index = 0;
199 size_t string_index_len;
200 char dst_buf[IT_MAX_WORD];
202 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
204 string_index_len = strlen(string_index);
205 if (retrieval_index == 0
206 || (string_index_len == retrieval_index_len
207 && !memcmp(string_index, retrieval_index,
210 if (retrieval_type == 0
211 || (retrieval_type_len == 1
212 && retrieval_type[0] == index_type)){
214 if (input_format == VAL_TEXT_XML){
215 wrbuf_printf(wrbuf, " <index name=\"%s\"",
218 wrbuf_printf(wrbuf, " type=\"%c\"", index_type);
220 wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">",
221 key_in.mem[key_in.len -1]);
223 zebra_term_untrans(zh, index_type, dst_buf, str);
224 wrbuf_xmlputs(wrbuf, dst_buf);
225 wrbuf_printf(wrbuf, "</index>\n");
227 else if (input_format == VAL_SUTRS){
228 wrbuf_printf(wrbuf, "%s ", string_index);
230 wrbuf_printf(wrbuf, "%c", index_type);
232 for (i = 1; i < key_in.len; i++)
233 wrbuf_printf(wrbuf, " " ZINT_FORMAT,
236 zebra_term_untrans(zh, index_type, dst_buf, str);
237 wrbuf_printf(wrbuf, " %s", dst_buf);
239 wrbuf_printf(wrbuf, "\n");
244 if (input_format == VAL_TEXT_XML)
245 wrbuf_printf(wrbuf, "</record>\n");
247 *rec_lenp = wrbuf_len(wrbuf);
248 *rec_bufp = odr_malloc(odr, *rec_lenp);
249 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
250 wrbuf_free(wrbuf, 1);
251 zebra_rec_keys_close(keys);
256 int zebra_special_fetch(ZebraHandle zh, zint sysno, ODR odr,
257 const char *elemsetname,
258 oid_value input_format,
259 oid_value *output_format,
260 char **rec_bufp, int *rec_lenp)
264 /* set output variables before processing possible error states */
267 /* only accept XML and SUTRS requests */
268 if (input_format != VAL_TEXT_XML
269 && input_format != VAL_SUTRS){
270 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
272 return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
275 /* processing zebra::meta::sysno elemset without fetching binary data */
276 if (elemsetname && 0 == strcmp(elemsetname, "meta::sysno"))
279 if (input_format == VAL_SUTRS){
280 sprintf(rec_str, ZINT_FORMAT, sysno);
281 *output_format = VAL_SUTRS;
283 else if (input_format == VAL_TEXT_XML){
284 sprintf(rec_str, "<record xmlns="
285 "\"http://www.indexdata.com/zebra/\""
286 " sysno=\"" ZINT_FORMAT "\""
287 " set=\"zebra::%s\"/>\n",
289 *output_format = VAL_TEXT_XML;
291 *rec_lenp = strlen(rec_str);
293 *rec_bufp = odr_strdup(odr, rec_str);
296 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
300 /* fetching binary record up for all other display elementsets */
301 rec = rec_get(zh->reg->records, sysno);
303 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
304 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
307 /* processing special elementsetnames zebra::index:: */
308 if (elemsetname && 0 == strncmp(elemsetname, "index::", 7)){
310 int ret = zebra_special_index_fetch(zh, sysno, odr, rec,
312 input_format, output_format,
319 /* processing special elementsetnames zebra::data */
320 if (elemsetname && 0 == strcmp(elemsetname, "data")){
321 struct ZebraRecStream stream;
322 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
323 zebra_create_record_stream(zh, &rec, &stream);
324 *output_format = input_format;
325 *rec_lenp = recordAttr->recordSize;
326 *rec_bufp = (char *) odr_malloc(odr, *rec_lenp);
327 stream.readf(&stream, *rec_bufp, *rec_lenp);
328 stream.destroy(&stream);
335 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
339 int zebra_record_fetch(ZebraHandle zh, zint sysno, int score,
340 zebra_snippets *hit_snippet, ODR odr,
341 oid_value input_format, Z_RecordComposition *comp,
342 oid_value *output_format,
343 char **rec_bufp, int *rec_lenp, char **basenamep,
347 char *fname, *file_type, *basename;
348 const char *elemsetname;
349 struct ZebraRecStream stream;
350 RecordAttr *recordAttr;
356 elemsetname = yaz_get_esn(comp);
358 /* processing zebra special elementset names of form 'zebra:: */
359 if (elemsetname && 0 == strncmp(elemsetname, "zebra::", 7))
360 return zebra_special_fetch(zh, sysno, odr,
362 input_format, output_format,
366 /* processing all other element set names */
367 rec = rec_get(zh->reg->records, sysno);
370 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
372 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
376 recordAttr = rec_init_attr(zh->reg->zei, rec);
378 file_type = rec->info[recInfo_fileType];
379 fname = rec->info[recInfo_filename];
380 basename = rec->info[recInfo_databaseName];
381 *basenamep = (char *) odr_malloc (odr, strlen(basename)+1);
382 strcpy (*basenamep, basename);
384 yaz_log(YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
387 zebra_create_record_stream(zh, &rec, &stream);
391 zebra_snippets *snippet;
392 zebra_rec_keys_t reckeys = zebra_rec_keys_open();
394 struct recRetrieveCtrl retrieveCtrl;
396 retrieveCtrl.stream = &stream;
397 retrieveCtrl.fname = fname;
398 retrieveCtrl.localno = sysno;
399 retrieveCtrl.staticrank = recordAttr->staticrank;
400 retrieveCtrl.score = score;
401 retrieveCtrl.recordSize = recordAttr->recordSize;
402 retrieveCtrl.odr = odr;
403 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
404 retrieveCtrl.comp = comp;
405 retrieveCtrl.encoding = zh->record_encoding;
406 retrieveCtrl.diagnostic = 0;
407 retrieveCtrl.addinfo = 0;
408 retrieveCtrl.dh = zh->reg->dh;
409 retrieveCtrl.res = zh->res;
410 retrieveCtrl.rec_buf = 0;
411 retrieveCtrl.rec_len = -1;
412 retrieveCtrl.hit_snippet = hit_snippet;
413 retrieveCtrl.doc_snippet = zebra_snippets_create();
415 zebra_rec_keys_set_buf(reckeys,
416 rec->info[recInfo_delKeys],
417 rec->size[recInfo_delKeys],
419 zebra_rec_keys_to_snippets(zh, reckeys, retrieveCtrl.doc_snippet);
420 zebra_rec_keys_close(reckeys);
423 /* for debugging purposes */
424 yaz_log(YLOG_LOG, "DOC SNIPPET:");
425 zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
426 yaz_log(YLOG_LOG, "HIT SNIPPET:");
427 zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
429 snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
430 retrieveCtrl.hit_snippet,
433 /* for debugging purposes */
434 yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
435 zebra_snippets_log(snippet, YLOG_LOG);
438 if (!(rt = recType_byName(zh->reg->recTypes, zh->res,
439 file_type, &clientData)))
441 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
445 (*rt->retrieve)(clientData, &retrieveCtrl);
446 return_code = retrieveCtrl.diagnostic;
448 *output_format = retrieveCtrl.output_format;
449 *rec_bufp = (char *) retrieveCtrl.rec_buf;
450 *rec_lenp = retrieveCtrl.rec_len;
451 *addinfo = retrieveCtrl.addinfo;
454 zebra_snippets_destroy(snippet);
455 zebra_snippets_destroy(retrieveCtrl.doc_snippet);
458 stream.destroy(&stream);
467 * indent-tabs-mode: nil
469 * vim: shiftwidth=4 tabstop=8 expandtab