1 /* $Id: rpnscan.c,v 1.1 2006-09-21 08:56:52 adam Exp $
2 Copyright (C) 1995-2006
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>
40 struct scan_info_entry {
46 struct scan_info_entry *list;
52 /* convert APT SCAN term to internal cmap */
53 static ZEBRA_RES trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
54 char *termz, int reg_type)
56 char termz0[IT_MAX_WORD];
58 if (zapt_term_to_utf8(zh, zapt, termz0) == ZEBRA_FAIL)
59 return ZEBRA_FAIL; /* error */
63 const char *cp = (const char *) termz0;
64 const char *cp_end = cp + strlen(cp);
67 const char *space_map = NULL;
70 while ((len = (cp_end - cp)) > 0)
72 map = zebra_maps_input(zh->reg->zebra_maps, reg_type, &cp, len, 0);
73 if (**map == *CHR_SPACE)
78 for (src = space_map; *src; src++)
81 for (src = *map; *src; src++)
90 static void count_set(ZebraHandle zh, RSET rset, zint *count)
96 yaz_log(YLOG_DEBUG, "count_set");
98 rset->hits_limit = zh->approx_limit;
101 rfd = rset_open(rset, RSETF_READ);
102 while (rset_read(rfd, &key,0 /* never mind terms */))
104 if (key.mem[0] != psysno)
107 if (rfd->counted_items >= rset->hits_limit)
112 *count = rset->hits_count;
115 static int scan_handle (char *name, const char *info, int pos, void *client)
118 struct scan_info *scan_info = (struct scan_info *) client;
120 len_prefix = strlen(scan_info->prefix);
121 if (memcmp (name, scan_info->prefix, len_prefix))
124 idx = scan_info->after - pos + scan_info->before;
128 /* skip special terms.. of no interest */
129 if (name[len_prefix] < 4)
134 scan_info->list[idx].term = (char *)
135 odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
136 strcpy(scan_info->list[idx].term, name + len_prefix);
137 assert (*info == sizeof(ISAM_P));
138 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
143 #define RPN_MAX_ORDS 32
145 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
146 oid_value attributeset,
147 int num_bases, char **basenames,
148 int *position, int *num_entries, ZebraScanEntry **list,
149 int *is_partial, RSET limit_set, int return_zero)
153 int num = *num_entries;
157 char termz[IT_MAX_WORD+20];
158 struct scan_info *scan_info_array;
159 ZebraScanEntry *glist;
160 int ords[RPN_MAX_ORDS], ord_no = 0;
161 int ptr[RPN_MAX_ORDS];
164 char *search_type = NULL;
168 NMEM rset_nmem = NULL;
169 struct rset_key_control *kc = 0;
174 if (attributeset == VAL_NONE)
175 attributeset = VAL_BIB1;
180 int termset_value_numeric;
181 const char *termset_value_string;
182 attr_init_APT(&termset, zapt, 8);
183 termset_value_numeric =
184 attr_find_ex(&termset, NULL, &termset_value_string);
185 if (termset_value_numeric != -1)
188 const char *termset_name = 0;
190 if (termset_value_numeric != -2)
193 sprintf(resname, "%d", termset_value_numeric);
194 termset_name = resname;
197 termset_name = termset_value_string;
199 limit_set = resultSetRef (zh, termset_name);
203 yaz_log(YLOG_DEBUG, "position = %d, num = %d set=%d",
204 pos, num, attributeset);
206 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
207 rank_type, &complete_flag, &sort_flag))
210 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
213 for (base_no = 0; base_no < num_bases && ord_no < RPN_MAX_ORDS; base_no++)
217 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
219 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
224 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
227 ords[ord_no++] = ord;
234 /* prepare dictionary scanning */
246 yaz_log(YLOG_DEBUG, "rpn_scan pos=%d num=%d before=%d "
247 "after=%d before+after=%d",
248 pos, num, before, after, before+after);
249 scan_info_array = (struct scan_info *)
250 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
251 for (i = 0; i < ord_no; i++)
253 int j, prefix_len = 0;
254 int before_tmp = before, after_tmp = after;
255 struct scan_info *scan_info = scan_info_array + i;
256 struct rpn_char_map_info rcmi;
258 rpn_char_map_prepare (zh->reg, index_type, &rcmi);
260 scan_info->before = before;
261 scan_info->after = after;
262 scan_info->odr = stream;
264 scan_info->list = (struct scan_info_entry *)
265 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
266 for (j = 0; j<before+after; j++)
267 scan_info->list[j].term = NULL;
269 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
270 termz[prefix_len] = 0;
271 strcpy(scan_info->prefix, termz);
273 if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) ==
277 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
278 scan_info, scan_handle);
280 glist = (ZebraScanEntry *)
281 odr_malloc(stream, (before+after)*sizeof(*glist));
283 rset_nmem = nmem_create();
284 kc = zebra_key_control_create(zh);
286 /* consider terms after main term */
287 for (i = 0; i < ord_no; i++)
291 for (i = 0; i<after; i++)
294 const char *mterm = NULL;
297 int lo = i + pos-1; /* offset in result list */
299 /* find: j0 is the first of the minimal values */
300 for (j = 0; j < ord_no; j++)
302 if (ptr[j] < before+after && ptr[j] >= 0 &&
303 (tst = scan_info_array[j].list[ptr[j]].term) &&
304 (!mterm || strcmp (tst, mterm) < 0))
311 break; /* no value found, stop */
313 /* get result set for first one , but only if it's within bounds */
316 /* get result set for first term */
317 zebra_term_untrans_iconv(zh, stream->mem, index_type,
318 &glist[lo].term, mterm);
319 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
320 glist[lo].term, strlen(glist[lo].term),
321 NULL, 0, zapt->term->which, rset_nmem,
322 kc, kc->scope, 0, index_type, 0 /* hits_limit */,
323 0 /* term_ref_id_str */);
325 ptr[j0]++; /* move index for this set .. */
326 /* get result set for remaining scan terms */
327 for (j = j0+1; j<ord_no; j++)
329 if (ptr[j] < before+after && ptr[j] >= 0 &&
330 (tst = scan_info_array[j].list[ptr[j]].term) &&
331 !strcmp (tst, mterm))
340 zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
342 strlen(glist[lo].term), NULL, 0,
343 zapt->term->which,rset_nmem,
344 kc, kc->scope, 0, index_type, 0 /* hits_limit */,
345 0 /* term_ref_id_str */ );
346 rset = rset_create_or(rset_nmem, kc,
347 kc->scope, 0 /* termid */,
356 /* merge with limit_set if given */
361 rsets[1] = rset_dup(limit_set);
363 rset = rset_create_and(rset_nmem, kc, kc->scope, 2, rsets);
366 count_set(zh, rset, &count);
367 glist[lo].occurrences = count;
373 *num_entries -= (after-i);
375 if (*num_entries < 0)
378 nmem_destroy(rset_nmem);
383 /* consider terms before main term */
384 for (i = 0; i<ord_no; i++)
387 for (i = 0; i<before; i++)
390 const char *mterm = NULL;
393 int lo = before-1-i; /* offset in result list */
396 for (j = 0; j <ord_no; j++)
398 if (ptr[j] < before && ptr[j] >= 0 &&
399 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
400 (!mterm || strcmp (tst, mterm) > 0))
409 zebra_term_untrans_iconv(zh, stream->mem, index_type,
410 &glist[lo].term, mterm);
413 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
414 glist[lo].term, strlen(glist[lo].term),
415 NULL, 0, zapt->term->which, rset_nmem,
416 kc, kc->scope, 0, index_type, 0 /* hits_limit */,
417 0 /* term_ref_id_str */);
421 for (j = j0+1; j<ord_no; j++)
423 if (ptr[j] < before && ptr[j] >= 0 &&
424 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
425 !strcmp (tst, mterm))
430 rsets[1] = rset_trunc(
432 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
434 strlen(glist[lo].term), NULL, 0,
435 zapt->term->which, rset_nmem,
436 kc, kc->scope, 0, index_type, 0 /* hits_limit */,
437 0 /* term_ref_id_str */);
438 rset = rset_create_or(rset_nmem, kc,
439 kc->scope, 0 /* termid */, 2, rsets);
448 rsets[1] = rset_dup(limit_set);
450 rset = rset_create_and(rset_nmem, kc, kc->scope, 2, rsets);
452 count_set(zh, rset, &count);
453 glist[lo].occurrences = count;
457 nmem_destroy(rset_nmem);
464 if (*num_entries <= 0)
471 *list = glist + i; /* list is set to first 'real' entry */
473 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
474 *position, *num_entries);
481 * indent-tabs-mode: nil
483 * vim: shiftwidth=4 tabstop=8 expandtab