1 /* $Id: rset.c,v 1.43 2005-01-17 01:21:44 adam Exp $
2 Copyright (C) 1995-2005
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
30 static int log_level = 0;
31 static int log_level_initialized = 0;
33 /** \fn rfd_create_base(RSET rs)
35 * creates an rfd. Either allocates a new one, in which case the priv
36 * pointer is null, and will have to be filled in, or picks up one
37 * from the freelist, in which case the priv is already allocated,
38 * and presumably everything that hangs from it as well
40 RSFD rfd_create_base(RSET rs)
42 RSFD rnew = rs->free_list;
46 rs->free_list = rnew->next;
47 assert(rnew->rset==rs);
48 yaz_log(log_level, "rfd_create_base (fl): rfd=%p rs=%p fl=%p priv=%p",
49 rnew, rs, rs->free_list, rnew->priv);
53 rnew = nmem_malloc(rs->nmem, sizeof(*rnew));
56 yaz_log(log_level, "rfd_create_base (new): rfd=%p rs=%p fl=%p priv=%p",
57 rnew, rs, rs->free_list, rnew->priv);
59 rnew->next = rs->use_list;
64 /** \fn rfd_delete_base
66 * puts an rfd into the freelist of the rset. Only when the rset gets
67 * deleted, will all the nmem disappear */
68 void rfd_delete_base(RSFD rfd)
72 yaz_log(log_level, "rfd_delete_base: rfd=%p rs=%p priv=%p fl=%p",
73 rfd, rs, rfd->priv, rs->free_list);
74 for (pfd = &rs->use_list; *pfd; pfd = &(*pfd)->next)
78 rfd->next = rs->free_list;
82 yaz_log(YLOG_WARN, "rset_close handle not found. type=%s",
86 RSET rset_create_base(const struct rset_control *sel,
87 NMEM nmem, const struct key_control *kcontrol,
88 int scope, TERMID term)
92 /* assert(nmem); */ /* can not yet be used, api/t4 fails */
93 if (!log_level_initialized)
95 log_level = yaz_log_module_level("rset");
96 log_level_initialized = 1;
103 rnew = (RSET) nmem_malloc(M, sizeof(*rnew));
104 yaz_log(log_level, "rs_create(%s) rs=%p (nm=%p)", sel->desc, rnew, nmem);
111 rnew->count = 1; /* refcount! */
113 rnew->free_list = NULL;
114 rnew->use_list = NULL;
115 rnew->keycontrol = kcontrol;
123 void rset_delete (RSET rs)
126 yaz_log(log_level, "rs_delete(%s), rs=%p, count=%d",
127 rs->control->desc, rs, rs->count);
131 yaz_log(YLOG_WARN, "rs_delete(%s) still has RFDs in use",
133 (*rs->control->f_delete)(rs);
135 nmem_destroy(rs->nmem);
139 int rfd_is_last(RSFD rfd)
141 if (rfd->rset->use_list == rfd && rfd->next == 0)
146 RSET rset_dup (RSET rs)
149 yaz_log(log_level, "rs_dup(%s), rs=%p, count=%d",
150 rs->control->desc, rs, rs->count);
154 int rset_default_forward(RSFD rfd, void *buf, TERMID *term,
155 const void *untilbuf)
158 int cmp = rfd->rset->scope;
161 yaz_log (log_level, "rset_default_forward starting '%s' (ct=%p rfd=%p)",
162 rfd->rset->control->desc, rfd->rset, rfd);
163 /* key_logdump(log_level, untilbuf); */
165 while (cmp>=rfd->rset->scope && more)
167 if (log_level) /* time-critical, check first */
168 yaz_log(log_level, "rset_default_forward looping m=%d c=%d",
170 more = rset_read(rfd, buf, term);
172 cmp = (rfd->rset->keycontrol->cmp)(untilbuf, buf);
175 yaz_log (log_level, "rset_default_forward exiting m=%d c=%d",
182 * rset_count uses rset_pos to get the total and returns that.
183 * This is ok for rsisamb/c/s, and for some other rsets, but in case of
184 * booleans etc it will give bad estimate, as nothing has been read
187 zint rset_count(RSET rs)
190 RSFD rfd = rset_open(rs, 0);
191 rset_pos(rfd, &cur, &tot);
197 /** rset_get_no_terms is a getterms function for those that don't have any */
198 void rset_get_no_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
203 /* rset_get_one_term gets that one term from an rset. Used by rsisamX */
204 void rset_get_one_term(RSET ct,TERMID *terms,int maxterms,int *curterm)
208 if (*curterm < maxterms)
209 terms[*curterm] = ct->term;
215 TERMID rset_term_create (const char *name, int length, const char *flags,
220 yaz_log (log_level, "term_create '%s' %d f=%s type=%d nmem=%p",
221 name, length, flags, type, nmem);
222 t= (TERMID) nmem_malloc(nmem, sizeof(*t));
225 else if (length == -1)
226 t->name = nmem_strdup(nmem, name);
229 t->name = (char*) nmem_malloc(nmem, length+1);
230 memcpy (t->name, name, length);
231 t->name[length] = '\0';
236 t->flags = nmem_strdup(nmem, flags);