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