2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1999-10-29 10:00:00 adam
8 * Fixed minor bug where database name wasn't set in zebra_record_fetch.
10 * Revision 1.10 1999/05/26 07:49:13 adam
13 * Revision 1.9 1999/05/20 12:57:18 adam
14 * Implemented TCL filter. Updated recctrl system.
16 * Revision 1.8 1999/03/09 16:27:49 adam
17 * More work on SDRKit integration.
19 * Revision 1.7 1999/03/02 16:15:43 quinn
20 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
22 * Revision 1.6 1999/02/18 15:01:25 adam
25 * Revision 1.5 1999/02/17 11:29:56 adam
26 * Fixed in record_fetch. Minor updates to API.
28 * Revision 1.4 1999/02/02 14:51:07 adam
29 * Updated WIN32 code specific sections. Changed header.
31 * Revision 1.3 1998/10/28 10:54:40 adam
34 * Revision 1.2 1998/10/16 08:14:33 adam
35 * Updated record control system.
37 * Revision 1.1 1998/03/05 08:45:13 adam
38 * New result set model and modular ranking system. Moved towards
39 * descent server API. System information stored as "SGML" records.
65 struct fetch_control {
73 static int record_ext_read (void *fh, char *buf, size_t count)
75 struct fetch_control *fc = (struct fetch_control *) fh;
76 return read (fc->fd, buf, count);
79 static off_t record_ext_seek (void *fh, off_t offset)
81 struct fetch_control *fc = (struct fetch_control *) fh;
82 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
85 static off_t record_ext_tell (void *fh)
87 struct fetch_control *fc = (struct fetch_control *) fh;
88 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
91 static off_t record_int_seek (void *fh, off_t offset)
93 struct fetch_control *fc = (struct fetch_control *) fh;
94 return (off_t) (fc->record_int_pos = offset);
97 static off_t record_int_tell (void *fh)
99 struct fetch_control *fc = (struct fetch_control *) fh;
100 return (off_t) fc->record_int_pos;
103 static int record_int_read (void *fh, char *buf, size_t count)
105 struct fetch_control *fc = (struct fetch_control *) fh;
106 int l = fc->record_int_len - fc->record_int_pos;
109 l = (l < (int) count) ? l : count;
110 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
111 fc->record_int_pos += l;
115 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
116 oid_value input_format, Z_RecordComposition *comp,
117 oid_value *output_format, char **rec_bufp,
118 int *rec_lenp, char **basenamep)
121 char *fname, *file_type, *basename;
123 struct recRetrieveCtrl retrieveCtrl;
125 struct fetch_control fc;
126 RecordAttr *recordAttr;
129 rec = rec_get (zh->records, sysno);
132 logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
136 recordAttr = rec_init_attr (zh->zei, rec);
138 file_type = rec->info[recInfo_fileType];
139 fname = rec->info[recInfo_filename];
140 basename = rec->info[recInfo_databaseName];
141 *basenamep = (char *) odr_malloc (stream, strlen(basename)+1);
142 strcpy (*basenamep, basename);
144 if (!(rt = recType_byName (zh->recTypes, file_type, subType, &clientData)))
146 logf (LOG_WARN, "Retrieve: Cannot handle type %s", file_type);
149 logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
150 retrieveCtrl.fh = &fc;
152 if (rec->size[recInfo_storeData] > 0)
154 retrieveCtrl.readf = record_int_read;
155 retrieveCtrl.seekf = record_int_seek;
156 retrieveCtrl.tellf = record_int_tell;
157 fc.record_int_len = rec->size[recInfo_storeData];
158 fc.record_int_buf = rec->info[recInfo_storeData];
159 fc.record_int_pos = 0;
160 logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
163 else if (*fname == '%')
167 char *cp, xname[128];
170 logf (LOG_DEBUG, "SDR");
171 strcpy (xname, fname+1);
172 if ((cp = strrchr (xname, '.')))
177 h = zebraSdr_open (xname);
180 logf (LOG_WARN, "sdr open %s", xname);
183 if (zebraSdr_segment (h, &segment) < 0)
185 logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
190 r = zebraSdr_read (h, &buf);
193 logf (LOG_WARN, "zebraSdr_read fail segment=%d",
200 fc.record_int_len = recordAttr->recordSize;
201 fc.record_int_buf = buf + recordAttr->recordOffset;
202 fc.record_int_pos = 0;
204 logf (LOG_LOG, "segment = %d len=%d off=%d",
206 recordAttr->recordSize,
207 recordAttr->recordOffset);
208 if (fc.record_int_len > 180)
210 logf (LOG_LOG, "%.70s", fc.record_int_buf);
211 logf (LOG_LOG, "%.70s", fc.record_int_buf +
212 (fc.record_int_len - 70));
215 logf (LOG_LOG, "%.*s",
216 fc.record_int_len, fc.record_int_buf);
218 /* the following two lines makes rec_rm delete buf */
219 rec->size[recInfo_storeData] = r;
220 rec->info[recInfo_storeData] = buf;
222 retrieveCtrl.readf = record_int_read;
223 retrieveCtrl.seekf = record_int_seek;
224 retrieveCtrl.tellf = record_int_tell;
229 if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
231 logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
236 fc.record_offset = recordAttr->recordOffset;
238 retrieveCtrl.readf = record_ext_read;
239 retrieveCtrl.seekf = record_ext_seek;
240 retrieveCtrl.tellf = record_ext_tell;
242 record_ext_seek (retrieveCtrl.fh, 0);
244 retrieveCtrl.subType = subType;
245 retrieveCtrl.localno = sysno;
246 retrieveCtrl.score = score;
247 retrieveCtrl.recordSize = recordAttr->recordSize;
248 retrieveCtrl.odr = stream;
249 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
250 retrieveCtrl.comp = comp;
251 retrieveCtrl.diagnostic = 0;
252 retrieveCtrl.dh = zh->dh;
253 retrieveCtrl.res = zh->res;
254 (*rt->retrieve)(clientData, &retrieveCtrl);
255 *output_format = retrieveCtrl.output_format;
256 *rec_bufp = (char *) retrieveCtrl.rec_buf;
257 *rec_lenp = retrieveCtrl.rec_len;
262 return retrieveCtrl.diagnostic;