2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1995-12-11 09:15:28 adam
8 * New set types: sand/sor/snot - ranked versions of and/or/not in
9 * ranked/semi-ranked result sets.
10 * Note: the snot not finished yet.
11 * New rset member: flag.
12 * Bug fix: r_delete in rsrel.c did free bad memory block.
14 * Revision 1.16 1995/11/28 14:47:02 adam
15 * New setting: tempSetPath. Location of temporary result sets.
17 * Revision 1.15 1995/10/12 12:41:58 adam
18 * Private info (buf) moved from struct rset_control to struct rset.
19 * Bug fixes in relevance.
21 * Revision 1.14 1995/10/10 14:00:04 adam
22 * Function rset_open changed its wflag parameter to general flags.
24 * Revision 1.13 1995/10/06 14:38:06 adam
25 * New result set method: r_score.
26 * Local no (sysno) and score is transferred to retrieveCtrl.
28 * Revision 1.12 1995/09/28 09:52:11 adam
29 * xfree/xmalloc used everywhere.
31 * Revision 1.11 1995/09/18 14:17:56 adam
34 * Revision 1.10 1995/09/15 14:45:39 adam
37 * Revision 1.9 1995/09/15 09:20:42 adam
40 * Revision 1.8 1995/09/08 14:52:42 adam
41 * Work on relevance feedback.
43 * Revision 1.7 1995/09/07 13:58:44 adam
44 * New parameter: result-set file descriptor (RSFD) to support multiple
45 * positions within the same result-set.
46 * Boolean operators: and, or, not implemented.
48 * Revision 1.6 1995/09/06 16:11:56 adam
49 * More work on boolean sets.
51 * Revision 1.5 1995/09/05 16:36:59 adam
54 * Revision 1.4 1995/09/05 11:43:24 adam
55 * Complete version of temporary sets. Not tested yet though.
57 * Revision 1.3 1995/09/04 15:20:40 adam
58 * More work on temp sets. is_open member removed.
60 * Revision 1.2 1995/09/04 09:10:56 adam
63 * Revision 1.1 1994/11/04 13:21:30 quinn
71 #include <sys/types.h>
77 static void *r_create(const struct rset_control *sel, void *parms,
79 static RSFD r_open (RSET ct, int flag);
80 static void r_close (RSFD rfd);
81 static void r_delete (RSET ct);
82 static void r_rewind (RSFD rfd);
83 static int r_count (RSET ct);
84 static int r_read (RSFD rfd, void *buf);
85 static int r_write (RSFD rfd, const void *buf);
86 static int r_score (RSFD rfd, int *score);
88 static int temppath_init = 0;
89 static char *temppath_root = NULL;
91 static const rset_control control =
105 const rset_control *rset_kind_temp = &control;
107 struct rset_temp_info {
110 size_t key_size; /* key size */
111 char *buf_mem; /* window buffer */
112 size_t buf_size; /* size of window */
113 size_t pos_end; /* last position in set */
114 size_t pos_cur; /* current position in set */
115 size_t pos_buf; /* position of first byte in window */
116 size_t pos_border; /* position of last byte+1 in window */
117 int dirty; /* window is dirty */
120 struct rset_temp_rfd {
121 struct rset_temp_info *info;
122 struct rset_temp_rfd *next;
125 static void *r_create(const struct rset_control *sel, void *parms, int *flags)
127 rset_temp_parms *temp_parms = parms;
128 struct rset_temp_info *info;
130 info = xmalloc (sizeof(struct rset_temp_info));
133 info->key_size = temp_parms->key_size;
134 info->buf_size = 4096;
135 info->buf_mem = xmalloc (info->buf_size);
144 temppath_root = res_get (common_resource, "tempSetPath");
149 static RSFD r_open (RSET ct, int flag)
151 struct rset_temp_info *info = ct->buf;
152 struct rset_temp_rfd *rfd;
154 assert (info->fd == -1);
157 if (flag & RSETF_WRITE)
158 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
160 info->fd = open (info->fname, O_RDONLY);
163 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
167 rfd = xmalloc (sizeof(*rfd));
174 flush current window to file if file is assocated with set
176 static void r_flush (RSFD rfd, int mk)
178 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
180 if (!info->fname && mk)
182 char *s = (char*) tempnam (temppath_root, "zrs");
184 info->fname = xmalloc (strlen(s)+1);
185 strcpy (info->fname, s);
187 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
188 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
191 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
195 if (info->fname && info->fd != -1 && info->dirty)
199 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
201 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
204 count = info->buf_size;
205 if (count > info->pos_end - info->pos_buf)
206 count = info->pos_end - info->pos_buf;
207 if ((r = write (info->fd, info->buf_mem, count)) < count)
210 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
212 logf (LOG_FATAL, "write of %ld but got %ld",
213 (long) count, (long) r);
220 static void r_close (RSFD rfd)
222 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
225 if (info->fname && info->fd != -1)
232 static void r_delete (RSET ct)
234 struct rset_temp_info *info = ct->buf;
237 unlink (info->fname);
238 free (info->buf_mem);
239 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
242 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
243 unlink (info->fname);
250 read from file to window if file is assocated with set -
253 static void r_reread (RSFD rfd)
255 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
261 info->pos_border = info->pos_cur + info->buf_size;
262 if (info->pos_border > info->pos_end)
263 info->pos_border = info->pos_end;
264 count = info->pos_border - info->pos_buf;
267 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
269 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
272 if ((r = read (info->fd, info->buf_mem, count)) < count)
275 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
277 logf (LOG_FATAL, "read of %ld but got %ld",
278 (long) count, (long) r);
284 info->pos_border = info->pos_end;
287 static void r_rewind (RSFD rfd)
289 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
297 static int r_count (RSET ct)
299 struct rset_temp_info *info = ct->buf;
301 return info->pos_end / info->key_size;
304 static int r_read (RSFD rfd, void *buf)
306 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
308 size_t nc = info->pos_cur + info->key_size;
310 if (nc > info->pos_border)
312 if (nc > info->pos_end)
315 info->pos_buf = info->pos_cur;
318 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
324 static int r_write (RSFD rfd, const void *buf)
326 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
328 size_t nc = info->pos_cur + info->key_size;
330 if (nc > info->pos_buf + info->buf_size)
333 info->pos_buf = info->pos_cur;
334 if (info->pos_buf < info->pos_end)
338 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
341 if (nc > info->pos_end)
342 info->pos_border = info->pos_end = nc;
346 static int r_score (RSFD rfd, int *score)