1 /* $Id: retrieve.c,v 1.29 2005-04-14 12:02:58 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
37 int zebra_record_ext_read (void *fh, char *buf, size_t count)
39 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
40 return read (fc->fd, buf, count);
43 off_t zebra_record_ext_seek (void *fh, off_t offset)
45 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
46 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
49 off_t zebra_record_ext_tell (void *fh)
51 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
52 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
55 off_t zebra_record_int_seek (void *fh, off_t offset)
57 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
58 return (off_t) (fc->record_int_pos = offset);
61 off_t zebra_record_int_tell (void *fh)
63 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
64 return (off_t) fc->record_int_pos;
67 int zebra_record_int_read (void *fh, char *buf, size_t count)
69 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
70 int l = fc->record_int_len - fc->record_int_pos;
73 l = (l < (int) count) ? l : (int) count;
74 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
75 fc->record_int_pos += l;
79 void zebra_record_int_end (void *fh, off_t off)
81 struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
85 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score, ODR stream,
86 oid_value input_format, Z_RecordComposition *comp,
87 oid_value *output_format, char **rec_bufp,
88 int *rec_lenp, char **basenamep,
92 char *fname, *file_type, *basename;
94 struct recRetrieveCtrl retrieveCtrl;
95 struct zebra_fetch_control fc;
96 RecordAttr *recordAttr;
102 if (comp && comp->which == Z_RecordComp_simple &&
103 comp->u.simple->which == Z_ElementSetNames_generic &&
104 !strcmp (comp->u.simple->u.generic, "_sysno_"))
107 sprintf(rec_str, ZINT_FORMAT, sysno);
108 *output_format = VAL_SUTRS;
109 *rec_lenp = strlen(rec_str);
110 *rec_bufp = odr_strdup(stream, rec_str);
113 rec = rec_get (zh->reg->records, sysno);
116 yaz_log (YLOG_DEBUG, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
120 recordAttr = rec_init_attr (zh->reg->zei, rec);
122 file_type = rec->info[recInfo_fileType];
123 fname = rec->info[recInfo_filename];
124 basename = rec->info[recInfo_databaseName];
125 *basenamep = (char *) odr_malloc (stream, strlen(basename)+1);
126 strcpy (*basenamep, basename);
128 if (comp && comp->which == Z_RecordComp_simple &&
129 comp->u.simple->which == Z_ElementSetNames_generic &&
130 !strcmp (comp->u.simple->u.generic, "R"))
134 if (!(rt = recType_byName (zh->reg->recTypes, zh->res,
135 file_type, &clientData)))
137 yaz_log (YLOG_WARN, "Retrieve: Cannot handle type %s", file_type);
140 yaz_log (YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d", sysno,score);
141 retrieveCtrl.fh = &fc;
143 retrieveCtrl.fname = fname;
144 if (rec->size[recInfo_storeData] > 0)
146 retrieveCtrl.readf = zebra_record_int_read;
147 retrieveCtrl.seekf = zebra_record_int_seek;
148 retrieveCtrl.tellf = zebra_record_int_tell;
149 fc.record_int_len = rec->size[recInfo_storeData];
150 fc.record_int_buf = rec->info[recInfo_storeData];
151 fc.record_int_pos = 0;
152 yaz_log (YLOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
155 *output_format = VAL_SUTRS;
156 *rec_lenp = rec->size[recInfo_storeData];
157 *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
158 memcpy(*rec_bufp, rec->info[recInfo_storeData], *rec_lenp);
167 if (zh->path_reg && !yaz_is_abspath (fname))
169 strcpy (full_rep, zh->path_reg);
170 strcat (full_rep, "/");
171 strcat (full_rep, fname);
174 strcpy (full_rep, fname);
176 if ((fc.fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
178 yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
183 fc.record_offset = recordAttr->recordOffset;
185 retrieveCtrl.readf = zebra_record_ext_read;
186 retrieveCtrl.seekf = zebra_record_ext_seek;
187 retrieveCtrl.tellf = zebra_record_ext_tell;
189 zebra_record_ext_seek (retrieveCtrl.fh, 0);
192 *output_format = VAL_SUTRS;
193 *rec_lenp = recordAttr->recordSize;
194 *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
195 zebra_record_ext_read(&fc, *rec_bufp, *rec_lenp);
201 retrieveCtrl.localno = sysno;
202 retrieveCtrl.score = score;
203 retrieveCtrl.recordSize = recordAttr->recordSize;
204 retrieveCtrl.odr = stream;
205 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
206 retrieveCtrl.comp = comp;
207 retrieveCtrl.encoding = zh->record_encoding;
208 retrieveCtrl.diagnostic = 0;
209 retrieveCtrl.addinfo = 0;
210 retrieveCtrl.dh = zh->reg->dh;
211 retrieveCtrl.res = zh->res;
212 retrieveCtrl.rec_buf = 0;
213 retrieveCtrl.rec_len = -1;
215 (*rt->retrieve)(clientData, &retrieveCtrl);
216 *output_format = retrieveCtrl.output_format;
217 *rec_bufp = (char *) retrieveCtrl.rec_buf;
218 *rec_lenp = retrieveCtrl.rec_len;
223 *addinfo = retrieveCtrl.addinfo;
224 return retrieveCtrl.diagnostic;