1 /* $Id: rsprox.c,v 1.1 2004-06-09 12:15:25 adam 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_count (RSET ct);
44 static int r_read (RSFD rfd, void *buf, int *term_index);
45 static int r_write (RSFD rfd, const void *buf);
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;
77 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
79 rset_prox_parms *prox_parms = (rset_prox_parms *) parms;
80 struct rset_prox_info *info;
83 int length_prox_term = 0;
84 int min_nn = 10000000;
85 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 info->p.rset = xmalloc(info->p.rset_no * sizeof(*info->p.rset));
91 memcpy(info->p.rset, prox_parms->rset,
92 info->p.rset_no * sizeof(*info->p.rset));
93 info->rfd_list = NULL;
95 for (i = 0; i<info->p.rset_no; i++)
96 if (rset_is_volatile(info->p.rset[i]))
97 ct->flags |= RSET_FLAG_VOLATILE;
100 for (i = 0; i<info->p.rset_no; i++)
103 for (j = 0; j < info->p.rset[i]->no_rset_terms; j++)
105 const char *nflags = info->p.rset[i]->rset_terms[j]->flags;
106 char *term = info->p.rset[i]->rset_terms[j]->name;
107 int lterm = strlen(term);
108 if (lterm + length_prox_term < sizeof(prox_term)-1)
110 if (length_prox_term)
111 prox_term[length_prox_term++] = ' ';
112 strcpy (prox_term + length_prox_term, term);
113 length_prox_term += lterm;
115 if (min_nn > info->p.rset[i]->rset_terms[j]->nn)
116 min_nn = info->p.rset[i]->rset_terms[j]->nn;
118 term_type = info->p.rset[i]->rset_terms[j]->type;
122 ct->no_rset_terms = 1;
123 ct->rset_terms = (RSET_TERM *)
124 xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
126 ct->rset_terms[0] = rset_term_create (prox_term, length_prox_term,
131 static RSFD r_open (RSET ct, int flag)
133 struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
134 struct rset_prox_rfd *rfd;
137 if (flag & RSETF_WRITE)
139 logf (LOG_FATAL, "prox set type is read-only");
142 rfd = (struct rset_prox_rfd *) xmalloc (sizeof(*rfd));
143 logf(LOG_DEBUG,"rsprox (%s) open [%p]", ct->control->desc, rfd);
144 rfd->next = info->rfd_list;
145 info->rfd_list = rfd;
148 rfd->more = xmalloc (sizeof(*rfd->more) * info->p.rset_no);
150 for (i = 0; i < info->p.rset_no; i++)
151 rfd->buf[i] = xmalloc (info->p.key_size);
153 for (i = 0; i < info->p.rset_no; i++)
154 rfd->rfd[i] = rset_open (info->p.rset[i], RSETF_READ);
156 for (i = 0; i < info->p.rset_no; i++)
157 rfd->more[i] = rset_read (info->p.rset[i], rfd->rfd[i],
158 rfd->buf[i], &dummy);
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 for (i = 0; i<info->p.rset_no; i++)
172 xfree ((*rfdp)->buf[i]);
173 xfree ((*rfdp)->more);
175 for (i = 0; i<info->p.rset_no; i++)
176 rset_close (info->p.rset[i], (*rfdp)->rfd[i]);
178 *rfdp = (*rfdp)->next;
182 logf (LOG_FATAL, "r_close but no rfd match!");
186 static void r_delete (RSET ct)
188 struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
191 assert (info->rfd_list == NULL);
192 rset_term_destroy(ct->rset_terms[0]);
193 xfree (ct->rset_terms);
194 for (i = 0; i<info->p.rset_no; i++)
195 rset_delete (info->p.rset[i]);
196 xfree (info->p.rset);
200 static void r_rewind (RSFD rfd)
202 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
203 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
206 logf (LOG_DEBUG, "rsprox_rewind");
208 for (i = 0; i < info->p.rset_no; i++)
210 rset_rewind (info->p.rset[i], p->rfd[i]);
211 p->more[i] = rset_read (info->p.rset[i], p->rfd[i], p->buf[i], &dummy);
215 static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
216 int (*cmpfunc)(const void *p1, const void *p2),
217 const void *untilbuf)
219 /* Note: CT is not used. We _can_ pass NULL for it */
220 struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
221 struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
228 /* it's enough to forward first one. Other will follow
230 if ( p->more[0] && ((cmpfunc)(untilbuf, p->buf[0]) >= 2) )
231 p->more[0] = rset_forward(info->p.rset[0], p->rfd[0],
232 p->buf[0], &dummy, info->p.cmp,
237 for (i = 1; i < info->p.rset_no; i++)
241 p->more[0] = 0; /* saves us a goto out of while loop. */
244 cmp = (*info->p.cmp) (p->buf[i], p->buf[i-1]);
247 p->more[i-1] = rset_forward (info->p.rset[i-1], p->rfd[i-1],
255 if ((*info->p.getseq)(p->buf[i-1]) +1 != (*info->p.getseq)(p->buf[i]))
257 p->more[i-1] = rset_read (info->p.rset[i-1], p->rfd[i-1],
258 p->buf[i-1], &dummy);
264 p->more[i] = rset_forward (info->p.rset[i], p->rfd[i],
271 if (i == p->info->p.rset_no)
273 memcpy (buf, p->buf[0], info->p.key_size);
276 p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
284 static int r_count (RSET ct)
289 static int r_read (RSFD rfd, void *buf, int *term_index)
291 return r_forward(0, rfd, buf, term_index, 0, 0);
294 static int r_write (RSFD rfd, const void *buf)
296 logf (LOG_FATAL, "prox set type is read-only");