2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1995-09-18 14:17:56 adam
10 * Revision 1.10 1995/09/15 14:45:39 adam
13 * Revision 1.9 1995/09/15 09:20:42 adam
16 * Revision 1.8 1995/09/08 14:52:42 adam
17 * Work on relevance feedback.
19 * Revision 1.7 1995/09/07 13:58:44 adam
20 * New parameter: result-set file descriptor (RSFD) to support multiple
21 * positions within the same result-set.
22 * Boolean operators: and, or, not implemented.
24 * Revision 1.6 1995/09/06 16:11:56 adam
25 * More work on boolean sets.
27 * Revision 1.5 1995/09/05 16:36:59 adam
30 * Revision 1.4 1995/09/05 11:43:24 adam
31 * Complete version of temporary sets. Not tested yet though.
33 * Revision 1.3 1995/09/04 15:20:40 adam
34 * More work on temp sets. is_open member removed.
36 * Revision 1.2 1995/09/04 09:10:56 adam
39 * Revision 1.1 1994/11/04 13:21:30 quinn
47 #include <sys/types.h>
53 static rset_control *r_create(const struct rset_control *sel, void *parms);
54 static RSFD r_open (rset_control *ct, int wflag);
55 static void r_close (RSFD rfd);
56 static void r_delete (rset_control *ct);
57 static void r_rewind (RSFD rfd);
58 static int r_count (rset_control *ct);
59 static int r_read (RSFD rfd, void *buf);
60 static int r_write (RSFD rfd, const void *buf);
62 static const rset_control control =
76 const rset_control *rset_kind_temp = &control;
78 struct rset_temp_info {
81 size_t key_size; /* key size */
82 char *buf_mem; /* window buffer */
83 size_t buf_size; /* size of window */
84 size_t pos_end; /* last position in set */
85 size_t pos_cur; /* current position in set */
86 size_t pos_buf; /* position of first byte in window */
87 size_t pos_border; /* position of last byte+1 in window */
88 int dirty; /* window is dirty */
91 struct rset_temp_rfd {
92 struct rset_temp_info *info;
93 struct rset_temp_rfd *next;
96 static struct rset_control *r_create(const struct rset_control *sel,
100 rset_temp_parms *temp_parms = parms;
101 struct rset_temp_info *info;
103 newct = xmalloc(sizeof(*newct));
104 memcpy(newct, sel, sizeof(*sel));
105 newct->buf = xmalloc (sizeof(struct rset_temp_info));
110 info->key_size = temp_parms->key_size;
111 info->buf_size = 4096;
112 info->buf_mem = xmalloc (info->buf_size);
121 static RSFD r_open (struct rset_control *ct, int wflag)
123 struct rset_temp_info *info = ct->buf;
124 struct rset_temp_rfd *rfd;
126 assert (info->fd == -1);
130 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
132 info->fd = open (info->fname, O_RDONLY);
135 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
139 rfd = xmalloc (sizeof(*rfd));
146 flush current window to file if file is assocated with set
148 static void r_flush (RSFD rfd, int mk)
150 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
152 if (!info->fname && mk)
154 char *s = (char*) tempnam (NULL, "zrs");
156 info->fname = xmalloc (strlen(s)+1);
157 strcpy (info->fname, s);
159 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
160 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
163 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
167 if (info->fname && info->fd != -1 && info->dirty)
171 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
173 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
176 count = info->buf_size;
177 if (count > info->pos_end - info->pos_buf)
178 count = info->pos_end - info->pos_buf;
179 if ((r = write (info->fd, info->buf_mem, count)) < count)
182 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
184 logf (LOG_FATAL, "write of %ld but got %ld",
185 (long) count, (long) r);
192 static void r_close (RSFD rfd)
194 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
197 if (info->fname && info->fd != -1)
204 static void r_delete (struct rset_control *ct)
206 struct rset_temp_info *info = ct->buf;
209 unlink (info->fname);
210 free (info->buf_mem);
216 read from file to window if file is assocated with set -
219 static void r_reread (RSFD rfd)
221 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
227 info->pos_border = info->pos_cur + info->buf_size;
228 if (info->pos_border > info->pos_end)
229 info->pos_border = info->pos_end;
230 count = info->pos_border - info->pos_buf;
233 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
235 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
238 if ((r = read (info->fd, info->buf_mem, count)) < count)
241 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
243 logf (LOG_FATAL, "read of %ld but got %ld",
244 (long) count, (long) r);
250 info->pos_border = info->pos_end;
253 static void r_rewind (RSFD rfd)
255 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
263 static int r_count (struct rset_control *ct)
265 struct rset_temp_info *info = ct->buf;
267 return info->pos_end / info->key_size;
270 static int r_read (RSFD rfd, void *buf)
272 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
274 size_t nc = info->pos_cur + info->key_size;
276 if (nc > info->pos_border)
278 if (nc > info->pos_end)
281 info->pos_buf = info->pos_cur;
284 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
290 static int r_write (RSFD rfd, const void *buf)
292 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
294 size_t nc = info->pos_cur + info->key_size;
296 if (nc > info->pos_buf + info->buf_size)
299 info->pos_buf = info->pos_cur;
300 if (info->pos_buf < info->pos_end)
304 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
307 if (nc > info->pos_end)
308 info->pos_border = info->pos_end = nc;