1 /* $Id: rpnscan.c,v 1.15 2007-10-29 16:57:53 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, const char *index_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, *index_type,
68 if (**map == *CHR_SPACE)
73 for (src = space_map; *src; src++)
76 for (src = *map; *src; src++)
85 static void count_set(ZebraHandle zh, RSET rset, zint *count)
91 yaz_log(YLOG_DEBUG, "count_set");
93 rset->hits_limit = zh->approx_limit;
96 rfd = rset_open(rset, RSETF_READ);
97 while (rset_read(rfd, &key,0 /* never mind terms */))
99 if (key.mem[0] != psysno)
102 if (rfd->counted_items >= rset->hits_limit)
107 *count = rset->hits_count;
110 static void get_first_snippet_from_rset(ZebraHandle zh,
111 RSET rset, zebra_snippets *snippets,
117 size_t sysno_mem_index = 0;
119 if (zh->m_staticrank)
122 yaz_log(YLOG_DEBUG, "get_first_snippet_from_rset");
124 rfd = rset_open(rset, RSETF_READ);
126 while (rset_read(rfd, &key, &termid))
128 if (key.mem[sysno_mem_index] != *sysno)
132 *sysno = key.mem[sysno_mem_index];
137 for (ol = termid->ol; ol; ol = ol->next)
139 zebra_snippets_append(snippets, key.mem[key.len-1], 0,
140 ol->ord, termid->name);
147 struct scan2_info_entry {
155 static int scan_handle2(char *name, const char *info, int pos, void *client)
158 struct scan2_info_entry *scan_info = (struct scan2_info_entry *) client;
160 if (scan_info->pos_to_save != pos)
163 len_prefix = strlen(scan_info->prefix);
164 if (memcmp(name, scan_info->prefix, len_prefix))
167 /* skip special terms such as first-in-field specials */
168 if (name[len_prefix] < CHR_BASE_CHAR)
171 wrbuf_rewind(scan_info->term);
172 wrbuf_puts(scan_info->term, name+len_prefix);
174 assert(*info == sizeof(ISAM_P));
175 memcpy(&scan_info->isam_p, info+1, sizeof(ISAM_P));
180 static int scan_save_set(ZebraHandle zh, ODR stream, NMEM nmem,
181 struct rset_key_control *kc,
182 Z_AttributesPlusTerm *zapt,
185 const char *index_type,
186 struct scan2_info_entry *ar, int ord_no,
187 ZebraScanEntry *glist, int pos)
191 for (i = 0; i < ord_no; i++)
193 if (ar[i].isam_p && strcmp(wrbuf_cstr(ar[i].term), term) == 0)
195 struct ord_list *ol = ord_list_create(nmem);
198 ol = ord_list_append(nmem, ol, ar[i].ord);
202 zh, &ar[i].isam_p, 1,
203 wrbuf_buf(ar[i].term), wrbuf_len(ar[i].term),
204 NULL, 1, zapt->term->which, nmem,
205 kc, kc->scope, ol, index_type,
207 0 /* term_ref_id_str */);
216 rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */,
225 /* merge with limit_set if given */
230 rsets[1] = rset_dup(limit_set);
232 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
235 count_set(zh, rset, &count);
241 zebra_snippets *rec_snippets = zebra_snippets_create();
242 zebra_snippets *hit_snippets = zebra_snippets_create();
245 glist[pos].display_term = 0;
247 get_first_snippet_from_rset(zh, rset, hit_snippets, &sysno);
249 code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
253 const struct zebra_snippet_word *w =
254 zebra_snippets_lookup(rec_snippets, hit_snippets);
257 glist[pos].display_term = odr_strdup(stream, w->term);
260 if (!glist[pos].term)
261 zebra_term_untrans_iconv(zh, stream->mem, *index_type,
262 &glist[pos].term, term);
263 glist[pos].occurrences = count;
264 zebra_snippets_destroy(rec_snippets);
265 zebra_snippets_destroy(hit_snippets);
276 static ZEBRA_RES rpn_scan_ver2(ZebraHandle zh, ODR stream, NMEM nmem,
277 struct rset_key_control *kc,
278 Z_AttributesPlusTerm *zapt,
279 int *position, int *num_entries,
280 ZebraScanEntry **list,
281 int *is_partial, RSET limit_set,
282 const char *index_type,
283 int ord_no, int *ords)
285 struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no);
286 struct rpn_char_map_info rcmi;
291 ZebraScanEntry *glist = (ZebraScanEntry *)
292 odr_malloc(stream, *num_entries * sizeof(*glist));
295 if (*position > *num_entries+1)
302 rpn_char_map_prepare(zh->reg, *index_type, &rcmi);
304 for (i = 0; i < ord_no; i++)
305 ar[i].term = wrbuf_alloc();
307 for (i = 0; i < ord_no; i++)
309 char termz[IT_MAX_WORD+20];
312 prefix_len = key_SU_encode(ords[i], termz);
313 termz[prefix_len] = 0;
314 strcpy(ar[i].prefix, termz);
316 if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) ==
319 for (i = 0; i < ord_no; i++)
320 wrbuf_destroy(ar[i].term);
323 wrbuf_rewind(ar[i].term);
324 wrbuf_puts(ar[i].term, termz + prefix_len);
328 /** deal with terms before position .. */
329 /* the glist index starts at zero (unlike scan positions */
330 for (pos = *position-2; pos >= 0; )
334 /* scan on all maximum terms */
335 for (i = 0; i < ord_no; i++)
337 if (ar[i].isam_p == 0)
339 char termz[IT_MAX_WORD+20];
343 ar[i].pos_to_save = -1;
345 strcpy(termz, ar[i].prefix);
346 strcat(termz, wrbuf_cstr(ar[i].term));
347 dict_scan(zh->reg->dict, termz, &before, &after,
351 /* get maximum after scan */
352 for (i = 0; i < ord_no; i++)
355 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
356 hi = wrbuf_cstr(ar[i].term);
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))
365 /* see if we got all terms before.. */
369 /* did not get all terms; adjust the real position and reduce
371 yaz_log(YLOG_LOG, "before terms dif=%d", dif);
377 for (i = 0; i < ord_no; i++)
379 char termz[IT_MAX_WORD+20];
382 prefix_len = key_SU_encode(ords[i], termz);
383 termz[prefix_len] = 0;
384 strcpy(ar[i].prefix, termz);
386 if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) ==
389 wrbuf_rewind(ar[i].term);
390 wrbuf_puts(ar[i].term, termz + prefix_len);
395 after_pos = 1; /* immediate term first.. */
396 for (pos = *position-1; pos < *num_entries; )
400 /* scan on all minimum terms */
401 for (i = 0; i < ord_no; i++)
403 if (ar[i].isam_p == 0)
405 char termz[IT_MAX_WORD+20];
407 int after = after_pos;
409 ar[i].pos_to_save = 1;
411 strcpy(termz, ar[i].prefix);
412 strcat(termz, wrbuf_cstr(ar[i].term));
413 dict_scan(zh->reg->dict, termz, &before, &after,
417 after_pos = 2; /* next round we grab following term */
419 /* get minimum after scan */
420 for (i = 0; i < ord_no; i++)
423 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
424 lo = wrbuf_cstr(ar[i].term);
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))
434 if (pos != *num_entries)
445 for (i = 0; i < ord_no; i++)
446 wrbuf_destroy(ar[i].term);
451 struct scan1_info_entry {
457 struct scan1_info_entry *list;
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)
470 int ords[RPN_MAX_ORDS], ord_no = 0;
472 const char *index_type;
473 char *search_type = NULL;
479 struct rset_key_control *kc = 0;
485 attributeset = yaz_oid_attset_bib_1;
487 if (!limit_set) /* no limit set given already */
489 /* see if there is a @attr 8=set */
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)
499 const char *termset_name = 0;
501 if (termset_value_numeric != -2)
503 sprintf(resname, "%d", termset_value_numeric);
504 termset_name = resname;
507 termset_name = termset_value_string;
509 limit_set = resultSetRef(zh, termset_name);
514 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
521 yaz_log(YLOG_DEBUG, "position = %d, num = %d",
522 *position, *num_entries);
524 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
525 rank_type, &complete_flag, &sort_flag))
528 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
531 if (num_bases > RPN_MAX_ORDS)
533 zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
537 for (base_no = 0; base_no < num_bases; base_no++)
541 if (zebraExplain_curDatabase(zh->reg->zei, basenames[base_no]))
543 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
548 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
551 ords[ord_no++] = ord;
555 *num_entries = 0; /* zebra_apt_get_ord should set error reason */
558 if (*num_entries < 1)
563 nmem = nmem_create();
564 kc = zebra_key_control_create(zh);
566 res = rpn_scan_ver2(zh, stream, nmem, kc, zapt, position, num_entries,
568 is_partial, limit_set, index_type, ord_no, ords);
577 * indent-tabs-mode: nil
579 * vim: shiftwidth=4 tabstop=8 expandtab