0c5ca0bebbfc7e50a67b835ba0cbce30d2987ed2
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <assert.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #ifdef WIN32
26 #include <io.h>
27 #endif
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <sys/types.h>
32
33 #include <idzebra/util.h>
34 #include <rset.h>
35
36 static RSFD r_open(RSET ct, int flag);
37 static void r_close(RSFD rfd);
38 static void r_delete(RSET ct);
39 static int r_read(RSFD rfd, void *buf, TERMID *term);
40 static int r_write(RSFD rfd, const void *buf);
41 static void r_pos(RSFD rfd, double *current, double  *total);
42 static void r_flush(RSFD rfd, int mk);
43 static void r_reread(RSFD rfd);
44
45 static const struct rset_control control = 
46 {
47     "temp",
48     r_delete,
49     rset_get_one_term,
50     r_open,
51     r_close,
52     0, /* no forward */
53     r_pos, 
54     r_read,
55     r_write,
56 };
57
58 struct rset_private {
59     int     fd;            /* file descriptor for temp file */
60     char   *fname;         /* name of temp file */
61     char   *buf_mem;       /* window buffer */
62     size_t  buf_size;      /* size of window */
63     size_t  pos_end;       /* last position in set */
64     size_t  pos_buf;       /* position of first byte in window */
65     size_t  pos_border;    /* position of last byte+1 in window */
66     int     dirty;         /* window is dirty */
67     zint    hits;          /* no of hits */
68     char   *temp_path;
69 };
70
71 struct rfd_private {
72     void *buf;
73     size_t  pos_cur;       /* current position in set */
74                            /* FIXME - term pos or what ??  */
75     zint cur; /* number of the current hit */
76 };
77
78 static int log_level = 0;
79 static int log_level_initialized = 0;
80
81 RSET rset_create_temp(NMEM nmem, struct rset_key_control *kcontrol,
82                       int scope, const char *temp_path, TERMID term)
83 {
84     RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, term,
85                                  0, 0);
86     struct rset_private *info;
87     if (!log_level_initialized)
88     {
89         log_level = yaz_log_module_level("rstemp");
90         log_level_initialized = 1;
91     }
92     info = (struct rset_private *) nmem_malloc(rnew->nmem, sizeof(*info));
93     info->fd = -1;
94     info->fname = NULL;
95     info->buf_size = 4096;
96     info->buf_mem = (char *) nmem_malloc(rnew->nmem, info->buf_size);
97     info->pos_end = 0;
98     info->pos_buf = 0;
99     info->dirty = 0;
100     info->hits = 0;
101
102     if (!temp_path)
103         info->temp_path = NULL;
104     else
105         info->temp_path = nmem_strdup(rnew->nmem, temp_path);
106     rnew->priv = info; 
107     return rnew;
108 } /* rstemp_create */
109
110 static void r_delete(RSET ct)
111 {
112     struct rset_private *info = (struct rset_private*) ct->priv;
113
114     yaz_log(log_level, "r_delete: set size %ld", (long) info->pos_end);
115     if (info->fname)
116     {
117         yaz_log(log_level, "r_delete: unlink %s", info->fname);
118         unlink(info->fname);
119     }
120 }
121
122 static RSFD r_open(RSET ct, int flag)
123 {
124     struct rset_private *info = (struct rset_private *) ct->priv;
125     RSFD rfd;
126     struct rfd_private *prfd;
127
128     if (info->fd == -1 && info->fname)
129     {
130         if (flag & RSETF_WRITE)
131             info->fd = open(info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
132         else
133             info->fd = open(info->fname, O_BINARY|O_RDONLY);
134         if (info->fd == -1)
135         {
136             yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: open failed %s", info->fname);
137             zebra_exit("r_open");
138         }
139     }
140     rfd = rfd_create_base(ct);
141     if (!rfd->priv)
142     {
143         prfd = (struct rfd_private *) nmem_malloc(ct->nmem, sizeof(*prfd));
144         rfd->priv = (void *)prfd;
145         prfd->buf = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
146     } 
147     else
148         prfd= rfd->priv;
149     r_flush(rfd, 0);
150     prfd->pos_cur = 0;
151     info->pos_buf = 0;
152     r_reread(rfd);
153     prfd->cur = 0;
154     return rfd;
155 }
156
157 /* r_flush:
158       flush current window to file if file is assocated with set
159  */
160 static void r_flush(RSFD rfd, int mk)
161 {
162     struct rset_private *info = rfd->rset->priv;
163
164     if (!info->fname && mk)
165     {
166 #if HAVE_MKSTEMP
167         char template[1024];
168
169         *template = '\0';
170
171         if (info->temp_path)
172             sprintf(template, "%s/", info->temp_path);
173         strcat(template, "zrs_");
174 #if HAVE_UNISTD_H
175         sprintf(template + strlen(template), "%ld_", (long) getpid());
176 #endif
177         strcat(template, "XXXXXX");
178
179         info->fd = mkstemp(template);
180         if (info->fd == -1)
181         {
182             yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: mkstemp %s", template);
183             zebra_exit("r_flush");
184         }
185         info->fname = nmem_strdup(rfd->rset->nmem, template);
186 #else
187         char *s = (char*) tempnam(info->temp_path, "zrs");
188         info->fname= nmem_strdup(rfd->rset->nmem, s);
189
190         yaz_log(log_level, "creating tempfile %s", info->fname);
191         info->fd = open(info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
192         if (info->fd == -1)
193         {
194             yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: open %s", info->fname);
195             zebra_exit("r_flush");
196         }
197 #endif
198     }
199     if (info->fname && info->fd != -1 && info->dirty)
200     {
201         size_t count;
202         int r;
203         
204         if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
205         {
206             yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (1) %s", info->fname);
207             zebra_exit("r_flusxh");
208         }
209         count = info->buf_size;
210         if (count > info->pos_end - info->pos_buf)
211             count = info->pos_end - info->pos_buf;
212         if ((r = write(info->fd, info->buf_mem, count)) < (int) count)
213         {
214             if (r == -1)
215                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: write %s", info->fname);
216             else
217                 yaz_log(YLOG_FATAL, "rstemp: write of %ld but got %ld",
218                       (long) count, (long) r);
219             zebra_exit("r_flush");
220         }
221         info->dirty = 0;
222     }
223 }
224
225 static void r_close(RSFD rfd)
226 {
227     struct rset_private *info = (struct rset_private *)rfd->rset->priv;
228     if (rfd_is_last(rfd))
229     {
230         r_flush(rfd, 0);
231         if (info->fname && info->fd != -1)
232         {
233             close(info->fd);
234             info->fd = -1;
235         }
236     }
237 }
238
239
240 /* r_reread:
241       read from file to window if file is assocated with set -
242       indicated by fname
243  */
244 static void r_reread(RSFD rfd)
245 {
246     struct rfd_private *mrfd = (struct rfd_private*) rfd->priv; 
247     struct rset_private *info = (struct rset_private *)rfd->rset->priv;
248
249     if (info->fname)
250     {
251         size_t count;
252         int r;
253
254         info->pos_border = mrfd->pos_cur +
255             info->buf_size;
256         if (info->pos_border > info->pos_end)
257             info->pos_border = info->pos_end;
258         count = info->pos_border - info->pos_buf;
259         if (count > 0)
260         {
261             if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
262             {
263                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (2) %s fd=%d", info->fname, info->fd);
264                 zebra_exit("r_reread");
265             }
266             if ((r = read(info->fd, info->buf_mem, count)) < (int) count)
267             {
268                 if (r == -1)
269                     yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: read %s", info->fname);
270                 else
271                     yaz_log(YLOG_FATAL, "read of %ld but got %ld",
272                           (long) count, (long) r);
273                 zebra_exit("r_reread");
274             }
275         }
276     }
277     else
278         info->pos_border = info->pos_end;
279 }
280
281 static int r_read(RSFD rfd, void *buf, TERMID *term)
282 {
283     struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;  
284     struct rset_private *info = (struct rset_private *)rfd->rset->priv;
285
286     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
287
288     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
289     {
290         if (nc > info->pos_end)
291             return 0;
292         r_flush(rfd, 0);
293         info->pos_buf = mrfd->pos_cur;
294         r_reread(rfd);
295     }
296     memcpy(buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
297             rfd->rset->keycontrol->key_size);
298     if (term)
299         *term = rfd->rset->term; 
300         /* FIXME - should we store and return terms ?? */
301     mrfd->pos_cur = nc;
302     mrfd->cur++;
303     return 1;
304 }
305
306 static int r_write(RSFD rfd, const void *buf)
307 {
308     struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;  
309     struct rset_private *info = (struct rset_private *)rfd->rset->priv;
310
311     size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
312
313     if (nc > info->pos_buf + info->buf_size)
314     {
315         r_flush(rfd, 1);
316         info->pos_buf = mrfd->pos_cur;
317         if (info->pos_buf < info->pos_end)
318             r_reread(rfd);
319     }
320     info->dirty = 1;
321     memcpy(info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
322             rfd->rset->keycontrol->key_size);
323     mrfd->pos_cur = nc;
324     if (nc > info->pos_end)
325         info->pos_border = info->pos_end = nc;
326     info->hits++;
327     return 1;
328 }
329
330 static void r_pos(RSFD rfd, double  *current, double  *total)
331 {
332     struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;  
333     struct rset_private *info = (struct rset_private *)rfd->rset->priv;
334     
335     *current = (double) mrfd->cur;
336     *total = (double) info->hits;
337 }
338 /*
339  * Local variables:
340  * c-basic-offset: 4
341  * indent-tabs-mode: nil
342  * End:
343  * vim: shiftwidth=4 tabstop=8 expandtab
344  */
345