2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1996-10-29 13:54:52 adam
8 * Changed name of setting tempSetDir to setTmpDir.
10 * Revision 1.17 1995/12/11 09:15:28 adam
11 * New set types: sand/sor/snot - ranked versions of and/or/not in
12 * ranked/semi-ranked result sets.
13 * Note: the snot not finished yet.
14 * New rset member: flag.
15 * Bug fix: r_delete in rsrel.c did free bad memory block.
17 * Revision 1.16 1995/11/28 14:47:02 adam
18 * New setting: tempSetPath. Location of temporary result sets.
20 * Revision 1.15 1995/10/12 12:41:58 adam
21 * Private info (buf) moved from struct rset_control to struct rset.
22 * Bug fixes in relevance.
24 * Revision 1.14 1995/10/10 14:00:04 adam
25 * Function rset_open changed its wflag parameter to general flags.
27 * Revision 1.13 1995/10/06 14:38:06 adam
28 * New result set method: r_score.
29 * Local no (sysno) and score is transferred to retrieveCtrl.
31 * Revision 1.12 1995/09/28 09:52:11 adam
32 * xfree/xmalloc used everywhere.
34 * Revision 1.11 1995/09/18 14:17:56 adam
37 * Revision 1.10 1995/09/15 14:45:39 adam
40 * Revision 1.9 1995/09/15 09:20:42 adam
43 * Revision 1.8 1995/09/08 14:52:42 adam
44 * Work on relevance feedback.
46 * Revision 1.7 1995/09/07 13:58:44 adam
47 * New parameter: result-set file descriptor (RSFD) to support multiple
48 * positions within the same result-set.
49 * Boolean operators: and, or, not implemented.
51 * Revision 1.6 1995/09/06 16:11:56 adam
52 * More work on boolean sets.
54 * Revision 1.5 1995/09/05 16:36:59 adam
57 * Revision 1.4 1995/09/05 11:43:24 adam
58 * Complete version of temporary sets. Not tested yet though.
60 * Revision 1.3 1995/09/04 15:20:40 adam
61 * More work on temp sets. is_open member removed.
63 * Revision 1.2 1995/09/04 09:10:56 adam
66 * Revision 1.1 1994/11/04 13:21:30 quinn
74 #include <sys/types.h>
80 static void *r_create(const struct rset_control *sel, void *parms,
82 static RSFD r_open (RSET ct, int flag);
83 static void r_close (RSFD rfd);
84 static void r_delete (RSET ct);
85 static void r_rewind (RSFD rfd);
86 static int r_count (RSET ct);
87 static int r_read (RSFD rfd, void *buf);
88 static int r_write (RSFD rfd, const void *buf);
89 static int r_score (RSFD rfd, int *score);
91 static int temppath_init = 0;
92 static char *temppath_root = NULL;
94 static const rset_control control =
108 const rset_control *rset_kind_temp = &control;
110 struct rset_temp_info {
113 size_t key_size; /* key size */
114 char *buf_mem; /* window buffer */
115 size_t buf_size; /* size of window */
116 size_t pos_end; /* last position in set */
117 size_t pos_cur; /* current position in set */
118 size_t pos_buf; /* position of first byte in window */
119 size_t pos_border; /* position of last byte+1 in window */
120 int dirty; /* window is dirty */
123 struct rset_temp_rfd {
124 struct rset_temp_info *info;
125 struct rset_temp_rfd *next;
128 static void *r_create(const struct rset_control *sel, void *parms, int *flags)
130 rset_temp_parms *temp_parms = parms;
131 struct rset_temp_info *info;
133 info = xmalloc (sizeof(struct rset_temp_info));
136 info->key_size = temp_parms->key_size;
137 info->buf_size = 4096;
138 info->buf_mem = xmalloc (info->buf_size);
147 temppath_root = res_get (common_resource, "setTmpDir");
152 static RSFD r_open (RSET ct, int flag)
154 struct rset_temp_info *info = ct->buf;
155 struct rset_temp_rfd *rfd;
157 assert (info->fd == -1);
160 if (flag & RSETF_WRITE)
161 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
163 info->fd = open (info->fname, O_RDONLY);
166 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
170 rfd = xmalloc (sizeof(*rfd));
177 flush current window to file if file is assocated with set
179 static void r_flush (RSFD rfd, int mk)
181 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
183 if (!info->fname && mk)
185 char *s = (char*) tempnam (temppath_root, "zrs");
187 info->fname = xmalloc (strlen(s)+1);
188 strcpy (info->fname, s);
190 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
191 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
194 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
198 if (info->fname && info->fd != -1 && info->dirty)
202 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
204 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
207 count = info->buf_size;
208 if (count > info->pos_end - info->pos_buf)
209 count = info->pos_end - info->pos_buf;
210 if ((r = write (info->fd, info->buf_mem, count)) < count)
213 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
215 logf (LOG_FATAL, "write of %ld but got %ld",
216 (long) count, (long) r);
223 static void r_close (RSFD rfd)
225 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
228 if (info->fname && info->fd != -1)
235 static void r_delete (RSET ct)
237 struct rset_temp_info *info = ct->buf;
240 unlink (info->fname);
241 free (info->buf_mem);
242 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
245 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
246 unlink (info->fname);
253 read from file to window if file is assocated with set -
256 static void r_reread (RSFD rfd)
258 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
264 info->pos_border = info->pos_cur + info->buf_size;
265 if (info->pos_border > info->pos_end)
266 info->pos_border = info->pos_end;
267 count = info->pos_border - info->pos_buf;
270 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
272 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
275 if ((r = read (info->fd, info->buf_mem, count)) < count)
278 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
280 logf (LOG_FATAL, "read of %ld but got %ld",
281 (long) count, (long) r);
287 info->pos_border = info->pos_end;
290 static void r_rewind (RSFD rfd)
292 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
300 static int r_count (RSET ct)
302 struct rset_temp_info *info = ct->buf;
304 return info->pos_end / info->key_size;
307 static int r_read (RSFD rfd, void *buf)
309 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
311 size_t nc = info->pos_cur + info->key_size;
313 if (nc > info->pos_border)
315 if (nc > info->pos_end)
318 info->pos_buf = info->pos_cur;
321 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
327 static int r_write (RSFD rfd, const void *buf)
329 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
331 size_t nc = info->pos_cur + info->key_size;
333 if (nc > info->pos_buf + info->buf_size)
336 info->pos_buf = info->pos_cur;
337 if (info->pos_buf < info->pos_end)
341 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
344 if (nc > info->pos_end)
345 info->pos_border = info->pos_end = nc;
349 static int r_score (RSFD rfd, int *score)