1 /* $Id: rsprox.c,v 1.13 2004-08-31 10:43:40 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 void r_rewind (RSFD rfd);
39 static int r_forward(RSFD rfd, void *buf,
40 int (*cmpfunc)(const void *p1, const void *p2),
41 const void *untilbuf);
42 static int r_read (RSFD rfd, void *buf);
43 static int r_write (RSFD rfd, const void *buf);
44 static void r_pos (RSFD rfd, double *current, double *total);
46 static const struct rset_control control =
59 const struct rset_control *rset_kind_prox = &control;
61 struct rset_prox_info {
62 /* struct rset_prox_parms p; */
70 int (*cmp)(const void *p1, const void *p2);
71 int (*getseq)(const void *p);
74 struct rset_prox_rfd {
76 char **buf; /* lookahead key buffers */
77 char *more; /* more in each lookahead? */
82 RSET rsprox_create( NMEM nmem, int key_size,
83 int (*cmp)(const void *p1, const void *p2),
84 int (*getseq)(const void *p),
85 int rset_no, RSET *rset,
86 int ordered, int exclusion,
87 int relation, int distance)
89 RSET rnew=rset_create_base(&control, nmem);
90 struct rset_prox_info *info;
91 info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
92 info->key_size = key_size;
94 info->getseq=getseq; /* FIXME - what about multi-level stuff ?? */
95 info->rset = nmem_malloc(rnew->nmem,rset_no * sizeof(*info->rset));
96 memcpy(info->rset, rset,
97 rset_no * sizeof(*info->rset));
98 info->rset_no=rset_no;
99 info->ordered=ordered;
100 info->exclusion=exclusion;
101 info->relation=relation;
102 info->distance=distance;
108 static void r_delete (RSET ct)
110 struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
113 for (i = 0; i<info->rset_no; i++)
114 rset_delete (info->rset[i]);
118 static RSFD r_open (RSET ct, int flag)
120 struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
122 struct rset_prox_rfd *p;
125 if (flag & RSETF_WRITE)
127 logf (LOG_FATAL, "prox set type is read-only");
130 rfd = rfd_create_base(ct);
132 p=(struct rset_prox_rfd *)(rfd->priv);
134 p = (struct rset_prox_rfd *) nmem_malloc (ct->nmem,sizeof(*p));
136 p->more = nmem_malloc (ct->nmem,sizeof(*p->more) * info->rset_no);
137 p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * info->rset_no);
138 for (i = 0; i < info->rset_no; i++)
139 p->buf[i] = nmem_malloc(ct->nmem,info->key_size);
140 p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * info->rset_no);
142 logf(LOG_DEBUG,"rsprox (%s) open [%p]", ct->control->desc, rfd);
144 for (i = 0; i < info->rset_no; i++) {
145 p->rfd[i] = rset_open (info->rset[i], RSETF_READ);
146 p->more[i] = rset_read (p->rfd[i], p->buf[i]);
152 static void r_close (RSFD rfd)
154 struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
155 struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
158 for (i = 0; i<info->rset_no; i++)
159 rset_close (p->rfd[i]);
160 rfd_delete_base(rfd);
163 static void r_rewind (RSFD rfd)
165 struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
166 struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
169 logf (LOG_DEBUG, "rsprox_rewind");
171 for (i = 0; i < info->rset_no; i++)
173 rset_rewind (p->rfd[i]);
174 p->more[i] = rset_read (p->rfd[i], p->buf[i]);
179 static int r_forward (RSFD rfd, void *buf,
180 int (*cmpfunc)(const void *p1, const void *p2),
181 const void *untilbuf)
183 struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
184 struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
190 /* it is enough to forward first one. Other will follow. */
191 if ( p->more[0] && ((cmpfunc)(untilbuf, p->buf[0]) >= 2) )
192 p->more[0] = rset_forward(p->rfd[0], p->buf[0],
193 info->cmp, untilbuf);
195 if (info->ordered && info->relation == 3 && info->exclusion == 0
196 && info->distance == 1)
200 for (i = 1; i < info->rset_no; i++)
204 p->more[0] = 0; /* saves us a goto out of while loop. */
207 cmp = (*info->cmp) (p->buf[i], p->buf[i-1]);
210 p->more[i-1] = rset_forward (p->rfd[i-1],
218 if ((*info->getseq)(p->buf[i-1]) +1 !=
219 (*info->getseq)(p->buf[i]))
220 { /* FIXME - We need more flexible multilevel stuff */
221 p->more[i-1] = rset_read ( p->rfd[i-1], p->buf[i-1]);
227 p->more[i] = rset_forward (p->rfd[i], p->buf[i],
228 info->cmp, p->buf[i-1]);
232 if (i == info->rset_no)
234 memcpy (buf, p->buf[0], info->key_size);
235 p->more[0] = rset_read (p->rfd[0], p->buf[0]);
241 else if (info->rset_no == 2)
243 while (p->more[0] && p->more[1])
245 int cmp = (*info->cmp)(p->buf[0], p->buf[1]);
247 p->more[0] = rset_forward (p->rfd[0],
248 p->buf[0], info->cmp, p->buf[0]);
250 p->more[1] = rset_forward (p->rfd[1],
251 p->buf[1], info->cmp, p->buf[1]);
257 seqno[n++] = (*info->getseq)(p->buf[0]);
258 while ((p->more[0] = rset_read (p->rfd[0],
262 seqno[n++] = (*info->getseq)(p->buf[0]);
264 for (i = 0; i<n; i++)
266 int diff = (*info->getseq)(p->buf[1]) - seqno[i];
267 int excl = info->exclusion;
268 if (!info->ordered && diff < 0)
270 switch (info->relation)
273 if (diff < info->distance && diff >= 0)
277 if (diff <= info->distance && diff >= 0)
281 if (diff == info->distance && diff >= 0)
285 if (diff >= info->distance && diff >= 0)
289 if (diff > info->distance && diff >= 0)
293 if (diff != info->distance && diff >= 0)
299 memcpy (buf, p->buf[1], info->key_size);
301 p->more[1] = rset_read ( p->rfd[1], p->buf[1]);
306 p->more[1] = rset_read (p->rfd[1], p->buf[1]);
314 static int r_read (RSFD rfd, void *buf)
316 return r_forward(rfd, buf, 0, 0);
319 static int r_write (RSFD rfd, const void *buf)
321 logf (LOG_FATAL, "prox set type is read-only");
325 static void r_pos (RSFD rfd, double *current, double *total)
327 struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
328 struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
331 double scur=0,stot=0;
334 logf (LOG_DEBUG, "rsprox_pos");
336 for (i = 0; i < info->rset_no; i++)
338 rset_pos(p->rfd[i], &cur, &tot);
344 if (tot <0) { /* nothing found */
347 } else if (tot <1) { /* most likely tot==0 */
355 logf(LOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
356 i,*current, *total, r);