1 /* $Id: retrieve.c,v 1.43 2006-08-14 10:40:15 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
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 = CAST_ZINT_TO_INT(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, &index_type, &db,
160 wrbuf_printf(wrbuf, "%s", string_index);
162 wrbuf_printf(wrbuf, "set=%d,use=%d", set, use);
164 zebra_term_untrans(zh, index_type, dst_buf, str);
165 wrbuf_printf(wrbuf, " %s", dst_buf);
167 for (i = 1; i < key_in.len; i++)
168 wrbuf_printf(wrbuf, " " ZINT_FORMAT, key_in.mem[i]);
169 wrbuf_printf(wrbuf, "\n");
173 *output_format = VAL_SUTRS;
174 *rec_lenp = wrbuf_len(wrbuf);
175 *rec_bufp = odr_malloc(stream, *rec_lenp);
176 memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
177 wrbuf_free(wrbuf, 1);
178 zebra_rec_keys_close(keys);
181 if (comp && comp->which == Z_RecordComp_simple &&
182 comp->u.simple->which == Z_ElementSetNames_generic &&
183 !strcmp (comp->u.simple->u.generic, "R"))
187 if (!(rt = recType_byName (zh->reg->recTypes, zh->res,
188 file_type, &clientData)))
190 yaz_log (YLOG_WARN, "Retrieve: Cannot handle type %s", file_type);
193 yaz_log (YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d", sysno,score);
194 retrieveCtrl.fh = &fc;
196 retrieveCtrl.fname = fname;
197 if (rec->size[recInfo_storeData] > 0)
199 retrieveCtrl.readf = zebra_record_int_read;
200 retrieveCtrl.seekf = zebra_record_int_seek;
201 retrieveCtrl.tellf = zebra_record_int_tell;
202 fc.record_int_len = rec->size[recInfo_storeData];
203 fc.record_int_buf = rec->info[recInfo_storeData];
204 fc.record_int_pos = 0;
205 yaz_log (YLOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
208 *output_format = VAL_SUTRS;
209 *rec_lenp = rec->size[recInfo_storeData];
210 *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
211 memcpy(*rec_bufp, rec->info[recInfo_storeData], *rec_lenp);
220 if (zh->path_reg && !yaz_is_abspath (fname))
222 strcpy (full_rep, zh->path_reg);
223 strcat (full_rep, "/");
224 strcat (full_rep, fname);
227 strcpy (full_rep, fname);
229 if ((fc.fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
231 yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
236 fc.record_offset = recordAttr->recordOffset;
238 retrieveCtrl.readf = zebra_record_ext_read;
239 retrieveCtrl.seekf = zebra_record_ext_seek;
240 retrieveCtrl.tellf = zebra_record_ext_tell;
242 zebra_record_ext_seek (retrieveCtrl.fh, 0);
245 *output_format = VAL_SUTRS;
246 *rec_lenp = recordAttr->recordSize;
247 *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
248 zebra_record_ext_read(&fc, *rec_bufp, *rec_lenp);
254 retrieveCtrl.localno = sysno;
255 retrieveCtrl.staticrank = recordAttr->staticrank;
256 retrieveCtrl.score = score;
257 retrieveCtrl.recordSize = recordAttr->recordSize;
258 retrieveCtrl.odr = stream;
259 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
260 retrieveCtrl.comp = comp;
261 retrieveCtrl.encoding = zh->record_encoding;
262 retrieveCtrl.diagnostic = 0;
263 retrieveCtrl.addinfo = 0;
264 retrieveCtrl.dh = zh->reg->dh;
265 retrieveCtrl.res = zh->res;
266 retrieveCtrl.rec_buf = 0;
267 retrieveCtrl.rec_len = -1;
268 retrieveCtrl.hit_snippet = hit_snippet;
269 retrieveCtrl.doc_snippet = zebra_snippets_create();
274 zebra_snippets *snippet;
276 zebra_rec_keys_t reckeys = zebra_rec_keys_open();
278 zebra_rec_keys_set_buf(reckeys,
279 rec->info[recInfo_delKeys],
280 rec->size[recInfo_delKeys],
282 zebra_snippets_rec_keys(zh, reckeys, retrieveCtrl.doc_snippet);
283 zebra_rec_keys_close(reckeys);
287 /* for debugging purposes */
288 yaz_log(YLOG_LOG, "DOC SNIPPET:");
289 zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
290 yaz_log(YLOG_LOG, "HIT SNIPPET:");
291 zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
293 snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
294 retrieveCtrl.hit_snippet,
297 /* for debugging purposes */
298 yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
299 zebra_snippets_log(snippet, YLOG_LOG);
301 (*rt->retrieve)(clientData, &retrieveCtrl);
303 zebra_snippets_destroy(snippet);
307 (*rt->retrieve)(clientData, &retrieveCtrl);
310 zebra_snippets_destroy(retrieveCtrl.doc_snippet);
312 *output_format = retrieveCtrl.output_format;
313 *rec_bufp = (char *) retrieveCtrl.rec_buf;
314 *rec_lenp = retrieveCtrl.rec_len;
319 *addinfo = retrieveCtrl.addinfo;
320 return retrieveCtrl.diagnostic;
325 * indent-tabs-mode: nil
327 * vim: shiftwidth=4 tabstop=8 expandtab