2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1995-10-12 12:41:58 adam
8 * Private info (buf) moved from struct rset_control to struct rset.
9 * Bug fixes in relevance.
11 * Revision 1.14 1995/10/10 14:00:04 adam
12 * Function rset_open changed its wflag parameter to general flags.
14 * Revision 1.13 1995/10/06 14:38:06 adam
15 * New result set method: r_score.
16 * Local no (sysno) and score is transferred to retrieveCtrl.
18 * Revision 1.12 1995/09/28 09:52:11 adam
19 * xfree/xmalloc used everywhere.
21 * Revision 1.11 1995/09/18 14:17:56 adam
24 * Revision 1.10 1995/09/15 14:45:39 adam
27 * Revision 1.9 1995/09/15 09:20:42 adam
30 * Revision 1.8 1995/09/08 14:52:42 adam
31 * Work on relevance feedback.
33 * Revision 1.7 1995/09/07 13:58:44 adam
34 * New parameter: result-set file descriptor (RSFD) to support multiple
35 * positions within the same result-set.
36 * Boolean operators: and, or, not implemented.
38 * Revision 1.6 1995/09/06 16:11:56 adam
39 * More work on boolean sets.
41 * Revision 1.5 1995/09/05 16:36:59 adam
44 * Revision 1.4 1995/09/05 11:43:24 adam
45 * Complete version of temporary sets. Not tested yet though.
47 * Revision 1.3 1995/09/04 15:20:40 adam
48 * More work on temp sets. is_open member removed.
50 * Revision 1.2 1995/09/04 09:10:56 adam
53 * Revision 1.1 1994/11/04 13:21:30 quinn
61 #include <sys/types.h>
67 static void *r_create(const struct rset_control *sel, void *parms);
68 static RSFD r_open (RSET ct, int flag);
69 static void r_close (RSFD rfd);
70 static void r_delete (RSET ct);
71 static void r_rewind (RSFD rfd);
72 static int r_count (RSET ct);
73 static int r_read (RSFD rfd, void *buf);
74 static int r_write (RSFD rfd, const void *buf);
75 static int r_score (RSFD rfd, int *score);
77 static const rset_control control =
91 const rset_control *rset_kind_temp = &control;
93 struct rset_temp_info {
96 size_t key_size; /* key size */
97 char *buf_mem; /* window buffer */
98 size_t buf_size; /* size of window */
99 size_t pos_end; /* last position in set */
100 size_t pos_cur; /* current position in set */
101 size_t pos_buf; /* position of first byte in window */
102 size_t pos_border; /* position of last byte+1 in window */
103 int dirty; /* window is dirty */
106 struct rset_temp_rfd {
107 struct rset_temp_info *info;
108 struct rset_temp_rfd *next;
111 static void *r_create(const struct rset_control *sel, void *parms)
113 rset_temp_parms *temp_parms = parms;
114 struct rset_temp_info *info;
116 info = xmalloc (sizeof(struct rset_temp_info));
119 info->key_size = temp_parms->key_size;
120 info->buf_size = 4096;
121 info->buf_mem = xmalloc (info->buf_size);
130 static RSFD r_open (RSET ct, int flag)
132 struct rset_temp_info *info = ct->buf;
133 struct rset_temp_rfd *rfd;
135 assert (info->fd == -1);
138 if (flag & RSETF_WRITE)
139 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
141 info->fd = open (info->fname, O_RDONLY);
144 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
148 rfd = xmalloc (sizeof(*rfd));
155 flush current window to file if file is assocated with set
157 static void r_flush (RSFD rfd, int mk)
159 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
161 if (!info->fname && mk)
163 char *s = (char*) tempnam (NULL, "zrs");
165 info->fname = xmalloc (strlen(s)+1);
166 strcpy (info->fname, s);
168 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
169 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
172 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
176 if (info->fname && info->fd != -1 && info->dirty)
180 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
182 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
185 count = info->buf_size;
186 if (count > info->pos_end - info->pos_buf)
187 count = info->pos_end - info->pos_buf;
188 if ((r = write (info->fd, info->buf_mem, count)) < count)
191 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
193 logf (LOG_FATAL, "write of %ld but got %ld",
194 (long) count, (long) r);
201 static void r_close (RSFD rfd)
203 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
206 if (info->fname && info->fd != -1)
213 static void r_delete (RSET ct)
215 struct rset_temp_info *info = ct->buf;
218 unlink (info->fname);
219 free (info->buf_mem);
220 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
223 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
224 unlink (info->fname);
231 read from file to window if file is assocated with set -
234 static void r_reread (RSFD rfd)
236 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
242 info->pos_border = info->pos_cur + info->buf_size;
243 if (info->pos_border > info->pos_end)
244 info->pos_border = info->pos_end;
245 count = info->pos_border - info->pos_buf;
248 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
250 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
253 if ((r = read (info->fd, info->buf_mem, count)) < count)
256 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
258 logf (LOG_FATAL, "read of %ld but got %ld",
259 (long) count, (long) r);
265 info->pos_border = info->pos_end;
268 static void r_rewind (RSFD rfd)
270 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
278 static int r_count (RSET ct)
280 struct rset_temp_info *info = ct->buf;
282 return info->pos_end / info->key_size;
285 static int r_read (RSFD rfd, void *buf)
287 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
289 size_t nc = info->pos_cur + info->key_size;
291 if (nc > info->pos_border)
293 if (nc > info->pos_end)
296 info->pos_buf = info->pos_cur;
299 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
305 static int r_write (RSFD rfd, const void *buf)
307 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
309 size_t nc = info->pos_cur + info->key_size;
311 if (nc > info->pos_buf + info->buf_size)
314 info->pos_buf = info->pos_cur;
315 if (info->pos_buf < info->pos_end)
319 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
322 if (nc > info->pos_end)
323 info->pos_border = info->pos_end = nc;
327 static int r_score (RSFD rfd, int *score)