1 /* $Id: rsbool.c,v 1.57 2005-05-24 20:40:15 adam Exp $
2 Copyright (C) 1995-2005
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_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 =
104 static RSET rsbool_create_base(const struct rset_control *ctrl,
106 struct rset_key_control *kcontrol,
107 int scope, RSET rset_l, RSET rset_r)
109 RSET children[2], rnew;
110 struct rset_private *info;
112 children[0] = rset_l;
113 children[1] = rset_r;
114 rnew = rset_create_base(ctrl, nmem, kcontrol, scope, 0, 2, children);
115 info = (struct rset_private *) nmem_malloc(rnew->nmem, sizeof(*info));
116 info->rset_l = rset_l;
117 info->rset_r = rset_r;
122 RSET rsbool_create_and( NMEM nmem, struct rset_key_control *kcontrol,
123 int scope, RSET rset_l, RSET rset_r)
125 return rsbool_create_base(&control_and, nmem, kcontrol,
130 RSET rsbool_create_or(NMEM nmem, struct rset_key_control *kcontrol,
131 int scope, RSET rset_l, RSET rset_r)
133 return rsbool_create_base(&control_or, nmem, kcontrol,
134 scope, rset_l, rset_r);
137 RSET rsbool_create_not(NMEM nmem, struct rset_key_control *kcontrol,
138 int scope, RSET rset_l, RSET rset_r)
140 return rsbool_create_base(&control_not, nmem, kcontrol,
141 scope, rset_l, rset_r);
144 static void r_delete(RSET ct)
148 static RSFD r_open(RSET ct, int flag)
150 struct rset_private *info = (struct rset_private *) ct->priv;
152 struct rfd_private *p;
154 if (flag & RSETF_WRITE)
156 yaz_log(YLOG_FATAL, "bool set type is read-only");
159 rfd = rfd_create_base(ct);
161 p = (struct rfd_private *)rfd->priv;
163 p = nmem_malloc(ct->nmem,sizeof(*p));
165 p->buf_l = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
166 p->buf_r = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
169 yaz_log(YLOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
172 p->rfd_l = rset_open (info->rset_l, RSETF_READ);
173 p->rfd_r = rset_open (info->rset_r, RSETF_READ);
174 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
175 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
180 static void r_close (RSFD rfd)
182 struct rfd_private *prfd=(struct rfd_private *)rfd->priv;
184 rset_close (prfd->rfd_l);
185 rset_close (prfd->rfd_r);
188 static int r_forward(RSFD rfd, void *buf, TERMID *term,
189 const void *untilbuf)
191 struct rfd_private *p = (struct rfd_private *)rfd->priv;
192 const struct rset_key_control *kctrl=rfd->rset->keycontrol;
194 if ( p->more_l && ((kctrl->cmp)(untilbuf,p->buf_l)>=rfd->rset->scope) )
195 p->more_l = rset_forward(p->rfd_l, p->buf_l, &p->term_l, untilbuf);
196 if ( p->more_r && ((kctrl->cmp)(untilbuf,p->buf_r)>=rfd->rset->scope))
197 p->more_r = rset_forward(p->rfd_r, p->buf_r, &p->term_r, untilbuf);
199 return rset_read(rfd,buf,term);
218 static int r_read_and(RSFD rfd, void *buf, TERMID *term)
220 struct rfd_private *p=(struct rfd_private *)rfd->priv;
221 const struct rset_key_control *kctrl=rfd->rset->keycontrol;
223 while (p->more_l || p->more_r)
227 if (p->more_l && p->more_r)
228 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
230 cmp = -rfd->rset->scope;
232 cmp = rfd->rset->scope;
234 yaz_log(YLOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
235 rfd, p->more_l, p->more_r, cmp, p->tail);
236 (*kctrl->log_item)(YLOG_DEBUG, p->buf_l, "left ");
237 (*kctrl->log_item)(YLOG_DEBUG, p->buf_r, "right ");
241 memcpy (buf, p->buf_l, kctrl->key_size);
244 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
247 else if ( (cmp>0) && (cmp<rfd->rset->scope))
248 { /* typically cmp == 1 */
249 memcpy (buf, p->buf_r, kctrl->key_size);
252 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
255 yaz_log(YLOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
256 rfd, p->more_l, p->more_r, cmp);
257 key_logdump(YLOG_DEBUG,buf);
258 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
263 else if ( (cmp<0) && (-cmp<rfd->rset->scope))
265 memcpy (buf, p->buf_l, kctrl->key_size);
268 p->more_l = rset_read(p->rfd_l, p->buf_l,&p->term_l);
271 yaz_log(YLOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
272 rfd, p->more_l, p->more_r, cmp);
273 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
278 else if (cmp >= rfd->rset->scope )
282 memcpy (buf, p->buf_r, kctrl->key_size);
285 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
286 if (!p->more_r || (*kctrl->cmp)(p->buf_r, buf) > 1)
289 yaz_log(YLOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
290 rfd, p->more_l, p->more_r, cmp);
291 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
299 yaz_log(YLOG_DEBUG, "r_read_and [%p] about to forward R "
301 rfd, p->more_l, p->more_r, cmp);
303 if (p->more_r && p->more_l)
304 p->more_r = rset_forward( p->rfd_r, p->buf_r,
305 &p->term_r, p->buf_l);
307 return 0; /* no point in reading further */
314 memcpy (buf, p->buf_l, kctrl->key_size);
317 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
318 if (!p->more_l || (*kctrl->cmp)(p->buf_l, buf) > 1)
321 yaz_log(YLOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
322 rfd, p->more_l, p->more_r, cmp);
323 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
331 yaz_log(YLOG_DEBUG, "r_read_and [%p] about to forward L "
333 rfd, p->more_l, p->more_r, cmp);
335 if (p->more_r && p->more_l)
336 p->more_l = rset_forward(p->rfd_l, p->buf_l,
337 &p->term_l, p->buf_r);
339 return 0; /* no point in reading further */
344 yaz_log(YLOG_DEBUG, "r_read_and [%p] reached its end",rfd);
349 static int r_read_or (RSFD rfd, void *buf, TERMID *term)
351 struct rfd_private *p = (struct rfd_private *)rfd->priv;
352 const struct rset_key_control *kctrl = rfd->rset->keycontrol;
354 while (p->more_l || p->more_r)
358 if (p->more_l && p->more_r)
359 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
361 cmp = rfd->rset->scope;
363 cmp = -rfd->rset->scope;
366 memcpy (buf, p->buf_l, kctrl->key_size);
369 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
370 /* FIXME - is this right, should we not leave _r as it is */
371 /* and return that in the next read, so that ranking etc */
372 /* get to see both? */
373 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
375 yaz_log(YLOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
376 p->more_l, p->more_r, cmp);
377 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
384 memcpy (buf, p->buf_r, kctrl->key_size);
387 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
389 yaz_log(YLOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
390 p->more_l, p->more_r, cmp);
391 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
398 memcpy (buf, p->buf_l, kctrl->key_size);
401 p->more_l = rset_read( p->rfd_l, p->buf_l, &p->term_l);
403 yaz_log(YLOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
404 p->more_l, p->more_r, cmp);
405 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
414 static int r_read_not(RSFD rfd, void *buf, TERMID *term)
416 struct rfd_private *p = (struct rfd_private *)rfd->priv;
417 const struct rset_key_control *kctrl = rfd->rset->keycontrol;
419 while (p->more_l || p->more_r)
423 if (p->more_l && p->more_r)
424 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
426 cmp = rfd->rset->scope;
428 cmp = -rfd->rset->scope;
430 if (cmp <= -rfd->rset->scope)
432 memcpy (buf, p->buf_l, kctrl->key_size);
435 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
439 else if (cmp >= rfd->rset->scope) /* cmp >1 */
440 p->more_r = rset_forward( p->rfd_r, p->buf_r,
441 &p->term_r, p->buf_l);
443 { /* cmp== -1, 0, or 1 */
444 memcpy (buf, p->buf_l, kctrl->key_size);
449 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
452 cmp = (*kctrl->cmp)(p->buf_l, buf);
453 } while (abs(cmp)<rfd->rset->scope);
454 /* (cmp >= -1 && cmp <= 1) */
457 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
460 cmp = (*kctrl->cmp)(p->buf_r, buf);
461 } while (abs(cmp)<rfd->rset->scope);
462 /* (cmp >= -1 && cmp <= 1) */
469 static int r_write(RSFD rfd, const void *buf)
471 yaz_log(YLOG_FATAL, "bool set type is read-only");
475 static void r_pos(RSFD rfd, double *current, double *total)
477 struct rfd_private *p = (struct rfd_private *)rfd->priv;
483 rset_pos(p->rfd_l, &lcur, <ot);
484 rset_pos(p->rfd_r, &rcur, &rtot);
485 if ( (rtot<0) && (ltot<0)) { /*no position */
486 *current = rcur; /* return same as you got */
487 *total = rtot; /* probably -1 for not available */
490 rtot = rcur = 0; /* if only one useful, use it */
495 *current = *total = 0;
498 r = 1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
499 *current = (double) (p->hits);
500 *total = *current/r ;
502 yaz_log(YLOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
503 info->rset_l->control->desc, info->rset_r->control->desc,
504 *current, *total, r);
508 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
510 struct rset_private *info = (struct rset_private *) ct->priv;
511 rset_getterms(info->rset_l, terms, maxterms, curterm);
512 rset_getterms(info->rset_r, terms, maxterms, curterm);