2 * Copyright (C) 1995-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1998-10-28 10:54:40 adam
10 * Revision 1.2 1998/10/16 08:14:33 adam
11 * Updated record control system.
13 * Revision 1.1 1998/03/05 08:45:13 adam
14 * New result set model and modular ranking system. Moved towards
15 * descent server API. System information stored as "SGML" records.
41 struct fetch_control {
49 static int record_ext_read (void *fh, char *buf, size_t count)
51 struct fetch_control *fc = fh;
52 return read (fc->fd, buf, count);
55 static off_t record_ext_seek (void *fh, off_t offset)
57 struct fetch_control *fc = fh;
58 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
61 static off_t record_ext_tell (void *fh)
63 struct fetch_control *fc = fh;
64 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
67 static off_t record_int_seek (void *fh, off_t offset)
69 struct fetch_control *fc = fh;
70 return (off_t) (fc->record_int_pos = offset);
73 static off_t record_int_tell (void *fh)
75 struct fetch_control *fc = fh;
76 return (off_t) fc->record_int_pos;
79 static int record_int_read (void *fh, char *buf, size_t count)
81 struct fetch_control *fc = fh;
82 int l = fc->record_int_len - fc->record_int_pos;
85 l = (l < (int) count) ? l : count;
86 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
87 fc->record_int_pos += l;
91 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
92 oid_value input_format, Z_RecordComposition *comp,
93 oid_value *output_format, char **rec_bufp,
94 int *rec_lenp, char **basenamep)
97 char *fname, *file_type, *basename;
99 struct recRetrieveCtrl retrieveCtrl;
101 struct fetch_control fc;
102 RecordAttr *recordAttr;
104 rec = rec_get (zh->records, sysno);
107 logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
110 recordAttr = rec_init_attr (zh->zei, rec);
112 file_type = rec->info[recInfo_fileType];
113 fname = rec->info[recInfo_filename];
114 basename = rec->info[recInfo_databaseName];
115 *basenamep = odr_malloc (stream, strlen(basename)+1);
116 strcpy (*basenamep, basename);
118 if (!(rt = recType_byName (zh->recTypes, file_type, subType)))
120 logf (LOG_WARN, "Retrieve: Cannot handle type %s", file_type);
123 logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
124 retrieveCtrl.fh = &fc;
126 if (rec->size[recInfo_storeData] > 0)
128 retrieveCtrl.readf = record_int_read;
129 retrieveCtrl.seekf = record_int_seek;
130 retrieveCtrl.tellf = record_int_tell;
131 fc.record_int_len = rec->size[recInfo_storeData];
132 fc.record_int_buf = rec->info[recInfo_storeData];
133 fc.record_int_pos = 0;
134 logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
137 else if (*fname == '%')
141 char *cp, xname[128];
144 logf (LOG_DEBUG, "SDR");
145 strcpy (xname, fname+1);
146 if ((cp = strrchr (xname, '.')))
151 h = zebraSdr_open (xname);
154 logf (LOG_WARN, "sdr open %s", xname);
157 if (zebraSdr_segment (h, &segment) < 0)
159 logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
164 r = zebraSdr_read (h, &buf);
167 logf (LOG_WARN, "zebraSdr_read fail segment=%d",
174 fc.record_int_len = recordAttr->recordSize;
175 fc.record_int_buf = buf + recordAttr->recordOffset;
176 fc.record_int_pos = 0;
178 logf (LOG_LOG, "segment = %d len=%d off=%d",
180 recordAttr->recordSize,
181 recordAttr->recordOffset);
182 if (fc.record_int_len > 180)
184 logf (LOG_LOG, "%0.70s", fc.record_int_buf);
185 logf (LOG_LOG, "%0.70s", fc.record_int_buf +
186 (fc.record_int_len - 70));
189 logf (LOG_LOG, "%0.*s",
190 fc.record_int_len, fc.record_int_buf);
192 /* the following two lines makes rec_rm delete buf */
193 rec->size[recInfo_storeData] = r;
194 rec->info[recInfo_storeData] = buf;
196 retrieveCtrl.readf = record_int_read;
197 retrieveCtrl.seekf = record_int_seek;
198 retrieveCtrl.tellf = record_int_tell;
203 if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
205 logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
210 fc.record_offset = recordAttr->recordOffset;
212 retrieveCtrl.readf = record_ext_read;
213 retrieveCtrl.seekf = record_ext_seek;
214 retrieveCtrl.tellf = record_ext_tell;
216 record_ext_seek (retrieveCtrl.fh, 0);
218 retrieveCtrl.subType = subType;
219 retrieveCtrl.localno = sysno;
220 retrieveCtrl.score = score;
221 retrieveCtrl.recordSize = recordAttr->recordSize;
222 retrieveCtrl.odr = stream;
223 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
224 retrieveCtrl.comp = comp;
225 retrieveCtrl.diagnostic = 0;
226 retrieveCtrl.dh = zh->dh;
227 (*rt->retrieve)(&retrieveCtrl);
228 *output_format = retrieveCtrl.output_format;
229 *rec_bufp = retrieveCtrl.rec_buf;
230 *rec_lenp = retrieveCtrl.rec_len;
235 return retrieveCtrl.diagnostic;