Removed the rewind function from rsets, it was unused
[idzebra-moved-to-github.git] / rset / rsprox.c
1 /* $Id: rsprox.c,v 1.17 2004-09-30 09:53:05 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 <rset.h>
30
31 #ifndef RSET_DEBUG
32 #define RSET_DEBUG 0
33 #endif
34
35 static RSFD r_open (RSET ct, int flag);
36 static void r_close (RSFD rfd);
37 static void r_delete (RSET ct);
38 static int r_forward(RSFD rfd, void *buf, const void *untilbuf);
39 static int r_read (RSFD rfd, void *buf);
40 static int r_write (RSFD rfd, const void *buf);
41 static void r_pos (RSFD rfd, double *current, double *total);
42
43 static const struct rset_control control = 
44 {
45     "prox",
46     r_delete,
47     r_open,
48     r_close,
49     r_forward,
50     r_pos,
51     r_read,
52     r_write,
53 };
54
55 const struct rset_control *rset_kind_prox = &control;
56
57 struct rset_prox_info {
58     RSET *rset;   /* array of 'child' rsets */
59     int rset_no;  /* how many of them */
60     int ordered;
61     int exclusion;
62     int relation;
63     int distance;
64 };
65
66 struct rset_prox_rfd {
67     RSFD *rfd;
68     char **buf;  /* lookahead key buffers */
69     char *more;  /* more in each lookahead? */
70     zint hits;
71 };    
72
73
74 RSET rsprox_create( NMEM nmem, const struct key_control *kcontrol, int scope,
75             int rset_no, RSET *rset,
76             int ordered, int exclusion,
77             int relation, int distance)
78 {
79     RSET rnew=rset_create_base(&control, nmem, kcontrol, scope);
80     struct rset_prox_info *info;
81     info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
82     info->rset = nmem_malloc(rnew->nmem,rset_no * sizeof(*info->rset));
83     memcpy(info->rset, rset,
84            rset_no * sizeof(*info->rset));
85     info->rset_no=rset_no;
86     info->ordered=ordered;
87     info->exclusion=exclusion;
88     info->relation=relation;
89     info->distance=distance;
90     rnew->priv=info;
91     return rnew;
92 }
93
94
95 static void r_delete (RSET ct)
96 {
97     struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
98     int i;
99
100     for (i = 0; i<info->rset_no; i++)
101         rset_delete (info->rset[i]);
102 }
103
104
105 static RSFD r_open (RSET ct, int flag)
106 {
107     struct rset_prox_info *info = (struct rset_prox_info *) ct->priv;
108     RSFD rfd;
109     struct rset_prox_rfd *p;
110     int i;
111
112     if (flag & RSETF_WRITE)
113     {
114         logf (LOG_FATAL, "prox set type is read-only");
115         return NULL;
116     }
117     rfd = rfd_create_base(ct);
118     if (rfd->priv)
119         p=(struct rset_prox_rfd *)(rfd->priv);
120     else {
121         p = (struct rset_prox_rfd *) nmem_malloc (ct->nmem,sizeof(*p));
122         rfd->priv=p;
123         p->more = nmem_malloc (ct->nmem,sizeof(*p->more) * info->rset_no);
124         p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * info->rset_no);
125         for (i = 0; i < info->rset_no; i++)
126             p->buf[i] = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
127         p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * info->rset_no);
128     }
129     logf(LOG_DEBUG,"rsprox (%s) open [%p] n=%d", 
130             ct->control->desc, rfd, info->rset_no);
131
132     for (i = 0; i < info->rset_no; i++) {
133         p->rfd[i] = rset_open (info->rset[i], RSETF_READ);
134         p->more[i] = rset_read (p->rfd[i], p->buf[i]);
135     }
136     p->hits=0;
137     return rfd;
138 }
139
140 static void r_close (RSFD rfd)
141 {
142     struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
143     struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
144     
145     int i;
146     for (i = 0; i<info->rset_no; i++)
147         rset_close (p->rfd[i]);
148     rfd_delete_base(rfd);
149 }
150
151 static int r_forward (RSFD rfd, void *buf, const void *untilbuf)
152 {
153     struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
154     struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
155     const struct key_control *kctrl=rfd->rset->keycontrol;
156     int cmp=0;
157     int i;
158
159     if (untilbuf)
160     {
161         /* it is enough to forward first one. Other will follow. */
162         if ( p->more[0] &&   /* was: cmp >=2 */
163            ((kctrl->cmp)(untilbuf, p->buf[0]) >= rfd->rset->scope) ) 
164             p->more[0] = rset_forward(p->rfd[0], p->buf[0], untilbuf);
165     }
166     if (info->ordered && info->relation == 3 && info->exclusion == 0
167         && info->distance == 1)
168     {
169         while (p->more[0]) 
170         {
171             for (i = 1; i < info->rset_no; i++)
172             {
173                 if (!p->more[i]) 
174                 {
175                     p->more[0] = 0; /* saves us a goto out of while loop. */
176                     break;
177                 }
178                 cmp = (*kctrl->cmp) (p->buf[i], p->buf[i-1]);
179                 if (cmp >= rfd->rset->scope )  /* cmp>1 */
180                 {
181                     p->more[i-1] = rset_forward (p->rfd[i-1],
182                                                  p->buf[i-1],
183                                                  p->buf[i]);
184                     break;
185                 }
186                 else if ( cmp>0 ) /* cmp == 1*/
187                 {
188                     if ((*kctrl->getseq)(p->buf[i-1]) +1 != 
189                         (*kctrl->getseq)(p->buf[i]))
190                     { /* FIXME - We need more flexible multilevel stuff */
191                         p->more[i-1] = rset_read ( p->rfd[i-1], p->buf[i-1]);
192                         break;
193                     }
194                 }
195                 else
196                 {
197                     p->more[i] = rset_forward (p->rfd[i], 
198                                   p->buf[i], p->buf[i-1]);
199                     break;
200                 }
201             }
202             if (i == info->rset_no)
203             {
204                 memcpy (buf, p->buf[0], kctrl->key_size);
205                 p->more[0] = rset_read (p->rfd[0], p->buf[0]);
206                 p->hits++;
207                 return 1;
208             }
209         }
210     }
211     else if (info->rset_no == 2)
212     {
213         while (p->more[0] && p->more[1]) 
214         {
215             int cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
216             if ( cmp <= - rfd->rset->scope) /* cmp<-1*/
217                 p->more[0] = rset_forward (p->rfd[0],
218                                            p->buf[0], p->buf[1]);
219             else if ( cmp >= rfd->rset->scope ) /* cmp>1 */
220                 p->more[1] = rset_forward (p->rfd[1],
221                                            p->buf[1], p->buf[0]);
222             else
223             {
224                 zint seqno[500]; /* FIXME - why 500 ?? */
225                 int n = 0;
226                 
227                 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
228                 while ((p->more[0] = rset_read (p->rfd[0],
229                                                 p->buf[0])) >= -1 &&
230                        p->more[0] <= -1)
231                     if (n < 500)
232                         seqno[n++] = (*kctrl->getseq)(p->buf[0]);
233                 
234                 for (i = 0; i<n; i++)
235                 {
236                     int diff = (*kctrl->getseq)(p->buf[1]) - seqno[i];
237                     int excl = info->exclusion;
238                     if (!info->ordered && diff < 0)
239                         diff = -diff;
240                     switch (info->relation)
241                     {
242                     case 1:      /* < */
243                         if (diff < info->distance && diff >= 0)
244                             excl = !excl;
245                         break;
246                     case 2:      /* <= */
247                         if (diff <= info->distance && diff >= 0)
248                             excl = !excl;
249                         break;
250                     case 3:      /* == */
251                         if (diff == info->distance && diff >= 0)
252                             excl = !excl;
253                         break;
254                     case 4:      /* >= */
255                         if (diff >= info->distance && diff >= 0)
256                             excl = !excl;
257                         break;
258                     case 5:      /* > */
259                         if (diff > info->distance && diff >= 0)
260                             excl = !excl;
261                         break;
262                     case 6:      /* != */
263                         if (diff != info->distance && diff >= 0)
264                             excl = !excl;
265                         break;
266                     }
267                     if (excl)
268                     {
269                         memcpy (buf, p->buf[1], kctrl->key_size);
270                         
271                         p->more[1] = rset_read ( p->rfd[1], p->buf[1]);
272                         p->hits++;
273                         return 1;
274                     }
275                 }
276                 p->more[1] = rset_read (p->rfd[1], p->buf[1]);
277             }
278         }
279     }
280     return 0;
281 }
282
283
284 static int r_read (RSFD rfd, void *buf)
285 {
286     return r_forward(rfd, buf, 0);
287 }
288
289 static int r_write (RSFD rfd, const void *buf)
290 {
291     logf (LOG_FATAL, "prox set type is read-only");
292     return -1;
293 }
294
295 static void r_pos (RSFD rfd, double *current, double *total)
296 {
297     struct rset_prox_info *info = (struct rset_prox_info *)(rfd->rset->priv);
298     struct rset_prox_rfd *p=(struct rset_prox_rfd *)(rfd->priv);
299     int i;
300     double cur,tot=-1;
301     double scur=0,stot=0;
302     double r;
303
304     logf (LOG_DEBUG, "rsprox_pos");
305
306     for (i = 0; i < info->rset_no; i++)
307     {
308         rset_pos(p->rfd[i],  &cur, &tot);
309         if (tot>0) {
310             scur += cur;
311             stot += tot;
312         }
313     }
314     if (tot <0) {  /* nothing found */
315         *current=-1;
316         *total=-1;
317     } else if (tot <1) { /* most likely tot==0 */
318         *current=0;
319         *total=0;
320     } else {
321         r=scur/stot; 
322         *current=p->hits;
323         *total=*current/r ; 
324     }
325     logf(LOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
326                     i,*current, *total, r);
327 }