/[LeafOK_CVS]/lbbs/src/trie_dict.c
ViewVC logotype

Annotation of /lbbs/src/trie_dict.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (hide annotations)
Sat Jan 3 10:27:14 2026 UTC (2 months, 1 week ago) by sysadm
Branch: MAIN
CVS Tags: HEAD
Changes since 1.26: +1 -1 lines
Content type: text/x-csrc
Update copyright info

1 sysadm 1.13 /* SPDX-License-Identifier: GPL-3.0-or-later */
2     /*
3     * trie_dict
4     * - trie-tree based dict feature
5     *
6 sysadm 1.27 * Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
7 sysadm 1.13 */
8 sysadm 1.1
9 sysadm 1.16 #ifdef HAVE_CONFIG_H
10     #include "config.h"
11     #endif
12    
13 sysadm 1.20 #include "common.h"
14 sysadm 1.11 #include "log.h"
15 sysadm 1.1 #include "trie_dict.h"
16 sysadm 1.7 #include <errno.h>
17 sysadm 1.20 #include <fcntl.h>
18 sysadm 1.10 #include <stdio.h>
19 sysadm 1.1 #include <stdlib.h>
20 sysadm 1.10 #include <string.h>
21 sysadm 1.7 #include <time.h>
22     #include <unistd.h>
23 sysadm 1.20 #include <sys/mman.h>
24     #include <sys/stat.h>
25 sysadm 1.7
26     struct trie_node_pool_t
27     {
28 sysadm 1.20 size_t shm_size;
29 sysadm 1.8 int node_count_limit;
30     int node_count;
31 sysadm 1.7 TRIE_NODE *p_node_free_list;
32     };
33     typedef struct trie_node_pool_t TRIE_NODE_POOL;
34    
35 sysadm 1.23 static char trie_node_shm_name[FILE_NAME_LEN];
36 sysadm 1.7 static TRIE_NODE_POOL *p_trie_node_pool;
37    
38 sysadm 1.8 int trie_dict_init(const char *filename, int node_count_limit)
39 sysadm 1.7 {
40 sysadm 1.20 char filepath[FILE_PATH_LEN];
41     int fd;
42 sysadm 1.7 size_t size;
43     void *p_shm;
44 sysadm 1.8 TRIE_NODE *p_trie_nodes;
45 sysadm 1.7 int i;
46    
47 sysadm 1.20 if (filename == NULL)
48     {
49 sysadm 1.25 log_error("NULL pointer error");
50 sysadm 1.20 return -1;
51     }
52    
53 sysadm 1.7 if (p_trie_node_pool != NULL)
54     {
55 sysadm 1.25 log_error("trie_dict_pool already initialized");
56 sysadm 1.7 return -1;
57     }
58    
59 sysadm 1.8 if (node_count_limit <= 0 || node_count_limit > TRIE_NODE_PER_POOL)
60     {
61 sysadm 1.25 log_error("trie_dict_init(%d) error: invalid node_count_limit", node_count_limit);
62 sysadm 1.8 return -1;
63     }
64    
65 sysadm 1.7 // Allocate shared memory
66 sysadm 1.20 size = sizeof(TRIE_NODE_POOL) + sizeof(TRIE_NODE) * (size_t)node_count_limit;
67    
68     strncpy(filepath, filename, sizeof(filepath) - 1);
69     filepath[sizeof(filepath) - 1] = '\0';
70     snprintf(trie_node_shm_name, sizeof(trie_node_shm_name), "/TRIE_DICT_SHM_%s", basename(filepath));
71    
72     if (shm_unlink(trie_node_shm_name) == -1 && errno != ENOENT)
73 sysadm 1.7 {
74 sysadm 1.25 log_error("shm_unlink(%s) error (%d)", trie_node_shm_name, errno);
75 sysadm 1.7 return -2;
76     }
77    
78 sysadm 1.20 if ((fd = shm_open(trie_node_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
79     {
80 sysadm 1.25 log_error("shm_open(%s) error (%d)", trie_node_shm_name, errno);
81 sysadm 1.20 return -2;
82     }
83     if (ftruncate(fd, (off_t)size) == -1)
84     {
85 sysadm 1.25 log_error("ftruncate(size=%d) error (%d)", size, errno);
86 sysadm 1.20 close(fd);
87     return -2;
88     }
89    
90     p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
91     if (p_shm == MAP_FAILED)
92 sysadm 1.7 {
93 sysadm 1.25 log_error("mmap() error (%d)", errno);
94 sysadm 1.20 close(fd);
95     return -2;
96 sysadm 1.7 }
97 sysadm 1.20
98     if (close(fd) < 0)
99 sysadm 1.7 {
100 sysadm 1.25 log_error("close(fd) error (%d)", errno);
101 sysadm 1.20 return -1;
102 sysadm 1.7 }
103    
104     p_trie_node_pool = p_shm;
105 sysadm 1.20 p_trie_node_pool->shm_size = size;
106 sysadm 1.8 p_trie_node_pool->node_count_limit = node_count_limit;
107 sysadm 1.7 p_trie_node_pool->node_count = 0;
108    
109 sysadm 1.12 p_trie_nodes = (TRIE_NODE *)((char *)p_shm + sizeof(TRIE_NODE_POOL));
110 sysadm 1.8 p_trie_node_pool->p_node_free_list = &(p_trie_nodes[0]);
111     for (i = 0; i < node_count_limit - 1; i++)
112 sysadm 1.7 {
113 sysadm 1.8 p_trie_nodes[i].p_nodes[0] = &(p_trie_nodes[i + 1]);
114 sysadm 1.7 }
115 sysadm 1.8 p_trie_nodes[node_count_limit - 1].p_nodes[0] = NULL;
116 sysadm 1.7
117     return 0;
118     }
119    
120 sysadm 1.23 void trie_dict_cleanup(void)
121 sysadm 1.7 {
122     if (p_trie_node_pool == NULL)
123     {
124 sysadm 1.23 return;
125 sysadm 1.7 }
126    
127     detach_trie_dict_shm();
128    
129 sysadm 1.20 if (shm_unlink(trie_node_shm_name) == -1 && errno != ENOENT)
130 sysadm 1.7 {
131 sysadm 1.25 log_error("shm_unlink(%s) error (%d)", trie_node_shm_name, errno);
132 sysadm 1.7 }
133 sysadm 1.20
134     trie_node_shm_name[0] = '\0';
135 sysadm 1.7 }
136    
137 sysadm 1.20 int set_trie_dict_shm_readonly(void)
138     {
139 sysadm 1.21 if (p_trie_node_pool != NULL && mprotect(p_trie_node_pool, p_trie_node_pool->shm_size, PROT_READ) < 0)
140 sysadm 1.20 {
141 sysadm 1.25 log_error("mprotect() error (%d)", errno);
142 sysadm 1.21 return -1;
143 sysadm 1.20 }
144 sysadm 1.7
145     return 0;
146     }
147    
148     int detach_trie_dict_shm(void)
149     {
150 sysadm 1.20 if (p_trie_node_pool != NULL && munmap(p_trie_node_pool, p_trie_node_pool->shm_size) < 0)
151 sysadm 1.7 {
152 sysadm 1.25 log_error("munmap() error (%d)", errno);
153 sysadm 1.7 return -1;
154     }
155 sysadm 1.8
156 sysadm 1.7 p_trie_node_pool = NULL;
157    
158     return 0;
159     }
160 sysadm 1.1
161     TRIE_NODE *trie_dict_create(void)
162     {
163 sysadm 1.7 TRIE_NODE *p_dict = NULL;
164    
165     if (p_trie_node_pool != NULL && p_trie_node_pool->p_node_free_list != NULL)
166     {
167     p_dict = p_trie_node_pool->p_node_free_list;
168     p_trie_node_pool->p_node_free_list = p_dict->p_nodes[0];
169 sysadm 1.1
170 sysadm 1.10 memset(p_dict, 0, sizeof(*p_dict));
171 sysadm 1.8
172     p_trie_node_pool->node_count++;
173     }
174     else if (p_trie_node_pool != NULL)
175     {
176 sysadm 1.25 log_error("trie_dict_create() error: node depleted %d >= %d", p_trie_node_pool->node_count, p_trie_node_pool->node_count_limit);
177 sysadm 1.7 }
178 sysadm 1.1
179     return p_dict;
180     }
181    
182     void trie_dict_destroy(TRIE_NODE *p_dict)
183     {
184 sysadm 1.7 if (p_trie_node_pool == NULL || p_dict == NULL)
185 sysadm 1.1 {
186     return;
187     }
188    
189     for (int i = 0; i < TRIE_CHILDREN; i++)
190     {
191     if (p_dict->p_nodes[i] != NULL)
192     {
193     trie_dict_destroy(p_dict->p_nodes[i]);
194     }
195 sysadm 1.7 }
196 sysadm 1.3
197 sysadm 1.10 memset(p_dict, 0, sizeof(*p_dict));
198 sysadm 1.1
199 sysadm 1.7 p_dict->p_nodes[0] = p_trie_node_pool->p_node_free_list;
200     p_trie_node_pool->p_node_free_list = p_dict;
201 sysadm 1.8
202     p_trie_node_pool->node_count--;
203 sysadm 1.1 }
204    
205     int trie_dict_set(TRIE_NODE *p_dict, const char *key, int64_t value)
206     {
207     int offset;
208    
209 sysadm 1.5 if (p_dict == NULL)
210     {
211     return -1;
212     }
213    
214 sysadm 1.1 while (key != NULL && *key != '\0')
215     {
216 sysadm 1.26 offset = (unsigned char)(*key);
217 sysadm 1.3 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
218     {
219     return -1;
220     }
221 sysadm 1.1
222     if (*(key + 1) == '\0')
223     {
224     if (p_dict->flags[offset] == 0 || p_dict->values[offset] != value)
225     {
226     p_dict->values[offset] = value;
227     p_dict->flags[offset] = 1;
228     return 1; // Set to new value
229     }
230     return 0; // Unchanged
231     }
232     else
233     {
234     if (p_dict->p_nodes[offset] == NULL)
235     {
236     if ((p_dict->p_nodes[offset] = trie_dict_create()) == NULL)
237     {
238     return -2; // OOM
239     }
240     }
241     p_dict = p_dict->p_nodes[offset];
242     key++;
243     }
244     }
245    
246     return -1; // NULL key
247     }
248    
249     int trie_dict_get(TRIE_NODE *p_dict, const char *key, int64_t *p_value)
250     {
251     int offset;
252    
253 sysadm 1.5 if (p_dict == NULL)
254     {
255     return -1;
256     }
257    
258 sysadm 1.1 while (key != NULL && *key != '\0')
259     {
260 sysadm 1.26 offset = (unsigned char)(*key);
261 sysadm 1.3 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
262     {
263     return -1;
264     }
265 sysadm 1.1
266     if (*(key + 1) == '\0')
267     {
268     if (p_dict->flags[offset] != 0)
269     {
270     *p_value = p_dict->values[offset];
271     return 1; // Found
272     }
273     else
274     {
275     return 0; // Not exist
276     }
277     }
278     else if (p_dict->p_nodes[offset] == NULL)
279     {
280     return 0; // Not exist
281     }
282     else
283     {
284     p_dict = p_dict->p_nodes[offset];
285     key++;
286     }
287     }
288    
289     return -1; // NULL key
290     }
291    
292     int trie_dict_del(TRIE_NODE *p_dict, const char *key)
293     {
294     int offset;
295    
296 sysadm 1.5 if (p_dict == NULL)
297     {
298     return -1;
299     }
300    
301 sysadm 1.1 while (key != NULL && *key != '\0')
302     {
303 sysadm 1.26 offset = (unsigned char)(*key);
304 sysadm 1.3 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
305     {
306     return -1;
307     }
308 sysadm 1.1
309     if (*(key + 1) == '\0')
310     {
311     if (p_dict->flags[offset] != 0)
312     {
313     p_dict->flags[offset] = 0;
314     p_dict->values[offset] = 0;
315     return 1; // Done
316     }
317     else
318     {
319     return 0; // Not exist
320     }
321     }
322     else if (p_dict->p_nodes[offset] == NULL)
323     {
324     return 0; // Not exist
325     }
326     else
327     {
328     p_dict = p_dict->p_nodes[offset];
329     key++;
330     }
331     }
332    
333     return -1; // NULL key
334     }
335 sysadm 1.2
336     static void _trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb, char *key, int depth)
337     {
338     if (p_dict == NULL || depth >= TRIE_MAX_KEY_LEN)
339     {
340     return;
341     }
342    
343     for (int i = 0; i < TRIE_CHILDREN; i++)
344     {
345     if (p_dict->flags[i] != 0)
346     {
347 sysadm 1.5 key[depth] = (char)i;
348 sysadm 1.3 key[depth + 1] = '\0';
349 sysadm 1.2 (*cb)(key, p_dict->values[i]);
350     }
351    
352     if (p_dict->p_nodes[i] != NULL && depth + 1 < TRIE_MAX_KEY_LEN)
353     {
354 sysadm 1.5 key[depth] = (char)i;
355 sysadm 1.2 _trie_dict_traverse(p_dict->p_nodes[i], cb, key, depth + 1);
356     }
357 sysadm 1.5 }
358 sysadm 1.2 }
359    
360     void trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb)
361     {
362     char key[TRIE_MAX_KEY_LEN + 1];
363 sysadm 1.5
364     if (p_dict == NULL)
365     {
366     return;
367     }
368 sysadm 1.2
369     _trie_dict_traverse(p_dict, cb, key, 0);
370     }
371 sysadm 1.8
372     int trie_dict_used_nodes(void)
373     {
374     return (p_trie_node_pool == NULL ? -1 : p_trie_node_pool->node_count);
375     }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1