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