Passing a TERMID to rsets when creating, and getting it back when reading.
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.51 2004-10-15 10:07:34 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 int r_read (RSFD rfd, void *buf, TERMID *term);
41 static int r_write (RSFD rfd, const void *buf);
42 static void r_pos (RSFD rfd, double *current, double  *total);
43 static void r_flush (RSFD rfd, int mk);
44 static void r_reread (RSFD rfd);
45
46
47 static const struct rset_control control = 
48 {
49     "temp",
50     r_delete,
51     r_open,
52     r_close,
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,0);
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     } else
143         prfd= rfd->priv;
144     r_flush (rfd, 0);
145     prfd->pos_cur = 0;
146     info->pos_buf = 0;
147     r_reread (rfd);
148     prfd->cur=0;
149     return rfd;
150 }
151
152 /* r_flush:
153       flush current window to file if file is assocated with set
154  */
155 static void r_flush (RSFD rfd, int mk)
156 {
157     /* struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info; */
158     struct rset_temp_info *info = rfd->rset->priv;
159
160     if (!info->fname && mk)
161     {
162 #if HAVE_MKSTEMP
163         char template[1024];
164
165         if (info->temp_path)
166             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
167         else
168             sprintf (template, "zrsXXXXXX");
169
170         info->fd = mkstemp (template);
171
172         if (info->fd == -1)
173         {
174             logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
175             exit (1);
176         }
177         info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
178         strcpy (info->fname, template);
179 #else
180         char *s = (char*) tempnam (info->temp_path, "zrs");
181         info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
182         strcpy (info->fname, s);
183
184         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
185         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
186         if (info->fd == -1)
187         {
188             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
189             exit (1);
190         }
191 #endif
192     }
193     if (info->fname && info->fd != -1 && info->dirty)
194     {
195         size_t count;
196         int r;
197         
198         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
199         {
200             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
201             exit (1);
202         }
203         count = info->buf_size;
204         if (count > info->pos_end - info->pos_buf)
205             count = info->pos_end - info->pos_buf;
206         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
207         {
208             if (r == -1)
209                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
210             else
211                 logf (LOG_FATAL, "write of %ld but got %ld",
212                       (long) count, (long) r);
213             exit (1);
214         }
215         info->dirty = 0;
216     }
217 }
218
219 static void r_close (RSFD rfd)
220 {
221     /*struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; */
222     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
223     r_flush (rfd, 0);
224     if (info->fname && info->fd != -1)
225     {
226         close (info->fd);
227         info->fd = -1;
228     } /* FIXME - Is this right, don't we risk closing the file too early ?*/
229     rfd_delete_base(rfd);
230 }
231
232
233 /* r_reread:
234       read from file to window if file is assocated with set -
235       indicated by fname
236  */
237 static void r_reread (RSFD rfd)
238 {
239     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; 
240     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
241
242     if (info->fname)
243     {
244         size_t count;
245         int r;
246
247         info->pos_border = mrfd->pos_cur +
248             info->buf_size;
249         if (info->pos_border > info->pos_end)
250             info->pos_border = info->pos_end;
251         count = info->pos_border - info->pos_buf;
252         if (count > 0)
253         {
254             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
255             {
256                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
257                 exit (1);
258             }
259             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
260             {
261                 if (r == -1)
262                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
263                 else
264                     logf (LOG_FATAL, "read of %ld but got %ld",
265                           (long) count, (long) r);
266                 exit (1);
267             }
268         }
269     }
270     else
271         info->pos_border = info->pos_end;
272 }
273
274
275 static int r_read (RSFD rfd, void *buf, TERMID *term)
276 {
277     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
278     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
279
280     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
281
282     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
283     {
284         if (nc > info->pos_end)
285             return 0;
286         r_flush (rfd, 0);
287         info->pos_buf = mrfd->pos_cur;
288         r_reread (rfd);
289     }
290     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
291             rfd->rset->keycontrol->key_size);
292     if (term)
293         *term=rfd->rset->term; 
294         /* FIXME - should we store and return terms ?? */
295     mrfd->pos_cur = nc;
296     mrfd->cur++;
297     return 1;
298 }
299
300 static int r_write (RSFD rfd, const void *buf)
301 {
302     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
303     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
304
305     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
306
307     if (nc > info->pos_buf + info->buf_size)
308     {
309         r_flush (rfd, 1);
310         info->pos_buf = mrfd->pos_cur;
311         if (info->pos_buf < info->pos_end)
312             r_reread (rfd);
313     }
314     info->dirty = 1;
315     memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
316             rfd->rset->keycontrol->key_size);
317     mrfd->pos_cur = nc;
318     if (nc > info->pos_end)
319         info->pos_border = info->pos_end = nc;
320     info->hits++;
321     return 1;
322 }
323
324 static void r_pos (RSFD rfd, double  *current, double  *total)
325 {
326     /* struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd; */
327     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
328     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
329     
330     *current=(double) mrfd->cur;
331     *total=(double) info->hits;
332 }