Changed the pos code to 64-bit clean. Still lots of stuff missing...
[idzebra-moved-to-github.git] / rset / rsisams.c
1 /* $Id: rsisams.c,v 1.7 2004-08-04 09:59:03 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 void *r_create(RSET ct, const struct rset_control *sel, void *parms);
31 static RSFD r_open (RSET ct, int flag);
32 static void r_close (RSFD rfd);
33 static void r_delete (RSET ct);
34 static void r_rewind (RSFD rfd);
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     "isams",
41     r_create,
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_isams = &control;
53
54 struct rset_pp_info {
55     ISAMS_PP pt;
56     struct rset_pp_info *next;
57     struct rset_isams_info *info;
58 };
59
60 struct rset_isams_info {
61     ISAMS   is;
62     ISAMS_P pos;
63     struct rset_pp_info *ispt_list;
64 };
65
66 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
67 {
68     rset_isams_parms *pt = (struct rset_isams_parms *) parms;
69     struct rset_isams_info *info;
70
71     ct->flags |= RSET_FLAG_VOLATILE;
72     info = (struct rset_isams_info *) xmalloc (sizeof(*info));
73     info->is = pt->is;
74     info->pos = pt->pos;
75     info->ispt_list = NULL;
76     ct->no_rset_terms = 1;
77     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
78     ct->rset_terms[0] = pt->rset_term;
79     return info;
80 }
81
82 RSFD r_open (RSET ct, int flag)
83 {
84     struct rset_isams_info *info = (struct rset_isams_info *) ct->buf;
85     struct rset_pp_info *ptinfo;
86
87     logf (LOG_DEBUG, "risams_open");
88     if (flag & RSETF_WRITE)
89     {
90         logf (LOG_FATAL, "ISAMS set type is read-only");
91         return NULL;
92     }
93     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
94     ptinfo->next = info->ispt_list;
95     info->ispt_list = ptinfo;
96     ptinfo->pt = isams_pp_open (info->is, info->pos);
97     ptinfo->info = info;
98     if (ct->rset_terms[0]->nn < 0)
99         ct->rset_terms[0]->nn = isams_pp_num (ptinfo->pt);
100     return ptinfo;
101 }
102
103 static void r_close (RSFD rfd)
104 {
105     struct rset_isams_info *info = ((struct rset_pp_info*) rfd)->info;
106     struct rset_pp_info **ptinfop;
107
108     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
109         if (*ptinfop == rfd)
110         {
111             isams_pp_close ((*ptinfop)->pt);
112             *ptinfop = (*ptinfop)->next;
113             xfree (rfd);
114             return;
115         }
116     logf (LOG_FATAL, "r_close but no rfd match!");
117     assert (0);
118 }
119
120 static void r_delete (RSET ct)
121 {
122     struct rset_isams_info *info = (struct rset_isams_info *) ct->buf;
123
124     logf (LOG_DEBUG, "rsisams_delete");
125     assert (info->ispt_list == NULL);
126     rset_term_destroy (ct->rset_terms[0]);
127     xfree (ct->rset_terms);
128     xfree (info);
129 }
130
131 static void r_rewind (RSFD rfd)
132 {   
133     logf (LOG_DEBUG, "rsisams_rewind");
134     abort ();
135 }
136
137
138 static int r_read (RSFD rfd, void *buf, int *term_index)
139 {
140     *term_index = 0;
141     return isams_pp_read( ((struct rset_pp_info*) rfd)->pt, buf);
142 }
143
144 static int r_write (RSFD rfd, const void *buf)
145 {
146     logf (LOG_FATAL, "ISAMS set type is read-only");
147     return -1;
148 }