1 /* $Id: rpnscan.c,v 1.2 2006-09-21 10:10:07 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 static ZEBRA_RES rpn_scan_ver1(ZebraHandle zh, ODR stream,
146 Z_AttributesPlusTerm *zapt,
147 int *position, int *num_entries,
148 ZebraScanEntry **list,
149 int *is_partial, RSET limit_set,
151 int index_type, int ord_no, int *ords)
154 int num = *num_entries;
158 struct scan_info *scan_info_array;
159 char termz[IT_MAX_WORD+20];
160 ZebraScanEntry *glist;
162 struct rset_key_control *kc = 0;
163 int ptr[RPN_MAX_ORDS];
171 yaz_log(YLOG_DEBUG, "rpn_scan pos=%d num=%d before=%d "
172 "after=%d before+after=%d",
173 pos, num, before, after, before+after);
174 scan_info_array = (struct scan_info *)
175 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
176 for (i = 0; i < ord_no; i++)
178 int j, prefix_len = 0;
179 int before_tmp = before, after_tmp = after;
180 struct scan_info *scan_info = scan_info_array + i;
181 struct rpn_char_map_info rcmi;
183 rpn_char_map_prepare (zh->reg, index_type, &rcmi);
185 scan_info->before = before;
186 scan_info->after = after;
187 scan_info->odr = stream;
189 scan_info->list = (struct scan_info_entry *)
190 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
191 for (j = 0; j<before+after; j++)
192 scan_info->list[j].term = NULL;
194 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
195 termz[prefix_len] = 0;
196 strcpy(scan_info->prefix, termz);
198 if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) ==
202 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
203 scan_info, scan_handle);
205 glist = (ZebraScanEntry *)
206 odr_malloc(stream, (before+after)*sizeof(*glist));
208 rset_nmem = nmem_create();
209 kc = zebra_key_control_create(zh);
211 /* consider terms after main term */
212 for (i = 0; i < ord_no; i++)
216 for (i = 0; i<after; i++)
219 const char *mterm = NULL;
222 int lo = i + pos-1; /* offset in result list */
224 /* find: j0 is the first of the minimal values */
225 for (j = 0; j < ord_no; j++)
227 if (ptr[j] < before+after && ptr[j] >= 0 &&
228 (tst = scan_info_array[j].list[ptr[j]].term) &&
229 (!mterm || strcmp (tst, mterm) < 0))
236 break; /* no value found, stop */
238 /* get result set for first one , but only if it's within bounds */
241 /* get result set for first term */
242 zebra_term_untrans_iconv(zh, stream->mem, index_type,
243 &glist[lo].term, mterm);
244 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
245 glist[lo].term, strlen(glist[lo].term),
246 NULL, 0, zapt->term->which, rset_nmem,
247 kc, kc->scope, 0, index_type, 0 /* hits_limit */,
248 0 /* term_ref_id_str */);
250 ptr[j0]++; /* move index for this set .. */
251 /* get result set for remaining scan terms */
252 for (j = j0+1; j<ord_no; j++)
254 if (ptr[j] < before+after && ptr[j] >= 0 &&
255 (tst = scan_info_array[j].list[ptr[j]].term) &&
256 !strcmp (tst, mterm))
265 zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
267 strlen(glist[lo].term), NULL, 0,
268 zapt->term->which,rset_nmem,
269 kc, kc->scope, 0, index_type, 0 /* hits_limit */,
270 0 /* term_ref_id_str */ );
271 rset = rset_create_or(rset_nmem, kc,
272 kc->scope, 0 /* termid */,
281 /* merge with limit_set if given */
286 rsets[1] = rset_dup(limit_set);
288 rset = rset_create_and(rset_nmem, kc, kc->scope, 2, rsets);
291 count_set(zh, rset, &count);
292 glist[lo].occurrences = count;
298 *num_entries -= (after-i);
300 if (*num_entries < 0)
303 nmem_destroy(rset_nmem);
308 /* consider terms before main term */
309 for (i = 0; i<ord_no; i++)
312 for (i = 0; i<before; i++)
315 const char *mterm = NULL;
318 int lo = before-1-i; /* offset in result list */
321 for (j = 0; j <ord_no; j++)
323 if (ptr[j] < before && ptr[j] >= 0 &&
324 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
325 (!mterm || strcmp (tst, mterm) > 0))
334 zebra_term_untrans_iconv(zh, stream->mem, index_type,
335 &glist[lo].term, mterm);
338 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
339 glist[lo].term, strlen(glist[lo].term),
340 NULL, 0, zapt->term->which, rset_nmem,
341 kc, kc->scope, 0, index_type, 0 /* hits_limit */,
342 0 /* term_ref_id_str */);
346 for (j = j0+1; j<ord_no; j++)
348 if (ptr[j] < before && ptr[j] >= 0 &&
349 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
350 !strcmp (tst, mterm))
355 rsets[1] = rset_trunc(
357 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
359 strlen(glist[lo].term), NULL, 0,
360 zapt->term->which, rset_nmem,
361 kc, kc->scope, 0, index_type, 0 /* hits_limit */,
362 0 /* term_ref_id_str */);
363 rset = rset_create_or(rset_nmem, kc,
364 kc->scope, 0 /* termid */, 2, rsets);
373 rsets[1] = rset_dup(limit_set);
375 rset = rset_create_and(rset_nmem, kc, kc->scope, 2, rsets);
377 count_set(zh, rset, &count);
378 glist[lo].occurrences = count;
382 nmem_destroy(rset_nmem);
389 if (*num_entries <= 0)
396 *list = glist + i; /* list is set to first 'real' entry */
398 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
399 *position, *num_entries);
404 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
405 oid_value attributeset,
406 int num_bases, char **basenames,
407 int *position, int *num_entries, ZebraScanEntry **list,
408 int *is_partial, RSET limit_set, int return_zero)
411 int ords[RPN_MAX_ORDS], ord_no = 0;
414 char *search_type = NULL;
422 if (attributeset == VAL_NONE)
423 attributeset = VAL_BIB1;
425 if (!limit_set) /* no limit set given already */
427 /* see if there is a @attr 8=set */
429 int termset_value_numeric;
430 const char *termset_value_string;
431 attr_init_APT(&termset, zapt, 8);
432 termset_value_numeric =
433 attr_find_ex(&termset, NULL, &termset_value_string);
434 if (termset_value_numeric != -1)
437 const char *termset_name = 0;
439 if (termset_value_numeric != -2)
442 sprintf(resname, "%d", termset_value_numeric);
443 termset_name = resname;
446 termset_name = termset_value_string;
448 limit_set = resultSetRef (zh, termset_name);
452 yaz_log(YLOG_DEBUG, "position = %d, num = %d set=%d",
453 *position, *num_entries, attributeset);
455 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
456 rank_type, &complete_flag, &sort_flag))
459 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
462 if (num_bases > RPN_MAX_ORDS)
464 zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
468 for (base_no = 0; base_no < num_bases; base_no++)
472 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
474 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
479 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
482 ords[ord_no++] = ord;
486 *num_entries = 0; /* zebra_apt_get_ord should set error reason */
489 /* prepare dictionary scanning */
490 if (*num_entries < 1)
495 return rpn_scan_ver1(zh, stream, zapt, position, num_entries, list,
496 is_partial, limit_set, return_zero,
497 index_type, ord_no, ords);
503 * indent-tabs-mode: nil
505 * vim: shiftwidth=4 tabstop=8 expandtab