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