072ff3a8c29e60dc8451bfc4a6e693f6eee5c75c
[idzebra-moved-to-github.git] / rset / rsprox.c
1 /* $Id: rsprox.c,v 1.2 2004-06-14 21:44:26 adam 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     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;
94
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;
98
99     *prox_term = '\0';
100     for (i = 0; i<info->p.rset_no; i++)
101     {
102         int j;
103         for (j = 0; j < info->p.rset[i]->no_rset_terms; j++)
104         {
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)
109             {
110                 if (length_prox_term)
111                     prox_term[length_prox_term++] = ' ';
112                 strcpy (prox_term + length_prox_term, term);
113                 length_prox_term += lterm;
114             }
115             if (min_nn > info->p.rset[i]->rset_terms[j]->nn)
116                 min_nn = info->p.rset[i]->rset_terms[j]->nn;
117             flags = nflags;
118             term_type = info->p.rset[i]->rset_terms[j]->type;
119         }
120     }
121
122     ct->no_rset_terms = 1;
123     ct->rset_terms = (RSET_TERM *)
124         xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
125
126     ct->rset_terms[0] = rset_term_create (prox_term, length_prox_term,
127                                           flags, term_type);
128     return info;
129 }
130
131 static RSFD r_open (RSET ct, int flag)
132 {
133     struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
134     struct rset_prox_rfd *rfd;
135     int i, dummy;
136
137     if (flag & RSETF_WRITE)
138     {
139         logf (LOG_FATAL, "prox set type is read-only");
140         return NULL;
141     }
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;
146     rfd->info = info;
147
148     rfd->more = xmalloc (sizeof(*rfd->more) * info->p.rset_no);
149
150     rfd->buf = xmalloc(sizeof(*rfd->buf) * info->p.rset_no);
151     for (i = 0; i < info->p.rset_no; i++)
152         rfd->buf[i] = xmalloc (info->p.key_size);
153
154     rfd->rfd = xmalloc(sizeof(*rfd->rfd) * info->p.rset_no);
155     for (i = 0; i < info->p.rset_no; i++)
156         rfd->rfd[i] = rset_open (info->p.rset[i], RSETF_READ);
157
158     for (i = 0; i < info->p.rset_no; i++)
159         rfd->more[i] = rset_read (info->p.rset[i], rfd->rfd[i],
160                                   rfd->buf[i], &dummy);
161     return rfd;
162 }
163
164 static void r_close (RSFD rfd)
165 {
166     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
167     struct rset_prox_rfd **rfdp;
168     
169     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
170         if (*rfdp == rfd)
171         {
172             int i;
173             for (i = 0; i<info->p.rset_no; i++)
174                 xfree ((*rfdp)->buf[i]);
175             xfree ((*rfdp)->buf);
176             xfree ((*rfdp)->more);
177
178             for (i = 0; i<info->p.rset_no; i++)
179                 rset_close (info->p.rset[i], (*rfdp)->rfd[i]);
180             xfree ((*rfdp)->rfd);
181
182             *rfdp = (*rfdp)->next;
183             xfree (rfd);
184             return;
185         }
186     logf (LOG_FATAL, "r_close but no rfd match!");
187     assert (0);
188 }
189
190 static void r_delete (RSET ct)
191 {
192     struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
193     int i;
194
195     assert (info->rfd_list == NULL);
196     rset_term_destroy(ct->rset_terms[0]);
197     xfree (ct->rset_terms);
198     for (i = 0; i<info->p.rset_no; i++)
199         rset_delete (info->p.rset[i]);
200     xfree (info->p.rset);
201     xfree (info);
202 }
203
204 static void r_rewind (RSFD rfd)
205 {
206     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
207     struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
208     int dummy, i;
209
210     logf (LOG_DEBUG, "rsprox_rewind");
211
212     for (i = 0; i < info->p.rset_no; i++)
213     {
214         rset_rewind (info->p.rset[i], p->rfd[i]);
215         p->more[i] = rset_read (info->p.rset[i], p->rfd[i], p->buf[i], &dummy);
216     }
217 }
218
219 static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
220                       int (*cmpfunc)(const void *p1, const void *p2),
221                       const void *untilbuf)
222 {
223     /* Note: CT is not used. We _can_ pass NULL for it */
224     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
225     struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
226     int cmp=0;
227     int i;
228     int dummy;
229
230     if (untilbuf)
231     {
232         /* it's enough to forward first one. Other will follow
233            automatically */
234         if ( p->more[0] && ((cmpfunc)(untilbuf, p->buf[0]) >= 2) )
235             p->more[0] = rset_forward(info->p.rset[0], p->rfd[0],
236                                       p->buf[0], &dummy, info->p.cmp,
237                                       untilbuf);
238     }
239     if (info->p.ordered && info->p.relation == 3 && info->p.exclusion == 0
240         && info->p.distance == 1)
241     {
242         while (p->more[0]) 
243         {
244             for (i = 1; i < info->p.rset_no; i++)
245             {
246                 if (!p->more[i]) 
247                 {
248                     p->more[0] = 0;    /* saves us a goto out of while loop. */
249                     break;
250                 }
251                 cmp = (*info->p.cmp) (p->buf[i], p->buf[i-1]);
252                 if (cmp > 1)
253                 {
254                     p->more[i-1] = rset_forward (info->p.rset[i-1], p->rfd[i-1],
255                                                  p->buf[i-1], &dummy,
256                                                  info->p.cmp,
257                                                  p->buf[i]);
258                     break;
259                 }
260                 else if (cmp == 1)
261                 {
262                     if ((*info->p.getseq)(p->buf[i-1]) +1 != (*info->p.getseq)(p->buf[i]))
263                     {
264                         p->more[i-1] = rset_read (info->p.rset[i-1], p->rfd[i-1],
265                                                   p->buf[i-1], &dummy);
266                         break;
267                     }
268                 }
269                 else
270                 {
271                     p->more[i] = rset_forward (info->p.rset[i], p->rfd[i],
272                                                p->buf[i], &dummy,
273                                                info->p.cmp,
274                                                p->buf[i-1]);
275                     break;
276                 }
277             }
278             if (i == p->info->p.rset_no)
279             {
280                 memcpy (buf, p->buf[0], info->p.key_size);
281                 *term_index = 0;
282                 
283                 p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
284                                         p->buf[0], &dummy);
285                 return 1;
286             }
287         }
288     }
289     else if (info->p.rset_no == 2)
290     {
291         while (p->more[0] && p->more[1]) 
292         {
293             int cmp = (*info->p.cmp)(p->buf[0], p->buf[1]);
294             if (cmp < -1)
295                 p->more[0] = rset_read (info->p.rset[0], p->rfd[0], p->buf[0],
296                                         term_index);
297             else if (cmp > 1)
298                 p->more[1] = rset_read (info->p.rset[1], p->rfd[1], p->buf[1],
299                                         term_index);
300             else
301             {
302                 int seqno[500];
303                 int n = 0;
304                 
305                 seqno[n++] = (*info->p.getseq)(p->buf[0]);
306                 while ((p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
307                                                 p->buf[0],
308                                                 term_index)) >= -1 &&
309                        p->more[0] <= -1)
310                     if (n < 500)
311                         seqno[n++] = (*info->p.getseq)(p->buf[0]);
312                 
313                 for (i = 0; i<n; i++)
314                 {
315                     int diff = (*info->p.getseq)(p->buf[1]) - seqno[i];
316                     int excl = info->p.exclusion;
317                     if (!info->p.ordered && diff < 0)
318                         diff = -diff;
319                     switch (info->p.relation)
320                     {
321                     case 1:      /* < */
322                         if (diff < info->p.distance && diff >= 0)
323                             excl = !excl;
324                         break;
325                     case 2:      /* <= */
326                         if (diff <= info->p.distance && diff >= 0)
327                             excl = !excl;
328                         break;
329                     case 3:      /* == */
330                         if (diff == info->p.distance && diff >= 0)
331                             excl = !excl;
332                         break;
333                     case 4:      /* >= */
334                         if (diff >= info->p.distance && diff >= 0)
335                             excl = !excl;
336                         break;
337                     case 5:      /* > */
338                         if (diff > info->p.distance && diff >= 0)
339                             excl = !excl;
340                         break;
341                     case 6:      /* != */
342                         if (diff != info->p.distance && diff >= 0)
343                             excl = !excl;
344                         break;
345                     }
346                     if (excl)
347                     {
348                         memcpy (buf, p->buf[1], info->p.key_size);
349                         *term_index = 0;
350                         
351                         p->more[1] = rset_read (info->p.rset[1],
352                                                 p->rfd[1], p->buf[1],
353                                                 term_index);
354                         return 1;
355                     }
356                 }
357                 p->more[1] = rset_read (info->p.rset[1], p->rfd[1],
358                                         p->buf[1],
359                                         term_index);
360             }
361         }
362     }
363     return 0;
364 }
365
366 static int r_count (RSET ct)
367 {
368     return 0;
369 }
370
371 static int r_read (RSFD rfd, void *buf, int *term_index)
372 {
373     return r_forward(0, rfd, buf, term_index, 0, 0);
374 }
375
376 static int r_write (RSFD rfd, const void *buf)
377 {
378     logf (LOG_FATAL, "prox set type is read-only");
379     return -1;
380 }
381