2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1999-03-09 16:27:49 adam
8 * More work on SDRKit integration.
10 * Revision 1.7 1999/03/02 16:15:43 quinn
11 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
13 * Revision 1.6 1999/02/18 15:01:25 adam
16 * Revision 1.5 1999/02/17 11:29:56 adam
17 * Fixed in record_fetch. Minor updates to API.
19 * Revision 1.4 1999/02/02 14:51:07 adam
20 * Updated WIN32 code specific sections. Changed header.
22 * Revision 1.3 1998/10/28 10:54:40 adam
25 * Revision 1.2 1998/10/16 08:14:33 adam
26 * Updated record control system.
28 * Revision 1.1 1998/03/05 08:45:13 adam
29 * New result set model and modular ranking system. Moved towards
30 * descent server API. System information stored as "SGML" records.
56 struct fetch_control {
64 static int record_ext_read (void *fh, char *buf, size_t count)
66 struct fetch_control *fc = fh;
67 return read (fc->fd, buf, count);
70 static off_t record_ext_seek (void *fh, off_t offset)
72 struct fetch_control *fc = fh;
73 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
76 static off_t record_ext_tell (void *fh)
78 struct fetch_control *fc = fh;
79 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
82 static off_t record_int_seek (void *fh, off_t offset)
84 struct fetch_control *fc = fh;
85 return (off_t) (fc->record_int_pos = offset);
88 static off_t record_int_tell (void *fh)
90 struct fetch_control *fc = fh;
91 return (off_t) fc->record_int_pos;
94 static int record_int_read (void *fh, char *buf, size_t count)
96 struct fetch_control *fc = fh;
97 int l = fc->record_int_len - fc->record_int_pos;
100 l = (l < (int) count) ? l : count;
101 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
102 fc->record_int_pos += l;
106 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
107 oid_value input_format, Z_RecordComposition *comp,
108 oid_value *output_format, char **rec_bufp,
109 int *rec_lenp, char **basenamep)
112 char *fname, *file_type, *basename;
114 struct recRetrieveCtrl retrieveCtrl;
116 struct fetch_control fc;
117 RecordAttr *recordAttr;
119 rec = rec_get (zh->records, sysno);
122 logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
125 recordAttr = rec_init_attr (zh->zei, rec);
127 file_type = rec->info[recInfo_fileType];
128 fname = rec->info[recInfo_filename];
129 basename = rec->info[recInfo_databaseName];
130 *basenamep = odr_malloc (stream, strlen(basename)+1);
131 strcpy (*basenamep, basename);
133 if (!(rt = recType_byName (zh->recTypes, file_type, subType)))
135 logf (LOG_WARN, "Retrieve: Cannot handle type %s", file_type);
138 logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
139 retrieveCtrl.fh = &fc;
141 if (rec->size[recInfo_storeData] > 0)
143 retrieveCtrl.readf = record_int_read;
144 retrieveCtrl.seekf = record_int_seek;
145 retrieveCtrl.tellf = record_int_tell;
146 fc.record_int_len = rec->size[recInfo_storeData];
147 fc.record_int_buf = rec->info[recInfo_storeData];
148 fc.record_int_pos = 0;
149 logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
152 else if (*fname == '%')
156 char *cp, xname[128];
159 logf (LOG_DEBUG, "SDR");
160 strcpy (xname, fname+1);
161 if ((cp = strrchr (xname, '.')))
166 h = zebraSdr_open (xname);
169 logf (LOG_WARN, "sdr open %s", xname);
172 if (zebraSdr_segment (h, &segment) < 0)
174 logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
179 r = zebraSdr_read (h, &buf);
182 logf (LOG_WARN, "zebraSdr_read fail segment=%d",
189 fc.record_int_len = recordAttr->recordSize;
190 fc.record_int_buf = buf + recordAttr->recordOffset;
191 fc.record_int_pos = 0;
193 logf (LOG_LOG, "segment = %d len=%d off=%d",
195 recordAttr->recordSize,
196 recordAttr->recordOffset);
197 if (fc.record_int_len > 180)
199 logf (LOG_LOG, "%.70s", fc.record_int_buf);
200 logf (LOG_LOG, "%.70s", fc.record_int_buf +
201 (fc.record_int_len - 70));
204 logf (LOG_LOG, "%.*s",
205 fc.record_int_len, fc.record_int_buf);
207 /* the following two lines makes rec_rm delete buf */
208 rec->size[recInfo_storeData] = r;
209 rec->info[recInfo_storeData] = buf;
211 retrieveCtrl.readf = record_int_read;
212 retrieveCtrl.seekf = record_int_seek;
213 retrieveCtrl.tellf = record_int_tell;
218 if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
220 logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
225 fc.record_offset = recordAttr->recordOffset;
227 retrieveCtrl.readf = record_ext_read;
228 retrieveCtrl.seekf = record_ext_seek;
229 retrieveCtrl.tellf = record_ext_tell;
231 record_ext_seek (retrieveCtrl.fh, 0);
233 retrieveCtrl.subType = subType;
234 retrieveCtrl.localno = sysno;
235 retrieveCtrl.score = score;
236 retrieveCtrl.recordSize = recordAttr->recordSize;
237 retrieveCtrl.odr = stream;
238 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
239 retrieveCtrl.comp = comp;
240 retrieveCtrl.diagnostic = 0;
241 retrieveCtrl.dh = zh->dh;
242 retrieveCtrl.res = zh->res;
243 (*rt->retrieve)(&retrieveCtrl);
244 *output_format = retrieveCtrl.output_format;
245 *rec_bufp = retrieveCtrl.rec_buf;
246 *rec_lenp = retrieveCtrl.rec_len;
251 return retrieveCtrl.diagnostic;