1 /* $Id: retrieve.c,v 1.49 2006-11-14 14:32:13 marc 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, SYSNO 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 /* int return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS; */
126 WRBUF wrbuf = wrbuf_alloc();
127 zebra_rec_keys_t keys;
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_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
138 keys = zebra_rec_keys_open();
139 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
140 rec->size[recInfo_delKeys], 0);
142 parse_zebra_elem(elemsetname,
143 &retrieval_index, &retrieval_index_len,
144 &retrieval_type, &retrieval_type_len);
146 if (zebra_rec_keys_rewind(keys)){
149 struct it_key key_in;
151 if (input_format == VAL_TEXT_XML){
152 *output_format = VAL_TEXT_XML;
153 /*wrbuf_printf(wrbuf,
154 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");*/
158 "\"http://www.indexdata.com/zebra/\""
159 " sysno=\"" ZINT_FORMAT "\""
160 " set=\"zebra::index::%s\">\n",
163 else if (input_format == VAL_SUTRS)
164 *output_format = VAL_SUTRS;
167 while(zebra_rec_keys_read(keys, &str, &slen, &key_in)){
169 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
172 const char *string_index = 0;
173 size_t string_index_len;
174 char dst_buf[IT_MAX_WORD];
176 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
178 string_index_len = strlen(string_index);
179 if (retrieval_index == 0
180 || (string_index_len == retrieval_index_len
181 && !memcmp(string_index, retrieval_index,
184 if (retrieval_type == 0
185 || (retrieval_type_len == 1
186 && retrieval_type[0] == index_type)){
188 if (input_format == VAL_TEXT_XML){
189 wrbuf_printf(wrbuf, " <index name=\"%s\"",
192 wrbuf_printf(wrbuf, " type=\"%c\"", index_type);
194 wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">",
195 key_in.mem[key_in.len -1]);
197 zebra_term_untrans(zh, index_type, dst_buf, str);
198 wrbuf_xmlputs(wrbuf, dst_buf);
199 wrbuf_printf(wrbuf, "</index>\n");
201 else if (input_format == VAL_SUTRS){
202 wrbuf_printf(wrbuf, "%s ", string_index);
204 wrbuf_printf(wrbuf, "%c", index_type);
206 for (i = 1; i < key_in.len; i++)
207 wrbuf_printf(wrbuf, " " ZINT_FORMAT,
210 zebra_term_untrans(zh, index_type, dst_buf, str);
211 wrbuf_printf(wrbuf, " %s", dst_buf);
213 wrbuf_printf(wrbuf, "\n");
218 if (input_format == VAL_TEXT_XML)
219 wrbuf_printf(wrbuf, "</record>\n");
222 *rec_lenp = wrbuf_len(wrbuf);
223 *rec_bufp = odr_malloc(odr, *rec_lenp);
224 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
225 wrbuf_free(wrbuf, 1);
226 zebra_rec_keys_close(keys);
231 int zebra_special_fetch(ZebraHandle zh, SYSNO sysno, ODR odr,
232 const char *elemsetname,
233 oid_value input_format,
234 oid_value *output_format,
235 char **rec_bufp, int *rec_lenp)
239 /* only accept XML and SUTRS requests */
240 if (input_format != VAL_TEXT_XML
241 && input_format != VAL_SUTRS){
242 yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s",
244 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
247 /* processing zebra::meta::sysno elemset without fetching binary data */
248 if (elemsetname && 0 == strcmp(elemsetname, "meta::sysno"))
251 if (input_format == VAL_SUTRS){
252 sprintf(rec_str, ZINT_FORMAT, sysno);
253 *output_format = VAL_SUTRS;
255 else if (input_format == VAL_TEXT_XML){
256 sprintf(rec_str, "<record xmlns="
257 "\"http://www.indexdata.com/zebra/\""
258 " sysno=\"" ZINT_FORMAT "\""
259 " set=\"zebra::%s\">\n",
261 *output_format = VAL_TEXT_XML;
263 *rec_lenp = strlen(rec_str);
265 *rec_bufp = odr_strdup(odr, rec_str);
268 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
272 /* fetching binary record up for all other display elementsets */
273 rec = rec_get(zh->reg->records, sysno);
275 yaz_log(YLOG_DEBUG, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
276 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
279 /* processing special elementsetnames zebra::index:: */
280 if (elemsetname && 0 == strncmp(elemsetname, "index::", 7)){
282 int ret = zebra_special_index_fetch(zh, sysno, odr, rec,
284 input_format, output_format,
291 /* processing special elementsetnames zebra::data */
292 if (elemsetname && 0 == strcmp(elemsetname, "data")){
293 struct ZebraRecStream stream;
294 RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec);
295 zebra_create_record_stream(zh, &rec, &stream);
296 *output_format = VAL_SUTRS;
297 *rec_lenp = recordAttr->recordSize;
298 *rec_bufp = (char *) odr_malloc(odr, *rec_lenp);
299 stream.readf(&stream, *rec_bufp, *rec_lenp);
300 stream.destroy(&stream);
307 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
311 int zebra_record_fetch(ZebraHandle zh, SYSNO sysno, int score,
312 zebra_snippets *hit_snippet, ODR odr,
313 oid_value input_format, Z_RecordComposition *comp,
314 oid_value *output_format,
315 char **rec_bufp, int *rec_lenp, char **basenamep,
319 char *fname, *file_type, *basename;
320 const char *elemsetname;
321 struct ZebraRecStream stream;
322 RecordAttr *recordAttr;
328 elemsetname = yaz_get_esn(comp);
330 /* processing zebra special elementset names of form 'zebra:: */
331 if (elemsetname && 0 == strncmp(elemsetname, "zebra::", 7))
332 return zebra_special_fetch(zh, sysno, odr,
334 input_format, output_format,
338 /* processing all other element set names */
339 rec = rec_get(zh->reg->records, sysno);
342 yaz_log(YLOG_DEBUG, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
344 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
348 recordAttr = rec_init_attr(zh->reg->zei, rec);
350 file_type = rec->info[recInfo_fileType];
351 fname = rec->info[recInfo_filename];
352 basename = rec->info[recInfo_databaseName];
353 *basenamep = (char *) odr_malloc (odr, strlen(basename)+1);
354 strcpy (*basenamep, basename);
357 yaz_log (YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
360 zebra_create_record_stream(zh, &rec, &stream);
364 zebra_snippets *snippet;
365 zebra_rec_keys_t reckeys = zebra_rec_keys_open();
367 struct recRetrieveCtrl retrieveCtrl;
369 retrieveCtrl.stream = &stream;
370 retrieveCtrl.fname = fname;
371 retrieveCtrl.localno = sysno;
372 retrieveCtrl.staticrank = recordAttr->staticrank;
373 retrieveCtrl.score = score;
374 retrieveCtrl.recordSize = recordAttr->recordSize;
375 retrieveCtrl.odr = odr;
376 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
377 retrieveCtrl.comp = comp;
378 retrieveCtrl.encoding = zh->record_encoding;
379 retrieveCtrl.diagnostic = 0;
380 retrieveCtrl.addinfo = 0;
381 retrieveCtrl.dh = zh->reg->dh;
382 retrieveCtrl.res = zh->res;
383 retrieveCtrl.rec_buf = 0;
384 retrieveCtrl.rec_len = -1;
385 retrieveCtrl.hit_snippet = hit_snippet;
386 retrieveCtrl.doc_snippet = zebra_snippets_create();
388 zebra_rec_keys_set_buf(reckeys,
389 rec->info[recInfo_delKeys],
390 rec->size[recInfo_delKeys],
392 zebra_rec_keys_to_snippets(zh, reckeys, retrieveCtrl.doc_snippet);
393 zebra_rec_keys_close(reckeys);
396 /* for debugging purposes */
397 yaz_log(YLOG_LOG, "DOC SNIPPET:");
398 zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
399 yaz_log(YLOG_LOG, "HIT SNIPPET:");
400 zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
402 snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
403 retrieveCtrl.hit_snippet,
406 /* for debugging purposes */
407 yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
408 zebra_snippets_log(snippet, YLOG_LOG);
411 if (!(rt = recType_byName (zh->reg->recTypes, zh->res,
412 file_type, &clientData)))
414 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
418 (*rt->retrieve)(clientData, &retrieveCtrl);
419 return_code = retrieveCtrl.diagnostic;
421 *output_format = retrieveCtrl.output_format;
422 *rec_bufp = (char *) retrieveCtrl.rec_buf;
423 *rec_lenp = retrieveCtrl.rec_len;
424 *addinfo = retrieveCtrl.addinfo;
426 zebra_snippets_destroy(snippet);
427 zebra_snippets_destroy(retrieveCtrl.doc_snippet);
430 stream.destroy(&stream);
439 * indent-tabs-mode: nil
441 * vim: shiftwidth=4 tabstop=8 expandtab