Added a comment in rsisamb
[idzebra-moved-to-github.git] / rset / rsisamb.c
1 /* $Id: rsisamb.c,v 1.10.2.1 2004-12-17 13:43:10 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, int *term_index,
40                      int (*cmpfunc)(const void *p1, const void *p2),
41                      const void *untilbuf);
42 static void r_pos (RSFD rfd, int *current, int *total);
43 static int r_read (RSFD rfd, void *buf, int *term_index);
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 /* FIXME - using the default forward reads all items from the isam */
61 /* and thus makes the term counts work OK. On the other hand, it   */
62 /* negates the speedup from forwarding */
63
64 const struct rset_control *rset_kind_isamb = &control;
65
66 struct rset_pp_info {
67     ISAMB_PP pt;
68     struct rset_pp_info *next;
69     struct rset_isamb_info *info;
70     int *countp;
71     void *buf;
72 };
73
74 struct rset_isamb_info {
75     ISAMB   is;
76     ISAMB_P pos;
77     int key_size;
78     int (*cmp)(const void *p1, const void *p2);
79     struct rset_pp_info *ispt_list;
80 };
81
82 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
83 {
84     rset_isamb_parms *pt = (rset_isamb_parms *) parms;
85     struct rset_isamb_info *info;
86
87     ct->flags |= RSET_FLAG_VOLATILE;
88     info = (struct rset_isamb_info *) xmalloc (sizeof(*info));
89     info->is = pt->is;
90     info->pos = pt->pos;
91     info->key_size = pt->key_size;
92     info->cmp = pt->cmp;
93     info->ispt_list = NULL;
94     ct->no_rset_terms = 1;
95     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
96     ct->rset_terms[0] = pt->rset_term;
97     return info;
98 }
99
100 RSFD r_open (RSET ct, int flag)
101 {
102     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->buf;
103     struct rset_pp_info *ptinfo;
104
105     logf (LOG_DEBUG, "risamb_open");
106     if (flag & RSETF_WRITE)
107     {
108         logf (LOG_FATAL, "ISAMB set type is read-only");
109         return NULL;
110     }
111     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
112     ptinfo->next = info->ispt_list;
113     info->ispt_list = ptinfo;
114     ptinfo->pt = isamb_pp_open (info->is, info->pos);
115     ptinfo->info = info;
116     if (ct->rset_terms[0]->nn < 0)
117         ct->rset_terms[0]->nn = isamb_pp_num (ptinfo->pt);
118     ct->rset_terms[0]->count = 0;
119     ptinfo->countp = &ct->rset_terms[0]->count;
120     ptinfo->buf = xmalloc (info->key_size);
121     return ptinfo;
122 }
123
124 static void r_close (RSFD rfd)
125 {
126     struct rset_isamb_info *info = ((struct rset_pp_info*) rfd)->info;
127     struct rset_pp_info **ptinfop;
128
129     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
130         if (*ptinfop == rfd)
131         {
132             xfree ((*ptinfop)->buf);
133             isamb_pp_close ((*ptinfop)->pt);
134             *ptinfop = (*ptinfop)->next;
135             xfree (rfd);
136             return;
137         }
138     logf (LOG_FATAL, "r_close but no rfd match!");
139     assert (0);
140 }
141
142 static void r_delete (RSET ct)
143 {
144     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->buf;
145
146     logf (LOG_DEBUG, "rsisamb_delete");
147     assert (info->ispt_list == NULL);
148     rset_term_destroy (ct->rset_terms[0]);
149     xfree (ct->rset_terms);
150     xfree (info);
151 }
152
153 static void r_rewind (RSFD rfd)
154 {   
155     logf (LOG_DEBUG, "rsisamb_rewind");
156     abort ();
157 }
158
159 static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index,
160                      int (*cmpfunc)(const void *p1, const void *p2),
161                      const void *untilbuf)
162 {
163     int i; 
164     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
165 #if RSET_DEBUG
166     logf (LOG_DEBUG, "rset_rsisamb_forward starting '%s' (ct=%p rfd=%p)",
167                       ct->control->desc, ct,rfd);
168     key_logdump(LOG_DEBUG, untilbuf);
169     key_logdump(LOG_DEBUG, buf);
170 #endif
171
172     i=isamb_pp_forward(pinfo->pt, buf, untilbuf);
173 #if RSET_DEBUG
174     logf (LOG_DEBUG, "rset_rsisamb_forward returning %d",i);
175 #endif
176     return i;
177 }
178
179 static void r_pos (RSFD rfd, int *current, int *total)
180 {
181     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
182     assert(rfd);
183     isamb_pp_pos(pinfo->pt, current, total);
184 }
185
186 static int r_read (RSFD rfd, void *buf, int *term_index)
187 {
188     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
189     int r;
190     *term_index = 0;
191     r = isamb_pp_read(pinfo->pt, buf);
192     if (r > 0)
193     {
194         if (*pinfo->countp == 0 || (*pinfo->info->cmp)(buf, pinfo->buf) > 1)
195         {
196             memcpy (pinfo->buf, buf, pinfo->info->key_size);
197             (*pinfo->countp)++;
198         }
199     }
200     return r;
201 }
202
203 static int r_write (RSFD rfd, const void *buf)
204 {
205     logf (LOG_FATAL, "ISAMB set type is read-only");
206     return -1;
207 }