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