1 /* $Id: rsbool.c,v 1.60 2006-06-28 09:40:17 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #include <idzebra/util.h>
35 static RSFD r_open(RSET ct, int flag);
36 static void r_close(RSFD rfd);
37 static void r_delete(RSET ct);
38 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf);
39 static void r_pos(RSFD rfd, double *current, double *total);
40 static int r_read_not(RSFD rfd, void *buf, TERMID *term);
41 static int r_write(RSFD rfd, const void *buf);
42 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
44 static const struct rset_control control_not =
75 static RSET rsbool_create_base(const struct rset_control *ctrl,
77 struct rset_key_control *kcontrol,
78 int scope, RSET rset_l, RSET rset_r)
80 RSET children[2], rnew;
81 struct rset_private *info;
85 rnew = rset_create_base(ctrl, nmem, kcontrol, scope, 0, 2, children);
86 info = (struct rset_private *) nmem_malloc(rnew->nmem, sizeof(*info));
87 info->rset_l = rset_l;
88 info->rset_r = rset_r;
93 RSET rset_create_not(NMEM nmem, struct rset_key_control *kcontrol,
94 int scope, RSET rset_l, RSET rset_r)
96 return rsbool_create_base(&control_not, nmem, kcontrol,
97 scope, rset_l, rset_r);
100 static void r_delete(RSET ct)
104 static RSFD r_open(RSET ct, int flag)
106 struct rset_private *info = (struct rset_private *) ct->priv;
108 struct rfd_private *p;
110 if (flag & RSETF_WRITE)
112 yaz_log(YLOG_FATAL, "bool set type is read-only");
115 rfd = rfd_create_base(ct);
117 p = (struct rfd_private *)rfd->priv;
119 p = nmem_malloc(ct->nmem,sizeof(*p));
121 p->buf_l = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
122 p->buf_r = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
125 yaz_log(YLOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
128 p->rfd_l = rset_open (info->rset_l, RSETF_READ);
129 p->rfd_r = rset_open (info->rset_r, RSETF_READ);
130 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
131 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
136 static void r_close (RSFD rfd)
138 struct rfd_private *prfd=(struct rfd_private *)rfd->priv;
140 rset_close (prfd->rfd_l);
141 rset_close (prfd->rfd_r);
144 static int r_forward(RSFD rfd, void *buf, TERMID *term,
145 const void *untilbuf)
147 struct rfd_private *p = (struct rfd_private *)rfd->priv;
148 const struct rset_key_control *kctrl=rfd->rset->keycontrol;
150 if ( p->more_l && ((kctrl->cmp)(untilbuf,p->buf_l)>=rfd->rset->scope) )
151 p->more_l = rset_forward(p->rfd_l, p->buf_l, &p->term_l, untilbuf);
152 if ( p->more_r && ((kctrl->cmp)(untilbuf,p->buf_r)>=rfd->rset->scope))
153 p->more_r = rset_forward(p->rfd_r, p->buf_r, &p->term_r, untilbuf);
155 return rset_read(rfd,buf,term);
174 static int r_read_not(RSFD rfd, void *buf, TERMID *term)
176 struct rfd_private *p = (struct rfd_private *)rfd->priv;
177 const struct rset_key_control *kctrl = rfd->rset->keycontrol;
184 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
186 cmp = -rfd->rset->scope;
188 if (cmp <= -rfd->rset->scope)
190 memcpy (buf, p->buf_l, kctrl->key_size);
193 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
197 else if (cmp >= rfd->rset->scope) /* cmp >1 */
199 p->more_r = rset_forward( p->rfd_r, p->buf_r,
200 &p->term_r, p->buf_l);
203 { /* cmp== -1, 0, or 1 */
204 memcpy (buf, p->buf_l, kctrl->key_size);
209 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
212 cmp = (*kctrl->cmp)(p->buf_l, buf);
213 } while (abs(cmp)<rfd->rset->scope);
214 /* (cmp >= -1 && cmp <= 1) */
217 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
220 cmp = (*kctrl->cmp)(p->buf_r, buf);
221 } while (abs(cmp)<rfd->rset->scope);
222 /* (cmp >= -1 && cmp <= 1) */
229 static int r_write(RSFD rfd, const void *buf)
231 yaz_log(YLOG_FATAL, "bool set type is read-only");
235 static void r_pos(RSFD rfd, double *current, double *total)
237 struct rfd_private *p = (struct rfd_private *)rfd->priv;
243 rset_pos(p->rfd_l, &lcur, <ot);
244 rset_pos(p->rfd_r, &rcur, &rtot);
245 if ( (rtot<0) && (ltot<0)) { /*no position */
246 *current = rcur; /* return same as you got */
247 *total = rtot; /* probably -1 for not available */
250 rtot = rcur = 0; /* if only one useful, use it */
255 *current = *total = 0;
258 r = 1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
259 *current = (double) (p->hits);
260 *total = *current/r ;
262 yaz_log(YLOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
263 info->rset_l->control->desc, info->rset_r->control->desc,
264 *current, *total, r);
268 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
270 struct rset_private *info = (struct rset_private *) ct->priv;
271 rset_getterms(info->rset_l, terms, maxterms, curterm);
272 rset_getterms(info->rset_r, terms, maxterms, curterm);
278 * indent-tabs-mode: nil
280 * vim: shiftwidth=4 tabstop=8 expandtab