Update copyright year + FSF address
[idzebra-moved-to-github.git] / rset / rsprox.c
1 /* $Id: rsprox.c,v 1.32 2006-08-14 10:40:21 adam Exp $
2    Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include <idzebra/util.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 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
43
44 static const struct rset_control control = 
45 {
46     "prox",
47     r_delete,
48     r_get_terms,
49     r_open,
50     r_close,
51     r_forward,
52     r_pos,
53     r_read,
54     r_write,
55 };
56
57 struct rset_prox_info {
58     int ordered;
59     int exclusion;
60     int relation;
61     int distance;
62 };
63
64 struct rset_prox_rfd {
65     RSFD *rfd;
66     char **buf;  /* lookahead key buffers */
67     char *more;  /* more in each lookahead? */
68     TERMID *terms; /* lookahead terms */
69     zint hits;
70 };    
71
72
73 RSET rset_create_prox(NMEM nmem, struct rset_key_control *kcontrol,
74                       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, 0,
80                                  rset_no, rset);
81     struct rset_prox_info *info;
82     info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
83     info->ordered = ordered;
84     info->exclusion = exclusion;
85     info->relation = relation;
86     info->distance = distance;
87     rnew->priv = info;
88     return rnew;
89 }
90
91 static void r_delete (RSET ct)
92 {
93 }
94
95 static RSFD r_open (RSET ct, int flag)
96 {
97     RSFD rfd;
98     struct rset_prox_rfd *p;
99     int i;
100
101     if (flag & RSETF_WRITE)
102     {
103         yaz_log(YLOG_FATAL, "prox set type is read-only");
104         return NULL;
105     }
106     rfd = rfd_create_base(ct);
107     if (rfd->priv)
108         p = (struct rset_prox_rfd *)(rfd->priv);
109     else {
110         p = (struct rset_prox_rfd *) nmem_malloc(ct->nmem,sizeof(*p));
111         rfd->priv = p;
112         p->more = nmem_malloc (ct->nmem,sizeof(*p->more) * ct->no_children);
113         p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * ct->no_children);
114         p->terms = nmem_malloc(ct->nmem,sizeof(*p->terms) * ct->no_children);
115         for (i = 0; i < ct->no_children; i++) 
116         {
117             p->buf[i] = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
118             p->terms[i] = 0;
119         }
120         p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * ct->no_children);
121     }
122     yaz_log(YLOG_DEBUG,"rsprox (%s) open [%p] n=%d", 
123             ct->control->desc, rfd, ct->no_children);
124
125     for (i = 0; i < ct->no_children; i++) {
126         p->rfd[i] = rset_open (ct->children[i], RSETF_READ);
127         p->more[i] = rset_read (p->rfd[i], p->buf[i], &p->terms[i]);
128     }
129     p->hits = 0;
130     return rfd;
131 }
132
133 static void r_close (RSFD rfd)
134 {
135     RSET ct = rfd->rset;
136     struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
137     
138     int i;
139     for (i = 0; i<ct->no_children; i++)
140         rset_close(p->rfd[i]);
141 }
142
143 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf)
144 {
145     RSET ct = rfd->rset;
146     struct rset_prox_info *info = (struct rset_prox_info *)(ct->priv);
147     struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
148     const struct rset_key_control *kctrl = ct->keycontrol;
149     int cmp = 0;
150     int i;
151
152     if (untilbuf)
153     {
154         /* it is enough to forward first one. Other will follow. */
155         if ( p->more[0] &&   /* was: cmp >=2 */
156            ((kctrl->cmp)(untilbuf, p->buf[0]) >= rfd->rset->scope) ) 
157             p->more[0] = rset_forward(p->rfd[0], p->buf[0], 
158                                       &p->terms[0], untilbuf);
159     }
160     if (info->ordered && info->relation == 3 && info->exclusion == 0
161         && info->distance == 1)
162     {
163         while (p->more[0]) 
164         {
165             for (i = 1; i < ct->no_children; i++)
166             {
167                 if (!p->more[i]) 
168                 {
169                     p->more[0] = 0; /* saves us a goto out of while loop. */
170                     break;
171                 }
172                 cmp = (*kctrl->cmp) (p->buf[i], p->buf[i-1]);
173                 if (cmp >= rfd->rset->scope )  /* cmp>1 */
174                 {
175                     p->more[i-1] = rset_forward (p->rfd[i-1],
176                                                  p->buf[i-1],
177                                                  &p->terms[i-1],
178                                                  p->buf[i]);
179                     break;
180                 }
181                 else if ( cmp>0 ) /* cmp == 1*/
182                 {
183                     if ((*kctrl->getseq)(p->buf[i-1]) +1 != 
184                         (*kctrl->getseq)(p->buf[i]))
185                     { /* FIXME - We need more flexible multilevel stuff */
186                         p->more[i-1] = rset_read ( p->rfd[i-1], p->buf[i-1],
187                                                    &p->terms[i-1]);
188                         break;
189                     }
190                 }
191                 else
192                 {
193                     p->more[i] = rset_forward (p->rfd[i], 
194                                   p->buf[i], &p->terms[i], p->buf[i-1]);
195                     break;
196                 }
197             }
198             if (i == ct->no_children)
199             {
200                 memcpy (buf, p->buf[0], kctrl->key_size);
201                 if (term)
202                     *term = p->terms[0];
203                 p->more[0] = rset_read (p->rfd[0], p->buf[0], &p->terms[0]);
204                 p->hits++;
205                 return 1;
206             }
207         }
208     }
209     else if (ct->no_children == 2)
210     {
211         while (p->more[0] && p->more[1]) 
212         {
213             int cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
214             if ( cmp <= - rfd->rset->scope) /* cmp<-1*/
215                 p->more[0] = rset_forward (p->rfd[0], p->buf[0], 
216                                            &p->terms[0],p->buf[1]);
217             else if ( cmp >= rfd->rset->scope ) /* cmp>1 */
218                 p->more[1] = rset_forward (p->rfd[1], p->buf[1], 
219                                            &p->terms[1],p->buf[0]);
220             else
221             {
222                 zint seqno[500]; /* FIXME - why 500 ?? */
223                 int n = 0;
224                 
225                 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
226                 while ((p->more[0] = rset_read (p->rfd[0],
227                                         p->buf[0], &p->terms[0])) >= -1 &&
228                        p->more[0] <= -1)
229                     if (n < 500)
230                         seqno[n++] = (*kctrl->getseq)(p->buf[0]);
231                 
232                 for (i = 0; i<n; i++)
233                 {
234                     zint diff = (*kctrl->getseq)(p->buf[1]) - seqno[i];
235                     int excl = info->exclusion;
236                     if (!info->ordered && diff < 0)
237                         diff = -diff;
238                     switch (info->relation)
239                     {
240                     case 1:      /* < */
241                         if (diff < info->distance && diff >= 0)
242                             excl = !excl;
243                         break;
244                     case 2:      /* <= */
245                         if (diff <= info->distance && diff >= 0)
246                             excl = !excl;
247                         break;
248                     case 3:      /* == */
249                         if (diff == info->distance && diff >= 0)
250                             excl = !excl;
251                         break;
252                     case 4:      /* >= */
253                         if (diff >= info->distance && diff >= 0)
254                             excl = !excl;
255                         break;
256                     case 5:      /* > */
257                         if (diff > info->distance && diff >= 0)
258                             excl = !excl;
259                         break;
260                     case 6:      /* != */
261                         if (diff != info->distance && diff >= 0)
262                             excl = !excl;
263                         break;
264                     }
265                     if (excl)
266                     {
267                         memcpy (buf, p->buf[1], kctrl->key_size);
268                         if (term)
269                             *term = p->terms[1];
270                         p->more[1] = rset_read ( p->rfd[1], p->buf[1],
271                                                  &p->terms[1]);
272                         p->hits++;
273                         return 1;
274                     }
275                 }
276                 p->more[1] = rset_read (p->rfd[1], p->buf[1], &p->terms[1]);
277             }
278         }
279     }
280     return 0;
281 }
282
283
284 static int r_read (RSFD rfd, void *buf, TERMID *term)
285 {
286     return r_forward(rfd, buf, term, 0);
287 }
288
289 static int r_write (RSFD rfd, const void *buf)
290 {
291     yaz_log(YLOG_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     RSET ct = rfd->rset;
298     struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
299     int i;
300     double r = 0.0;
301     double cur, tot = -1.0;
302     double scur = 0.0, stot = 0.0;
303
304     yaz_log(YLOG_DEBUG, "rsprox_pos");
305
306     for (i = 0; i < ct->no_children; 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 = (double) p->hits;
323         *total=*current/r ; 
324     }
325     yaz_log(YLOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
326                     i,*current, *total, r);
327 }
328
329 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
330 {
331     int i;
332     for (i = 0; i<ct->no_children; i++)
333         rset_getterms(ct->children[i], terms, maxterms, curterm);
334 }
335
336 /*
337  * Local variables:
338  * c-basic-offset: 4
339  * indent-tabs-mode: nil
340  * End:
341  * vim: shiftwidth=4 tabstop=8 expandtab
342  */
343