1 /* $Id: rsprox.c,v 1.5 2004-08-03 14:54:41 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, int *term_index,
41 int (*cmpfunc)(const void *p1, const void *p2),
42 const void *untilbuf);
43 static int r_read (RSFD rfd, void *buf, int *term_index);
44 static int r_write (RSFD rfd, const void *buf);
46 static const struct rset_control control_prox =
60 const struct rset_control *rset_kind_prox = &control_prox;
62 struct rset_prox_info {
63 struct rset_prox_parms p;
65 struct rset_prox_rfd *rfd_list;
68 struct rset_prox_rfd {
70 char **buf; /* lookahead key buffers */
71 char *more; /* more in each lookahead? */
72 struct rset_prox_rfd *next;
73 struct rset_prox_info *info;
76 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
78 rset_prox_parms *prox_parms = (rset_prox_parms *) parms;
79 struct rset_prox_info *info;
82 int length_prox_term = 0;
83 int min_nn = 10000000;
84 const char *flags = NULL;
88 info = (struct rset_prox_info *) xmalloc (sizeof(*info));
89 memcpy(&info->p, prox_parms, sizeof(struct rset_prox_parms));
90 assert(info->p.rset_no >= 2);
91 info->p.rset = xmalloc(info->p.rset_no * sizeof(*info->p.rset));
92 memcpy(info->p.rset, prox_parms->rset,
93 info->p.rset_no * sizeof(*info->p.rset));
94 info->rfd_list = NULL;
96 for (i = 0; i<info->p.rset_no; i++)
97 if (rset_is_volatile(info->p.rset[i]))
98 ct->flags |= RSET_FLAG_VOLATILE;
101 for (i = 0; i<info->p.rset_no; i++)
104 for (j = 0; j < info->p.rset[i]->no_rset_terms; j++)
106 const char *nflags = info->p.rset[i]->rset_terms[j]->flags;
107 char *term = info->p.rset[i]->rset_terms[j]->name;
108 int lterm = strlen(term);
109 if (lterm + length_prox_term < sizeof(prox_term)-1)
111 if (length_prox_term)
112 prox_term[length_prox_term++] = ' ';
113 strcpy (prox_term + length_prox_term, term);
114 length_prox_term += lterm;
116 if (min_nn > info->p.rset[i]->rset_terms[j]->nn)
117 min_nn = info->p.rset[i]->rset_terms[j]->nn;
119 term_type = info->p.rset[i]->rset_terms[j]->type;
123 ct->no_rset_terms = 1;
124 ct->rset_terms = (RSET_TERM *)
125 xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
127 ct->rset_terms[0] = rset_term_create (prox_term, length_prox_term,
132 static RSFD r_open (RSET ct, int flag)
134 struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
135 struct rset_prox_rfd *rfd;
138 if (flag & RSETF_WRITE)
140 logf (LOG_FATAL, "prox set type is read-only");
143 rfd = (struct rset_prox_rfd *) xmalloc (sizeof(*rfd));
144 logf(LOG_DEBUG,"rsprox (%s) open [%p]", ct->control->desc, rfd);
145 rfd->next = info->rfd_list;
146 info->rfd_list = rfd;
149 rfd->more = xmalloc (sizeof(*rfd->more) * info->p.rset_no);
151 rfd->buf = xmalloc(sizeof(*rfd->buf) * info->p.rset_no);
152 for (i = 0; i < info->p.rset_no; i++)
153 rfd->buf[i] = xmalloc (info->p.key_size);
155 rfd->rfd = xmalloc(sizeof(*rfd->rfd) * info->p.rset_no);
156 for (i = 0; i < info->p.rset_no; i++)
157 rfd->rfd[i] = rset_open (info->p.rset[i], RSETF_READ);
159 for (i = 0; i < info->p.rset_no; i++)
160 rfd->more[i] = rset_read (info->p.rset[i], rfd->rfd[i],
161 rfd->buf[i], &dummy);
165 static void r_close (RSFD rfd)
167 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
168 struct rset_prox_rfd **rfdp;
170 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
174 for (i = 0; i<info->p.rset_no; i++)
175 xfree ((*rfdp)->buf[i]);
176 xfree ((*rfdp)->buf);
177 xfree ((*rfdp)->more);
179 for (i = 0; i<info->p.rset_no; i++)
180 rset_close (info->p.rset[i], (*rfdp)->rfd[i]);
181 xfree ((*rfdp)->rfd);
183 *rfdp = (*rfdp)->next;
187 logf (LOG_FATAL, "r_close but no rfd match!");
191 static void r_delete (RSET ct)
193 struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
196 assert (info->rfd_list == NULL);
197 rset_term_destroy(ct->rset_terms[0]);
198 xfree (ct->rset_terms);
199 for (i = 0; i<info->p.rset_no; i++)
200 rset_delete (info->p.rset[i]);
201 xfree (info->p.rset);
205 static void r_rewind (RSFD rfd)
207 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
208 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
211 logf (LOG_DEBUG, "rsprox_rewind");
213 for (i = 0; i < info->p.rset_no; i++)
215 rset_rewind (info->p.rset[i], p->rfd[i]);
216 p->more[i] = rset_read (info->p.rset[i], p->rfd[i], p->buf[i], &dummy);
220 static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
221 int (*cmpfunc)(const void *p1, const void *p2),
222 const void *untilbuf)
224 /* Note: CT is not used. We _can_ pass NULL for it */
225 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
226 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
233 /* it's enough to forward first one. Other will follow
235 if ( p->more[0] && ((cmpfunc)(untilbuf, p->buf[0]) >= 2) )
236 p->more[0] = rset_forward(info->p.rset[0], p->rfd[0],
237 p->buf[0], &dummy, info->p.cmp,
240 if (info->p.ordered && info->p.relation == 3 && info->p.exclusion == 0
241 && info->p.distance == 1)
245 for (i = 1; i < info->p.rset_no; i++)
249 p->more[0] = 0; /* saves us a goto out of while loop. */
252 cmp = (*info->p.cmp) (p->buf[i], p->buf[i-1]);
255 p->more[i-1] = rset_forward (info->p.rset[i-1],
264 if ((*info->p.getseq)(p->buf[i-1]) +1 !=
265 (*info->p.getseq)(p->buf[i]))
267 p->more[i-1] = rset_read (
268 info->p.rset[i-1], p->rfd[i-1],
269 p->buf[i-1], &dummy);
275 p->more[i] = rset_forward (info->p.rset[i], p->rfd[i],
282 if (i == p->info->p.rset_no)
284 memcpy (buf, p->buf[0], info->p.key_size);
287 p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
293 else if (info->p.rset_no == 2)
295 while (p->more[0] && p->more[1])
297 int cmp = (*info->p.cmp)(p->buf[0], p->buf[1]);
299 p->more[0] = rset_forward (info->p.rset[0], p->rfd[0],
301 term_index, info->p.cmp, p->buf[0]);
303 p->more[1] = rset_forward (info->p.rset[1], p->rfd[1],
305 term_index, info->p.cmp, p->buf[1]);
311 seqno[n++] = (*info->p.getseq)(p->buf[0]);
312 while ((p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
314 term_index)) >= -1 &&
317 seqno[n++] = (*info->p.getseq)(p->buf[0]);
319 for (i = 0; i<n; i++)
321 int diff = (*info->p.getseq)(p->buf[1]) - seqno[i];
322 int excl = info->p.exclusion;
323 if (!info->p.ordered && diff < 0)
325 switch (info->p.relation)
328 if (diff < info->p.distance && diff >= 0)
332 if (diff <= info->p.distance && diff >= 0)
336 if (diff == info->p.distance && diff >= 0)
340 if (diff >= info->p.distance && diff >= 0)
344 if (diff > info->p.distance && diff >= 0)
348 if (diff != info->p.distance && diff >= 0)
354 memcpy (buf, p->buf[1], info->p.key_size);
357 p->more[1] = rset_read (info->p.rset[1],
358 p->rfd[1], p->buf[1],
363 p->more[1] = rset_read (info->p.rset[1], p->rfd[1],
373 static int r_read (RSFD rfd, void *buf, int *term_index)
375 return r_forward(0, rfd, buf, term_index, 0, 0);
378 static int r_write (RSFD rfd, const void *buf)
380 logf (LOG_FATAL, "prox set type is read-only");