1 /* $Id: rstemp.c,v 1.66 2006-05-10 08:13:34 adam Exp $
2 Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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 rstemp_create(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);
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)
172 sprintf(template, "%s/zrsXXXXXX", info->temp_path);
174 sprintf(template, "zrsXXXXXX");
176 info->fd = mkstemp(template);
180 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: mkstemp %s", template);
183 info->fname = nmem_strdup(rfd->rset->nmem, template);
185 char *s = (char*) tempnam(info->temp_path, "zrs");
186 info->fname= nmem_strdup(rfd->rset->nmem, s);
188 yaz_log(log_level, "creating tempfile %s", info->fname);
189 info->fd = open(info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
192 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: open %s", info->fname);
197 if (info->fname && info->fd != -1 && info->dirty)
202 if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
204 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (1) %s", info->fname);
207 count = info->buf_size;
208 if (count > info->pos_end - info->pos_buf)
209 count = info->pos_end - info->pos_buf;
210 if ((r = write(info->fd, info->buf_mem, count)) < (int) count)
213 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: write %s", info->fname);
215 yaz_log(YLOG_FATAL, "rstemp: write of %ld but got %ld",
216 (long) count, (long) r);
223 static void r_close(RSFD rfd)
225 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
226 if (rfd_is_last(rfd))
229 if (info->fname && info->fd != -1)
239 read from file to window if file is assocated with set -
242 static void r_reread(RSFD rfd)
244 struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;
245 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
252 info->pos_border = mrfd->pos_cur +
254 if (info->pos_border > info->pos_end)
255 info->pos_border = info->pos_end;
256 count = info->pos_border - info->pos_buf;
259 if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
261 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (2) %s fd=%d", info->fname, info->fd);
264 if ((r = read(info->fd, info->buf_mem, count)) < (int) count)
267 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: read %s", info->fname);
269 yaz_log(YLOG_FATAL, "read of %ld but got %ld",
270 (long) count, (long) r);
276 info->pos_border = info->pos_end;
279 static int r_read(RSFD rfd, void *buf, TERMID *term)
281 struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;
282 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
284 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
286 if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
288 if (nc > info->pos_end)
291 info->pos_buf = mrfd->pos_cur;
294 memcpy(buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
295 rfd->rset->keycontrol->key_size);
297 *term = rfd->rset->term;
298 /* FIXME - should we store and return terms ?? */
304 static int r_write(RSFD rfd, const void *buf)
306 struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;
307 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
309 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
311 if (nc > info->pos_buf + info->buf_size)
314 info->pos_buf = mrfd->pos_cur;
315 if (info->pos_buf < info->pos_end)
319 memcpy(info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
320 rfd->rset->keycontrol->key_size);
322 if (nc > info->pos_end)
323 info->pos_border = info->pos_end = nc;
328 static void r_pos(RSFD rfd, double *current, double *total)
330 struct rfd_private *mrfd = (struct rfd_private*) rfd->priv;
331 struct rset_private *info = (struct rset_private *)rfd->rset->priv;
333 *current = (double) mrfd->cur;
334 *total = (double) info->hits;
339 * indent-tabs-mode: nil
341 * vim: shiftwidth=4 tabstop=8 expandtab