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