1 /* $Id: rpnscan.c,v 1.11 2007-05-09 07:07:18 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 struct scan2_info_entry {
116 static int scan_handle2(char *name, const char *info, int pos, void *client)
119 struct scan2_info_entry *scan_info = (struct scan2_info_entry *) client;
121 if (scan_info->pos_to_save != pos)
124 len_prefix = strlen(scan_info->prefix);
125 if (memcmp (name, scan_info->prefix, len_prefix))
128 /* skip special terms such as first-in-field specials */
129 if (name[len_prefix] < CHR_BASE_CHAR)
132 wrbuf_rewind(scan_info->term);
133 wrbuf_puts(scan_info->term, name+len_prefix);
135 assert (*info == sizeof(ISAM_P));
136 memcpy (&scan_info->isam_p, info+1, sizeof(ISAM_P));
141 static int scan_save_set(ZebraHandle zh, ODR stream, NMEM nmem,
142 struct rset_key_control *kc,
143 Z_AttributesPlusTerm *zapt,
147 struct scan2_info_entry *ar, int ord_no,
148 ZebraScanEntry *glist, int pos)
152 for (i = 0; i < ord_no; i++)
154 if (ar[i].isam_p && strcmp(wrbuf_cstr(ar[i].term), term) == 0)
156 RSET rset_t = rset_trunc(
157 zh, &ar[i].isam_p, 1,
158 wrbuf_buf(ar[i].term), wrbuf_len(ar[i].term),
159 NULL, 0, zapt->term->which, nmem,
160 kc, kc->scope, 0, index_type,
162 0 /* term_ref_id_str */);
171 rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */,
180 /* merge with limit_set if given */
185 rsets[1] = rset_dup(limit_set);
187 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
190 count_set(zh, rset, &count);
196 zebra_term_untrans_iconv(zh, stream->mem, index_type,
197 &glist[pos].term, term);
198 glist[pos].occurrences = count;
206 static ZEBRA_RES rpn_scan_ver2(ZebraHandle zh, ODR stream, NMEM nmem,
207 struct rset_key_control *kc,
208 Z_AttributesPlusTerm *zapt,
209 int *position, int *num_entries,
210 ZebraScanEntry **list,
211 int *is_partial, RSET limit_set,
212 int index_type, int ord_no, int *ords)
214 struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no);
215 struct rpn_char_map_info rcmi;
220 ZebraScanEntry *glist = (ZebraScanEntry *)
221 odr_malloc(stream, *num_entries * sizeof(*glist));
224 if (*position > *num_entries+1)
231 rpn_char_map_prepare (zh->reg, index_type, &rcmi);
233 for (i = 0; i < ord_no; i++)
234 ar[i].term = wrbuf_alloc();
236 for (i = 0; i < ord_no; i++)
238 char termz[IT_MAX_WORD+20];
241 prefix_len = key_SU_encode (ords[i], termz);
242 termz[prefix_len] = 0;
243 strcpy(ar[i].prefix, termz);
245 if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) ==
248 for (i = 0; i < ord_no; i++)
249 wrbuf_destroy(ar[i].term);
252 wrbuf_rewind(ar[i].term);
253 wrbuf_puts(ar[i].term, termz + prefix_len);
256 /** deal with terms before position .. */
257 /* the glist index starts at zero (unlike scan positions */
258 for (pos = *position-2; pos >= 0; )
262 /* scan on all maximum terms */
263 for (i = 0; i < ord_no; i++)
265 if (ar[i].isam_p == 0)
267 char termz[IT_MAX_WORD+20];
271 ar[i].pos_to_save = -1;
273 strcpy(termz, ar[i].prefix);
274 strcat(termz, wrbuf_cstr(ar[i].term));
275 dict_scan(zh->reg->dict, termz, &before, &after,
279 /* get maximum after scan */
280 for (i = 0; i < ord_no; i++)
283 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
284 hi = wrbuf_cstr(ar[i].term);
288 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, hi,
289 index_type, ar, ord_no, glist,
290 (pos >= 0 && pos < *num_entries) ? pos : -1))
293 /* see if we got all terms before.. */
297 /* did not get all terms; adjust the real position and reduce
299 yaz_log(YLOG_LOG, "before terms dif=%d", dif);
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 wrbuf_rewind(ar[i].term);
318 wrbuf_puts(ar[i].term, termz + prefix_len);
322 after_pos = 1; /* immediate term first.. */
323 for (pos = *position-1; pos < *num_entries; )
327 /* scan on all minimum terms */
328 for (i = 0; i < ord_no; i++)
330 if (ar[i].isam_p == 0)
332 char termz[IT_MAX_WORD+20];
334 int after = after_pos;
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 after_pos = 2; /* next round we grab following term */
346 /* get minimum after scan */
347 for (i = 0; i < ord_no; i++)
350 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
351 lo = wrbuf_cstr(ar[i].term);
355 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, lo,
356 index_type, ar, ord_no, glist,
357 (pos >= 0 && pos < *num_entries) ? pos : -1))
361 if (pos != *num_entries)
372 for (i = 0; i < ord_no; i++)
373 wrbuf_destroy(ar[i].term);
378 struct scan1_info_entry {
384 struct scan1_info_entry *list;
390 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
391 const Odr_oid *attributeset,
392 int num_bases, char **basenames,
393 int *position, int *num_entries, ZebraScanEntry **list,
394 int *is_partial, RSET limit_set)
397 int ords[RPN_MAX_ORDS], ord_no = 0;
400 char *search_type = NULL;
406 struct rset_key_control *kc = 0;
412 attributeset = yaz_oid_attset_bib_1;
414 if (!limit_set) /* no limit set given already */
416 /* see if there is a @attr 8=set */
418 int termset_value_numeric;
419 const char *termset_value_string;
420 attr_init_APT(&termset, zapt, 8);
421 termset_value_numeric =
422 attr_find_ex(&termset, NULL, &termset_value_string);
423 if (termset_value_numeric != -1)
426 const char *termset_name = 0;
428 if (termset_value_numeric != -2)
430 sprintf(resname, "%d", termset_value_numeric);
431 termset_name = resname;
434 termset_name = termset_value_string;
436 limit_set = resultSetRef (zh, termset_name);
441 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
448 yaz_log(YLOG_DEBUG, "position = %d, num = %d",
449 *position, *num_entries);
451 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
452 rank_type, &complete_flag, &sort_flag))
455 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
458 if (num_bases > RPN_MAX_ORDS)
460 zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
464 for (base_no = 0; base_no < num_bases; base_no++)
468 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
470 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
475 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
478 ords[ord_no++] = ord;
482 *num_entries = 0; /* zebra_apt_get_ord should set error reason */
485 if (*num_entries < 1)
490 nmem = nmem_create();
491 kc = zebra_key_control_create(zh);
493 res = rpn_scan_ver2(zh, stream, nmem, kc, zapt, position, num_entries,
495 is_partial, limit_set, index_type, ord_no, ords);
504 * indent-tabs-mode: nil
506 * vim: shiftwidth=4 tabstop=8 expandtab