Added include stdlib.h a few places to get prototype for atoi/exit/..
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.58 2005-01-16 23:14:57 adam Exp $
2    Copyright (C) 1995-2005
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 <stdlib.h>
25 #include <assert.h>
26 #ifdef WIN32
27 #include <io.h>
28 #else
29 #include <unistd.h>
30 #endif
31 #include <string.h>
32 #include <sys/types.h>
33 #include <stdio.h>
34
35 #include <zebrautl.h>
36 #include <rset.h>
37
38 static RSFD r_open (RSET ct, int flag);
39 static void r_close (RSFD rfd);
40 static void r_delete (RSET ct);
41 static int r_read (RSFD rfd, void *buf, TERMID *term);
42 static int r_write (RSFD rfd, const void *buf);
43 static void r_pos (RSFD rfd, double *current, double  *total);
44 static void r_flush (RSFD rfd, int mk);
45 static void r_reread (RSFD rfd);
46
47
48 static const struct rset_control control = 
49 {
50     "temp",
51     r_delete,
52     rset_get_one_term,
53     r_open,
54     r_close,
55     rset_default_forward,
56     r_pos, 
57     r_read,
58     r_write,
59 };
60
61 const struct rset_control *rset_kind_temp = &control;
62
63 struct rset_temp_info {
64     int     fd;            /* file descriptor for temp file */
65     char   *fname;         /* name of temp file */
66     char   *buf_mem;       /* window buffer */
67     size_t  buf_size;      /* size of window */
68     size_t  pos_end;       /* last position in set */
69     size_t  pos_buf;       /* position of first byte in window */
70     size_t  pos_border;    /* position of last byte+1 in window */
71     int     dirty;         /* window is dirty */
72     zint     hits;          /* no of hits */
73     char   *temp_path;
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 static int log_level=0;
84 static int log_level_initialized=0;
85
86 RSET rstemp_create( NMEM nmem, const struct key_control *kcontrol,
87                     int scope, 
88                     const char *temp_path,TERMID term)
89 {
90     RSET rnew=rset_create_base(&control, nmem, kcontrol, scope,term);
91     struct rset_temp_info *info;
92     if (!log_level_initialized)
93     {
94         log_level=yaz_log_module_level("rstemp");
95         log_level_initialized=1;
96     }
97     info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
98     info->fd = -1;
99     info->fname = NULL;
100     info->buf_size = 4096;
101     info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
102     info->pos_end = 0;
103     info->pos_buf = 0;
104     info->dirty = 0;
105     info->hits = 0;
106
107     if (!temp_path)
108         info->temp_path = NULL;
109     else
110         info->temp_path = nmem_strdup(rnew->nmem,temp_path);
111     rnew->priv=info; 
112     return rnew;
113 } /* rstemp_create */
114
115 static void r_delete (RSET ct)
116 {
117     struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
118
119     yaz_log (log_level, "r_delete: set size %ld", (long) info->pos_end);
120     if (info->fname)
121     {
122         yaz_log (log_level, "r_delete: unlink %s", info->fname);
123         unlink (info->fname);
124     }
125 }
126
127
128 static RSFD r_open (RSET ct, int flag)
129 {
130     struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
131     RSFD rfd;
132     struct rset_temp_rfd *prfd;
133
134     if (info->fd == -1 && info->fname)
135     {
136         if (flag & RSETF_WRITE)
137             info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
138         else
139             info->fd = open (info->fname, O_BINARY|O_RDONLY);
140         if (info->fd == -1)
141         {
142             yaz_log (YLOG_FATAL|YLOG_ERRNO, "rstemp: open failed %s", info->fname);
143             exit (1);
144         }
145     }
146     rfd = rfd_create_base(ct);
147     if (!rfd->priv){
148         prfd= (struct rset_temp_rfd *) nmem_malloc(ct->nmem, sizeof(*prfd));
149         rfd->priv=(void *)prfd;
150         prfd->buf = nmem_malloc (ct->nmem,ct->keycontrol->key_size);
151     } else
152         prfd= rfd->priv;
153     r_flush (rfd, 0);
154     prfd->pos_cur = 0;
155     info->pos_buf = 0;
156     r_reread (rfd);
157     prfd->cur=0;
158     return rfd;
159 }
160
161 /* r_flush:
162       flush current window to file if file is assocated with set
163  */
164 static void r_flush (RSFD rfd, int mk)
165 {
166     /* struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info; */
167     struct rset_temp_info *info = rfd->rset->priv;
168
169     if (!info->fname && mk)
170     {
171         char template[1024];
172 #if HAVE_MKSTEMP
173
174         if (info->temp_path)
175             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
176         else
177             sprintf (template, "zrsXXXXXX");
178
179         info->fd = mkstemp (template);
180
181         if (info->fd == -1)
182         {
183             yaz_log (YLOG_FATAL|YLOG_ERRNO, "rstemp: mkstemp %s", template);
184             exit (1);
185         }
186         info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
187         strcpy (info->fname, template);
188 #else
189         char *s = (char*) tempnam (info->temp_path, "zrs");
190         info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
191         strcpy (info->fname, s);
192
193         yaz_log (log_level, "creating tempfile %s", info->fname);
194         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
195         if (info->fd == -1)
196         {
197             yaz_log (YLOG_FATAL|YLOG_ERRNO, "rstemp: open %s", info->fname);
198             exit (1);
199         }
200 #endif
201     }
202     if (info->fname && info->fd != -1 && info->dirty)
203     {
204         size_t count;
205         int r;
206         
207         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
208         {
209             yaz_log (YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek %s", info->fname);
210             exit (1);
211         }
212         count = info->buf_size;
213         if (count > info->pos_end - info->pos_buf)
214             count = info->pos_end - info->pos_buf;
215         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
216         {
217             if (r == -1)
218                 yaz_log (YLOG_FATAL|YLOG_ERRNO, "rstemp: write %s", info->fname);
219             else
220                 yaz_log (YLOG_FATAL, "rstemp: write of %ld but got %ld",
221                       (long) count, (long) r);
222             exit (1);
223         }
224         info->dirty = 0;
225     }
226 }
227
228 static void r_close (RSFD rfd)
229 {
230     /*struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; */
231     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
232     r_flush (rfd, 0);
233     if (info->fname && info->fd != -1)
234     {
235         close (info->fd);
236         info->fd = -1;
237     } /* FIXME - Is this right, don't we risk closing the file too early ?*/
238     rfd_delete_base(rfd);
239 }
240
241
242 /* r_reread:
243       read from file to window if file is assocated with set -
244       indicated by fname
245  */
246 static void r_reread (RSFD rfd)
247 {
248     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; 
249     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
250
251     if (info->fname)
252     {
253         size_t count;
254         int r;
255
256         info->pos_border = mrfd->pos_cur +
257             info->buf_size;
258         if (info->pos_border > info->pos_end)
259             info->pos_border = info->pos_end;
260         count = info->pos_border - info->pos_buf;
261         if (count > 0)
262         {
263             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
264             {
265                 yaz_log (YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek %s", info->fname);
266                 exit (1);
267             }
268             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
269             {
270                 if (r == -1)
271                     yaz_log (YLOG_FATAL|YLOG_ERRNO, "rstemp: read %s", info->fname);
272                 else
273                     yaz_log (YLOG_FATAL, "read of %ld but got %ld",
274                           (long) count, (long) r);
275                 exit (1);
276             }
277         }
278     }
279     else
280         info->pos_border = info->pos_end;
281 }
282
283
284 static int r_read (RSFD rfd, void *buf, TERMID *term)
285 {
286     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
287     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
288
289     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
290
291     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
292     {
293         if (nc > info->pos_end)
294             return 0;
295         r_flush (rfd, 0);
296         info->pos_buf = mrfd->pos_cur;
297         r_reread (rfd);
298     }
299     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
300             rfd->rset->keycontrol->key_size);
301     if (term)
302         *term=rfd->rset->term; 
303         /* FIXME - should we store and return terms ?? */
304     mrfd->pos_cur = nc;
305     mrfd->cur++;
306     return 1;
307 }
308
309 static int r_write (RSFD rfd, const void *buf)
310 {
311     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
312     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
313
314     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
315
316     if (nc > info->pos_buf + info->buf_size)
317     {
318         r_flush (rfd, 1);
319         info->pos_buf = mrfd->pos_cur;
320         if (info->pos_buf < info->pos_end)
321             r_reread (rfd);
322     }
323     info->dirty = 1;
324     memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
325             rfd->rset->keycontrol->key_size);
326     mrfd->pos_cur = nc;
327     if (nc > info->pos_end)
328         info->pos_border = info->pos_end = nc;
329     info->hits++;
330     return 1;
331 }
332
333 static void r_pos (RSFD rfd, double  *current, double  *total)
334 {
335     /* struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd; */
336     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;  
337     struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
338     
339     *current=(double) mrfd->cur;
340     *total=(double) info->hits;
341 }