1 /* $Id: rpnscan.c,v 1.14 2007-10-29 09:25:40 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 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, int reg_type)
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(zh->reg->zebra_maps, reg_type, &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)
90 yaz_log(YLOG_DEBUG, "count_set");
92 rset->hits_limit = zh->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,
185 struct scan2_info_entry *ar, int ord_no,
186 ZebraScanEntry *glist, int pos)
190 for (i = 0; i < ord_no; i++)
192 if (ar[i].isam_p && strcmp(wrbuf_cstr(ar[i].term), term) == 0)
194 struct ord_list *ol = ord_list_create(nmem);
197 ol = ord_list_append(nmem, ol, ar[i].ord);
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,
206 0 /* term_ref_id_str */);
215 rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */,
224 /* merge with limit_set if given */
229 rsets[1] = rset_dup(limit_set);
231 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
234 count_set(zh, rset, &count);
240 zebra_snippets *rec_snippets = zebra_snippets_create();
241 zebra_snippets *hit_snippets = zebra_snippets_create();
244 glist[pos].display_term = 0;
246 get_first_snippet_from_rset(zh, rset, hit_snippets, &sysno);
248 code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
252 const struct zebra_snippet_word *w =
253 zebra_snippets_lookup(rec_snippets, hit_snippets);
256 glist[pos].display_term = odr_strdup(stream, w->term);
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);
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)
283 struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no);
284 struct rpn_char_map_info rcmi;
289 ZebraScanEntry *glist = (ZebraScanEntry *)
290 odr_malloc(stream, *num_entries * sizeof(*glist));
293 if (*position > *num_entries+1)
300 rpn_char_map_prepare(zh->reg, index_type, &rcmi);
302 for (i = 0; i < ord_no; i++)
303 ar[i].term = wrbuf_alloc();
305 for (i = 0; i < ord_no; i++)
307 char termz[IT_MAX_WORD+20];
310 prefix_len = key_SU_encode(ords[i], termz);
311 termz[prefix_len] = 0;
312 strcpy(ar[i].prefix, termz);
314 if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) ==
317 for (i = 0; i < ord_no; i++)
318 wrbuf_destroy(ar[i].term);
321 wrbuf_rewind(ar[i].term);
322 wrbuf_puts(ar[i].term, termz + prefix_len);
326 /** deal with terms before position .. */
327 /* the glist index starts at zero (unlike scan positions */
328 for (pos = *position-2; pos >= 0; )
332 /* scan on all maximum terms */
333 for (i = 0; i < ord_no; i++)
335 if (ar[i].isam_p == 0)
337 char termz[IT_MAX_WORD+20];
341 ar[i].pos_to_save = -1;
343 strcpy(termz, ar[i].prefix);
344 strcat(termz, wrbuf_cstr(ar[i].term));
345 dict_scan(zh->reg->dict, termz, &before, &after,
349 /* get maximum after scan */
350 for (i = 0; i < ord_no; i++)
353 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
354 hi = wrbuf_cstr(ar[i].term);
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))
363 /* see if we got all terms before.. */
367 /* did not get all terms; adjust the real position and reduce
369 yaz_log(YLOG_LOG, "before terms dif=%d", dif);
375 for (i = 0; i < ord_no; i++)
377 char termz[IT_MAX_WORD+20];
380 prefix_len = key_SU_encode(ords[i], termz);
381 termz[prefix_len] = 0;
382 strcpy(ar[i].prefix, termz);
384 if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) ==
387 wrbuf_rewind(ar[i].term);
388 wrbuf_puts(ar[i].term, termz + prefix_len);
393 after_pos = 1; /* immediate term first.. */
394 for (pos = *position-1; pos < *num_entries; )
398 /* scan on all minimum terms */
399 for (i = 0; i < ord_no; i++)
401 if (ar[i].isam_p == 0)
403 char termz[IT_MAX_WORD+20];
405 int after = after_pos;
407 ar[i].pos_to_save = 1;
409 strcpy(termz, ar[i].prefix);
410 strcat(termz, wrbuf_cstr(ar[i].term));
411 dict_scan(zh->reg->dict, termz, &before, &after,
415 after_pos = 2; /* next round we grab following term */
417 /* get minimum after scan */
418 for (i = 0; i < ord_no; i++)
421 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
422 lo = wrbuf_cstr(ar[i].term);
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))
432 if (pos != *num_entries)
443 for (i = 0; i < ord_no; i++)
444 wrbuf_destroy(ar[i].term);
449 struct scan1_info_entry {
455 struct scan1_info_entry *list;
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)
468 int ords[RPN_MAX_ORDS], ord_no = 0;
471 char *search_type = NULL;
477 struct rset_key_control *kc = 0;
483 attributeset = yaz_oid_attset_bib_1;
485 if (!limit_set) /* no limit set given already */
487 /* see if there is a @attr 8=set */
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)
497 const char *termset_name = 0;
499 if (termset_value_numeric != -2)
501 sprintf(resname, "%d", termset_value_numeric);
502 termset_name = resname;
505 termset_name = termset_value_string;
507 limit_set = resultSetRef(zh, termset_name);
512 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
519 yaz_log(YLOG_DEBUG, "position = %d, num = %d",
520 *position, *num_entries);
522 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
523 rank_type, &complete_flag, &sort_flag))
526 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
529 if (num_bases > RPN_MAX_ORDS)
531 zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
535 for (base_no = 0; base_no < num_bases; base_no++)
539 if (zebraExplain_curDatabase(zh->reg->zei, basenames[base_no]))
541 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
546 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
549 ords[ord_no++] = ord;
553 *num_entries = 0; /* zebra_apt_get_ord should set error reason */
556 if (*num_entries < 1)
561 nmem = nmem_create();
562 kc = zebra_key_control_create(zh);
564 res = rpn_scan_ver2(zh, stream, nmem, kc, zapt, position, num_entries,
566 is_partial, limit_set, index_type, ord_no, ords);
575 * indent-tabs-mode: nil
577 * vim: shiftwidth=4 tabstop=8 expandtab