1 /* $Id: rsbool.c,v 1.51 2004-11-19 10:27:14 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
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_and (RSFD rfd, void *buf, TERMID *term);
41 static int r_read_or (RSFD rfd, void *buf, TERMID *term);
42 static int r_read_not (RSFD rfd, void *buf, TERMID *term);
43 static int r_write (RSFD rfd, const void *buf);
44 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
47 static const struct rset_control control_and =
60 static const struct rset_control control_or =
73 static const struct rset_control control_not =
87 const struct rset_control *rset_kind_and = &control_and;
88 const struct rset_control *rset_kind_or = &control_or;
89 const struct rset_control *rset_kind_not = &control_not;
91 struct rset_bool_info {
96 struct rset_bool_rfd {
109 static RSET rsbool_create_base( const struct rset_control *ctrl,
110 NMEM nmem, const struct key_control *kcontrol,
112 RSET rset_l, RSET rset_r)
114 RSET rnew=rset_create_base(ctrl, nmem, kcontrol, scope,0);
115 struct rset_bool_info *info;
116 info = (struct rset_bool_info *) nmem_malloc(rnew->nmem,sizeof(*info));
117 info->rset_l = rset_l;
118 info->rset_r = rset_r;
124 RSET rsbool_create_and( NMEM nmem, const struct key_control *kcontrol,
126 RSET rset_l, RSET rset_r)
128 return rsbool_create_base(rset_kind_and, nmem, kcontrol,
133 RSET rsbool_create_or( NMEM nmem, const struct key_control *kcontrol,
135 RSET rset_l, RSET rset_r)
137 return rsbool_create_base(rset_kind_or, nmem, kcontrol,
142 RSET rsbool_create_not( NMEM nmem, const struct key_control *kcontrol,
144 RSET rset_l, RSET rset_r)
146 return rsbool_create_base(rset_kind_not, nmem, kcontrol,
151 static void r_delete (RSET ct)
153 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
154 rset_delete (info->rset_l);
155 rset_delete (info->rset_r);
159 static RSFD r_open (RSET ct, int flag)
161 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
163 struct rset_bool_rfd *p;
166 if (flag & RSETF_WRITE)
168 yaz_log (YLOG_FATAL, "bool set type is read-only");
171 rfd = rfd_create_base(ct);
173 p=(struct rset_bool_rfd *)rfd->priv;
175 p=nmem_malloc(ct->nmem,sizeof(*p));
177 p->buf_l = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
178 p->buf_r = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
181 yaz_log(YLOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
184 p->rfd_l = rset_open (info->rset_l, RSETF_READ);
185 p->rfd_r = rset_open (info->rset_r, RSETF_READ);
186 p->more_l = rset_read (p->rfd_l, p->buf_l, &p->term_l);
187 p->more_r = rset_read (p->rfd_r, p->buf_r, &p->term_r);
192 static void r_close (RSFD rfd)
194 /* struct rset_bool_info *info = (struct rset_bool_info*)(rfd->rset->priv); */
195 struct rset_bool_rfd *prfd=(struct rset_bool_rfd *)rfd->priv;
197 rset_close (prfd->rfd_l);
198 rset_close (prfd->rfd_r);
199 rfd_delete_base(rfd);
204 static int r_forward (RSFD rfd, void *buf, TERMID *term,
205 const void *untilbuf)
207 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
208 const struct key_control *kctrl=rfd->rset->keycontrol;
210 if ( p->more_l && ((kctrl->cmp)(untilbuf,p->buf_l)>=rfd->rset->scope) )
211 p->more_l = rset_forward(p->rfd_l, p->buf_l, &p->term_l, untilbuf);
212 if ( p->more_r && ((kctrl->cmp)(untilbuf,p->buf_r)>=rfd->rset->scope))
213 p->more_r = rset_forward(p->rfd_r, p->buf_r, &p->term_r, untilbuf);
215 return rset_read(rfd,buf,term);
234 static int r_read_and (RSFD rfd, void *buf, TERMID *term)
236 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
237 const struct key_control *kctrl=rfd->rset->keycontrol;
239 while (p->more_l || p->more_r)
243 if (p->more_l && p->more_r)
244 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
246 cmp = -rfd->rset->scope;
248 cmp = rfd->rset->scope;
250 yaz_log (YLOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
251 rfd, p->more_l, p->more_r, cmp, p->tail);
252 (*kctrl->log_item)(YLOG_DEBUG, p->buf_l, "left ");
253 (*kctrl->log_item)(YLOG_DEBUG, p->buf_r, "right ");
257 memcpy (buf, p->buf_l, kctrl->key_size);
260 p->more_l = rset_read (p->rfd_l, p->buf_l, &p->term_l);
263 else if ( (cmp>0) && (cmp<rfd->rset->scope))
264 { /* typically cmp == 1 */
265 memcpy (buf, p->buf_r, kctrl->key_size);
268 p->more_r = rset_read (p->rfd_r, p->buf_r, &p->term_r);
271 yaz_log (YLOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
272 rfd, p->more_l, p->more_r, cmp);
273 key_logdump(YLOG_DEBUG,buf);
274 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
279 else if ( (cmp<0) && (-cmp<rfd->rset->scope))
281 memcpy (buf, p->buf_l, kctrl->key_size);
284 p->more_l = rset_read (p->rfd_l, p->buf_l,&p->term_l);
287 yaz_log (YLOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
288 rfd, p->more_l, p->more_r, cmp);
289 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
294 else if (cmp >= rfd->rset->scope )
298 memcpy (buf, p->buf_r, kctrl->key_size);
301 p->more_r = rset_read (p->rfd_r, p->buf_r, &p->term_r);
302 if (!p->more_r || (*kctrl->cmp)(p->buf_r, buf) > 1)
305 yaz_log (YLOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
306 rfd, p->more_l, p->more_r, cmp);
307 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
315 yaz_log (YLOG_DEBUG, "r_read_and [%p] about to forward R "
317 rfd, p->more_l, p->more_r, cmp);
319 if (p->more_r && p->more_l)
320 p->more_r = rset_forward( p->rfd_r, p->buf_r,
321 &p->term_r, p->buf_l);
323 return 0; /* no point in reading further */
330 memcpy (buf, p->buf_l, kctrl->key_size);
333 p->more_l = rset_read (p->rfd_l, p->buf_l, &p->term_l);
334 if (!p->more_l || (*kctrl->cmp)(p->buf_l, buf) > 1)
337 yaz_log (YLOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
338 rfd, p->more_l, p->more_r, cmp);
339 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
347 yaz_log (YLOG_DEBUG, "r_read_and [%p] about to forward L "
349 rfd, p->more_l, p->more_r, cmp);
351 if (p->more_r && p->more_l)
352 p->more_l = rset_forward(p->rfd_l, p->buf_l,
353 &p->term_l, p->buf_r);
355 return 0; /* no point in reading further */
360 yaz_log (YLOG_DEBUG, "r_read_and [%p] reached its end",rfd);
365 static int r_read_or (RSFD rfd, void *buf, TERMID *term)
367 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
368 const struct key_control *kctrl=rfd->rset->keycontrol;
370 while (p->more_l || p->more_r)
374 if (p->more_l && p->more_r)
375 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
377 cmp = rfd->rset->scope;
379 cmp = -rfd->rset->scope;
382 memcpy (buf, p->buf_l, kctrl->key_size);
385 p->more_l = rset_read (p->rfd_l, p->buf_l, &p->term_l);
386 /* FIXME - is this right, should we not leave _r as it is */
387 /* and return that in the next read, so that ranking etc */
388 /* get to see both? */
389 p->more_r = rset_read (p->rfd_r, p->buf_r, &p->term_r);
391 yaz_log (YLOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
392 p->more_l, p->more_r, cmp);
393 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
400 memcpy (buf, p->buf_r, kctrl->key_size);
403 p->more_r = rset_read (p->rfd_r, p->buf_r, &p->term_r);
405 yaz_log (YLOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
406 p->more_l, p->more_r, cmp);
407 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
414 memcpy (buf, p->buf_l, kctrl->key_size);
417 p->more_l = rset_read ( p->rfd_l, p->buf_l, &p->term_l);
419 yaz_log (YLOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
420 p->more_l, p->more_r, cmp);
421 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
430 static int r_read_not (RSFD rfd, void *buf, TERMID *term)
432 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
433 const struct key_control *kctrl=rfd->rset->keycontrol;
435 while (p->more_l || p->more_r)
439 if (p->more_l && p->more_r)
440 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
442 cmp = rfd->rset->scope;
444 cmp = -rfd->rset->scope;
446 if (cmp <= -rfd->rset->scope)
448 memcpy (buf, p->buf_l, kctrl->key_size);
451 p->more_l = rset_read (p->rfd_l, p->buf_l, &p->term_l);
455 else if (cmp >= rfd->rset->scope) /* cmp >1 */
456 p->more_r = rset_forward( p->rfd_r, p->buf_r,
457 &p->term_r, p->buf_l);
459 { /* cmp== -1, 0, or 1 */
460 memcpy (buf, p->buf_l, kctrl->key_size);
465 p->more_l = rset_read (p->rfd_l, p->buf_l, &p->term_l);
468 cmp = (*kctrl->cmp)(p->buf_l, buf);
469 } while (abs(cmp)<rfd->rset->scope);
470 /* (cmp >= -1 && cmp <= 1) */
473 p->more_r = rset_read (p->rfd_r, p->buf_r, &p->term_r);
476 cmp = (*kctrl->cmp)(p->buf_r, buf);
477 } while (abs(cmp)<rfd->rset->scope);
478 /* (cmp >= -1 && cmp <= 1) */
485 static int r_write (RSFD rfd, const void *buf)
487 yaz_log (YLOG_FATAL, "bool set type is read-only");
491 static void r_pos (RSFD rfd, double *current, double *total)
493 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
498 rset_pos(p->rfd_l, &lcur, <ot);
499 rset_pos(p->rfd_r, &rcur, &rtot);
500 if ( (rtot<0) && (ltot<0)) { /*no position */
501 *current=rcur; /* return same as you got */
502 *total=rtot; /* probably -1 for not available */
504 if ( rtot<0) { rtot=0; rcur=0;} /* if only one useful, use it */
505 if ( ltot<0) { ltot=0; lcur=0;}
506 if ( rtot+ltot < 1 ) { /* empty rset */
511 r=1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
512 *current=(double) (p->hits);
515 yaz_log(YLOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
516 info->rset_l->control->desc, info->rset_r->control->desc,
517 *current, *total, r);
521 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
523 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
524 rset_getterms(info->rset_l, terms, maxterms, curterm);
525 rset_getterms(info->rset_r, terms, maxterms, curterm);