1 /* $Id: rsbool.c,v 1.38 2004-08-20 14:44:46 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
36 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
37 static RSFD r_open (RSET ct, int flag);
38 static void r_close (RSFD rfd);
39 static void r_delete (RSET ct);
40 static void r_rewind (RSFD rfd);
41 static int r_forward(RSET ct, RSFD rfd, void *buf,
42 int (*cmpfunc)(const void *p1, const void *p2),
43 const void *untilbuf);
44 static void r_pos (RSFD rfd, double *current, double *total);
45 static int r_read_and (RSFD rfd, void *buf);
46 static int r_read_or (RSFD rfd, void *buf);
47 static int r_read_not (RSFD rfd, void *buf);
48 static int r_write (RSFD rfd, const void *buf);
50 static const struct rset_control control_and =
64 static const struct rset_control control_or =
78 static const struct rset_control control_not =
93 const struct rset_control *rset_kind_and = &control_and;
94 const struct rset_control *rset_kind_or = &control_or;
95 const struct rset_control *rset_kind_not = &control_not;
97 struct rset_bool_info {
101 int (*cmp)(const void *p1, const void *p2);
102 void (*log_item)(int logmask, const void *p, const char *txt);
103 struct rset_bool_rfd *rfd_list;
106 struct rset_bool_rfd {
115 struct rset_bool_rfd *next;
116 struct rset_bool_info *info;
119 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
121 rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
122 struct rset_bool_info *info;
124 info = (struct rset_bool_info *) xmalloc (sizeof(*info));
125 info->key_size = bool_parms->key_size;
126 info->rset_l = bool_parms->rset_l;
127 info->rset_r = bool_parms->rset_r;
128 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
129 ct->flags |= RSET_FLAG_VOLATILE;
130 info->cmp = bool_parms->cmp;
131 info->log_item = bool_parms->log_item;
132 info->rfd_list = NULL;
137 static RSFD r_open (RSET ct, int flag)
139 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
140 struct rset_bool_rfd *rfd;
142 if (flag & RSETF_WRITE)
144 logf (LOG_FATAL, "bool set type is read-only");
147 rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
148 logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
149 rfd->next = info->rfd_list;
150 info->rfd_list = rfd;
154 rfd->buf_l = xmalloc (info->key_size);
155 rfd->buf_r = xmalloc (info->key_size);
156 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
157 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
158 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l);
159 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r);
164 static void r_close (RSFD rfd)
166 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
167 struct rset_bool_rfd **rfdp;
169 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
172 xfree ((*rfdp)->buf_l);
173 xfree ((*rfdp)->buf_r);
174 rset_close (info->rset_l, (*rfdp)->rfd_l);
175 rset_close (info->rset_r, (*rfdp)->rfd_r);
176 *rfdp = (*rfdp)->next;
180 logf (LOG_FATAL, "r_close but no rfd match!");
184 static void r_delete (RSET ct)
186 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
188 assert (info->rfd_list == NULL);
189 rset_delete (info->rset_l);
190 rset_delete (info->rset_r);
194 static void r_rewind (RSFD rfd)
196 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
197 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
199 logf (LOG_DEBUG, "rsbool_rewind");
200 rset_rewind (info->rset_l, p->rfd_l);
201 rset_rewind (info->rset_r, p->rfd_r);
202 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
203 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
207 static int r_forward (RSET ct, RSFD rfd, void *buf,
208 int (*cmpfunc)(const void *p1, const void *p2),
209 const void *untilbuf)
211 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
212 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
216 logf (LOG_DEBUG, "rsbool_forward (L) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
217 rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
219 if ( p->more_l && ((cmpfunc)(untilbuf,p->buf_l)==2) )
220 p->more_l = rset_forward(info->rset_l, p->rfd_l, p->buf_l,
221 info->cmp, untilbuf);
223 logf (LOG_DEBUG, "rsbool_forward (R) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
224 rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
226 if ( p->more_r && ((cmpfunc)(untilbuf,p->buf_r)==2))
227 p->more_r = rset_forward(info->rset_r, p->rfd_r, p->buf_r,
228 info->cmp, untilbuf);
230 logf (LOG_DEBUG, "rsbool_forward [%p] calling read, m=%d,%d t=%d",
231 rfd, p->more_l, p->more_r, p->tail);
235 rc = rset_read(ct,rfd,buf);
237 logf (LOG_DEBUG, "rsbool_forward returning [%p] %d m=%d,%d",
238 rfd, rc, p->more_l, p->more_r);
259 static int r_read_and (RSFD rfd, void *buf)
261 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
262 struct rset_bool_info *info = p->info;
264 while (p->more_l || p->more_r)
268 if (p->more_l && p->more_r)
269 cmp = (*info->cmp)(p->buf_l, p->buf_r);
275 logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
276 rfd, p->more_l, p->more_r, cmp, p->tail);
277 (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
278 (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
282 memcpy (buf, p->buf_l, info->key_size);
283 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
288 memcpy (buf, p->buf_r, info->key_size);
289 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
292 logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
293 rfd, p->more_l, p->more_r, cmp);
294 key_logdump(LOG_DEBUG,buf);
295 (*info->log_item)(LOG_DEBUG, buf, "");
302 memcpy (buf, p->buf_l, info->key_size);
303 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
306 logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
307 rfd, p->more_l, p->more_r, cmp);
308 (*info->log_item)(LOG_DEBUG, buf, "");
313 else if (cmp > 1) /* cmp == 2 */
317 memcpy (buf, p->buf_r, info->key_size);
319 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
322 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
325 logf (LOG_DEBUG, "r_read_and returning C m=%d/%d c=%d",
326 p->more_l, p->more_r, cmp);
327 (*info->log_item)(LOG_DEBUG, buf, "");
336 memcpy (buf, p->buf_r, info->key_size);
337 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
338 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
341 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
342 rfd, p->more_l, p->more_r, cmp);
343 (*info->log_item)(LOG_DEBUG, buf, "");
351 logf (LOG_DEBUG, "r_read_and [%p] about to forward R m=%d/%d c=%d",
352 rfd, p->more_l, p->more_r, cmp);
354 if (p->more_r && p->more_l)
355 p->more_r = rset_forward( info->rset_r, p->rfd_r,
356 p->buf_r, (info->cmp), p->buf_l);
358 return 0; /* no point in reading further */
365 memcpy (buf, p->buf_l, info->key_size);
366 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
369 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
372 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
373 rfd, p->more_l, p->more_r, cmp);
374 (*info->log_item)(LOG_DEBUG, buf, "");
382 memcpy (buf, p->buf_l, info->key_size);
383 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
384 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
387 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
388 rfd, p->more_l, p->more_r, cmp);
389 (*info->log_item)(LOG_DEBUG, buf, "");
397 logf (LOG_DEBUG, "r_read_and [%p] about to forward L m=%d/%d c=%d",
398 rfd, p->more_l, p->more_r, cmp);
400 if (p->more_r && p->more_l)
401 p->more_l = rset_forward(
402 info->rset_l, p->rfd_l,
403 p->buf_l, (info->cmp), p->buf_r);
405 return 0; /* no point in reading further */
411 logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
416 static int r_read_or (RSFD rfd, void *buf)
418 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
419 struct rset_bool_info *info = p->info;
421 while (p->more_l || p->more_r)
425 if (p->more_l && p->more_r)
426 cmp = (*info->cmp)(p->buf_l, p->buf_r);
433 memcpy (buf, p->buf_l, info->key_size);
434 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
435 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
437 logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
438 p->more_l, p->more_r, cmp);
439 (*info->log_item)(LOG_DEBUG, buf, "");
446 memcpy (buf, p->buf_r, info->key_size);
447 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
449 logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
450 p->more_l, p->more_r, cmp);
451 (*info->log_item)(LOG_DEBUG, buf, "");
458 memcpy (buf, p->buf_l, info->key_size);
459 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
461 logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
462 p->more_l, p->more_r, cmp);
463 (*info->log_item)(LOG_DEBUG, buf, "");
472 static int r_read_not (RSFD rfd, void *buf)
474 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
475 struct rset_bool_info *info = p->info;
477 while (p->more_l || p->more_r)
481 if (p->more_l && p->more_r)
482 cmp = (*info->cmp)(p->buf_l, p->buf_r);
489 memcpy (buf, p->buf_l, info->key_size);
490 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
496 p->more_r = rset_forward(
497 info->rset_r, p->rfd_r,
498 p->buf_r, (info->cmp), p->buf_l);
502 memcpy (buf, p->buf_l, info->key_size);
505 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
508 cmp = (*info->cmp)(p->buf_l, buf);
509 } while (cmp >= -1 && cmp <= 1);
512 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
515 cmp = (*info->cmp)(p->buf_r, buf);
516 } while (cmp >= -1 && cmp <= 1);
523 static int r_write (RSFD rfd, const void *buf)
525 logf (LOG_FATAL, "bool set type is read-only");
529 static void r_pos (RSFD rfd, double *current, double *total)
531 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
532 struct rset_bool_info *info = p->info;
537 rset_pos(info->rset_l, p->rfd_l, &lcur, <ot);
538 rset_pos(info->rset_r, p->rfd_r, &rcur, &rtot);
539 if ( (rtot<0) && (ltot<0)) { /*no position */
540 *current=rcur; /* return same as you got */
541 *total=rtot; /* probably -1 for not available */
543 if ( rtot<0) { rtot=0; rcur=0;} /* if only one useful, use it */
544 if ( ltot<0) { ltot=0; lcur=0;}
545 if ( rtot+ltot < 1 ) { /* empty rset */
550 r=1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
551 *current=(double) (p->hits);
554 yaz_log(LOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
555 info->rset_l->control->desc, info->rset_r->control->desc,
556 *current, *total, r);