74295a8d0c3691acc3147a325c34b603fa195bdc
[idzebra-moved-to-github.git] / rset / rsisamd.c
1 /* $Id: rsisamd.c,v 1.7 2004-08-03 12:15:45 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
24
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <zebrautl.h>
30 #include <rsisamd.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     "isamd",
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_isamd = &control;
56
57 struct rset_pp_info {
58     ISAMD_PP pt;
59     struct rset_pp_info *next;
60     struct rset_isamd_info *info;
61 };
62
63 struct rset_isamd_info {
64     ISAMD   is; 
65     /* ISAMD_P pos; */
66     char dictentry[ISAMD_MAX_DICT_LEN];
67     int dictlen;
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_isamd_parms *pt = (rset_isamd_parms *) parms;
74     struct rset_isamd_info *info;
75
76     ct->flags |= RSET_FLAG_VOLATILE;
77     info = (struct rset_isamd_info *) xmalloc (sizeof(*info));
78     info->is = pt->is;
79     /*info->pos = pt->pos;*/
80     info->dictlen = pt->dictlen;
81     memcpy(info->dictentry, pt->dictentry, pt->dictlen);
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_isamd_info *info = (struct rset_isamd_info *) ct->buf;
92     struct rset_pp_info *ptinfo;
93
94     logf (LOG_DEBUG, "risamd_open");
95     if (flag & RSETF_WRITE)
96     {
97         logf (LOG_FATAL, "ISAMD 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 = isamd_pp_open (info->is, info->dictentry, info->dictlen);
104     ptinfo->info = info;
105     if (ct->rset_terms[0]->nn < 0)
106         ct->rset_terms[0]->nn = isamd_pp_num (ptinfo->pt);
107     return ptinfo;
108 }
109
110 static void r_close (RSFD rfd)
111 {
112     struct rset_isamd_info *info = ((struct rset_pp_info*) rfd)->info;
113     struct rset_pp_info **ptinfop;
114
115     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
116         if (*ptinfop == rfd)
117         {
118             isamd_pp_close ((*ptinfop)->pt);
119             *ptinfop = (*ptinfop)->next;
120             xfree (rfd);
121             return;
122         }
123     logf (LOG_FATAL, "r_close but no rfd match!");
124     assert (0);
125 }
126
127 static void r_delete (RSET ct)
128 {
129     struct rset_isamd_info *info = (struct rset_isamd_info *) ct->buf;
130
131     logf (LOG_DEBUG, "rsisamd_delete");
132     assert (info->ispt_list == NULL);
133     rset_term_destroy (ct->rset_terms[0]);
134     xfree (ct->rset_terms);
135     xfree (info);
136 }
137
138 static void r_rewind (RSFD rfd)
139 {   
140     logf (LOG_DEBUG, "rsisamd_rewind");
141     abort ();
142 }
143
144 /*
145 static int r_count (RSET ct)
146 {
147     return 0;
148 }
149 */
150
151 static int r_read (RSFD rfd, void *buf, int *term_index)
152 {
153     *term_index = 0;
154     return isamd_pp_read( ((struct rset_pp_info*) rfd)->pt, buf);
155 }
156
157 static int r_write (RSFD rfd, const void *buf)
158 {
159     logf (LOG_FATAL, "ISAMD set type is read-only");
160     return -1;
161 }