1 /* $Id: rstemp.c,v 1.71 2007-01-15 15:10:19 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
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
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
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <sys/types.h>
36 #include <idzebra/util.h>
39 static RSFD r_open(RSET ct, int flag);
40 static void r_close(RSFD rfd);
41 static void r_delete(RSET ct);
42 static int r_read(RSFD rfd, void *buf, TERMID *term);
43 static int r_write(RSFD rfd, const void *buf);
44 static void r_pos(RSFD rfd, double *current, double *total);
45 static void r_flush(RSFD rfd, int mk);
46 static void r_reread(RSFD rfd);
48 static const struct rset_control control =
62 int fd; /* file descriptor for temp file */
63 char *fname; /* name of temp file */
64 char *buf_mem; /* window buffer */
65 size_t buf_size; /* size of window */
66 size_t pos_end; /* last position in set */
67 size_t pos_buf; /* position of first byte in window */
68 size_t pos_border; /* position of last byte+1 in window */
69 int dirty; /* window is dirty */
70 zint hits; /* no of hits */
76 size_t pos_cur; /* current position in set */
77 /* FIXME - term pos or what ?? */
78 zint cur; /* number of the current hit */
81 static int log_level = 0;
82 static int log_level_initialized = 0;
84 RSET rset_create_temp(NMEM nmem, struct rset_key_control *kcontrol,
85 int scope, const char *temp_path, TERMID term)
87 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, term,
89 struct rset_private *info;
90 if (!log_level_initialized)
92 log_level = yaz_log_module_level("rstemp");
93 log_level_initialized = 1;
95 info = (struct rset_private *) nmem_malloc(rnew->nmem, sizeof(*info));
98 info->buf_size = 4096;
99 info->buf_mem = (char *) nmem_malloc(rnew->nmem, info->buf_size);
106 info->temp_path = NULL;
108 info->temp_path = nmem_strdup(rnew->nmem, temp_path);
111 } /* rstemp_create */
113 static void r_delete(RSET ct)
115 struct rset_private *info = (struct rset_private*) ct->priv;
117 yaz_log(log_level, "r_delete: set size %ld", (long) info->pos_end);
120 yaz_log(log_level, "r_delete: unlink %s", info->fname);
125 static RSFD r_open(RSET ct, int flag)
127 struct rset_private *info = (struct rset_private *) ct->priv;
129 struct rfd_private *prfd;
131 if (info->fd == -1 && info->fname)
133 if (flag & RSETF_WRITE)
134 info->fd = open(info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
136 info->fd = open(info->fname, O_BINARY|O_RDONLY);
139 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: open failed %s", info->fname);
140 zebra_exit("r_open");
143 rfd = rfd_create_base(ct);
146 prfd = (struct rfd_private *) nmem_malloc(ct->nmem, sizeof(*prfd));
147 rfd->priv = (void *)prfd;
148 prfd->buf = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
161 flush current window to file if file is assocated with set
163 static void r_flush(RSFD rfd, int mk)
165 struct rset_private *info = rfd->rset->priv;
167 if (!info->fname && mk)
175 sprintf(template, "%s/", info->temp_path);
176 strcat(template, "zrs_");
178 sprintf(template + strlen(template), "%ld_", (long) getpid());
180 strcat(template, "XXXXXX");
182 info->fd = mkstemp(template);
185 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: mkstemp %s", template);
186 zebra_exit("r_flush");
188 info->fname = nmem_strdup(rfd->rset->nmem, template);
190 char *s = (char*) tempnam(info->temp_path, "zrs");
191 info->fname= nmem_strdup(rfd->rset->nmem, s);
193 yaz_log(log_level, "creating tempfile %s", info->fname);
194 info->fd = open(info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
197 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: open %s", info->fname);
198 zebra_exit("r_flush");
202 if (info->fname && info->fd != -1 && info->dirty)
207 if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
209 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (1) %s", info->fname);
210 zebra_exit("r_flusxh");
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)
218 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: write %s", info->fname);
220 yaz_log(YLOG_FATAL, "rstemp: write of %ld but got %ld",
221 (long) count, (long) r);
222 zebra_exit("r_flush");
228 static void r_close(RSFD rfd)
230 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
231 if (rfd_is_last(rfd))
234 if (info->fname && info->fd != -1)
244 read from file to window if file is assocated with set -
247 static void r_reread(RSFD rfd)
249 struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;
250 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
257 info->pos_border = mrfd->pos_cur +
259 if (info->pos_border > info->pos_end)
260 info->pos_border = info->pos_end;
261 count = info->pos_border - info->pos_buf;
264 if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
266 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (2) %s fd=%d", info->fname, info->fd);
267 zebra_exit("r_reread");
269 if ((r = read(info->fd, info->buf_mem, count)) < (int) count)
272 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: read %s", info->fname);
274 yaz_log(YLOG_FATAL, "read of %ld but got %ld",
275 (long) count, (long) r);
276 zebra_exit("r_reread");
281 info->pos_border = info->pos_end;
284 static int r_read(RSFD rfd, void *buf, TERMID *term)
286 struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;
287 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
289 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
291 if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
293 if (nc > info->pos_end)
296 info->pos_buf = mrfd->pos_cur;
299 memcpy(buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
300 rfd->rset->keycontrol->key_size);
302 *term = rfd->rset->term;
303 /* FIXME - should we store and return terms ?? */
309 static int r_write(RSFD rfd, const void *buf)
311 struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;
312 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
314 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
316 if (nc > info->pos_buf + info->buf_size)
319 info->pos_buf = mrfd->pos_cur;
320 if (info->pos_buf < info->pos_end)
324 memcpy(info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
325 rfd->rset->keycontrol->key_size);
327 if (nc > info->pos_end)
328 info->pos_border = info->pos_end = nc;
333 static void r_pos(RSFD rfd, double *current, double *total)
335 struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;
336 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
338 *current = (double) mrfd->cur;
339 *total = (double) info->hits;
344 * indent-tabs-mode: nil
346 * vim: shiftwidth=4 tabstop=8 expandtab