Added framework for the pos calls. rsisamb and rsnull have a real one,
[idzebra-moved-to-github.git] / rset / rsprox.c
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
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_read (RSFD rfd, void *buf, int *term_index);
44 static int r_write (RSFD rfd, const void *buf);
45
46 static const struct rset_control control_prox = 
47 {
48     "prox",
49     r_create,
50     r_open,
51     r_close,
52     r_delete,
53     r_rewind,
54     r_forward,
55     rset_default_pos,
56     r_read,
57     r_write,
58 };
59
60 const struct rset_control *rset_kind_prox = &control_prox;
61
62 struct rset_prox_info {
63     struct rset_prox_parms p;
64
65     struct rset_prox_rfd *rfd_list;
66 };
67
68 struct rset_prox_rfd {
69     RSFD *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;
74 };    
75
76 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
77 {
78     rset_prox_parms *prox_parms = (rset_prox_parms *) parms;
79     struct rset_prox_info *info;
80     int i;
81     char prox_term[512];
82     int length_prox_term = 0;
83     int min_nn = 10000000;
84     const char *flags = NULL;
85     int term_type = 0;
86
87
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;
95
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;
99
100     *prox_term = '\0';
101     for (i = 0; i<info->p.rset_no; i++)
102     {
103         int j;
104         for (j = 0; j < info->p.rset[i]->no_rset_terms; j++)
105         {
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)
110             {
111                 if (length_prox_term)
112                     prox_term[length_prox_term++] = ' ';
113                 strcpy (prox_term + length_prox_term, term);
114                 length_prox_term += lterm;
115             }
116             if (min_nn > info->p.rset[i]->rset_terms[j]->nn)
117                 min_nn = info->p.rset[i]->rset_terms[j]->nn;
118             flags = nflags;
119             term_type = info->p.rset[i]->rset_terms[j]->type;
120         }
121     }
122
123     ct->no_rset_terms = 1;
124     ct->rset_terms = (RSET_TERM *)
125         xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
126
127     ct->rset_terms[0] = rset_term_create (prox_term, length_prox_term,
128                                           flags, term_type);
129     return info;
130 }
131
132 static RSFD r_open (RSET ct, int flag)
133 {
134     struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
135     struct rset_prox_rfd *rfd;
136     int i, dummy;
137
138     if (flag & RSETF_WRITE)
139     {
140         logf (LOG_FATAL, "prox set type is read-only");
141         return NULL;
142     }
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;
147     rfd->info = info;
148
149     rfd->more = xmalloc (sizeof(*rfd->more) * info->p.rset_no);
150
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);
154
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);
158
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);
162     return rfd;
163 }
164
165 static void r_close (RSFD rfd)
166 {
167     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
168     struct rset_prox_rfd **rfdp;
169     
170     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
171         if (*rfdp == rfd)
172         {
173             int i;
174             for (i = 0; i<info->p.rset_no; i++)
175                 xfree ((*rfdp)->buf[i]);
176             xfree ((*rfdp)->buf);
177             xfree ((*rfdp)->more);
178
179             for (i = 0; i<info->p.rset_no; i++)
180                 rset_close (info->p.rset[i], (*rfdp)->rfd[i]);
181             xfree ((*rfdp)->rfd);
182
183             *rfdp = (*rfdp)->next;
184             xfree (rfd);
185             return;
186         }
187     logf (LOG_FATAL, "r_close but no rfd match!");
188     assert (0);
189 }
190
191 static void r_delete (RSET ct)
192 {
193     struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
194     int i;
195
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);
202     xfree (info);
203 }
204
205 static void r_rewind (RSFD rfd)
206 {
207     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
208     struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
209     int dummy, i;
210
211     logf (LOG_DEBUG, "rsprox_rewind");
212
213     for (i = 0; i < info->p.rset_no; i++)
214     {
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);
217     }
218 }
219
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)
223 {
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;
227     int cmp=0;
228     int i;
229     int dummy;
230
231     if (untilbuf)
232     {
233         /* it's enough to forward first one. Other will follow
234            automatically */
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,
238                                       untilbuf);
239     }
240     if (info->p.ordered && info->p.relation == 3 && info->p.exclusion == 0
241         && info->p.distance == 1)
242     {
243         while (p->more[0]) 
244         {
245             for (i = 1; i < info->p.rset_no; i++)
246             {
247                 if (!p->more[i]) 
248                 {
249                     p->more[0] = 0;    /* saves us a goto out of while loop. */
250                     break;
251                 }
252                 cmp = (*info->p.cmp) (p->buf[i], p->buf[i-1]);
253                 if (cmp > 1)
254                 {
255                     p->more[i-1] = rset_forward (info->p.rset[i-1],
256                                                  p->rfd[i-1],
257                                                  p->buf[i-1], &dummy,
258                                                  info->p.cmp,
259                                                  p->buf[i]);
260                     break;
261                 }
262                 else if (cmp == 1)
263                 {
264                     if ((*info->p.getseq)(p->buf[i-1]) +1 != 
265                         (*info->p.getseq)(p->buf[i]))
266                     {
267                         p->more[i-1] = rset_read (
268                             info->p.rset[i-1], p->rfd[i-1],
269                             p->buf[i-1], &dummy);
270                         break;
271                     }
272                 }
273                 else
274                 {
275                     p->more[i] = rset_forward (info->p.rset[i], p->rfd[i],
276                                                p->buf[i], &dummy,
277                                                info->p.cmp,
278                                                p->buf[i-1]);
279                     break;
280                 }
281             }
282             if (i == p->info->p.rset_no)
283             {
284                 memcpy (buf, p->buf[0], info->p.key_size);
285                 *term_index = 0;
286                 
287                 p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
288                                         p->buf[0], &dummy);
289                 return 1;
290             }
291         }
292     }
293     else if (info->p.rset_no == 2)
294     {
295         while (p->more[0] && p->more[1]) 
296         {
297             int cmp = (*info->p.cmp)(p->buf[0], p->buf[1]);
298             if (cmp < -1)
299                 p->more[0] = rset_forward (info->p.rset[0], p->rfd[0],
300                                            p->buf[0],
301                                            term_index, info->p.cmp, p->buf[0]);
302             else if (cmp > 1)
303                 p->more[1] = rset_forward (info->p.rset[1], p->rfd[1],
304                                            p->buf[1],
305                                            term_index, info->p.cmp, p->buf[1]);
306             else
307             {
308                 int seqno[500];
309                 int n = 0;
310                 
311                 seqno[n++] = (*info->p.getseq)(p->buf[0]);
312                 while ((p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
313                                                 p->buf[0],
314                                                 term_index)) >= -1 &&
315                        p->more[0] <= -1)
316                     if (n < 500)
317                         seqno[n++] = (*info->p.getseq)(p->buf[0]);
318                 
319                 for (i = 0; i<n; i++)
320                 {
321                     int diff = (*info->p.getseq)(p->buf[1]) - seqno[i];
322                     int excl = info->p.exclusion;
323                     if (!info->p.ordered && diff < 0)
324                         diff = -diff;
325                     switch (info->p.relation)
326                     {
327                     case 1:      /* < */
328                         if (diff < info->p.distance && diff >= 0)
329                             excl = !excl;
330                         break;
331                     case 2:      /* <= */
332                         if (diff <= info->p.distance && diff >= 0)
333                             excl = !excl;
334                         break;
335                     case 3:      /* == */
336                         if (diff == info->p.distance && diff >= 0)
337                             excl = !excl;
338                         break;
339                     case 4:      /* >= */
340                         if (diff >= info->p.distance && diff >= 0)
341                             excl = !excl;
342                         break;
343                     case 5:      /* > */
344                         if (diff > info->p.distance && diff >= 0)
345                             excl = !excl;
346                         break;
347                     case 6:      /* != */
348                         if (diff != info->p.distance && diff >= 0)
349                             excl = !excl;
350                         break;
351                     }
352                     if (excl)
353                     {
354                         memcpy (buf, p->buf[1], info->p.key_size);
355                         *term_index = 0;
356                         
357                         p->more[1] = rset_read (info->p.rset[1],
358                                                 p->rfd[1], p->buf[1],
359                                                 term_index);
360                         return 1;
361                     }
362                 }
363                 p->more[1] = rset_read (info->p.rset[1], p->rfd[1],
364                                         p->buf[1],
365                                         term_index);
366             }
367         }
368     }
369     return 0;
370 }
371
372
373 static int r_read (RSFD rfd, void *buf, int *term_index)
374 {
375     return r_forward(0, rfd, buf, term_index, 0, 0);
376 }
377
378 static int r_write (RSFD rfd, const void *buf)
379 {
380     logf (LOG_FATAL, "prox set type is read-only");
381     return -1;
382 }
383