Added framework for the pos calls. rsisamb and rsnull have a real one,
[idzebra-moved-to-github.git] / rset / rsisamd.c
1 /* $Id: rsisamd.c,v 1.8 2004-08-03 14:54:41 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_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     "isamd",
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_isamd = &control;
55
56 struct rset_pp_info {
57     ISAMD_PP pt;
58     struct rset_pp_info *next;
59     struct rset_isamd_info *info;
60 };
61
62 struct rset_isamd_info {
63     ISAMD   is; 
64     /* ISAMD_P pos; */
65     char dictentry[ISAMD_MAX_DICT_LEN];
66     int dictlen;
67     struct rset_pp_info *ispt_list;
68 };
69
70 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
71 {
72     rset_isamd_parms *pt = (rset_isamd_parms *) parms;
73     struct rset_isamd_info *info;
74
75     ct->flags |= RSET_FLAG_VOLATILE;
76     info = (struct rset_isamd_info *) xmalloc (sizeof(*info));
77     info->is = pt->is;
78     /*info->pos = pt->pos;*/
79     info->dictlen = pt->dictlen;
80     memcpy(info->dictentry, pt->dictentry, pt->dictlen);
81     info->ispt_list = NULL;
82     ct->no_rset_terms = 1;
83     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
84     ct->rset_terms[0] = pt->rset_term;
85     return info;
86 }
87
88 RSFD r_open (RSET ct, int flag)
89 {
90     struct rset_isamd_info *info = (struct rset_isamd_info *) ct->buf;
91     struct rset_pp_info *ptinfo;
92
93     logf (LOG_DEBUG, "risamd_open");
94     if (flag & RSETF_WRITE)
95     {
96         logf (LOG_FATAL, "ISAMD set type is read-only");
97         return NULL;
98     }
99     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
100     ptinfo->next = info->ispt_list;
101     info->ispt_list = ptinfo;
102     ptinfo->pt = isamd_pp_open (info->is, info->dictentry, info->dictlen);
103     ptinfo->info = info;
104     if (ct->rset_terms[0]->nn < 0)
105         ct->rset_terms[0]->nn = isamd_pp_num (ptinfo->pt);
106     return ptinfo;
107 }
108
109 static void r_close (RSFD rfd)
110 {
111     struct rset_isamd_info *info = ((struct rset_pp_info*) rfd)->info;
112     struct rset_pp_info **ptinfop;
113
114     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
115         if (*ptinfop == rfd)
116         {
117             isamd_pp_close ((*ptinfop)->pt);
118             *ptinfop = (*ptinfop)->next;
119             xfree (rfd);
120             return;
121         }
122     logf (LOG_FATAL, "r_close but no rfd match!");
123     assert (0);
124 }
125
126 static void r_delete (RSET ct)
127 {
128     struct rset_isamd_info *info = (struct rset_isamd_info *) ct->buf;
129
130     logf (LOG_DEBUG, "rsisamd_delete");
131     assert (info->ispt_list == NULL);
132     rset_term_destroy (ct->rset_terms[0]);
133     xfree (ct->rset_terms);
134     xfree (info);
135 }
136
137 static void r_rewind (RSFD rfd)
138 {   
139     logf (LOG_DEBUG, "rsisamd_rewind");
140     abort ();
141 }
142
143
144 static int r_read (RSFD rfd, void *buf, int *term_index)
145 {
146     *term_index = 0;
147     return isamd_pp_read( ((struct rset_pp_info*) rfd)->pt, buf);
148 }
149
150 static int r_write (RSFD rfd, const void *buf)
151 {
152     logf (LOG_FATAL, "ISAMD set type is read-only");
153     return -1;
154 }