Allow multiple open instances of rstemp
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.35 2003-03-31 21:53:40 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <fcntl.h>
24 #include <assert.h>
25 #ifdef WIN32
26 #include <io.h>
27 #else
28 #include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <sys/types.h>
32 #include <stdio.h>
33
34 #include <zebrautl.h>
35 #include <rstemp.h>
36
37 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
38 static RSFD r_open (RSET ct, int flag);
39 static void r_close (RSFD rfd);
40 static void r_delete (RSET ct);
41 static void r_rewind (RSFD rfd);
42 static int r_count (RSET ct);
43 static int r_read (RSFD rfd, void *buf, int *term_index);
44 static int r_write (RSFD rfd, const void *buf);
45
46 static const struct rset_control control = 
47 {
48     "temp",
49     r_create,
50     r_open,
51     r_close,
52     r_delete,
53     r_rewind,
54     r_count,
55     r_read,
56     r_write,
57 };
58
59 const struct rset_control *rset_kind_temp = &control;
60
61 struct rset_temp_info {
62     int     fd;
63     char   *fname;
64     size_t  key_size;      /* key size */
65     char   *buf_mem;       /* window buffer */
66     size_t  buf_size;      /* size of window */
67     size_t  pos_end;       /* last position in set */
68     size_t  pos_buf;       /* position of first byte in window */
69     size_t  pos_border;    /* position of last byte+1 in window */
70     int     dirty;         /* window is dirty */
71     int     hits;          /* no of hits */
72     char   *temp_path;
73     int     (*cmp)(const void *p1, const void *p2);
74     struct rset_temp_rfd *rfd_list;
75 };
76
77 struct rset_temp_rfd {
78     struct rset_temp_info *info;
79     struct rset_temp_rfd *next;
80     int *countp;
81     void *buf;
82     size_t  pos_cur;       /* current position in set */
83 };
84
85 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
86 {
87     rset_temp_parms *temp_parms = (rset_temp_parms *) parms;
88     struct rset_temp_info *info;
89    
90     info = (struct rset_temp_info *) xmalloc (sizeof(struct rset_temp_info));
91     info->fd = -1;
92     info->fname = NULL;
93     info->key_size = temp_parms->key_size;
94     info->buf_size = 4096;
95     info->buf_mem = (char *) xmalloc (info->buf_size);
96     info->pos_end = 0;
97     info->pos_buf = 0;
98     info->dirty = 0;
99     info->hits = -1;
100     info->cmp = temp_parms->cmp;
101     info->rfd_list = NULL;
102
103     if (!temp_parms->temp_path)
104         info->temp_path = NULL;
105     else
106     {
107         info->temp_path = (char *) xmalloc (strlen(temp_parms->temp_path)+1);
108         strcpy (info->temp_path, temp_parms->temp_path);
109     }
110     ct->no_rset_terms = 1;
111     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
112     ct->rset_terms[0] = temp_parms->rset_term;
113
114     return info;
115 }
116
117 static RSFD r_open (RSET ct, int flag)
118 {
119     struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
120     struct rset_temp_rfd *rfd;
121
122     if (info->fd == -1 && info->fname)
123     {
124         if (flag & RSETF_WRITE)
125             info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
126         else
127             info->fd = open (info->fname, O_BINARY|O_RDONLY);
128         if (info->fd == -1)
129         {
130             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
131             exit (1);
132         }
133     }
134     rfd = (struct rset_temp_rfd *) xmalloc (sizeof(*rfd));
135     rfd->next = info->rfd_list;
136     info->rfd_list = rfd;
137     rfd->info = info;
138     r_rewind (rfd);
139
140     rfd->countp = &ct->rset_terms[0]->count;
141     *rfd->countp = 0;
142     rfd->buf = xmalloc (info->key_size);
143
144     return rfd;
145 }
146
147 /* r_flush:
148       flush current window to file if file is assocated with set
149  */
150 static void r_flush (RSFD rfd, int mk)
151 {
152     struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
153
154     if (!info->fname && mk)
155     {
156 #if HAVE_MKSTEMP
157         char template[1024];
158
159         if (info->temp_path)
160             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
161         else
162             sprintf (template, "zrsXXXXXX");
163
164         info->fd = mkstemp (template);
165
166         if (info->fd == -1)
167         {
168             logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
169             exit (1);
170         }
171         info->fname = (char *) xmalloc (strlen(template)+1);
172         strcpy (info->fname, template);
173 #else
174         char *s = (char*) tempnam (info->temp_path, "zrs");
175         info->fname = (char *) xmalloc (strlen(s)+1);
176         strcpy (info->fname, s);
177
178         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
179         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
180         if (info->fd == -1)
181         {
182             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
183             exit (1);
184         }
185 #endif
186     }
187     if (info->fname && info->fd != -1 && info->dirty)
188     {
189         size_t count;
190         int r;
191         
192         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
193         {
194             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
195             exit (1);
196         }
197         count = info->buf_size;
198         if (count > info->pos_end - info->pos_buf)
199             count = info->pos_end - info->pos_buf;
200         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
201         {
202             if (r == -1)
203                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
204             else
205                 logf (LOG_FATAL, "write of %ld but got %ld",
206                       (long) count, (long) r);
207             exit (1);
208         }
209         info->dirty = 0;
210     }
211 }
212
213 static void r_close (RSFD rfd)
214 {
215     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
216     struct rset_temp_rfd **rfdp;
217
218     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
219         if (*rfdp == rfd)
220         {
221             r_flush (*rfdp, 0);
222             xfree ((*rfdp)->buf);
223
224             *rfdp = (*rfdp)->next;
225             xfree (rfd);
226
227             if (!info->rfd_list && info->fname && info->fd != -1)
228             {
229                 close (info->fd);
230                 info->fd = -1;
231             }
232             return;
233         }
234     logf (LOG_FATAL, "r_close but no rfd match!");
235     assert (0);
236 }
237
238 static void r_delete (RSET ct)
239 {
240     struct rset_temp_info *info = (struct rset_temp_info*) ct->buf;
241
242     if (info->fname)
243         unlink (info->fname);        
244     xfree (info->buf_mem);
245     logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
246     if (info->fname)
247     {
248         logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
249         unlink (info->fname);
250         xfree (info->fname);
251     }
252     if (info->temp_path)
253         xfree (info->temp_path);
254     rset_term_destroy (ct->rset_terms[0]);
255     xfree (ct->rset_terms);
256     xfree (info);
257 }
258
259 /* r_reread:
260       read from file to window if file is assocated with set -
261       indicated by fname
262  */
263 static void r_reread (RSFD rfd)
264 {
265     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
266
267     if (info->fname)
268     {
269         size_t count;
270         int r;
271
272         info->pos_border = ((struct rset_temp_rfd *)rfd)->pos_cur +
273             info->buf_size;
274         if (info->pos_border > info->pos_end)
275             info->pos_border = info->pos_end;
276         count = info->pos_border - info->pos_buf;
277         if (count > 0)
278         {
279             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
280             {
281                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
282                 exit (1);
283             }
284             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
285             {
286                 if (r == -1)
287                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
288                 else
289                     logf (LOG_FATAL, "read of %ld but got %ld",
290                           (long) count, (long) r);
291                 exit (1);
292             }
293         }
294     }
295     else
296         info->pos_border = info->pos_end;
297 }
298
299 static void r_rewind (RSFD rfd)
300 {
301     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
302
303     r_flush (rfd, 0);
304     ((struct rset_temp_rfd *)rfd)->pos_cur = 0;
305     info->pos_buf = 0;
306     r_reread (rfd);
307 }
308
309 static int r_count (RSET ct)
310 {
311     struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
312
313     return info->pos_end / info->key_size;
314 }
315
316 static int r_read (RSFD rfd, void *buf, int *term_index)
317 {
318     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
319     struct rset_temp_info *info = mrfd->info;
320
321     size_t nc = mrfd->pos_cur + info->key_size;
322
323     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
324     {
325         if (nc > info->pos_end)
326             return 0;
327         r_flush (rfd, 0);
328         info->pos_buf = mrfd->pos_cur;
329         r_reread (rfd);
330     }
331     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
332             info->key_size);
333     mrfd->pos_cur = nc;
334     *term_index = 0;
335
336     if (*mrfd->countp == 0 || (*info->cmp)(buf, mrfd->buf) > 1)
337     {
338         memcpy (mrfd->buf, buf, mrfd->info->key_size);
339         (*mrfd->countp)++;
340     }
341     return 1;
342 }
343
344 static int r_write (RSFD rfd, const void *buf)
345 {
346     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
347     struct rset_temp_info *info = mrfd->info;
348
349     size_t nc = mrfd->pos_cur + info->key_size;
350
351     if (nc > info->pos_buf + info->buf_size)
352     {
353         r_flush (rfd, 1);
354         info->pos_buf = mrfd->pos_cur;
355         if (info->pos_buf < info->pos_end)
356             r_reread (rfd);
357     }
358     info->dirty = 1;
359     memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
360             info->key_size);
361     mrfd->pos_cur = nc;
362     if (nc > info->pos_end)
363         info->pos_border = info->pos_end = nc;
364     return 1;
365 }