68a41ee8b4016145e459f79a5e49efe48ee00844
[idzebra-moved-to-github.git] / rset / rset.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <idzebra/util.h>
23 #include <assert.h>
24 #include <yaz/nmem.h>
25 #include <rset.h>
26
27 static int log_level = 0;
28 static int log_level_initialized = 0;
29
30
31 /**
32    \brief Common constuctor for RFDs
33    \param rs Result set handle.
34    
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 
39 */
40 RSFD rfd_create_base(RSET rs)
41 {
42     RSFD rnew = rs->free_list;
43
44     if (rnew) 
45     {
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); 
50     } 
51     else
52     {
53         rnew = nmem_malloc(rs->nmem, sizeof(*rnew));
54         rnew->counted_buf = nmem_malloc(rs->nmem, rs->keycontrol->key_size);
55         rnew->priv = 0;
56         rnew->rset = rs;
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); 
59     }
60     rnew->next = rs->use_list;
61     rs->use_list = rnew;
62     rnew->counted_items = 0;
63     return rnew;
64 }
65
66 static void rset_close_int(RSET rs, RSFD rfd)
67 {
68     RSFD *pfd;
69     (*rs->control->f_close)(rfd);
70     
71     yaz_log(log_level, "rfd_delete_base: rfd=%p rs=%p priv=%p fl=%p",
72             rfd, rs, rfd->priv, rs->free_list); 
73     for (pfd = &rs->use_list; *pfd; pfd = &(*pfd)->next)
74         if (*pfd == rfd)
75         {
76             *pfd = (*pfd)->next;
77             rfd->next = rs->free_list;
78             rs->free_list = rfd;
79             return;
80         }
81     yaz_log(YLOG_WARN, "rset_close handle not found. type=%s",
82             rs->control->desc);
83 }
84
85 void rset_set_hits_limit(RSET rs, zint l)
86 {
87     yaz_log(log_level, "rset_set_hits_limit %p l=" ZINT_FORMAT, rs, l);
88     rs->hits_limit = l;
89 }
90
91 /**
92    \brief Closes a result set RFD handle
93    \param rfd the RFD handle.
94 */
95 void rset_close(RSFD rfd)
96 {
97     RSET rs = rfd->rset;
98
99     if (rs->hits_count == 0)
100     {
101         TERMID termid;
102         char buf[100];
103
104         while (rfd->counted_items <= rs->hits_limit
105               && rset_default_read(rfd, buf, &termid))
106             ;
107         
108         rs->hits_count = rfd->counted_items;
109         yaz_log(log_level, "rset_close rset=%p hits_count=" ZINT_FORMAT
110                 " hits_limit=" ZINT_FORMAT,
111                 rs, rs->hits_count, rs->hits_limit);
112         rs->hits_approx = 0;
113         if (rs->hits_count > rs->hits_limit && rs->hits_limit > 0)
114         {
115             double cur, tot;
116             zint est;
117             rset_pos(rfd, &cur, &tot); 
118             if (tot > 0) {
119                 int i;
120                 double ratio = cur/tot;
121                 est = (zint)(0.5 + rs->hits_count / ratio);
122                 yaz_log(log_level, "Estimating hits (%s) "
123                         "%0.1f->" ZINT_FORMAT
124                         "; %0.1f->" ZINT_FORMAT,
125                         rs->control->desc,
126                         cur, rs->hits_count,
127                         tot, est);
128                 i = 0; /* round to significant digits */
129                 while (est > rs->hits_round) {
130                     est /= 10;
131                     i++;
132                 }
133                 while (i--)
134                     est *= 10;
135                 rs->hits_count = est;
136                 rs->hits_approx = 1;
137             }
138         }
139         yaz_log(log_level, "rset_close(%s) p=%p count=" ZINT_FORMAT, 
140                 rs->control->desc, rs,
141                 rs->hits_count);
142     }
143     rset_close_int(rs, rfd);
144 }
145
146 /**
147    \brief Common constuctor for RSETs
148    \param sel The interface control handle
149    \param nmem The memory handle for it.
150    \param kcontrol Key control info (decode, encode, comparison etc)
151    \param scope scope for set
152    \param term Information about term for it (NULL for none).
153    \param no_children number of child rsets (0 for none)
154    \param children child rsets (NULL for none).
155    
156    Creates an rfd. Either allocates a new one, in which case the priv 
157    pointer is null, and will have to be filled in, or picks up one 
158    from the freelist, in which case the priv is already allocated,
159    and presumably everything that hangs from it as well 
160 */
161 RSET rset_create_base(const struct rset_control *sel, 
162                       NMEM nmem, struct rset_key_control *kcontrol,
163                       int scope, TERMID term,
164                       int no_children, RSET *children)
165 {
166     RSET rset;
167     assert(nmem);
168     if (!log_level_initialized) 
169     {
170         log_level = yaz_log_module_level("rset");
171         log_level_initialized = 1;
172     }
173
174     rset = (RSET) nmem_malloc(nmem, sizeof(*rset));
175     yaz_log(log_level, "rs_create(%s) rs=%p (nm=%p)", sel->desc, rset, nmem); 
176     yaz_log(log_level, " ref_id=%s", 
177             (term && term->ref_id ? term->ref_id : "null"));
178     rset->nmem = nmem;
179     rset->control = sel;
180     rset->refcount = 1;
181     rset->priv = 0;
182     rset->free_list = NULL;
183     rset->use_list = NULL;
184     rset->hits_count = 0;
185     rset->hits_limit = 0;
186     rset->hits_round = 1000;
187     rset->keycontrol = kcontrol;
188
189     (*kcontrol->inc)(kcontrol);
190     rset->scope = scope;
191     rset->term = term;
192     if (term)
193     {
194         term->rset = rset;
195         rset->hits_limit = term->hits_limit;
196     }
197     rset->no_children = no_children;
198     rset->children = 0;
199     if (no_children)
200     {
201         rset->children = (RSET*)
202             nmem_malloc(rset->nmem, no_children*sizeof(RSET *));
203         memcpy(rset->children, children, no_children*sizeof(RSET *));
204     }
205     return rset;
206 }
207
208 /**
209    \brief Destructor RSETs
210    \param rs Handle for result set.
211    
212    Destroys a result set and all its children.
213    The f_delete method of control is called for the result set.
214 */
215 void rset_delete(RSET rs)
216 {
217     (rs->refcount)--;
218     yaz_log(log_level, "rs_delete(%s), rs=%p, refcount=%d",
219             rs->control->desc, rs, rs->refcount); 
220     if (!rs->refcount)
221     {
222         int i;
223         if (rs->use_list)
224             yaz_log(YLOG_WARN, "rs_delete(%s) still has RFDs in use",
225                     rs->control->desc);
226         for (i = 0; i<rs->no_children; i++)
227             rset_delete(rs->children[i]);
228         (*rs->control->f_delete)(rs);
229         (*rs->keycontrol->dec)(rs->keycontrol);
230     }
231 }
232
233 /**
234    \brief Test for last use of RFD
235    \param rfd RFD handle.
236    
237    Returns 1 if this RFD is the last reference to it; 0 otherwise.
238 */
239 int rfd_is_last(RSFD rfd)
240 {
241     if (rfd->rset->use_list == rfd && rfd->next == 0)
242         return 1;
243     return 0;
244 }
245
246 /**
247    \brief Duplicate an RSET
248    \param rs Handle for result set.
249    
250    Duplicates a result set by incrementing the reference count to it.
251 */
252 RSET rset_dup (RSET rs)
253 {
254     (rs->refcount)++;
255     yaz_log(log_level, "rs_dup(%s), rs=%p, refcount=%d",
256             rs->control->desc, rs, rs->refcount); 
257     return rs;
258 }
259
260 /** 
261     \brief Estimates hit count for result set.
262     \param rs Result Set.
263
264     rset_count uses rset_pos to get the total and returns that.
265     This is ok for rsisamb/c/s, and for some other rsets, but in case of
266     booleans etc it will give bad estimate, as nothing has been read
267     from that rset
268 */
269 zint rset_count(RSET rs)
270 {
271     double cur, tot;
272     RSFD rfd = rset_open(rs, 0);
273     rset_pos(rfd, &cur, &tot);
274     rset_close_int(rs, rfd);
275     return (zint) tot;
276 }
277
278 /**
279    \brief is a getterms function for those that don't have any
280    \param ct result set handle
281    \param terms array of terms (0..maxterms-1)
282    \param maxterms length of terms array
283    \param curterm current size of terms array
284
285    If there is a term associated with rset the term is appeneded; otherwise
286    the terms array is untouched but curterm is incremented anyway.
287 */
288 void rset_get_one_term(RSET ct, TERMID *terms, int maxterms, int *curterm)
289 {
290     if (ct->term)
291     {
292         if (*curterm < maxterms)
293             terms[*curterm] = ct->term;
294         (*curterm)++;
295     }
296 }
297
298 struct ord_list *ord_list_create(NMEM nmem)
299 {
300     return 0;
301 }
302
303 struct ord_list *ord_list_append(NMEM nmem, struct ord_list *list,
304                                         int ord)
305 {
306     struct ord_list *n = nmem_malloc(nmem, sizeof(*n));
307     n->ord = ord;
308     n->next = list;
309     return n;
310 }
311
312 struct ord_list *ord_list_dup(NMEM nmem, struct ord_list *list)
313 {
314     struct ord_list *n = ord_list_create(nmem);
315     for (; list; list = list->next)
316         n = ord_list_append(nmem, n, list->ord);
317     return n;
318 }
319
320 void ord_list_print(struct ord_list *list)
321 {
322     for (; list; list = list->next)
323         yaz_log(YLOG_LOG, "ord_list %d", list->ord);
324 }
325 /**
326    \brief Creates a TERMID entry.
327    \param name Term/Name buffer with given length
328    \param length of term
329    \param flags for term
330    \param type Term Type, Z_Term_general, Z_Term_characterString,..
331    \param nmem memory for term.
332    \param ol ord list
333    \param reg_type register type
334    \param hits_limit limit before counting stops and gets approximate
335    \param ref_id supplied ID for term that can be used to identify this
336 */
337 TERMID rset_term_create(const char *name, int length, const char *flags,
338                         int type, NMEM nmem, struct ord_list *ol,
339                         int reg_type,
340                         zint hits_limit, const char *ref_id)
341
342 {
343     TERMID t;
344     yaz_log (log_level, "term_create '%s' %d f=%s type=%d nmem=%p",
345             name, length, flags, type, nmem);
346     t= (TERMID) nmem_malloc(nmem, sizeof(*t));
347     if (!name)
348         t->name = NULL;
349     else if (length == -1)
350         t->name = nmem_strdup(nmem, name);
351     else
352         t->name = nmem_strdupn(nmem, name, length);
353     if (!ref_id)
354         t->ref_id = 0;
355     else
356         t->ref_id = nmem_strdup(nmem, ref_id);
357     if (!flags)
358         t->flags = NULL;
359     else
360         t->flags = nmem_strdup(nmem, flags);
361     t->hits_limit = hits_limit;
362     t->type = type;
363     t->reg_type = reg_type;
364     t->rankpriv = 0;
365     t->rset = 0;
366     t->ol = ord_list_dup(nmem, ol);
367     return t;
368 }
369
370 int rset_default_read(RSFD rfd, void *buf, TERMID *term)
371 {
372     RSET rset = rfd->rset;
373     int rc = (*rset->control->f_read)(rfd, buf, term);
374     if (rc > 0)
375     {
376         int got_scope;
377         if (rfd->counted_items == 0)
378             got_scope = rset->scope+1;
379         else
380             got_scope = rset->keycontrol->cmp(buf, rfd->counted_buf);
381        
382 #if 0
383         key_logdump_txt(YLOG_LOG, buf, "rset_default_read");
384         yaz_log(YLOG_LOG, "rset_scope=%d got_scope=%d", rset->scope, got_scope);
385 #endif
386         if (got_scope > rset->scope)
387         {
388             memcpy(rfd->counted_buf, buf, rset->keycontrol->key_size);
389             rfd->counted_items++;
390         }
391     }
392     return rc;
393 }
394
395 int rset_default_forward(RSFD rfd, void *buf, TERMID *term,
396                          const void *untilbuf)
397 {
398     RSET rset = rfd->rset;
399     int more;
400
401     if (rset->control->f_forward &&
402         rfd->counted_items >= rset->hits_limit)
403     {
404         assert (rset->control->f_forward != rset_default_forward);
405         return rset->control->f_forward(rfd, buf, term, untilbuf);
406     }
407     
408     while ((more = rset_read(rfd, buf, term)) > 0)
409     {
410         if ((rfd->rset->keycontrol->cmp)(untilbuf, buf) < rset->scope)
411             break;
412     }
413     if (log_level)
414         yaz_log(log_level, "rset_default_forward exiting rfd=%p scope=%d m=%d c=%d",
415                 rfd, rset->scope, more, rset->scope);
416     
417     return more;
418 }
419
420 void rset_visit(RSET rset, int level)
421 {
422     int i;
423     yaz_log(YLOG_LOG, "%*s%c " ZINT_FORMAT, level, "",
424             rset->hits_approx ? '~' : '=',
425             rset->hits_count);
426     for (i = 0; i<rset->no_children; i++)
427         rset_visit(rset->children[i], level+1);
428 }
429
430 /*
431  * Local variables:
432  * c-basic-offset: 4
433  * c-file-style: "Stroustrup"
434  * indent-tabs-mode: nil
435  * End:
436  * vim: shiftwidth=4 tabstop=8 expandtab
437  */
438