Fixed bug #203, rank-1 crashed with isam-c
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.53 2004-11-03 16:04:46 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     rset_get_one_term,
52     r_open,
53     r_close,
54     rset_default_forward,
55     r_pos, 
56     r_read,
57     r_write,
58 };
59
60 const struct rset_control *rset_kind_temp = &control;
61
62 struct rset_temp_info {
63     int     fd;            /* file descriptor for temp file */
64     char   *fname;         /* name of temp file */
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 };
74
75 struct rset_temp_rfd {
76     void *buf;
77     size_t  pos_cur;       /* current position in set */
78                            /* FIXME - term pos or what ??  */
79     zint cur; /* number of the current hit */
80 };
81
82 RSET rstemp_create( NMEM nmem, const struct key_control *kcontrol,
83                     int scope, 
84                     const char *temp_path,TERMID term)
85 {
86     RSET rnew=rset_create_base(&control, nmem, kcontrol, scope,term);
87     struct rset_temp_info *info;
88    
89     info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
90     info->fd = -1;
91     info->fname = NULL;
92     info->buf_size = 4096;
93     info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
94     info->pos_end = 0;
95     info->pos_buf = 0;
96     info->dirty = 0;
97     info->hits = 0;
98
99     if (!temp_path)
100         info->temp_path = NULL;
101     else
102         info->temp_path = nmem_strdup(rnew->nmem,temp_path);
103     rnew->priv=info; 
104     return rnew;
105 } /* rstemp_create */
106
107 static void r_delete (RSET ct)
108 {
109     struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
110
111     logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
112     if (info->fname)
113     {
114         logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
115         unlink (info->fname);
116     }
117 }
118
119
120 static RSFD r_open (RSET ct, int flag)
121 {
122     struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
123     RSFD rfd;
124     struct rset_temp_rfd *prfd;
125
126     if (info->fd == -1 && info->fname)
127     {
128         if (flag & RSETF_WRITE)
129             info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
130         else
131             info->fd = open (info->fname, O_BINARY|O_RDONLY);
132         if (info->fd == -1)
133         {
134             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
135             exit (1);
136         }
137     }
138     rfd = rfd_create_base(ct);
139     if (!rfd->priv){
140         prfd= (struct rset_temp_rfd *) nmem_malloc(ct->nmem, sizeof(*prfd));
141         rfd->priv=(void *)prfd;
142         prfd->buf = nmem_malloc (ct->nmem,ct->keycontrol->key_size);
143     } else
144         prfd= rfd->priv;
145     r_flush (rfd, 0);
146     prfd->pos_cur = 0;
147     info->pos_buf = 0;
148     r_reread (rfd);
149     prfd->cur=0;
150     return rfd;
151 }
152
153 /* r_flush:
154       flush current window to file if file is assocated with set
155  */
156 static void r_flush (RSFD rfd, int mk)
157 {
158     /* struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info; */
159     struct rset_temp_info *info = rfd->rset->priv;
160
161     if (!info->fname && mk)
162     {
163 #if HAVE_MKSTEMP
164         char template[1024];
165
166         if (info->temp_path)
167             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
168         else
169             sprintf (template, "zrsXXXXXX");
170
171         info->fd = mkstemp (template);
172
173         if (info->fd == -1)
174         {
175             logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
176             exit (1);
177         }
178         info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
179         strcpy (info->fname, template);
180 #else
181         char *s = (char*) tempnam (info->temp_path, "zrs");
182         info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
183         strcpy (info->fname, s);
184
185         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
186         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
187         if (info->fd == -1)
188         {
189             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
190             exit (1);
191         }
192 #endif
193     }
194     if (info->fname && info->fd != -1 && info->dirty)
195     {
196         size_t count;
197         int r;
198         
199         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
200         {
201             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
202             exit (1);
203         }
204         count = info->buf_size;
205         if (count > info->pos_end - info->pos_buf)
206             count = info->pos_end - info->pos_buf;
207         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
208         {
209             if (r == -1)
210                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
211             else
212                 logf (LOG_FATAL, "write of %ld but got %ld",
213                       (long) count, (long) r);
214             exit (1);
215         }
216         info->dirty = 0;
217     }
218 }
219
220 static void r_close (RSFD rfd)
221 {
222     /*struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; */
223     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
224     r_flush (rfd, 0);
225     if (info->fname && info->fd != -1)
226     {
227         close (info->fd);
228         info->fd = -1;
229     } /* FIXME - Is this right, don't we risk closing the file too early ?*/
230     rfd_delete_base(rfd);
231 }
232
233
234 /* r_reread:
235       read from file to window if file is assocated with set -
236       indicated by fname
237  */
238 static void r_reread (RSFD rfd)
239 {
240     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; 
241     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
242
243     if (info->fname)
244     {
245         size_t count;
246         int r;
247
248         info->pos_border = mrfd->pos_cur +
249             info->buf_size;
250         if (info->pos_border > info->pos_end)
251             info->pos_border = info->pos_end;
252         count = info->pos_border - info->pos_buf;
253         if (count > 0)
254         {
255             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
256             {
257                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
258                 exit (1);
259             }
260             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
261             {
262                 if (r == -1)
263                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
264                 else
265                     logf (LOG_FATAL, "read of %ld but got %ld",
266                           (long) count, (long) r);
267                 exit (1);
268             }
269         }
270     }
271     else
272         info->pos_border = info->pos_end;
273 }
274
275
276 static int r_read (RSFD rfd, void *buf, TERMID *term)
277 {
278     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
279     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
280
281     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
282
283     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
284     {
285         if (nc > info->pos_end)
286             return 0;
287         r_flush (rfd, 0);
288         info->pos_buf = mrfd->pos_cur;
289         r_reread (rfd);
290     }
291     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
292             rfd->rset->keycontrol->key_size);
293     if (term)
294         *term=rfd->rset->term; 
295         /* FIXME - should we store and return terms ?? */
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 }