1 /* $Id: rsbool.c,v 1.48 2004-09-30 09:53:05 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, const void *untilbuf);
39 static void r_pos (RSFD rfd, double *current, double *total);
40 static int r_read_and (RSFD rfd, void *buf);
41 static int r_read_or (RSFD rfd, void *buf);
42 static int r_read_not (RSFD rfd, void *buf);
43 static int r_write (RSFD rfd, const void *buf);
45 static const struct rset_control control_and =
57 static const struct rset_control control_or =
69 static const struct rset_control control_not =
82 const struct rset_control *rset_kind_and = &control_and;
83 const struct rset_control *rset_kind_or = &control_or;
84 const struct rset_control *rset_kind_not = &control_not;
86 struct rset_bool_info {
91 struct rset_bool_rfd {
102 static RSET rsbool_create_base( const struct rset_control *ctrl,
103 NMEM nmem, const struct key_control *kcontrol,
105 RSET rset_l, RSET rset_r)
107 RSET rnew=rset_create_base(ctrl, nmem, kcontrol, scope);
108 struct rset_bool_info *info;
109 info = (struct rset_bool_info *) nmem_malloc(rnew->nmem,sizeof(*info));
110 info->rset_l = rset_l;
111 info->rset_r = rset_r;
117 RSET rsbool_create_and( NMEM nmem, const struct key_control *kcontrol,
119 RSET rset_l, RSET rset_r)
121 return rsbool_create_base(rset_kind_and, nmem, kcontrol,
126 RSET rsbool_create_or( NMEM nmem, const struct key_control *kcontrol,
128 RSET rset_l, RSET rset_r)
130 return rsbool_create_base(rset_kind_or, nmem, kcontrol,
135 RSET rsbool_create_not( NMEM nmem, const struct key_control *kcontrol,
137 RSET rset_l, RSET rset_r)
139 return rsbool_create_base(rset_kind_not, nmem, kcontrol,
144 static void r_delete (RSET ct)
146 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
147 rset_delete (info->rset_l);
148 rset_delete (info->rset_r);
152 static RSFD r_open (RSET ct, int flag)
154 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
156 struct rset_bool_rfd *p;
159 if (flag & RSETF_WRITE)
161 logf (LOG_FATAL, "bool set type is read-only");
164 rfd = rfd_create_base(ct);
166 p=(struct rset_bool_rfd *)rfd->priv;
168 p=nmem_malloc(ct->nmem,sizeof(*p));
170 p->buf_l = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
171 p->buf_r = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
174 logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
177 p->rfd_l = rset_open (info->rset_l, RSETF_READ);
178 p->rfd_r = rset_open (info->rset_r, RSETF_READ);
179 p->more_l = rset_read (p->rfd_l, p->buf_l);
180 p->more_r = rset_read (p->rfd_r, p->buf_r);
185 static void r_close (RSFD rfd)
187 /* struct rset_bool_info *info = (struct rset_bool_info*)(rfd->rset->priv); */
188 struct rset_bool_rfd *prfd=(struct rset_bool_rfd *)rfd->priv;
190 rset_close (prfd->rfd_l);
191 rset_close (prfd->rfd_r);
192 rfd_delete_base(rfd);
197 static int r_forward (RSFD rfd, void *buf,
198 const void *untilbuf)
200 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
201 const struct key_control *kctrl=rfd->rset->keycontrol;
203 if ( p->more_l && ((kctrl->cmp)(untilbuf,p->buf_l)>=rfd->rset->scope) )
204 p->more_l = rset_forward(p->rfd_l, p->buf_l, untilbuf);
205 if ( p->more_r && ((kctrl->cmp)(untilbuf,p->buf_r)>=rfd->rset->scope))
206 p->more_r = rset_forward(p->rfd_r, p->buf_r, untilbuf);
208 return rset_read(rfd,buf);
227 static int r_read_and (RSFD rfd, void *buf)
229 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
230 const struct key_control *kctrl=rfd->rset->keycontrol;
232 while (p->more_l || p->more_r)
236 if (p->more_l && p->more_r)
237 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
239 cmp = -rfd->rset->scope;
241 cmp = rfd->rset->scope;
243 logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
244 rfd, p->more_l, p->more_r, cmp, p->tail);
245 (*kctrl->log_item)(LOG_DEBUG, p->buf_l, "left ");
246 (*kctrl->log_item)(LOG_DEBUG, p->buf_r, "right ");
250 memcpy (buf, p->buf_l, kctrl->key_size);
251 p->more_l = rset_read (p->rfd_l, p->buf_l);
254 else if ( (cmp>0) && (cmp<rfd->rset->scope))
255 { /* typically cmp == 1 */
256 memcpy (buf, p->buf_r, kctrl->key_size);
257 p->more_r = rset_read (p->rfd_r, p->buf_r);
260 logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
261 rfd, p->more_l, p->more_r, cmp);
262 key_logdump(LOG_DEBUG,buf);
263 (*kctrl->log_item)(LOG_DEBUG, buf, "");
268 else if ( (cmp<0) && (-cmp<rfd->rset->scope))
270 memcpy (buf, p->buf_l, kctrl->key_size);
271 p->more_l = rset_read (p->rfd_l, p->buf_l);
274 logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
275 rfd, p->more_l, p->more_r, cmp);
276 (*kctrl->log_item)(LOG_DEBUG, buf, "");
281 else if (cmp >= rfd->rset->scope )
285 memcpy (buf, p->buf_r, kctrl->key_size);
286 p->more_r = rset_read (p->rfd_r, p->buf_r);
287 if (!p->more_r || (*kctrl->cmp)(p->buf_r, buf) > 1)
290 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
291 rfd, p->more_l, p->more_r, cmp);
292 (*kctrl->log_item)(LOG_DEBUG, buf, "");
300 logf (LOG_DEBUG, "r_read_and [%p] about to forward R "
302 rfd, p->more_l, p->more_r, cmp);
304 if (p->more_r && p->more_l)
305 p->more_r = rset_forward( p->rfd_r, p->buf_r, p->buf_l);
307 return 0; /* no point in reading further */
314 memcpy (buf, p->buf_l, kctrl->key_size);
315 p->more_l = rset_read (p->rfd_l, p->buf_l);
316 if (!p->more_l || (*kctrl->cmp)(p->buf_l, buf) > 1)
319 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
320 rfd, p->more_l, p->more_r, cmp);
321 (*kctrl->log_item)(LOG_DEBUG, buf, "");
329 logf (LOG_DEBUG, "r_read_and [%p] about to forward L "
331 rfd, p->more_l, p->more_r, cmp);
333 if (p->more_r && p->more_l)
334 p->more_l = rset_forward(p->rfd_l, p->buf_l, p->buf_r);
336 return 0; /* no point in reading further */
341 logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
346 static int r_read_or (RSFD rfd, void *buf)
348 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
349 const struct key_control *kctrl=rfd->rset->keycontrol;
351 while (p->more_l || p->more_r)
355 if (p->more_l && p->more_r)
356 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
358 cmp = rfd->rset->scope;
360 cmp = -rfd->rset->scope;
363 memcpy (buf, p->buf_l, kctrl->key_size);
364 p->more_l = rset_read (p->rfd_l, p->buf_l);
365 p->more_r = rset_read (p->rfd_r, p->buf_r);
367 logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
368 p->more_l, p->more_r, cmp);
369 (*kctrl->log_item)(LOG_DEBUG, buf, "");
376 memcpy (buf, p->buf_r, kctrl->key_size);
377 p->more_r = rset_read (p->rfd_r, p->buf_r);
379 logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
380 p->more_l, p->more_r, cmp);
381 (*kctrl->log_item)(LOG_DEBUG, buf, "");
388 memcpy (buf, p->buf_l, kctrl->key_size);
389 p->more_l = rset_read ( p->rfd_l, p->buf_l);
391 logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
392 p->more_l, p->more_r, cmp);
393 (*kctrl->log_item)(LOG_DEBUG, buf, "");
402 static int r_read_not (RSFD rfd, void *buf)
404 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
405 const struct key_control *kctrl=rfd->rset->keycontrol;
407 while (p->more_l || p->more_r)
411 if (p->more_l && p->more_r)
412 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
414 cmp = rfd->rset->scope;
416 cmp = -rfd->rset->scope;
418 if (cmp <= -rfd->rset->scope)
420 memcpy (buf, p->buf_l, kctrl->key_size);
421 p->more_l = rset_read (p->rfd_l, p->buf_l);
425 else if (cmp >= rfd->rset->scope) /* cmp >1 */
426 p->more_r = rset_forward( p->rfd_r, p->buf_r, p->buf_l);
428 { /* cmp== -1, 0, or 1 */
429 memcpy (buf, p->buf_l, kctrl->key_size);
432 p->more_l = rset_read (p->rfd_l, p->buf_l);
435 cmp = (*kctrl->cmp)(p->buf_l, buf);
436 } while (abs(cmp)<rfd->rset->scope);
437 /* (cmp >= -1 && cmp <= 1) */
440 p->more_r = rset_read (p->rfd_r, p->buf_r);
443 cmp = (*kctrl->cmp)(p->buf_r, buf);
444 } while (abs(cmp)<rfd->rset->scope);
445 /* (cmp >= -1 && cmp <= 1) */
452 static int r_write (RSFD rfd, const void *buf)
454 logf (LOG_FATAL, "bool set type is read-only");
458 static void r_pos (RSFD rfd, double *current, double *total)
460 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
465 rset_pos(p->rfd_l, &lcur, <ot);
466 rset_pos(p->rfd_r, &rcur, &rtot);
467 if ( (rtot<0) && (ltot<0)) { /*no position */
468 *current=rcur; /* return same as you got */
469 *total=rtot; /* probably -1 for not available */
471 if ( rtot<0) { rtot=0; rcur=0;} /* if only one useful, use it */
472 if ( ltot<0) { ltot=0; lcur=0;}
473 if ( rtot+ltot < 1 ) { /* empty rset */
478 r=1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
479 *current=(double) (p->hits);
482 yaz_log(LOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
483 info->rset_l->control->desc, info->rset_r->control->desc,
484 *current, *total, r);