IDZEBRA  2.2.7
open.c
Go to the documentation of this file.
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 
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;
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);
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)
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 
124 {
125  return dict->no_lookup;
126 }
127 
129 {
130  return dict->no_insert;
131 }
132 
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 
int dict_bf_close(Dict_BFile dbf)
Definition: dclose.c:32
Dict_BFile dict_bf_open(BFiles bfs, const char *name, int block_size, int cache, int rw)
Definition: dopen.c:75
void dict_bf_compact(Dict_BFile dbf)
Definition: dopen.c:91
int dict_bf_readp(Dict_BFile bf, int no, void **bufp)
Definition: drdwr.c:188
#define DICT_DEFAULT_PAGESIZE
Definition: dict-p.h:31
unsigned char Dict_char
Definition: dict-p.h:33
int dict_bf_newp(Dict_BFile bf, int no, void **bufp, int nbytes)
Definition: drdwr.c:226
#define DICT_MAGIC
Definition: dict-p.h:29
struct Dict_struct * Dict
Dictionary handle.
Definition: dict.h:41
static Dict dict
Definition: dicttest.c:35
int dict_strncmp(const Dict_char *s1, const Dict_char *s2, size_t n)
Definition: open.c:113
zint dict_get_no_split(Dict dict)
get number of page split operations, since dict_open
Definition: open.c:133
Dict dict_open(BFiles bfs, const char *name, int cache, int rw, int compact_flag, int page_size)
open dictionary
Definition: open.c:50
void dict_clean(Dict dict)
reset Dictionary (makes it empty)
Definition: open.c:31
zint dict_get_no_insert(Dict dict)
get number of insert operations, since dict_open
Definition: open.c:128
int dict_strlen(const Dict_char *s)
Definition: open.c:118
int dict_strcmp(const Dict_char *s1, const Dict_char *s2)
Definition: open.c:108
zint dict_get_no_lookup(Dict dict)
get number of lookup operations, since dict_open
Definition: open.c:123
int page_size
Definition: dict-p.h:38
Dict_ptr root
Definition: dict-p.h:40
int compact_flag
Definition: dict-p.h:39
Dict_ptr freelist
Definition: dict-p.h:40
Dict_ptr last
Definition: dict-p.h:40
char magic_str[8]
Definition: dict-p.h:37
zint no_lookup
Definition: dict-p.h:82
zint no_split
Definition: dict-p.h:78
int rw
Definition: dict-p.h:73
struct Dict_head head
Definition: dict-p.h:83
zint no_insert
Definition: dict-p.h:80
const char **(* grep_cmap)(void *vp, const char **from, int len)
Definition: dict-p.h:75
Dict_BFile dbf
Definition: dict-p.h:74
long zint
Zebra integer.
Definition: util.h:66