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