2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-09-08 14:52:42 adam
8 * Work on relevance feedback.
10 * Revision 1.7 1995/09/07 13:58:44 adam
11 * New parameter: result-set file descriptor (RSFD) to support multiple
12 * positions within the same result-set.
13 * Boolean operators: and, or, not implemented.
15 * Revision 1.6 1995/09/06 16:11:56 adam
16 * More work on boolean sets.
18 * Revision 1.5 1995/09/05 16:36:59 adam
21 * Revision 1.4 1995/09/05 11:43:24 adam
22 * Complete version of temporary sets. Not tested yet though.
24 * Revision 1.3 1995/09/04 15:20:40 adam
25 * More work on temp sets. is_open member removed.
27 * Revision 1.2 1995/09/04 09:10:56 adam
30 * Revision 1.1 1994/11/04 13:21:30 quinn
38 #include <sys/types.h>
44 static rset_control *r_create(const struct rset_control *sel, void *parms);
45 static RSFD r_open (rset_control *ct, int wflag);
46 static void r_close (RSFD rfd);
47 static void r_delete (rset_control *ct);
48 static void r_rewind (RSFD rfd);
49 static int r_count (rset_control *ct);
50 static int r_read (RSFD rfd, void *buf);
51 static int r_write (RSFD rfd, const void *buf);
53 static const rset_control control =
67 const rset_control *rset_kind_temp = &control;
69 struct rset_temp_info {
82 struct rset_temp_rfd {
83 struct rset_temp_info *info;
84 struct rset_temp_rfd *next;
87 static struct rset_control *r_create(const struct rset_control *sel,
91 rset_temp_parms *temp_parms = parms;
92 struct rset_temp_info *info;
94 newct = xmalloc(sizeof(*newct));
95 memcpy(newct, sel, sizeof(*sel));
96 newct->buf = xmalloc (sizeof(struct rset_temp_info));
101 info->key_size = temp_parms->key_size;
102 info->buf_size = 1024;
103 info->buf_mem = xmalloc (info->buf_size);
112 static RSFD r_open (struct rset_control *ct, int wflag)
114 struct rset_temp_info *info = ct->buf;
115 struct rset_temp_rfd *rfd;
117 assert (info->fd == -1);
121 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
123 info->fd = open (info->fname, O_RDONLY);
126 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
130 rfd = xmalloc (sizeof(*rfd));
136 static void r_flush (RSFD rfd, int mk)
138 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
140 if (!info->fname && mk)
142 char *s = (char*) tempnam (NULL, "zrs");
144 info->fname = xmalloc (strlen(s)+1);
145 strcpy (info->fname, s);
147 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
150 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
154 if (info->fname && info->fd != -1 && info->dirty)
158 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
160 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
163 count = info->buf_size;
164 if (count > info->pos_end - info->pos_buf)
165 count = info->pos_end - info->pos_buf;
166 if ((r = write (info->fd, info->buf_mem, count)) < count)
169 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
171 logf (LOG_FATAL, "write of %ld but got %ld",
172 (long) count, (long) r);
179 static void r_close (RSFD rfd)
181 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
184 if (info->fname && info->fd != -1)
191 static void r_delete (struct rset_control *ct)
193 struct rset_temp_info *info = ct->buf;
197 unlink (info->fname);
198 free (info->buf_mem);
203 static void r_reread (RSFD rfd)
205 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
211 info->pos_border = info->pos_cur + info->buf_size;
212 if (info->pos_border > info->pos_end)
213 info->pos_border = info->pos_end;
214 count = info->pos_border - info->pos_buf;
216 if ((r = read (info->fd, info->buf_mem, count)) < count)
219 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
221 logf (LOG_FATAL, "read of %ld but got %ld",
222 (long) count, (long) r);
227 info->pos_border = info->pos_end;
230 static void r_rewind (RSFD rfd)
232 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
240 static int r_count (struct rset_control *ct)
242 struct rset_temp_info *info = ct->buf;
244 return info->pos_end / info->key_size;
247 static int r_read (RSFD rfd, void *buf)
249 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
251 size_t nc = info->pos_cur + info->key_size;
253 if (nc > info->pos_border)
255 if (nc > info->pos_end)
258 info->pos_buf = info->pos_cur;
261 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
267 static int r_write (RSFD rfd, const 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_buf + info->buf_size)
276 info->pos_buf = info->pos_cur;
279 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
282 info->pos_border = info->pos_cur = nc;