5e2ed207f68e9cd8fa3ef368be544134083a9e79
[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.8  1999-03-09 16:27:49  adam
8  * More work on SDRKit integration.
9  *
10  * Revision 1.7  1999/03/02 16:15:43  quinn
11  * Added "tagsysno" and "tagrank" directives to zebra.cfg.
12  *
13  * Revision 1.6  1999/02/18 15:01:25  adam
14  * Minor changes.
15  *
16  * Revision 1.5  1999/02/17 11:29:56  adam
17  * Fixed in record_fetch. Minor updates to API.
18  *
19  * Revision 1.4  1999/02/02 14:51:07  adam
20  * Updated WIN32 code specific sections. Changed header.
21  *
22  * Revision 1.3  1998/10/28 10:54:40  adam
23  * SDRKit integration.
24  *
25  * Revision 1.2  1998/10/16 08:14:33  adam
26  * Updated record control system.
27  *
28  * Revision 1.1  1998/03/05 08:45:13  adam
29  * New result set model and modular ranking system. Moved towards
30  * descent server API. System information stored as "SGML" records.
31  *
32  */
33
34 #include <stdio.h>
35 #include <assert.h>
36
37 #include <fcntl.h>
38 #ifdef WIN32
39 #include <io.h>
40 #include <process.h>
41 #else
42 #include <unistd.h>
43 #endif
44
45 #include <recctrl.h>
46 #include "zserver.h"
47
48 #ifndef ZEBRASDR
49 #define ZEBRASDR 0
50 #endif
51
52 #if ZEBRASDR
53 #include "zebrasdr.h"
54 #endif
55
56 struct fetch_control {
57     int record_offset;
58     int record_int_pos;
59     char *record_int_buf;
60     int record_int_len;
61     int fd;
62 };
63
64 static int record_ext_read (void *fh, char *buf, size_t count)
65 {
66     struct fetch_control *fc = fh;
67     return read (fc->fd, buf, count);
68 }
69
70 static off_t record_ext_seek (void *fh, off_t offset)
71 {
72     struct fetch_control *fc = fh;
73     return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
74 }
75
76 static off_t record_ext_tell (void *fh)
77 {
78     struct fetch_control *fc = fh;
79     return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
80 }
81
82 static off_t record_int_seek (void *fh, off_t offset)
83 {
84     struct fetch_control *fc = fh;
85     return (off_t) (fc->record_int_pos = offset);
86 }
87
88 static off_t record_int_tell (void *fh)
89 {
90     struct fetch_control *fc = fh;
91     return (off_t) fc->record_int_pos;
92 }
93
94 static int record_int_read (void *fh, char *buf, size_t count)
95 {
96     struct fetch_control *fc = fh;
97     int l = fc->record_int_len - fc->record_int_pos;
98     if (l <= 0)
99         return 0;
100     l = (l < (int) count) ? l : count;
101     memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
102     fc->record_int_pos += l;
103     return l;
104 }
105
106 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
107                         oid_value input_format, Z_RecordComposition *comp,
108                         oid_value *output_format, char **rec_bufp,
109                         int *rec_lenp, char **basenamep)
110 {
111     Record rec;
112     char *fname, *file_type, *basename;
113     RecType rt;
114     struct recRetrieveCtrl retrieveCtrl;
115     char subType[128];
116     struct fetch_control fc;
117     RecordAttr *recordAttr;
118
119     rec = rec_get (zh->records, sysno);
120     if (!rec)
121     {
122         logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
123         return 14;
124     }
125     recordAttr = rec_init_attr (zh->zei, rec);
126
127     file_type = rec->info[recInfo_fileType];
128     fname = rec->info[recInfo_filename];
129     basename = rec->info[recInfo_databaseName];
130     *basenamep = odr_malloc (stream, strlen(basename)+1);
131     strcpy (*basenamep, basename);
132
133     if (!(rt = recType_byName (zh->recTypes, file_type, subType)))
134     {
135         logf (LOG_WARN, "Retrieve: Cannot handle type %s",  file_type);
136         return 14;
137     }
138     logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
139     retrieveCtrl.fh = &fc;
140     fc.fd = -1;
141     if (rec->size[recInfo_storeData] > 0)
142     {
143         retrieveCtrl.readf = record_int_read;
144         retrieveCtrl.seekf = record_int_seek;
145         retrieveCtrl.tellf = record_int_tell;
146         fc.record_int_len = rec->size[recInfo_storeData];
147         fc.record_int_buf = rec->info[recInfo_storeData];
148         fc.record_int_pos = 0;
149         logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
150     }
151 #if ZEBRASDR
152     else if (*fname == '%')
153     {   
154         ZebraSdrHandle h;
155         int segment = 0, r;
156         char *cp, xname[128];
157         unsigned char *buf;
158
159         logf (LOG_DEBUG, "SDR");
160         strcpy (xname, fname+1);
161         if ((cp = strrchr (xname, '.')))
162         {
163             *cp++ = 0;
164             segment = atoi(cp);
165         }
166         h = zebraSdr_open (xname);
167         if (!h)
168         {
169             logf (LOG_WARN, "sdr open %s", xname);
170             return 0;
171         }
172         if (zebraSdr_segment (h, &segment) < 0)
173         {
174             logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
175                 segment);
176             rec_rm (&rec);
177             return 14;
178         }    
179         r = zebraSdr_read (h, &buf);
180         if (r < 1)
181         {
182             logf (LOG_WARN, "zebraSdr_read fail segment=%d",
183                 segment);
184             rec_rm (&rec);
185             return 14;
186         }
187         zebraSdr_close (h);
188
189         fc.record_int_len = recordAttr->recordSize;
190         fc.record_int_buf = buf + recordAttr->recordOffset;
191         fc.record_int_pos = 0;
192
193         logf (LOG_LOG, "segment = %d len=%d off=%d",
194             segment,
195             recordAttr->recordSize,
196             recordAttr->recordOffset);
197         if (fc.record_int_len > 180)
198         {
199             logf (LOG_LOG, "%.70s", fc.record_int_buf);
200             logf (LOG_LOG, "%.70s", fc.record_int_buf +
201                 (fc.record_int_len - 70));
202         }
203         else
204             logf (LOG_LOG, "%.*s",
205                 fc.record_int_len, fc.record_int_buf);
206
207         /* the following two lines makes rec_rm delete buf */
208         rec->size[recInfo_storeData] = r;
209         rec->info[recInfo_storeData] = buf;
210
211         retrieveCtrl.readf = record_int_read;
212         retrieveCtrl.seekf = record_int_seek;
213         retrieveCtrl.tellf = record_int_tell;
214     }
215 #endif
216     else
217     {
218         if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
219         {
220             logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
221                   fname);
222             rec_rm (&rec);
223             return 14;
224         }
225         fc.record_offset = recordAttr->recordOffset;
226
227         retrieveCtrl.readf = record_ext_read;
228         retrieveCtrl.seekf = record_ext_seek;
229         retrieveCtrl.tellf = record_ext_tell;
230
231         record_ext_seek (retrieveCtrl.fh, 0);
232     }
233     retrieveCtrl.subType = subType;
234     retrieveCtrl.localno = sysno;
235     retrieveCtrl.score = score;
236     retrieveCtrl.recordSize = recordAttr->recordSize;
237     retrieveCtrl.odr = stream;
238     retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
239     retrieveCtrl.comp = comp;
240     retrieveCtrl.diagnostic = 0;
241     retrieveCtrl.dh = zh->dh;
242     retrieveCtrl.res = zh->res;
243     (*rt->retrieve)(&retrieveCtrl);
244     *output_format = retrieveCtrl.output_format;
245     *rec_bufp = retrieveCtrl.rec_buf;
246     *rec_lenp = retrieveCtrl.rec_len;
247     if (fc.fd != -1)
248         close (fc.fd);
249     rec_rm (&rec);
250
251     return retrieveCtrl.diagnostic;
252 }