Cleaned up the creation of rsets, added nmem
[idzebra-moved-to-github.git] / rset / rset.c
1 /* $Id: rset.c,v 1.27 2004-08-24 14:25: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
38     logf (LOG_DEBUG, "rs_create(%s)", sel->desc);
39     rnew = (RSET) xmalloc(sizeof(*rnew));
40     rnew->control = sel;
41     rnew->count = 1;
42     rnew->priv = 0;
43     rnew->nmem=nmem;
44     if (nmem)
45         rnew->my_nmem=0;
46     else {
47         rnew->nmem=nmem_create();
48         rnew->my_nmem=1;
49     }
50     return rnew;
51 }
52
53 void rset_delete (RSET rs)
54 {
55     (rs->count)--;
56     if (!rs->count)
57     {
58         (*rs->control->f_delete)(rs);
59         if (rs->my_nmem)
60             nmem_destroy(rs->nmem);
61         xfree(rs);
62     }
63 }
64
65 RSET rset_dup (RSET rs)
66 {
67     (rs->count)++;
68     return rs;
69 }
70
71 void rset_default_pos (RSFD rfd, double *current, double *total)
72 { /* This should never really be needed, but it is still used in */
73   /* those rsets that we don't really plan to use, like isam-s */
74     assert(rfd);
75     assert(current);
76     assert(total);
77     *current=-1; /* signal that pos is not implemented */
78     *total=-1;
79 } /* rset_default_pos */
80
81 int rset_default_forward(RSET ct, RSFD rfd, void *buf, 
82                            int (*cmpfunc)(const void *p1, const void *p2), 
83                            const void *untilbuf)
84 {
85     int more=1;
86     int cmp=2;
87     logf (LOG_DEBUG, "rset_default_forward starting '%s' (ct=%p rfd=%p)",
88                     ct->control->desc, ct,rfd);
89     /* key_logdump(LOG_DEBUG, untilbuf); */
90     while ( (cmp==2) && (more))
91     {
92         logf (LOG_DEBUG, "rset_default_forward looping m=%d c=%d",more,cmp);
93         more=rset_read(ct, rfd, buf);
94         if (more)
95             cmp=(*cmpfunc)(untilbuf,buf);
96 /*        if (more)
97             key_logdump(LOG_DEBUG,buf); */
98     }
99     logf (LOG_DEBUG, "rset_default_forward exiting m=%d c=%d",more,cmp);
100
101     return more;
102 }
103