2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1995-10-10 14:00:04 adam
8 * Function rset_open changed its wflag parameter to general flags.
10 * Revision 1.13 1995/10/06 14:38:06 adam
11 * New result set method: r_score.
12 * Local no (sysno) and score is transferred to retrieveCtrl.
14 * Revision 1.12 1995/09/28 09:52:11 adam
15 * xfree/xmalloc used everywhere.
17 * Revision 1.11 1995/09/18 14:17:56 adam
20 * Revision 1.10 1995/09/15 14:45:39 adam
23 * Revision 1.9 1995/09/15 09:20:42 adam
26 * Revision 1.8 1995/09/08 14:52:42 adam
27 * Work on relevance feedback.
29 * Revision 1.7 1995/09/07 13:58:44 adam
30 * New parameter: result-set file descriptor (RSFD) to support multiple
31 * positions within the same result-set.
32 * Boolean operators: and, or, not implemented.
34 * Revision 1.6 1995/09/06 16:11:56 adam
35 * More work on boolean sets.
37 * Revision 1.5 1995/09/05 16:36:59 adam
40 * Revision 1.4 1995/09/05 11:43:24 adam
41 * Complete version of temporary sets. Not tested yet though.
43 * Revision 1.3 1995/09/04 15:20:40 adam
44 * More work on temp sets. is_open member removed.
46 * Revision 1.2 1995/09/04 09:10:56 adam
49 * Revision 1.1 1994/11/04 13:21:30 quinn
57 #include <sys/types.h>
63 static rset_control *r_create(const struct rset_control *sel, void *parms);
64 static RSFD r_open (rset_control *ct, int flag);
65 static void r_close (RSFD rfd);
66 static void r_delete (rset_control *ct);
67 static void r_rewind (RSFD rfd);
68 static int r_count (rset_control *ct);
69 static int r_read (RSFD rfd, void *buf);
70 static int r_write (RSFD rfd, const void *buf);
71 static int r_score (RSFD rfd, int *score);
73 static const rset_control control =
88 const rset_control *rset_kind_temp = &control;
90 struct rset_temp_info {
93 size_t key_size; /* key size */
94 char *buf_mem; /* window buffer */
95 size_t buf_size; /* size of window */
96 size_t pos_end; /* last position in set */
97 size_t pos_cur; /* current position in set */
98 size_t pos_buf; /* position of first byte in window */
99 size_t pos_border; /* position of last byte+1 in window */
100 int dirty; /* window is dirty */
103 struct rset_temp_rfd {
104 struct rset_temp_info *info;
105 struct rset_temp_rfd *next;
108 static struct rset_control *r_create(const struct rset_control *sel,
112 rset_temp_parms *temp_parms = parms;
113 struct rset_temp_info *info;
115 newct = xmalloc(sizeof(*newct));
116 memcpy(newct, sel, sizeof(*sel));
117 newct->buf = xmalloc (sizeof(struct rset_temp_info));
122 info->key_size = temp_parms->key_size;
123 info->buf_size = 4096;
124 info->buf_mem = xmalloc (info->buf_size);
133 static RSFD r_open (struct rset_control *ct, int flag)
135 struct rset_temp_info *info = ct->buf;
136 struct rset_temp_rfd *rfd;
138 assert (info->fd == -1);
141 if (flag & RSETF_WRITE)
142 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
144 info->fd = open (info->fname, O_RDONLY);
147 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
151 rfd = xmalloc (sizeof(*rfd));
158 flush current window to file if file is assocated with set
160 static void r_flush (RSFD rfd, int mk)
162 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
164 if (!info->fname && mk)
166 char *s = (char*) tempnam (NULL, "zrs");
168 info->fname = xmalloc (strlen(s)+1);
169 strcpy (info->fname, s);
171 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
172 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
175 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
179 if (info->fname && info->fd != -1 && info->dirty)
183 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
185 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
188 count = info->buf_size;
189 if (count > info->pos_end - info->pos_buf)
190 count = info->pos_end - info->pos_buf;
191 if ((r = write (info->fd, info->buf_mem, count)) < count)
194 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
196 logf (LOG_FATAL, "write of %ld but got %ld",
197 (long) count, (long) r);
204 static void r_close (RSFD rfd)
206 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
209 if (info->fname && info->fd != -1)
216 static void r_delete (struct rset_control *ct)
218 struct rset_temp_info *info = ct->buf;
221 unlink (info->fname);
222 free (info->buf_mem);
223 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
226 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
227 unlink (info->fname);
234 read from file to window if file is assocated with set -
237 static void r_reread (RSFD rfd)
239 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
245 info->pos_border = info->pos_cur + info->buf_size;
246 if (info->pos_border > info->pos_end)
247 info->pos_border = info->pos_end;
248 count = info->pos_border - info->pos_buf;
251 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
253 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
256 if ((r = read (info->fd, info->buf_mem, count)) < count)
259 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
261 logf (LOG_FATAL, "read of %ld but got %ld",
262 (long) count, (long) r);
268 info->pos_border = info->pos_end;
271 static void r_rewind (RSFD rfd)
273 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
281 static int r_count (struct rset_control *ct)
283 struct rset_temp_info *info = ct->buf;
285 return info->pos_end / info->key_size;
288 static int r_read (RSFD rfd, void *buf)
290 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
292 size_t nc = info->pos_cur + info->key_size;
294 if (nc > info->pos_border)
296 if (nc > info->pos_end)
299 info->pos_buf = info->pos_cur;
302 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
308 static int r_write (RSFD rfd, const void *buf)
310 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
312 size_t nc = info->pos_cur + info->key_size;
314 if (nc > info->pos_buf + info->buf_size)
317 info->pos_buf = info->pos_cur;
318 if (info->pos_buf < info->pos_end)
322 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
325 if (nc > info->pos_end)
326 info->pos_border = info->pos_end = nc;
330 static int r_score (RSFD rfd, int *score)