1 /* $Id: rsbool.c,v 1.46 2004-09-01 15:01:32 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 void r_rewind (RSFD rfd);
39 static int r_forward(RSFD rfd, void *buf, const void *untilbuf);
40 static void r_pos (RSFD rfd, double *current, double *total);
41 static int r_read_and (RSFD rfd, void *buf);
42 static int r_read_or (RSFD rfd, void *buf);
43 static int r_read_not (RSFD rfd, void *buf);
44 static int r_write (RSFD rfd, const void *buf);
46 static const struct rset_control control_and =
59 static const struct rset_control control_or =
72 static const struct rset_control control_not =
86 const struct rset_control *rset_kind_and = &control_and;
87 const struct rset_control *rset_kind_or = &control_or;
88 const struct rset_control *rset_kind_not = &control_not;
90 struct rset_bool_info {
95 struct rset_bool_rfd {
106 static RSET rsbool_create_base( const struct rset_control *ctrl,
107 NMEM nmem, const struct key_control *kcontrol,
108 RSET rset_l, RSET rset_r)
110 RSET rnew=rset_create_base(ctrl, nmem, kcontrol);
111 struct rset_bool_info *info;
112 info = (struct rset_bool_info *) nmem_malloc(rnew->nmem,sizeof(*info));
113 info->rset_l = rset_l;
114 info->rset_r = rset_r;
120 RSET rsbool_create_and( NMEM nmem, const struct key_control *kcontrol,
121 RSET rset_l, RSET rset_r)
123 return rsbool_create_base(rset_kind_and, nmem, kcontrol,
127 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,
134 RSET rsbool_create_not( NMEM nmem, const struct key_control *kcontrol,
135 RSET rset_l, RSET rset_r)
137 return rsbool_create_base(rset_kind_not, nmem, kcontrol,
141 static void r_delete (RSET ct)
143 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
144 rset_delete (info->rset_l);
145 rset_delete (info->rset_r);
149 static RSFD r_open (RSET ct, int flag)
151 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
153 struct rset_bool_rfd *p;
156 if (flag & RSETF_WRITE)
158 logf (LOG_FATAL, "bool set type is read-only");
161 rfd = rfd_create_base(ct);
163 p=(struct rset_bool_rfd *)rfd->priv;
165 p=nmem_malloc(ct->nmem,sizeof(*p));
167 p->buf_l = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
168 p->buf_r = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
171 logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
174 p->rfd_l = rset_open (info->rset_l, RSETF_READ);
175 p->rfd_r = rset_open (info->rset_r, RSETF_READ);
176 p->more_l = rset_read (p->rfd_l, p->buf_l);
177 p->more_r = rset_read (p->rfd_r, p->buf_r);
182 static void r_close (RSFD rfd)
184 /* struct rset_bool_info *info = (struct rset_bool_info*)(rfd->rset->priv); */
185 struct rset_bool_rfd *prfd=(struct rset_bool_rfd *)rfd->priv;
187 rset_close (prfd->rfd_l);
188 rset_close (prfd->rfd_r);
189 rfd_delete_base(rfd);
193 static void r_rewind (RSFD rfd)
195 /* struct rset_bool_info *info = (struct rset_bool_info*)(rfd->rset->priv); */
196 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
198 logf (LOG_DEBUG, "rsbool_rewind");
199 rset_rewind (p->rfd_l);
200 rset_rewind (p->rfd_r);
201 p->more_l = rset_read (p->rfd_l, p->buf_l);
202 p->more_r = rset_read (p->rfd_r, p->buf_r);
206 static int r_forward (RSFD rfd, void *buf,
207 const void *untilbuf)
209 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
210 const struct key_control *kctrl=rfd->rset->keycontrol;
212 if ( p->more_l && ((kctrl->cmp)(untilbuf,p->buf_l)==2) )
213 p->more_l = rset_forward(p->rfd_l, p->buf_l, untilbuf);
214 if ( p->more_r && ((kctrl->cmp)(untilbuf,p->buf_r)==2))
215 p->more_r = rset_forward(p->rfd_r, p->buf_r, untilbuf);
217 return rset_read(rfd,buf);
236 static int r_read_and (RSFD rfd, void *buf)
238 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
239 const struct key_control *kctrl=rfd->rset->keycontrol;
241 while (p->more_l || p->more_r)
245 if (p->more_l && p->more_r)
246 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
252 logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
253 rfd, p->more_l, p->more_r, cmp, p->tail);
254 (*kctrl->log_item)(LOG_DEBUG, p->buf_l, "left ");
255 (*kctrl->log_item)(LOG_DEBUG, p->buf_r, "right ");
259 memcpy (buf, p->buf_l, kctrl->key_size);
260 p->more_l = rset_read (p->rfd_l, p->buf_l);
265 memcpy (buf, p->buf_r, kctrl->key_size);
266 p->more_r = rset_read (p->rfd_r, p->buf_r);
269 logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
270 rfd, p->more_l, p->more_r, cmp);
271 key_logdump(LOG_DEBUG,buf);
272 (*kctrl->log_item)(LOG_DEBUG, buf, "");
279 memcpy (buf, p->buf_l, kctrl->key_size);
280 p->more_l = rset_read (p->rfd_l, p->buf_l);
283 logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
284 rfd, p->more_l, p->more_r, cmp);
285 (*kctrl->log_item)(LOG_DEBUG, buf, "");
290 else if (cmp > 1) /* cmp == 2 */
294 memcpy (buf, p->buf_r, kctrl->key_size);
295 p->more_r = rset_read (p->rfd_r, p->buf_r);
296 if (!p->more_r || (*kctrl->cmp)(p->buf_r, buf) > 1)
299 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
300 rfd, p->more_l, p->more_r, cmp);
301 (*kctrl->log_item)(LOG_DEBUG, buf, "");
309 logf (LOG_DEBUG, "r_read_and [%p] about to forward R "
311 rfd, p->more_l, p->more_r, cmp);
313 if (p->more_r && p->more_l)
314 p->more_r = rset_forward( p->rfd_r, p->buf_r, p->buf_l);
316 return 0; /* no point in reading further */
323 memcpy (buf, p->buf_l, kctrl->key_size);
324 p->more_l = rset_read (p->rfd_l, p->buf_l);
325 if (!p->more_l || (*kctrl->cmp)(p->buf_l, buf) > 1)
328 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
329 rfd, p->more_l, p->more_r, cmp);
330 (*kctrl->log_item)(LOG_DEBUG, buf, "");
338 logf (LOG_DEBUG, "r_read_and [%p] about to forward L "
340 rfd, p->more_l, p->more_r, cmp);
342 if (p->more_r && p->more_l)
343 p->more_l = rset_forward(p->rfd_l, p->buf_l, p->buf_r);
345 return 0; /* no point in reading further */
350 logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
355 static int r_read_or (RSFD rfd, void *buf)
357 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
358 const struct key_control *kctrl=rfd->rset->keycontrol;
360 while (p->more_l || p->more_r)
364 if (p->more_l && p->more_r)
365 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
372 memcpy (buf, p->buf_l, kctrl->key_size);
373 p->more_l = rset_read (p->rfd_l, p->buf_l);
374 p->more_r = rset_read (p->rfd_r, p->buf_r);
376 logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
377 p->more_l, p->more_r, cmp);
378 (*kctrl->log_item)(LOG_DEBUG, buf, "");
385 memcpy (buf, p->buf_r, kctrl->key_size);
386 p->more_r = rset_read (p->rfd_r, p->buf_r);
388 logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
389 p->more_l, p->more_r, cmp);
390 (*kctrl->log_item)(LOG_DEBUG, buf, "");
397 memcpy (buf, p->buf_l, kctrl->key_size);
398 p->more_l = rset_read ( p->rfd_l, p->buf_l);
400 logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
401 p->more_l, p->more_r, cmp);
402 (*kctrl->log_item)(LOG_DEBUG, buf, "");
411 static int r_read_not (RSFD rfd, void *buf)
413 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
414 const struct key_control *kctrl=rfd->rset->keycontrol;
416 while (p->more_l || p->more_r)
420 if (p->more_l && p->more_r)
421 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
428 memcpy (buf, p->buf_l, kctrl->key_size);
429 p->more_l = rset_read (p->rfd_l, p->buf_l);
434 p->more_r = rset_forward( p->rfd_r, p->buf_r, p->buf_l);
437 memcpy (buf, p->buf_l, kctrl->key_size);
440 p->more_l = rset_read (p->rfd_l, p->buf_l);
443 cmp = (*kctrl->cmp)(p->buf_l, buf);
444 } while (cmp >= -1 && cmp <= 1);
447 p->more_r = rset_read (p->rfd_r, p->buf_r);
450 cmp = (*kctrl->cmp)(p->buf_r, buf);
451 } while (cmp >= -1 && cmp <= 1);
458 static int r_write (RSFD rfd, const void *buf)
460 logf (LOG_FATAL, "bool set type is read-only");
464 static void r_pos (RSFD rfd, double *current, double *total)
466 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
471 rset_pos(p->rfd_l, &lcur, <ot);
472 rset_pos(p->rfd_r, &rcur, &rtot);
473 if ( (rtot<0) && (ltot<0)) { /*no position */
474 *current=rcur; /* return same as you got */
475 *total=rtot; /* probably -1 for not available */
477 if ( rtot<0) { rtot=0; rcur=0;} /* if only one useful, use it */
478 if ( ltot<0) { ltot=0; lcur=0;}
479 if ( rtot+ltot < 1 ) { /* empty rset */
484 r=1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
485 *current=(double) (p->hits);
488 yaz_log(LOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
489 info->rset_l->control->desc, info->rset_r->control->desc,
490 *current, *total, r);