1 /* $Id: rstemp.c,v 1.62 2005-04-26 10:09:38 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
33 #include <sys/types.h>
35 #include <idzebra/util.h>
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);
47 static const struct rset_control control =
60 struct rset_temp_info {
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 */
73 struct rset_temp_rfd {
75 size_t pos_cur; /* current position in set */
76 /* FIXME - term pos or what ?? */
77 zint cur; /* number of the current hit */
80 static int log_level = 0;
81 static int log_level_initialized = 0;
83 RSET rstemp_create(NMEM nmem, const struct key_control *kcontrol,
84 int scope, const char *temp_path, TERMID term)
86 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope,term);
87 struct rset_temp_info *info;
88 if (!log_level_initialized)
90 log_level = yaz_log_module_level("rstemp");
91 log_level_initialized = 1;
93 info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
96 info->buf_size = 4096;
97 info->buf_mem = (char *) nmem_malloc(rnew->nmem, info->buf_size);
104 info->temp_path = NULL;
106 info->temp_path = nmem_strdup(rnew->nmem, temp_path);
109 } /* rstemp_create */
111 static void r_delete(RSET ct)
113 struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
115 yaz_log(log_level, "r_delete: set size %ld", (long) info->pos_end);
118 yaz_log(log_level, "r_delete: unlink %s", info->fname);
123 static RSFD r_open(RSET ct, int flag)
125 struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
127 struct rset_temp_rfd *prfd;
129 if (info->fd == -1 && info->fname)
131 if (flag & RSETF_WRITE)
132 info->fd = open(info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
134 info->fd = open(info->fname, O_BINARY|O_RDONLY);
137 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: open failed %s", info->fname);
141 rfd = rfd_create_base(ct);
144 prfd = (struct rset_temp_rfd *) nmem_malloc(ct->nmem, sizeof(*prfd));
145 rfd->priv = (void *)prfd;
146 prfd->buf = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
159 flush current window to file if file is assocated with set
161 static void r_flush(RSFD rfd, int mk)
163 /* struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info; */
164 struct rset_temp_info *info = rfd->rset->priv;
166 if (!info->fname && mk)
171 sprintf(template, "%s/zrsXXXXXX", info->temp_path);
173 sprintf(template, "zrsXXXXXX");
175 info->fd = mkstemp(template);
179 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: mkstemp %s", template);
182 info->fname = nmem_strdup(rfd->rset->nmem, template);
184 char *s = (char*) tempnam(info->temp_path, "zrs");
185 info->fname= nmem_strdup(rfd->rset->nmem, s);
187 yaz_log(log_level, "creating tempfile %s", info->fname);
188 info->fd = open(info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
191 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: open %s", info->fname);
196 if (info->fname && info->fd != -1 && info->dirty)
201 if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
203 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (1) %s", info->fname);
206 count = info->buf_size;
207 if (count > info->pos_end - info->pos_buf)
208 count = info->pos_end - info->pos_buf;
209 if ((r = write(info->fd, info->buf_mem, count)) < (int) count)
212 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: write %s", info->fname);
214 yaz_log(YLOG_FATAL, "rstemp: write of %ld but got %ld",
215 (long) count, (long) r);
222 static void r_close(RSFD rfd)
224 /*struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; */
225 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
226 if (rfd_is_last(rfd))
229 if (info->fname && info->fd != -1)
235 rfd_delete_base(rfd);
240 read from file to window if file is assocated with set -
243 static void r_reread(RSFD rfd)
245 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
246 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
253 info->pos_border = mrfd->pos_cur +
255 if (info->pos_border > info->pos_end)
256 info->pos_border = info->pos_end;
257 count = info->pos_border - info->pos_buf;
260 if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
262 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (2) %s fd=%d", info->fname, info->fd);
265 if ((r = read(info->fd, info->buf_mem, count)) < (int) count)
268 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: read %s", info->fname);
270 yaz_log(YLOG_FATAL, "read of %ld but got %ld",
271 (long) count, (long) r);
277 info->pos_border = info->pos_end;
280 static int r_read(RSFD rfd, void *buf, TERMID *term)
282 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
283 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
285 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
287 if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
289 if (nc > info->pos_end)
292 info->pos_buf = mrfd->pos_cur;
295 memcpy(buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
296 rfd->rset->keycontrol->key_size);
298 *term = rfd->rset->term;
299 /* FIXME - should we store and return terms ?? */
305 static int r_write(RSFD rfd, const void *buf)
307 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
308 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
310 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
312 if (nc > info->pos_buf + info->buf_size)
315 info->pos_buf = mrfd->pos_cur;
316 if (info->pos_buf < info->pos_end)
320 memcpy(info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
321 rfd->rset->keycontrol->key_size);
323 if (nc > info->pos_end)
324 info->pos_border = info->pos_end = nc;
329 static void r_pos(RSFD rfd, double *current, double *total)
331 /* struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd; */
332 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
333 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
335 *current = (double) mrfd->cur;
336 *total = (double) info->hits;