1 /* $Id: rsprox.c,v 1.17 2004-09-30 09:53:05 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 int r_forward(RSFD rfd, void *buf, const void *untilbuf);
39 static int r_read (RSFD rfd, void *buf);
40 static int r_write (RSFD rfd, const void *buf);
41 static void r_pos (RSFD rfd, double *current, double *total);
43 static const struct rset_control control =
55 const struct rset_control *rset_kind_prox = &control;
57 struct rset_prox_info {
58 RSET *rset; /* array of 'child' rsets */
59 int rset_no; /* how many of them */
66 struct rset_prox_rfd {
68 char **buf; /* lookahead key buffers */
69 char *more; /* more in each lookahead? */
74 RSET rsprox_create( NMEM nmem, const struct key_control *kcontrol, int scope,
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);
80 struct rset_prox_info *info;
81 info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
82 info->rset = nmem_malloc(rnew->nmem,rset_no * sizeof(*info->rset));
83 memcpy(info->rset, rset,
84 rset_no * sizeof(*info->rset));
85 info->rset_no=rset_no;
86 info->ordered=ordered;
87 info->exclusion=exclusion;
88 info->relation=relation;
89 info->distance=distance;
95 static void r_delete (RSET ct)
97 struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
100 for (i = 0; i<info->rset_no; i++)
101 rset_delete (info->rset[i]);
105 static RSFD r_open (RSET ct, int flag)
107 struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
109 struct rset_prox_rfd *p;
112 if (flag & RSETF_WRITE)
114 logf (LOG_FATAL, "prox set type is read-only");
117 rfd = rfd_create_base(ct);
119 p=(struct rset_prox_rfd *)(rfd->priv);
121 p = (struct rset_prox_rfd *) nmem_malloc (ct->nmem,sizeof(*p));
123 p->more = nmem_malloc (ct->nmem,sizeof(*p->more) * info->rset_no);
124 p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * info->rset_no);
125 for (i = 0; i < info->rset_no; i++)
126 p->buf[i] = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
127 p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * info->rset_no);
129 logf(LOG_DEBUG,"rsprox (%s) open [%p] n=%d",
130 ct->control->desc, rfd, info->rset_no);
132 for (i = 0; i < info->rset_no; i++) {
133 p->rfd[i] = rset_open (info->rset[i], RSETF_READ);
134 p->more[i] = rset_read (p->rfd[i], p->buf[i]);
140 static void r_close (RSFD rfd)
142 struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
143 struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
146 for (i = 0; i<info->rset_no; i++)
147 rset_close (p->rfd[i]);
148 rfd_delete_base(rfd);
151 static int r_forward (RSFD rfd, void *buf, const void *untilbuf)
153 struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
154 struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
155 const struct key_control *kctrl=rfd->rset->keycontrol;
161 /* it is enough to forward first one. Other will follow. */
162 if ( p->more[0] && /* was: cmp >=2 */
163 ((kctrl->cmp)(untilbuf, p->buf[0]) >= rfd->rset->scope) )
164 p->more[0] = rset_forward(p->rfd[0], p->buf[0], untilbuf);
166 if (info->ordered && info->relation == 3 && info->exclusion == 0
167 && info->distance == 1)
171 for (i = 1; i < info->rset_no; i++)
175 p->more[0] = 0; /* saves us a goto out of while loop. */
178 cmp = (*kctrl->cmp) (p->buf[i], p->buf[i-1]);
179 if (cmp >= rfd->rset->scope ) /* cmp>1 */
181 p->more[i-1] = rset_forward (p->rfd[i-1],
186 else if ( cmp>0 ) /* cmp == 1*/
188 if ((*kctrl->getseq)(p->buf[i-1]) +1 !=
189 (*kctrl->getseq)(p->buf[i]))
190 { /* FIXME - We need more flexible multilevel stuff */
191 p->more[i-1] = rset_read ( p->rfd[i-1], p->buf[i-1]);
197 p->more[i] = rset_forward (p->rfd[i],
198 p->buf[i], p->buf[i-1]);
202 if (i == info->rset_no)
204 memcpy (buf, p->buf[0], kctrl->key_size);
205 p->more[0] = rset_read (p->rfd[0], p->buf[0]);
211 else if (info->rset_no == 2)
213 while (p->more[0] && p->more[1])
215 int cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
216 if ( cmp <= - rfd->rset->scope) /* cmp<-1*/
217 p->more[0] = rset_forward (p->rfd[0],
218 p->buf[0], p->buf[1]);
219 else if ( cmp >= rfd->rset->scope ) /* cmp>1 */
220 p->more[1] = rset_forward (p->rfd[1],
221 p->buf[1], p->buf[0]);
224 zint seqno[500]; /* FIXME - why 500 ?? */
227 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
228 while ((p->more[0] = rset_read (p->rfd[0],
232 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
234 for (i = 0; i<n; i++)
236 int diff = (*kctrl->getseq)(p->buf[1]) - seqno[i];
237 int excl = info->exclusion;
238 if (!info->ordered && diff < 0)
240 switch (info->relation)
243 if (diff < info->distance && diff >= 0)
247 if (diff <= info->distance && diff >= 0)
251 if (diff == info->distance && diff >= 0)
255 if (diff >= info->distance && diff >= 0)
259 if (diff > info->distance && diff >= 0)
263 if (diff != info->distance && diff >= 0)
269 memcpy (buf, p->buf[1], kctrl->key_size);
271 p->more[1] = rset_read ( p->rfd[1], p->buf[1]);
276 p->more[1] = rset_read (p->rfd[1], p->buf[1]);
284 static int r_read (RSFD rfd, void *buf)
286 return r_forward(rfd, buf, 0);
289 static int r_write (RSFD rfd, const void *buf)
291 logf (LOG_FATAL, "prox set type is read-only");
295 static void r_pos (RSFD rfd, double *current, double *total)
297 struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
298 struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
301 double scur=0,stot=0;
304 logf (LOG_DEBUG, "rsprox_pos");
306 for (i = 0; i < info->rset_no; i++)
308 rset_pos(p->rfd[i], &cur, &tot);
314 if (tot <0) { /* nothing found */
317 } else if (tot <1) { /* most likely tot==0 */
325 logf(LOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
326 i,*current, *total, r);