2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1995-11-28 14:47:02 adam
8 * New setting: tempSetPath. Location of temporary result sets.
10 * Revision 1.15 1995/10/12 12:41:58 adam
11 * Private info (buf) moved from struct rset_control to struct rset.
12 * Bug fixes in relevance.
14 * Revision 1.14 1995/10/10 14:00:04 adam
15 * Function rset_open changed its wflag parameter to general flags.
17 * Revision 1.13 1995/10/06 14:38:06 adam
18 * New result set method: r_score.
19 * Local no (sysno) and score is transferred to retrieveCtrl.
21 * Revision 1.12 1995/09/28 09:52:11 adam
22 * xfree/xmalloc used everywhere.
24 * Revision 1.11 1995/09/18 14:17:56 adam
27 * Revision 1.10 1995/09/15 14:45:39 adam
30 * Revision 1.9 1995/09/15 09:20:42 adam
33 * Revision 1.8 1995/09/08 14:52:42 adam
34 * Work on relevance feedback.
36 * Revision 1.7 1995/09/07 13:58:44 adam
37 * New parameter: result-set file descriptor (RSFD) to support multiple
38 * positions within the same result-set.
39 * Boolean operators: and, or, not implemented.
41 * Revision 1.6 1995/09/06 16:11:56 adam
42 * More work on boolean sets.
44 * Revision 1.5 1995/09/05 16:36:59 adam
47 * Revision 1.4 1995/09/05 11:43:24 adam
48 * Complete version of temporary sets. Not tested yet though.
50 * Revision 1.3 1995/09/04 15:20:40 adam
51 * More work on temp sets. is_open member removed.
53 * Revision 1.2 1995/09/04 09:10:56 adam
56 * Revision 1.1 1994/11/04 13:21:30 quinn
64 #include <sys/types.h>
70 static void *r_create(const struct rset_control *sel, void *parms);
71 static RSFD r_open (RSET ct, int flag);
72 static void r_close (RSFD rfd);
73 static void r_delete (RSET ct);
74 static void r_rewind (RSFD rfd);
75 static int r_count (RSET ct);
76 static int r_read (RSFD rfd, void *buf);
77 static int r_write (RSFD rfd, const void *buf);
78 static int r_score (RSFD rfd, int *score);
80 static int temppath_init = 0;
81 static char *temppath_root = NULL;
83 static const rset_control control =
97 const rset_control *rset_kind_temp = &control;
99 struct rset_temp_info {
102 size_t key_size; /* key size */
103 char *buf_mem; /* window buffer */
104 size_t buf_size; /* size of window */
105 size_t pos_end; /* last position in set */
106 size_t pos_cur; /* current position in set */
107 size_t pos_buf; /* position of first byte in window */
108 size_t pos_border; /* position of last byte+1 in window */
109 int dirty; /* window is dirty */
112 struct rset_temp_rfd {
113 struct rset_temp_info *info;
114 struct rset_temp_rfd *next;
117 static void *r_create(const struct rset_control *sel, void *parms)
119 rset_temp_parms *temp_parms = parms;
120 struct rset_temp_info *info;
122 info = xmalloc (sizeof(struct rset_temp_info));
125 info->key_size = temp_parms->key_size;
126 info->buf_size = 4096;
127 info->buf_mem = xmalloc (info->buf_size);
136 temppath_root = res_get (common_resource, "tempSetPath");
141 static RSFD r_open (RSET ct, int flag)
143 struct rset_temp_info *info = ct->buf;
144 struct rset_temp_rfd *rfd;
146 assert (info->fd == -1);
149 if (flag & RSETF_WRITE)
150 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
152 info->fd = open (info->fname, O_RDONLY);
155 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
159 rfd = xmalloc (sizeof(*rfd));
166 flush current window to file if file is assocated with set
168 static void r_flush (RSFD rfd, int mk)
170 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
172 if (!info->fname && mk)
174 char *s = (char*) tempnam (temppath_root, "zrs");
176 info->fname = xmalloc (strlen(s)+1);
177 strcpy (info->fname, s);
179 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
180 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
183 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
187 if (info->fname && info->fd != -1 && info->dirty)
191 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
193 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
196 count = info->buf_size;
197 if (count > info->pos_end - info->pos_buf)
198 count = info->pos_end - info->pos_buf;
199 if ((r = write (info->fd, info->buf_mem, count)) < count)
202 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
204 logf (LOG_FATAL, "write of %ld but got %ld",
205 (long) count, (long) r);
212 static void r_close (RSFD rfd)
214 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
217 if (info->fname && info->fd != -1)
224 static void r_delete (RSET ct)
226 struct rset_temp_info *info = ct->buf;
229 unlink (info->fname);
230 free (info->buf_mem);
231 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
234 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
235 unlink (info->fname);
242 read from file to window if file is assocated with set -
245 static void r_reread (RSFD rfd)
247 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
253 info->pos_border = info->pos_cur + info->buf_size;
254 if (info->pos_border > info->pos_end)
255 info->pos_border = info->pos_end;
256 count = info->pos_border - info->pos_buf;
259 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
261 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
264 if ((r = read (info->fd, info->buf_mem, count)) < count)
267 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
269 logf (LOG_FATAL, "read of %ld but got %ld",
270 (long) count, (long) r);
276 info->pos_border = info->pos_end;
279 static void r_rewind (RSFD rfd)
281 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
289 static int r_count (RSET ct)
291 struct rset_temp_info *info = ct->buf;
293 return info->pos_end / info->key_size;
296 static int r_read (RSFD rfd, void *buf)
298 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
300 size_t nc = info->pos_cur + info->key_size;
302 if (nc > info->pos_border)
304 if (nc > info->pos_end)
307 info->pos_buf = info->pos_cur;
310 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
316 static int r_write (RSFD rfd, const void *buf)
318 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
320 size_t nc = info->pos_cur + info->key_size;
322 if (nc > info->pos_buf + info->buf_size)
325 info->pos_buf = info->pos_cur;
326 if (info->pos_buf < info->pos_end)
330 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
333 if (nc > info->pos_end)
334 info->pos_border = info->pos_end = nc;
338 static int r_score (RSFD rfd, int *score)