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