Updated code to use new YAZ log functions/defines.
[idzebra-moved-to-github.git] / dict / dopen.c
1 /* $Id: dopen.c,v 1.9.2.2 2006-12-05 21:14:40 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <sys/types.h>
24 #include <fcntl.h>
25 #ifndef WIN32
26 #include <unistd.h>
27 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <yaz/xmalloc.h>
32 #include <dict.h>
33
34 static void common_init (Dict_BFile bf, int block_size, int cache)
35 {
36     int i;
37
38     bf->block_size = block_size;
39     bf->compact_flag = 0;
40     bf->cache = cache;
41     bf->hash_size = 31;
42
43     bf->hits = bf->misses = 0;
44
45     /* Allocate all blocks in one chunk. */
46     bf->all_data = xmalloc (block_size * cache);
47
48     /* Allocate and initialize hash array (as empty) */
49     bf->hash_array = (struct Dict_file_block **)
50         xmalloc(sizeof(*bf->hash_array) * bf->hash_size);
51     for (i=bf->hash_size; --i >= 0; )
52         bf->hash_array[i] = NULL;
53
54     /* Allocate all block descriptors in one chunk */
55     bf->all_blocks = (struct Dict_file_block *)
56         xmalloc (sizeof(*bf->all_blocks) * cache);
57
58     /* Initialize the free list */
59     bf->free_list = bf->all_blocks;
60     for (i=0; i<cache-1; i++)
61         bf->all_blocks[i].h_next = bf->all_blocks+(i+1);
62     bf->all_blocks[i].h_next = NULL;
63
64     /* Initialize the data for each block. Will never be moved again */
65     for (i=0; i<cache; i++)
66         bf->all_blocks[i].data = (char*) bf->all_data + i*block_size;
67
68     /* Initialize lru queue */
69     bf->lru_back = NULL;
70     bf->lru_front = NULL;
71 }
72
73
74 Dict_BFile dict_bf_open (BFiles bfs, const char *name, int block_size,
75                          int cache, int rw)
76 {
77     Dict_BFile dbf;
78
79     dbf = (Dict_BFile) xmalloc (sizeof(*dbf));
80     dbf->bf = bf_open (bfs, name, block_size, rw);
81     if (!dbf->bf)
82         return NULL;
83     common_init (dbf, block_size, cache);
84     return dbf;
85 }
86
87 void dict_bf_compact (Dict_BFile dbf)
88 {
89     dbf->compact_flag = 1;
90 }