2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.25 1999-02-02 14:51:37 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.24 1998/03/05 08:36:28 adam
11 * New result set model.
13 * Revision 1.23 1997/12/18 10:54:25 adam
14 * New method result set method rs_hits that returns the number of
15 * hits in result-set (if known). The ranked result set returns real
16 * number of hits but only when not combined with other operands.
18 * Revision 1.22 1997/10/31 12:38:12 adam
19 * Bug fix: added missing xfree() call.
21 * Revision 1.21 1997/09/17 12:19:23 adam
22 * Zebra version corresponds to YAZ version 1.4.
23 * Changed Zebra server so that it doesn't depend on global common_resource.
25 * Revision 1.20 1997/09/09 13:38:17 adam
26 * Partial port to WIN95/NT.
28 * Revision 1.19 1997/09/04 13:58:57 adam
29 * Added O_BINARY for open calls.
31 * Revision 1.18 1996/10/29 13:54:52 adam
32 * Changed name of setting tempSetDir to setTmpDir.
34 * Revision 1.17 1995/12/11 09:15:28 adam
35 * New set types: sand/sor/snot - ranked versions of and/or/not in
36 * ranked/semi-ranked result sets.
37 * Note: the snot not finished yet.
38 * New rset member: flag.
39 * Bug fix: r_delete in rsrel.c did free bad memory block.
41 * Revision 1.16 1995/11/28 14:47:02 adam
42 * New setting: tempSetPath. Location of temporary result sets.
44 * Revision 1.15 1995/10/12 12:41:58 adam
45 * Private info (buf) moved from struct rset_control to struct rset.
46 * Bug fixes in relevance.
48 * Revision 1.14 1995/10/10 14:00:04 adam
49 * Function rset_open changed its wflag parameter to general flags.
51 * Revision 1.13 1995/10/06 14:38:06 adam
52 * New result set method: r_score.
53 * Local no (sysno) and score is transferred to retrieveCtrl.
55 * Revision 1.12 1995/09/28 09:52:11 adam
56 * xfree/xmalloc used everywhere.
58 * Revision 1.11 1995/09/18 14:17:56 adam
61 * Revision 1.10 1995/09/15 14:45:39 adam
64 * Revision 1.9 1995/09/15 09:20:42 adam
67 * Revision 1.8 1995/09/08 14:52:42 adam
68 * Work on relevance feedback.
70 * Revision 1.7 1995/09/07 13:58:44 adam
71 * New parameter: result-set file descriptor (RSFD) to support multiple
72 * positions within the same result-set.
73 * Boolean operators: and, or, not implemented.
75 * Revision 1.6 1995/09/06 16:11:56 adam
76 * More work on boolean sets.
78 * Revision 1.5 1995/09/05 16:36:59 adam
81 * Revision 1.4 1995/09/05 11:43:24 adam
82 * Complete version of temporary sets. Not tested yet though.
84 * Revision 1.3 1995/09/04 15:20:40 adam
85 * More work on temp sets. is_open member removed.
87 * Revision 1.2 1995/09/04 09:10:56 adam
90 * Revision 1.1 1994/11/04 13:21:30 quinn
103 #include <sys/types.h>
106 #include <zebrautl.h>
109 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
110 static RSFD r_open (RSET ct, int flag);
111 static void r_close (RSFD rfd);
112 static void r_delete (RSET ct);
113 static void r_rewind (RSFD rfd);
114 static int r_count (RSET ct);
115 static int r_read (RSFD rfd, void *buf, int *term_index);
116 static int r_write (RSFD rfd, const void *buf);
118 static const struct rset_control control =
131 const struct rset_control *rset_kind_temp = &control;
133 struct rset_temp_info {
136 size_t key_size; /* key size */
137 char *buf_mem; /* window buffer */
138 size_t buf_size; /* size of window */
139 size_t pos_end; /* last position in set */
140 size_t pos_cur; /* current position in set */
141 size_t pos_buf; /* position of first byte in window */
142 size_t pos_border; /* position of last byte+1 in window */
143 int dirty; /* window is dirty */
144 int hits; /* no of hits */
148 struct rset_temp_rfd {
149 struct rset_temp_info *info;
150 struct rset_temp_rfd *next;
153 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
155 rset_temp_parms *temp_parms = parms;
156 struct rset_temp_info *info;
158 info = xmalloc (sizeof(struct rset_temp_info));
161 info->key_size = temp_parms->key_size;
162 info->buf_size = 4096;
163 info->buf_mem = xmalloc (info->buf_size);
169 if (!temp_parms->temp_path)
170 info->temp_path = NULL;
173 info->temp_path = xmalloc (strlen(temp_parms->temp_path)+1);
174 strcpy (info->temp_path, temp_parms->temp_path);
176 ct->no_rset_terms = 1;
177 ct->rset_terms = xmalloc (sizeof(*ct->rset_terms));
178 ct->rset_terms[0] = temp_parms->rset_term;
182 static RSFD r_open (RSET ct, int flag)
184 struct rset_temp_info *info = ct->buf;
185 struct rset_temp_rfd *rfd;
187 assert (info->fd == -1);
190 if (flag & RSETF_WRITE)
191 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
193 info->fd = open (info->fname, O_BINARY|O_RDONLY);
196 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
200 rfd = xmalloc (sizeof(*rfd));
207 flush current window to file if file is assocated with set
209 static void r_flush (RSFD rfd, int mk)
211 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
213 if (!info->fname && mk)
215 char *s = (char*) tempnam (info->temp_path, "zrs");
217 info->fname = xmalloc (strlen(s)+1);
218 strcpy (info->fname, s);
220 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
221 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
224 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
228 if (info->fname && info->fd != -1 && info->dirty)
232 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
234 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
237 count = info->buf_size;
238 if (count > info->pos_end - info->pos_buf)
239 count = info->pos_end - info->pos_buf;
240 if ((r = write (info->fd, info->buf_mem, count)) < count)
243 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
245 logf (LOG_FATAL, "write of %ld but got %ld",
246 (long) count, (long) r);
253 static void r_close (RSFD rfd)
255 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
258 if (info->fname && info->fd != -1)
266 static void r_delete (RSET ct)
268 struct rset_temp_info *info = ct->buf;
271 unlink (info->fname);
272 xfree (info->buf_mem);
273 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
276 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
277 unlink (info->fname);
281 xfree (info->temp_path);
282 rset_term_destroy (ct->rset_terms[0]);
283 xfree (ct->rset_terms);
288 read from file to window if file is assocated with set -
291 static void r_reread (RSFD rfd)
293 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
299 info->pos_border = info->pos_cur + info->buf_size;
300 if (info->pos_border > info->pos_end)
301 info->pos_border = info->pos_end;
302 count = info->pos_border - info->pos_buf;
305 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
307 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
310 if ((r = read (info->fd, info->buf_mem, count)) < count)
313 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
315 logf (LOG_FATAL, "read of %ld but got %ld",
316 (long) count, (long) r);
322 info->pos_border = info->pos_end;
325 static void r_rewind (RSFD rfd)
327 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
335 static int r_count (RSET ct)
337 struct rset_temp_info *info = ct->buf;
339 return info->pos_end / info->key_size;
342 static int r_read (RSFD rfd, void *buf, int *term_index)
344 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
346 size_t nc = info->pos_cur + info->key_size;
348 if (nc > info->pos_border)
350 if (nc > info->pos_end)
353 info->pos_buf = info->pos_cur;
356 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
363 static int r_write (RSFD rfd, const void *buf)
365 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
367 size_t nc = info->pos_cur + info->key_size;
369 if (nc > info->pos_buf + info->buf_size)
372 info->pos_buf = info->pos_cur;
373 if (info->pos_buf < info->pos_end)
377 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
380 if (nc > info->pos_end)
381 info->pos_border = info->pos_end = nc;