Update copyright year + FSF address
[idzebra-moved-to-github.git] / index / retrieve.c
1 /* $Id: retrieve.c,v 1.43 2006-08-14 10:40:15 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25
26 #include <fcntl.h>
27 #ifdef WIN32
28 #include <io.h>
29 #include <process.h>
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include "index.h"
36 #include <direntz.h>
37
38 int zebra_record_ext_read (void *fh, char *buf, size_t count)
39 {
40     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
41     return read (fc->fd, buf, count);
42 }
43
44 off_t zebra_record_ext_seek (void *fh, off_t offset)
45 {
46     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
47     return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
48 }
49
50 off_t zebra_record_ext_tell (void *fh)
51 {
52     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
53     return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
54 }
55
56 off_t zebra_record_int_seek (void *fh, off_t offset)
57 {
58     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
59     return (off_t) (fc->record_int_pos = offset);
60 }
61
62 off_t zebra_record_int_tell (void *fh)
63 {
64     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
65     return (off_t) fc->record_int_pos;
66 }
67
68 int zebra_record_int_read (void *fh, char *buf, size_t count)
69 {
70     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
71     int l = fc->record_int_len - fc->record_int_pos;
72     if (l <= 0)
73         return 0;
74     l = (l < (int) count) ? l : (int) count;
75     memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
76     fc->record_int_pos += l;
77     return l;
78 }
79
80 void zebra_record_int_end (void *fh, off_t off)
81 {
82     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
83     fc->offset_end = off;
84 }
85
86 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score,
87                         zebra_snippets *hit_snippet, ODR stream,
88                         oid_value input_format, Z_RecordComposition *comp,
89                         oid_value *output_format, char **rec_bufp,
90                         int *rec_lenp, char **basenamep,
91                         char **addinfo)
92 {
93     Record rec;
94     char *fname, *file_type, *basename;
95     RecType rt;
96     struct recRetrieveCtrl retrieveCtrl;
97     struct zebra_fetch_control fc;
98     RecordAttr *recordAttr;
99     void *clientData;
100     int raw_mode = 0;
101
102     *basenamep = 0;
103     *addinfo = 0;
104     if (comp && comp->which == Z_RecordComp_simple &&
105         comp->u.simple->which == Z_ElementSetNames_generic && 
106         !strcmp (comp->u.simple->u.generic, "_sysno_"))
107     {
108         char rec_str[60];
109         sprintf(rec_str, ZINT_FORMAT, sysno);
110         *output_format = VAL_SUTRS;
111         *rec_lenp = strlen(rec_str);
112         *rec_bufp = odr_strdup(stream, rec_str);
113         return 0;
114     }
115     rec = rec_get (zh->reg->records, sysno);
116     if (!rec)
117     {
118         yaz_log (YLOG_DEBUG, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
119         *basenamep = 0;
120         return 14;
121     }
122     recordAttr = rec_init_attr (zh->reg->zei, rec);
123
124     file_type = rec->info[recInfo_fileType];
125     fname = rec->info[recInfo_filename];
126     basename = rec->info[recInfo_databaseName];
127     *basenamep = (char *) odr_malloc (stream, strlen(basename)+1);
128     strcpy (*basenamep, basename);
129
130     if (comp && comp->which == Z_RecordComp_simple &&
131         comp->u.simple->which == Z_ElementSetNames_generic && 
132         !strcmp (comp->u.simple->u.generic, "_storekeys_"))
133     {
134         WRBUF wrbuf = wrbuf_alloc();
135         zebra_rec_keys_t keys = zebra_rec_keys_open();
136         zebra_rec_keys_set_buf(keys,
137                                rec->info[recInfo_delKeys],
138                                rec->size[recInfo_delKeys],
139                                0);
140         if (zebra_rec_keys_rewind(keys))
141         {
142             size_t slen;
143             const char *str;
144             struct it_key key_in;
145             while(zebra_rec_keys_read(keys, &str, &slen, &key_in))
146             {
147                 int i;
148                 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
149                 int index_type;
150                 const char *db = 0;
151                 int set = 0;
152                 int use = 0;
153                 const char *string_index = 0;
154                 char dst_buf[IT_MAX_WORD];
155                 
156                 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
157                                         &string_index);
158
159                 if (string_index)
160                     wrbuf_printf(wrbuf, "%s", string_index);
161                 else
162                     wrbuf_printf(wrbuf, "set=%d,use=%d", set, use);
163
164                 zebra_term_untrans(zh, index_type, dst_buf, str);
165                 wrbuf_printf(wrbuf, " %s", dst_buf);
166
167                 for (i = 1; i < key_in.len; i++)
168                     wrbuf_printf(wrbuf, " " ZINT_FORMAT, key_in.mem[i]);
169                 wrbuf_printf(wrbuf, "\n");
170
171             }
172         }
173         *output_format = VAL_SUTRS;
174         *rec_lenp = wrbuf_len(wrbuf);
175         *rec_bufp = odr_malloc(stream, *rec_lenp);
176         memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
177         wrbuf_free(wrbuf, 1);
178         zebra_rec_keys_close(keys);
179         return 0;
180     }
181     if (comp && comp->which == Z_RecordComp_simple &&
182         comp->u.simple->which == Z_ElementSetNames_generic && 
183         !strcmp (comp->u.simple->u.generic, "R"))
184     {
185         raw_mode = 1;
186     }
187     if (!(rt = recType_byName (zh->reg->recTypes, zh->res,
188                                file_type, &clientData)))
189     {
190         yaz_log (YLOG_WARN, "Retrieve: Cannot handle type %s",  file_type);
191         return 14;
192     }
193     yaz_log (YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d", sysno,score);
194     retrieveCtrl.fh = &fc;
195     fc.fd = -1;
196     retrieveCtrl.fname = fname;
197     if (rec->size[recInfo_storeData] > 0)
198     {
199         retrieveCtrl.readf = zebra_record_int_read;
200         retrieveCtrl.seekf = zebra_record_int_seek;
201         retrieveCtrl.tellf = zebra_record_int_tell;
202         fc.record_int_len = rec->size[recInfo_storeData];
203         fc.record_int_buf = rec->info[recInfo_storeData];
204         fc.record_int_pos = 0;
205         yaz_log (YLOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
206         if (raw_mode)
207         {
208             *output_format = VAL_SUTRS;
209             *rec_lenp = rec->size[recInfo_storeData];
210             *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
211             memcpy(*rec_bufp, rec->info[recInfo_storeData], *rec_lenp);
212             rec_rm (&rec);
213             return 0;
214         }
215     }
216     else
217     {
218         char full_rep[1024];
219
220         if (zh->path_reg && !yaz_is_abspath (fname))
221         {
222             strcpy (full_rep, zh->path_reg);
223             strcat (full_rep, "/");
224             strcat (full_rep, fname);
225         }
226         else
227             strcpy (full_rep, fname);
228
229         if ((fc.fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
230         {
231             yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
232                   full_rep);
233             rec_rm (&rec);
234             return 14;
235         }
236         fc.record_offset = recordAttr->recordOffset;
237
238         retrieveCtrl.readf = zebra_record_ext_read;
239         retrieveCtrl.seekf = zebra_record_ext_seek;
240         retrieveCtrl.tellf = zebra_record_ext_tell;
241
242         zebra_record_ext_seek (retrieveCtrl.fh, 0);
243         if (raw_mode)
244         {
245             *output_format = VAL_SUTRS;
246             *rec_lenp = recordAttr->recordSize;
247             *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
248             zebra_record_ext_read(&fc, *rec_bufp, *rec_lenp);
249             rec_rm (&rec);
250             close (fc.fd);
251             return 0;
252         }
253     }
254     retrieveCtrl.localno = sysno;
255     retrieveCtrl.staticrank = recordAttr->staticrank;
256     retrieveCtrl.score = score;
257     retrieveCtrl.recordSize = recordAttr->recordSize;
258     retrieveCtrl.odr = stream;
259     retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
260     retrieveCtrl.comp = comp;
261     retrieveCtrl.encoding = zh->record_encoding;
262     retrieveCtrl.diagnostic = 0;
263     retrieveCtrl.addinfo = 0;
264     retrieveCtrl.dh = zh->reg->dh;
265     retrieveCtrl.res = zh->res;
266     retrieveCtrl.rec_buf = 0;
267     retrieveCtrl.rec_len = -1;
268     retrieveCtrl.hit_snippet = hit_snippet;
269     retrieveCtrl.doc_snippet = zebra_snippets_create();
270     
271     if (1)
272     {
273         /* snippets code */
274         zebra_snippets *snippet;
275
276         zebra_rec_keys_t reckeys = zebra_rec_keys_open();
277         
278         zebra_rec_keys_set_buf(reckeys,
279                                rec->info[recInfo_delKeys],
280                                rec->size[recInfo_delKeys], 
281                                0);
282         zebra_snippets_rec_keys(zh, reckeys, retrieveCtrl.doc_snippet);
283         zebra_rec_keys_close(reckeys);
284
285
286 #if 0
287         /* for debugging purposes */
288         yaz_log(YLOG_LOG, "DOC SNIPPET:");
289         zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
290         yaz_log(YLOG_LOG, "HIT SNIPPET:");
291         zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
292 #endif
293         snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
294                                         retrieveCtrl.hit_snippet,
295                                         10);
296 #if 0
297         /* for debugging purposes */
298         yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
299         zebra_snippets_log(snippet, YLOG_LOG);
300 #endif
301         (*rt->retrieve)(clientData, &retrieveCtrl);
302
303         zebra_snippets_destroy(snippet);
304     }
305     else
306     {
307         (*rt->retrieve)(clientData, &retrieveCtrl);
308     }
309
310     zebra_snippets_destroy(retrieveCtrl.doc_snippet);
311
312     *output_format = retrieveCtrl.output_format;
313     *rec_bufp = (char *) retrieveCtrl.rec_buf;
314     *rec_lenp = retrieveCtrl.rec_len;
315     if (fc.fd != -1)
316         close (fc.fd);
317     rec_rm (&rec);
318
319     *addinfo = retrieveCtrl.addinfo;
320     return retrieveCtrl.diagnostic;
321 }
322 /*
323  * Local variables:
324  * c-basic-offset: 4
325  * indent-tabs-mode: nil
326  * End:
327  * vim: shiftwidth=4 tabstop=8 expandtab
328  */
329