Added support for raw retrieval (element set name "R").
[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.14  2001-01-22 11:41:41  adam
8  * Added support for raw retrieval (element set name "R").
9  *
10  * Revision 1.13  2000/03/20 19:08:36  adam
11  * Added remote record import using Z39.50 extended services and Segment
12  * Requests.
13  *
14  * Revision 1.12  2000/03/15 15:00:30  adam
15  * First work on threaded version.
16  *
17  * Revision 1.11  1999/10/29 10:00:00  adam
18  * Fixed minor bug where database name wasn't set in zebra_record_fetch.
19  *
20  * Revision 1.10  1999/05/26 07:49:13  adam
21  * C++ compilation.
22  *
23  * Revision 1.9  1999/05/20 12:57:18  adam
24  * Implemented TCL filter. Updated recctrl system.
25  *
26  * Revision 1.8  1999/03/09 16:27:49  adam
27  * More work on SDRKit integration.
28  *
29  * Revision 1.7  1999/03/02 16:15:43  quinn
30  * Added "tagsysno" and "tagrank" directives to zebra.cfg.
31  *
32  * Revision 1.6  1999/02/18 15:01:25  adam
33  * Minor changes.
34  *
35  * Revision 1.5  1999/02/17 11:29:56  adam
36  * Fixed in record_fetch. Minor updates to API.
37  *
38  * Revision 1.4  1999/02/02 14:51:07  adam
39  * Updated WIN32 code specific sections. Changed header.
40  *
41  * Revision 1.3  1998/10/28 10:54:40  adam
42  * SDRKit integration.
43  *
44  * Revision 1.2  1998/10/16 08:14:33  adam
45  * Updated record control system.
46  *
47  * Revision 1.1  1998/03/05 08:45:13  adam
48  * New result set model and modular ranking system. Moved towards
49  * descent server API. System information stored as "SGML" records.
50  *
51  */
52
53 #include <stdio.h>
54 #include <assert.h>
55
56 #include <fcntl.h>
57 #ifdef WIN32
58 #include <io.h>
59 #include <process.h>
60 #else
61 #include <unistd.h>
62 #endif
63
64 #include <recctrl.h>
65 #include "zserver.h"
66
67 #ifndef ZEBRASDR
68 #define ZEBRASDR 0
69 #endif
70
71 #if ZEBRASDR
72 #include "zebrasdr.h"
73 #endif
74
75 int zebra_record_ext_read (void *fh, char *buf, size_t count)
76 {
77     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
78     return read (fc->fd, buf, count);
79 }
80
81 off_t zebra_record_ext_seek (void *fh, off_t offset)
82 {
83     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
84     return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
85 }
86
87 off_t zebra_record_ext_tell (void *fh)
88 {
89     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
90     return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
91 }
92
93 off_t zebra_record_int_seek (void *fh, off_t offset)
94 {
95     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
96     return (off_t) (fc->record_int_pos = offset);
97 }
98
99 off_t zebra_record_int_tell (void *fh)
100 {
101     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
102     return (off_t) fc->record_int_pos;
103 }
104
105 int zebra_record_int_read (void *fh, char *buf, size_t count)
106 {
107     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
108     int l = fc->record_int_len - fc->record_int_pos;
109     if (l <= 0)
110         return 0;
111     l = (l < (int) count) ? l : count;
112     memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
113     fc->record_int_pos += l;
114     return l;
115 }
116
117 void zebra_record_int_end (void *fh, off_t off)
118 {
119     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
120     fc->offset_end = off;
121 }
122
123 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
124                         oid_value input_format, Z_RecordComposition *comp,
125                         oid_value *output_format, char **rec_bufp,
126                         int *rec_lenp, char **basenamep)
127 {
128     Record rec;
129     char *fname, *file_type, *basename;
130     RecType rt;
131     struct recRetrieveCtrl retrieveCtrl;
132     char subType[128];
133     struct zebra_fetch_control fc;
134     RecordAttr *recordAttr;
135     void *clientData;
136
137     rec = rec_get (zh->service->records, sysno);
138     if (!rec)
139     {
140         logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
141         *basenamep = 0;
142         return 14;
143     }
144     recordAttr = rec_init_attr (zh->service->zei, rec);
145
146     file_type = rec->info[recInfo_fileType];
147     fname = rec->info[recInfo_filename];
148     basename = rec->info[recInfo_databaseName];
149     *basenamep = (char *) odr_malloc (stream, strlen(basename)+1);
150     strcpy (*basenamep, basename);
151
152     if (comp && comp->which == Z_RecordComp_simple &&
153         comp->u.simple->which == Z_ElementSetNames_generic)
154     {
155         if (!strcmp (comp->u.simple->u.generic, "R"))
156             file_type = "text";
157     }
158     if (!(rt = recType_byName (zh->service->recTypes,
159                                file_type, subType, &clientData)))
160     {
161         logf (LOG_WARN, "Retrieve: Cannot handle type %s",  file_type);
162         return 14;
163     }
164     logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
165     retrieveCtrl.fh = &fc;
166     fc.fd = -1;
167     if (rec->size[recInfo_storeData] > 0)
168     {
169         retrieveCtrl.readf = zebra_record_int_read;
170         retrieveCtrl.seekf = zebra_record_int_seek;
171         retrieveCtrl.tellf = zebra_record_int_tell;
172         fc.record_int_len = rec->size[recInfo_storeData];
173         fc.record_int_buf = rec->info[recInfo_storeData];
174         fc.record_int_pos = 0;
175         logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
176     }
177     else
178     {
179         if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
180         {
181             logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
182                   fname);
183             rec_rm (&rec);
184             return 14;
185         }
186         fc.record_offset = recordAttr->recordOffset;
187
188         retrieveCtrl.readf = zebra_record_ext_read;
189         retrieveCtrl.seekf = zebra_record_ext_seek;
190         retrieveCtrl.tellf = zebra_record_ext_tell;
191
192         zebra_record_ext_seek (retrieveCtrl.fh, 0);
193     }
194     retrieveCtrl.subType = subType;
195     retrieveCtrl.localno = sysno;
196     retrieveCtrl.score = score;
197     retrieveCtrl.recordSize = recordAttr->recordSize;
198     retrieveCtrl.odr = stream;
199     retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
200     retrieveCtrl.comp = comp;
201     retrieveCtrl.diagnostic = 0;
202     retrieveCtrl.dh = zh->service->dh;
203     retrieveCtrl.res = zh->service->res;
204     (*rt->retrieve)(clientData, &retrieveCtrl);
205     *output_format = retrieveCtrl.output_format;
206     *rec_bufp = (char *) retrieveCtrl.rec_buf;
207     *rec_lenp = retrieveCtrl.rec_len;
208     if (fc.fd != -1)
209         close (fc.fd);
210     rec_rm (&rec);
211
212     return retrieveCtrl.diagnostic;
213 }