2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1995-09-28 09:52:11 adam
8 * xfree/xmalloc used everywhere.
10 * Revision 1.11 1995/09/18 14:17:56 adam
13 * Revision 1.10 1995/09/15 14:45:39 adam
16 * Revision 1.9 1995/09/15 09:20:42 adam
19 * Revision 1.8 1995/09/08 14:52:42 adam
20 * Work on relevance feedback.
22 * Revision 1.7 1995/09/07 13:58:44 adam
23 * New parameter: result-set file descriptor (RSFD) to support multiple
24 * positions within the same result-set.
25 * Boolean operators: and, or, not implemented.
27 * Revision 1.6 1995/09/06 16:11:56 adam
28 * More work on boolean sets.
30 * Revision 1.5 1995/09/05 16:36:59 adam
33 * Revision 1.4 1995/09/05 11:43:24 adam
34 * Complete version of temporary sets. Not tested yet though.
36 * Revision 1.3 1995/09/04 15:20:40 adam
37 * More work on temp sets. is_open member removed.
39 * Revision 1.2 1995/09/04 09:10:56 adam
42 * Revision 1.1 1994/11/04 13:21:30 quinn
50 #include <sys/types.h>
56 static rset_control *r_create(const struct rset_control *sel, void *parms);
57 static RSFD r_open (rset_control *ct, int wflag);
58 static void r_close (RSFD rfd);
59 static void r_delete (rset_control *ct);
60 static void r_rewind (RSFD rfd);
61 static int r_count (rset_control *ct);
62 static int r_read (RSFD rfd, void *buf);
63 static int r_write (RSFD rfd, const void *buf);
65 static const rset_control control =
79 const rset_control *rset_kind_temp = &control;
81 struct rset_temp_info {
84 size_t key_size; /* key size */
85 char *buf_mem; /* window buffer */
86 size_t buf_size; /* size of window */
87 size_t pos_end; /* last position in set */
88 size_t pos_cur; /* current position in set */
89 size_t pos_buf; /* position of first byte in window */
90 size_t pos_border; /* position of last byte+1 in window */
91 int dirty; /* window is dirty */
94 struct rset_temp_rfd {
95 struct rset_temp_info *info;
96 struct rset_temp_rfd *next;
99 static struct rset_control *r_create(const struct rset_control *sel,
103 rset_temp_parms *temp_parms = parms;
104 struct rset_temp_info *info;
106 newct = xmalloc(sizeof(*newct));
107 memcpy(newct, sel, sizeof(*sel));
108 newct->buf = xmalloc (sizeof(struct rset_temp_info));
113 info->key_size = temp_parms->key_size;
114 info->buf_size = 4096;
115 info->buf_mem = xmalloc (info->buf_size);
124 static RSFD r_open (struct rset_control *ct, int wflag)
126 struct rset_temp_info *info = ct->buf;
127 struct rset_temp_rfd *rfd;
129 assert (info->fd == -1);
133 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
135 info->fd = open (info->fname, O_RDONLY);
138 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
142 rfd = xmalloc (sizeof(*rfd));
149 flush current window to file if file is assocated with set
151 static void r_flush (RSFD rfd, int mk)
153 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
155 if (!info->fname && mk)
157 char *s = (char*) tempnam (NULL, "zrs");
159 info->fname = xmalloc (strlen(s)+1);
160 strcpy (info->fname, s);
162 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
163 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
166 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
170 if (info->fname && info->fd != -1 && info->dirty)
174 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
176 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
179 count = info->buf_size;
180 if (count > info->pos_end - info->pos_buf)
181 count = info->pos_end - info->pos_buf;
182 if ((r = write (info->fd, info->buf_mem, count)) < count)
185 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
187 logf (LOG_FATAL, "write of %ld but got %ld",
188 (long) count, (long) r);
195 static void r_close (RSFD rfd)
197 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
200 if (info->fname && info->fd != -1)
207 static void r_delete (struct rset_control *ct)
209 struct rset_temp_info *info = ct->buf;
212 unlink (info->fname);
213 free (info->buf_mem);
214 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
217 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
218 unlink (info->fname);
225 read from file to window if file is assocated with set -
228 static void r_reread (RSFD rfd)
230 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
236 info->pos_border = info->pos_cur + info->buf_size;
237 if (info->pos_border > info->pos_end)
238 info->pos_border = info->pos_end;
239 count = info->pos_border - info->pos_buf;
242 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
244 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
247 if ((r = read (info->fd, info->buf_mem, count)) < count)
250 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
252 logf (LOG_FATAL, "read of %ld but got %ld",
253 (long) count, (long) r);
259 info->pos_border = info->pos_end;
262 static void r_rewind (RSFD rfd)
264 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
272 static int r_count (struct rset_control *ct)
274 struct rset_temp_info *info = ct->buf;
276 return info->pos_end / info->key_size;
279 static int r_read (RSFD rfd, void *buf)
281 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
283 size_t nc = info->pos_cur + info->key_size;
285 if (nc > info->pos_border)
287 if (nc > info->pos_end)
290 info->pos_buf = info->pos_cur;
293 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
299 static int r_write (RSFD rfd, const void *buf)
301 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
303 size_t nc = info->pos_cur + info->key_size;
305 if (nc > info->pos_buf + info->buf_size)
308 info->pos_buf = info->pos_cur;
309 if (info->pos_buf < info->pos_end)
313 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
316 if (nc > info->pos_end)
317 info->pos_border = info->pos_end = nc;