Using a bit more of those nmems in rsets
[idzebra-moved-to-github.git] / rset / rset.c
1 /* $Id: rset.c,v 1.28 2004-08-24 15:00:16 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 RSET rset_create_base(const struct rset_control *sel, NMEM nmem)
33         /* FIXME - Add keysize and cmp function */
34         /* FIXME - Add a general key-func block for cmp, dump, etc */
35 {
36     RSET rnew;
37     NMEM M;
38     logf (LOG_DEBUG, "rs_create(%s)", sel->desc);
39     if (nmem) 
40         M=nmem;
41     else
42         M=nmem_create();
43     rnew = (RSET) nmem_malloc(M,sizeof(*rnew));
44     rnew->nmem=M;
45     if (nmem)
46         rnew->my_nmem=0;
47     else 
48         rnew->my_nmem=1;
49     rnew->control = sel;
50     rnew->count = 1;
51     rnew->priv = 0;
52     
53     return rnew;
54 }
55
56 void rset_delete (RSET rs)
57 {
58     (rs->count)--;
59     if (!rs->count)
60     {
61         (*rs->control->f_delete)(rs);
62         if (rs->my_nmem)
63             nmem_destroy(rs->nmem);
64     }
65 }
66
67 RSET rset_dup (RSET rs)
68 {
69     (rs->count)++;
70     return rs;
71 }
72
73 void rset_default_pos (RSFD rfd, double *current, double *total)
74 { /* This should never really be needed, but it is still used in */
75   /* those rsets that we don't really plan to use, like isam-s */
76     assert(rfd);
77     assert(current);
78     assert(total);
79     *current=-1; /* signal that pos is not implemented */
80     *total=-1;
81 } /* rset_default_pos */
82
83 int rset_default_forward(RSET ct, RSFD rfd, void *buf, 
84                            int (*cmpfunc)(const void *p1, const void *p2), 
85                            const void *untilbuf)
86 {
87     int more=1;
88     int cmp=2;
89     logf (LOG_DEBUG, "rset_default_forward starting '%s' (ct=%p rfd=%p)",
90                     ct->control->desc, ct,rfd);
91     /* key_logdump(LOG_DEBUG, untilbuf); */
92     while ( (cmp==2) && (more))
93     {
94         logf (LOG_DEBUG, "rset_default_forward looping m=%d c=%d",more,cmp);
95         more=rset_read(ct, rfd, buf);
96         if (more)
97             cmp=(*cmpfunc)(untilbuf,buf);
98 /*        if (more)
99             key_logdump(LOG_DEBUG,buf); */
100     }
101     logf (LOG_DEBUG, "rset_default_forward exiting m=%d c=%d",more,cmp);
102
103     return more;
104 }
105