ed1e61f87e3b1c4b0e9dfbbfb9fcd2f981574324
[idzebra-moved-to-github.git] / include / dict.h
1 /*
2  * Copyright (C) 1994-2000, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dict.h,v $
7  * Revision 1.31  2000-12-05 09:59:10  adam
8  * Work on dict_delete_subtree.
9  *
10  * Revision 1.30  1999/11/30 13:48:03  adam
11  * Improved installation. Updated for inclusion of YAZ header files.
12  *
13  * Revision 1.29  1999/05/15 14:36:37  adam
14  * Updated dictionary. Implemented "compression" of dictionary.
15  *
16  * Revision 1.28  1999/03/09 13:07:06  adam
17  * Work on dict_compact routine.
18  *
19  * Revision 1.27  1999/02/02 14:50:32  adam
20  * Updated WIN32 code specific sections. Changed header.
21  *
22  * Revision 1.26  1997/09/18 08:59:18  adam
23  * Extra generic handle for the character mapping routines.
24  *
25  * Revision 1.25  1997/09/17 12:19:09  adam
26  * Zebra version corresponds to YAZ version 1.4.
27  * Changed Zebra server so that it doesn't depend on global common_resource.
28  *
29  * Revision 1.24  1997/09/05 15:30:00  adam
30  * Changed prototype for chr_map_input - added const.
31  * Added support for C++, headers uses extern "C" for public definitions.
32  *
33  * Revision 1.23  1996/10/29 13:45:33  adam
34  * Changed definition of DICT_DEFAULT_PAGESIZE.
35  *
36  * Revision 1.22  1996/06/04 10:20:10  adam
37  * Added support for character mapping.
38  *
39  * Revision 1.21  1996/05/24  14:46:07  adam
40  * Added dict_grep_cmap function to define user-mapping in grep lookups.
41  *
42  * Revision 1.20  1996/03/20  09:35:23  adam
43  * Function dict_lookup_grep got extra parameter, init_pos, which marks
44  * from which position in pattern approximate pattern matching should occur.
45  *
46  * Revision 1.19  1996/02/02  13:43:54  adam
47  * The public functions simply use char instead of Dict_char to represent
48  * search strings. Dict_char is used internally only.
49  *
50  * Revision 1.18  1996/02/01  20:41:06  adam
51  * Bug fix: insert didn't work on 8-bit characters due to unsigned char
52  * compares in dict_strcmp (strcmp) and signed Dict_char. Dict_char is
53  * unsigned now.
54  *
55  * Revision 1.17  1995/12/07  11:47:04  adam
56  * Default pagesize is 4k instead of 8k.
57  *
58  * Revision 1.16  1995/12/06  14:41:13  adam
59  * New function: dict_delete.
60  *
61  * Revision 1.15  1995/10/27  13:59:17  adam
62  * Function dict_look_grep got extra parameter max_pos that upon return
63  * hold length of longest prefix that matches pattern.
64  *
65  * Revision 1.14  1995/10/09  16:18:35  adam
66  * Function dict_lookup_grep got extra client data parameter.
67  *
68  * Revision 1.13  1995/10/06  09:03:51  adam
69  * First version of scan.
70  *
71  * Revision 1.12  1995/09/14  11:53:02  adam
72  * Grep handle function parameter info is const now.
73  *
74  * Revision 1.11  1995/09/04  09:09:51  adam
75  * String arg in dict lookup is const.
76  * Minor changes.
77  *
78  * Revision 1.10  1994/10/05  12:16:58  adam
79  * Pagesize is a resource now.
80  *
81  * Revision 1.9  1994/10/04  12:08:19  adam
82  * Minor changes.
83  *
84  * Revision 1.8  1994/10/03  17:23:11  adam
85  * First version of dictionary lookup with regular expressions and errors.
86  *
87  * Revision 1.7  1994/09/22  10:44:47  adam
88  * Don't remember what changed!!
89  *
90  * Revision 1.6  1994/09/16  15:39:21  adam
91  * Initial code of lookup - not tested yet.
92  *
93  * Revision 1.5  1994/09/06  13:05:29  adam
94  * Further development of insertion. Some special cases are
95  * not properly handled yet! assert(0) are put here. The
96  * binary search in each page definitely reduce usr CPU.
97  *
98  * Revision 1.4  1994/09/01  17:44:40  adam
99  * Work on insertion in dictionary. Not finished yet.
100  *
101  * Revision 1.3  1994/08/18  12:41:12  adam
102  * Some development of dictionary. Not finished at all!
103  *
104  * Revision 1.2  1994/08/17  13:32:33  adam
105  * Use cache in dict - not in bfile.
106  *
107  * Revision 1.1  1994/08/16  16:26:53  adam
108  * Added dict.
109  *
110  */
111
112 #ifndef DICT_H
113 #define DICT_H
114
115 #include <bfile.h>
116 #include <yaz/log.h>
117
118 #ifdef __cplusplus
119 extern "C" {
120 #endif
121
122 typedef unsigned Dict_ptr;
123 typedef unsigned char Dict_char;
124
125 struct Dict_head {
126     char magic_str[8];
127     int page_size;
128     int compact_flag;
129     Dict_ptr root, last, freelist;
130 };
131
132 struct Dict_file_block
133 {
134     struct Dict_file_block *h_next, **h_prev;
135     struct Dict_file_block *lru_next, *lru_prev;
136     void *data;
137     int dirty;
138     int no;
139     int nbytes;
140 };
141
142 typedef struct Dict_file_struct
143 {
144     int cache;
145     BFile bf;
146     
147     struct Dict_file_block *all_blocks;
148     struct Dict_file_block *free_list;
149     struct Dict_file_block **hash_array;
150     
151     struct Dict_file_block *lru_back, *lru_front;
152     int hash_size;
153     void *all_data;
154     
155     int  block_size;
156     int  hits;
157     int  misses;
158     int  compact_flag;
159 } *Dict_BFile;
160
161 typedef struct Dict_struct {
162     int rw;
163     Dict_BFile dbf;
164     const char **(*grep_cmap)(void *vp, const char **from, int len);
165     void *grep_cmap_data;
166     struct Dict_head head;
167 } *Dict;
168
169 #define DICT_MAGIC "dict01"
170
171 #define DICT_DEFAULT_PAGESIZE 4096
172
173 int        dict_bf_readp (Dict_BFile bf, int no, void **bufp);
174 int        dict_bf_newp (Dict_BFile bf, int no, void **bufp, int nbytes);
175 int        dict_bf_touch (Dict_BFile bf, int no);
176 void       dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush);
177 Dict_BFile dict_bf_open (BFiles bfs, const char *name, int block_size,
178                          int cache, int rw);
179 int        dict_bf_close (Dict_BFile dbf);
180 void       dict_bf_compact (Dict_BFile dbf);
181      
182 Dict       dict_open (BFiles bfs, const char *name, int cache, int rw,
183                       int compact_flag);
184 int        dict_close (Dict dict);
185 int        dict_insert (Dict dict, const char *p, int userlen, void *userinfo);
186 int        dict_delete (Dict dict, const char *p);
187 int        dict_delete_subtree (Dict dict, const char *p, void *client,
188                                 int (*f)(const char *info, void *client));
189 char      *dict_lookup (Dict dict, const char *p);
190 int        dict_lookup_ec (Dict dict, char *p, int range,
191                            int (*f)(char *name));
192 int        dict_lookup_grep (Dict dict, const char *p, int range, void *client,
193                              int *max_pos, int init_pos,
194                              int (*f)(char *name, const char *info,
195                                       void *client));
196 int        dict_strcmp (const Dict_char *s1, const Dict_char *s2);
197 int        dict_strncmp (const Dict_char *s1, const Dict_char *s2, size_t n);
198 int        dict_strlen (const Dict_char *s);
199 int        dict_scan (Dict dict, char *str, 
200                       int *before, int *after, void *client,
201                       int (*f)(char *name, const char *info, int pos,
202                                void *client));
203
204 void       dict_grep_cmap (Dict dict, void *vp,
205                            const char **(*cmap)(void *vp,
206                                                 const char **from, int len));
207 int        dict_copy_compact (BFiles bfs, const char *from, const char *to);
208
209
210 #define DICT_EOS        0
211 #define DICT_type(x)    0[(Dict_ptr*) x]
212 #define DICT_backptr(x) 1[(Dict_ptr*) x]
213 #define DICT_bsize(x)   2[(short*)((char*)(x)+2*sizeof(Dict_ptr))]
214 #define DICT_nodir(x)   0[(short*)((char*)(x)+2*sizeof(Dict_ptr))]
215 #define DICT_size(x)    1[(short*)((char*)(x)+2*sizeof(Dict_ptr))]
216 #define DICT_infoffset  (2*sizeof(Dict_ptr)+3*sizeof(short))
217 #define DICT_xxxxpagesize(x) ((x)->head.page_size)
218
219 #define DICT_to_str(x)  sizeof(Dict_info)+sizeof(Dict_ptr)
220
221 /*
222    type            type of page
223    backptr         pointer to parent
224    nextptr         pointer to next page (if any)
225    nodir           no of words
226    size            size of strings,info,ptr entries
227
228    dir[0..nodir-1]
229    ptr,info,string
230  */
231 #ifdef __cplusplus
232 }
233 #endif
234
235    
236 #endif