1 /* $Id: rsprox.c,v 1.30 2006-05-10 08:13:34 adam Exp $
2 Copyright (C) 1995-2005
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
28 #include <idzebra/util.h>
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 int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf);
39 static int r_read (RSFD rfd, void *buf, TERMID *term);
40 static int r_write (RSFD rfd, const void *buf);
41 static void r_pos (RSFD rfd, double *current, double *total);
42 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
44 static const struct rset_control control =
57 struct rset_prox_info {
64 struct rset_prox_rfd {
66 char **buf; /* lookahead key buffers */
67 char *more; /* more in each lookahead? */
68 TERMID *terms; /* lookahead terms */
73 RSET rsprox_create(NMEM nmem, struct rset_key_control *kcontrol,
75 int rset_no, RSET *rset,
76 int ordered, int exclusion,
77 int relation, int distance)
79 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, 0,
81 struct rset_prox_info *info;
82 info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
83 info->ordered = ordered;
84 info->exclusion = exclusion;
85 info->relation = relation;
86 info->distance = distance;
91 static void r_delete (RSET ct)
95 static RSFD r_open (RSET ct, int flag)
98 struct rset_prox_rfd *p;
101 if (flag & RSETF_WRITE)
103 yaz_log(YLOG_FATAL, "prox set type is read-only");
106 rfd = rfd_create_base(ct);
108 p = (struct rset_prox_rfd *)(rfd->priv);
110 p = (struct rset_prox_rfd *) nmem_malloc(ct->nmem,sizeof(*p));
112 p->more = nmem_malloc (ct->nmem,sizeof(*p->more) * ct->no_children);
113 p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * ct->no_children);
114 p->terms = nmem_malloc(ct->nmem,sizeof(*p->terms) * ct->no_children);
115 for (i = 0; i < ct->no_children; i++)
117 p->buf[i] = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
120 p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * ct->no_children);
122 yaz_log(YLOG_DEBUG,"rsprox (%s) open [%p] n=%d",
123 ct->control->desc, rfd, ct->no_children);
125 for (i = 0; i < ct->no_children; i++) {
126 p->rfd[i] = rset_open (ct->children[i], RSETF_READ);
127 p->more[i] = rset_read (p->rfd[i], p->buf[i], &p->terms[i]);
133 static void r_close (RSFD rfd)
136 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
139 for (i = 0; i<ct->no_children; i++)
140 rset_close(p->rfd[i]);
143 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf)
146 struct rset_prox_info *info = (struct rset_prox_info *)(ct->priv);
147 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
148 const struct rset_key_control *kctrl = ct->keycontrol;
154 /* it is enough to forward first one. Other will follow. */
155 if ( p->more[0] && /* was: cmp >=2 */
156 ((kctrl->cmp)(untilbuf, p->buf[0]) >= rfd->rset->scope) )
157 p->more[0] = rset_forward(p->rfd[0], p->buf[0],
158 &p->terms[0], untilbuf);
160 if (info->ordered && info->relation == 3 && info->exclusion == 0
161 && info->distance == 1)
165 for (i = 1; i < ct->no_children; i++)
169 p->more[0] = 0; /* saves us a goto out of while loop. */
172 cmp = (*kctrl->cmp) (p->buf[i], p->buf[i-1]);
173 if (cmp >= rfd->rset->scope ) /* cmp>1 */
175 p->more[i-1] = rset_forward (p->rfd[i-1],
181 else if ( cmp>0 ) /* cmp == 1*/
183 if ((*kctrl->getseq)(p->buf[i-1]) +1 !=
184 (*kctrl->getseq)(p->buf[i]))
185 { /* FIXME - We need more flexible multilevel stuff */
186 p->more[i-1] = rset_read ( p->rfd[i-1], p->buf[i-1],
193 p->more[i] = rset_forward (p->rfd[i],
194 p->buf[i], &p->terms[i], p->buf[i-1]);
198 if (i == ct->no_children)
200 memcpy (buf, p->buf[0], kctrl->key_size);
203 p->more[0] = rset_read (p->rfd[0], p->buf[0], &p->terms[0]);
209 else if (ct->no_children == 2)
211 while (p->more[0] && p->more[1])
213 int cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
214 if ( cmp <= - rfd->rset->scope) /* cmp<-1*/
215 p->more[0] = rset_forward (p->rfd[0], p->buf[0],
216 &p->terms[0],p->buf[1]);
217 else if ( cmp >= rfd->rset->scope ) /* cmp>1 */
218 p->more[1] = rset_forward (p->rfd[1], p->buf[1],
219 &p->terms[1],p->buf[0]);
222 zint seqno[500]; /* FIXME - why 500 ?? */
225 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
226 while ((p->more[0] = rset_read (p->rfd[0],
227 p->buf[0], &p->terms[0])) >= -1 &&
230 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
232 for (i = 0; i<n; i++)
234 zint diff = (*kctrl->getseq)(p->buf[1]) - seqno[i];
235 int excl = info->exclusion;
236 if (!info->ordered && diff < 0)
238 switch (info->relation)
241 if (diff < info->distance && diff >= 0)
245 if (diff <= info->distance && diff >= 0)
249 if (diff == info->distance && diff >= 0)
253 if (diff >= info->distance && diff >= 0)
257 if (diff > info->distance && diff >= 0)
261 if (diff != info->distance && diff >= 0)
267 memcpy (buf, p->buf[1], kctrl->key_size);
270 p->more[1] = rset_read ( p->rfd[1], p->buf[1],
276 p->more[1] = rset_read (p->rfd[1], p->buf[1], &p->terms[1]);
284 static int r_read (RSFD rfd, void *buf, TERMID *term)
286 return r_forward(rfd, buf, term, 0);
289 static int r_write (RSFD rfd, const void *buf)
291 yaz_log(YLOG_FATAL, "prox set type is read-only");
295 static void r_pos (RSFD rfd, double *current, double *total)
298 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
301 double cur, tot = -1.0;
302 double scur = 0.0, stot = 0.0;
304 yaz_log(YLOG_DEBUG, "rsprox_pos");
306 for (i = 0; i < ct->no_children; i++)
308 rset_pos(p->rfd[i], &cur, &tot);
314 if (tot <0) { /* nothing found */
317 } else if (tot < 1) { /* most likely tot==0 */
322 *current = (double) p->hits;
325 yaz_log(YLOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
326 i,*current, *total, r);
329 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
332 for (i = 0; i<ct->no_children; i++)
333 rset_getterms(ct->children[i], terms, maxterms, curterm);
339 * indent-tabs-mode: nil
341 * vim: shiftwidth=4 tabstop=8 expandtab