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