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