Scan now returns displayTerm which is extract from original record.
[idzebra-moved-to-github.git] / index / rpnscan.c
1 /* $Id: rpnscan.c,v 1.12 2007-08-21 13:27:04 adam Exp $
2    Copyright (C) 1995-2007
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 #ifdef WIN32
26 #include <io.h>
27 #endif
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <ctype.h>
32
33 #include <yaz/diagbib1.h>
34 #include "index.h"
35 #include <zebra_xpath.h>
36 #include <yaz/wrbuf.h>
37 #include <attrfind.h>
38 #include <charmap.h>
39 #include <rset.h>
40 #include <yaz/oid_db.h>
41
42 #define RPN_MAX_ORDS 32
43
44 int log_scan = YLOG_LOG;
45
46 /* convert APT SCAN term to internal cmap */
47 static ZEBRA_RES trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
48                                  char *termz, int reg_type)
49 {
50     char termz0[IT_MAX_WORD];
51
52     if (zapt_term_to_utf8(zh, zapt, termz0) == ZEBRA_FAIL)
53         return ZEBRA_FAIL;    /* error */
54     else
55     {
56         const char **map;
57         const char *cp = (const char *) termz0;
58         const char *cp_end = cp + strlen(cp);
59         const char *src;
60         int i = 0;
61         const char *space_map = NULL;
62         int len;
63             
64         while ((len = (cp_end - cp)) > 0)
65         {
66             map = zebra_maps_input(zh->reg->zebra_maps, reg_type, &cp, len, 0);
67             if (**map == *CHR_SPACE)
68                 space_map = *map;
69             else
70             {
71                 if (i && space_map)
72                     for (src = space_map; *src; src++)
73                         termz[i++] = *src;
74                 space_map = NULL;
75                 for (src = *map; *src; src++)
76                     termz[i++] = *src;
77             }
78         }
79         termz[i] = '\0';
80     }
81     return ZEBRA_OK;
82 }
83
84 static void count_set(ZebraHandle zh, RSET rset, zint *count)
85 {
86     zint psysno = 0;
87     struct it_key key;
88     RSFD rfd;
89
90     yaz_log(YLOG_DEBUG, "count_set");
91
92     rset->hits_limit = zh->approx_limit;
93
94     *count = 0;
95     rfd = rset_open(rset, RSETF_READ);
96     while (rset_read(rfd, &key,0 /* never mind terms */))
97     {
98         if (key.mem[0] != psysno)
99         {
100             psysno = key.mem[0];
101             if (rfd->counted_items >= rset->hits_limit)
102                 break;
103         }
104     }
105     rset_close (rfd);
106     *count = rset->hits_count;
107 }
108
109 static void get_first_snippet_from_rset(RSET rset, zebra_snippets *snippets, 
110                                         zint *sysno)
111 {
112     struct it_key key;
113     RSFD rfd;
114     TERMID termid;
115
116     yaz_log(YLOG_DEBUG, "get_first_snippet_from_rset");
117
118     rfd = rset_open(rset, RSETF_READ);
119     *sysno = 0;
120     while (rset_read(rfd, &key, &termid))
121     {
122         if (key.mem[0] != *sysno)
123         {
124             if (*sysno)
125                 break;
126             *sysno = key.mem[0];
127         }
128         if (termid)
129         {
130             struct ord_list *ol;
131             for (ol = termid->ol; ol; ol = ol->next)
132             {
133                 zebra_snippets_append(snippets, key.mem[key.len-1], 0,
134                                       ol->ord, termid->name);
135             }
136         }
137     }
138     rset_close (rfd);
139 }
140
141 struct scan2_info_entry {
142     WRBUF term;
143     char prefix[20];
144     ISAM_P isam_p;
145     int pos_to_save;
146     int ord;
147 };
148
149 static int scan_handle2(char *name, const char *info, int pos, void *client)
150 {
151     int len_prefix;
152     struct scan2_info_entry *scan_info = (struct scan2_info_entry *) client;
153
154     if (scan_info->pos_to_save != pos)
155         return 0;
156
157     len_prefix = strlen(scan_info->prefix);
158     if (memcmp (name, scan_info->prefix, len_prefix))
159         return 1;
160
161     /* skip special terms such as first-in-field specials */
162     if (name[len_prefix] < CHR_BASE_CHAR)
163         return 1;
164
165     wrbuf_rewind(scan_info->term);
166     wrbuf_puts(scan_info->term, name+len_prefix);
167
168     assert (*info == sizeof(ISAM_P));
169     memcpy (&scan_info->isam_p, info+1, sizeof(ISAM_P));
170     return 0;
171 }
172
173
174 static int scan_save_set(ZebraHandle zh, ODR stream, NMEM nmem,
175                          struct rset_key_control *kc,
176                          Z_AttributesPlusTerm *zapt,
177                          RSET limit_set,
178                          const char *term, 
179                          int index_type,
180                          struct scan2_info_entry *ar, int ord_no,
181                          ZebraScanEntry *glist, int pos)
182 {
183     int i;
184     RSET rset = 0;
185     for (i = 0; i < ord_no; i++)
186     {
187         if (ar[i].isam_p && strcmp(wrbuf_cstr(ar[i].term), term) == 0)
188         {
189             struct ord_list *ol = ord_list_create(nmem);
190             RSET rset_t;
191
192             ol = ord_list_append(nmem, ol, ar[i].ord);
193
194             assert(ol);
195             rset_t = rset_trunc(
196                     zh, &ar[i].isam_p, 1,
197                     wrbuf_buf(ar[i].term), wrbuf_len(ar[i].term),
198                     NULL, 1, zapt->term->which, nmem, 
199                     kc, kc->scope, ol, index_type, 
200                     0 /* hits_limit */,
201                     0 /* term_ref_id_str */);
202             if (!rset)
203                 rset = rset_t;
204             else
205             {
206                 RSET rsets[2];
207                 
208                 rsets[0] = rset;
209                 rsets[1] = rset_t;
210                 rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */,
211                                       2, rsets);
212             }
213             ar[i].isam_p = 0;
214         }
215     }
216     if (rset)
217     {
218         zint count;
219         /* merge with limit_set if given */
220         if (limit_set)
221         {
222             RSET rsets[2];
223             rsets[0] = rset;
224             rsets[1] = rset_dup(limit_set);
225             
226             rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
227         }
228         /* count it */
229         count_set(zh, rset, &count);
230
231         if (pos != -1)
232         {
233             zint sysno;
234             int code = -1;
235             zebra_snippets *rec_snippets = zebra_snippets_create();
236             zebra_snippets *hit_snippets = zebra_snippets_create();
237
238             glist[pos].term = 0;
239             glist[pos].display_term = 0;
240             
241             get_first_snippet_from_rset(rset, hit_snippets, &sysno);
242             if (sysno)
243                 code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
244          
245             if (code == 0)
246             {
247                 const struct zebra_snippet_word *w = 
248                     zebra_snippets_lookup(rec_snippets, hit_snippets);
249                 if (w)
250                 {
251                     glist[pos].display_term = odr_strdup(stream, w->term);
252                 }
253             }
254             if (!glist[pos].term)
255                 zebra_term_untrans_iconv(zh, stream->mem, index_type,
256                                          &glist[pos].term, term);
257             glist[pos].occurrences = count;
258             zebra_snippets_destroy(rec_snippets);
259             zebra_snippets_destroy(hit_snippets);
260         }
261         rset_delete(rset);
262         if (count > 0)
263             return 1;
264         else
265             return 0;
266     }
267     return 0;
268 }
269         
270 static ZEBRA_RES rpn_scan_ver2(ZebraHandle zh, ODR stream, NMEM nmem,
271                                struct rset_key_control *kc,
272                                Z_AttributesPlusTerm *zapt,
273                                int *position, int *num_entries, 
274                                ZebraScanEntry **list,
275                                int *is_partial, RSET limit_set,
276                                int index_type, int ord_no, int *ords)
277 {
278     struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no);
279     struct rpn_char_map_info rcmi;
280     int i, dif;
281     int after_pos;
282     int pos = 0;
283
284     ZebraScanEntry *glist = (ZebraScanEntry *)
285         odr_malloc(stream, *num_entries * sizeof(*glist));
286
287     *is_partial = 0;
288     if (*position > *num_entries+1)
289     {
290         *is_partial = 1;
291         *position = 1;
292         *num_entries = 0;
293         return ZEBRA_OK;
294     }
295     rpn_char_map_prepare (zh->reg, index_type, &rcmi);
296
297     for (i = 0; i < ord_no; i++)
298         ar[i].term = wrbuf_alloc();
299
300     for (i = 0; i < ord_no; i++)
301     {
302         char termz[IT_MAX_WORD+20];
303         int prefix_len = 0;
304         
305         prefix_len = key_SU_encode (ords[i], termz);
306         termz[prefix_len] = 0;
307         strcpy(ar[i].prefix, termz);
308         
309         if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) == 
310             ZEBRA_FAIL)
311         {
312             for (i = 0; i < ord_no; i++)
313                 wrbuf_destroy(ar[i].term);
314             return ZEBRA_FAIL;
315         }
316         wrbuf_rewind(ar[i].term);
317         wrbuf_puts(ar[i].term, termz + prefix_len);
318         ar[i].isam_p = 0;
319         ar[i].ord = ords[i];
320     }
321     /** deal with terms before position .. */
322     /* the glist index starts at zero (unlike scan positions */
323     for (pos = *position-2; pos >= 0; )
324     {
325         const char *hi = 0;
326
327         /* scan on all maximum terms */
328         for (i = 0; i < ord_no; i++)
329         {
330             if (ar[i].isam_p == 0)
331             {
332                 char termz[IT_MAX_WORD+20];
333                 int before = 1;
334                 int after = 0;
335
336                 ar[i].pos_to_save = -1;
337
338                 strcpy(termz, ar[i].prefix);
339                 strcat(termz, wrbuf_cstr(ar[i].term));
340                 dict_scan(zh->reg->dict, termz, &before, &after,
341                           ar+i, scan_handle2);
342             }
343         }
344         /* get maximum after scan */
345         for (i = 0; i < ord_no; i++)
346         {
347             if (ar[i].isam_p 
348                 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
349                 hi = wrbuf_cstr(ar[i].term);
350         }
351         if (!hi)
352             break;
353         if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, hi,
354                           index_type, ar, ord_no, glist,
355                           (pos >= 0 && pos < *num_entries) ? pos : -1))
356             --pos;
357     }
358     /* see if we got all terms before.. */
359     dif = 1 + pos;
360     if (dif > 0)
361     {
362         /* did not get all terms; adjust the real position and reduce
363            number of entries */
364         yaz_log(YLOG_LOG, "before terms dif=%d", dif);
365         glist = glist + dif;
366         *num_entries -= dif;
367         *position -= dif;
368         *is_partial = 1;
369     }
370     for (i = 0; i < ord_no; i++)
371     {
372         char termz[IT_MAX_WORD+20];
373         int prefix_len = 0;
374         
375         prefix_len = key_SU_encode (ords[i], termz);
376         termz[prefix_len] = 0;
377         strcpy(ar[i].prefix, termz);
378         
379         if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) == 
380             ZEBRA_FAIL)
381             return ZEBRA_FAIL;
382         wrbuf_rewind(ar[i].term);
383         wrbuf_puts(ar[i].term, termz + prefix_len);
384         ar[i].isam_p = 0;
385         ar[i].ord = ords[i];
386     }
387
388     after_pos = 1;  /* immediate term first.. */
389     for (pos = *position-1; pos < *num_entries; )
390     {
391         const char *lo = 0;
392
393         /* scan on all minimum terms */
394         for (i = 0; i < ord_no; i++)
395         {
396             if (ar[i].isam_p == 0)
397             {
398                 char termz[IT_MAX_WORD+20];
399                 int before = 0;
400                 int after = after_pos;
401
402                 ar[i].pos_to_save = 1;
403
404                 strcpy(termz, ar[i].prefix);
405                 strcat(termz, wrbuf_cstr(ar[i].term));
406                 dict_scan(zh->reg->dict, termz, &before, &after,
407                           ar+i, scan_handle2);
408             }
409         }
410         after_pos = 2;  /* next round we grab following term */
411
412         /* get minimum after scan */
413         for (i = 0; i < ord_no; i++)
414         {
415             if (ar[i].isam_p 
416                 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
417                 lo = wrbuf_cstr(ar[i].term);
418         }
419         if (!lo)
420             break;
421         if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, lo,
422                           index_type, ar, ord_no, glist,
423                           (pos >= 0 && pos < *num_entries) ? pos : -1))
424             pos++;
425
426     }
427     if (pos != *num_entries)
428     {
429         if (pos >= 0)
430             *num_entries = pos;
431         else
432             *num_entries = 0;
433         *is_partial = 1;
434     }
435
436     *list = glist;
437
438     for (i = 0; i < ord_no; i++)
439         wrbuf_destroy(ar[i].term);
440
441     return ZEBRA_OK;
442 }
443
444 struct scan1_info_entry {
445     char *term;
446     ISAM_P isam_p;
447 };
448
449 struct scan_info {
450     struct scan1_info_entry *list;
451     ODR odr;
452     int before, after;
453     char prefix[20];
454 };
455
456 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
457                    const Odr_oid *attributeset,
458                    int num_bases, char **basenames,
459                    int *position, int *num_entries, ZebraScanEntry **list,
460                    int *is_partial, RSET limit_set)
461 {
462     int base_no;
463     int ords[RPN_MAX_ORDS], ord_no = 0;
464
465     unsigned index_type;
466     char *search_type = NULL;
467     char rank_type[128];
468     int complete_flag;
469     int sort_flag;
470     NMEM nmem;
471     ZEBRA_RES res;
472     struct rset_key_control *kc = 0;
473
474     *list = 0;
475     *is_partial = 0;
476
477     if (!attributeset)
478         attributeset = yaz_oid_attset_bib_1;
479
480     if (!limit_set) /* no limit set given already */
481     {
482         /* see if there is a @attr 8=set */
483         AttrType termset;
484         int termset_value_numeric;
485         const char *termset_value_string;
486         attr_init_APT(&termset, zapt, 8);
487         termset_value_numeric =
488             attr_find_ex(&termset, NULL, &termset_value_string);
489         if (termset_value_numeric != -1)
490         {
491             char resname[32];
492             const char *termset_name = 0;
493             
494             if (termset_value_numeric != -2)
495             {
496                 sprintf(resname, "%d", termset_value_numeric);
497                 termset_name = resname;
498             }
499             else
500                 termset_name = termset_value_string;
501             
502             limit_set = resultSetRef (zh, termset_name);
503
504             if (!limit_set)
505             {
506                 zebra_setError(zh, 
507                                YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
508                                termset_name);
509                 return ZEBRA_FAIL;
510             }
511         }
512     }
513         
514     yaz_log(YLOG_DEBUG, "position = %d, num = %d",
515             *position, *num_entries);
516         
517     if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
518                         rank_type, &complete_flag, &sort_flag))
519     {
520         *num_entries = 0;
521         zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
522         return ZEBRA_FAIL;
523     }
524     if (num_bases > RPN_MAX_ORDS)
525     {
526         zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
527         return ZEBRA_FAIL;
528     }
529
530     for (base_no = 0; base_no < num_bases; base_no++)
531     {
532         int ord;
533
534         if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
535         {
536             zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
537                            basenames[base_no]);
538             *num_entries = 0;
539             return ZEBRA_FAIL;
540         }
541         if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord) 
542             != ZEBRA_OK)
543             continue;
544         ords[ord_no++] = ord;
545     }
546     if (ord_no == 0)
547     {
548         *num_entries = 0; /* zebra_apt_get_ord should set error reason */
549         return ZEBRA_FAIL;
550     }
551     if (*num_entries < 1)
552     {
553         *num_entries = 0;
554         return ZEBRA_OK;
555     }
556     nmem = nmem_create();
557     kc = zebra_key_control_create(zh);
558
559     res = rpn_scan_ver2(zh, stream, nmem, kc, zapt, position, num_entries,
560                         list,
561                         is_partial, limit_set, index_type, ord_no, ords);
562     nmem_destroy(nmem);
563     (*kc->dec)(kc);
564     return res;
565 }
566
567 /*
568  * Local variables:
569  * c-basic-offset: 4
570  * indent-tabs-mode: nil
571  * End:
572  * vim: shiftwidth=4 tabstop=8 expandtab
573  */
574