0932c9d85989e238b7fc430b64ce810a59235634
[idzebra-moved-to-github.git] / rset / rsprox.c
1 /* $Id: rsprox.c,v 1.9 2004-08-20 14:44:46 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 <zebrautl.h>
29 #include <rsprox.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, 
41                      int (*cmpfunc)(const void *p1, const void *p2),
42                      const void *untilbuf);
43 static int r_read (RSFD rfd, void *buf);
44 static int r_write (RSFD rfd, const void *buf);
45 static void r_pos (RSFD rfd, double *current, double *total);
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_pos,
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     zint hits;
76 };    
77
78 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
79 {
80     rset_prox_parms *prox_parms = (rset_prox_parms *) parms;
81     struct rset_prox_info *info;
82     int i;
83     /*
84     char prox_term[512];
85     int length_prox_term = 0;
86     zint min_nn = 10000000;
87     const char *flags = NULL;
88     int term_type = 0;
89 */
90
91     info = (struct rset_prox_info *) xmalloc (sizeof(*info));
92     memcpy(&info->p, prox_parms, sizeof(struct rset_prox_parms));
93     assert(info->p.rset_no >= 2);
94     info->p.rset = xmalloc(info->p.rset_no * sizeof(*info->p.rset));
95     memcpy(info->p.rset, prox_parms->rset,
96            info->p.rset_no * sizeof(*info->p.rset));
97     info->rfd_list = NULL;
98
99     for (i = 0; i<info->p.rset_no; i++)
100         if (rset_is_volatile(info->p.rset[i]))
101             ct->flags |= RSET_FLAG_VOLATILE;
102
103 #if 0 /* This should all be about terms! */
104     *prox_term = '\0';
105     for (i = 0; i<info->p.rset_no; i++)
106     {
107         int j;
108         for (j = 0; j < info->p.rset[i]->no_rset_terms; j++)
109         {
110             const char *nflags = info->p.rset[i]->rset_terms[j]->flags;
111             char *term = info->p.rset[i]->rset_terms[j]->name;
112             int lterm = strlen(term);
113             if (lterm + length_prox_term < sizeof(prox_term)-1)
114             {
115                 if (length_prox_term)
116                     prox_term[length_prox_term++] = ' ';
117                 strcpy (prox_term + length_prox_term, term);
118                 length_prox_term += lterm;
119             }
120             if (min_nn > info->p.rset[i]->rset_terms[j]->nn)
121                 min_nn = info->p.rset[i]->rset_terms[j]->nn;
122             flags = nflags;
123             term_type = info->p.rset[i]->rset_terms[j]->type;
124         }
125     }
126 #endif 
127     return info;
128 }
129
130 static RSFD r_open (RSET ct, int flag)
131 {
132     struct rset_prox_info *info = (struct rset_prox_info *) ct->buf;
133     struct rset_prox_rfd *rfd;
134     int i;
135
136     if (flag & RSETF_WRITE)
137     {
138         logf (LOG_FATAL, "prox set type is read-only");
139         return NULL;
140     }
141     rfd = (struct rset_prox_rfd *) xmalloc (sizeof(*rfd));
142     logf(LOG_DEBUG,"rsprox (%s) open [%p]", ct->control->desc, rfd);
143     rfd->next = info->rfd_list;
144     info->rfd_list = rfd;
145     rfd->info = info;
146
147     rfd->more = xmalloc (sizeof(*rfd->more) * info->p.rset_no);
148
149     rfd->buf = xmalloc(sizeof(*rfd->buf) * info->p.rset_no);
150     for (i = 0; i < info->p.rset_no; i++)
151         rfd->buf[i] = xmalloc (info->p.key_size);
152
153     rfd->rfd = xmalloc(sizeof(*rfd->rfd) * info->p.rset_no);
154     for (i = 0; i < info->p.rset_no; i++)
155         rfd->rfd[i] = rset_open (info->p.rset[i], RSETF_READ);
156
157     for (i = 0; i < info->p.rset_no; i++)
158         rfd->more[i] = rset_read (info->p.rset[i], rfd->rfd[i],
159                                   rfd->buf[i]);
160     rfd->hits=0;
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     for (i = 0; i<info->p.rset_no; i++)
197         rset_delete (info->p.rset[i]);
198     xfree (info->p.rset);
199     xfree (info);
200 }
201
202 static void r_rewind (RSFD rfd)
203 {
204     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
205     struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
206     int i;
207
208     logf (LOG_DEBUG, "rsprox_rewind");
209
210     for (i = 0; i < info->p.rset_no; i++)
211     {
212         rset_rewind (info->p.rset[i], p->rfd[i]);
213         p->more[i] = rset_read (info->p.rset[i], p->rfd[i], p->buf[i]);
214     }
215     p->hits=0;
216 }
217
218 static int r_forward (RSET ct, RSFD rfd, void *buf, 
219                       int (*cmpfunc)(const void *p1, const void *p2),
220                       const void *untilbuf)
221 {
222     /* Note: CT is not used. We _can_ pass NULL for it */
223     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
224     struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
225     int cmp=0;
226     int i;
227
228     if (untilbuf)
229     {
230         /* it's enough to forward first one. Other will follow
231            automatically */
232         if ( p->more[0] && ((cmpfunc)(untilbuf, p->buf[0]) >= 2) )
233             p->more[0] = rset_forward(info->p.rset[0], p->rfd[0],
234                                       p->buf[0], info->p.cmp,
235                                       untilbuf);
236     }
237     if (info->p.ordered && info->p.relation == 3 && info->p.exclusion == 0
238         && info->p.distance == 1)
239     {
240         while (p->more[0]) 
241         {
242             for (i = 1; i < info->p.rset_no; i++)
243             {
244                 if (!p->more[i]) 
245                 {
246                     p->more[0] = 0;    /* saves us a goto out of while loop. */
247                     break;
248                 }
249                 cmp = (*info->p.cmp) (p->buf[i], p->buf[i-1]);
250                 if (cmp > 1)
251                 {
252                     p->more[i-1] = rset_forward (info->p.rset[i-1],
253                                                  p->rfd[i-1],
254                                                  p->buf[i-1],
255                                                  info->p.cmp,
256                                                  p->buf[i]);
257                     break;
258                 }
259                 else if (cmp == 1)
260                 {
261                     if ((*info->p.getseq)(p->buf[i-1]) +1 != 
262                         (*info->p.getseq)(p->buf[i]))
263                     {
264                         p->more[i-1] = rset_read ( info->p.rset[i-1], 
265                                              p->rfd[i-1], p->buf[i-1]);
266                         break;
267                     }
268                 }
269                 else
270                 {
271                     p->more[i] = rset_forward (info->p.rset[i], p->rfd[i],
272                                                p->buf[i], info->p.cmp,
273                                                p->buf[i-1]);
274                     break;
275                 }
276             }
277             if (i == p->info->p.rset_no)
278             {
279                 memcpy (buf, p->buf[0], info->p.key_size);
280                 p->more[0] = rset_read (info->p.rset[0], p->rfd[0], p->buf[0]);
281                 p->hits++;
282                 return 1;
283             }
284         }
285     }
286     else if (info->p.rset_no == 2)
287     {
288         while (p->more[0] && p->more[1]) 
289         {
290             int cmp = (*info->p.cmp)(p->buf[0], p->buf[1]);
291             if (cmp < -1)
292                 p->more[0] = rset_forward (info->p.rset[0], p->rfd[0],
293                                            p->buf[0], info->p.cmp, p->buf[0]);
294             else if (cmp > 1)
295                 p->more[1] = rset_forward (info->p.rset[1], p->rfd[1],
296                                            p->buf[1], info->p.cmp, p->buf[1]);
297             else
298             {
299                 int seqno[500];
300                 int n = 0;
301                 
302                 seqno[n++] = (*info->p.getseq)(p->buf[0]);
303                 while ((p->more[0] = rset_read (info->p.rset[0], p->rfd[0],
304                                                 p->buf[0])) >= -1 &&
305                        p->more[0] <= -1)
306                     if (n < 500)
307                         seqno[n++] = (*info->p.getseq)(p->buf[0]);
308                 
309                 for (i = 0; i<n; i++)
310                 {
311                     int diff = (*info->p.getseq)(p->buf[1]) - seqno[i];
312                     int excl = info->p.exclusion;
313                     if (!info->p.ordered && diff < 0)
314                         diff = -diff;
315                     switch (info->p.relation)
316                     {
317                     case 1:      /* < */
318                         if (diff < info->p.distance && diff >= 0)
319                             excl = !excl;
320                         break;
321                     case 2:      /* <= */
322                         if (diff <= info->p.distance && diff >= 0)
323                             excl = !excl;
324                         break;
325                     case 3:      /* == */
326                         if (diff == info->p.distance && diff >= 0)
327                             excl = !excl;
328                         break;
329                     case 4:      /* >= */
330                         if (diff >= info->p.distance && diff >= 0)
331                             excl = !excl;
332                         break;
333                     case 5:      /* > */
334                         if (diff > info->p.distance && diff >= 0)
335                             excl = !excl;
336                         break;
337                     case 6:      /* != */
338                         if (diff != info->p.distance && diff >= 0)
339                             excl = !excl;
340                         break;
341                     }
342                     if (excl)
343                     {
344                         memcpy (buf, p->buf[1], info->p.key_size);
345                         
346                         p->more[1] = rset_read (info->p.rset[1],
347                                                 p->rfd[1], p->buf[1]);
348                         p->hits++;
349                         return 1;
350                     }
351                 }
352                 p->more[1] = rset_read (info->p.rset[1], p->rfd[1],
353                                         p->buf[1]);
354             }
355         }
356     }
357     return 0;
358 }
359
360
361 static int r_read (RSFD rfd, void *buf)
362 {
363     { double cur,tot; r_pos(rfd,&cur,&tot); } /*!*/
364     return r_forward(0, rfd, buf, 0, 0);
365 }
366
367 static int r_write (RSFD rfd, const void *buf)
368 {
369     logf (LOG_FATAL, "prox set type is read-only");
370     return -1;
371 }
372
373 static void r_pos (RSFD rfd, double *current, double *total)
374 {
375     struct rset_prox_info *info = ((struct rset_prox_rfd*)rfd)->info;
376     struct rset_prox_rfd *p = (struct rset_prox_rfd *) rfd;
377     int i;
378     double cur,tot=-1;
379     double scur=0,stot=0;
380     double r;
381
382     logf (LOG_DEBUG, "rsprox_pos");
383
384     for (i = 0; i < info->p.rset_no; i++)
385     {
386         rset_pos(info->p.rset[i], p->rfd[i],  &cur, &tot);
387         if (tot>0) {
388             scur += cur;
389             stot += tot;
390         }
391     }
392     if (tot <0) {  /* nothing found */
393         *current=-1;
394         *total=-1;
395     } else if (tot <1) { /* most likely tot==0 */
396         *current=0;
397         *total=0;
398     } else {
399         r=scur/stot; 
400         *current=p->hits;
401         *total=*current/r ; 
402     }
403     logf(LOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
404                     i,*current, *total, r);
405 }