Removed the count function from the rset f-table, in preparation
[idzebra-moved-to-github.git] / rset / rsisam.c
1 /* $Id: rsisam.c,v 1.25 2004-08-03 12:15:45 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 #include <stdio.h>
25 #include <assert.h>
26 #include <zebrautl.h>
27 #include <rsisam.h>
28
29 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
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_count (RSET ct); */
35 static int r_read (RSFD rfd, void *buf, int *term_index);
36 static int r_write (RSFD rfd, const void *buf);
37
38 static const struct rset_control control = 
39 {
40     "isam",
41     r_create,
42     r_open,
43     r_close,
44     r_delete,
45     r_rewind,
46     rset_default_forward,
47     /* r_count, */
48     r_read,
49     r_write,
50 };
51
52 const struct rset_control *rset_kind_isam = &control;
53
54 struct rset_ispt_info {
55     ISPT   pt;
56     struct rset_ispt_info *next;
57     struct rset_isam_info *info;
58 };
59
60 struct rset_isam_info {
61     ISAM   is;
62     ISAM_P pos;
63     struct rset_ispt_info *ispt_list;
64 };
65
66 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
67 {
68     rset_isam_parms *pt = (rset_isam_parms *) parms;
69     struct rset_isam_info *info;
70
71     ct->flags |= RSET_FLAG_VOLATILE;
72     info = (struct rset_isam_info *) xmalloc (sizeof(struct rset_isam_info));
73     info->is = pt->is;
74     info->pos = pt->pos;
75     info->ispt_list = NULL;
76
77     ct->no_rset_terms = 1;
78     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
79     ct->rset_terms[0] = pt->rset_term;
80     return info;
81 }
82
83 RSFD r_open (RSET ct, int flag)
84 {
85     struct rset_isam_info *info = (struct rset_isam_info *) ct->buf;
86     struct rset_ispt_info *ptinfo;
87
88     logf (LOG_DEBUG, "risam_open");
89     if (flag & RSETF_WRITE)
90     {
91         logf (LOG_FATAL, "ISAM set type is read-only");
92         return NULL;
93     }
94     ptinfo = (struct rset_ispt_info *) xmalloc (sizeof(*ptinfo));
95     ptinfo->next = info->ispt_list;
96     info->ispt_list = ptinfo;
97     ptinfo->pt = is_position (info->is, info->pos);
98     ptinfo->info = info;
99
100     if (ct->rset_terms[0]->nn < 0)
101         ct->rset_terms[0]->nn = is_numkeys (ptinfo->pt);
102     return ptinfo;
103 }
104
105 static void r_close (RSFD rfd)
106 {
107     struct rset_isam_info *info = ((struct rset_ispt_info*) rfd)->info;
108     struct rset_ispt_info **ptinfop;
109
110     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
111         if (*ptinfop == rfd)
112         {
113             is_pt_free ((*ptinfop)->pt);
114             *ptinfop = (*ptinfop)->next;
115             xfree (rfd);
116             return;
117         }
118     logf (LOG_FATAL, "r_close but no rfd match!");
119     assert (0);
120 }
121
122 static void r_delete (RSET ct)
123 {
124     struct rset_isam_info *info = (struct rset_isam_info *) ct->buf;
125
126     logf (LOG_DEBUG, "rsisam_delete");
127     assert (info->ispt_list == NULL);
128     rset_term_destroy (ct->rset_terms[0]);
129     xfree (ct->rset_terms);
130     xfree (info);
131 }
132
133 static void r_rewind (RSFD rfd)
134 {   
135     logf (LOG_DEBUG, "rsisam_rewind");
136     is_rewind( ((struct rset_ispt_info*) rfd)->pt);
137 }
138
139 /*
140 static int r_count (RSET ct)
141 {
142     return 0;
143 }
144 */
145
146 static int r_read (RSFD rfd, void *buf, int *term_index)
147 {
148     *term_index = 0;
149     return is_readkey( ((struct rset_ispt_info*) rfd)->pt, buf);
150 }
151
152 static int r_write (RSFD rfd, const void *buf)
153 {
154     logf (LOG_FATAL, "ISAM set type is read-only");
155     return -1;
156 }