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