afdeb54fb6b114aaff7378e685ea696ccc3cac63
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.48 2004-09-01 15:01:32 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 <rset.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;            /* file descriptor for temp file */
63     char   *fname;         /* name of temp file */
64     char   *buf_mem;       /* window buffer */
65     size_t  buf_size;      /* size of window */
66     size_t  pos_end;       /* last position in set */
67     size_t  pos_buf;       /* position of first byte in window */
68     size_t  pos_border;    /* position of last byte+1 in window */
69     int     dirty;         /* window is dirty */
70     zint     hits;          /* no of hits */
71     char   *temp_path;
72 };
73
74 struct rset_temp_rfd {
75     void *buf;
76     size_t  pos_cur;       /* current position in set */
77                            /* FIXME - term pos or what ??  */
78     zint cur; /* number of the current hit */
79 };
80
81 RSET rstemp_create( NMEM nmem, const struct key_control *kcontrol,
82                     const char *temp_path)
83 {
84     RSET rnew=rset_create_base(&control, nmem, kcontrol);
85     struct rset_temp_info *info;
86    
87     info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
88     info->fd = -1;
89     info->fname = NULL;
90     info->buf_size = 4096;
91     info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
92     info->pos_end = 0;
93     info->pos_buf = 0;
94     info->dirty = 0;
95     info->hits = 0;
96
97     if (!temp_path)
98         info->temp_path = NULL;
99     else
100         info->temp_path = nmem_strdup(rnew->nmem,temp_path);
101     rnew->priv=info; 
102     return rnew;
103 } /* rstemp_create */
104
105 static void r_delete (RSET ct)
106 {
107     struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
108
109     logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
110     if (info->fname)
111     {
112         logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
113         unlink (info->fname);
114     }
115 }
116
117
118 static RSFD r_open (RSET ct, int flag)
119 {
120     struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
121     RSFD rfd;
122     struct rset_temp_rfd *prfd;
123
124     if (info->fd == -1 && info->fname)
125     {
126         if (flag & RSETF_WRITE)
127             info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
128         else
129             info->fd = open (info->fname, O_BINARY|O_RDONLY);
130         if (info->fd == -1)
131         {
132             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
133             exit (1);
134         }
135     }
136     rfd = rfd_create_base(ct);
137     if (!rfd->priv){
138         prfd= (struct rset_temp_rfd *) nmem_malloc(ct->nmem, sizeof(*prfd));
139         rfd->priv=(void *)prfd;
140         prfd->buf = nmem_malloc (ct->nmem,ct->keycontrol->key_size);
141     }
142     r_rewind (rfd);
143     return rfd;
144 }
145
146 /* r_flush:
147       flush current window to file if file is assocated with set
148  */
149 static void r_flush (RSFD rfd, int mk)
150 {
151     /* struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info; */
152     struct rset_temp_info *info = rfd->rset->priv;
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= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
172         strcpy (info->fname, template);
173 #else
174         char *s = (char*) tempnam (info->temp_path, "zrs");
175         info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+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_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; */
216     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
217     r_flush (rfd, 0);
218     if (info->fname && info->fd != -1)
219     {
220         close (info->fd);
221         info->fd = -1;
222     } /* FIXME - Is this right, don't we risk closing the file too early ?*/
223     rfd_delete_base(rfd);
224 }
225
226
227 /* r_reread:
228       read from file to window if file is assocated with set -
229       indicated by fname
230  */
231 static void r_reread (RSFD rfd)
232 {
233     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; 
234     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
235
236     if (info->fname)
237     {
238         size_t count;
239         int r;
240
241         info->pos_border = mrfd->pos_cur +
242             info->buf_size;
243         if (info->pos_border > info->pos_end)
244             info->pos_border = info->pos_end;
245         count = info->pos_border - info->pos_buf;
246         if (count > 0)
247         {
248             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
249             {
250                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
251                 exit (1);
252             }
253             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
254             {
255                 if (r == -1)
256                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
257                 else
258                     logf (LOG_FATAL, "read of %ld but got %ld",
259                           (long) count, (long) r);
260                 exit (1);
261             }
262         }
263     }
264     else
265         info->pos_border = info->pos_end;
266 }
267
268 static void r_rewind (RSFD rfd)
269 {
270     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) (rfd->priv);  
271     struct rset_temp_info *info = (struct rset_temp_info *)(rfd->rset->priv);
272     r_flush (rfd, 0);
273     mrfd->pos_cur = 0;
274     info->pos_buf = 0;
275     r_reread (rfd);
276     mrfd->cur=0;
277 }
278
279 static int r_read (RSFD rfd, void *buf)
280 {
281     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
282     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
283
284     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
285
286     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
287     {
288         if (nc > info->pos_end)
289             return 0;
290         r_flush (rfd, 0);
291         info->pos_buf = mrfd->pos_cur;
292         r_reread (rfd);
293     }
294     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
295             rfd->rset->keycontrol->key_size);
296     mrfd->pos_cur = nc;
297     mrfd->cur++;
298     return 1;
299 }
300
301 static int r_write (RSFD rfd, const void *buf)
302 {
303     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
304     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
305
306     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
307
308     if (nc > info->pos_buf + info->buf_size)
309     {
310         r_flush (rfd, 1);
311         info->pos_buf = mrfd->pos_cur;
312         if (info->pos_buf < info->pos_end)
313             r_reread (rfd);
314     }
315     info->dirty = 1;
316     memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
317             rfd->rset->keycontrol->key_size);
318     mrfd->pos_cur = nc;
319     if (nc > info->pos_end)
320         info->pos_border = info->pos_end = nc;
321     info->hits++;
322     return 1;
323 }
324
325 static void r_pos (RSFD rfd, double  *current, double  *total)
326 {
327     /* struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd; */
328     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
329     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
330     
331     *current=(double) mrfd->cur;
332     *total=(double) info->hits;
333 }