Bug fixes.
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rstemp.c,v $
7  * Revision 1.11  1995-09-18 14:17:56  adam
8  * Bug fixes.
9  *
10  * Revision 1.10  1995/09/15  14:45:39  adam
11  * Bug fixes.
12  *
13  * Revision 1.9  1995/09/15  09:20:42  adam
14  * Bug fixes.
15  *
16  * Revision 1.8  1995/09/08  14:52:42  adam
17  * Work on relevance feedback.
18  *
19  * Revision 1.7  1995/09/07  13:58:44  adam
20  * New parameter: result-set file descriptor (RSFD) to support multiple
21  * positions within the same result-set.
22  * Boolean operators: and, or, not implemented.
23  *
24  * Revision 1.6  1995/09/06  16:11:56  adam
25  * More work on boolean sets.
26  *
27  * Revision 1.5  1995/09/05  16:36:59  adam
28  * Minor changes.
29  *
30  * Revision 1.4  1995/09/05  11:43:24  adam
31  * Complete version of temporary sets. Not tested yet though.
32  *
33  * Revision 1.3  1995/09/04  15:20:40  adam
34  * More work on temp sets. is_open member removed.
35  *
36  * Revision 1.2  1995/09/04  09:10:56  adam
37  * Minor changes.
38  *
39  * Revision 1.1  1994/11/04  13:21:30  quinn
40  * Working.
41  *
42  */
43
44 #include <fcntl.h>
45 #include <assert.h>
46 #include <unistd.h>
47 #include <sys/types.h>
48 #include <stdio.h>
49
50 #include <alexutil.h>
51 #include <rstemp.h>
52
53 static rset_control *r_create(const struct rset_control *sel, void *parms);
54 static RSFD r_open (rset_control *ct, int wflag);
55 static void r_close (RSFD rfd);
56 static void r_delete (rset_control *ct);
57 static void r_rewind (RSFD rfd);
58 static int r_count (rset_control *ct);
59 static int r_read (RSFD rfd, void *buf);
60 static int r_write (RSFD rfd, const void *buf);
61
62 static const rset_control control = 
63 {
64     "Temporary set",
65     0,
66     r_create,
67     r_open,
68     r_close,
69     r_delete,
70     r_rewind,
71     r_count,
72     r_read,
73     r_write
74 };
75
76 const rset_control *rset_kind_temp = &control;
77
78 struct rset_temp_info {
79     int     fd;
80     char   *fname;
81     size_t  key_size;      /* key size */
82     char   *buf_mem;       /* window buffer */
83     size_t  buf_size;      /* size of window */
84     size_t  pos_end;       /* last position in set */
85     size_t  pos_cur;       /* current position in set */
86     size_t  pos_buf;       /* position of first byte in window */
87     size_t  pos_border;    /* position of last byte+1 in window */
88     int     dirty;         /* window is dirty */
89 };
90
91 struct rset_temp_rfd {
92     struct rset_temp_info *info;
93     struct rset_temp_rfd *next;
94 };
95
96 static struct rset_control *r_create(const struct rset_control *sel,
97                                      void *parms)
98 {
99     rset_control *newct;
100     rset_temp_parms *temp_parms = parms;
101     struct rset_temp_info *info;
102     
103     newct = xmalloc(sizeof(*newct));
104     memcpy(newct, sel, sizeof(*sel));
105     newct->buf = xmalloc (sizeof(struct rset_temp_info));
106     info = newct->buf;
107
108     info->fd = -1;
109     info->fname = NULL;
110     info->key_size = temp_parms->key_size;
111     info->buf_size = 4096;
112     info->buf_mem = xmalloc (info->buf_size);
113     info->pos_cur = 0;
114     info->pos_end = 0;
115     info->pos_buf = 0;
116     info->dirty = 0;
117
118     return newct;
119 }
120
121 static RSFD r_open (struct rset_control *ct, int wflag)
122 {
123     struct rset_temp_info *info = ct->buf;
124     struct rset_temp_rfd *rfd;
125
126     assert (info->fd == -1);
127     if (info->fname)
128     {
129         if (wflag)
130             info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
131         else
132             info->fd = open (info->fname, O_RDONLY);
133         if (info->fd == -1)
134         {
135             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
136             exit (1);
137         }
138     }
139     rfd = xmalloc (sizeof(*rfd));
140     rfd->info = info;
141     r_rewind (rfd);
142     return rfd;
143 }
144
145 /* r_flush:
146       flush current window to file if file is assocated with set
147  */
148 static void r_flush (RSFD rfd, int mk)
149 {
150     struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
151
152     if (!info->fname && mk)
153     {
154         char *s = (char*) tempnam (NULL, "zrs");
155
156         info->fname = xmalloc (strlen(s)+1);
157         strcpy (info->fname, s);
158
159         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
160         info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
161         if (info->fd == -1)
162         {
163             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
164             exit (1);
165         }
166     }
167     if (info->fname && info->fd != -1 && info->dirty)
168     {
169         size_t r, count;
170         
171         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
172         {
173             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
174             exit (1);
175         }
176         count = info->buf_size;
177         if (count > info->pos_end - info->pos_buf)
178             count = info->pos_end - info->pos_buf;
179         if ((r = write (info->fd, info->buf_mem, count)) < count)
180         {
181             if (r == -1)
182                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
183             else
184                 logf (LOG_FATAL, "write of %ld but got %ld",
185                       (long) count, (long) r);
186             exit (1);
187         }
188         info->dirty = 0;
189     }
190 }
191
192 static void r_close (RSFD rfd)
193 {
194     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
195
196     r_flush (rfd, 0);
197     if (info->fname && info->fd != -1)
198     {
199         close (info->fd);
200         info->fd = -1;
201     }
202 }
203
204 static void r_delete (struct rset_control *ct)
205 {
206     struct rset_temp_info *info = ct->buf;
207
208     if (info->fname)
209         unlink (info->fname);        
210     free (info->buf_mem);
211     free (info->fname);
212     free (info);
213 }
214
215 /* r_reread:
216       read from file to window if file is assocated with set -
217       indicated by fname
218  */
219 static void r_reread (RSFD rfd)
220 {
221     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
222
223     if (info->fname)
224     {
225         size_t r, count;
226
227         info->pos_border = info->pos_cur + info->buf_size;
228         if (info->pos_border > info->pos_end)
229             info->pos_border = info->pos_end;
230         count = info->pos_border - info->pos_buf;
231         if (count > 0)
232         {
233             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
234             {
235                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
236                 exit (1);
237             }
238             if ((r = read (info->fd, info->buf_mem, count)) < count)
239             {
240                 if (r == -1)
241                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
242                 else
243                     logf (LOG_FATAL, "read of %ld but got %ld",
244                           (long) count, (long) r);
245                 exit (1);
246             }
247         }
248     }
249     else
250         info->pos_border = info->pos_end;
251 }
252
253 static void r_rewind (RSFD rfd)
254 {
255     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
256
257     r_flush (rfd, 0);
258     info->pos_cur = 0;
259     info->pos_buf = 0;
260     r_reread (rfd);
261 }
262
263 static int r_count (struct rset_control *ct)
264 {
265     struct rset_temp_info *info = ct->buf;
266
267     return info->pos_end / info->key_size;
268 }
269
270 static int r_read (RSFD rfd, void *buf)
271 {
272     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
273
274     size_t nc = info->pos_cur + info->key_size;
275
276     if (nc > info->pos_border)
277     {
278         if (nc > info->pos_end)
279             return 0;
280         r_flush (rfd, 0);
281         info->pos_buf = info->pos_cur;
282         r_reread (rfd);
283     }
284     memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
285             info->key_size);
286     info->pos_cur = nc;
287     return 1;
288 }
289
290 static int r_write (RSFD rfd, const void *buf)
291 {
292     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
293
294     size_t nc = info->pos_cur + info->key_size;
295
296     if (nc > info->pos_buf + info->buf_size)
297     {
298         r_flush (rfd, 1);
299         info->pos_buf = info->pos_cur;
300         if (info->pos_buf < info->pos_end)
301             r_reread (rfd);
302     }
303     info->dirty = 1;
304     memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
305             info->key_size);
306     info->pos_cur = nc;
307     if (nc > info->pos_end)
308         info->pos_border = info->pos_end = nc;
309     return 1;
310 }
311