2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1999-03-02 16:15:43 quinn
8 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
10 * Revision 1.6 1999/02/18 15:01:25 adam
13 * Revision 1.5 1999/02/17 11:29:56 adam
14 * Fixed in record_fetch. Minor updates to API.
16 * Revision 1.4 1999/02/02 14:51:07 adam
17 * Updated WIN32 code specific sections. Changed header.
19 * Revision 1.3 1998/10/28 10:54:40 adam
22 * Revision 1.2 1998/10/16 08:14:33 adam
23 * Updated record control system.
25 * Revision 1.1 1998/03/05 08:45:13 adam
26 * New result set model and modular ranking system. Moved towards
27 * descent server API. System information stored as "SGML" records.
53 struct fetch_control {
61 static int record_ext_read (void *fh, char *buf, size_t count)
63 struct fetch_control *fc = fh;
64 return read (fc->fd, buf, count);
67 static off_t record_ext_seek (void *fh, off_t offset)
69 struct fetch_control *fc = fh;
70 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
73 static off_t record_ext_tell (void *fh)
75 struct fetch_control *fc = fh;
76 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
79 static off_t record_int_seek (void *fh, off_t offset)
81 struct fetch_control *fc = fh;
82 return (off_t) (fc->record_int_pos = offset);
85 static off_t record_int_tell (void *fh)
87 struct fetch_control *fc = fh;
88 return (off_t) fc->record_int_pos;
91 static int record_int_read (void *fh, char *buf, size_t count)
93 struct fetch_control *fc = fh;
94 int l = fc->record_int_len - fc->record_int_pos;
97 l = (l < (int) count) ? l : count;
98 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
99 fc->record_int_pos += l;
103 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
104 oid_value input_format, Z_RecordComposition *comp,
105 oid_value *output_format, char **rec_bufp,
106 int *rec_lenp, char **basenamep)
109 char *fname, *file_type, *basename;
111 struct recRetrieveCtrl retrieveCtrl;
113 struct fetch_control fc;
114 RecordAttr *recordAttr;
116 rec = rec_get (zh->records, sysno);
119 logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
122 recordAttr = rec_init_attr (zh->zei, rec);
124 file_type = rec->info[recInfo_fileType];
125 fname = rec->info[recInfo_filename];
126 basename = rec->info[recInfo_databaseName];
127 *basenamep = odr_malloc (stream, strlen(basename)+1);
128 strcpy (*basenamep, basename);
130 if (!(rt = recType_byName (zh->recTypes, file_type, subType)))
132 logf (LOG_WARN, "Retrieve: Cannot handle type %s", file_type);
135 logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
136 retrieveCtrl.fh = &fc;
138 if (rec->size[recInfo_storeData] > 0)
140 retrieveCtrl.readf = record_int_read;
141 retrieveCtrl.seekf = record_int_seek;
142 retrieveCtrl.tellf = record_int_tell;
143 fc.record_int_len = rec->size[recInfo_storeData];
144 fc.record_int_buf = rec->info[recInfo_storeData];
145 fc.record_int_pos = 0;
146 logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
149 else if (*fname == '%')
153 char *cp, xname[128];
156 logf (LOG_DEBUG, "SDR");
157 strcpy (xname, fname+1);
158 if ((cp = strrchr (xname, '.')))
163 h = zebraSdr_open (xname);
166 logf (LOG_WARN, "sdr open %s", xname);
169 if (zebraSdr_segment (h, &segment) < 0)
171 logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
176 r = zebraSdr_read (h, &buf);
179 logf (LOG_WARN, "zebraSdr_read fail segment=%d",
186 fc.record_int_len = recordAttr->recordSize;
187 fc.record_int_buf = buf + recordAttr->recordOffset;
188 fc.record_int_pos = 0;
190 logf (LOG_LOG, "segment = %d len=%d off=%d",
192 recordAttr->recordSize,
193 recordAttr->recordOffset);
194 if (fc.record_int_len > 180)
196 logf (LOG_LOG, "%0.70s", fc.record_int_buf);
197 logf (LOG_LOG, "%0.70s", fc.record_int_buf +
198 (fc.record_int_len - 70));
201 logf (LOG_LOG, "%0.*s",
202 fc.record_int_len, fc.record_int_buf);
204 /* the following two lines makes rec_rm delete buf */
205 rec->size[recInfo_storeData] = r;
206 rec->info[recInfo_storeData] = buf;
208 retrieveCtrl.readf = record_int_read;
209 retrieveCtrl.seekf = record_int_seek;
210 retrieveCtrl.tellf = record_int_tell;
215 if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
217 logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
222 fc.record_offset = recordAttr->recordOffset;
224 retrieveCtrl.readf = record_ext_read;
225 retrieveCtrl.seekf = record_ext_seek;
226 retrieveCtrl.tellf = record_ext_tell;
228 record_ext_seek (retrieveCtrl.fh, 0);
230 retrieveCtrl.subType = subType;
231 retrieveCtrl.localno = sysno;
232 retrieveCtrl.score = score;
233 retrieveCtrl.recordSize = recordAttr->recordSize;
234 retrieveCtrl.odr = stream;
235 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
236 retrieveCtrl.comp = comp;
237 retrieveCtrl.diagnostic = 0;
238 retrieveCtrl.dh = zh->dh;
239 retrieveCtrl.res = zh->res;
240 (*rt->retrieve)(&retrieveCtrl);
241 *output_format = retrieveCtrl.output_format;
242 *rec_bufp = retrieveCtrl.rec_buf;
243 *rec_lenp = retrieveCtrl.rec_len;
248 return retrieveCtrl.diagnostic;