Cleaned up the creation of rsets, added nmem
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.45 2004-08-24 14:25:16 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <fcntl.h>
24 #include <assert.h>
25 #ifdef WIN32
26 #include <io.h>
27 #else
28 #include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <sys/types.h>
32 #include <stdio.h>
33
34 #include <zebrautl.h>
35 #include <rstemp.h>
36
37 static RSFD r_open (RSET ct, int flag);
38 static void r_close (RSFD rfd);
39 static void r_delete (RSET ct);
40 static void r_rewind (RSFD rfd);
41 /* static int r_count (RSET ct);*/
42 static int r_read (RSFD rfd, void *buf);
43 static int r_write (RSFD rfd, const void *buf);
44 static void r_pos (RSFD rfd, double *current, double  *total);
45
46 /* FIXME - Use the nmem instead of xmalloc all the way through */
47
48 static const struct rset_control control = 
49 {
50     "temp",
51     r_open,
52     r_close,
53     r_delete,
54     r_rewind,
55     rset_default_forward,
56     r_pos, 
57     r_read,
58     r_write,
59 };
60
61 const struct rset_control *rset_kind_temp = &control;
62
63 struct rset_temp_info {
64     int     fd;
65     char   *fname;
66     size_t  key_size;      /* key size */
67     char   *buf_mem;       /* window buffer */
68     size_t  buf_size;      /* size of window */
69     size_t  pos_end;       /* last position in set */
70     size_t  pos_buf;       /* position of first byte in window */
71     size_t  pos_border;    /* position of last byte+1 in window */
72     int     dirty;         /* window is dirty */
73     zint     hits;          /* no of hits */
74     char   *temp_path;
75     int     (*cmp)(const void *p1, const void *p2);
76     struct rset_temp_rfd *rfd_list;
77 };
78
79 struct rset_temp_rfd {
80     struct rset_temp_info *info;
81     struct rset_temp_rfd *next;
82     void *buf;
83     size_t  pos_cur;       /* current position in set */
84     zint cur; /* number of the current hit */
85 };
86
87 RSET rstemp_create( NMEM nmem, int key_size, 
88                     int (*cmp)(const void *p1, const void *p2),
89                     const char *temp_path)
90 {
91     RSET rnew=rset_create_base(&control, nmem);
92     struct rset_temp_info *info;
93    
94     info = (struct rset_temp_info *) nmem_malloc ( rnew->nmem, sizeof(*info));
95     info->fd = -1;
96     info->fname = NULL;
97     info->key_size = key_size;
98     info->buf_size = 4096;
99     info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
100     info->pos_end = 0;
101     info->pos_buf = 0;
102     info->dirty = 0;
103     info->hits = 0;
104     info->cmp = cmp;
105     info->rfd_list = NULL;
106
107     if (!temp_path)
108         info->temp_path = NULL;
109     else
110         info->temp_path = nmem_strdup(rnew->nmem,temp_path);
111     rnew->priv=info; 
112     return rnew;
113 } /* rstemp_create */
114
115 #if 0
116 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
117 {
118     rset_temp_parms *temp_parms = (rset_temp_parms *) parms;
119     struct rset_temp_info *info;
120    
121     info = (struct rset_temp_info *) xmalloc (sizeof(struct rset_temp_info));
122     info->fd = -1;
123     info->fname = NULL;
124     info->key_size = temp_parms->key_size;
125     info->buf_size = 4096;
126     info->buf_mem = (char *) xmalloc (info->buf_size);
127     info->pos_end = 0;
128     info->pos_buf = 0;
129     info->dirty = 0;
130     info->hits = 0;
131     info->cmp = temp_parms->cmp;
132     info->rfd_list = NULL;
133
134     if (!temp_parms->temp_path)
135         info->temp_path = NULL;
136     else
137     {
138         info->temp_path = (char *) xmalloc (strlen(temp_parms->temp_path)+1);
139         strcpy (info->temp_path, temp_parms->temp_path);
140     }
141
142     return info;
143 }
144 #endif
145
146 static void r_delete (RSET ct)
147 {
148     struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
149
150     xfree (info->buf_mem);
151     logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
152     if (info->fname)
153     {
154         logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
155         unlink (info->fname);
156         xfree (info->fname);  /* FIXME should be nmem'd, and not freed here */
157     }
158     /*
159     if (info->temp_path)
160         xfree (info->temp_path);
161     xfree (info);
162     */  /* nmem'd */
163 }
164
165
166 static RSFD r_open (RSET ct, int flag)
167 {
168     struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
169     struct rset_temp_rfd *rfd;
170
171     if (info->fd == -1 && info->fname)
172     {
173         if (flag & RSETF_WRITE)
174             info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
175         else
176             info->fd = open (info->fname, O_BINARY|O_RDONLY);
177         if (info->fd == -1)
178         {
179             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
180             exit (1);
181         }
182     }
183     rfd = (struct rset_temp_rfd *) xmalloc (sizeof(*rfd));
184     rfd->next = info->rfd_list;
185     info->rfd_list = rfd;
186     rfd->info = info;
187     r_rewind (rfd);
188
189     rfd->buf = xmalloc (info->key_size);
190
191     return rfd;
192 }
193
194 /* r_flush:
195       flush current window to file if file is assocated with set
196  */
197 static void r_flush (RSFD rfd, int mk)
198 {
199     struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
200
201     if (!info->fname && mk)
202     {
203 #if HAVE_MKSTEMP
204         char template[1024];
205
206         if (info->temp_path)
207             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
208         else
209             sprintf (template, "zrsXXXXXX");
210
211         info->fd = mkstemp (template);
212
213         if (info->fd == -1)
214         {
215             logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
216             exit (1);
217         }
218         info->fname = (char *) xmalloc (strlen(template)+1);
219         strcpy (info->fname, template);
220 #else
221         char *s = (char*) tempnam (info->temp_path, "zrs");
222         info->fname = (char *) xmalloc (strlen(s)+1);
223         strcpy (info->fname, s);
224
225         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
226         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
227         if (info->fd == -1)
228         {
229             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
230             exit (1);
231         }
232 #endif
233     }
234     if (info->fname && info->fd != -1 && info->dirty)
235     {
236         size_t count;
237         int r;
238         
239         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
240         {
241             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
242             exit (1);
243         }
244         count = info->buf_size;
245         if (count > info->pos_end - info->pos_buf)
246             count = info->pos_end - info->pos_buf;
247         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
248         {
249             if (r == -1)
250                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
251             else
252                 logf (LOG_FATAL, "write of %ld but got %ld",
253                       (long) count, (long) r);
254             exit (1);
255         }
256         info->dirty = 0;
257     }
258 }
259
260 static void r_close (RSFD rfd)
261 {
262     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
263     struct rset_temp_rfd **rfdp;
264
265     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
266         if (*rfdp == rfd)
267         {
268             r_flush (*rfdp, 0);
269             xfree ((*rfdp)->buf);
270
271             *rfdp = (*rfdp)->next;
272             xfree (rfd);
273
274             if (!info->rfd_list && info->fname && info->fd != -1)
275             {
276                 close (info->fd);
277                 info->fd = -1;
278             }
279             return;
280         }
281     logf (LOG_FATAL, "r_close but no rfd match!");
282     assert (0);
283 }
284
285
286 /* r_reread:
287       read from file to window if file is assocated with set -
288       indicated by fname
289  */
290 static void r_reread (RSFD rfd)
291 {
292     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
293
294     if (info->fname)
295     {
296         size_t count;
297         int r;
298
299         info->pos_border = ((struct rset_temp_rfd *)rfd)->pos_cur +
300             info->buf_size;
301         if (info->pos_border > info->pos_end)
302             info->pos_border = info->pos_end;
303         count = info->pos_border - info->pos_buf;
304         if (count > 0)
305         {
306             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
307             {
308                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
309                 exit (1);
310             }
311             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
312             {
313                 if (r == -1)
314                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
315                 else
316                     logf (LOG_FATAL, "read of %ld but got %ld",
317                           (long) count, (long) r);
318                 exit (1);
319             }
320         }
321     }
322     else
323         info->pos_border = info->pos_end;
324 }
325
326 static void r_rewind (RSFD rfd)
327 {
328     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
329
330     r_flush (rfd, 0);
331     ((struct rset_temp_rfd *)rfd)->pos_cur = 0;
332     info->pos_buf = 0;
333     r_reread (rfd);
334     ((struct rset_temp_rfd *)rfd)->cur=0;
335 }
336
337 /*
338 static int r_count (RSET ct)
339 {
340     struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
341
342     return info->pos_end / info->key_size;
343 }
344 */
345 static int r_read (RSFD rfd, void *buf)
346 {
347     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
348     struct rset_temp_info *info = mrfd->info;
349
350     size_t nc = mrfd->pos_cur + info->key_size;
351
352     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
353     {
354         if (nc > info->pos_end)
355             return 0;
356         r_flush (rfd, 0);
357         info->pos_buf = mrfd->pos_cur;
358         r_reread (rfd);
359     }
360     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
361             info->key_size);
362     mrfd->pos_cur = nc;
363     mrfd->cur++;
364     return 1;
365 }
366
367 static int r_write (RSFD rfd, const void *buf)
368 {
369     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
370     struct rset_temp_info *info = mrfd->info;
371
372     size_t nc = mrfd->pos_cur + info->key_size;
373
374     if (nc > info->pos_buf + info->buf_size)
375     {
376         r_flush (rfd, 1);
377         info->pos_buf = mrfd->pos_cur;
378         if (info->pos_buf < info->pos_end)
379             r_reread (rfd);
380     }
381     info->dirty = 1;
382     memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
383             info->key_size);
384     mrfd->pos_cur = nc;
385     if (nc > info->pos_end)
386         info->pos_border = info->pos_end = nc;
387     info->hits++;
388     return 1;
389 }
390
391 static void r_pos (RSFD rfd, double  *current, double  *total)
392 {
393     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
394     *current=(double) mrfd->cur;
395     *total=(double) mrfd->info->hits;
396 }