Added a routine to get an array of terms in a query, in preparation
[idzebra-moved-to-github.git] / rset / rset.c
1 /* $Id: rset.c,v 1.36 2004-10-22 10:12:51 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 #include <yaz/nmem.h>
30 #include <rset.h>
31
32
33 /* creates an rfd. Either allocates a new one, in which case the priv */
34 /* pointer is null, and will have to be filled in, or picks up one */
35 /* from the freelist, in which case the priv is already allocated, */
36 /* and presumably everything that hangs from it as well */
37
38 RSFD rfd_create_base(RSET rs)
39 {
40     RSFD rnew=rs->free_list;
41     if (rnew) {
42         rs->free_list=rnew->next;
43         assert(rnew->rset==rs);
44   /*    logf(LOG_DEBUG,"rfd-create_base (fl): rfd=%p rs=%p fl=%p priv=%p", 
45                        rnew, rs, rs->free_list, rnew->priv); */
46     } else {
47         rnew=nmem_malloc(rs->nmem, sizeof(*rnew));
48         rnew->priv=NULL;
49         rnew->rset=rs;
50   /*    logf(LOG_DEBUG,"rfd_create_base (new): rfd=%p rs=%p fl=%p priv=%p", 
51                        rnew, rs, rs->free_list, rnew->priv); */
52     }
53     rnew->next=NULL; /* not part of any (free?) list */
54     return rnew;
55 }
56
57 /* puts an rfd into the freelist of the rset. Only when the rset gets */
58 /* deleted, will all the nmem disappear */
59 void rfd_delete_base(RSFD rfd) 
60 {
61     RSET rs=rfd->rset;
62  /* logf(LOG_DEBUG,"rfd_delete_base: rfd=%p rs=%p priv=%p fl=%p",
63             rfd, rs, rfd->priv, rs->free_list); */
64     assert(NULL == rfd->next); 
65     rfd->next=rs->free_list;
66     rs->free_list=rfd;
67 }
68
69
70 RSET rset_create_base(const struct rset_control *sel, 
71                       NMEM nmem, const struct key_control *kcontrol,
72                       int scope, TERMID term)
73 {
74     RSET rnew;
75     NMEM M;
76     /* assert(nmem); */ /* can not yet be used, api/t4 fails */
77     if (nmem) 
78         M=nmem;
79     else
80         M=nmem_create();
81     rnew = (RSET) nmem_malloc(M,sizeof(*rnew));
82  /* logf (LOG_DEBUG, "rs_create(%s) rs=%p (nm=%p)", sel->desc, rnew, nmem); */
83     rnew->nmem=M;
84     if (nmem)
85         rnew->my_nmem=0;
86     else 
87         rnew->my_nmem=1;
88     rnew->control = sel;
89     rnew->count = 1;
90     rnew->priv = 0;
91     rnew->free_list=NULL;
92     rnew->keycontrol=kcontrol;
93     rnew->scope=scope;
94     rnew->term=term;
95     return rnew;
96 }
97
98 void rset_delete (RSET rs)
99 {
100     (rs->count)--;
101 /*  logf(LOG_DEBUG,"rs_delete(%s), rs=%p, count=%d",
102             rs->control->desc, rs, rs->count); */
103     if (!rs->count)
104     {
105         (*rs->control->f_delete)(rs);
106         if (rs->my_nmem)
107             nmem_destroy(rs->nmem);
108     }
109 }
110
111 RSET rset_dup (RSET rs)
112 {
113     (rs->count)++;
114     return rs;
115 }
116
117 #if 0
118 void rset_default_pos (RSFD rfd, double *current, double *total)
119 { /* This should never really be needed, but it is still used in */
120   /* those rsets that we don't really plan to use, like isam-s */
121     assert(rfd);
122     assert(current);
123     assert(total);
124     *current=-1; /* signal that pos is not implemented */
125     *total=-1;
126 } /* rset_default_pos */
127 #endif
128
129 int rset_default_forward(RSFD rfd, void *buf, TERMID *term,
130                            const void *untilbuf)
131 {
132     int more=1;
133     int cmp=rfd->rset->scope;
134     logf (LOG_DEBUG, "rset_default_forward starting '%s' (ct=%p rfd=%p)",
135                     rfd->rset->control->desc, rfd->rset, rfd);
136     /* key_logdump(LOG_DEBUG, untilbuf); */
137     while ( (cmp>=rfd->rset->scope) && (more))
138     {
139         logf (LOG_DEBUG, "rset_default_forward looping m=%d c=%d",more,cmp);
140         more=rset_read(rfd, buf, term);
141         if (more)
142             cmp=(rfd->rset->keycontrol->cmp)(untilbuf,buf);
143 /*        if (more)
144             key_logdump(LOG_DEBUG,buf); */
145     }
146     logf (LOG_DEBUG, "rset_default_forward exiting m=%d c=%d",more,cmp);
147
148     return more;
149 }
150
151 /** rset_get_no_terms is a getterms function for those that don't have any */
152 void rset_get_no_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
153 {
154     return;
155 }
156
157 /* rset_get_one_term gets that one term from an rset. Used by rsisamX */
158 void rset_get_one_term(RSET ct,TERMID *terms,int maxterms,int *curterm)
159 {
160     yaz_log(LOG_LOG,"FIXME: get_one_term: max=%d cur=%d", maxterms, *curterm);
161     if (ct->term)
162     {
163         yaz_log(LOG_LOG,"FIXME: get_one_term: '%s'", ct->term->name);
164         if (*curterm < maxterms)
165             terms[*curterm]=ct->term;
166         (*curterm)++;
167     }
168 }
169
170
171 TERMID rset_term_create (const char *name, int length, const char *flags,
172                                     int type, NMEM nmem)
173
174 {
175     TERMID t;
176     logf (LOG_DEBUG, "term_create '%s' %d f=%s type=%d nmem=%p",
177             name, length, flags, type, nmem);
178     t= (TERMID) nmem_malloc (nmem, sizeof(*t));
179     if (!name)
180         t->name = NULL;
181     else if (length == -1)
182         t->name = nmem_strdup(nmem,name);
183     else
184     {
185         t->name = (char*) nmem_malloc(nmem,length+1);
186         memcpy (t->name, name, length);
187         t->name[length] = '\0';
188     }
189     if (!flags)
190         t->flags = NULL;
191     else
192         t->flags = nmem_strdup(nmem,flags);
193     t->nn = -1;
194     t->count = 0;
195     t->type = type;
196     return t;
197 }
198
199