1 /* $Id: retrieve.c,v 1.37 2005-10-28 09:22:50 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, "R"))
136 if (!(rt = recType_byName (zh->reg->recTypes, zh->res,
137 file_type, &clientData)))
139 yaz_log (YLOG_WARN, "Retrieve: Cannot handle type %s", file_type);
142 yaz_log (YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d", sysno,score);
143 retrieveCtrl.fh = &fc;
145 retrieveCtrl.fname = fname;
146 if (rec->size[recInfo_storeData] > 0)
148 retrieveCtrl.readf = zebra_record_int_read;
149 retrieveCtrl.seekf = zebra_record_int_seek;
150 retrieveCtrl.tellf = zebra_record_int_tell;
151 fc.record_int_len = rec->size[recInfo_storeData];
152 fc.record_int_buf = rec->info[recInfo_storeData];
153 fc.record_int_pos = 0;
154 yaz_log (YLOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
157 *output_format = VAL_SUTRS;
158 *rec_lenp = rec->size[recInfo_storeData];
159 *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
160 memcpy(*rec_bufp, rec->info[recInfo_storeData], *rec_lenp);
169 if (zh->path_reg && !yaz_is_abspath (fname))
171 strcpy (full_rep, zh->path_reg);
172 strcat (full_rep, "/");
173 strcat (full_rep, fname);
176 strcpy (full_rep, fname);
178 if ((fc.fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
180 yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
185 fc.record_offset = recordAttr->recordOffset;
187 retrieveCtrl.readf = zebra_record_ext_read;
188 retrieveCtrl.seekf = zebra_record_ext_seek;
189 retrieveCtrl.tellf = zebra_record_ext_tell;
191 zebra_record_ext_seek (retrieveCtrl.fh, 0);
194 *output_format = VAL_SUTRS;
195 *rec_lenp = recordAttr->recordSize;
196 *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
197 zebra_record_ext_read(&fc, *rec_bufp, *rec_lenp);
203 retrieveCtrl.localno = sysno;
204 retrieveCtrl.score = score;
205 retrieveCtrl.recordSize = recordAttr->recordSize;
206 retrieveCtrl.odr = stream;
207 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
208 retrieveCtrl.comp = comp;
209 retrieveCtrl.encoding = zh->record_encoding;
210 retrieveCtrl.diagnostic = 0;
211 retrieveCtrl.addinfo = 0;
212 retrieveCtrl.dh = zh->reg->dh;
213 retrieveCtrl.res = zh->res;
214 retrieveCtrl.rec_buf = 0;
215 retrieveCtrl.rec_len = -1;
216 retrieveCtrl.hit_snippet = hit_snippet;
217 retrieveCtrl.doc_snippet = zebra_snippets_create();
222 zebra_snippets *snippet;
224 zebra_rec_keys_t reckeys = zebra_rec_keys_open();
226 zebra_rec_keys_set_buf(reckeys,
227 rec->info[recInfo_delKeys],
228 rec->size[recInfo_delKeys],
230 zebra_snippets_rec_keys(zh, reckeys, retrieveCtrl.doc_snippet);
231 zebra_rec_keys_close(reckeys);
235 /* for debugging purposes */
236 yaz_log(YLOG_LOG, "DOC SNIPPET:");
237 zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
238 yaz_log(YLOG_LOG, "HIT SNIPPET:");
239 zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
241 snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
242 retrieveCtrl.hit_snippet,
245 /* for debugging purposes */
246 yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
247 zebra_snippets_log(snippet, YLOG_LOG);
249 (*rt->retrieve)(clientData, &retrieveCtrl);
251 zebra_snippets_destroy(snippet);
255 (*rt->retrieve)(clientData, &retrieveCtrl);
258 zebra_snippets_destroy(retrieveCtrl.doc_snippet);
260 *output_format = retrieveCtrl.output_format;
261 *rec_bufp = (char *) retrieveCtrl.rec_buf;
262 *rec_lenp = retrieveCtrl.rec_len;
267 *addinfo = retrieveCtrl.addinfo;
268 return retrieveCtrl.diagnostic;