Fixed a crash with isam-c and multior (incremented a non-existing countp)
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.44 2004-08-24 08:52:30 heikki 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);
44 static int r_write (RSFD rfd, const void *buf);
45 static void r_pos (RSFD rfd, double *current, double  *total);
46
47 static const struct rset_control control = 
48 {
49     "temp",
50     r_create,
51     r_open,
52     r_close,
53     r_delete,
54     r_rewind,
55     rset_default_forward,
56     r_pos, 
57     r_read,
58     r_write,
59 };
60
61 const struct rset_control *rset_kind_temp = &control;
62
63 struct rset_temp_info {
64     int     fd;
65     char   *fname;
66     size_t  key_size;      /* key size */
67     char   *buf_mem;       /* window buffer */
68     size_t  buf_size;      /* size of window */
69     size_t  pos_end;       /* last position in set */
70     size_t  pos_buf;       /* position of first byte in window */
71     size_t  pos_border;    /* position of last byte+1 in window */
72     int     dirty;         /* window is dirty */
73     zint     hits;          /* no of hits */
74     char   *temp_path;
75     int     (*cmp)(const void *p1, const void *p2);
76     struct rset_temp_rfd *rfd_list;
77 };
78
79 struct rset_temp_rfd {
80     struct rset_temp_info *info;
81     struct rset_temp_rfd *next;
82     void *buf;
83     size_t  pos_cur;       /* current position in set */
84     zint cur; /* number of the current hit */
85 };
86
87 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
88 {
89     rset_temp_parms *temp_parms = (rset_temp_parms *) parms;
90     struct rset_temp_info *info;
91    
92     info = (struct rset_temp_info *) xmalloc (sizeof(struct rset_temp_info));
93     info->fd = -1;
94     info->fname = NULL;
95     info->key_size = temp_parms->key_size;
96     info->buf_size = 4096;
97     info->buf_mem = (char *) xmalloc (info->buf_size);
98     info->pos_end = 0;
99     info->pos_buf = 0;
100     info->dirty = 0;
101     info->hits = 0;
102     info->cmp = temp_parms->cmp;
103     info->rfd_list = NULL;
104
105     if (!temp_parms->temp_path)
106         info->temp_path = NULL;
107     else
108     {
109         info->temp_path = (char *) xmalloc (strlen(temp_parms->temp_path)+1);
110         strcpy (info->temp_path, temp_parms->temp_path);
111     }
112
113     return info;
114 }
115
116 static RSFD r_open (RSET ct, int flag)
117 {
118     struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
119     struct rset_temp_rfd *rfd;
120
121     if (info->fd == -1 && info->fname)
122     {
123         if (flag & RSETF_WRITE)
124             info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
125         else
126             info->fd = open (info->fname, O_BINARY|O_RDONLY);
127         if (info->fd == -1)
128         {
129             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
130             exit (1);
131         }
132     }
133     rfd = (struct rset_temp_rfd *) xmalloc (sizeof(*rfd));
134     rfd->next = info->rfd_list;
135     info->rfd_list = rfd;
136     rfd->info = info;
137     r_rewind (rfd);
138
139     rfd->buf = xmalloc (info->key_size);
140
141     return rfd;
142 }
143
144 /* r_flush:
145       flush current window to file if file is assocated with set
146  */
147 static void r_flush (RSFD rfd, int mk)
148 {
149     struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
150
151     if (!info->fname && mk)
152     {
153 #if HAVE_MKSTEMP
154         char template[1024];
155
156         if (info->temp_path)
157             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
158         else
159             sprintf (template, "zrsXXXXXX");
160
161         info->fd = mkstemp (template);
162
163         if (info->fd == -1)
164         {
165             logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
166             exit (1);
167         }
168         info->fname = (char *) xmalloc (strlen(template)+1);
169         strcpy (info->fname, template);
170 #else
171         char *s = (char*) tempnam (info->temp_path, "zrs");
172         info->fname = (char *) xmalloc (strlen(s)+1);
173         strcpy (info->fname, s);
174
175         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
176         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
177         if (info->fd == -1)
178         {
179             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
180             exit (1);
181         }
182 #endif
183     }
184     if (info->fname && info->fd != -1 && info->dirty)
185     {
186         size_t count;
187         int r;
188         
189         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
190         {
191             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
192             exit (1);
193         }
194         count = info->buf_size;
195         if (count > info->pos_end - info->pos_buf)
196             count = info->pos_end - info->pos_buf;
197         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
198         {
199             if (r == -1)
200                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
201             else
202                 logf (LOG_FATAL, "write of %ld but got %ld",
203                       (long) count, (long) r);
204             exit (1);
205         }
206         info->dirty = 0;
207     }
208 }
209
210 static void r_close (RSFD rfd)
211 {
212     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
213     struct rset_temp_rfd **rfdp;
214
215     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
216         if (*rfdp == rfd)
217         {
218             r_flush (*rfdp, 0);
219             xfree ((*rfdp)->buf);
220
221             *rfdp = (*rfdp)->next;
222             xfree (rfd);
223
224             if (!info->rfd_list && info->fname && info->fd != -1)
225             {
226                 close (info->fd);
227                 info->fd = -1;
228             }
229             return;
230         }
231     logf (LOG_FATAL, "r_close but no rfd match!");
232     assert (0);
233 }
234
235 static void r_delete (RSET ct)
236 {
237     struct rset_temp_info *info = (struct rset_temp_info*) ct->buf;
238
239     if (info->fname)
240         unlink (info->fname);        
241     xfree (info->buf_mem);
242     logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
243     if (info->fname)
244     {
245         logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
246         unlink (info->fname);
247         xfree (info->fname);
248     }
249     if (info->temp_path)
250         xfree (info->temp_path);
251     xfree (info);
252 }
253
254 /* r_reread:
255       read from file to window if file is assocated with set -
256       indicated by fname
257  */
258 static void r_reread (RSFD rfd)
259 {
260     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
261
262     if (info->fname)
263     {
264         size_t count;
265         int r;
266
267         info->pos_border = ((struct rset_temp_rfd *)rfd)->pos_cur +
268             info->buf_size;
269         if (info->pos_border > info->pos_end)
270             info->pos_border = info->pos_end;
271         count = info->pos_border - info->pos_buf;
272         if (count > 0)
273         {
274             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
275             {
276                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
277                 exit (1);
278             }
279             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
280             {
281                 if (r == -1)
282                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
283                 else
284                     logf (LOG_FATAL, "read of %ld but got %ld",
285                           (long) count, (long) r);
286                 exit (1);
287             }
288         }
289     }
290     else
291         info->pos_border = info->pos_end;
292 }
293
294 static void r_rewind (RSFD rfd)
295 {
296     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
297
298     r_flush (rfd, 0);
299     ((struct rset_temp_rfd *)rfd)->pos_cur = 0;
300     info->pos_buf = 0;
301     r_reread (rfd);
302     ((struct rset_temp_rfd *)rfd)->cur=0;
303 }
304
305 /*
306 static int r_count (RSET ct)
307 {
308     struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
309
310     return info->pos_end / info->key_size;
311 }
312 */
313 static int r_read (RSFD rfd, void *buf)
314 {
315     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
316     struct rset_temp_info *info = mrfd->info;
317
318     size_t nc = mrfd->pos_cur + info->key_size;
319
320     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
321     {
322         if (nc > info->pos_end)
323             return 0;
324         r_flush (rfd, 0);
325         info->pos_buf = mrfd->pos_cur;
326         r_reread (rfd);
327     }
328     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
329             info->key_size);
330     mrfd->pos_cur = nc;
331     mrfd->cur++;
332     return 1;
333 }
334
335 static int r_write (RSFD rfd, const void *buf)
336 {
337     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
338     struct rset_temp_info *info = mrfd->info;
339
340     size_t nc = mrfd->pos_cur + info->key_size;
341
342     if (nc > info->pos_buf + info->buf_size)
343     {
344         r_flush (rfd, 1);
345         info->pos_buf = mrfd->pos_cur;
346         if (info->pos_buf < info->pos_end)
347             r_reread (rfd);
348     }
349     info->dirty = 1;
350     memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
351             info->key_size);
352     mrfd->pos_cur = nc;
353     if (nc > info->pos_end)
354         info->pos_border = info->pos_end = nc;
355     info->hits++;
356     return 1;
357 }
358
359 static void r_pos (RSFD rfd, double  *current, double  *total)
360 {
361     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
362     *current=(double) mrfd->cur;
363     *total=(double) mrfd->info->hits;
364 }