1 /* $Id: retrieve.c,v 1.47 2006-11-13 09:07:05 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>
39 static void parse_zebra_elem(const char *elem,
40 const char **index, size_t *index_len,
41 const char **type, size_t *type_len)
51 const char *cp = strchr(elem, ':');
53 if (!cp) /* no colon */
56 *index_len = strlen(elem);
58 else if (cp[1] == '\0') /* 'index:' */
61 *index_len = cp - elem;
66 *index_len = cp - elem;
68 *type_len = strlen(cp+1);
73 int zebra_storekeys_fetch(ZebraHandle zh, SYSNO sysno, ODR odr,
75 const char *element_set,
76 oid_value input_format,
77 oid_value *output_format,
78 char **rec_bufp, int *rec_lenp)
80 const char *retrieval_index;
81 size_t retrieval_index_len;
82 const char *retrieval_type;
83 size_t retrieval_type_len;
85 WRBUF wrbuf = wrbuf_alloc();
86 zebra_rec_keys_t keys = zebra_rec_keys_open();
87 zebra_rec_keys_set_buf(keys,
88 rec->info[recInfo_delKeys],
89 rec->size[recInfo_delKeys],
92 yaz_log(YLOG_LOG, "element_set=%s", element_set);
94 parse_zebra_elem(element_set,
95 &retrieval_index, &retrieval_index_len,
96 &retrieval_type, &retrieval_type_len);
99 if (input_format == VAL_TEXT_XML)
101 yaz_log(YLOG_LOG, "want XML output");
103 else if (input_format == VAL_SUTRS)
105 yaz_log(YLOG_LOG, "want SUTRS output");
109 yaz_log(YLOG_LOG, "unsupported.. We must produce an error");
111 if (zebra_rec_keys_rewind(keys))
115 struct it_key key_in;
116 while(zebra_rec_keys_read(keys, &str, &slen, &key_in))
119 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
122 const char *string_index = 0;
123 size_t string_index_len;
124 char dst_buf[IT_MAX_WORD];
126 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
128 string_index_len = strlen(string_index);
129 if (retrieval_index == 0
130 || (string_index_len == retrieval_index_len
131 && !memcmp(string_index, retrieval_index,
135 if (retrieval_type == 0
136 || (retrieval_type_len == 1
137 && retrieval_type[0] == index_type))
140 wrbuf_printf(wrbuf, "%s ", string_index);
142 wrbuf_printf(wrbuf, "%c", index_type);
144 zebra_term_untrans(zh, index_type, dst_buf, str);
145 wrbuf_printf(wrbuf, " %s", dst_buf);
147 for (i = 1; i < key_in.len; i++)
148 wrbuf_printf(wrbuf, " " ZINT_FORMAT, key_in.mem[i]);
149 wrbuf_printf(wrbuf, "\n");
154 *output_format = VAL_SUTRS;
155 *rec_lenp = wrbuf_len(wrbuf);
156 *rec_bufp = odr_malloc(odr, *rec_lenp);
157 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
158 wrbuf_free(wrbuf, 1);
159 zebra_rec_keys_close(keys);
163 int zebra_record_fetch(ZebraHandle zh, SYSNO sysno, int score,
164 zebra_snippets *hit_snippet, ODR odr,
165 oid_value input_format, Z_RecordComposition *comp,
166 oid_value *output_format,
167 char **rec_bufp, int *rec_lenp, char **basenamep,
171 char *fname, *file_type, *basename;
172 struct ZebraRecStream stream;
173 RecordAttr *recordAttr;
180 if (comp && comp->which == Z_RecordComp_simple &&
181 comp->u.simple->which == Z_ElementSetNames_generic &&
182 !strcmp (comp->u.simple->u.generic, "_sysno_"))
185 sprintf(rec_str, ZINT_FORMAT, sysno);
186 *output_format = VAL_SUTRS;
187 *rec_lenp = strlen(rec_str);
188 *rec_bufp = odr_strdup(odr, rec_str);
191 rec = rec_get (zh->reg->records, sysno);
194 yaz_log (YLOG_DEBUG, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
196 return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
198 recordAttr = rec_init_attr (zh->reg->zei, rec);
200 file_type = rec->info[recInfo_fileType];
201 fname = rec->info[recInfo_filename];
202 basename = rec->info[recInfo_databaseName];
203 *basenamep = (char *) odr_malloc (odr, strlen(basename)+1);
204 strcpy (*basenamep, basename);
206 if (comp && comp->which == Z_RecordComp_simple
207 && comp->u.simple->which == Z_ElementSetNames_generic
208 && strncmp(comp->u.simple->u.generic, "zebra::", 7) == 0)
210 int r = zebra_storekeys_fetch(zh, sysno, odr, rec,
211 comp->u.simple->u.generic + 7,
212 input_format, output_format,
219 if (comp && comp->which == Z_RecordComp_simple &&
220 comp->u.simple->which == Z_ElementSetNames_generic &&
221 !strcmp (comp->u.simple->u.generic, "R"))
225 yaz_log (YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
227 if (rec->size[recInfo_storeData] > 0)
229 zebra_create_stream_mem(&stream, rec->info[recInfo_storeData],
230 rec->size[recInfo_storeData]);
237 if (zh->path_reg && !yaz_is_abspath (fname))
239 strcpy (full_rep, zh->path_reg);
240 strcat (full_rep, "/");
241 strcat (full_rep, fname);
244 strcpy (full_rep, fname);
246 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
248 yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
253 zebra_create_stream_fd(&stream, fd, recordAttr->recordOffset);
258 *output_format = VAL_SUTRS;
259 *rec_lenp = recordAttr->recordSize;
260 *rec_bufp = (char *) odr_malloc(odr, *rec_lenp);
261 stream.readf(&stream, *rec_bufp, *rec_lenp);
266 zebra_snippets *snippet;
267 zebra_rec_keys_t reckeys = zebra_rec_keys_open();
269 struct recRetrieveCtrl retrieveCtrl;
271 retrieveCtrl.stream = &stream;
272 retrieveCtrl.fname = fname;
273 retrieveCtrl.localno = sysno;
274 retrieveCtrl.staticrank = recordAttr->staticrank;
275 retrieveCtrl.score = score;
276 retrieveCtrl.recordSize = recordAttr->recordSize;
277 retrieveCtrl.odr = odr;
278 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
279 retrieveCtrl.comp = comp;
280 retrieveCtrl.encoding = zh->record_encoding;
281 retrieveCtrl.diagnostic = 0;
282 retrieveCtrl.addinfo = 0;
283 retrieveCtrl.dh = zh->reg->dh;
284 retrieveCtrl.res = zh->res;
285 retrieveCtrl.rec_buf = 0;
286 retrieveCtrl.rec_len = -1;
287 retrieveCtrl.hit_snippet = hit_snippet;
288 retrieveCtrl.doc_snippet = zebra_snippets_create();
290 zebra_rec_keys_set_buf(reckeys,
291 rec->info[recInfo_delKeys],
292 rec->size[recInfo_delKeys],
294 zebra_rec_keys_to_snippets(zh, reckeys, retrieveCtrl.doc_snippet);
295 zebra_rec_keys_close(reckeys);
298 /* for debugging purposes */
299 yaz_log(YLOG_LOG, "DOC SNIPPET:");
300 zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
301 yaz_log(YLOG_LOG, "HIT SNIPPET:");
302 zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
304 snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
305 retrieveCtrl.hit_snippet,
308 /* for debugging purposes */
309 yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
310 zebra_snippets_log(snippet, YLOG_LOG);
313 if (!(rt = recType_byName (zh->reg->recTypes, zh->res,
314 file_type, &clientData)))
316 return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
320 (*rt->retrieve)(clientData, &retrieveCtrl);
321 return_code = retrieveCtrl.diagnostic;
323 *output_format = retrieveCtrl.output_format;
324 *rec_bufp = (char *) retrieveCtrl.rec_buf;
325 *rec_lenp = retrieveCtrl.rec_len;
326 *addinfo = retrieveCtrl.addinfo;
328 zebra_snippets_destroy(snippet);
329 zebra_snippets_destroy(retrieveCtrl.doc_snippet);
331 stream.destroy(&stream);
340 * indent-tabs-mode: nil
342 * vim: shiftwidth=4 tabstop=8 expandtab