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