1 /* $Id: rsbool.c,v 1.39 2004-08-23 12:38:53 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 info->cmp = bool_parms->cmp;
129 info->log_item = bool_parms->log_item;
130 info->rfd_list = NULL;
135 static RSFD r_open (RSET ct, int flag)
137 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
138 struct rset_bool_rfd *rfd;
140 if (flag & RSETF_WRITE)
142 logf (LOG_FATAL, "bool set type is read-only");
145 rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
146 logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
147 rfd->next = info->rfd_list;
148 info->rfd_list = rfd;
152 rfd->buf_l = xmalloc (info->key_size);
153 rfd->buf_r = xmalloc (info->key_size);
154 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
155 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
156 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l);
157 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r);
162 static void r_close (RSFD rfd)
164 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
165 struct rset_bool_rfd **rfdp;
167 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
170 xfree ((*rfdp)->buf_l);
171 xfree ((*rfdp)->buf_r);
172 rset_close (info->rset_l, (*rfdp)->rfd_l);
173 rset_close (info->rset_r, (*rfdp)->rfd_r);
174 *rfdp = (*rfdp)->next;
178 logf (LOG_FATAL, "r_close but no rfd match!");
182 static void r_delete (RSET ct)
184 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
186 assert (info->rfd_list == NULL);
187 rset_delete (info->rset_l);
188 rset_delete (info->rset_r);
192 static void r_rewind (RSFD rfd)
194 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
195 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
197 logf (LOG_DEBUG, "rsbool_rewind");
198 rset_rewind (info->rset_l, p->rfd_l);
199 rset_rewind (info->rset_r, p->rfd_r);
200 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
201 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
205 static int r_forward (RSET ct, RSFD rfd, void *buf,
206 int (*cmpfunc)(const void *p1, const void *p2),
207 const void *untilbuf)
209 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
210 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
214 logf (LOG_DEBUG, "rsbool_forward (L) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
215 rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
217 if ( p->more_l && ((cmpfunc)(untilbuf,p->buf_l)==2) )
218 p->more_l = rset_forward(info->rset_l, p->rfd_l, p->buf_l,
219 info->cmp, untilbuf);
221 logf (LOG_DEBUG, "rsbool_forward (R) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
222 rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
224 if ( p->more_r && ((cmpfunc)(untilbuf,p->buf_r)==2))
225 p->more_r = rset_forward(info->rset_r, p->rfd_r, p->buf_r,
226 info->cmp, untilbuf);
228 logf (LOG_DEBUG, "rsbool_forward [%p] calling read, m=%d,%d t=%d",
229 rfd, p->more_l, p->more_r, p->tail);
233 rc = rset_read(ct,rfd,buf);
235 logf (LOG_DEBUG, "rsbool_forward returning [%p] %d m=%d,%d",
236 rfd, rc, p->more_l, p->more_r);
257 static int r_read_and (RSFD rfd, void *buf)
259 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
260 struct rset_bool_info *info = p->info;
262 while (p->more_l || p->more_r)
266 if (p->more_l && p->more_r)
267 cmp = (*info->cmp)(p->buf_l, p->buf_r);
273 logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
274 rfd, p->more_l, p->more_r, cmp, p->tail);
275 (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
276 (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
280 memcpy (buf, p->buf_l, info->key_size);
281 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
286 memcpy (buf, p->buf_r, info->key_size);
287 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
290 logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
291 rfd, p->more_l, p->more_r, cmp);
292 key_logdump(LOG_DEBUG,buf);
293 (*info->log_item)(LOG_DEBUG, buf, "");
300 memcpy (buf, p->buf_l, info->key_size);
301 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
304 logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
305 rfd, p->more_l, p->more_r, cmp);
306 (*info->log_item)(LOG_DEBUG, buf, "");
311 else if (cmp > 1) /* cmp == 2 */
315 memcpy (buf, p->buf_r, info->key_size);
317 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
320 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
323 logf (LOG_DEBUG, "r_read_and returning C m=%d/%d c=%d",
324 p->more_l, p->more_r, cmp);
325 (*info->log_item)(LOG_DEBUG, buf, "");
334 memcpy (buf, p->buf_r, info->key_size);
335 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
336 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
339 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
340 rfd, p->more_l, p->more_r, cmp);
341 (*info->log_item)(LOG_DEBUG, buf, "");
349 logf (LOG_DEBUG, "r_read_and [%p] about to forward R m=%d/%d c=%d",
350 rfd, p->more_l, p->more_r, cmp);
352 if (p->more_r && p->more_l)
353 p->more_r = rset_forward( info->rset_r, p->rfd_r,
354 p->buf_r, (info->cmp), p->buf_l);
356 return 0; /* no point in reading further */
363 memcpy (buf, p->buf_l, info->key_size);
364 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
367 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
370 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
371 rfd, p->more_l, p->more_r, cmp);
372 (*info->log_item)(LOG_DEBUG, buf, "");
380 memcpy (buf, p->buf_l, info->key_size);
381 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
382 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
385 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
386 rfd, p->more_l, p->more_r, cmp);
387 (*info->log_item)(LOG_DEBUG, buf, "");
395 logf (LOG_DEBUG, "r_read_and [%p] about to forward L m=%d/%d c=%d",
396 rfd, p->more_l, p->more_r, cmp);
398 if (p->more_r && p->more_l)
399 p->more_l = rset_forward(
400 info->rset_l, p->rfd_l,
401 p->buf_l, (info->cmp), p->buf_r);
403 return 0; /* no point in reading further */
409 logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
414 static int r_read_or (RSFD rfd, void *buf)
416 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
417 struct rset_bool_info *info = p->info;
419 while (p->more_l || p->more_r)
423 if (p->more_l && p->more_r)
424 cmp = (*info->cmp)(p->buf_l, p->buf_r);
431 memcpy (buf, p->buf_l, info->key_size);
432 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
433 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
435 logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
436 p->more_l, p->more_r, cmp);
437 (*info->log_item)(LOG_DEBUG, buf, "");
444 memcpy (buf, p->buf_r, info->key_size);
445 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
447 logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
448 p->more_l, p->more_r, cmp);
449 (*info->log_item)(LOG_DEBUG, buf, "");
456 memcpy (buf, p->buf_l, info->key_size);
457 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
459 logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
460 p->more_l, p->more_r, cmp);
461 (*info->log_item)(LOG_DEBUG, buf, "");
470 static int r_read_not (RSFD rfd, void *buf)
472 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
473 struct rset_bool_info *info = p->info;
475 while (p->more_l || p->more_r)
479 if (p->more_l && p->more_r)
480 cmp = (*info->cmp)(p->buf_l, p->buf_r);
487 memcpy (buf, p->buf_l, info->key_size);
488 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
494 p->more_r = rset_forward(
495 info->rset_r, p->rfd_r,
496 p->buf_r, (info->cmp), p->buf_l);
500 memcpy (buf, p->buf_l, info->key_size);
503 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
506 cmp = (*info->cmp)(p->buf_l, buf);
507 } while (cmp >= -1 && cmp <= 1);
510 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
513 cmp = (*info->cmp)(p->buf_r, buf);
514 } while (cmp >= -1 && cmp <= 1);
521 static int r_write (RSFD rfd, const void *buf)
523 logf (LOG_FATAL, "bool set type is read-only");
527 static void r_pos (RSFD rfd, double *current, double *total)
529 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
530 struct rset_bool_info *info = p->info;
535 rset_pos(info->rset_l, p->rfd_l, &lcur, <ot);
536 rset_pos(info->rset_r, p->rfd_r, &rcur, &rtot);
537 if ( (rtot<0) && (ltot<0)) { /*no position */
538 *current=rcur; /* return same as you got */
539 *total=rtot; /* probably -1 for not available */
541 if ( rtot<0) { rtot=0; rcur=0;} /* if only one useful, use it */
542 if ( ltot<0) { ltot=0; lcur=0;}
543 if ( rtot+ltot < 1 ) { /* empty rset */
548 r=1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
549 *current=(double) (p->hits);
552 yaz_log(LOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
553 info->rset_l->control->desc, info->rset_r->control->desc,
554 *current, *total, r);