2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1999-02-18 15:01:25 adam
10 * Revision 1.5 1999/02/17 11:29:56 adam
11 * Fixed in record_fetch. Minor updates to API.
13 * Revision 1.4 1999/02/02 14:51:07 adam
14 * Updated WIN32 code specific sections. Changed header.
16 * Revision 1.3 1998/10/28 10:54:40 adam
19 * Revision 1.2 1998/10/16 08:14:33 adam
20 * Updated record control system.
22 * Revision 1.1 1998/03/05 08:45:13 adam
23 * New result set model and modular ranking system. Moved towards
24 * descent server API. System information stored as "SGML" records.
50 struct fetch_control {
58 static int record_ext_read (void *fh, char *buf, size_t count)
60 struct fetch_control *fc = fh;
61 return read (fc->fd, buf, count);
64 static off_t record_ext_seek (void *fh, off_t offset)
66 struct fetch_control *fc = fh;
67 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
70 static off_t record_ext_tell (void *fh)
72 struct fetch_control *fc = fh;
73 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
76 static off_t record_int_seek (void *fh, off_t offset)
78 struct fetch_control *fc = fh;
79 return (off_t) (fc->record_int_pos = offset);
82 static off_t record_int_tell (void *fh)
84 struct fetch_control *fc = fh;
85 return (off_t) fc->record_int_pos;
88 static int record_int_read (void *fh, char *buf, size_t count)
90 struct fetch_control *fc = fh;
91 int l = fc->record_int_len - fc->record_int_pos;
94 l = (l < (int) count) ? l : count;
95 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
96 fc->record_int_pos += l;
100 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
101 oid_value input_format, Z_RecordComposition *comp,
102 oid_value *output_format, char **rec_bufp,
103 int *rec_lenp, char **basenamep)
106 char *fname, *file_type, *basename;
108 struct recRetrieveCtrl retrieveCtrl;
110 struct fetch_control fc;
111 RecordAttr *recordAttr;
113 rec = rec_get (zh->records, sysno);
116 logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
119 recordAttr = rec_init_attr (zh->zei, rec);
121 file_type = rec->info[recInfo_fileType];
122 fname = rec->info[recInfo_filename];
123 basename = rec->info[recInfo_databaseName];
124 *basenamep = odr_malloc (stream, strlen(basename)+1);
125 strcpy (*basenamep, basename);
127 if (!(rt = recType_byName (zh->recTypes, file_type, subType)))
129 logf (LOG_WARN, "Retrieve: Cannot handle type %s", file_type);
132 logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
133 retrieveCtrl.fh = &fc;
135 if (rec->size[recInfo_storeData] > 0)
137 retrieveCtrl.readf = record_int_read;
138 retrieveCtrl.seekf = record_int_seek;
139 retrieveCtrl.tellf = record_int_tell;
140 fc.record_int_len = rec->size[recInfo_storeData];
141 fc.record_int_buf = rec->info[recInfo_storeData];
142 fc.record_int_pos = 0;
143 logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
146 else if (*fname == '%')
150 char *cp, xname[128];
153 logf (LOG_DEBUG, "SDR");
154 strcpy (xname, fname+1);
155 if ((cp = strrchr (xname, '.')))
160 h = zebraSdr_open (xname);
163 logf (LOG_WARN, "sdr open %s", xname);
166 if (zebraSdr_segment (h, &segment) < 0)
168 logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
173 r = zebraSdr_read (h, &buf);
176 logf (LOG_WARN, "zebraSdr_read fail segment=%d",
183 fc.record_int_len = recordAttr->recordSize;
184 fc.record_int_buf = buf + recordAttr->recordOffset;
185 fc.record_int_pos = 0;
187 logf (LOG_LOG, "segment = %d len=%d off=%d",
189 recordAttr->recordSize,
190 recordAttr->recordOffset);
191 if (fc.record_int_len > 180)
193 logf (LOG_LOG, "%0.70s", fc.record_int_buf);
194 logf (LOG_LOG, "%0.70s", fc.record_int_buf +
195 (fc.record_int_len - 70));
198 logf (LOG_LOG, "%0.*s",
199 fc.record_int_len, fc.record_int_buf);
201 /* the following two lines makes rec_rm delete buf */
202 rec->size[recInfo_storeData] = r;
203 rec->info[recInfo_storeData] = buf;
205 retrieveCtrl.readf = record_int_read;
206 retrieveCtrl.seekf = record_int_seek;
207 retrieveCtrl.tellf = record_int_tell;
212 if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
214 logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
219 fc.record_offset = recordAttr->recordOffset;
221 retrieveCtrl.readf = record_ext_read;
222 retrieveCtrl.seekf = record_ext_seek;
223 retrieveCtrl.tellf = record_ext_tell;
225 record_ext_seek (retrieveCtrl.fh, 0);
227 retrieveCtrl.subType = subType;
228 retrieveCtrl.localno = sysno;
229 retrieveCtrl.score = score;
230 retrieveCtrl.recordSize = recordAttr->recordSize;
231 retrieveCtrl.odr = stream;
232 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
233 retrieveCtrl.comp = comp;
234 retrieveCtrl.diagnostic = 0;
235 retrieveCtrl.dh = zh->dh;
236 (*rt->retrieve)(&retrieveCtrl);
237 *output_format = retrieveCtrl.output_format;
238 *rec_bufp = retrieveCtrl.rec_buf;
239 *rec_lenp = retrieveCtrl.rec_len;
244 return retrieveCtrl.diagnostic;