2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1995-09-07 13:58:44 adam
8 * New parameter: result-set file descriptor (RSFD) to support multiple
9 * positions within the same result-set.
10 * Boolean operators: and, or, not implemented.
12 * Revision 1.6 1995/09/06 16:11:56 adam
13 * More work on boolean sets.
15 * Revision 1.5 1995/09/05 16:36:59 adam
18 * Revision 1.4 1995/09/05 11:43:24 adam
19 * Complete version of temporary sets. Not tested yet though.
21 * Revision 1.3 1995/09/04 15:20:40 adam
22 * More work on temp sets. is_open member removed.
24 * Revision 1.2 1995/09/04 09:10:56 adam
27 * Revision 1.1 1994/11/04 13:21:30 quinn
35 #include <sys/types.h>
41 static rset_control *r_create(const struct rset_control *sel, void *parms);
42 static RSFD r_open (rset_control *ct, int wflag);
43 static void r_close (RSFD rfd);
44 static void r_delete (rset_control *ct);
45 static void r_rewind (RSFD rfd);
46 static int r_count (rset_control *ct);
47 static int r_read (RSFD rfd, void *buf);
48 static int r_write (RSFD rfd, const void *buf);
50 static const rset_control control =
64 const rset_control *rset_kind_temp = &control;
66 struct rset_temp_info {
79 struct rset_temp_rfd {
80 struct rset_temp_info *info;
81 struct rset_temp_rfd *next;
84 static struct rset_control *r_create(const struct rset_control *sel,
88 rset_temp_parms *temp_parms = parms;
89 struct rset_temp_info *info;
91 logf (LOG_DEBUG, "ritemp_create(%s)", sel->desc);
92 newct = xmalloc(sizeof(*newct));
93 memcpy(newct, sel, sizeof(*sel));
94 newct->buf = xmalloc (sizeof(struct rset_temp_info));
99 info->key_size = temp_parms->key_size;
100 info->buf_size = 1024;
101 info->buf_mem = xmalloc (info->buf_size);
110 static RSFD r_open (struct rset_control *ct, int wflag)
112 struct rset_temp_info *info = ct->buf;
113 struct rset_temp_rfd *rfd;
115 assert (info->fd == -1);
119 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
121 info->fd = open (info->fname, O_RDONLY);
124 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
128 rfd = xmalloc (sizeof(*rfd));
134 static void r_flush (RSFD rfd, int mk)
136 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
138 if (!info->fname && mk)
140 char *s = (char*) tempnam (NULL, "zrs");
142 info->fname = xmalloc (strlen(s)+1);
143 strcpy (info->fname, s);
145 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
148 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
152 if (info->fname && info->fd != -1 && info->dirty)
156 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
158 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
161 count = info->buf_size;
162 if (count > info->pos_end - info->pos_buf)
163 count = info->pos_end - info->pos_buf;
164 if ((r = write (info->fd, info->buf_mem, count)) < count)
167 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
169 logf (LOG_FATAL, "write of %ld but got %ld",
170 (long) count, (long) r);
177 static void r_close (RSFD rfd)
179 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
182 if (info->fname && info->fd != -1)
189 static void r_delete (struct rset_control *ct)
191 struct rset_temp_info *info = ct->buf;
195 unlink (info->fname);
196 free (info->buf_mem);
201 static void r_reread (RSFD rfd)
203 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
209 info->pos_border = info->pos_cur + info->buf_size;
210 if (info->pos_border > info->pos_end)
211 info->pos_border = info->pos_end;
212 count = info->pos_border - info->pos_buf;
214 if ((r = read (info->fd, info->buf_mem, count)) < count)
217 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
219 logf (LOG_FATAL, "read of %ld but got %ld",
220 (long) count, (long) r);
225 info->pos_border = info->pos_end;
228 static void r_rewind (RSFD rfd)
230 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
238 static int r_count (struct rset_control *ct)
240 struct rset_temp_info *info = ct->buf;
242 return info->pos_end / info->key_size;
245 static int r_read (RSFD rfd, void *buf)
247 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
249 size_t nc = info->pos_cur + info->key_size;
251 if (nc > info->pos_border)
253 if (nc > info->pos_end)
256 info->pos_buf = info->pos_cur;
259 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
265 static int r_write (RSFD rfd, const void *buf)
267 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
269 size_t nc = info->pos_cur + info->key_size;
271 if (nc > info->pos_buf + info->buf_size)
274 info->pos_buf = info->pos_cur;
277 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
280 info->pos_border = info->pos_cur = nc;