2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 1998-03-05 08:36:28 adam
8 * New result set model.
10 * Revision 1.23 1997/12/18 10:54:25 adam
11 * New method result set method rs_hits that returns the number of
12 * hits in result-set (if known). The ranked result set returns real
13 * number of hits but only when not combined with other operands.
15 * Revision 1.22 1997/10/31 12:38:12 adam
16 * Bug fix: added missing xfree() call.
18 * Revision 1.21 1997/09/17 12:19:23 adam
19 * Zebra version corresponds to YAZ version 1.4.
20 * Changed Zebra server so that it doesn't depend on global common_resource.
22 * Revision 1.20 1997/09/09 13:38:17 adam
23 * Partial port to WIN95/NT.
25 * Revision 1.19 1997/09/04 13:58:57 adam
26 * Added O_BINARY for open calls.
28 * Revision 1.18 1996/10/29 13:54:52 adam
29 * Changed name of setting tempSetDir to setTmpDir.
31 * Revision 1.17 1995/12/11 09:15:28 adam
32 * New set types: sand/sor/snot - ranked versions of and/or/not in
33 * ranked/semi-ranked result sets.
34 * Note: the snot not finished yet.
35 * New rset member: flag.
36 * Bug fix: r_delete in rsrel.c did free bad memory block.
38 * Revision 1.16 1995/11/28 14:47:02 adam
39 * New setting: tempSetPath. Location of temporary result sets.
41 * Revision 1.15 1995/10/12 12:41:58 adam
42 * Private info (buf) moved from struct rset_control to struct rset.
43 * Bug fixes in relevance.
45 * Revision 1.14 1995/10/10 14:00:04 adam
46 * Function rset_open changed its wflag parameter to general flags.
48 * Revision 1.13 1995/10/06 14:38:06 adam
49 * New result set method: r_score.
50 * Local no (sysno) and score is transferred to retrieveCtrl.
52 * Revision 1.12 1995/09/28 09:52:11 adam
53 * xfree/xmalloc used everywhere.
55 * Revision 1.11 1995/09/18 14:17:56 adam
58 * Revision 1.10 1995/09/15 14:45:39 adam
61 * Revision 1.9 1995/09/15 09:20:42 adam
64 * Revision 1.8 1995/09/08 14:52:42 adam
65 * Work on relevance feedback.
67 * Revision 1.7 1995/09/07 13:58:44 adam
68 * New parameter: result-set file descriptor (RSFD) to support multiple
69 * positions within the same result-set.
70 * Boolean operators: and, or, not implemented.
72 * Revision 1.6 1995/09/06 16:11:56 adam
73 * More work on boolean sets.
75 * Revision 1.5 1995/09/05 16:36:59 adam
78 * Revision 1.4 1995/09/05 11:43:24 adam
79 * Complete version of temporary sets. Not tested yet though.
81 * Revision 1.3 1995/09/04 15:20:40 adam
82 * More work on temp sets. is_open member removed.
84 * Revision 1.2 1995/09/04 09:10:56 adam
87 * Revision 1.1 1994/11/04 13:21:30 quinn
100 #include <sys/types.h>
103 #include <zebrautl.h>
106 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
107 static RSFD r_open (RSET ct, int flag);
108 static void r_close (RSFD rfd);
109 static void r_delete (RSET ct);
110 static void r_rewind (RSFD rfd);
111 static int r_count (RSET ct);
112 static int r_read (RSFD rfd, void *buf, int *term_index);
113 static int r_write (RSFD rfd, const void *buf);
115 static const struct rset_control control =
128 const struct rset_control *rset_kind_temp = &control;
130 struct rset_temp_info {
133 size_t key_size; /* key size */
134 char *buf_mem; /* window buffer */
135 size_t buf_size; /* size of window */
136 size_t pos_end; /* last position in set */
137 size_t pos_cur; /* current position in set */
138 size_t pos_buf; /* position of first byte in window */
139 size_t pos_border; /* position of last byte+1 in window */
140 int dirty; /* window is dirty */
141 int hits; /* no of hits */
145 struct rset_temp_rfd {
146 struct rset_temp_info *info;
147 struct rset_temp_rfd *next;
150 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
152 rset_temp_parms *temp_parms = parms;
153 struct rset_temp_info *info;
155 info = xmalloc (sizeof(struct rset_temp_info));
158 info->key_size = temp_parms->key_size;
159 info->buf_size = 4096;
160 info->buf_mem = xmalloc (info->buf_size);
166 if (!temp_parms->temp_path)
167 info->temp_path = NULL;
170 info->temp_path = xmalloc (strlen(temp_parms->temp_path)+1);
171 strcpy (info->temp_path, temp_parms->temp_path);
173 ct->no_rset_terms = 1;
174 ct->rset_terms = xmalloc (sizeof(*ct->rset_terms));
175 ct->rset_terms[0] = temp_parms->rset_term;
179 static RSFD r_open (RSET ct, int flag)
181 struct rset_temp_info *info = ct->buf;
182 struct rset_temp_rfd *rfd;
184 assert (info->fd == -1);
187 if (flag & RSETF_WRITE)
188 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
190 info->fd = open (info->fname, O_BINARY|O_RDONLY);
193 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
197 rfd = xmalloc (sizeof(*rfd));
204 flush current window to file if file is assocated with set
206 static void r_flush (RSFD rfd, int mk)
208 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
210 if (!info->fname && mk)
212 char *s = (char*) tempnam (info->temp_path, "zrs");
214 info->fname = xmalloc (strlen(s)+1);
215 strcpy (info->fname, s);
217 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
218 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
221 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
225 if (info->fname && info->fd != -1 && info->dirty)
229 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
231 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
234 count = info->buf_size;
235 if (count > info->pos_end - info->pos_buf)
236 count = info->pos_end - info->pos_buf;
237 if ((r = write (info->fd, info->buf_mem, count)) < count)
240 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
242 logf (LOG_FATAL, "write of %ld but got %ld",
243 (long) count, (long) r);
250 static void r_close (RSFD rfd)
252 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
255 if (info->fname && info->fd != -1)
263 static void r_delete (RSET ct)
265 struct rset_temp_info *info = ct->buf;
268 unlink (info->fname);
269 xfree (info->buf_mem);
270 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
273 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
274 unlink (info->fname);
278 xfree (info->temp_path);
279 rset_term_destroy (ct->rset_terms[0]);
280 xfree (ct->rset_terms);
285 read from file to window if file is assocated with set -
288 static void r_reread (RSFD rfd)
290 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
296 info->pos_border = info->pos_cur + info->buf_size;
297 if (info->pos_border > info->pos_end)
298 info->pos_border = info->pos_end;
299 count = info->pos_border - info->pos_buf;
302 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
304 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
307 if ((r = read (info->fd, info->buf_mem, count)) < count)
310 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
312 logf (LOG_FATAL, "read of %ld but got %ld",
313 (long) count, (long) r);
319 info->pos_border = info->pos_end;
322 static void r_rewind (RSFD rfd)
324 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
332 static int r_count (RSET ct)
334 struct rset_temp_info *info = ct->buf;
336 return info->pos_end / info->key_size;
339 static int r_read (RSFD rfd, void *buf, int *term_index)
341 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
343 size_t nc = info->pos_cur + info->key_size;
345 if (nc > info->pos_border)
347 if (nc > info->pos_end)
350 info->pos_buf = info->pos_cur;
353 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
360 static int r_write (RSFD rfd, const void *buf)
362 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
364 size_t nc = info->pos_cur + info->key_size;
366 if (nc > info->pos_buf + info->buf_size)
369 info->pos_buf = info->pos_cur;
370 if (info->pos_buf < info->pos_end)
374 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
377 if (nc > info->pos_end)
378 info->pos_border = info->pos_end = nc;