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