From 918c8b1ec479083d82c390d5dceb4899654cb666 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 21 Sep 2006 20:22:33 +0000 Subject: [PATCH] Skip 0 hit counts in scan. --- include/charmap.h | 5 +- index/index.h | 12 +- index/rpnscan.c | 331 +++++++++++++++++++++++++++++++++++++++++++++++------ index/zebraapi.c | 10 +- util/charmap.c | 4 +- 5 files changed, 312 insertions(+), 50 deletions(-) diff --git a/include/charmap.h b/include/charmap.h index 4ea931a..e4cc08e 100644 --- a/include/charmap.h +++ b/include/charmap.h @@ -1,4 +1,4 @@ -/* $Id: charmap.h,v 1.16 2006-08-14 10:40:12 adam Exp $ +/* $Id: charmap.h,v 1.17 2006-09-21 20:22:33 adam Exp $ Copyright (C) 1995-2006 Index Data ApS @@ -32,6 +32,9 @@ YAZ_EXPORT extern const char *CHR_SPACE; YAZ_EXPORT extern const char *CHR_CUT; YAZ_EXPORT extern const char *CHR_BASE; +/* defines first char we map to (0, 1, .. are specials) */ +#define CHR_BASE_CHAR 5 + struct chr_t_entry; typedef struct chr_t_entry chr_t_entry; diff --git a/index/index.h b/index/index.h index 3fb6b1d..985e1b5 100644 --- a/index/index.h +++ b/index/index.h @@ -1,4 +1,4 @@ -/* $Id: index.h,v 1.177 2006-09-21 08:39:14 adam Exp $ +/* $Id: index.h,v 1.178 2006-09-21 20:22:34 adam Exp $ Copyright (C) 1995-2006 Index Data ApS @@ -299,11 +299,11 @@ ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs, int num_bases, char **basenames, RSET *result_set); -ZEBRA_RES rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, - oid_value attributeset, - int num_bases, char **basenames, - int *position, int *num_entries, ZebraScanEntry **list, - int *is_partial, RSET limit_set, int return_zero); +ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, + oid_value attributeset, + int num_bases, char **basenames, + int *position, int *num_entries, ZebraScanEntry **list, + int *is_partial, RSET limit_set); RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no, const char *term, int length_term, const char *flags, diff --git a/index/rpnscan.c b/index/rpnscan.c index 487075f..02bf5da 100644 --- a/index/rpnscan.c +++ b/index/rpnscan.c @@ -1,4 +1,4 @@ -/* $Id: rpnscan.c,v 1.2 2006-09-21 10:10:07 adam Exp $ +/* $Id: rpnscan.c,v 1.3 2006-09-21 20:22:34 adam Exp $ Copyright (C) 1995-2006 Index Data ApS @@ -33,21 +33,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include "index.h" #include +#include #include #include #include -struct scan_info_entry { - char *term; - ISAM_P isam_p; -}; +#define RPN_MAX_ORDS 32 -struct scan_info { - struct scan_info_entry *list; - ODR odr; - int before, after; - char prefix[20]; -}; +int log_scan = YLOG_LOG; /* convert APT SCAN term to internal cmap */ static ZEBRA_RES trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt, @@ -112,7 +105,277 @@ static void count_set(ZebraHandle zh, RSET rset, zint *count) *count = rset->hits_count; } -static int scan_handle (char *name, const char *info, int pos, void *client) +struct scan2_info_entry { + WRBUF term; + char prefix[20]; + ISAM_P isam_p; + int pos_to_save; +}; + +static int scan_handle2(char *name, const char *info, int pos, void *client) +{ + int len_prefix; + struct scan2_info_entry *scan_info = (struct scan2_info_entry *) client; + + if (scan_info->pos_to_save != pos) + return 0; + + len_prefix = strlen(scan_info->prefix); + if (memcmp (name, scan_info->prefix, len_prefix)) + return 1; + + /* skip special terms such as first-in-field specials */ + if (name[len_prefix] < CHR_BASE_CHAR) + return 1; + + wrbuf_rewind(scan_info->term); + wrbuf_puts(scan_info->term, name+len_prefix); + + assert (*info == sizeof(ISAM_P)); + memcpy (&scan_info->isam_p, info+1, sizeof(ISAM_P)); + return 0; +} + + +static int scan_save_set(ZebraHandle zh, ODR stream, NMEM nmem, + struct rset_key_control *kc, + Z_AttributesPlusTerm *zapt, + RSET limit_set, + const char *term, + int index_type, + struct scan2_info_entry *ar, int ord_no, + ZebraScanEntry *glist, int pos) +{ + int i; + RSET rset = 0; + for (i = 0; i < ord_no; i++) + { + if (ar[i].isam_p && strcmp(wrbuf_buf(ar[i].term), term) == 0) + { + RSET rset_t = rset_trunc( + zh, &ar[i].isam_p, 1, + wrbuf_buf(ar[i].term), wrbuf_len(ar[i].term), + NULL, 0, zapt->term->which, nmem, + kc, kc->scope, 0, index_type, + 0 /* hits_limit */, + 0 /* term_ref_id_str */); + if (!rset) + rset = rset_t; + else + { + RSET rsets[2]; + + rsets[0] = rset; + rsets[1] = rset_t; + rset = rset_create_or(nmem, kc, kc->scope, 0 /* termid */, + 2, rsets); + } + ar[i].isam_p = 0; + } + } + if (rset) + { + zint count; + /* merge with limit_set if given */ + if (limit_set) + { + RSET rsets[2]; + rsets[0] = rset; + rsets[1] = rset_dup(limit_set); + + rset = rset_create_and(nmem, kc, kc->scope, 2, rsets); + } + /* count it */ + count_set(zh, rset, &count); + rset_delete(rset); + if (count > 0) + { + if (pos != -1) + { + zebra_term_untrans_iconv(zh, stream->mem, index_type, + &glist[pos].term, term); + glist[pos].occurrences = count; + } + return 1; + } + } + return 0; +} + +static ZEBRA_RES rpn_scan_ver2(ZebraHandle zh, ODR stream, NMEM nmem, + struct rset_key_control *kc, + Z_AttributesPlusTerm *zapt, + int *position, int *num_entries, + ZebraScanEntry **list, + int *is_partial, RSET limit_set, + int index_type, int ord_no, int *ords) +{ + struct scan2_info_entry *ar = nmem_malloc(nmem, sizeof(*ar) * ord_no); + struct rpn_char_map_info rcmi; + int i, dif; + int pos = 0; + + ZebraScanEntry *glist = (ZebraScanEntry *) + odr_malloc(stream, *num_entries * sizeof(*glist)); + + *is_partial = 0; + if (*position > *num_entries+1) + { + *is_partial = 1; + *position = 1; + *num_entries = 0; + return ZEBRA_OK; + } + rpn_char_map_prepare (zh->reg, index_type, &rcmi); + + for (i = 0; i < ord_no; i++) + ar[i].term = wrbuf_alloc(); + + for (i = 0; i < ord_no; i++) + { + char termz[IT_MAX_WORD+20]; + int prefix_len = 0; + + prefix_len = key_SU_encode (ords[i], termz); + termz[prefix_len] = 0; + strcpy(ar[i].prefix, termz); + + if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) == + ZEBRA_FAIL) + return ZEBRA_FAIL; + wrbuf_rewind(ar[i].term); + wrbuf_puts(ar[i].term, termz + prefix_len); + ar[i].isam_p = 0; + } + /** deal with terms before position .. */ + /* the glist index starts at zero (unlike scan positions */ + for (pos = *position-2; pos >= 0; ) + { + const char *hi = 0; + + /* scan on all maximum terms */ + for (i = 0; i < ord_no; i++) + { + if (ar[i].isam_p == 0) + { + char termz[IT_MAX_WORD+20]; + int before = 1; + int after = 0; + + ar[i].pos_to_save = -1; + + strcpy(termz, ar[i].prefix); + strcat(termz, wrbuf_buf(ar[i].term)); + dict_scan(zh->reg->dict, termz, &before, &after, + ar+i, scan_handle2); + } + } + /* get maximum after scan */ + for (i = 0; i < ord_no; i++) + { + if (ar[i].isam_p + && (hi == 0 || strcmp(wrbuf_buf(ar[i].term), hi) > 0)) + hi = wrbuf_buf(ar[i].term); + } + if (!hi) + break; + if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, hi, + index_type, ar, ord_no, glist, + (pos >= 0 && pos < *num_entries) ? pos : -1)) + --pos; + } + /* see if we got all terms before.. */ + dif = 1 + pos; + if (dif > 0) + { + /* did not get all terms; adjust the real position and reduce + number of entries */ + yaz_log(YLOG_LOG, "before terms dif=%d", dif); + glist = glist + dif; + *num_entries -= dif; + *position -= dif; + *is_partial = 1; + } + for (i = 0; i < ord_no; i++) + { + char termz[IT_MAX_WORD+20]; + int prefix_len = 0; + + prefix_len = key_SU_encode (ords[i], termz); + termz[prefix_len] = 0; + strcpy(ar[i].prefix, termz); + + if (trans_scan_term(zh, zapt, termz+prefix_len, index_type) == + ZEBRA_FAIL) + return ZEBRA_FAIL; + wrbuf_rewind(ar[i].term); + wrbuf_puts(ar[i].term, termz + prefix_len); + ar[i].isam_p = 0; + } + + for (pos = *position-1; pos < *num_entries; ) + { + const char *lo = 0; + + /* scan on all minimum terms */ + for (i = 0; i < ord_no; i++) + { + if (ar[i].isam_p == 0) + { + char termz[IT_MAX_WORD+20]; + int before = 0; + int after = (pos == *position-1) ? 1 : 2; + + ar[i].pos_to_save = 1; + + strcpy(termz, ar[i].prefix); + strcat(termz, wrbuf_buf(ar[i].term)); + dict_scan(zh->reg->dict, termz, &before, &after, + ar+i, scan_handle2); + } + } + /* get minimum after scan */ + for (i = 0; i < ord_no; i++) + { + if (ar[i].isam_p + && (lo == 0 || strcmp(wrbuf_buf(ar[i].term), lo) < 0)) + lo = wrbuf_buf(ar[i].term); + } + if (!lo) + break; + if (scan_save_set(zh, stream, nmem, kc, zapt, limit_set, lo, + index_type, ar, ord_no, glist, + (pos >= 0 && pos < *num_entries) ? pos : -1)) + pos++; + + } + if (pos != *num_entries) + { + if (pos >= 0) + *num_entries = pos; + else + *num_entries = 0; + *is_partial = 1; + } + + *list = glist; + + return ZEBRA_OK; +} + +struct scan1_info_entry { + char *term; + ISAM_P isam_p; +}; + +struct scan_info { + struct scan1_info_entry *list; + ODR odr; + int before, after; + char prefix[20]; +}; + +static int scan_handle1(char *name, const char *info, int pos, void *client) { int len_prefix, idx; struct scan_info *scan_info = (struct scan_info *) client; @@ -125,8 +388,8 @@ static int scan_handle (char *name, const char *info, int pos, void *client) else idx = - pos - 1; - /* skip special terms.. of no interest */ - if (name[len_prefix] < 4) + /* skip special terms such as first-in-field specials */ + if (name[len_prefix] < CHR_BASE_CHAR) return 1; if (idx < 0) @@ -139,15 +402,12 @@ static int scan_handle (char *name, const char *info, int pos, void *client) return 0; } - -#define RPN_MAX_ORDS 32 - -static ZEBRA_RES rpn_scan_ver1(ZebraHandle zh, ODR stream, +static ZEBRA_RES rpn_scan_ver1(ZebraHandle zh, ODR stream, NMEM rset_nmem, + struct rset_key_control *kc, Z_AttributesPlusTerm *zapt, int *position, int *num_entries, ZebraScanEntry **list, int *is_partial, RSET limit_set, - int return_zero, int index_type, int ord_no, int *ords) { int pos = *position; @@ -158,8 +418,6 @@ static ZEBRA_RES rpn_scan_ver1(ZebraHandle zh, ODR stream, struct scan_info *scan_info_array; char termz[IT_MAX_WORD+20]; ZebraScanEntry *glist; - NMEM rset_nmem = 0; - struct rset_key_control *kc = 0; int ptr[RPN_MAX_ORDS]; before = pos-1; @@ -186,7 +444,7 @@ static ZEBRA_RES rpn_scan_ver1(ZebraHandle zh, ODR stream, scan_info->after = after; scan_info->odr = stream; - scan_info->list = (struct scan_info_entry *) + scan_info->list = (struct scan1_info_entry *) odr_malloc(stream, (before+after) * sizeof(*scan_info->list)); for (j = 0; jlist[j].term = NULL; @@ -200,14 +458,11 @@ static ZEBRA_RES rpn_scan_ver1(ZebraHandle zh, ODR stream, return ZEBRA_FAIL; dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp, - scan_info, scan_handle); + scan_info, scan_handle1); } glist = (ZebraScanEntry *) odr_malloc(stream, (before+after)*sizeof(*glist)); - rset_nmem = nmem_create(); - kc = zebra_key_control_create(zh); - /* consider terms after main term */ for (i = 0; i < ord_no; i++) ptr[i] = before; @@ -299,8 +554,6 @@ static ZEBRA_RES rpn_scan_ver1(ZebraHandle zh, ODR stream, *is_partial = 1; if (*num_entries < 0) { - (*kc->dec)(kc); - nmem_destroy(rset_nmem); *num_entries = 0; return ZEBRA_OK; } @@ -378,8 +631,6 @@ static ZEBRA_RES rpn_scan_ver1(ZebraHandle zh, ODR stream, glist[lo].occurrences = count; rset_delete (rset); } - (*kc->dec)(kc); - nmem_destroy(rset_nmem); i = before-i; if (i) { @@ -405,7 +656,7 @@ ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, oid_value attributeset, int num_bases, char **basenames, int *position, int *num_entries, ZebraScanEntry **list, - int *is_partial, RSET limit_set, int return_zero) + int *is_partial, RSET limit_set) { int base_no; int ords[RPN_MAX_ORDS], ord_no = 0; @@ -415,6 +666,9 @@ ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, char rank_type[128]; int complete_flag; int sort_flag; + NMEM nmem; + ZEBRA_RES res; + struct rset_key_control *kc = 0; *list = 0; *is_partial = 0; @@ -486,15 +740,20 @@ ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, *num_entries = 0; /* zebra_apt_get_ord should set error reason */ return ZEBRA_FAIL; } - /* prepare dictionary scanning */ if (*num_entries < 1) { *num_entries = 0; - return ZEBRA_OK; + return ZEBRA_OK; } - return rpn_scan_ver1(zh, stream, zapt, position, num_entries, list, - is_partial, limit_set, return_zero, - index_type, ord_no, ords); + nmem = nmem_create(); + kc = zebra_key_control_create(zh); + + res = rpn_scan_ver2(zh, stream, nmem, kc, zapt, position, num_entries, + list, + is_partial, limit_set, index_type, ord_no, ords); + nmem_destroy(nmem); + (*kc->dec)(kc); + return res; } /* diff --git a/index/zebraapi.c b/index/zebraapi.c index 6b60956..c944c96 100644 --- a/index/zebraapi.c +++ b/index/zebraapi.c @@ -1,4 +1,4 @@ -/* $Id: zebraapi.c,v 1.227 2006-09-11 22:57:54 adam Exp $ +/* $Id: zebraapi.c,v 1.228 2006-09-21 20:22:34 adam Exp $ Copyright (C) 1995-2006 Index Data ApS @@ -1177,10 +1177,10 @@ ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, return ZEBRA_FAIL; } } - res = rpn_scan (zh, stream, zapt, attributeset, - zh->num_basenames, zh->basenames, position, - num_entries, entries, is_partial, limit_rset, 0); - zebra_end_read (zh); + res = rpn_scan(zh, stream, zapt, attributeset, + zh->num_basenames, zh->basenames, position, + num_entries, entries, is_partial, limit_rset); + zebra_end_read(zh); return res; } diff --git a/util/charmap.c b/util/charmap.c index 093681e..374c35f 100644 --- a/util/charmap.c +++ b/util/charmap.c @@ -1,4 +1,4 @@ -/* $Id: charmap.c,v 1.41 2006-08-14 10:40:34 adam Exp $ +/* $Id: charmap.c,v 1.42 2006-09-21 20:22:35 adam Exp $ Copyright (C) 1995-2006 Index Data ApS @@ -46,7 +46,7 @@ const unsigned char CHR_FIELD_BEGIN = '^'; const char *CHR_UNKNOWN = "\001"; const char *CHR_SPACE = "\002"; const char *CHR_CUT = "\003"; -const char *CHR_BASE = "\005"; +const char *CHR_BASE = "\005"; /* CHECK CHR_BASE_CHAR as well */ struct chrmaptab_info { -- 1.7.10.4