1 /* $Id: rset.c,v 1.38 2004-10-22 11:33:29 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
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
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
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
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 */
38 RSFD rfd_create_base(RSET rs)
40 RSFD rnew=rs->free_list;
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); */
47 rnew=nmem_malloc(rs->nmem, sizeof(*rnew));
50 /* logf(LOG_DEBUG,"rfd_create_base (new): rfd=%p rs=%p fl=%p priv=%p",
51 rnew, rs, rs->free_list, rnew->priv); */
53 rnew->next=NULL; /* not part of any (free?) list */
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)
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;
70 RSET rset_create_base(const struct rset_control *sel,
71 NMEM nmem, const struct key_control *kcontrol,
72 int scope, TERMID term)
76 /* assert(nmem); */ /* can not yet be used, api/t4 fails */
81 rnew = (RSET) nmem_malloc(M,sizeof(*rnew));
82 /* logf (LOG_DEBUG, "rs_create(%s) rs=%p (nm=%p)", sel->desc, rnew, nmem); */
89 rnew->count = 1; /* refcount! */
92 rnew->keycontrol=kcontrol;
100 void rset_delete (RSET rs)
103 /* logf(LOG_DEBUG,"rs_delete(%s), rs=%p, count=%d",
104 rs->control->desc, rs, rs->count); */
107 (*rs->control->f_delete)(rs);
109 nmem_destroy(rs->nmem);
113 RSET rset_dup (RSET rs)
120 void rset_default_pos (RSFD rfd, double *current, double *total)
121 { /* This should never really be needed, but it is still used in */
122 /* those rsets that we don't really plan to use, like isam-s */
126 *current=-1; /* signal that pos is not implemented */
128 } /* rset_default_pos */
131 int rset_default_forward(RSFD rfd, void *buf, TERMID *term,
132 const void *untilbuf)
135 int cmp=rfd->rset->scope;
136 logf (LOG_DEBUG, "rset_default_forward starting '%s' (ct=%p rfd=%p)",
137 rfd->rset->control->desc, rfd->rset, rfd);
138 /* key_logdump(LOG_DEBUG, untilbuf); */
139 while ( (cmp>=rfd->rset->scope) && (more))
141 logf (LOG_DEBUG, "rset_default_forward looping m=%d c=%d",more,cmp);
142 more=rset_read(rfd, buf, term);
144 cmp=(rfd->rset->keycontrol->cmp)(untilbuf,buf);
146 key_logdump(LOG_DEBUG,buf); */
148 logf (LOG_DEBUG, "rset_default_forward exiting m=%d c=%d",more,cmp);
154 * rset_count uses rset_pos to get the total and returns that.
155 * This is ok for rsisamb, and for some other rsets, but in case of
156 * booleans etc it will give bad estimate, as nothing has been read
159 zint rset_count(RSET rs)
162 RSFD rfd=rset_open(rs,0);
163 rset_pos(rfd,&cur,&tot);
169 /** rset_get_no_terms is a getterms function for those that don't have any */
170 void rset_get_no_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
175 /* rset_get_one_term gets that one term from an rset. Used by rsisamX */
176 void rset_get_one_term(RSET ct,TERMID *terms,int maxterms,int *curterm)
180 if (*curterm < maxterms)
181 terms[*curterm]=ct->term;
187 TERMID rset_term_create (const char *name, int length, const char *flags,
192 logf (LOG_DEBUG, "term_create '%s' %d f=%s type=%d nmem=%p",
193 name, length, flags, type, nmem);
194 t= (TERMID) nmem_malloc (nmem, sizeof(*t));
197 else if (length == -1)
198 t->name = nmem_strdup(nmem,name);
201 t->name = (char*) nmem_malloc(nmem,length+1);
202 memcpy (t->name, name, length);
203 t->name[length] = '\0';
208 t->flags = nmem_strdup(nmem,flags);