1 /* $Id: rsbool.c,v 1.37 2004-08-06 14:09:02 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, int *term_index,
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, int *term_index);
46 static int r_read_or (RSFD rfd, void *buf, int *term_index);
47 static int r_read_not (RSFD rfd, void *buf, int *term_index);
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 {
102 int (*cmp)(const void *p1, const void *p2);
103 void (*log_item)(int logmask, const void *p, const char *txt);
104 struct rset_bool_rfd *rfd_list;
107 struct rset_bool_rfd {
118 struct rset_bool_rfd *next;
119 struct rset_bool_info *info;
122 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
124 rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
125 struct rset_bool_info *info;
127 info = (struct rset_bool_info *) xmalloc (sizeof(*info));
128 info->key_size = bool_parms->key_size;
129 info->rset_l = bool_parms->rset_l;
130 info->rset_r = bool_parms->rset_r;
131 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
132 ct->flags |= RSET_FLAG_VOLATILE;
133 info->cmp = bool_parms->cmp;
134 info->log_item = bool_parms->log_item;
135 info->rfd_list = NULL;
137 info->term_index_s = info->rset_l->no_rset_terms;
139 info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
140 ct->rset_terms = (RSET_TERM *)
141 xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
143 memcpy (ct->rset_terms, info->rset_l->rset_terms,
144 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
145 memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
146 info->rset_r->rset_terms,
147 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
151 static RSFD r_open (RSET ct, int flag)
153 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
154 struct rset_bool_rfd *rfd;
156 if (flag & RSETF_WRITE)
158 logf (LOG_FATAL, "bool set type is read-only");
161 rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
162 logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
163 rfd->next = info->rfd_list;
164 info->rfd_list = rfd;
168 rfd->buf_l = xmalloc (info->key_size);
169 rfd->buf_r = xmalloc (info->key_size);
170 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
171 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
172 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
174 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
180 static void r_close (RSFD rfd)
182 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
183 struct rset_bool_rfd **rfdp;
185 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
188 xfree ((*rfdp)->buf_l);
189 xfree ((*rfdp)->buf_r);
190 rset_close (info->rset_l, (*rfdp)->rfd_l);
191 rset_close (info->rset_r, (*rfdp)->rfd_r);
192 *rfdp = (*rfdp)->next;
196 logf (LOG_FATAL, "r_close but no rfd match!");
200 static void r_delete (RSET ct)
202 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
204 assert (info->rfd_list == NULL);
205 xfree (ct->rset_terms);
206 rset_delete (info->rset_l);
207 rset_delete (info->rset_r);
211 static void r_rewind (RSFD rfd)
213 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
214 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
216 logf (LOG_DEBUG, "rsbool_rewind");
217 rset_rewind (info->rset_l, p->rfd_l);
218 rset_rewind (info->rset_r, p->rfd_r);
219 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
220 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
224 static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
225 int (*cmpfunc)(const void *p1, const void *p2),
226 const void *untilbuf)
228 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
229 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
233 logf (LOG_DEBUG, "rsbool_forward (L) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
234 rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
236 if ( p->more_l && ((cmpfunc)(untilbuf,p->buf_l)==2) )
237 p->more_l = rset_forward(info->rset_l, p->rfd_l, p->buf_l,
238 &p->term_index_l, info->cmp, untilbuf);
240 logf (LOG_DEBUG, "rsbool_forward (R) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
241 rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
243 if ( p->more_r && ((cmpfunc)(untilbuf,p->buf_r)==2))
244 p->more_r = rset_forward(info->rset_r, p->rfd_r, p->buf_r,
245 &p->term_index_r, info->cmp, untilbuf);
247 logf (LOG_DEBUG, "rsbool_forward [%p] calling read, m=%d,%d t=%d",
248 rfd, p->more_l, p->more_r, p->tail);
252 rc = rset_read(ct,rfd,buf,term_index);
254 logf (LOG_DEBUG, "rsbool_forward returning [%p] %d m=%d,%d",
255 rfd, rc, p->more_l, p->more_r);
276 static int r_read_and (RSFD rfd, void *buf, int *term_index)
278 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
279 struct rset_bool_info *info = p->info;
281 while (p->more_l || p->more_r)
285 if (p->more_l && p->more_r)
286 cmp = (*info->cmp)(p->buf_l, p->buf_r);
292 logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
293 rfd, p->more_l, p->more_r, cmp, p->tail);
294 (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
295 (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
299 memcpy (buf, p->buf_l, info->key_size);
300 *term_index = p->term_index_l;
301 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
307 memcpy (buf, p->buf_r, info->key_size);
308 *term_index = p->term_index_r + info->term_index_s;
309 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
313 logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
314 rfd, p->more_l, p->more_r, cmp);
315 key_logdump(LOG_DEBUG,buf);
316 (*info->log_item)(LOG_DEBUG, buf, "");
323 memcpy (buf, p->buf_l, info->key_size);
324 *term_index = p->term_index_l;
325 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
329 logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
330 rfd, p->more_l, p->more_r, cmp);
331 (*info->log_item)(LOG_DEBUG, buf, "");
336 else if (cmp > 1) /* cmp == 2 */
340 memcpy (buf, p->buf_r, info->key_size);
341 *term_index = p->term_index_r + info->term_index_s;
343 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
347 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
350 logf (LOG_DEBUG, "r_read_and returning C m=%d/%d c=%d",
351 p->more_l, p->more_r, cmp);
352 (*info->log_item)(LOG_DEBUG, buf, "");
361 memcpy (buf, p->buf_r, info->key_size);
362 *term_index = p->term_index_r + info->term_index_s;
363 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
365 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
368 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
369 rfd, p->more_l, p->more_r, cmp);
370 (*info->log_item)(LOG_DEBUG, buf, "");
378 logf (LOG_DEBUG, "r_read_and [%p] about to forward R m=%d/%d c=%d",
379 rfd, p->more_l, p->more_r, cmp);
381 if (p->more_r && p->more_l)
382 p->more_r = rset_forward(
383 info->rset_r, p->rfd_r,
384 p->buf_r, &p->term_index_r,
385 (info->cmp), p->buf_l);
387 return 0; /* no point in reading further */
394 memcpy (buf, p->buf_l, info->key_size);
395 *term_index = p->term_index_l;
396 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
400 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
403 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
404 rfd, p->more_l, p->more_r, cmp);
405 (*info->log_item)(LOG_DEBUG, buf, "");
413 memcpy (buf, p->buf_l, info->key_size);
414 *term_index = p->term_index_l;
415 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
417 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
420 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
421 rfd, p->more_l, p->more_r, cmp);
422 (*info->log_item)(LOG_DEBUG, buf, "");
430 logf (LOG_DEBUG, "r_read_and [%p] about to forward L m=%d/%d c=%d",
431 rfd, p->more_l, p->more_r, cmp);
433 if (p->more_r && p->more_l)
434 p->more_l = rset_forward(
435 /* p->more_l = rset_default_forward( */
436 info->rset_l, p->rfd_l,
437 p->buf_l, &p->term_index_l,
438 (info->cmp), p->buf_r);
440 return 0; /* no point in reading further */
446 logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
451 static int r_read_or (RSFD rfd, void *buf, int *term_index)
453 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
454 struct rset_bool_info *info = p->info;
456 while (p->more_l || p->more_r)
460 if (p->more_l && p->more_r)
461 cmp = (*info->cmp)(p->buf_l, p->buf_r);
468 memcpy (buf, p->buf_l, info->key_size);
469 *term_index = p->term_index_l;
470 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
472 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
475 logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
476 p->more_l, p->more_r, cmp);
477 (*info->log_item)(LOG_DEBUG, buf, "");
484 memcpy (buf, p->buf_r, info->key_size);
485 *term_index = p->term_index_r + info->term_index_s;
486 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
489 logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
490 p->more_l, p->more_r, cmp);
491 (*info->log_item)(LOG_DEBUG, buf, "");
498 memcpy (buf, p->buf_l, info->key_size);
499 *term_index = p->term_index_l;
500 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
503 logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
504 p->more_l, p->more_r, cmp);
505 (*info->log_item)(LOG_DEBUG, buf, "");
514 static int r_read_not (RSFD rfd, void *buf, int *term_index)
516 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
517 struct rset_bool_info *info = p->info;
519 while (p->more_l || p->more_r)
523 if (p->more_l && p->more_r)
524 cmp = (*info->cmp)(p->buf_l, p->buf_r);
531 memcpy (buf, p->buf_l, info->key_size);
532 *term_index = p->term_index_l;
533 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
540 p->more_r = rset_forward(
541 info->rset_r, p->rfd_r,
542 p->buf_r, &p->term_index_r,
543 (info->cmp), p->buf_l);
547 memcpy (buf, p->buf_l, info->key_size);
550 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
554 cmp = (*info->cmp)(p->buf_l, buf);
555 } while (cmp >= -1 && cmp <= 1);
558 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
562 cmp = (*info->cmp)(p->buf_r, buf);
563 } while (cmp >= -1 && cmp <= 1);
570 static int r_write (RSFD rfd, const void *buf)
572 logf (LOG_FATAL, "bool set type is read-only");
576 static void r_pos (RSFD rfd, double *current, double *total)
578 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
579 struct rset_bool_info *info = p->info;
584 rset_pos(info->rset_l, p->rfd_l, &lcur, <ot);
585 rset_pos(info->rset_r, p->rfd_r, &rcur, &rtot);
586 if ( (rtot<0) && (ltot<0)) { /*no position */
587 *current=rcur; /* return same as you got */
588 *total=rtot; /* probably -1 for not available */
590 if ( rtot<0) { rtot=0; rcur=0;} /* if only one useful, use it */
591 if ( ltot<0) { ltot=0; lcur=0;}
592 if ( rtot+ltot < 1 ) { /* empty rset */
597 r=1.0*(lcur+rcur)/(ltot+rtot); /* weighed average of l and r */
598 *current=(double) (p->hits);
601 yaz_log(LOG_DEBUG,"bool_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
602 info->rset_l->control->desc, info->rset_r->control->desc,
603 *current, *total, r);