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))
150 /* skip special terms such as first-in-field specials */
151 if (name[len_prefix] < CHR_BASE_CHAR)
154 wrbuf_rewind(scan_info->term);
155 wrbuf_puts(scan_info->term, name+len_prefix);
157 assert(*info == sizeof(ISAM_P));
158 memcpy(&scan_info->isam_p, info+1, sizeof(ISAM_P));
163 static int scan_save_set(ZebraHandle zh, ODR stream, NMEM nmem,
164 struct rset_key_control *kc,
165 Z_AttributesPlusTerm *zapt,
168 const char *index_type,
169 struct scan2_info_entry *ar, int ord_no,
170 ZebraScanEntry *glist, int pos)
174 zint approx_limit = zh->approx_limit;
175 AttrType global_hits_limit_attr;
177 attr_init_APT(&global_hits_limit_attr, zapt, 12);
179 l = attr_find(&global_hits_limit_attr, NULL);
183 for (i = 0; i < ord_no; i++)
185 if (ar[i].isam_p && strcmp(wrbuf_cstr(ar[i].term), term) == 0)
187 struct ord_list *ol = ord_list_create(nmem);
190 ol = ord_list_append(nmem, ol, ar[i].ord);
194 zh, &ar[i].isam_p, 1,
195 wrbuf_buf(ar[i].term), wrbuf_len(ar[i].term),
196 NULL, 1, zapt->term->which, nmem,
197 kc, kc->scope, ol, index_type,
198 0 /* hits_limit_value */,
199 0 /* term_ref_id_str */);
208 rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */,
217 /* merge with limit_set if given */
222 rsets[1] = rset_dup(limit_set);
224 rset = rset_create_and(nmem, kc, kc->scope, 2, rsets);
227 zebra_count_set(zh, rset, &count, approx_limit);
232 zebra_snippets *hit_snippets = zebra_snippets_create();
235 glist[pos].display_term = 0;
237 get_first_snippet_from_rset(zh, rset, hit_snippets, &sysno);
240 zebra_snippets *rec_snippets = zebra_snippets_create();
241 int code = zebra_get_rec_snippets(zh, sysno, rec_snippets);
244 const struct zebra_snippet_word *w =
245 zebra_snippets_lookup(rec_snippets, hit_snippets);
248 glist[pos].display_term = odr_strdup(stream, w->term);
252 yaz_log(YLOG_WARN, "zebra_snippets_lookup failed for pos=%d", pos);
255 zebra_snippets_destroy(rec_snippets);
257 if (zebra_term_untrans_iconv(zh, stream->mem, index_type,
258 &glist[pos].term, term))
260 /* failed.. use display_term instead (which could be 0) */
261 glist[pos].term = glist[pos].display_term;
264 if (!glist[pos].term)
266 yaz_log(YLOG_WARN, "Could not generate scan term for pos=%d",
268 glist[pos].term = "None";
270 glist[pos].occurrences = count;
271 zebra_snippets_destroy(hit_snippets);
282 static ZEBRA_RES rpn_scan_norm(ZebraHandle zh, ODR stream, NMEM nmem,
283 struct rset_key_control *kc,
284 Z_AttributesPlusTerm *zapt,
285 int *position, int *num_entries,
286 ZebraScanEntry **list,
287 int *is_partial, RSET limit_set,
288 const char *index_type,
289 int ord_no, int *ords)
291 struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no);
292 struct rpn_char_map_info rcmi;
293 zebra_map_t zm = zebra_map_get_or_add(zh->reg->zebra_maps, index_type);
298 ZebraScanEntry *glist = (ZebraScanEntry *)
299 odr_malloc(stream, *num_entries * sizeof(*glist));
302 if (*position > *num_entries+1)
309 rpn_char_map_prepare(zh->reg, zm, &rcmi);
311 for (i = 0; i < ord_no; i++)
312 ar[i].term = wrbuf_alloc();
314 for (i = 0; i < ord_no; i++)
316 char termz[IT_MAX_WORD+20];
319 prefix_len = key_SU_encode(ords[i], termz);
320 termz[prefix_len] = 0;
321 strcpy(ar[i].prefix, termz);
323 if (trans_scan_term(zh, zapt, termz+prefix_len, zm) ==
326 for (i = 0; i < ord_no; i++)
327 wrbuf_destroy(ar[i].term);
330 wrbuf_rewind(ar[i].term);
331 wrbuf_puts(ar[i].term, termz + prefix_len);
335 /** deal with terms before position .. */
336 /* the glist index starts at zero (unlike scan positions */
337 for (pos = *position-2; pos >= 0; )
341 /* scan on all maximum terms */
342 for (i = 0; i < ord_no; i++)
344 if (ar[i].isam_p == 0)
346 char termz[IT_MAX_WORD+20];
350 ar[i].pos_to_save = -1;
352 strcpy(termz, ar[i].prefix);
353 strcat(termz, wrbuf_cstr(ar[i].term));
354 dict_scan(zh->reg->dict, termz, &before, &after,
358 /* get maximum after scan */
359 for (i = 0; i < ord_no; i++)
362 && (hi == 0 || strcmp(wrbuf_cstr(ar[i].term), hi) > 0))
363 hi = wrbuf_cstr(ar[i].term);
367 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, hi,
368 index_type, ar, ord_no, glist,
369 (pos >= 0 && pos < *num_entries) ? pos : -1))
372 /* see if we got all terms before.. */
376 /* did not get all terms; adjust the real position and reduce
378 yaz_log(YLOG_LOG, "before terms dif=%d", dif);
384 for (i = 0; i < ord_no; i++)
386 char termz[IT_MAX_WORD+20];
389 prefix_len = key_SU_encode(ords[i], termz);
390 termz[prefix_len] = 0;
391 strcpy(ar[i].prefix, termz);
393 if (trans_scan_term(zh, zapt, termz+prefix_len, zm) ==
396 wrbuf_rewind(ar[i].term);
397 wrbuf_puts(ar[i].term, termz + prefix_len);
402 after_pos = 1; /* immediate term first.. */
403 for (pos = *position-1; pos < *num_entries; )
407 /* scan on all minimum terms */
408 for (i = 0; i < ord_no; i++)
410 if (ar[i].isam_p == 0)
412 char termz[IT_MAX_WORD+20];
414 int after = after_pos;
416 ar[i].pos_to_save = 1;
418 strcpy(termz, ar[i].prefix);
419 strcat(termz, wrbuf_cstr(ar[i].term));
420 dict_scan(zh->reg->dict, termz, &before, &after,
424 after_pos = 2; /* next round we grab following term */
426 /* get minimum after scan */
427 for (i = 0; i < ord_no; i++)
430 && (lo == 0 || strcmp(wrbuf_cstr(ar[i].term), lo) < 0))
431 lo = wrbuf_cstr(ar[i].term);
435 if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, lo,
436 index_type, ar, ord_no, glist,
437 (pos >= 0 && pos < *num_entries) ? pos : -1))
441 if (pos != *num_entries)
452 for (i = 0; i < ord_no; i++)
453 wrbuf_destroy(ar[i].term);
458 struct scan1_info_entry {
464 struct scan1_info_entry *list;
470 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
471 const Odr_oid *attributeset,
472 int num_bases, char **basenames,
473 int *position, int *num_entries, ZebraScanEntry **list,
474 int *is_partial, const char *set_name)
477 int ords[RPN_MAX_ORDS], ord_no = 0;
479 const char *index_type;
480 char *search_type = NULL;
486 struct rset_key_control *kc = 0;
493 attributeset = yaz_oid_attset_bib_1;
497 /* see if there is a @attr 8=set */
499 int termset_value_numeric;
500 const char *termset_value_string = 0;
501 attr_init_APT(&termset, zapt, 8);
502 termset_value_numeric =
503 attr_find_ex(&termset, NULL, &termset_value_string);
504 if (termset_value_numeric != -1)
506 if (termset_value_numeric != -2)
509 sprintf(resname, "%d", termset_value_numeric);
510 set_name = odr_strdup(stream, resname);
513 set_name = odr_strdup(stream, termset_value_string);
519 limit_set = resultSetRef(zh, set_name);
524 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
530 yaz_log(YLOG_DEBUG, "position = %d, num = %d",
531 *position, *num_entries);
533 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, &index_type, &search_type,
534 rank_type, &complete_flag, &sort_flag))
537 zebra_setError(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE, 0);
540 if (num_bases > RPN_MAX_ORDS)
542 zebra_setError(zh, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
545 for (base_no = 0; base_no < num_bases; base_no++)
549 if (zebraExplain_curDatabase(zh->reg->zei, basenames[base_no]))
551 zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
556 if (zebra_apt_get_ord(zh, zapt, index_type, 0, attributeset, &ord)
559 ords[ord_no++] = ord;
563 *num_entries = 0; /* zebra_apt_get_ord should set error reason */
566 if (*num_entries < 1)
571 nmem = nmem_create();
572 kc = zebra_key_control_create(zh);
574 res = rpn_scan_norm(zh, stream, nmem, kc, zapt, position, num_entries,
575 list, is_partial, limit_set,
576 index_type, ord_no, ords);
585 * c-file-style: "Stroustrup"
586 * indent-tabs-mode: nil
588 * vim: shiftwidth=4 tabstop=8 expandtab