1 /* $Id: rsbool.c,v 1.47 2004-09-09 10:08:06 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,
109 RSET rset_l, RSET rset_r)
111 RSET rnew=rset_create_base(ctrl, nmem, kcontrol, scope);
112 struct rset_bool_info *info;
113 info = (struct rset_bool_info *) nmem_malloc(rnew->nmem,sizeof(*info));
114 info->rset_l = rset_l;
115 info->rset_r = rset_r;
121 RSET rsbool_create_and( NMEM nmem, const struct key_control *kcontrol,
123 RSET rset_l, RSET rset_r)
125 return rsbool_create_base(rset_kind_and, nmem, kcontrol,
130 RSET rsbool_create_or( NMEM nmem, const struct key_control *kcontrol,
132 RSET rset_l, RSET rset_r)
134 return rsbool_create_base(rset_kind_or, nmem, kcontrol,
139 RSET rsbool_create_not( NMEM nmem, const struct key_control *kcontrol,
141 RSET rset_l, RSET rset_r)
143 return rsbool_create_base(rset_kind_not, nmem, kcontrol,
148 static void r_delete (RSET ct)
150 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
151 rset_delete (info->rset_l);
152 rset_delete (info->rset_r);
156 static RSFD r_open (RSET ct, int flag)
158 struct rset_bool_info *info = (struct rset_bool_info *) ct->priv;
160 struct rset_bool_rfd *p;
163 if (flag & RSETF_WRITE)
165 logf (LOG_FATAL, "bool set type is read-only");
168 rfd = rfd_create_base(ct);
170 p=(struct rset_bool_rfd *)rfd->priv;
172 p=nmem_malloc(ct->nmem,sizeof(*p));
174 p->buf_l = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
175 p->buf_r = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
178 logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
181 p->rfd_l = rset_open (info->rset_l, RSETF_READ);
182 p->rfd_r = rset_open (info->rset_r, RSETF_READ);
183 p->more_l = rset_read (p->rfd_l, p->buf_l);
184 p->more_r = rset_read (p->rfd_r, p->buf_r);
189 static void r_close (RSFD rfd)
191 /* struct rset_bool_info *info = (struct rset_bool_info*)(rfd->rset->priv); */
192 struct rset_bool_rfd *prfd=(struct rset_bool_rfd *)rfd->priv;
194 rset_close (prfd->rfd_l);
195 rset_close (prfd->rfd_r);
196 rfd_delete_base(rfd);
200 static void r_rewind (RSFD rfd)
202 /* struct rset_bool_info *info = (struct rset_bool_info*)(rfd->rset->priv); */
203 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
205 logf (LOG_DEBUG, "rsbool_rewind");
206 rset_rewind (p->rfd_l);
207 rset_rewind (p->rfd_r);
208 p->more_l = rset_read (p->rfd_l, p->buf_l);
209 p->more_r = rset_read (p->rfd_r, p->buf_r);
213 static int r_forward (RSFD rfd, void *buf,
214 const void *untilbuf)
216 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
217 const struct key_control *kctrl=rfd->rset->keycontrol;
219 if ( p->more_l && ((kctrl->cmp)(untilbuf,p->buf_l)>=rfd->rset->scope) )
220 p->more_l = rset_forward(p->rfd_l, p->buf_l, untilbuf);
221 if ( p->more_r && ((kctrl->cmp)(untilbuf,p->buf_r)>=rfd->rset->scope))
222 p->more_r = rset_forward(p->rfd_r, p->buf_r, untilbuf);
224 return rset_read(rfd,buf);
243 static int r_read_and (RSFD rfd, void *buf)
245 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
246 const struct key_control *kctrl=rfd->rset->keycontrol;
248 while (p->more_l || p->more_r)
252 if (p->more_l && p->more_r)
253 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
255 cmp = -rfd->rset->scope;
257 cmp = rfd->rset->scope;
259 logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
260 rfd, p->more_l, p->more_r, cmp, p->tail);
261 (*kctrl->log_item)(LOG_DEBUG, p->buf_l, "left ");
262 (*kctrl->log_item)(LOG_DEBUG, p->buf_r, "right ");
266 memcpy (buf, p->buf_l, kctrl->key_size);
267 p->more_l = rset_read (p->rfd_l, p->buf_l);
270 else if ( (cmp>0) && (cmp<rfd->rset->scope))
271 { /* typically cmp == 1 */
272 memcpy (buf, p->buf_r, kctrl->key_size);
273 p->more_r = rset_read (p->rfd_r, p->buf_r);
276 logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
277 rfd, p->more_l, p->more_r, cmp);
278 key_logdump(LOG_DEBUG,buf);
279 (*kctrl->log_item)(LOG_DEBUG, buf, "");
284 else if ( (cmp<0) && (-cmp<rfd->rset->scope))
286 memcpy (buf, p->buf_l, kctrl->key_size);
287 p->more_l = rset_read (p->rfd_l, p->buf_l);
290 logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
291 rfd, p->more_l, p->more_r, cmp);
292 (*kctrl->log_item)(LOG_DEBUG, buf, "");
297 else if (cmp >= rfd->rset->scope )
301 memcpy (buf, p->buf_r, kctrl->key_size);
302 p->more_r = rset_read (p->rfd_r, p->buf_r);
303 if (!p->more_r || (*kctrl->cmp)(p->buf_r, buf) > 1)
306 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
307 rfd, p->more_l, p->more_r, cmp);
308 (*kctrl->log_item)(LOG_DEBUG, buf, "");
316 logf (LOG_DEBUG, "r_read_and [%p] about to forward R "
318 rfd, p->more_l, p->more_r, cmp);
320 if (p->more_r && p->more_l)
321 p->more_r = rset_forward( p->rfd_r, p->buf_r, p->buf_l);
323 return 0; /* no point in reading further */
330 memcpy (buf, p->buf_l, kctrl->key_size);
331 p->more_l = rset_read (p->rfd_l, p->buf_l);
332 if (!p->more_l || (*kctrl->cmp)(p->buf_l, buf) > 1)
335 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
336 rfd, p->more_l, p->more_r, cmp);
337 (*kctrl->log_item)(LOG_DEBUG, buf, "");
345 logf (LOG_DEBUG, "r_read_and [%p] about to forward L "
347 rfd, p->more_l, p->more_r, cmp);
349 if (p->more_r && p->more_l)
350 p->more_l = rset_forward(p->rfd_l, p->buf_l, p->buf_r);
352 return 0; /* no point in reading further */
357 logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
362 static int r_read_or (RSFD rfd, void *buf)
364 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
365 const struct key_control *kctrl=rfd->rset->keycontrol;
367 while (p->more_l || p->more_r)
371 if (p->more_l && p->more_r)
372 cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
374 cmp = rfd->rset->scope;
376 cmp = -rfd->rset->scope;
379 memcpy (buf, p->buf_l, kctrl->key_size);
380 p->more_l = rset_read (p->rfd_l, p->buf_l);
381 p->more_r = rset_read (p->rfd_r, p->buf_r);
383 logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
384 p->more_l, p->more_r, cmp);
385 (*kctrl->log_item)(LOG_DEBUG, buf, "");
392 memcpy (buf, p->buf_r, kctrl->key_size);
393 p->more_r = rset_read (p->rfd_r, p->buf_r);
395 logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
396 p->more_l, p->more_r, cmp);
397 (*kctrl->log_item)(LOG_DEBUG, buf, "");
404 memcpy (buf, p->buf_l, kctrl->key_size);
405 p->more_l = rset_read ( p->rfd_l, p->buf_l);
407 logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
408 p->more_l, p->more_r, cmp);
409 (*kctrl->log_item)(LOG_DEBUG, buf, "");
418 static int r_read_not (RSFD rfd, void *buf)
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);
437 p->more_l = rset_read (p->rfd_l, p->buf_l);
441 else if (cmp >= rfd->rset->scope) /* cmp >1 */
442 p->more_r = rset_forward( p->rfd_r, p->buf_r, p->buf_l);
444 { /* cmp== -1, 0, or 1 */
445 memcpy (buf, p->buf_l, kctrl->key_size);
448 p->more_l = rset_read (p->rfd_l, p->buf_l);
451 cmp = (*kctrl->cmp)(p->buf_l, buf);
452 } while (abs(cmp)<rfd->rset->scope);
453 /* (cmp >= -1 && cmp <= 1) */
456 p->more_r = rset_read (p->rfd_r, p->buf_r);
459 cmp = (*kctrl->cmp)(p->buf_r, buf);
460 } while (abs(cmp)<rfd->rset->scope);
461 /* (cmp >= -1 && cmp <= 1) */
468 static int r_write (RSFD rfd, const void *buf)
470 logf (LOG_FATAL, "bool set type is read-only");
474 static void r_pos (RSFD rfd, double *current, double *total)
476 struct rset_bool_rfd *p=(struct rset_bool_rfd *)rfd->priv;
481 rset_pos(p->rfd_l, &lcur, <ot);
482 rset_pos(p->rfd_r, &rcur, &rtot);
483 if ( (rtot<0) && (ltot<0)) { /*no position */
484 *current=rcur; /* return same as you got */
485 *total=rtot; /* probably -1 for not available */
487 if ( rtot<0) { rtot=0; rcur=0;} /* if only one useful, use it */
488 if ( ltot<0) { ltot=0; lcur=0;}
489 if ( rtot+ltot < 1 ) { /* empty rset */
494 r=1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
495 *current=(double) (p->hits);
498 yaz_log(LOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
499 info->rset_l->control->desc, info->rset_r->control->desc,
500 *current, *total, r);