Use size of rec->info stuff in meta record fetch. Failing that will
[idzebra-moved-to-github.git] / index / retrieve.c
1 /* $Id: retrieve.c,v 1.57 2006-11-23 14:01:21 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 <yaz/diagbib1.h>
37 #include <direntz.h>
38
39
40
41
42 static int zebra_create_record_stream(ZebraHandle zh, 
43                                Record *rec,
44                                struct ZebraRecStream *stream){
45
46     RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, *rec);
47
48     if ((*rec)->size[recInfo_storeData] > 0){ 
49         zebra_create_stream_mem(stream, (*rec)->info[recInfo_storeData],
50                                 (*rec)->size[recInfo_storeData]);
51     }
52     else {
53         char full_rep[1024];
54         int fd;
55             
56         if (zh->path_reg && !yaz_is_abspath((*rec)->info[recInfo_filename])){
57             strcpy(full_rep, zh->path_reg);
58             strcat(full_rep, "/");
59             strcat(full_rep, (*rec)->info[recInfo_filename]);
60         }
61         else
62             strcpy(full_rep, (*rec)->info[recInfo_filename]);
63             
64         if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1){
65             yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
66                      full_rep);
67             rec_free(rec);
68             return 14;
69         }
70         zebra_create_stream_fd(stream, fd, recordAttr->recordOffset);
71     }
72     return 0;
73 }
74     
75
76
77 static int parse_zebra_elem(const char *elem,
78                              const char **index, size_t *index_len,
79                              const char **type, size_t *type_len)
80 {
81     *index = 0;
82     *index_len = 0;
83
84     *type = 0;
85     *type_len = 0;
86
87     if (elem && *elem)
88     {
89         char *cp;
90         /* verify that '::' is in the beginning of *elem 
91            and something more follows */
92         if (':' != *elem
93             || !(elem +1) || ':' != *(elem +1)
94             || !(elem +2) || '\0' == *(elem +2))
95             return 0;
96  
97         /* pick out info from string after '::' */
98         elem = elem + 2;
99         cp = strchr(elem, ':');
100
101         if (!cp) /* index, no colon, no type */
102         {
103             *index = elem;
104             *index_len = strlen(elem);
105         }
106         else if (cp[1] == '\0') /* colon, but no following type */
107         {
108             return 0;
109         }
110         else  /* index, colon and type */
111         {
112             *index = elem;
113             *index_len = cp - elem;
114             *type = cp+1;
115             *type_len = strlen(cp+1);
116         }
117     }
118     return 1;
119 }
120
121
122 int zebra_special_index_fetch(ZebraHandle zh, zint sysno, ODR odr,
123                           Record rec,
124                           const char *elemsetname,
125                           oid_value input_format,
126                           oid_value *output_format,
127                           char **rec_bufp, int *rec_lenp)
128 {
129     const char *retrieval_index;
130     size_t retrieval_index_len; 
131     const char *retrieval_type;
132     size_t retrieval_type_len;
133     WRBUF wrbuf = 0;
134     zebra_rec_keys_t keys;
135     
136     /* set output variables before processing possible error states */
137     /* *rec_lenp = 0; */
138
139     /* only accept XML and SUTRS requests */
140     if (input_format != VAL_TEXT_XML
141         && input_format != VAL_SUTRS){
142         yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s", 
143                 elemsetname);
144         *output_format = VAL_NONE;
145         return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
146     }
147
148     if (!parse_zebra_elem(elemsetname,
149                      &retrieval_index, &retrieval_index_len,
150                      &retrieval_type,  &retrieval_type_len))
151         return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
152
153     if (retrieval_type_len != 0 && retrieval_type_len != 1)
154     {
155         return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
156     }
157
158     if (retrieval_index_len)
159     {
160         char retrieval_index_cstr[256];
161
162         if (retrieval_index_len  < sizeof(retrieval_index_cstr) -1)
163         {
164             memcpy(retrieval_index_cstr, retrieval_index, retrieval_index_len);
165             retrieval_index_cstr[retrieval_index_len] = '\0';
166             
167             if (zebraExplain_lookup_attr_str(zh->reg->zei,
168                                              zinfo_index_category_index,
169                                              (retrieval_type_len == 0 ? -1 : 
170                                               retrieval_type[0]),
171                                              retrieval_index_cstr) == -1)
172                 return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
173         }
174     }
175
176     keys = zebra_rec_keys_open();
177     zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
178                            rec->size[recInfo_delKeys], 0);
179
180     wrbuf = wrbuf_alloc();
181     if (zebra_rec_keys_rewind(keys)){
182         size_t slen;
183         const char *str;
184         struct it_key key_in;
185
186         if (input_format == VAL_TEXT_XML){
187             *output_format = VAL_TEXT_XML;
188             /*wrbuf_printf(wrbuf, 
189               "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");*/
190             
191             wrbuf_printf(wrbuf, 
192                          "<record xmlns="
193                          "\"http://www.indexdata.com/zebra/\""
194                          " sysno=\"" ZINT_FORMAT "\""
195                          " set=\"zebra::index%s/\">\n",
196                          sysno, elemsetname);
197         }
198         else if (input_format == VAL_SUTRS)
199             *output_format = VAL_SUTRS;
200
201
202         while(zebra_rec_keys_read(keys, &str, &slen, &key_in)){
203             int i;
204             int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
205             int index_type;
206             const char *db = 0;
207             const char *string_index = 0;
208             size_t string_index_len;
209             char dst_buf[IT_MAX_WORD];
210             
211             zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db,
212                                     &string_index);
213             string_index_len = strlen(string_index);
214
215             /* process only if index is not defined, 
216                or if defined and matching */
217             if (retrieval_index == 0 
218                 || (string_index_len == retrieval_index_len 
219                     && !memcmp(string_index, retrieval_index,
220                                string_index_len))){
221                
222                 /* process only if type is not defined, or is matching */
223                 if (retrieval_type == 0 
224                     || (retrieval_type_len == 1 
225                         && retrieval_type[0] == index_type)){
226                     
227
228                     zebra_term_untrans(zh, index_type, dst_buf, str);
229                     if (strlen(dst_buf)){
230
231                         if (input_format == VAL_TEXT_XML){
232                             wrbuf_printf(wrbuf, "  <index name=\"%s\"", 
233                                          string_index);
234                             
235                             wrbuf_printf(wrbuf, " type=\"%c\"", index_type);
236                             
237                             wrbuf_printf(wrbuf, " seq=\"" ZINT_FORMAT "\">", 
238                                          key_in.mem[key_in.len -1]);
239                         
240                             wrbuf_xmlputs(wrbuf, dst_buf);
241                             wrbuf_printf(wrbuf, "</index>\n");
242                         }
243                         else if (input_format == VAL_SUTRS){
244                             wrbuf_printf(wrbuf, "%s ", string_index);
245                             
246                             wrbuf_printf(wrbuf, "%c", index_type);
247                             
248                             for (i = 1; i < key_in.len; i++)
249                                 wrbuf_printf(wrbuf, " " ZINT_FORMAT, 
250                                              key_in.mem[i]);
251
252                         /* zebra_term_untrans(zh, index_type, dst_buf, str); */
253                             wrbuf_printf(wrbuf, " %s", dst_buf);
254                         
255                             wrbuf_printf(wrbuf, "\n");
256                         }
257                     }
258                     
259                 }
260             }
261         }
262         if (input_format == VAL_TEXT_XML)
263             wrbuf_printf(wrbuf, "</record>\n");
264      }
265     *rec_lenp = wrbuf_len(wrbuf);
266     *rec_bufp = odr_malloc(odr, *rec_lenp);
267     memcpy(*rec_bufp, wrbuf_buf(wrbuf), *rec_lenp);
268     wrbuf_free(wrbuf, 1);
269     zebra_rec_keys_close(keys);
270     return 0;
271 }
272
273
274
275 int zebra_special_fetch(ZebraHandle zh, zint sysno, int score, ODR odr,
276                            const char *elemsetname,
277                            oid_value input_format,
278                            oid_value *output_format,
279                            char **rec_bufp, int *rec_lenp)
280 {
281     Record rec;
282     
283     /* set output variables before processing possible error states */
284     /* *rec_lenp = 0; */
285
286     /* only accept XML and SUTRS requests */
287     if (input_format != VAL_TEXT_XML
288         && input_format != VAL_SUTRS){
289         yaz_log(YLOG_WARN, "unsupported format for element set zebra::%s", 
290                 elemsetname);
291         return YAZ_BIB1_NO_SYNTAXES_AVAILABLE_FOR_THIS_REQUEST;
292     }
293     
294     /* processing zebra::meta::sysno elemset without fetching binary data */
295     if (elemsetname  && 0 == strcmp(elemsetname, "meta::sysno"))
296     {
297         char rec_str[128];
298         if (input_format == VAL_SUTRS){
299             sprintf(rec_str, ZINT_FORMAT, sysno);
300             *output_format = VAL_SUTRS;
301         } 
302         else if (input_format == VAL_TEXT_XML){
303             sprintf(rec_str, "<record xmlns="
304                     "\"http://www.indexdata.com/zebra/\""
305                              " sysno=\"" ZINT_FORMAT "\""
306                              " set=\"zebra::%s\"/>\n",
307                     sysno, elemsetname);
308             *output_format = VAL_TEXT_XML;
309         }
310         *rec_lenp = strlen(rec_str);
311         if (*rec_lenp){
312             *rec_bufp = odr_strdup(odr, rec_str);
313             return 0;
314         } else {
315             return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
316         }
317     }
318
319     /* fetching binary record up for all other display elementsets */
320     rec = rec_get(zh->reg->records, sysno);
321     if (!rec){
322         yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
323         return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
324     }
325
326     /* processing special elementsetnames zebra::meta:: */
327     if (elemsetname && 0 == strcmp(elemsetname, "meta")){
328         int ret = 0;
329         char rec_str[1024];
330         RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec); 
331
332         if (input_format == VAL_TEXT_XML){
333             *output_format = VAL_TEXT_XML;
334
335              sprintf(rec_str, 
336                      "<record xmlns="
337                      "\"http://www.indexdata.com/zebra/\""
338                      " sysno=\"" ZINT_FORMAT "\""
339                      " base=\"%.*s\""
340                      " file=\"%.*s\""
341                      " type=\"%.*s\""
342                      " score=\"%i\""
343                      " rank=\"" ZINT_FORMAT "\""
344                      " size=\"%i\""
345                      " set=\"zebra::%s/\">\n",
346                      sysno, 
347                      rec->size[recInfo_databaseName], rec->info[recInfo_databaseName],
348                      rec->size[recInfo_filename], rec->info[recInfo_filename],
349                      rec->size[recInfo_fileType], rec->info[recInfo_fileType],
350                      score,
351                      recordAttr->staticrank,
352                      recordAttr->recordSize,
353                      elemsetname);
354         }
355         else if (input_format == VAL_SUTRS){
356             *output_format = VAL_SUTRS;
357              sprintf(rec_str, 
358                      "sysno " ZINT_FORMAT "\n"
359                      "base %.*s\n"
360                      "file %.*s\n"
361                      "type %.*s\n"
362                      "score %i\n"
363                      "rank " ZINT_FORMAT "\n"
364                      "size %i\n"
365                      "set zebra::%s\n",
366                      sysno, 
367                      rec->size[recInfo_databaseName], rec->info[recInfo_databaseName],
368                      rec->size[recInfo_filename], rec->info[recInfo_filename],
369                      rec->size[recInfo_fileType], rec->info[recInfo_fileType],
370                      score,
371                      recordAttr->staticrank,
372                      recordAttr->recordSize,
373                      elemsetname);
374         }
375         
376         
377         *rec_lenp = strlen(rec_str);
378         if (*rec_lenp){
379             *rec_bufp = odr_strdup(odr, rec_str);
380             ret = 0;
381         } else {
382             ret = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
383         }
384
385         rec_free(&rec);
386         return ret;
387     }
388
389     /* processing special elementsetnames zebra::index:: */
390     if (elemsetname && 0 == strncmp(elemsetname, "index", 5)){
391         
392         int ret = zebra_special_index_fetch(zh, sysno, odr, rec,
393                                             elemsetname + 5,
394                                             input_format, output_format,
395                                             rec_bufp, rec_lenp);
396         
397         rec_free(&rec);
398         return ret;
399     }
400
401     /* processing special elementsetnames zebra::data */    
402     if (elemsetname && 0 == strcmp(elemsetname, "data")){
403         struct ZebraRecStream stream;
404         RecordAttr *recordAttr = rec_init_attr(zh->reg->zei, rec); 
405         zebra_create_record_stream(zh, &rec, &stream);
406         *output_format = input_format;
407         *rec_lenp = recordAttr->recordSize;
408         *rec_bufp = (char *) odr_malloc(odr, *rec_lenp);
409         stream.readf(&stream, *rec_bufp, *rec_lenp);
410         stream.destroy(&stream);
411         rec_free(&rec);
412         return 0;
413     }
414
415     if (rec)
416         rec_free(&rec);
417     return YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
418 }
419
420                           
421 int zebra_record_fetch(ZebraHandle zh, zint sysno, int score,
422                        zebra_snippets *hit_snippet, ODR odr,
423                        oid_value input_format, Z_RecordComposition *comp,
424                        oid_value *output_format,
425                        char **rec_bufp, int *rec_lenp, char **basenamep,
426                        char **addinfo)
427 {
428     Record rec;
429     char *fname, *file_type, *basename;
430     const char *elemsetname;
431     struct ZebraRecStream stream;
432     RecordAttr *recordAttr;
433     void *clientData;
434     int return_code = 0;
435
436     *basenamep = 0;
437     *addinfo = 0;
438     elemsetname = yaz_get_esn(comp);
439
440     /* processing zebra special elementset names of form 'zebra:: */
441     if (elemsetname && 0 == strncmp(elemsetname, "zebra::", 7))
442         return  zebra_special_fetch(zh, sysno, score, odr,
443                                     elemsetname + 7,
444                                     input_format, output_format,
445                                     rec_bufp, rec_lenp);
446
447
448     /* processing all other element set names */
449     rec = rec_get(zh->reg->records, sysno);
450     if (!rec)
451     {
452         yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
453         *basenamep = 0;
454         return YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
455     }
456
457
458     recordAttr = rec_init_attr(zh->reg->zei, rec);
459
460     file_type = rec->info[recInfo_fileType];
461     fname = rec->info[recInfo_filename];
462     basename = rec->info[recInfo_databaseName];
463     *basenamep = (char *) odr_malloc (odr, strlen(basename)+1);
464     strcpy (*basenamep, basename);
465
466     yaz_log(YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d",
467             sysno, score);
468
469     zebra_create_record_stream(zh, &rec, &stream);
470     
471     {
472         /* snippets code */
473         zebra_snippets *snippet;
474         zebra_rec_keys_t reckeys = zebra_rec_keys_open();
475         RecType rt;
476         struct recRetrieveCtrl retrieveCtrl;
477
478         retrieveCtrl.stream = &stream;
479         retrieveCtrl.fname = fname;
480         retrieveCtrl.localno = sysno;
481         retrieveCtrl.staticrank = recordAttr->staticrank;
482         retrieveCtrl.score = score;
483         retrieveCtrl.recordSize = recordAttr->recordSize;
484         retrieveCtrl.odr = odr;
485         retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
486         retrieveCtrl.comp = comp;
487         retrieveCtrl.encoding = zh->record_encoding;
488         retrieveCtrl.diagnostic = 0;
489         retrieveCtrl.addinfo = 0;
490         retrieveCtrl.dh = zh->reg->dh;
491         retrieveCtrl.res = zh->res;
492         retrieveCtrl.rec_buf = 0;
493         retrieveCtrl.rec_len = -1;
494         retrieveCtrl.hit_snippet = hit_snippet;
495         retrieveCtrl.doc_snippet = zebra_snippets_create();
496
497         zebra_rec_keys_set_buf(reckeys,
498                                rec->info[recInfo_delKeys],
499                                rec->size[recInfo_delKeys], 
500                                0);
501         zebra_rec_keys_to_snippets(zh, reckeys, retrieveCtrl.doc_snippet);
502         zebra_rec_keys_close(reckeys);
503
504 #if 0
505         /* for debugging purposes */
506         yaz_log(YLOG_LOG, "DOC SNIPPET:");
507         zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
508         yaz_log(YLOG_LOG, "HIT SNIPPET:");
509         zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
510 #endif
511         snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
512                                         retrieveCtrl.hit_snippet,
513                                         10);
514 #if 0
515         /* for debugging purposes */
516         yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
517         zebra_snippets_log(snippet, YLOG_LOG);
518 #endif
519
520         if (!(rt = recType_byName(zh->reg->recTypes, zh->res,
521                                   file_type, &clientData)))
522         {
523             return_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
524         }
525         else
526         {
527             (*rt->retrieve)(clientData, &retrieveCtrl);
528             return_code = retrieveCtrl.diagnostic;
529
530             *output_format = retrieveCtrl.output_format;
531             *rec_bufp = (char *) retrieveCtrl.rec_buf;
532             *rec_lenp = retrieveCtrl.rec_len;
533             *addinfo = retrieveCtrl.addinfo;
534         }
535
536         zebra_snippets_destroy(snippet);
537         zebra_snippets_destroy(retrieveCtrl.doc_snippet);
538      }
539
540     stream.destroy(&stream);
541     rec_free(&rec);
542
543     return return_code;
544 }
545
546 /*
547  * Local variables:
548  * c-basic-offset: 4
549  * indent-tabs-mode: nil
550  * End:
551  * vim: shiftwidth=4 tabstop=8 expandtab
552  */
553