1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <sys/types.h>
34 void dict_pr_lru (Dict_BFile bf)
36 struct Dict_file_block *p;
37 for (p=bf->lru_back; p; p = p->lru_next)
39 printf (" %d", p->no);
45 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
47 struct Dict_file_block *p;
49 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
55 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
59 /* remove from lru queue */
61 p->lru_prev->lru_next = p->lru_next;
63 bf->lru_back = p->lru_next;
65 p->lru_next->lru_prev = p->lru_prev;
67 bf->lru_front = p->lru_prev;
69 /* remove from hash chain */
70 *p->h_prev = p->h_next;
72 p->h_next->h_prev = p->h_prev;
74 /* move to list of free blocks */
75 p->h_next = bf->free_list;
79 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
81 struct Dict_file_block *p;
83 for (i=0; i != no_to_flush && bf->lru_back; i++)
88 if (!bf->compact_flag)
89 bf_write (bf->bf, p->no, 0, 0, p->data);
92 int effective_block = p->no / bf->block_size;
93 int effective_offset = p->no -
94 effective_block * bf->block_size;
95 int remain = bf->block_size - effective_offset;
97 if (remain >= p->nbytes)
99 bf_write (bf->bf, effective_block, effective_offset,
102 yaz_log (YLOG_LOG, "bf_write no=%d offset=%d size=%d",
103 effective_block, effective_offset,
111 yaz_log (YLOG_LOG, "bf_write1 no=%d offset=%d size=%d",
112 effective_block, effective_offset,
115 bf_write (bf->bf, effective_block, effective_offset,
118 yaz_log (YLOG_LOG, "bf_write2 no=%d offset=%d size=%d",
119 effective_block+1, 0, p->nbytes - remain);
121 bf_write (bf->bf, effective_block+1, 0,
122 p->nbytes - remain, (char*)p->data + remain);
126 release_block (bf, p);
130 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
132 struct Dict_file_block *p, **pp;
135 dict_bf_flush_blocks (bf, 1);
136 assert (bf->free_list);
138 bf->free_list = p->h_next;
142 /* insert at front in lru chain */
144 p->lru_prev = bf->lru_front;
146 bf->lru_front->lru_next = p;
151 /* insert in hash chain */
152 pp = bf->hash_array + (no % bf->hash_size);
156 (*pp)->h_prev = &p->h_next;
162 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
164 /* Already at front? */
170 p->lru_prev->lru_next = p->lru_next;
172 bf->lru_back = p->lru_next;
173 p->lru_next->lru_prev = p->lru_prev;
175 /* Insert at front */
177 p->lru_prev = bf->lru_front;
179 bf->lru_front->lru_next = p;
185 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
187 struct Dict_file_block *p;
189 if ((p = find_block (bf, no)))
192 move_to_front (bf, p);
197 p = alloc_block (bf, no);
199 if (!bf->compact_flag)
200 i = bf_read (bf->bf, no, 0, 0, p->data);
203 int effective_block = no / bf->block_size;
204 int effective_offset = no - effective_block * bf->block_size;
206 i = bf_read (bf->bf, effective_block, effective_offset,
207 bf->block_size - effective_offset, p->data);
208 if (i > 0 && effective_offset > 0)
209 i = bf_read (bf->bf, effective_block+1, 0, effective_offset,
210 (char*) p->data + bf->block_size - effective_offset);
218 release_block (bf, p);
223 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp, int nbytes)
225 struct Dict_file_block *p;
226 if (!(p = find_block (dbf, no)))
227 p = alloc_block (dbf, no);
229 move_to_front (dbf, p);
231 memset (p->data, 0, dbf->block_size);
235 printf ("bf_newp of %d:", no);
241 int dict_bf_touch (Dict_BFile dbf, int no)
243 struct Dict_file_block *p;
244 if ((p = find_block (dbf, no)))
255 * indent-tabs-mode: nil
257 * vim: shiftwidth=4 tabstop=8 expandtab