Removed the count function from the rset f-table, in preparation
[idzebra-moved-to-github.git] / rset / rsprox.c
1 /* $Id: rsprox.c,v 1.4 2004-08-03 12:15:45 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include <rsprox.h>
29 #include <zebrautl.h>
30
31 #ifndef RSET_DEBUG
32 #define RSET_DEBUG 0
33 #endif
34
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);
46
47 static const struct rset_control control_prox = 
48 {
49     "prox",
50     r_create,
51     r_open,
52     r_close,
53     r_delete,
54     r_rewind,
55     r_forward,
56     /* r_count, */
57     r_read,
58     r_write,
59 };
60
61 const struct rset_control *rset_kind_prox = &control_prox;
62
63 struct rset_prox_info {
64     struct rset_prox_parms p;
65
66     struct rset_prox_rfd *rfd_list;
67 };
68
69 struct rset_prox_rfd {
70     RSFD *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;
75 };    
76
77 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
78 {
79     rset_prox_parms *prox_parms = (rset_prox_parms *) parms;
80     struct rset_prox_info *info;
81     int i;
82     char prox_term[512];
83     int length_prox_term = 0;
84     int min_nn = 10000000;
85     const char *flags = NULL;
86     int term_type = 0;
87
88
89     info = (struct rset_prox_info *) xmalloc (sizeof(*info));
90     memcpy(&info->p, prox_parms, sizeof(struct rset_prox_parms));
91     assert(info->p.rset_no >= 2);
92     info->p.rset = xmalloc(info->p.rset_no * sizeof(*info->p.rset));
93     memcpy(info->p.rset, prox_parms->rset,
94            info->p.rset_no * sizeof(*info->p.rset));
95     info->rfd_list = NULL;
96
97     for (i = 0; i<info->p.rset_no; i++)
98         if (rset_is_volatile(info->p.rset[i]))
99             ct->flags |= RSET_FLAG_VOLATILE;
100
101     *prox_term = '\0';
102     for (i = 0; i<info->p.rset_no; i++)
103     {
104         int j;
105         for (j = 0; j < info->p.rset[i]->no_rset_terms; j++)
106         {
107             const char *nflags = info->p.rset[i]->rset_terms[j]->flags;
108             char *term = info->p.rset[i]->rset_terms[j]->name;
109             int lterm = strlen(term);
110             if (lterm + length_prox_term < sizeof(prox_term)-1)
111             {
112                 if (length_prox_term)
113                     prox_term[length_prox_term++] = ' ';
114                 strcpy (prox_term + length_prox_term, term);
115                 length_prox_term += lterm;
116             }
117             if (min_nn > info->p.rset[i]->rset_terms[j]->nn)
118                 min_nn = info->p.rset[i]->rset_terms[j]->nn;
119             flags = nflags;
120             term_type = info->p.rset[i]->rset_terms[j]->type;
121         }
122     }
123
124     ct->no_rset_terms = 1;
125     ct->rset_terms = (RSET_TERM *)
126         xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
127
128     ct->rset_terms[0] = rset_term_create (prox_term, length_prox_term,
129                                           flags, term_type);
130     return info;
131 }
132
133 static RSFD r_open (RSET ct, int flag)
134 {
135     struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
136     struct rset_prox_rfd *rfd;
137     int i, dummy;
138
139     if (flag & RSETF_WRITE)
140     {
141         logf (LOG_FATAL, "prox set type is read-only");
142         return NULL;
143     }
144     rfd = (struct rset_prox_rfd *) xmalloc (sizeof(*rfd));
145     logf(LOG_DEBUG,"rsprox (%s) open [%p]", ct->control->desc, rfd);
146     rfd->next = info->rfd_list;
147     info->rfd_list = rfd;
148     rfd->info = info;
149
150     rfd->more = xmalloc (sizeof(*rfd->more) * info->p.rset_no);
151
152     rfd->buf = xmalloc(sizeof(*rfd->buf) * info->p.rset_no);
153     for (i = 0; i < info->p.rset_no; i++)
154         rfd->buf[i] = xmalloc (info->p.key_size);
155
156     rfd->rfd = xmalloc(sizeof(*rfd->rfd) * info->p.rset_no);
157     for (i = 0; i < info->p.rset_no; i++)
158         rfd->rfd[i] = rset_open (info->p.rset[i], RSETF_READ);
159
160     for (i = 0; i < info->p.rset_no; i++)
161         rfd->more[i] = rset_read (info->p.rset[i], rfd->rfd[i],
162                                   rfd->buf[i], &dummy);
163     return rfd;
164 }
165
166 static void r_close (RSFD rfd)
167 {
168     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
169     struct rset_prox_rfd **rfdp;
170     
171     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
172         if (*rfdp == rfd)
173         {
174             int i;
175             for (i = 0; i<info->p.rset_no; i++)
176                 xfree ((*rfdp)->buf[i]);
177             xfree ((*rfdp)->buf);
178             xfree ((*rfdp)->more);
179
180             for (i = 0; i<info->p.rset_no; i++)
181                 rset_close (info->p.rset[i], (*rfdp)->rfd[i]);
182             xfree ((*rfdp)->rfd);
183
184             *rfdp = (*rfdp)->next;
185             xfree (rfd);
186             return;
187         }
188     logf (LOG_FATAL, "r_close but no rfd match!");
189     assert (0);
190 }
191
192 static void r_delete (RSET ct)
193 {
194     struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
195     int i;
196
197     assert (info->rfd_list == NULL);
198     rset_term_destroy(ct->rset_terms[0]);
199     xfree (ct->rset_terms);
200     for (i = 0; i<info->p.rset_no; i++)
201         rset_delete (info->p.rset[i]);
202     xfree (info->p.rset);
203     xfree (info);
204 }
205
206 static void r_rewind (RSFD rfd)
207 {
208     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
209     struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
210     int dummy, i;
211
212     logf (LOG_DEBUG, "rsprox_rewind");
213
214     for (i = 0; i < info->p.rset_no; i++)
215     {
216         rset_rewind (info->p.rset[i], p->rfd[i]);
217         p->more[i] = rset_read (info->p.rset[i], p->rfd[i], p->buf[i], &dummy);
218     }
219 }
220
221 static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
222                       int (*cmpfunc)(const void *p1, const void *p2),
223                       const void *untilbuf)
224 {
225     /* Note: CT is not used. We _can_ pass NULL for it */
226     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
227     struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
228     int cmp=0;
229     int i;
230     int dummy;
231
232     if (untilbuf)
233     {
234         /* it's enough to forward first one. Other will follow
235            automatically */
236         if ( p->more[0] && ((cmpfunc)(untilbuf, p->buf[0]) >= 2) )
237             p->more[0] = rset_forward(info->p.rset[0], p->rfd[0],
238                                       p->buf[0], &dummy, info->p.cmp,
239                                       untilbuf);
240     }
241     if (info->p.ordered && info->p.relation == 3 && info->p.exclusion == 0
242         && info->p.distance == 1)
243     {
244         while (p->more[0]) 
245         {
246             for (i = 1; i < info->p.rset_no; i++)
247             {
248                 if (!p->more[i]) 
249                 {
250                     p->more[0] = 0;    /* saves us a goto out of while loop. */
251                     break;
252                 }
253                 cmp = (*info->p.cmp) (p->buf[i], p->buf[i-1]);
254                 if (cmp > 1)
255                 {
256                     p->more[i-1] = rset_forward (info->p.rset[i-1],
257                                                  p->rfd[i-1],
258                                                  p->buf[i-1], &dummy,
259                                                  info->p.cmp,
260                                                  p->buf[i]);
261                     break;
262                 }
263                 else if (cmp == 1)
264                 {
265                     if ((*info->p.getseq)(p->buf[i-1]) +1 != 
266                         (*info->p.getseq)(p->buf[i]))
267                     {
268                         p->more[i-1] = rset_read (
269                             info->p.rset[i-1], p->rfd[i-1],
270                             p->buf[i-1], &dummy);
271                         break;
272                     }
273                 }
274                 else
275                 {
276                     p->more[i] = rset_forward (info->p.rset[i], p->rfd[i],
277                                                p->buf[i], &dummy,
278                                                info->p.cmp,
279                                                p->buf[i-1]);
280                     break;
281                 }
282             }
283             if (i == p->info->p.rset_no)
284             {
285                 memcpy (buf, p->buf[0], info->p.key_size);
286                 *term_index = 0;
287                 
288                 p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
289                                         p->buf[0], &dummy);
290                 return 1;
291             }
292         }
293     }
294     else if (info->p.rset_no == 2)
295     {
296         while (p->more[0] && p->more[1]) 
297         {
298             int cmp = (*info->p.cmp)(p->buf[0], p->buf[1]);
299             if (cmp < -1)
300                 p->more[0] = rset_forward (info->p.rset[0], p->rfd[0],
301                                            p->buf[0],
302                                            term_index, info->p.cmp, p->buf[0]);
303             else if (cmp > 1)
304                 p->more[1] = rset_forward (info->p.rset[1], p->rfd[1],
305                                            p->buf[1],
306                                            term_index, info->p.cmp, p->buf[1]);
307             else
308             {
309                 int seqno[500];
310                 int n = 0;
311                 
312                 seqno[n++] = (*info->p.getseq)(p->buf[0]);
313                 while ((p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
314                                                 p->buf[0],
315                                                 term_index)) >= -1 &&
316                        p->more[0] <= -1)
317                     if (n < 500)
318                         seqno[n++] = (*info->p.getseq)(p->buf[0]);
319                 
320                 for (i = 0; i<n; i++)
321                 {
322                     int diff = (*info->p.getseq)(p->buf[1]) - seqno[i];
323                     int excl = info->p.exclusion;
324                     if (!info->p.ordered && diff < 0)
325                         diff = -diff;
326                     switch (info->p.relation)
327                     {
328                     case 1:      /* < */
329                         if (diff < info->p.distance && diff >= 0)
330                             excl = !excl;
331                         break;
332                     case 2:      /* <= */
333                         if (diff <= info->p.distance && diff >= 0)
334                             excl = !excl;
335                         break;
336                     case 3:      /* == */
337                         if (diff == info->p.distance && diff >= 0)
338                             excl = !excl;
339                         break;
340                     case 4:      /* >= */
341                         if (diff >= info->p.distance && diff >= 0)
342                             excl = !excl;
343                         break;
344                     case 5:      /* > */
345                         if (diff > info->p.distance && diff >= 0)
346                             excl = !excl;
347                         break;
348                     case 6:      /* != */
349                         if (diff != info->p.distance && diff >= 0)
350                             excl = !excl;
351                         break;
352                     }
353                     if (excl)
354                     {
355                         memcpy (buf, p->buf[1], info->p.key_size);
356                         *term_index = 0;
357                         
358                         p->more[1] = rset_read (info->p.rset[1],
359                                                 p->rfd[1], p->buf[1],
360                                                 term_index);
361                         return 1;
362                     }
363                 }
364                 p->more[1] = rset_read (info->p.rset[1], p->rfd[1],
365                                         p->buf[1],
366                                         term_index);
367             }
368         }
369     }
370     return 0;
371 }
372
373 /*
374 static int r_count (RSET ct)
375 {
376     return 0;
377 }
378 */
379
380 static int r_read (RSFD rfd, void *buf, int *term_index)
381 {
382     return r_forward(0, rfd, buf, term_index, 0, 0);
383 }
384
385 static int r_write (RSFD rfd, const void *buf)
386 {
387     logf (LOG_FATAL, "prox set type is read-only");
388     return -1;
389 }
390