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