1327f5f8f0ae357e120a50e4a9c9dce41811ad95
[idzebra-moved-to-github.git] / index / rpnscan.c
1 /* $Id: rpnscan.c,v 1.16 2007-10-30 19:17:15 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, zebra_map_t zm)
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(zm, &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                          const char *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                                const char *index_type,
282                                int ord_no, int *ords)
283 {
284     struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no);
285     struct rpn_char_map_info rcmi;
286     zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, *index_type);
287     int i, dif;
288     int after_pos;
289     int pos = 0;
290
291     ZebraScanEntry *glist = (ZebraScanEntry *)
292         odr_malloc(stream, *num_entries * sizeof(*glist));
293
294     *is_partial = 0;
295     if (*position > *num_entries+1)
296     {
297         *is_partial = 1;
298         *position = 1;
299         *num_entries = 0;
300         return ZEBRA_OK;
301     }
302     rpn_char_map_prepare(zh->reg, zm, &rcmi);
303
304     for (i = 0; i < ord_no; i++)
305         ar[i].term = wrbuf_alloc();
306
307     for (i = 0; i < ord_no; i++)
308     {
309         char termz[IT_MAX_WORD+20];
310         int prefix_len = 0;
311         
312         prefix_len = key_SU_encode(ords[i], termz);
313         termz[prefix_len] = 0;
314         strcpy(ar[i].prefix, termz);
315         
316         if (trans_scan_term(zh, zapt, termz+prefix_len, zm) == 
317             ZEBRA_FAIL)
318         {
319             for (i = 0; i < ord_no; i++)
320                 wrbuf_destroy(ar[i].term);
321             return ZEBRA_FAIL;
322         }
323         wrbuf_rewind(ar[i].term);
324         wrbuf_puts(ar[i].term, termz + prefix_len);
325         ar[i].isam_p = 0;
326         ar[i].ord = ords[i];
327     }
328     /** deal with terms before position .. */
329     /* the glist index starts at zero (unlike scan positions */
330     for (pos = *position-2; pos >= 0; )
331     {
332         const char *hi = 0;
333
334         /* scan on all maximum terms */
335         for (i = 0; i < ord_no; i++)
336         {
337             if (ar[i].isam_p == 0)
338             {
339                 char termz[IT_MAX_WORD+20];
340                 int before = 1;
341                 int after = 0;
342
343                 ar[i].pos_to_save = -1;
344
345                 strcpy(termz, ar[i].prefix);
346                 strcat(termz, wrbuf_cstr(ar[i].term));
347                 dict_scan(zh->reg->dict, termz, &before, &after,
348                           ar+i, scan_handle2);
349             }
350         }
351         /* get maximum after scan */
352         for (i = 0; i < ord_no; i++)
353         {
354             if (ar[i].isam_p 
355                 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
356                 hi = wrbuf_cstr(ar[i].term);
357         }
358         if (!hi)
359             break;
360         if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, hi,
361                           index_type, ar, ord_no, glist,
362                           (pos >= 0 && pos < *num_entries) ? pos : -1))
363             --pos;
364     }
365     /* see if we got all terms before.. */
366     dif = 1 + pos;
367     if (dif > 0)
368     {
369         /* did not get all terms; adjust the real position and reduce
370            number of entries */
371         yaz_log(YLOG_LOG, "before terms dif=%d", dif);
372         glist = glist + dif;
373         *num_entries -= dif;
374         *position -= dif;
375         *is_partial = 1;
376     }
377     for (i = 0; i < ord_no; i++)
378     {
379         char termz[IT_MAX_WORD+20];
380         int prefix_len = 0;
381         
382         prefix_len = key_SU_encode(ords[i], termz);
383         termz[prefix_len] = 0;
384         strcpy(ar[i].prefix, termz);
385         
386         if (trans_scan_term(zh, zapt, termz+prefix_len, zm) == 
387             ZEBRA_FAIL)
388             return ZEBRA_FAIL;
389         wrbuf_rewind(ar[i].term);
390         wrbuf_puts(ar[i].term, termz + prefix_len);
391         ar[i].isam_p = 0;
392         ar[i].ord = ords[i];
393     }
394
395     after_pos = 1;  /* immediate term first.. */
396     for (pos = *position-1; pos < *num_entries; )
397     {
398         const char *lo = 0;
399
400         /* scan on all minimum terms */
401         for (i = 0; i < ord_no; i++)
402         {
403             if (ar[i].isam_p == 0)
404             {
405                 char termz[IT_MAX_WORD+20];
406                 int before = 0;
407                 int after = after_pos;
408
409                 ar[i].pos_to_save = 1;
410
411                 strcpy(termz, ar[i].prefix);
412                 strcat(termz, wrbuf_cstr(ar[i].term));
413                 dict_scan(zh->reg->dict, termz, &before, &after,
414                           ar+i, scan_handle2);
415             }
416         }
417         after_pos = 2;  /* next round we grab following term */
418
419         /* get minimum after scan */
420         for (i = 0; i < ord_no; i++)
421         {
422             if (ar[i].isam_p 
423                 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
424                 lo = wrbuf_cstr(ar[i].term);
425         }
426         if (!lo)
427             break;
428         if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, lo,
429                           index_type, ar, ord_no, glist,
430                           (pos >= 0 && pos < *num_entries) ? pos : -1))
431             pos++;
432
433     }
434     if (pos != *num_entries)
435     {
436         if (pos >= 0)
437             *num_entries = pos;
438         else
439             *num_entries = 0;
440         *is_partial = 1;
441     }
442
443     *list = glist;
444
445     for (i = 0; i < ord_no; i++)
446         wrbuf_destroy(ar[i].term);
447
448     return ZEBRA_OK;
449 }
450
451 struct scan1_info_entry {
452     char *term;
453     ISAM_P isam_p;
454 };
455
456 struct scan_info {
457     struct scan1_info_entry *list;
458     ODR odr;
459     int before, after;
460     char prefix[20];
461 };
462
463 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
464                    const Odr_oid *attributeset,
465                    int num_bases, char **basenames,
466                    int *position, int *num_entries, ZebraScanEntry **list,
467                    int *is_partial, RSET limit_set)
468 {
469     int base_no;
470     int ords[RPN_MAX_ORDS], ord_no = 0;
471
472     const char *index_type;
473     char *search_type = NULL;
474     char rank_type[128];
475     int complete_flag;
476     int sort_flag;
477     NMEM nmem;
478     ZEBRA_RES res;
479     struct rset_key_control *kc = 0;
480
481     *list = 0;
482     *is_partial = 0;
483
484     if (!attributeset)
485         attributeset = yaz_oid_attset_bib_1;
486
487     if (!limit_set) /* no limit set given already */
488     {
489         /* see if there is a @attr 8=set */
490         AttrType termset;
491         int termset_value_numeric;
492         const char *termset_value_string;
493         attr_init_APT(&termset, zapt, 8);
494         termset_value_numeric =
495             attr_find_ex(&termset, NULL, &termset_value_string);
496         if (termset_value_numeric != -1)
497         {
498             char resname[32];
499             const char *termset_name = 0;
500             
501             if (termset_value_numeric != -2)
502             {
503                 sprintf(resname, "%d", termset_value_numeric);
504                 termset_name = resname;
505             }
506             else
507                 termset_name = termset_value_string;
508             
509             limit_set = resultSetRef(zh, termset_name);
510
511             if (!limit_set)
512             {
513                 zebra_setError(zh, 
514                                YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
515                                termset_name);
516                 return ZEBRA_FAIL;
517             }
518         }
519     }
520         
521     yaz_log(YLOG_DEBUG, "position = %d, num = %d",
522             *position, *num_entries);
523         
524     if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
525                         rank_type, &complete_flag, &sort_flag))
526     {
527         *num_entries = 0;
528         zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
529         return ZEBRA_FAIL;
530     }
531     if (num_bases > RPN_MAX_ORDS)
532     {
533         zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
534         return ZEBRA_FAIL;
535     }
536
537     for (base_no = 0; base_no < num_bases; base_no++)
538     {
539         int ord;
540
541         if (zebraExplain_curDatabase(zh->reg->zei, basenames[base_no]))
542         {
543             zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
544                            basenames[base_no]);
545             *num_entries = 0;
546             return ZEBRA_FAIL;
547         }
548         if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord) 
549             != ZEBRA_OK)
550             continue;
551         ords[ord_no++] = ord;
552     }
553     if (ord_no == 0)
554     {
555         *num_entries = 0; /* zebra_apt_get_ord should set error reason */
556         return ZEBRA_FAIL;
557     }
558     if (*num_entries < 1)
559     {
560         *num_entries = 0;
561         return ZEBRA_OK;
562     }
563     nmem = nmem_create();
564     kc = zebra_key_control_create(zh);
565
566     res = rpn_scan_ver2(zh, stream, nmem, kc, zapt, position, num_entries,
567                         list,
568                         is_partial, limit_set, index_type, ord_no, ords);
569     nmem_destroy(nmem);
570     (*kc->dec)(kc);
571     return res;
572 }
573
574 /*
575  * Local variables:
576  * c-basic-offset: 4
577  * indent-tabs-mode: nil
578  * End:
579  * vim: shiftwidth=4 tabstop=8 expandtab
580  */
581