1 /* $Id: drdwr.c,v 1.18 2006-05-10 08:13:18 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
25 #include <sys/types.h>
37 void dict_pr_lru (Dict_BFile bf)
39 struct Dict_file_block *p;
40 for (p=bf->lru_back; p; p = p->lru_next)
42 printf (" %d", p->no);
48 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
50 struct Dict_file_block *p;
52 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
58 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
62 /* remove from lru queue */
64 p->lru_prev->lru_next = p->lru_next;
66 bf->lru_back = p->lru_next;
68 p->lru_next->lru_prev = p->lru_prev;
70 bf->lru_front = p->lru_prev;
72 /* remove from hash chain */
73 *p->h_prev = p->h_next;
75 p->h_next->h_prev = p->h_prev;
77 /* move to list of free blocks */
78 p->h_next = bf->free_list;
82 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
84 struct Dict_file_block *p;
86 for (i=0; i != no_to_flush && bf->lru_back; i++)
91 if (!bf->compact_flag)
92 bf_write (bf->bf, p->no, 0, 0, p->data);
95 int effective_block = p->no / bf->block_size;
96 int effective_offset = p->no -
97 effective_block * bf->block_size;
98 int remain = bf->block_size - effective_offset;
100 if (remain >= p->nbytes)
102 bf_write (bf->bf, effective_block, effective_offset,
105 yaz_log (YLOG_LOG, "bf_write no=%d offset=%d size=%d",
106 effective_block, effective_offset,
114 yaz_log (YLOG_LOG, "bf_write1 no=%d offset=%d size=%d",
115 effective_block, effective_offset,
118 bf_write (bf->bf, effective_block, effective_offset,
121 yaz_log (YLOG_LOG, "bf_write2 no=%d offset=%d size=%d",
122 effective_block+1, 0, p->nbytes - remain);
124 bf_write (bf->bf, effective_block+1, 0,
125 p->nbytes - remain, (char*)p->data + remain);
129 release_block (bf, p);
133 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
135 struct Dict_file_block *p, **pp;
138 dict_bf_flush_blocks (bf, 1);
139 assert (bf->free_list);
141 bf->free_list = p->h_next;
145 /* insert at front in lru chain */
147 p->lru_prev = bf->lru_front;
149 bf->lru_front->lru_next = p;
154 /* insert in hash chain */
155 pp = bf->hash_array + (no % bf->hash_size);
159 (*pp)->h_prev = &p->h_next;
165 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
167 /* Already at front? */
173 p->lru_prev->lru_next = p->lru_next;
175 bf->lru_back = p->lru_next;
176 p->lru_next->lru_prev = p->lru_prev;
178 /* Insert at front */
180 p->lru_prev = bf->lru_front;
182 bf->lru_front->lru_next = p;
188 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
190 struct Dict_file_block *p;
192 if ((p = find_block (bf, no)))
195 move_to_front (bf, p);
200 p = alloc_block (bf, no);
202 if (!bf->compact_flag)
203 i = bf_read (bf->bf, no, 0, 0, p->data);
206 int effective_block = no / bf->block_size;
207 int effective_offset = no - effective_block * bf->block_size;
209 i = bf_read (bf->bf, effective_block, effective_offset,
210 bf->block_size - effective_offset, p->data);
211 if (i > 0 && effective_offset > 0)
212 i = bf_read (bf->bf, effective_block+1, 0, effective_offset,
213 (char*) p->data + bf->block_size - effective_offset);
221 release_block (bf, p);
226 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp, int nbytes)
228 struct Dict_file_block *p;
229 if (!(p = find_block (dbf, no)))
230 p = alloc_block (dbf, no);
232 move_to_front (dbf, p);
234 memset (p->data, 0, dbf->block_size);
238 printf ("bf_newp of %d:", no);
244 int dict_bf_touch (Dict_BFile dbf, int no)
246 struct Dict_file_block *p;
247 if ((p = find_block (dbf, no)))
258 * indent-tabs-mode: nil
260 * vim: shiftwidth=4 tabstop=8 expandtab