Added forward functions to rsbool, and a few tests for them.
[idzebra-moved-to-github.git] / rset / rsisamb.c
1 /* $Id: rsisamb.c,v 1.5 2004-01-30 11:43:41 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
26 #include <stdio.h>
27 #include <assert.h>
28 #include <zebrautl.h>
29 #include <rsisamb.h>
30 #include <string.h>
31
32 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
33 static RSFD r_open (RSET ct, int flag);
34 static void r_close (RSFD rfd);
35 static void r_delete (RSET ct);
36 static void r_rewind (RSFD rfd);
37 static int r_count (RSET ct);
38 static int r_read (RSFD rfd, void *buf, int *term_index);
39 static int r_write (RSFD rfd, const void *buf);
40
41 static const struct rset_control control = 
42 {
43     "isamb",
44     r_create,
45     r_open,
46     r_close,
47     r_delete,
48     r_rewind,
49     rset_default_forward,
50     r_count,
51     r_read,
52     r_write,
53 };
54
55 const struct rset_control *rset_kind_isamb = &control;
56
57 struct rset_pp_info {
58     ISAMB_PP pt;
59     struct rset_pp_info *next;
60     struct rset_isamb_info *info;
61     int *countp;
62     void *buf;
63 };
64
65 struct rset_isamb_info {
66     ISAMB   is;
67     ISAMB_P pos;
68     int key_size;
69     int (*cmp)(const void *p1, const void *p2);
70     struct rset_pp_info *ispt_list;
71 };
72
73 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
74 {
75     rset_isamb_parms *pt = (rset_isamb_parms *) parms;
76     struct rset_isamb_info *info;
77
78     ct->flags |= RSET_FLAG_VOLATILE;
79     info = (struct rset_isamb_info *) xmalloc (sizeof(*info));
80     info->is = pt->is;
81     info->pos = pt->pos;
82     info->key_size = pt->key_size;
83     info->cmp = pt->cmp;
84     info->ispt_list = NULL;
85     ct->no_rset_terms = 1;
86     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
87     ct->rset_terms[0] = pt->rset_term;
88     return info;
89 }
90
91 RSFD r_open (RSET ct, int flag)
92 {
93     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->buf;
94     struct rset_pp_info *ptinfo;
95
96     logf (LOG_DEBUG, "risamb_open");
97     if (flag & RSETF_WRITE)
98     {
99         logf (LOG_FATAL, "ISAMB set type is read-only");
100         return NULL;
101     }
102     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
103     ptinfo->next = info->ispt_list;
104     info->ispt_list = ptinfo;
105     ptinfo->pt = isamb_pp_open (info->is, info->pos);
106     ptinfo->info = info;
107     if (ct->rset_terms[0]->nn < 0)
108         ct->rset_terms[0]->nn = isamb_pp_num (ptinfo->pt);
109     ct->rset_terms[0]->count = 0;
110     ptinfo->countp = &ct->rset_terms[0]->count;
111     ptinfo->buf = xmalloc (info->key_size);
112     return ptinfo;
113 }
114
115 static void r_close (RSFD rfd)
116 {
117     struct rset_isamb_info *info = ((struct rset_pp_info*) rfd)->info;
118     struct rset_pp_info **ptinfop;
119
120     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
121         if (*ptinfop == rfd)
122         {
123             xfree ((*ptinfop)->buf);
124             isamb_pp_close ((*ptinfop)->pt);
125             *ptinfop = (*ptinfop)->next;
126             xfree (rfd);
127             return;
128         }
129     logf (LOG_FATAL, "r_close but no rfd match!");
130     assert (0);
131 }
132
133 static void r_delete (RSET ct)
134 {
135     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->buf;
136
137     logf (LOG_DEBUG, "rsisamb_delete");
138     assert (info->ispt_list == NULL);
139     rset_term_destroy (ct->rset_terms[0]);
140     xfree (ct->rset_terms);
141     xfree (info);
142 }
143
144 static void r_rewind (RSFD rfd)
145 {   
146     logf (LOG_DEBUG, "rsisamb_rewind");
147     abort ();
148 }
149
150 static int r_count (RSET ct)
151 {
152     return 0;
153 }
154
155 static int r_read (RSFD rfd, void *buf, int *term_index)
156 {
157     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
158     int r;
159     *term_index = 0;
160     r = isamb_pp_read(pinfo->pt, buf);
161     if (r > 0)
162     {
163         if (*pinfo->countp == 0 || (*pinfo->info->cmp)(buf, pinfo->buf) > 1)
164         {
165             memcpy (pinfo->buf, buf, pinfo->info->key_size);
166             (*pinfo->countp)++;
167         }
168     }
169     return r;
170 }
171
172 static int r_write (RSFD rfd, const void *buf)
173 {
174     logf (LOG_FATAL, "ISAMB set type is read-only");
175     return -1;
176 }