Fix term counters to be of type zint. Fix several printfs of zint.
[idzebra-moved-to-github.git] / rset / rsisamc.c
1 /* $Id: rsisamc.c,v 1.17 2004-08-06 12:55:03 adam 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
24
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <zebrautl.h>
30 #include <rsisamc.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_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     rset_default_forward,
49     rset_default_pos,
50     r_read,
51     r_write,
52 };
53
54 const struct rset_control *rset_kind_isamc = &control;
55
56 struct rset_pp_info {
57     ISAMC_PP pt;
58     struct rset_pp_info *next;
59     struct rset_isamc_info *info;
60     zint *countp;
61     void *buf;
62 };
63
64 struct rset_isamc_info {
65     ISAMC   is;
66     ISAMC_P pos;
67     int key_size;
68     int (*cmp)(const void *p1, const void *p2);
69     struct rset_pp_info *ispt_list;
70 };
71
72 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
73 {
74     rset_isamc_parms *pt = (rset_isamc_parms *) parms;
75     struct rset_isamc_info *info;
76
77     ct->flags |= RSET_FLAG_VOLATILE;
78     info = (struct rset_isamc_info *) xmalloc (sizeof(*info));
79     info->is = pt->is;
80     info->pos = pt->pos;
81     info->key_size = pt->key_size;
82     info->cmp = pt->cmp;
83     info->ispt_list = NULL;
84     ct->no_rset_terms = 1;
85     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
86     ct->rset_terms[0] = pt->rset_term;
87     return info;
88 }
89
90 RSFD r_open (RSET ct, int flag)
91 {
92     struct rset_isamc_info *info = (struct rset_isamc_info *) ct->buf;
93     struct rset_pp_info *ptinfo;
94
95     logf (LOG_DEBUG, "risamc_open");
96     if (flag & RSETF_WRITE)
97     {
98         logf (LOG_FATAL, "ISAMC set type is read-only");
99         return NULL;
100     }
101     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
102     ptinfo->next = info->ispt_list;
103     info->ispt_list = ptinfo;
104     ptinfo->pt = isc_pp_open (info->is, info->pos);
105     ptinfo->info = info;
106     if (ct->rset_terms[0]->nn < 0)
107         ct->rset_terms[0]->nn = isc_pp_num (ptinfo->pt);
108     ct->rset_terms[0]->count = 0;
109     ptinfo->countp = &ct->rset_terms[0]->count;
110     ptinfo->buf = xmalloc (info->key_size);
111     return ptinfo;
112 }
113
114 static void r_close (RSFD rfd)
115 {
116     struct rset_isamc_info *info = ((struct rset_pp_info*) rfd)->info;
117     struct rset_pp_info **ptinfop;
118
119     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
120         if (*ptinfop == rfd)
121         {
122             xfree ((*ptinfop)->buf);
123             isc_pp_close ((*ptinfop)->pt);
124             *ptinfop = (*ptinfop)->next;
125             xfree (rfd);
126             return;
127         }
128     logf (LOG_FATAL, "r_close but no rfd match!");
129     assert (0);
130 }
131
132 static void r_delete (RSET ct)
133 {
134     struct rset_isamc_info *info = (struct rset_isamc_info *) ct->buf;
135
136     logf (LOG_DEBUG, "rsisamc_delete");
137     assert (info->ispt_list == NULL);
138     rset_term_destroy (ct->rset_terms[0]);
139     xfree (ct->rset_terms);
140     xfree (info);
141 }
142
143 static void r_rewind (RSFD rfd)
144 {   
145     logf (LOG_DEBUG, "rsisamc_rewind");
146     abort ();
147 }
148
149 static int r_read (RSFD rfd, void *buf, int *term_index)
150 {
151     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
152     int r;
153     *term_index = 0;
154     r = isc_pp_read(pinfo->pt, buf);
155     if (r > 0)
156     {
157         if (*pinfo->countp == 0 || (*pinfo->info->cmp)(buf, pinfo->buf) > 1)
158         {
159             memcpy (pinfo->buf, buf, pinfo->info->key_size);
160             (*pinfo->countp)++;
161         }
162     }
163     return r;
164 }
165
166 static int r_write (RSFD rfd, const void *buf)
167 {
168     logf (LOG_FATAL, "ISAMC set type is read-only");
169     return -1;
170 }