C++ compilation.
[idzebra-moved-to-github.git] / rset / rsisam.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsisam.c,v $
7  * Revision 1.20  1999-05-26 07:49:14  adam
8  * C++ compilation.
9  *
10  * Revision 1.19  1999/02/02 14:51:34  adam
11  * Updated WIN32 code specific sections. Changed header.
12  *
13  * Revision 1.18  1998/03/05 08:36:28  adam
14  * New result set model.
15  *
16  * Revision 1.17  1997/12/18 10:54:25  adam
17  * New method result set method rs_hits that returns the number of
18  * hits in result-set (if known). The ranked result set returns real
19  * number of hits but only when not combined with other operands.
20  *
21  * Revision 1.16  1997/10/31 12:37:01  adam
22  * Code calls xfree() instead of free().
23  *
24  * Revision 1.15  1996/10/29 13:55:22  adam
25  * Include of zebrautl.h instead of alexutil.h.
26  *
27  * Revision 1.14  1995/12/11 09:15:24  adam
28  * New set types: sand/sor/snot - ranked versions of and/or/not in
29  * ranked/semi-ranked result sets.
30  * Note: the snot not finished yet.
31  * New rset member: flag.
32  * Bug fix: r_delete in rsrel.c did free bad memory block.
33  *
34  * Revision 1.13  1995/10/12  12:41:56  adam
35  * Private info (buf) moved from struct rset_control to struct rset.
36  * Bug fixes in relevance.
37  *
38  * Revision 1.12  1995/10/10  14:00:04  adam
39  * Function rset_open changed its wflag parameter to general flags.
40  *
41  * Revision 1.11  1995/10/06  14:38:05  adam
42  * New result set method: r_score.
43  * Local no (sysno) and score is transferred to retrieveCtrl.
44  *
45  * Revision 1.10  1995/09/08  14:52:42  adam
46  * Work on relevance feedback.
47  *
48  * Revision 1.9  1995/09/07  13:58:43  adam
49  * New parameter: result-set file descriptor (RSFD) to support multiple
50  * positions within the same result-set.
51  * Boolean operators: and, or, not implemented.
52  *
53  * Revision 1.8  1995/09/06  16:11:56  adam
54  * More work on boolean sets.
55  *
56  * Revision 1.7  1995/09/06  10:35:44  adam
57  * Null set implemented.
58  *
59  * Revision 1.6  1995/09/05  11:43:24  adam
60  * Complete version of temporary sets. Not tested yet though.
61  *
62  * Revision 1.5  1995/09/04  12:33:56  adam
63  * Various cleanup. YAZ util used instead.
64  *
65  * Revision 1.4  1995/09/04  09:10:55  adam
66  * Minor changes.
67  *
68  * Revision 1.3  1994/11/22  13:15:37  quinn
69  * Simple
70  *
71  * Revision 1.2  1994/11/04  14:53:12  quinn
72  * Work
73  *
74  */
75
76 #include <stdio.h>
77 #include <assert.h>
78 #include <rsisam.h>
79 #include <zebrautl.h>
80
81 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
82 static RSFD r_open (RSET ct, int flag);
83 static void r_close (RSFD rfd);
84 static void r_delete (RSET ct);
85 static void r_rewind (RSFD rfd);
86 static int r_count (RSET ct);
87 static int r_read (RSFD rfd, void *buf, int *term_index);
88 static int r_write (RSFD rfd, const void *buf);
89
90 static const struct rset_control control = 
91 {
92     "isam",
93     r_create,
94     r_open,
95     r_close,
96     r_delete,
97     r_rewind,
98     r_count,
99     r_read,
100     r_write,
101 };
102
103 const struct rset_control *rset_kind_isam = &control;
104
105 struct rset_ispt_info {
106     ISPT   pt;
107     struct rset_ispt_info *next;
108     struct rset_isam_info *info;
109 };
110
111 struct rset_isam_info {
112     ISAM   is;
113     ISAM_P pos;
114     struct rset_ispt_info *ispt_list;
115 };
116
117 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
118 {
119     rset_isam_parms *pt = (rset_isam_parms *) parms;
120     struct rset_isam_info *info;
121
122     ct->flags |= RSET_FLAG_VOLATILE;
123     info = (struct rset_isam_info *) xmalloc (sizeof(struct rset_isam_info));
124     info->is = pt->is;
125     info->pos = pt->pos;
126     info->ispt_list = NULL;
127
128     ct->no_rset_terms = 1;
129     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
130     ct->rset_terms[0] = pt->rset_term;
131     return info;
132 }
133
134 RSFD r_open (RSET ct, int flag)
135 {
136     struct rset_isam_info *info = (struct rset_isam_info *) ct->buf;
137     struct rset_ispt_info *ptinfo;
138
139     logf (LOG_DEBUG, "risam_open");
140     if (flag & RSETF_WRITE)
141     {
142         logf (LOG_FATAL, "ISAM set type is read-only");
143         return NULL;
144     }
145     ptinfo = (struct rset_ispt_info *) xmalloc (sizeof(*ptinfo));
146     ptinfo->next = info->ispt_list;
147     info->ispt_list = ptinfo;
148     ptinfo->pt = is_position (info->is, info->pos);
149     ptinfo->info = info;
150
151     if (ct->rset_terms[0]->nn < 0)
152         ct->rset_terms[0]->nn = is_numkeys (ptinfo->pt);
153     return ptinfo;
154 }
155
156 static void r_close (RSFD rfd)
157 {
158     struct rset_isam_info *info = ((struct rset_ispt_info*) rfd)->info;
159     struct rset_ispt_info **ptinfop;
160
161     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
162         if (*ptinfop == rfd)
163         {
164             is_pt_free ((*ptinfop)->pt);
165             *ptinfop = (*ptinfop)->next;
166             xfree (rfd);
167             return;
168         }
169     logf (LOG_FATAL, "r_close but no rfd match!");
170     assert (0);
171 }
172
173 static void r_delete (RSET ct)
174 {
175     struct rset_isam_info *info = (struct rset_isam_info *) ct->buf;
176
177     logf (LOG_DEBUG, "rsisam_delete");
178     assert (info->ispt_list == NULL);
179     rset_term_destroy (ct->rset_terms[0]);
180     xfree (ct->rset_terms);
181     xfree (info);
182 }
183
184 static void r_rewind (RSFD rfd)
185 {   
186     logf (LOG_DEBUG, "rsisam_rewind");
187     is_rewind( ((struct rset_ispt_info*) rfd)->pt);
188 }
189
190 static int r_count (RSET ct)
191 {
192     return 0;
193 }
194
195 static int r_read (RSFD rfd, void *buf, int *term_index)
196 {
197     *term_index = 0;
198     return is_readkey( ((struct rset_ispt_info*) rfd)->pt, buf);
199 }
200
201 static int r_write (RSFD rfd, const void *buf)
202 {
203     logf (LOG_FATAL, "ISAM set type is read-only");
204     return -1;
205 }