Bump year. Change Aps->ApS
[idzebra-moved-to-github.git] / rset / rset.c
1 /* $Id: rset.c,v 1.42 2005-01-15 19:38:34 adam Exp $
2    Copyright (C) 1995-2005
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 static int log_level=0;
33 static int log_level_initialized=0;
34
35 /**
36  * creates an rfd. Either allocates a new one, in which case the priv 
37  * pointer is null, and will have to be filled in, or picks up one 
38  * from the freelist, in which case the priv is already allocated,
39  * and presumably everything that hangs from it as well 
40  */
41
42 RSFD rfd_create_base(RSET rs)
43 {
44     RSFD rnew=rs->free_list;
45
46     if (!log_level_initialized) 
47     {
48         log_level=yaz_log_module_level("rset");
49         log_level_initialized=1;
50     }
51
52     if (rnew) 
53     {
54         rs->free_list=rnew->next;
55         assert(rnew->rset==rs);
56         yaz_log(log_level,"rfd-create_base (fl): rfd=%p rs=%p fl=%p priv=%p", 
57                        rnew, rs, rs->free_list, rnew->priv); 
58     } else {
59         rnew=nmem_malloc(rs->nmem, sizeof(*rnew));
60         rnew->priv=NULL;
61         rnew->rset=rs;
62         yaz_log(log_level,"rfd_create_base (new): rfd=%p rs=%p fl=%p priv=%p", 
63                        rnew, rs, rs->free_list, rnew->priv); 
64     }
65     rnew->next=NULL; /* not part of any (free?) list */
66     return rnew;
67 }
68
69 /* puts an rfd into the freelist of the rset. Only when the rset gets */
70 /* deleted, will all the nmem disappear */
71 void rfd_delete_base(RSFD rfd) 
72 {
73     RSET rs=rfd->rset;
74     yaz_log(log_level,"rfd_delete_base: rfd=%p rs=%p priv=%p fl=%p",
75             rfd, rs, rfd->priv, rs->free_list); 
76     assert(NULL == rfd->next); 
77     rfd->next=rs->free_list;
78     rs->free_list=rfd;
79 }
80
81
82 RSET rset_create_base(const struct rset_control *sel, 
83                       NMEM nmem, const struct key_control *kcontrol,
84                       int scope, TERMID term)
85 {
86     RSET rnew;
87     NMEM M;
88     /* assert(nmem); */ /* can not yet be used, api/t4 fails */
89     if (nmem) 
90         M=nmem;
91     else
92         M=nmem_create();
93     rnew = (RSET) nmem_malloc(M,sizeof(*rnew));
94     yaz_log (log_level, "rs_create(%s) rs=%p (nm=%p)", sel->desc, rnew, nmem); 
95     rnew->nmem=M;
96     if (nmem)
97         rnew->my_nmem=0;
98     else 
99         rnew->my_nmem=1;
100     rnew->control = sel;
101     rnew->count = 1; /* refcount! */
102     rnew->priv = 0;
103     rnew->free_list=NULL;
104     rnew->keycontrol=kcontrol;
105     rnew->scope=scope;
106     rnew->term=term;
107     if (term)
108         term->rset=rnew;
109     return rnew;
110 }
111
112 void rset_delete (RSET rs)
113 {
114     (rs->count)--;
115     yaz_log(log_level,"rs_delete(%s), rs=%p, count=%d",
116             rs->control->desc, rs, rs->count); 
117     if (!rs->count)
118     {
119         (*rs->control->f_delete)(rs);
120         if (rs->my_nmem)
121             nmem_destroy(rs->nmem);
122     }
123 }
124
125 RSET rset_dup (RSET rs)
126 {
127     (rs->count)++;
128     yaz_log(log_level,"rs_dup(%s), rs=%p, count=%d",
129             rs->control->desc, rs, rs->count); 
130     return rs;
131 }
132
133 int rset_default_forward(RSFD rfd, void *buf, TERMID *term,
134                            const void *untilbuf)
135 {
136     int more=1;
137     int cmp=rfd->rset->scope;
138     if (log_level)
139     {
140         yaz_log (log_level, "rset_default_forward starting '%s' (ct=%p rfd=%p)",
141                     rfd->rset->control->desc, rfd->rset, rfd);
142         /* key_logdump(log_level, untilbuf); */
143     }
144     while ( (cmp>=rfd->rset->scope) && (more))
145     {
146         if (log_level)  /* time-critical, check first */
147             yaz_log(log_level,"rset_default_forward looping m=%d c=%d",more,cmp);
148         more=rset_read(rfd, buf, term);
149         if (more)
150             cmp=(rfd->rset->keycontrol->cmp)(untilbuf,buf);
151 /*        if (more)
152             key_logdump(log_level,buf); */
153     }
154     if (log_level)
155         yaz_log (log_level, "rset_default_forward exiting m=%d c=%d",more,cmp);
156
157     return more;
158 }
159
160 /** 
161  * rset_count uses rset_pos to get the total and returns that.
162  * This is ok for rsisamb/c/s, and for some other rsets, but in case of
163  * booleans etc it will give bad estimate, as nothing has been read
164  * from that rset
165  */
166 zint rset_count(RSET rs)
167 {
168     double cur,tot;
169     RSFD rfd=rset_open(rs,0);
170     rset_pos(rfd,&cur,&tot);
171     rset_close(rfd);
172     return (zint)(tot);
173 }
174
175
176 /** rset_get_no_terms is a getterms function for those that don't have any */
177 void rset_get_no_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
178 {
179     return;
180 }
181
182 /* rset_get_one_term gets that one term from an rset. Used by rsisamX */
183 void rset_get_one_term(RSET ct,TERMID *terms,int maxterms,int *curterm)
184 {
185     if (ct->term)
186     {
187         if (*curterm < maxterms)
188             terms[*curterm]=ct->term;
189         (*curterm)++;
190     }
191 }
192
193
194 TERMID rset_term_create (const char *name, int length, const char *flags,
195                                     int type, NMEM nmem)
196
197 {
198     TERMID t;
199     yaz_log (log_level, "term_create '%s' %d f=%s type=%d nmem=%p",
200             name, length, flags, type, nmem);
201     t= (TERMID) nmem_malloc (nmem, sizeof(*t));
202     if (!name)
203         t->name = NULL;
204     else if (length == -1)
205         t->name = nmem_strdup(nmem,name);
206     else
207     {
208         t->name = (char*) nmem_malloc(nmem,length+1);
209         memcpy (t->name, name, length);
210         t->name[length] = '\0';
211     }
212     if (!flags)
213         t->flags = NULL;
214     else
215         t->flags = nmem_strdup(nmem,flags);
216     t->type = type;
217     t->rankpriv=0;
218     t->rset=0;
219     return t;
220 }
221
222