1 /* $Id: rsprox.c,v 1.12 2004-08-26 11:11:59 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;
73 struct rset_prox_rfd *free_list;
76 struct rset_prox_rfd {
78 char **buf; /* lookahead key buffers */
79 char *more; /* more in each lookahead? */
80 struct rset_prox_rfd *next;
81 struct rset_prox_info *info;
86 RSET rsprox_create( NMEM nmem, int key_size,
87 int (*cmp)(const void *p1, const void *p2),
88 int (*getseq)(const void *p),
89 int rset_no, RSET *rset,
90 int ordered, int exclusion,
91 int relation, int distance)
93 RSET rnew=rset_create_base(&control, nmem);
94 struct rset_prox_info *info;
95 info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
96 info->key_size = key_size;
98 info->getseq=getseq; /* FIXME - what about multi-level stuff ?? */
99 info->rset = nmem_malloc(rnew->nmem,rset_no * sizeof(*info->rset));
100 memcpy(info->rset, rset,
101 rset_no * sizeof(*info->rset));
102 info->rset_no=rset_no;
103 info->ordered=ordered;
104 info->exclusion=exclusion;
105 info->relation=relation;
106 info->distance=distance;
107 info->rfd_list = NULL;
108 info->free_list = NULL;
114 static void r_delete (RSET ct)
116 struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
119 assert (info->rfd_list == NULL);
120 for (i = 0; i<info->rset_no; i++)
121 rset_delete (info->rset[i]);
125 static RSFD r_open (RSET ct, int flag)
127 struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
128 struct rset_prox_rfd *rfd;
131 if (flag & RSETF_WRITE)
133 logf (LOG_FATAL, "prox set type is read-only");
136 rfd = info->free_list;
138 info->free_list=rfd->next;
140 rfd = (struct rset_prox_rfd *) xmalloc (sizeof(*rfd));
141 rfd->more = nmem_malloc (ct->nmem,sizeof(*rfd->more) * info->rset_no);
142 rfd->buf = nmem_malloc(ct->nmem,sizeof(*rfd->buf) * info->rset_no);
143 for (i = 0; i < info->rset_no; i++)
144 rfd->buf[i] = nmem_malloc(ct->nmem,info->key_size);
145 rfd->rfd = nmem_malloc(ct->nmem,sizeof(*rfd->rfd) * info->rset_no);
147 logf(LOG_DEBUG,"rsprox (%s) open [%p]", ct->control->desc, rfd);
148 rfd->next = info->rfd_list;
149 info->rfd_list = rfd;
153 for (i = 0; i < info->rset_no; i++) {
154 rfd->rfd[i] = rset_open (info->rset[i], RSETF_READ);
155 rfd->more[i] = rset_read (info->rset[i], rfd->rfd[i],
162 static void r_close (RSFD rfd)
164 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
165 struct rset_prox_rfd **rfdp;
167 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
171 struct rset_prox_rfd *rfd_tmp=*rfdp;
172 for (i = 0; i<info->rset_no; i++)
173 rset_close (info->rset[i], (*rfdp)->rfd[i]);
174 *rfdp = (*rfdp)->next;
175 rfd_tmp->next=info->free_list;
176 info->free_list=rfd_tmp;
179 logf (LOG_FATAL, "r_close but no rfd match!");
183 static void r_rewind (RSFD rfd)
185 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
186 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
189 logf (LOG_DEBUG, "rsprox_rewind");
191 for (i = 0; i < info->rset_no; i++)
193 rset_rewind (info->rset[i], p->rfd[i]);
194 p->more[i] = rset_read (info->rset[i], p->rfd[i], p->buf[i]);
199 static int r_forward (RSET ct, RSFD rfd, void *buf,
200 int (*cmpfunc)(const void *p1, const void *p2),
201 const void *untilbuf)
203 /* Note: CT is not used. We _can_ pass NULL for it */
204 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
205 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
211 /* it's enough to forward first one. Other will follow
213 if ( p->more[0] && ((cmpfunc)(untilbuf, p->buf[0]) >= 2) )
214 p->more[0] = rset_forward(info->rset[0], p->rfd[0],
215 p->buf[0], info->cmp,
218 if (info->ordered && info->relation == 3 && info->exclusion == 0
219 && info->distance == 1)
223 for (i = 1; i < info->rset_no; i++)
227 p->more[0] = 0; /* saves us a goto out of while loop. */
230 cmp = (*info->cmp) (p->buf[i], p->buf[i-1]);
233 p->more[i-1] = rset_forward (info->rset[i-1],
242 if ((*info->getseq)(p->buf[i-1]) +1 !=
243 (*info->getseq)(p->buf[i]))
245 p->more[i-1] = rset_read ( info->rset[i-1],
246 p->rfd[i-1], p->buf[i-1]);
252 p->more[i] = rset_forward (info->rset[i], p->rfd[i],
253 p->buf[i], info->cmp,
258 if (i == p->info->rset_no)
260 memcpy (buf, p->buf[0], info->key_size);
261 p->more[0] = rset_read (info->rset[0], p->rfd[0], p->buf[0]);
267 else if (info->rset_no == 2)
269 while (p->more[0] && p->more[1])
271 int cmp = (*info->cmp)(p->buf[0], p->buf[1]);
273 p->more[0] = rset_forward (info->rset[0], p->rfd[0],
274 p->buf[0], info->cmp, p->buf[0]);
276 p->more[1] = rset_forward (info->rset[1], p->rfd[1],
277 p->buf[1], info->cmp, p->buf[1]);
283 seqno[n++] = (*info->getseq)(p->buf[0]);
284 while ((p->more[0] = rset_read (info->rset[0], p->rfd[0],
288 seqno[n++] = (*info->getseq)(p->buf[0]);
290 for (i = 0; i<n; i++)
292 int diff = (*info->getseq)(p->buf[1]) - seqno[i];
293 int excl = info->exclusion;
294 if (!info->ordered && diff < 0)
296 switch (info->relation)
299 if (diff < info->distance && diff >= 0)
303 if (diff <= info->distance && diff >= 0)
307 if (diff == info->distance && diff >= 0)
311 if (diff >= info->distance && diff >= 0)
315 if (diff > info->distance && diff >= 0)
319 if (diff != info->distance && diff >= 0)
325 memcpy (buf, p->buf[1], info->key_size);
327 p->more[1] = rset_read (info->rset[1],
328 p->rfd[1], p->buf[1]);
333 p->more[1] = rset_read (info->rset[1], p->rfd[1],
342 static int r_read (RSFD rfd, void *buf)
344 { double cur,tot; r_pos(rfd,&cur,&tot); } /*!*/
345 return r_forward(0, rfd, buf, 0, 0);
348 static int r_write (RSFD rfd, const void *buf)
350 logf (LOG_FATAL, "prox set type is read-only");
354 static void r_pos (RSFD rfd, double *current, double *total)
356 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
357 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
360 double scur=0,stot=0;
363 logf (LOG_DEBUG, "rsprox_pos");
365 for (i = 0; i < info->rset_no; i++)
367 rset_pos(info->rset[i], p->rfd[i], &cur, &tot);
373 if (tot <0) { /* nothing found */
376 } else if (tot <1) { /* most likely tot==0 */
384 logf(LOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
385 i,*current, *total, r);