Fixed bug #657: Problem with drop database and recordId: file.
[idzebra-moved-to-github.git] / dict / open.c
1 /* $Id: open.c,v 1.27 2006-09-11 22:57:54 adam Exp $
2    Copyright (C) 1995-2006
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
24
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
72     if(!dict->dbf)
73     {
74         yaz_log (YLOG_WARN, "Cannot open `%s'", name);
75         xfree (dict);
76         return NULL;
77     }
78     if (dict_bf_readp (dict->dbf, 0, &head_buf) <= 0)
79     {
80         dict->head.page_size = page_size;
81         dict->head.compact_flag = compact_flag;
82         dict_clean(dict);
83     }
84     else /* header was there, check magic and page size */
85     {
86         memcpy (&dict->head, head_buf, sizeof(dict->head));
87         if (strcmp (dict->head.magic_str, DICT_MAGIC))
88         {
89             yaz_log (YLOG_WARN, "Bad magic of `%s'", name);
90             dict_bf_close(dict->dbf);
91             xfree(dict);
92             return 0;
93         }
94         if (dict->head.page_size != page_size)
95         {
96             yaz_log (YLOG_WARN, "Page size for existing dict %s is %d. Current is %d",
97                   name, dict->head.page_size, page_size);
98         }
99     }
100     if (dict->head.compact_flag)
101         dict_bf_compact(dict->dbf);
102     return dict;
103 }
104
105 int dict_strcmp (const Dict_char *s1, const Dict_char *s2)
106 {
107     return strcmp ((const char *) s1, (const char *) s2);
108 }
109
110 int dict_strncmp (const Dict_char *s1, const Dict_char *s2, size_t n)
111 {
112     return strncmp ((const char *) s1, (const char *) s2, n);
113 }
114
115 int dict_strlen (const Dict_char *s)
116 {
117     return strlen((const char *) s);
118 }
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * indent-tabs-mode: nil
123  * End:
124  * vim: shiftwidth=4 tabstop=8 expandtab
125  */
126