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