the new multior. Seems to work, forwards OK, does not yet estimate pos.
[idzebra-moved-to-github.git] / rset / rset.c
1 /* $Id: rset.c,v 1.24 2004-08-19 12:49:15 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
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <zebrautl.h>
28 #include <assert.h>
29
30 #include <rset.h>
31 #include <../index/index.h> /* for log_keydump. Debugging only */
32
33 RSET rset_create(const struct rset_control *sel, void *parms)
34 {
35     RSET rnew;
36     int i;
37
38     logf (LOG_DEBUG, "rs_create(%s)", sel->desc);
39     rnew = (RSET) xmalloc(sizeof(*rnew));
40     rnew->control = sel;
41     rnew->flags = 0;
42     rnew->count = 1;
43     rnew->rset_terms = NULL;
44     rnew->no_rset_terms = 0;
45     rnew->buf = (*sel->f_create)(rnew, sel, parms);
46     logf (LOG_DEBUG, "no_rset_terms: %d", rnew->no_rset_terms);
47     for (i = 0; i<rnew->no_rset_terms; i++)
48         logf (LOG_DEBUG, " %s", rnew->rset_terms[i]->name);
49     return rnew;
50 }
51
52 void rset_delete (RSET rs)
53 {
54     (rs->count)--;
55     if (!rs->count)
56     {
57         (*rs->control->f_delete)(rs);
58         xfree(rs);
59     }
60 }
61
62 RSET rset_dup (RSET rs)
63 {
64     (rs->count)++;
65     return rs;
66 }
67
68 void rset_default_pos (RSFD rfd, double *current, double *total)
69 { /* This should never really be needed, but it is still used in */
70   /* those rsets that we don't really plan to use, like isam-s */
71     assert(rfd);
72     assert(current);
73     assert(total);
74     *current=-1; /* signal that pos is not implemented */
75     *total=-1;
76 } /* rset_default_pos */
77
78 int rset_default_forward(RSET ct, RSFD rfd, void *buf, int *term_index, 
79                            int (*cmpfunc)(const void *p1, const void *p2), 
80                            const void *untilbuf)
81 {
82     int more=1;
83     int cmp=2;
84     logf (LOG_DEBUG, "rset_default_forward starting '%s' (ct=%p rfd=%p)",
85                     ct->control->desc, ct,rfd);
86     key_logdump(LOG_DEBUG, untilbuf);
87     while ( (cmp==2) && (more))
88     {
89         logf (LOG_DEBUG, "rset_default_forward looping m=%d c=%d",more,cmp);
90         more=rset_read(ct, rfd, buf, term_index);
91         if (more)
92             cmp=(*cmpfunc)(untilbuf,buf);
93         if (more)
94             key_logdump(LOG_DEBUG,buf);
95     }
96     logf (LOG_DEBUG, "rset_default_forward exiting m=%d c=%d",more,cmp);
97
98     return more;
99 }
100
101 RSET_TERM *rset_terms(RSET rs, int *no)
102 {
103     *no = rs->no_rset_terms;
104     return rs->rset_terms;
105 }
106
107 RSET_TERM rset_term_create (const char *name, int length, const char *flags,
108                             int type)
109 {
110     RSET_TERM t = (RSET_TERM) xmalloc (sizeof(*t));
111     if (!name)
112         t->name = NULL;
113     else if (length == -1)
114         t->name = xstrdup (name);
115     else
116     {
117         t->name = (char*) xmalloc (length+1);
118         memcpy (t->name, name, length);
119         t->name[length] = '\0';
120     }
121     if (!flags)
122         t->flags = NULL;
123     else
124         t->flags = xstrdup (flags);
125     t->nn = -1;
126     t->count = 0;
127     t->type = type;
128     return t;
129 }
130
131 void rset_term_destroy (RSET_TERM t)
132 {
133     if (t) { /* rsmultior uses things without terms at all ! */
134         xfree (t->name);
135         xfree (t->flags);
136         xfree (t);
137     }
138 }
139
140 RSET_TERM rset_term_dup (RSET_TERM t)
141 {
142     RSET_TERM nt = (RSET_TERM) xmalloc (sizeof(*nt));
143     if (t->name)
144         nt->name = xstrdup (t->name);
145     else
146         nt->name = NULL;
147     if (t->flags)
148         nt->flags = xstrdup (t->flags);
149     else
150         nt->flags = NULL;
151     nt->nn = t->nn;
152     return nt;
153 }