X-Git-Url: http://git.indexdata.com/?p=idzebra-moved-to-github.git;a=blobdiff_plain;f=rset%2Frsprox.c;h=15e5b19622e21cbc7da2d2150134692f7164dc99;hp=fa562d8c8be55f50fa277df57f359f2f525e205f;hb=fb791f9a754adf7b26695fdb1ddd0fdee9bf28f9;hpb=0229bb1ac74706a4e460a11a8a8712b4ea0f2ebe diff --git a/rset/rsprox.c b/rset/rsprox.c index fa562d8..15e5b19 100644 --- a/rset/rsprox.c +++ b/rset/rsprox.c @@ -1,8 +1,5 @@ -/* $Id: rsprox.c,v 1.18 2004-10-15 10:07:34 heikki Exp $ - Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004 - Index Data Aps - -This file is part of the Zebra server. +/* This file is part of the Zebra server. + Copyright (C) 1994-2009 Index Data 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 @@ -15,9 +12,9 @@ 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. +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ #include @@ -25,7 +22,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include -#include +#include #include #ifndef RSET_DEBUG @@ -39,11 +36,13 @@ static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf); static int r_read (RSFD rfd, void *buf, TERMID *term); static int r_write (RSFD rfd, const void *buf); static void r_pos (RSFD rfd, double *current, double *total); +static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm); static const struct rset_control control = { "prox", r_delete, + r_get_terms, r_open, r_close, r_forward, @@ -52,11 +51,7 @@ static const struct rset_control control = r_write, }; -const struct rset_control *rset_kind_prox = &control; - struct rset_prox_info { - RSET *rset; /* array of 'child' rsets */ - int rset_no; /* how many of them */ int ordered; int exclusion; int relation; @@ -72,93 +67,83 @@ struct rset_prox_rfd { }; -RSET rsprox_create( NMEM nmem, const struct key_control *kcontrol, int scope, - int rset_no, RSET *rset, - int ordered, int exclusion, - int relation, int distance) +RSET rset_create_prox(NMEM nmem, struct rset_key_control *kcontrol, + int scope, + int rset_no, RSET *rset, + int ordered, int exclusion, + int relation, int distance) { - RSET rnew=rset_create_base(&control, nmem, kcontrol, scope,0); + RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, 0, + rset_no, rset); struct rset_prox_info *info; info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info)); - info->rset = nmem_malloc(rnew->nmem,rset_no * sizeof(*info->rset)); - memcpy(info->rset, rset, - rset_no * sizeof(*info->rset)); - info->rset_no=rset_no; - info->ordered=ordered; - info->exclusion=exclusion; - info->relation=relation; - info->distance=distance; - rnew->priv=info; + info->ordered = ordered; + info->exclusion = exclusion; + info->relation = relation; + info->distance = distance; + rnew->priv = info; return rnew; } - static void r_delete (RSET ct) { - struct rset_prox_info *info = (struct rset_prox_info *) ct->priv; - int i; - - for (i = 0; irset_no; i++) - rset_delete (info->rset[i]); } - static RSFD r_open (RSET ct, int flag) { - struct rset_prox_info *info = (struct rset_prox_info *) ct->priv; RSFD rfd; struct rset_prox_rfd *p; int i; if (flag & RSETF_WRITE) { - logf (LOG_FATAL, "prox set type is read-only"); + yaz_log(YLOG_FATAL, "prox set type is read-only"); return NULL; } rfd = rfd_create_base(ct); if (rfd->priv) - p=(struct rset_prox_rfd *)(rfd->priv); + p = (struct rset_prox_rfd *)(rfd->priv); else { - p = (struct rset_prox_rfd *) nmem_malloc (ct->nmem,sizeof(*p)); - rfd->priv=p; - p->more = nmem_malloc (ct->nmem,sizeof(*p->more) * info->rset_no); - p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * info->rset_no); - p->terms = nmem_malloc(ct->nmem,sizeof(*p->terms) * info->rset_no); - for (i = 0; i < info->rset_no; i++) + p = (struct rset_prox_rfd *) nmem_malloc(ct->nmem,sizeof(*p)); + rfd->priv = p; + p->more = nmem_malloc (ct->nmem,sizeof(*p->more) * ct->no_children); + p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * ct->no_children); + p->terms = nmem_malloc(ct->nmem,sizeof(*p->terms) * ct->no_children); + for (i = 0; i < ct->no_children; i++) { p->buf[i] = nmem_malloc(ct->nmem,ct->keycontrol->key_size); p->terms[i] = 0; } - p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * info->rset_no); + p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * ct->no_children); } - logf(LOG_DEBUG,"rsprox (%s) open [%p] n=%d", - ct->control->desc, rfd, info->rset_no); + yaz_log(YLOG_DEBUG,"rsprox (%s) open [%p] n=%d", + ct->control->desc, rfd, ct->no_children); - for (i = 0; i < info->rset_no; i++) { - p->rfd[i] = rset_open (info->rset[i], RSETF_READ); + for (i = 0; i < ct->no_children; i++) { + p->rfd[i] = rset_open (ct->children[i], RSETF_READ); p->more[i] = rset_read (p->rfd[i], p->buf[i], &p->terms[i]); } - p->hits=0; + p->hits = 0; return rfd; } static void r_close (RSFD rfd) { - struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv); - struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv); + RSET ct = rfd->rset; + struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv); int i; - for (i = 0; irset_no; i++) - rset_close (p->rfd[i]); - rfd_delete_base(rfd); + for (i = 0; ino_children; i++) + rset_close(p->rfd[i]); } -static int r_forward (RSFD rfd, void *buf, TERMID *term, const void *untilbuf) +static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf) { - struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv); - struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv); - const struct key_control *kctrl=rfd->rset->keycontrol; - int cmp=0; + RSET ct = rfd->rset; + struct rset_prox_info *info = (struct rset_prox_info *)(ct->priv); + struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv); + const struct rset_key_control *kctrl = ct->keycontrol; + int cmp = 0; int i; if (untilbuf) @@ -174,7 +159,7 @@ static int r_forward (RSFD rfd, void *buf, TERMID *term, const void *untilbuf) { while (p->more[0]) { - for (i = 1; i < info->rset_no; i++) + for (i = 1; i < ct->no_children; i++) { if (!p->more[i]) { @@ -196,7 +181,7 @@ static int r_forward (RSFD rfd, void *buf, TERMID *term, const void *untilbuf) (*kctrl->getseq)(p->buf[i])) { /* FIXME - We need more flexible multilevel stuff */ p->more[i-1] = rset_read ( p->rfd[i-1], p->buf[i-1], - &p->terms[i]); + &p->terms[i-1]); break; } } @@ -207,18 +192,19 @@ static int r_forward (RSFD rfd, void *buf, TERMID *term, const void *untilbuf) break; } } - if (i == info->rset_no) + if (i == ct->no_children) { - memcpy (buf, p->buf[0], kctrl->key_size); + i = ct->no_children-1; + memcpy(buf, p->buf[i], kctrl->key_size); if (term) - *term=p->terms[i]; - p->more[0] = rset_read (p->rfd[0], p->buf[0], &p->terms[i]); + *term = p->terms[i]; + p->more[i] = rset_read(p->rfd[i], p->buf[i], &p->terms[i]); p->hits++; return 1; } } } - else if (info->rset_no == 2) + else if (ct->no_children == 2) { while (p->more[0] && p->more[1]) { @@ -236,56 +222,68 @@ static int r_forward (RSFD rfd, void *buf, TERMID *term, const void *untilbuf) seqno[n++] = (*kctrl->getseq)(p->buf[0]); while ((p->more[0] = rset_read (p->rfd[0], - p->buf[0], &p->terms[0])) >= -1 && - p->more[0] <= -1) + p->buf[0], &p->terms[0]))) + { + cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]); + if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope) + break; if (n < 500) seqno[n++] = (*kctrl->getseq)(p->buf[0]); - - for (i = 0; ibuf[1], kctrl->key_size); + if (term) + *term = p->terms[1]; + while (1) { - int diff = (*kctrl->getseq)(p->buf[1]) - seqno[i]; - int excl = info->exclusion; - if (!info->ordered && diff < 0) - diff = -diff; - switch (info->relation) + for (i = 0; i < n; i++) { - case 1: /* < */ - if (diff < info->distance && diff >= 0) - excl = !excl; - break; - case 2: /* <= */ - if (diff <= info->distance && diff >= 0) - excl = !excl; - break; - case 3: /* == */ - if (diff == info->distance && diff >= 0) - excl = !excl; - break; - case 4: /* >= */ - if (diff >= info->distance && diff >= 0) - excl = !excl; - break; - case 5: /* > */ - if (diff > info->distance && diff >= 0) - excl = !excl; + zint diff = (*kctrl->getseq)(p->buf[1]) - seqno[i]; + int excl = info->exclusion; + if (!info->ordered && diff < 0) + diff = -diff; + switch (info->relation) + { + case 1: /* < */ + if (diff < info->distance && diff >= 0) + excl = !excl; + break; + case 2: /* <= */ + if (diff <= info->distance && diff >= 0) + excl = !excl; + break; + case 3: /* == */ + if (diff == info->distance && diff >= 0) + excl = !excl; + break; + case 4: /* >= */ + if (diff >= info->distance && diff >= 0) + excl = !excl; + break; + case 5: /* > */ + if (diff > info->distance && diff >= 0) + excl = !excl; + break; + case 6: /* != */ + if (diff != info->distance && diff >= 0) + excl = !excl; + break; + } + if (excl) + { + p->more[1] = rset_read ( p->rfd[1], p->buf[1], + &p->terms[1]); + p->hits++; + return 1; + } + } + p->more[1] = rset_read(p->rfd[1], p->buf[1], &p->terms[1]); + if (!p->more[1]) break; - case 6: /* != */ - if (diff != info->distance && diff >= 0) - excl = !excl; + cmp = (*kctrl->cmp)(buf, p->buf[1]); + if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope) break; - } - if (excl) - { - memcpy (buf, p->buf[1], kctrl->key_size); - if (term) - *term=p->terms[1]; - p->more[1] = rset_read ( p->rfd[1], p->buf[1], - &p->terms[1]); - p->hits++; - return 1; - } } - p->more[1] = rset_read (p->rfd[1], p->buf[1],&p->terms[i]); } } } @@ -300,22 +298,22 @@ static int r_read (RSFD rfd, void *buf, TERMID *term) static int r_write (RSFD rfd, const void *buf) { - logf (LOG_FATAL, "prox set type is read-only"); + yaz_log(YLOG_FATAL, "prox set type is read-only"); return -1; } static void r_pos (RSFD rfd, double *current, double *total) { - struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv); - struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv); + RSET ct = rfd->rset; + struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv); int i; - double cur,tot=-1; - double scur=0,stot=0; - double r; + double r = 0.0; + double cur, tot = -1.0; + double scur = 0.0, stot = 0.0; - logf (LOG_DEBUG, "rsprox_pos"); + yaz_log(YLOG_DEBUG, "rsprox_pos"); - for (i = 0; i < info->rset_no; i++) + for (i = 0; i < ct->no_children; i++) { rset_pos(p->rfd[i], &cur, &tot); if (tot>0) { @@ -324,16 +322,33 @@ static void r_pos (RSFD rfd, double *current, double *total) } } if (tot <0) { /* nothing found */ - *current=-1; - *total=-1; - } else if (tot <1) { /* most likely tot==0 */ - *current=0; - *total=0; + *current = -1; + *total = -1; + } else if (tot < 1) { /* most likely tot==0 */ + *current = 0; + *total = 0; } else { - r=scur/stot; - *current=p->hits; + r = scur/stot; + *current = (double) p->hits; *total=*current/r ; } - logf(LOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ", + yaz_log(YLOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ", i,*current, *total, r); } + +static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm) +{ + int i; + for (i = 0; ino_children; i++) + rset_getterms(ct->children[i], terms, maxterms, curterm); +} + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +