Major restructuring in rsets.
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.47 2004-08-31 10:43:40 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 RSFD r_open (RSET ct, int flag);
38 static void r_close (RSFD rfd);
39 static void r_delete (RSET ct);
40 static void r_rewind (RSFD rfd);
41 static int r_read (RSFD rfd, void *buf);
42 static int r_write (RSFD rfd, const void *buf);
43 static void r_pos (RSFD rfd, double *current, double  *total);
44
45
46 static const struct rset_control control = 
47 {
48     "temp",
49     r_delete,
50     r_open,
51     r_close,
52     r_rewind,
53     rset_default_forward,
54     r_pos, 
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     zint     hits;          /* no of hits */
72     char   *temp_path;
73     int     (*cmp)(const void *p1, const void *p2);
74 };
75
76 struct rset_temp_rfd {
77     void *buf;
78     size_t  pos_cur;       /* current position in set */
79                            /* FIXME - term pos or what ??  */
80     zint cur; /* number of the current hit */
81 };
82
83 RSET rstemp_create( NMEM nmem, int key_size, 
84                     int (*cmp)(const void *p1, const void *p2),
85                     const char *temp_path)
86 {
87     RSET rnew=rset_create_base(&control, nmem);
88     struct rset_temp_info *info;
89    
90     info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
91     info->fd = -1;
92     info->fname = NULL;
93     info->key_size = key_size;
94     info->buf_size = 4096;
95     info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
96     info->pos_end = 0;
97     info->pos_buf = 0;
98     info->dirty = 0;
99     info->hits = 0;
100     info->cmp = cmp;
101
102     if (!temp_path)
103         info->temp_path = NULL;
104     else
105         info->temp_path = nmem_strdup(rnew->nmem,temp_path);
106     rnew->priv=info; 
107     return rnew;
108 } /* rstemp_create */
109
110 static void r_delete (RSET ct)
111 {
112     struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
113
114     xfree (info->buf_mem);
115     logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
116     if (info->fname)
117     {
118         logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
119         unlink (info->fname);
120         xfree (info->fname);  /* FIXME should be nmem'd, and not freed here */
121     }
122     /*
123     if (info->temp_path)
124         xfree (info->temp_path);
125     xfree (info);
126     */  /* nmem'd */
127 }
128
129
130 static RSFD r_open (RSET ct, int flag)
131 {
132     struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
133     RSFD rfd;
134     struct rset_temp_rfd *prfd;
135
136     if (info->fd == -1 && info->fname)
137     {
138         if (flag & RSETF_WRITE)
139             info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
140         else
141             info->fd = open (info->fname, O_BINARY|O_RDONLY);
142         if (info->fd == -1)
143         {
144             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
145             exit (1);
146         }
147     }
148     rfd = rfd_create_base(ct);
149     if (!rfd->priv){
150         prfd= (struct rset_temp_rfd *) nmem_malloc(ct->nmem, sizeof(*prfd));
151         rfd->priv=(void *)prfd;
152         prfd->buf = nmem_malloc (ct->nmem,info->key_size);
153     }
154     r_rewind (rfd);
155     return rfd;
156 }
157
158 /* r_flush:
159       flush current window to file if file is assocated with set
160  */
161 static void r_flush (RSFD rfd, int mk)
162 {
163     /* struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info; */
164     struct rset_temp_info *info = rfd->rset->priv;
165
166     if (!info->fname && mk)
167     {
168 #if HAVE_MKSTEMP
169         char template[1024];
170
171         if (info->temp_path)
172             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
173         else
174             sprintf (template, "zrsXXXXXX");
175
176         info->fd = mkstemp (template);
177
178         if (info->fd == -1)
179         {
180             logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
181             exit (1);
182         }
183         info->fname = (char *) xmalloc (strlen(template)+1);
184         strcpy (info->fname, template);
185 #else
186         char *s = (char*) tempnam (info->temp_path, "zrs");
187         info->fname = (char *) xmalloc (strlen(s)+1);
188         strcpy (info->fname, s);
189
190         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
191         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
192         if (info->fd == -1)
193         {
194             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
195             exit (1);
196         }
197 #endif
198     }
199     if (info->fname && info->fd != -1 && info->dirty)
200     {
201         size_t count;
202         int r;
203         
204         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
205         {
206             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
207             exit (1);
208         }
209         count = info->buf_size;
210         if (count > info->pos_end - info->pos_buf)
211             count = info->pos_end - info->pos_buf;
212         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
213         {
214             if (r == -1)
215                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
216             else
217                 logf (LOG_FATAL, "write of %ld but got %ld",
218                       (long) count, (long) r);
219             exit (1);
220         }
221         info->dirty = 0;
222     }
223 }
224
225 static void r_close (RSFD rfd)
226 {
227     /*struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; */
228     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
229     r_flush (rfd, 0);
230     if (info->fname && info->fd != -1)
231     {
232         close (info->fd);
233         info->fd = -1;
234     } /* FIXME - Is this right, don't we risk closing the file too early ?*/
235     rfd_delete_base(rfd);
236 }
237
238
239 /* r_reread:
240       read from file to window if file is assocated with set -
241       indicated by fname
242  */
243 static void r_reread (RSFD rfd)
244 {
245     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; 
246     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
247
248     if (info->fname)
249     {
250         size_t count;
251         int r;
252
253         info->pos_border = mrfd->pos_cur +
254             info->buf_size;
255         if (info->pos_border > info->pos_end)
256             info->pos_border = info->pos_end;
257         count = info->pos_border - info->pos_buf;
258         if (count > 0)
259         {
260             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
261             {
262                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
263                 exit (1);
264             }
265             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
266             {
267                 if (r == -1)
268                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
269                 else
270                     logf (LOG_FATAL, "read of %ld but got %ld",
271                           (long) count, (long) r);
272                 exit (1);
273             }
274         }
275     }
276     else
277         info->pos_border = info->pos_end;
278 }
279
280 static void r_rewind (RSFD rfd)
281 {
282     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) (rfd->priv);  
283     struct rset_temp_info *info = (struct rset_temp_info *)(rfd->rset->priv);
284     r_flush (rfd, 0);
285     mrfd->pos_cur = 0;
286     info->pos_buf = 0;
287     r_reread (rfd);
288     mrfd->cur=0;
289 }
290
291 static int r_read (RSFD rfd, void *buf)
292 {
293     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
294     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
295
296     size_t nc = mrfd->pos_cur + info->key_size;
297
298     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
299     {
300         if (nc > info->pos_end)
301             return 0;
302         r_flush (rfd, 0);
303         info->pos_buf = mrfd->pos_cur;
304         r_reread (rfd);
305     }
306     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
307             info->key_size);
308     mrfd->pos_cur = nc;
309     mrfd->cur++;
310     return 1;
311 }
312
313 static int r_write (RSFD rfd, const void *buf)
314 {
315     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
316     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
317
318     size_t nc = mrfd->pos_cur + info->key_size;
319
320     if (nc > info->pos_buf + info->buf_size)
321     {
322         r_flush (rfd, 1);
323         info->pos_buf = mrfd->pos_cur;
324         if (info->pos_buf < info->pos_end)
325             r_reread (rfd);
326     }
327     info->dirty = 1;
328     memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
329             info->key_size);
330     mrfd->pos_cur = nc;
331     if (nc > info->pos_end)
332         info->pos_border = info->pos_end = nc;
333     info->hits++;
334     return 1;
335 }
336
337 static void r_pos (RSFD rfd, double  *current, double  *total)
338 {
339     /* struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd; */
340     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
341     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
342     
343     *current=(double) mrfd->cur;
344     *total=(double) info->hits;
345 }