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