1 /* $Id: retrieve.c,v 1.40 2006-05-10 08:13:22 adam Exp $
2 Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
38 int zebra_record_ext_read (void *fh, char *buf, size_t count)
40 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
41 return read (fc->fd, buf, count);
44 off_t zebra_record_ext_seek (void *fh, off_t offset)
46 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
47 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
50 off_t zebra_record_ext_tell (void *fh)
52 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
53 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
56 off_t zebra_record_int_seek (void *fh, off_t offset)
58 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
59 return (off_t) (fc->record_int_pos = offset);
62 off_t zebra_record_int_tell (void *fh)
64 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
65 return (off_t) fc->record_int_pos;
68 int zebra_record_int_read (void *fh, char *buf, size_t count)
70 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
71 int l = fc->record_int_len - fc->record_int_pos;
74 l = (l < (int) count) ? l : (int) count;
75 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
76 fc->record_int_pos += l;
80 void zebra_record_int_end (void *fh, off_t off)
82 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
86 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score,
87 zebra_snippets *hit_snippet, ODR stream,
88 oid_value input_format, Z_RecordComposition *comp,
89 oid_value *output_format, char **rec_bufp,
90 int *rec_lenp, char **basenamep,
94 char *fname, *file_type, *basename;
96 struct recRetrieveCtrl retrieveCtrl;
97 struct zebra_fetch_control fc;
98 RecordAttr *recordAttr;
104 if (comp && comp->which == Z_RecordComp_simple &&
105 comp->u.simple->which == Z_ElementSetNames_generic &&
106 !strcmp (comp->u.simple->u.generic, "_sysno_"))
109 sprintf(rec_str, ZINT_FORMAT, sysno);
110 *output_format = VAL_SUTRS;
111 *rec_lenp = strlen(rec_str);
112 *rec_bufp = odr_strdup(stream, rec_str);
115 rec = rec_get (zh->reg->records, sysno);
118 yaz_log (YLOG_DEBUG, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
122 recordAttr = rec_init_attr (zh->reg->zei, rec);
124 file_type = rec->info[recInfo_fileType];
125 fname = rec->info[recInfo_filename];
126 basename = rec->info[recInfo_databaseName];
127 *basenamep = (char *) odr_malloc (stream, strlen(basename)+1);
128 strcpy (*basenamep, basename);
130 if (comp && comp->which == Z_RecordComp_simple &&
131 comp->u.simple->which == Z_ElementSetNames_generic &&
132 !strcmp (comp->u.simple->u.generic, "_storekeys_"))
134 WRBUF wrbuf = wrbuf_alloc();
135 zebra_rec_keys_t keys = zebra_rec_keys_open();
136 zebra_rec_keys_set_buf(keys,
137 rec->info[recInfo_delKeys],
138 rec->size[recInfo_delKeys],
140 if (zebra_rec_keys_rewind(keys))
144 struct it_key key_in;
145 while(zebra_rec_keys_read(keys, &str, &slen, &key_in))
148 int ord = key_in.mem[0];
153 const char *string_index = 0;
154 char dst_buf[IT_MAX_WORD];
156 zebraExplain_lookup_ord (zh->reg->zei, ord,
158 &set, &use, &string_index);
161 wrbuf_printf(wrbuf, "%s", string_index);
163 wrbuf_printf(wrbuf, "set=%d,use=%d", set, use);
165 zebra_term_untrans(zh, index_type, dst_buf, str);
166 wrbuf_printf(wrbuf, " %s", dst_buf);
168 for (i = 1; i < key_in.len; i++)
169 wrbuf_printf(wrbuf, " " ZINT_FORMAT, key_in.mem[i]);
170 wrbuf_printf(wrbuf, "\n");
174 *output_format = VAL_SUTRS;
175 *rec_lenp = wrbuf_len(wrbuf);
176 *rec_bufp = odr_malloc(stream, *rec_lenp);
177 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
178 wrbuf_free(wrbuf, 1);
179 zebra_rec_keys_close(keys);
182 if (comp && comp->which == Z_RecordComp_simple &&
183 comp->u.simple->which == Z_ElementSetNames_generic &&
184 !strcmp (comp->u.simple->u.generic, "R"))
188 if (!(rt = recType_byName (zh->reg->recTypes, zh->res,
189 file_type, &clientData)))
191 yaz_log (YLOG_WARN, "Retrieve: Cannot handle type %s", file_type);
194 yaz_log (YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d", sysno,score);
195 retrieveCtrl.fh = &fc;
197 retrieveCtrl.fname = fname;
198 if (rec->size[recInfo_storeData] > 0)
200 retrieveCtrl.readf = zebra_record_int_read;
201 retrieveCtrl.seekf = zebra_record_int_seek;
202 retrieveCtrl.tellf = zebra_record_int_tell;
203 fc.record_int_len = rec->size[recInfo_storeData];
204 fc.record_int_buf = rec->info[recInfo_storeData];
205 fc.record_int_pos = 0;
206 yaz_log (YLOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
209 *output_format = VAL_SUTRS;
210 *rec_lenp = rec->size[recInfo_storeData];
211 *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
212 memcpy(*rec_bufp, rec->info[recInfo_storeData], *rec_lenp);
221 if (zh->path_reg && !yaz_is_abspath (fname))
223 strcpy (full_rep, zh->path_reg);
224 strcat (full_rep, "/");
225 strcat (full_rep, fname);
228 strcpy (full_rep, fname);
230 if ((fc.fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
232 yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
237 fc.record_offset = recordAttr->recordOffset;
239 retrieveCtrl.readf = zebra_record_ext_read;
240 retrieveCtrl.seekf = zebra_record_ext_seek;
241 retrieveCtrl.tellf = zebra_record_ext_tell;
243 zebra_record_ext_seek (retrieveCtrl.fh, 0);
246 *output_format = VAL_SUTRS;
247 *rec_lenp = recordAttr->recordSize;
248 *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
249 zebra_record_ext_read(&fc, *rec_bufp, *rec_lenp);
255 retrieveCtrl.localno = sysno;
256 retrieveCtrl.staticrank = recordAttr->staticrank;
257 retrieveCtrl.score = score;
258 retrieveCtrl.recordSize = recordAttr->recordSize;
259 retrieveCtrl.odr = stream;
260 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
261 retrieveCtrl.comp = comp;
262 retrieveCtrl.encoding = zh->record_encoding;
263 retrieveCtrl.diagnostic = 0;
264 retrieveCtrl.addinfo = 0;
265 retrieveCtrl.dh = zh->reg->dh;
266 retrieveCtrl.res = zh->res;
267 retrieveCtrl.rec_buf = 0;
268 retrieveCtrl.rec_len = -1;
269 retrieveCtrl.hit_snippet = hit_snippet;
270 retrieveCtrl.doc_snippet = zebra_snippets_create();
275 zebra_snippets *snippet;
277 zebra_rec_keys_t reckeys = zebra_rec_keys_open();
279 zebra_rec_keys_set_buf(reckeys,
280 rec->info[recInfo_delKeys],
281 rec->size[recInfo_delKeys],
283 zebra_snippets_rec_keys(zh, reckeys, retrieveCtrl.doc_snippet);
284 zebra_rec_keys_close(reckeys);
288 /* for debugging purposes */
289 yaz_log(YLOG_LOG, "DOC SNIPPET:");
290 zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
291 yaz_log(YLOG_LOG, "HIT SNIPPET:");
292 zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
294 snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
295 retrieveCtrl.hit_snippet,
298 /* for debugging purposes */
299 yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
300 zebra_snippets_log(snippet, YLOG_LOG);
302 (*rt->retrieve)(clientData, &retrieveCtrl);
304 zebra_snippets_destroy(snippet);
308 (*rt->retrieve)(clientData, &retrieveCtrl);
311 zebra_snippets_destroy(retrieveCtrl.doc_snippet);
313 *output_format = retrieveCtrl.output_format;
314 *rec_bufp = (char *) retrieveCtrl.rec_buf;
315 *rec_lenp = retrieveCtrl.rec_len;
320 *addinfo = retrieveCtrl.addinfo;
321 return retrieveCtrl.diagnostic;
326 * indent-tabs-mode: nil
328 * vim: shiftwidth=4 tabstop=8 expandtab