2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1999-05-26 07:49:13 adam
10 * Revision 1.9 1999/05/20 12:57:18 adam
11 * Implemented TCL filter. Updated recctrl system.
13 * Revision 1.8 1999/03/09 16:27:49 adam
14 * More work on SDRKit integration.
16 * Revision 1.7 1999/03/02 16:15:43 quinn
17 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
19 * Revision 1.6 1999/02/18 15:01:25 adam
22 * Revision 1.5 1999/02/17 11:29:56 adam
23 * Fixed in record_fetch. Minor updates to API.
25 * Revision 1.4 1999/02/02 14:51:07 adam
26 * Updated WIN32 code specific sections. Changed header.
28 * Revision 1.3 1998/10/28 10:54:40 adam
31 * Revision 1.2 1998/10/16 08:14:33 adam
32 * Updated record control system.
34 * Revision 1.1 1998/03/05 08:45:13 adam
35 * New result set model and modular ranking system. Moved towards
36 * descent server API. System information stored as "SGML" records.
62 struct fetch_control {
70 static int record_ext_read (void *fh, char *buf, size_t count)
72 struct fetch_control *fc = (struct fetch_control *) fh;
73 return read (fc->fd, buf, count);
76 static off_t record_ext_seek (void *fh, off_t offset)
78 struct fetch_control *fc = (struct fetch_control *) fh;
79 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
82 static off_t record_ext_tell (void *fh)
84 struct fetch_control *fc = (struct fetch_control *) fh;
85 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
88 static off_t record_int_seek (void *fh, off_t offset)
90 struct fetch_control *fc = (struct fetch_control *) fh;
91 return (off_t) (fc->record_int_pos = offset);
94 static off_t record_int_tell (void *fh)
96 struct fetch_control *fc = (struct fetch_control *) fh;
97 return (off_t) fc->record_int_pos;
100 static int record_int_read (void *fh, char *buf, size_t count)
102 struct fetch_control *fc = (struct fetch_control *) fh;
103 int l = fc->record_int_len - fc->record_int_pos;
106 l = (l < (int) count) ? l : count;
107 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
108 fc->record_int_pos += l;
112 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
113 oid_value input_format, Z_RecordComposition *comp,
114 oid_value *output_format, char **rec_bufp,
115 int *rec_lenp, char **basenamep)
118 char *fname, *file_type, *basename;
120 struct recRetrieveCtrl retrieveCtrl;
122 struct fetch_control fc;
123 RecordAttr *recordAttr;
126 rec = rec_get (zh->records, sysno);
129 logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
132 recordAttr = rec_init_attr (zh->zei, rec);
134 file_type = rec->info[recInfo_fileType];
135 fname = rec->info[recInfo_filename];
136 basename = rec->info[recInfo_databaseName];
137 *basenamep = (char *) odr_malloc (stream, strlen(basename)+1);
138 strcpy (*basenamep, basename);
140 if (!(rt = recType_byName (zh->recTypes, file_type, subType, &clientData)))
142 logf (LOG_WARN, "Retrieve: Cannot handle type %s", file_type);
145 logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
146 retrieveCtrl.fh = &fc;
148 if (rec->size[recInfo_storeData] > 0)
150 retrieveCtrl.readf = record_int_read;
151 retrieveCtrl.seekf = record_int_seek;
152 retrieveCtrl.tellf = record_int_tell;
153 fc.record_int_len = rec->size[recInfo_storeData];
154 fc.record_int_buf = rec->info[recInfo_storeData];
155 fc.record_int_pos = 0;
156 logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
159 else if (*fname == '%')
163 char *cp, xname[128];
166 logf (LOG_DEBUG, "SDR");
167 strcpy (xname, fname+1);
168 if ((cp = strrchr (xname, '.')))
173 h = zebraSdr_open (xname);
176 logf (LOG_WARN, "sdr open %s", xname);
179 if (zebraSdr_segment (h, &segment) < 0)
181 logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
186 r = zebraSdr_read (h, &buf);
189 logf (LOG_WARN, "zebraSdr_read fail segment=%d",
196 fc.record_int_len = recordAttr->recordSize;
197 fc.record_int_buf = buf + recordAttr->recordOffset;
198 fc.record_int_pos = 0;
200 logf (LOG_LOG, "segment = %d len=%d off=%d",
202 recordAttr->recordSize,
203 recordAttr->recordOffset);
204 if (fc.record_int_len > 180)
206 logf (LOG_LOG, "%.70s", fc.record_int_buf);
207 logf (LOG_LOG, "%.70s", fc.record_int_buf +
208 (fc.record_int_len - 70));
211 logf (LOG_LOG, "%.*s",
212 fc.record_int_len, fc.record_int_buf);
214 /* the following two lines makes rec_rm delete buf */
215 rec->size[recInfo_storeData] = r;
216 rec->info[recInfo_storeData] = buf;
218 retrieveCtrl.readf = record_int_read;
219 retrieveCtrl.seekf = record_int_seek;
220 retrieveCtrl.tellf = record_int_tell;
225 if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
227 logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
232 fc.record_offset = recordAttr->recordOffset;
234 retrieveCtrl.readf = record_ext_read;
235 retrieveCtrl.seekf = record_ext_seek;
236 retrieveCtrl.tellf = record_ext_tell;
238 record_ext_seek (retrieveCtrl.fh, 0);
240 retrieveCtrl.subType = subType;
241 retrieveCtrl.localno = sysno;
242 retrieveCtrl.score = score;
243 retrieveCtrl.recordSize = recordAttr->recordSize;
244 retrieveCtrl.odr = stream;
245 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
246 retrieveCtrl.comp = comp;
247 retrieveCtrl.diagnostic = 0;
248 retrieveCtrl.dh = zh->dh;
249 retrieveCtrl.res = zh->res;
250 (*rt->retrieve)(clientData, &retrieveCtrl);
251 *output_format = retrieveCtrl.output_format;
252 *rec_bufp = (char *) retrieveCtrl.rec_buf;
253 *rec_lenp = retrieveCtrl.rec_len;
258 return retrieveCtrl.diagnostic;