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