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