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