1 /* $Id: rpnscan.c,v 1.23 2007-12-03 11:49:11 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 /* 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)
48 char termz0[IT_MAX_WORD];
50 if (zapt_term_to_utf8(zh, zapt, termz0) == ZEBRA_FAIL)
51 return ZEBRA_FAIL; /* error */
55 const char *cp = (const char *) termz0;
56 const char *cp_end = cp + strlen(cp);
59 const char *space_map = NULL;
62 while ((len = (cp_end - cp)) > 0)
64 map = zebra_maps_input(zm, &cp, len, 0);
65 if (**map == *CHR_SPACE)
70 for (src = space_map; *src; src++)
73 for (src = *map; *src; src++)
82 static void get_first_snippet_from_rset(ZebraHandle zh,
83 RSET rset, zebra_snippets *snippets,
89 size_t sysno_mem_index = 0;
94 yaz_log(YLOG_DEBUG, "get_first_snippet_from_rset");
96 rfd = rset_open(rset, RSETF_READ);
98 while (rset_read(rfd, &key, &termid))
100 if (key.mem[sysno_mem_index] != *sysno)
104 *sysno = key.mem[sysno_mem_index];
109 for (ol = termid->ol; ol; ol = ol->next)
111 zebra_snippets_append(snippets, key.mem[key.len-1], 0,
112 ol->ord, termid->name);
119 struct scan2_info_entry {
127 static int scan_handle2(char *name, const char *info, int pos, void *client)
130 struct scan2_info_entry *scan_info = (struct scan2_info_entry *) client;
132 if (scan_info->pos_to_save != pos)
135 len_prefix = strlen(scan_info->prefix);
136 if (memcmp(name, scan_info->prefix, len_prefix))
139 /* skip special terms such as first-in-field specials */
140 if (name[len_prefix] < CHR_BASE_CHAR)
143 wrbuf_rewind(scan_info->term);
144 wrbuf_puts(scan_info->term, name+len_prefix);
146 assert(*info == sizeof(ISAM_P));
147 memcpy(&scan_info->isam_p, info+1, sizeof(ISAM_P));
152 static int scan_save_set(ZebraHandle zh, ODR stream, NMEM nmem,
153 struct rset_key_control *kc,
154 Z_AttributesPlusTerm *zapt,
157 const char *index_type,
158 struct scan2_info_entry *ar, int ord_no,
159 ZebraScanEntry *glist, int pos)
163 zint approx_limit = zh->approx_limit;
164 AttrType global_hits_limit_attr;
166 attr_init_APT(&global_hits_limit_attr, zapt, 12);
168 l = attr_find(&global_hits_limit_attr, NULL);
172 for (i = 0; i < ord_no; i++)
174 if (ar[i].isam_p && strcmp(wrbuf_cstr(ar[i].term), term) == 0)
176 struct ord_list *ol = ord_list_create(nmem);
179 ol = ord_list_append(nmem, ol, ar[i].ord);
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 */);
197 rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */,
206 /* merge with limit_set if given */
211 rsets[1] = rset_dup(limit_set);
213 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
216 zebra_count_set(zh, rset, &count, approx_limit);
222 zebra_snippets *rec_snippets = zebra_snippets_create();
223 zebra_snippets *hit_snippets = zebra_snippets_create();
226 glist[pos].display_term = 0;
228 get_first_snippet_from_rset(zh, rset, hit_snippets, &sysno);
230 code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
234 const struct zebra_snippet_word *w =
235 zebra_snippets_lookup(rec_snippets, hit_snippets);
238 glist[pos].display_term = odr_strdup(stream, w->term);
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);
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)
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);
273 ZebraScanEntry *glist = (ZebraScanEntry *)
274 odr_malloc(stream, *num_entries * sizeof(*glist));
277 if (*position > *num_entries+1)
284 rpn_char_map_prepare(zh->reg, zm, &rcmi);
286 for (i = 0; i < ord_no; i++)
287 ar[i].term = wrbuf_alloc();
289 for (i = 0; i < ord_no; i++)
291 char termz[IT_MAX_WORD+20];
294 prefix_len = key_SU_encode(ords[i], termz);
295 termz[prefix_len] = 0;
296 strcpy(ar[i].prefix, termz);
298 if (trans_scan_term(zh, zapt, termz+prefix_len, zm) ==
301 for (i = 0; i < ord_no; i++)
302 wrbuf_destroy(ar[i].term);
305 wrbuf_rewind(ar[i].term);
306 wrbuf_puts(ar[i].term, termz + prefix_len);
310 /** deal with terms before position .. */
311 /* the glist index starts at zero (unlike scan positions */
312 for (pos = *position-2; pos >= 0; )
316 /* scan on all maximum terms */
317 for (i = 0; i < ord_no; i++)
319 if (ar[i].isam_p == 0)
321 char termz[IT_MAX_WORD+20];
325 ar[i].pos_to_save = -1;
327 strcpy(termz, ar[i].prefix);
328 strcat(termz, wrbuf_cstr(ar[i].term));
329 dict_scan(zh->reg->dict, termz, &before, &after,
333 /* get maximum after scan */
334 for (i = 0; i < ord_no; i++)
337 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
338 hi = wrbuf_cstr(ar[i].term);
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))
347 /* see if we got all terms before.. */
351 /* did not get all terms; adjust the real position and reduce
353 yaz_log(YLOG_LOG, "before terms dif=%d", dif);
359 for (i = 0; i < ord_no; i++)
361 char termz[IT_MAX_WORD+20];
364 prefix_len = key_SU_encode(ords[i], termz);
365 termz[prefix_len] = 0;
366 strcpy(ar[i].prefix, termz);
368 if (trans_scan_term(zh, zapt, termz+prefix_len, zm) ==
371 wrbuf_rewind(ar[i].term);
372 wrbuf_puts(ar[i].term, termz + prefix_len);
377 after_pos = 1; /* immediate term first.. */
378 for (pos = *position-1; pos < *num_entries; )
382 /* scan on all minimum terms */
383 for (i = 0; i < ord_no; i++)
385 if (ar[i].isam_p == 0)
387 char termz[IT_MAX_WORD+20];
389 int after = after_pos;
391 ar[i].pos_to_save = 1;
393 strcpy(termz, ar[i].prefix);
394 strcat(termz, wrbuf_cstr(ar[i].term));
395 dict_scan(zh->reg->dict, termz, &before, &after,
399 after_pos = 2; /* next round we grab following term */
401 /* get minimum after scan */
402 for (i = 0; i < ord_no; i++)
405 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
406 lo = wrbuf_cstr(ar[i].term);
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))
416 if (pos != *num_entries)
427 for (i = 0; i < ord_no; i++)
428 wrbuf_destroy(ar[i].term);
433 struct scan1_info_entry {
439 struct scan1_info_entry *list;
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)
452 int ords[RPN_MAX_ORDS], ord_no = 0;
454 const char *index_type;
455 char *search_type = NULL;
461 struct rset_key_control *kc = 0;
468 attributeset = yaz_oid_attset_bib_1;
472 /* see if there is a @attr 8=set */
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)
481 if (termset_value_numeric != -2)
484 sprintf(resname, "%d", termset_value_numeric);
485 set_name = odr_strdup(stream, resname);
488 set_name = odr_strdup(stream, termset_value_string);
494 limit_set = resultSetRef(zh, set_name);
499 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
505 yaz_log(YLOG_DEBUG, "position = %d, num = %d",
506 *position, *num_entries);
508 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
509 rank_type, &complete_flag, &sort_flag))
512 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
515 if (num_bases > RPN_MAX_ORDS)
517 zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
522 return rpn_facet(zh, stream, zapt, attributeset, position, num_entries,
523 list, is_partial, set_name);
525 for (base_no = 0; base_no < num_bases; base_no++)
529 if (zebraExplain_curDatabase(zh->reg->zei, basenames[base_no]))
531 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
536 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
539 ords[ord_no++] = ord;
543 *num_entries = 0; /* zebra_apt_get_ord should set error reason */
546 if (*num_entries < 1)
551 nmem = nmem_create();
552 kc = zebra_key_control_create(zh);
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);
565 * indent-tabs-mode: nil
567 * vim: shiftwidth=4 tabstop=8 expandtab