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