Implemented Multi-or result set.
[idzebra-moved-to-github.git] / rset / rsm_or.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsm_or.c,v $
7  * Revision 1.1  1996-12-20 11:07:21  adam
8  * Implemented Multi-or result set.
9  *
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <assert.h>
15
16 #include <isam.h>
17 #include <isamc.h>
18 #include <rsm_or.h>
19 #include <zebrautl.h>
20
21 static void *r_create(const struct rset_control *sel, void *parms,
22                       int *flags);
23 static RSFD r_open (RSET ct, int flag);
24 static void r_close (RSFD rfd);
25 static void r_delete (RSET ct);
26 static void r_rewind (RSFD rfd);
27 static int r_count (RSET ct);
28 static int r_read (RSFD rfd, void *buf);
29 static int r_write (RSFD rfd, const void *buf);
30 static int r_score (RSFD rfd, int *score);
31
32 static const rset_control control = 
33 {
34     "multi-or",
35     r_create,
36     r_open,
37     r_close,
38     r_delete,
39     r_rewind,
40     r_count,
41     r_read,
42     r_write,
43     r_score
44 };
45
46 const rset_control *rset_kind_m_or = &control;
47
48 struct rset_mor_info {
49     int     key_size;
50     int     no_rec;
51     int     (*cmp)(const void *p1, const void *p2);
52     ISAMC   isc;
53     ISAM_P  *isam_positions;
54
55     int     no_isam_positions;
56     struct rset_mor_rfd *rfd_list;
57 };
58
59 struct trunc_info {
60     int  *ptr;
61     int  *indx;
62     char **heap;
63     int  heapnum;
64     int  (*cmp)(const void *p1, const void *p2);
65     int  keysize;
66     char *swapbuf;
67     char *tmpbuf;
68     char *buf;
69 };
70
71 struct rset_mor_rfd {
72     int flag;
73     ISAMC_PP *ispt;
74     struct rset_mor_rfd *next;
75     struct rset_mor_info *info;
76     struct trunc_info *ti;
77 };
78
79 static void heap_swap (struct trunc_info *ti, int i1, int i2)
80 {
81     int swap;
82
83     swap = ti->ptr[i1];
84     ti->ptr[i1] = ti->ptr[i2];
85     ti->ptr[i2] = swap;
86 }
87
88 static void heap_delete (struct trunc_info *ti)
89 {
90     int cur = 1, child = 2;
91
92     heap_swap (ti, 1, ti->heapnum--);
93     while (child <= ti->heapnum) {
94         if (child < ti->heapnum &&
95             (*ti->cmp)(ti->heap[ti->ptr[child]],
96                        ti->heap[ti->ptr[1+child]]) > 0)
97             child++;
98         if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
99                        ti->heap[ti->ptr[child]]) > 0)
100         {
101             heap_swap (ti, cur, child);
102             cur = child;
103             child = 2*cur;
104         }
105         else
106             break;
107     }
108 }
109
110 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
111 {
112     int cur, parent;
113
114     cur = ++(ti->heapnum);
115     memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
116     ti->indx[ti->ptr[cur]] = indx;
117     parent = cur/2;
118     while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
119                                 ti->heap[ti->ptr[cur]]) > 0)
120     {
121         heap_swap (ti, cur, parent);
122         cur = parent;
123         parent = cur/2;
124     }
125 }
126
127 static
128 struct trunc_info *heap_init (int size, int key_size,
129                               int (*cmp)(const void *p1, const void *p2))
130 {
131     struct trunc_info *ti = xmalloc (sizeof(*ti));
132     int i;
133
134     ++size;
135     ti->heapnum = 0;
136     ti->keysize = key_size;
137     ti->cmp = cmp;
138     ti->indx = xmalloc (size * sizeof(*ti->indx));
139     ti->heap = xmalloc (size * sizeof(*ti->heap));
140     ti->ptr = xmalloc (size * sizeof(*ti->ptr));
141     ti->swapbuf = xmalloc (ti->keysize);
142     ti->tmpbuf = xmalloc (ti->keysize);
143     ti->buf = xmalloc (size * ti->keysize);
144     for (i = size; --i >= 0; )
145     {
146         ti->ptr[i] = i;
147         ti->heap[i] = ti->buf + ti->keysize * i;
148     }
149     return ti;
150 }
151
152 static void heap_close (struct trunc_info *ti)
153 {
154     xfree (ti->ptr);
155     xfree (ti->indx);
156     xfree (ti->heap);
157     xfree (ti->swapbuf);
158     xfree (ti->tmpbuf);
159     xfree (ti);
160 }
161
162
163 static void *r_create (const struct rset_control *sel, void *parms,
164                        int *flags)
165 {
166     rset_m_or_parms *r_parms = parms;
167     struct rset_mor_info *info;
168
169     *flags |= RSET_FLAG_VOLATILE;
170     info = xmalloc (sizeof(*info));
171     info->key_size = r_parms->key_size;
172     assert (info->key_size > 1);
173     info->cmp = r_parms->cmp;
174
175     info->isc = r_parms->isc;
176     info->no_isam_positions = r_parms->no_isam_positions;
177     info->isam_positions = xmalloc (sizeof(*info->isam_positions) *
178                                     info->no_isam_positions);
179     memcpy (info->isam_positions, r_parms->isam_positions,
180             sizeof(*info->isam_positions) * info->no_isam_positions);
181     info->rfd_list = NULL;
182
183     return info;
184 }
185
186 static RSFD r_open (RSET ct, int flag)
187 {
188     struct rset_mor_rfd *rfd;
189     struct rset_mor_info *info = ct->buf;
190     int i;
191
192     if (flag & RSETF_WRITE)
193     {
194         logf (LOG_FATAL, "m_or set type is read-only");
195         return NULL;
196     }
197     rfd = xmalloc (sizeof(*rfd));
198     rfd->flag = flag;
199     rfd->next = info->rfd_list;
200     rfd->info = info;
201     info->rfd_list = rfd;
202
203     rfd->ispt = xmalloc (sizeof(*rfd->ispt) * info->no_isam_positions);
204         
205     rfd->ti = heap_init (info->no_isam_positions, info->key_size, info->cmp);
206
207     for (i = 0; i<info->no_isam_positions; i++)
208     {
209         rfd->ispt[i] = isc_pp_open (info->isc, info->isam_positions[i]);
210         if (isc_pp_read (rfd->ispt[i], rfd->ti->tmpbuf))
211             heap_insert (rfd->ti, rfd->ti->tmpbuf, i);
212         else
213         {
214             isc_pp_close (rfd->ispt[i]);
215             rfd->ispt[i] = NULL;
216         }
217     }
218     r_rewind (rfd);
219     return rfd;
220 }
221
222 static void r_close (RSFD rfd)
223 {
224     struct rset_mor_info *info = ((struct rset_mor_rfd*)rfd)->info;
225     struct rset_mor_rfd **rfdp;
226     int i;
227     
228     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
229         if (*rfdp == rfd)
230         {
231             *rfdp = (*rfdp)->next;
232         
233             heap_close (((struct rset_mor_rfd *) rfd)->ti);
234             for (i = 0; i<info->no_isam_positions; i++)
235                 if (((struct rset_mor_rfd *) rfd)->ispt[i])
236                     isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[i]);
237             free (((struct rset_mor_rfd *)rfd)->ispt);
238             free (rfd);
239             return;
240         }
241     logf (LOG_FATAL, "r_close but no rfd match!");
242     assert (0);
243 }
244
245 static void r_delete (RSET ct)
246 {
247     struct rset_mor_info *info = ct->buf;
248
249     assert (info->rfd_list == NULL);
250     xfree (info->isam_positions);
251     xfree (info);
252 }
253
254 static void r_rewind (RSFD rfd)
255 {
256 }
257
258 static int r_count (RSET ct)
259 {
260     return 0;
261 }
262
263 static int r_read (RSFD rfd, void *buf)
264 {
265     struct trunc_info *ti = ((struct rset_mor_rfd *) rfd)->ti;
266     int n = ti->indx[ti->ptr[1]];
267
268     if (!ti->heapnum)
269         return 0;
270     memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize);
271     heap_delete (ti);
272     if (isc_pp_read (((struct rset_mor_rfd *) rfd)->ispt[n], ti->tmpbuf))
273         heap_insert (ti, ti->tmpbuf, n);
274     else
275     {
276         isc_pp_close (((struct rset_mor_rfd *) rfd)->ispt[n]);
277         ((struct rset_mor_rfd*) rfd)->ispt[n] = NULL;
278     }
279     return 1;
280 }
281
282 static int r_score (RSFD rfd, int *score)
283 {
284     *score = -1;
285     return -1;
286 }
287
288 static int r_write (RSFD rfd, const void *buf)
289 {
290     logf (LOG_FATAL, "mor set type is read-only");
291     return -1;
292 }