2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1995-09-15 14:45:39 adam
10 * Revision 1.9 1995/09/15 09:20:42 adam
13 * Revision 1.8 1995/09/08 14:52:42 adam
14 * Work on relevance feedback.
16 * Revision 1.7 1995/09/07 13:58:44 adam
17 * New parameter: result-set file descriptor (RSFD) to support multiple
18 * positions within the same result-set.
19 * Boolean operators: and, or, not implemented.
21 * Revision 1.6 1995/09/06 16:11:56 adam
22 * More work on boolean sets.
24 * Revision 1.5 1995/09/05 16:36:59 adam
27 * Revision 1.4 1995/09/05 11:43:24 adam
28 * Complete version of temporary sets. Not tested yet though.
30 * Revision 1.3 1995/09/04 15:20:40 adam
31 * More work on temp sets. is_open member removed.
33 * Revision 1.2 1995/09/04 09:10:56 adam
36 * Revision 1.1 1994/11/04 13:21:30 quinn
44 #include <sys/types.h>
50 static rset_control *r_create(const struct rset_control *sel, void *parms);
51 static RSFD r_open (rset_control *ct, int wflag);
52 static void r_close (RSFD rfd);
53 static void r_delete (rset_control *ct);
54 static void r_rewind (RSFD rfd);
55 static int r_count (rset_control *ct);
56 static int r_read (RSFD rfd, void *buf);
57 static int r_write (RSFD rfd, const void *buf);
59 static const rset_control control =
73 const rset_control *rset_kind_temp = &control;
75 struct rset_temp_info {
78 size_t key_size; /* key size */
79 char *buf_mem; /* window buffer */
80 size_t buf_size; /* size of window */
81 size_t pos_end; /* last position in set */
82 size_t pos_cur; /* current position in set */
83 size_t pos_buf; /* position of first byte in window */
84 size_t pos_border; /* position of last byte+1 in window */
85 int dirty; /* window is dirty */
88 struct rset_temp_rfd {
89 struct rset_temp_info *info;
90 struct rset_temp_rfd *next;
93 static struct rset_control *r_create(const struct rset_control *sel,
97 rset_temp_parms *temp_parms = parms;
98 struct rset_temp_info *info;
100 newct = xmalloc(sizeof(*newct));
101 memcpy(newct, sel, sizeof(*sel));
102 newct->buf = xmalloc (sizeof(struct rset_temp_info));
107 info->key_size = temp_parms->key_size;
108 info->buf_size = 2048;
109 info->buf_mem = xmalloc (info->buf_size);
118 static RSFD r_open (struct rset_control *ct, int wflag)
120 struct rset_temp_info *info = ct->buf;
121 struct rset_temp_rfd *rfd;
123 assert (info->fd == -1);
127 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
129 info->fd = open (info->fname, O_RDONLY);
132 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
136 rfd = xmalloc (sizeof(*rfd));
143 flush current window to file if file is assocated with set
145 static void r_flush (RSFD rfd, int mk)
147 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
149 if (!info->fname && mk)
151 char *s = (char*) tempnam (NULL, "zrs");
153 info->fname = xmalloc (strlen(s)+1);
154 strcpy (info->fname, s);
156 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
157 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
160 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
164 if (info->fname && info->fd != -1 && info->dirty)
168 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
170 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
173 count = info->buf_size;
174 if (count > info->pos_end - info->pos_buf)
175 count = info->pos_end - info->pos_buf;
176 if ((r = write (info->fd, info->buf_mem, count)) < count)
179 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
181 logf (LOG_FATAL, "write of %ld but got %ld",
182 (long) count, (long) r);
189 static void r_close (RSFD rfd)
191 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
194 if (info->fname && info->fd != -1)
201 static void r_delete (struct rset_control *ct)
203 struct rset_temp_info *info = ct->buf;
206 unlink (info->fname);
207 free (info->buf_mem);
213 read from file to window if file is assocated with set -
216 static void r_reread (RSFD rfd)
218 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
224 info->pos_border = info->pos_cur + info->buf_size;
225 if (info->pos_border > info->pos_end)
226 info->pos_border = info->pos_end;
227 count = info->pos_border - info->pos_buf;
230 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
232 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
235 if ((r = read (info->fd, info->buf_mem, count)) < count)
238 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
240 logf (LOG_FATAL, "read of %ld but got %ld",
241 (long) count, (long) r);
247 info->pos_border = info->pos_end;
250 static void r_rewind (RSFD rfd)
252 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
260 static int r_count (struct rset_control *ct)
262 struct rset_temp_info *info = ct->buf;
264 return info->pos_end / info->key_size;
267 static int r_read (RSFD rfd, void *buf)
269 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
271 size_t nc = info->pos_cur + info->key_size;
273 if (nc > info->pos_border)
275 if (nc > info->pos_end)
278 info->pos_buf = info->pos_cur;
281 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
287 static int r_write (RSFD rfd, const void *buf)
289 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
291 size_t nc = info->pos_cur + info->key_size;
293 if (nc > info->pos_buf + info->buf_size)
296 info->pos_buf = info->pos_cur;
300 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
303 if (nc > info->pos_end)
304 info->pos_border = info->pos_end = nc;