2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1999-05-20 12:57:18 adam
8 * Implemented TCL filter. Updated recctrl system.
10 * Revision 1.8 1999/03/09 16:27:49 adam
11 * More work on SDRKit integration.
13 * Revision 1.7 1999/03/02 16:15:43 quinn
14 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
16 * Revision 1.6 1999/02/18 15:01:25 adam
19 * Revision 1.5 1999/02/17 11:29:56 adam
20 * Fixed in record_fetch. Minor updates to API.
22 * Revision 1.4 1999/02/02 14:51:07 adam
23 * Updated WIN32 code specific sections. Changed header.
25 * Revision 1.3 1998/10/28 10:54:40 adam
28 * Revision 1.2 1998/10/16 08:14:33 adam
29 * Updated record control system.
31 * Revision 1.1 1998/03/05 08:45:13 adam
32 * New result set model and modular ranking system. Moved towards
33 * descent server API. System information stored as "SGML" records.
59 struct fetch_control {
67 static int record_ext_read (void *fh, char *buf, size_t count)
69 struct fetch_control *fc = fh;
70 return read (fc->fd, buf, count);
73 static off_t record_ext_seek (void *fh, off_t offset)
75 struct fetch_control *fc = fh;
76 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
79 static off_t record_ext_tell (void *fh)
81 struct fetch_control *fc = fh;
82 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
85 static off_t record_int_seek (void *fh, off_t offset)
87 struct fetch_control *fc = fh;
88 return (off_t) (fc->record_int_pos = offset);
91 static off_t record_int_tell (void *fh)
93 struct fetch_control *fc = fh;
94 return (off_t) fc->record_int_pos;
97 static int record_int_read (void *fh, char *buf, size_t count)
99 struct fetch_control *fc = fh;
100 int l = fc->record_int_len - fc->record_int_pos;
103 l = (l < (int) count) ? l : count;
104 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
105 fc->record_int_pos += l;
109 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
110 oid_value input_format, Z_RecordComposition *comp,
111 oid_value *output_format, char **rec_bufp,
112 int *rec_lenp, char **basenamep)
115 char *fname, *file_type, *basename;
117 struct recRetrieveCtrl retrieveCtrl;
119 struct fetch_control fc;
120 RecordAttr *recordAttr;
123 rec = rec_get (zh->records, sysno);
126 logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
129 recordAttr = rec_init_attr (zh->zei, rec);
131 file_type = rec->info[recInfo_fileType];
132 fname = rec->info[recInfo_filename];
133 basename = rec->info[recInfo_databaseName];
134 *basenamep = odr_malloc (stream, strlen(basename)+1);
135 strcpy (*basenamep, basename);
137 if (!(rt = recType_byName (zh->recTypes, file_type, subType, &clientData)))
139 logf (LOG_WARN, "Retrieve: Cannot handle type %s", file_type);
142 logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
143 retrieveCtrl.fh = &fc;
145 if (rec->size[recInfo_storeData] > 0)
147 retrieveCtrl.readf = record_int_read;
148 retrieveCtrl.seekf = record_int_seek;
149 retrieveCtrl.tellf = record_int_tell;
150 fc.record_int_len = rec->size[recInfo_storeData];
151 fc.record_int_buf = rec->info[recInfo_storeData];
152 fc.record_int_pos = 0;
153 logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
156 else if (*fname == '%')
160 char *cp, xname[128];
163 logf (LOG_DEBUG, "SDR");
164 strcpy (xname, fname+1);
165 if ((cp = strrchr (xname, '.')))
170 h = zebraSdr_open (xname);
173 logf (LOG_WARN, "sdr open %s", xname);
176 if (zebraSdr_segment (h, &segment) < 0)
178 logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
183 r = zebraSdr_read (h, &buf);
186 logf (LOG_WARN, "zebraSdr_read fail segment=%d",
193 fc.record_int_len = recordAttr->recordSize;
194 fc.record_int_buf = buf + recordAttr->recordOffset;
195 fc.record_int_pos = 0;
197 logf (LOG_LOG, "segment = %d len=%d off=%d",
199 recordAttr->recordSize,
200 recordAttr->recordOffset);
201 if (fc.record_int_len > 180)
203 logf (LOG_LOG, "%.70s", fc.record_int_buf);
204 logf (LOG_LOG, "%.70s", fc.record_int_buf +
205 (fc.record_int_len - 70));
208 logf (LOG_LOG, "%.*s",
209 fc.record_int_len, fc.record_int_buf);
211 /* the following two lines makes rec_rm delete buf */
212 rec->size[recInfo_storeData] = r;
213 rec->info[recInfo_storeData] = buf;
215 retrieveCtrl.readf = record_int_read;
216 retrieveCtrl.seekf = record_int_seek;
217 retrieveCtrl.tellf = record_int_tell;
222 if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
224 logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
229 fc.record_offset = recordAttr->recordOffset;
231 retrieveCtrl.readf = record_ext_read;
232 retrieveCtrl.seekf = record_ext_seek;
233 retrieveCtrl.tellf = record_ext_tell;
235 record_ext_seek (retrieveCtrl.fh, 0);
237 retrieveCtrl.subType = subType;
238 retrieveCtrl.localno = sysno;
239 retrieveCtrl.score = score;
240 retrieveCtrl.recordSize = recordAttr->recordSize;
241 retrieveCtrl.odr = stream;
242 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
243 retrieveCtrl.comp = comp;
244 retrieveCtrl.diagnostic = 0;
245 retrieveCtrl.dh = zh->dh;
246 retrieveCtrl.res = zh->res;
247 (*rt->retrieve)(clientData, &retrieveCtrl);
248 *output_format = retrieveCtrl.output_format;
249 *rec_bufp = retrieveCtrl.rec_buf;
250 *rec_lenp = retrieveCtrl.rec_len;
255 return retrieveCtrl.diagnostic;