1 /* $Id: rsprox.c,v 1.10 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
35 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
36 static RSFD r_open (RSET ct, int flag);
37 static void r_close (RSFD rfd);
38 static void r_delete (RSET ct);
39 static void r_rewind (RSFD rfd);
40 static int r_forward(RSET ct, RSFD rfd, void *buf,
41 int (*cmpfunc)(const void *p1, const void *p2),
42 const void *untilbuf);
43 static int r_read (RSFD rfd, void *buf);
44 static int r_write (RSFD rfd, const void *buf);
45 static void r_pos (RSFD rfd, double *current, double *total);
47 static const struct rset_control control_prox =
61 const struct rset_control *rset_kind_prox = &control_prox;
63 struct rset_prox_info {
64 struct rset_prox_parms p;
66 struct rset_prox_rfd *rfd_list;
69 struct rset_prox_rfd {
71 char **buf; /* lookahead key buffers */
72 char *more; /* more in each lookahead? */
73 struct rset_prox_rfd *next;
74 struct rset_prox_info *info;
78 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
80 rset_prox_parms *prox_parms = (rset_prox_parms *) parms;
81 struct rset_prox_info *info;
83 info = (struct rset_prox_info *) xmalloc (sizeof(*info));
84 memcpy(&info->p, prox_parms, sizeof(struct rset_prox_parms));
85 assert(info->p.rset_no >= 2);
86 info->p.rset = xmalloc(info->p.rset_no * sizeof(*info->p.rset));
87 memcpy(info->p.rset, prox_parms->rset,
88 info->p.rset_no * sizeof(*info->p.rset));
89 info->rfd_list = NULL;
93 static RSFD r_open (RSET ct, int flag)
95 struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
96 struct rset_prox_rfd *rfd;
99 if (flag & RSETF_WRITE)
101 logf (LOG_FATAL, "prox set type is read-only");
104 rfd = (struct rset_prox_rfd *) xmalloc (sizeof(*rfd));
105 logf(LOG_DEBUG,"rsprox (%s) open [%p]", ct->control->desc, rfd);
106 rfd->next = info->rfd_list;
107 info->rfd_list = rfd;
110 rfd->more = xmalloc (sizeof(*rfd->more) * info->p.rset_no);
112 rfd->buf = xmalloc(sizeof(*rfd->buf) * info->p.rset_no);
113 for (i = 0; i < info->p.rset_no; i++)
114 rfd->buf[i] = xmalloc (info->p.key_size);
116 rfd->rfd = xmalloc(sizeof(*rfd->rfd) * info->p.rset_no);
117 for (i = 0; i < info->p.rset_no; i++)
118 rfd->rfd[i] = rset_open (info->p.rset[i], RSETF_READ);
120 for (i = 0; i < info->p.rset_no; i++)
121 rfd->more[i] = rset_read (info->p.rset[i], rfd->rfd[i],
127 static void r_close (RSFD rfd)
129 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
130 struct rset_prox_rfd **rfdp;
132 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
136 for (i = 0; i<info->p.rset_no; i++)
137 xfree ((*rfdp)->buf[i]);
138 xfree ((*rfdp)->buf);
139 xfree ((*rfdp)->more);
141 for (i = 0; i<info->p.rset_no; i++)
142 rset_close (info->p.rset[i], (*rfdp)->rfd[i]);
143 xfree ((*rfdp)->rfd);
145 *rfdp = (*rfdp)->next;
149 logf (LOG_FATAL, "r_close but no rfd match!");
153 static void r_delete (RSET ct)
155 struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
158 assert (info->rfd_list == NULL);
159 for (i = 0; i<info->p.rset_no; i++)
160 rset_delete (info->p.rset[i]);
161 xfree (info->p.rset);
165 static void r_rewind (RSFD rfd)
167 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
168 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
171 logf (LOG_DEBUG, "rsprox_rewind");
173 for (i = 0; i < info->p.rset_no; i++)
175 rset_rewind (info->p.rset[i], p->rfd[i]);
176 p->more[i] = rset_read (info->p.rset[i], p->rfd[i], p->buf[i]);
181 static int r_forward (RSET ct, RSFD rfd, void *buf,
182 int (*cmpfunc)(const void *p1, const void *p2),
183 const void *untilbuf)
185 /* Note: CT is not used. We _can_ pass NULL for it */
186 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
187 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
193 /* it's enough to forward first one. Other will follow
195 if ( p->more[0] && ((cmpfunc)(untilbuf, p->buf[0]) >= 2) )
196 p->more[0] = rset_forward(info->p.rset[0], p->rfd[0],
197 p->buf[0], info->p.cmp,
200 if (info->p.ordered && info->p.relation == 3 && info->p.exclusion == 0
201 && info->p.distance == 1)
205 for (i = 1; i < info->p.rset_no; i++)
209 p->more[0] = 0; /* saves us a goto out of while loop. */
212 cmp = (*info->p.cmp) (p->buf[i], p->buf[i-1]);
215 p->more[i-1] = rset_forward (info->p.rset[i-1],
224 if ((*info->p.getseq)(p->buf[i-1]) +1 !=
225 (*info->p.getseq)(p->buf[i]))
227 p->more[i-1] = rset_read ( info->p.rset[i-1],
228 p->rfd[i-1], p->buf[i-1]);
234 p->more[i] = rset_forward (info->p.rset[i], p->rfd[i],
235 p->buf[i], info->p.cmp,
240 if (i == p->info->p.rset_no)
242 memcpy (buf, p->buf[0], info->p.key_size);
243 p->more[0] = rset_read (info->p.rset[0], p->rfd[0], p->buf[0]);
249 else if (info->p.rset_no == 2)
251 while (p->more[0] && p->more[1])
253 int cmp = (*info->p.cmp)(p->buf[0], p->buf[1]);
255 p->more[0] = rset_forward (info->p.rset[0], p->rfd[0],
256 p->buf[0], info->p.cmp, p->buf[0]);
258 p->more[1] = rset_forward (info->p.rset[1], p->rfd[1],
259 p->buf[1], info->p.cmp, p->buf[1]);
265 seqno[n++] = (*info->p.getseq)(p->buf[0]);
266 while ((p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
270 seqno[n++] = (*info->p.getseq)(p->buf[0]);
272 for (i = 0; i<n; i++)
274 int diff = (*info->p.getseq)(p->buf[1]) - seqno[i];
275 int excl = info->p.exclusion;
276 if (!info->p.ordered && diff < 0)
278 switch (info->p.relation)
281 if (diff < info->p.distance && diff >= 0)
285 if (diff <= info->p.distance && diff >= 0)
289 if (diff == info->p.distance && diff >= 0)
293 if (diff >= info->p.distance && diff >= 0)
297 if (diff > info->p.distance && diff >= 0)
301 if (diff != info->p.distance && diff >= 0)
307 memcpy (buf, p->buf[1], info->p.key_size);
309 p->more[1] = rset_read (info->p.rset[1],
310 p->rfd[1], p->buf[1]);
315 p->more[1] = rset_read (info->p.rset[1], p->rfd[1],
324 static int r_read (RSFD rfd, void *buf)
326 { double cur,tot; r_pos(rfd,&cur,&tot); } /*!*/
327 return r_forward(0, rfd, buf, 0, 0);
330 static int r_write (RSFD rfd, const void *buf)
332 logf (LOG_FATAL, "prox set type is read-only");
336 static void r_pos (RSFD rfd, double *current, double *total)
338 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
339 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
342 double scur=0,stot=0;
345 logf (LOG_DEBUG, "rsprox_pos");
347 for (i = 0; i < info->p.rset_no; i++)
349 rset_pos(info->p.rset[i], p->rfd[i], &cur, &tot);
355 if (tot <0) { /* nothing found */
358 } else if (tot <1) { /* most likely tot==0 */
366 logf(LOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
367 i,*current, *total, r);