2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 1997-09-17 12:19:23 adam
8 * Zebra version corresponds to YAZ version 1.4.
9 * Changed Zebra server so that it doesn't depend on global common_resource.
11 * Revision 1.20 1997/09/09 13:38:17 adam
12 * Partial port to WIN95/NT.
14 * Revision 1.19 1997/09/04 13:58:57 adam
15 * Added O_BINARY for open calls.
17 * Revision 1.18 1996/10/29 13:54:52 adam
18 * Changed name of setting tempSetDir to setTmpDir.
20 * Revision 1.17 1995/12/11 09:15:28 adam
21 * New set types: sand/sor/snot - ranked versions of and/or/not in
22 * ranked/semi-ranked result sets.
23 * Note: the snot not finished yet.
24 * New rset member: flag.
25 * Bug fix: r_delete in rsrel.c did free bad memory block.
27 * Revision 1.16 1995/11/28 14:47:02 adam
28 * New setting: tempSetPath. Location of temporary result sets.
30 * Revision 1.15 1995/10/12 12:41:58 adam
31 * Private info (buf) moved from struct rset_control to struct rset.
32 * Bug fixes in relevance.
34 * Revision 1.14 1995/10/10 14:00:04 adam
35 * Function rset_open changed its wflag parameter to general flags.
37 * Revision 1.13 1995/10/06 14:38:06 adam
38 * New result set method: r_score.
39 * Local no (sysno) and score is transferred to retrieveCtrl.
41 * Revision 1.12 1995/09/28 09:52:11 adam
42 * xfree/xmalloc used everywhere.
44 * Revision 1.11 1995/09/18 14:17:56 adam
47 * Revision 1.10 1995/09/15 14:45:39 adam
50 * Revision 1.9 1995/09/15 09:20:42 adam
53 * Revision 1.8 1995/09/08 14:52:42 adam
54 * Work on relevance feedback.
56 * Revision 1.7 1995/09/07 13:58:44 adam
57 * New parameter: result-set file descriptor (RSFD) to support multiple
58 * positions within the same result-set.
59 * Boolean operators: and, or, not implemented.
61 * Revision 1.6 1995/09/06 16:11:56 adam
62 * More work on boolean sets.
64 * Revision 1.5 1995/09/05 16:36:59 adam
67 * Revision 1.4 1995/09/05 11:43:24 adam
68 * Complete version of temporary sets. Not tested yet though.
70 * Revision 1.3 1995/09/04 15:20:40 adam
71 * More work on temp sets. is_open member removed.
73 * Revision 1.2 1995/09/04 09:10:56 adam
76 * Revision 1.1 1994/11/04 13:21:30 quinn
89 #include <sys/types.h>
95 static void *r_create(const struct rset_control *sel, void *parms,
97 static RSFD r_open (RSET ct, int flag);
98 static void r_close (RSFD rfd);
99 static void r_delete (RSET ct);
100 static void r_rewind (RSFD rfd);
101 static int r_count (RSET ct);
102 static int r_read (RSFD rfd, void *buf);
103 static int r_write (RSFD rfd, const void *buf);
104 static int r_score (RSFD rfd, int *score);
106 static const rset_control control =
120 const rset_control *rset_kind_temp = &control;
122 struct rset_temp_info {
125 size_t key_size; /* key size */
126 char *buf_mem; /* window buffer */
127 size_t buf_size; /* size of window */
128 size_t pos_end; /* last position in set */
129 size_t pos_cur; /* current position in set */
130 size_t pos_buf; /* position of first byte in window */
131 size_t pos_border; /* position of last byte+1 in window */
132 int dirty; /* window is dirty */
136 struct rset_temp_rfd {
137 struct rset_temp_info *info;
138 struct rset_temp_rfd *next;
141 static void *r_create(const struct rset_control *sel, void *parms, int *flags)
143 rset_temp_parms *temp_parms = parms;
144 struct rset_temp_info *info;
146 info = xmalloc (sizeof(struct rset_temp_info));
149 info->key_size = temp_parms->key_size;
150 info->buf_size = 4096;
151 info->buf_mem = xmalloc (info->buf_size);
156 if (!temp_parms->temp_path)
157 info->temp_path = NULL;
160 info->temp_path = xmalloc (strlen(temp_parms->temp_path)+1);
161 strcpy (info->temp_path, temp_parms->temp_path);
167 static RSFD r_open (RSET ct, int flag)
169 struct rset_temp_info *info = ct->buf;
170 struct rset_temp_rfd *rfd;
172 assert (info->fd == -1);
175 if (flag & RSETF_WRITE)
176 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
178 info->fd = open (info->fname, O_BINARY|O_RDONLY);
181 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
185 rfd = xmalloc (sizeof(*rfd));
192 flush current window to file if file is assocated with set
194 static void r_flush (RSFD rfd, int mk)
196 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
198 if (!info->fname && mk)
200 char *s = (char*) tempnam (info->temp_path, "zrs");
202 info->fname = xmalloc (strlen(s)+1);
203 strcpy (info->fname, s);
205 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
206 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
209 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
213 if (info->fname && info->fd != -1 && info->dirty)
217 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
219 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
222 count = info->buf_size;
223 if (count > info->pos_end - info->pos_buf)
224 count = info->pos_end - info->pos_buf;
225 if ((r = write (info->fd, info->buf_mem, count)) < count)
228 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
230 logf (LOG_FATAL, "write of %ld but got %ld",
231 (long) count, (long) r);
238 static void r_close (RSFD rfd)
240 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
243 if (info->fname && info->fd != -1)
250 static void r_delete (RSET ct)
252 struct rset_temp_info *info = ct->buf;
255 unlink (info->fname);
256 xfree (info->buf_mem);
257 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
260 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
261 unlink (info->fname);
265 xfree (info->temp_path);
270 read from file to window if file is assocated with set -
273 static void r_reread (RSFD rfd)
275 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
281 info->pos_border = info->pos_cur + info->buf_size;
282 if (info->pos_border > info->pos_end)
283 info->pos_border = info->pos_end;
284 count = info->pos_border - info->pos_buf;
287 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
289 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
292 if ((r = read (info->fd, info->buf_mem, count)) < count)
295 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
297 logf (LOG_FATAL, "read of %ld but got %ld",
298 (long) count, (long) r);
304 info->pos_border = info->pos_end;
307 static void r_rewind (RSFD rfd)
309 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
317 static int r_count (RSET ct)
319 struct rset_temp_info *info = ct->buf;
321 return info->pos_end / info->key_size;
324 static int r_read (RSFD rfd, void *buf)
326 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
328 size_t nc = info->pos_cur + info->key_size;
330 if (nc > info->pos_border)
332 if (nc > info->pos_end)
335 info->pos_buf = info->pos_cur;
338 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
344 static int r_write (RSFD rfd, const void *buf)
346 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
348 size_t nc = info->pos_cur + info->key_size;
350 if (nc > info->pos_buf + info->buf_size)
353 info->pos_buf = info->pos_cur;
354 if (info->pos_buf < info->pos_end)
358 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
361 if (nc > info->pos_end)
362 info->pos_border = info->pos_end = nc;
366 static int r_score (RSFD rfd, int *score)