Cleaned up the creation of rsets, added nmem
[idzebra-moved-to-github.git] / rset / rsisams.c
1 /* $Id: rsisams.c,v 1.10 2004-08-24 14:25:16 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 <assert.h>
27 #include <zebrautl.h>
28 #include <rsisams.h>
29
30 static RSFD r_open (RSET ct, int flag);
31 static void r_close (RSFD rfd);
32 static void r_delete (RSET ct);
33 static void r_rewind (RSFD rfd);
34 static int r_read (RSFD rfd, void *buf);
35 static int r_write (RSFD rfd, const void *buf);
36
37 static const struct rset_control control = 
38 {
39     "isams",
40     r_open,
41     r_close,
42     r_delete,
43     r_rewind,
44     rset_default_forward,
45     rset_default_pos,
46     r_read,
47     r_write,
48 };
49
50 const struct rset_control *rset_kind_isams = &control;
51
52 struct rset_pp_info {
53     ISAMS_PP pt;
54     struct rset_pp_info *next;
55     struct rset_isams_info *info;
56 };
57
58 struct rset_isams_info {
59     ISAMS   is;
60     ISAMS_P pos;
61     struct rset_pp_info *ispt_list;
62 };
63
64
65 RSET rsisams_create( NMEM nmem, int key_size, 
66             int (*cmp)(const void *p1, const void *p2),
67             ISAMS is, ISAMS_P pos)
68 {
69     RSET rnew=rset_create_base(&control, nmem);
70     struct rset_isams_info *info;
71     info = (struct rset_isams_info *) nmem_malloc(rnew->nmem,sizeof(*info));
72     assert(key_size); /* FIXME - these belong to the general rset */
73     assert(cmp);
74     info->is=is;
75     info->pos=pos;
76     info->ispt_list = NULL;
77     rnew->priv=info;
78     return rnew;
79 }
80
81 static void r_delete (RSET ct)
82 {
83     struct rset_isams_info *info = (struct rset_isams_info *) ct->priv;
84
85     logf (LOG_DEBUG, "rsisams_delete");
86     assert (info->ispt_list == NULL);
87     /* xfree (info); */
88 }
89
90 #if 0
91 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
92 {
93     rset_isams_parms *pt = (struct rset_isams_parms *) parms;
94     struct rset_isams_info *info;
95
96     info = (struct rset_isams_info *) xmalloc (sizeof(*info));
97     info->is = pt->is;
98     info->pos = pt->pos;
99     info->ispt_list = NULL;
100     return info;
101 }
102 #endif
103
104 RSFD r_open (RSET ct, int flag)
105 {
106     struct rset_isams_info *info = (struct rset_isams_info *) ct->priv;
107     struct rset_pp_info *ptinfo;
108
109     logf (LOG_DEBUG, "risams_open");
110     if (flag & RSETF_WRITE)
111     {
112         logf (LOG_FATAL, "ISAMS set type is read-only");
113         return NULL;
114     }
115     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
116     ptinfo->next = info->ispt_list;
117     info->ispt_list = ptinfo;
118     ptinfo->pt = isams_pp_open (info->is, info->pos);
119     ptinfo->info = info;
120     return ptinfo;
121 }
122
123 static void r_close (RSFD rfd)
124 {
125     struct rset_isams_info *info = ((struct rset_pp_info*) rfd)->info;
126     struct rset_pp_info **ptinfop;
127
128     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
129         if (*ptinfop == rfd)
130         {
131             isams_pp_close ((*ptinfop)->pt);
132             *ptinfop = (*ptinfop)->next;
133             xfree (rfd);
134             return;
135         }
136     logf (LOG_FATAL, "r_close but no rfd match!");
137     assert (0);
138 }
139
140 static void r_rewind (RSFD rfd)
141 {   
142     logf (LOG_DEBUG, "rsisams_rewind");
143     abort ();
144 }
145
146
147 static int r_read (RSFD rfd, void *buf)
148 {
149     return isams_pp_read( ((struct rset_pp_info*) rfd)->pt, buf);
150 }
151
152 static int r_write (RSFD rfd, const void *buf)
153 {
154     logf (LOG_FATAL, "ISAMS set type is read-only");
155     return -1;
156 }