Fixed a crash with isam-c and multior (incremented a non-existing countp)
[idzebra-moved-to-github.git] / rset / rsisamc.c
1 /* $Id: rsisamc.c,v 1.20 2004-08-24 08:52:30 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 void *r_create(RSET ct, const struct rset_control *sel, void *parms);
33 static RSFD r_open (RSET ct, int flag);
34 static void r_close (RSFD rfd);
35 static void r_delete (RSET ct);
36 static void r_rewind (RSFD rfd);
37 static int r_read (RSFD rfd, void *buf);
38 static int r_write (RSFD rfd, const void *buf);
39
40 static const struct rset_control control = 
41 {
42     "isamc",
43     r_create,
44     r_open,
45     r_close,
46     r_delete,
47     r_rewind,
48     rset_default_forward,
49     rset_default_pos,
50     r_read,
51     r_write,
52 };
53
54 const struct rset_control *rset_kind_isamc = &control;
55
56 struct rset_pp_info {
57     ISAMC_PP pt;
58     struct rset_pp_info *next;
59     struct rset_isamc_info *info;
60     void *buf;
61 };
62
63 struct rset_isamc_info {
64     ISAMC   is;
65     ISAMC_P pos;
66     int key_size;
67     int (*cmp)(const void *p1, const void *p2);
68     struct rset_pp_info *ispt_list;
69 };
70
71 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
72 {
73     rset_isamc_parms *pt = (rset_isamc_parms *) parms;
74     struct rset_isamc_info *info;
75
76     info = (struct rset_isamc_info *) xmalloc (sizeof(*info));
77     info->is = pt->is;
78     info->pos = pt->pos;
79     info->key_size = pt->key_size;
80     info->cmp = pt->cmp;
81     info->ispt_list = NULL;
82     return info;
83 }
84
85 RSFD r_open (RSET ct, int flag)
86 {
87     struct rset_isamc_info *info = (struct rset_isamc_info *) ct->buf;
88     struct rset_pp_info *ptinfo;
89
90     logf (LOG_DEBUG, "risamc_open");
91     if (flag & RSETF_WRITE)
92     {
93         logf (LOG_FATAL, "ISAMC set type is read-only");
94         return NULL;
95     }
96     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
97     ptinfo->next = info->ispt_list;
98     info->ispt_list = ptinfo;
99     ptinfo->pt = isc_pp_open (info->is, info->pos);
100     ptinfo->info = info;
101     ptinfo->buf = xmalloc (info->key_size);
102     return ptinfo;
103 }
104
105 static void r_close (RSFD rfd)
106 {
107     struct rset_isamc_info *info = ((struct rset_pp_info*) rfd)->info;
108     struct rset_pp_info **ptinfop;
109
110     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
111         if (*ptinfop == rfd)
112         {
113             xfree ((*ptinfop)->buf);
114             isc_pp_close ((*ptinfop)->pt);
115             *ptinfop = (*ptinfop)->next;
116             xfree (rfd);
117             return;
118         }
119     logf (LOG_FATAL, "r_close but no rfd match!");
120     assert (0);
121 }
122
123 static void r_delete (RSET ct)
124 {
125     struct rset_isamc_info *info = (struct rset_isamc_info *) ct->buf;
126
127     logf (LOG_DEBUG, "rsisamc_delete");
128     assert (info->ispt_list == NULL);
129     xfree (info);
130 }
131
132 static void r_rewind (RSFD rfd)
133 {   
134     logf (LOG_DEBUG, "rsisamc_rewind");
135     abort ();
136 }
137
138 static int r_read (RSFD rfd, void *buf)
139 {
140     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
141     int r;
142     r = isc_pp_read(pinfo->pt, buf);
143     return r;
144 }
145
146 static int r_write (RSFD rfd, const void *buf)
147 {
148     logf (LOG_FATAL, "ISAMC set type is read-only");
149     return -1;
150 }