1 /* $Id: rsbool.c,v 1.54 2005-04-26 10:09:38 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 =
86 struct rset_bool_info {
91 struct rset_bool_rfd {
104 static RSET rsbool_create_base(const struct rset_control *ctrl,
105 NMEM nmem, const struct key_control *kcontrol,
106 int scope, RSET rset_l, RSET rset_r)
108 RSET rnew = rset_create_base(ctrl, nmem, kcontrol, scope,0);
109 struct rset_bool_info *info;
110 info = (struct rset_bool_info *) nmem_malloc(rnew->nmem,sizeof(*info));
111 info->rset_l = rset_l;
112 info->rset_r = rset_r;
117 RSET rsbool_create_and( NMEM nmem, const struct key_control *kcontrol,
118 int scope, RSET rset_l, RSET rset_r)
120 return rsbool_create_base(&control_and, nmem, kcontrol,
125 RSET rsbool_create_or( NMEM nmem, const struct key_control *kcontrol,
126 int scope, RSET rset_l, RSET rset_r)
128 return rsbool_create_base(&control_or, nmem, kcontrol,
129 scope, rset_l, rset_r);
132 RSET rsbool_create_not( NMEM nmem, const struct key_control *kcontrol,
133 int scope, RSET rset_l, RSET rset_r)
135 return rsbool_create_base(&control_not, nmem, kcontrol,
136 scope, rset_l, rset_r);
139 static void r_delete (RSET ct)
141 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
142 rset_delete (info->rset_l);
143 rset_delete (info->rset_r);
147 static RSFD r_open (RSET ct, int flag)
149 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
151 struct rset_bool_rfd *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 rset_bool_rfd *)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 rset_bool_info *info = (struct rset_bool_info*)(rfd->rset->priv); */
183 struct rset_bool_rfd *prfd=(struct rset_bool_rfd *)rfd->priv;
185 rset_close (prfd->rfd_l);
186 rset_close (prfd->rfd_r);
187 rfd_delete_base(rfd);
192 static int r_forward(RSFD rfd, void *buf, TERMID *term,
193 const void *untilbuf)
195 struct rset_bool_rfd *p = (struct rset_bool_rfd *)rfd->priv;
196 const struct key_control *kctrl=rfd->rset->keycontrol;
198 if ( p->more_l && ((kctrl->cmp)(untilbuf,p->buf_l)>=rfd->rset->scope) )
199 p->more_l = rset_forward(p->rfd_l, p->buf_l, &p->term_l, untilbuf);
200 if ( p->more_r && ((kctrl->cmp)(untilbuf,p->buf_r)>=rfd->rset->scope))
201 p->more_r = rset_forward(p->rfd_r, p->buf_r, &p->term_r, untilbuf);
203 return rset_read(rfd,buf,term);
222 static int r_read_and(RSFD rfd, void *buf, TERMID *term)
224 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
225 const struct key_control *kctrl=rfd->rset->keycontrol;
227 while (p->more_l || p->more_r)
231 if (p->more_l && p->more_r)
232 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
234 cmp = -rfd->rset->scope;
236 cmp = rfd->rset->scope;
238 yaz_log(YLOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
239 rfd, p->more_l, p->more_r, cmp, p->tail);
240 (*kctrl->log_item)(YLOG_DEBUG, p->buf_l, "left ");
241 (*kctrl->log_item)(YLOG_DEBUG, p->buf_r, "right ");
245 memcpy (buf, p->buf_l, kctrl->key_size);
248 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
251 else if ( (cmp>0) && (cmp<rfd->rset->scope))
252 { /* typically cmp == 1 */
253 memcpy (buf, p->buf_r, kctrl->key_size);
256 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
259 yaz_log(YLOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
260 rfd, p->more_l, p->more_r, cmp);
261 key_logdump(YLOG_DEBUG,buf);
262 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
267 else if ( (cmp<0) && (-cmp<rfd->rset->scope))
269 memcpy (buf, p->buf_l, kctrl->key_size);
272 p->more_l = rset_read(p->rfd_l, p->buf_l,&p->term_l);
275 yaz_log(YLOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
276 rfd, p->more_l, p->more_r, cmp);
277 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
282 else if (cmp >= rfd->rset->scope )
286 memcpy (buf, p->buf_r, kctrl->key_size);
289 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
290 if (!p->more_r || (*kctrl->cmp)(p->buf_r, buf) > 1)
293 yaz_log(YLOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
294 rfd, p->more_l, p->more_r, cmp);
295 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
303 yaz_log(YLOG_DEBUG, "r_read_and [%p] about to forward R "
305 rfd, p->more_l, p->more_r, cmp);
307 if (p->more_r && p->more_l)
308 p->more_r = rset_forward( p->rfd_r, p->buf_r,
309 &p->term_r, p->buf_l);
311 return 0; /* no point in reading further */
318 memcpy (buf, p->buf_l, kctrl->key_size);
321 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
322 if (!p->more_l || (*kctrl->cmp)(p->buf_l, buf) > 1)
325 yaz_log(YLOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
326 rfd, p->more_l, p->more_r, cmp);
327 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
335 yaz_log(YLOG_DEBUG, "r_read_and [%p] about to forward L "
337 rfd, p->more_l, p->more_r, cmp);
339 if (p->more_r && p->more_l)
340 p->more_l = rset_forward(p->rfd_l, p->buf_l,
341 &p->term_l, p->buf_r);
343 return 0; /* no point in reading further */
348 yaz_log(YLOG_DEBUG, "r_read_and [%p] reached its end",rfd);
353 static int r_read_or (RSFD rfd, void *buf, TERMID *term)
355 struct rset_bool_rfd *p = (struct rset_bool_rfd *)rfd->priv;
356 const struct key_control *kctrl = rfd->rset->keycontrol;
358 while (p->more_l || p->more_r)
362 if (p->more_l && p->more_r)
363 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
365 cmp = rfd->rset->scope;
367 cmp = -rfd->rset->scope;
370 memcpy (buf, p->buf_l, kctrl->key_size);
373 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
374 /* FIXME - is this right, should we not leave _r as it is */
375 /* and return that in the next read, so that ranking etc */
376 /* get to see both? */
377 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
379 yaz_log(YLOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
380 p->more_l, p->more_r, cmp);
381 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
388 memcpy (buf, p->buf_r, kctrl->key_size);
391 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
393 yaz_log(YLOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
394 p->more_l, p->more_r, cmp);
395 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
402 memcpy (buf, p->buf_l, kctrl->key_size);
405 p->more_l = rset_read( p->rfd_l, p->buf_l, &p->term_l);
407 yaz_log(YLOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
408 p->more_l, p->more_r, cmp);
409 (*kctrl->log_item)(YLOG_DEBUG, buf, "");
418 static int r_read_not(RSFD rfd, void *buf, TERMID *term)
420 struct rset_bool_rfd *p = (struct rset_bool_rfd *)rfd->priv;
421 const struct key_control *kctrl = rfd->rset->keycontrol;
423 while (p->more_l || p->more_r)
427 if (p->more_l && p->more_r)
428 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
430 cmp = rfd->rset->scope;
432 cmp = -rfd->rset->scope;
434 if (cmp <= -rfd->rset->scope)
436 memcpy (buf, p->buf_l, kctrl->key_size);
439 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
443 else if (cmp >= rfd->rset->scope) /* cmp >1 */
444 p->more_r = rset_forward( p->rfd_r, p->buf_r,
445 &p->term_r, p->buf_l);
447 { /* cmp== -1, 0, or 1 */
448 memcpy (buf, p->buf_l, kctrl->key_size);
453 p->more_l = rset_read(p->rfd_l, p->buf_l, &p->term_l);
456 cmp = (*kctrl->cmp)(p->buf_l, buf);
457 } while (abs(cmp)<rfd->rset->scope);
458 /* (cmp >= -1 && cmp <= 1) */
461 p->more_r = rset_read(p->rfd_r, p->buf_r, &p->term_r);
464 cmp = (*kctrl->cmp)(p->buf_r, buf);
465 } while (abs(cmp)<rfd->rset->scope);
466 /* (cmp >= -1 && cmp <= 1) */
473 static int r_write(RSFD rfd, const void *buf)
475 yaz_log(YLOG_FATAL, "bool set type is read-only");
479 static void r_pos(RSFD rfd, double *current, double *total)
481 struct rset_bool_rfd *p = (struct rset_bool_rfd *)rfd->priv;
487 rset_pos(p->rfd_l, &lcur, <ot);
488 rset_pos(p->rfd_r, &rcur, &rtot);
489 if ( (rtot<0) && (ltot<0)) { /*no position */
490 *current = rcur; /* return same as you got */
491 *total = rtot; /* probably -1 for not available */
494 rtot = rcur = 0; /* if only one useful, use it */
499 *current = *total = 0;
502 r = 1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
503 *current = (double) (p->hits);
504 *total = *current/r ;
506 yaz_log(YLOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
507 info->rset_l->control->desc, info->rset_r->control->desc,
508 *current, *total, r);
512 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
514 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
515 rset_getterms(info->rset_l, terms, maxterms, curterm);
516 rset_getterms(info->rset_r, terms, maxterms, curterm);