Implemented TCL filter. Updated recctrl system.
[idzebra-moved-to-github.git] / index / retrieve.c
1 /*
2  * Copyright (C) 1995-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: retrieve.c,v $
7  * Revision 1.9  1999-05-20 12:57:18  adam
8  * Implemented TCL filter. Updated recctrl system.
9  *
10  * Revision 1.8  1999/03/09 16:27:49  adam
11  * More work on SDRKit integration.
12  *
13  * Revision 1.7  1999/03/02 16:15:43  quinn
14  * Added "tagsysno" and "tagrank" directives to zebra.cfg.
15  *
16  * Revision 1.6  1999/02/18 15:01:25  adam
17  * Minor changes.
18  *
19  * Revision 1.5  1999/02/17 11:29:56  adam
20  * Fixed in record_fetch. Minor updates to API.
21  *
22  * Revision 1.4  1999/02/02 14:51:07  adam
23  * Updated WIN32 code specific sections. Changed header.
24  *
25  * Revision 1.3  1998/10/28 10:54:40  adam
26  * SDRKit integration.
27  *
28  * Revision 1.2  1998/10/16 08:14:33  adam
29  * Updated record control system.
30  *
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.
34  *
35  */
36
37 #include <stdio.h>
38 #include <assert.h>
39
40 #include <fcntl.h>
41 #ifdef WIN32
42 #include <io.h>
43 #include <process.h>
44 #else
45 #include <unistd.h>
46 #endif
47
48 #include <recctrl.h>
49 #include "zserver.h"
50
51 #ifndef ZEBRASDR
52 #define ZEBRASDR 0
53 #endif
54
55 #if ZEBRASDR
56 #include "zebrasdr.h"
57 #endif
58
59 struct fetch_control {
60     int record_offset;
61     int record_int_pos;
62     char *record_int_buf;
63     int record_int_len;
64     int fd;
65 };
66
67 static int record_ext_read (void *fh, char *buf, size_t count)
68 {
69     struct fetch_control *fc = fh;
70     return read (fc->fd, buf, count);
71 }
72
73 static off_t record_ext_seek (void *fh, off_t offset)
74 {
75     struct fetch_control *fc = fh;
76     return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
77 }
78
79 static off_t record_ext_tell (void *fh)
80 {
81     struct fetch_control *fc = fh;
82     return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
83 }
84
85 static off_t record_int_seek (void *fh, off_t offset)
86 {
87     struct fetch_control *fc = fh;
88     return (off_t) (fc->record_int_pos = offset);
89 }
90
91 static off_t record_int_tell (void *fh)
92 {
93     struct fetch_control *fc = fh;
94     return (off_t) fc->record_int_pos;
95 }
96
97 static int record_int_read (void *fh, char *buf, size_t count)
98 {
99     struct fetch_control *fc = fh;
100     int l = fc->record_int_len - fc->record_int_pos;
101     if (l <= 0)
102         return 0;
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;
106     return l;
107 }
108
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)
113 {
114     Record rec;
115     char *fname, *file_type, *basename;
116     RecType rt;
117     struct recRetrieveCtrl retrieveCtrl;
118     char subType[128];
119     struct fetch_control fc;
120     RecordAttr *recordAttr;
121     void *clientData;
122
123     rec = rec_get (zh->records, sysno);
124     if (!rec)
125     {
126         logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
127         return 14;
128     }
129     recordAttr = rec_init_attr (zh->zei, rec);
130
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);
136
137     if (!(rt = recType_byName (zh->recTypes, file_type, subType, &clientData)))
138     {
139         logf (LOG_WARN, "Retrieve: Cannot handle type %s",  file_type);
140         return 14;
141     }
142     logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
143     retrieveCtrl.fh = &fc;
144     fc.fd = -1;
145     if (rec->size[recInfo_storeData] > 0)
146     {
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);
154     }
155 #if ZEBRASDR
156     else if (*fname == '%')
157     {   
158         ZebraSdrHandle h;
159         int segment = 0, r;
160         char *cp, xname[128];
161         unsigned char *buf;
162
163         logf (LOG_DEBUG, "SDR");
164         strcpy (xname, fname+1);
165         if ((cp = strrchr (xname, '.')))
166         {
167             *cp++ = 0;
168             segment = atoi(cp);
169         }
170         h = zebraSdr_open (xname);
171         if (!h)
172         {
173             logf (LOG_WARN, "sdr open %s", xname);
174             return 0;
175         }
176         if (zebraSdr_segment (h, &segment) < 0)
177         {
178             logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
179                 segment);
180             rec_rm (&rec);
181             return 14;
182         }    
183         r = zebraSdr_read (h, &buf);
184         if (r < 1)
185         {
186             logf (LOG_WARN, "zebraSdr_read fail segment=%d",
187                 segment);
188             rec_rm (&rec);
189             return 14;
190         }
191         zebraSdr_close (h);
192
193         fc.record_int_len = recordAttr->recordSize;
194         fc.record_int_buf = buf + recordAttr->recordOffset;
195         fc.record_int_pos = 0;
196
197         logf (LOG_LOG, "segment = %d len=%d off=%d",
198             segment,
199             recordAttr->recordSize,
200             recordAttr->recordOffset);
201         if (fc.record_int_len > 180)
202         {
203             logf (LOG_LOG, "%.70s", fc.record_int_buf);
204             logf (LOG_LOG, "%.70s", fc.record_int_buf +
205                 (fc.record_int_len - 70));
206         }
207         else
208             logf (LOG_LOG, "%.*s",
209                 fc.record_int_len, fc.record_int_buf);
210
211         /* the following two lines makes rec_rm delete buf */
212         rec->size[recInfo_storeData] = r;
213         rec->info[recInfo_storeData] = buf;
214
215         retrieveCtrl.readf = record_int_read;
216         retrieveCtrl.seekf = record_int_seek;
217         retrieveCtrl.tellf = record_int_tell;
218     }
219 #endif
220     else
221     {
222         if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
223         {
224             logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
225                   fname);
226             rec_rm (&rec);
227             return 14;
228         }
229         fc.record_offset = recordAttr->recordOffset;
230
231         retrieveCtrl.readf = record_ext_read;
232         retrieveCtrl.seekf = record_ext_seek;
233         retrieveCtrl.tellf = record_ext_tell;
234
235         record_ext_seek (retrieveCtrl.fh, 0);
236     }
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;
251     if (fc.fd != -1)
252         close (fc.fd);
253     rec_rm (&rec);
254
255     return retrieveCtrl.diagnostic;
256 }