SDRKit integration.
[idzebra-moved-to-github.git] / index / retrieve.c
1 /*
2  * Copyright (C) 1995-1998, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: retrieve.c,v $
7  * Revision 1.3  1998-10-28 10:54:40  adam
8  * SDRKit integration.
9  *
10  * Revision 1.2  1998/10/16 08:14:33  adam
11  * Updated record control system.
12  *
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.
16  *
17  */
18
19 #include <stdio.h>
20 #include <assert.h>
21
22 #include <fcntl.h>
23 #ifdef WINDOWS
24 #include <io.h>
25 #include <process.h>
26 #else
27 #include <unistd.h>
28 #endif
29
30 #include <recctrl.h>
31 #include "zserver.h"
32
33 #ifndef ZEBRASDR
34 #define ZEBRASDR 0
35 #endif
36
37 #if ZEBRASDR
38 #include "zebrasdr.h"
39 #endif
40
41 struct fetch_control {
42     int record_offset;
43     int record_int_pos;
44     char *record_int_buf;
45     int record_int_len;
46     int fd;
47 };
48
49 static int record_ext_read (void *fh, char *buf, size_t count)
50 {
51     struct fetch_control *fc = fh;
52     return read (fc->fd, buf, count);
53 }
54
55 static off_t record_ext_seek (void *fh, off_t offset)
56 {
57     struct fetch_control *fc = fh;
58     return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
59 }
60
61 static off_t record_ext_tell (void *fh)
62 {
63     struct fetch_control *fc = fh;
64     return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
65 }
66
67 static off_t record_int_seek (void *fh, off_t offset)
68 {
69     struct fetch_control *fc = fh;
70     return (off_t) (fc->record_int_pos = offset);
71 }
72
73 static off_t record_int_tell (void *fh)
74 {
75     struct fetch_control *fc = fh;
76     return (off_t) fc->record_int_pos;
77 }
78
79 static int record_int_read (void *fh, char *buf, size_t count)
80 {
81     struct fetch_control *fc = fh;
82     int l = fc->record_int_len - fc->record_int_pos;
83     if (l <= 0)
84         return 0;
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;
88     return l;
89 }
90
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)
95 {
96     Record rec;
97     char *fname, *file_type, *basename;
98     RecType rt;
99     struct recRetrieveCtrl retrieveCtrl;
100     char subType[128];
101     struct fetch_control fc;
102     RecordAttr *recordAttr;
103
104     rec = rec_get (zh->records, sysno);
105     if (!rec)
106     {
107         logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
108         return 14;
109     }
110     recordAttr = rec_init_attr (zh->zei, rec);
111
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);
117
118     if (!(rt = recType_byName (zh->recTypes, file_type, subType)))
119     {
120         logf (LOG_WARN, "Retrieve: Cannot handle type %s",  file_type);
121         return 14;
122     }
123     logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
124     retrieveCtrl.fh = &fc;
125     fc.fd = -1;
126     if (rec->size[recInfo_storeData] > 0)
127     {
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);
135     }
136 #if ZEBRASDR
137     else if (*fname == '%')
138     {   
139         ZebraSdrHandle h;
140         int segment = 0, r;
141         char *cp, xname[128];
142         unsigned char *buf;
143
144         logf (LOG_DEBUG, "SDR");
145         strcpy (xname, fname+1);
146         if ((cp = strrchr (xname, '.')))
147         {
148             *cp++ = 0;
149             segment = atoi(cp);
150         }
151         h = zebraSdr_open (xname);
152         if (!h)
153         {
154             logf (LOG_WARN, "sdr open %s", xname);
155             return 0;
156         }
157         if (zebraSdr_segment (h, &segment) < 0)
158         {
159             logf (LOG_WARN, "zebraSdr_segment fail segment=%d",
160                 segment);
161             rec_rm (&rec);
162             return 14;
163         }    
164         r = zebraSdr_read (h, &buf);
165         if (r < 1)
166         {
167             logf (LOG_WARN, "zebraSdr_read fail segment=%d",
168                 segment);
169             rec_rm (&rec);
170             return 14;
171         }
172         zebraSdr_close (h);
173
174         fc.record_int_len = recordAttr->recordSize;
175         fc.record_int_buf = buf + recordAttr->recordOffset;
176         fc.record_int_pos = 0;
177
178         logf (LOG_LOG, "segment = %d len=%d off=%d",
179             segment,
180             recordAttr->recordSize,
181             recordAttr->recordOffset);
182         if (fc.record_int_len > 180)
183         {
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));
187         }
188         else
189             logf (LOG_LOG, "%0.*s",
190                 fc.record_int_len, fc.record_int_buf);
191
192         /* the following two lines makes rec_rm delete buf */
193         rec->size[recInfo_storeData] = r;
194         rec->info[recInfo_storeData] = buf;
195
196         retrieveCtrl.readf = record_int_read;
197         retrieveCtrl.seekf = record_int_seek;
198         retrieveCtrl.tellf = record_int_tell;
199     }
200 #endif
201     else
202     {
203         if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
204         {
205             logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
206                   fname);
207             rec_rm (&rec);
208             return 14;
209         }
210         fc.record_offset = recordAttr->recordOffset;
211
212         retrieveCtrl.readf = record_ext_read;
213         retrieveCtrl.seekf = record_ext_seek;
214         retrieveCtrl.tellf = record_ext_tell;
215
216         record_ext_seek (retrieveCtrl.fh, 0);
217     }
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;
231     if (fc.fd != -1)
232         close (fc.fd);
233     rec_rm (&rec);
234
235     return retrieveCtrl.diagnostic;
236 }