1 /* $Id: rstemp.c,v 1.61 2005-03-30 09:25:24 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);
48 static const struct rset_control control =
61 const struct rset_control *rset_kind_temp = &control;
63 struct rset_temp_info {
64 int fd; /* file descriptor for temp file */
65 char *fname; /* name of temp file */
66 char *buf_mem; /* window buffer */
67 size_t buf_size; /* size of window */
68 size_t pos_end; /* last position in set */
69 size_t pos_buf; /* position of first byte in window */
70 size_t pos_border; /* position of last byte+1 in window */
71 int dirty; /* window is dirty */
72 zint hits; /* no of hits */
76 struct rset_temp_rfd {
78 size_t pos_cur; /* current position in set */
79 /* FIXME - term pos or what ?? */
80 zint cur; /* number of the current hit */
83 static int log_level = 0;
84 static int log_level_initialized = 0;
86 RSET rstemp_create( NMEM nmem, const struct key_control *kcontrol,
87 int scope, const char *temp_path, TERMID term)
89 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope,term);
90 struct rset_temp_info *info;
91 if (!log_level_initialized)
93 log_level = yaz_log_module_level("rstemp");
94 log_level_initialized = 1;
96 info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
99 info->buf_size = 4096;
100 info->buf_mem = (char *) nmem_malloc(rnew->nmem, info->buf_size);
107 info->temp_path = NULL;
109 info->temp_path = nmem_strdup(rnew->nmem, temp_path);
112 } /* rstemp_create */
114 static void r_delete(RSET ct)
116 struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
118 yaz_log(log_level, "r_delete: set size %ld", (long) info->pos_end);
121 yaz_log(log_level, "r_delete: unlink %s", info->fname);
126 static RSFD r_open(RSET ct, int flag)
128 struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
130 struct rset_temp_rfd *prfd;
132 if (info->fd == -1 && info->fname)
134 if (flag & RSETF_WRITE)
135 info->fd = open(info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
137 info->fd = open(info->fname, O_BINARY|O_RDONLY);
140 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: open failed %s", info->fname);
144 rfd = rfd_create_base(ct);
147 prfd = (struct rset_temp_rfd *) nmem_malloc(ct->nmem, sizeof(*prfd));
148 rfd->priv = (void *)prfd;
149 prfd->buf = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
162 flush current window to file if file is assocated with set
164 static void r_flush(RSFD rfd, int mk)
166 /* struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info; */
167 struct rset_temp_info *info = rfd->rset->priv;
169 if (!info->fname && mk)
174 sprintf(template, "%s/zrsXXXXXX", info->temp_path);
176 sprintf(template, "zrsXXXXXX");
178 info->fd = mkstemp(template);
182 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: mkstemp %s", template);
185 info->fname = nmem_strdup(rfd->rset->nmem, template);
187 char *s = (char*) tempnam(info->temp_path, "zrs");
188 info->fname= nmem_strdup(rfd->rset->nmem, s);
190 yaz_log(log_level, "creating tempfile %s", info->fname);
191 info->fd = open(info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
194 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: open %s", info->fname);
199 if (info->fname && info->fd != -1 && info->dirty)
204 if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
206 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (1) %s", info->fname);
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)
215 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: write %s", info->fname);
217 yaz_log(YLOG_FATAL, "rstemp: write of %ld but got %ld",
218 (long) count, (long) r);
225 static void r_close(RSFD rfd)
227 /*struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; */
228 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
229 if (rfd_is_last(rfd))
232 if (info->fname && info->fd != -1)
238 rfd_delete_base(rfd);
243 read from file to window if file is assocated with set -
246 static void r_reread(RSFD rfd)
248 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
249 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
256 info->pos_border = mrfd->pos_cur +
258 if (info->pos_border > info->pos_end)
259 info->pos_border = info->pos_end;
260 count = info->pos_border - info->pos_buf;
263 if (lseek(info->fd, info->pos_buf, SEEK_SET) == -1)
265 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: lseek (2) %s fd=%d", info->fname, info->fd);
268 if ((r = read(info->fd, info->buf_mem, count)) < (int) count)
271 yaz_log(YLOG_FATAL|YLOG_ERRNO, "rstemp: read %s", info->fname);
273 yaz_log(YLOG_FATAL, "read of %ld but got %ld",
274 (long) count, (long) r);
280 info->pos_border = info->pos_end;
283 static int r_read(RSFD rfd, void *buf, TERMID *term)
285 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
286 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
288 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
290 if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
292 if (nc > info->pos_end)
295 info->pos_buf = mrfd->pos_cur;
298 memcpy(buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
299 rfd->rset->keycontrol->key_size);
301 *term = rfd->rset->term;
302 /* FIXME - should we store and return terms ?? */
308 static int r_write(RSFD rfd, const void *buf)
310 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
311 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
313 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
315 if (nc > info->pos_buf + info->buf_size)
318 info->pos_buf = mrfd->pos_cur;
319 if (info->pos_buf < info->pos_end)
323 memcpy(info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
324 rfd->rset->keycontrol->key_size);
326 if (nc > info->pos_end)
327 info->pos_border = info->pos_end = nc;
332 static void r_pos(RSFD rfd, double *current, double *total)
334 /* struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd; */
335 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
336 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
338 *current = (double) mrfd->cur;
339 *total = (double) info->hits;