Using nmem for all rsets, and keeping a freelist for freed rfds, so
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.46 2004-08-26 11:11:59 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_read (RSFD rfd, void *buf);
42 static int r_write (RSFD rfd, const void *buf);
43 static void r_pos (RSFD rfd, double *current, double  *total);
44
45
46 static const struct rset_control control = 
47 {
48     "temp",
49     r_open,
50     r_close,
51     r_delete,
52     r_rewind,
53     rset_default_forward,
54     r_pos, 
55     r_read,
56     r_write,
57 };
58
59 const struct rset_control *rset_kind_temp = &control;
60
61 struct rset_temp_info {
62     int     fd;
63     char   *fname;
64     size_t  key_size;      /* key size */
65     char   *buf_mem;       /* window buffer */
66     size_t  buf_size;      /* size of window */
67     size_t  pos_end;       /* last position in set */
68     size_t  pos_buf;       /* position of first byte in window */
69     size_t  pos_border;    /* position of last byte+1 in window */
70     int     dirty;         /* window is dirty */
71     zint     hits;          /* no of hits */
72     char   *temp_path;
73     int     (*cmp)(const void *p1, const void *p2);
74     struct rset_temp_rfd *rfd_list;  /* rfds in use */
75     struct rset_temp_rfd *free_list; /* fully alloc'd rfds waiting for reuse*/
76 };
77
78 struct rset_temp_rfd {
79     struct rset_temp_info *info;
80     struct rset_temp_rfd *next;
81     void *buf;
82     size_t  pos_cur;       /* current position in set */
83     zint cur; /* number of the current hit */
84 };
85
86 RSET rstemp_create( NMEM nmem, int key_size, 
87                     int (*cmp)(const void *p1, const void *p2),
88                     const char *temp_path)
89 {
90     RSET rnew=rset_create_base(&control, nmem);
91     struct rset_temp_info *info;
92    
93     info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
94     info->fd = -1;
95     info->fname = NULL;
96     info->key_size = key_size;
97     info->buf_size = 4096;
98     info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
99     info->pos_end = 0;
100     info->pos_buf = 0;
101     info->dirty = 0;
102     info->hits = 0;
103     info->cmp = cmp;
104     info->rfd_list = NULL;
105     info->free_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 static void r_delete (RSET ct)
116 {
117     struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
118
119     xfree (info->buf_mem);
120     logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
121     if (info->fname)
122     {
123         logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
124         unlink (info->fname);
125         xfree (info->fname);  /* FIXME should be nmem'd, and not freed here */
126     }
127     /*
128     if (info->temp_path)
129         xfree (info->temp_path);
130     xfree (info);
131     */  /* nmem'd */
132 }
133
134
135 static RSFD r_open (RSET ct, int flag)
136 {
137     struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
138     struct rset_temp_rfd *rfd;
139
140     if (info->fd == -1 && info->fname)
141     {
142         if (flag & RSETF_WRITE)
143             info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
144         else
145             info->fd = open (info->fname, O_BINARY|O_RDONLY);
146         if (info->fd == -1)
147         {
148             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
149             exit (1);
150         }
151     }
152     rfd = info->free_list;
153     if (rfd)
154         info->free_list=rfd->next;
155     else {
156         rfd = (struct rset_temp_rfd *) xmalloc (sizeof(*rfd));
157         rfd->buf = xmalloc (info->key_size);
158     }
159     rfd->next = info->rfd_list;
160     info->rfd_list = rfd;
161     rfd->info = info;
162     r_rewind (rfd);
163
164     return rfd;
165 }
166
167 /* r_flush:
168       flush current window to file if file is assocated with set
169  */
170 static void r_flush (RSFD rfd, int mk)
171 {
172     struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
173
174     if (!info->fname && mk)
175     {
176 #if HAVE_MKSTEMP
177         char template[1024];
178
179         if (info->temp_path)
180             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
181         else
182             sprintf (template, "zrsXXXXXX");
183
184         info->fd = mkstemp (template);
185
186         if (info->fd == -1)
187         {
188             logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
189             exit (1);
190         }
191         info->fname = (char *) xmalloc (strlen(template)+1);
192         strcpy (info->fname, template);
193 #else
194         char *s = (char*) tempnam (info->temp_path, "zrs");
195         info->fname = (char *) xmalloc (strlen(s)+1);
196         strcpy (info->fname, s);
197
198         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
199         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
200         if (info->fd == -1)
201         {
202             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
203             exit (1);
204         }
205 #endif
206     }
207     if (info->fname && info->fd != -1 && info->dirty)
208     {
209         size_t count;
210         int r;
211         
212         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
213         {
214             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
215             exit (1);
216         }
217         count = info->buf_size;
218         if (count > info->pos_end - info->pos_buf)
219             count = info->pos_end - info->pos_buf;
220         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
221         {
222             if (r == -1)
223                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
224             else
225                 logf (LOG_FATAL, "write of %ld but got %ld",
226                       (long) count, (long) r);
227             exit (1);
228         }
229         info->dirty = 0;
230     }
231 }
232
233 static void r_close (RSFD rfd)
234 {
235     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
236     struct rset_temp_rfd **rfdp;
237
238     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
239         if (*rfdp == rfd)
240         {
241             struct rset_temp_rfd *rfd_tmp=*rfdp;
242             r_flush (*rfdp, 0);
243             *rfdp = (*rfdp)->next;
244             rfd_tmp->next=info->free_list;
245             info->free_list=rfd_tmp;
246
247             if (!info->rfd_list && info->fname && info->fd != -1)
248             {
249                 close (info->fd);
250                 info->fd = -1;
251             }
252             return;
253         }
254     logf (LOG_FATAL, "r_close but no rfd match!");
255     assert (0);
256 }
257
258
259 /* r_reread:
260       read from file to window if file is assocated with set -
261       indicated by fname
262  */
263 static void r_reread (RSFD rfd)
264 {
265     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
266
267     if (info->fname)
268     {
269         size_t count;
270         int r;
271
272         info->pos_border = ((struct rset_temp_rfd *)rfd)->pos_cur +
273             info->buf_size;
274         if (info->pos_border > info->pos_end)
275             info->pos_border = info->pos_end;
276         count = info->pos_border - info->pos_buf;
277         if (count > 0)
278         {
279             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
280             {
281                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
282                 exit (1);
283             }
284             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
285             {
286                 if (r == -1)
287                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
288                 else
289                     logf (LOG_FATAL, "read of %ld but got %ld",
290                           (long) count, (long) r);
291                 exit (1);
292             }
293         }
294     }
295     else
296         info->pos_border = info->pos_end;
297 }
298
299 static void r_rewind (RSFD rfd)
300 {
301     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
302
303     r_flush (rfd, 0);
304     ((struct rset_temp_rfd *)rfd)->pos_cur = 0;
305     info->pos_buf = 0;
306     r_reread (rfd);
307     ((struct rset_temp_rfd *)rfd)->cur=0;
308 }
309
310 /*
311 static int r_count (RSET ct)
312 {
313     struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
314
315     return info->pos_end / info->key_size;
316 }
317 */
318 static int r_read (RSFD rfd, void *buf)
319 {
320     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
321     struct rset_temp_info *info = mrfd->info;
322
323     size_t nc = mrfd->pos_cur + info->key_size;
324
325     if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
326     {
327         if (nc > info->pos_end)
328             return 0;
329         r_flush (rfd, 0);
330         info->pos_buf = mrfd->pos_cur;
331         r_reread (rfd);
332     }
333     memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
334             info->key_size);
335     mrfd->pos_cur = nc;
336     mrfd->cur++;
337     return 1;
338 }
339
340 static int r_write (RSFD rfd, const void *buf)
341 {
342     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
343     struct rset_temp_info *info = mrfd->info;
344
345     size_t nc = mrfd->pos_cur + info->key_size;
346
347     if (nc > info->pos_buf + info->buf_size)
348     {
349         r_flush (rfd, 1);
350         info->pos_buf = mrfd->pos_cur;
351         if (info->pos_buf < info->pos_end)
352             r_reread (rfd);
353     }
354     info->dirty = 1;
355     memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
356             info->key_size);
357     mrfd->pos_cur = nc;
358     if (nc > info->pos_end)
359         info->pos_border = info->pos_end = nc;
360     info->hits++;
361     return 1;
362 }
363
364 static void r_pos (RSFD rfd, double  *current, double  *total)
365 {
366     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
367     *current=(double) mrfd->cur;
368     *total=(double) mrfd->info->hits;
369 }