X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=rset%2Frsbetween.c;h=bf6399271336acdecf8e4c2941b42d6939d48067;hb=b5eb8cd7ae4134d915ed8b4aec25ab90c8bd0dcb;hp=4222c3081a9908300f1640c0d3751ef8a8d687aa;hpb=dbf7ead21f60be189f0d532175c92924a8eb73fd;p=idzebra-moved-to-github.git diff --git a/rset/rsbetween.c b/rset/rsbetween.c index 4222c30..bf63992 100644 --- a/rset/rsbetween.c +++ b/rset/rsbetween.c @@ -1,27 +1,56 @@ -/* - * Copyright (C) 1994-2002, Index Data - * All rights reserved. - * Heikki Levanto - * - * $Id: rsbetween.c,v 1.6 2002-08-01 08:53:35 adam Exp $ - */ +/* $Id: rsbetween.c,v 1.17 2004-08-06 14:09:02 heikki Exp $ + Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 + Index Data Aps + +This file is part of the Zebra server. + +Zebra is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +Zebra is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Zebra; see the file LICENSE.zebra. If not, write to the +Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA +02111-1307, USA. +*/ + + +/* rsbetween is (mostly) used for xml searches. It returns the hits of the + * "middle" rset, that are in between the "left" and "right" rsets. For + * example "Shakespeare" in between "" and . The thing is + * complicated by the inclusion of attributes (from their own rset). If attrs + * specified, they must match the "left" rset (start tag). "Hamlet" between + * "" and "". (This assumes that the attributes are + * indexed to the same seqno as the tags). +*/ #include #include #include #include -#include #include +#include + +#define RSBETWEEN_DEBUG 0 static void *r_create_between(RSET ct, const struct rset_control *sel, void *parms); static RSFD r_open_between (RSET ct, int flag); static void r_close_between (RSFD rfd); static void r_delete_between (RSET ct); static void r_rewind_between (RSFD rfd); -static int r_count_between (RSET ct); +static int r_forward_between(RSET ct, RSFD rfd, void *buf, int *term_index, + int (*cmpfunc)(const void *p1, const void *p2), + const void *untilbuf); static int r_read_between (RSFD rfd, void *buf, int *term_index); static int r_write_between (RSFD rfd, const void *buf); +static void r_pos_between (RSFD rfd, double *current, double *total); static const struct rset_control control_between = { @@ -31,7 +60,8 @@ static const struct rset_control control_between = r_close_between, r_delete_between, r_rewind_between, - r_count_between, + r_forward_between, /* rset_default_forward, */ + r_pos_between, r_read_between, r_write_between, }; @@ -70,8 +100,24 @@ struct rset_between_rfd { int level; struct rset_between_rfd *next; struct rset_between_info *info; + zint hits; }; +#if RSBETWEEN_DEBUG +static void log2 (struct rset_between_rfd *p, char *msg, int cmp_l, int cmp_r) +{ + char buf_l[32]; + char buf_m[32]; + char buf_r[32]; + logf(LOG_DEBUG,"btw: %s l=%s(%d/%d) m=%s(%d) r=%s(%d/%d), lev=%d", + msg, + (*p->info->printer)(p->buf_l, buf_l), p->more_l, cmp_l, + (*p->info->printer)(p->buf_m, buf_m), p->more_m, + (*p->info->printer)(p->buf_r, buf_r), p->more_r, cmp_r, + p->level); +} +#endif + static void *r_create_between (RSET ct, const struct rset_control *sel, void *parms) { @@ -135,8 +181,8 @@ static RSFD r_open_between (RSET ct, int flag) if (flag & RSETF_WRITE) { - logf (LOG_FATAL, "between set type is read-only"); - return NULL; + logf (LOG_FATAL, "between set type is read-only"); + return NULL; } rfd = (struct rset_between_rfd *) xmalloc (sizeof(*rfd)); rfd->next = info->rfd_list; @@ -153,11 +199,11 @@ static RSFD r_open_between (RSET ct, int flag) rfd->rfd_r = rset_open (info->rset_r, RSETF_READ); rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l, - &rfd->term_index_l); + &rfd->term_index_l); rfd->more_m = rset_read (info->rset_m, rfd->rfd_m, rfd->buf_m, - &rfd->term_index_m); + &rfd->term_index_m); rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r, - &rfd->term_index_r); + &rfd->term_index_r); if (info->rset_attr) { int dummy; @@ -166,6 +212,7 @@ static RSFD r_open_between (RSET ct, int flag) rfd->buf_attr, &dummy); } rfd->level=0; + rfd->hits=0; return rfd; } @@ -214,7 +261,9 @@ static void r_rewind_between (RSFD rfd) struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info; struct rset_between_rfd *p = (struct rset_between_rfd *) rfd; +#if RSBETWEEN_DEBUG logf (LOG_DEBUG, "rsbetween_rewind"); +#endif rset_rewind (info->rset_l, p->rfd_l); rset_rewind (info->rset_m, p->rfd_m); rset_rewind (info->rset_r, p->rfd_r); @@ -229,51 +278,72 @@ static void r_rewind_between (RSFD rfd) &dummy); } p->level=0; + p->hits=0; } -static int r_count_between (RSET ct) -{ - return 0; -} -static void logit( struct rset_between_info *info, char *prefix, void *l, void *m, void *r) + +static int r_forward_between(RSET ct, RSFD rfd, void *buf, int *term_index, + int (*cmpfunc)(const void *p1, const void *p2), + const void *untilbuf) { - char buf_l[32]; - char buf_m[32]; - char buf_r[32]; - logf(LOG_DEBUG,"btw: %s l=%s m=%s r=%s", - prefix, - (*info->printer)(l, buf_l), - (*info->printer)(m, buf_m), - (*info->printer)(r, buf_r) ); + struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info; + struct rset_between_rfd *p = (struct rset_between_rfd *) rfd; + int rc; +#if RSBETWEEN_DEBUG + log2( p, "fwd: before forward", 0,0); +#endif + /* It is enough to forward the m pointer here, the read will */ + /* naturally forward the l, m, and attr pointers */ + if (p->more_m) + p->more_m=rset_forward(info->rset_m,p->rfd_m, p->buf_m, + &p->term_index_m, info->cmp,untilbuf); +#if RSBETWEEN_DEBUG + log2( p, "fwd: after forward M", 0,0); +#endif + rc = r_read_between(rfd, buf, term_index); +#if RSBETWEEN_DEBUG + log2( p, "fwd: after forward", 0,0); +#endif + return rc; } + + + static int r_read_between (RSFD rfd, void *buf, int *term_index) { struct rset_between_rfd *p = (struct rset_between_rfd *) rfd; struct rset_between_info *info = p->info; - int cmp_l; - int cmp_r; - int attr_match; + int cmp_l=0; + int cmp_r=0; + int attr_match = 0; while (p->more_m) { - logit( info, "start of loop", p->buf_l, p->buf_m, p->buf_r); - - /* forward L until past m, count levels, note rec boundaries */ - if (p->more_l) - cmp_l= (*info->cmp)(p->buf_l, p->buf_m); - else - cmp_l=2; /* past this record */ - logf(LOG_DEBUG, "cmp_l=%d", cmp_l); +#if RSBETWEEN_DEBUG + log2( p, "start of loop", cmp_l, cmp_r); +#endif + + /* forward L until past m, count levels, note rec boundaries */ + if (p->more_l) + cmp_l= (*info->cmp)(p->buf_l, p->buf_m); + else + { + p->level = 0; + cmp_l=2; /* past this record */ + } +#if RSBETWEEN_DEBUG + log2( p, "after first L", cmp_l, cmp_r); +#endif while (cmp_l < 0) /* l before m */ - { + { if (cmp_l == -2) - p->level=0; /* earlier record */ + p->level=0; /* earlier record */ if (cmp_l == -1) { - p->level++; /* relevant start tag */ + p->level++; /* relevant start tag */ if (!info->rset_attr) attr_match = 1; @@ -292,68 +362,161 @@ static int r_read_between (RSFD rfd, void *buf, int *term_index) } else if (cmp_attr > 0) break; - p->more_attr = rset_read (info->rset_attr, p->rfd_attr, + else if (cmp_attr==-1) + p->more_attr = rset_read (info->rset_attr, p->rfd_attr, p->buf_attr, &dummy_term); - } + /* if we had a forward that went all the way to + * the seqno, we could use that. But fwd only goes + * to the sysno */ + else if (cmp_attr==-2) + { + p->more_attr = rset_forward( + info->rset_attr, p->rfd_attr, + p->buf_attr, &dummy_term, + info->cmp, p->buf_l); +#if RSBETWEEN_DEBUG + logf(LOG_DEBUG, "btw: after frowarding attr m=%d",p->more_attr); +#endif + } + } /* while more_attr */ } } - if (p->more_l) +#define NEWCODE 1 +#if NEWCODE + if (cmp_l==-2) + { + if (p->more_l) + { + p->more_l=rset_forward( + info->rset_l, p->rfd_l, + p->buf_l, &p->term_index_l, + info->cmp, p->buf_m); + if (p->more_l) + cmp_l= (*info->cmp)(p->buf_l, p->buf_m); + else + cmp_l=2; +#if RSBETWEEN_DEBUG + log2( p, "after forwarding L", cmp_l, cmp_r); +#endif + } + } else { p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, - &p->term_index_l); - cmp_l= (*info->cmp)(p->buf_l, p->buf_m); - logit( info, "forwarded L", p->buf_l, p->buf_m, p->buf_r); - logf(LOG_DEBUG, " cmp_l=%d", cmp_l); + &p->term_index_l); + } +#else + p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, + &p->term_index_l); +#endif + if (p->more_l) + { + cmp_l= (*info->cmp)(p->buf_l, p->buf_m); } else - cmp_l=2; + cmp_l=2; +#if RSBETWEEN_DEBUG + log2( p, "end of L loop", cmp_l, cmp_r); +#endif } /* forward L */ - /* forward R until past m, count levels */ + /* forward R until past m, count levels */ +#if RSBETWEEN_DEBUG + log2( p, "Before moving R", cmp_l, cmp_r); +#endif if (p->more_r) - cmp_r= (*info->cmp)(p->buf_r, p->buf_m); - else - cmp_r=2; - logf(LOG_DEBUG, "cmp_r=%d", cmp_r); + cmp_r= (*info->cmp)(p->buf_r, p->buf_m); + else + cmp_r=2; +#if RSBETWEEN_DEBUG + log2( p, "after first R", cmp_l, cmp_r); +#endif while (cmp_r < 0) /* r before m */ - { - /* -2, earlier record, doesn't matter */ + { + /* -2, earlier record, don't count level */ if (cmp_r == -1) - p->level--; /* relevant end tag */ + p->level--; /* relevant end tag */ if (p->more_r) { +#if NEWCODE + if (cmp_r==-2) + { + p->more_r=rset_forward( + info->rset_r, p->rfd_r, + p->buf_r, &p->term_index_r, + info->cmp, p->buf_m); + } else + { + p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, + &p->term_index_r); + } + if (p->more_r) + cmp_r= (*info->cmp)(p->buf_r, p->buf_m); + +#else p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, - &p->term_index_r); - cmp_r= (*info->cmp)(p->buf_r, p->buf_m); - logit( info, "forwarded R", p->buf_l, p->buf_m, p->buf_r); - logf(LOG_DEBUG, " cmp_r=%d", cmp_r); + &p->term_index_r); + cmp_r= (*info->cmp)(p->buf_r, p->buf_m); +#endif } else - cmp_r=2; + cmp_r=2; +#if RSBETWEEN_DEBUG + log2( p, "End of R loop", cmp_l, cmp_r); +#endif } /* forward R */ - - if ( ( p->level <= 0 ) && ! p->more_l) - return 0; /* no more start tags, nothing more to find */ - if ( attr_match && p->level > 0) /* within a tag pair (or deeper) */ - { - memcpy (buf, p->buf_m, info->key_size); + if ( ( p->level <= 0 ) && ! p->more_l) + return 0; /* no more start tags, nothing more to find */ + + if ( attr_match && p->level > 0) /* within a tag pair (or deeper) */ + { + memcpy (buf, p->buf_m, info->key_size); *term_index = p->term_index_m; - logit( info, "Returning a hit (m)", p->buf_l, p->buf_m, p->buf_r); +#if RSBETWEEN_DEBUG + log2( p, "Returning a hit (and forwarding m)", cmp_l, cmp_r); +#endif p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m, &p->term_index_m); - return 1; - } - else - if ( ! p->more_l ) /* not in data, no more starts */ - return 0; /* ergo, nothing can be found. stop scanning */ - + if (cmp_l == 2) + p->level = 0; + p->hits++; + return 1; + } + else if ( ! p->more_l ) /* not in data, no more starts */ + { +#if RSBETWEEN_DEBUG + log2( p, "no more starts, exiting without a hit", cmp_l, cmp_r); +#endif + return 0; /* ergo, nothing can be found. stop scanning */ + } +#if NEWCODE + if (cmp_l == 2) + { + p->level = 0; + p->more_m=rset_forward( + info->rset_m, p->rfd_m, + p->buf_m, &p->term_index_m, + info->cmp, p->buf_l); + } else + { + p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m, + &p->term_index_m); + } +#else + if (cmp_l == 2) + p->level = 0; p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m, &p->term_index_m); +#endif +#if RSBETWEEN_DEBUG + log2( p, "End of M loop", cmp_l, cmp_r); +#endif } /* while more_m */ - - logf(LOG_DEBUG,"Exiting, no more stuff in m"); + +#if RSBETWEEN_DEBUG + log2( p, "Exiting, nothing more in m", cmp_l, cmp_r); +#endif return 0; /* no more data possible */ @@ -366,3 +529,37 @@ static int r_write_between (RSFD rfd, const void *buf) return -1; } + +static void r_pos_between (RSFD rfd, double *current, double *total) +{ + struct rset_between_rfd *p = (struct rset_between_rfd *) rfd; + struct rset_between_info *info = p->info; + double lcur,ltot; + double mcur,mtot; + double rcur,rtot; + double r; + ltot=-1; rtot=-1; + rset_pos(info->rset_l, p->rfd_l, &lcur, <ot); + rset_pos(info->rset_m, p->rfd_m, &mcur, &mtot); + rset_pos(info->rset_r, p->rfd_r, &rcur, &rtot); + if ( (ltot<0) && (mtot<0) && (rtot<0) ) { /*no position */ + *current=mcur; /* return same as you got */ + *total=mtot; /* probably -1 for not available */ + } + if ( ltot<0) { ltot=0; lcur=0;} /* if only one useful, use it */ + if ( mtot<0) { mtot=0; mcur=0;} + if ( rtot<0) { rtot=0; rcur=0;} + if ( ltot+mtot+rtot < 1 ) { /* empty rset */ + *current=0; + *total=0; + return; + } + r=1.0*(lcur+mcur+rcur)/(ltot+mtot+rtot); /* weighed average of l and r */ + *current=p->hits; + *total=*current/r ; +#if RSBETWEEN_DEBUG + yaz_log(LOG_DEBUG,"betw_pos: (%s/%s) %0.1f/%0.1f= %0.4f ", + info->rset_l->control->desc, info->rset_r->control->desc, + *current, *total, r); +#endif +}