Cleaned up the creation of rsets, added nmem
[idzebra-moved-to-github.git] / rset / rsisamc.c
1 /* $Id: rsisamc.c,v 1.21 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
26 #include <stdio.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <zebrautl.h>
30 #include <rsisamc.h>
31
32 static RSFD r_open (RSET ct, int flag);
33 static void r_close (RSFD rfd);
34 static void r_delete (RSET ct);
35 static void r_rewind (RSFD rfd);
36 static int r_read (RSFD rfd, void *buf);
37 static int r_write (RSFD rfd, const void *buf);
38
39 static const struct rset_control control = 
40 {
41     "isamc",
42     r_open,
43     r_close,
44     r_delete,
45     r_rewind,
46     rset_default_forward,
47     rset_default_pos,
48     r_read,
49     r_write,
50 };
51
52 const struct rset_control *rset_kind_isamc = &control;
53
54 struct rset_pp_info {
55     ISAMC_PP pt;
56     struct rset_pp_info *next;
57     struct rset_isamc_info *info;
58     void *buf;
59 };
60
61 struct rset_isamc_info {
62     ISAMC   is;
63     ISAMC_P pos;
64     int key_size;
65     int (*cmp)(const void *p1, const void *p2);
66     struct rset_pp_info *ispt_list;
67 };
68
69 RSET rsisamc_create( NMEM nmem, int key_size, 
70             int (*cmp)(const void *p1, const void *p2),
71             ISAMC is, ISAMC_P pos)
72 {
73     RSET rnew=rset_create_base(&control, nmem);
74     struct rset_isamc_info *info;
75     info = (struct rset_isamc_info *) nmem_malloc(rnew->nmem,sizeof(*info));
76     info->key_size = key_size;
77     info->cmp = cmp;
78     info->ispt_list = NULL;
79     info->is=is;
80     info->pos=pos;
81     rnew->priv=info;
82     return rnew;
83 }
84
85 static void r_delete (RSET ct)
86 {
87     struct rset_isamc_info *info = (struct rset_isamc_info *) ct->priv;
88
89     logf (LOG_DEBUG, "rsisamc_delete");
90     assert (info->ispt_list == NULL);
91 }
92
93 /*
94 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
95 {
96     rset_isamc_parms *pt = (rset_isamc_parms *) parms;
97     struct rset_isamc_info *info;
98
99     info = (struct rset_isamc_info *) xmalloc (sizeof(*info));
100     info->is = pt->is;
101     info->pos = pt->pos;
102     info->key_size = pt->key_size;
103     info->cmp = pt->cmp;
104     info->ispt_list = NULL;
105     return info;
106 }
107 */
108
109 RSFD r_open (RSET ct, int flag)
110 {
111     struct rset_isamc_info *info = (struct rset_isamc_info *) ct->priv;
112     struct rset_pp_info *ptinfo;
113
114     logf (LOG_DEBUG, "risamc_open");
115     if (flag & RSETF_WRITE)
116     {
117         logf (LOG_FATAL, "ISAMC set type is read-only");
118         return NULL;
119     }
120     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
121     ptinfo->next = info->ispt_list;
122     info->ispt_list = ptinfo;
123     ptinfo->pt = isc_pp_open (info->is, info->pos);
124     ptinfo->info = info;
125     ptinfo->buf = xmalloc (info->key_size);
126     return ptinfo;
127 }
128
129 static void r_close (RSFD rfd)
130 {
131     struct rset_isamc_info *info = ((struct rset_pp_info*) rfd)->info;
132     struct rset_pp_info **ptinfop;
133
134     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
135         if (*ptinfop == rfd)
136         {
137             xfree ((*ptinfop)->buf);
138             isc_pp_close ((*ptinfop)->pt);
139             *ptinfop = (*ptinfop)->next;
140             xfree (rfd);
141             return;
142         }
143     logf (LOG_FATAL, "r_close but no rfd match!");
144     assert (0);
145 }
146
147
148 static void r_rewind (RSFD rfd)
149 {   
150     logf (LOG_DEBUG, "rsisamc_rewind");
151     abort ();
152 }
153
154 static int r_read (RSFD rfd, void *buf)
155 {
156     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
157     int r;
158     r = isc_pp_read(pinfo->pt, buf);
159     return r;
160 }
161
162 static int r_write (RSFD rfd, const void *buf)
163 {
164     logf (LOG_FATAL, "ISAMC set type is read-only");
165     return -1;
166 }