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