0a3e63c1f01d7e84807ce00376ee23f15e66907c
[idzebra-moved-to-github.git] / dict / dopen.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dopen.c,v $
7  * Revision 1.7  1999-05-15 14:36:37  adam
8  * Updated dictionary. Implemented "compression" of dictionary.
9  *
10  * Revision 1.6  1999/02/02 14:50:20  adam
11  * Updated WIN32 code specific sections. Changed header.
12  *
13  * Revision 1.5  1997/09/17 12:19:07  adam
14  * Zebra version corresponds to YAZ version 1.4.
15  * Changed Zebra server so that it doesn't depend on global common_resource.
16  *
17  * Revision 1.4  1997/09/09 13:38:01  adam
18  * Partial port to WIN95/NT.
19  *
20  * Revision 1.3  1994/09/01 17:49:37  adam
21  * Removed stupid line. Work on insertion in dictionary. Not finished yet.
22  *
23  */
24
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #ifndef WIN32
28 #include <unistd.h>
29 #endif
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include <dict.h>
34
35 static void common_init (Dict_BFile bf, int block_size, int cache)
36 {
37     int i;
38
39     bf->block_size = block_size;
40     bf->compact_flag = 0;
41     bf->cache = cache;
42     bf->hash_size = 31;
43
44     bf->hits = bf->misses = 0;
45
46     /* Allocate all blocks in one chunk. */
47     bf->all_data = xmalloc (block_size * cache);
48
49     /* Allocate and initialize hash array (as empty) */
50     bf->hash_array = 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 = xmalloc (sizeof(*bf->all_blocks) * cache);
56
57     /* Initialize the free list */
58     bf->free_list = bf->all_blocks;
59     for (i=0; i<cache-1; i++)
60         bf->all_blocks[i].h_next = bf->all_blocks+(i+1);
61     bf->all_blocks[i].h_next = NULL;
62
63     /* Initialize the data for each block. Will never be moved again */
64     for (i=0; i<cache; i++)
65         bf->all_blocks[i].data = (char*) bf->all_data + i*block_size;
66
67     /* Initialize lru queue */
68     bf->lru_back = NULL;
69     bf->lru_front = NULL;
70 }
71
72
73 Dict_BFile dict_bf_open (BFiles bfs, const char *name, int block_size,
74                          int cache, int rw)
75 {
76     Dict_BFile dbf;
77
78     dbf = xmalloc (sizeof(*dbf));
79     dbf->bf = bf_open (bfs, name, block_size, rw);
80     if (!dbf->bf)
81         return NULL;
82     common_init (dbf, block_size, cache);
83     return dbf;
84 }
85
86 void dict_bf_compact (Dict_BFile dbf)
87 {
88     dbf->compact_flag = 1;
89 }