Using nmem for all rsets, and keeping a freelist for freed rfds, so
[idzebra-moved-to-github.git] / rset / rsisamb.c
1 /* $Id: rsisamb.c,v 1.18 2004-08-26 11:11:59 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     struct rset_pp_info *free_list;
74 };
75
76 RSET rsisamb_create( NMEM nmem, int key_size, 
77             int (*cmp)(const void *p1, const void *p2),
78             ISAMB is, ISAMB_P pos)
79 {
80     RSET rnew=rset_create_base(&control, nmem);
81     struct rset_isamb_info *info;
82     info = (struct rset_isamb_info *) nmem_malloc(rnew->nmem,sizeof(*info));
83     info->key_size = key_size;
84     info->cmp = cmp;
85     info->is=is;
86     info->pos=pos;
87     info->ispt_list = NULL;
88     info->free_list = NULL;
89     rnew->priv=info;
90     return rnew;
91 }
92
93 static void r_delete (RSET ct)
94 {
95     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->priv;
96
97     logf (LOG_DEBUG, "rsisamb_delete");
98     assert (info->ispt_list == NULL);
99 }
100
101 #if 0
102 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
103 {
104     rset_isamb_parms *pt = (rset_isamb_parms *) parms;
105     struct rset_isamb_info *info;
106
107     info = (struct rset_isamb_info *) xmalloc (sizeof(*info));
108     info->is = pt->is;
109     info->pos = pt->pos;
110     info->key_size = pt->key_size;
111     info->cmp = pt->cmp;
112     info->ispt_list = NULL;
113     return info;
114 }
115 #endif
116
117 RSFD r_open (RSET ct, int flag)
118 {
119     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->priv;
120     struct rset_pp_info *ptinfo;
121
122     logf (LOG_DEBUG, "risamb_open");
123     if (flag & RSETF_WRITE)
124     {
125         logf (LOG_FATAL, "ISAMB set type is read-only");
126         return NULL;
127     }
128     ptinfo = info->free_list;
129     if (ptinfo)
130         info->free_list=ptinfo->next;
131     else {
132         ptinfo = (struct rset_pp_info *) nmem_malloc (ct->nmem,sizeof(*ptinfo));
133         ptinfo->buf = nmem_malloc (ct->nmem,info->key_size);
134     }
135     ptinfo->next = info->ispt_list;
136     info->ispt_list = ptinfo;
137     ptinfo->pt = isamb_pp_open (info->is, info->pos);
138     ptinfo->info = info;
139     return ptinfo;
140 }
141
142 static void r_close (RSFD rfd)
143 {
144     struct rset_isamb_info *info = ((struct rset_pp_info*) rfd)->info;
145     struct rset_pp_info **ptinfop;
146
147     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
148         if (*ptinfop == rfd)
149         {
150             struct rset_pp_info *tmp=(struct rset_pp_info*) rfd;
151             isamb_pp_close ((*ptinfop)->pt);
152             *ptinfop = (*ptinfop)->next;
153             tmp->next=info->free_list;
154             info->free_list=tmp;
155             return;
156         }
157     logf (LOG_FATAL, "r_close but no rfd match!");
158     assert (0);
159 }
160
161
162 static void r_rewind (RSFD rfd)
163 {   
164     logf (LOG_DEBUG, "rsisamb_rewind");
165     abort ();
166 }
167
168 static int r_forward(RSET ct, RSFD rfd, void *buf, 
169                      int (*cmpfunc)(const void *p1, const void *p2),
170                      const void *untilbuf)
171 {
172     int i; 
173     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
174 #if RSET_DEBUG
175     logf (LOG_DEBUG, "rset_rsisamb_forward starting '%s' (ct=%p rfd=%p)",
176                       ct->control->desc, ct,rfd);
177     key_logdump(LOG_DEBUG, untilbuf);
178     key_logdump(LOG_DEBUG, buf);
179 #endif
180
181     i=isamb_pp_forward(pinfo->pt, buf, untilbuf);
182 #if RSET_DEBUG
183     logf (LOG_DEBUG, "rset_rsisamb_forward returning %d",i);
184 #endif
185     return i;
186 }
187
188 static void r_pos (RSFD rfd, double *current, double *total)
189 {
190     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
191     assert(rfd);
192     isamb_pp_pos(pinfo->pt, current, total);
193 #if RSET_DEBUG
194     logf(LOG_DEBUG,"isamb.r_pos returning %0.1f/%0.1f",
195               *current, *total);
196 #endif
197 }
198
199 static int r_read (RSFD rfd, void *buf)
200 {
201     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
202     int r;
203
204     r = isamb_pp_read(pinfo->pt, buf);
205     return r;
206 }
207
208 static int r_write (RSFD rfd, const void *buf)
209 {
210     logf (LOG_FATAL, "ISAMB set type is read-only");
211     return -1;
212 }