1 /* $Id: rset.c,v 1.46 2005-05-24 11:35:43 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
25 #include <idzebra/util.h>
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));
54 rnew->counted_buf = nmem_malloc(rs->nmem, rs->keycontrol->key_size);
57 yaz_log(log_level, "rfd_create_base (new): rfd=%p rs=%p fl=%p priv=%p",
58 rnew, rs, rs->free_list, rnew->priv);
60 rnew->next = rs->use_list;
62 rnew->counted_items = 0;
66 /** \fn rfd_delete_base
68 * puts an rfd into the freelist of the rset. Only when the rset gets
69 * deleted, will all the nmem disappear */
70 void rset_close(RSFD rfd)
75 if (rs->hits_count == 0)
79 while(rfd->counted_items < rs->hits_limit
80 && rset_default_read(rfd, buf, &termid))
83 rs->hits_count = rfd->counted_items;
85 if (rs->hits_count >= rs->hits_limit)
89 rset_pos(rfd, &cur, &tot);
92 double ratio = cur/tot;
93 est = (zint)(0.5 + rs->hits_count / ratio);
94 yaz_log(log_level, "Estimating hits (%s) "
96 "; %0.1f->" ZINT_FORMAT,
100 i = 0; /* round to significant digits */
101 while (est > rs->hits_round) {
107 rs->hits_count = est;
111 yaz_log(log_level, "rset_close p=%p count=" ZINT_FORMAT, rs,
114 (*rs->control->f_close)(rfd);
116 yaz_log(log_level, "rfd_delete_base: rfd=%p rs=%p priv=%p fl=%p",
117 rfd, rs, rfd->priv, rs->free_list);
118 for (pfd = &rs->use_list; *pfd; pfd = &(*pfd)->next)
122 rfd->next = rs->free_list;
126 yaz_log(YLOG_WARN, "rset_close handle not found. type=%s",
130 RSET rset_create_base(const struct rset_control *sel,
131 NMEM nmem, struct rset_key_control *kcontrol,
132 int scope, TERMID term,
133 int no_children, RSET *children)
137 assert(nmem); /* can not yet be used, api/t4 fails */
138 if (!log_level_initialized)
140 log_level = yaz_log_module_level("rset");
141 log_level_initialized = 1;
148 rset = (RSET) nmem_malloc(M, sizeof(*rset));
149 yaz_log(log_level, "rs_create(%s) rs=%p (nm=%p)", sel->desc, rset, nmem);
158 rset->free_list = NULL;
159 rset->use_list = NULL;
160 rset->hits_count = 0;
161 rset->hits_limit = 1000;
162 rset->hits_round = 1000;
163 rset->keycontrol = kcontrol;
164 (*kcontrol->inc)(kcontrol);
170 rset->no_children = no_children;
174 rset->children = (RSET*)
175 nmem_malloc(rset->nmem, no_children*sizeof(RSET *));
176 memcpy(rset->children, children, no_children*sizeof(RSET *));
181 void rset_delete(RSET rs)
184 yaz_log(log_level, "rs_delete(%s), rs=%p, refcount=%d",
185 rs->control->desc, rs, rs->refcount);
190 yaz_log(YLOG_WARN, "rs_delete(%s) still has RFDs in use",
192 for (i = 0; i<rs->no_children; i++)
193 rset_delete(rs->children[i]);
194 (*rs->control->f_delete)(rs);
195 (*rs->keycontrol->dec)(rs->keycontrol);
197 nmem_destroy(rs->nmem);
201 int rfd_is_last(RSFD rfd)
203 if (rfd->rset->use_list == rfd && rfd->next == 0)
208 RSET rset_dup (RSET rs)
211 yaz_log(log_level, "rs_dup(%s), rs=%p, refcount=%d",
212 rs->control->desc, rs, rs->refcount);
213 (*rs->keycontrol->inc)(rs->keycontrol);
218 * rset_count uses rset_pos to get the total and returns that.
219 * This is ok for rsisamb/c/s, and for some other rsets, but in case of
220 * booleans etc it will give bad estimate, as nothing has been read
223 zint rset_count(RSET rs)
226 RSFD rfd = rset_open(rs, 0);
227 rset_pos(rfd, &cur, &tot);
232 /** rset_get_no_terms is a getterms function for those that don't have any */
233 void rset_get_no_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
238 /* rset_get_one_term gets that one term from an rset. Used by rsisamX */
239 void rset_get_one_term(RSET ct, TERMID *terms, int maxterms, int *curterm)
243 if (*curterm < maxterms)
244 terms[*curterm] = ct->term;
250 TERMID rset_term_create(const char *name, int length, const char *flags,
255 yaz_log (log_level, "term_create '%s' %d f=%s type=%d nmem=%p",
256 name, length, flags, type, nmem);
257 t= (TERMID) nmem_malloc(nmem, sizeof(*t));
260 else if (length == -1)
261 t->name = nmem_strdup(nmem, name);
264 t->name = (char*) nmem_malloc(nmem, length+1);
265 memcpy (t->name, name, length);
266 t->name[length] = '\0';
271 t->flags = nmem_strdup(nmem, flags);
278 int rset_default_read(RSFD rfd, void *buf, TERMID *term)
280 RSET rset = rfd->rset;
281 int rc = (*rset->control->f_read)(rfd, buf, term);
284 if (rfd->counted_items == 0 ||
285 (rset->keycontrol->cmp)(buf, rfd->counted_buf) >= rset->scope)
287 memcpy(rfd->counted_buf, buf, rset->keycontrol->key_size);
288 rfd->counted_items++;
294 int rset_default_forward(RSFD rfd, void *buf, TERMID *term,
295 const void *untilbuf)
297 RSET rset = rfd->rset;
300 if (rset->control->f_forward &&
301 rfd->counted_items >= rset->hits_limit)
303 assert (rset->control->f_forward != rset_default_forward);
304 return rset->control->f_forward(rfd, buf, term, untilbuf);
307 while ((more = rset_read(rfd, buf, term)) > 0)
309 if ((rfd->rset->keycontrol->cmp)(untilbuf, buf) <= 1)
313 yaz_log (log_level, "rset_default_forward exiting m=%d c=%d",
319 void rset_visit(RSET rset, int level)
322 yaz_log(YLOG_LOG, "%*s%c " ZINT_FORMAT, level, "",
323 rset->hits_approx ? '~' : '=',
325 for (i = 0; i<rset->no_children; i++)
326 rset_visit(rset->children[i], level+1);