Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/idzebra
[idzebra-moved-to-github.git] / dict / open.c
1 /* This file is part of the Zebra server.
2    Copyright (C) Index Data
3
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
7    version.
8
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
12    for more details.
13
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
17
18 */
19
20
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "dict-p.h"
30
31 void dict_clean(Dict dict)
32 {
33     int page_size = dict->head.page_size;
34     void *head_buf;
35     int compact_flag = dict->head.compact_flag;
36
37     memset(dict->head.magic_str, 0, sizeof(dict->head.magic_str));
38     strcpy(dict->head.magic_str, DICT_MAGIC);
39     dict->head.last = 1;
40     dict->head.root = 0;
41     dict->head.freelist = 0;
42     dict->head.page_size = page_size;
43     dict->head.compact_flag = compact_flag;
44
45     /* create header with information (page 0) */
46     if (dict->rw)
47         dict_bf_newp(dict->dbf, 0, &head_buf, page_size);
48 }
49
50 Dict dict_open(BFiles bfs, const char *name, int cache, int rw,
51                int compact_flag, int page_size)
52 {
53     Dict dict;
54     void *head_buf;
55
56     dict = (Dict) xmalloc(sizeof(*dict));
57
58     if (cache < 5)
59         cache = 5;
60
61     dict->grep_cmap = NULL;
62     page_size = DICT_DEFAULT_PAGESIZE;
63     if (page_size < 2048)
64     {
65         yaz_log(YLOG_WARN, "Page size for dict %s %d<2048. Set to 2048",
66                 name, page_size);
67         page_size = 2048;
68     }
69     dict->dbf = dict_bf_open(bfs, name, page_size, cache, rw);
70     dict->rw = rw;
71     dict->no_split = 0;
72     dict->no_insert = 0;
73     dict->no_lookup = 0;
74
75     if(!dict->dbf)
76     {
77         yaz_log(YLOG_WARN, "Cannot open `%s'", name);
78         xfree(dict);
79         return NULL;
80     }
81     if (dict_bf_readp(dict->dbf, 0, &head_buf) <= 0)
82     {
83         dict->head.page_size = page_size;
84         dict->head.compact_flag = compact_flag;
85         dict_clean(dict);
86     }
87     else /* header was there, check magic and page size */
88     {
89         memcpy(&dict->head, head_buf, sizeof(dict->head));
90         if (strcmp(dict->head.magic_str, DICT_MAGIC))
91         {
92             yaz_log(YLOG_WARN, "Bad magic of `%s'", name);
93             dict_bf_close(dict->dbf);
94             xfree(dict);
95             return 0;
96         }
97         if (dict->head.page_size != page_size)
98         {
99             yaz_log(YLOG_WARN, "Page size for existing dict %s is %d. Current is %d",
100                     name, dict->head.page_size, page_size);
101         }
102     }
103     if (dict->head.compact_flag)
104         dict_bf_compact(dict->dbf);
105     return dict;
106 }
107
108 int dict_strcmp(const Dict_char *s1, const Dict_char *s2)
109 {
110     return strcmp((const char *) s1, (const char *) s2);
111 }
112
113 int dict_strncmp(const Dict_char *s1, const Dict_char *s2, size_t n)
114 {
115     return strncmp((const char *) s1, (const char *) s2, n);
116 }
117
118 int dict_strlen(const Dict_char *s)
119 {
120     return strlen((const char *) s);
121 }
122
123 zint dict_get_no_lookup(Dict dict)
124 {
125     return dict->no_lookup;
126 }
127
128 zint dict_get_no_insert(Dict dict)
129 {
130     return dict->no_insert;
131 }
132
133 zint dict_get_no_split(Dict dict)
134 {
135     return dict->no_split;
136 }
137
138 /*
139  * Local variables:
140  * c-basic-offset: 4
141  * c-file-style: "Stroustrup"
142  * indent-tabs-mode: nil
143  * End:
144  * vim: shiftwidth=4 tabstop=8 expandtab
145  */
146