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