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