1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <yaz/diagbib1.h>
32 #include <zebra_xpath.h>
33 #include <yaz/wrbuf.h>
37 #include <yaz/oid_db.h>
39 #define RPN_MAX_ORDS 32
41 /* convert APT SCAN term to internal cmap */
42 static ZEBRA_RES trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
43 char *termz, zebra_map_t zm)
45 char term_utf8[IT_MAX_WORD];
47 if (zapt_term_to_utf8(zh, zapt, term_utf8) == ZEBRA_FAIL)
48 return ZEBRA_FAIL; /* error */
49 else if (zebra_maps_is_icu(zm))
53 zebra_map_tokenize_start(zm, term_utf8, strlen(term_utf8));
55 if (zebra_map_tokenize_next(zm, &res_buf, &res_len, 0, 0))
57 memcpy(termz, res_buf, res_len);
58 termz[res_len] = '\0';
66 const char *cp = (const char *) term_utf8;
67 const char *cp_end = cp + strlen(cp);
70 const char *space_map = NULL;
73 while ((len = (cp_end - cp)) > 0)
75 map = zebra_maps_input(zm, &cp, len, 0);
76 if (**map == *CHR_SPACE)
81 for (src = space_map; *src; src++)
84 for (src = *map; *src; src++)
93 static void get_first_snippet_from_rset(ZebraHandle zh,
94 RSET rset, zebra_snippets *snippets,
100 size_t sysno_mem_index = 0;
102 if (zh->m_staticrank)
105 yaz_log(YLOG_DEBUG, "get_first_snippet_from_rset");
107 rfd = rset_open(rset, RSETF_READ);
109 while (rset_read(rfd, &key, &termid))
111 if (key.mem[sysno_mem_index] != *sysno)
115 *sysno = key.mem[sysno_mem_index];
120 for (ol = termid->ol; ol; ol = ol->next)
122 zebra_snippets_append(snippets, key.mem[key.len-1], 0,
123 ol->ord, termid->name);
130 struct scan2_info_entry {
138 static int scan_handle2(char *name, const char *info, int pos, void *client)
141 struct scan2_info_entry *scan_info = (struct scan2_info_entry *) client;
143 if (scan_info->pos_to_save != pos)
146 len_prefix = strlen(scan_info->prefix);
147 if (memcmp(name, scan_info->prefix, len_prefix))
149 wrbuf_rewind(scan_info->term);
150 wrbuf_puts(scan_info->term, name+len_prefix);
152 assert(*info == sizeof(ISAM_P));
153 memcpy(&scan_info->isam_p, info+1, sizeof(ISAM_P));
158 static int scan_save_set(ZebraHandle zh, ODR stream, NMEM nmem,
159 struct rset_key_control *kc,
160 Z_AttributesPlusTerm *zapt,
163 const char *index_type,
164 struct scan2_info_entry *ar, int ord_no,
165 ZebraScanEntry *glist, int pos)
169 zint approx_limit = zh->approx_limit;
170 AttrType global_hits_limit_attr;
172 attr_init_APT(&global_hits_limit_attr, zapt, 12);
174 l = attr_find(&global_hits_limit_attr, NULL);
178 for (i = 0; i < ord_no; i++)
180 if (ar[i].isam_p && strcmp(wrbuf_cstr(ar[i].term), term) == 0)
182 if (strcmp(term, FIRST_IN_FIELD_STR))
184 struct ord_list *ol = ord_list_create(nmem);
187 ol = ord_list_append(nmem, ol, ar[i].ord);
191 zh, &ar[i].isam_p, 1,
192 wrbuf_buf(ar[i].term), wrbuf_len(ar[i].term),
193 NULL, 1, zapt->term->which, nmem,
194 kc, kc->scope, ol, index_type,
195 0 /* hits_limit_value */,
196 0 /* term_ref_id_str */);
205 rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */,
215 /* merge with limit_set if given */
220 rsets[1] = rset_dup(limit_set);
222 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
225 zebra_count_set(zh, rset, &count, approx_limit);
230 zebra_snippets *hit_snippets = zebra_snippets_create();
233 glist[pos].display_term = 0;
235 get_first_snippet_from_rset(zh, rset, hit_snippets, &sysno);
238 zebra_snippets *rec_snippets = zebra_snippets_create();
239 int code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
242 const struct zebra_snippet_word *w =
243 zebra_snippets_lookup(rec_snippets, hit_snippets);
246 glist[pos].display_term = odr_strdup(stream, w->term);
250 yaz_log(YLOG_WARN, "zebra_snippets_lookup failed for pos=%d", pos);
253 zebra_snippets_destroy(rec_snippets);
255 if (zebra_term_untrans_iconv(zh, stream->mem, index_type,
256 &glist[pos].term, term))
258 /* failed.. use display_term instead (which could be 0) */
259 glist[pos].term = glist[pos].display_term;
262 if (!glist[pos].term)
264 yaz_log(YLOG_WARN, "Could not generate scan term for pos=%d",
266 glist[pos].term = "None";
268 glist[pos].occurrences = count;
269 zebra_snippets_destroy(hit_snippets);
280 static ZEBRA_RES rpn_scan_norm(ZebraHandle zh, ODR stream, NMEM nmem,
281 struct rset_key_control *kc,
282 Z_AttributesPlusTerm *zapt,
283 int *position, int *num_entries,
284 ZebraScanEntry **list,
285 int *is_partial, RSET limit_set,
286 const char *index_type,
287 int ord_no, int *ords)
289 struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no);
290 struct rpn_char_map_info rcmi;
291 zebra_map_t zm = zebra_map_get_or_add(zh->reg->zebra_maps, index_type);
296 ZebraScanEntry *glist = (ZebraScanEntry *)
297 odr_malloc(stream, *num_entries * sizeof(*glist));
300 if (*position > *num_entries+1)
307 rpn_char_map_prepare(zh->reg, zm, &rcmi);
309 for (i = 0; i < ord_no; i++)
310 ar[i].term = wrbuf_alloc();
312 for (i = 0; i < ord_no; i++)
314 char termz[IT_MAX_WORD+20];
317 prefix_len = key_SU_encode(ords[i], termz);
318 termz[prefix_len] = 0;
319 strcpy(ar[i].prefix, termz);
321 if (trans_scan_term(zh, zapt, termz+prefix_len, zm) ==
324 for (i = 0; i < ord_no; i++)
325 wrbuf_destroy(ar[i].term);
328 wrbuf_rewind(ar[i].term);
329 wrbuf_puts(ar[i].term, termz + prefix_len);
333 /** deal with terms before position .. */
334 /* the glist index starts at zero (unlike scan positions */
335 for (pos = *position-2; pos >= 0; )
339 /* scan on all maximum terms */
340 for (i = 0; i < ord_no; i++)
342 if (ar[i].isam_p == 0)
344 char termz[IT_MAX_WORD+20];
348 ar[i].pos_to_save = -1;
350 strcpy(termz, ar[i].prefix);
351 strcat(termz, wrbuf_cstr(ar[i].term));
352 dict_scan(zh->reg->dict, termz, &before, &after,
356 /* get maximum after scan */
357 for (i = 0; i < ord_no; i++)
360 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
361 hi = wrbuf_cstr(ar[i].term);
365 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, hi,
366 index_type, ar, ord_no, glist,
367 (pos >= 0 && pos < *num_entries) ? pos : -1))
370 /* see if we got all terms before.. */
374 /* did not get all terms; adjust the real position and reduce
381 for (i = 0; i < ord_no; i++)
383 char termz[IT_MAX_WORD+20];
386 prefix_len = key_SU_encode(ords[i], termz);
387 termz[prefix_len] = 0;
388 strcpy(ar[i].prefix, termz);
390 if (trans_scan_term(zh, zapt, termz+prefix_len, zm) ==
393 wrbuf_rewind(ar[i].term);
394 wrbuf_puts(ar[i].term, termz + prefix_len);
399 after_pos = 1; /* immediate term first.. */
400 for (pos = *position-1; pos < *num_entries; )
404 /* scan on all minimum terms */
405 for (i = 0; i < ord_no; i++)
407 if (ar[i].isam_p == 0)
409 char termz[IT_MAX_WORD+20];
411 int after = after_pos;
413 ar[i].pos_to_save = 1;
415 strcpy(termz, ar[i].prefix);
416 strcat(termz, wrbuf_cstr(ar[i].term));
417 dict_scan(zh->reg->dict, termz, &before, &after,
421 after_pos = 2; /* next round we grab following term */
423 /* get minimum after scan */
424 for (i = 0; i < ord_no; i++)
427 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
428 lo = wrbuf_cstr(ar[i].term);
432 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, lo,
433 index_type, ar, ord_no, glist,
434 (pos >= 0 && pos < *num_entries) ? pos : -1))
438 if (pos != *num_entries)
449 for (i = 0; i < ord_no; i++)
450 wrbuf_destroy(ar[i].term);
455 struct scan1_info_entry {
461 struct scan1_info_entry *list;
467 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
468 const Odr_oid *attributeset,
469 int num_bases, char **basenames,
470 int *position, int *num_entries, ZebraScanEntry **list,
471 int *is_partial, const char *set_name)
474 int ords[RPN_MAX_ORDS], ord_no = 0;
476 const char *index_type;
477 char *search_type = NULL;
483 struct rset_key_control *kc = 0;
490 attributeset = yaz_oid_attset_bib_1;
494 /* see if there is a @attr 8=set */
496 int termset_value_numeric;
497 const char *termset_value_string = 0;
498 attr_init_APT(&termset, zapt, 8);
499 termset_value_numeric =
500 attr_find_ex(&termset, NULL, &termset_value_string);
501 if (termset_value_numeric != -1)
503 if (termset_value_numeric != -2)
506 sprintf(resname, "%d", termset_value_numeric);
507 set_name = odr_strdup(stream, resname);
510 set_name = odr_strdup(stream, termset_value_string);
516 limit_set = resultSetRef(zh, set_name);
521 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
527 yaz_log(YLOG_DEBUG, "position = %d, num = %d",
528 *position, *num_entries);
530 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
531 rank_type, &complete_flag, &sort_flag))
534 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
537 if (num_bases > RPN_MAX_ORDS)
539 zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
542 for (base_no = 0; base_no < num_bases; base_no++)
546 if (zebraExplain_curDatabase(zh->reg->zei, basenames[base_no]))
548 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
553 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
556 ords[ord_no++] = ord;
560 *num_entries = 0; /* zebra_apt_get_ord should set error reason */
563 if (*num_entries < 1)
568 nmem = nmem_create();
569 kc = zebra_key_control_create(zh);
571 res = rpn_scan_norm(zh, stream, nmem, kc, zapt, position, num_entries,
572 list, is_partial, limit_set,
573 index_type, ord_no, ords);
582 * c-file-style: "Stroustrup"
583 * indent-tabs-mode: nil
585 * vim: shiftwidth=4 tabstop=8 expandtab