1 /* $Id: rpnscan.c,v 1.19 2007-11-01 14:56:07 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
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
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
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
33 #include <yaz/diagbib1.h>
35 #include <zebra_xpath.h>
36 #include <yaz/wrbuf.h>
40 #include <yaz/oid_db.h>
42 #define RPN_MAX_ORDS 32
44 static int log_scan = YLOG_LOG;
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)
50 char termz0[IT_MAX_WORD];
52 if (zapt_term_to_utf8(zh, zapt, termz0) == ZEBRA_FAIL)
53 return ZEBRA_FAIL; /* error */
57 const char *cp = (const char *) termz0;
58 const char *cp_end = cp + strlen(cp);
61 const char *space_map = NULL;
64 while ((len = (cp_end - cp)) > 0)
66 map = zebra_maps_input(zm, &cp, len, 0);
67 if (**map == *CHR_SPACE)
72 for (src = space_map; *src; src++)
75 for (src = *map; *src; src++)
84 static void count_set(ZebraHandle zh, RSET rset, zint *count, zint approx_limit)
90 yaz_log(YLOG_DEBUG, "count_set");
92 rset->hits_limit = approx_limit;
95 rfd = rset_open(rset, RSETF_READ);
96 while (rset_read(rfd, &key,0 /* never mind terms */))
98 if (key.mem[0] != psysno)
101 if (rfd->counted_items >= rset->hits_limit)
106 *count = rset->hits_count;
109 static void get_first_snippet_from_rset(ZebraHandle zh,
110 RSET rset, zebra_snippets *snippets,
116 size_t sysno_mem_index = 0;
118 if (zh->m_staticrank)
121 yaz_log(YLOG_DEBUG, "get_first_snippet_from_rset");
123 rfd = rset_open(rset, RSETF_READ);
125 while (rset_read(rfd, &key, &termid))
127 if (key.mem[sysno_mem_index] != *sysno)
131 *sysno = key.mem[sysno_mem_index];
136 for (ol = termid->ol; ol; ol = ol->next)
138 zebra_snippets_append(snippets, key.mem[key.len-1], 0,
139 ol->ord, termid->name);
146 struct scan2_info_entry {
154 static int scan_handle2(char *name, const char *info, int pos, void *client)
157 struct scan2_info_entry *scan_info = (struct scan2_info_entry *) client;
159 if (scan_info->pos_to_save != pos)
162 len_prefix = strlen(scan_info->prefix);
163 if (memcmp(name, scan_info->prefix, len_prefix))
166 /* skip special terms such as first-in-field specials */
167 if (name[len_prefix] < CHR_BASE_CHAR)
170 wrbuf_rewind(scan_info->term);
171 wrbuf_puts(scan_info->term, name+len_prefix);
173 assert(*info == sizeof(ISAM_P));
174 memcpy(&scan_info->isam_p, info+1, sizeof(ISAM_P));
179 static int scan_save_set(ZebraHandle zh, ODR stream, NMEM nmem,
180 struct rset_key_control *kc,
181 Z_AttributesPlusTerm *zapt,
184 const char *index_type,
185 struct scan2_info_entry *ar, int ord_no,
186 ZebraScanEntry *glist, int pos)
190 zint approx_limit = zh->approx_limit;
191 AttrType global_hits_limit_attr;
193 attr_init_APT(&global_hits_limit_attr, zapt, 12);
195 l = attr_find(&global_hits_limit_attr, NULL);
199 for (i = 0; i < ord_no; i++)
201 if (ar[i].isam_p && strcmp(wrbuf_cstr(ar[i].term), term) == 0)
203 struct ord_list *ol = ord_list_create(nmem);
206 ol = ord_list_append(nmem, ol, ar[i].ord);
210 zh, &ar[i].isam_p, 1,
211 wrbuf_buf(ar[i].term), wrbuf_len(ar[i].term),
212 NULL, 1, zapt->term->which, nmem,
213 kc, kc->scope, ol, index_type,
214 0 /* hits_limit_value */,
215 0 /* term_ref_id_str */);
224 rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */,
233 /* merge with limit_set if given */
238 rsets[1] = rset_dup(limit_set);
240 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
243 count_set(zh, rset, &count, approx_limit);
249 zebra_snippets *rec_snippets = zebra_snippets_create();
250 zebra_snippets *hit_snippets = zebra_snippets_create();
253 glist[pos].display_term = 0;
255 get_first_snippet_from_rset(zh, rset, hit_snippets, &sysno);
257 code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
261 const struct zebra_snippet_word *w =
262 zebra_snippets_lookup(rec_snippets, hit_snippets);
265 glist[pos].display_term = odr_strdup(stream, w->term);
268 if (!glist[pos].term)
269 zebra_term_untrans_iconv(zh, stream->mem, index_type,
270 &glist[pos].term, term);
271 glist[pos].occurrences = count;
272 zebra_snippets_destroy(rec_snippets);
273 zebra_snippets_destroy(hit_snippets);
284 static ZEBRA_RES rpn_scan_norm(ZebraHandle zh, ODR stream, NMEM nmem,
285 struct rset_key_control *kc,
286 Z_AttributesPlusTerm *zapt,
287 int *position, int *num_entries,
288 ZebraScanEntry **list,
289 int *is_partial, RSET limit_set,
290 const char *index_type,
291 int ord_no, int *ords)
293 struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no);
294 struct rpn_char_map_info rcmi;
295 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
300 ZebraScanEntry *glist = (ZebraScanEntry *)
301 odr_malloc(stream, *num_entries * sizeof(*glist));
304 if (*position > *num_entries+1)
311 rpn_char_map_prepare(zh->reg, zm, &rcmi);
313 for (i = 0; i < ord_no; i++)
314 ar[i].term = wrbuf_alloc();
316 for (i = 0; i < ord_no; i++)
318 char termz[IT_MAX_WORD+20];
321 prefix_len = key_SU_encode(ords[i], termz);
322 termz[prefix_len] = 0;
323 strcpy(ar[i].prefix, termz);
325 if (trans_scan_term(zh, zapt, termz+prefix_len, zm) ==
328 for (i = 0; i < ord_no; i++)
329 wrbuf_destroy(ar[i].term);
332 wrbuf_rewind(ar[i].term);
333 wrbuf_puts(ar[i].term, termz + prefix_len);
337 /** deal with terms before position .. */
338 /* the glist index starts at zero (unlike scan positions */
339 for (pos = *position-2; pos >= 0; )
343 /* scan on all maximum terms */
344 for (i = 0; i < ord_no; i++)
346 if (ar[i].isam_p == 0)
348 char termz[IT_MAX_WORD+20];
352 ar[i].pos_to_save = -1;
354 strcpy(termz, ar[i].prefix);
355 strcat(termz, wrbuf_cstr(ar[i].term));
356 dict_scan(zh->reg->dict, termz, &before, &after,
360 /* get maximum after scan */
361 for (i = 0; i < ord_no; i++)
364 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
365 hi = wrbuf_cstr(ar[i].term);
369 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, hi,
370 index_type, ar, ord_no, glist,
371 (pos >= 0 && pos < *num_entries) ? pos : -1))
374 /* see if we got all terms before.. */
378 /* did not get all terms; adjust the real position and reduce
380 yaz_log(YLOG_LOG, "before terms dif=%d", dif);
386 for (i = 0; i < ord_no; i++)
388 char termz[IT_MAX_WORD+20];
391 prefix_len = key_SU_encode(ords[i], termz);
392 termz[prefix_len] = 0;
393 strcpy(ar[i].prefix, termz);
395 if (trans_scan_term(zh, zapt, termz+prefix_len, zm) ==
398 wrbuf_rewind(ar[i].term);
399 wrbuf_puts(ar[i].term, termz + prefix_len);
404 after_pos = 1; /* immediate term first.. */
405 for (pos = *position-1; pos < *num_entries; )
409 /* scan on all minimum terms */
410 for (i = 0; i < ord_no; i++)
412 if (ar[i].isam_p == 0)
414 char termz[IT_MAX_WORD+20];
416 int after = after_pos;
418 ar[i].pos_to_save = 1;
420 strcpy(termz, ar[i].prefix);
421 strcat(termz, wrbuf_cstr(ar[i].term));
422 dict_scan(zh->reg->dict, termz, &before, &after,
426 after_pos = 2; /* next round we grab following term */
428 /* get minimum after scan */
429 for (i = 0; i < ord_no; i++)
432 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
433 lo = wrbuf_cstr(ar[i].term);
437 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, lo,
438 index_type, ar, ord_no, glist,
439 (pos >= 0 && pos < *num_entries) ? pos : -1))
443 if (pos != *num_entries)
454 for (i = 0; i < ord_no; i++)
455 wrbuf_destroy(ar[i].term);
460 struct scan1_info_entry {
466 struct scan1_info_entry *list;
472 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
473 const Odr_oid *attributeset,
474 int num_bases, char **basenames,
475 int *position, int *num_entries, ZebraScanEntry **list,
476 int *is_partial, RSET limit_set)
479 int ords[RPN_MAX_ORDS], ord_no = 0;
481 const char *index_type;
482 char *search_type = NULL;
488 struct rset_key_control *kc = 0;
494 attributeset = yaz_oid_attset_bib_1;
496 if (!limit_set) /* no limit set given already */
498 /* see if there is a @attr 8=set */
500 int termset_value_numeric;
501 const char *termset_value_string;
502 attr_init_APT(&termset, zapt, 8);
503 termset_value_numeric =
504 attr_find_ex(&termset, NULL, &termset_value_string);
505 if (termset_value_numeric != -1)
508 const char *termset_name = 0;
510 if (termset_value_numeric != -2)
512 sprintf(resname, "%d", termset_value_numeric);
513 termset_name = resname;
516 termset_name = termset_value_string;
518 limit_set = resultSetRef(zh, termset_name);
523 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
530 yaz_log(YLOG_DEBUG, "position = %d, num = %d",
531 *position, *num_entries);
533 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
534 rank_type, &complete_flag, &sort_flag))
537 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
540 if (num_bases > RPN_MAX_ORDS)
542 zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
546 for (base_no = 0; base_no < num_bases; base_no++)
550 if (zebraExplain_curDatabase(zh->reg->zei, basenames[base_no]))
552 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
557 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
560 ords[ord_no++] = ord;
564 *num_entries = 0; /* zebra_apt_get_ord should set error reason */
567 if (*num_entries < 1)
572 nmem = nmem_create();
573 kc = zebra_key_control_create(zh);
576 res = rpn_facet(zh, stream, nmem, kc, zapt, position, num_entries,
578 is_partial, limit_set, index_type, ord_no, ords);
580 res = rpn_scan_norm(zh, stream, nmem, kc, zapt, position, num_entries,
582 is_partial, limit_set, index_type, ord_no, ords);
591 * indent-tabs-mode: nil
593 * vim: shiftwidth=4 tabstop=8 expandtab