1 /* $Id: rpnscan.c,v 1.12 2007-08-21 13:27:04 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(RSET rset, zebra_snippets *snippets,
116 yaz_log(YLOG_DEBUG, "get_first_snippet_from_rset");
118 rfd = rset_open(rset, RSETF_READ);
120 while (rset_read(rfd, &key, &termid))
122 if (key.mem[0] != *sysno)
131 for (ol = termid->ol; ol; ol = ol->next)
133 zebra_snippets_append(snippets, key.mem[key.len-1], 0,
134 ol->ord, termid->name);
141 struct scan2_info_entry {
149 static int scan_handle2(char *name, const char *info, int pos, void *client)
152 struct scan2_info_entry *scan_info = (struct scan2_info_entry *) client;
154 if (scan_info->pos_to_save != pos)
157 len_prefix = strlen(scan_info->prefix);
158 if (memcmp (name, scan_info->prefix, len_prefix))
161 /* skip special terms such as first-in-field specials */
162 if (name[len_prefix] < CHR_BASE_CHAR)
165 wrbuf_rewind(scan_info->term);
166 wrbuf_puts(scan_info->term, name+len_prefix);
168 assert (*info == sizeof(ISAM_P));
169 memcpy (&scan_info->isam_p, info+1, sizeof(ISAM_P));
174 static int scan_save_set(ZebraHandle zh, ODR stream, NMEM nmem,
175 struct rset_key_control *kc,
176 Z_AttributesPlusTerm *zapt,
180 struct scan2_info_entry *ar, int ord_no,
181 ZebraScanEntry *glist, int pos)
185 for (i = 0; i < ord_no; i++)
187 if (ar[i].isam_p && strcmp(wrbuf_cstr(ar[i].term), term) == 0)
189 struct ord_list *ol = ord_list_create(nmem);
192 ol = ord_list_append(nmem, ol, ar[i].ord);
196 zh, &ar[i].isam_p, 1,
197 wrbuf_buf(ar[i].term), wrbuf_len(ar[i].term),
198 NULL, 1, zapt->term->which, nmem,
199 kc, kc->scope, ol, index_type,
201 0 /* term_ref_id_str */);
210 rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */,
219 /* merge with limit_set if given */
224 rsets[1] = rset_dup(limit_set);
226 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
229 count_set(zh, rset, &count);
235 zebra_snippets *rec_snippets = zebra_snippets_create();
236 zebra_snippets *hit_snippets = zebra_snippets_create();
239 glist[pos].display_term = 0;
241 get_first_snippet_from_rset(rset, hit_snippets, &sysno);
243 code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
247 const struct zebra_snippet_word *w =
248 zebra_snippets_lookup(rec_snippets, hit_snippets);
251 glist[pos].display_term = odr_strdup(stream, w->term);
254 if (!glist[pos].term)
255 zebra_term_untrans_iconv(zh, stream->mem, index_type,
256 &glist[pos].term, term);
257 glist[pos].occurrences = count;
258 zebra_snippets_destroy(rec_snippets);
259 zebra_snippets_destroy(hit_snippets);
270 static ZEBRA_RES rpn_scan_ver2(ZebraHandle zh, ODR stream, NMEM nmem,
271 struct rset_key_control *kc,
272 Z_AttributesPlusTerm *zapt,
273 int *position, int *num_entries,
274 ZebraScanEntry **list,
275 int *is_partial, RSET limit_set,
276 int index_type, int ord_no, int *ords)
278 struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no);
279 struct rpn_char_map_info rcmi;
284 ZebraScanEntry *glist = (ZebraScanEntry *)
285 odr_malloc(stream, *num_entries * sizeof(*glist));
288 if (*position > *num_entries+1)
295 rpn_char_map_prepare (zh->reg, index_type, &rcmi);
297 for (i = 0; i < ord_no; i++)
298 ar[i].term = wrbuf_alloc();
300 for (i = 0; i < ord_no; i++)
302 char termz[IT_MAX_WORD+20];
305 prefix_len = key_SU_encode (ords[i], termz);
306 termz[prefix_len] = 0;
307 strcpy(ar[i].prefix, termz);
309 if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) ==
312 for (i = 0; i < ord_no; i++)
313 wrbuf_destroy(ar[i].term);
316 wrbuf_rewind(ar[i].term);
317 wrbuf_puts(ar[i].term, termz + prefix_len);
321 /** deal with terms before position .. */
322 /* the glist index starts at zero (unlike scan positions */
323 for (pos = *position-2; pos >= 0; )
327 /* scan on all maximum terms */
328 for (i = 0; i < ord_no; i++)
330 if (ar[i].isam_p == 0)
332 char termz[IT_MAX_WORD+20];
336 ar[i].pos_to_save = -1;
338 strcpy(termz, ar[i].prefix);
339 strcat(termz, wrbuf_cstr(ar[i].term));
340 dict_scan(zh->reg->dict, termz, &before, &after,
344 /* get maximum after scan */
345 for (i = 0; i < ord_no; i++)
348 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
349 hi = wrbuf_cstr(ar[i].term);
353 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, hi,
354 index_type, ar, ord_no, glist,
355 (pos >= 0 && pos < *num_entries) ? pos : -1))
358 /* see if we got all terms before.. */
362 /* did not get all terms; adjust the real position and reduce
364 yaz_log(YLOG_LOG, "before terms dif=%d", dif);
370 for (i = 0; i < ord_no; i++)
372 char termz[IT_MAX_WORD+20];
375 prefix_len = key_SU_encode (ords[i], termz);
376 termz[prefix_len] = 0;
377 strcpy(ar[i].prefix, termz);
379 if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) ==
382 wrbuf_rewind(ar[i].term);
383 wrbuf_puts(ar[i].term, termz + prefix_len);
388 after_pos = 1; /* immediate term first.. */
389 for (pos = *position-1; pos < *num_entries; )
393 /* scan on all minimum terms */
394 for (i = 0; i < ord_no; i++)
396 if (ar[i].isam_p == 0)
398 char termz[IT_MAX_WORD+20];
400 int after = after_pos;
402 ar[i].pos_to_save = 1;
404 strcpy(termz, ar[i].prefix);
405 strcat(termz, wrbuf_cstr(ar[i].term));
406 dict_scan(zh->reg->dict, termz, &before, &after,
410 after_pos = 2; /* next round we grab following term */
412 /* get minimum after scan */
413 for (i = 0; i < ord_no; i++)
416 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
417 lo = wrbuf_cstr(ar[i].term);
421 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, lo,
422 index_type, ar, ord_no, glist,
423 (pos >= 0 && pos < *num_entries) ? pos : -1))
427 if (pos != *num_entries)
438 for (i = 0; i < ord_no; i++)
439 wrbuf_destroy(ar[i].term);
444 struct scan1_info_entry {
450 struct scan1_info_entry *list;
456 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
457 const Odr_oid *attributeset,
458 int num_bases, char **basenames,
459 int *position, int *num_entries, ZebraScanEntry **list,
460 int *is_partial, RSET limit_set)
463 int ords[RPN_MAX_ORDS], ord_no = 0;
466 char *search_type = NULL;
472 struct rset_key_control *kc = 0;
478 attributeset = yaz_oid_attset_bib_1;
480 if (!limit_set) /* no limit set given already */
482 /* see if there is a @attr 8=set */
484 int termset_value_numeric;
485 const char *termset_value_string;
486 attr_init_APT(&termset, zapt, 8);
487 termset_value_numeric =
488 attr_find_ex(&termset, NULL, &termset_value_string);
489 if (termset_value_numeric != -1)
492 const char *termset_name = 0;
494 if (termset_value_numeric != -2)
496 sprintf(resname, "%d", termset_value_numeric);
497 termset_name = resname;
500 termset_name = termset_value_string;
502 limit_set = resultSetRef (zh, termset_name);
507 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
514 yaz_log(YLOG_DEBUG, "position = %d, num = %d",
515 *position, *num_entries);
517 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
518 rank_type, &complete_flag, &sort_flag))
521 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
524 if (num_bases > RPN_MAX_ORDS)
526 zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
530 for (base_no = 0; base_no < num_bases; base_no++)
534 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
536 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
541 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
544 ords[ord_no++] = ord;
548 *num_entries = 0; /* zebra_apt_get_ord should set error reason */
551 if (*num_entries < 1)
556 nmem = nmem_create();
557 kc = zebra_key_control_create(zh);
559 res = rpn_scan_ver2(zh, stream, nmem, kc, zapt, position, num_entries,
561 is_partial, limit_set, index_type, ord_no, ords);
570 * indent-tabs-mode: nil
572 * vim: shiftwidth=4 tabstop=8 expandtab