Cleaned up the creation of rsets, added nmem
[idzebra-moved-to-github.git] / rset / rsisamb.c
1 /* $Id: rsisamb.c,v 1.17 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 #include <stdio.h>
24 #include <assert.h>
25 #include <zebrautl.h>
26 #include <rsisamb.h>
27 #include <string.h>
28 #include <../index/index.h> /* for log_keydump. Debugging only */
29
30 #ifndef RSET_DEBUG
31 #define RSET_DEBUG 0
32 #endif
33
34 static RSFD r_open (RSET ct, int flag);
35 static void r_close (RSFD rfd);
36 static void r_delete (RSET ct);
37 static void r_rewind (RSFD rfd);
38 static int r_forward(RSET ct, RSFD rfd, void *buf,
39                      int (*cmpfunc)(const void *p1, const void *p2),
40                      const void *untilbuf);
41 static void r_pos (RSFD rfd, double *current, double *total);
42 static int r_read (RSFD rfd, void *buf);
43 static int r_write (RSFD rfd, const void *buf);
44
45 static const struct rset_control control = 
46 {
47     "isamb",
48     r_open,
49     r_close,
50     r_delete,
51     r_rewind,
52     r_forward, /* rset_default_forward, */
53     r_pos,
54     r_read,
55     r_write,
56 };
57
58 const struct rset_control *rset_kind_isamb = &control;
59
60 struct rset_pp_info {
61     ISAMB_PP pt;
62     struct rset_pp_info *next;
63     struct rset_isamb_info *info;
64     void *buf;
65 };
66
67 struct rset_isamb_info {
68     ISAMB   is;
69     ISAMB_P pos;
70     int key_size;
71     int (*cmp)(const void *p1, const void *p2);
72     struct rset_pp_info *ispt_list;
73 };
74
75 RSET rsisamb_create( NMEM nmem, int key_size, 
76             int (*cmp)(const void *p1, const void *p2),
77             ISAMB is, ISAMB_P pos)
78 {
79     RSET rnew=rset_create_base(&control, nmem);
80     struct rset_isamb_info *info;
81     info = (struct rset_isamb_info *) nmem_malloc(rnew->nmem,sizeof(*info));
82     info->key_size = key_size;
83     info->cmp = cmp;
84     info->is=is;
85     info->pos=pos;
86     info->ispt_list = NULL;
87     rnew->priv=info;
88     return rnew;
89 }
90
91 static void r_delete (RSET ct)
92 {
93     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->priv;
94
95     logf (LOG_DEBUG, "rsisamb_delete");
96     assert (info->ispt_list == NULL);
97 }
98
99 #if 0
100 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
101 {
102     rset_isamb_parms *pt = (rset_isamb_parms *) parms;
103     struct rset_isamb_info *info;
104
105     info = (struct rset_isamb_info *) xmalloc (sizeof(*info));
106     info->is = pt->is;
107     info->pos = pt->pos;
108     info->key_size = pt->key_size;
109     info->cmp = pt->cmp;
110     info->ispt_list = NULL;
111     return info;
112 }
113 #endif
114
115 RSFD r_open (RSET ct, int flag)
116 {
117     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->priv;
118     struct rset_pp_info *ptinfo;
119
120     logf (LOG_DEBUG, "risamb_open");
121     if (flag & RSETF_WRITE)
122     {
123         logf (LOG_FATAL, "ISAMB set type is read-only");
124         return NULL;
125     }
126     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
127     ptinfo->next = info->ispt_list;
128     info->ispt_list = ptinfo;
129     ptinfo->pt = isamb_pp_open (info->is, info->pos);
130     ptinfo->info = info;
131     ptinfo->buf = xmalloc (info->key_size);
132     return ptinfo;
133 }
134
135 static void r_close (RSFD rfd)
136 {
137     struct rset_isamb_info *info = ((struct rset_pp_info*) rfd)->info;
138     struct rset_pp_info **ptinfop;
139
140     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
141         if (*ptinfop == rfd)
142         {
143             xfree ((*ptinfop)->buf);
144             isamb_pp_close ((*ptinfop)->pt);
145             *ptinfop = (*ptinfop)->next;
146             xfree (rfd);
147             return;
148         }
149     logf (LOG_FATAL, "r_close but no rfd match!");
150     assert (0);
151 }
152
153
154 static void r_rewind (RSFD rfd)
155 {   
156     logf (LOG_DEBUG, "rsisamb_rewind");
157     abort ();
158 }
159
160 static int r_forward(RSET ct, RSFD rfd, void *buf, 
161                      int (*cmpfunc)(const void *p1, const void *p2),
162                      const void *untilbuf)
163 {
164     int i; 
165     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
166 #if RSET_DEBUG
167     logf (LOG_DEBUG, "rset_rsisamb_forward starting '%s' (ct=%p rfd=%p)",
168                       ct->control->desc, ct,rfd);
169     key_logdump(LOG_DEBUG, untilbuf);
170     key_logdump(LOG_DEBUG, buf);
171 #endif
172
173     i=isamb_pp_forward(pinfo->pt, buf, untilbuf);
174 #if RSET_DEBUG
175     logf (LOG_DEBUG, "rset_rsisamb_forward returning %d",i);
176 #endif
177     return i;
178 }
179
180 static void r_pos (RSFD rfd, double *current, double *total)
181 {
182     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
183     assert(rfd);
184     isamb_pp_pos(pinfo->pt, current, total);
185 #if RSET_DEBUG
186     logf(LOG_DEBUG,"isamb.r_pos returning %0.1f/%0.1f",
187               *current, *total);
188 #endif
189 }
190
191 static int r_read (RSFD rfd, void *buf)
192 {
193     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
194     int r;
195
196     r = isamb_pp_read(pinfo->pt, buf);
197     return r;
198 }
199
200 static int r_write (RSFD rfd, const void *buf)
201 {
202     logf (LOG_FATAL, "ISAMB set type is read-only");
203     return -1;
204 }