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