8dbe8a55a8e85623ea47c36d089b31c03a73cee9
[idzebra-moved-to-github.git] / index / retrieve.c
1 /* $Id: retrieve.c,v 1.31 2005-06-07 11:36:38 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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 #else
31 #include <unistd.h>
32 #endif
33
34 #include "index.h"
35 #include <direntz.h>
36
37 int zebra_record_ext_read (void *fh, char *buf, size_t count)
38 {
39     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
40     return read (fc->fd, buf, count);
41 }
42
43 off_t zebra_record_ext_seek (void *fh, off_t offset)
44 {
45     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
46     return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
47 }
48
49 off_t zebra_record_ext_tell (void *fh)
50 {
51     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
52     return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
53 }
54
55 off_t zebra_record_int_seek (void *fh, off_t offset)
56 {
57     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
58     return (off_t) (fc->record_int_pos = offset);
59 }
60
61 off_t zebra_record_int_tell (void *fh)
62 {
63     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
64     return (off_t) fc->record_int_pos;
65 }
66
67 int zebra_record_int_read (void *fh, char *buf, size_t count)
68 {
69     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
70     int l = fc->record_int_len - fc->record_int_pos;
71     if (l <= 0)
72         return 0;
73     l = (l < (int) count) ? l : (int) count;
74     memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
75     fc->record_int_pos += l;
76     return l;
77 }
78
79 void zebra_record_int_end (void *fh, off_t off)
80 {
81     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
82     fc->offset_end = off;
83 }
84
85 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score,
86                         zebra_snippets *hit_snippet, ODR stream,
87                         oid_value input_format, Z_RecordComposition *comp,
88                         oid_value *output_format, char **rec_bufp,
89                         int *rec_lenp, char **basenamep,
90                         char **addinfo)
91 {
92     Record rec;
93     char *fname, *file_type, *basename;
94     RecType rt;
95     struct recRetrieveCtrl retrieveCtrl;
96     struct zebra_fetch_control fc;
97     RecordAttr *recordAttr;
98     void *clientData;
99     int raw_mode = 0;
100
101     *basenamep = 0;
102     *addinfo = 0;
103     if (comp && comp->which == Z_RecordComp_simple &&
104         comp->u.simple->which == Z_ElementSetNames_generic && 
105         !strcmp (comp->u.simple->u.generic, "_sysno_"))
106     {
107         char rec_str[60];
108         sprintf(rec_str, ZINT_FORMAT, sysno);
109         *output_format = VAL_SUTRS;
110         *rec_lenp = strlen(rec_str);
111         *rec_bufp = odr_strdup(stream, rec_str);
112         return 0;
113     }
114     rec = rec_get (zh->reg->records, sysno);
115     if (!rec)
116     {
117         yaz_log (YLOG_DEBUG, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
118         *basenamep = 0;
119         return 14;
120     }
121     recordAttr = rec_init_attr (zh->reg->zei, rec);
122
123     file_type = rec->info[recInfo_fileType];
124     fname = rec->info[recInfo_filename];
125     basename = rec->info[recInfo_databaseName];
126     *basenamep = (char *) odr_malloc (stream, strlen(basename)+1);
127     strcpy (*basenamep, basename);
128
129     if (comp && comp->which == Z_RecordComp_simple &&
130         comp->u.simple->which == Z_ElementSetNames_generic && 
131         !strcmp (comp->u.simple->u.generic, "R"))
132     {
133         raw_mode = 1;
134     }
135     if (!(rt = recType_byName (zh->reg->recTypes, zh->res,
136                                file_type, &clientData)))
137     {
138         yaz_log (YLOG_WARN, "Retrieve: Cannot handle type %s",  file_type);
139         return 14;
140     }
141     yaz_log (YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d", sysno,score);
142     retrieveCtrl.fh = &fc;
143     fc.fd = -1;
144     retrieveCtrl.fname = fname;
145     if (rec->size[recInfo_storeData] > 0)
146     {
147         retrieveCtrl.readf = zebra_record_int_read;
148         retrieveCtrl.seekf = zebra_record_int_seek;
149         retrieveCtrl.tellf = zebra_record_int_tell;
150         fc.record_int_len = rec->size[recInfo_storeData];
151         fc.record_int_buf = rec->info[recInfo_storeData];
152         fc.record_int_pos = 0;
153         yaz_log (YLOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
154         if (raw_mode)
155         {
156             *output_format = VAL_SUTRS;
157             *rec_lenp = rec->size[recInfo_storeData];
158             *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
159             memcpy(*rec_bufp, rec->info[recInfo_storeData], *rec_lenp);
160             rec_rm (&rec);
161             return 0;
162         }
163     }
164     else
165     {
166         char full_rep[1024];
167
168         if (zh->path_reg && !yaz_is_abspath (fname))
169         {
170             strcpy (full_rep, zh->path_reg);
171             strcat (full_rep, "/");
172             strcat (full_rep, fname);
173         }
174         else
175             strcpy (full_rep, fname);
176
177         if ((fc.fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
178         {
179             yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
180                   full_rep);
181             rec_rm (&rec);
182             return 14;
183         }
184         fc.record_offset = recordAttr->recordOffset;
185
186         retrieveCtrl.readf = zebra_record_ext_read;
187         retrieveCtrl.seekf = zebra_record_ext_seek;
188         retrieveCtrl.tellf = zebra_record_ext_tell;
189
190         zebra_record_ext_seek (retrieveCtrl.fh, 0);
191         if (raw_mode)
192         {
193             *output_format = VAL_SUTRS;
194             *rec_lenp = recordAttr->recordSize;
195             *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
196             zebra_record_ext_read(&fc, *rec_bufp, *rec_lenp);
197             rec_rm (&rec);
198             close (fc.fd);
199             return 0;
200         }
201     }
202     retrieveCtrl.localno = sysno;
203     retrieveCtrl.score = score;
204     retrieveCtrl.recordSize = recordAttr->recordSize;
205     retrieveCtrl.odr = stream;
206     retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
207     retrieveCtrl.comp = comp;
208     retrieveCtrl.encoding = zh->record_encoding;
209     retrieveCtrl.diagnostic = 0;
210     retrieveCtrl.addinfo = 0;
211     retrieveCtrl.dh = zh->reg->dh;
212     retrieveCtrl.res = zh->res;
213     retrieveCtrl.rec_buf = 0;
214     retrieveCtrl.rec_len = -1;
215     retrieveCtrl.hit_snippet = hit_snippet;
216     retrieveCtrl.doc_snippet = zebra_snippets_create();
217     
218     if (1)
219     {
220         /* snippets code */
221         struct recKeys reckeys;
222         zebra_snippets *snippet;
223         reckeys.buf = rec->info[recInfo_delKeys];
224         reckeys.buf_used = rec->size[recInfo_delKeys];
225
226         zebra_snippets_rec_keys(zh, &reckeys, retrieveCtrl.doc_snippet);
227
228
229         yaz_log(YLOG_LOG, "DOC SNIPPET:");
230         zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
231         yaz_log(YLOG_LOG, "HIT SNIPPET:");
232         zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
233
234         snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
235                                         retrieveCtrl.hit_snippet,
236                                         10);
237         
238         yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
239         zebra_snippets_log(snippet, YLOG_LOG);
240
241         (*rt->retrieve)(clientData, &retrieveCtrl);
242
243         zebra_snippets_destroy(snippet);
244     }
245     else
246     {
247         (*rt->retrieve)(clientData, &retrieveCtrl);
248     }
249
250     zebra_snippets_destroy(retrieveCtrl.doc_snippet);
251
252     *output_format = retrieveCtrl.output_format;
253     *rec_bufp = (char *) retrieveCtrl.rec_buf;
254     *rec_lenp = retrieveCtrl.rec_len;
255     if (fc.fd != -1)
256         close (fc.fd);
257     rec_rm (&rec);
258
259     *addinfo = retrieveCtrl.addinfo;
260     return retrieveCtrl.diagnostic;
261 }