2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1997-09-09 13:38:17 adam
8 * Partial port to WIN95/NT.
10 * Revision 1.19 1997/09/04 13:58:57 adam
11 * Added O_BINARY for open calls.
13 * Revision 1.18 1996/10/29 13:54:52 adam
14 * Changed name of setting tempSetDir to setTmpDir.
16 * Revision 1.17 1995/12/11 09:15:28 adam
17 * New set types: sand/sor/snot - ranked versions of and/or/not in
18 * ranked/semi-ranked result sets.
19 * Note: the snot not finished yet.
20 * New rset member: flag.
21 * Bug fix: r_delete in rsrel.c did free bad memory block.
23 * Revision 1.16 1995/11/28 14:47:02 adam
24 * New setting: tempSetPath. Location of temporary result sets.
26 * Revision 1.15 1995/10/12 12:41:58 adam
27 * Private info (buf) moved from struct rset_control to struct rset.
28 * Bug fixes in relevance.
30 * Revision 1.14 1995/10/10 14:00:04 adam
31 * Function rset_open changed its wflag parameter to general flags.
33 * Revision 1.13 1995/10/06 14:38:06 adam
34 * New result set method: r_score.
35 * Local no (sysno) and score is transferred to retrieveCtrl.
37 * Revision 1.12 1995/09/28 09:52:11 adam
38 * xfree/xmalloc used everywhere.
40 * Revision 1.11 1995/09/18 14:17:56 adam
43 * Revision 1.10 1995/09/15 14:45:39 adam
46 * Revision 1.9 1995/09/15 09:20:42 adam
49 * Revision 1.8 1995/09/08 14:52:42 adam
50 * Work on relevance feedback.
52 * Revision 1.7 1995/09/07 13:58:44 adam
53 * New parameter: result-set file descriptor (RSFD) to support multiple
54 * positions within the same result-set.
55 * Boolean operators: and, or, not implemented.
57 * Revision 1.6 1995/09/06 16:11:56 adam
58 * More work on boolean sets.
60 * Revision 1.5 1995/09/05 16:36:59 adam
63 * Revision 1.4 1995/09/05 11:43:24 adam
64 * Complete version of temporary sets. Not tested yet though.
66 * Revision 1.3 1995/09/04 15:20:40 adam
67 * More work on temp sets. is_open member removed.
69 * Revision 1.2 1995/09/04 09:10:56 adam
72 * Revision 1.1 1994/11/04 13:21:30 quinn
85 #include <sys/types.h>
91 static void *r_create(const struct rset_control *sel, void *parms,
93 static RSFD r_open (RSET ct, int flag);
94 static void r_close (RSFD rfd);
95 static void r_delete (RSET ct);
96 static void r_rewind (RSFD rfd);
97 static int r_count (RSET ct);
98 static int r_read (RSFD rfd, void *buf);
99 static int r_write (RSFD rfd, const void *buf);
100 static int r_score (RSFD rfd, int *score);
102 static int temppath_init = 0;
103 static char *temppath_root = NULL;
105 static const rset_control control =
119 const rset_control *rset_kind_temp = &control;
121 struct rset_temp_info {
124 size_t key_size; /* key size */
125 char *buf_mem; /* window buffer */
126 size_t buf_size; /* size of window */
127 size_t pos_end; /* last position in set */
128 size_t pos_cur; /* current position in set */
129 size_t pos_buf; /* position of first byte in window */
130 size_t pos_border; /* position of last byte+1 in window */
131 int dirty; /* window is dirty */
134 struct rset_temp_rfd {
135 struct rset_temp_info *info;
136 struct rset_temp_rfd *next;
139 static void *r_create(const struct rset_control *sel, void *parms, int *flags)
141 rset_temp_parms *temp_parms = parms;
142 struct rset_temp_info *info;
144 info = xmalloc (sizeof(struct rset_temp_info));
147 info->key_size = temp_parms->key_size;
148 info->buf_size = 4096;
149 info->buf_mem = xmalloc (info->buf_size);
158 temppath_root = res_get (common_resource, "setTmpDir");
163 static RSFD r_open (RSET ct, int flag)
165 struct rset_temp_info *info = ct->buf;
166 struct rset_temp_rfd *rfd;
168 assert (info->fd == -1);
171 if (flag & RSETF_WRITE)
172 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
174 info->fd = open (info->fname, O_BINARY|O_RDONLY);
177 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
181 rfd = xmalloc (sizeof(*rfd));
188 flush current window to file if file is assocated with set
190 static void r_flush (RSFD rfd, int mk)
192 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
194 if (!info->fname && mk)
196 char *s = (char*) tempnam (temppath_root, "zrs");
198 info->fname = xmalloc (strlen(s)+1);
199 strcpy (info->fname, s);
201 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
202 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
205 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
209 if (info->fname && info->fd != -1 && info->dirty)
213 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
215 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
218 count = info->buf_size;
219 if (count > info->pos_end - info->pos_buf)
220 count = info->pos_end - info->pos_buf;
221 if ((r = write (info->fd, info->buf_mem, count)) < count)
224 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
226 logf (LOG_FATAL, "write of %ld but got %ld",
227 (long) count, (long) r);
234 static void r_close (RSFD rfd)
236 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
239 if (info->fname && info->fd != -1)
246 static void r_delete (RSET ct)
248 struct rset_temp_info *info = ct->buf;
251 unlink (info->fname);
252 free (info->buf_mem);
253 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
256 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
257 unlink (info->fname);
264 read from file to window if file is assocated with set -
267 static void r_reread (RSFD rfd)
269 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
275 info->pos_border = info->pos_cur + info->buf_size;
276 if (info->pos_border > info->pos_end)
277 info->pos_border = info->pos_end;
278 count = info->pos_border - info->pos_buf;
281 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
283 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
286 if ((r = read (info->fd, info->buf_mem, count)) < count)
289 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
291 logf (LOG_FATAL, "read of %ld but got %ld",
292 (long) count, (long) r);
298 info->pos_border = info->pos_end;
301 static void r_rewind (RSFD rfd)
303 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
311 static int r_count (RSET ct)
313 struct rset_temp_info *info = ct->buf;
315 return info->pos_end / info->key_size;
318 static int r_read (RSFD rfd, void *buf)
320 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
322 size_t nc = info->pos_cur + info->key_size;
324 if (nc > info->pos_border)
326 if (nc > info->pos_end)
329 info->pos_buf = info->pos_cur;
332 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
338 static int r_write (RSFD rfd, const void *buf)
340 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
342 size_t nc = info->pos_cur + info->key_size;
344 if (nc > info->pos_buf + info->buf_size)
347 info->pos_buf = info->pos_cur;
348 if (info->pos_buf < info->pos_end)
352 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
355 if (nc > info->pos_end)
356 info->pos_border = info->pos_end = nc;
360 static int r_score (RSFD rfd, int *score)