Pass log function to some rsets
[idzebra-moved-to-github.git] / rset / rsbool.c
1 /* $Id: rsbool.c,v 1.28 2004-06-02 12:31:23 adam 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 <rsbool.h>
29 #include <zebrautl.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_count (RSET ct);
44 static int r_read_and (RSFD rfd, void *buf, int *term_index);
45 static int r_read_or (RSFD rfd, void *buf, int *term_index);
46 static int r_read_not (RSFD rfd, void *buf, int *term_index);
47 static int r_write (RSFD rfd, const void *buf);
48
49 static const struct rset_control control_and = 
50 {
51     "and",
52     r_create,
53     r_open,
54     r_close,
55     r_delete,
56     r_rewind,
57     r_forward, /* rset_default_forward, */
58     r_count,
59     r_read_and,
60     r_write,
61 };
62
63 static const struct rset_control control_or = 
64 {
65     "or",
66     r_create,
67     r_open,
68     r_close,
69     r_delete,
70     r_rewind,
71 #if 1
72     r_forward, 
73 #else
74     rset_default_forward,
75 #endif
76     r_count,
77     r_read_or,
78     r_write,
79 };
80
81 static const struct rset_control control_not = 
82 {
83     "not",
84     r_create,
85     r_open,
86     r_close,
87     r_delete,
88     r_rewind,
89     r_forward, 
90     r_count,
91     r_read_not,
92     r_write,
93 };
94
95
96 const struct rset_control *rset_kind_and = &control_and;
97 const struct rset_control *rset_kind_or = &control_or;
98 const struct rset_control *rset_kind_not = &control_not;
99
100 struct rset_bool_info {
101     int key_size;
102     RSET rset_l;
103     RSET rset_r;
104     int term_index_s;
105     int (*cmp)(const void *p1, const void *p2);
106     void (*log_item)(int logmask, const void *p, const char *txt);
107     struct rset_bool_rfd *rfd_list;
108 };
109
110 struct rset_bool_rfd {
111     RSFD rfd_l;
112     RSFD rfd_r;
113     int  more_l;
114     int  more_r;
115     int term_index_l;
116     int term_index_r;
117     void *buf_l;
118     void *buf_r;
119     int tail;
120     struct rset_bool_rfd *next;
121     struct rset_bool_info *info;
122 };    
123
124 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
125 {
126     rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
127     struct rset_bool_info *info;
128
129     info = (struct rset_bool_info *) xmalloc (sizeof(*info));
130     info->key_size = bool_parms->key_size;
131     info->rset_l = bool_parms->rset_l;
132     info->rset_r = bool_parms->rset_r;
133     if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
134         ct->flags |= RSET_FLAG_VOLATILE;
135     info->cmp = bool_parms->cmp;
136     info->log_item = bool_parms->log_item;
137     info->rfd_list = NULL;
138     
139     info->term_index_s = info->rset_l->no_rset_terms;
140     ct->no_rset_terms =
141         info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
142     ct->rset_terms = (RSET_TERM *)
143         xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
144
145     memcpy (ct->rset_terms, info->rset_l->rset_terms,
146             info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
147     memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
148             info->rset_r->rset_terms,
149             info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
150     return info;
151 }
152
153 static RSFD r_open (RSET ct, int flag)
154 {
155     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
156     struct rset_bool_rfd *rfd;
157
158     if (flag & RSETF_WRITE)
159     {
160         logf (LOG_FATAL, "bool set type is read-only");
161         return NULL;
162     }
163     rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
164     logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
165     rfd->next = info->rfd_list;
166     info->rfd_list = rfd;
167     rfd->info = info;
168
169     rfd->buf_l = xmalloc (info->key_size);
170     rfd->buf_r = xmalloc (info->key_size);
171     rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
172     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
173     rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
174                              &rfd->term_index_l);
175     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
176                              &rfd->term_index_r);
177     rfd->tail = 0;
178     return rfd;
179 }
180
181 static void r_close (RSFD rfd)
182 {
183     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
184     struct rset_bool_rfd **rfdp;
185     
186     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
187         if (*rfdp == rfd)
188         {
189             xfree ((*rfdp)->buf_l);
190             xfree ((*rfdp)->buf_r);
191             rset_close (info->rset_l, (*rfdp)->rfd_l);
192             rset_close (info->rset_r, (*rfdp)->rfd_r);
193             *rfdp = (*rfdp)->next;
194             xfree (rfd);
195             return;
196         }
197     logf (LOG_FATAL, "r_close but no rfd match!");
198     assert (0);
199 }
200
201 static void r_delete (RSET ct)
202 {
203     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
204
205     assert (info->rfd_list == NULL);
206     xfree (ct->rset_terms);
207     rset_delete (info->rset_l);
208     rset_delete (info->rset_r);
209     xfree (info);
210 }
211
212 static void r_rewind (RSFD rfd)
213 {
214     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
215     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
216
217     logf (LOG_DEBUG, "rsbool_rewind");
218     rset_rewind (info->rset_l, p->rfd_l);
219     rset_rewind (info->rset_r, p->rfd_r);
220     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
221     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
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     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
229     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
230     int cmp=0;
231     int rc;
232
233 #if RSET_DEBUG
234     logf (LOG_DEBUG, "rsbool_forward (L) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
235                       rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
236 #endif
237     if ( p->more_l && ((cmpfunc)(untilbuf,p->buf_l)==2) )
238         p->more_l = rset_forward(info->rset_l, p->rfd_l, p->buf_l,
239                         &p->term_index_l, info->cmp, untilbuf);
240 #if RSET_DEBUG
241     logf (LOG_DEBUG, "rsbool_forward (R) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
242                       rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
243 #endif
244     if ( p->more_r && ((cmpfunc)(untilbuf,p->buf_r)==2))
245         p->more_r = rset_forward(info->rset_r, p->rfd_r, p->buf_r,
246                         &p->term_index_r, info->cmp, untilbuf);
247 #if RSET_DEBUG
248     logf (LOG_DEBUG, "rsbool_forward [%p] calling read, m=%d,%d t=%d", 
249                        rfd, p->more_l, p->more_r, p->tail);
250 #endif
251     
252     p->tail=0; 
253     rc = rset_read(ct,rfd,buf,term_index); 
254 #if RSET_DEBUG
255     logf (LOG_DEBUG, "rsbool_forward returning [%p] %d m=%d,%d", 
256                        rfd, rc, p->more_l, p->more_r);
257 #endif
258     return rc;
259
260     if (p->more_l && p->more_r)
261            cmp = (*info->cmp)(p->buf_l, p->buf_r);
262        else if (p->more_l)
263            cmp = -2;
264        else
265            cmp = 2;
266     if ( (cmp<0) && (p->more_l) )
267     {
268         memcpy (buf, p->buf_l, info->key_size);
269             *term_index = p->term_index_l;
270 #if RSET_DEBUG
271         logf (LOG_DEBUG, "rsbool_forward returning L (cmp=%d)",cmp);
272 #endif
273         return 1;
274     } else if ( (cmp>0) && (p->more_r) )
275     {
276         memcpy (buf, p->buf_r, info->key_size);
277             *term_index = p->term_index_r + info->term_index_s;
278 #if RSET_DEBUG
279         logf (LOG_DEBUG, "rsbool_forward returning R (cmp=%d)",cmp);
280 #endif
281         return 1;
282     }
283     /* return ( p->more_l || p->more_r); */
284     return 0;
285 }
286
287 static int r_count (RSET ct)
288 {
289     return 0;
290 }
291
292
293 /*
294     1,1         1,3
295     1,9         2,1
296     1,11        3,1
297     2,9
298
299   1,1     1,1
300   1,3     1,3
301           1,9
302           1,11
303   2,1     2,1
304           2,9
305           3,1
306 */
307
308 static int r_read_and (RSFD rfd, void *buf, int *term_index)
309 {
310     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
311     struct rset_bool_info *info = p->info;
312
313     while (p->more_l || p->more_r)
314     {
315         int cmp;
316
317         if (p->more_l && p->more_r)
318             cmp = (*info->cmp)(p->buf_l, p->buf_r);
319         else if (p->more_l)
320             cmp = -2;
321         else
322             cmp = 2;
323 #if RSET_DEBUG
324         logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
325                         rfd, p->more_l, p->more_r, cmp, p->tail);
326         (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
327         (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
328 #endif
329         if (!cmp)
330         {
331             memcpy (buf, p->buf_l, info->key_size);
332                 *term_index = p->term_index_l;
333             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
334                                    &p->term_index_l);
335             p->tail = 1;
336         }
337         else if (cmp == 1)
338         {
339             memcpy (buf, p->buf_r, info->key_size);
340                 *term_index = p->term_index_r + info->term_index_s;
341             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
342                                    &p->term_index_r);
343             p->tail = 1;
344 #if RSET_DEBUG
345             logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
346                     rfd, p->more_l, p->more_r, cmp);
347             key_logdump(LOG_DEBUG,buf);
348             (*info->log_item)(LOG_DEBUG, buf, "");
349 #endif
350             return 1;
351         }
352         else if (cmp == -1)
353         {
354             memcpy (buf, p->buf_l, info->key_size);
355                 *term_index = p->term_index_l;
356             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
357                                    &p->term_index_l);
358             p->tail = 1;
359 #if RSET_DEBUG
360             logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
361                     rfd, p->more_l, p->more_r, cmp);
362             (*info->log_item)(LOG_DEBUG, buf, "");
363 #endif
364             return 1;
365         }
366         else if (cmp > 1)  /* cmp == 2 */
367         {
368 #define OLDCODE 0
369 #if OLDCODE
370             memcpy (buf, p->buf_r, info->key_size);
371             *term_index = p->term_index_r + info->term_index_s;
372             
373             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
374                                    &p->term_index_r);
375             if (p->tail)
376             {
377                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
378                     p->tail = 0;
379 #if RSET_DEBUG
380                 logf (LOG_DEBUG, "r_read_and returning C m=%d/%d c=%d",
381                         p->more_l, p->more_r, cmp);
382                 (*info->log_item)(LOG_DEBUG, buf, "");
383 #endif
384                 return 1;
385             }
386 #else
387             
388             if (p->tail)
389             {
390                 memcpy (buf, p->buf_r, info->key_size);
391                 *term_index = p->term_index_r + info->term_index_s;
392                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
393                                    &p->term_index_r);
394                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
395                     p->tail = 0;
396 #if RSET_DEBUG
397                 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
398                         rfd, p->more_l, p->more_r, cmp);
399                 (*info->log_item)(LOG_DEBUG, buf, "");
400 #endif
401                 return 1;
402             } else
403             {
404 #if RSET_DEBUG
405                 logf (LOG_DEBUG, "r_read_and [%p] about to forward R m=%d/%d c=%d",
406                         rfd, p->more_l, p->more_r, cmp);
407 #endif
408                 if (p->more_r && p->more_l)
409                     p->more_r = rset_forward( 
410                                     info->rset_r, p->rfd_r, 
411                                     p->buf_r, &p->term_index_r, 
412                                     (info->cmp), p->buf_l);
413                 else 
414                     return 0; /* no point in reading further */
415             }
416 #endif
417         }
418         else  /* cmp == -2 */
419         {
420 #if OLDCODE
421              memcpy (buf, p->buf_l, info->key_size);
422              *term_index = p->term_index_l;
423              p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
424                                     &p->term_index_l);
425              if (p->tail)
426              {
427                  if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
428                      p->tail = 0;
429 #if RSET_DEBUG
430                  logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
431                         rfd, p->more_l, p->more_r, cmp);
432                  (*info->log_item)(LOG_DEBUG, buf, "");
433 #endif
434                  return 1;
435              }
436 #else
437             if (p->tail)
438             {
439                 memcpy (buf, p->buf_l, info->key_size);
440                     *term_index = p->term_index_l;
441                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
442                                    &p->term_index_l);
443                 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
444                     p->tail = 0;
445 #if RSET_DEBUG
446                 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
447                         rfd, p->more_l, p->more_r, cmp);
448                 (*info->log_item)(LOG_DEBUG, buf, "");
449 #endif
450                 return 1;
451             }
452             else
453             {
454 #if RSET_DEBUG
455                 logf (LOG_DEBUG, "r_read_and [%p] about to forward L m=%d/%d c=%d",
456                         rfd, p->more_l, p->more_r, cmp);
457 #endif
458                 if (p->more_r && p->more_l)
459                     p->more_l = rset_forward( 
460                     /* p->more_l = rset_default_forward( */
461                                     info->rset_l, p->rfd_l, 
462                                     p->buf_l, &p->term_index_l, 
463                                     (info->cmp), p->buf_r);
464                 else 
465                     return 0; /* no point in reading further */
466             }
467 #endif
468         }
469     }
470 #if RSET_DEBUG
471     logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
472 #endif
473     return 0;
474 }
475
476 static int r_read_or (RSFD rfd, void *buf, int *term_index)
477 {
478     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
479     struct rset_bool_info *info = p->info;
480
481     while (p->more_l || p->more_r)
482     {
483         int cmp;
484
485         if (p->more_l && p->more_r)
486             cmp = (*info->cmp)(p->buf_l, p->buf_r);
487         else if (p->more_r)
488             cmp = 2;
489         else
490             cmp = -2;
491         if (!cmp)
492         {
493             memcpy (buf, p->buf_l, info->key_size);
494                 *term_index = p->term_index_l;
495             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
496                                    &p->term_index_l);
497             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
498                                    &p->term_index_r);
499 #if RSET_DEBUG
500             logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
501                     p->more_l, p->more_r, cmp);
502             (*info->log_item)(LOG_DEBUG, buf, "");
503 #endif
504             return 1;
505         }
506         else if (cmp > 0)
507         {
508             memcpy (buf, p->buf_r, info->key_size);
509                 *term_index = p->term_index_r + info->term_index_s;
510             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
511                                    &p->term_index_r);
512 #if RSET_DEBUG
513             logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
514                     p->more_l, p->more_r, cmp);
515             (*info->log_item)(LOG_DEBUG, buf, "");
516 #endif
517             return 1;
518         }
519         else
520         {
521             memcpy (buf, p->buf_l, info->key_size);
522                 *term_index = p->term_index_l;
523             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
524                                    &p->term_index_l);
525 #if RSET_DEBUG
526             logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
527                     p->more_l, p->more_r, cmp);
528             (*info->log_item)(LOG_DEBUG, buf, "");
529 #endif
530             return 1;
531         }
532     }
533     return 0;
534 }
535
536 static int r_read_not (RSFD rfd, void *buf, int *term_index)
537 {
538     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
539     struct rset_bool_info *info = p->info;
540
541     while (p->more_l || p->more_r)
542     {
543         int cmp;
544
545         if (p->more_l && p->more_r)
546             cmp = (*info->cmp)(p->buf_l, p->buf_r);
547         else if (p->more_r)
548             cmp = 2;
549         else
550             cmp = -2;
551         if (cmp < -1)
552         {
553             memcpy (buf, p->buf_l, info->key_size);
554                 *term_index = p->term_index_l;
555             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
556                                    &p->term_index_l);
557             return 1;
558         }
559         else if (cmp > 1)
560 #if 0
561             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
562                                    &p->term_index_r);
563 #else
564             p->more_r = rset_forward( 
565                             info->rset_r, p->rfd_r, 
566                             p->buf_r, &p->term_index_r, 
567                             (info->cmp), p->buf_l);
568 #endif
569         else
570         {
571             memcpy (buf, p->buf_l, info->key_size);
572             do
573             { 
574                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
575                                        &p->term_index_l);
576                 if (!p->more_l)
577                     break;
578                 cmp = (*info->cmp)(p->buf_l, buf);
579             } while (cmp >= -1 && cmp <= 1);
580             do
581             {
582                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
583                                        &p->term_index_r);
584                 if (!p->more_r)
585                     break;
586                 cmp = (*info->cmp)(p->buf_r, buf);
587             } while (cmp >= -1 && cmp <= 1);
588         }
589     }
590     return 0;
591 }
592
593
594 static int r_write (RSFD rfd, const void *buf)
595 {
596     logf (LOG_FATAL, "bool set type is read-only");
597     return -1;
598 }
599