2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1995-09-15 09:20:42 adam
10 * Revision 1.8 1995/09/08 14:52:42 adam
11 * Work on relevance feedback.
13 * Revision 1.7 1995/09/07 13:58:44 adam
14 * New parameter: result-set file descriptor (RSFD) to support multiple
15 * positions within the same result-set.
16 * Boolean operators: and, or, not implemented.
18 * Revision 1.6 1995/09/06 16:11:56 adam
19 * More work on boolean sets.
21 * Revision 1.5 1995/09/05 16:36:59 adam
24 * Revision 1.4 1995/09/05 11:43:24 adam
25 * Complete version of temporary sets. Not tested yet though.
27 * Revision 1.3 1995/09/04 15:20:40 adam
28 * More work on temp sets. is_open member removed.
30 * Revision 1.2 1995/09/04 09:10:56 adam
33 * Revision 1.1 1994/11/04 13:21:30 quinn
41 #include <sys/types.h>
47 static rset_control *r_create(const struct rset_control *sel, void *parms);
48 static RSFD r_open (rset_control *ct, int wflag);
49 static void r_close (RSFD rfd);
50 static void r_delete (rset_control *ct);
51 static void r_rewind (RSFD rfd);
52 static int r_count (rset_control *ct);
53 static int r_read (RSFD rfd, void *buf);
54 static int r_write (RSFD rfd, const void *buf);
56 static const rset_control control =
70 const rset_control *rset_kind_temp = &control;
72 struct rset_temp_info {
75 size_t key_size; /* key size */
76 char *buf_mem; /* window buffer */
77 size_t buf_size; /* size of window */
78 size_t pos_end; /* last position in set */
79 size_t pos_cur; /* current position in set */
80 size_t pos_buf; /* position of first byte in window */
81 size_t pos_border; /* position of last byte+1 in window */
82 int dirty; /* window is dirty */
85 struct rset_temp_rfd {
86 struct rset_temp_info *info;
87 struct rset_temp_rfd *next;
90 static struct rset_control *r_create(const struct rset_control *sel,
94 rset_temp_parms *temp_parms = parms;
95 struct rset_temp_info *info;
97 newct = xmalloc(sizeof(*newct));
98 memcpy(newct, sel, sizeof(*sel));
99 newct->buf = xmalloc (sizeof(struct rset_temp_info));
104 info->key_size = temp_parms->key_size;
105 info->buf_size = 1024;
106 info->buf_mem = xmalloc (info->buf_size);
115 static RSFD r_open (struct rset_control *ct, int wflag)
117 struct rset_temp_info *info = ct->buf;
118 struct rset_temp_rfd *rfd;
120 assert (info->fd == -1);
124 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
126 info->fd = open (info->fname, O_RDONLY);
129 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
133 rfd = xmalloc (sizeof(*rfd));
140 flush current window to file if file is assocated with set
142 static void r_flush (RSFD rfd, int mk)
144 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
146 if (!info->fname && mk)
148 char *s = (char*) tempnam (NULL, "zrs");
150 info->fname = xmalloc (strlen(s)+1);
151 strcpy (info->fname, s);
153 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
156 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
160 if (info->fname && info->fd != -1 && info->dirty)
164 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
166 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
169 count = info->buf_size;
170 if (count > info->pos_end - info->pos_buf)
171 count = info->pos_end - info->pos_buf;
172 if ((r = write (info->fd, info->buf_mem, count)) < count)
175 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
177 logf (LOG_FATAL, "write of %ld but got %ld",
178 (long) count, (long) r);
185 static void r_close (RSFD rfd)
187 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
190 if (info->fname && info->fd != -1)
197 static void r_delete (struct rset_control *ct)
199 struct rset_temp_info *info = ct->buf;
202 unlink (info->fname);
203 free (info->buf_mem);
209 read from file to window if file is assocated with set -
212 static void r_reread (RSFD rfd)
214 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
220 info->pos_border = info->pos_cur + info->buf_size;
221 if (info->pos_border > info->pos_end)
222 info->pos_border = info->pos_end;
223 count = info->pos_border - info->pos_buf;
225 if ((r = read (info->fd, info->buf_mem, count)) < count)
228 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
230 logf (LOG_FATAL, "read of %ld but got %ld",
231 (long) count, (long) r);
236 info->pos_border = info->pos_end;
239 static void r_rewind (RSFD rfd)
241 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
249 static int r_count (struct rset_control *ct)
251 struct rset_temp_info *info = ct->buf;
253 return info->pos_end / info->key_size;
256 static int r_read (RSFD rfd, void *buf)
258 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
260 size_t nc = info->pos_cur + info->key_size;
262 if (nc > info->pos_border)
264 if (nc > info->pos_end)
267 info->pos_buf = info->pos_cur;
270 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
276 static int r_write (RSFD rfd, const void *buf)
278 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
280 size_t nc = info->pos_cur + info->key_size;
282 if (nc > info->pos_buf + info->buf_size)
285 info->pos_buf = info->pos_cur;
289 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
292 if (nc > info->pos_end)
293 info->pos_border = info->pos_end = nc;