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