1 /* $Id: rsprox.c,v 1.11 2004-08-24 14:25:16 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(RSET ct, 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);
72 struct rset_prox_rfd *rfd_list;
75 struct rset_prox_rfd {
77 char **buf; /* lookahead key buffers */
78 char *more; /* more in each lookahead? */
79 struct rset_prox_rfd *next;
80 struct rset_prox_info *info;
85 RSET rsprox_create( NMEM nmem, int key_size,
86 int (*cmp)(const void *p1, const void *p2),
87 int (*getseq)(const void *p),
88 int rset_no, RSET *rset,
89 int ordered, int exclusion,
90 int relation, int distance)
92 RSET rnew=rset_create_base(&control, nmem);
93 struct rset_prox_info *info;
94 info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
95 info->key_size = key_size;
97 info->getseq=getseq; /* FIXME - what about multi-level stuff ?? */
98 info->rset = nmem_malloc(rnew->nmem,rset_no * sizeof(*info->rset));
99 memcpy(info->rset, rset,
100 rset_no * sizeof(*info->rset));
101 info->rset_no=rset_no;
102 info->ordered=ordered;
103 info->exclusion=exclusion;
104 info->relation=relation;
105 info->distance=distance;
106 info->rfd_list = NULL;
112 static void r_delete (RSET ct)
114 struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
117 assert (info->rfd_list == NULL);
118 for (i = 0; i<info->rset_no; i++)
119 rset_delete (info->rset[i]);
120 /* xfree (info->rset); */ /* nmems! */
125 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
127 rset_prox_parms *prox_parms = (rset_prox_parms *) parms;
128 struct rset_prox_info *info;
130 info = (struct rset_prox_info *) xmalloc (sizeof(*info));
131 memcpy(&info->p, prox_parms, sizeof(struct rset_prox_parms));
132 assert(info->p.rset_no >= 2);
133 info->p.rset = xmalloc(info->p.rset_no * sizeof(*info->p.rset));
134 memcpy(info->p.rset, prox_parms->rset,
135 info->p.rset_no * sizeof(*info->p.rset));
136 info->rfd_list = NULL;
141 static RSFD r_open (RSET ct, int flag)
143 struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
144 struct rset_prox_rfd *rfd;
147 if (flag & RSETF_WRITE)
149 logf (LOG_FATAL, "prox set type is read-only");
152 rfd = (struct rset_prox_rfd *) xmalloc (sizeof(*rfd));
153 logf(LOG_DEBUG,"rsprox (%s) open [%p]", ct->control->desc, rfd);
154 rfd->next = info->rfd_list;
155 info->rfd_list = rfd;
158 rfd->more = xmalloc (sizeof(*rfd->more) * info->rset_no);
160 rfd->buf = xmalloc(sizeof(*rfd->buf) * info->rset_no);
161 for (i = 0; i < info->rset_no; i++)
162 rfd->buf[i] = xmalloc (info->key_size);
164 rfd->rfd = xmalloc(sizeof(*rfd->rfd) * info->rset_no);
165 for (i = 0; i < info->rset_no; i++)
166 rfd->rfd[i] = rset_open (info->rset[i], RSETF_READ);
168 for (i = 0; i < info->rset_no; i++)
169 rfd->more[i] = rset_read (info->rset[i], rfd->rfd[i],
175 static void r_close (RSFD rfd)
177 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
178 struct rset_prox_rfd **rfdp;
180 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
184 for (i = 0; i<info->rset_no; i++)
185 xfree ((*rfdp)->buf[i]);
186 xfree ((*rfdp)->buf);
187 xfree ((*rfdp)->more);
189 for (i = 0; i<info->rset_no; i++)
190 rset_close (info->rset[i], (*rfdp)->rfd[i]);
191 xfree ((*rfdp)->rfd);
193 *rfdp = (*rfdp)->next;
197 logf (LOG_FATAL, "r_close but no rfd match!");
201 static void r_rewind (RSFD rfd)
203 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
204 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
207 logf (LOG_DEBUG, "rsprox_rewind");
209 for (i = 0; i < info->rset_no; i++)
211 rset_rewind (info->rset[i], p->rfd[i]);
212 p->more[i] = rset_read (info->rset[i], p->rfd[i], p->buf[i]);
217 static int r_forward (RSET ct, RSFD rfd, void *buf,
218 int (*cmpfunc)(const void *p1, const void *p2),
219 const void *untilbuf)
221 /* Note: CT is not used. We _can_ pass NULL for it */
222 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
223 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
229 /* it's enough to forward first one. Other will follow
231 if ( p->more[0] && ((cmpfunc)(untilbuf, p->buf[0]) >= 2) )
232 p->more[0] = rset_forward(info->rset[0], p->rfd[0],
233 p->buf[0], info->cmp,
236 if (info->ordered && info->relation == 3 && info->exclusion == 0
237 && info->distance == 1)
241 for (i = 1; i < info->rset_no; i++)
245 p->more[0] = 0; /* saves us a goto out of while loop. */
248 cmp = (*info->cmp) (p->buf[i], p->buf[i-1]);
251 p->more[i-1] = rset_forward (info->rset[i-1],
260 if ((*info->getseq)(p->buf[i-1]) +1 !=
261 (*info->getseq)(p->buf[i]))
263 p->more[i-1] = rset_read ( info->rset[i-1],
264 p->rfd[i-1], p->buf[i-1]);
270 p->more[i] = rset_forward (info->rset[i], p->rfd[i],
271 p->buf[i], info->cmp,
276 if (i == p->info->rset_no)
278 memcpy (buf, p->buf[0], info->key_size);
279 p->more[0] = rset_read (info->rset[0], p->rfd[0], p->buf[0]);
285 else if (info->rset_no == 2)
287 while (p->more[0] && p->more[1])
289 int cmp = (*info->cmp)(p->buf[0], p->buf[1]);
291 p->more[0] = rset_forward (info->rset[0], p->rfd[0],
292 p->buf[0], info->cmp, p->buf[0]);
294 p->more[1] = rset_forward (info->rset[1], p->rfd[1],
295 p->buf[1], info->cmp, p->buf[1]);
301 seqno[n++] = (*info->getseq)(p->buf[0]);
302 while ((p->more[0] = rset_read (info->rset[0], p->rfd[0],
306 seqno[n++] = (*info->getseq)(p->buf[0]);
308 for (i = 0; i<n; i++)
310 int diff = (*info->getseq)(p->buf[1]) - seqno[i];
311 int excl = info->exclusion;
312 if (!info->ordered && diff < 0)
314 switch (info->relation)
317 if (diff < info->distance && diff >= 0)
321 if (diff <= info->distance && diff >= 0)
325 if (diff == info->distance && diff >= 0)
329 if (diff >= info->distance && diff >= 0)
333 if (diff > info->distance && diff >= 0)
337 if (diff != info->distance && diff >= 0)
343 memcpy (buf, p->buf[1], info->key_size);
345 p->more[1] = rset_read (info->rset[1],
346 p->rfd[1], p->buf[1]);
351 p->more[1] = rset_read (info->rset[1], p->rfd[1],
360 static int r_read (RSFD rfd, void *buf)
362 { double cur,tot; r_pos(rfd,&cur,&tot); } /*!*/
363 return r_forward(0, rfd, buf, 0, 0);
366 static int r_write (RSFD rfd, const void *buf)
368 logf (LOG_FATAL, "prox set type is read-only");
372 static void r_pos (RSFD rfd, double *current, double *total)
374 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
375 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
378 double scur=0,stot=0;
381 logf (LOG_DEBUG, "rsprox_pos");
383 for (i = 0; i < info->rset_no; i++)
385 rset_pos(info->rset[i], p->rfd[i], &cur, &tot);
391 if (tot <0) { /* nothing found */
394 } else if (tot <1) { /* most likely tot==0 */
402 logf(LOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
403 i,*current, *total, r);