Added the scope parameter to rsets, and using it in all forwards and reads
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.49 2004-09-09 10:08:06 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                     int scope, 
83                     const char *temp_path)
84 {
85     RSET rnew=rset_create_base(&control, nmem, kcontrol, scope);
86     struct rset_temp_info *info;
87    
88     info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
89     info->fd = -1;
90     info->fname = NULL;
91     info->buf_size = 4096;
92     info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
93     info->pos_end = 0;
94     info->pos_buf = 0;
95     info->dirty = 0;
96     info->hits = 0;
97
98     if (!temp_path)
99         info->temp_path = NULL;
100     else
101         info->temp_path = nmem_strdup(rnew->nmem,temp_path);
102     rnew->priv=info; 
103     return rnew;
104 } /* rstemp_create */
105
106 static void r_delete (RSET ct)
107 {
108     struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
109
110     logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
111     if (info->fname)
112     {
113         logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
114         unlink (info->fname);
115     }
116 }
117
118
119 static RSFD r_open (RSET ct, int flag)
120 {
121     struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
122     RSFD rfd;
123     struct rset_temp_rfd *prfd;
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 = rfd_create_base(ct);
138     if (!rfd->priv){
139         prfd= (struct rset_temp_rfd *) nmem_malloc(ct->nmem, sizeof(*prfd));
140         rfd->priv=(void *)prfd;
141         prfd->buf = nmem_malloc (ct->nmem,ct->keycontrol->key_size);
142     }
143     r_rewind (rfd);
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     struct rset_temp_info *info = rfd->rset->priv;
154
155     if (!info->fname && mk)
156     {
157 #if HAVE_MKSTEMP
158         char template[1024];
159
160         if (info->temp_path)
161             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
162         else
163             sprintf (template, "zrsXXXXXX");
164
165         info->fd = mkstemp (template);
166
167         if (info->fd == -1)
168         {
169             logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
170             exit (1);
171         }
172         info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
173         strcpy (info->fname, template);
174 #else
175         char *s = (char*) tempnam (info->temp_path, "zrs");
176         info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
177         strcpy (info->fname, s);
178
179         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
180         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
181         if (info->fd == -1)
182         {
183             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
184             exit (1);
185         }
186 #endif
187     }
188     if (info->fname && info->fd != -1 && info->dirty)
189     {
190         size_t count;
191         int r;
192         
193         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
194         {
195             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
196             exit (1);
197         }
198         count = info->buf_size;
199         if (count > info->pos_end - info->pos_buf)
200             count = info->pos_end - info->pos_buf;
201         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
202         {
203             if (r == -1)
204                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
205             else
206                 logf (LOG_FATAL, "write of %ld but got %ld",
207                       (long) count, (long) r);
208             exit (1);
209         }
210         info->dirty = 0;
211     }
212 }
213
214 static void r_close (RSFD rfd)
215 {
216     /*struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; */
217     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
218     r_flush (rfd, 0);
219     if (info->fname && info->fd != -1)
220     {
221         close (info->fd);
222         info->fd = -1;
223     } /* FIXME - Is this right, don't we risk closing the file too early ?*/
224     rfd_delete_base(rfd);
225 }
226
227
228 /* r_reread:
229       read from file to window if file is assocated with set -
230       indicated by fname
231  */
232 static void r_reread (RSFD rfd)
233 {
234     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; 
235     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
236
237     if (info->fname)
238     {
239         size_t count;
240         int r;
241
242         info->pos_border = mrfd->pos_cur +
243             info->buf_size;
244         if (info->pos_border > info->pos_end)
245             info->pos_border = info->pos_end;
246         count = info->pos_border - info->pos_buf;
247         if (count > 0)
248         {
249             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
250             {
251                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
252                 exit (1);
253             }
254             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
255             {
256                 if (r == -1)
257                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
258                 else
259                     logf (LOG_FATAL, "read of %ld but got %ld",
260                           (long) count, (long) r);
261                 exit (1);
262             }
263         }
264     }
265     else
266         info->pos_border = info->pos_end;
267 }
268
269 static void r_rewind (RSFD rfd)
270 {
271     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) (rfd->priv);  
272     struct rset_temp_info *info = (struct rset_temp_info *)(rfd->rset->priv);
273     r_flush (rfd, 0);
274     mrfd->pos_cur = 0;
275     info->pos_buf = 0;
276     r_reread (rfd);
277     mrfd->cur=0;
278 }
279
280 static int r_read (RSFD rfd, void *buf)
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
285     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
286
287     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
288     {
289         if (nc > info->pos_end)
290             return 0;
291         r_flush (rfd, 0);
292         info->pos_buf = mrfd->pos_cur;
293         r_reread (rfd);
294     }
295     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
296             rfd->rset->keycontrol->key_size);
297     mrfd->pos_cur = nc;
298     mrfd->cur++;
299     return 1;
300 }
301
302 static int r_write (RSFD rfd, const void *buf)
303 {
304     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
305     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
306
307     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
308
309     if (nc > info->pos_buf + info->buf_size)
310     {
311         r_flush (rfd, 1);
312         info->pos_buf = mrfd->pos_cur;
313         if (info->pos_buf < info->pos_end)
314             r_reread (rfd);
315     }
316     info->dirty = 1;
317     memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
318             rfd->rset->keycontrol->key_size);
319     mrfd->pos_cur = nc;
320     if (nc > info->pos_end)
321         info->pos_border = info->pos_end = nc;
322     info->hits++;
323     return 1;
324 }
325
326 static void r_pos (RSFD rfd, double  *current, double  *total)
327 {
328     /* struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd; */
329     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
330     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
331     
332     *current=(double) mrfd->cur;
333     *total=(double) info->hits;
334 }